From 80aba4475eaf13048a0fc9151a074dae1df75bdc Mon Sep 17 00:00:00 2001 From: Yifan Wu Date: Fri, 21 Jan 2022 14:12:23 -0800 Subject: [PATCH] cargo clippy & fmt --- os/build.rs | 20 ++++++++++----- os/src/config.rs | 2 +- os/src/console.rs | 4 +-- os/src/lang_items.rs | 9 +++++-- os/src/loader.rs | 52 ++++++++++++++++++++------------------- os/src/main.rs | 14 +++++------ os/src/sbi.rs | 1 - os/src/sync/mod.rs | 2 +- os/src/sync/up.rs | 6 +++-- os/src/syscall/fs.rs | 4 +-- os/src/syscall/mod.rs | 1 - os/src/syscall/process.rs | 7 ++---- os/src/task/context.rs | 5 ++-- os/src/task/mod.rs | 47 +++++++++++++++-------------------- os/src/task/switch.rs | 5 +--- os/src/task/task.rs | 2 +- os/src/trap/context.rs | 6 +++-- os/src/trap/mod.rs | 26 ++++++++++---------- user/src/bin/00write_a.rs | 6 +++-- user/src/bin/01write_b.rs | 4 ++- user/src/bin/02write_c.rs | 4 ++- user/src/console.rs | 4 +-- user/src/lang_items.rs | 9 +++++-- user/src/lib.rs | 18 +++++++++----- 24 files changed, 138 insertions(+), 120 deletions(-) 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/config.rs b/os/src/config.rs index 7f56952c..c9b854fa 100644 --- a/os/src/config.rs +++ b/os/src/config.rs @@ -2,4 +2,4 @@ pub const USER_STACK_SIZE: usize = 4096 * 2; pub const KERNEL_STACK_SIZE: usize = 4096 * 2; pub const MAX_APP_NUM: usize = 4; pub const APP_BASE_ADDRESS: usize = 0x80400000; -pub const APP_SIZE_LIMIT: usize = 0x20000; \ No newline at end of file +pub const APP_SIZE_LIMIT: usize = 0x20000; 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/loader.rs b/os/src/loader.rs index 0d0a5774..0f08a174 100644 --- a/os/src/loader.rs +++ b/os/src/loader.rs @@ -1,5 +1,5 @@ -use crate::trap::TrapContext; use crate::config::*; +use crate::trap::TrapContext; use core::arch::asm; #[repr(align(4096))] @@ -14,15 +14,13 @@ struct UserStack { data: [u8; USER_STACK_SIZE], } -static KERNEL_STACK: [KernelStack; MAX_APP_NUM] = [ - KernelStack { data: [0; KERNEL_STACK_SIZE], }; - MAX_APP_NUM -]; +static KERNEL_STACK: [KernelStack; MAX_APP_NUM] = [KernelStack { + data: [0; KERNEL_STACK_SIZE], +}; MAX_APP_NUM]; -static USER_STACK: [UserStack; MAX_APP_NUM] = [ - UserStack { data: [0; USER_STACK_SIZE], }; - MAX_APP_NUM -]; +static USER_STACK: [UserStack; MAX_APP_NUM] = [UserStack { + data: [0; USER_STACK_SIZE], +}; MAX_APP_NUM]; impl KernelStack { fn get_sp(&self) -> usize { @@ -30,7 +28,9 @@ impl KernelStack { } pub fn push_context(&self, trap_cx: TrapContext) -> usize { let trap_cx_ptr = (self.get_sp() - core::mem::size_of::()) as *mut TrapContext; - unsafe { *trap_cx_ptr = trap_cx; } + unsafe { + *trap_cx_ptr = trap_cx; + } trap_cx_ptr as usize } } @@ -46,39 +46,41 @@ fn get_base_i(app_id: usize) -> usize { } pub fn get_num_app() -> usize { - extern "C" { fn _num_app(); } + extern "C" { + fn _num_app(); + } unsafe { (_num_app as usize as *const usize).read_volatile() } } pub fn load_apps() { - extern "C" { fn _num_app(); } + extern "C" { + fn _num_app(); + } let num_app_ptr = _num_app as usize as *const usize; let num_app = get_num_app(); - let app_start = unsafe { - core::slice::from_raw_parts(num_app_ptr.add(1), num_app + 1) - }; + let app_start = unsafe { core::slice::from_raw_parts(num_app_ptr.add(1), num_app + 1) }; // clear i-cache first - unsafe { asm!("fence.i"); } + unsafe { + asm!("fence.i"); + } // load apps for i in 0..num_app { let base_i = get_base_i(i); // clear region - (base_i..base_i + APP_SIZE_LIMIT).for_each(|addr| unsafe { - (addr as *mut u8).write_volatile(0) - }); + (base_i..base_i + APP_SIZE_LIMIT) + .for_each(|addr| unsafe { (addr as *mut u8).write_volatile(0) }); // load app from data section to memory let src = unsafe { core::slice::from_raw_parts(app_start[i] as *const u8, app_start[i + 1] - app_start[i]) }; - let dst = unsafe { - core::slice::from_raw_parts_mut(base_i as *mut u8, src.len()) - }; + let dst = unsafe { core::slice::from_raw_parts_mut(base_i as *mut u8, src.len()) }; dst.copy_from_slice(src); } } pub fn init_app_cx(app_id: usize) -> usize { - KERNEL_STACK[app_id].push_context( - TrapContext::app_init_context(get_base_i(app_id), USER_STACK[app_id].get_sp()), - ) + KERNEL_STACK[app_id].push_context(TrapContext::app_init_context( + get_base_i(app_id), + USER_STACK[app_id].get_sp(), + )) } diff --git a/os/src/main.rs b/os/src/main.rs index c64cece5..db3379ec 100644 --- a/os/src/main.rs +++ b/os/src/main.rs @@ -6,14 +6,14 @@ use core::arch::global_asm; #[macro_use] mod console; +mod config; mod lang_items; +mod loader; mod sbi; +mod sync; mod syscall; -mod trap; -mod loader; -mod config; mod task; -mod sync; +mod trap; global_asm!(include_str!("entry.asm")); global_asm!(include_str!("link_app.S")); @@ -24,10 +24,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); } } 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 d64bc73c..804c94b4 100644 --- a/os/src/syscall/mod.rs +++ b/os/src/syscall/mod.rs @@ -17,4 +17,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 f8701599..55912373 100644 --- a/os/src/syscall/process.rs +++ b/os/src/syscall/process.rs @@ -1,7 +1,4 @@ -use crate::task::{ - suspend_current_and_run_next, - exit_current_and_run_next, -}; +use crate::task::{exit_current_and_run_next, suspend_current_and_run_next}; pub fn sys_exit(exit_code: i32) -> ! { println!("[kernel] Application exited with code {}", exit_code); @@ -12,4 +9,4 @@ pub fn sys_exit(exit_code: i32) -> ! { pub fn sys_yield() -> isize { suspend_current_and_run_next(); 0 -} \ No newline at end of file +} diff --git a/os/src/task/context.rs b/os/src/task/context.rs index 35055d0c..375042d8 100644 --- a/os/src/task/context.rs +++ b/os/src/task/context.rs @@ -15,7 +15,9 @@ impl TaskContext { } } pub fn goto_restore(kstack_ptr: usize) -> Self { - extern "C" { fn __restore(); } + extern "C" { + fn __restore(); + } Self { ra: __restore as usize, sp: kstack_ptr, @@ -23,4 +25,3 @@ impl TaskContext { } } } - diff --git a/os/src/task/mod.rs b/os/src/task/mod.rs index 66c8a796..6f6b54cf 100644 --- a/os/src/task/mod.rs +++ b/os/src/task/mod.rs @@ -1,13 +1,15 @@ mod context; mod switch; + +#[allow(clippy::module_inception)] mod task; use crate::config::MAX_APP_NUM; use crate::loader::{get_num_app, init_app_cx}; +use crate::sync::UPSafeCell; use lazy_static::*; use switch::__switch; use task::{TaskControlBlock, TaskStatus}; -use crate::sync::UPSafeCell; pub use context::TaskContext; @@ -24,23 +26,22 @@ struct TaskManagerInner { lazy_static! { pub static ref TASK_MANAGER: TaskManager = { let num_app = get_num_app(); - let mut tasks = [ - TaskControlBlock { - task_cx: TaskContext::zero_init(), - task_status: TaskStatus::UnInit - }; - MAX_APP_NUM - ]; - for i in 0..num_app { - tasks[i].task_cx = TaskContext::goto_restore(init_app_cx(i)); - tasks[i].task_status = TaskStatus::Ready; + let mut tasks = [TaskControlBlock { + task_cx: TaskContext::zero_init(), + task_status: TaskStatus::UnInit, + }; MAX_APP_NUM]; + for (i, task) in tasks.iter_mut().enumerate() { + task.task_cx = TaskContext::goto_restore(init_app_cx(i)); + task.task_status = TaskStatus::Ready; } TaskManager { num_app, - inner: unsafe { UPSafeCell::new(TaskManagerInner { - tasks, - current_task: 0, - })}, + inner: unsafe { + UPSafeCell::new(TaskManagerInner { + tasks, + current_task: 0, + }) + }, } }; } @@ -55,10 +56,7 @@ impl TaskManager { let mut _unused = TaskContext::zero_init(); // before this, we should drop local variables that must be dropped manually unsafe { - __switch( - &mut _unused as *mut TaskContext, - next_task_cx_ptr, - ); + __switch(&mut _unused as *mut TaskContext, next_task_cx_ptr); } panic!("unreachable in run_first_task!"); } @@ -80,9 +78,7 @@ impl TaskManager { let current = inner.current_task; (current + 1..current + self.num_app + 1) .map(|id| id % self.num_app) - .find(|id| { - inner.tasks[*id].task_status == TaskStatus::Ready - }) + .find(|id| inner.tasks[*id].task_status == TaskStatus::Ready) } fn run_next_task(&self) { @@ -96,10 +92,7 @@ impl TaskManager { drop(inner); // before this, we should drop local variables that must be dropped manually unsafe { - __switch( - current_task_cx_ptr, - next_task_cx_ptr, - ); + __switch(current_task_cx_ptr, next_task_cx_ptr); } // go back to user mode } else { @@ -132,4 +125,4 @@ pub fn suspend_current_and_run_next() { pub fn exit_current_and_run_next() { mark_current_exited(); run_next_task(); -} \ No newline at end of file +} diff --git a/os/src/task/switch.rs b/os/src/task/switch.rs index dd3a2d3e..59f8b1a0 100644 --- a/os/src/task/switch.rs +++ b/os/src/task/switch.rs @@ -4,8 +4,5 @@ use core::arch::global_asm; global_asm!(include_str!("switch.S")); extern "C" { - pub fn __switch( - current_task_cx_ptr: *mut TaskContext, - next_task_cx_ptr: *const TaskContext - ); + pub fn __switch(current_task_cx_ptr: *mut TaskContext, next_task_cx_ptr: *const TaskContext); } diff --git a/os/src/task/task.rs b/os/src/task/task.rs index fd5f5f9c..43c4f9c2 100644 --- a/os/src/task/task.rs +++ b/os/src/task/task.rs @@ -12,4 +12,4 @@ pub enum TaskStatus { Ready, Running, Exited, -} \ 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 588339a3..db33a18a 100644 --- a/os/src/trap/mod.rs +++ b/os/src/trap/mod.rs @@ -1,22 +1,19 @@ mod context; +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 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); } @@ -31,8 +28,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, bad addr = {:#x}, bad instruction = {:#x}, kernel killed it.", stval, cx.sepc); panic!("[kernel] Cannot continue!"); //run_next_app(); @@ -43,7 +39,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/00write_a.rs b/user/src/bin/00write_a.rs index 979ec0bc..8bebc9d0 100644 --- a/user/src/bin/00write_a.rs +++ b/user/src/bin/00write_a.rs @@ -12,10 +12,12 @@ const HEIGHT: usize = 5; #[no_mangle] fn main() -> i32 { for i in 0..HEIGHT { - for _ in 0..WIDTH { print!("A"); } + for _ in 0..WIDTH { + print!("A"); + } println!(" [{}/{}]", i + 1, HEIGHT); yield_(); } println!("Test write_a OK!"); 0 -} \ No newline at end of file +} diff --git a/user/src/bin/01write_b.rs b/user/src/bin/01write_b.rs index 7691b315..652d5dd5 100644 --- a/user/src/bin/01write_b.rs +++ b/user/src/bin/01write_b.rs @@ -12,7 +12,9 @@ const HEIGHT: usize = 2; #[no_mangle] fn main() -> i32 { for i in 0..HEIGHT { - for _ in 0..WIDTH { print!("B"); } + for _ in 0..WIDTH { + print!("B"); + } println!(" [{}/{}]", i + 1, HEIGHT); yield_(); } diff --git a/user/src/bin/02write_c.rs b/user/src/bin/02write_c.rs index 6f342d12..9bed4da1 100644 --- a/user/src/bin/02write_c.rs +++ b/user/src/bin/02write_c.rs @@ -12,7 +12,9 @@ const HEIGHT: usize = 3; #[no_mangle] fn main() -> i32 { for i in 0..HEIGHT { - for _ in 0..WIDTH { print!("C"); } + for _ in 0..WIDTH { + print!("C"); + } println!(" [{}/{}]", i + 1, HEIGHT); yield_(); } 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 c8fabfee..44a43f79 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,13 +26,19 @@ 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 yield_() -> isize { sys_yield() } \ No newline at end of file +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 yield_() -> isize { + sys_yield() +}