From ea8172296fa5b56137f97c01650b8d392bdb897f Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Thu, 26 Feb 2026 11:21:18 +0100 Subject: implemented remapping of bootloader modules --- arch/x86_64/kapi/memory.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/memory.cpp b/arch/x86_64/kapi/memory.cpp index a9e1216..3a2d66b 100644 --- a/arch/x86_64/kapi/memory.cpp +++ b/arch/x86_64/kapi/memory.cpp @@ -118,6 +118,27 @@ namespace kapi::memory mapper.map(page, frame, page_mapper::flags::supervisor_only); } } + + [[maybe_unused]] auto remap_bootloader_modules(page_mapper & mapper) -> void + { + auto modules = boot::bootstrap_information.mbi->modules(); + + for (auto module : modules) + { + auto module_physical_start = physical_address{module.start_address}; + auto module_virtual_start = + linear_address{module.start_address + std::bit_cast(&arch::boot::TEACHOS_VMA)}; + auto module_size = module.end_address - module.start_address; + auto module_block_count = (module_size + PLATFORM_FRAME_SIZE - 1) / PLATFORM_FRAME_SIZE; + + for (auto i = 0uz; i < module_block_count; ++i) + { + auto page = page::containing(module_virtual_start) + i; + auto frame = frame::containing(module_physical_start) + i; + mapper.map(page, frame, page_mapper::flags::empty); // TODO BA-FS26 make writable? + } + } + } [[maybe_unused]] auto handoff_to_kernel_pmm(frame_allocator & new_allocator) -> void { @@ -196,6 +217,7 @@ namespace kapi::memory remap_kernel(*higher_half_mapper); remap_vga_text_mode_buffer(*higher_half_mapper); remap_multiboot_information(*higher_half_mapper); + remap_bootloader_modules(*recursive_page_mapper); auto current_cr3 = arch::cpu::cr3::read(); auto old_pml4 = static_cast(current_cr3.address()); -- cgit v1.2.3 From 035c8d6e38fd901e6769a81f67b8d9e1e3fcea20 Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Thu, 26 Feb 2026 11:24:27 +0100 Subject: implemented boot_modules and boot_module_registry, init boot_modules in kernel main --- arch/x86_64/kapi/boot_modules.cpp | 51 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 arch/x86_64/kapi/boot_modules.cpp (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/boot_modules.cpp b/arch/x86_64/kapi/boot_modules.cpp new file mode 100644 index 0000000..5d06eb5 --- /dev/null +++ b/arch/x86_64/kapi/boot_modules.cpp @@ -0,0 +1,51 @@ +#include "kapi/boot_modules.hpp" + +#include "kapi/boot.hpp" +#include "kapi/boot_module/boot_module.hpp" +#include "kapi/boot_module/boot_module_registry.hpp" +#include "kapi/system.hpp" + +#include "arch/boot/boot.hpp" +#include "arch/boot/ld.hpp" + +#include + +#include + +#include +#include +#include +#include +#include + +namespace kapi::boot_modules +{ + namespace + { + auto constinit registry = std::optional{}; + } // namespace + + auto init() -> void + { + auto static constinit is_initialized = std::atomic_flag{}; + if (is_initialized.test_and_set()) + { + system::panic("[x86_64] Boot module registry has already been initialized."); + } + + kstd::println("[x86_64:BOOT_MODULES] Initializing boot module registry."); + + registry.emplace(kapi::boot_modules::boot_module_registry{}); + + auto modules = boot::bootstrap_information.mbi->modules(); + std::ranges::for_each(modules, [](auto const & module) { + registry->add_boot_module(kapi::boot_modules::boot_module{ + .name = module.string(), + .start_address = module.start_address + std::bit_cast(&arch::boot::TEACHOS_VMA), + .size = module.end_address - module.start_address, + }); + }); + + set_boot_module_registry(*registry); + } +} // namespace kapi::boot_modules \ No newline at end of file -- cgit v1.2.3 From e84c7fbf336847d3ff62aac10ed8f6d04a06cbe8 Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Fri, 27 Feb 2026 19:27:41 +0100 Subject: use linear_address instead of size_t --- arch/x86_64/kapi/boot_modules.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/boot_modules.cpp b/arch/x86_64/kapi/boot_modules.cpp index 5d06eb5..ba01285 100644 --- a/arch/x86_64/kapi/boot_modules.cpp +++ b/arch/x86_64/kapi/boot_modules.cpp @@ -3,6 +3,7 @@ #include "kapi/boot.hpp" #include "kapi/boot_module/boot_module.hpp" #include "kapi/boot_module/boot_module_registry.hpp" +#include "kapi/memory.hpp" #include "kapi/system.hpp" #include "arch/boot/boot.hpp" @@ -41,7 +42,8 @@ namespace kapi::boot_modules std::ranges::for_each(modules, [](auto const & module) { registry->add_boot_module(kapi::boot_modules::boot_module{ .name = module.string(), - .start_address = module.start_address + std::bit_cast(&arch::boot::TEACHOS_VMA), + .start_address = + memory::linear_address{module.start_address + std::bit_cast(&arch::boot::TEACHOS_VMA)}, .size = module.end_address - module.start_address, }); }); -- cgit v1.2.3 From 09e16896dfcd87c289be18b13867e64441efcaf5 Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Tue, 3 Mar 2026 11:18:16 +0100 Subject: make module pages writable --- arch/x86_64/kapi/memory.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/memory.cpp b/arch/x86_64/kapi/memory.cpp index 3a2d66b..d6c0ad5 100644 --- a/arch/x86_64/kapi/memory.cpp +++ b/arch/x86_64/kapi/memory.cpp @@ -135,7 +135,7 @@ namespace kapi::memory { auto page = page::containing(module_virtual_start) + i; auto frame = frame::containing(module_physical_start) + i; - mapper.map(page, frame, page_mapper::flags::empty); // TODO BA-FS26 make writable? + mapper.map(page, frame, page_mapper::flags::writable); } } } -- cgit v1.2.3 From 3733baef9603581d6de2d35fda4535d37b6826b0 Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Mon, 16 Mar 2026 19:51:53 +0100 Subject: protect multiboot2 boot modules --- arch/x86_64/kapi/memory.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/memory.cpp b/arch/x86_64/kapi/memory.cpp index d6c0ad5..d9fbe28 100644 --- a/arch/x86_64/kapi/memory.cpp +++ b/arch/x86_64/kapi/memory.cpp @@ -174,9 +174,19 @@ namespace kapi::memory auto mbi_start = frame::containing(mbi_address); auto mbi_end = frame::containing(mbi_address + mbi_size) + 1; - // TODO BA-FS26: Protect MB2 boot modules - std::ranges::for_each(std::views::iota(mbi_start, mbi_end), [&](auto frame) { new_allocator.mark_used(frame); }); + + auto modules = boot::bootstrap_information.mbi->modules(); + for (auto module : modules) + { + auto module_physical_start = physical_address{module.start_address}; + auto module_size = module.end_address - module.start_address; + auto module_start = frame::containing(module_physical_start); + auto module_end = frame::containing(module_physical_start + module_size) + 1; + + std::ranges::for_each(std::views::iota(module_start, module_end), + [&](auto frame) { new_allocator.mark_used(frame); }); + } } } // namespace -- cgit v1.2.3 From 292c5ff7c0c0ae89cfed7124c3ad931b9f555d19 Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Mon, 16 Mar 2026 19:58:15 +0100 Subject: refactoring --- arch/x86_64/kapi/memory.cpp | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/memory.cpp b/arch/x86_64/kapi/memory.cpp index d9fbe28..f29afe8 100644 --- a/arch/x86_64/kapi/memory.cpp +++ b/arch/x86_64/kapi/memory.cpp @@ -121,10 +121,7 @@ namespace kapi::memory [[maybe_unused]] auto remap_bootloader_modules(page_mapper & mapper) -> void { - auto modules = boot::bootstrap_information.mbi->modules(); - - for (auto module : modules) - { + std::ranges::for_each(boot::bootstrap_information.mbi->modules(), [&mapper](auto const & module) { auto module_physical_start = physical_address{module.start_address}; auto module_virtual_start = linear_address{module.start_address + std::bit_cast(&arch::boot::TEACHOS_VMA)}; @@ -135,9 +132,9 @@ namespace kapi::memory { auto page = page::containing(module_virtual_start) + i; auto frame = frame::containing(module_physical_start) + i; - mapper.map(page, frame, page_mapper::flags::writable); + mapper.map(page, frame, page_mapper::flags::writable | page_mapper::flags::supervisor_only); } - } + }); } [[maybe_unused]] auto handoff_to_kernel_pmm(frame_allocator & new_allocator) -> void @@ -176,9 +173,7 @@ namespace kapi::memory std::ranges::for_each(std::views::iota(mbi_start, mbi_end), [&](auto frame) { new_allocator.mark_used(frame); }); - auto modules = boot::bootstrap_information.mbi->modules(); - for (auto module : modules) - { + std::ranges::for_each(boot::bootstrap_information.mbi->modules(), [&](auto const & module) { auto module_physical_start = physical_address{module.start_address}; auto module_size = module.end_address - module.start_address; auto module_start = frame::containing(module_physical_start); @@ -186,7 +181,7 @@ namespace kapi::memory std::ranges::for_each(std::views::iota(module_start, module_end), [&](auto frame) { new_allocator.mark_used(frame); }); - } + }); } } // namespace -- cgit v1.2.3 From a20045fb209edc1e338039c28634c942e3113ea4 Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Mon, 16 Mar 2026 21:06:12 +0100 Subject: Protect boot modules in region_allocator --- arch/x86_64/kapi/memory.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/memory.cpp b/arch/x86_64/kapi/memory.cpp index f29afe8..a354576 100644 --- a/arch/x86_64/kapi/memory.cpp +++ b/arch/x86_64/kapi/memory.cpp @@ -53,6 +53,7 @@ namespace kapi::memory .image_range = std::make_pair(physical_address{&image_span.front()}, physical_address{&image_span.back()}), .mbi_range = std::make_pair(physical_address{&mbi_span.front()}, physical_address{&mbi_span.back()}), .memory_map = *memory_map, + .mbi = mbi, }; } -- cgit v1.2.3 From 59504cfd677dd3e9d9ddb0deea4df7614efedb84 Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Tue, 17 Mar 2026 16:48:26 +0100 Subject: fix rebase (use higher_half_mapper not recursive_page_mapper) --- arch/x86_64/kapi/memory.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/memory.cpp b/arch/x86_64/kapi/memory.cpp index a354576..547d359 100644 --- a/arch/x86_64/kapi/memory.cpp +++ b/arch/x86_64/kapi/memory.cpp @@ -119,7 +119,7 @@ namespace kapi::memory mapper.map(page, frame, page_mapper::flags::supervisor_only); } } - + [[maybe_unused]] auto remap_bootloader_modules(page_mapper & mapper) -> void { std::ranges::for_each(boot::bootstrap_information.mbi->modules(), [&mapper](auto const & module) { @@ -223,7 +223,7 @@ namespace kapi::memory remap_kernel(*higher_half_mapper); remap_vga_text_mode_buffer(*higher_half_mapper); remap_multiboot_information(*higher_half_mapper); - remap_bootloader_modules(*recursive_page_mapper); + remap_bootloader_modules(*higher_half_mapper); auto current_cr3 = arch::cpu::cr3::read(); auto old_pml4 = static_cast(current_cr3.address()); -- cgit v1.2.3 From 12c0586ee15cadfa178e6982dc0f76b047cb2df9 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 18 Mar 2026 15:24:26 +0100 Subject: kapi/memory: remove page/frame size macros --- arch/x86_64/kapi/memory.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/memory.cpp b/arch/x86_64/kapi/memory.cpp index 547d359..07e7465 100644 --- a/arch/x86_64/kapi/memory.cpp +++ b/arch/x86_64/kapi/memory.cpp @@ -110,7 +110,7 @@ namespace kapi::memory auto mbi_size = boot::bootstrap_information.mbi->size_bytes(); auto mbi_physical_start = physical_address{mbi_base & ~std::bit_cast(&arch::boot::TEACHOS_VMA)}; auto mbi_virtual_start = linear_address{mbi_base}; - auto mbi_block_count = (mbi_size + PLATFORM_FRAME_SIZE - 1) / PLATFORM_FRAME_SIZE; + auto mbi_block_count = (mbi_size + frame::size - 1) / frame::size; for (auto i = 0uz; i < mbi_block_count; ++i) { @@ -127,7 +127,7 @@ namespace kapi::memory auto module_virtual_start = linear_address{module.start_address + std::bit_cast(&arch::boot::TEACHOS_VMA)}; auto module_size = module.end_address - module.start_address; - auto module_block_count = (module_size + PLATFORM_FRAME_SIZE - 1) / PLATFORM_FRAME_SIZE; + auto module_block_count = (module_size + frame::size - 1) / frame::size; for (auto i = 0uz; i < module_block_count; ++i) { -- cgit v1.2.3 From e7ccb96aecae7b231fb05818d7e45a767aebc31d Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 18 Mar 2026 17:18:37 +0100 Subject: kstd: introduce strong type for memory amounts --- arch/x86_64/kapi/memory.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/memory.cpp b/arch/x86_64/kapi/memory.cpp index 07e7465..5f12e5c 100644 --- a/arch/x86_64/kapi/memory.cpp +++ b/arch/x86_64/kapi/memory.cpp @@ -13,6 +13,7 @@ #include "arch/memory/region_allocator.hpp" #include +#include #include #include @@ -28,6 +29,8 @@ #include #include +using namespace kstd::units_literals; + namespace kapi::memory { @@ -46,7 +49,7 @@ namespace kapi::memory } auto const & mbi = boot::bootstrap_information.mbi; - auto mbi_span = std::span{std::bit_cast(mbi), mbi->size_bytes()}; + auto mbi_span = std::span{std::bit_cast(mbi), static_cast(mbi->size())}; auto image_span = std::span{&arch::boot::_start_physical, &arch::boot::_end_physical}; return arch::memory::region_allocator::memory_information{ @@ -107,10 +110,10 @@ namespace kapi::memory [[maybe_unused]] auto remap_multiboot_information(page_mapper & mapper) -> void { auto mbi_base = std::bit_cast(boot::bootstrap_information.mbi); - auto mbi_size = boot::bootstrap_information.mbi->size_bytes(); + auto mbi_size = boot::bootstrap_information.mbi->size(); auto mbi_physical_start = physical_address{mbi_base & ~std::bit_cast(&arch::boot::TEACHOS_VMA)}; auto mbi_virtual_start = linear_address{mbi_base}; - auto mbi_block_count = (mbi_size + frame::size - 1) / frame::size; + auto mbi_block_count = (mbi_size + frame::size - 1_B) / frame::size; for (auto i = 0uz; i < mbi_block_count; ++i) { @@ -126,8 +129,8 @@ namespace kapi::memory auto module_physical_start = physical_address{module.start_address}; auto module_virtual_start = linear_address{module.start_address + std::bit_cast(&arch::boot::TEACHOS_VMA)}; - auto module_size = module.end_address - module.start_address; - auto module_block_count = (module_size + frame::size - 1) / frame::size; + auto module_size = static_cast(module.end_address - module.start_address); + auto module_block_count = (module_size + frame::size - 1_B) / frame::size; for (auto i = 0uz; i < module_block_count; ++i) { @@ -147,7 +150,7 @@ namespace kapi::memory })) { auto start = frame::containing(physical_address{region.base}); - auto count = region.size_in_B / page::size; + auto count = kstd::units::bytes{region.size_in_B} / page::size; new_allocator.release_many({start, count}); } @@ -167,7 +170,7 @@ namespace kapi::memory [&](auto frame) { new_allocator.mark_used(frame); }); auto mbi_base = std::bit_cast(boot::bootstrap_information.mbi); - auto mbi_size = boot::bootstrap_information.mbi->size_bytes(); + auto mbi_size = boot::bootstrap_information.mbi->size(); auto mbi_address = physical_address{mbi_base & ~std::bit_cast(&arch::boot::TEACHOS_VMA)}; auto mbi_start = frame::containing(mbi_address); auto mbi_end = frame::containing(mbi_address + mbi_size) + 1; -- cgit v1.2.3 From 69b8b89542530eb7360dddd0875610f4cca9268b Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 20 Mar 2026 17:34:56 +0100 Subject: x86_64/cpu: move gdt initialization code --- arch/x86_64/kapi/system.cpp | 113 +------------------------------------------- 1 file changed, 2 insertions(+), 111 deletions(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/system.cpp b/arch/x86_64/kapi/system.cpp index ca4418e..ffb6a46 100644 --- a/arch/x86_64/kapi/system.cpp +++ b/arch/x86_64/kapi/system.cpp @@ -1,122 +1,13 @@ #include "kapi/system.hpp" -#include "arch/cpu/global_descriptor_table.hpp" -#include "arch/cpu/segment_descriptor.hpp" -#include "arch/cpu/task_state_segment.hpp" - -#include - -#include -#include +#include "arch/cpu/initialization.hpp" namespace kapi::system { - namespace - { - constexpr auto gdt_null_descriptor = arch::cpu::segment_descriptor{}; - - constexpr auto gdt_kernel_code_descriptor = arch::cpu::segment_descriptor{ - .limit_low = 0xffff, - .base_low = 0, - .accessed = false, - .read_write = false, - .direction_or_conforming = false, - .executable = true, - .type = arch::cpu::segment_type::code_or_data, - .privilege_level = 0, - .present = true, - .limit_high = 0xf, - .long_mode = true, - .protected_mode = false, - .granularity = arch::cpu::segment_granularity::page, - .base_high = 0, - }; - - constexpr auto gdt_kernel_data_descriptor = arch::cpu::segment_descriptor{ - .limit_low = 0xffff, - .base_low = 0, - .accessed = false, - .read_write = true, - .direction_or_conforming = false, - .executable = false, - .type = arch::cpu::segment_type::code_or_data, - .privilege_level = 0, - .present = true, - .limit_high = 0xf, - .long_mode = false, - .protected_mode = true, - .granularity = arch::cpu::segment_granularity::page, - .base_high = 0, - }; - - constexpr auto gdt_user_code_descriptor = arch::cpu::segment_descriptor{ - .limit_low = 0xffff, - .base_low = 0, - .accessed = false, - .read_write = false, - .direction_or_conforming = false, - .executable = true, - .type = arch::cpu::segment_type::code_or_data, - .privilege_level = 3, - .present = true, - .limit_high = 0xf, - .long_mode = true, - .protected_mode = false, - .granularity = arch::cpu::segment_granularity::page, - .base_high = 0, - }; - - constexpr auto gdt_user_data_descriptor = arch::cpu::segment_descriptor{ - .limit_low = 0xffff, - .base_low = 0, - .accessed = false, - .read_write = true, - .direction_or_conforming = false, - .executable = false, - .type = arch::cpu::segment_type::code_or_data, - .privilege_level = 3, - .present = true, - .limit_high = 0xf, - .long_mode = false, - .protected_mode = false, - .granularity = arch::cpu::segment_granularity::page, - .base_high = 0, - }; - } // namespace - auto memory_initialized() -> void { - auto static tss = arch::cpu::task_state_segment{}; - auto static tss_descriptor = arch::cpu::system_segment_descriptor{ - { - .limit_low = (sizeof(tss) - 1) & 0xffff, // NOLINT(readability-magic-numbers) - .base_low = std::bit_cast(&tss) & 0xffffff, // NOLINT(readability-magic-numbers) - .accessed = false, - .read_write = false, - .direction_or_conforming = false, - .executable = false, - .type = arch::cpu::segment_type::system, - .privilege_level = 0, - .present = true, - .limit_high = ((sizeof(tss) - 1) >> 16) & 0xf, // NOLINT(readability-magic-numbers) - .long_mode = false, - .protected_mode = false, - .granularity = arch::cpu::segment_granularity::byte, - .base_high = (std::bit_cast(&tss) >> 24) & 0xff, // NOLINT(readability-magic-numbers) - }, - (std::bit_cast(&tss) >> 32) & 0xffff'ffff, // NOLINT(readability-magic-numbers) - }; - - auto static gdt = arch::cpu::global_descriptor_table{ - gdt_null_descriptor, gdt_kernel_code_descriptor, gdt_kernel_data_descriptor, - gdt_user_code_descriptor, gdt_user_data_descriptor, tss_descriptor, - }; - - kstd::println("[x86_64:SYS] Reloading Global Descriptor Table."); - gdt.load(1, 2); - - kstd::println("[x86_64:SYS] TODO: initialize Interrupt Descriptor Table."); + arch::cpu::initialize_descriptors(); } } // namespace kapi::system \ No newline at end of file -- cgit v1.2.3 From eb8074e9003034ef2186b62fc66b1073455be5de Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Sat, 21 Mar 2026 08:48:26 +0100 Subject: x86_64/cpu: fixup 8259 interrupts --- arch/x86_64/kapi/system.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/system.cpp b/arch/x86_64/kapi/system.cpp index ffb6a46..301169f 100644 --- a/arch/x86_64/kapi/system.cpp +++ b/arch/x86_64/kapi/system.cpp @@ -1,6 +1,7 @@ #include "kapi/system.hpp" #include "arch/cpu/initialization.hpp" +#include "arch/cpu/interrupts.hpp" namespace kapi::system { @@ -8,6 +9,8 @@ namespace kapi::system auto memory_initialized() -> void { arch::cpu::initialize_descriptors(); + arch::cpu::initialize_legacy_interrupts(); + arch::cpu::enable_interrupts(); } } // namespace kapi::system \ No newline at end of file -- cgit v1.2.3 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 --- arch/x86_64/kapi/cpu.cpp | 21 +++++++++++++++++++++ arch/x86_64/kapi/system.cpp | 10 +--------- 2 files changed, 22 insertions(+), 9 deletions(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/cpu.cpp b/arch/x86_64/kapi/cpu.cpp index 2a0f8f7..693d328 100644 --- a/arch/x86_64/kapi/cpu.cpp +++ b/arch/x86_64/kapi/cpu.cpp @@ -1,8 +1,29 @@ #include "kapi/cpu.hpp" +#include "kapi/system.hpp" + +#include "arch/cpu/initialization.hpp" +#include "arch/cpu/interrupts.hpp" + +#include + namespace kapi::cpu { + auto init() -> void + { + auto static constinit is_initialized = std::atomic_flag{}; + + if (is_initialized.test_and_set()) + { + system::panic("[x86_64] CPU has already been initialized."); + } + + arch::cpu::initialize_descriptors(); + arch::cpu::initialize_legacy_interrupts(); + arch::cpu::enable_interrupts(); + } + auto halt() -> void { asm volatile("1: hlt\njmp 1b"); diff --git a/arch/x86_64/kapi/system.cpp b/arch/x86_64/kapi/system.cpp index 301169f..09c7152 100644 --- a/arch/x86_64/kapi/system.cpp +++ b/arch/x86_64/kapi/system.cpp @@ -1,16 +1,8 @@ #include "kapi/system.hpp" -#include "arch/cpu/initialization.hpp" -#include "arch/cpu/interrupts.hpp" - namespace kapi::system { - auto memory_initialized() -> void - { - arch::cpu::initialize_descriptors(); - arch::cpu::initialize_legacy_interrupts(); - arch::cpu::enable_interrupts(); - } + auto memory_initialized() -> void {} } // namespace kapi::system \ 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 --- arch/x86_64/kapi/cpu.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/cpu.cpp b/arch/x86_64/kapi/cpu.cpp index 693d328..8ca3847 100644 --- a/arch/x86_64/kapi/cpu.cpp +++ b/arch/x86_64/kapi/cpu.cpp @@ -3,7 +3,6 @@ #include "kapi/system.hpp" #include "arch/cpu/initialization.hpp" -#include "arch/cpu/interrupts.hpp" #include @@ -21,7 +20,6 @@ namespace kapi::cpu arch::cpu::initialize_descriptors(); arch::cpu::initialize_legacy_interrupts(); - arch::cpu::enable_interrupts(); } auto halt() -> void @@ -30,4 +28,14 @@ namespace kapi::cpu __builtin_unreachable(); } + auto enable_interrupts() -> void + { + asm volatile("sti"); + } + + auto disable_interrupts() -> void + { + asm volatile("cli"); + } + } // namespace kapi::cpu -- 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 --- arch/x86_64/kapi/cpu.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/cpu.cpp b/arch/x86_64/kapi/cpu.cpp index 8ca3847..b19ba21 100644 --- a/arch/x86_64/kapi/cpu.cpp +++ b/arch/x86_64/kapi/cpu.cpp @@ -4,11 +4,22 @@ #include "arch/cpu/initialization.hpp" +#include +#include + +#include #include +#include namespace kapi::cpu { + namespace + { + constexpr auto irq_offset = 32uz; + auto constinit interrupt_handlers = std::array, 256 - irq_offset>{}; + } // namespace + auto init() -> void { auto static constinit is_initialized = std::atomic_flag{}; @@ -38,4 +49,42 @@ namespace kapi::cpu 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 -- cgit v1.2.3 From d56700342ea0266a6e49f9515eb83279f66b4fcf Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 26 Mar 2026 15:28:41 +0100 Subject: x86_64: split kapi::cpu implementation --- arch/x86_64/kapi/cpu.cpp | 59 -------------------------------- arch/x86_64/kapi/cpu/interrupts.cpp | 67 +++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 59 deletions(-) create mode 100644 arch/x86_64/kapi/cpu/interrupts.cpp (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/cpu.cpp b/arch/x86_64/kapi/cpu.cpp index b19ba21..12edb0f 100644 --- a/arch/x86_64/kapi/cpu.cpp +++ b/arch/x86_64/kapi/cpu.cpp @@ -4,22 +4,11 @@ #include "arch/cpu/initialization.hpp" -#include -#include - -#include #include -#include namespace kapi::cpu { - namespace - { - constexpr auto irq_offset = 32uz; - auto constinit interrupt_handlers = std::array, 256 - irq_offset>{}; - } // namespace - auto init() -> void { auto static constinit is_initialized = std::atomic_flag{}; @@ -39,52 +28,4 @@ namespace kapi::cpu __builtin_unreachable(); } - 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 diff --git a/arch/x86_64/kapi/cpu/interrupts.cpp b/arch/x86_64/kapi/cpu/interrupts.cpp new file mode 100644 index 0000000..b98595c --- /dev/null +++ b/arch/x86_64/kapi/cpu/interrupts.cpp @@ -0,0 +1,67 @@ +#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 -- 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 --- arch/x86_64/kapi/cpu/interrupts.cpp | 67 ------------------------------------ arch/x86_64/kapi/interrupts.cpp | 68 +++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 67 deletions(-) delete mode 100644 arch/x86_64/kapi/cpu/interrupts.cpp create mode 100644 arch/x86_64/kapi/interrupts.cpp (limited to 'arch/x86_64/kapi') 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 -- 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 --- arch/x86_64/kapi/interrupts.cpp | 52 ----------------------------------------- 1 file changed, 52 deletions(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/interrupts.cpp b/arch/x86_64/kapi/interrupts.cpp index babc926..cf1defa 100644 --- a/arch/x86_64/kapi/interrupts.cpp +++ b/arch/x86_64/kapi/interrupts.cpp @@ -1,22 +1,8 @@ #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"); @@ -27,42 +13,4 @@ namespace kapi::interrupts 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 -- cgit v1.2.3 From b84c4c9d8c90f3d3fd5a60de282278912fad2f04 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 2 Apr 2026 13:59:27 +0200 Subject: x86_64/devices: implement ISA bus stub --- arch/x86_64/kapi/devices.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 arch/x86_64/kapi/devices.cpp (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/devices.cpp b/arch/x86_64/kapi/devices.cpp new file mode 100644 index 0000000..25185d6 --- /dev/null +++ b/arch/x86_64/kapi/devices.cpp @@ -0,0 +1,22 @@ +#include "kapi/devices.hpp" + +#include "arch/bus/isa.hpp" + +#include +#include + +#include + +namespace kapi::devices +{ + + auto init_platform_devices() -> void + { + kstd::println("[x86_64:devices] Initializing ISA bus..."); + auto isa_bus = kstd::make_unique(); + + auto & root_bus = get_root_bus(); + root_bus.add_child(std::move(isa_bus)); + } + +} // namespace kapi::devices \ No newline at end of file -- cgit v1.2.3 From ab4c59912c526d515e6e72188c08a7f92e5573e8 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 2 Apr 2026 15:07:54 +0200 Subject: x86_64: implement legacy PIT driver --- arch/x86_64/kapi/devices.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/devices.cpp b/arch/x86_64/kapi/devices.cpp index 25185d6..7aa7090 100644 --- a/arch/x86_64/kapi/devices.cpp +++ b/arch/x86_64/kapi/devices.cpp @@ -1,20 +1,31 @@ #include "kapi/devices.hpp" #include "arch/bus/isa.hpp" +#include "arch/devices/legacy_pit.hpp" #include #include +#include #include namespace kapi::devices { + namespace + { + constexpr auto pit_frequency_in_hz = std::uint32_t{100u}; + } + auto init_platform_devices() -> void { kstd::println("[x86_64:devices] Initializing ISA bus..."); + auto isa_bus = kstd::make_unique(); + auto pit = kstd::make_unique(pit_frequency_in_hz); + isa_bus->add_child(std::move(pit)); + auto & root_bus = get_root_bus(); root_bus.add_child(std::move(isa_bus)); } -- cgit v1.2.3 From 21489576381d827871e7cdf060929c5d7f3d4e9f Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 2 Apr 2026 15:49:14 +0200 Subject: devices: don't automatically allocate major numbers in ctors --- arch/x86_64/kapi/devices.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/devices.cpp b/arch/x86_64/kapi/devices.cpp index 7aa7090..b15503d 100644 --- a/arch/x86_64/kapi/devices.cpp +++ b/arch/x86_64/kapi/devices.cpp @@ -21,9 +21,11 @@ namespace kapi::devices { kstd::println("[x86_64:devices] Initializing ISA bus..."); - auto isa_bus = kstd::make_unique(); + auto isa_major_number = kapi::devices::allocate_major_number(); + auto isa_bus = kstd::make_unique(isa_major_number); - auto pit = kstd::make_unique(pit_frequency_in_hz); + auto pit_major_number = kapi::devices::allocate_major_number(); + auto pit = kstd::make_unique(pit_major_number, pit_frequency_in_hz); isa_bus->add_child(std::move(pit)); auto & root_bus = get_root_bus(); -- cgit v1.2.3 From 3dcd14a0570fef3bcc68d7df42fe3ff4cd496f93 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 6 Apr 2026 14:47:37 +0200 Subject: kapi: hook ACPI initialization up to boot process --- arch/x86_64/kapi/devices.cpp | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/devices.cpp b/arch/x86_64/kapi/devices.cpp index b15503d..e1773e7 100644 --- a/arch/x86_64/kapi/devices.cpp +++ b/arch/x86_64/kapi/devices.cpp @@ -1,5 +1,9 @@ #include "kapi/devices.hpp" +#include "kapi/acpi.hpp" +#include "kapi/boot.hpp" + +#include "arch/boot/boot.hpp" #include "arch/bus/isa.hpp" #include "arch/devices/legacy_pit.hpp" @@ -19,7 +23,33 @@ namespace kapi::devices auto init_platform_devices() -> void { - kstd::println("[x86_64:devices] Initializing ISA bus..."); + auto const & mbi = boot::bootstrap_information.mbi; + auto system_description_pointer = static_cast(nullptr); + + if (auto const & xsdp = mbi->maybe_acpi_xsdp()) + { + auto data = xsdp->pointer().data(); + system_description_pointer = reinterpret_cast(data); + } + else if (auto const & rsdp = mbi->maybe_acpi_rsdp()) + { + auto data = rsdp->pointer().data(); + system_description_pointer = reinterpret_cast(data); + } + + if (system_description_pointer) + { + if (!kapi::acpi::init(*system_description_pointer)) + { + kstd::println(kstd::print_sink::stderr, "[x86_64:DEV] ACPI initialization failed. No tables loaded."); + } + } + else + { + kstd::println(kstd::print_sink::stderr, "[x86_64:DEV] No ACPI RSDP found. Most devices will not be available."); + } + + kstd::println("[x86_64:DEV] Initializing ISA bus..."); auto isa_major_number = kapi::devices::allocate_major_number(); auto isa_bus = kstd::make_unique(isa_major_number); -- cgit v1.2.3 From c18feddd51d1a1398d1245229c5f889dd40554b3 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 6 Apr 2026 15:54:21 +0200 Subject: x86_64/devices: extract initialization code --- arch/x86_64/kapi/devices.cpp | 57 +++----------------------------------------- 1 file changed, 3 insertions(+), 54 deletions(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/devices.cpp b/arch/x86_64/kapi/devices.cpp index e1773e7..47c7f8c 100644 --- a/arch/x86_64/kapi/devices.cpp +++ b/arch/x86_64/kapi/devices.cpp @@ -1,65 +1,14 @@ #include "kapi/devices.hpp" -#include "kapi/acpi.hpp" -#include "kapi/boot.hpp" - -#include "arch/boot/boot.hpp" -#include "arch/bus/isa.hpp" -#include "arch/devices/legacy_pit.hpp" - -#include -#include - -#include -#include +#include "arch/devices/init.hpp" namespace kapi::devices { - namespace - { - constexpr auto pit_frequency_in_hz = std::uint32_t{100u}; - } - auto init_platform_devices() -> void { - auto const & mbi = boot::bootstrap_information.mbi; - auto system_description_pointer = static_cast(nullptr); - - if (auto const & xsdp = mbi->maybe_acpi_xsdp()) - { - auto data = xsdp->pointer().data(); - system_description_pointer = reinterpret_cast(data); - } - else if (auto const & rsdp = mbi->maybe_acpi_rsdp()) - { - auto data = rsdp->pointer().data(); - system_description_pointer = reinterpret_cast(data); - } - - if (system_description_pointer) - { - if (!kapi::acpi::init(*system_description_pointer)) - { - kstd::println(kstd::print_sink::stderr, "[x86_64:DEV] ACPI initialization failed. No tables loaded."); - } - } - else - { - kstd::println(kstd::print_sink::stderr, "[x86_64:DEV] No ACPI RSDP found. Most devices will not be available."); - } - - kstd::println("[x86_64:DEV] Initializing ISA bus..."); - - auto isa_major_number = kapi::devices::allocate_major_number(); - auto isa_bus = kstd::make_unique(isa_major_number); - - auto pit_major_number = kapi::devices::allocate_major_number(); - auto pit = kstd::make_unique(pit_major_number, pit_frequency_in_hz); - isa_bus->add_child(std::move(pit)); - - auto & root_bus = get_root_bus(); - root_bus.add_child(std::move(isa_bus)); + arch::devices::init_acpi_devices(); + arch::devices::init_legacy_devices(); } } // namespace kapi::devices \ No newline at end of file -- cgit v1.2.3 From f456f1674d48932846eb7b5ec1df630ad67e7e3d Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 6 Apr 2026 17:24:36 +0200 Subject: kernel/acpi: discover local interrupt controllers --- arch/x86_64/kapi/acpi.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 arch/x86_64/kapi/acpi.cpp (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/acpi.cpp b/arch/x86_64/kapi/acpi.cpp new file mode 100644 index 0000000..9766154 --- /dev/null +++ b/arch/x86_64/kapi/acpi.cpp @@ -0,0 +1,43 @@ +#include "kapi/acpi.hpp" + +#include "kapi/devices.hpp" +#include "kapi/memory.hpp" + +#include "arch/boot/boot.hpp" +#include "arch/devices/local_apic.hpp" + +#include + +#include +#include + +namespace kapi::acpi +{ + + auto get_root_pointer() -> kstd::observer_ptr + { + auto const & mbi = kapi::boot::bootstrap_information.mbi; + auto system_description_pointer = static_cast(nullptr); + + if (auto const & xsdp = mbi->maybe_acpi_xsdp()) + { + auto data = xsdp->pointer().data(); + + system_description_pointer = reinterpret_cast(data); + } + else if (auto const & rsdp = mbi->maybe_acpi_rsdp()) + { + auto data = rsdp->pointer().data(); + system_description_pointer = reinterpret_cast(data); + } + + return kstd::make_observer(system_description_pointer); + } + + auto create_local_interrupt_controller(std::size_t major, std::size_t minor, std::uint64_t hardware_id, + memory::physical_address address) -> kstd::unique_ptr + { + return kstd::make_unique(major, minor, hardware_id, address); + } + +} // namespace kapi::acpi \ No newline at end of file -- cgit v1.2.3 From f50815110789a0f8f6e5ca66ffd49b26578791a9 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 6 Apr 2026 18:43:28 +0200 Subject: kernel: generalize CPU discovery --- arch/x86_64/kapi/acpi.cpp | 13 -------- arch/x86_64/kapi/platform.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 13 deletions(-) create mode 100644 arch/x86_64/kapi/platform.cpp (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/acpi.cpp b/arch/x86_64/kapi/acpi.cpp index 9766154..ec38aee 100644 --- a/arch/x86_64/kapi/acpi.cpp +++ b/arch/x86_64/kapi/acpi.cpp @@ -1,16 +1,9 @@ #include "kapi/acpi.hpp" -#include "kapi/devices.hpp" -#include "kapi/memory.hpp" - #include "arch/boot/boot.hpp" -#include "arch/devices/local_apic.hpp" #include -#include -#include - namespace kapi::acpi { @@ -34,10 +27,4 @@ namespace kapi::acpi return kstd::make_observer(system_description_pointer); } - auto create_local_interrupt_controller(std::size_t major, std::size_t minor, std::uint64_t hardware_id, - memory::physical_address address) -> kstd::unique_ptr - { - return kstd::make_unique(major, minor, hardware_id, address); - } - } // namespace kapi::acpi \ No newline at end of file diff --git a/arch/x86_64/kapi/platform.cpp b/arch/x86_64/kapi/platform.cpp new file mode 100644 index 0000000..2e7c7e4 --- /dev/null +++ b/arch/x86_64/kapi/platform.cpp @@ -0,0 +1,70 @@ +#include "kapi/platform.hpp" + +#include "kapi/acpi.hpp" +#include "kapi/devices.hpp" +#include "kapi/memory.hpp" + +#include "arch/devices/local_apic.hpp" + +#include +#include + +#include +#include + +namespace kapi::platform +{ + + namespace + { + constexpr auto default_lapic_address = kapi::memory::physical_address{0xFEE0'0000}; + } + + auto discover_cpu_topology(kapi::devices::bus & bus) -> bool + { + auto madt = kapi::acpi::get_table("APIC"); + if (!madt) + { + kstd::println("[x86_64:PLT] Failed to find ACPI APIC table"); + return false; + } + + auto const * current = reinterpret_cast(madt.get()) + sizeof(kapi::acpi::madt_header); + auto const * end = reinterpret_cast(madt.get()) + madt->length(); + + auto bsp_found = false; + auto core_count = 0uz; + + while (current < end) + { + auto const * sub_table = reinterpret_cast(current); + if (sub_table->type() == 0) + { + auto const * local_apic = reinterpret_cast(sub_table); + if (local_apic->flags() & 0b11) + { + auto is_bsp = !bsp_found; + bsp_found = true; + + if (kapi::platform::cpu_detected(bus, local_apic->processor_id(), is_bsp)) + { + ++core_count; + } + } + } + + current += sub_table->length(); + } + + kstd::println("[x86_64:PLT] Found {} CPU cores", core_count); + + return core_count > 0; + } + + auto create_core_interrupt_controller(std::size_t major, std::size_t minor, std::uint64_t hardware_id) + -> kstd::unique_ptr + { + return kstd::make_unique(major, minor, hardware_id, default_lapic_address); + } + +} // namespace kapi::platform \ No newline at end of file -- cgit v1.2.3 From d5c2e101d62f6b4b69c45c127e7a729d246da566 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 6 Apr 2026 19:04:16 +0200 Subject: kapi/platform: invert discovery dependencies --- arch/x86_64/kapi/platform.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/platform.cpp b/arch/x86_64/kapi/platform.cpp index 2e7c7e4..380fc66 100644 --- a/arch/x86_64/kapi/platform.cpp +++ b/arch/x86_64/kapi/platform.cpp @@ -10,7 +10,7 @@ #include #include -#include +#include namespace kapi::platform { @@ -22,6 +22,10 @@ namespace kapi::platform auto discover_cpu_topology(kapi::devices::bus & bus) -> bool { + auto static const core_major = kapi::devices::allocate_major_number(); + auto static const interrupt_controller_major = kapi::devices::allocate_major_number(); + auto static core_index = 0uz; + auto madt = kapi::acpi::get_table("APIC"); if (!madt) { @@ -46,7 +50,10 @@ namespace kapi::platform auto is_bsp = !bsp_found; bsp_found = true; - if (kapi::platform::cpu_detected(bus, local_apic->processor_id(), is_bsp)) + auto lapic = kstd::make_unique(interrupt_controller_major, core_index, + local_apic->apic_id(), default_lapic_address); + if (kapi::platform::cpu_detected(bus, core_major, core_index, local_apic->processor_id(), is_bsp, + std::move(lapic))) { ++core_count; } @@ -61,10 +68,4 @@ namespace kapi::platform return core_count > 0; } - auto create_core_interrupt_controller(std::size_t major, std::size_t minor, std::uint64_t hardware_id) - -> kstd::unique_ptr - { - return kstd::make_unique(major, minor, hardware_id, default_lapic_address); - } - } // namespace kapi::platform \ No newline at end of file -- cgit v1.2.3 From 878852c94c4d56f303366cec177b3edef9b3b9c5 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 8 Apr 2026 13:54:52 +0200 Subject: kapi: add basic support for MMIO mapping --- arch/x86_64/kapi/memory.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/memory.cpp b/arch/x86_64/kapi/memory.cpp index 5f12e5c..853639c 100644 --- a/arch/x86_64/kapi/memory.cpp +++ b/arch/x86_64/kapi/memory.cpp @@ -245,6 +245,7 @@ namespace kapi::memory [](auto const & region) { return region.base + region.size_in_B; })); init_pmm(frame::containing(physical_address{highest_byte}).number() + 1, handoff_to_kernel_pmm); + init_mmio(mmio_base, 1_GiB / page::size); kstd::println("[x86_64:MEM] Releasing bootstrap memory allocators."); region_based_allocator.reset(); -- cgit v1.2.3 From 1c52a859d105f6b0f8afb16565b10435fa728882 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 8 Apr 2026 14:15:56 +0200 Subject: kapi: fix mmio initialization --- arch/x86_64/kapi/memory.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/memory.cpp b/arch/x86_64/kapi/memory.cpp index 853639c..5f12e5c 100644 --- a/arch/x86_64/kapi/memory.cpp +++ b/arch/x86_64/kapi/memory.cpp @@ -245,7 +245,6 @@ namespace kapi::memory [](auto const & region) { return region.base + region.size_in_B; })); init_pmm(frame::containing(physical_address{highest_byte}).number() + 1, handoff_to_kernel_pmm); - init_mmio(mmio_base, 1_GiB / page::size); kstd::println("[x86_64:MEM] Releasing bootstrap memory allocators."); region_based_allocator.reset(); -- cgit v1.2.3 From 296742bfa509524dc8effc3dcae4b6231d37705f Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 8 Apr 2026 14:30:53 +0200 Subject: x86_64: don't hardcode the LAPIC address --- arch/x86_64/kapi/platform.cpp | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/platform.cpp b/arch/x86_64/kapi/platform.cpp index 380fc66..c6e41d2 100644 --- a/arch/x86_64/kapi/platform.cpp +++ b/arch/x86_64/kapi/platform.cpp @@ -2,7 +2,6 @@ #include "kapi/acpi.hpp" #include "kapi/devices.hpp" -#include "kapi/memory.hpp" #include "arch/devices/local_apic.hpp" @@ -15,11 +14,6 @@ namespace kapi::platform { - namespace - { - constexpr auto default_lapic_address = kapi::memory::physical_address{0xFEE0'0000}; - } - auto discover_cpu_topology(kapi::devices::bus & bus) -> bool { auto static const core_major = kapi::devices::allocate_major_number(); @@ -33,8 +27,9 @@ namespace kapi::platform return false; } - auto const * current = reinterpret_cast(madt.get()) + sizeof(kapi::acpi::madt_header); - auto const * end = reinterpret_cast(madt.get()) + madt->length(); + auto real_madt = static_cast(madt.get()); + auto current = reinterpret_cast(madt.get()) + sizeof(kapi::acpi::madt_header); + auto end = reinterpret_cast(madt.get()) + madt->length(); auto bsp_found = false; auto core_count = 0uz; @@ -49,9 +44,9 @@ namespace kapi::platform { auto is_bsp = !bsp_found; bsp_found = true; - auto lapic = kstd::make_unique(interrupt_controller_major, core_index, - local_apic->apic_id(), default_lapic_address); + local_apic->apic_id(), + real_madt->local_interrupt_controller_address()); if (kapi::platform::cpu_detected(bus, core_major, core_index, local_apic->processor_id(), is_bsp, std::move(lapic))) { @@ -68,4 +63,4 @@ namespace kapi::platform return core_count > 0; } -} // namespace kapi::platform \ No newline at end of file +} // namespace kapi::platform -- cgit v1.2.3 From 2ed34cc51a534171f0fe08808634834bc22cf84d Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 8 Apr 2026 14:55:18 +0200 Subject: x86_64: only initialize BSP LAPIC --- arch/x86_64/kapi/platform.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/platform.cpp b/arch/x86_64/kapi/platform.cpp index c6e41d2..d881f8a 100644 --- a/arch/x86_64/kapi/platform.cpp +++ b/arch/x86_64/kapi/platform.cpp @@ -44,9 +44,9 @@ namespace kapi::platform { auto is_bsp = !bsp_found; bsp_found = true; - auto lapic = kstd::make_unique(interrupt_controller_major, core_index, - local_apic->apic_id(), - real_madt->local_interrupt_controller_address()); + auto lapic = kstd::make_unique( + interrupt_controller_major, core_index, local_apic->apic_id(), + real_madt->local_interrupt_controller_address(), is_bsp); if (kapi::platform::cpu_detected(bus, core_major, core_index, local_apic->processor_id(), is_bsp, std::move(lapic))) { -- cgit v1.2.3 From f36cbd54bb6319050404165e8a3280ccbda05cf3 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 8 Apr 2026 15:51:04 +0200 Subject: x86_64: fix CPU enumeration --- arch/x86_64/kapi/platform.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/platform.cpp b/arch/x86_64/kapi/platform.cpp index d881f8a..4ee35c7 100644 --- a/arch/x86_64/kapi/platform.cpp +++ b/arch/x86_64/kapi/platform.cpp @@ -18,7 +18,6 @@ namespace kapi::platform { auto static const core_major = kapi::devices::allocate_major_number(); auto static const interrupt_controller_major = kapi::devices::allocate_major_number(); - auto static core_index = 0uz; auto madt = kapi::acpi::get_table("APIC"); if (!madt) @@ -45,9 +44,9 @@ namespace kapi::platform auto is_bsp = !bsp_found; bsp_found = true; auto lapic = kstd::make_unique( - interrupt_controller_major, core_index, local_apic->apic_id(), + interrupt_controller_major, core_count, local_apic->apic_id(), real_madt->local_interrupt_controller_address(), is_bsp); - if (kapi::platform::cpu_detected(bus, core_major, core_index, local_apic->processor_id(), is_bsp, + if (kapi::platform::cpu_detected(bus, core_major, core_count, local_apic->processor_id(), is_bsp, std::move(lapic))) { ++core_count; -- cgit v1.2.3 From f6bea6a5f1939f3261392633f6caca186befd555 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 9 Apr 2026 15:54:04 +0200 Subject: kapi: restructure ACPI implementation --- arch/x86_64/kapi/platform.cpp | 49 ++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 26 deletions(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/platform.cpp b/arch/x86_64/kapi/platform.cpp index 4ee35c7..fb27329 100644 --- a/arch/x86_64/kapi/platform.cpp +++ b/arch/x86_64/kapi/platform.cpp @@ -8,57 +8,54 @@ #include #include -#include +#include #include namespace kapi::platform { + namespace + { + constexpr auto candidate_flags = acpi::processor_local_apic::flags::processor_enabled // + | acpi::processor_local_apic::flags::online_capable; + } auto discover_cpu_topology(kapi::devices::bus & bus) -> bool { auto static const core_major = kapi::devices::allocate_major_number(); auto static const interrupt_controller_major = kapi::devices::allocate_major_number(); - auto madt = kapi::acpi::get_table("APIC"); + auto madt = kapi::acpi::get_table(); if (!madt) { kstd::println("[x86_64:PLT] Failed to find ACPI APIC table"); return false; } - auto real_madt = static_cast(madt.get()); - auto current = reinterpret_cast(madt.get()) + sizeof(kapi::acpi::madt_header); - auto end = reinterpret_cast(madt.get()) + madt->length(); - auto bsp_found = false; auto core_count = 0uz; + auto local_apic_address = madt->local_interrupt_controller_address(); - while (current < end) + auto lapic_entries = *madt | std::views::filter([](auto const & entry) { + return entry.type() == acpi::multiple_apic_description_table_entry::types::processor_local_apic; + }) | std::views::transform([](auto const & entry) { + return static_cast(entry); + }) | std::views::filter([](auto const & entry) { + return static_cast(entry.active_flags() & candidate_flags); + }); + + for (auto const & apic : lapic_entries) { - auto const * sub_table = reinterpret_cast(current); - if (sub_table->type() == 0) + auto is_bsp = !bsp_found; + bsp_found = true; + auto instance = kstd::make_unique(interrupt_controller_major, core_count, + apic.apic_id(), local_apic_address, is_bsp); + if (kapi::platform::cpu_detected(bus, core_major, core_count, apic.processor_id(), is_bsp, std::move(instance))) { - auto const * local_apic = reinterpret_cast(sub_table); - if (local_apic->flags() & 0b11) - { - auto is_bsp = !bsp_found; - bsp_found = true; - auto lapic = kstd::make_unique( - interrupt_controller_major, core_count, local_apic->apic_id(), - real_madt->local_interrupt_controller_address(), is_bsp); - if (kapi::platform::cpu_detected(bus, core_major, core_count, local_apic->processor_id(), is_bsp, - std::move(lapic))) - { - ++core_count; - } - } + ++core_count; } - - current += sub_table->length(); } kstd::println("[x86_64:PLT] Found {} CPU cores", core_count); - return core_count > 0; } -- cgit v1.2.3 From 3ad230fab8dc17758559aac3c20ba67a8c619878 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 10 Apr 2026 09:01:59 +0200 Subject: kapi: move platform functions to CPU --- arch/x86_64/kapi/cpu.cpp | 54 +++++++++++++++++++++++++++++++++++++ arch/x86_64/kapi/platform.cpp | 62 ------------------------------------------- 2 files changed, 54 insertions(+), 62 deletions(-) delete mode 100644 arch/x86_64/kapi/platform.cpp (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/cpu.cpp b/arch/x86_64/kapi/cpu.cpp index 12edb0f..1382e08 100644 --- a/arch/x86_64/kapi/cpu.cpp +++ b/arch/x86_64/kapi/cpu.cpp @@ -1,14 +1,28 @@ #include "kapi/cpu.hpp" +#include "kapi/acpi.hpp" +#include "kapi/devices.hpp" #include "kapi/system.hpp" #include "arch/cpu/initialization.hpp" +#include "arch/devices/local_apic.hpp" + +#include +#include #include +#include +#include namespace kapi::cpu { + namespace + { + constexpr auto candidate_flags = acpi::processor_local_apic::flags::processor_enabled // + | acpi::processor_local_apic::flags::online_capable; + } + auto init() -> void { auto static constinit is_initialized = std::atomic_flag{}; @@ -28,4 +42,44 @@ namespace kapi::cpu __builtin_unreachable(); } + auto discover_topology(kapi::devices::bus & bus) -> bool + { + auto static const core_major = kapi::devices::allocate_major_number(); + auto static const interrupt_controller_major = kapi::devices::allocate_major_number(); + + auto madt = kapi::acpi::get_table(); + if (!madt) + { + kstd::println("[x86_64:PLT] Failed to find ACPI APIC table"); + return false; + } + + auto bsp_found = false; + auto core_count = 0uz; + auto local_apic_address = madt->local_interrupt_controller_address(); + + auto lapic_entries = *madt | std::views::filter([](auto const & entry) { + return entry.type() == acpi::multiple_apic_description_table_entry::types::processor_local_apic; + }) | std::views::transform([](auto const & entry) { + return static_cast(entry); + }) | std::views::filter([](auto const & entry) { + return static_cast(entry.active_flags() & candidate_flags); + }); + + for (auto const & apic : lapic_entries) + { + auto is_bsp = !bsp_found; + bsp_found = true; + auto instance = kstd::make_unique(interrupt_controller_major, core_count, + apic.apic_id(), local_apic_address, is_bsp); + if (core_detected(bus, core_major, core_count, apic.processor_id(), is_bsp, std::move(instance))) + { + ++core_count; + } + } + + kstd::println("[x86_64:PLT] Found {} CPU cores", core_count); + return core_count > 0; + } + } // namespace kapi::cpu diff --git a/arch/x86_64/kapi/platform.cpp b/arch/x86_64/kapi/platform.cpp deleted file mode 100644 index fb27329..0000000 --- a/arch/x86_64/kapi/platform.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#include "kapi/platform.hpp" - -#include "kapi/acpi.hpp" -#include "kapi/devices.hpp" - -#include "arch/devices/local_apic.hpp" - -#include -#include - -#include -#include - -namespace kapi::platform -{ - namespace - { - constexpr auto candidate_flags = acpi::processor_local_apic::flags::processor_enabled // - | acpi::processor_local_apic::flags::online_capable; - } - - auto discover_cpu_topology(kapi::devices::bus & bus) -> bool - { - auto static const core_major = kapi::devices::allocate_major_number(); - auto static const interrupt_controller_major = kapi::devices::allocate_major_number(); - - auto madt = kapi::acpi::get_table(); - if (!madt) - { - kstd::println("[x86_64:PLT] Failed to find ACPI APIC table"); - return false; - } - - auto bsp_found = false; - auto core_count = 0uz; - auto local_apic_address = madt->local_interrupt_controller_address(); - - auto lapic_entries = *madt | std::views::filter([](auto const & entry) { - return entry.type() == acpi::multiple_apic_description_table_entry::types::processor_local_apic; - }) | std::views::transform([](auto const & entry) { - return static_cast(entry); - }) | std::views::filter([](auto const & entry) { - return static_cast(entry.active_flags() & candidate_flags); - }); - - for (auto const & apic : lapic_entries) - { - auto is_bsp = !bsp_found; - bsp_found = true; - auto instance = kstd::make_unique(interrupt_controller_major, core_count, - apic.apic_id(), local_apic_address, is_bsp); - if (kapi::platform::cpu_detected(bus, core_major, core_count, apic.processor_id(), is_bsp, std::move(instance))) - { - ++core_count; - } - } - - kstd::println("[x86_64:PLT] Found {} CPU cores", core_count); - return core_count > 0; - } - -} // namespace kapi::platform -- cgit v1.2.3 From dd8dfa3e674d05927e9ed4b7efcb634a634bfdcc Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 10 Apr 2026 10:30:32 +0200 Subject: kapi: move CPU to kapi --- arch/x86_64/kapi/cpu.cpp | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/cpu.cpp b/arch/x86_64/kapi/cpu.cpp index 1382e08..726ec6a 100644 --- a/arch/x86_64/kapi/cpu.cpp +++ b/arch/x86_64/kapi/cpu.cpp @@ -2,6 +2,7 @@ #include "kapi/acpi.hpp" #include "kapi/devices.hpp" +#include "kapi/devices/cpu.hpp" #include "kapi/system.hpp" #include "arch/cpu/initialization.hpp" @@ -42,8 +43,9 @@ namespace kapi::cpu __builtin_unreachable(); } - auto discover_topology(kapi::devices::bus & bus) -> bool + auto discover_topology() -> bool { + auto static const cpu_major = kapi::devices::allocate_major_number(); auto static const core_major = kapi::devices::allocate_major_number(); auto static const interrupt_controller_major = kapi::devices::allocate_major_number(); @@ -54,10 +56,6 @@ namespace kapi::cpu return false; } - auto bsp_found = false; - auto core_count = 0uz; - auto local_apic_address = madt->local_interrupt_controller_address(); - auto lapic_entries = *madt | std::views::filter([](auto const & entry) { return entry.type() == acpi::multiple_apic_description_table_entry::types::processor_local_apic; }) | std::views::transform([](auto const & entry) { @@ -66,18 +64,24 @@ namespace kapi::cpu return static_cast(entry.active_flags() & candidate_flags); }); + auto bsp_found = false; + auto core_count = 0uz; + auto local_apic_address = madt->local_interrupt_controller_address(); + auto cpu_bus = kstd::make_unique(cpu_major, 0); + for (auto const & apic : lapic_entries) { auto is_bsp = !bsp_found; bsp_found = true; - auto instance = kstd::make_unique(interrupt_controller_major, core_count, - apic.apic_id(), local_apic_address, is_bsp); - if (core_detected(bus, core_major, core_count, apic.processor_id(), is_bsp, std::move(instance))) - { - ++core_count; - } + auto core = kstd::make_unique(core_major, core_count, apic.processor_id(), is_bsp); + core->add_child(kstd::make_unique(interrupt_controller_major, core_count, + apic.apic_id(), local_apic_address, is_bsp)); + cpu_bus->add_child(std::move(core)); + ++core_count; } + devices::get_root_bus().add_child(std::move(cpu_bus)); + kstd::println("[x86_64:PLT] Found {} CPU cores", core_count); return core_count > 0; } -- cgit v1.2.3 From c3f7b747f02a79b34ed914c54ce74be973b17af1 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 10 Apr 2026 17:39:14 +0200 Subject: kapi: extract ACPI functionality to libs --- arch/x86_64/kapi/acpi.cpp | 10 ++++++---- arch/x86_64/kapi/cpu.cpp | 15 +++++++++------ 2 files changed, 15 insertions(+), 10 deletions(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/acpi.cpp b/arch/x86_64/kapi/acpi.cpp index ec38aee..40b7160 100644 --- a/arch/x86_64/kapi/acpi.cpp +++ b/arch/x86_64/kapi/acpi.cpp @@ -4,24 +4,26 @@ #include +#include + namespace kapi::acpi { - auto get_root_pointer() -> kstd::observer_ptr + auto get_root_pointer() -> kstd::observer_ptr<::acpi::rsdp const> { auto const & mbi = kapi::boot::bootstrap_information.mbi; - auto system_description_pointer = static_cast(nullptr); + auto system_description_pointer = static_cast<::acpi::rsdp const *>(nullptr); if (auto const & xsdp = mbi->maybe_acpi_xsdp()) { auto data = xsdp->pointer().data(); - system_description_pointer = reinterpret_cast(data); + system_description_pointer = reinterpret_cast<::acpi::xsdp const *>(data); } else if (auto const & rsdp = mbi->maybe_acpi_rsdp()) { auto data = rsdp->pointer().data(); - system_description_pointer = reinterpret_cast(data); + system_description_pointer = reinterpret_cast<::acpi::rsdp const *>(data); } return kstd::make_observer(system_description_pointer); diff --git a/arch/x86_64/kapi/cpu.cpp b/arch/x86_64/kapi/cpu.cpp index 726ec6a..a836b20 100644 --- a/arch/x86_64/kapi/cpu.cpp +++ b/arch/x86_64/kapi/cpu.cpp @@ -3,6 +3,7 @@ #include "kapi/acpi.hpp" #include "kapi/devices.hpp" #include "kapi/devices/cpu.hpp" +#include "kapi/memory.hpp" #include "kapi/system.hpp" #include "arch/cpu/initialization.hpp" @@ -11,6 +12,8 @@ #include #include +#include + #include #include #include @@ -20,8 +23,8 @@ namespace kapi::cpu namespace { - constexpr auto candidate_flags = acpi::processor_local_apic::flags::processor_enabled // - | acpi::processor_local_apic::flags::online_capable; + constexpr auto candidate_flags = ::acpi::processor_local_apic::flags::processor_enabled // + | ::acpi::processor_local_apic::flags::online_capable; } auto init() -> void @@ -49,7 +52,7 @@ namespace kapi::cpu auto static const core_major = kapi::devices::allocate_major_number(); auto static const interrupt_controller_major = kapi::devices::allocate_major_number(); - auto madt = kapi::acpi::get_table(); + auto madt = kapi::acpi::get_table<::acpi::madt_table_signature>(); if (!madt) { kstd::println("[x86_64:PLT] Failed to find ACPI APIC table"); @@ -57,16 +60,16 @@ namespace kapi::cpu } auto lapic_entries = *madt | std::views::filter([](auto const & entry) { - return entry.type() == acpi::multiple_apic_description_table_entry::types::processor_local_apic; + return entry.type() == ::acpi::madt_entry::types::processor_local_apic; }) | std::views::transform([](auto const & entry) { - return static_cast(entry); + return static_cast<::acpi::processor_local_apic const &>(entry); }) | std::views::filter([](auto const & entry) { return static_cast(entry.active_flags() & candidate_flags); }); auto bsp_found = false; auto core_count = 0uz; - auto local_apic_address = madt->local_interrupt_controller_address(); + auto local_apic_address = memory::physical_address{madt->local_interrupt_controller_address()}; auto cpu_bus = kstd::make_unique(cpu_major, 0); for (auto const & apic : lapic_entries) -- cgit v1.2.3 From 21fd1281cf19572e202d583689b99c33ec68da50 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 10 Apr 2026 17:49:40 +0200 Subject: kernel: let arch initialize the ACPI manager --- arch/x86_64/kapi/acpi.cpp | 32 -------------------------------- 1 file changed, 32 deletions(-) delete mode 100644 arch/x86_64/kapi/acpi.cpp (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/acpi.cpp b/arch/x86_64/kapi/acpi.cpp deleted file mode 100644 index 40b7160..0000000 --- a/arch/x86_64/kapi/acpi.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "kapi/acpi.hpp" - -#include "arch/boot/boot.hpp" - -#include - -#include - -namespace kapi::acpi -{ - - auto get_root_pointer() -> kstd::observer_ptr<::acpi::rsdp const> - { - auto const & mbi = kapi::boot::bootstrap_information.mbi; - auto system_description_pointer = static_cast<::acpi::rsdp const *>(nullptr); - - if (auto const & xsdp = mbi->maybe_acpi_xsdp()) - { - auto data = xsdp->pointer().data(); - - system_description_pointer = reinterpret_cast<::acpi::xsdp const *>(data); - } - else if (auto const & rsdp = mbi->maybe_acpi_rsdp()) - { - auto data = rsdp->pointer().data(); - system_description_pointer = reinterpret_cast<::acpi::rsdp const *>(data); - } - - return kstd::make_observer(system_description_pointer); - } - -} // namespace kapi::acpi \ No newline at end of file -- cgit v1.2.3 From 1fa31688a0e237dec1170dcea9e8f0a0571a25e5 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 15 Apr 2026 08:55:03 +0200 Subject: acpi: add basic MADT tests --- arch/x86_64/kapi/cpu.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/cpu.cpp b/arch/x86_64/kapi/cpu.cpp index a836b20..22936c2 100644 --- a/arch/x86_64/kapi/cpu.cpp +++ b/arch/x86_64/kapi/cpu.cpp @@ -60,7 +60,7 @@ namespace kapi::cpu } auto lapic_entries = *madt | std::views::filter([](auto const & entry) { - return entry.type() == ::acpi::madt_entry::types::processor_local_apic; + return entry.type() == ::acpi::madt_entry::type::processor_local_apic; }) | std::views::transform([](auto const & entry) { return static_cast<::acpi::processor_local_apic const &>(entry); }) | std::views::filter([](auto const & entry) { -- cgit v1.2.3 From 776ab2749d5af0a34fd2aa6103a377ddc04d4c53 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 16 Apr 2026 10:03:35 +0200 Subject: acpi: introduce XSDT type --- arch/x86_64/kapi/cpu.cpp | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/cpu.cpp b/arch/x86_64/kapi/cpu.cpp index 22936c2..965998c 100644 --- a/arch/x86_64/kapi/cpu.cpp +++ b/arch/x86_64/kapi/cpu.cpp @@ -23,8 +23,8 @@ namespace kapi::cpu namespace { - constexpr auto candidate_flags = ::acpi::processor_local_apic::flags::processor_enabled // - | ::acpi::processor_local_apic::flags::online_capable; + constexpr auto candidate_flags = ::acpi::processor_local_apic_entry::flags::processor_enabled // + | ::acpi::processor_local_apic_entry::flags::online_capable; } auto init() -> void @@ -52,7 +52,7 @@ namespace kapi::cpu auto static const core_major = kapi::devices::allocate_major_number(); auto static const interrupt_controller_major = kapi::devices::allocate_major_number(); - auto madt = kapi::acpi::get_table<::acpi::madt_table_signature>(); + auto madt = kapi::acpi::get_table<::acpi::table_signature_v<::acpi::madt>>(); if (!madt) { kstd::println("[x86_64:PLT] Failed to find ACPI APIC table"); @@ -62,10 +62,8 @@ namespace kapi::cpu auto lapic_entries = *madt | std::views::filter([](auto const & entry) { return entry.type() == ::acpi::madt_entry::type::processor_local_apic; }) | std::views::transform([](auto const & entry) { - return static_cast<::acpi::processor_local_apic const &>(entry); - }) | std::views::filter([](auto const & entry) { - return static_cast(entry.active_flags() & candidate_flags); - }); + return static_cast<::acpi::processor_local_apic_entry const &>(entry); + }) | std::views::filter([](auto const & entry) { return static_cast(entry.flags() & candidate_flags); }); auto bsp_found = false; auto core_count = 0uz; @@ -77,8 +75,8 @@ namespace kapi::cpu auto is_bsp = !bsp_found; bsp_found = true; auto core = kstd::make_unique(core_major, core_count, apic.processor_id(), is_bsp); - core->add_child(kstd::make_unique(interrupt_controller_major, core_count, - apic.apic_id(), local_apic_address, is_bsp)); + core->add_child(kstd::make_unique(interrupt_controller_major, core_count, apic.id(), + local_apic_address, is_bsp)); cpu_bus->add_child(std::move(core)); ++core_count; } -- cgit v1.2.3 From 2d8fed40bd0d0f8144783b6b344dc79944291b72 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 23 Apr 2026 13:31:17 +0200 Subject: chore: organize includes --- arch/x86_64/kapi/boot_modules.cpp | 6 +++--- arch/x86_64/kapi/cpu.cpp | 8 ++++---- arch/x86_64/kapi/memory.cpp | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/boot_modules.cpp b/arch/x86_64/kapi/boot_modules.cpp index ba01285..1a588cf 100644 --- a/arch/x86_64/kapi/boot_modules.cpp +++ b/arch/x86_64/kapi/boot_modules.cpp @@ -1,14 +1,14 @@ #include "kapi/boot_modules.hpp" +#include "arch/boot/boot.hpp" +#include "arch/boot/ld.hpp" + #include "kapi/boot.hpp" #include "kapi/boot_module/boot_module.hpp" #include "kapi/boot_module/boot_module_registry.hpp" #include "kapi/memory.hpp" #include "kapi/system.hpp" -#include "arch/boot/boot.hpp" -#include "arch/boot/ld.hpp" - #include #include diff --git a/arch/x86_64/kapi/cpu.cpp b/arch/x86_64/kapi/cpu.cpp index 965998c..baeab4b 100644 --- a/arch/x86_64/kapi/cpu.cpp +++ b/arch/x86_64/kapi/cpu.cpp @@ -1,19 +1,19 @@ #include "kapi/cpu.hpp" +#include "arch/cpu/initialization.hpp" +#include "arch/devices/local_apic.hpp" + #include "kapi/acpi.hpp" #include "kapi/devices.hpp" #include "kapi/devices/cpu.hpp" #include "kapi/memory.hpp" #include "kapi/system.hpp" -#include "arch/cpu/initialization.hpp" -#include "arch/devices/local_apic.hpp" +#include #include #include -#include - #include #include #include diff --git a/arch/x86_64/kapi/memory.cpp b/arch/x86_64/kapi/memory.cpp index 5f12e5c..423913d 100644 --- a/arch/x86_64/kapi/memory.cpp +++ b/arch/x86_64/kapi/memory.cpp @@ -1,8 +1,5 @@ #include "kapi/memory.hpp" -#include "kapi/boot.hpp" -#include "kapi/system.hpp" - #include "arch/boot/boot.hpp" #include "arch/boot/ld.hpp" #include "arch/cpu/registers.hpp" @@ -12,6 +9,9 @@ #include "arch/memory/page_utilities.hpp" #include "arch/memory/region_allocator.hpp" +#include "kapi/boot.hpp" +#include "kapi/system.hpp" + #include #include -- cgit v1.2.3 From f6f10575f75ac23d06e1d94f7861611503daa7af Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 23 Apr 2026 14:03:28 +0200 Subject: chore: banish relative includes --- arch/x86_64/kapi/boot_modules.cpp | 16 ++++++++-------- arch/x86_64/kapi/cio.cpp | 6 +++--- arch/x86_64/kapi/cpu.cpp | 16 ++++++++-------- arch/x86_64/kapi/devices.cpp | 4 ++-- arch/x86_64/kapi/interrupts.cpp | 2 +- arch/x86_64/kapi/memory.cpp | 26 +++++++++++++------------- arch/x86_64/kapi/system.cpp | 2 +- 7 files changed, 36 insertions(+), 36 deletions(-) (limited to 'arch/x86_64/kapi') diff --git a/arch/x86_64/kapi/boot_modules.cpp b/arch/x86_64/kapi/boot_modules.cpp index 1a588cf..fb6bf46 100644 --- a/arch/x86_64/kapi/boot_modules.cpp +++ b/arch/x86_64/kapi/boot_modules.cpp @@ -1,13 +1,13 @@ -#include "kapi/boot_modules.hpp" +#include -#include "arch/boot/boot.hpp" -#include "arch/boot/ld.hpp" +#include +#include -#include "kapi/boot.hpp" -#include "kapi/boot_module/boot_module.hpp" -#include "kapi/boot_module/boot_module_registry.hpp" -#include "kapi/memory.hpp" -#include "kapi/system.hpp" +#include +#include +#include +#include +#include #include diff --git a/arch/x86_64/kapi/cio.cpp b/arch/x86_64/kapi/cio.cpp index 015cf5e..b33c6e0 100644 --- a/arch/x86_64/kapi/cio.cpp +++ b/arch/x86_64/kapi/cio.cpp @@ -1,7 +1,7 @@ -#include "kapi/cio.hpp" +#include -#include "arch/debug/qemu_output.hpp" -#include "arch/vga/text.hpp" +#include +#include #include diff --git a/arch/x86_64/kapi/cpu.cpp b/arch/x86_64/kapi/cpu.cpp index baeab4b..40dc228 100644 --- a/arch/x86_64/kapi/cpu.cpp +++ b/arch/x86_64/kapi/cpu.cpp @@ -1,13 +1,13 @@ -#include "kapi/cpu.hpp" +#include -#include "arch/cpu/initialization.hpp" -#include "arch/devices/local_apic.hpp" +#include +#include -#include "kapi/acpi.hpp" -#include "kapi/devices.hpp" -#include "kapi/devices/cpu.hpp" -#include "kapi/memory.hpp" -#include "kapi/system.hpp" +#include +#include +#include +#include +#include #include diff --git a/arch/x86_64/kapi/devices.cpp b/arch/x86_64/kapi/devices.cpp index 47c7f8c..f188c92 100644 --- a/arch/x86_64/kapi/devices.cpp +++ b/arch/x86_64/kapi/devices.cpp @@ -1,6 +1,6 @@ -#include "kapi/devices.hpp" +#include -#include "arch/devices/init.hpp" +#include namespace kapi::devices { diff --git a/arch/x86_64/kapi/interrupts.cpp b/arch/x86_64/kapi/interrupts.cpp index cf1defa..85acc0f 100644 --- a/arch/x86_64/kapi/interrupts.cpp +++ b/arch/x86_64/kapi/interrupts.cpp @@ -1,4 +1,4 @@ -#include "kapi/interrupts.hpp" +#include namespace kapi::interrupts { diff --git a/arch/x86_64/kapi/memory.cpp b/arch/x86_64/kapi/memory.cpp index 423913d..5b870d5 100644 --- a/arch/x86_64/kapi/memory.cpp +++ b/arch/x86_64/kapi/memory.cpp @@ -1,16 +1,16 @@ -#include "kapi/memory.hpp" - -#include "arch/boot/boot.hpp" -#include "arch/boot/ld.hpp" -#include "arch/cpu/registers.hpp" -#include "arch/memory/higher_half_mapper.hpp" -#include "arch/memory/kernel_mapper.hpp" -#include "arch/memory/page_table.hpp" -#include "arch/memory/page_utilities.hpp" -#include "arch/memory/region_allocator.hpp" - -#include "kapi/boot.hpp" -#include "kapi/system.hpp" +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include #include #include diff --git a/arch/x86_64/kapi/system.cpp b/arch/x86_64/kapi/system.cpp index 09c7152..73a77e5 100644 --- a/arch/x86_64/kapi/system.cpp +++ b/arch/x86_64/kapi/system.cpp @@ -1,4 +1,4 @@ -#include "kapi/system.hpp" +#include namespace kapi::system { -- cgit v1.2.3