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
use anyhow::{anyhow, Result};
use signal_hook::consts::TERM_SIGNALS;
use signal_hook::flag;
use signal_hook::iterator::{exfiltrator::SignalOnly, SignalsInfo};
use std::path::PathBuf;
use std::sync::{atomic::AtomicBool, Arc};
use std::thread;
use crate::hal::SYSTEM;
use slos::kmain;
pub mod interrupts;
pub fn init(kargs: String, rootfs: Option<PathBuf>) -> Result<()> {
let _ = kargs;
let _ = rootfs;
Ok(())
}
pub fn hosted_kmain() -> Result<()> {
kmain().or_else(|x| Err(anyhow!("kmain returned an error!? {:#?}", x)))?;
Ok(())
}
pub fn run_kernel() -> Result<()> {
let term_now = Arc::new(AtomicBool::new(false));
for sig in TERM_SIGNALS {
flag::register_conditional_shutdown(*sig, 1, Arc::clone(&term_now))?;
flag::register(*sig, Arc::clone(&term_now))?;
}
debug!("Initializing kernel");
slos::init(SYSTEM.get()).or_else(|x| Err(anyhow!("slos::init error {:#?}", x)))?;
debug!("Starting hosted_kmain in a thread");
let kmain_handle = thread::spawn(hosted_kmain);
let kmain_thread = kmain_handle.thread().clone();
SYSTEM.get().kmain_thread = Some(kmain_thread.clone());
debug!("Starting utility threads");
let handles = vec![
thread::spawn(move || interrupts::dispatcher(kmain_thread.clone())),
thread::spawn(interrupts::clock_tick),
];
let mut signals = SignalsInfo::<SignalOnly>::new(TERM_SIGNALS)?;
for _ in &mut signals {
error!("Requesting kmain return, ^C again to terminate");
let mut system = SYSTEM.get();
system.return_next_iter = true;
kmain_handle.thread().unpark();
break;
}
kmain_handle
.join()
.or_else(|x| Err(anyhow!("hosted_main error {:#?}", x)))??;
for handle in handles {
handle.join().expect("failed to join thread");
}
Ok(())
}