From 21c3c492ac5666ce003a5380b163950489708447 Mon Sep 17 00:00:00 2001 From: Yu Chen Date: Fri, 20 May 2022 08:54:07 +0800 Subject: [PATCH] add cargo fmt in Makefile, and exec make fmt --- Makefile | 2 ++ os/src/boards/k210.rs | 1 - os/src/boards/qemu.rs | 1 - os/src/config.rs | 1 - os/src/main.rs | 1 - os/src/mm/address.rs | 12 ++++++------ os/src/mm/frame_allocator.rs | 2 +- os/src/mm/memory_set.rs | 36 +++++++++++++++--------------------- os/src/mm/mod.rs | 4 ++-- os/src/mm/page_table.rs | 20 ++++++++++---------- os/src/task/mod.rs | 22 +++++++++++----------- os/src/task/pid.rs | 2 +- os/src/task/task.rs | 2 +- os/src/timer.rs | 1 - os/src/trap/mod.rs | 4 ++-- user/src/bin/forkexec.rs | 14 +++++++++++--- user/src/bin/hello_world.rs | 2 +- 17 files changed, 63 insertions(+), 64 deletions(-) diff --git a/Makefile b/Makefile index 2e339762..c67d48a1 100644 --- a/Makefile +++ b/Makefile @@ -6,3 +6,5 @@ docker: build_docker: docker build -t ${DOCKER_NAME} . +fmt: + cd os ; cargo fmt; cd ../user; cargo fmt; cd .. \ No newline at end of file diff --git a/os/src/boards/k210.rs b/os/src/boards/k210.rs index ba3d6cc5..5e7e047e 100644 --- a/os/src/boards/k210.rs +++ b/os/src/boards/k210.rs @@ -1,3 +1,2 @@ pub const CLOCK_FREQ: usize = 403000000 / 62; pub const MEMORY_END: usize = 0x80600000; - diff --git a/os/src/boards/qemu.rs b/os/src/boards/qemu.rs index 00789d24..a00a6835 100644 --- a/os/src/boards/qemu.rs +++ b/os/src/boards/qemu.rs @@ -1,3 +1,2 @@ pub const CLOCK_FREQ: usize = 12500000; pub const MEMORY_END: usize = 0x80800000; - diff --git a/os/src/config.rs b/os/src/config.rs index e808e4b2..8d6d0458 100644 --- a/os/src/config.rs +++ b/os/src/config.rs @@ -10,4 +10,3 @@ pub const TRAMPOLINE: usize = usize::MAX - PAGE_SIZE + 1; pub const TRAP_CONTEXT: usize = TRAMPOLINE - PAGE_SIZE; pub use crate::board::{CLOCK_FREQ, MEMORY_END}; - diff --git a/os/src/main.rs b/os/src/main.rs index fef55a77..28e57ae5 100644 --- a/os/src/main.rs +++ b/os/src/main.rs @@ -19,7 +19,6 @@ #![deny(missing_docs)] #![deny(warnings)] - #![no_std] #![no_main] #![feature(panic_info_message)] diff --git a/os/src/mm/address.rs b/os/src/mm/address.rs index 76f06364..be303b5c 100644 --- a/os/src/mm/address.rs +++ b/os/src/mm/address.rs @@ -102,11 +102,11 @@ impl VirtAddr { pub fn ceil(&self) -> VirtPageNum { VirtPageNum((self.0 - 1 + PAGE_SIZE) / PAGE_SIZE) } - ///Get page offset + ///Get page offset pub fn page_offset(&self) -> usize { self.0 & (PAGE_SIZE - 1) } - ///Check page aligned + ///Check page aligned pub fn aligned(&self) -> bool { self.page_offset() == 0 } @@ -131,11 +131,11 @@ impl PhysAddr { pub fn ceil(&self) -> PhysPageNum { PhysPageNum((self.0 - 1 + PAGE_SIZE) / PAGE_SIZE) } - ///Get page offset + ///Get page offset pub fn page_offset(&self) -> usize { self.0 & (PAGE_SIZE - 1) } - ///Check page aligned + ///Check page aligned pub fn aligned(&self) -> bool { self.page_offset() == 0 } @@ -153,7 +153,7 @@ impl From for PhysAddr { } impl VirtPageNum { - ///Return VPN 3 level index + ///Return VPN 3 level index pub fn indexes(&self) -> [usize; 3] { let mut vpn = self.0; let mut idx = [0usize; 3]; @@ -166,7 +166,7 @@ impl VirtPageNum { } impl PhysAddr { - ///Get mutable reference to `PhysAddr` value + ///Get mutable reference to `PhysAddr` value pub fn get_mut(&self) -> &'static mut T { unsafe { (self.0 as *mut T).as_mut().unwrap() } } diff --git a/os/src/mm/frame_allocator.rs b/os/src/mm/frame_allocator.rs index ab3cebf1..3d0ec225 100644 --- a/os/src/mm/frame_allocator.rs +++ b/os/src/mm/frame_allocator.rs @@ -1,4 +1,4 @@ -//! Implementation of [`FrameAllocator`] which +//! Implementation of [`FrameAllocator`] which //! controls all the frames in the operating system. use super::{PhysAddr, PhysPageNum}; use crate::config::MEMORY_END; diff --git a/os/src/mm/memory_set.rs b/os/src/mm/memory_set.rs index 779fff3f..917bb655 100644 --- a/os/src/mm/memory_set.rs +++ b/os/src/mm/memory_set.rs @@ -373,26 +373,20 @@ pub fn remap_test() { let mid_text: VirtAddr = ((stext as usize + etext as usize) / 2).into(); let mid_rodata: VirtAddr = ((srodata as usize + erodata as usize) / 2).into(); let mid_data: VirtAddr = ((sdata as usize + edata as usize) / 2).into(); - assert!( - !kernel_space - .page_table - .translate(mid_text.floor()) - .unwrap() - .writable(), - ); - assert!( - !kernel_space - .page_table - .translate(mid_rodata.floor()) - .unwrap() - .writable(), - ); - assert!( - !kernel_space - .page_table - .translate(mid_data.floor()) - .unwrap() - .executable(), - ); + assert!(!kernel_space + .page_table + .translate(mid_text.floor()) + .unwrap() + .writable(),); + assert!(!kernel_space + .page_table + .translate(mid_rodata.floor()) + .unwrap() + .writable(),); + assert!(!kernel_space + .page_table + .translate(mid_data.floor()) + .unwrap() + .executable(),); println!("remap_test passed!"); } diff --git a/os/src/mm/mod.rs b/os/src/mm/mod.rs index 302b87fc..d216861c 100644 --- a/os/src/mm/mod.rs +++ b/os/src/mm/mod.rs @@ -1,9 +1,9 @@ //! Memory management implementation -//! +//! //! SV39 page-based virtual-memory architecture for RV64 systems, and //! everything about memory management, like frame allocator, page table, //! map area and memory set, is implemented here. -//! +//! //! Every task or process has a memory_set to control its virtual memory. mod address; mod frame_allocator; diff --git a/os/src/mm/page_table.rs b/os/src/mm/page_table.rs index a35dfc50..e2972b4f 100644 --- a/os/src/mm/page_table.rs +++ b/os/src/mm/page_table.rs @@ -37,7 +37,7 @@ impl PageTableEntry { pub fn empty() -> Self { PageTableEntry { bits: 0 } } - ///Return 44bit ppn + ///Return 44bit ppn pub fn ppn(&self) -> PhysPageNum { (self.bits >> 10 & ((1usize << 44) - 1)).into() } @@ -149,8 +149,8 @@ impl PageTable { 8usize << 60 | self.root_ppn.0 } } - /// translate a pointer to a mutable u8 Vec through page table - pub fn translated_byte_buffer(token: usize, ptr: *const u8, len: usize) -> Vec<&'static mut [u8]> { +/// translate a pointer to a mutable u8 Vec through page table +pub fn translated_byte_buffer(token: usize, ptr: *const u8, len: usize) -> Vec<&'static mut [u8]> { let page_table = PageTable::from_token(token); let mut start = ptr as usize; let end = start + len; @@ -170,9 +170,9 @@ impl PageTable { start = end_va.into(); } v - } - /// translate a pointer to a mutable u8 Vec end with `\0` through page table to a `String` - pub fn translated_str(token: usize, ptr: *const u8) -> String { +} +/// translate a pointer to a mutable u8 Vec end with `\0` through page table to a `String` +pub fn translated_str(token: usize, ptr: *const u8) -> String { let page_table = PageTable::from_token(token); let mut string = String::new(); let mut va = ptr as usize; @@ -189,9 +189,9 @@ impl PageTable { } } string - } - ///translate a generic through page table and return a mutable reference - pub fn translated_refmut(token: usize, ptr: *mut T) -> &'static mut T { +} +///translate a generic through page table and return a mutable reference +pub fn translated_refmut(token: usize, ptr: *mut T) -> &'static mut T { //println!("into translated_refmut!"); let page_table = PageTable::from_token(token); let va = ptr as usize; @@ -200,4 +200,4 @@ impl PageTable { .translate_va(VirtAddr::from(va)) .unwrap() .get_mut() - } +} diff --git a/os/src/task/mod.rs b/os/src/task/mod.rs index a53bacc3..483f62f8 100644 --- a/os/src/task/mod.rs +++ b/os/src/task/mod.rs @@ -5,13 +5,13 @@ //! //! A single global instance of [`TaskManager`] called `TASK_MANAGER` controls //! all the tasks in the whole operating system. -//! -//! A single global instance of [`Processor`] called `PROCESSOR` monitors running -//! task(s) for each core. -//! -//! A single global instance of [`PidAllocator`] called `PID_ALLOCATOR` allocates +//! +//! A single global instance of [`Processor`] called `PROCESSOR` monitors running +//! task(s) for each core. +//! +//! A single global instance of [`PidAllocator`] called `PID_ALLOCATOR` allocates //! pid for user apps. -//! +//! //! Be careful when you see `__switch` ASM function in `switch.S`. Control flow around this function //! might not be what you expect. mod context; @@ -20,21 +20,21 @@ mod pid; mod processor; mod switch; #[allow(clippy::module_inception)] - mod task; use crate::loader::get_app_data_by_name; use alloc::sync::Arc; use lazy_static::*; -pub use manager::{fetch_task,TaskManager}; +pub use manager::{fetch_task, TaskManager}; use switch::__switch; use task::{TaskControlBlock, TaskStatus}; pub use context::TaskContext; pub use manager::add_task; -pub use pid::{pid_alloc, KernelStack, PidHandle,PidAllocator}; +pub use pid::{pid_alloc, KernelStack, PidAllocator, PidHandle}; pub use processor::{ - current_task, current_trap_cx, current_user_token, run_tasks, schedule, take_current_task,Processor + current_task, current_trap_cx, current_user_token, run_tasks, schedule, take_current_task, + Processor, }; /// Suspend the current 'Running' task and run the next task in task list. pub fn suspend_current_and_run_next() { @@ -89,7 +89,7 @@ pub fn exit_current_and_run_next(exit_code: i32) { } lazy_static! { - ///Globle process that init user shell + ///Globle process that init user shell pub static ref INITPROC: Arc = Arc::new(TaskControlBlock::new( get_app_data_by_name("initproc").unwrap() )); diff --git a/os/src/task/pid.rs b/os/src/task/pid.rs index 7c8799e6..a7bdea92 100644 --- a/os/src/task/pid.rs +++ b/os/src/task/pid.rs @@ -63,7 +63,7 @@ pub fn kernel_stack_position(app_id: usize) -> (usize, usize) { let bottom = top - KERNEL_STACK_SIZE; (bottom, top) } -///Kernelstack for app +///Kernelstack for app pub struct KernelStack { pid: usize, } diff --git a/os/src/task/task.rs b/os/src/task/task.rs index b45df0d4..5298e318 100644 --- a/os/src/task/task.rs +++ b/os/src/task/task.rs @@ -1,4 +1,4 @@ -//!Implementation of [`TaskControlBlock`] +//!Implementation of [`TaskControlBlock`] use super::TaskContext; use super::{pid_alloc, KernelStack, PidHandle}; use crate::config::TRAP_CONTEXT; diff --git a/os/src/timer.rs b/os/src/timer.rs index a491faaa..50ce53a4 100644 --- a/os/src/timer.rs +++ b/os/src/timer.rs @@ -1,4 +1,3 @@ - //! RISC-V timer-related functionality use crate::config::CLOCK_FREQ; diff --git a/os/src/trap/mod.rs b/os/src/trap/mod.rs index 4e126731..36b006de 100644 --- a/os/src/trap/mod.rs +++ b/os/src/trap/mod.rs @@ -105,7 +105,7 @@ pub fn trap_handler() -> ! { #[no_mangle] /// set the new addr of __restore asm function in TRAMPOLINE page, /// set the reg a0 = trap_cx_ptr, reg a1 = phy addr of usr page table, -/// finally, jump to new addr of __restore asm function +/// finally, jump to new addr of __restore asm function pub fn trap_return() -> ! { set_user_trap_entry(); let trap_cx_ptr = TRAP_CONTEXT; @@ -129,7 +129,7 @@ pub fn trap_return() -> ! { #[no_mangle] /// Unimplement: traps/interrupts/exceptions from kernel mode -/// Todo: Chapter 9: I/O device +/// Todo: Chapter 9: I/O device pub fn trap_from_kernel() -> ! { panic!("a trap {:?} from kernel!", scause::read().cause()); } diff --git a/user/src/bin/forkexec.rs b/user/src/bin/forkexec.rs index 4f55f205..59430311 100644 --- a/user/src/bin/forkexec.rs +++ b/user/src/bin/forkexec.rs @@ -4,7 +4,7 @@ #[macro_use] extern crate user_lib; -use user_lib::{fork, getpid, wait, exec}; +use user_lib::{exec, fork, getpid, wait}; #[no_mangle] pub fn main() -> i32 { @@ -12,7 +12,10 @@ pub fn main() -> i32 { let pid = fork(); if pid == 0 { // child process - println!("pid {}: forked child start execing hello_world app ... ", getpid()); + println!( + "pid {}: forked child start execing hello_world app ... ", + getpid() + ); exec("hello_world"); 100 } else { @@ -21,7 +24,12 @@ pub fn main() -> i32 { println!("pid {}: ready waiting child ...", getpid()); assert_eq!(pid, wait(&mut exit_code)); assert_eq!(exit_code, 0); - println!("pid {}: got child info:: pid {}, exit code: {}", getpid() , pid, exit_code); + println!( + "pid {}: got child info:: pid {}, exit code: {}", + getpid(), + pid, + exit_code + ); 0 } } diff --git a/user/src/bin/hello_world.rs b/user/src/bin/hello_world.rs index 4b370c20..edf7daff 100644 --- a/user/src/bin/hello_world.rs +++ b/user/src/bin/hello_world.rs @@ -4,7 +4,7 @@ #[macro_use] extern crate user_lib; -use user_lib::{getpid}; +use user_lib::getpid; #[no_mangle] pub fn main() -> i32 {