From 833642d405bf79e511c69bc51217c247aab70d7b Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sun, 28 May 2023 20:53:10 +0300 Subject: [PATCH] Kernel: ISR will now crash userspace process instead of panicing kernel --- kernel/arch/x86_64/IDT.cpp | 17 +++++++++++++++-- kernel/include/kernel/Thread.h | 3 +++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/kernel/arch/x86_64/IDT.cpp b/kernel/arch/x86_64/IDT.cpp index 9cb16de905..a1d451f21b 100644 --- a/kernel/arch/x86_64/IDT.cpp +++ b/kernel/arch/x86_64/IDT.cpp @@ -6,6 +6,8 @@ #include #include +#include + #define REGISTER_ISR_HANDLER(i) register_interrupt_handler(i, isr ## i) #define REGISTER_IRQ_HANDLER(i) register_interrupt_handler(IRQ_VECTOR_BASE + i, irq ## i) @@ -99,19 +101,30 @@ namespace IDT extern "C" void cpp_isr_handler(uint64_t isr, uint64_t error, const Registers* regs) { - Kernel::panic( + dwarnln( "{} (error code: 0x{16H}), pid {}, tid {}\r\n" "Register dump\r\n" "rax=0x{16H}, rbx=0x{16H}, rcx=0x{16H}, rdx=0x{16H}\r\n" "rsp=0x{16H}, rbp=0x{16H}, rdi=0x{16H}, rsi=0x{16H}\r\n" "rip=0x{16H}, rflags=0x{16H}\r\n" - "cr0=0x{16H}, cr2=0x{16H}, cr3=0x{16H}, cr4=0x{16H}\r\n", + "cr0=0x{16H}, cr2=0x{16H}, cr3=0x{16H}, cr4=0x{16H}", isr_exceptions[isr], error, Kernel::Process::current().pid(), Kernel::Thread::current().tid(), regs->rax, regs->rbx, regs->rcx, regs->rdx, regs->rsp, regs->rbp, regs->rdi, regs->rsi, regs->rip, regs->rflags, regs->cr0, regs->cr2, regs->cr3, regs->cr4 ); + + if (Kernel::Thread::current().is_userspace() && !Kernel::Thread::current().is_in_syscall()) + { + auto message = BAN::String::formatted("{}, aborting\n", isr_exceptions[isr]); + (void)Kernel::Process::current().write(STDERR_FILENO, message.data(), message.size()); + Kernel::Process::current().exit(); + } + else + { + Kernel::panic("Unhandled exception"); + } } extern "C" void cpp_irq_handler(uint64_t irq) diff --git a/kernel/include/kernel/Thread.h b/kernel/include/kernel/Thread.h index 7643ed3bee..40987f351b 100644 --- a/kernel/include/kernel/Thread.h +++ b/kernel/include/kernel/Thread.h @@ -58,6 +58,9 @@ namespace Kernel void set_in_syscall(bool b) { m_in_syscall = b; } + bool is_userspace() const { return m_is_userspace; } + bool is_in_syscall() const { return m_in_syscall; } + private: Thread(pid_t tid, Process*); void on_exit();