aboutsummaryrefslogtreecommitdiff
path: root/arch
diff options
context:
space:
mode:
Diffstat (limited to 'arch')
-rw-r--r--arch/x86_64/CMakeLists.txt2
-rw-r--r--arch/x86_64/include/arch/memory/heap/allocator.hpp32
-rw-r--r--arch/x86_64/include/arch/memory/heap/concept.hpp15
-rw-r--r--arch/x86_64/include/arch/memory/main.hpp13
-rw-r--r--arch/x86_64/include/arch/memory/paging/active_page_table.hpp2
-rw-r--r--arch/x86_64/scripts/kernel.ld5
-rw-r--r--arch/x86_64/src/kernel/main.cpp68
-rw-r--r--arch/x86_64/src/memory/heap/allocator.cpp27
-rw-r--r--arch/x86_64/src/memory/main.cpp47
9 files changed, 149 insertions, 62 deletions
diff --git a/arch/x86_64/CMakeLists.txt b/arch/x86_64/CMakeLists.txt
index 1444054..5b0e3a9 100644
--- a/arch/x86_64/CMakeLists.txt
+++ b/arch/x86_64/CMakeLists.txt
@@ -41,6 +41,7 @@ target_sources("_video" PRIVATE
#]============================================================================]
target_sources("_memory" PRIVATE
+ "src/memory/main.cpp"
"src/memory/multiboot/elf_symbols_section.cpp"
"src/memory/multiboot/reader.cpp"
"src/memory/allocator/area_frame_allocator.cpp"
@@ -55,6 +56,7 @@ target_sources("_memory" PRIVATE
"src/memory/cpu/tlb.cpp"
"src/memory/cpu/control_register.cpp"
"src/memory/cpu/msr.cpp"
+ "src/memory/heap/allocator.cpp"
)
#[============================================================================[
diff --git a/arch/x86_64/include/arch/memory/heap/allocator.hpp b/arch/x86_64/include/arch/memory/heap/allocator.hpp
new file mode 100644
index 0000000..c486a4c
--- /dev/null
+++ b/arch/x86_64/include/arch/memory/heap/allocator.hpp
@@ -0,0 +1,32 @@
+#ifndef TEACHOS_ARCH_X86_64_MEMORY_HEAP_ALLOCATOR_HPP
+#define TEACHOS_ARCH_X86_64_MEMORY_HEAP_ALLOCATOR_HPP
+
+#include <cstdint>
+
+namespace teachos::arch::memory::heap
+{
+ std::size_t constexpr HEAP_START = 0x4000'0000;
+ std::size_t constexpr HEAP_SIZE = 100 * 1024;
+
+ struct bump_allocator
+ {
+ bump_allocator(std::size_t heap_start, std::size_t heap_end)
+ : heap_start{heap_start}
+ , heap_end{heap_end}
+ , next{heap_start}
+ {
+ }
+
+ auto allocate(std::size_t size) -> void *;
+
+ auto deallocate(uint8_t * pointer, std::size_t size) -> void;
+
+ private:
+ std::size_t heap_start;
+ std::size_t heap_end;
+ std::size_t next;
+ };
+
+} // namespace teachos::arch::memory::heap
+
+#endif \ No newline at end of file
diff --git a/arch/x86_64/include/arch/memory/heap/concept.hpp b/arch/x86_64/include/arch/memory/heap/concept.hpp
new file mode 100644
index 0000000..a525b0b
--- /dev/null
+++ b/arch/x86_64/include/arch/memory/heap/concept.hpp
@@ -0,0 +1,15 @@
+#ifndef TEACHOS_ARCH_X86_64_MEMORY_HEAP_CONCEPT_HPP
+#define TEACHOS_ARCH_X86_64_MEMORY_HEAP_CONCEPT_HPP
+
+#include <cstdint>
+
+namespace teachos::arch::memory::heap
+{
+ template<typename T>
+ concept HeapAllocator = requires(T t, uint8_t * pointer, std::size_t size) {
+ { t.allocate(size) } -> std::same_as<uint8_t *>;
+ { t.deallocate(pointer, size) } -> std::same_as<void>;
+ };
+} // namespace teachos::arch::memory::heap
+
+#endif // TEACHOS_ARCH_X86_64_MEMORY_HEAP_CONCEPT_HPP \ No newline at end of file
diff --git a/arch/x86_64/include/arch/memory/main.hpp b/arch/x86_64/include/arch/memory/main.hpp
new file mode 100644
index 0000000..e166285
--- /dev/null
+++ b/arch/x86_64/include/arch/memory/main.hpp
@@ -0,0 +1,13 @@
+#ifndef TEACHOS_ARCH_X86_64_MEMORY_MAIN_HPP
+#define TEACHOS_ARCH_X86_64_MEMORY_MAIN_HPP
+
+namespace teachos::arch::memory
+{
+ /**
+ * @brief
+ *
+ */
+ auto initialize_memory_management() -> void;
+} // namespace teachos::arch::memory
+
+#endif
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 4687209..1b2aaed 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
@@ -98,7 +98,7 @@ namespace teachos::arch::memory::paging
* @see map_page_to_frame
*/
template<allocator::FrameAllocator T>
- auto map_next_free_page_to_frame(T & allocator, virtual_page page, std::bitset<64U> flags) -> void
+ auto map_page_to_next_free_frame(T & allocator, virtual_page page, std::bitset<64U> flags) -> void
{
auto const frame = allocator.allocate_frame();
exception_handling::assert(frame.has_value(), "[Page mapper] Out of memory exception");
diff --git a/arch/x86_64/scripts/kernel.ld b/arch/x86_64/scripts/kernel.ld
index e9d245f..c3eea9c 100644
--- a/arch/x86_64/scripts/kernel.ld
+++ b/arch/x86_64/scripts/kernel.ld
@@ -60,9 +60,8 @@ SECTIONS
}
/***************************************************************************
- * Now it is time to load the 64-bit kernel code. We virtually load it into
- * the upper 2GiB, while adjusting the linear load address appropriately. We
- * also make sure to align the loaded data onto a page boundary.
+ * Now it is time to load the 64-bit kernel code. We
+ * make sure to align the loaded data onto a page boundary.
***************************************************************************/
.init ALIGN(4K) : AT(ADDR (.init))
{
diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp
index 8a73630..cbd6e68 100644
--- a/arch/x86_64/src/kernel/main.cpp
+++ b/arch/x86_64/src/kernel/main.cpp
@@ -1,16 +1,8 @@
#include "arch/kernel/main.hpp"
-#include "arch/exception_handling/assert.hpp"
-#include "arch/memory/allocator/area_frame_allocator.hpp"
-#include "arch/memory/cpu/control_register.hpp"
-#include "arch/memory/cpu/msr.hpp"
-#include "arch/memory/multiboot/reader.hpp"
-#include "arch/memory/paging/kernel_mapper.hpp"
-#include "arch/memory/paging/temporary_page.hpp"
+#include "arch/memory/main.hpp"
#include "arch/video/vga/text.hpp"
-#include <algorithm>
-
namespace teachos::arch::kernel
{
auto main() -> void
@@ -19,61 +11,21 @@ namespace teachos::arch::kernel
video::vga::text::cursor(false);
video::vga::text::write("TeachOS is starting up...", video::vga::text::common_attributes::green_on_black);
- auto const memory_information = memory::multiboot::read_multiboot2();
- memory::allocator::area_frame_allocator allocator(memory_information);
+ memory::initialize_memory_management();
+
+ // heap::bump_allocator heap_allocator{memory::heap::HEAP_START, heap::HEAP_START + memory::heap::HEAP_SIZE};
+ // auto test = heap_allocator.allocate(1024);
- memory::cpu::set_cr2_bit(memory::cpu::cr2_flags::WRITE_PROTECT);
- memory::cpu::set_efer_bit(memory::cpu::efer_flags::NXE);
+ // auto test2 = new (test) multiboot::memory_information{};
- memory::paging::kernel_mapper kernel(allocator, memory_information);
- auto & active_table = kernel.remap_kernel();
- auto x = active_table.active_handle.next_table(0);
- auto y = x.value().next_table(0);
- auto z = y.value().next_table(0);
- if (z.has_value())
- {
- video::vga::text::write("Kernel remapping successfull", video::vga::text::common_attributes::green_on_black);
- }
+ // if (test || test2)
+ // {
+ // video::vga::text::write("Kernel remapping successfull", video::vga::text::common_attributes::green_on_black);
+ // }
// TODO: Why is identity mapping multiboot2 information structure with new kernel not required and
// allocator.allocate_frame still works?
// TODO: Fix unmapping old level 4 page table and turn it into guard page, use Stack Probes for stack allocation if
// possible.
-
- // TODO: Map heap virtual pages with active table
-
- /*
- 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/heap/allocator.cpp b/arch/x86_64/src/memory/heap/allocator.cpp
new file mode 100644
index 0000000..c9ddd78
--- /dev/null
+++ b/arch/x86_64/src/memory/heap/allocator.cpp
@@ -0,0 +1,27 @@
+#include "arch/memory/heap/allocator.hpp"
+
+#include "arch/exception_handling/assert.hpp"
+
+namespace teachos::arch::memory::heap
+{
+ auto bump_allocator::allocate(std::size_t size) -> void *
+ {
+ // Uses some sort of alignment orignally:
+ // https://github.com/phil-opp/blog_os/blob/7f6576c9dc34e360b81236c54c25c7827fd6a2df/src/memory/heap_allocator.rs#L24
+ auto alloc_start = next;
+ auto alloc_end = next + size;
+
+ arch::exception_handling::assert(alloc_end <= heap_end, "[Heap Allocator] Out of memory!");
+
+ next = alloc_end;
+ return reinterpret_cast<void *>(alloc_start);
+ }
+
+ auto bump_allocator::deallocate(uint8_t * pointer, std::size_t size) -> void
+ {
+ // Memory leak
+ if (pointer || size)
+ {
+ }
+ }
+} // namespace teachos::arch::memory::heap \ No newline at end of file
diff --git a/arch/x86_64/src/memory/main.cpp b/arch/x86_64/src/memory/main.cpp
new file mode 100644
index 0000000..ce07115
--- /dev/null
+++ b/arch/x86_64/src/memory/main.cpp
@@ -0,0 +1,47 @@
+#include "arch/memory/main.hpp"
+
+#include "arch/exception_handling/assert.hpp"
+#include "arch/memory/allocator/area_frame_allocator.hpp"
+#include "arch/memory/cpu/control_register.hpp"
+#include "arch/memory/cpu/msr.hpp"
+#include "arch/memory/heap/allocator.hpp"
+#include "arch/memory/paging/active_page_table.hpp"
+#include "arch/memory/paging/kernel_mapper.hpp"
+
+namespace teachos::arch::memory
+{
+ namespace
+ {
+ auto remap_heap(allocator::area_frame_allocator allocator, paging::active_page_table & active_table) -> void
+ {
+ auto heap_start_page = paging::virtual_page::containing_address(memory::heap::HEAP_START);
+ auto heap_end_page =
+ paging::virtual_page::containing_address(memory::heap::HEAP_START + memory::heap::HEAP_SIZE - 1);
+
+ for (auto i = heap_start_page.page_number; i <= heap_end_page.page_number; i++)
+ {
+ active_table.map_page_to_next_free_frame(allocator, paging::virtual_page{i}, paging::entry::WRITABLE);
+ }
+ }
+ } // namespace
+
+ auto initialize_memory_management() -> void
+ {
+ static bool has_been_called = false;
+ arch::exception_handling::assert(!has_been_called,
+ "[Initialization] Memory management has already been initialized");
+ has_been_called = true;
+
+ auto const memory_information = multiboot::read_multiboot2();
+ allocator::area_frame_allocator allocator(memory_information);
+
+ cpu::set_cr2_bit(memory::cpu::cr2_flags::WRITE_PROTECT);
+ cpu::set_efer_bit(memory::cpu::efer_flags::NXE);
+
+ paging::kernel_mapper kernel(allocator, memory_information);
+ auto & active_table = kernel.remap_kernel();
+ video::vga::text::write("Kernel remapping successfull", video::vga::text::common_attributes::green_on_black);
+
+ remap_heap(allocator, active_table);
+ }
+} // namespace teachos::arch::memory \ No newline at end of file