diff --git a/os/Cargo.lock b/os/Cargo.lock index 3775338..930eae3 100644 --- a/os/Cargo.lock +++ b/os/Cargo.lock @@ -50,7 +50,7 @@ checksum = "8d5439c4ad607c3c23abf66de8c8bf57ba8adcd1f129e699851a6e43935d339d" [[package]] name = "os" -version = "0.1.0" +version = "0.3.0" dependencies = [ "lazy_static", "riscv", diff --git a/os/Cargo.toml b/os/Cargo.toml index 22acc70..ca83c2e 100644 --- a/os/Cargo.toml +++ b/os/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "os" -version = "0.1.0" +version = "0.3.0" edition = "2021" [dependencies] diff --git a/os/src/boards.rs b/os/src/boards.rs index 1d6396d..d3daf7f 100644 --- a/os/src/boards.rs +++ b/os/src/boards.rs @@ -1 +1 @@ -pub mod qemu; \ No newline at end of file +pub mod qemu; diff --git a/os/src/boards/qemu.rs b/os/src/boards/qemu.rs index ed95dd0..7412c84 100644 --- a/os/src/boards/qemu.rs +++ b/os/src/boards/qemu.rs @@ -1 +1 @@ -pub const CLOCK_FREQUENCY: usize = 10000000; \ No newline at end of file +pub const CLOCK_FREQUENCY: usize = 10000000; diff --git a/os/src/config.rs b/os/src/config.rs index 1c7c317..31c0be9 100644 --- a/os/src/config.rs +++ b/os/src/config.rs @@ -5,4 +5,4 @@ pub const MAX_APP_NUM: usize = 4; pub const APP_BASE_ADDRESS: usize = 0x80400000; pub const APP_SIZE_LIMIT: usize = 0x20000; -pub use crate::boards::qemu::CLOCK_FREQUENCY; \ No newline at end of file +pub use crate::boards::qemu::CLOCK_FREQUENCY; diff --git a/os/src/console.rs b/os/src/console.rs index e88704f..8b22298 100644 --- a/os/src/console.rs +++ b/os/src/console.rs @@ -35,34 +35,34 @@ macro_rules! println { #[macro_export] macro_rules! log_error { ($fmt: literal $(, $($arg: tt)+)?) => { - $crate::console::print(format_args!(concat!("\x1b[31m" ,concat!($fmt, "\x1b[0m\n")) $(, $($arg)+)?)); + $crate::console::print(format_args!(concat!("\x1b[31m [kernel]" ,concat!($fmt, "\x1b[0m\n")) $(, $($arg)+)?)); } } #[macro_export] macro_rules! log_warning { ($fmt: literal $(, $($arg: tt)+)?) => { - $crate::console::print(format_args!(concat!("\x1b[93m" ,concat!($fmt, "\x1b[0m\n")) $(, $($arg)+)?)); + $crate::console::print(format_args!(concat!("\x1b[93m [kernel]" ,concat!($fmt, "\x1b[0m\n")) $(, $($arg)+)?)); } } #[macro_export] macro_rules! log_information { ($fmt: literal $(, $($arg: tt)+)?) => { - $crate::console::print(format_args!(concat!("\x1b[34m" ,concat!($fmt, "\x1b[0m\n")) $(, $($arg)+)?)); + $crate::console::print(format_args!(concat!("\x1b[34m [kernel]" ,concat!($fmt, "\x1b[0m\n")) $(, $($arg)+)?)); } } #[macro_export] macro_rules! log_debug { ($fmt: literal $(, $($arg: tt)+)?) => { - $crate::console::print(format_args!(concat!("\x1b[32m" ,concat!($fmt, "\x1b[0m\n")) $(, $($arg)+)?)); + $crate::console::print(format_args!(concat!("\x1b[32m [kernel]" ,concat!($fmt, "\x1b[0m\n")) $(, $($arg)+)?)); } } #[macro_export] macro_rules! log_trace { ($fmt: literal $(, $($arg: tt)+)?) => { - $crate::console::print(format_args!(concat!("\x1b[90m" ,concat!($fmt, "\x1b[0m\n")) $(, $($arg)+)?)); + $crate::console::print(format_args!(concat!("\x1b[90m [kernel]" ,concat!($fmt, "\x1b[0m\n")) $(, $($arg)+)?)); } } diff --git a/os/src/loader.rs b/os/src/loader.rs index e33e14b..25323f9 100644 --- a/os/src/loader.rs +++ b/os/src/loader.rs @@ -1,8 +1,9 @@ use crate::config::{ APP_BASE_ADDRESS, APP_SIZE_LIMIT, KERNEL_STACK_SIZE, MAX_APP_NUM, USER_STACK_SIZE, }; -use core::arch::asm; +use crate::log_information; use crate::trap::context::TrapContext; +use core::arch::asm; #[repr(align(4096))] #[derive(Copy, Clone)] @@ -30,7 +31,7 @@ impl KernelStack { } pub fn push_context(&self, trap_context: TrapContext) -> usize { - let pointer = (self.get_sp() - core::mem::size_of::()) as *mut TrapContext; + let pointer = (self.get_sp() - size_of::()) as *mut TrapContext; unsafe { *pointer = trap_context; @@ -85,6 +86,7 @@ pub fn load_apps() { destination.copy_from_slice(source); } + log_information!("Load {} applications in memory.", app_num); unsafe { asm!("fence.i"); } @@ -93,6 +95,6 @@ pub fn load_apps() { pub fn initialize_app_context(app_id: usize) -> usize { KERNEL_STACK[app_id].push_context(TrapContext::init_application_context( APP_BASE_ADDRESS + app_id * APP_SIZE_LIMIT, - USER_STACK[app_id].get_sp() + USER_STACK[app_id].get_sp(), )) -} \ No newline at end of file +} diff --git a/os/src/main.rs b/os/src/main.rs index b1f37a1..351c8f4 100644 --- a/os/src/main.rs +++ b/os/src/main.rs @@ -4,17 +4,17 @@ use crate::utils::clear_bss; use core::arch::global_asm; +mod boards; +mod config; mod console; +mod loader; mod sbi; mod sync; mod syscall; -mod trap; -mod utils; -mod loader; -mod boards; -mod config; mod task; mod timer; +mod trap; +mod utils; global_asm!(include_str!("entry.asm")); global_asm!(include_str!("link_app.asm")); @@ -22,7 +22,7 @@ global_asm!(include_str!("link_app.asm")); #[no_mangle] fn rust_main() -> ! { clear_bss(); - log_information!("[kernel] Hello, rCore!"); + log_information!("Hello, rCore!"); trap::init(); loader::load_apps(); task::run_first_task(); diff --git a/os/src/sync/single_cell.rs b/os/src/sync/single_cell.rs index f6687cd..20ab3be 100644 --- a/os/src/sync/single_cell.rs +++ b/os/src/sync/single_cell.rs @@ -8,7 +8,7 @@ pub struct SingleCell { unsafe impl Sync for SingleCell {} impl SingleCell { - pub unsafe fn new(value: T) -> SingleCell { + pub fn new(value: T) -> SingleCell { SingleCell { inner: RefCell::new(value), } diff --git a/os/src/syscall.rs b/os/src/syscall.rs index 03dcfbe..6e9bd4d 100644 --- a/os/src/syscall.rs +++ b/os/src/syscall.rs @@ -1,3 +1,4 @@ +use crate::log_error; use crate::syscall::fs::sys_write; use crate::syscall::process::{sys_exit, sys_get_time, sys_yield}; @@ -16,7 +17,8 @@ pub fn syscall(syscall_id: usize, args: [usize; 3]) -> usize { SYSCALL_YIELD => sys_yield(), SYSCALL_GET_TIME => sys_get_time(), _ => { - panic!("Unsupported syscall: {}", syscall_id) - }, + log_error!("Unsupported syscall {} used.", syscall_id); + 0 + } } } diff --git a/os/src/syscall/fs.rs b/os/src/syscall/fs.rs index 97d129b..3eef5e3 100644 --- a/os/src/syscall/fs.rs +++ b/os/src/syscall/fs.rs @@ -1,4 +1,4 @@ -use crate::print; +use crate::{log_error, print}; const STDOUT_FD: usize = 1; @@ -11,7 +11,8 @@ pub fn sys_write(fd: usize, buffer: *const u8, len: usize) -> usize { len } _ => { - panic!("Unsupported file descriptor in sys_write"); + log_error!("Unsupported file descriptor {} used.", fd); + 0 } } } diff --git a/os/src/syscall/process.rs b/os/src/syscall/process.rs index 54059cd..06a14fd 100644 --- a/os/src/syscall/process.rs +++ b/os/src/syscall/process.rs @@ -1,9 +1,9 @@ -use crate::println; +use crate::log_information; use crate::task::{exit_current_and_run_next, suspend_current_and_run_next}; use crate::timer::get_time_ms; pub fn sys_exit(exit_state: i32) -> ! { - println!("[kernel] Application exited with code {}.", exit_state); + log_information!("Application exited with code {}.", exit_state); exit_current_and_run_next(); unreachable!() diff --git a/os/src/task.rs b/os/src/task.rs index e65477c..7a97b46 100644 --- a/os/src/task.rs +++ b/os/src/task.rs @@ -1,3 +1,5 @@ +use lazy_static::lazy_static; + use crate::config::MAX_APP_NUM; use crate::loader::{get_app_num, initialize_app_context}; use crate::log_information; @@ -5,7 +7,6 @@ use crate::sbi::shutdown; use crate::sync::single_cell::SingleCell; use crate::task::context::TaskContext; use crate::task::switch::__switch; -use lazy_static::lazy_static; mod context; mod switch; @@ -30,7 +31,7 @@ pub struct TaskManagerInner { } pub struct TaskManager { - app_numer: usize, + app_number: usize, inner: SingleCell, } @@ -48,8 +49,8 @@ lazy_static! { } TaskManager { - app_numer: app_num, - inner: unsafe { + app_number: app_num, + inner: { SingleCell::new(TaskManagerInner { tasks, current_task: 0, @@ -90,8 +91,8 @@ impl TaskManager { fn find_next_task(&self) -> Option { let inner = self.inner.exclusive_borrow(); - (inner.current_task + 1..inner.current_task + self.app_numer + 1) - .map(|id| id % self.app_numer) + (inner.current_task + 1..inner.current_task + self.app_number + 1) + .map(|id| id % self.app_number) .find(|id| inner.tasks[*id].status == TaskStatus::Ready) } @@ -116,7 +117,7 @@ impl TaskManager { } } - fn run_first_task(&self) -> ! { + fn run_first_task(&self) { let mut inner = self.inner.exclusive_borrow(); let task0 = &mut inner.tasks[0]; task0.status = TaskStatus::Running; @@ -127,7 +128,5 @@ impl TaskManager { unsafe { __switch(&mut unused as *mut TaskContext, next_task_context); } - - unreachable!(); } } diff --git a/os/src/task/context.rs b/os/src/task/context.rs index 9b0b887..3c0373a 100644 --- a/os/src/task/context.rs +++ b/os/src/task/context.rs @@ -1,10 +1,9 @@ - #[derive(Copy, Clone)] #[repr(C)] pub struct TaskContext { ra: usize, sp: usize, - s: [usize; 12] + s: [usize; 12], } impl TaskContext { @@ -12,7 +11,7 @@ impl TaskContext { Self { ra: 0, sp: 0, - s: [0; 12] + s: [0; 12], } } @@ -25,7 +24,7 @@ impl TaskContext { Self { ra: __restore as usize, sp: kernel_stack_pointer, - s: [0; 12] + s: [0; 12], } } } diff --git a/os/src/task/switch.rs b/os/src/task/switch.rs index 6ef6dcf..2fa2abb 100644 --- a/os/src/task/switch.rs +++ b/os/src/task/switch.rs @@ -6,5 +6,3 @@ global_asm!(include_str!("switch.asm")); extern "C" { pub fn __switch(current_task_context: *mut TaskContext, next_task_context: *const TaskContext); } - - diff --git a/os/src/timer.rs b/os/src/timer.rs index 6c0ec82..d2eff6f 100644 --- a/os/src/timer.rs +++ b/os/src/timer.rs @@ -1,6 +1,6 @@ -use riscv::register::time; use crate::config::CLOCK_FREQUENCY; use crate::sbi::set_timer; +use riscv::register::time; pub fn get_time() -> usize { time::read() diff --git a/os/src/trap.asm b/os/src/trap.asm index 3aaad85..9a05fd6 100644 --- a/os/src/trap.asm +++ b/os/src/trap.asm @@ -59,4 +59,5 @@ __restore: addi sp, sp, 34*8 # now sp->kernel stack, sscratch->user stack csrrw sp, sscratch, sp + # now sp->user stack, sscratch->kernel stack sret \ No newline at end of file diff --git a/os/src/trap.rs b/os/src/trap.rs index d97caac..ff6496f 100644 --- a/os/src/trap.rs +++ b/os/src/trap.rs @@ -1,10 +1,14 @@ -use crate::println; +use crate::log_warning; use crate::syscall::syscall; -use crate::trap::context::TrapContext; -use core::arch::global_asm; -use riscv::register::{mtvec::TrapMode, scause::{self, Exception, Trap, Interrupt}, sie, stval, stvec}; use crate::task::{exit_current_and_run_next, suspend_current_and_run_next}; use crate::timer::set_next_trigger; +use crate::trap::context::TrapContext; +use core::arch::global_asm; +use riscv::register::{ + mtvec::TrapMode, + scause::{self, Exception, Interrupt, Trap}, + sie, stval, stvec, +}; pub mod context; @@ -35,11 +39,15 @@ pub fn trap_handler(context: &mut TrapContext) -> &mut TrapContext { context.x[10] = syscall(context.x[17], [context.x[10], context.x[11], context.x[12]]); } Trap::Exception(Exception::StoreFault) | Trap::Exception(Exception::StorePageFault) => { - println!("[kernel] PageFault in application, kernel will kill it."); + log_warning!("PageFault in application, kernel will kill it."); exit_current_and_run_next(); } Trap::Exception(Exception::IllegalInstruction) => { - println!("[kernel] Illegal instruction in application, kernel will kill it."); + log_warning!("Illegal instruction in application, kernel will kill it."); + exit_current_and_run_next(); + } + Trap::Exception(Exception::LoadFault) => { + log_warning!("Illegal memory access in application, kernel will kill it."); exit_current_and_run_next(); } Trap::Interrupt(Interrupt::SupervisorTimer) => { diff --git a/user/src/bin/sleep.rs b/user/src/bin/0sleep.rs similarity index 91% rename from user/src/bin/sleep.rs rename to user/src/bin/0sleep.rs index b19774a..d3df999 100644 --- a/user/src/bin/sleep.rs +++ b/user/src/bin/0sleep.rs @@ -11,10 +11,11 @@ fn main() -> i32 { let current_time = get_time(); let wait_time = current_time + 3000; + println!("Sleep~~~~"); while get_time() < wait_time { sys_yield(); } println!("Test sleep OK!"); 0 -} \ No newline at end of file +} diff --git a/user/src/bin/power_3.rs b/user/src/bin/power_3.rs index e0ec5f3..1a04dc7 100644 --- a/user/src/bin/power_3.rs +++ b/user/src/bin/power_3.rs @@ -25,4 +25,4 @@ fn main() -> i32 { println!("{}^{} = {}(MOD {})", p, iter, s[cur], m); println!("Test power_3 OK!"); 0 -} \ No newline at end of file +} diff --git a/user/src/bin/power_5.rs b/user/src/bin/power_5.rs index f31a24e..e47761b 100644 --- a/user/src/bin/power_5.rs +++ b/user/src/bin/power_5.rs @@ -25,4 +25,4 @@ fn main() -> i32 { println!("{}^{} = {}(MOD {})", p, iter, s[cur], m); println!("Test power_5 OK!"); 0 -} \ No newline at end of file +} diff --git a/user/src/bin/power_7.rs b/user/src/bin/power_7.rs index fd9f23e..a97d2f5 100644 --- a/user/src/bin/power_7.rs +++ b/user/src/bin/power_7.rs @@ -25,4 +25,4 @@ fn main() -> i32 { println!("{}^{} = {}(MOD {})", p, iter, s[cur], m); println!("Test power_7 OK!"); 0 -} \ No newline at end of file +}