From 2c3b7be962ba222da5a7cb642f5f2c74b13e996f Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Mon, 13 Nov 2023 22:37:19 -0500 Subject: [PATCH] sched: save registers and IRETQ frame on IRQ --- include/kernel/proc.h | 9 ++++--- sched/entry.S | 58 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/include/kernel/proc.h b/include/kernel/proc.h index e9a2944..044cc0c 100644 --- a/include/kernel/proc.h +++ b/include/kernel/proc.h @@ -23,13 +23,14 @@ struct procregs { uint64_t r13; uint64_t r14; uint64_t r15; - uint64_t rip; + /* IRETQ stack frame */ + uint64_t rip; + uint64_t cs; uint64_t rflags; uint64_t rsp; - uint16_t fs; - uint16_t gs; - uint16_t cs; + uint64_t ss; + } PACKED; struct proc { diff --git a/sched/entry.S b/sched/entry.S index 78bfe9b..4f62feb 100644 --- a/sched/entry.S +++ b/sched/entry.S @@ -139,8 +139,56 @@ irq 0x0F .global _irq_schedule _irq_schedule: cli - push_regs - movq $0x00, %rdi - call irq_schedule - pop_regs - iretq + /* push scratch registers */ + pushq %rax + pushq %rcx + pushq %rdx + pushq %rsi + pushq %rdi + pushq %r8 + pushq %r9 + pushq %r10 + pushq %r11 + + /* get current task registers */ + call proc_current_regs + + /* save current task registers */ + popq %rdi + movq %rdi, 80(%rax) /* r11 */ + popq %rdi + movq %rdi, 72(%rax) /* r10 */ + popq %rdi + movq %rdi, 64(%rax) /* r9 */ + popq %rdi + movq %rdi, 56(%rax) /* r8 */ + popq %rdi + movq %rdi, 40(%rax) /* rdi */ + popq %rdi + movq %rdi, 32(%rax) /* rsi */ + popq %rdi + movq %rdi, 24(%rax) /* rdx */ + popq %rdi + movq %rdi, 16(%rax) /* rcx */ + popq %rdi + movq %rdi, 0(%rax) /* rax */ + + /* save remaining registers */ + movq %rbx, 8(%rax) + movq %rbp, 48(%rax) + movq %r12, 88(%rax) + movq %r13, 96(%rax) + movq %r14, 104(%rax) + movq %r15, 112(%rax) + + /* save IRETQ frame */ + popq %rdi + movq %rdi, 120(%rax) + popq %rdi + movq %rdi, 128(%rax) + popq %rdi + movq %rdi, 136(%rax) + popq %rdi + movq %rdi, 144(%rax) + popq %rdi + movq %rdi, 152(%rax)