1 use core::sync::atomic::{AtomicUsize, Ordering};
15 global_asm!(include_str!("entry.S"));
18 unsafe extern "C" fn tcs_init(secondary: bool) {
19 // Be very careful when changing this code: it runs before the binary has been
20 // relocated. Any indirect accesses to symbols will likely fail.
21 const UNINIT: usize = 0;
22 const BUSY: usize = 1;
23 const DONE: usize = 2;
24 // Three-state spin-lock
25 static RELOC_STATE: AtomicUsize = AtomicUsize::new(UNINIT);
27 if secondary && RELOC_STATE.load(Ordering::Relaxed) != DONE {
28 panic::panic_msg("Entered secondary TCS before main TCS!")
31 // Try to atomically swap UNINIT with BUSY. The returned state can be:
32 match RELOC_STATE.compare_and_swap(UNINIT, BUSY, Ordering::Acquire) {
33 // This thread just obtained the lock and other threads will observe BUSY
35 reloc::relocate_elf_rela();
36 RELOC_STATE.store(DONE, Ordering::Release);
38 // We need to wait until the initialization is done.
39 BUSY => while RELOC_STATE.load(Ordering::Acquire) == BUSY {
40 core::arch::x86_64::_mm_pause()
42 // Initialization is done.
48 // FIXME: this item should only exist if this is linked into an executable
49 // (main function exists). If this is a library, the crate author should be
50 // able to specify this
52 extern "C" fn entry(p1: u64, p2: u64, p3: u64, secondary: bool, p4: u64, p5: u64) -> (u64, u64) {
53 // FIXME: how to support TLS in library mode?
54 let tls = Box::new(tls::Tls::new());
55 let _tls_guard = unsafe { tls.activate() };
58 super::thread::Thread::entry();
63 fn main(argc: isize, argv: *const *const u8) -> isize;
66 // check entry is being called according to ABI
72 // The actual types of these arguments are `p1: *const Arg, p2:
73 // usize`. We can't currently customize the argument list of Rust's
74 // main function, so we pass these in as the standard pointer-sized
75 // values in `argc` and `argv`.
76 let ret = main(p2 as _, p1 as _);
82 pub(super) fn exit_with_code(code: isize) -> ! {
84 if let Some(mut out) = panic::SgxPanicOutput::new() {
85 let _ = write!(out, "Exited with status code {}", code);
88 usercalls::exit(code != 0);