Handle backspace for k210/qemu platform && Add lots of user tests.

This commit is contained in:
Yifan Wu 2020-12-11 01:44:07 +08:00
parent f69b3e11dd
commit 48a11e53ff
20 changed files with 361 additions and 35 deletions

View file

@ -48,6 +48,7 @@ impl StackFrameAllocator {
pub fn init(&mut self, l: PhysPageNum, r: PhysPageNum) {
self.current = l.0;
self.end = r.0;
println!("last {} Physical Frames.", self.end - self.current);
}
}
impl FrameAllocator for StackFrameAllocator {

View file

@ -47,10 +47,6 @@ impl MemorySet {
areas: Vec::new(),
}
}
#[allow(unused)]
pub fn dealloc_all_frames(&mut self) {
*self = Self::new_bare();
}
pub fn token(&self) -> usize {
self.page_table.token()
}
@ -217,7 +213,8 @@ impl MemorySet {
self.page_table.translate(vpn)
}
pub fn clear(&mut self) {
*self = Self::new_bare();
//*self = Self::new_bare();
self.areas.clear();
}
}

View file

@ -5,7 +5,7 @@ use crate::task::{
current_user_token,
add_task,
};
use crate::timer::get_time;
use crate::timer::get_time_ms;
use crate::mm::{
translated_str,
translated_refmut,
@ -14,18 +14,7 @@ use crate::loader::get_app_data_by_name;
use alloc::sync::Arc;
pub fn sys_exit(exit_code: i32) -> ! {
// save exit code
let task = current_task().unwrap();
// ---- hold current PCB lock
task.acquire_inner_lock().exit_code = exit_code;
// ---- release current PCB lock
// this function will not return
// drop task manually to maintain rc correctly
drop(task);
exit_current_and_run_next();
exit_current_and_run_next(exit_code);
panic!("Unreachable in sys_exit!");
}
@ -35,7 +24,7 @@ pub fn sys_yield() -> isize {
}
pub fn sys_get_time() -> isize {
get_time() as isize
get_time_ms() as isize
}
pub fn sys_getpid() -> isize {
@ -68,11 +57,21 @@ pub fn sys_exec(path: *const u8) -> isize {
}
}
/// If there is not a child process whose pid is same as given, return -1.
/// Else if there is a child process but it is still running, return -2.
pub fn sys_waitpid(pid: isize, exit_code_ptr: *mut i32) -> isize {
let task = current_task().unwrap();
// find a child process
// ---- hold current PCB lock
let mut inner = task.acquire_inner_lock();
if inner.children
.iter()
.find(|p| {pid == -1 || pid as usize == p.getpid()})
.is_none() {
return -1;
// ---- release current PCB lock
}
let pair = inner.children
.iter()
.enumerate()
@ -92,7 +91,7 @@ pub fn sys_waitpid(pid: isize, exit_code_ptr: *mut i32) -> isize {
*translated_refmut(inner.memory_set.token(), exit_code_ptr) = exit_code;
found_pid as isize
} else {
-1
-2
}
// ---- release current PCB lock automatically
}

View file

@ -42,14 +42,17 @@ pub fn suspend_current_and_run_next() {
schedule(task_cx_ptr);
}
pub fn exit_current_and_run_next() {
pub fn exit_current_and_run_next(exit_code: i32) {
// take from Processor
let task = take_current_task().unwrap();
// **** hold current PCB lock
let mut inner = task.acquire_inner_lock();
// Change status to Zombie
inner.task_status = TaskStatus::Zombie;
// Record exit code
inner.exit_code = exit_code;
// move any child to its parent
// TODO: do not move to its parent but under initproc
// ++++++ hold parent PCB lock here
{

View file

@ -3,11 +3,16 @@ use crate::sbi::set_timer;
use crate::config::CPU_FREQ;
const TICKS_PER_SEC: usize = 100;
const MSEC_PER_SEC: usize = 1000;
pub fn get_time() -> usize {
time::read()
}
pub fn get_time_ms() -> usize {
time::read() / (CPU_FREQ / MSEC_PER_SEC)
}
pub fn set_next_trigger() {
set_timer(get_time() + CPU_FREQ / TICKS_PER_SEC);
}

View file

@ -67,13 +67,24 @@ pub fn trap_handler() -> ! {
cx.x[10] = result as usize;
}
Trap::Exception(Exception::StoreFault) |
Trap::Exception(Exception::StorePageFault) => {
println!("[kernel] PageFault in application, bad addr = {:#x}, bad instruction = {:#x}, core dumped.", stval, current_trap_cx().sepc);
exit_current_and_run_next();
Trap::Exception(Exception::StorePageFault) |
Trap::Exception(Exception::InstructionFault) |
Trap::Exception(Exception::InstructionPageFault) |
Trap::Exception(Exception::LoadFault) |
Trap::Exception(Exception::LoadPageFault) => {
println!(
"[kernel] {:?} in application, bad addr = {:#x}, bad instruction = {:#x}, core dumped.",
scause.cause(),
stval,
current_trap_cx().sepc,
);
// page fault exit code
exit_current_and_run_next(-2);
}
Trap::Exception(Exception::IllegalInstruction) => {
println!("[kernel] IllegalInstruction in application, core dumped.");
exit_current_and_run_next();
// illegal instruction exit code
exit_current_and_run_next(-3);
}
Trap::Interrupt(Interrupt::SupervisorTimer) => {
set_next_trigger();