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
use riscv::{asm, register::*};
use crate::arch::{hart_id, wait_forever};
use crate::{clint, plic, mem, uart, process, spinlock, trap, virtio};
use crate::info;
use crate::jump::*;
#[no_mangle]
unsafe extern "C" fn kinit() {
mstatus::set_mpp(mstatus::MPP::Supervisor);
mepc::write(kmain as usize);
asm!("csrw satp, zero");
asm!("li t0, 0xffff");
asm!("csrw medeleg, t0");
asm!("li t0, 0xffff");
asm!("csrw mideleg, t0");
asm!("csrr a1, mhartid");
asm!("mv tp, a1");
clint::timer_init();
asm!("mret");
}
static mut MAY_BOOT: bool = false;
#[no_mangle]
extern "C" fn kmain() -> ! {
if hart_id() == 0 {
unsafe { uart::init(); }
info!("booting core-os on hart {}...", hart_id());
info!(" UART... \x1b[0;32minitialized\x1b[0m");
unsafe { mem::init(); }
info!(" kernel page table... \x1b[0;32minitialized\x1b[0m");
unsafe { virtio::init(); }
info!(" virt-io... \x1b[0;32minitialized\x1b[0m");
unsafe { plic::init(); }
info!(" PLIC... \x1b[0;32minitialized\x1b[0m");
mem::hartinit();
info!("kernel page table configured");
info!(" Trap... \x1b[0;32minitialized\x1b[0m");
info!(" Timer... \x1b[0;32minitialized\x1b[0m");
plic::hartinit();
info!(" PLIC... \x1b[0;32minitialized\x1b[0m");
unsafe { trap::hartinit(); }
info!(" Interrupt... \x1b[0;32minitialized\x1b[0m");
unsafe { process::init(); }
process::init_proc();
unsafe {
asm!("fence");
MAY_BOOT = true
}
} else {
loop {
if unsafe { MAY_BOOT } == true {
break;
}
}
info!("hart {} booting", hart_id());
mem::hartinit();
unsafe { trap::hartinit(); }
plic::hartinit();
}
process::scheduler()
}