|
|
|
@ -1,22 +1,32 @@
|
|
|
|
|
use crate::mm::{MemorySet, MapPermission, PhysPageNum, KERNEL_SPACE, VirtAddr};
|
|
|
|
|
use crate::mm::{MemorySet, PhysPageNum, KERNEL_SPACE, VirtAddr};
|
|
|
|
|
use crate::trap::{TrapContext, trap_handler};
|
|
|
|
|
use crate::config::{TRAP_CONTEXT, kernel_stack_position};
|
|
|
|
|
use crate::config::{TRAP_CONTEXT};
|
|
|
|
|
use super::TaskContext;
|
|
|
|
|
use super::{PidHandle, pid_alloc, KernelStack};
|
|
|
|
|
use alloc::sync::{Weak, Arc};
|
|
|
|
|
use alloc::vec::Vec;
|
|
|
|
|
use spin::{Mutex, MutexGuard};
|
|
|
|
|
|
|
|
|
|
pub struct TaskControlBlock {
|
|
|
|
|
// immutable
|
|
|
|
|
pub trap_cx_ppn: PhysPageNum,
|
|
|
|
|
pub base_size: usize,
|
|
|
|
|
pub pid: PidHandle,
|
|
|
|
|
pub kernel_stack: KernelStack,
|
|
|
|
|
// mutable
|
|
|
|
|
inner: Mutex<TaskControlBlockInner>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct TaskControlBlockInner {
|
|
|
|
|
pub trap_cx_ppn: PhysPageNum,
|
|
|
|
|
pub base_size: usize,
|
|
|
|
|
pub task_cx_ptr: usize,
|
|
|
|
|
pub task_status: TaskStatus,
|
|
|
|
|
pub memory_set: MemorySet,
|
|
|
|
|
pub parent: Option<Weak<TaskControlBlock>>,
|
|
|
|
|
pub children: Vec<Arc<TaskControlBlock>>,
|
|
|
|
|
pub exit_code: i32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TaskControlBlock {
|
|
|
|
|
impl TaskControlBlockInner {
|
|
|
|
|
pub fn get_task_cx_ptr2(&self) -> *const usize {
|
|
|
|
|
&self.task_cx_ptr as *const usize
|
|
|
|
|
}
|
|
|
|
@ -26,7 +36,19 @@ impl TaskControlBlock {
|
|
|
|
|
pub fn get_user_token(&self) -> usize {
|
|
|
|
|
self.memory_set.token()
|
|
|
|
|
}
|
|
|
|
|
pub fn new(elf_data: &[u8], app_id: usize) -> Self {
|
|
|
|
|
fn get_status(&self) -> TaskStatus {
|
|
|
|
|
self.task_status
|
|
|
|
|
}
|
|
|
|
|
pub fn is_zombie(&self) -> bool {
|
|
|
|
|
self.get_status() == TaskStatus::Zombie
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TaskControlBlock {
|
|
|
|
|
pub fn acquire_inner_lock(&self) -> MutexGuard<TaskControlBlockInner> {
|
|
|
|
|
self.inner.lock()
|
|
|
|
|
}
|
|
|
|
|
pub fn new(elf_data: &[u8]) -> Self {
|
|
|
|
|
// memory_set with elf program headers/trampoline/trap context/user stack
|
|
|
|
|
let (memory_set, user_sp, entry_point) = MemorySet::from_elf(elf_data);
|
|
|
|
|
let trap_cx_ppn = memory_set
|
|
|
|
@ -41,16 +63,23 @@ impl TaskControlBlock {
|
|
|
|
|
// push a task context which goes to trap_return to the top of kernel stack
|
|
|
|
|
let task_cx_ptr = kernel_stack.push_on_top(TaskContext::goto_trap_return());
|
|
|
|
|
let task_control_block = Self {
|
|
|
|
|
trap_cx_ppn,
|
|
|
|
|
base_size: user_sp,
|
|
|
|
|
pid: pid_handle,
|
|
|
|
|
kernel_stack,
|
|
|
|
|
task_cx_ptr: task_cx_ptr as usize,
|
|
|
|
|
task_status,
|
|
|
|
|
memory_set,
|
|
|
|
|
inner: Mutex::new(TaskControlBlockInner {
|
|
|
|
|
trap_cx_ppn,
|
|
|
|
|
base_size: user_sp,
|
|
|
|
|
task_cx_ptr: task_cx_ptr as usize,
|
|
|
|
|
task_status,
|
|
|
|
|
memory_set,
|
|
|
|
|
parent: None,
|
|
|
|
|
children: Vec::new(),
|
|
|
|
|
exit_code: 0,
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
// prepare TrapContext in user space
|
|
|
|
|
let trap_cx = task_control_block.get_trap_cx();
|
|
|
|
|
// ---- acquire child PCB lock
|
|
|
|
|
let trap_cx = task_control_block.acquire_inner_lock().get_trap_cx();
|
|
|
|
|
// ---- release child PCB lock
|
|
|
|
|
*trap_cx = TrapContext::app_init_context(
|
|
|
|
|
entry_point,
|
|
|
|
|
user_sp,
|
|
|
|
@ -60,12 +89,86 @@ impl TaskControlBlock {
|
|
|
|
|
);
|
|
|
|
|
task_control_block
|
|
|
|
|
}
|
|
|
|
|
pub fn exec(&self, elf_data: &[u8]) {
|
|
|
|
|
// memory_set with elf program headers/trampoline/trap context/user stack
|
|
|
|
|
let (memory_set, user_sp, entry_point) = MemorySet::from_elf(elf_data);
|
|
|
|
|
let trap_cx_ppn = memory_set
|
|
|
|
|
.translate(VirtAddr::from(TRAP_CONTEXT).into())
|
|
|
|
|
.unwrap()
|
|
|
|
|
.ppn();
|
|
|
|
|
|
|
|
|
|
// **** hold current PCB lock
|
|
|
|
|
let mut inner = self.inner.lock();
|
|
|
|
|
// substitute memory_set
|
|
|
|
|
inner.memory_set = memory_set;
|
|
|
|
|
// update trap_cx ppn
|
|
|
|
|
inner.trap_cx_ppn = trap_cx_ppn;
|
|
|
|
|
drop(inner);
|
|
|
|
|
// **** release current PCB lock manually
|
|
|
|
|
|
|
|
|
|
// initialize trap_cx
|
|
|
|
|
// **** acquire current PCB lock
|
|
|
|
|
let trap_cx = self.acquire_inner_lock().get_trap_cx();
|
|
|
|
|
// **** release current PCB lock
|
|
|
|
|
*trap_cx = TrapContext::app_init_context(
|
|
|
|
|
entry_point,
|
|
|
|
|
user_sp,
|
|
|
|
|
KERNEL_SPACE.lock().token(),
|
|
|
|
|
self.kernel_stack.get_top(),
|
|
|
|
|
trap_handler as usize,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
pub fn fork(self: &Arc<TaskControlBlock>) -> Arc<TaskControlBlock> {
|
|
|
|
|
// ---- hold parent PCB lock
|
|
|
|
|
let mut parent_inner = self.inner.lock();
|
|
|
|
|
// copy user space(include trap context)
|
|
|
|
|
let memory_set = MemorySet::from_existed_user(
|
|
|
|
|
&parent_inner.memory_set
|
|
|
|
|
);
|
|
|
|
|
let trap_cx_ppn = memory_set
|
|
|
|
|
.translate(VirtAddr::from(TRAP_CONTEXT).into())
|
|
|
|
|
.unwrap()
|
|
|
|
|
.ppn();
|
|
|
|
|
let task_status = TaskStatus::Ready;
|
|
|
|
|
// alloc a pid and a kernel stack in kernel space
|
|
|
|
|
let pid_handle = pid_alloc();
|
|
|
|
|
let kernel_stack = KernelStack::new(&pid_handle);
|
|
|
|
|
let kernel_stack_top = kernel_stack.get_top();
|
|
|
|
|
// push a goto_trap_return task_cx on the top of kernel stack
|
|
|
|
|
let task_cx_ptr = kernel_stack.push_on_top(TaskContext::goto_trap_return());
|
|
|
|
|
let task_control_block = Arc::new(TaskControlBlock {
|
|
|
|
|
pid: pid_handle,
|
|
|
|
|
kernel_stack,
|
|
|
|
|
inner: Mutex::new(TaskControlBlockInner {
|
|
|
|
|
trap_cx_ppn,
|
|
|
|
|
base_size: parent_inner.base_size,
|
|
|
|
|
task_cx_ptr: task_cx_ptr as usize,
|
|
|
|
|
task_status,
|
|
|
|
|
memory_set,
|
|
|
|
|
parent: Some(Arc::downgrade(self)),
|
|
|
|
|
children: Vec::new(),
|
|
|
|
|
exit_code: 0,
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
// add child
|
|
|
|
|
parent_inner.children.push(task_control_block.clone());
|
|
|
|
|
// modify kernel_sp in trap_cx
|
|
|
|
|
// **** acquire child PCB lock
|
|
|
|
|
let trap_cx = task_control_block.acquire_inner_lock().get_trap_cx();
|
|
|
|
|
// **** release child PCB lock
|
|
|
|
|
trap_cx.kernel_sp = kernel_stack_top;
|
|
|
|
|
// return
|
|
|
|
|
task_control_block
|
|
|
|
|
// ---- release parent PCB lock
|
|
|
|
|
}
|
|
|
|
|
pub fn getpid(&self) -> usize {
|
|
|
|
|
self.pid.0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq)]
|
|
|
|
|
pub enum TaskStatus {
|
|
|
|
|
Ready,
|
|
|
|
|
Running,
|
|
|
|
|
Exited,
|
|
|
|
|
Zombie,
|
|
|
|
|
}
|