#include "x86_64/memory/kernel_mapper.hpp" #include "kapi/memory.hpp" #include "kapi/system.hpp" #include "x86_64/boot/ld.hpp" #include #include #include #include #include #include #include #include #include #include #include namespace teachos::memory::x86_64 { namespace { using namespace std::string_view_literals; constexpr auto static ignored_section_prefixes = std::array{ ".boot_"sv, }; constexpr auto static user_accessible_prefixes = std::array{ ".user"sv, ".stl"sv, }; } // namespace kernel_mapper::kernel_mapper(multiboot2::information_view const * mbi) : m_mbi{std::move(mbi)} , m_kernel_load_base{std::bit_cast(&boot::x86_64::TEACHOS_VMA)} {} auto kernel_mapper::remap_kernel(page_mapper & mapper) -> void { auto elf_information = m_mbi->maybe_elf_symbols(); if (!elf_information) { system::panic("[x86_64:MEM] ELF section information is not available."); } auto sections = *elf_information; auto allocated_sections = std::views::all(sections) | std::views::filter(&elf::section_header::allocated) | std::views::filter([&](auto const & section) -> auto { auto name = sections.name(section); return !std::ranges::any_of(ignored_section_prefixes, [&](auto const & prefix) -> auto { return name.starts_with(prefix); }); }); if (allocated_sections.empty()) { 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 { kstd::println("[x86_64:MEM] mapping {} ({} bytes)", name, section.size); 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 first_page = page::containing(linear_start_address); auto first_frame = frame::containing(physical_start_address); auto page_flags = page_mapper::flags::empty; if (section.writable()) { page_flags |= page_mapper::flags::writable; } if (section.executable()) { page_flags |= page_mapper::flags::executable; } auto is_prefix_of_name = [=](auto prefix) -> bool { return name.starts_with(prefix); }; if (!std::ranges::any_of(user_accessible_prefixes, is_prefix_of_name)) { page_flags |= page_mapper::flags::supervisor_only; } for (auto i = 0uz; i < number_of_pages; ++i) { mapper.map(first_page + i, first_frame + i, page_flags); } } } // namespace teachos::memory::x86_64