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/memory.cpp') 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 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/memory.cpp') 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/memory.cpp') 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/memory.cpp') 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/memory.cpp') 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/memory.cpp') 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/memory.cpp') 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/memory.cpp') 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 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/memory.cpp') 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/memory.cpp') 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 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/memory.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'arch/x86_64/kapi/memory.cpp') 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/memory.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'arch/x86_64/kapi/memory.cpp') 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 -- cgit v1.2.3