Simple, unsafe context switch

This commit is contained in:
Jeremy Soller 2016-08-28 18:38:53 -06:00
parent 6715d5c534
commit 6e3f59ba8e
5 changed files with 53 additions and 7 deletions

View file

@ -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()
}
}

View file

@ -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 {