]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/sgx/abi/mod.rs
Improve some compiletest documentation
[rust.git] / src / libstd / sys / sgx / abi / mod.rs
1 use core::sync::atomic::{AtomicUsize, Ordering};
2 use crate::io::Write;
3
4 // runtime features
5 mod reloc;
6 pub(super) mod panic;
7
8 // library features
9 pub mod mem;
10 pub mod thread;
11 pub mod tls;
12 #[macro_use]
13 pub mod usercalls;
14
15 global_asm!(include_str!("entry.S"));
16
17 #[no_mangle]
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);
26
27     if secondary && RELOC_STATE.load(Ordering::Relaxed) != DONE {
28         panic::panic_msg("Entered secondary TCS before main TCS!")
29     }
30
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
34         UNINIT => {
35             reloc::relocate_elf_rela();
36             RELOC_STATE.store(DONE, Ordering::Release);
37         },
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()
41         },
42         // Initialization is done.
43         DONE => {},
44         _ => unreachable!()
45     }
46 }
47
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
51 #[no_mangle]
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() };
56
57     if secondary {
58         super::thread::Thread::entry();
59
60         (0, 0)
61     } else {
62         extern "C" {
63             fn main(argc: isize, argv: *const *const u8) -> isize;
64         }
65
66         // check entry is being called according to ABI
67         assert_eq!(p3, 0);
68         assert_eq!(p4, 0);
69         assert_eq!(p5, 0);
70
71         unsafe {
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 _);
77             exit_with_code(ret)
78         }
79     }
80 }
81
82 pub(super) fn exit_with_code(code: isize) -> ! {
83     if code != 0 {
84         if let Some(mut out) = panic::SgxPanicOutput::new() {
85             let _ = write!(out, "Exited with status code {}", code);
86         }
87     }
88     usercalls::exit(code != 0);
89 }