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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
use alloc::boxed::Box;
use core::fmt::{Debug, Formatter, Result};
use core::sync::atomic::{AtomicUsize, Ordering};
use fs::ROOT_FS;
use interrupts::{esp0, on, off};
use io::NonBlockingBuffer;
use machine::{self, context_switch};
use memory::AddressSpace;
use static_linked_list::StaticLinkedList;
use self::context::KContext;
use self::idle::IDLE_PROCESS;
use self::proc_table::PROCESS_TABLE;
pub mod context;
pub mod focus;
pub mod proc_table;
pub mod ready_queue;
mod idle;
mod init;
mod reaper;
mod user;
const STACK_SIZE: usize = 2048;
static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
pub static mut CURRENT_PROCESS: *mut Process = 0 as *mut Process;
pub type ProcessQueue = StaticLinkedList<*mut Process>;
#[derive(Clone, Copy, PartialEq)]
pub enum CF {
Back,
Next(usize),
}
#[repr(C)]
#[derive(Clone, Copy, PartialEq)]
pub enum State {
INIT,
READY,
RUNNING,
BLOCKED,
TERMINATED,
}
pub struct Process {
name: &'static str,
pid: usize,
run: fn(&Process) -> usize,
state: State,
stack: usize,
kcontext: KContext,
pub addr_space: AddressSpace,
pub disable_cnt: usize,
pub buffer: Option<NonBlockingBuffer>,
pub cwf: usize,
pub path: StaticLinkedList<usize>,
}
impl Process {
pub fn new(name: &'static str, run: fn(&Process) -> usize) -> *mut Process {
let mut p = Process {
name: name,
pid: NEXT_ID.fetch_add(1, Ordering::Relaxed),
run: run,
state: State::INIT,
stack: 0,
kcontext: KContext::new(),
addr_space: AddressSpace::new(),
disable_cnt: 0,
buffer: None,
cwf: 0,
path: StaticLinkedList::new(),
};
p.get_stack();
let process = Box::into_raw(box p);
unsafe {
PROCESS_TABLE.push(process);
}
process
}
fn get_stack(&mut self) {
let stack: Box<[usize; STACK_SIZE]> = box [0; STACK_SIZE];
let stack_ptr = Box::into_raw(stack) as *mut usize;
self.stack = stack_ptr as usize;
unsafe {
*stack_ptr.offset(STACK_SIZE as isize - 2) = start_proc as usize;
}
self.kcontext.esp = unsafe { stack_ptr.offset(STACK_SIZE as isize - 2) } as usize;
}
fn set_state(&mut self, s: State) {
self.state = s;
}
pub fn get_state(&self) -> State {
self.state
}
pub fn get_pid(&self) -> usize {
self.pid
}
pub fn accept_kbd(&mut self, cap: usize) {
if self.buffer.is_none() {
self.buffer = Some(NonBlockingBuffer::new(cap));
}
}
pub fn cwf(&self) -> usize {
self.cwf
}
pub fn cf(&mut self, new_cwf: CF) {
match new_cwf {
CF::Back => {
if self.path.len() > 0 {
self.cwf = self.path.pop_back().unwrap();
}
}
CF::Next(new) => {
match unsafe { (*ROOT_FS).stat(new) } {
Some(inode) => {
let cwf_inode = unsafe { (*ROOT_FS).stat(self.cwf()).unwrap() };
let mut linked = false;
for i in inode.links.iter() {
if *i == self.cwf() {
linked = true;
break;
}
}
if linked {
self.path.push_back(new);
self.cwf = new;
}
}
None => {}
}
}
}
}
}
impl Drop for Process {
fn drop(&mut self) {
unsafe {
Box::from_raw(self.stack as *mut [usize; 2048]);
}
}
}
impl Debug for Process {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "Process #{} @ 0x{:x}: {}",
self.pid, self as *const Process as usize, self.name)
}
}
impl PartialEq for Process {
fn eq(&self, other: &Process) -> bool {
self.pid == other.pid
}
}
pub fn init() {
ready_queue::make_ready(Process::new("init", self::init::run));
idle::init();
reaper::init();
printf!("processes inited\n");
}
#[allow(private_no_mangle_fns)]
#[no_mangle]
fn start_proc() {
unsafe {
let code = if !CURRENT_PROCESS.is_null() {
printf!("{:?} [Starting]\n", *CURRENT_PROCESS);
(*CURRENT_PROCESS).set_state(State::RUNNING);
((*CURRENT_PROCESS).run)(&*CURRENT_PROCESS)
} else {
panic!("No current process to start")
};
exit(code);
}
}
pub fn proc_yield<'a>(q: Option<&'a mut ProcessQueue>) {
unsafe {
off();
machine::proc_yield(q);
on();
}
}
#[no_mangle]
#[inline(never)]
pub unsafe fn _proc_yield<'a>(q: Option<&'a mut ProcessQueue>) {
if !CURRENT_PROCESS.is_null() {
if let Some(queue) = q {
(*CURRENT_PROCESS).set_state(State::BLOCKED);
queue.push_back(CURRENT_PROCESS);
} else {
ready_queue::make_ready(CURRENT_PROCESS);
}
CURRENT_PROCESS = 0 as *mut Process;
}
let mut next = ready_queue::get_next();
if next.is_null() {
next = IDLE_PROCESS;
}
(*next).addr_space.activate();
esp0((*next).stack + STACK_SIZE*4);
CURRENT_PROCESS = next;
context_switch((*CURRENT_PROCESS).kcontext,
if (*CURRENT_PROCESS).disable_cnt == 0 {
1 << 9
} else {
0
});
panic!("The impossible has happened!");
}
pub fn exit(code: usize) {
unsafe {
if CURRENT_PROCESS.is_null() {
panic!("Exiting with no current process!\n");
}
(*CURRENT_PROCESS).set_state(State::TERMINATED);
(*CURRENT_PROCESS).addr_space.clear();
off();
bootlog!("{:?} [Exit 0x{:X}]\n", *CURRENT_PROCESS, code);
self::reaper::reaper_add(CURRENT_PROCESS);
CURRENT_PROCESS = 0 as *mut Process;
_proc_yield(None);
}
panic!("The impossible has happened!");
}