From 1d7795e22c90bf718c68b43aeecded849af829dc Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sun, 30 Jul 2023 14:17:39 +0300 Subject: [PATCH] Kernel: Hardware exceptions now sends signals to userspace --- kernel/arch/x86_64/IDT.cpp | 31 +++++++++++++++++++++++++++---- userspace/Shell/main.cpp | 5 +++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/kernel/arch/x86_64/IDT.cpp b/kernel/arch/x86_64/IDT.cpp index 37c2f7b7c6..78acea542d 100644 --- a/kernel/arch/x86_64/IDT.cpp +++ b/kernel/arch/x86_64/IDT.cpp @@ -163,10 +163,33 @@ namespace IDT if (tid && 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().sys_write(STDERR_FILENO, message.data(), message.size()); - asm volatile("sti"); - Kernel::Process::current().exit(1); + // TODO: Confirm and fix the exception to signal mappings + + int signal = 0; + switch (isr) + { + case ISR::DeviceNotAvailable: + case ISR::DivisionError: + case ISR::SIMDFloatingPointException: + case ISR::x87FloatingPointException: + signal = SIGFPE; + break; + case ISR::AlignmentCheck: + signal = SIGBUS; + break; + case ISR::InvalidOpcode: + signal = SIGILL; + break; + case ISR::PageFault: + signal = SIGSEGV; + break; + default: + dwarnln("Unhandled exception"); + signal = SIGABRT; + break; + } + + Kernel::Thread::current().handle_signal(-signal); } else { diff --git a/userspace/Shell/main.cpp b/userspace/Shell/main.cpp index 7e890a47eb..8af6561aaf 100644 --- a/userspace/Shell/main.cpp +++ b/userspace/Shell/main.cpp @@ -260,6 +260,11 @@ int execute_command(BAN::Vector& args) while (*current) printf("%s\n", *current++); } + else if (args.front() == "page-fault-test"sv) + { + volatile int* ptr = nullptr; + *ptr = 0; + } else if (args.front() == "kill-test"sv) { pid_t pid = fork();