aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src/memory
diff options
context:
space:
mode:
authorMatteo Gmür <matteo.gmuer1@ost.ch>2025-05-18 14:45:05 +0000
committerMatteo Gmür <matteo.gmuer1@ost.ch>2025-05-18 14:46:17 +0000
commit8d39f3f67734bf39cada370c39243e6ef33bf4a0 (patch)
tree50493df40018c2ea9587280ac8d7bc3fb42a4990 /arch/x86_64/src/memory
parent1b5a771a34743a2973a82de5ebdfd22da030b841 (diff)
downloadteachos-8d39f3f67734bf39cada370c39243e6ef33bf4a0.tar.xz
teachos-8d39f3f67734bf39cada370c39243e6ef33bf4a0.zip
Make new usable for both kernel and user calls
Diffstat (limited to 'arch/x86_64/src/memory')
-rw-r--r--arch/x86_64/src/memory/heap/global_heap_allocator.cpp66
1 files changed, 51 insertions, 15 deletions
diff --git a/arch/x86_64/src/memory/heap/global_heap_allocator.cpp b/arch/x86_64/src/memory/heap/global_heap_allocator.cpp
index e6c268a..35cd623 100644
--- a/arch/x86_64/src/memory/heap/global_heap_allocator.cpp
+++ b/arch/x86_64/src/memory/heap/global_heap_allocator.cpp
@@ -1,12 +1,29 @@
#include "arch/memory/heap/global_heap_allocator.hpp"
+#include "arch/context_switching/syscall/main.hpp"
#include "arch/exception_handling/assert.hpp"
+#include "arch/kernel/cpu/segment_register.hpp"
#include "arch/memory/heap/bump_allocator.hpp"
#include "arch/memory/heap/linked_list_allocator.hpp"
#include "arch/memory/heap/user_heap_allocator.hpp"
namespace teachos::arch::memory::heap
{
+ namespace
+ {
+ constexpr char NOT_REGISTRED_ERROR_MESSAGE[] =
+ "Attempted to allocate or deallocate using the global_heap_allocator before "
+ "register_heap_allocation_type was called.";
+ constexpr uint16_t KERNEL_CODE_INDEX = 1U;
+
+ [[gnu::section(".user_text")]]
+ auto os_in_kernel_mode() -> bool
+ {
+ auto const cs = teachos::arch::kernel::cpu::read_code_segment_register();
+ return cs.get_index() == KERNEL_CODE_INDEX;
+ }
+ } // namespace
+
heap_allocator * global_heap_allocator::kernel_allocator_instance = nullptr;
user_heap_allocator * global_heap_allocator::user_allocator_instance = nullptr;
@@ -47,53 +64,72 @@ namespace teachos::arch::memory::heap
auto global_heap_allocator::kernel() -> heap_allocator &
{
- exception_handling::assert(kernel_allocator_instance != nullptr,
- "Attempted to allocate or deallocate using the global_heap_allocator before "
- "register_heap_allocation_type was called.");
+ exception_handling::assert(kernel_allocator_instance != nullptr, NOT_REGISTRED_ERROR_MESSAGE);
return *kernel_allocator_instance;
}
auto global_heap_allocator::user() -> user_heap_allocator &
{
- // TODO: Assert Method does not exist in .user_text meaning this causes a Page Fault when accessed, Make it into a
- // syscall instead
- // exception_handling::assert(user_allocator_instance != nullptr,
- // "Attempted to allocate or deallocate using the global_heap_allocator before "
- // "register_heap_allocation_type was called.");
-
+ context_switching::syscall::syscall(
+ context_switching::syscall::type::ASSERT,
+ {user_allocator_instance != nullptr, reinterpret_cast<uint64_t>(&NOT_REGISTRED_ERROR_MESSAGE)});
return *user_allocator_instance;
}
} // namespace teachos::arch::memory::heap
auto operator new(std::size_t size) -> void *
{
- return teachos::arch::memory::heap::global_heap_allocator::kmalloc(size);
+ if (teachos::arch::memory::heap::os_in_kernel_mode())
+ {
+ return teachos::arch::memory::heap::global_heap_allocator::kmalloc(size);
+ }
+ return teachos::arch::memory::heap::global_heap_allocator::malloc(size);
}
auto operator delete(void * pointer) noexcept -> void
{
- teachos::arch::memory::heap::global_heap_allocator::kfree(pointer);
+ if (teachos::arch::memory::heap::os_in_kernel_mode())
+ {
+ teachos::arch::memory::heap::global_heap_allocator::kfree(pointer);
+ }
+ teachos::arch::memory::heap::global_heap_allocator::free(pointer);
}
auto operator delete(void * pointer, std::size_t size) noexcept -> void
{
(void)size;
- teachos::arch::memory::heap::global_heap_allocator::kfree(pointer);
+ if (teachos::arch::memory::heap::os_in_kernel_mode())
+ {
+ teachos::arch::memory::heap::global_heap_allocator::kfree(pointer);
+ }
+ teachos::arch::memory::heap::global_heap_allocator::free(pointer);
}
auto operator new[](std::size_t size) -> void *
{
- return teachos::arch::memory::heap::global_heap_allocator::kmalloc(size);
+ if (teachos::arch::memory::heap::os_in_kernel_mode())
+ {
+ return teachos::arch::memory::heap::global_heap_allocator::kmalloc(size);
+ }
+ return teachos::arch::memory::heap::global_heap_allocator::malloc(size);
}
auto operator delete[](void * pointer) noexcept -> void
{
- teachos::arch::memory::heap::global_heap_allocator::kfree(pointer);
+ if (teachos::arch::memory::heap::os_in_kernel_mode())
+ {
+ teachos::arch::memory::heap::global_heap_allocator::kfree(pointer);
+ }
+ teachos::arch::memory::heap::global_heap_allocator::free(pointer);
}
auto operator delete[](void * pointer, std::size_t size) noexcept -> void
{
(void)size;
- teachos::arch::memory::heap::global_heap_allocator::kfree(pointer);
+ if (teachos::arch::memory::heap::os_in_kernel_mode())
+ {
+ teachos::arch::memory::heap::global_heap_allocator::kfree(pointer);
+ }
+ teachos::arch::memory::heap::global_heap_allocator::free(pointer);
}