Allow cloning of kernel threads. Userspace breaks potentially due to stack aliasing

This commit is contained in:
Jeremy Soller 2016-09-13 20:06:39 -06:00
parent 4341a2d725
commit ce50faf7ca
6 changed files with 109 additions and 38 deletions

View file

@ -141,11 +141,25 @@ pub unsafe fn switch() {
let mut to_ptr = 0 as *mut Context;
for (_pid, context_lock) in contexts().map.iter() {
let mut context = context_lock.write();
if ! context.running && ! context.blocked && ! context.exited {
to_ptr = context.deref_mut() as *mut Context;
break;
for (pid, context_lock) in contexts().map.iter() {
if *pid > (*from_ptr).id {
let mut context = context_lock.write();
if ! context.running && ! context.blocked && ! context.exited {
to_ptr = context.deref_mut() as *mut Context;
break;
}
}
}
if to_ptr as usize == 0 {
for (pid, context_lock) in contexts().map.iter() {
if *pid < (*from_ptr).id {
let mut context = context_lock.write();
if ! context.running && ! context.blocked && ! context.exited {
to_ptr = context.deref_mut() as *mut Context;
break;
}
}
}
}
@ -159,6 +173,9 @@ pub unsafe fn switch() {
(&mut *from_ptr).running = false;
(&mut *to_ptr).running = true;
if let Some(ref stack) = (*to_ptr).kstack {
arch::gdt::TSS.rsp[0] = (stack.as_ptr() as usize + stack.len() - 256) as u64;
}
CONTEXT_ID.store((&mut *to_ptr).id, Ordering::SeqCst);
(&mut *from_ptr).arch.switch_to(&mut (&mut *to_ptr).arch);