From 0b62fdb3fe657d056a42d7567b67951ba3468738 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Thu, 8 May 2025 08:51:52 +0000 Subject: wip allocating heap memory in user mode --- .../arch/memory/heap/global_heap_allocator.hpp | 7 +++- .../arch/memory/heap/user_heap_allocator.hpp | 48 ++++++++++++++++++++++ .../include/arch/memory/paging/kernel_mapper.hpp | 2 +- 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 arch/x86_64/include/arch/memory/heap/user_heap_allocator.hpp (limited to 'arch/x86_64/include') diff --git a/arch/x86_64/include/arch/memory/heap/global_heap_allocator.hpp b/arch/x86_64/include/arch/memory/heap/global_heap_allocator.hpp index bfc7b67..d0d1080 100644 --- a/arch/x86_64/include/arch/memory/heap/global_heap_allocator.hpp +++ b/arch/x86_64/include/arch/memory/heap/global_heap_allocator.hpp @@ -2,6 +2,7 @@ #define TEACHOS_ARCH_X86_64_MEMORY_HEAP_GLOBAL_HEAP_ALLOCATOR_HPP #include "arch/memory/heap/heap_allocator.hpp" +#include "arch/memory/heap/user_heap_allocator.hpp" namespace teachos::arch::memory::heap { @@ -76,7 +77,8 @@ namespace teachos::arch::memory::heap private: static heap_allocator * kernel_allocator_instance; ///< Instance used to allocate and deallocate kernel heap memory - static heap_allocator * user_allocator_instance; ///< Instance used to allocate and deallocate user heap memory + static user_heap_allocator * + user_allocator_instance; ///< Instance used to allocate and deallocate user heap memory /** * @brief Either returns the previously registered heap allocated or halts further execution @@ -90,7 +92,8 @@ namespace teachos::arch::memory::heap * * @return Reference to the registered user heap allocation */ - static auto user() -> heap_allocator &; + [[gnu::section(".user_text")]] + static auto user() -> user_heap_allocator &; }; } // namespace teachos::arch::memory::heap diff --git a/arch/x86_64/include/arch/memory/heap/user_heap_allocator.hpp b/arch/x86_64/include/arch/memory/heap/user_heap_allocator.hpp new file mode 100644 index 0000000..1dab047 --- /dev/null +++ b/arch/x86_64/include/arch/memory/heap/user_heap_allocator.hpp @@ -0,0 +1,48 @@ +#ifndef TEACHOS_ARCH_X86_64_MEMORY_HEAP_USER_HEAP_ALLOCATOR_HPP +#define TEACHOS_ARCH_X86_64_MEMORY_HEAP_USER_HEAP_ALLOCATOR_HPP + +#include "arch/memory/heap/heap_allocator.hpp" + +#include +#include + +namespace teachos::arch::memory::heap +{ + /** + * @brief Simple heap allocator, which allocates linearly and leaks all allocated memory, because it does not really + * deallocate anything. + */ + struct user_heap_allocator + { + /** + * @brief Constructor. + * + * @param heap_start Start of the allocatable heap area + * @param heap_end End of the allocatable heap area (Start + Size) + */ + user_heap_allocator(std::size_t heap_start, std::size_t heap_end) + : heap_start{heap_start} + , heap_end{heap_end} + , next{heap_start} + { + // Nothing to do + } + + auto allocate(std::size_t size) -> void *; + + /** + * @copybrief heap_allocator::deallocate + * + * @note Simply does nothing, because this allocator leaks all memory + */ + auto deallocate(void * pointer) noexcept -> void; + + private: + std::size_t heap_start; ///< Start of the allocatable heap area + std::size_t heap_end; ///< End of the allocatable heap area + std::atomic_uint64_t next; ///< Current address, which is the start of still unused allocatable heap area + }; + +} // namespace teachos::arch::memory::heap + +#endif // TEACHOS_ARCH_X86_64_MEMORY_HEAP_USER_HEAP_ALLOCATOR_HPP 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 ce74b98..6459107 100644 --- a/arch/x86_64/include/arch/memory/paging/kernel_mapper.hpp +++ b/arch/x86_64/include/arch/memory/paging/kernel_mapper.hpp @@ -143,7 +143,7 @@ namespace teachos::arch::memory::paging // statically allocated variables), .boot_rodata (Contains constant data stored in ROM) // Not required: .text, .rodata, .ctors, .dtors, .bss, .data, .boot_data, .boot_text, .init if (section.physical_address == 0x100000 || section.physical_address == 0x102000 || - section.physical_address == 0x216000) + section.physical_address == 0x217000) { entry.set_user_accesible(); } -- cgit v1.2.3