From 603a8064a3c851ea52adbbd3b5fafe9a2ffa470b Mon Sep 17 00:00:00 2001 From: Mark Date: Thu, 30 Jan 2020 12:07:15 +0200 Subject: [PATCH] Thread timer-based preemption --- sys/amd64/hw/irq0.S | 21 +++++++++++++++++++++ sys/sched.c | 4 ++++ 2 files changed, 25 insertions(+) diff --git a/sys/amd64/hw/irq0.S b/sys/amd64/hw/irq0.S index 909a96f..a176cce 100644 --- a/sys/amd64/hw/irq0.S +++ b/sys/amd64/hw/irq0.S @@ -17,9 +17,30 @@ _msg0: .string "CPU stopped!\n" amd64_irq0: cli + + // Push caller-saved registers so it appears as if a thread just called yield() + pushq %r11 + pushq %r10 + pushq %r9 + pushq %r8 + pushq %rcx + pushq %rdx + pushq %rsi + pushq %rdi pushq %rax irq_eoi_lapic 0 + + call yield + popq %rax + popq %rdi + popq %rsi + popq %rdx + popq %rcx + popq %r8 + popq %r9 + popq %r10 + popq %r11 iretq amd64_irq2_counting: diff --git a/sys/sched.c b/sys/sched.c index c2d3b00..3c05c91 100644 --- a/sys/sched.c +++ b/sys/sched.c @@ -1,4 +1,6 @@ #include "sys/amd64/mm/phys.h" +#include "sys/amd64/hw/irq.h" +#include "sys/amd64/hw/idt.h" #include "sys/assert.h" #include "sys/thread.h" #include "sys/mm.h" @@ -164,5 +166,7 @@ void sched_init(void) { } void sched_enter(void) { + extern void amd64_irq0(void); + amd64_idt_set(0, 32, (uintptr_t) amd64_irq0, 0x08, IDT_FLG_P | IDT_FLG_R0 | IDT_FLG_INT32); context_switch_first(&sched_threads[0]); }