aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-01-16 13:36:38 +0100
committerFelix Morgner <felix.morgner@ost.ch>2026-01-16 13:36:38 +0100
commit7d6f0ed063790042a808f4bf07c50d308b3f2de4 (patch)
tree1a2e1c4ed7e2f3d8e6cdcfb012e554d1a4eb1e5a /arch/x86_64/src
parent9750405757396d006ab6992fb93baf414b3e2ae8 (diff)
downloadteachos-7d6f0ed063790042a808f4bf07c50d308b3f2de4.tar.xz
teachos-7d6f0ed063790042a808f4bf07c50d308b3f2de4.zip
chore: restructure namespaces
Diffstat (limited to 'arch/x86_64/src')
-rw-r--r--arch/x86_64/src/boot/boot32.S2
-rw-r--r--arch/x86_64/src/boot/initialize_runtime.cpp23
-rw-r--r--arch/x86_64/src/debug/qemu_output.cpp8
-rw-r--r--arch/x86_64/src/kapi/cio.cpp20
-rw-r--r--arch/x86_64/src/kapi/cpu.cpp12
-rw-r--r--arch/x86_64/src/kapi/memory.cpp192
-rw-r--r--arch/x86_64/src/memory/kernel_mapper.cpp36
-rw-r--r--arch/x86_64/src/memory/mmu.cpp12
-rw-r--r--arch/x86_64/src/memory/page_table.cpp12
-rw-r--r--arch/x86_64/src/memory/paging_root.cpp6
-rw-r--r--arch/x86_64/src/memory/recursive_page_mapper.cpp30
-rw-r--r--arch/x86_64/src/memory/region_allocator.cpp30
-rw-r--r--arch/x86_64/src/memory/scoped_mapping.cpp22
-rw-r--r--arch/x86_64/src/vga/text/buffer.cpp8
-rw-r--r--arch/x86_64/src/vga/text/device.cpp24
15 files changed, 110 insertions, 327 deletions
diff --git a/arch/x86_64/src/boot/boot32.S b/arch/x86_64/src/boot/boot32.S
index 0e164e2..056cd8e 100644
--- a/arch/x86_64/src/boot/boot32.S
+++ b/arch/x86_64/src/boot/boot32.S
@@ -1,4 +1,4 @@
-#include "x86_64/boot/boot.hpp"
+#include "arch/boot/boot.hpp"
/**
* @brief Uninitialized data for the bootstrapping process.
diff --git a/arch/x86_64/src/boot/initialize_runtime.cpp b/arch/x86_64/src/boot/initialize_runtime.cpp
index f413448..b08c13c 100644
--- a/arch/x86_64/src/boot/initialize_runtime.cpp
+++ b/arch/x86_64/src/boot/initialize_runtime.cpp
@@ -2,16 +2,21 @@
#include <functional>
#include <span>
-extern "C"
+namespace arch::boot
{
- using global_initializer = auto (*)() -> void;
- extern global_initializer __init_array_start;
- extern global_initializer __init_array_end;
-
- auto invoke_global_constructors() -> void
+ extern "C"
{
- auto initializers = std::span{&__init_array_start, &__init_array_end};
- std::ranges::for_each(initializers, [](auto invokable) { std::invoke(invokable); });
+ using global_initializer = auto (*)() -> void;
+
+ extern global_initializer __init_array_start;
+ extern global_initializer __init_array_end;
+
+ auto invoke_global_constructors() -> void
+ {
+ auto initializers = std::span{&__init_array_start, &__init_array_end};
+ std::ranges::for_each(initializers, [](auto invokable) { std::invoke(invokable); });
+ }
}
-}
+
+} // namespace arch::boot \ No newline at end of file
diff --git a/arch/x86_64/src/debug/qemu_output.cpp b/arch/x86_64/src/debug/qemu_output.cpp
index 0af5bdc..535017d 100644
--- a/arch/x86_64/src/debug/qemu_output.cpp
+++ b/arch/x86_64/src/debug/qemu_output.cpp
@@ -1,11 +1,11 @@
-#include "x86_64/debug/qemu_output.hpp"
+#include "arch/debug/qemu_output.hpp"
#include "kapi/cio.hpp"
#include <algorithm>
#include <string_view>
-namespace teachos::debug::x86_64
+namespace arch::debug
{
qemu_output::qemu_output(output_device & lower)
@@ -13,7 +13,7 @@ namespace teachos::debug::x86_64
, m_present{port::read() == port::address}
{}
- auto qemu_output::write(cio::output_stream stream, std::string_view text) -> void
+ auto qemu_output::write(kapi::cio::output_stream stream, std::string_view text) -> void
{
if (m_present)
{
@@ -22,4 +22,4 @@ namespace teachos::debug::x86_64
m_lower.write(stream, text);
}
-} // namespace teachos::debug::x86_64
+} // namespace arch::debug
diff --git a/arch/x86_64/src/kapi/cio.cpp b/arch/x86_64/src/kapi/cio.cpp
deleted file mode 100644
index 5594fcc..0000000
--- a/arch/x86_64/src/kapi/cio.cpp
+++ /dev/null
@@ -1,20 +0,0 @@
-#include "kapi/cio.hpp"
-
-#include "x86_64/debug/qemu_output.hpp"
-#include "x86_64/vga/text.hpp"
-
-#include <optional>
-
-namespace teachos::cio
-{
-
- auto static constinit vga_device = std::optional<vga::x86_64::text::device>{};
- auto static constinit dbg_device = std::optional<debug::x86_64::qemu_output>{};
-
- auto init() -> void
- {
- vga_device.emplace().cursor(false);
- set_output_device(dbg_device.emplace(*vga_device));
- }
-
-} // namespace teachos::cio
diff --git a/arch/x86_64/src/kapi/cpu.cpp b/arch/x86_64/src/kapi/cpu.cpp
deleted file mode 100644
index 22543ee..0000000
--- a/arch/x86_64/src/kapi/cpu.cpp
+++ /dev/null
@@ -1,12 +0,0 @@
-#include "kapi/cpu.hpp"
-
-namespace teachos::cpu
-{
-
- auto halt() -> void
- {
- asm volatile("1: hlt\njmp 1b");
- __builtin_unreachable();
- }
-
-} // namespace teachos::cpu
diff --git a/arch/x86_64/src/kapi/memory.cpp b/arch/x86_64/src/kapi/memory.cpp
deleted file mode 100644
index e2681a6..0000000
--- a/arch/x86_64/src/kapi/memory.cpp
+++ /dev/null
@@ -1,192 +0,0 @@
-#include "kapi/memory.hpp"
-
-#include "kapi/boot.hpp"
-#include "kapi/system.hpp"
-
-#include "x86_64/boot/boot.hpp"
-#include "x86_64/boot/ld.hpp"
-#include "x86_64/cpu/registers.hpp"
-#include "x86_64/memory/buffered_allocator.hpp"
-#include "x86_64/memory/kernel_mapper.hpp"
-#include "x86_64/memory/mmu.hpp"
-#include "x86_64/memory/page_table.hpp"
-#include "x86_64/memory/page_utilities.hpp"
-#include "x86_64/memory/paging_root.hpp"
-#include "x86_64/memory/recursive_page_mapper.hpp"
-#include "x86_64/memory/region_allocator.hpp"
-#include "x86_64/memory/scoped_mapping.hpp"
-
-#include <kstd/print>
-
-#include <multiboot2/information.hpp>
-
-#include <atomic>
-#include <bit>
-#include <cstddef>
-#include <cstdint>
-#include <memory>
-#include <optional>
-#include <span>
-#include <utility>
-
-namespace teachos::memory
-{
-
- namespace
- {
- constexpr auto static unused_page_address = linear_address{0x0000'7fff'cafe'faceuz};
- constexpr auto static recursive_page_map_index = x86_64::page_table::entry_count - 2;
-
- //! Instantiate a basic, memory region based, early frame allocator for remapping.
- auto collect_memory_information()
- {
- auto memory_map = boot::bootstrap_information.mbi->maybe_memory_map();
- if (!memory_map)
- {
- system::panic("[x86_64] Failed to create early allocator, no memory map available.");
- }
-
- auto const & mbi = boot::bootstrap_information.mbi;
- auto mbi_span = std::span{std::bit_cast<std::byte *>(mbi), mbi->size_bytes()};
- auto image_span = std::span{&boot::x86_64::_start_physical, &boot::x86_64::_end_physical};
-
- return x86_64::region_allocator::memory_information{
- .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,
- };
- }
-
- //! Enable additional CPU protection features, required during later stages of the kernel.
- auto enable_cpu_protections() -> void
- {
- cpu::x86_64::cr0::set(cpu::x86_64::cr0::flags::write_protect);
- cpu::x86_64::i32_efer::set(cpu::x86_64::i32_efer::flags::execute_disable_bit_enable);
- }
-
- //! Inject, or graft, a faux recursive PML4 into the active page mapping structure.
- auto inject_faux_pml4(frame_allocator & allocator, page_mapper & mapper)
- {
- using namespace x86_64;
- using entry_flags = page_table::entry::flags;
-
- auto page = page::containing(unused_page_address);
-
- auto temporary_mapper = scoped_mapping{page, mapper};
- auto new_pml4_frame = allocator.allocate();
-
- auto pml4 = std::construct_at(temporary_mapper.map_as<page_table>(*new_pml4_frame, entry_flags::writable));
- (*pml4)[recursive_page_map_index].frame(new_pml4_frame.value(), entry_flags::present | entry_flags::writable);
-
- auto pml4_index = pml_index<4>(page);
- auto old_pml4 = paging_root::get();
- auto pml4_entry = (*old_pml4)[pml4_index];
-
- auto pml3_index = pml_index<3>(page);
- auto old_pml3 = old_pml4->next(pml4_index);
- auto pml3_entry = (**old_pml3)[pml3_index];
-
- auto pml2_index = pml_index<2>(page);
- auto old_pml2 = (**old_pml3).next(pml3_index);
- auto pml2_entry = (**old_pml2)[pml2_index];
-
- auto pml1_index = pml_index<1>(page);
- auto old_pml1 = (**old_pml2).next(pml2_index);
- auto pml1_entry = (**old_pml1)[pml1_index];
-
- (*paging_root::get())[recursive_page_map_index].frame(new_pml4_frame.value(),
- entry_flags::present | entry_flags::writable);
-
- tlb_flush_all();
-
- auto new_pml4 = paging_root::get();
- (*new_pml4)[pml4_index] = pml4_entry;
-
- auto new_pml3 = new_pml4->next(pml4_index);
- (**new_pml3)[pml3_index] = pml3_entry;
-
- auto new_pml2 = (**new_pml3).next(pml3_index);
- (**new_pml2)[pml2_index] = pml2_entry;
-
- auto new_pml1 = (**new_pml2).next(pml2_index);
- (**new_pml1)[pml1_index] = pml1_entry;
-
- return *new_pml4_frame;
- }
-
- auto remap_kernel(page_mapper & mapper) -> void
- {
- auto kernel_mapper = x86_64::kernel_mapper{boot::bootstrap_information.mbi};
- kernel_mapper.remap_kernel(mapper);
- }
-
- auto remap_vga_text_mode_buffer(page_mapper & mapper) -> void
- {
- constexpr auto vga_base = std::uintptr_t{0xb8000};
- auto vga_physical_start = physical_address{vga_base};
- auto vga_virtual_start = linear_address{vga_base + std::bit_cast<std::uintptr_t>(&boot::x86_64::TEACHOS_VMA)};
-
- auto page = page::containing(vga_virtual_start);
- auto frame = frame::containing(vga_physical_start);
-
- mapper.map(page, frame, page_mapper::flags::writable);
- }
-
- auto remap_multiboot_information(page_mapper & mapper) -> void
- {
- auto mbi_base = std::bit_cast<std::uintptr_t>(boot::bootstrap_information.mbi);
- auto mbi_size = boot::bootstrap_information.mbi->size_bytes();
- auto mbi_physical_start = physical_address{mbi_base & ~std::bit_cast<std::uintptr_t>(&boot::x86_64::TEACHOS_VMA)};
- auto mbi_virtual_start = linear_address{mbi_base};
- auto mbi_block_count = (mbi_size + PLATFORM_FRAME_SIZE - 1) / PLATFORM_FRAME_SIZE;
-
- for (auto i = 0uz; i < mbi_block_count; ++i)
- {
- auto page = page::containing(mbi_virtual_start) + i;
- auto frame = frame::containing(mbi_physical_start) + i;
- mapper.map(page, frame, page_mapper::flags::empty);
- }
- }
-
- auto constinit region_based_allocator = std::optional<x86_64::region_allocator>{};
- auto constinit buffered_allocator = std::optional<x86_64::buffered_allocator<4>>{};
- auto constinit recursive_page_mapper = std::optional<x86_64::recursive_page_mapper>{};
-
- } // namespace
-
- auto init() -> void
- {
- auto static constinit is_initialized = std::atomic_flag{};
-
- if (is_initialized.test_and_set())
- {
- system::panic("[x86_64] Memory management has already been initialized.");
- }
-
- kstd::println("[x86_64:MEM] Enabling additional CPU protection features.");
-
- enable_cpu_protections();
-
- region_based_allocator.emplace(collect_memory_information());
- buffered_allocator.emplace(&*region_based_allocator);
- recursive_page_mapper.emplace(*buffered_allocator);
-
- kstd::println("[x86_64:MEM] Preparing new paging hierarchy.");
-
- auto new_pml4_frame = inject_faux_pml4(*buffered_allocator, *recursive_page_mapper);
-
- remap_kernel(*recursive_page_mapper);
- remap_vga_text_mode_buffer(*recursive_page_mapper);
- remap_multiboot_information(*recursive_page_mapper);
-
- kstd::println("[x86_64:MEM] Switching to new paging hierarchy.");
-
- auto cr3 = cpu::x86_64::cr3::read();
- cr3.frame(new_pml4_frame);
- cpu::x86_64::cr3::write(cr3);
-
- set_frame_allocator(*buffered_allocator);
- set_page_mapper(*recursive_page_mapper);
- }
-
-} // namespace teachos::memory
diff --git a/arch/x86_64/src/memory/kernel_mapper.cpp b/arch/x86_64/src/memory/kernel_mapper.cpp
index 89b2e83..08c32c5 100644
--- a/arch/x86_64/src/memory/kernel_mapper.cpp
+++ b/arch/x86_64/src/memory/kernel_mapper.cpp
@@ -1,9 +1,9 @@
-#include "x86_64/memory/kernel_mapper.hpp"
+#include "arch/memory/kernel_mapper.hpp"
#include "kapi/memory.hpp"
#include "kapi/system.hpp"
-#include "x86_64/boot/ld.hpp"
+#include "arch/boot/ld.hpp"
#include <kstd/print>
@@ -19,7 +19,7 @@
#include <string_view>
#include <utility>
-namespace teachos::memory::x86_64
+namespace arch::memory
{
namespace
@@ -39,15 +39,15 @@ namespace teachos::memory::x86_64
kernel_mapper::kernel_mapper(multiboot2::information_view const * mbi)
: m_mbi{std::move(mbi)}
- , m_kernel_load_base{std::bit_cast<std::uintptr_t>(&boot::x86_64::TEACHOS_VMA)}
+ , m_kernel_load_base{std::bit_cast<std::uintptr_t>(&arch::boot::TEACHOS_VMA)}
{}
- auto kernel_mapper::remap_kernel(page_mapper & mapper) -> void
+ auto kernel_mapper::remap_kernel(kapi::memory::page_mapper & mapper) -> void
{
auto elf_information = m_mbi->maybe_elf_symbols<elf::format::elf64>();
if (!elf_information)
{
- system::panic("[x86_64:MEM] ELF section information is not available.");
+ kapi::system::panic("[x86_64:MEM] ELF section information is not available.");
}
auto sections = *elf_information;
@@ -61,38 +61,38 @@ namespace teachos::memory::x86_64
if (allocated_sections.empty())
{
- system::panic("[x86_64:MEM] No allocated ELF sections were found.");
+ kapi::system::panic("[x86_64:MEM] No allocated ELF sections were found.");
}
std::ranges::for_each(allocated_sections,
[&](auto const & section) -> auto { map_section(section, sections.name(section), mapper); });
}
- auto kernel_mapper::map_section(section_header_type const & section, std::string_view name, page_mapper & mapper)
- -> void
+ auto kernel_mapper::map_section(section_header_type const & section, std::string_view name,
+ kapi::memory::page_mapper & mapper) -> void
{
auto number_of_pages = (section.size + (PLATFORM_PAGE_SIZE - 1)) / PLATFORM_PAGE_SIZE;
- auto linear_start_address = linear_address{section.virtual_load_address};
- auto physical_start_address = physical_address{section.virtual_load_address & ~m_kernel_load_base};
+ auto linear_start_address = kapi::memory::linear_address{section.virtual_load_address};
+ auto physical_start_address = kapi::memory::physical_address{section.virtual_load_address & ~m_kernel_load_base};
kstd::println("[x86_64:MEM] mapping {}"
"\n {} bytes -> page count: {}"
"\n {} @ {}",
name, section.size, number_of_pages, linear_start_address, physical_start_address);
- auto first_page = page::containing(linear_start_address);
- auto first_frame = frame::containing(physical_start_address);
+ auto first_page = kapi::memory::page::containing(linear_start_address);
+ auto first_frame = kapi::memory::frame::containing(physical_start_address);
- auto page_flags = page_mapper::flags::empty;
+ auto page_flags = kapi::memory::page_mapper::flags::empty;
if (section.writable())
{
- page_flags |= page_mapper::flags::writable;
+ page_flags |= kapi::memory::page_mapper::flags::writable;
}
if (section.executable())
{
- page_flags |= page_mapper::flags::executable;
+ page_flags |= kapi::memory::page_mapper::flags::executable;
}
auto is_prefix_of_name = [=](auto prefix) -> bool {
@@ -101,7 +101,7 @@ namespace teachos::memory::x86_64
if (!std::ranges::any_of(user_accessible_prefixes, is_prefix_of_name))
{
- page_flags |= page_mapper::flags::supervisor_only;
+ page_flags |= kapi::memory::page_mapper::flags::supervisor_only;
}
for (auto i = 0uz; i < number_of_pages; ++i)
@@ -110,4 +110,4 @@ namespace teachos::memory::x86_64
}
}
-} // namespace teachos::memory::x86_64 \ No newline at end of file
+} // namespace arch::memory \ No newline at end of file
diff --git a/arch/x86_64/src/memory/mmu.cpp b/arch/x86_64/src/memory/mmu.cpp
index e15d94e..ea23278 100644
--- a/arch/x86_64/src/memory/mmu.cpp
+++ b/arch/x86_64/src/memory/mmu.cpp
@@ -1,14 +1,12 @@
-#include "x86_64/memory/mmu.hpp"
+#include "arch/memory/mmu.hpp"
#include "kapi/memory.hpp"
-#include "x86_64/cpu/registers.hpp"
+#include "arch/cpu/registers.hpp"
-namespace teachos::memory::x86_64
+namespace arch::memory
{
- namespace cpu = cpu::x86_64;
-
- auto tlb_flush(linear_address address) -> void
+ auto tlb_flush(kapi::memory::linear_address address) -> void
{
asm volatile("invlpg (%[input])" : /* no output from call */ : [input] "r"(address) : "memory");
}
@@ -18,4 +16,4 @@ namespace teachos::memory::x86_64
auto paging_root = cpu::cr3::read();
cpu::cr3::write(paging_root);
}
-} // namespace teachos::memory::x86_64
+} // namespace arch::memory
diff --git a/arch/x86_64/src/memory/page_table.cpp b/arch/x86_64/src/memory/page_table.cpp
index 2de099d..26cdd29 100644
--- a/arch/x86_64/src/memory/page_table.cpp
+++ b/arch/x86_64/src/memory/page_table.cpp
@@ -1,4 +1,4 @@
-#include "x86_64/memory/page_table.hpp"
+#include "arch/memory/page_table.hpp"
#include "kapi/memory.hpp"
@@ -9,7 +9,7 @@
#include <optional>
#include <utility>
-namespace teachos::memory::x86_64
+namespace arch::memory
{
auto page_table::entry::clear() noexcept -> void
@@ -44,16 +44,16 @@ namespace teachos::memory::x86_64
return *this;
}
- auto page_table::entry::frame() const noexcept -> std::optional<struct frame>
+ auto page_table::entry::frame() const noexcept -> std::optional<kapi::memory::frame>
{
if (present())
{
- return frame::containing(physical_address{m_raw & frame_number_mask});
+ return kapi::memory::frame::containing(kapi::memory::physical_address{m_raw & frame_number_mask});
}
return std::nullopt;
}
- auto page_table::entry::frame(struct frame frame, flags flags) noexcept -> void
+ auto page_table::entry::frame(kapi::memory::frame frame, flags flags) noexcept -> void
{
m_raw = (frame.start_address().raw() | static_cast<std::uint64_t>(flags));
};
@@ -79,4 +79,4 @@ namespace teachos::memory::x86_64
[](auto const & entry) -> auto { return entry.all_flags() == entry::flags::empty; });
}
-} // namespace teachos::memory::x86_64
+} // namespace arch::memory
diff --git a/arch/x86_64/src/memory/paging_root.cpp b/arch/x86_64/src/memory/paging_root.cpp
index d849a82..41f40ed 100644
--- a/arch/x86_64/src/memory/paging_root.cpp
+++ b/arch/x86_64/src/memory/paging_root.cpp
@@ -1,9 +1,9 @@
-#include "x86_64/memory/paging_root.hpp"
+#include "arch/memory/paging_root.hpp"
#include <bit>
#include <cstdint>
-namespace teachos::memory::x86_64
+namespace arch::memory
{
namespace
@@ -16,4 +16,4 @@ namespace teachos::memory::x86_64
return std::bit_cast<paging_root *>(recursive_base);
}
-} // namespace teachos::memory::x86_64 \ No newline at end of file
+} // namespace arch::memory \ No newline at end of file
diff --git a/arch/x86_64/src/memory/recursive_page_mapper.cpp b/arch/x86_64/src/memory/recursive_page_mapper.cpp
index c5aabcb..d8273e1 100644
--- a/arch/x86_64/src/memory/recursive_page_mapper.cpp
+++ b/arch/x86_64/src/memory/recursive_page_mapper.cpp
@@ -1,16 +1,16 @@
-#include "x86_64/memory/recursive_page_mapper.hpp"
+#include "arch/memory/recursive_page_mapper.hpp"
#include "kapi/memory.hpp"
#include "kapi/system.hpp"
-#include "x86_64/memory/page_table.hpp"
-#include "x86_64/memory/page_utilities.hpp"
-#include "x86_64/memory/paging_root.hpp"
+#include "arch/memory/page_table.hpp"
+#include "arch/memory/page_utilities.hpp"
+#include "arch/memory/paging_root.hpp"
#include <cstddef>
#include <optional>
-namespace teachos::memory::x86_64
+namespace arch::memory
{
namespace
{
@@ -22,7 +22,8 @@ namespace teachos::memory::x86_64
//! added, thus still enforcing non-writability and non-execution of the affected page.
template<std::size_t Level>
requires(Level > 1uz && Level <= PLATFORM_PAGING_LEVELS)
- auto do_map(recursive_page_table<Level> * pml, page page, frame_allocator & allocator, page_mapper::flags flags)
+ auto do_map(recursive_page_table<Level> * pml, kapi::memory::page page, kapi::memory::frame_allocator & allocator,
+ kapi::memory::page_mapper::flags flags)
{
auto index = pml_index<Level>(page);
auto entry_flags = to_table_flags(flags);
@@ -41,12 +42,13 @@ namespace teachos::memory::x86_64
}
//! Perform the actual PML1 update.
- auto do_map(page_table * pml, page page, frame frame, page_mapper::flags flags) -> std::optional<std::byte *>
+ auto do_map(page_table * pml, kapi::memory::page page, kapi::memory::frame frame,
+ kapi::memory::page_mapper::flags flags) -> std::optional<std::byte *>
{
auto index = pml_index<1>(page);
if ((*pml)[index].present())
{
- system::panic("[x86_64:MEM] Tried to map a page that is already mapped");
+ kapi::system::panic("[x86_64:MEM] Tried to map a page that is already mapped");
}
(*pml)[index].frame(frame, page_table::entry::flags::present | to_table_flags(flags));
return std::optional{static_cast<std::byte *>(page.start_address())};
@@ -54,11 +56,11 @@ namespace teachos::memory::x86_64
} // namespace
- recursive_page_mapper::recursive_page_mapper(frame_allocator & allocator)
+ recursive_page_mapper::recursive_page_mapper(kapi::memory::frame_allocator & allocator)
: m_allocator{&allocator}
{}
- auto recursive_page_mapper::map(page page, frame frame, flags flags) -> std::byte *
+ auto recursive_page_mapper::map(kapi::memory::page page, kapi::memory::frame frame, flags flags) -> std::byte *
{
auto pml4 = static_cast<recursive_page_table<4> *>((paging_root::get()));
@@ -70,15 +72,15 @@ namespace teachos::memory::x86_64
.value_or(nullptr);
}
- auto recursive_page_mapper::unmap(page page) -> void
+ auto recursive_page_mapper::unmap(kapi::memory::page page) -> void
{
if (!try_unmap(page))
{
- system::panic("[x86_64:MEM] Tried to unmap a page that was not mapped.");
+ kapi::system::panic("[x86_64:MEM] Tried to unmap a page that was not mapped.");
}
}
- auto recursive_page_mapper::try_unmap(page page) noexcept -> bool
+ auto recursive_page_mapper::try_unmap(kapi::memory::page page) noexcept -> bool
{
if (!paging_root::get()->translate(page))
{
@@ -116,4 +118,4 @@ namespace teachos::memory::x86_64
return true;
}
-} // namespace teachos::memory::x86_64 \ No newline at end of file
+} // namespace arch::memory \ No newline at end of file
diff --git a/arch/x86_64/src/memory/region_allocator.cpp b/arch/x86_64/src/memory/region_allocator.cpp
index 7a8fb8b..a2dfd48 100644
--- a/arch/x86_64/src/memory/region_allocator.cpp
+++ b/arch/x86_64/src/memory/region_allocator.cpp
@@ -1,4 +1,4 @@
-#include "x86_64/memory/region_allocator.hpp"
+#include "arch/memory/region_allocator.hpp"
#include "kapi/memory/address.hpp"
#include "kapi/memory/frame.hpp"
@@ -11,16 +11,17 @@
#include <ranges>
#include <utility>
-namespace teachos::memory::x86_64
+namespace arch::memory
{
namespace
{
constexpr auto last_frame(multiboot2::memory_map::region const & region)
{
- return frame::containing(physical_address{region.base + region.size_in_B - 1});
+ return kapi::memory::frame::containing(kapi::memory::physical_address{region.base + region.size_in_B - 1});
}
- constexpr auto falls_within(frame const & candidate, frame const & start, frame const & end)
+ constexpr auto falls_within(kapi::memory::frame const & candidate, kapi::memory::frame const & start,
+ kapi::memory::frame const & end)
{
return candidate >= start && candidate <= end;
}
@@ -30,10 +31,10 @@ namespace teachos::memory::x86_64
: m_next_frame{}
, m_current_region{}
, m_memory_map{mem_info.memory_map}
- , m_kernel_start(frame::containing(mem_info.image_range.first))
- , m_kernel_end(frame::containing(mem_info.image_range.second))
- , m_multiboot_start(frame::containing(mem_info.mbi_range.first))
- , m_multiboot_end(frame::containing(mem_info.mbi_range.second))
+ , m_kernel_start{kapi::memory::frame::containing(mem_info.image_range.first)}
+ , m_kernel_end{kapi::memory::frame::containing(mem_info.image_range.second)}
+ , m_multiboot_start{kapi::memory::frame::containing(mem_info.mbi_range.first)}
+ , m_multiboot_end{kapi::memory::frame::containing(mem_info.mbi_range.second)}
{
choose_next_region();
}
@@ -56,14 +57,14 @@ namespace teachos::memory::x86_64
}
m_current_region = *lowest_region;
- if (auto start_of_region = frame::containing(physical_address{m_current_region->base});
+ if (auto start_of_region = kapi::memory::frame::containing(kapi::memory::physical_address{m_current_region->base});
start_of_region > m_next_frame)
{
m_next_frame = start_of_region;
}
}
- auto region_allocator::find_next_frame() -> std::optional<frame>
+ auto region_allocator::find_next_frame() -> std::optional<kapi::memory::frame>
{
if (!m_current_region || m_next_frame > last_frame(*m_current_region))
{
@@ -97,7 +98,8 @@ namespace teachos::memory::x86_64
return m_current_region.transform([this](auto) -> auto { return m_next_frame; });
}
- auto region_allocator::allocate_many(std::size_t count) noexcept -> std::optional<std::pair<frame, std::size_t>>
+ auto region_allocator::allocate_many(std::size_t count) noexcept
+ -> std::optional<std::pair<kapi::memory::frame, std::size_t>>
{
while (m_current_region)
{
@@ -122,11 +124,11 @@ namespace teachos::memory::x86_64
return std::nullopt;
}
- auto region_allocator::release_many(std::pair<frame, std::size_t>) -> void {}
+ auto region_allocator::release_many(std::pair<kapi::memory::frame, std::size_t>) -> void {}
- auto region_allocator::next_free_frame() noexcept -> std::optional<frame>
+ auto region_allocator::next_free_frame() noexcept -> std::optional<kapi::memory::frame>
{
return find_next_frame();
}
-} // namespace teachos::memory::x86_64
+} // namespace arch::memory
diff --git a/arch/x86_64/src/memory/scoped_mapping.cpp b/arch/x86_64/src/memory/scoped_mapping.cpp
index 945183d..dde1dda 100644
--- a/arch/x86_64/src/memory/scoped_mapping.cpp
+++ b/arch/x86_64/src/memory/scoped_mapping.cpp
@@ -1,32 +1,32 @@
-#include "x86_64/memory/scoped_mapping.hpp"
+#include "arch/memory/scoped_mapping.hpp"
#include "kapi/memory.hpp"
#include "kapi/system.hpp"
-#include "x86_64/memory/mmu.hpp"
-#include "x86_64/memory/page_table.hpp"
-#include "x86_64/memory/paging_root.hpp"
+#include "arch/memory/mmu.hpp"
+#include "arch/memory/page_table.hpp"
+#include "arch/memory/paging_root.hpp"
#include <cstddef>
#include <utility>
-namespace teachos::memory::x86_64
+namespace arch::memory
{
scoped_mapping::scoped_mapping(scoped_mapping && other) noexcept
- : m_page{std::exchange(other.m_page, page{})}
+ : m_page{std::exchange(other.m_page, kapi::memory::page{})}
, m_mapper{std::exchange(other.m_mapper, nullptr)}
, m_mapped{std::exchange(other.m_mapped, false)}
{}
- scoped_mapping::scoped_mapping(page page, page_mapper & mapper)
+ scoped_mapping::scoped_mapping(kapi::memory::page page, kapi::memory::page_mapper & mapper)
: m_page{page}
, m_mapper{&mapper}
, m_mapped{false}
{
if (paging_root::get()->translate(page))
{
- system::panic("[MEM] Tried to map a page that is already mapped!");
+ kapi::system::panic("[MEM] Tried to map a page that is already mapped!");
}
}
@@ -35,7 +35,7 @@ namespace teachos::memory::x86_64
if (m_mapped)
{
unmap();
- x86_64::tlb_flush(m_page.start_address());
+ tlb_flush(m_page.start_address());
}
}
@@ -45,7 +45,7 @@ namespace teachos::memory::x86_64
return *this;
}
- auto scoped_mapping::map(frame frame, page_table::entry::flags flags) -> std::byte *
+ auto scoped_mapping::map(kapi::memory::frame frame, page_table::entry::flags flags) -> std::byte *
{
auto result = m_mapper->map(m_page, frame, to_mapper_flags(flags));
m_mapped = true;
@@ -66,4 +66,4 @@ namespace teachos::memory::x86_64
swap(lhs.m_mapped, rhs.m_mapped);
}
-} // namespace teachos::memory::x86_64 \ No newline at end of file
+} // namespace arch::memory \ No newline at end of file
diff --git a/arch/x86_64/src/vga/text/buffer.cpp b/arch/x86_64/src/vga/text/buffer.cpp
index 2dcf084..7112573 100644
--- a/arch/x86_64/src/vga/text/buffer.cpp
+++ b/arch/x86_64/src/vga/text/buffer.cpp
@@ -1,6 +1,6 @@
-#include "x86_64/vga/text/buffer.hpp"
+#include "arch/vga/text/buffer.hpp"
-#include "x86_64/vga/text/attribute.hpp"
+#include "arch/vga/text/attribute.hpp"
#include <algorithm>
#include <bit>
@@ -9,7 +9,7 @@
#include <string_view>
#include <utility>
-namespace teachos::vga::x86_64::text
+namespace arch::vga::text
{
buffer::buffer(std::size_t width, std::size_t height, cell * start, std::size_t position)
: m_width{width}
@@ -98,4 +98,4 @@ namespace teachos::vga::x86_64::text
m_buffer[m_position++] = std::pair{code_point, std::bit_cast<std::byte>(attribute)};
}
-} // namespace teachos::vga::x86_64::text
+} // namespace arch::vga::text
diff --git a/arch/x86_64/src/vga/text/device.cpp b/arch/x86_64/src/vga/text/device.cpp
index 2da9e06..dcacd8c 100644
--- a/arch/x86_64/src/vga/text/device.cpp
+++ b/arch/x86_64/src/vga/text/device.cpp
@@ -1,16 +1,16 @@
#include "kapi/cio.hpp"
-#include "x86_64/boot/boot.hpp"
-#include "x86_64/boot/ld.hpp"
-#include "x86_64/vga/crtc.hpp"
-#include "x86_64/vga/text.hpp"
+#include "arch/boot/boot.hpp"
+#include "arch/boot/ld.hpp"
+#include "arch/vga/crtc.hpp"
+#include "arch/vga/text.hpp"
#include <bit>
#include <cstddef>
#include <cstdint>
#include <string_view>
-namespace teachos::vga::x86_64::text
+namespace arch::vga::text
{
namespace
{
@@ -22,10 +22,10 @@ namespace teachos::vga::x86_64::text
} // namespace
device::device()
- : m_buffer{default_buffer_width, default_buffer_height,
- std::bit_cast<buffer::cell *>(default_buffer_address +
- std::bit_cast<std::uintptr_t>(&teachos::boot::x86_64::TEACHOS_VMA)),
- boot::bootstrap_information.vga_buffer_index}
+ : m_buffer{
+ default_buffer_width, default_buffer_height,
+ std::bit_cast<buffer::cell *>(default_buffer_address + std::bit_cast<std::uintptr_t>(&boot::TEACHOS_VMA)),
+ kapi::boot::bootstrap_informatio