diff --git a/kernel/include/memory/KernelMemoryManager.h b/kernel/include/memory/MemoryManager.h similarity index 95% rename from kernel/include/memory/KernelMemoryManager.h rename to kernel/include/memory/MemoryManager.h index eb843a38..c16ac43f 100644 --- a/kernel/include/memory/KernelMemoryManager.h +++ b/kernel/include/memory/MemoryManager.h @@ -5,7 +5,7 @@ #define MAP_USER 1 << 1 #define MAP_EXEC 1 << 2 -namespace KernelMemoryManager +namespace MemoryManager { void* get_mapping(void* physicalAddress, int flags = MAP_READ_WRITE); void release_mapping(void* mapping); diff --git a/kernel/src/acpi/RSDT.cpp b/kernel/src/acpi/RSDT.cpp index 3c2c336d..9fc02b87 100644 --- a/kernel/src/acpi/RSDT.cpp +++ b/kernel/src/acpi/RSDT.cpp @@ -3,7 +3,7 @@ #include "acpi/RSDT.h" #include "bootboot.h" #include "log/Log.h" -#include "memory/KernelMemoryManager.h" +#include "memory/MemoryManager.h" #include "std/stdio.h" #include "std/string.h" @@ -17,14 +17,14 @@ ACPI::SDTHeader* ACPI::GetRSDTOrXSDT() void* physical = (void*)bootboot.arch.x86_64.acpi_ptr; uint64_t offset = (uint64_t)physical % 4096; kdbgln("RSDT/XSDT physical address: %lx", (uint64_t)physical); - cache = KernelMemoryManager::get_unaligned_mapping(physical); + cache = MemoryManager::get_unaligned_mapping(physical); uint64_t numPages = 1; while ((offset + ((SDTHeader*)cache)->Length) > (numPages * 4096)) { kwarnln("RSDT/XSDT extends beyond the mapped page, mapping one more page"); - KernelMemoryManager::release_unaligned_mappings(cache, numPages); + MemoryManager::release_unaligned_mappings(cache, numPages); numPages++; - cache = KernelMemoryManager::get_unaligned_mappings(cache, numPages); + cache = MemoryManager::get_unaligned_mappings(cache, numPages); } kdbgln("Mapped RSDT/XSDT to virtual address %lx, uses %ld pages", (uint64_t)cache, numPages); SDTHeader* result = (SDTHeader*)cache; @@ -86,12 +86,12 @@ void* ACPI::FindTable(ACPI::SDTHeader* rootSDT, const char* signature) continue; } kdbgln("Physical address of entry: %lx", (uint64_t)h); - SDTHeader* realHeader = (SDTHeader*)KernelMemoryManager::get_unaligned_mapping(h); + SDTHeader* realHeader = (SDTHeader*)MemoryManager::get_unaligned_mapping(h); kdbgln("Mapped entry to virtual address %lx", (uint64_t)realHeader); if (!ValidateSDTHeader(realHeader)) { kwarnln("Header of entry %d is not valid, skipping this entry", i); - KernelMemoryManager::release_unaligned_mapping(realHeader); + MemoryManager::release_unaligned_mapping(realHeader); continue; } char tableSignature[5]; @@ -104,7 +104,7 @@ void* ACPI::FindTable(ACPI::SDTHeader* rootSDT, const char* signature) return (void*)realHeader; } kdbgln("Signatures do not match, unmapping entry and continuing"); - KernelMemoryManager::release_unaligned_mapping(realHeader); + MemoryManager::release_unaligned_mapping(realHeader); } return NULL; diff --git a/kernel/src/gdt/GDT.cpp b/kernel/src/gdt/GDT.cpp index 3b7447d8..fa6acbfa 100644 --- a/kernel/src/gdt/GDT.cpp +++ b/kernel/src/gdt/GDT.cpp @@ -3,7 +3,7 @@ #include "gdt/GDT.h" #include "assert.h" #include "log/Log.h" -#include "memory/KernelMemoryManager.h" +#include "memory/MemoryManager.h" #include "std/string.h" #include @@ -84,8 +84,7 @@ void GDT::load() gdtr.offset = (uint64_t)&internal_gdt; gdtr.size = sizeof(InternalGDT); memset(&main_tss, 0, sizeof(TSS)); - main_tss.rsp[0] = - (uint64_t)KernelMemoryManager::get_pages(4) + (4096 * 4) - 8; // allocate 16KB for the syscall stack + main_tss.rsp[0] = (uint64_t)MemoryManager::get_pages(4) + (4096 * 4) - 8; // allocate 16KB for the syscall stack set_base(&internal_gdt.tss, (uint64_t)&main_tss & 0xffffffff); internal_gdt.tss2.base_high = (uint64_t)&main_tss >> 32; set_limit(&internal_gdt.tss, sizeof(TSS) - 1); diff --git a/kernel/src/init/Init.cpp b/kernel/src/init/Init.cpp index 7809d7e1..c5dd9052 100644 --- a/kernel/src/init/Init.cpp +++ b/kernel/src/init/Init.cpp @@ -6,7 +6,7 @@ #include "interrupts/Interrupts.h" #include "io/Serial.h" #include "log/Log.h" -#include "memory/KernelMemoryManager.h" +#include "memory/MemoryManager.h" #include "memory/PMM.h" #include "memory/VMM.h" #include "misc/hang.h" diff --git a/kernel/src/init/InitRD.cpp b/kernel/src/init/InitRD.cpp index c9ce492f..3b53925b 100644 --- a/kernel/src/init/InitRD.cpp +++ b/kernel/src/init/InitRD.cpp @@ -4,7 +4,7 @@ #include "bootboot.h" #include "io/Serial.h" #include "log/Log.h" -#include "memory/KernelMemoryManager.h" +#include "memory/MemoryManager.h" #include "std/stdlib.h" #include @@ -107,8 +107,7 @@ void InitRD::for_each(void (*callback)(File& f)) void InitRD::init() { - initrd_base = - KernelMemoryManager::get_unaligned_mappings((void*)bootboot.initrd_ptr, bootboot.initrd_size / 4096 + 1); + initrd_base = MemoryManager::get_unaligned_mappings((void*)bootboot.initrd_ptr, bootboot.initrd_size / 4096 + 1); kdbgln("physical base at %lx, size %lx, mapped to %lx", bootboot.initrd_ptr, bootboot.initrd_size, (uint64_t)initrd_base); } \ No newline at end of file diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index a7621407..8dbde7f6 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -14,8 +14,8 @@ #include "io/PIC.h" #include "io/Serial.h" #include "log/Log.h" -#include "memory/KernelMemoryManager.h" #include "memory/Memory.h" +#include "memory/MemoryManager.h" #include "memory/MemoryMap.h" #include "memory/PMM.h" #include "memory/VMM.h" diff --git a/kernel/src/memory/KernelMemoryManager.cpp b/kernel/src/memory/MemoryManager.cpp similarity index 77% rename from kernel/src/memory/KernelMemoryManager.cpp rename to kernel/src/memory/MemoryManager.cpp index cdb04f3d..65c86f20 100644 --- a/kernel/src/memory/KernelMemoryManager.cpp +++ b/kernel/src/memory/MemoryManager.cpp @@ -1,17 +1,17 @@ -#include "memory/KernelMemoryManager.h" +#include "memory/MemoryManager.h" #include "assert.h" #include "memory/KernelHeap.h" #include "memory/PMM.h" #include "memory/VMM.h" -void* KernelMemoryManager::get_mapping(void* physicalAddress, int flags) +void* MemoryManager::get_mapping(void* physicalAddress, int flags) { uint64_t virtualAddress = KernelHeap::request_virtual_page(); kernelVMM.map(virtualAddress, (uint64_t)physicalAddress, flags); return (void*)virtualAddress; } -void* KernelMemoryManager::get_unaligned_mapping(void* physicalAddress, int flags) +void* MemoryManager::get_unaligned_mapping(void* physicalAddress, int flags) { uint64_t offset = (uint64_t)physicalAddress % 4096; uint64_t virtualAddress = KernelHeap::request_virtual_page(); @@ -19,7 +19,7 @@ void* KernelMemoryManager::get_unaligned_mapping(void* physicalAddress, int flag return (void*)(virtualAddress + offset); } -void* KernelMemoryManager::get_unaligned_mappings(void* physicalAddress, uint64_t count, int flags) +void* MemoryManager::get_unaligned_mappings(void* physicalAddress, uint64_t count, int flags) { uint64_t offset = (uint64_t)physicalAddress % 4096; uint64_t virtualAddress = KernelHeap::request_virtual_pages(count); @@ -30,27 +30,27 @@ void* KernelMemoryManager::get_unaligned_mappings(void* physicalAddress, uint64_ return (void*)(virtualAddress + offset); } -void KernelMemoryManager::release_unaligned_mapping(void* mapping) +void MemoryManager::release_unaligned_mapping(void* mapping) { uint64_t offset = (uint64_t)mapping % 4096; kernelVMM.unmap((uint64_t)mapping - offset); KernelHeap::free_virtual_page((uint64_t)mapping - offset); } -void KernelMemoryManager::release_unaligned_mappings(void* mapping, uint64_t count) +void MemoryManager::release_unaligned_mappings(void* mapping, uint64_t count) { uint64_t offset = (uint64_t)mapping % 4096; KernelHeap::free_virtual_pages((uint64_t)mapping - offset, count); for (uint64_t i = 0; i < count; i++) { kernelVMM.unmap(((uint64_t)mapping - offset) + (i * 4096)); } } -void KernelMemoryManager::release_mapping(void* mapping) +void MemoryManager::release_mapping(void* mapping) { kernelVMM.unmap((uint64_t)mapping); KernelHeap::free_virtual_page((uint64_t)mapping); } -void* KernelMemoryManager::get_page(int flags) +void* MemoryManager::get_page(int flags) { void* physicalAddress = PMM::request_page(); uint64_t virtualAddress = KernelHeap::request_virtual_page(); @@ -58,7 +58,7 @@ void* KernelMemoryManager::get_page(int flags) return (void*)virtualAddress; } -void KernelMemoryManager::release_page(void* page) +void MemoryManager::release_page(void* page) { uint64_t physicalAddress = kernelVMM.getPhysical((uint64_t)page); ASSERT(physicalAddress != UINT64_MAX); @@ -66,7 +66,7 @@ void KernelMemoryManager::release_page(void* page) PMM::free_page((void*)physicalAddress); } -void* KernelMemoryManager::get_pages(uint64_t count, int flags) +void* MemoryManager::get_pages(uint64_t count, int flags) { uint64_t virtualAddress = KernelHeap::request_virtual_pages(count); for (uint64_t i = 0; i < count; i++) @@ -77,7 +77,7 @@ void* KernelMemoryManager::get_pages(uint64_t count, int flags) return (void*)virtualAddress; } -void KernelMemoryManager::release_pages(void* pages, uint64_t count) +void MemoryManager::release_pages(void* pages, uint64_t count) { for (uint64_t i = 0; i < count; i++) { diff --git a/kernel/src/memory/PMM.cpp b/kernel/src/memory/PMM.cpp index ba9ee2e2..6c533519 100644 --- a/kernel/src/memory/PMM.cpp +++ b/kernel/src/memory/PMM.cpp @@ -3,8 +3,8 @@ #include "memory/PMM.h" #include "assert.h" #include "bootboot.h" -#include "memory/KernelMemoryManager.h" #include "memory/Memory.h" +#include "memory/MemoryManager.h" #include "std/string.h" extern BOOTBOOT bootboot; @@ -47,7 +47,8 @@ void PMM::init() } bitmap_addr = (char*)biggest_chunk; - virtual_bitmap_addr = bitmap_addr; + virtual_bitmap_addr = + bitmap_addr; // FIXME: map this to a virtual address (ideally in the kernel heap between -128M and -64M) ASSERT((total_mem / 4096 / 8) < biggest_chunk_size); bitmap_size = total_mem / 4096 / 8 + 1; memset(bitmap_addr, 0xFF, bitmap_size); diff --git a/kernel/src/thread/Scheduler.cpp b/kernel/src/thread/Scheduler.cpp index f1083169..8b0eae09 100644 --- a/kernel/src/thread/Scheduler.cpp +++ b/kernel/src/thread/Scheduler.cpp @@ -4,7 +4,7 @@ #include "assert.h" #include "interrupts/Interrupts.h" #include "log/Log.h" -#include "memory/KernelMemoryManager.h" +#include "memory/MemoryManager.h" #include "memory/VMM.h" #include "misc/hang.h" #include "std/string.h" @@ -34,7 +34,7 @@ void Scheduler::init() memset(&idle_task, 0, sizeof(Task)); idle_task.id = free_tid++; idle_task.regs.rip = (uint64_t)&idle_task_function; - idle_task.regs.rsp = (uint64_t)KernelMemoryManager::get_page(); + idle_task.regs.rsp = (uint64_t)MemoryManager::get_page(); idle_task.regs.cs = 0x08; idle_task.regs.ss = 0x10; asm volatile("pushfq; movq (%%rsp), %%rax; movq %%rax, %0; popfq;" : "=m"(idle_task.regs.rflags)::"%rax"); @@ -42,7 +42,7 @@ void Scheduler::init() idle_task.task_sleep = 1000; idle_task.state = idle_task.Idle; - base_task = (Task*)KernelMemoryManager::get_page(); + base_task = (Task*)MemoryManager::get_page(); memset(base_task, 0, sizeof(Task)); end_task = base_task; sched_current_task = base_task; @@ -59,12 +59,12 @@ void Scheduler::init() void Scheduler::add_kernel_task(void (*task)(void)) { - Task* new_task = (Task*)KernelMemoryManager::get_page(); // FIXME: allocate memory the size of Task, not 4 KB for - // each task (YES, I know, I need malloc) + Task* new_task = (Task*)MemoryManager::get_page(); // FIXME: allocate memory the size of Task, not 4 KB for + // each task (YES, I know, I need malloc) memset(new_task, 0, sizeof(Task)); new_task->id = free_tid++; new_task->regs.rip = (uint64_t)task; - new_task->allocated_stack = (uint64_t)KernelMemoryManager::get_pages(4); // 16 KB is enough for everyone, right? + new_task->allocated_stack = (uint64_t)MemoryManager::get_pages(4); // 16 KB is enough for everyone, right? new_task->regs.rsp = new_task->allocated_stack + (4096 * 4) - sizeof(uintptr_t); new_task->regs.cs = 0x08; new_task->regs.ss = 0x10; @@ -86,11 +86,11 @@ void Scheduler::add_kernel_task(void (*task)(void)) void Scheduler::add_user_task(void* task) { Task* new_task = - (Task*)KernelMemoryManager::get_page(); // FIXME: allocate memory the size of Task, not 4 KB for each task + (Task*)MemoryManager::get_page(); // FIXME: allocate memory the size of Task, not 4 KB for each task new_task->id = free_tid++; new_task->regs.rip = (uint64_t)task; new_task->allocated_stack = - (uint64_t)KernelMemoryManager::get_pages(4, MAP_READ_WRITE | MAP_USER); // 16 KB is enough for everyone, right? + (uint64_t)MemoryManager::get_pages(4, MAP_READ_WRITE | MAP_USER); // 16 KB is enough for everyone, right? new_task->regs.rsp = new_task->allocated_stack + (4096 * 4) - sizeof(uintptr_t); new_task->regs.cs = 0x18 | 0x03; new_task->regs.ss = 0x20 | 0x03; @@ -114,8 +114,8 @@ void Scheduler::reap_task(Task* task) task_num--; Task* exiting_task = task; kinfoln("reaping task %ld", exiting_task->id); - if (exiting_task->allocated_stack) KernelMemoryManager::release_pages((void*)exiting_task->allocated_stack, 4); - KernelMemoryManager::release_page((void*)exiting_task); + if (exiting_task->allocated_stack) MemoryManager::release_pages((void*)exiting_task->allocated_stack, 4); + MemoryManager::release_page((void*)exiting_task); } void Scheduler::task_exit(Context* context)