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! {
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 {

View File

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

View File

@ -46,7 +46,8 @@ use lazy_static::*;
use sync::UPIntrFreeCell;
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]

View File

@ -1,5 +1,8 @@
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};
pub struct Condvar {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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