diff --git a/os/build.rs b/os/build.rs index 5e032311..944b46fe 100644 --- a/os/build.rs +++ b/os/build.rs @@ -1,5 +1,5 @@ +use std::fs::{read_dir, File}; use std::io::{Result, Write}; -use std::fs::{File, read_dir}; fn main() { println!("cargo:rerun-if-changed=../user/src/"); @@ -22,12 +22,16 @@ fn insert_app_data() -> Result<()> { .collect(); apps.sort(); - writeln!(f, r#" + writeln!( + f, + r#" .align 3 .section .data .global _num_app _num_app: - .quad {}"#, apps.len())?; + .quad {}"#, + apps.len() + )?; for i in 0..apps.len() { writeln!(f, r#" .quad app_{}_start"#, i)?; @@ -36,13 +40,17 @@ _num_app: for (idx, app) in apps.iter().enumerate() { println!("app_{}: {}", idx, app); - writeln!(f, r#" + writeln!( + f, + r#" .section .data .global app_{0}_start .global app_{0}_end app_{0}_start: .incbin "{2}{1}.bin" -app_{0}_end:"#, idx, app, TARGET_PATH)?; +app_{0}_end:"#, + idx, app, TARGET_PATH + )?; } Ok(()) -} \ No newline at end of file +} diff --git a/os/src/batch.rs b/os/src/batch.rs index 7f29b21b..06764a0c 100644 --- a/os/src/batch.rs +++ b/os/src/batch.rs @@ -1,7 +1,7 @@ -use lazy_static::*; -use crate::trap::TrapContext; use crate::sync::UPSafeCell; +use crate::trap::TrapContext; use core::arch::asm; +use lazy_static::*; const USER_STACK_SIZE: usize = 4096 * 2; const KERNEL_STACK_SIZE: usize = 4096 * 2; @@ -19,8 +19,12 @@ struct UserStack { data: [u8; USER_STACK_SIZE], } -static KERNEL_STACK: KernelStack = KernelStack { data: [0; KERNEL_STACK_SIZE] }; -static USER_STACK: UserStack = UserStack { data: [0; USER_STACK_SIZE] }; +static KERNEL_STACK: KernelStack = KernelStack { + data: [0; KERNEL_STACK_SIZE], +}; +static USER_STACK: UserStack = UserStack { + data: [0; USER_STACK_SIZE], +}; impl KernelStack { fn get_sp(&self) -> usize { @@ -28,7 +32,9 @@ impl KernelStack { } pub fn push_context(&self, cx: TrapContext) -> &'static mut TrapContext { let cx_ptr = (self.get_sp() - core::mem::size_of::()) as *mut TrapContext; - unsafe { *cx_ptr = cx; } + unsafe { + *cx_ptr = cx; + } unsafe { cx_ptr.as_mut().unwrap() } } } @@ -49,7 +55,12 @@ impl AppManager { pub fn print_app_info(&self) { println!("[kernel] num_app = {}", self.num_app); for i in 0..self.num_app { - println!("[kernel] app_{} [{:#x}, {:#x})", i, self.app_start[i], self.app_start[i + 1]); + println!( + "[kernel] app_{} [{:#x}, {:#x})", + i, + self.app_start[i], + self.app_start[i + 1] + ); } } @@ -61,22 +72,18 @@ impl AppManager { // clear icache asm!("fence.i"); // clear app area - core::slice::from_raw_parts_mut( - APP_BASE_ADDRESS as *mut u8, - APP_SIZE_LIMIT - ).fill(0); + core::slice::from_raw_parts_mut(APP_BASE_ADDRESS as *mut u8, APP_SIZE_LIMIT).fill(0); let app_src = core::slice::from_raw_parts( self.app_start[app_id] as *const u8, - self.app_start[app_id + 1] - self.app_start[app_id] - ); - let app_dst = core::slice::from_raw_parts_mut( - APP_BASE_ADDRESS as *mut u8, - app_src.len() + self.app_start[app_id + 1] - self.app_start[app_id], ); + let app_dst = core::slice::from_raw_parts_mut(APP_BASE_ADDRESS as *mut u8, app_src.len()); app_dst.copy_from_slice(app_src); } - pub fn get_current_app(&self) -> usize { self.current_app } + pub fn get_current_app(&self) -> usize { + self.current_app + } pub fn move_to_next_app(&mut self) { self.current_app += 1; @@ -84,21 +91,24 @@ impl AppManager { } lazy_static! { - static ref APP_MANAGER: UPSafeCell = unsafe { UPSafeCell::new({ - extern "C" { fn _num_app(); } - let num_app_ptr = _num_app as usize as *const usize; - let num_app = num_app_ptr.read_volatile(); - let mut app_start: [usize; MAX_APP_NUM + 1] = [0; MAX_APP_NUM + 1]; - let app_start_raw: &[usize] = core::slice::from_raw_parts( - num_app_ptr.add(1), num_app + 1 - ); - app_start[..=num_app].copy_from_slice(app_start_raw); - AppManager { - num_app, - current_app: 0, - app_start, - } - })}; + static ref APP_MANAGER: UPSafeCell = unsafe { + UPSafeCell::new({ + extern "C" { + fn _num_app(); + } + let num_app_ptr = _num_app as usize as *const usize; + let num_app = num_app_ptr.read_volatile(); + let mut app_start: [usize; MAX_APP_NUM + 1] = [0; MAX_APP_NUM + 1]; + let app_start_raw: &[usize] = + core::slice::from_raw_parts(num_app_ptr.add(1), num_app + 1); + app_start[..=num_app].copy_from_slice(app_start_raw); + AppManager { + num_app, + current_app: 0, + app_start, + } + }) + }; } pub fn init() { @@ -119,11 +129,14 @@ pub fn run_next_app() -> ! { drop(app_manager); // before this we have to drop local variables related to resources manually // and release the resources - extern "C" { fn __restore(cx_addr: usize); } + extern "C" { + fn __restore(cx_addr: usize); + } unsafe { - __restore(KERNEL_STACK.push_context( - TrapContext::app_init_context(APP_BASE_ADDRESS, USER_STACK.get_sp()) - ) as *const _ as usize); + __restore(KERNEL_STACK.push_context(TrapContext::app_init_context( + APP_BASE_ADDRESS, + USER_STACK.get_sp(), + )) as *const _ as usize); } panic!("Unreachable in batch::run_current_app!"); } diff --git a/os/src/console.rs b/os/src/console.rs index 2bd55930..dda4911a 100644 --- a/os/src/console.rs +++ b/os/src/console.rs @@ -1,5 +1,5 @@ -use core::fmt::{self, Write}; use crate::sbi::console_putchar; +use core::fmt::{self, Write}; struct Stdout; @@ -29,5 +29,3 @@ macro_rules! println { $crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?)); } } - - diff --git a/os/src/lang_items.rs b/os/src/lang_items.rs index 3f5462ab..af3e5152 100644 --- a/os/src/lang_items.rs +++ b/os/src/lang_items.rs @@ -1,10 +1,15 @@ -use core::panic::PanicInfo; use crate::sbi::shutdown; +use core::panic::PanicInfo; #[panic_handler] fn panic(info: &PanicInfo) -> ! { if let Some(location) = info.location() { - println!("[kernel] Panicked at {}:{} {}", location.file(), location.line(), info.message().unwrap()); + println!( + "[kernel] Panicked at {}:{} {}", + location.file(), + location.line(), + info.message().unwrap() + ); } else { println!("[kernel] Panicked: {}", info.message().unwrap()); } diff --git a/os/src/main.rs b/os/src/main.rs index 3cea360f..7c661b43 100644 --- a/os/src/main.rs +++ b/os/src/main.rs @@ -6,12 +6,12 @@ use core::arch::global_asm; #[macro_use] mod console; +mod batch; mod lang_items; mod sbi; +mod sync; mod syscall; mod trap; -mod batch; -mod sync; global_asm!(include_str!("entry.asm")); global_asm!(include_str!("link_app.S")); @@ -22,10 +22,8 @@ fn clear_bss() { fn ebss(); } unsafe { - core::slice::from_raw_parts_mut( - sbss as usize as *mut u8, - ebss as usize - sbss as usize, - ).fill(0); + core::slice::from_raw_parts_mut(sbss as usize as *mut u8, ebss as usize - sbss as usize) + .fill(0); } } @@ -36,4 +34,4 @@ pub fn rust_main() -> ! { trap::init(); batch::init(); batch::run_next_app(); -} \ No newline at end of file +} diff --git a/os/src/sbi.rs b/os/src/sbi.rs index aa12b7f3..90ba2c2a 100644 --- a/os/src/sbi.rs +++ b/os/src/sbi.rs @@ -39,4 +39,3 @@ pub fn shutdown() -> ! { sbi_call(SBI_SHUTDOWN, 0, 0, 0); panic!("It should shutdown!"); } - diff --git a/os/src/sync/mod.rs b/os/src/sync/mod.rs index 77295248..d1ce5bcf 100644 --- a/os/src/sync/mod.rs +++ b/os/src/sync/mod.rs @@ -1,3 +1,3 @@ mod up; -pub use up::UPSafeCell; \ No newline at end of file +pub use up::UPSafeCell; diff --git a/os/src/sync/up.rs b/os/src/sync/up.rs index 642668c1..c7b2c9ee 100644 --- a/os/src/sync/up.rs +++ b/os/src/sync/up.rs @@ -18,10 +18,12 @@ impl UPSafeCell { /// User is responsible to guarantee that inner struct is only used in /// uniprocessor. pub unsafe fn new(value: T) -> Self { - Self { inner: RefCell::new(value) } + Self { + inner: RefCell::new(value), + } } /// Panic if the data has been borrowed. pub fn exclusive_access(&self) -> RefMut<'_, T> { self.inner.borrow_mut() } -} \ No newline at end of file +} diff --git a/os/src/syscall/fs.rs b/os/src/syscall/fs.rs index b8c46591..3e38eae7 100644 --- a/os/src/syscall/fs.rs +++ b/os/src/syscall/fs.rs @@ -7,9 +7,9 @@ pub fn sys_write(fd: usize, buf: *const u8, len: usize) -> isize { let str = core::str::from_utf8(slice).unwrap(); print!("{}", str); len as isize - }, + } _ => { panic!("Unsupported fd in sys_write!"); } } -} \ No newline at end of file +} diff --git a/os/src/syscall/mod.rs b/os/src/syscall/mod.rs index 51dea1e7..430af564 100644 --- a/os/src/syscall/mod.rs +++ b/os/src/syscall/mod.rs @@ -14,4 +14,3 @@ pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize { _ => panic!("Unsupported syscall_id: {}", syscall_id), } } - diff --git a/os/src/syscall/process.rs b/os/src/syscall/process.rs index 66808bcf..8f4170d1 100644 --- a/os/src/syscall/process.rs +++ b/os/src/syscall/process.rs @@ -3,4 +3,4 @@ use crate::batch::run_next_app; pub fn sys_exit(exit_code: i32) -> ! { println!("[kernel] Application exited with code {}", exit_code); run_next_app() -} \ No newline at end of file +} diff --git a/os/src/trap/context.rs b/os/src/trap/context.rs index e2575de9..af5a53a8 100644 --- a/os/src/trap/context.rs +++ b/os/src/trap/context.rs @@ -1,4 +1,4 @@ -use riscv::register::sstatus::{Sstatus, self, SPP}; +use riscv::register::sstatus::{self, Sstatus, SPP}; #[repr(C)] pub struct TrapContext { @@ -8,7 +8,9 @@ pub struct TrapContext { } impl TrapContext { - pub fn set_sp(&mut self, sp: usize) { self.x[2] = sp; } + pub fn set_sp(&mut self, sp: usize) { + self.x[2] = sp; + } pub fn app_init_context(entry: usize, sp: usize) -> Self { let mut sstatus = sstatus::read(); sstatus.set_spp(SPP::User); diff --git a/os/src/trap/mod.rs b/os/src/trap/mod.rs index 83396231..0210809e 100644 --- a/os/src/trap/mod.rs +++ b/os/src/trap/mod.rs @@ -1,23 +1,20 @@ mod context; +use crate::batch::run_next_app; +use crate::syscall::syscall; +use core::arch::global_asm; use riscv::register::{ mtvec::TrapMode, - stvec, - scause::{ - self, - Trap, - Exception, - }, - stval, + scause::{self, Exception, Trap}, + stval, stvec, }; -use crate::syscall::syscall; -use crate::batch::run_next_app; -use core::arch::global_asm; global_asm!(include_str!("trap.S")); pub fn init() { - extern "C" { fn __alltraps(); } + extern "C" { + fn __alltraps(); + } unsafe { stvec::write(__alltraps as usize, TrapMode::Direct); } @@ -32,8 +29,7 @@ pub fn trap_handler(cx: &mut TrapContext) -> &mut TrapContext { cx.sepc += 4; cx.x[10] = syscall(cx.x[17], [cx.x[10], cx.x[11], cx.x[12]]) as usize; } - Trap::Exception(Exception::StoreFault) | - Trap::Exception(Exception::StorePageFault) => { + Trap::Exception(Exception::StoreFault) | Trap::Exception(Exception::StorePageFault) => { println!("[kernel] PageFault in application, kernel killed it."); run_next_app(); } @@ -42,7 +38,11 @@ pub fn trap_handler(cx: &mut TrapContext) -> &mut TrapContext { run_next_app(); } _ => { - panic!("Unsupported trap {:?}, stval = {:#x}!", scause.cause(), stval); + panic!( + "Unsupported trap {:?}, stval = {:#x}!", + scause.cause(), + stval + ); } } cx diff --git a/user/src/bin/01store_fault.rs b/user/src/bin/01store_fault.rs index 994d8bfd..f8023eb7 100644 --- a/user/src/bin/01store_fault.rs +++ b/user/src/bin/01store_fault.rs @@ -8,6 +8,8 @@ extern crate user_lib; fn main() -> i32 { println!("Into Test store_fault, we will insert an invalid store operation..."); println!("Kernel should kill this application!"); - unsafe { (0x0 as *mut u8).write_volatile(0); } + unsafe { + core::ptr::null_mut::().write_volatile(0); + } 0 -} \ No newline at end of file +} diff --git a/user/src/bin/02power.rs b/user/src/bin/02power.rs index 54fde244..f628f348 100644 --- a/user/src/bin/02power.rs +++ b/user/src/bin/02power.rs @@ -24,4 +24,4 @@ fn main() -> i32 { } println!("Test power OK!"); 0 -} \ No newline at end of file +} diff --git a/user/src/console.rs b/user/src/console.rs index ac801174..d37e867c 100644 --- a/user/src/console.rs +++ b/user/src/console.rs @@ -1,5 +1,5 @@ -use core::fmt::{self, Write}; use super::write; +use core::fmt::{self, Write}; struct Stdout; @@ -28,4 +28,4 @@ macro_rules! println { ($fmt: literal $(, $($arg: tt)+)?) => { $crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?)); } -} \ No newline at end of file +} diff --git a/user/src/lang_items.rs b/user/src/lang_items.rs index c90d297f..3aee17ba 100644 --- a/user/src/lang_items.rs +++ b/user/src/lang_items.rs @@ -2,9 +2,14 @@ fn panic_handler(panic_info: &core::panic::PanicInfo) -> ! { let err = panic_info.message().unwrap(); if let Some(location) = panic_info.location() { - println!("Panicked at {}:{}, {}", location.file(), location.line(), err); + println!( + "Panicked at {}:{}, {}", + location.file(), + location.line(), + err + ); } else { println!("Panicked: {}", err); } loop {} -} \ No newline at end of file +} diff --git a/user/src/lib.rs b/user/src/lib.rs index baa59e81..73a4037f 100644 --- a/user/src/lib.rs +++ b/user/src/lib.rs @@ -4,8 +4,8 @@ #[macro_use] pub mod console; -mod syscall; mod lang_items; +mod syscall; #[no_mangle] #[link_section = ".text.entry"] @@ -26,12 +26,16 @@ fn clear_bss() { fn start_bss(); fn end_bss(); } - (start_bss as usize..end_bss as usize).for_each(|addr| { - unsafe { (addr as *mut u8).write_volatile(0); } + (start_bss as usize..end_bss as usize).for_each(|addr| unsafe { + (addr as *mut u8).write_volatile(0); }); } use syscall::*; -pub fn write(fd: usize, buf: &[u8]) -> isize { sys_write(fd, buf) } -pub fn exit(exit_code: i32) -> isize { sys_exit(exit_code) } +pub fn write(fd: usize, buf: &[u8]) -> isize { + sys_write(fd, buf) +} +pub fn exit(exit_code: i32) -> isize { + sys_exit(exit_code) +}