1pub type ExitCode = crate::bindings::exit_code;
5#[cfg(not(feature = "kernel"))]
6use crate::error::{GenericError, TwzError};
7
8#[cfg(not(feature = "kernel"))]
11pub fn twz_rt_exit(code: ExitCode) -> ! {
12 unsafe {
13 crate::bindings::twz_rt_exit(code);
14 unreachable!()
15 }
16}
17
18#[cfg(not(feature = "kernel"))]
20pub fn twz_rt_abort() -> ! {
21 unsafe {
22 crate::bindings::twz_rt_abort();
23 unreachable!()
24 }
25}
26
27#[cfg(not(feature = "kernel"))]
31pub fn twz_rt_pre_main_hook() -> Option<ExitCode> {
32 unsafe { crate::bindings::twz_rt_pre_main_hook().into() }
33}
34
35impl From<crate::bindings::option_exit_code> for Option<ExitCode> {
36 #[inline]
37 fn from(value: crate::bindings::option_exit_code) -> Self {
38 if value.is_some == 0 {
39 None
40 } else {
41 Some(value.value)
42 }
43 }
44}
45
46#[cfg(not(feature = "kernel"))]
48pub fn twz_rt_post_main_hook() {
49 unsafe {
50 crate::bindings::twz_rt_post_main_hook();
51 }
52}
53
54#[cfg(not(feature = "kernel"))]
56pub fn twz_rt_cross_compartment_entry() -> Result<(), TwzError> {
57 unsafe {
58 if crate::bindings::twz_rt_cross_compartment_entry() {
59 Ok(())
60 } else {
61 Err(GenericError::AccessDenied.into())
62 }
63 }
64}
65
66pub use crate::bindings::{
67 basic_aux as BasicAux, basic_return as BasicReturn, comp_init_info as CompartmentInitInfo,
68 ctor_set as CtorSet, init_info_ptrs as InitInfoPtrs, minimal_init_info as MinimalInitInfo,
69 runtime_info as RuntimeInfo, RUNTIME_INIT_COMP, RUNTIME_INIT_MIN, RUNTIME_INIT_MONITOR,
70};
71
72unsafe impl Send for CtorSet {}
74
75unsafe impl Sync for CtorSet {}
77
78unsafe impl Send for RuntimeInfo {}
79unsafe impl Sync for RuntimeInfo {}
80unsafe impl Send for CompartmentInitInfo {}
81unsafe impl Sync for CompartmentInitInfo {}
82
83#[cfg(not(feature = "kernel"))]
85pub fn twz_rt_runtime_entry(
86 info: *const RuntimeInfo,
87 std_entry: unsafe extern "C-unwind" fn(BasicAux) -> BasicReturn,
88) -> ! {
89 unsafe {
90 crate::bindings::twz_rt_runtime_entry(info, Some(std_entry));
91 unreachable!()
92 }
93}
94
95#[cfg(all(feature = "rt0", not(feature = "kernel")))]
96pub mod rt0 {
97 #[cfg(target_arch = "aarch64")]
104 #[no_mangle]
105 #[naked]
106 pub unsafe extern "C" fn _start() {
107 core::arch::naked_asm!(
108 "b {entry}",
109 entry = sym entry,
110 );
111 }
112
113 #[cfg(target_arch = "x86_64")]
114 #[no_mangle]
115 #[naked]
116 pub unsafe extern "C" fn _start() {
117 core::arch::naked_asm!(
119 "and rsp, 0xfffffffffffffff0",
120 "call {entry}",
121 "ud2",
122 entry = sym entry,
123 );
124 }
125
126 #[used]
127 static ENTRY: unsafe extern "C" fn() = _start;
129
130 use super::{BasicAux, BasicReturn, RuntimeInfo};
131
132 unsafe extern "C" fn entry(arg: usize) -> ! {
134 rust_entry(arg as *const _)
136 }
137
138 pub unsafe fn rust_entry(arg: *const RuntimeInfo) -> ! {
143 super::twz_rt_runtime_entry(arg, std_entry_from_runtime)
146 }
147
148 extern "C-unwind" {
149 fn std_entry_from_runtime(aux: BasicAux) -> BasicReturn;
150 }
151}