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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use crate::*;
use crate::task::*;
struct SystemControlBlock {
interrupt: bool,
cpu_lock: bool,
dispatch_disable: bool,
dispatch_reserve: bool,
}
static mut SYSCB: SystemControlBlock = SystemControlBlock {
interrupt: false,
cpu_lock: false,
dispatch_disable: false,
dispatch_reserve: false,
};
pub(crate) fn test_interrupt_flag() -> bool {
unsafe { SYSCB.interrupt }
}
pub(crate) fn set_interrupt_flag() {
unsafe {
SYSCB.interrupt = true;
}
}
pub(crate) fn clear_interrupt_flag() {
unsafe {
SYSCB.interrupt = false;
}
}
pub(crate) fn test_cpu_lock_flag() -> bool {
unsafe { SYSCB.cpu_lock }
}
pub(crate) fn set_cpu_lock_flag() {
unsafe {
SYSCB.cpu_lock = true;
}
}
pub(crate) fn clear_cpu_lock_flag() {
unsafe {
SYSCB.cpu_lock = false;
}
}
pub(crate) fn test_dispatch_disable_flag() -> bool {
unsafe { SYSCB.dispatch_disable }
}
pub(crate) fn set_dispatch_disable_flag() {
unsafe {
SYSCB.dispatch_disable = true;
}
}
pub(crate) fn clear_dispatch_disable_flag() {
unsafe {
SYSCB.dispatch_disable = false;
}
}
pub(crate) fn test_dispatch_reserve_flag() -> bool {
unsafe { SYSCB.dispatch_reserve }
}
pub(crate) fn set_dispatch_reserve_flag() {
unsafe {
SYSCB.dispatch_reserve = true;
}
}
pub(crate) fn clear_dispatch_reserve_flag() {
unsafe {
SYSCB.dispatch_reserve = false;
}
}
pub(crate) unsafe fn enter_system_call() {
cpu_lock();
}
pub(crate) unsafe fn leave_system_call() {
if test_dispatch_reserve_flag()
&& !test_interrupt_flag()
&& !test_dispatch_disable_flag()
&& !test_cpu_lock_flag()
{
clear_dispatch_reserve_flag();
task_switch();
}
if !test_cpu_lock_flag() {
cpu_unlock();
}
}
pub(crate) struct SystemCall {}
impl SystemCall {
pub(crate) fn new() -> Self {
unsafe {
enter_system_call();
Self {}
}
}
}
impl Drop for SystemCall {
fn drop(&mut self) {
unsafe {
leave_system_call();
}
}
}
pub fn lock_cpu() {
let _sc = SystemCall::new();
set_cpu_lock_flag();
}
pub fn unlock_cpu() {
let _sc = SystemCall::new();
clear_cpu_lock_flag();
}
pub fn is_cpu_locked() -> bool {
let _sc = SystemCall::new();
test_cpu_lock_flag()
}
pub fn disable_dispatch() {
let _sc = SystemCall::new();
set_dispatch_disable_flag();
}
pub fn enable_dispatch() {
let _sc = SystemCall::new();
clear_dispatch_disable_flag();
}
pub fn is_dispatch_disabled() -> bool {
let _sc = SystemCall::new();
test_dispatch_disable_flag()
}
pub fn is_dispatch_pending_state() -> bool {
let _sc = SystemCall::new();
test_cpu_lock_flag() || test_dispatch_disable_flag()
}
pub fn is_interrupt_state() -> bool {
test_interrupt_flag()
}
pub fn idle_loop() -> ! {
loop {
unsafe {
cpu_halt();
}
}
}