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

@ -6,7 +6,7 @@ use core::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering};
/// This must be done, as no locks can be held on the stack during switch
pub static CONTEXT_SWITCH_LOCK: AtomicBool = ATOMIC_BOOL_INIT;
#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct Context {
/// Page table pointer
cr3: usize,
@ -96,5 +96,7 @@ impl Context {
// Unset global lock, set inside of kernel
CONTEXT_SWITCH_LOCK.store(false, Ordering::SeqCst);
asm!("xchg bx, bx" : : : : "intel", "volatile");
}
}

View file

@ -3,7 +3,7 @@ pub unsafe extern fn syscall() {
#[inline(never)]
unsafe fn inner() {
extern {
fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize) -> usize;
fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, stack: usize) -> usize;
}
let mut a;
@ -13,10 +13,11 @@ pub unsafe extern fn syscall() {
let d;
let e;
let f;
asm!("" : "={rax}"(a), "={rbx}"(b), "={rcx}"(c), "={rdx}"(d), "={rsi}"(e), "={rdi}"(f)
let stack;
asm!("" : "={rax}"(a), "={rbx}"(b), "={rcx}"(c), "={rdx}"(d), "={rsi}"(e), "={rdi}"(f), "={rbp}"(stack)
: : : "intel", "volatile");
a = syscall(a, b, c, d, e, f);
a = syscall(a, b, c, d, e, f, stack);
}
asm!("" : : "{rax}"(a) : : "intel", "volatile");
@ -36,3 +37,10 @@ pub unsafe extern fn syscall() {
iretq"
: : : : "intel", "volatile");
}
#[naked]
pub unsafe extern fn clone_ret() -> usize {
asm!("pop rbp"
: : : : "intel", "volatile");
0
}

View file

@ -187,8 +187,7 @@ pub unsafe extern fn kstart_ap(cpu_id: usize, page_table: usize, stack_start: us
pub unsafe fn usermode(ip: usize, sp: usize) -> ! {
// Go to usermode
asm!("xchg bx, bx
mov ds, ax
asm!("mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
@ -201,7 +200,7 @@ pub unsafe fn usermode(ip: usize, sp: usize) -> ! {
: // No output because it never returns
: "{rax}"(gdt::GDT_USER_DATA << 3 | 3), // Stack segment
"{rbx}"(sp), // Stack pointer
"{rcx}"(3 << 12 | 1 << 9), // Flags - Set IOPL and interrupt enable flag
"{rcx}"(3 << 12/* | 1 << 9*/), // Flags - Set IOPL and interrupt enable flag
"{rdx}"(gdt::GDT_USER_CODE << 3 | 3), // Code segment
"{rsi}"(ip) // IP
: // No clobers because it never returns