提交 1493ec94 编写于 作者: Y Yifan Wu

Stage1 clear! All applications work but now they are based on threads.

上级 c599a31d
......@@ -19,14 +19,14 @@ pub fn print(args: fmt::Arguments) {
#[macro_export]
macro_rules! print {
($fmt: literal $(, $($arg: tt)+)?) => {
$crate::console::print(format_args!($fmt $(, $($arg)+)?));
$crate::console::print(format_args!($fmt $(, $($arg)+)?))
}
}
#[macro_export]
macro_rules! println {
($fmt: literal $(, $($arg: tt)+)?) => {
$crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?));
$crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?))
}
}
......
......@@ -163,15 +163,9 @@ impl TaskUserRes {
);
}
pub fn dealloc_tid(&self) {
let mut process = self.process.inner_exclusive_access();
process.dealloc_tid(self.tid);
}
fn dealloc_user_res(&self) {
// dealloc tid
let mut process = self.process.inner_exclusive_access();
process.dealloc_tid(self.tid);
// dealloc ustack manually
let ustack_bottom_va: VirtAddr = ustack_bottom_from_tid(self.ustack_base, self.tid).into();
process.memory_set.remove_area_with_start_vpn(ustack_bottom_va.into());
......@@ -180,6 +174,16 @@ impl TaskUserRes {
process.memory_set.remove_area_with_start_vpn(trap_cx_bottom_va.into());
}
#[allow(unused)]
pub fn alloc_tid(&mut self) {
self.tid = self.process.inner_exclusive_access().alloc_tid();
}
pub fn dealloc_tid(&self) {
let mut process = self.process.inner_exclusive_access();
process.dealloc_tid(self.tid);
}
pub fn trap_cx_user_va(&self) -> usize {
trap_cx_bottom_from_tid(self.tid)
}
......@@ -202,6 +206,7 @@ impl TaskUserRes {
impl Drop for TaskUserRes {
fn drop(&mut self) {
self.dealloc_tid();
self.dealloc_user_res();
// kstack can also be deallocated automatically
}
......
......@@ -33,4 +33,4 @@ pub fn add_task(task: Arc<TaskControlBlock>) {
pub fn fetch_task() -> Option<Arc<TaskControlBlock>> {
TASK_MANAGER.exclusive_access().fetch()
}
\ No newline at end of file
}
......@@ -55,7 +55,7 @@ pub fn suspend_current_and_run_next() {
pub fn exit_current_and_run_next(exit_code: i32) {
// take from Processor
let task = take_current_task().unwrap();
let task_exit_code = task.inner_exclusive_access().exit_code;
task.inner_exclusive_access().exit_code = exit_code;
let tid = task.inner_exclusive_access().res.tid;
// remove thread
let process = task.process.upgrade().unwrap();
......@@ -66,7 +66,7 @@ pub fn exit_current_and_run_next(exit_code: i32) {
// mark this process as a zombie process
process_inner.is_zombie = true;
// record exit code of main process
process_inner.exit_code = task_exit_code;
process_inner.exit_code = exit_code;
{
// move all child processes under init process
......@@ -99,5 +99,5 @@ lazy_static! {
}
pub fn add_initproc() {
let initproc = INITPROC.clone();
let _initproc = INITPROC.clone();
}
......@@ -3,8 +3,6 @@ use crate::mm::{
KERNEL_SPACE,
translated_refmut,
};
use crate::task::TaskContext;
use crate::task::id::TaskUserRes;
use crate::trap::{TrapContext, trap_handler};
use crate::sync::UPSafeCell;
use core::cell::RefMut;
......@@ -37,6 +35,7 @@ pub struct ProcessControlBlockInner {
}
impl ProcessControlBlockInner {
#[allow(unused)]
pub fn get_user_token(&self) -> usize {
self.memory_set.token()
}
......@@ -138,9 +137,9 @@ impl ProcessControlBlock {
// since memory_set has been changed
let task = self.inner_exclusive_access().get_task(0);
let mut task_inner = task.inner_exclusive_access();
task_inner.res.dealloc_tid();
task_inner.res.ustack_base = ustack_base;
task_inner.res.alloc_user_res();
task_inner.trap_cx_ppn = task_inner.res.trap_cx_ppn();
// push arguments on user stack
let mut user_sp = task_inner.res.ustack_top();
user_sp -= (args.len() + 1) * core::mem::size_of::<usize>();
......@@ -177,7 +176,6 @@ impl ProcessControlBlock {
trap_cx.x[10] = args.len();
trap_cx.x[11] = argv_base;
*task_inner.get_trap_cx() = trap_cx;
task_inner.task_cx = TaskContext::goto_trap_return(task_inner.res.kstack_top());
}
/// Only support processes with a single thread.
......
......@@ -41,7 +41,7 @@ impl TaskControlBlockInner {
self.trap_cx_ppn.get_mut()
}
#[allow(unused)]
fn get_status(&self) -> TaskStatus {
self.task_status
}
......
......@@ -47,7 +47,6 @@ pub fn enable_timer_interrupt() {
#[no_mangle]
pub fn trap_handler() -> ! {
println!("into trap!");
set_kernel_trap_entry();
let scause = scause::read();
let stval = stval::read();
......@@ -55,7 +54,6 @@ pub fn trap_handler() -> ! {
Trap::Exception(Exception::UserEnvCall) => {
// jump to next instruction anyway
let mut cx = current_trap_cx();
println!("syscall #{}", cx.x[17]);
cx.sepc += 4;
// get system call return value
let result = syscall(cx.x[17], [cx.x[10], cx.x[11], cx.x[12]]);
......@@ -96,11 +94,9 @@ pub fn trap_handler() -> ! {
#[no_mangle]
pub fn trap_return() -> ! {
println!("into trap_return!");
set_user_trap_entry();
let trap_cx_user_va = current_trap_cx_user_va();
let user_satp = current_user_token();
println!("trap_cx = {:#x}, user_satp = {:#x}", trap_cx_user_va, user_satp);
extern "C" {
fn __alltraps();
fn __restore();
......
......@@ -105,4 +105,4 @@ pub fn sleep(period_ms: usize) {
while sys_get_time() < start + period_ms as isize {
sys_yield();
}
}
\ No newline at end of file
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册