From 4d76f412b071a05f0aae1d1307f2b58cb2778569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sat, 2 Nov 2024 14:08:55 +0000 Subject: Attempt to fix crashes --- .../arch/memory/allocator/tiny_frame_allocator.hpp | 19 +++++- .../arch/memory/paging/active_page_table.hpp | 2 +- .../include/arch/memory/paging/kernel_mapper.hpp | 30 ++++----- .../include/arch/memory/paging/temporary_page.hpp | 7 +- arch/x86_64/src/kernel/main.cpp | 76 +++++++++++----------- .../src/memory/allocator/tiny_frame_allocator.cpp | 4 +- 6 files changed, 75 insertions(+), 63 deletions(-) (limited to 'arch') diff --git a/arch/x86_64/include/arch/memory/allocator/tiny_frame_allocator.hpp b/arch/x86_64/include/arch/memory/allocator/tiny_frame_allocator.hpp index e55b376..b8ed506 100644 --- a/arch/x86_64/include/arch/memory/allocator/tiny_frame_allocator.hpp +++ b/arch/x86_64/include/arch/memory/allocator/tiny_frame_allocator.hpp @@ -9,6 +9,11 @@ namespace teachos::arch::memory::allocator { + namespace + { + uint8_t constexpr TINY_ALLOCATOR_FRAMES_COUNT = 3U; + } + /** * @brief Allocates memory using memory areas read from the multiboot2 information pointer. */ @@ -23,9 +28,16 @@ namespace teachos::arch::memory::allocator * entries *when a new page table is required. */ tiny_frame_allocator(area_frame_allocator & allocator) - : frames{allocator.allocate_frame(), allocator.allocate_frame(), allocator.allocate_frame()} + : frames{} { - // Nothing to do + for (auto & frame : frames) + { + auto allocated = allocator.allocate_frame(); + if (allocated.has_value()) + { + frame.emplace(allocated.value()); + } + } } /** @@ -47,7 +59,8 @@ namespace teachos::arch::memory::allocator auto deallocate_frame(physical_frame physical_frame) -> void; private: - std::array, 3U> frames = {}; + std::array, TINY_ALLOCATOR_FRAMES_COUNT> frames = + {}; ///< Container that holds the frames allocated by another allocator. }; } // namespace teachos::arch::memory::allocator diff --git a/arch/x86_64/include/arch/memory/paging/active_page_table.hpp b/arch/x86_64/include/arch/memory/paging/active_page_table.hpp index 71f70b5..5140c3d 100644 --- a/arch/x86_64/include/arch/memory/paging/active_page_table.hpp +++ b/arch/x86_64/include/arch/memory/paging/active_page_table.hpp @@ -128,7 +128,7 @@ namespace teachos::arch::memory::paging "[Page Mapper] Attempted to unmap page, which has not been mapped previously"); auto current_handle = active_handle; - std::array handles{current_handle, current_handle, current_handle, current_handle}; + std::array handles{current_handle, current_handle, current_handle}; for (auto level = page_table_handle::LEVEL4; level != page_table_handle::LEVEL1; --level) { diff --git a/arch/x86_64/include/arch/memory/paging/kernel_mapper.hpp b/arch/x86_64/include/arch/memory/paging/kernel_mapper.hpp index 5ee4ea8..a7ae3aa 100644 --- a/arch/x86_64/include/arch/memory/paging/kernel_mapper.hpp +++ b/arch/x86_64/include/arch/memory/paging/kernel_mapper.hpp @@ -16,21 +16,16 @@ namespace teachos::arch::memory::paging /** * @brief Kernel mapper that allows to remap the kernel elf sections in C++. - * - * @tparam T Contract the allocator that should be used to allocate frames for the remapping process has to fulfill. */ - template struct kernel_mapper { /** * @brief Constructor. * - * @param allocator Allocator that should be used to allocate frames for the remapping process. * @param mem_info Information about elf kernel sections required for remapping process. */ - kernel_mapper(T & allocator, multiboot::memory_information const & mem_info) - : allocator(allocator) - , mem_info(mem_info) + kernel_mapper(multiboot::memory_information const & mem_info) + : mem_info(mem_info) { // Nothing to do } @@ -42,16 +37,22 @@ namespace teachos::arch::memory::paging * @note We have to use a workaround with an * inactive page table, that is not used by the CPU to ensure we are not changign memory that we are using. Because * remapping active kernel memory in the kernel wouldn't work. + * + * @tparam T Contract the allocator that should be used to allocate frames for the remapping process has to fulfill. + * + * @param allocator Allocator that should be used to allocate frames for the remapping process. */ - auto remap_kernel() -> void + template + auto remap_kernel(T & allocator) -> void { - temporary_page temp_page{virtual_page{UNUSED_VIRTUAL_ADDRESS}, allocator}; + virtual_page temporary_address{UNUSED_VIRTUAL_ADDRESS}; + temporary_page temporary_page{temporary_address, allocator}; auto & active_table = active_page_table::create_or_get(); auto const frame = allocator.allocate_frame(); exception_handling::assert(frame.has_value(), "[Kernel Mapper] Frame could not be allocated and therefore kernel not mapped"); - auto const inactive_table = inactive_page_table{frame.value(), active_table, temp_page}; - remap_elf_kernel_sections(inactive_table, temp_page, active_table); + inactive_page_table inactive_table{frame.value(), active_table, temporary_page}; + remap_elf_kernel_sections(inactive_table, temporary_page, active_table); } private: @@ -79,7 +80,7 @@ namespace teachos::arch::memory::paging active_table.active_handle[511].set_entry(inactive_table.page_table_level_4_frame, entry::PRESENT | entry::WRITABLE); tlb_flush_all(); - map_elf_kernel_sections(active_table); + map_elf_kernel_sections(temporary_page, active_table); page_table_level4[511].set_entry(backup, entry::PRESENT | entry::WRITABLE); tlb_flush_all(); @@ -93,7 +94,7 @@ namespace teachos::arch::memory::paging * @param active_table Active level 4 page table that should be used to map the required elf sections into entries. * Has had its recursive mapping temporarily replaced and points to unmapped place in memory. */ - auto map_elf_kernel_sections(active_page_table & active_table) -> void + auto map_elf_kernel_sections(temporary_page & temporary_page, active_page_table & active_table) -> void { for (auto const & section : mem_info.sections) { @@ -114,12 +115,11 @@ namespace teachos::arch::memory::paging for (auto const & frame : frames) { // TODO: Use actual elf section flags, convert from one to the other flag type. - active_table.identity_map(allocator, frame, entry::WRITABLE); + active_table.identity_map(temporary_page.allocator, frame, entry::WRITABLE); } } } - T & allocator; ///< Allocator that should be used to allocate frames for the mapping process. multiboot::memory_information const & mem_info; ///< Information about elf kernel sections required for remapping process. }; diff --git a/arch/x86_64/include/arch/memory/paging/temporary_page.hpp b/arch/x86_64/include/arch/memory/paging/temporary_page.hpp index 0cadc29..6b3bcb6 100644 --- a/arch/x86_64/include/arch/memory/paging/temporary_page.hpp +++ b/arch/x86_64/include/arch/memory/paging/temporary_page.hpp @@ -51,6 +51,10 @@ namespace teachos::arch::memory::paging */ auto map_table_frame(allocator::physical_frame frame, active_page_table & active_table) -> page_table_handle; + // TODO: Better way to do this? + virtual_page page; + allocator::tiny_frame_allocator allocator; + private: /** * @brief Map the temporary page to a frame. @@ -60,9 +64,6 @@ namespace teachos::arch::memory::paging * @return The virtual address of the page. */ auto map_to_frame(allocator::physical_frame frame, active_page_table & active_table) -> virtual_address; - - virtual_page page; - allocator::tiny_frame_allocator allocator; }; } // namespace teachos::arch::memory::paging diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index 59e453a..5cfd9b3 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -20,44 +20,42 @@ namespace teachos::arch::kernel auto const memory_information = memory::multiboot::read_multiboot2(); memory::allocator::area_frame_allocator allocator(memory_information); - size_t address = 42 * memory::paging::PAGE_TABLE_ENTRY_COUNT * memory::paging::PAGE_TABLE_ENTRY_COUNT * - memory::allocator::PAGE_FRAME_SIZE; // 42th P3 entry - auto const page = memory::paging::virtual_page::containing_address(address); - - auto & active_table = memory::paging::active_page_table::create_or_get(); - memory::paging::temporary_page temp_page{page, allocator}; - temp_page.zero_entries(active_table); - - memory::paging::kernel_mapper kernel(allocator, memory_information); - kernel.remap_kernel(); - - // memory::paging::map_next_free_page_to_frame(allocator, page, 0U); - // auto optional_frame = memory::paging::translate_page(page); - // video::vga::text::newline(); - // video::vga::text::write("Mapped physical frame: ", video::vga::text::common_attributes::green_on_black); - // video::vga::text::write_number(optional_frame.value().frame_number, - // video::vga::text::common_attributes::green_on_black); - // video::vga::text::write(" to virtual page: ", video::vga::text::common_attributes::green_on_black); - // video::vga::text::write_number(page.page_number, video::vga::text::common_attributes::green_on_black); - - // memory::paging::unmap_page(allocator, page); - // video::vga::text::newline(); - // video::vga::text::write("Unapped virtual page from physical frame: ", - // video::vga::text::common_attributes::green_on_black); - // optional_frame = memory::paging::translate_page(page); - // exception_handling::assert(!optional_frame.has_value(), "[Main] Ummapping failed"); - // video::vga::text::write_number(page.page_number, video::vga::text::common_attributes::green_on_black); - - auto last_allocated = allocator.allocate_frame(); - auto allocated = last_allocated; - do - { - last_allocated = allocated; - allocated = allocator.allocate_frame(); - } while (allocated); - video::vga::text::newline(); - video::vga::text::write("Allocated Frames: ", video::vga::text::common_attributes::green_on_black); - video::vga::text::write_number(last_allocated.value().frame_number, - video::vga::text::common_attributes::green_on_black); + memory::paging::kernel_mapper kernel(memory_information); + kernel.remap_kernel(allocator); + video::vga::text::write("Kernel remapping successfull", video::vga::text::common_attributes::green_on_black); + + /* + size_t address = 42 * memory::paging::PAGE_TABLE_ENTRY_COUNT * memory::paging::PAGE_TABLE_ENTRY_COUNT * + memory::allocator::PAGE_FRAME_SIZE; // 42th P3 entry + auto const page = memory::paging::virtual_page::containing_address(address); + memory::paging::map_next_free_page_to_frame(allocator, page, 0U); + auto optional_frame = memory::paging::translate_page(page); + video::vga::text::newline(); + video::vga::text::write("Mapped physical frame: ", video::vga::text::common_attributes::green_on_black); + video::vga::text::write_number(optional_frame.value().frame_number, + video::vga::text::common_attributes::green_on_black); + video::vga::text::write(" to virtual page: ", video::vga::text::common_attributes::green_on_black); + video::vga::text::write_number(page.page_number, video::vga::text::common_attributes::green_on_black); + + memory::paging::unmap_page(allocator, page); + video::vga::text::newline(); + video::vga::text::write("Unapped virtual page from physical frame: ", + video::vga::text::common_attributes::green_on_black); + optional_frame = memory::paging::translate_page(page); + exception_handling::assert(!optional_frame.has_value(), "[Main] Ummapping failed"); + video::vga::text::write_number(page.page_number, video::vga::text::common_attributes::green_on_black); + + auto last_allocated = allocator.allocate_frame(); + auto allocated = last_allocated; + do + { + last_allocated = allocated; + allocated = allocator.allocate_frame(); + } while (allocated); + video::vga::text::newline(); + video::vga::text::write("Allocated Frames: ", video::vga::text::common_attributes::green_on_black); + video::vga::text::write_number(last_allocated.value().frame_number, + video::vga::text::common_attributes::green_on_black); + */ } } // namespace teachos::arch::kernel diff --git a/arch/x86_64/src/memory/allocator/tiny_frame_allocator.cpp b/arch/x86_64/src/memory/allocator/tiny_frame_allocator.cpp index d07398a..b9fd2c8 100644 --- a/arch/x86_64/src/memory/allocator/tiny_frame_allocator.cpp +++ b/arch/x86_64/src/memory/allocator/tiny_frame_allocator.cpp @@ -11,7 +11,7 @@ namespace teachos::arch::memory::allocator if (frame_option.has_value()) { auto value = frame_option; - frame_option = std::nullopt; + frame_option.reset(); return value; } } @@ -24,7 +24,7 @@ namespace teachos::arch::memory::allocator { if (!frame_option.has_value()) { - frame_option = physical_frame; + frame_option.emplace(physical_frame); return; } } -- cgit v1.2.3