diff options
Diffstat (limited to 'kernel')
| -rw-r--r-- | kernel/CMakeLists.txt | 11 | ||||
| -rw-r--r-- | kernel/kapi/cpu.cpp | 35 | ||||
| -rw-r--r-- | kernel/kapi/interrupts.cpp | 65 | ||||
| -rw-r--r-- | kernel/src/main.cpp | 5 |
4 files changed, 116 insertions, 0 deletions
diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index eb762ac..cadc373 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -2,6 +2,8 @@ add_executable("kernel" # Platform-independent KAPI implementation "kapi/boot_modules.cpp" "kapi/cio.cpp" + "kapi/cpu.cpp" + "kapi/interrupts.cpp" "kapi/memory.cpp" "kapi/system.cpp" @@ -60,6 +62,15 @@ set_property(TARGET "kernel" "${KERNEL_LINKER_SCRIPT}" ) +file(GLOB_RECURSE KERNEL_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "include/**.hpp") + +target_sources("kernel" PUBLIC + FILE_SET HEADERS + BASE_DIRS "include" + FILES + ${KERNEL_HEADERS} +) + target_disassemble("kernel") target_extract_debug_symbols("kernel") target_strip("kernel") diff --git a/kernel/kapi/cpu.cpp b/kernel/kapi/cpu.cpp new file mode 100644 index 0000000..13de584 --- /dev/null +++ b/kernel/kapi/cpu.cpp @@ -0,0 +1,35 @@ +#include "kapi/cpu.hpp" + +#include "kapi/system.hpp" + +#include <kstd/print> + +namespace kapi::cpu +{ + + namespace + { + 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."); + } + } // namespace + + auto dispatch(exception const & context) -> bool + { + 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/kapi/interrupts.cpp b/kernel/kapi/interrupts.cpp new file mode 100644 index 0000000..0e37bc3 --- /dev/null +++ b/kernel/kapi/interrupts.cpp @@ -0,0 +1,65 @@ +#include "kapi/interrupts.hpp" + +#include <kstd/flat_map> +#include <kstd/print> +#include <kstd/vector> + +#include <algorithm> +#include <cstdint> + +namespace kapi::interrupts +{ + + namespace + { + auto constinit handlers = kstd::flat_map<std::uint32_t, kstd::vector<handler *>>{}; + } // namespace + + auto register_handler(std::uint32_t irq_number, handler & handler) -> void + { + if (handlers.contains(irq_number)) + { + auto & handler_list = handlers.at(irq_number); + handler_list.push_back(&handler); + } + else + { + handlers.emplace(irq_number, kstd::vector{&handler}); + } + } + + auto unregister_handler(std::uint32_t irq_number, handler & handler) -> void + { + auto & handler_list = handlers.at(irq_number); + auto [first, last] = std::ranges::remove(handler_list, &handler); + handler_list.erase(first, last); + } + + auto dispatch(std::uint32_t irq_number) -> status + { + if (!handlers.contains(irq_number)) + { + kstd::println(kstd::print_sink::stderr, "[OS:interrupts] No handler for IRQ{}", irq_number); + return status::unhandled; + } + + auto & handler_list = handlers.at(irq_number); + + if (handler_list.empty()) + { + kstd::println(kstd::print_sink::stderr, "[OS:interrupts] No handler for IRQ{}", irq_number); + return status::unhandled; + } + + for (auto handler : handler_list) + { + 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/kernel/src/main.cpp b/kernel/src/main.cpp index 98c88f2..bb6d57d 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -1,5 +1,7 @@ #include "kapi/boot_modules.hpp" #include "kapi/cio.hpp" +#include "kapi/cpu.hpp" +#include "kapi/interrupts.hpp" #include "kapi/memory.hpp" #include "kapi/system.hpp" @@ -169,6 +171,9 @@ auto main() -> int kapi::cio::init(); kstd::println("[OS] IO subsystem initialized."); + kapi::cpu::init(); + kapi::interrupts::enable(); + kapi::memory::init(); kernel::memory::init_heap(kapi::memory::heap_base); kstd::println("[OS] Memory subsystem initialized."); |
