From cb60cdebdc36dd2358fe1ce06eec197e213af491 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Tue, 24 Mar 2026 17:35:54 +0100 Subject: kapi/cpu: introduce CPU API --- kapi/include/kapi/cpu.hpp | 7 +++++ kapi/include/kapi/cpu/exception.hpp | 60 +++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 kapi/include/kapi/cpu/exception.hpp (limited to 'kapi') diff --git a/kapi/include/kapi/cpu.hpp b/kapi/include/kapi/cpu.hpp index 05b84b7..1dd7cfe 100644 --- a/kapi/include/kapi/cpu.hpp +++ b/kapi/include/kapi/cpu.hpp @@ -1,6 +1,8 @@ #ifndef TEACHOS_KAPI_CPU_HPP #define TEACHOS_KAPI_CPU_HPP +#include "kapi/cpu/exception.hpp" + namespace kapi::cpu { //! @qualifier platform-defined @@ -8,6 +10,11 @@ namespace kapi::cpu //! //! This function terminates execution of the kernel. [[noreturn]] auto halt() -> void; + + //! @qualifier platform-defined + //! Perform early CPU initialization. + auto init() -> void; + } // namespace kapi::cpu #endif diff --git a/kapi/include/kapi/cpu/exception.hpp b/kapi/include/kapi/cpu/exception.hpp new file mode 100644 index 0000000..09e15a7 --- /dev/null +++ b/kapi/include/kapi/cpu/exception.hpp @@ -0,0 +1,60 @@ +#ifndef TEACHOS_KAPI_CPU_EXCEPTION_HPP +#define TEACHOS_KAPI_CPU_EXCEPTION_HPP + +// IWYU pragma: private, include "kapi/cpu.hpp" + +#include "kapi/memory.hpp" + +#include + +namespace kapi::cpu +{ + + struct exception + { + enum class type : std::uint8_t + { + unknown, + page_fault, + alignment_fault, + memory_access_fault, + illegal_instruction, + privilege_violation, + arithmetic_error, + breakpoint, + single_step, + }; + + //! The type of exception. + type type{}; + + //! The instruction pointer at the time of the exception. + kapi::memory::linear_address instruction_pointer{}; + + //! The memory address that caused the exception. + kapi::memory::linear_address access_address{}; + + //! Whether the page was present at the time of the exception. + bool is_present{}; + + //! Whether the exception was caused by a write access. + bool is_write_access{}; + + //! Whether the exception was caused by a user mode access. + bool is_user_mode{}; + }; + + struct exception_handler + { + virtual ~exception_handler() = default; + + virtual auto handle(exception const & context) -> bool = 0; + }; + + auto get_exception_handler() -> exception_handler &; + + auto set_exception_handler(exception_handler & handler) -> void; + +} // namespace kapi::cpu + +#endif \ No newline at end of file -- cgit v1.2.3 From 42895684b631380c8aca94f82209297ac0c0e5f2 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Tue, 24 Mar 2026 17:44:21 +0100 Subject: kapi: extract interrupt enablement --- kapi/include/kapi/cpu.hpp | 3 ++- kapi/include/kapi/cpu/interrupts.hpp | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 kapi/include/kapi/cpu/interrupts.hpp (limited to 'kapi') diff --git a/kapi/include/kapi/cpu.hpp b/kapi/include/kapi/cpu.hpp index 1dd7cfe..ade954c 100644 --- a/kapi/include/kapi/cpu.hpp +++ b/kapi/include/kapi/cpu.hpp @@ -1,7 +1,8 @@ #ifndef TEACHOS_KAPI_CPU_HPP #define TEACHOS_KAPI_CPU_HPP -#include "kapi/cpu/exception.hpp" +#include "kapi/cpu/exception.hpp" // IWYU pragma: export +#include "kapi/cpu/interrupts.hpp" // IWYU pragma: export namespace kapi::cpu { diff --git a/kapi/include/kapi/cpu/interrupts.hpp b/kapi/include/kapi/cpu/interrupts.hpp new file mode 100644 index 0000000..26a215e --- /dev/null +++ b/kapi/include/kapi/cpu/interrupts.hpp @@ -0,0 +1,19 @@ +#ifndef TEACHOS_KAPI_CPU_INTERRUPTS_HPP +#define TEACHOS_KAPI_CPU_INTERRUPTS_HPP + +// IWYU pragma: private, include "kapi/cpu.hpp" + +namespace kapi::cpu +{ + + //! @qualifier platform-defined + //! Enable external interrupts. + auto enable_interrupts() -> void; + + //! @qualifier platform-defined + //! Disable external interrupts. + auto disable_interrupts() -> void; + +} // namespace kapi::cpu + +#endif \ No newline at end of file -- cgit v1.2.3 From 9b879b06e202a41cdecc25e08ed5e69c57814141 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 25 Mar 2026 07:44:57 +0100 Subject: kapi: add missing header to build --- kapi/CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) (limited to 'kapi') diff --git a/kapi/CMakeLists.txt b/kapi/CMakeLists.txt index b239adb..4c94829 100644 --- a/kapi/CMakeLists.txt +++ b/kapi/CMakeLists.txt @@ -8,6 +8,9 @@ target_sources("kapi" PUBLIC "include/kapi/boot_modules.hpp" "include/kapi/boot.hpp" "include/kapi/cio.hpp" + "include/kapi/cpu.hpp" + "include/kapi/cpu/interrupts.hpp" + "include/kapi/cpu/exception.hpp" "include/kapi/boot_module/boot_module.hpp" "include/kapi/boot_module/boot_module_registry.hpp" "include/kapi/memory.hpp" -- cgit v1.2.3 From fd6ac1cbfbe90fa807dca60657bb80ed43c78aee Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 25 Mar 2026 07:45:08 +0100 Subject: kapi/cpu: improve documentation --- kapi/include/kapi/cpu/exception.hpp | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'kapi') diff --git a/kapi/include/kapi/cpu/exception.hpp b/kapi/include/kapi/cpu/exception.hpp index 09e15a7..6d39175 100644 --- a/kapi/include/kapi/cpu/exception.hpp +++ b/kapi/include/kapi/cpu/exception.hpp @@ -10,28 +10,40 @@ namespace kapi::cpu { + //! An exception originating from the CPU directly. + //! + //! Exception generally model interrupts that are synchronous to the instruction stream. This means that they do not + //! originate from external hardware, but rather are a product of program execution. struct exception { + //! The type of the exception, which identifies the reason for it being raised. enum class type : std::uint8_t { + //! The reason for the exception is unknown or platform-specific unknown, + //! A page fault occurred page_fault, + //! A memory access (either in the data or instruction stream) violated it's alignment constraints. alignment_fault, + //! A memory access (either in the data or instruction stream) violated it's permissions. memory_access_fault, - illegal_instruction, + //! The precoditions for the execution of an instruction were not met. privilege_violation, + //! An arithmetic error occurred. arithmetic_error, + //! A breakpoint was hit in the instruction stream. breakpoint, + //! The CPU is single-stepping through the instruction stream. single_step, }; - //! The type of exception. + //! The type of this exception. type type{}; - //! The instruction pointer at the time of the exception. + //! The value of the instruction pointer at the time this exception was raised. kapi::memory::linear_address instruction_pointer{}; - //! The memory address that caused the exception. + //! The memory address that caused this exception. kapi::memory::linear_address access_address{}; //! Whether the page was present at the time of the exception. @@ -44,15 +56,26 @@ 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; } // namespace kapi::cpu -- cgit v1.2.3 From 2f8c5ca6d5ab6131a148502e1d1be4ce2a65b339 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 25 Mar 2026 07:47:04 +0100 Subject: kapi/cpu: add missing exception type --- kapi/include/kapi/cpu/exception.hpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'kapi') diff --git a/kapi/include/kapi/cpu/exception.hpp b/kapi/include/kapi/cpu/exception.hpp index 6d39175..9fc697a 100644 --- a/kapi/include/kapi/cpu/exception.hpp +++ b/kapi/include/kapi/cpu/exception.hpp @@ -27,6 +27,8 @@ namespace kapi::cpu alignment_fault, //! A memory access (either in the data or instruction stream) violated it's permissions. 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. privilege_violation, //! An arithmetic error occurred. -- cgit v1.2.3 From fd1c5a50bb35f772b8e37125188640447d4b3b2a Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 25 Mar 2026 13:24:03 +0100 Subject: kapi/cpu: enable formatting of exception types --- kapi/include/kapi/cpu/exception.hpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'kapi') diff --git a/kapi/include/kapi/cpu/exception.hpp b/kapi/include/kapi/cpu/exception.hpp index 9fc697a..00b02e7 100644 --- a/kapi/include/kapi/cpu/exception.hpp +++ b/kapi/include/kapi/cpu/exception.hpp @@ -5,6 +5,8 @@ #include "kapi/memory.hpp" +#include + #include namespace kapi::cpu @@ -82,4 +84,38 @@ namespace kapi::cpu } // namespace kapi::cpu +template<> +struct kstd::formatter +{ + constexpr auto parse(kstd::format_parse_context & ctx) -> decltype(ctx.begin()) + { + return ctx.begin(); + } + + constexpr auto format(enum kapi::cpu::exception::type type, kstd::format_context & ctx) const -> void + { + switch (type) + { + case kapi::cpu::exception::type::unknown: + return ctx.push("unknown"); + case kapi::cpu::exception::type::page_fault: + return ctx.push("page fault"); + case kapi::cpu::exception::type::alignment_fault: + return ctx.push("alignment fault"); + case kapi::cpu::exception::type::memory_access_fault: + return ctx.push("memory access fault"); + case kapi::cpu::exception::type::illegal_instruction: + return ctx.push("illegal instruction"); + case kapi::cpu::exception::type::privilege_violation: + return ctx.push("privilege violation"); + case kapi::cpu::exception::type::arithmetic_error: + return ctx.push("arithmetic error"); + case kapi::cpu::exception::type::breakpoint: + return ctx.push("breakpoint"); + case kapi::cpu::exception::type::single_step: + return ctx.push("single step"); + } + } +}; + #endif \ No newline at end of file -- cgit v1.2.3 From a82416648d148152338dc612c25bf8dff428e773 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 25 Mar 2026 16:39:13 +0100 Subject: kapi: introduce cpu::interrupt_handler --- kapi/include/kapi/cpu/interrupts.hpp | 44 ++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'kapi') diff --git a/kapi/include/kapi/cpu/interrupts.hpp b/kapi/include/kapi/cpu/interrupts.hpp index 26a215e..328e4a7 100644 --- a/kapi/include/kapi/cpu/interrupts.hpp +++ b/kapi/include/kapi/cpu/interrupts.hpp @@ -3,9 +3,32 @@ // IWYU pragma: private, include "kapi/cpu.hpp" +#include + namespace kapi::cpu { + enum class status : bool + { + unhandled, + handled, + }; + + //! The interface for all interrupt handlers. + struct interrupt_handler + { + virtual ~interrupt_handler() = default; + + //! Handle an interrupt with the given IRQ number. + // + //! This function will be called by the kernel in an interrupt context. As such, the function should complete its + //! task quickly and must take care when acquiring globally shared locks. + //! + //! @param irq_number The identifier of the interrupt request that triggered the handler. + //! @return status::handled if the handler successfully handled the interrupt, status::unhandled otherwise. + virtual auto handle_interrupt(std::uint32_t irq_number) -> status = 0; + }; + //! @qualifier platform-defined //! Enable external interrupts. auto enable_interrupts() -> void; @@ -14,6 +37,27 @@ namespace kapi::cpu //! Disable external interrupts. auto disable_interrupts() -> void; + //! @qualifier platform-defined + //! Register an interrupt handler for the given IRQ number. + //! + //! @param irq_number The IRQ number to register the handler for. + //! @param handler The interrupt handler to register. + auto register_interrupt_handler(std::uint32_t irq_number, interrupt_handler & handler) -> void; + + //! @qualifier platform-defined + //! Unregister a previously registered interrupt handler. + //! + //! @param irq_number The IRQ number to unregister the handler for. + //! @param handler The interrupt handler to unregister. + auto unregister_interrupt_handler(std::uint32_t irq_number, interrupt_handler & handler) -> void; + + //! @qualifier platform-defined + //! Dispatch an interrupt to all registered handlers. + //! + //! @param irq_number The IRQ number to dispatch. + //! @return status::handled if the interrupt was handled by at least one handler, status::unhandled otherwise. + auto dispatch_interrupt(std::uint32_t irq_number) -> status; + } // namespace kapi::cpu #endif \ No newline at end of file -- cgit v1.2.3 From 1cb599798a0b29302ab71d1ee0fb9febff8f6a75 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 26 Mar 2026 15:24:10 +0100 Subject: kapi/cpu: update documentation of init() --- kapi/include/kapi/cpu.hpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'kapi') diff --git a/kapi/include/kapi/cpu.hpp b/kapi/include/kapi/cpu.hpp index ade954c..712de16 100644 --- a/kapi/include/kapi/cpu.hpp +++ b/kapi/include/kapi/cpu.hpp @@ -14,6 +14,9 @@ namespace kapi::cpu //! @qualifier platform-defined //! Perform early CPU initialization. + //! + //! When this function returns, the CPU is in a state where interrupt could be enabled. This function must not enable + //! interrupts itself. auto init() -> void; } // namespace kapi::cpu -- cgit v1.2.3 From 8d06763f47e7b7c93af2a55f6bd2dbc4aa9abfa2 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 26 Mar 2026 16:10:50 +0100 Subject: kapi/cpu: simplify exception handling --- kapi/include/kapi/cpu/exception.hpp | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) (limited to 'kapi') 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 -- cgit v1.2.3 From 2521d58e7a5c16595e401e1af7becb572ad35f53 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 26 Mar 2026 16:25:35 +0100 Subject: kapi: dissolve cpu/exception.hpp into cpu.hpp --- kapi/include/kapi/cpu.hpp | 96 +++++++++++++++++++++++++++++++- kapi/include/kapi/cpu/exception.hpp | 106 ------------------------------------ 2 files changed, 95 insertions(+), 107 deletions(-) delete mode 100644 kapi/include/kapi/cpu/exception.hpp (limited to 'kapi') diff --git a/kapi/include/kapi/cpu.hpp b/kapi/include/kapi/cpu.hpp index 712de16..041a5db 100644 --- a/kapi/include/kapi/cpu.hpp +++ b/kapi/include/kapi/cpu.hpp @@ -1,11 +1,64 @@ #ifndef TEACHOS_KAPI_CPU_HPP #define TEACHOS_KAPI_CPU_HPP -#include "kapi/cpu/exception.hpp" // IWYU pragma: export #include "kapi/cpu/interrupts.hpp" // IWYU pragma: export +#include "kapi/memory.hpp" + +#include + +#include namespace kapi::cpu { + + //! An exception originating from the CPU directly. + //! + //! Exception generally model interrupts that are synchronous to the instruction stream. This means that they do not + //! originate from external hardware, but rather are a product of program execution. + struct exception + { + //! The type of the exception, which identifies the reason for it being raised. + enum class type : std::uint8_t + { + //! The reason for the exception is unknown or platform-specific + unknown, + //! A page fault occurred + page_fault, + //! A memory access (either in the data or instruction stream) violated it's alignment constraints. + alignment_fault, + //! A memory access (either in the data or instruction stream) violated it's permissions. + memory_access_fault, + //! An invalid instruction was present in the instruction stream. + illegal_instruction, + //! The preconditions for the execution of an instruction were not met. + privilege_violation, + //! An arithmetic error occurred. + arithmetic_error, + //! A breakpoint was hit in the instruction stream. + breakpoint, + //! The CPU is single-stepping through the instruction stream. + single_step, + }; + + //! The type of this exception. + type type{}; + + //! The value of the instruction pointer at the time this exception was raised. + kapi::memory::linear_address instruction_pointer{}; + + //! The memory address that caused this exception. + kapi::memory::linear_address access_address{}; + + //! Whether the page was present at the time of the exception. + bool is_present{}; + + //! Whether the exception was caused by a write access. + bool is_write_access{}; + + //! Whether the exception was caused by a user mode access. + bool is_user_mode{}; + }; + //! @qualifier platform-defined //! Halt the CPU. //! @@ -19,6 +72,47 @@ namespace kapi::cpu //! interrupts itself. auto init() -> void; + //! @qualifier kernel-defined + //! Dispatch an exception to the appropriate handler. + //! + //! @param context The exception context. + //! @return Whether the exception was handled. + [[nodiscard]] auto dispatch(exception const & context) -> bool; + } // namespace kapi::cpu +template<> +struct kstd::formatter +{ + constexpr auto parse(kstd::format_parse_context & ctx) -> decltype(ctx.begin()) + { + return ctx.begin(); + } + + constexpr auto format(enum kapi::cpu::exception::type type, kstd::format_context & ctx) const -> void + { + switch (type) + { + case kapi::cpu::exception::type::unknown: + return ctx.push("unknown"); + case kapi::cpu::exception::type::page_fault: + return ctx.push("page fault"); + case kapi::cpu::exception::type::alignment_fault: + return ctx.push("alignment fault"); + case kapi::cpu::exception::type::memory_access_fault: + return ctx.push("memory access fault"); + case kapi::cpu::exception::type::illegal_instruction: + return ctx.push("illegal instruction"); + case kapi::cpu::exception::type::privilege_violation: + return ctx.push("privilege violation"); + case kapi::cpu::exception::type::arithmetic_error: + return ctx.push("arithmetic error"); + case kapi::cpu::exception::type::breakpoint: + return ctx.push("breakpoint"); + case kapi::cpu::exception::type::single_step: + return ctx.push("single step"); + } + } +}; + #endif diff --git a/kapi/include/kapi/cpu/exception.hpp b/kapi/include/kapi/cpu/exception.hpp deleted file mode 100644 index d6e8511..0000000 --- a/kapi/include/kapi/cpu/exception.hpp +++ /dev/null @@ -1,106 +0,0 @@ -#ifndef TEACHOS_KAPI_CPU_EXCEPTION_HPP -#define TEACHOS_KAPI_CPU_EXCEPTION_HPP - -// IWYU pragma: private, include "kapi/cpu.hpp" - -#include "kapi/memory.hpp" - -#include - -#include - -namespace kapi::cpu -{ - - //! An exception originating from the CPU directly. - //! - //! Exception generally model interrupts that are synchronous to the instruction stream. This means that they do not - //! originate from external hardware, but rather are a product of program execution. - struct exception - { - //! The type of the exception, which identifies the reason for it being raised. - enum class type : std::uint8_t - { - //! The reason for the exception is unknown or platform-specific - unknown, - //! A page fault occurred - page_fault, - //! A memory access (either in the data or instruction stream) violated it's alignment constraints. - alignment_fault, - //! A memory access (either in the data or instruction stream) violated it's permissions. - memory_access_fault, - //! An invalid instruction was present in the instruction stream. - illegal_instruction, - //! The preconditions for the execution of an instruction were not met. - privilege_violation, - //! An arithmetic error occurred. - arithmetic_error, - //! A breakpoint was hit in the instruction stream. - breakpoint, - //! The CPU is single-stepping through the instruction stream. - single_step, - }; - - //! The type of this exception. - type type{}; - - //! The value of the instruction pointer at the time this exception was raised. - kapi::memory::linear_address instruction_pointer{}; - - //! The memory address that caused this exception. - kapi::memory::linear_address access_address{}; - - //! Whether the page was present at the time of the exception. - bool is_present{}; - - //! Whether the exception was caused by a write access. - bool is_write_access{}; - - //! Whether the exception was caused by a user mode access. - bool is_user_mode{}; - }; - - //! @qualifier kernel-defined - //! 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 - -template<> -struct kstd::formatter -{ - constexpr auto parse(kstd::format_parse_context & ctx) -> decltype(ctx.begin()) - { - return ctx.begin(); - } - - constexpr auto format(enum kapi::cpu::exception::type type, kstd::format_context & ctx) const -> void - { - switch (type) - { - case kapi::cpu::exception::type::unknown: - return ctx.push("unknown"); - case kapi::cpu::exception::type::page_fault: - return ctx.push("page fault"); - case kapi::cpu::exception::type::alignment_fault: - return ctx.push("alignment fault"); - case kapi::cpu::exception::type::memory_access_fault: - return ctx.push("memory access fault"); - case kapi::cpu::exception::type::illegal_instruction: - return ctx.push("illegal instruction"); - case kapi::cpu::exception::type::privilege_violation: - return ctx.push("privilege violation"); - case kapi::cpu::exception::type::arithmetic_error: - return ctx.push("arithmetic error"); - case kapi::cpu::exception::type::breakpoint: - return ctx.push("breakpoint"); - case kapi::cpu::exception::type::single_step: - return ctx.push("single step"); - } - } -}; - -#endif \ No newline at end of file -- cgit v1.2.3 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 --- kapi/CMakeLists.txt | 3 +- kapi/include/kapi/cpu.hpp | 1 - kapi/include/kapi/cpu/interrupts.hpp | 63 ------------------------------------ kapi/include/kapi/interrupts.hpp | 61 ++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 66 deletions(-) delete mode 100644 kapi/include/kapi/cpu/interrupts.hpp create mode 100644 kapi/include/kapi/interrupts.hpp (limited to 'kapi') diff --git a/kapi/CMakeLists.txt b/kapi/CMakeLists.txt index 4c94829..99b737c 100644 --- a/kapi/CMakeLists.txt +++ b/kapi/CMakeLists.txt @@ -9,8 +9,7 @@ target_sources("kapi" PUBLIC "include/kapi/boot.hpp" "include/kapi/cio.hpp" "include/kapi/cpu.hpp" - "include/kapi/cpu/interrupts.hpp" - "include/kapi/cpu/exception.hpp" + "include/kapi/interrupts.hpp" "include/kapi/boot_module/boot_module.hpp" "include/kapi/boot_module/boot_module_registry.hpp" "include/kapi/memory.hpp" diff --git a/kapi/include/kapi/cpu.hpp b/kapi/include/kapi/cpu.hpp index 041a5db..c6aa6ff 100644 --- a/kapi/include/kapi/cpu.hpp +++ b/kapi/include/kapi/cpu.hpp @@ -1,7 +1,6 @@ #ifndef TEACHOS_KAPI_CPU_HPP #define TEACHOS_KAPI_CPU_HPP -#include "kapi/cpu/interrupts.hpp" // IWYU pragma: export #include "kapi/memory.hpp" #include diff --git a/kapi/include/kapi/cpu/interrupts.hpp b/kapi/include/kapi/cpu/interrupts.hpp deleted file mode 100644 index 328e4a7..0000000 --- a/kapi/include/kapi/cpu/interrupts.hpp +++ /dev/null @@ -1,63 +0,0 @@ -#ifndef TEACHOS_KAPI_CPU_INTERRUPTS_HPP -#define TEACHOS_KAPI_CPU_INTERRUPTS_HPP - -// IWYU pragma: private, include "kapi/cpu.hpp" - -#include - -namespace kapi::cpu -{ - - enum class status : bool - { - unhandled, - handled, - }; - - //! The interface for all interrupt handlers. - struct interrupt_handler - { - virtual ~interrupt_handler() = default; - - //! Handle an interrupt with the given IRQ number. - // - //! This function will be called by the kernel in an interrupt context. As such, the function should complete its - //! task quickly and must take care when acquiring globally shared locks. - //! - //! @param irq_number The identifier of the interrupt request that triggered the handler. - //! @return status::handled if the handler successfully handled the interrupt, status::unhandled otherwise. - virtual auto handle_interrupt(std::uint32_t irq_number) -> status = 0; - }; - - //! @qualifier platform-defined - //! Enable external interrupts. - auto enable_interrupts() -> void; - - //! @qualifier platform-defined - //! Disable external interrupts. - auto disable_interrupts() -> void; - - //! @qualifier platform-defined - //! Register an interrupt handler for the given IRQ number. - //! - //! @param irq_number The IRQ number to register the handler for. - //! @param handler The interrupt handler to register. - auto register_interrupt_handler(std::uint32_t irq_number, interrupt_handler & handler) -> void; - - //! @qualifier platform-defined - //! Unregister a previously registered interrupt handler. - //! - //! @param irq_number The IRQ number to unregister the handler for. - //! @param handler The interrupt handler to unregister. - auto unregister_interrupt_handler(std::uint32_t irq_number, interrupt_handler & handler) -> void; - - //! @qualifier platform-defined - //! Dispatch an interrupt to all registered handlers. - //! - //! @param irq_number The IRQ number to dispatch. - //! @return status::handled if the interrupt was handled by at least one handler, status::unhandled otherwise. - auto dispatch_interrupt(std::uint32_t irq_number) -> status; - -} // namespace kapi::cpu - -#endif \ No newline at end of file diff --git a/kapi/include/kapi/interrupts.hpp b/kapi/include/kapi/interrupts.hpp new file mode 100644 index 0000000..fa4bc95 --- /dev/null +++ b/kapi/include/kapi/interrupts.hpp @@ -0,0 +1,61 @@ +#ifndef TEACHOS_KAPI_INTERRUPTS_HPP +#define TEACHOS_KAPI_INTERRUPTS_HPP + +#include + +namespace kapi::interrupts +{ + + enum class status : bool + { + unhandled, + handled, + }; + + //! The interface for all interrupt handlers. + struct handler + { + virtual ~handler() = default; + + //! Handle an interrupt with the given IRQ number. + // + //! This function will be called by the kernel in an interrupt context. As such, the function should complete its + //! task quickly and must take care when acquiring globally shared locks. + //! + //! @param irq_number The identifier of the interrupt request that triggered the handler. + //! @return status::handled if the handler successfully handled the interrupt, status::unhandled otherwise. + virtual auto handle_interrupt(std::uint32_t irq_number) -> status = 0; + }; + + //! @qualifier platform-defined + //! Enable external interrupts. + auto enable() -> void; + + //! @qualifier platform-defined + //! Disable external interrupts. + auto disable() -> void; + + //! @qualifier platform-defined + //! Register an interrupt handler for the given IRQ number. + //! + //! @param irq_number The IRQ number to register the handler for. + //! @param handler The interrupt handler to register. + auto register_handler(std::uint32_t irq_number, handler & handler) -> void; + + //! @qualifier platform-defined + //! Unregister a previously registered interrupt handler. + //! + //! @param irq_number The IRQ number to unregister the handler for. + //! @param handler The interrupt handler to unregister. + auto unregister_handler(std::uint32_t irq_number, handler & handler) -> void; + + //! @qualifier platform-defined + //! Dispatch an interrupt to all registered handlers. + //! + //! @param irq_number The IRQ number to dispatch. + //! @return status::handled if the interrupt was handled by at least one handler, status::unhandled otherwise. + auto dispatch(std::uint32_t irq_number) -> status; + +} // namespace kapi::interrupts + +#endif \ No newline at end of file -- cgit v1.2.3 From f4dc64976049761a6f56dd55d9d0b651f1e9475f Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 26 Mar 2026 16:47:41 +0100 Subject: kapi: move interrupt handling to kernel --- kapi/include/kapi/interrupts.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'kapi') diff --git a/kapi/include/kapi/interrupts.hpp b/kapi/include/kapi/interrupts.hpp index fa4bc95..f72ef8c 100644 --- a/kapi/include/kapi/interrupts.hpp +++ b/kapi/include/kapi/interrupts.hpp @@ -35,21 +35,21 @@ namespace kapi::interrupts //! Disable external interrupts. auto disable() -> void; - //! @qualifier platform-defined + //! @qualifier kernel-defined //! Register an interrupt handler for the given IRQ number. //! //! @param irq_number The IRQ number to register the handler for. //! @param handler The interrupt handler to register. auto register_handler(std::uint32_t irq_number, handler & handler) -> void; - //! @qualifier platform-defined + //! @qualifier kernel-defined //! Unregister a previously registered interrupt handler. //! //! @param irq_number The IRQ number to unregister the handler for. //! @param handler The interrupt handler to unregister. auto unregister_handler(std::uint32_t irq_number, handler & handler) -> void; - //! @qualifier platform-defined + //! @qualifier kernel-defined //! Dispatch an interrupt to all registered handlers. //! //! @param irq_number The IRQ number to dispatch. -- cgit v1.2.3 From a2834cc22b096e848448bb681ab7b517ecbe70b9 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 26 Mar 2026 16:54:07 +0100 Subject: build: simplify header scanning --- kapi/CMakeLists.txt | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) (limited to 'kapi') diff --git a/kapi/CMakeLists.txt b/kapi/CMakeLists.txt index 99b737c..c9aa23f 100644 --- a/kapi/CMakeLists.txt +++ b/kapi/CMakeLists.txt @@ -1,24 +1,13 @@ add_library("kapi" INTERFACE) add_library("os::kapi" ALIAS "kapi") +file(GLOB_RECURSE KAPI_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "include/**.hpp") + target_sources("kapi" PUBLIC FILE_SET HEADERS BASE_DIRS "include" FILES - "include/kapi/boot_modules.hpp" - "include/kapi/boot.hpp" - "include/kapi/cio.hpp" - "include/kapi/cpu.hpp" - "include/kapi/interrupts.hpp" - "include/kapi/boot_module/boot_module.hpp" - "include/kapi/boot_module/boot_module_registry.hpp" - "include/kapi/memory.hpp" - "include/kapi/memory/address.hpp" - "include/kapi/memory/frame_allocator.hpp" - "include/kapi/memory/frame.hpp" - "include/kapi/memory/page_mapper.hpp" - "include/kapi/memory/page.hpp" - "include/kapi/system.hpp" + ${KAPI_HEADERS} ) target_include_directories("kapi" INTERFACE -- cgit v1.2.3