aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabian Imhof <fabian.imhof@ost.ch>2025-05-11 09:29:56 +0000
committerFabian Imhof <fabian.imhof@ost.ch>2025-05-11 09:29:56 +0000
commitef156dd6430855434b54275b22cd43ee3cedcfdc (patch)
treef3517844fe334cc1f0608184dc1372ce5a197f67
parent833cd6446d9981a262959749c0e248e33b54c174 (diff)
downloadteachos-ef156dd6430855434b54275b22cd43ee3cedcfdc.tar.xz
teachos-ef156dd6430855434b54275b22cd43ee3cedcfdc.zip
make frame_allocator and active_page_table statically available
-rw-r--r--arch/x86_64/include/arch/memory/allocator/area_frame_allocator.hpp4
-rw-r--r--arch/x86_64/include/arch/memory/main.hpp14
-rw-r--r--arch/x86_64/include/arch/memory/paging/kernel_mapper.hpp3
-rw-r--r--arch/x86_64/src/context_switching/syscall/syscall_handler.cpp6
-rw-r--r--arch/x86_64/src/memory/heap/memory_block.cpp1
-rw-r--r--arch/x86_64/src/memory/main.cpp59
6 files changed, 60 insertions, 27 deletions
diff --git a/arch/x86_64/include/arch/memory/allocator/area_frame_allocator.hpp b/arch/x86_64/include/arch/memory/allocator/area_frame_allocator.hpp
index 2244613..6cb5f56 100644
--- a/arch/x86_64/include/arch/memory/allocator/area_frame_allocator.hpp
+++ b/arch/x86_64/include/arch/memory/allocator/area_frame_allocator.hpp
@@ -15,9 +15,9 @@ namespace teachos::arch::memory::allocator
struct area_frame_allocator
{
/**
- * @brief Constructor
+ * @brief Constructor.
*
- * @param mem_info Structure containg all relevant information to map and allocate memory
+ * @param mem_info Structure containg all relevant information to map and allocate memory.
*/
area_frame_allocator(multiboot::memory_information const & mem_info);
diff --git a/arch/x86_64/include/arch/memory/main.hpp b/arch/x86_64/include/arch/memory/main.hpp
index 164abbc..d51815f 100644
--- a/arch/x86_64/include/arch/memory/main.hpp
+++ b/arch/x86_64/include/arch/memory/main.hpp
@@ -1,8 +1,22 @@
#ifndef TEACHOS_ARCH_X86_64_MEMORY_MAIN_HPP
#define TEACHOS_ARCH_X86_64_MEMORY_MAIN_HPP
+#include "arch/memory/paging/page_entry.hpp"
+
+#include <cstdint>
+
namespace teachos::arch::memory
{
+
+ /**
+ * @brief Maps a heap section to a page.
+ *
+ * @param heap_start Start-address of the heap.
+ * @param heap_size Size of the heap.
+ * @param additional_flags Additional flags to apply to the page entry.
+ */
+ auto remap_heap(std::size_t heap_start, std::size_t heap_size, paging::entry::bitset additional_flags) -> void;
+
/**
* @brief Initializes memory management.
*
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 7321548..756eeb1 100644
--- a/arch/x86_64/include/arch/memory/paging/kernel_mapper.hpp
+++ b/arch/x86_64/include/arch/memory/paging/kernel_mapper.hpp
@@ -38,7 +38,7 @@ namespace teachos::arch::memory::paging
* 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.
*/
- auto remap_kernel() -> active_page_table &
+ auto remap_kernel() -> void
{
// Set Page Global Enable bit
auto cr4 = kernel::cpu::read_control_register(kernel::cpu::control_register::CR4);
@@ -57,7 +57,6 @@ namespace teachos::arch::memory::paging
auto const old_level_4_page =
virtual_page::containing_address(old_table.page_table_level_4_frame.start_address());
active_table.unmap_page(allocator, old_level_4_page);
- return active_table;
}
private:
diff --git a/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp b/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp
index b88f273..9ca03d9 100644
--- a/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp
+++ b/arch/x86_64/src/context_switching/syscall/syscall_handler.cpp
@@ -2,6 +2,8 @@
#include "arch/context_switching/syscall/main.hpp"
#include "arch/exception_handling/panic.hpp"
+#include "arch/memory/heap/global_heap_allocator.hpp"
+#include "arch/memory/main.hpp"
#include "arch/video/vga/text.hpp"
namespace teachos::arch::context_switching::syscall
@@ -19,6 +21,10 @@ namespace teachos::arch::context_switching::syscall
auto expand_user_heap() -> error
{
+ // TODO: use actual addresses instead of this constant!
+ memory::remap_heap(memory::heap::USER_HEAP_SIZE + memory::heap::USER_HEAP_SIZE, memory::heap::USER_HEAP_SIZE,
+ memory::paging::entry::USER_ACCESSIBLE);
+
arguments args{};
asm volatile("mov %[input], %%rdi"
: /* no output from call */
diff --git a/arch/x86_64/src/memory/heap/memory_block.cpp b/arch/x86_64/src/memory/heap/memory_block.cpp
index 446cd96..9f1fea8 100644
--- a/arch/x86_64/src/memory/heap/memory_block.cpp
+++ b/arch/x86_64/src/memory/heap/memory_block.cpp
@@ -6,6 +6,7 @@ namespace teachos::arch::memory::heap
{
memory_block::memory_block(std::size_t size, memory_block * next)
{
+ // TODO: Figure out why this memset fails
memset(static_cast<void *>(this), 0, size);
this->size = size;
this->next = next;
diff --git a/arch/x86_64/src/memory/main.cpp b/arch/x86_64/src/memory/main.cpp
index 4cdfa80..595cb0d 100644
--- a/arch/x86_64/src/memory/main.cpp
+++ b/arch/x86_64/src/memory/main.cpp
@@ -9,37 +9,47 @@
#include "arch/memory/paging/active_page_table.hpp"
#include "arch/memory/paging/kernel_mapper.hpp"
+#include <optional>
+
namespace teachos::arch::memory
{
namespace
{
- template<allocator::FrameAllocator T>
- auto create_frame_allocator(multiboot::memory_information const & memory_information) -> T
+ static std::optional<allocator::area_frame_allocator> frame_allocator;
+
+ auto create_frame_allocator(multiboot::memory_information const & memory_information)
+ -> allocator::area_frame_allocator
{
- return allocator::area_frame_allocator{memory_information};
+ frame_allocator.emplace(memory_information);
+ return frame_allocator.value();
}
- template<allocator::FrameAllocator T>
- auto remap_heap(T & allocator, paging::active_page_table & active_table, bool is_user_heap = false) -> void
+ auto get_frame_allocator() -> allocator::area_frame_allocator
{
- auto const heap_start = is_user_heap ? heap::USER_HEAP_START : heap::KERNEL_HEAP_START;
- auto const heap_size = is_user_heap ? heap::USER_HEAP_SIZE : heap::KERNEL_HEAP_SIZE;
+ exception_handling::assert(frame_allocator.has_value(), "[Memory main] Frame allocator has not been created yet");
+ return frame_allocator.value();
+ }
+ } // namespace
+
+ auto remap_heap(std::size_t heap_start, std::size_t heap_size, paging::entry::bitset additional_flags = {}) -> void
+ {
+ auto allocator = get_frame_allocator();
+ decltype(auto) active_table = paging::active_page_table::create_or_get();
+ auto const start_page = paging::virtual_page::containing_address(heap_start);
+ auto const end_page = ++(paging::virtual_page::containing_address(heap_start + heap_size - 1));
- auto const start_page = paging::virtual_page::containing_address(heap_start);
- auto const end_page = ++(paging::virtual_page::containing_address(heap_start + heap_size - 1));
- paging::page_container::iterator const begin{start_page};
- paging::page_container::iterator const end{end_page};
- paging::page_container const pages{begin, end};
+ paging::page_container::iterator const begin{start_page};
+ paging::page_container::iterator const end{end_page};
+ paging::page_container const pages{begin, end};
- constexpr auto base_flags = paging::entry::WRITABLE;
- auto const flags = is_user_heap ? base_flags | paging::entry::USER_ACCESSIBLE : base_flags;
+ constexpr auto base_flags = paging::entry::WRITABLE;
+ auto const flags = base_flags | additional_flags;
- for (auto const & page : pages)
- {
- active_table.map_page_to_next_free_frame(allocator, page, flags);
- }
+ for (auto const & page : pages)
+ {
+ active_table.map_page_to_next_free_frame(allocator, page, flags);
}
- } // namespace
+ }
auto initialize_memory_management() -> void
{
@@ -49,18 +59,21 @@ namespace teachos::arch::memory
has_been_called = true;
auto const memory_information = multiboot::read_multiboot2();
- auto allocator = create_frame_allocator<allocator::area_frame_allocator>(memory_information);
+ auto allocator = create_frame_allocator(memory_information);
kernel::cpu::set_cr0_bit(kernel::cpu::cr0_flags::WRITE_PROTECT);
kernel::cpu::set_efer_bit(kernel::cpu::efer_flags::NXE);
paging::kernel_mapper kernel(allocator, memory_information);
- auto & active_table = kernel.remap_kernel();
+ kernel.remap_kernel();
video::vga::text::write("Kernel remapping successful", video::vga::text::common_attributes::green_on_black);
video::vga::text::newline();
- remap_heap(allocator, active_table); // Remap kernel heap
- remap_heap(allocator, active_table, true); // Remap user heap
+ // Remap kernel heap
+ remap_heap(heap::KERNEL_HEAP_START, heap::KERNEL_HEAP_SIZE);
+ // Remap user heap
+ remap_heap(heap::USER_HEAP_SIZE, heap::USER_HEAP_SIZE, paging::entry::USER_ACCESSIBLE);
+
video::vga::text::write("Heap remapping successful", video::vga::text::common_attributes::green_on_black);
video::vga::text::newline();
}