1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#![cfg_attr(test, allow(unused))]
use crate::io::Write;
use core::sync::atomic::{AtomicUsize, Ordering};
pub(super) mod panic;
mod reloc;
pub mod mem;
pub mod thread;
pub mod tls;
#[macro_use]
pub mod usercalls;
#[cfg(not(test))]
global_asm!(include_str!("entry.S"), options(att_syntax));
#[repr(C)]
struct EntryReturn(u64, u64);
#[cfg(not(test))]
#[no_mangle]
unsafe extern "C" fn tcs_init(secondary: bool) {
const UNINIT: usize = 0;
const BUSY: usize = 1;
const DONE: usize = 2;
static RELOC_STATE: AtomicUsize = AtomicUsize::new(UNINIT);
if secondary && RELOC_STATE.load(Ordering::Relaxed) != DONE {
rtabort!("Entered secondary TCS before main TCS!")
}
match RELOC_STATE.compare_exchange(UNINIT, BUSY, Ordering::Acquire, Ordering::Acquire) {
Ok(_) => {
reloc::relocate_elf_rela();
RELOC_STATE.store(DONE, Ordering::Release);
}
Err(BUSY) => {
while RELOC_STATE.load(Ordering::Acquire) == BUSY {
core::hint::spin_loop();
}
}
Err(DONE) => {}
_ => unreachable!(),
}
}
#[cfg(not(test))]
#[no_mangle]
extern "C" fn entry(p1: u64, p2: u64, p3: u64, secondary: bool, p4: u64, p5: u64) -> EntryReturn {
let tls = Box::new(tls::Tls::new());
let tls_guard = unsafe { tls.activate() };
if secondary {
let join_notifier = super::thread::Thread::entry();
drop(tls_guard);
drop(join_notifier);
EntryReturn(0, 0)
} else {
extern "C" {
fn main(argc: isize, argv: *const *const u8) -> isize;
}
rtassert!(p3 == 0);
rtassert!(p4 == 0);
rtassert!(p5 == 0);
unsafe {
let ret = main(p2 as _, p1 as _);
exit_with_code(ret)
}
}
}
pub(super) fn exit_with_code(code: isize) -> ! {
if code != 0 {
if let Some(mut out) = panic::SgxPanicOutput::new() {
let _ = write!(out, "Exited with status code {}", code);
}
}
usercalls::exit(code != 0);
}
#[cfg(not(test))]
#[no_mangle]
extern "C" fn abort_reentry() -> ! {
usercalls::exit(false)
}