From 38fc31ff11f02473e7ef04297feacaa4cf23981e Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 19 Jan 2020 09:54:58 +0100 Subject: [PATCH] Kernel: Always switch to own page tables when crashing/asserting I noticed this while debugging a crash in backtrace generation. If a process would crash while temporarily inspecting another process's address space, the crashing thread would still use the other process's page tables while handling the crash, causing all kinds of confusion when trying to walk the stack of the crashing thread. --- Kernel/Arch/i386/CPU.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Kernel/Arch/i386/CPU.cpp b/Kernel/Arch/i386/CPU.cpp index b83899136b..d1ef90e6a4 100644 --- a/Kernel/Arch/i386/CPU.cpp +++ b/Kernel/Arch/i386/CPU.cpp @@ -208,6 +208,10 @@ void handle_crash(RegisterDump& regs, const char* description, int signal) hang(); } + // If a process crashed while inspecting another process, + // make sure we switch back to the right page tables. + MM.enter_process_paging_scope(current->process()); + kprintf("\033[31;1mCRASH: %s. %s: %s(%u)\033[0m\n", description, current->process().is_ring0() ? "Kernel" : "Process", @@ -547,6 +551,12 @@ void __assertion_failed(const char* msg, const char* file, unsigned line, const { asm volatile("cli"); kprintf("ASSERTION FAILED: %s\n%s:%u in %s\n", msg, file, line, func); + + // Switch back to the current process's page tables if there are any. + // Otherwise stack walking will be a disaster. + if (current) + MM.enter_process_paging_scope(current->process()); + dump_backtrace(); asm volatile("hlt"); for (;;)