From 02f0ba9aad127e611d62aac2cce934240187083a Mon Sep 17 00:00:00 2001 From: nub31 Date: Sat, 6 Sep 2025 17:42:05 +0200 Subject: [PATCH] mem cleanup --- src/arch/x86_64/mem/pmm.c | 9 +++++++-- src/arch/x86_64/mem/pmm.h | 4 ++-- src/arch/x86_64/mem/vmm.c | 37 +++++++++++++++++++------------------ src/arch/x86_64/mem/vmm.h | 2 +- 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/arch/x86_64/mem/pmm.c b/src/arch/x86_64/mem/pmm.c index 350d49d..8c4c393 100644 --- a/src/arch/x86_64/mem/pmm.c +++ b/src/arch/x86_64/mem/pmm.c @@ -123,9 +123,9 @@ u64 pmm_alloc() return 0; } -void pmm_free(u64 address) +void pmm_free(u64 physical_address) { - u64 page = address / PAGE_SIZE; + u64 page = physical_address / PAGE_SIZE; if (page < BITMAP_SIZE * 8) { if (page_bitmap[page / 8] & (1 << (page % 8))) @@ -133,5 +133,10 @@ void pmm_free(u64 address) page_bitmap[page / 8] &= ~(1 << (page % 8)); free_pages++; } + else + { + printf("pmm_free: Physical address %x is already free", physical_address); + panic("pmm_free: Failed to free"); + } } } \ No newline at end of file diff --git a/src/arch/x86_64/mem/pmm.h b/src/arch/x86_64/mem/pmm.h index dc195cb..ceecc1b 100644 --- a/src/arch/x86_64/mem/pmm.h +++ b/src/arch/x86_64/mem/pmm.h @@ -3,7 +3,7 @@ #include "std.h" #include "x86_64/multiboot.h" -// Fixed at 2mb for now +// todo(nub31): Fixed at 2mb for now, might add support for 4k pages later #define PAGE_SIZE MiB(2) // Defines the theoretical max memory the kernel can allocate, not the actual memory of the system @@ -16,4 +16,4 @@ void pmm_init(multiboot_info_t* info); u64 pmm_alloc(); // Free the 2mb physical page at the specified address -void pmm_free(u64 address); \ No newline at end of file +void pmm_free(u64 physical_address); \ No newline at end of file diff --git a/src/arch/x86_64/mem/vmm.c b/src/arch/x86_64/mem/vmm.c index 5f4bdac..3194591 100644 --- a/src/arch/x86_64/mem/vmm.c +++ b/src/arch/x86_64/mem/vmm.c @@ -26,7 +26,7 @@ static u64 create_pte(u64 physical_address) return (physical_address & PTE_MASK) | PTE_PRESENT | PTE_WRITABLE | PTE_PS; } -void vmm_map(u64 virtual_address, u64 physical_address) +void vmm_map(u64 physical_address, u64 virtual_address) { u64 pml4_idx = PML4_INDEX(virtual_address); u64 pdpt_idx = PDPT_INDEX(virtual_address); @@ -40,8 +40,8 @@ void vmm_map(u64 virtual_address, u64 physical_address) panic("Failed to map virtual to physical page"); } - u64* pdpt_phys = (u64*)(pdpt & PTE_MASK); - u64 pd = pdpt_phys[pdpt_idx]; + u64* pdpt_physical_address = (u64*)(pdpt & PTE_MASK); + u64 pd = pdpt_physical_address[pdpt_idx]; if (!(pd & PTE_PRESENT)) { // todo(nub31): Dynamically create a pd table @@ -49,8 +49,8 @@ void vmm_map(u64 virtual_address, u64 physical_address) panic("Failed to map virtual to physical page"); } - u64* pd_phys = (u64*)(pd & PTE_MASK); - u64 entry = pd_phys[pd_idx]; + u64* pd_physical_address = (u64*)(pd & PTE_MASK); + u64 entry = pd_physical_address[pd_idx]; if (entry & PTE_PRESENT) { @@ -58,7 +58,7 @@ void vmm_map(u64 virtual_address, u64 physical_address) panic("Failed to map virtual to physical page"); } - pd_phys[pd_idx] = create_pte(physical_address); + pd_physical_address[pd_idx] = create_pte(physical_address); } u64 vmm_unmap(u64 virtual_address) @@ -74,42 +74,43 @@ u64 vmm_unmap(u64 virtual_address) panic("Failed to unmap virtual address"); } - u64* pdpt_phys = (u64*)(pdpt_entry & PTE_MASK); - u64 pd_entry = pdpt_phys[pdpt_idx]; + u64* pdpt_physical_address = (u64*)(pdpt_entry & PTE_MASK); + u64 pd_entry = pdpt_physical_address[pdpt_idx]; if (!(pd_entry & PTE_PRESENT)) { printf("PD not present at PDPT index %llu\n", pdpt_idx); panic("Failed to unmap virtual address"); } - u64* pd_phys = (u64*)(pd_entry & PTE_MASK); - if (!(pd_phys[pd_idx] & PTE_PRESENT)) + u64* pd_physical_address = (u64*)(pd_entry & PTE_MASK); + if (!(pd_physical_address[pd_idx] & PTE_PRESENT)) { printf("Virtual address 0x%llx is not mapped\n", virtual_address); panic("Failed to unmap virtual address"); } - u64 phys = pd_phys[pd_idx] & PTE_MASK; - pd_phys[pd_idx] = 0; + + u64 physical_address = pd_physical_address[pd_idx] & PTE_MASK; + pd_physical_address[pd_idx] = 0; __asm__ volatile("invlpg (%0)" : : "r"(virtual_address) : "memory"); - return phys; + return physical_address; } void* vmm_alloc(u64 virtual_address) { - u64 phys = pmm_alloc(); - if (!phys) + u64 physical_address = pmm_alloc(); + if (!physical_address) { panic("Out of physical memory"); } - vmm_map(virtual_address, phys); + vmm_map(physical_address, virtual_address); return (void*)virtual_address; } void vmm_free(u64 virtual_address) { - u64 phys = vmm_unmap(virtual_address); - pmm_free(phys); + u64 physical_address = vmm_unmap(virtual_address); + pmm_free(physical_address); } \ No newline at end of file diff --git a/src/arch/x86_64/mem/vmm.h b/src/arch/x86_64/mem/vmm.h index 3c37d6d..8afa56a 100644 --- a/src/arch/x86_64/mem/vmm.h +++ b/src/arch/x86_64/mem/vmm.h @@ -2,7 +2,7 @@ #include "std.h" -void vmm_map(u64 virtual_address, u64 physical_address); +void vmm_map(u64 physical_address, u64 virtual_address); u64 vmm_unmap(u64 virtual_address); void* vmm_alloc(u64 virtual_address);