From 87a4bc91d84ee7eca4ca25ef005542cfbc4bbea6 Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 6 Jan 2023 17:31:22 +0100 Subject: [PATCH] arch/x86_64: Nicer display for page faults that shows a human-readable version of the error code --- kernel/src/arch/x86_64/CPU.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/kernel/src/arch/x86_64/CPU.cpp b/kernel/src/arch/x86_64/CPU.cpp index 72289888..3f88e237 100644 --- a/kernel/src/arch/x86_64/CPU.cpp +++ b/kernel/src/arch/x86_64/CPU.cpp @@ -29,6 +29,19 @@ extern void setup_idt(); kerrorln("FIXME(interrupt): %s", name); \ CPU::efficient_halt(); +#define PF_PRESENT 1 << 0 +#define PF_WRITE 1 << 1 +#define PF_USER 1 << 2 +#define PF_RESERVED 1 << 3 +#define PF_NX_VIOLATION 1 << 4 + +void decode_page_fault_error_code(u64 code) +{ + kwarnln("Fault details: %s | %s | %s%s%s", (code & PF_PRESENT) ? "Present" : "Not present", + (code & PF_WRITE) ? "Write access" : "Read access", (code & PF_USER) ? "User mode" : "Kernel mode", + (code & PF_RESERVED) ? " | Reserved bits set" : "", (code & PF_NX_VIOLATION) ? " | NX violation" : ""); +} + [[noreturn]] void handle_page_fault(Registers* regs) { CPU::disable_interrupts(); @@ -37,6 +50,8 @@ extern void setup_idt(); asm volatile("mov %%cr2, %0" : "=r"(cr2)); kerrorln("Page fault at RIP %lx while accessing %lx!", regs->rip, cr2); + decode_page_fault_error_code(regs->error); + CPU::print_stack_trace_at(regs); CPU::efficient_halt();