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
#![no_std]
#![no_main]
#![deny(warnings)]
#![feature(panic_info_message)]
#![feature(naked_functions, asm_const)]
#![feature(btree_drain_filter)]
#![feature(drain_filter)]
#[macro_use]
extern crate log;
#[macro_use]
pub mod console;
pub mod constants;
pub mod drivers;
pub mod error;
pub mod file;
pub mod lang;
pub mod loaders;
pub mod memory;
pub mod signal;
pub mod syscall;
pub mod task;
pub mod testcases;
pub mod trap;
pub mod utils;
#[path = "arch/riscv/mod.rs"]
mod arch;
#[macro_use]
extern crate bitflags;
#[macro_use]
extern crate alloc;
extern crate fatfs;
extern crate fscommon;
extern crate lock;
mod fsio {
pub use fscommon::{Read, Seek, Write};
}
core::arch::global_asm!(include_str!("fs.S"));
use alloc::sync::Arc;
use base_file::File;
use log::*;
struct TaskTrampoline;
impl task_trampoline::TaskTrampoline for TaskTrampoline {
fn suspend_current_task(&self) {
task::suspend_current_task()
}
fn get_file(&self, fd: usize) -> Option<Arc<dyn base_file::File>> {
let task = task::get_current_task().unwrap();
let fd_manager = task.fd_manager.lock();
if let Ok(file) = fd_manager.get_file(fd) {
Some(file)
} else {
None
}
}
fn push_file(&self, file: Arc<dyn File>) -> Result<usize, u64> {
let task = task::get_current_task().unwrap();
let mut fd_manager = task.fd_manager.lock();
fd_manager.push(file).map_err(|_| 1)
}
fn manually_alloc_user_str(&self, buf: *const u8, len: usize) -> Result<(), u64> {
let task = task::get_current_task().unwrap();
let mut task_vm = task.vm.lock();
task_vm.manually_alloc_user_str(buf, len).map_err(|_| 1)
}
fn manually_alloc_range(&self, start_vaddr: usize, end_vaddr: usize) -> Result<(), u64> {
let task = task::get_current_task().unwrap();
let mut task_vm = task.vm.lock();
task_vm
.manually_alloc_range(start_vaddr, end_vaddr)
.map_err(|_| 1)
}
fn raw_time(&self) -> (usize, usize) {
let task = task::get_current_task().unwrap();
let time = task.time.lock();
time.output_raw()
}
fn raw_timer(&self) -> (usize, usize) {
let task = task::get_current_task().unwrap();
let time = task.time.lock();
time.output_raw_timer()
}
fn set_timer(
&self,
timer_interval_us: usize,
timer_remained_us: usize,
timer_type: usize,
) -> bool {
let task = task::get_current_task().unwrap();
let mut time = task.time.lock();
time.set_raw_timer(timer_interval_us, timer_remained_us, timer_type)
}
}
#[no_mangle]
pub extern "C" fn start_kernel(_arg0: usize, _arg1: usize) -> ! {
arch::clear_bss(); console::init_logger(crate::constants::LOG_LEVEL).unwrap();
task_trampoline::init_task_trampoline(&TaskTrampoline);
memory::allocator_init(); memory::enable_kernel_page_table(); trap::init(); arch::allow_sum_access(); file::list_files_at_root(); file::fs_init(); let cpu_id = arch::get_cpu_id();
info!("CPU [{cpu_id}] bootstrap");
for other_cpu in constants::FIRST_CPU_ID..=constants::LAST_CPU_ID {
if other_cpu != cpu_id {
let _entry = arch::secondary_entry as usize;
}
}
if constants::SPIN_LOOP_AFTER_BOOT {
loop {}
} else {
task::run_tasks();
}
}
#[no_mangle]
pub extern "C" fn start_kernel_secondary(_arg0: usize, _arg1: usize) -> ! {
memory::enable_kernel_page_table(); trap::init(); arch::allow_sum_access(); let cpu_id = arch::get_cpu_id();
info!("I'm CPU [{cpu_id}]");
if constants::SPIN_LOOP_AFTER_BOOT || constants::IS_SINGLE_CORE {
loop {}
} else {
task::run_tasks();
}
}
pub fn test_vm() {
extern "C" {
fn stext();
fn etext();
fn sdata();
fn edata();
fn srodata();
fn erodata();
fn sbss();
fn ebss();
}
println!(
"\
stext = {:x}
etext = {:x}
sdata = {:x}
edata = {:x}
srodata = {:x}
erodata = {:x}
sbss = {:x}
ebss = {:x}
",
stext as usize,
etext as usize,
sdata as usize,
edata as usize,
srodata as usize,
erodata as usize,
sbss as usize,
ebss as usize,
);
}