From f15dd0532e618469322f72b84e094a2ca70821de Mon Sep 17 00:00:00 2001 From: Mathieu Serandour Date: Fri, 25 Mar 2022 20:44:28 +0100 Subject: [PATCH] schedule when irqs fire --- kernel/int/irq.c | 8 ++++- kernel/int/irq.s | 72 ++++++++++++++++++++++++++++++++++--------- kernel/int/syscalls.c | 0 kernel/int/syscalls.h | 0 4 files changed, 64 insertions(+), 16 deletions(-) create mode 100644 kernel/int/syscalls.c create mode 100644 kernel/int/syscalls.h diff --git a/kernel/int/irq.c b/kernel/int/irq.c index 092e842..a4795aa 100644 --- a/kernel/int/irq.c +++ b/kernel/int/irq.c @@ -3,6 +3,7 @@ #include "../lib/panic.h" #include "../lib/logging.h" #include "../lib/sprintf.h" +#include "../sched/thread.h" #include "irq.h" @@ -65,7 +66,7 @@ void release_irq(unsigned n) { } // called from irq.s -void irq_common_handler(uint8_t irq_n) { +void irq_common_handler(uint8_t irq_n, gp_regs_t* context) { assert(irq_n <= IRQ_END); assert(irq_n >= IRQ_BEGIN); // the irq_n th irq just fired @@ -79,6 +80,11 @@ void irq_common_handler(uint8_t irq_n) { } + sched_save(context); + handler(driver); + + if(!schedule()) + return; } diff --git a/kernel/int/irq.s b/kernel/int/irq.s index f22968c..8e954c3 100644 --- a/kernel/int/irq.s +++ b/kernel/int/irq.s @@ -1,20 +1,29 @@ [extern irq_common_handler] + ; macro argument: irq number %macro create_irq 1 [extern _irq_handler%1] _irq_handler%1: - ; enter stack frame - ; push rbp - ; mov rbp, rsp +; already pushed stuf: +; uint64_t RIP; +; uint64_t CS; +; uint64_t RFLAGS; +; uint64_t RSP; +; uint64_t SS; ; enter stack frame push rbp - mov rbp, rsp - push rdi - mov dil, byte %1 + ; save stack pointer + ; save frame pointer and enter stack frame +; mov rbp, rsp + + ; save rax + push rax + mov al, byte %1 + jmp common_stub @@ -41,33 +50,66 @@ create_irq 47 common_stub: - push rax +; clear DF flag +; DF need to be clear on function +; entry and exit (System-V ABI) + cld + + ; save context + ; we already saved rsp, rbp, rax + ; here, al = the irq handler number + push rcx push rdx + push rbx push rsi + push rdi + push r8 push r9 push r10 push r11 + push r12 + push r13 + push r14 + push r15 + + + + ; put the irq handler number in dil: the + ; argument for the irq_common_handler function + mov dil, al + + ; beginning of the interrupt stack = beginning of + ; the context structure + mov rsi, rsp + -; clear DF flag -; DF need to be clear on function -; entry and exit (System-V ABI) - cld call irq_common_handler + ; restore the context + ; this function only returns + ; if no rescheduling is done + + + pop r15 + pop r14 + pop r13 + pop r12 pop r11 pop r10 pop r9 pop r8 + + pop rdi pop rsi + pop rbx pop rdx pop rcx pop rax - pop rdi - ; mov rsp, rbp - ; pop rbp - leave + pop rbp + + iretq \ No newline at end of file diff --git a/kernel/int/syscalls.c b/kernel/int/syscalls.c new file mode 100644 index 0000000..e69de29 diff --git a/kernel/int/syscalls.h b/kernel/int/syscalls.h new file mode 100644 index 0000000..e69de29