add cargo fmt in Makefile, and exec make fmt

This commit is contained in:
Yu Chen 2022-05-20 08:54:07 +08:00
parent 3fd8b8e94d
commit 21c3c492ac
17 changed files with 63 additions and 64 deletions

View File

@ -6,3 +6,5 @@ docker:
build_docker:
docker build -t ${DOCKER_NAME} .
fmt:
cd os ; cargo fmt; cd ../user; cargo fmt; cd ..

View File

@ -1,3 +1,2 @@
pub const CLOCK_FREQ: usize = 403000000 / 62;
pub const MEMORY_END: usize = 0x80600000;

View File

@ -1,3 +1,2 @@
pub const CLOCK_FREQ: usize = 12500000;
pub const MEMORY_END: usize = 0x80800000;

View File

@ -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};

View File

@ -19,7 +19,6 @@
#![deny(missing_docs)]
#![deny(warnings)]
#![no_std]
#![no_main]
#![feature(panic_info_message)]

View File

@ -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<PhysPageNum> 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<T>(&self) -> &'static mut T {
unsafe { (self.0 as *mut T).as_mut().unwrap() }
}

View File

@ -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;

View File

@ -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!");
}

View File

@ -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;

View File

@ -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<T>(token: usize, ptr: *mut T) -> &'static mut T {
}
///translate a generic through page table and return a mutable reference
pub fn translated_refmut<T>(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()
}
}

View File

@ -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<TaskControlBlock> = Arc::new(TaskControlBlock::new(
get_app_data_by_name("initproc").unwrap()
));

View File

@ -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,
}

View File

@ -1,4 +1,4 @@
//!Implementation of [`TaskControlBlock`]
//!Implementation of [`TaskControlBlock`]
use super::TaskContext;
use super::{pid_alloc, KernelStack, PidHandle};
use crate::config::TRAP_CONTEXT;

View File

@ -1,4 +1,3 @@
//! RISC-V timer-related functionality
use crate::config::CLOCK_FREQ;

View File

@ -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());
}

View File

@ -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
}
}

View File

@ -4,7 +4,7 @@
#[macro_use]
extern crate user_lib;
use user_lib::{getpid};
use user_lib::getpid;
#[no_mangle]
pub fn main() -> i32 {