Commit 9694825f authored by Nifou's avatar Nifou

Really save the context during the timer interrupt

parent 095825f4
......@@ -15,6 +15,10 @@ pages:
image: rustlang/rust:nightly
stage: deploy
script:
# Create dummy files for binaries
- touch root/bin/hello
- touch root/bin/hello2
- touch root/bin/test-std
- cd kernel && cargo doc
- cd .. && mkdir public
- cp -r kernel/target/doc/* public
......
......@@ -46,42 +46,57 @@ fn end(id: IrqId) {
/// - Choose the next thread (with the scheduler)
/// - Load the new context
#[naked]
pub extern "x86-interrupt" fn timer_interrupt_handler(stack_frame: &mut InterruptStackFrame) {
pub extern "x86-interrupt" fn timer_interrupt_handler(_: &mut InterruptStackFrame) {
unsafe {
// Save registers which will be modified during the next lines
llvm_asm!("
sub rsp, 16
push rdi
push rsi"
: : : : "intel", "volatile");
}
// Use the kernel's page table
crate::memory::switch_to_kernel_page_table();
// Save the current context
if !FIRST_TIME.load(Ordering::Relaxed) {
// Use a closure to avoid messing up the stack (with the stack frame)
let get_kstack = || { SCHEDULER.get().current().unwrap().kstack};
unsafe {
// Save the stack frame
llvm_asm!("push r14 // Stack segment
push r12 // Stack pointer
push r11 // Rflags
push r15 // Code segment
push r13 // Instruction pointer"
llvm_asm!("
// Restore the saved registers
pop rsi
pop rdi
// Registers
mov qword ptr [r15 + 8], r11
mov qword ptr [r15 + 16], r10
mov qword ptr [r15 + 24], r9
mov qword ptr [r15 + 32], r8
mov qword ptr [r15 + 40], rsi
mov qword ptr [r15 + 48], rdi
mov qword ptr [r15 + 56], rdx
mov qword ptr [r15 + 64], rcx
mov qword ptr [r15 + 72], rbx
mov qword ptr [r15 + 80], rax
// Stack frame
mov rax, qword ptr [rsp + 16]
mov qword ptr [r15 + 88], rax
mov rax, qword ptr [rsp + 24]
mov qword ptr [r15 + 96], rax
mov rax, qword ptr [rsp + 32]
mov qword ptr [r15 + 104], rax
mov rax, qword ptr [rsp + 40]
mov qword ptr [r15 + 112], rax
mov rax, qword ptr [rsp + 48]
mov qword ptr [r15 + 120], rax"
:
: "{r13}"(stack_frame.instruction_pointer),
"{r12}"(stack_frame.stack_pointer),
"{r14}"(stack_frame.stack_segment),
"{r15}"(stack_frame.code_segment)
: "{r15}"(get_kstack())
:
: "intel", "volatile");
// Save registers
llvm_asm!("push rax
push rbx
push rcx
push rdx
push rdi
push rsi
push r8
push r9
push r10
push r11
push r15
mov r11, cr3
push r11"
: : : : "intel", "volatile");
}
}
else {
......
......@@ -18,8 +18,8 @@
//! See [OSDEV](https://wiki.osdev.org/Context_Switching)
#[allow(dead_code)]
#[repr(packed)]
#[derive(Clone, Copy)]
/// A sructure which contains all registers of the CPU
#[derive(Debug, Clone, Copy)]
/// A structure which contains all registers of the CPU
pub struct Registers {
/// The CR3 register (which contains the page table)
pub cr3: usize,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment