From 82893fa3a44c554fc042d50b3f98ae4cf88a7fdb Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 23 Nov 2022 19:34:04 +0100 Subject: [PATCH] Introduce a kernel_yield function to use once a scheduler is implemented --- kernel/src/arch/CPU.h | 4 ++- kernel/src/arch/x86_64/CPU.asm | 46 ++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/kernel/src/arch/CPU.h b/kernel/src/arch/CPU.h index daad9bd0..1558551a 100644 --- a/kernel/src/arch/CPU.h +++ b/kernel/src/arch/CPU.h @@ -18,4 +18,6 @@ namespace CPU void enable_interrupts(); void disable_interrupts(); void wait_for_interrupt(); -} \ No newline at end of file +} + +extern "C" void kernel_yield(); \ No newline at end of file diff --git a/kernel/src/arch/x86_64/CPU.asm b/kernel/src/arch/x86_64/CPU.asm index c5b37810..2ecc7f73 100644 --- a/kernel/src/arch/x86_64/CPU.asm +++ b/kernel/src/arch/x86_64/CPU.asm @@ -48,6 +48,51 @@ load_tr: ltr ax ret +global switch_task +switch_task: + cli +.loop: + hlt + jmp .loop + +global kernel_yield +kernel_yield: + mov rdi, [rsp] ; return address is now in RDI + mov rcx, rsp ; save current RSP + add rcx, 8 ; skip over the return address + + mov eax, ss + push rax ; SS + push rcx ; RSP + pushfq ; RFLAGS + mov eax, cs + push rax ; CS + push rdi ; RIP + + ; ISR and error code + push 0 + push 0 + + push 0 ; RAX + push rbx ; Preserve RBX + push 0 ; RCX + push 0 ; RDX + push 0 ; RSI + push 0 ; RDI + push rbp ; Preserve RBP + push 0 ; R8 + push 0 ; R9 + push 0 ; R10 + push 0 ; R11 + push r12 ; Preserve R12 + push r13 ; Preserve R13 + push r14 ; Preserve R14 + push r15 ; Preserve R15 + + mov rdi, rsp + call switch_task + jmp _asm_interrupt_exit + %macro ISR 1 global _isr%1 _isr%1: @@ -97,6 +142,7 @@ _asm_interrupt_entry: mov rdi, rsp call arch_interrupt_entry +_asm_interrupt_exit: pop r15 pop r14 pop r13