From 4aa093034bf8cd439db835515f7e909f2e47492f Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Tue, 26 Oct 2021 21:57:45 +0200 Subject: [PATCH] Page fault: more information --- arch/x86/cpu_context.c | 21 +++++++++++++++++++++ arch/x86/cpu_context_switch.S | 14 +++++++------- arch/x86/exception.c | 8 ++------ core/cpu_context.h | 2 +- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/arch/x86/cpu_context.c b/arch/x86/cpu_context.c index 40f77bf..3c8a2ca 100644 --- a/arch/x86/cpu_context.c +++ b/arch/x86/cpu_context.c @@ -390,6 +390,27 @@ vaddr_t cpu_context_get_SP(const struct cpu_state *ctxt) return (vaddr_t)ctxt; } +uint32_t cpu_context_get_EX_err(const struct cpu_state *ctxt) +{ + assert(NULL != ctxt); + + /* This is the Err_code of the interrupted context (ie kernel or user + context). */ + return ctxt->error_code; +} + +vaddr_t cpu_context_get_EX_faulting_vaddr(const struct cpu_state *ctxt) +{ + assert(NULL != ctxt); + + // A page fault has occurred. + // The faulting address is stored in the CR2 register. + vaddr_t faulting_address; + asm volatile("mov %%cr2, %0" : "=r"(faulting_address)); + + return faulting_address; +} + void cpu_context_dump(const struct cpu_state *ctxt) { printf("CPU: eip=%x esp=%x eflags=%x cs=%x ds=%x ss=%x err=%x", (unsigned)ctxt->eip, diff --git a/arch/x86/cpu_context_switch.S b/arch/x86/cpu_context_switch.S index c043510..7e661f6 100644 --- a/arch/x86/cpu_context_switch.S +++ b/arch/x86/cpu_context_switch.S @@ -17,13 +17,13 @@ .globl cpu_context_switch .type cpu_context_switch, @function cpu_context_switch: - // arg2= to_context -- esp+68 - // arg1= from_context -- esp+64 - // caller ip -- esp+60 - pushf // (eflags) esp+56 - pushl %cs // (cs) esp+52 - pushl $resume_pc // (ip) esp+48 - pushl $0 // (error code) esp+12+8x4 + // arg2= to_context -- esp+64 + // arg1= from_context -- esp+60 + // caller ip -- esp+56 + pushf // (eflags) esp+52 + pushl %cs // (cs) esp+48 + pushl $resume_pc // (ip) esp+44 + pushl $0 // (error code) esp+12+7x4 pushl %ebp pushl %eax pushl %ecx diff --git a/arch/x86/exception.c b/arch/x86/exception.c index 5b3d9a8..4aac3df 100644 --- a/arch/x86/exception.c +++ b/arch/x86/exception.c @@ -51,14 +51,10 @@ void print_handler(struct cpu_state *frame, ulong intr) void pagefault_handler(struct cpu_state *frame, ulong intr) { - // A page fault has occurred. - // The faulting address is stored in the CR2 register. - uint32_t faulting_address; - asm volatile("mov %%cr2, %0" : "=r"(faulting_address)); struct kthread *current = getCurrentThread(); - printf("page fault while in thread %s at 0x%x 0x%x\n", current->name, faulting_address, - cpu_context_get_PC(frame)); + printf("page fault while in thread %s code at 0x%x when trying to access 0x%x err_code 0x%x\n", current->name, + cpu_context_get_PC(frame), cpu_context_get_EX_faulting_vaddr(frame), cpu_context_get_EX_err(frame)); VGAPrintf(RED, BLACK, 0, VGA_HEIGHT - 1, "PAGE FAULT %d", intr); (void)intr; for (;;) diff --git a/core/cpu_context.h b/core/cpu_context.h index 9d141cc..e17c31b 100644 --- a/core/cpu_context.h +++ b/core/cpu_context.h @@ -156,7 +156,7 @@ void cpu_context_dump(const struct cpu_state *ctxt); * Return the argument passed by the CPU upon exception, as stored in the * saved context */ -uint32_t cpu_context_get_EX_info(const struct cpu_state *ctxt); +uint32_t cpu_context_get_EX_err(const struct cpu_state *ctxt); /** * Return the faulting address of the exception