add cargo fmt in Makefile, and exec make fmt

ch9-log
Yu Chen 3 years ago
parent 4a62e17755
commit 5eae9509bb

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

@ -1,10 +1,10 @@
pub const CLOCK_FREQ: usize = 12500000;
pub const MMIO: &[(usize, usize)] = &[
(0x1000_0000, 0x1000), // VIRT_UART0 in virt machine
(0x1000_1000, 0x1000), // VIRT_VIRTIO in virt machine
(0x0C00_0000, 0x40_0000), // VIRT_PLIC in virt machine
(0x0010_0000, 0x00_2000), // VIRT_TEST/RTC in virt machine
(0x1000_0000, 0x1000), // VIRT_UART0 in virt machine
(0x1000_1000, 0x1000), // VIRT_VIRTIO in virt machine
(0x0C00_0000, 0x40_0000), // VIRT_PLIC in virt machine
(0x0010_0000, 0x00_2000), // VIRT_TEST/RTC in virt machine
];
pub type BlockDeviceImpl = crate::drivers::block::VirtIOBlock;
@ -19,7 +19,7 @@ use crate::drivers::plic::{IntrTargetPriority, PLIC};
pub fn device_init() {
use riscv::register::sie;
kprintln!("[KERN] board::qemu::device_init() begin");
let mut plic = unsafe { PLIC::new(VIRT_PLIC) };
let hart_id: usize = 0;
@ -54,8 +54,8 @@ use core::arch::asm;
const EXIT_SUCCESS: u32 = 0x5555; // Equals `exit(0)`. qemu successful exit
const EXIT_FAILURE_FLAG: u32 = 0x3333;
const EXIT_FAILURE: u32 = exit_code_encode(1); // Equals `exit(1)`. qemu failed exit
const EXIT_RESET: u32 = 0x7777; // qemu reset
const EXIT_FAILURE: u32 = exit_code_encode(1); // Equals `exit(1)`. qemu failed exit
const EXIT_RESET: u32 = 0x7777; // qemu reset
pub trait QEMUExit {
/// Exit with specified return code.
@ -72,7 +72,6 @@ pub trait QEMUExit {
fn exit_failure(&self) -> !;
}
/// RISCV64 configuration
pub struct RISCV64 {
/// Address of the sifive_test mapped device.
@ -125,6 +124,6 @@ impl QEMUExit for RISCV64 {
}
}
const VIRT_TEST: u64 =0x100000;
const VIRT_TEST: u64 = 0x100000;
pub const QEMU_EXIT_HANDLE: RISCV64 = RISCV64::new(VIRT_TEST);
pub const QEMU_EXIT_HANDLE: RISCV64 = RISCV64::new(VIRT_TEST);

@ -58,4 +58,4 @@ macro_rules! kprintln {
($fmt: literal $(, $($arg: tt)+)?) => {
$crate::console::kprint(format_args!(concat!($fmt, "\n") $(, $($arg)+)?));
}
}
}

@ -5,8 +5,8 @@ mod virtio_blk;
pub use virtio_blk::VirtIOBlock;
use crate::board::BlockDeviceImpl;
use alloc::sync::Arc;
use crate::fs::easy_fs::BlockDevice;
use alloc::sync::Arc;
use lazy_static::*;
lazy_static! {

@ -1,8 +1,8 @@
use super::BLOCK_SZ;
use super::{
block_cache_sync_all, get_block_cache, Bitmap, BlockDevice, DiskInode, DiskInodeType, Inode,
SuperBlock,
};
use super::BLOCK_SZ;
use alloc::sync::Arc;
use spin::Mutex;

@ -1,3 +1,4 @@
use super::easy_fs::{EasyFileSystem, Inode};
use super::File;
use crate::drivers::BLOCK_DEVICE;
use crate::mm::UserBuffer;
@ -5,7 +6,6 @@ use crate::sync::UPIntrFreeCell;
use alloc::sync::Arc;
use alloc::vec::Vec;
use bitflags::*;
use super::easy_fs::{EasyFileSystem, Inode};
use lazy_static::*;
pub struct OSInode {

@ -1,7 +1,7 @@
pub mod easy_fs;
mod inode;
mod pipe;
mod stdio;
pub mod easy_fs;
use crate::mm::UserBuffer;
pub trait File: Send + Sync {
@ -11,7 +11,7 @@ pub trait File: Send + Sync {
fn write(&self, buf: UserBuffer) -> usize;
}
pub use easy_fs::*;
pub use inode::{list_apps, open_file, OSInode, OpenFlags};
pub use pipe::{make_pipe, Pipe};
pub use stdio::{Stdin, Stdout};
pub use easy_fs::*;

@ -17,6 +17,7 @@ extern crate bitflags;
#[macro_use]
mod console;
mod board;
mod config;
mod drivers;
mod fs;
@ -28,7 +29,6 @@ mod syscall;
mod task;
mod timer;
mod trap;
mod board;
// use board::*;
core::arch::global_asm!(include_str!("entry.asm"));

@ -85,7 +85,7 @@ impl FrameAllocator for StackFrameAllocator {
type FrameAllocatorImpl = StackFrameAllocator;
lazy_static! {
pub static ref FRAME_ALLOCATOR: UPIntrFreeCell<FrameAllocatorImpl> ={
pub static ref FRAME_ALLOCATOR: UPIntrFreeCell<FrameAllocatorImpl> = {
kprintln!("[KERN] mm::frame_allocator::lazy_static!FRAME_ALLOCATOR begin");
unsafe { UPIntrFreeCell::new(FrameAllocatorImpl::new()) }
};

@ -25,7 +25,7 @@ extern "C" {
}
lazy_static! {
pub static ref KERNEL_SPACE: Arc<UPIntrFreeCell<MemorySet>> ={
pub static ref KERNEL_SPACE: Arc<UPIntrFreeCell<MemorySet>> = {
kprintln!("[KERN] mm::memory_set::lazy_static!KERNEL_SPACE begin");
Arc::new(unsafe { UPIntrFreeCell::new(MemorySet::new_kernel()) })
};
@ -95,9 +95,18 @@ impl MemorySet {
// map trampoline
memory_set.map_trampoline();
// map kernel sections
println!("[KERN] .text [{:#x}, {:#x})", stext as usize, etext as usize);
println!("[KERN] .rodata [{:#x}, {:#x})", srodata as usize, erodata as usize);
println!("[KERN] .data [{:#x}, {:#x})", sdata as usize, edata as usize);
println!(
"[KERN] .text [{:#x}, {:#x})",
stext as usize, etext as usize
);
println!(
"[KERN] .rodata [{:#x}, {:#x})",
srodata as usize, erodata as usize
);
println!(
"[KERN] .data [{:#x}, {:#x})",
sdata as usize, edata as usize
);
println!(
"[KERN] .bss [{:#x}, {:#x})",
sbss_with_stack as usize, ebss as usize
@ -271,7 +280,11 @@ impl MapArea {
kprintln!("[KERN] mm::memory_set::MapArea::new() begin");
let start_vpn: VirtPageNum = start_va.floor();
let end_vpn: VirtPageNum = end_va.ceil();
kprintln!("[KERN] mm::memory_set::MapArea::new(start_vpn: {:?}, end_vpn: {:?}) end", start_vpn, end_vpn);
kprintln!(
"[KERN] mm::memory_set::MapArea::new(start_vpn: {:?}, end_vpn: {:?}) end",
start_vpn,
end_vpn
);
Self {
vpn_range: VPNRange::new(start_vpn, end_vpn),
data_frames: BTreeMap::new(),

@ -58,7 +58,7 @@ pub struct IntrMaskingInfo {
lazy_static! {
static ref INTR_MASKING_INFO: UPSafeCellRaw<IntrMaskingInfo> = unsafe {
kprintln!("[KERN] sync::up::lazy_static!INTR_MASKING_INFO begin");
UPSafeCellRaw::new(IntrMaskingInfo::new())
UPSafeCellRaw::new(IntrMaskingInfo::new())
};
}

@ -4,9 +4,9 @@ use crate::task::{current_process, current_user_token};
use alloc::sync::Arc;
pub fn sys_write(fd: usize, buf: *const u8, len: usize) -> isize {
if fd!=1 && fd!=2 {
if fd != 1 && fd != 2 {
kprintln!("[KERN] syscall::fs::sys_write(fd: {}) begin", fd);
}
}
let token = current_user_token();
let process = current_process();
let inner = process.inner_exclusive_access();
@ -27,8 +27,8 @@ pub fn sys_write(fd: usize, buf: *const u8, len: usize) -> isize {
}
pub fn sys_read(fd: usize, buf: *const u8, len: usize) -> isize {
if fd!=0 {
kprintln!("[KERN] syscall::fs::sys_read(fd: {}) begin", fd);
if fd != 0 {
kprintln!("[KERN] syscall::fs::sys_read(fd: {}) begin", fd);
}
let token = current_user_token();
let process = current_process();
@ -58,7 +58,7 @@ pub fn sys_open(path: *const u8, flags: u32) -> isize {
let mut inner = process.inner_exclusive_access();
let fd = inner.alloc_fd();
inner.fd_table[fd] = Some(inode);
kprintln!("[KERN] syscall::fs::sys_open() return fd {}, end",fd);
kprintln!("[KERN] syscall::fs::sys_open() return fd {}, end", fd);
fd as isize
} else {
-1
@ -66,7 +66,7 @@ pub fn sys_open(path: *const u8, flags: u32) -> isize {
}
pub fn sys_close(fd: usize) -> isize {
kprintln!("[KERN] syscall::fs::sys_close(fd: {}) begin",fd);
kprintln!("[KERN] syscall::fs::sys_close(fd: {}) begin", fd);
let process = current_process();
let mut inner = process.inner_exclusive_access();
if fd >= inner.fd_table.len() {

@ -37,11 +37,15 @@ use sync::*;
use thread::*;
pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
if syscall_id !=SYSCALL_YIELD &&
syscall_id !=SYSCALL_WAITPID &&
!(syscall_id ==SYSCALL_READ && args[0] == 0) &&
!(syscall_id ==SYSCALL_WRITE && (args[0] == 1|| args[0] == 2)) {
kprintln!("[KERN] syscall::syscall(id: {}) begin", sys_id_str(syscall_id));
if syscall_id != SYSCALL_YIELD
&& syscall_id != SYSCALL_WAITPID
&& !(syscall_id == SYSCALL_READ && args[0] == 0)
&& !(syscall_id == SYSCALL_WRITE && (args[0] == 1 || args[0] == 2))
{
kprintln!(
"[KERN] syscall::syscall(id: {}) begin",
sys_id_str(syscall_id)
);
}
match syscall_id {
SYSCALL_DUP => sys_dup(args[0]),
@ -106,4 +110,4 @@ pub fn sys_id_str(syscall_id: usize) -> &'static str {
SYSCALL_CONDVAR_WAIT => "sys_condvar_wait",
_ => "Unsupported syscall_id",
}
}
}

@ -96,13 +96,17 @@ pub fn sys_waitpid(pid: isize, exit_code_ptr: *mut i32) -> isize {
// ++++ release child PCB
});
if let Some((idx, _)) = pair {
kprintln!("[KERN] syscall::process::sys_waitpid(): remove child from PCB's children Vector");
kprintln!(
"[KERN] syscall::process::sys_waitpid(): remove child from PCB's children Vector"
);
let child = inner.children.remove(idx);
// confirm that child will be deallocated after being removed from children list
assert_eq!(Arc::strong_count(&child), 1);
let found_pid = child.getpid();
// ++++ temporarily access child PCB exclusively
kprintln!("[KERN] syscall::process::sys_waitpid(): get child's exit_code and return child pid");
kprintln!(
"[KERN] syscall::process::sys_waitpid(): get child's exit_code and return child pid"
);
let exit_code = child.inner_exclusive_access().exit_code;
// ++++ release child PCB
*translated_refmut(inner.memory_set.token(), exit_code_ptr) = exit_code;

@ -47,7 +47,7 @@ lazy_static! {
}
lazy_static! {
static ref KSTACK_ALLOCATOR: UPIntrFreeCell<RecycleAllocator> ={
static ref KSTACK_ALLOCATOR: UPIntrFreeCell<RecycleAllocator> = {
kprintln!("[KERN] task::id::lazy_static!KSTACK_ALLOCATOR begin");
unsafe { UPIntrFreeCell::new(RecycleAllocator::new()) }
};
@ -101,7 +101,7 @@ impl Drop for KernelStack {
KERNEL_SPACE
.exclusive_access()
.remove_area_with_start_vpn(kernel_stack_bottom_va.into());
kprintln!("[KERN] task::id::Drop<KernelStack>::drop end");
kprintln!("[KERN] task::id::Drop<KernelStack>::drop end");
}
}
@ -201,8 +201,8 @@ impl TaskUserRes {
process_inner
.memory_set
.remove_area_with_start_vpn(trap_cx_bottom_va.into());
kprintln!("[KERN] task::id::TaskUserRes::dealloc_user_res() end");
kprintln!("[KERN] task::id::TaskUserRes::dealloc_user_res() end");
}
#[allow(unused)]

@ -24,14 +24,14 @@ impl TaskManager {
}
lazy_static! {
pub static ref TASK_MANAGER: UPIntrFreeCell<TaskManager> ={
pub static ref TASK_MANAGER: UPIntrFreeCell<TaskManager> = {
kprintln!("[KERN] task::manager::lazy_static!TASK_MANAGER begin");
unsafe { UPIntrFreeCell::new(TaskManager::new()) }
};
}
lazy_static! {
pub static ref PID2PCB: UPIntrFreeCell<BTreeMap<usize, Arc<ProcessControlBlock>>> ={
pub static ref PID2PCB: UPIntrFreeCell<BTreeMap<usize, Arc<ProcessControlBlock>>> = {
kprintln!("[KERN] task::manager::lazy_static!PID2PCB begin");
unsafe { UPIntrFreeCell::new(BTreeMap::new()) }
};

@ -101,7 +101,9 @@ pub fn exit_current_and_run_next(exit_code: i32) {
remove_from_pid2process(pid);
let mut process_inner = process.inner_exclusive_access();
// mark this process as a zombie process
kprintln!("[KERN] task::exit_current_and_run_next(): mark this process as a zombie process");
kprintln!(
"[KERN] task::exit_current_and_run_next(): mark this process as a zombie process"
);
process_inner.is_zombie = true;
// record exit code of main process
kprintln!("[KERN] task::exit_current_and_run_next(): record exit code in process_inner");
@ -109,7 +111,9 @@ pub fn exit_current_and_run_next(exit_code: i32) {
{
// move all child processes under init process
kprintln!("[KERN] task::exit_current_and_run_next(): move all child processes under INITPROC");
kprintln!(
"[KERN] task::exit_current_and_run_next(): move all child processes under INITPROC"
);
let mut initproc_inner = INITPROC.inner_exclusive_access();
for child in process_inner.children.iter() {
child.inner_exclusive_access().parent = Some(Arc::downgrade(&INITPROC));
@ -130,7 +134,9 @@ pub fn exit_current_and_run_next(exit_code: i32) {
}
}
kprintln!("[KERN] task::exit_current_and_run_next(): clear children Vector in process_inner");
kprintln!(
"[KERN] task::exit_current_and_run_next(): clear children Vector in process_inner"
);
// dealloc_tid and dealloc_user_res require access to PCB inner, so we
// need to collect those user res first, then release process_inner
// for now to avoid deadlock/double borrow problem.

@ -164,7 +164,9 @@ impl ProcessControlBlock {
kprintln!("[KERN] task::process::PCB::exec(): set trap_cx_ppn for this thread");
task_inner.trap_cx_ppn = task_inner.res.as_mut().unwrap().trap_cx_ppn();
// push arguments on user stack
kprintln!("[KERN] task::process::PCB::exec(): push arguments on user stack for this thread");
kprintln!(
"[KERN] task::process::PCB::exec(): push arguments on user stack for this thread"
);
let mut user_sp = task_inner.res.as_mut().unwrap().ustack_top();
user_sp -= (args.len() + 1) * core::mem::size_of::<usize>();
let argv_base = user_sp;
@ -270,12 +272,16 @@ impl ProcessControlBlock {
child_inner.tasks.push(Some(Arc::clone(&task)));
drop(child_inner);
// modify kstack_top in trap_cx of this thread
kprintln!("[KERN] task::process::PCB::fork(): modify child's kstack_top in trap_cx of child");
kprintln!(
"[KERN] task::process::PCB::fork(): modify child's kstack_top in trap_cx of child"
);
let task_inner = task.inner_exclusive_access();
let trap_cx = task_inner.get_trap_cx();
trap_cx.kernel_sp = task.kstack.get_top();
drop(task_inner);
kprintln!("[KERN] task::process::PCB::fork(): insert <child pid, child PCB> in PID2PCB BTreeMap");
kprintln!(
"[KERN] task::process::PCB::fork(): insert <child pid, child PCB> in PID2PCB BTreeMap"
);
insert_into_pid2process(child.getpid(), Arc::clone(&child));
// add this thread to scheduler
kprintln!("[KERN] task::process::PCB::fork(): add_task(child task): add child thread to scheduler");

@ -52,7 +52,7 @@ impl Ord for TimerCondVar {
}
lazy_static! {
static ref TIMERS: UPIntrFreeCell<BinaryHeap<TimerCondVar>> ={
static ref TIMERS: UPIntrFreeCell<BinaryHeap<TimerCondVar>> = {
kprintln!("[KERN] timer::lazy_static!TIMERS begin");
unsafe { UPIntrFreeCell::new(BinaryHeap::<TimerCondVar>::new()) }
};

@ -7,16 +7,18 @@ extern crate user_lib;
extern crate alloc;
extern crate core;
use user_lib::{thread_create, waittid, exit, sleep};
use alloc::vec::Vec;
use core::sync::atomic::{AtomicUsize, Ordering};
use user_lib::{exit, sleep, thread_create, waittid};
const N: usize = 2;
const THREAD_NUM: usize = 10;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum FlagState {
Out, Want, In,
Out,
Want,
In,
}
static mut TURN: usize = 0;
@ -25,7 +27,7 @@ static mut FLAG: [FlagState; THREAD_NUM] = [FlagState::Out; THREAD_NUM];
static GUARD: AtomicUsize = AtomicUsize::new(0);
fn critical_test_enter() {
assert_eq!(GUARD.fetch_add(1, Ordering::SeqCst), 0);
assert_eq!(GUARD.fetch_add(1, Ordering::SeqCst), 0);
}
fn critical_test_claim() {
@ -33,7 +35,7 @@ fn critical_test_claim() {
}
fn critical_test_exit() {
assert_eq!(GUARD.fetch_sub(1, Ordering::SeqCst), 1);
assert_eq!(GUARD.fetch_sub(1, Ordering::SeqCst), 1);
}
fn eisenberg_enter_critical(id: usize) {
@ -43,7 +45,7 @@ fn eisenberg_enter_critical(id: usize) {
vstore!(&FLAG[id], FlagState::Want);
loop {
/* check if any with higher priority is `Want` or `In` */
let mut prior_thread:Option<usize> = None;
let mut prior_thread: Option<usize> = None;
let turn = vload!(&TURN);
let ring_id = if id < turn { id + THREAD_NUM } else { id };
// FLAG.iter() may lead to some errors, use for-loop instead
@ -56,8 +58,11 @@ fn eisenberg_enter_critical(id: usize) {
if prior_thread.is_none() {
break;
}
println!("Thread[{}]: prior thread {} exist, sleep and retry",
id, prior_thread.unwrap());
println!(
"Thread[{}]: prior thread {} exist, sleep and retry",
id,
prior_thread.unwrap()
);
sleep(1);
}
/* now tentatively claim the resource */
@ -86,7 +91,7 @@ fn eisenberg_exit_critical(id: usize) {
/* find next one who wants to enter and give the turn to it*/
let mut next = id;
let ring_id = id + THREAD_NUM;
for i in (id+1)..ring_id {
for i in (id + 1)..ring_id {
let idx = i % THREAD_NUM;
if vload!(&FLAG[idx]) == FlagState::Want {
next = idx;
@ -119,7 +124,7 @@ pub fn main() -> i32 {
let mut v = Vec::new();
// TODO: really shuffle
assert_eq!(THREAD_NUM, 10);
let shuffle:[usize; 10] = [0, 7, 4, 6, 2, 9, 8, 1, 3, 5];
let shuffle: [usize; 10] = [0, 7, 4, 6, 2, 9, 8, 1, 3, 5];
for i in 0..THREAD_NUM {
v.push(thread_create(thread_fn as usize, shuffle[i]));
}
@ -130,4 +135,4 @@ pub fn main() -> i32 {
}
println!("main thread exited.");
0
}
}

@ -4,7 +4,7 @@
#[macro_use]
extern crate user_lib;
use user_lib::{exit, fork, getpid, sleep, yield_, wait};
use user_lib::{exit, fork, getpid, sleep, wait, yield_};
const DEPTH: usize = 4;
@ -28,7 +28,7 @@ fn fork_tree(cur: &str) {
fork_child(cur, '0');
fork_child(cur, '1');
let mut exit_code: i32 = 0;
for _ in 0..2{
for _ in 0..2 {
wait(&mut exit_code);
}
}
@ -37,7 +37,7 @@ fn fork_tree(cur: &str) {
pub fn main() -> i32 {
fork_tree("");
let mut exit_code: i32 = 0;
for _ in 0..2{
for _ in 0..2 {
wait(&mut exit_code);
}
sleep(3000);

@ -8,9 +8,9 @@ extern crate user_lib;
extern crate alloc;
extern crate core;
use user_lib::{thread_create, waittid, exit, sleep};
use core::sync::atomic::{AtomicUsize, Ordering};
use alloc::vec::Vec;
use core::sync::atomic::{AtomicUsize, Ordering};
use user_lib::{exit, sleep, thread_create, waittid};
const N: usize = 3;
static mut TURN: usize = 0;
@ -26,7 +26,7 @@ fn critical_test_claim() {
}
fn critical_test_exit() {
assert_eq!(GUARD.fetch_sub(1, Ordering::SeqCst), 1);
assert_eq!(GUARD.fetch_sub(1, Ordering::SeqCst), 1);
}
fn peterson_enter_critical(id: usize, peer_id: usize) {
@ -75,4 +75,4 @@ pub fn main() -> i32 {
}
println!("main thread exited.");
0
}
}

@ -4,7 +4,7 @@
#[macro_use]
extern crate user_lib;
// not in SUCC_TESTS & FAIL_TESTS
// not in SUCC_TESTS & FAIL_TESTS
// count_lines, infloop, user_shell, usertests
// item of TESTS : app_name(argv_0), argv_1, argv_2, argv_3, exit_code
@ -115,7 +115,11 @@ pub fn main() -> i32 {
let succ_num = run_tests(SUCC_TESTS);
let err_num = run_tests(FAIL_TESTS);
if succ_num == SUCC_TESTS.len() as i32 && err_num == FAIL_TESTS.len() as i32 {
println!("{} of sueecssed apps, {} of failed apps run correctly. \nUsertests passed!", SUCC_TESTS.len(), FAIL_TESTS.len() );
println!(
"{} of sueecssed apps, {} of failed apps run correctly. \nUsertests passed!",
SUCC_TESTS.len(),
FAIL_TESTS.len()
);
return 0;
}
if succ_num != SUCC_TESTS.len() as i32 {

@ -127,7 +127,8 @@ pub fn waitpid(pid: usize, exit_code: &mut i32) -> isize {
// -1 or a real pid
exit_pid => {
println!("[USER] lib::waitpid() end: exit_pid {}", exit_pid);
return exit_pid; }
return exit_pid;
}
}
}
}
@ -204,14 +205,14 @@ pub fn condvar_wait(condvar_id: usize, mutex_id: usize) {
#[macro_export]
macro_rules! vstore {
($var_ref: expr, $value: expr) => {
($var_ref: expr, $value: expr) => {
unsafe { core::intrinsics::volatile_store($var_ref as *const _ as _, $value) }
};
}
#[macro_export]
macro_rules! vload {
($var_ref: expr) => {
($var_ref: expr) => {
unsafe { core::intrinsics::volatile_load($var_ref as *const _ as _) }
};
}
@ -221,4 +222,4 @@ macro_rules! memory_fence {
() => {
core::sync::atomic::fence(core::sync::atomic::Ordering::SeqCst)
};
}
}

Loading…
Cancel
Save