aboutsummaryrefslogtreecommitdiff
path: root/src/proc
diff options
context:
space:
mode:
Diffstat (limited to 'src/proc')
-rw-r--r--src/proc/mod.rs1
-rw-r--r--src/proc/sched.rs17
-rw-r--r--src/proc/task.rs45
3 files changed, 62 insertions, 1 deletions
diff --git a/src/proc/mod.rs b/src/proc/mod.rs
index cdafe4a..020ac83 100644
--- a/src/proc/mod.rs
+++ b/src/proc/mod.rs
@@ -1 +1,2 @@
+pub mod sched;
pub mod task;
diff --git a/src/proc/sched.rs b/src/proc/sched.rs
new file mode 100644
index 0000000..8e86c7f
--- /dev/null
+++ b/src/proc/sched.rs
@@ -0,0 +1,17 @@
+use crate::proc::task::*;
+use alloc::collections::linked_list::LinkedList;
+// TODO the lifetime here is pretty much broken. Fix this later
+pub struct Scheduler<'a> {
+ run_list: LinkedList<&'a Task>,
+}
+
+impl<'a> Scheduler<'a> {
+ #[inline]
+ pub fn pop_front(&mut self) -> Option<&Task> {
+ self.run_list.pop_front()
+ }
+ #[inline]
+ pub fn push_back(&mut self, t: &'a Task) {
+ self.run_list.push_back(t);
+ }
+}
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!()
+ }
}