forked from rcore-os/tg-rcore-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
600 lines (547 loc) · 20.1 KB
/
Copy pathmain.rs
File metadata and controls
600 lines (547 loc) · 20.1 KB
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
#![no_std]
#![no_main]
mod fs;
mod process;
mod processor;
mod virtio_block;
#[macro_use]
extern crate rcore_console;
#[macro_use]
extern crate alloc;
use crate::{
fs::{read_all, FS},
impls::{Sv39Manager, SyscallContext},
process::Process,
processor::ProcManager,
};
use alloc::alloc::alloc;
use core::{alloc::Layout, cell::UnsafeCell, mem::MaybeUninit};
use easy_fs::{FSManager, OpenFlags};
use impls::Console;
use kernel_context::foreign::MultislotPortal;
use kernel_vm::{
page_table::{MmuMeta, Sv39, VAddr, VmFlags, VmMeta, PPN, VPN},
AddressSpace,
};
use processor::PROCESSOR;
use rcore_console::log;
use rcore_task_manage::{PManager, ProcId};
use riscv::register::*;
use sbi;
use syscall::Caller;
use xmas_elf::ElfFile;
// 定义内核入口。
linker::boot0!(rust_main; stack = 32 * 4096);
// 物理内存容量 = 48 MiB。
const MEMORY: usize = 48 << 20;
// 传送门所在虚页。
const PROTAL_TRANSIT: VPN<Sv39> = VPN::MAX;
struct KernelSpace {
inner: UnsafeCell<MaybeUninit<AddressSpace<Sv39, Sv39Manager>>>,
}
unsafe impl Sync for KernelSpace {}
impl KernelSpace {
const fn new() -> Self {
Self {
inner: UnsafeCell::new(MaybeUninit::uninit()),
}
}
unsafe fn write(&self, space: AddressSpace<Sv39, Sv39Manager>) {
*self.inner.get() = MaybeUninit::new(space);
}
unsafe fn assume_init_ref(&self) -> &AddressSpace<Sv39, Sv39Manager> {
&*(*self.inner.get()).as_ptr()
}
}
// 内核地址空间。
static KERNEL_SPACE: KernelSpace = KernelSpace::new();
extern "C" fn rust_main() -> ! {
let layout = linker::KernelLayout::locate();
// bss 段清零
unsafe { layout.zero_bss() };
// 初始化 `console`
rcore_console::init_console(&Console);
rcore_console::set_log_level(option_env!("LOG"));
rcore_console::test_log();
// 初始化内核堆
kernel_alloc::init(layout.start() as _);
unsafe {
kernel_alloc::transfer(core::slice::from_raw_parts_mut(
layout.end() as _,
MEMORY - layout.len(),
))
};
// 建立异界传送门
let portal_size = MultislotPortal::calculate_size(1);
let portal_layout = Layout::from_size_align(portal_size, 1 << Sv39::PAGE_BITS).unwrap();
let portal_ptr = unsafe { alloc(portal_layout) };
assert!(portal_layout.size() < 1 << Sv39::PAGE_BITS);
// 建立内核地址空间
kernel_space(layout, MEMORY, portal_ptr as _);
// 初始化异界传送门
let portal = unsafe { MultislotPortal::init_transit(PROTAL_TRANSIT.base().val(), 1) };
// 初始化 syscall
syscall::init_io(&SyscallContext);
syscall::init_process(&SyscallContext);
syscall::init_scheduling(&SyscallContext);
syscall::init_clock(&SyscallContext);
syscall::init_memory(&SyscallContext);
// 加载初始进程
let initproc = read_all(FS.open("initproc", OpenFlags::RDONLY).unwrap());
if let Some(process) = Process::from_elf(ElfFile::new(initproc.as_slice()).unwrap()) {
PROCESSOR.get_mut().set_manager(ProcManager::new());
PROCESSOR
.get_mut()
.add(process.pid, process, ProcId::from_usize(usize::MAX));
}
loop {
let processor: *mut PManager<Process, ProcManager> = PROCESSOR.get_mut() as *mut _;
if let Some(task) = unsafe { (*processor).find_next() } {
unsafe { task.context.execute(portal, ()) };
match scause::read().cause() {
scause::Trap::Exception(scause::Exception::UserEnvCall) => {
use syscall::{SyscallId as Id, SyscallResult as Ret};
let ctx = &mut task.context.context;
ctx.move_next();
let id: Id = ctx.a(7).into();
let args = [ctx.a(0), ctx.a(1), ctx.a(2), ctx.a(3), ctx.a(4), ctx.a(5)];
match syscall::handle(Caller { entity: 0, flow: 0 }, id, args) {
Ret::Done(ret) => match id {
Id::EXIT => unsafe { (*processor).make_current_exited(ret) },
_ => {
let ctx = &mut task.context.context;
*ctx.a_mut(0) = ret as _;
unsafe { (*processor).make_current_suspend() };
}
},
Ret::Unsupported(_) => {
log::info!("id = {id:?}");
unsafe { (*processor).make_current_exited(-2) };
}
}
}
e => {
log::error!("unsupported trap: {e:?}");
unsafe { (*processor).make_current_exited(-3) };
}
}
} else {
println!("no task");
break;
}
}
sbi::shutdown(false)
}
/// Rust 异常处理函数,以异常方式关机。
#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
println!("{info}");
sbi::shutdown(true)
}
pub const MMIO: &[(usize, usize)] = &[
(0x1000_1000, 0x00_1000), // Virtio Block in virt machine
];
fn kernel_space(layout: linker::KernelLayout, memory: usize, portal: usize) {
let mut space = AddressSpace::new();
for region in layout.iter() {
log::info!("{region}");
use linker::KernelRegionTitle::*;
let flags = match region.title {
Text => "X_RV",
Rodata => "__RV",
Data | Boot => "_WRV",
};
let s = VAddr::<Sv39>::new(region.range.start);
let e = VAddr::<Sv39>::new(region.range.end);
space.map_extern(
s.floor()..e.ceil(),
PPN::new(s.floor().val()),
VmFlags::build_from_str(flags),
)
}
let s = VAddr::<Sv39>::new(layout.end());
let e = VAddr::<Sv39>::new(layout.start() + memory);
log::info!("(heap) ---> {:#10x}..{:#10x}", s.val(), e.val());
space.map_extern(
s.floor()..e.ceil(),
PPN::new(s.floor().val()),
VmFlags::build_from_str("_WRV"),
);
space.map_extern(
PROTAL_TRANSIT..PROTAL_TRANSIT + 1,
PPN::new(portal >> Sv39::PAGE_BITS),
VmFlags::build_from_str("__G_XWRV"),
);
println!();
// MMIO
for (base, len) in MMIO {
let s = VAddr::<Sv39>::new(*base);
let e = VAddr::<Sv39>::new(*base + *len);
log::info!("MMIO range -> {:#10x}..{:#10x}", s.val(), e.val());
space.map_extern(
s.floor()..e.ceil(),
PPN::new(s.floor().val()),
VmFlags::build_from_str("_WRV"),
);
}
unsafe { satp::set(satp::Mode::Sv39, 0, space.root_ppn().val()) };
unsafe { KERNEL_SPACE.write(space) };
}
/// 映射异界传送门。
fn map_portal(space: &AddressSpace<Sv39, Sv39Manager>) {
let portal_idx = PROTAL_TRANSIT.index_in(Sv39::MAX_LEVEL);
space.root()[portal_idx] = unsafe { KERNEL_SPACE.assume_init_ref() }.root()[portal_idx];
}
/// 各种接口库的实现。
mod impls {
use crate::{
fs::{read_all, FS},
process::Process as ProcStruct,
processor::ProcManager,
PROCESSOR,
};
use alloc::vec::Vec;
use alloc::{alloc::alloc_zeroed, string::String};
use core::{alloc::Layout, ptr::NonNull};
use easy_fs::UserBuffer;
use easy_fs::{FSManager, OpenFlags};
use kernel_vm::{
page_table::{MmuMeta, Pte, Sv39, VAddr, VmFlags, PPN, VPN},
PageManager,
};
use rcore_console::log;
use rcore_task_manage::{PManager, ProcId};
use spin::Mutex;
use syscall::*;
use xmas_elf::ElfFile;
#[repr(transparent)]
pub struct Sv39Manager(NonNull<Pte<Sv39>>);
impl Sv39Manager {
const OWNED: VmFlags<Sv39> = unsafe { VmFlags::from_raw(1 << 8) };
#[inline]
fn page_alloc<T>(count: usize) -> *mut T {
unsafe {
alloc_zeroed(Layout::from_size_align_unchecked(
count << Sv39::PAGE_BITS,
1 << Sv39::PAGE_BITS,
))
}
.cast()
}
}
impl PageManager<Sv39> for Sv39Manager {
#[inline]
fn new_root() -> Self {
Self(NonNull::new(Self::page_alloc(1)).unwrap())
}
#[inline]
fn root_ppn(&self) -> PPN<Sv39> {
PPN::new(self.0.as_ptr() as usize >> Sv39::PAGE_BITS)
}
#[inline]
fn root_ptr(&self) -> NonNull<Pte<Sv39>> {
self.0
}
#[inline]
fn p_to_v<T>(&self, ppn: PPN<Sv39>) -> NonNull<T> {
unsafe { NonNull::new_unchecked(VPN::<Sv39>::new(ppn.val()).base().as_mut_ptr()) }
}
#[inline]
fn v_to_p<T>(&self, ptr: NonNull<T>) -> PPN<Sv39> {
PPN::new(VAddr::<Sv39>::new(ptr.as_ptr() as _).floor().val())
}
#[inline]
fn check_owned(&self, pte: Pte<Sv39>) -> bool {
pte.flags().contains(Self::OWNED)
}
#[inline]
fn allocate(&mut self, len: usize, flags: &mut VmFlags<Sv39>) -> NonNull<u8> {
*flags |= Self::OWNED;
NonNull::new(Self::page_alloc(len)).unwrap()
}
fn deallocate(&mut self, _pte: Pte<Sv39>, _len: usize) -> usize {
todo!()
}
fn drop_root(&mut self) {
todo!()
}
}
pub struct Console;
impl rcore_console::Console for Console {
#[inline]
fn put_char(&self, c: u8) {
sbi::console_putchar(c);
}
}
pub struct SyscallContext;
const READABLE: VmFlags<Sv39> = VmFlags::build_from_str("RV");
const WRITEABLE: VmFlags<Sv39> = VmFlags::build_from_str("W_V");
impl IO for SyscallContext {
fn write(&self, _caller: Caller, fd: usize, buf: usize, count: usize) -> isize {
let current = PROCESSOR.get_mut().current().unwrap();
if let Some(ptr) = current.address_space.translate(VAddr::new(buf), READABLE) {
if fd == STDOUT || fd == STDDEBUG {
print!("{}", unsafe {
core::str::from_utf8_unchecked(core::slice::from_raw_parts(
ptr.as_ptr(),
count,
))
});
count as _
} else if let Some(file) = ¤t.fd_table[fd] {
let mut file = file.lock();
if file.writable() {
let mut v: Vec<&'static mut [u8]> = Vec::new();
unsafe { v.push(core::slice::from_raw_parts_mut(ptr.as_ptr(), count)) };
file.write(UserBuffer::new(v)) as _
} else {
log::error!("file not writable");
-1
}
} else {
log::error!("unsupported fd: {fd}");
-1
}
} else {
log::error!("ptr not readable");
-1
}
}
fn read(&self, _caller: Caller, fd: usize, buf: usize, count: usize) -> isize {
let current = PROCESSOR.get_mut().current().unwrap();
if let Some(ptr) = current.address_space.translate(VAddr::new(buf), WRITEABLE) {
if fd == STDIN {
let mut ptr = ptr.as_ptr();
for _ in 0..count {
unsafe {
*ptr = sbi::console_getchar() as u8;
ptr = ptr.add(1);
}
}
count as _
} else if let Some(file) = ¤t.fd_table[fd] {
let mut file = file.lock();
if file.readable() {
let mut v: Vec<&'static mut [u8]> = Vec::new();
unsafe { v.push(core::slice::from_raw_parts_mut(ptr.as_ptr(), count)) };
file.read(UserBuffer::new(v)) as _
} else {
log::error!("file not readable");
-1
}
} else {
log::error!("unsupported fd: {fd}");
-1
}
} else {
log::error!("ptr not writeable");
-1
}
}
fn open(&self, _caller: Caller, path: usize, flags: usize) -> isize {
// FS.open(, flags)
let current = PROCESSOR.get_mut().current().unwrap();
if let Some(ptr) = current.address_space.translate(VAddr::new(path), READABLE) {
let mut string = String::new();
let mut raw_ptr: *mut u8 = ptr.as_ptr();
loop {
unsafe {
let ch = *raw_ptr;
if ch == 0 {
break;
}
string.push(ch as char);
raw_ptr = (raw_ptr as usize + 1) as *mut u8;
}
}
if let Some(fd) =
FS.open(string.as_str(), OpenFlags::from_bits(flags as u32).unwrap())
{
let new_fd = current.fd_table.len();
current.fd_table.push(Some(Mutex::new(fd.as_ref().clone())));
new_fd as isize
} else {
-1
}
} else {
log::error!("ptr not writeable");
-1
}
}
#[inline]
fn close(&self, _caller: Caller, fd: usize) -> isize {
let current = PROCESSOR.get_mut().current().unwrap();
if fd >= current.fd_table.len() || current.fd_table[fd].is_none() {
return -1;
}
current.fd_table[fd].take();
0
}
// TODO: 实现 linkat 系统调用
fn linkat(
&self,
_caller: Caller,
_olddirfd: i32,
_oldpath: usize,
_newdirfd: i32,
_newpath: usize,
_flags: u32,
) -> isize {
rcore_console::log::info!("linkat: not implemented");
-1
}
// TODO: 实现 unlinkat 系统调用
fn unlinkat(&self, _caller: Caller, _dirfd: i32, _path: usize, _flags: u32) -> isize {
rcore_console::log::info!("unlinkat: not implemented");
-1
}
// TODO: 实现 fstat 系统调用
fn fstat(&self, _caller: Caller, _fd: usize, _st: usize) -> isize {
rcore_console::log::info!("fstat: not implemented");
-1
}
}
impl Process for SyscallContext {
#[inline]
fn exit(&self, _caller: Caller, exit_code: usize) -> isize {
exit_code as isize
}
fn fork(&self, _caller: Caller) -> isize {
let processor: *mut PManager<ProcStruct, ProcManager> = PROCESSOR.get_mut() as *mut _;
let current = unsafe { (*processor).current().unwrap() };
let mut child_proc = current.fork().unwrap();
let pid = child_proc.pid;
let context = &mut child_proc.context.context;
*context.a_mut(0) = 0 as _;
unsafe {
(*processor).add(pid, child_proc, current.pid);
}
pid.get_usize() as isize
}
fn exec(&self, _caller: Caller, path: usize, count: usize) -> isize {
const READABLE: VmFlags<Sv39> = VmFlags::build_from_str("RV");
let current = PROCESSOR.get_mut().current().unwrap();
current
.address_space
.translate(VAddr::new(path), READABLE)
.map(|ptr| unsafe {
core::str::from_utf8_unchecked(core::slice::from_raw_parts(ptr.as_ptr(), count))
})
.and_then(|name| FS.open(name, OpenFlags::RDONLY))
.map_or_else(
|| {
log::error!("unknown app, select one in the list: ");
FS.readdir("")
.unwrap()
.into_iter()
.for_each(|app| println!("{app}"));
println!();
-1
},
|fd| {
current.exec(ElfFile::new(&read_all(fd)).unwrap());
0
},
)
}
fn wait(&self, _caller: Caller, pid: isize, exit_code_ptr: usize) -> isize {
let processor: *mut PManager<ProcStruct, ProcManager> = PROCESSOR.get_mut() as *mut _;
let current = unsafe { (*processor).current().unwrap() };
const WRITABLE: VmFlags<Sv39> = VmFlags::build_from_str("W_V");
if let Some((dead_pid, exit_code)) =
unsafe { (*processor).wait(ProcId::from_usize(pid as usize)) }
{
if let Some(mut ptr) = current
.address_space
.translate::<i32>(VAddr::new(exit_code_ptr), WRITABLE)
{
unsafe { *ptr.as_mut() = exit_code as i32 };
}
return dead_pid.get_usize() as isize;
} else {
// 等待的子进程不存在
return -1;
}
}
fn getpid(&self, _caller: Caller) -> isize {
let current = PROCESSOR.get_mut().current().unwrap();
current.pid.get_usize() as _
}
// TODO: 实现 spawn 系统调用
fn spawn(&self, _caller: Caller, _path: usize, _count: usize) -> isize {
let current = PROCESSOR.get_mut().current().unwrap();
rcore_console::log::info!(
"spawn: parent pid = {}, not implemented",
current.pid.get_usize()
);
-1
}
}
impl Scheduling for SyscallContext {
#[inline]
fn sched_yield(&self, _caller: Caller) -> isize {
0
}
// TODO: 实现 set_priority 系统调用
fn set_priority(&self, _caller: Caller, prio: isize) -> isize {
let current = PROCESSOR.get_mut().current().unwrap();
rcore_console::log::info!(
"set_priority: pid = {}, prio = {}, not implemented",
current.pid.get_usize(),
prio
);
-1
}
}
impl Clock for SyscallContext {
#[inline]
fn clock_gettime(&self, _caller: Caller, clock_id: ClockId, tp: usize) -> isize {
const WRITABLE: VmFlags<Sv39> = VmFlags::build_from_str("W_V");
match clock_id {
ClockId::CLOCK_MONOTONIC => {
if let Some(mut ptr) = PROCESSOR
.get_mut()
.current()
.unwrap()
.address_space
.translate(VAddr::new(tp), WRITABLE)
{
let time = riscv::register::time::read() * 10000 / 125;
*unsafe { ptr.as_mut() } = TimeSpec {
tv_sec: time / 1_000_000_000,
tv_nsec: time % 1_000_000_000,
};
0
} else {
log::error!("ptr not readable");
-1
}
}
_ => -1,
}
}
}
impl Memory for SyscallContext {
// TODO: 实现 mmap 系统调用
fn mmap(
&self,
_caller: Caller,
addr: usize,
len: usize,
prot: i32,
_flags: i32,
_fd: i32,
_offset: usize,
) -> isize {
rcore_console::log::info!(
"mmap: addr = {addr:#x}, len = {len}, prot = {prot}, not implemented"
);
-1
}
// TODO: 实现 munmap 系统调用
fn munmap(&self, _caller: Caller, addr: usize, len: usize) -> isize {
rcore_console::log::info!("munmap: addr = {addr:#x}, len = {len}, not implemented");
-1
}
}
}