From d495a1745c83a8186bf1c6c531177887dd245436 Mon Sep 17 00:00:00 2001 From: Tianhao Wang Date: Thu, 6 Jun 2024 02:08:21 +0200 Subject: proc: basic infra for multithreading including task and scheduler wrapper, context swap assembly, and some notes... The lifetime of task is tricky, I'll fix it later Signed-off-by: Tianhao Wang --- src/proc/task.rs | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) (limited to 'src/proc/task.rs') diff --git a/src/proc/task.rs b/src/proc/task.rs index 0ff2065..5cbe2f6 100644 --- a/src/proc/task.rs +++ b/src/proc/task.rs @@ -1,4 +1,7 @@ use crate::arch::x86_64::arch_regs; +use crate::defs::*; +use crate::io::*; +use core::arch::asm; /// currently only kernelSp and Context are important. /// the task struct will be placed on the starting addr (low addr) of the kernel stack. @@ -6,8 +9,48 @@ use crate::arch::x86_64::arch_regs; #[repr(C)] #[repr(packed)] pub struct Task { + pub magic: u64, pub task_id: u32, pub kernel_stack: u64, - pub user_stack: u64, + // pub user_stack: u64, pub context: arch_regs::Context64, + pub state: TaskState, +} + +pub enum TaskState { + Run, + Block, + Dead, + Eating, + Purr, + Meow, + Angry, +} + +#[no_mangle] +pub extern "C" fn _task_entry() -> ! { + println!("I'm Mr.Meeseeks, look at me~"); + unsafe { asm!("hlt") }; + panic!("shoud not reach"); +} + +extern "C" { + fn context_swap(from_ctx: u64, to_ctx: u64); + fn context_swap_to(to_ctx: u64); +} + +impl Task { + /// create new task. This is tricky because the task struct sits at the + /// bottom of the kernel stack, so that you can get the current task by + /// masking the stack pointer. + /// 1. allocate the kernel stack with predefined size and make sure the + /// address is properly aligned + /// 2. cast a task struct onto the stack + /// 3. set whatever fields necessary in the task struct, including a magic + /// 4. create a new stack frame, and update the stack pointer in the context, + /// so that when first swapped to, the task will immediately "return to" + /// the _func_ptr + pub fn new(_id: u32, _func_ptr: u64) -> Self { + todo!() + } } -- cgit v1.2.3-70-g09d2