]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/sgx/abi/mod.rs
Add x86_64-fortanix-unknown-sgx target to libstd and dependencies
[rust.git] / src / libstd / sys / sgx / abi / mod.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use core::sync::atomic::{AtomicUsize, Ordering};
12 use io::Write;
13
14 // runtime features
15 mod reloc;
16 mod mem;
17 pub(super) mod panic;
18
19 // library features
20 #[macro_use]
21 mod usercalls;
22
23 global_asm!(concat!(usercalls_asm!(), include_str!("entry.S")));
24
25 #[no_mangle]
26 unsafe extern "C" fn tcs_init(secondary: bool) {
27     // Be very careful when changing this code: it runs before the binary has been
28     // relocated. Any indirect accesses to symbols will likely fail.
29     const UNINIT: usize = 0;
30     const BUSY: usize = 1;
31     const DONE: usize = 2;
32     // Three-state spin-lock
33     static RELOC_STATE: AtomicUsize = AtomicUsize::new(UNINIT);
34
35     if secondary && RELOC_STATE.load(Ordering::Relaxed) != DONE {
36         panic::panic_msg("Entered secondary TCS before main TCS!")
37     }
38
39     // Try to atomically swap UNINIT with BUSY. The returned state can be:
40     match RELOC_STATE.compare_and_swap(UNINIT, BUSY, Ordering::Acquire) {
41         // This thread just obtained the lock and other threads will observe BUSY
42         UNINIT => {
43             reloc::relocate_elf_rela();
44             RELOC_STATE.store(DONE, Ordering::Release);
45         },
46         // We need to wait until the initialization is done.
47         BUSY => while RELOC_STATE.load(Ordering::Acquire) == BUSY  {
48             ::core::arch::x86_64::_mm_pause()
49         },
50         // Initialization is done.
51         DONE => {},
52         _ => unreachable!()
53     }
54 }
55
56 // FIXME: this item should only exist if this is linked into an executable
57 // (main function exists). If this is a library, the crate author should be
58 // able to specify this
59 #[no_mangle]
60 #[allow(unreachable_code)]
61 extern "C" fn entry(p1: u64, p2: u64, p3: u64, secondary: bool, p4: u64, p5: u64) -> (u64, u64) {
62     if secondary {
63         unimplemented!("thread entrypoint");
64
65         (0, 0)
66     } else {
67         extern "C" {
68             fn main(argc: isize, argv: *const *const u8) -> isize;
69         }
70
71         // check entry is being called according to ABI
72         assert_eq!(p3, 0);
73         assert_eq!(p4, 0);
74         assert_eq!(p5, 0);
75
76         unsafe {
77             // The actual types of these arguments are `p1: *const Arg, p2:
78             // usize`. We can't currently customize the argument list of Rust's
79             // main function, so we pass these in as the standard pointer-sized
80             // values in `argc` and `argv`.
81             let ret = main(p2 as _, p1 as _);
82             exit_with_code(ret)
83         }
84     }
85 }
86
87 pub(super) fn exit_with_code(code: isize) -> ! {
88     if code != 0 {
89         if let Some(mut out) = panic::SgxPanicOutput::new() {
90             let _ = write!(out, "Exited with status code {}", code);
91         }
92     }
93     unsafe { usercalls::raw::exit(code != 0) };
94 }