diff --git a/Makefile b/Makefile index 2e339762..a657cd92 100644 --- a/Makefile +++ b/Makefile @@ -6,3 +6,5 @@ docker: build_docker: docker build -t ${DOCKER_NAME} . +fmt: + cd easy-fs; cargo fmt; cd ../easy-fs-fuse cargo fmt; cd ../os ; cargo fmt; cd ../user; cargo fmt; cd .. \ No newline at end of file diff --git a/os/src/boards/qemu.rs b/os/src/boards/qemu.rs index b3492526..b49ccc27 100644 --- a/os/src/boards/qemu.rs +++ b/os/src/boards/qemu.rs @@ -3,4 +3,3 @@ pub const CLOCK_FREQ: usize = 12500000; pub const MMIO: &[(usize, usize)] = &[(0x10001000, 0x1000)]; pub type BlockDeviceImpl = crate::drivers::block::VirtIOBlock; - diff --git a/os/src/config.rs b/os/src/config.rs index 3e349ecc..2af194b0 100644 --- a/os/src/config.rs +++ b/os/src/config.rs @@ -12,4 +12,3 @@ pub const TRAMPOLINE: usize = usize::MAX - PAGE_SIZE + 1; pub const TRAP_CONTEXT: usize = TRAMPOLINE - PAGE_SIZE; pub use crate::board::{CLOCK_FREQ, MMIO}; - diff --git a/os/src/drivers/block/mod.rs b/os/src/drivers/block/mod.rs index 7c1bb55d..7361ec83 100644 --- a/os/src/drivers/block/mod.rs +++ b/os/src/drivers/block/mod.rs @@ -1,13 +1,13 @@ mod sdcard; mod virtio_blk; -pub use virtio_blk::VirtIOBlock; pub use sdcard::SDCardWrapper; +pub use virtio_blk::VirtIOBlock; +use crate::board::BlockDeviceImpl; use alloc::sync::Arc; use easy_fs::BlockDevice; use lazy_static::*; -use crate::board::BlockDeviceImpl; lazy_static! { pub static ref BLOCK_DEVICE: Arc = Arc::new(BlockDeviceImpl::new()); diff --git a/os/src/fs/inode.rs b/os/src/fs/inode.rs index 89f957f7..2490d6ac 100644 --- a/os/src/fs/inode.rs +++ b/os/src/fs/inode.rs @@ -1,8 +1,8 @@ -//! `Arc` -> `OSInodeInner`: In order to open files concurrently -//! we need to wrap `Inode` into `Arc`,but `Mutex` in `Inode` prevents -//! file systems from being accessed simultaneously -//! -//! `UPSafeCell` -> `OSInode`: for static `ROOT_INODE`,we +//! `Arc` -> `OSInodeInner`: In order to open files concurrently +//! we need to wrap `Inode` into `Arc`,but `Mutex` in `Inode` prevents +//! file systems from being accessed simultaneously +//! +//! `UPSafeCell` -> `OSInode`: for static `ROOT_INODE`,we //! need to wrap `OSInodeInner` into `UPSafeCell` use super::File; use crate::drivers::BLOCK_DEVICE; @@ -14,7 +14,7 @@ use bitflags::*; use easy_fs::{EasyFileSystem, Inode}; use lazy_static::*; /// A wrapper around a filesystem inode -/// to implement File trait atop +/// to implement File trait atop pub struct OSInode { readable: bool, writable: bool, @@ -67,7 +67,7 @@ pub fn list_apps() { println!("**************/"); } -bitflags! { +bitflags! { ///Open file flags pub struct OpenFlags: u32 { ///Read only diff --git a/os/src/fs/mod.rs b/os/src/fs/mod.rs index ab91cc1d..6c790fe0 100644 --- a/os/src/fs/mod.rs +++ b/os/src/fs/mod.rs @@ -3,7 +3,7 @@ mod inode; mod stdio; use crate::mm::UserBuffer; -/// File trait +/// File trait pub trait File: Send + Sync { /// If readable fn readable(&self) -> bool; diff --git a/os/src/main.rs b/os/src/main.rs index 091afa7b..5f8811ad 100644 --- a/os/src/main.rs +++ b/os/src/main.rs @@ -9,7 +9,7 @@ //! - [`mm`]: Address map using SV39 //! - [`sync`]: Wrap a static data structure inside it so that we are able to access it without any `unsafe`. //! - [`fs`]: Separate user from file system with some structures -//! +//! //! The operating system also starts in this module. Kernel code starts //! executing from `entry.asm`, after which [`rust_main()`] is called to //! initialize various pieces of functionality. (See its source code for diff --git a/os/src/mm/address.rs b/os/src/mm/address.rs index 81099008..97d29bbe 100644 --- a/os/src/mm/address.rs +++ b/os/src/mm/address.rs @@ -109,11 +109,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 } @@ -138,11 +138,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 } @@ -160,7 +160,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]; @@ -173,11 +173,11 @@ impl VirtPageNum { } impl PhysAddr { - ///Get reference to `PhysAddr` value + ///Get reference to `PhysAddr` value pub fn get_ref(&self) -> &'static T { unsafe { (self.0 as *const T).as_ref().unwrap() } } - ///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 ed398977..f3d18eae 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 9b28bfff..8b2b48f0 100644 --- a/os/src/mm/memory_set.rs +++ b/os/src/mm/memory_set.rs @@ -389,26 +389,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 5ebf92da..65f1e9a2 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 a76e938a..9fbd21bf 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() } @@ -150,13 +150,13 @@ impl PageTable { (aligned_pa_usize + offset).into() }) } - /// Get root ppn + /// Get root ppn pub fn token(&self) -> usize { 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; diff --git a/os/src/task/mod.rs b/os/src/task/mod.rs index 1c5dc90a..81cb5de1 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; @@ -27,14 +27,15 @@ use crate::fs::{open_file, OpenFlags}; use alloc::sync::Arc; pub use context::TaskContext; use lazy_static::*; -pub use manager::{fetch_task,TaskManager}; +pub use manager::{fetch_task, TaskManager}; use switch::__switch; use task::{TaskControlBlock, TaskStatus}; 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 +90,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({ let inode = open_file("initproc", OpenFlags::RDONLY).unwrap(); let v = inode.read_all(); 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 101415b3..9e668dcc 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 f0684401..49b85eca 100644 --- a/os/src/trap/mod.rs +++ b/os/src/trap/mod.rs @@ -106,7 +106,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; @@ -130,7 +130,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() -> ! { use riscv::register::sepc; println!("stval = {:#x}, sepc = {:#x}", stval::read(), sepc::read());