Skip to content
135 changes: 73 additions & 62 deletions xv7-kernel/src/arch/x86_64/context.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use core::mem::{offset_of, size_of};

use super::trapframe::{TrapFrame, trap_return};
use crate::paging::VirtAddr;

#[derive(Debug)]
/// Pure kernel-mode coroutine context (callee-saved registers + `%rsp`).
///
/// Address space (`CR3`), privilege entry stack (`PerCpu` / `TSS.rsp0`), and
/// interrupt masking (`IF == 0` across context switches) are managed orthogonally
/// by the scheduler and `TrapFrame`.
#[derive(Debug, Default)]
#[repr(C)]
pub struct Context {
pub cr3: usize,
pub rsp: usize,
pub rflags: usize,
pub r15: usize,
pub r14: usize,
pub r13: usize,
Expand All @@ -14,77 +20,82 @@ pub struct Context {
pub rbx: usize,
}

#[must_use = "call `.to(target)` to complete the context switch"]
pub struct SwitchFrom(*mut Context);

impl SwitchFrom {
/// Saves the current CPU callee-saved registers + `%rsp` into `from` and
/// restores execution from `to`.
#[inline(always)]
pub unsafe fn to(self, to: *const Context) {
unsafe {
Context::switch_raw(self.0, to);
}
}
}

impl Context {
pub const fn new() -> Context {
Context {
cr3: 0,
rflags: 0,
rbx: 0,
r12: 0,
r13: 0,
r14: 0,
pub const fn new() -> Self {
Self {
rsp: 0,
r15: 0,
r14: 0,
r13: 0,
r12: 0,
rbp: 0,
rsp: 0,
rbx: 0,
}
}

pub const fn user(stack_pointer: VirtAddr) -> Context {
Context {
cr3: 0,
rflags: 0x282,
rbx: 0,
r12: 0,
r13: 0,
r14: 0,
r15: 0,
rbp: 0,
rsp: stack_pointer.as_u64() as usize,
/// Initializes a kernel context at `kstack_top` that reserves a `TrapFrame`
/// at the top of the stack and returns directly into `trap_return` when first scheduled.
pub fn init_user(kstack_top: VirtAddr) -> Self {
let tf_addr = kstack_top - size_of::<TrapFrame>() as u64;
let ret_slot = tf_addr - size_of::<usize>() as u64;
unsafe {
tf_addr.as_mut_ptr::<TrapFrame>().write(TrapFrame::default());
ret_slot
.as_mut_ptr::<usize>()
.write(trap_return as *const () as usize);
}
Self {
rsp: ret_slot.as_u64() as usize,
..Self::new()
}
}

/// Begins a context switch saving the current execution state into `from`.
#[inline(always)]
pub fn switch_from(from: *mut Context) -> SwitchFrom {
SwitchFrom(from)
}

#[unsafe(naked)]
pub unsafe extern "C" fn switch_to(&mut self, _next: &Context) {
unsafe extern "C" fn switch_raw(from: *mut Context, to: *const Context) {
core::arch::naked_asm!(
"
mov rcx, cr3
mov [rdi], rcx
mov rax, [rsi]
cmp rax, rcx

je switch_to.same_cr3
mov cr3, rax

switch_to.same_cr3:
pushfq
pop QWORD PTR [rdi + 0x10]

push QWORD PTR [rsi + 0x10]
popfq

mov [rdi + 0x18], r15
mov r15, [rsi + 0x18]

mov [rdi + 0x20], r14
mov r14, [rsi + 0x20]

mov [rdi + 0x28], r13
mov r13, [rsi + 0x28]

mov [rdi + 0x30], r12
mov r12, [rsi + 0x30]

mov [rdi + 0x38], rbp
mov rbp, [rsi + 0x38]

mov [rdi + 0x40], rbx
mov rbx, [rsi + 0x40]

mov [rdi + 0x08], rsp
mov rsp, [rsi + 0x08]
"mov [rdi + {r15}], r15",
"mov [rdi + {r14}], r14",
"mov [rdi + {r13}], r13",
"mov [rdi + {r12}], r12",
"mov [rdi + {rbp}], rbp",
"mov [rdi + {rbx}], rbx",
"mov [rdi + {rsp}], rsp",

ret
",
"mov r15, [rsi + {r15}]",
"mov r14, [rsi + {r14}]",
"mov r13, [rsi + {r13}]",
"mov r12, [rsi + {r12}]",
"mov rbp, [rsi + {rbp}]",
"mov rbx, [rsi + {rbx}]",
"mov rsp, [rsi + {rsp}]",
"ret",
rsp = const offset_of!(Context, rsp),
r15 = const offset_of!(Context, r15),
r14 = const offset_of!(Context, r14),
r13 = const offset_of!(Context, r13),
r12 = const offset_of!(Context, r12),
rbp = const offset_of!(Context, rbp),
rbx = const offset_of!(Context, rbx),
);
}
}
20 changes: 9 additions & 11 deletions xv7-kernel/src/arch/x86_64/gdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use x86_64::structures::tss::TaskStateSegment;
pub const DOUBLE_FAULT_IST_INDEX: u16 = 0;
pub const PAGE_FAULT_IST_INDEX: u16 = 1;

pub static TSS: SyncUnsafeCell<TaskStateSegment> = SyncUnsafeCell::new(TaskStateSegment::new());
static TSS: SyncUnsafeCell<TaskStateSegment> = SyncUnsafeCell::new(TaskStateSegment::new());

lazy_static! {
pub static ref GDT: (GlobalDescriptorTable, Selectors) = {
Expand Down Expand Up @@ -48,9 +48,8 @@ lazy_static! {
Descriptor::UserSegment(flags.bits())
};

// The order is required.
// Order required by `STAR` MSR (`sysret`).
let kernel_code_selector = gdt.append(kernel_code);

let kernel_data_selector = gdt.append(kernel_data);

let mut user_data_selector = gdt.append(user_data);
Expand All @@ -61,14 +60,6 @@ lazy_static! {

let tss = unsafe {
let tss = &mut *TSS.get();
tss.privilege_stack_table[0] = {
const STACK_SIZE: usize = 4096;
static STACK: SyncUnsafeCell<[u8; STACK_SIZE]> =
SyncUnsafeCell::new([0; STACK_SIZE]);

let stack_start = VirtAddr::from_ptr(STACK.get());
stack_start + STACK_SIZE as u64
};
tss.interrupt_stack_table[DOUBLE_FAULT_IST_INDEX as usize] = {
const STACK_SIZE: usize = 4096;
static STACK: SyncUnsafeCell<[u8; STACK_SIZE]> =
Expand Down Expand Up @@ -123,3 +114,10 @@ pub fn init() {
load_tss(GDT.1.tss_selector);
}
}

/// Updates `TSS.privilege_stack_table[0]` (`RSP0`) for hardware Ring 3 -> Ring 0 transitions.
pub fn set_tss_rsp0(kstack_top: VirtAddr) {
unsafe {
(*TSS.get()).privilege_stack_table[0] = kstack_top;
}
}
9 changes: 0 additions & 9 deletions xv7-kernel/src/arch/x86_64/interrupt/timer.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
use crate::cpu::my_cpu;

use super::controller::LOCAL_APIC;
use x86_64::structures::idt::InterruptStackFrame;

pub extern "x86-interrupt" fn handler(_stack_frame: InterruptStackFrame) {
LOCAL_APIC.lock().end_of_interrupt();

let cpu = my_cpu();
if cpu.current_process.is_some() {
//unsafe {
// cpu.switch_to_kernel();
//}
}
}
42 changes: 0 additions & 42 deletions xv7-kernel/src/arch/x86_64/macros.rs

This file was deleted.

16 changes: 13 additions & 3 deletions xv7-kernel/src/arch/x86_64/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#[macro_use]
mod macros;

pub mod allocator;
pub mod config;
mod console;
Expand All @@ -10,8 +7,21 @@ pub mod device;
pub mod gdt;
pub mod interrupt;
pub mod paging;
pub mod percpu;
mod start;
pub mod syscall;
pub mod trapframe;

pub use interrupt::{disable as disable_interrupts, without_interrupts};
pub use percpu::set_kernel_stack;

/// Atomically enables interrupts, halts until the next hardware interrupt,
/// and disables interrupts again upon wakeup.
#[inline]
pub fn wait_for_interrupt() {
interrupt::enable_and_hlt();
interrupt::disable();
}

#[inline(always)]
pub fn idle() -> ! {
Expand Down
Loading
Loading