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
#[link_section = ".nipd"]
#[no_mangle]
pub static mut ENCLAVE_STATE: u32 = 0;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
#[repr(u32)]
pub enum State {
NotStarted = 0,
InProgress = 1,
InitDone = 2,
Crashed = 3,
}
impl State {
#[link_section = ".nipx"]
pub fn is_done(&self) -> bool {
*self == Self::InitDone
}
#[link_section = ".nipx"]
pub fn is_crashed(&self) -> bool {
*self == Self::Crashed
}
#[link_section = ".nipx"]
pub fn is_not_started(&self) -> bool {
*self == Self::NotStarted
}
}
#[link_section = ".nipx"]
pub fn get_state() -> State {
unsafe {
match ENCLAVE_STATE {
0 => State::NotStarted,
1 => State::InProgress,
2 => State::InitDone,
3 => State::Crashed,
_ => unreachable!(),
}
}
}
#[link_section = ".nipx"]
pub fn set_state(state: State) {
let state = match state {
State::NotStarted => 0,
State::InProgress => 1,
State::InitDone => 2,
State::Crashed => 3,
};
unsafe {
ENCLAVE_STATE = state;
}
}
#[link_section = ".nipx"]
pub fn lock_state() -> State {
const NOT_STARTED: u32 = 0;
const IN_PROGRESS: u32 = 1;
let state = unsafe {
let (state, _) = core::intrinsics::atomic_cxchg_seqcst_seqcst(
&mut ENCLAVE_STATE,
NOT_STARTED,
IN_PROGRESS,
);
state
};
match state {
0 => State::NotStarted,
1 => State::InProgress,
2 => State::InitDone,
3 => State::Crashed,
_ => unreachable!(),
}
}
#[link_section = ".nipx"]
pub fn is_crashed() -> bool {
get_state().is_crashed()
}