add cargo fmt in Makefile, and exec make fmt
This commit is contained in:
parent
daf439cff4
commit
3aa7fac88a
17 changed files with 57 additions and 63 deletions
2
Makefile
2
Makefile
|
@ -6,3 +6,5 @@ docker:
|
||||||
|
|
||||||
build_docker:
|
build_docker:
|
||||||
docker build -t ${DOCKER_NAME} .
|
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 ..
|
|
@ -3,4 +3,3 @@ pub const CLOCK_FREQ: usize = 12500000;
|
||||||
pub const MMIO: &[(usize, usize)] = &[(0x10001000, 0x1000)];
|
pub const MMIO: &[(usize, usize)] = &[(0x10001000, 0x1000)];
|
||||||
|
|
||||||
pub type BlockDeviceImpl = crate::drivers::block::VirtIOBlock;
|
pub type BlockDeviceImpl = crate::drivers::block::VirtIOBlock;
|
||||||
|
|
||||||
|
|
|
@ -12,4 +12,3 @@ pub const TRAMPOLINE: usize = usize::MAX - PAGE_SIZE + 1;
|
||||||
pub const TRAP_CONTEXT: usize = TRAMPOLINE - PAGE_SIZE;
|
pub const TRAP_CONTEXT: usize = TRAMPOLINE - PAGE_SIZE;
|
||||||
|
|
||||||
pub use crate::board::{CLOCK_FREQ, MMIO};
|
pub use crate::board::{CLOCK_FREQ, MMIO};
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
mod sdcard;
|
mod sdcard;
|
||||||
mod virtio_blk;
|
mod virtio_blk;
|
||||||
|
|
||||||
pub use virtio_blk::VirtIOBlock;
|
|
||||||
pub use sdcard::SDCardWrapper;
|
pub use sdcard::SDCardWrapper;
|
||||||
|
pub use virtio_blk::VirtIOBlock;
|
||||||
|
|
||||||
|
use crate::board::BlockDeviceImpl;
|
||||||
use alloc::sync::Arc;
|
use alloc::sync::Arc;
|
||||||
use easy_fs::BlockDevice;
|
use easy_fs::BlockDevice;
|
||||||
use lazy_static::*;
|
use lazy_static::*;
|
||||||
use crate::board::BlockDeviceImpl;
|
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
pub static ref BLOCK_DEVICE: Arc<dyn BlockDevice> = Arc::new(BlockDeviceImpl::new());
|
pub static ref BLOCK_DEVICE: Arc<dyn BlockDevice> = Arc::new(BlockDeviceImpl::new());
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
//! `Arc<Inode>` -> `OSInodeInner`: In order to open files concurrently
|
//! `Arc<Inode>` -> `OSInodeInner`: In order to open files concurrently
|
||||||
//! we need to wrap `Inode` into `Arc`,but `Mutex` in `Inode` prevents
|
//! we need to wrap `Inode` into `Arc`,but `Mutex` in `Inode` prevents
|
||||||
//! file systems from being accessed simultaneously
|
//! file systems from being accessed simultaneously
|
||||||
//!
|
//!
|
||||||
//! `UPSafeCell<OSInodeInner>` -> `OSInode`: for static `ROOT_INODE`,we
|
//! `UPSafeCell<OSInodeInner>` -> `OSInode`: for static `ROOT_INODE`,we
|
||||||
//! need to wrap `OSInodeInner` into `UPSafeCell`
|
//! need to wrap `OSInodeInner` into `UPSafeCell`
|
||||||
use super::File;
|
use super::File;
|
||||||
use crate::drivers::BLOCK_DEVICE;
|
use crate::drivers::BLOCK_DEVICE;
|
||||||
|
@ -14,7 +14,7 @@ use bitflags::*;
|
||||||
use easy_fs::{EasyFileSystem, Inode};
|
use easy_fs::{EasyFileSystem, Inode};
|
||||||
use lazy_static::*;
|
use lazy_static::*;
|
||||||
/// A wrapper around a filesystem inode
|
/// A wrapper around a filesystem inode
|
||||||
/// to implement File trait atop
|
/// to implement File trait atop
|
||||||
pub struct OSInode {
|
pub struct OSInode {
|
||||||
readable: bool,
|
readable: bool,
|
||||||
writable: bool,
|
writable: bool,
|
||||||
|
@ -67,7 +67,7 @@ pub fn list_apps() {
|
||||||
println!("**************/");
|
println!("**************/");
|
||||||
}
|
}
|
||||||
|
|
||||||
bitflags! {
|
bitflags! {
|
||||||
///Open file flags
|
///Open file flags
|
||||||
pub struct OpenFlags: u32 {
|
pub struct OpenFlags: u32 {
|
||||||
///Read only
|
///Read only
|
||||||
|
|
|
@ -3,7 +3,7 @@ mod inode;
|
||||||
mod stdio;
|
mod stdio;
|
||||||
|
|
||||||
use crate::mm::UserBuffer;
|
use crate::mm::UserBuffer;
|
||||||
/// File trait
|
/// File trait
|
||||||
pub trait File: Send + Sync {
|
pub trait File: Send + Sync {
|
||||||
/// If readable
|
/// If readable
|
||||||
fn readable(&self) -> bool;
|
fn readable(&self) -> bool;
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
//! - [`mm`]: Address map using SV39
|
//! - [`mm`]: Address map using SV39
|
||||||
//! - [`sync`]: Wrap a static data structure inside it so that we are able to access it without any `unsafe`.
|
//! - [`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
|
//! - [`fs`]: Separate user from file system with some structures
|
||||||
//!
|
//!
|
||||||
//! The operating system also starts in this module. Kernel code starts
|
//! The operating system also starts in this module. Kernel code starts
|
||||||
//! executing from `entry.asm`, after which [`rust_main()`] is called to
|
//! executing from `entry.asm`, after which [`rust_main()`] is called to
|
||||||
//! initialize various pieces of functionality. (See its source code for
|
//! initialize various pieces of functionality. (See its source code for
|
||||||
|
|
|
@ -109,11 +109,11 @@ impl VirtAddr {
|
||||||
pub fn ceil(&self) -> VirtPageNum {
|
pub fn ceil(&self) -> VirtPageNum {
|
||||||
VirtPageNum((self.0 - 1 + PAGE_SIZE) / PAGE_SIZE)
|
VirtPageNum((self.0 - 1 + PAGE_SIZE) / PAGE_SIZE)
|
||||||
}
|
}
|
||||||
///Get page offset
|
///Get page offset
|
||||||
pub fn page_offset(&self) -> usize {
|
pub fn page_offset(&self) -> usize {
|
||||||
self.0 & (PAGE_SIZE - 1)
|
self.0 & (PAGE_SIZE - 1)
|
||||||
}
|
}
|
||||||
///Check page aligned
|
///Check page aligned
|
||||||
pub fn aligned(&self) -> bool {
|
pub fn aligned(&self) -> bool {
|
||||||
self.page_offset() == 0
|
self.page_offset() == 0
|
||||||
}
|
}
|
||||||
|
@ -138,11 +138,11 @@ impl PhysAddr {
|
||||||
pub fn ceil(&self) -> PhysPageNum {
|
pub fn ceil(&self) -> PhysPageNum {
|
||||||
PhysPageNum((self.0 - 1 + PAGE_SIZE) / PAGE_SIZE)
|
PhysPageNum((self.0 - 1 + PAGE_SIZE) / PAGE_SIZE)
|
||||||
}
|
}
|
||||||
///Get page offset
|
///Get page offset
|
||||||
pub fn page_offset(&self) -> usize {
|
pub fn page_offset(&self) -> usize {
|
||||||
self.0 & (PAGE_SIZE - 1)
|
self.0 & (PAGE_SIZE - 1)
|
||||||
}
|
}
|
||||||
///Check page aligned
|
///Check page aligned
|
||||||
pub fn aligned(&self) -> bool {
|
pub fn aligned(&self) -> bool {
|
||||||
self.page_offset() == 0
|
self.page_offset() == 0
|
||||||
}
|
}
|
||||||
|
@ -160,7 +160,7 @@ impl From<PhysPageNum> for PhysAddr {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VirtPageNum {
|
impl VirtPageNum {
|
||||||
///Return VPN 3 level index
|
///Return VPN 3 level index
|
||||||
pub fn indexes(&self) -> [usize; 3] {
|
pub fn indexes(&self) -> [usize; 3] {
|
||||||
let mut vpn = self.0;
|
let mut vpn = self.0;
|
||||||
let mut idx = [0usize; 3];
|
let mut idx = [0usize; 3];
|
||||||
|
@ -173,11 +173,11 @@ impl VirtPageNum {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PhysAddr {
|
impl PhysAddr {
|
||||||
///Get reference to `PhysAddr` value
|
///Get reference to `PhysAddr` value
|
||||||
pub fn get_ref<T>(&self) -> &'static T {
|
pub fn get_ref<T>(&self) -> &'static T {
|
||||||
unsafe { (self.0 as *const T).as_ref().unwrap() }
|
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<T>(&self) -> &'static mut T {
|
pub fn get_mut<T>(&self) -> &'static mut T {
|
||||||
unsafe { (self.0 as *mut T).as_mut().unwrap() }
|
unsafe { (self.0 as *mut T).as_mut().unwrap() }
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
//! Implementation of [`FrameAllocator`] which
|
//! Implementation of [`FrameAllocator`] which
|
||||||
//! controls all the frames in the operating system.
|
//! controls all the frames in the operating system.
|
||||||
use super::{PhysAddr, PhysPageNum};
|
use super::{PhysAddr, PhysPageNum};
|
||||||
use crate::config::MEMORY_END;
|
use crate::config::MEMORY_END;
|
||||||
|
|
|
@ -389,26 +389,20 @@ pub fn remap_test() {
|
||||||
let mid_text: VirtAddr = ((stext as usize + etext as usize) / 2).into();
|
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_rodata: VirtAddr = ((srodata as usize + erodata as usize) / 2).into();
|
||||||
let mid_data: VirtAddr = ((sdata as usize + edata as usize) / 2).into();
|
let mid_data: VirtAddr = ((sdata as usize + edata as usize) / 2).into();
|
||||||
assert!(
|
assert!(!kernel_space
|
||||||
!kernel_space
|
.page_table
|
||||||
.page_table
|
.translate(mid_text.floor())
|
||||||
.translate(mid_text.floor())
|
.unwrap()
|
||||||
.unwrap()
|
.writable(),);
|
||||||
.writable(),
|
assert!(!kernel_space
|
||||||
);
|
.page_table
|
||||||
assert!(
|
.translate(mid_rodata.floor())
|
||||||
!kernel_space
|
.unwrap()
|
||||||
.page_table
|
.writable(),);
|
||||||
.translate(mid_rodata.floor())
|
assert!(!kernel_space
|
||||||
.unwrap()
|
.page_table
|
||||||
.writable(),
|
.translate(mid_data.floor())
|
||||||
);
|
.unwrap()
|
||||||
assert!(
|
.executable(),);
|
||||||
!kernel_space
|
|
||||||
.page_table
|
|
||||||
.translate(mid_data.floor())
|
|
||||||
.unwrap()
|
|
||||||
.executable(),
|
|
||||||
);
|
|
||||||
println!("remap_test passed!");
|
println!("remap_test passed!");
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
//! Memory management implementation
|
//! Memory management implementation
|
||||||
//!
|
//!
|
||||||
//! SV39 page-based virtual-memory architecture for RV64 systems, and
|
//! SV39 page-based virtual-memory architecture for RV64 systems, and
|
||||||
//! everything about memory management, like frame allocator, page table,
|
//! everything about memory management, like frame allocator, page table,
|
||||||
//! map area and memory set, is implemented here.
|
//! map area and memory set, is implemented here.
|
||||||
//!
|
//!
|
||||||
//! Every task or process has a memory_set to control its virtual memory.
|
//! Every task or process has a memory_set to control its virtual memory.
|
||||||
mod address;
|
mod address;
|
||||||
mod frame_allocator;
|
mod frame_allocator;
|
||||||
|
|
|
@ -37,7 +37,7 @@ impl PageTableEntry {
|
||||||
pub fn empty() -> Self {
|
pub fn empty() -> Self {
|
||||||
PageTableEntry { bits: 0 }
|
PageTableEntry { bits: 0 }
|
||||||
}
|
}
|
||||||
///Return 44bit ppn
|
///Return 44bit ppn
|
||||||
pub fn ppn(&self) -> PhysPageNum {
|
pub fn ppn(&self) -> PhysPageNum {
|
||||||
(self.bits >> 10 & ((1usize << 44) - 1)).into()
|
(self.bits >> 10 & ((1usize << 44) - 1)).into()
|
||||||
}
|
}
|
||||||
|
@ -150,13 +150,13 @@ impl PageTable {
|
||||||
(aligned_pa_usize + offset).into()
|
(aligned_pa_usize + offset).into()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
/// Get root ppn
|
/// Get root ppn
|
||||||
pub fn token(&self) -> usize {
|
pub fn token(&self) -> usize {
|
||||||
8usize << 60 | self.root_ppn.0
|
8usize << 60 | self.root_ppn.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Translate a pointer to a mutable u8 Vec through page table
|
/// 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]> {
|
pub fn translated_byte_buffer(token: usize, ptr: *const u8, len: usize) -> Vec<&'static mut [u8]> {
|
||||||
let page_table = PageTable::from_token(token);
|
let page_table = PageTable::from_token(token);
|
||||||
let mut start = ptr as usize;
|
let mut start = ptr as usize;
|
||||||
let end = start + len;
|
let end = start + len;
|
||||||
|
|
|
@ -5,13 +5,13 @@
|
||||||
//!
|
//!
|
||||||
//! A single global instance of [`TaskManager`] called `TASK_MANAGER` controls
|
//! A single global instance of [`TaskManager`] called `TASK_MANAGER` controls
|
||||||
//! all the tasks in the whole operating system.
|
//! all the tasks in the whole operating system.
|
||||||
//!
|
//!
|
||||||
//! A single global instance of [`Processor`] called `PROCESSOR` monitors running
|
//! A single global instance of [`Processor`] called `PROCESSOR` monitors running
|
||||||
//! task(s) for each core.
|
//! task(s) for each core.
|
||||||
//!
|
//!
|
||||||
//! A single global instance of [`PidAllocator`] called `PID_ALLOCATOR` allocates
|
//! A single global instance of [`PidAllocator`] called `PID_ALLOCATOR` allocates
|
||||||
//! pid for user apps.
|
//! pid for user apps.
|
||||||
//!
|
//!
|
||||||
//! Be careful when you see `__switch` ASM function in `switch.S`. Control flow around this function
|
//! Be careful when you see `__switch` ASM function in `switch.S`. Control flow around this function
|
||||||
//! might not be what you expect.
|
//! might not be what you expect.
|
||||||
mod context;
|
mod context;
|
||||||
|
@ -27,14 +27,15 @@ use crate::fs::{open_file, OpenFlags};
|
||||||
use alloc::sync::Arc;
|
use alloc::sync::Arc;
|
||||||
pub use context::TaskContext;
|
pub use context::TaskContext;
|
||||||
use lazy_static::*;
|
use lazy_static::*;
|
||||||
pub use manager::{fetch_task,TaskManager};
|
pub use manager::{fetch_task, TaskManager};
|
||||||
use switch::__switch;
|
use switch::__switch;
|
||||||
use task::{TaskControlBlock, TaskStatus};
|
use task::{TaskControlBlock, TaskStatus};
|
||||||
|
|
||||||
pub use manager::add_task;
|
pub use manager::add_task;
|
||||||
pub use pid::{pid_alloc, KernelStack, PidHandle,PidAllocator};
|
pub use pid::{pid_alloc, KernelStack, PidAllocator, PidHandle};
|
||||||
pub use processor::{
|
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.
|
/// Suspend the current 'Running' task and run the next task in task list.
|
||||||
pub fn suspend_current_and_run_next() {
|
pub fn suspend_current_and_run_next() {
|
||||||
|
@ -89,7 +90,7 @@ pub fn exit_current_and_run_next(exit_code: i32) {
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
///Globle process that init user shell
|
///Globle process that init user shell
|
||||||
pub static ref INITPROC: Arc<TaskControlBlock> = Arc::new({
|
pub static ref INITPROC: Arc<TaskControlBlock> = Arc::new({
|
||||||
let inode = open_file("initproc", OpenFlags::RDONLY).unwrap();
|
let inode = open_file("initproc", OpenFlags::RDONLY).unwrap();
|
||||||
let v = inode.read_all();
|
let v = inode.read_all();
|
||||||
|
|
|
@ -63,7 +63,7 @@ pub fn kernel_stack_position(app_id: usize) -> (usize, usize) {
|
||||||
let bottom = top - KERNEL_STACK_SIZE;
|
let bottom = top - KERNEL_STACK_SIZE;
|
||||||
(bottom, top)
|
(bottom, top)
|
||||||
}
|
}
|
||||||
///Kernelstack for app
|
///Kernelstack for app
|
||||||
pub struct KernelStack {
|
pub struct KernelStack {
|
||||||
pid: usize,
|
pid: usize,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
//!Implementation of [`TaskControlBlock`]
|
//!Implementation of [`TaskControlBlock`]
|
||||||
use super::TaskContext;
|
use super::TaskContext;
|
||||||
use super::{pid_alloc, KernelStack, PidHandle};
|
use super::{pid_alloc, KernelStack, PidHandle};
|
||||||
use crate::config::TRAP_CONTEXT;
|
use crate::config::TRAP_CONTEXT;
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
//! RISC-V timer-related functionality
|
//! RISC-V timer-related functionality
|
||||||
|
|
||||||
use crate::config::CLOCK_FREQ;
|
use crate::config::CLOCK_FREQ;
|
||||||
|
|
|
@ -106,7 +106,7 @@ pub fn trap_handler() -> ! {
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
/// set the new addr of __restore asm function in TRAMPOLINE page,
|
/// 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,
|
/// 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() -> ! {
|
pub fn trap_return() -> ! {
|
||||||
set_user_trap_entry();
|
set_user_trap_entry();
|
||||||
let trap_cx_ptr = TRAP_CONTEXT;
|
let trap_cx_ptr = TRAP_CONTEXT;
|
||||||
|
@ -130,7 +130,7 @@ pub fn trap_return() -> ! {
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
/// Unimplement: traps/interrupts/exceptions from kernel mode
|
/// Unimplement: traps/interrupts/exceptions from kernel mode
|
||||||
/// Todo: Chapter 9: I/O device
|
/// Todo: Chapter 9: I/O device
|
||||||
pub fn trap_from_kernel() -> ! {
|
pub fn trap_from_kernel() -> ! {
|
||||||
use riscv::register::sepc;
|
use riscv::register::sepc;
|
||||||
println!("stval = {:#x}, sepc = {:#x}", stval::read(), sepc::read());
|
println!("stval = {:#x}, sepc = {:#x}", stval::read(), sepc::read());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue