From 00a77644192642e06462c11479a5c0e9bd859e9a Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 26 Mar 2026 16:35:32 +0100 Subject: kapi: extract interrupts API --- arch/x86_64/CMakeLists.txt | 2 +- arch/x86_64/kapi/cpu/interrupts.cpp | 67 ------------------------------------ arch/x86_64/kapi/interrupts.cpp | 68 +++++++++++++++++++++++++++++++++++++ arch/x86_64/src/cpu/interrupts.cpp | 3 +- 4 files changed, 71 insertions(+), 69 deletions(-) delete mode 100644 arch/x86_64/kapi/cpu/interrupts.cpp create mode 100644 arch/x86_64/kapi/interrupts.cpp (limited to 'arch') diff --git a/arch/x86_64/CMakeLists.txt b/arch/x86_64/CMakeLists.txt index 8ff81d8..4427e4c 100644 --- a/arch/x86_64/CMakeLists.txt +++ b/arch/x86_64/CMakeLists.txt @@ -15,7 +15,7 @@ target_sources("x86_64" PRIVATE "kapi/boot_modules.cpp" "kapi/cio.cpp" "kapi/cpu.cpp" - "kapi/cpu/interrupts.cpp" + "kapi/interrupts.cpp" "kapi/memory.cpp" "kapi/system.cpp" diff --git a/arch/x86_64/kapi/cpu/interrupts.cpp b/arch/x86_64/kapi/cpu/interrupts.cpp deleted file mode 100644 index b98595c..0000000 --- a/arch/x86_64/kapi/cpu/interrupts.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include "kapi/cpu.hpp" -#include "kapi/system.hpp" - -#include -#include - -#include -#include - -namespace kapi::cpu -{ - - namespace - { - constexpr auto irq_offset = 32uz; - auto constinit interrupt_handlers = std::array, 256 - irq_offset>{}; - } // namespace - - auto enable_interrupts() -> void - { - asm volatile("sti"); - } - - auto disable_interrupts() -> void - { - asm volatile("cli"); - } - - auto register_interrupt_handler(std::uint32_t irq_number, interrupt_handler & handler) -> void - { - if (irq_number < irq_offset) - { - system::panic("[x86_64:CPU] IRQ number must be in range [32, 255]."); - } - - interrupt_handlers[irq_number - irq_offset].push_back(&handler); - } - - auto unregister_interrupt_handler(std::uint32_t irq_number, [[maybe_unused]] interrupt_handler & handler) -> void - { - if (irq_number < irq_offset) - { - system::panic("[x86_64:CPU] IRQ number must be in range [32, 255]."); - } - - kstd::println("[x86_64:CPU] TODO: support erasure from vector."); - } - - auto dispatch_interrupt(std::uint32_t irq_number) -> status - { - if (irq_number < irq_offset) - { - return status::unhandled; - } - - for (auto handler : interrupt_handlers[irq_number - irq_offset]) - { - if (handler && handler->handle_interrupt(irq_number) == status::handled) - { - return status::handled; - } - } - - return status::unhandled; - } - -} // namespace kapi::cpu \ No newline at end of file diff --git a/arch/x86_64/kapi/interrupts.cpp b/arch/x86_64/kapi/interrupts.cpp new file mode 100644 index 0000000..babc926 --- /dev/null +++ b/arch/x86_64/kapi/interrupts.cpp @@ -0,0 +1,68 @@ +#include "kapi/interrupts.hpp" + +#include "kapi/system.hpp" + +#include +#include + +#include +#include + +namespace kapi::interrupts +{ + + namespace + { + constexpr auto irq_offset = 32uz; + auto constinit handlers = std::array, 256 - irq_offset>{}; + } // namespace + + auto enable() -> void + { + asm volatile("sti"); + } + + auto disable() -> void + { + asm volatile("cli"); + } + + auto register_handler(std::uint32_t irq_number, handler & handler) -> void + { + if (irq_number < irq_offset) + { + system::panic("[x86_64:CPU] IRQ number must be in range [32, 255]."); + } + + handlers[irq_number - irq_offset].push_back(&handler); + } + + auto unregister_handler(std::uint32_t irq_number, [[maybe_unused]] handler & handler) -> void + { + if (irq_number < irq_offset) + { + system::panic("[x86_64:CPU] IRQ number must be in range [32, 255]."); + } + + kstd::println("[x86_64:CPU] TODO: support erasure from vector."); + } + + auto dispatch(std::uint32_t irq_number) -> status + { + if (irq_number < irq_offset) + { + return status::unhandled; + } + + for (auto handler : handlers[irq_number - irq_offset]) + { + if (handler && handler->handle_interrupt(irq_number) == status::handled) + { + return status::handled; + } + } + + return status::unhandled; + } + +} // namespace kapi::interrupts \ No newline at end of file diff --git a/arch/x86_64/src/cpu/interrupts.cpp b/arch/x86_64/src/cpu/interrupts.cpp index 9ee3ce8..2f23f07 100644 --- a/arch/x86_64/src/cpu/interrupts.cpp +++ b/arch/x86_64/src/cpu/interrupts.cpp @@ -1,6 +1,7 @@ #include "arch/cpu/interrupts.hpp" #include "kapi/cpu.hpp" +#include "kapi/interrupts.hpp" #include "kapi/memory.hpp" #include "arch/cpu/legacy_pic.hpp" @@ -146,7 +147,7 @@ namespace arch::cpu } } else if (number >= number_of_exception_vectors && - kapi::cpu::dispatch_interrupt(number) == kapi::cpu::status::unhandled) + kapi::interrupts::dispatch(number) == kapi::interrupts::status::unhandled) { kstd::println(kstd::print_sink::stderr, "[x86_64:CPU] Unhandled interrupt {:#04x}", number); } -- cgit v1.2.3