diff --git a/src/arch/x86/interrupt/handler.rs b/src/arch/x86/interrupt/handler.rs index e996f69..358e232 100644 --- a/src/arch/x86/interrupt/handler.rs +++ b/src/arch/x86/interrupt/handler.rs @@ -70,6 +70,7 @@ impl IretRegisters { #[derive(Default)] #[repr(packed)] pub struct InterruptStack { + pub gs: usize, pub preserved: PreservedRegisters, pub scratch: ScratchRegisters, pub iret: IretRegisters, @@ -204,6 +205,22 @@ macro_rules! pop_preserved { " }; } +macro_rules! enter_gs { + () => { " + // Enter kernel GS segment + push gs + push 0x18 + pop gs + " } +} + +macro_rules! exit_gs { + () => { " + // Exit kernel GS segment + pop gs + " } +} + #[macro_export] macro_rules! interrupt_stack { // XXX: Apparently we cannot use $expr and check for bool exhaustiveness, so we will have to @@ -234,6 +251,9 @@ macro_rules! interrupt_stack { push_scratch!(), push_preserved!(), + // Enter kernel TLS segment + enter_gs!(), + // TODO: Map PTI // $crate::arch::x86::pti::map(); @@ -247,6 +267,9 @@ macro_rules! interrupt_stack { // TODO: Unmap PTI // $crate::arch::x86::pti::unmap(); + // Exit kernel TLS segment + exit_gs!(), + // Restore all userspace registers pop_preserved!(), pop_scratch!(), @@ -279,6 +302,9 @@ macro_rules! interrupt { "push eax\n", push_scratch!(), + // Enter kernel TLS segment + enter_gs!(), + // TODO: Map PTI // $crate::arch::x86::pti::map(); @@ -288,6 +314,9 @@ macro_rules! interrupt { // TODO: Unmap PTI // $crate::arch::x86::pti::unmap(); + // Exit kernel TLS segment + exit_gs!(), + // Restore all userspace registers pop_scratch!(), @@ -336,6 +365,9 @@ macro_rules! interrupt_error { push_scratch!(), push_preserved!(), + // Enter kernel TLS segment + enter_gs!(), + // Put code in, it's now in eax "push eax\n", @@ -355,6 +387,9 @@ macro_rules! interrupt_error { // Pop code "add esp, 8\n", + // Exit kernel TLS segment + exit_gs!(), + // Restore all userspace registers pop_preserved!(), pop_scratch!(), diff --git a/src/arch/x86/start.rs b/src/arch/x86/start.rs index 511fe83..1a29189 100644 --- a/src/arch/x86/start.rs +++ b/src/arch/x86/start.rs @@ -323,7 +323,7 @@ pub unsafe extern "C" fn usermode(_ip: usize, _sp: usize, _arg: usize, _is_singl mov ds, eax mov es, eax mov fs, eax - // gs keeps kernel selector for simplicity + mov gs, eax // Set up iret stack push eax // stack selector