cargo fmt

This commit is contained in:
Yu Chen 2022-05-14 22:53:45 +08:00
parent 8657b656e5
commit 8e73480c99
11 changed files with 47 additions and 38 deletions

View File

@ -20,7 +20,8 @@ pub struct VirtIOBlock {
} }
lazy_static! { lazy_static! {
static ref QUEUE_FRAMES: UPIntrFreeCell<Vec<FrameTracker>> = unsafe { UPIntrFreeCell::new(Vec::new()) }; static ref QUEUE_FRAMES: UPIntrFreeCell<Vec<FrameTracker>> =
unsafe { UPIntrFreeCell::new(Vec::new()) };
} }
impl BlockDevice for VirtIOBlock { impl BlockDevice for VirtIOBlock {

View File

@ -1,7 +1,6 @@
///! Ref: https://www.lammertbies.nl/comm/info/serial-uart ///! Ref: https://www.lammertbies.nl/comm/info/serial-uart
///! Ref: ns16550a datasheet: https://datasheetspdf.com/pdf-file/605590/NationalSemiconductor/NS16550A/1 ///! Ref: ns16550a datasheet: https://datasheetspdf.com/pdf-file/605590/NationalSemiconductor/NS16550A/1
///! Ref: ns16450 datasheet: https://datasheetspdf.com/pdf-file/1311818/NationalSemiconductor/NS16450/1 ///! Ref: ns16450 datasheet: https://datasheetspdf.com/pdf-file/1311818/NationalSemiconductor/NS16450/1
use super::CharDevice; use super::CharDevice;
use crate::sync::{Condvar, UPIntrFreeCell}; use crate::sync::{Condvar, UPIntrFreeCell};
use crate::task::schedule; use crate::task::schedule;

View File

@ -46,7 +46,8 @@ use lazy_static::*;
use sync::UPIntrFreeCell; use sync::UPIntrFreeCell;
lazy_static! { lazy_static! {
pub static ref DEV_NON_BLOCKING_ACCESS: UPIntrFreeCell<bool> = unsafe { UPIntrFreeCell::new(false) }; pub static ref DEV_NON_BLOCKING_ACCESS: UPIntrFreeCell<bool> =
unsafe { UPIntrFreeCell::new(false) };
} }
#[no_mangle] #[no_mangle]

View File

@ -1,5 +1,8 @@
use crate::sync::{Mutex, UPIntrFreeCell}; use crate::sync::{Mutex, UPIntrFreeCell};
use crate::task::{add_task, block_current_task, block_current_and_run_next, current_task, TaskControlBlock, TaskContext}; use crate::task::{
add_task, block_current_and_run_next, block_current_task, current_task, TaskContext,
TaskControlBlock,
};
use alloc::{collections::VecDeque, sync::Arc}; use alloc::{collections::VecDeque, sync::Arc};
pub struct Condvar { pub struct Condvar {

View File

@ -1,7 +1,7 @@
use core::cell::{RefCell, RefMut, UnsafeCell}; use core::cell::{RefCell, RefMut, UnsafeCell};
use core::ops::{Deref, DerefMut}; use core::ops::{Deref, DerefMut};
use riscv::register::sstatus;
use lazy_static::*; use lazy_static::*;
use riscv::register::sstatus;
/* /*
/// Wrap a static data structure inside it so that we are /// Wrap a static data structure inside it so that we are
@ -56,9 +56,8 @@ pub struct IntrMaskingInfo {
} }
lazy_static! { lazy_static! {
static ref INTR_MASKING_INFO: UPSafeCellRaw<IntrMaskingInfo> = unsafe { static ref INTR_MASKING_INFO: UPSafeCellRaw<IntrMaskingInfo> =
UPSafeCellRaw::new(IntrMaskingInfo::new()) unsafe { UPSafeCellRaw::new(IntrMaskingInfo::new()) };
};
} }
impl IntrMaskingInfo { impl IntrMaskingInfo {
@ -71,7 +70,9 @@ impl IntrMaskingInfo {
pub fn enter(&mut self) { pub fn enter(&mut self) {
let sie = sstatus::read().sie(); let sie = sstatus::read().sie();
unsafe { sstatus::clear_sie(); } unsafe {
sstatus::clear_sie();
}
if self.nested_level == 0 { if self.nested_level == 0 {
self.sie_before_masking = sie; self.sie_before_masking = sie;
} }
@ -81,7 +82,9 @@ impl IntrMaskingInfo {
pub fn exit(&mut self) { pub fn exit(&mut self) {
self.nested_level -= 1; self.nested_level -= 1;
if self.nested_level == 0 && self.sie_before_masking { if self.nested_level == 0 && self.sie_before_masking {
unsafe { sstatus::set_sie(); } unsafe {
sstatus::set_sie();
}
} }
} }
} }
@ -101,14 +104,17 @@ impl<T> UPIntrFreeCell<T> {
inner: RefCell::new(value), inner: RefCell::new(value),
} }
} }
/// Panic if the data has been borrowed. /// Panic if the data has been borrowed.
pub fn exclusive_access(&self) -> UPIntrRefMut<'_, T> { pub fn exclusive_access(&self) -> UPIntrRefMut<'_, T> {
INTR_MASKING_INFO.get_mut().enter(); INTR_MASKING_INFO.get_mut().enter();
UPIntrRefMut(Some(self.inner.borrow_mut())) UPIntrRefMut(Some(self.inner.borrow_mut()))
} }
pub fn exclusive_session<F, V>(&self, f: F) -> V where F: FnOnce(&mut T) -> V { pub fn exclusive_session<F, V>(&self, f: F) -> V
where
F: FnOnce(&mut T) -> V,
{
let mut inner = self.exclusive_access(); let mut inner = self.exclusive_access();
f(inner.deref_mut()) f(inner.deref_mut())
} }
@ -132,4 +138,3 @@ impl<'a, T> DerefMut for UPIntrRefMut<'a, T> {
self.0.as_mut().unwrap().deref_mut() self.0.as_mut().unwrap().deref_mut()
} }
} }

View File

@ -26,7 +26,6 @@ pub use processor::{
pub use signal::SignalFlags; pub use signal::SignalFlags;
pub use task::{TaskControlBlock, TaskStatus}; pub use task::{TaskControlBlock, TaskStatus};
pub fn suspend_current_and_run_next() { pub fn suspend_current_and_run_next() {
// There must be an application running. // There must be an application running.
let task = take_current_task().unwrap(); let task = take_current_task().unwrap();
@ -54,7 +53,7 @@ pub fn block_current_task() -> *mut TaskContext {
} }
pub fn block_current_and_run_next() { pub fn block_current_and_run_next() {
let task_cx_ptr = block_current_task(); let task_cx_ptr = block_current_task();
schedule(task_cx_ptr); schedule(task_cx_ptr);
} }

View File

@ -30,7 +30,8 @@ impl Processor {
} }
lazy_static! { lazy_static! {
pub static ref PROCESSOR: UPIntrFreeCell<Processor> = unsafe { UPIntrFreeCell::new(Processor::new()) }; pub static ref PROCESSOR: UPIntrFreeCell<Processor> =
unsafe { UPIntrFreeCell::new(Processor::new()) };
} }
pub fn run_tasks() { pub fn run_tasks() {
@ -94,9 +95,8 @@ pub fn current_kstack_top() -> usize {
} }
pub fn schedule(switched_task_cx_ptr: *mut TaskContext) { pub fn schedule(switched_task_cx_ptr: *mut TaskContext) {
let idle_task_cx_ptr = PROCESSOR.exclusive_session(|processor| { let idle_task_cx_ptr =
processor.get_idle_task_cx_ptr() PROCESSOR.exclusive_session(|processor| processor.get_idle_task_cx_ptr());
});
unsafe { unsafe {
__switch(switched_task_cx_ptr, idle_task_cx_ptr); __switch(switched_task_cx_ptr, idle_task_cx_ptr);
} }

View File

@ -1,7 +1,10 @@
use super::id::TaskUserRes; use super::id::TaskUserRes;
use super::{kstack_alloc, KernelStack, ProcessControlBlock, TaskContext}; use super::{kstack_alloc, KernelStack, ProcessControlBlock, TaskContext};
use crate::trap::TrapContext; use crate::trap::TrapContext;
use crate::{mm::PhysPageNum, sync::{UPIntrFreeCell, UPIntrRefMut}}; use crate::{
mm::PhysPageNum,
sync::{UPIntrFreeCell, UPIntrRefMut},
};
use alloc::sync::{Arc, Weak}; use alloc::sync::{Arc, Weak};
pub struct TaskControlBlock { pub struct TaskControlBlock {

View File

@ -11,7 +11,7 @@ use core::arch::{asm, global_asm};
use riscv::register::{ use riscv::register::{
mtvec::TrapMode, mtvec::TrapMode,
scause::{self, Exception, Interrupt, Trap}, scause::{self, Exception, Interrupt, Trap},
sie, stval, stvec, sstatus, sscratch, sie, sscratch, sstatus, stval, stvec,
}; };
global_asm!(include_str!("trap.S")); global_asm!(include_str!("trap.S"));
@ -23,7 +23,7 @@ pub fn init() {
fn set_kernel_trap_entry() { fn set_kernel_trap_entry() {
extern "C" { extern "C" {
fn __alltraps(); fn __alltraps();
fn __alltraps_k(); fn __alltraps_k();
} }
let __alltraps_k_va = __alltraps_k as usize - __alltraps as usize + TRAMPOLINE; let __alltraps_k_va = __alltraps_k as usize - __alltraps as usize + TRAMPOLINE;
unsafe { unsafe {
@ -52,7 +52,7 @@ fn enable_supervisor_interrupt() {
fn disable_supervisor_interrupt() { fn disable_supervisor_interrupt() {
unsafe { unsafe {
sstatus::clear_sie(); sstatus::clear_sie();
} }
} }
@ -67,7 +67,7 @@ pub fn trap_handler() -> ! {
// jump to next instruction anyway // jump to next instruction anyway
let mut cx = current_trap_cx(); let mut cx = current_trap_cx();
cx.sepc += 4; cx.sepc += 4;
enable_supervisor_interrupt(); enable_supervisor_interrupt();
// get system call return value // get system call return value
@ -150,19 +150,19 @@ pub fn trap_from_kernel(_trap_cx: &TrapContext) {
match scause.cause() { match scause.cause() {
Trap::Interrupt(Interrupt::SupervisorExternal) => { Trap::Interrupt(Interrupt::SupervisorExternal) => {
crate::board::irq_handler(); crate::board::irq_handler();
}, }
Trap::Interrupt(Interrupt::SupervisorTimer) => { Trap::Interrupt(Interrupt::SupervisorTimer) => {
set_next_trigger(); set_next_trigger();
check_timer(); check_timer();
// do not schedule now // do not schedule now
}, }
_ => { _ => {
panic!( panic!(
"Unsupported trap from kernel: {:?}, stval = {:#x}!", "Unsupported trap from kernel: {:?}, stval = {:#x}!",
scause.cause(), scause.cause(),
stval stval
); );
}, }
} }
} }

View File

@ -5,9 +5,9 @@
extern crate user_lib; extern crate user_lib;
extern crate alloc; extern crate alloc;
use alloc::{vec::Vec, string::String, fmt::format}; use alloc::{fmt::format, string::String, vec::Vec};
use user_lib::{close, get_time, gettid, open, write, OpenFlags};
use user_lib::{exit, thread_create, waittid}; use user_lib::{exit, thread_create, waittid};
use user_lib::{close, get_time, open, write, OpenFlags, gettid};
fn worker(size_kib: usize) { fn worker(size_kib: usize) {
let mut buffer = [0u8; 1024]; // 1KiB let mut buffer = [0u8; 1024]; // 1KiB
@ -17,11 +17,11 @@ fn worker(size_kib: usize) {
let filename = format(format_args!("testf{}\0", gettid())); let filename = format(format_args!("testf{}\0", gettid()));
let f = open(filename.as_str(), OpenFlags::CREATE | OpenFlags::WRONLY); let f = open(filename.as_str(), OpenFlags::CREATE | OpenFlags::WRONLY);
if f < 0 { if f < 0 {
panic!("Open test file failed!"); panic!("Open test file failed!");
} }
let f = f as usize; let f = f as usize;
for _ in 0..size_kib { for _ in 0..size_kib {
write(f, &buffer); write(f, &buffer);
} }
close(f); close(f);
exit(0) exit(0)
@ -34,18 +34,18 @@ pub fn main(argc: usize, argv: &[&str]) -> i32 {
let size_kb = size_mb << 10; let size_kb = size_mb << 10;
let workers = argv[1].parse::<usize>().expect("wrong argument"); let workers = argv[1].parse::<usize>().expect("wrong argument");
assert!(workers >= 1 && size_kb % workers == 0, "wrong argument"); assert!(workers >= 1 && size_kb % workers == 0, "wrong argument");
let start = get_time(); let start = get_time();
let mut v = Vec::new(); let mut v = Vec::new();
let size_mb = 1usize; let size_mb = 1usize;
for _ in 0..workers { for _ in 0..workers {
v.push(thread_create(worker as usize, size_kb / workers)); v.push(thread_create(worker as usize, size_kb / workers));
} }
for tid in v.iter() { for tid in v.iter() {
assert_eq!(0, waittid(*tid as usize)); assert_eq!(0, waittid(*tid as usize));
} }
let time_ms = (get_time() - start) as usize; let time_ms = (get_time() - start) as usize;
let speed_kbs = size_kb * 1000 / time_ms; let speed_kbs = size_kb * 1000 / time_ms;
println!( println!(

View File

@ -17,9 +17,7 @@ static SUCC_TESTS: &[&str] = &[
"yield\0", "yield\0",
]; ];
static FAIL_TESTS: &[&str] = &[ static FAIL_TESTS: &[&str] = &["stack_overflow\0"];
"stack_overflow\0",
];
use user_lib::{exec, fork, waitpid}; use user_lib::{exec, fork, waitpid};