From 8c32471e0d7f629de908a70173dd06fd3af3905a Mon Sep 17 00:00:00 2001 From: "Justin C. Miller" Date: Thu, 7 Feb 2019 17:47:42 -0800 Subject: [PATCH] Pass CPU state as a pointer Previously CPU statue was passed on the stack, but the compiler is allowed to clobber values passed to it on the stack in the SysV x86 ABI. So now leave the state on the stack but pass a pointer to it into the ISR functions. --- src/kernel/interrupts.cpp | 56 +++++++++++++++++++-------------------- src/kernel/interrupts.s | 6 +++-- src/kernel/scheduler.h | 6 ++--- src/kernel/syscall.cpp | 6 ++--- src/kernel/syscall.h | 2 +- 5 files changed, 39 insertions(+), 37 deletions(-) diff --git a/src/kernel/interrupts.cpp b/src/kernel/interrupts.cpp index 9c2f50b..dc54281 100644 --- a/src/kernel/interrupts.cpp +++ b/src/kernel/interrupts.cpp @@ -18,8 +18,8 @@ static const uint16_t PIC2 = 0xa0; extern "C" { void _halt(); - uintptr_t isr_handler(uintptr_t, cpu_state); - uintptr_t irq_handler(uintptr_t, cpu_state); + uintptr_t isr_handler(uintptr_t, cpu_state*); + uintptr_t irq_handler(uintptr_t, cpu_state*); uintptr_t syscall_handler(uintptr_t, cpu_state); #define ISR(i, name) extern void name (); @@ -105,23 +105,23 @@ interrupts_init() } uintptr_t -isr_handler(uintptr_t return_rsp, cpu_state regs) +isr_handler(uintptr_t return_rsp, cpu_state *regs) { console *cons = console::get(); - switch (static_cast(regs.interrupt & 0xff)) { + switch (static_cast(regs->interrupt & 0xff)) { case isr::isrGPFault: { cons->set_color(9); cons->puts("\nGeneral Protection Fault:\n"); cons->set_color(); - cons->printf(" errorcode: %lx", regs.errorcode); - if (regs.errorcode & 0x01) cons->puts(" external"); + cons->printf(" errorcode: %lx", regs->errorcode); + if (regs->errorcode & 0x01) cons->puts(" external"); - int index = (regs.errorcode & 0xffff) >> 4; + int index = (regs->errorcode & 0xffff) >> 4; if (index) { - switch ((regs.errorcode & 0x07) >> 1) { + switch ((regs->errorcode & 0x07) >> 1) { case 0: cons->printf(" GDT[%x]\n", index); gdt_dump(); @@ -140,10 +140,10 @@ isr_handler(uintptr_t return_rsp, cpu_state regs) } else { cons->putc('\n'); } - print_regs(regs); + print_regs(*regs); /* print_stacktrace(2); - print_stack(regs); + print_stack(*regs); */ } @@ -156,19 +156,19 @@ isr_handler(uintptr_t return_rsp, cpu_state regs) cons->set_color(); cons->puts(" flags:"); - if (regs.errorcode & 0x01) cons->puts(" present"); - if (regs.errorcode & 0x02) cons->puts(" write"); - if (regs.errorcode & 0x04) cons->puts(" user"); - if (regs.errorcode & 0x08) cons->puts(" reserved"); - if (regs.errorcode & 0x10) cons->puts(" ip"); + if (regs->errorcode & 0x01) cons->puts(" present"); + if (regs->errorcode & 0x02) cons->puts(" write"); + if (regs->errorcode & 0x04) cons->puts(" user"); + if (regs->errorcode & 0x08) cons->puts(" reserved"); + if (regs->errorcode & 0x10) cons->puts(" ip"); cons->puts("\n"); uint64_t cr2 = 0; __asm__ __volatile__ ("mov %%cr2, %0" : "=r"(cr2)); print_reg("cr2", cr2); - print_reg("rsp", regs.user_rsp); - print_reg("rip", regs.rip); + print_reg("rsp", regs->user_rsp); + print_reg("rip", regs->rip); cons->puts("\n"); print_stacktrace(2); @@ -192,14 +192,14 @@ isr_handler(uintptr_t return_rsp, cpu_state regs) case isr::isrAssert: { cons->set_color(); - print_regs(regs); + print_regs(*regs); print_stacktrace(2); } _halt(); break; case isr::isrSyscall: { - return_rsp = syscall_dispatch(return_rsp, regs); + return_rsp = syscall_dispatch(return_rsp, *regs); } break; @@ -215,7 +215,7 @@ isr_handler(uintptr_t return_rsp, cpu_state regs) case isr::isrIgnore5: case isr::isrIgnore6: case isr::isrIgnore7: - //cons->printf("\nIGNORED: %02x\n", regs.interrupt); + //cons->printf("\nIGNORED: %02x\n", regs->interrupt); outb(PIC1, 0x20); break; @@ -227,7 +227,7 @@ isr_handler(uintptr_t return_rsp, cpu_state regs) case isr::isrIgnoreD: case isr::isrIgnoreE: case isr::isrIgnoreF: - //cons->printf("\nIGNORED: %02x\n", regs.interrupt); + //cons->printf("\nIGNORED: %02x\n", regs->interrupt); outb(PIC1, 0x20); outb(PIC2, 0x20); break; @@ -235,13 +235,13 @@ isr_handler(uintptr_t return_rsp, cpu_state regs) default: cons->set_color(9); cons->printf("\nReceived %02x interrupt:\n", - (static_cast(regs.interrupt))); + (static_cast(regs->interrupt))); cons->set_color(); cons->printf(" ISR: %02lx ERR: %lx\n\n", - regs.interrupt, regs.errorcode); + regs->interrupt, regs->errorcode); - print_regs(regs); + print_regs(*regs); //print_stacktrace(2); _halt(); } @@ -251,16 +251,16 @@ isr_handler(uintptr_t return_rsp, cpu_state regs) } uintptr_t -irq_handler(uintptr_t return_rsp, cpu_state regs) +irq_handler(uintptr_t return_rsp, cpu_state *regs) { console *cons = console::get(); - uint8_t irq = get_irq(regs.interrupt); + uint8_t irq = get_irq(regs->interrupt); if (! device_manager::get().dispatch_irq(irq)) { cons->set_color(11); cons->printf("\nReceived unknown IRQ: %d (vec %d)\n", - irq, regs.interrupt); + irq, regs->interrupt); cons->set_color(); - print_regs(regs); + print_regs(*regs); _halt(); } diff --git a/src/kernel/interrupts.s b/src/kernel/interrupts.s index f7fb0dc..9013898 100644 --- a/src/kernel/interrupts.s +++ b/src/kernel/interrupts.s @@ -6,6 +6,7 @@ isr_handler_prelude: push_all_and_segments mov rdi, rsp + mov rsi, rsp call isr_handler mov rsp, rax @@ -21,6 +22,7 @@ irq_handler_prelude: push_all_and_segments mov rdi, rsp + mov rsi, rsp call irq_handler mov rsp, rax @@ -56,8 +58,8 @@ irq_handler_prelude: jmp irq_handler_prelude %endmacro -%define EISR(i, name) EMIT_EISR name, i -%define UISR(i, name) EMIT_ISR name, i +%define EISR(i, name) EMIT_EISR name, i ; ISR with error code +%define UISR(i, name) EMIT_ISR name, i ; ISR callable from user space %define ISR(i, name) EMIT_ISR name, i %define IRQ(i, q, name) EMIT_IRQ name, i diff --git a/src/kernel/scheduler.h b/src/kernel/scheduler.h index f67a89e..2253473 100644 --- a/src/kernel/scheduler.h +++ b/src/kernel/scheduler.h @@ -10,7 +10,7 @@ class lapic; struct page_table; struct cpu_state; -extern "C" uintptr_t isr_handler(uintptr_t, cpu_state); +extern "C" uintptr_t isr_handler(uintptr_t, cpu_state*); /// The task scheduler @@ -59,8 +59,8 @@ public: static scheduler & get() { return s_instance; } private: - friend uintptr_t syscall_dispatch(uintptr_t, const cpu_state &); - friend uintptr_t isr_handler(uintptr_t, cpu_state); + friend uintptr_t syscall_dispatch(uintptr_t, cpu_state &); + friend uintptr_t isr_handler(uintptr_t, cpu_state*); /// Handle a timer tick /// \arg rsp0 The stack pointer of the current interrupt handler diff --git a/src/kernel/syscall.cpp b/src/kernel/syscall.cpp index 50f35bf..bdd65e2 100644 --- a/src/kernel/syscall.cpp +++ b/src/kernel/syscall.cpp @@ -35,7 +35,7 @@ syscall_enable() } uintptr_t -syscall_dispatch(uintptr_t return_rsp, const cpu_state ®s) +syscall_dispatch(uintptr_t return_rsp, cpu_state ®s) { console *cons = console::get(); syscall call = static_cast(regs.rax); @@ -65,7 +65,7 @@ syscall_dispatch(uintptr_t return_rsp, const cpu_state ®s) auto *p = s.current(); p->wait_on_signal(-1ull); cons->printf("\nReceived PAUSE syscall\n"); - return_rsp = s.tick(return_rsp); + return_rsp = s.schedule(return_rsp); cons->set_color(); } break; @@ -78,7 +78,7 @@ syscall_dispatch(uintptr_t return_rsp, const cpu_state ®s) auto *p = s.current(); p->wait_on_time(regs.rbx); cons->printf("\nReceived SLEEP syscall\n"); - return_rsp = s.tick(return_rsp); + return_rsp = s.schedule(return_rsp); cons->set_color(); } break; diff --git a/src/kernel/syscall.h b/src/kernel/syscall.h index 3aad6ae..12df735 100644 --- a/src/kernel/syscall.h +++ b/src/kernel/syscall.h @@ -16,5 +16,5 @@ enum class syscall : uint64_t }; void syscall_enable(); -uintptr_t syscall_dispatch(uintptr_t, const cpu_state &); +uintptr_t syscall_dispatch(uintptr_t, cpu_state &);