diff --git a/context/mod.rs b/context/mod.rs index 3e5e84913a55c50a07612da1bb811acd69d93b0b..54877809c4ba230c38bb7cacc7ba727a46ae9a56 100644 --- a/context/mod.rs +++ b/context/mod.rs @@ -1,6 +1,8 @@ //! Context management +use alloc::boxed::Box; use collections::{BTreeMap, Vec}; +use core::mem; use core::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering}; use spin::{Once, RwLock, RwLockReadGuard, RwLockWriteGuard}; @@ -61,6 +63,15 @@ impl ContextList { let context_lock = self.new_context()?; { let mut context = context_lock.write(); + let mut stack = Box::new([0; 4096]); + let offset = stack.len() - mem::size_of::<usize>(); + unsafe { + let offset = stack.len() - mem::size_of::<usize>(); + let func_ptr = stack.as_mut_ptr().offset(offset as isize); + *(func_ptr as *mut usize) = func as usize; + } + context.arch.set_stack(stack.as_ptr() as usize + offset); + context.kstack = Some(stack); print!("{}", format!("{}: {:X}\n", context.id, func as usize)); } Ok(context_lock) @@ -108,6 +119,8 @@ pub struct Context { pub id: usize, /// The architecture specific context pub arch: ArchContext, + /// Kernel stack + pub kstack: Option<Box<[u8]>>, /// The open files in the scheme pub files: Vec<Option<file::File>> } @@ -118,6 +131,7 @@ impl Context { Context { id: id, arch: ArchContext::new(), + kstack: None, files: Vec::new() } } diff --git a/lib.rs b/lib.rs index 3b1d6eb25843441170fb32237c19d043ba62e154..acdf02881614c22ba1dc48068faeb526a0605706 100644 --- a/lib.rs +++ b/lib.rs @@ -134,8 +134,24 @@ pub extern fn kmain() { print!("{}", format!("BSP: {:?}\n", syscall::getpid())); - if let Ok(context) = context::contexts_mut().spawn(context_test) { - + let to_ptr = if let Ok(context_lock) = context::contexts_mut().spawn(context_test) { + print!("Spawned context\n"); + let mut context = context_lock.write(); + &mut context.arch as *mut arch::context::Context + } else { + 0 as *mut arch::context::Context + }; + + let from_ptr = if let Some(context_lock) = context::contexts().current() { + let mut context = context_lock.write(); + &mut context.arch as *mut arch::context::Context + } else { + 0 as *mut arch::context::Context + }; + + if to_ptr as usize != 0 && from_ptr as usize != 0 { + print!("Switching\n"); + unsafe { (&mut *from_ptr).switch_to(&mut *to_ptr); } } loop {