diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2026-03-26 16:10:50 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2026-03-27 07:02:35 +0100 |
| commit | 8d06763f47e7b7c93af2a55f6bd2dbc4aa9abfa2 (patch) | |
| tree | b4da8395e2ba5dec565500b6b257a2091faeeb17 | |
| parent | d56700342ea0266a6e49f9515eb83279f66b4fcf (diff) | |
| download | teachos-8d06763f47e7b7c93af2a55f6bd2dbc4aa9abfa2.tar.xz teachos-8d06763f47e7b7c93af2a55f6bd2dbc4aa9abfa2.zip | |
kapi/cpu: simplify exception handling
| -rw-r--r-- | arch/x86_64/src/cpu/interrupts.cpp | 5 | ||||
| -rw-r--r-- | kapi/include/kapi/cpu/exception.hpp | 27 | ||||
| -rw-r--r-- | kernel/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | kernel/include/kernel/cpu.hpp | 14 | ||||
| -rw-r--r-- | kernel/kapi/cpu.cpp | 35 | ||||
| -rw-r--r-- | kernel/src/cpu.cpp | 45 | ||||
| -rw-r--r-- | kernel/src/main.cpp | 2 |
7 files changed, 28 insertions, 101 deletions
diff --git a/arch/x86_64/src/cpu/interrupts.cpp b/arch/x86_64/src/cpu/interrupts.cpp index 4e52d71..9ee3ce8 100644 --- a/arch/x86_64/src/cpu/interrupts.cpp +++ b/arch/x86_64/src/cpu/interrupts.cpp @@ -108,11 +108,10 @@ namespace arch::cpu auto write = (frame->interrupt.error_code & 0x2) != 0; auto user = (frame->interrupt.error_code & 0x4) != 0; - return kapi::cpu::get_exception_handler().handle( - {type, instruction_pointer, fault_address, present, write, user}); + return kapi::cpu::dispatch({type, instruction_pointer, fault_address, present, write, user}); } default: - return kapi::cpu::get_exception_handler().handle({type, instruction_pointer}); + return kapi::cpu::dispatch({type, instruction_pointer}); } } diff --git a/kapi/include/kapi/cpu/exception.hpp b/kapi/include/kapi/cpu/exception.hpp index 00b02e7..d6e8511 100644 --- a/kapi/include/kapi/cpu/exception.hpp +++ b/kapi/include/kapi/cpu/exception.hpp @@ -31,7 +31,7 @@ namespace kapi::cpu memory_access_fault, //! An invalid instruction was present in the instruction stream. illegal_instruction, - //! The precoditions for the execution of an instruction were not met. + //! The preconditions for the execution of an instruction were not met. privilege_violation, //! An arithmetic error occurred. arithmetic_error, @@ -60,27 +60,12 @@ namespace kapi::cpu bool is_user_mode{}; }; - //! The abstract interface for exception handlers. - //! - //! The kernel must define an exception handler to be used during execution. - struct exception_handler - { - virtual ~exception_handler() = default; - - //! Handle an exception. - //! - //! @param context The exception context. - //! @return Whether the exception was handled. - virtual auto handle(exception const & context) -> bool = 0; - }; - - //! @qualifier kernel-defined - //! Get the currently active exception handler. - auto get_exception_handler() -> exception_handler &; - //! @qualifier kernel-defined - //! Set the exception handler. - auto set_exception_handler(exception_handler & handler) -> void; + //! Dispatch an exception to the appropriate handler. + //! + //! @param context The exception context. + //! @return Whether the exception was handled. + auto dispatch(exception const & context) -> bool; } // namespace kapi::cpu diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index d9a5c75..9b1e2ad 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -11,7 +11,6 @@ add_executable("kernel" "kstd/print.cpp" # Kernel Implementation - "src/cpu.cpp" "src/main.cpp" "src/memory/bitmap_allocator.cpp" "src/memory/block_list_allocator.cpp" diff --git a/kernel/include/kernel/cpu.hpp b/kernel/include/kernel/cpu.hpp deleted file mode 100644 index d4e1ced..0000000 --- a/kernel/include/kernel/cpu.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef TEACHOS_KERNEL_CPU_HPP -#define TEACHOS_KERNEL_CPU_HPP - -namespace kernel::cpu -{ - - //! Initialize the kernel heap. - auto init() -> void; - - //! - -} // namespace kernel::cpu - -#endif
\ No newline at end of file diff --git a/kernel/kapi/cpu.cpp b/kernel/kapi/cpu.cpp index 2089098..13de584 100644 --- a/kernel/kapi/cpu.cpp +++ b/kernel/kapi/cpu.cpp @@ -1,30 +1,35 @@ #include "kapi/cpu.hpp" +#include "kapi/system.hpp" + +#include <kstd/print> + namespace kapi::cpu { namespace { - struct null_exception_handler : exception_handler + auto handle_page_fault(kapi::cpu::exception const & context) -> bool { - auto handle(exception const &) -> bool override - { - return false; - } - } static constinit default_exception_handler; - - exception_handler * current_handler = &default_exception_handler; + kstd::println(kstd::print_sink::stderr, "\tFault address: {:#018x}", context.access_address); + kstd::println(kstd::print_sink::stderr, "\tPresent: {}", context.is_present); + kstd::println(kstd::print_sink::stderr, "\tWrite: {}", context.is_write_access); + kstd::println(kstd::print_sink::stderr, "\tUser: {}", context.is_user_mode); + kapi::system::panic("Halting the system due to an unrecoverable page fault."); + } } // namespace - auto get_exception_handler() -> exception_handler & + auto dispatch(exception const & context) -> bool { - return *current_handler; - } - - auto set_exception_handler(exception_handler & handler) -> void - { - current_handler = &handler; + kstd::println(kstd::print_sink::stderr, "[OS:CPU] {} @ {:#018x}", context.type, context.instruction_pointer); + switch (context.type) + { + case kapi::cpu::exception::type::page_fault: + return handle_page_fault(context); + default: + return false; + } } } // namespace kapi::cpu
\ No newline at end of file diff --git a/kernel/src/cpu.cpp b/kernel/src/cpu.cpp deleted file mode 100644 index 11b6551..0000000 --- a/kernel/src/cpu.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "kernel/cpu.hpp" - -#include "kapi/cpu.hpp" -#include "kapi/system.hpp" - -#include <kstd/print> - -namespace kernel::cpu -{ - - namespace - { - struct exception_handler : kapi::cpu::exception_handler - { - auto handle(kapi::cpu::exception const & context) -> bool override - { - kstd::println(kstd::print_sink::stderr, "[OS:CPU] {} @ {:#018x}", context.type, context.instruction_pointer); - switch (context.type) - { - case kapi::cpu::exception::type::page_fault: - return handle_page_fault(context); - default: - return false; - } - } - - private: - auto handle_page_fault(kapi::cpu::exception const & context) -> bool - { - kstd::println(kstd::print_sink::stderr, "\tFault address: {:#018x}", context.access_address); - kstd::println(kstd::print_sink::stderr, "\tPresent: {}", context.is_present); - kstd::println(kstd::print_sink::stderr, "\tWrite: {}", context.is_write_access); - kstd::println(kstd::print_sink::stderr, "\tUser: {}", context.is_user_mode); - - kapi::system::panic("Halting the system due to an unrecoverable page fault."); - } - } static constinit handler; - } // namespace - - auto init() -> void - { - kapi::cpu::set_exception_handler(handler); - } - -} // namespace kernel::cpu
\ No newline at end of file diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index 6bd168c..45a4aae 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -4,7 +4,6 @@ #include "kapi/memory.hpp" #include "kapi/system.hpp" -#include "kernel/cpu.hpp" #include "kernel/devices/storage/storage_management.hpp" #include "kernel/filesystem/device_file.hpp" #include "kernel/filesystem/file_descriptor_table.hpp" @@ -93,7 +92,6 @@ auto main() -> int kstd::println("[OS] IO subsystem initialized."); kapi::cpu::init(); - kernel::cpu::init(); kapi::cpu::enable_interrupts(); kapi::memory::init(); |
