From 19ffd9d677193a8b2c82706a5bf4a28ef6929c93 Mon Sep 17 00:00:00 2001 From: Tom Date: Thu, 10 Sep 2020 09:29:09 -0600 Subject: [PATCH] Kernel: Fix detecting in what ring a crash happened The ring is determined based on the CS register. This fixes crashes being handled as ring 3 crashes even though EIP/CS clearly showed that the crash happened in the kernel. --- Kernel/Arch/i386/CPU.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Kernel/Arch/i386/CPU.cpp b/Kernel/Arch/i386/CPU.cpp index 6b414e0a73..5e8d2df100 100644 --- a/Kernel/Arch/i386/CPU.cpp +++ b/Kernel/Arch/i386/CPU.cpp @@ -122,8 +122,8 @@ static void dump(const RegisterState& regs) { u16 ss; u32 esp; - auto process = Process::current(); - if (!process || process->is_ring0()) { + + if (!(regs.cs & 3)) { ss = regs.ss; esp = regs.esp; } else { @@ -149,6 +149,7 @@ static void dump(const RegisterState& regs) : "=a"(cr4)); klog() << "cr0=" << String::format("%08x", cr0) << " cr2=" << String::format("%08x", cr2) << " cr3=" << String::format("%08x", cr3) << " cr4=" << String::format("%08x", cr4); + auto process = Process::current(); if (process && process->validate_read((void*)regs.eip, 8)) { SmapDisabler disabler; u8* codeptr = (u8*)regs.eip; @@ -168,10 +169,10 @@ void handle_crash(RegisterState& regs, const char* description, int signal, bool // make sure we switch back to the right page tables. MM.enter_process_paging_scope(*process); - klog() << "CRASH: CPU #" << Processor::current().id() << " " << description << ". Ring " << (process->is_ring0() ? 0 : 3) << "."; + klog() << "CRASH: CPU #" << Processor::current().id() << " " << description << ". Ring " << (regs.cs & 3) << "."; dump(regs); - if (process->is_ring0()) { + if (!(regs.cs & 3)) { klog() << "Crash in ring 0 :("; dump_backtrace(); Processor::halt();