From 2cf6fe3f4d0811f1d62ed3ba73d15c8a187f600f Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Fri, 25 Apr 2025 18:42:30 -0400 Subject: mem_get_phys fn --- kernel/include/comus/memory.h | 13 +++++++++++++ kernel/memory/memory.c | 5 +++++ kernel/memory/paging.c | 23 +++++++++++++++++------ kernel/memory/physalloc.c | 3 ++- 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/kernel/include/comus/memory.h b/kernel/include/comus/memory.h index 92525da..408521b 100644 --- a/kernel/include/comus/memory.h +++ b/kernel/include/comus/memory.h @@ -123,6 +123,13 @@ void *mem_mapaddr(mem_ctx_t ctx, void *phys, void *virt, size_t len, */ void mem_unmapaddr(mem_ctx_t ctx, const void *virt); +/** + * Gets the physical address for a given vitural address + * @param ctx - the memory context + * @param virt - the vitural address + */ +void *mem_get_phys(mem_ctx_t ctx, const void *virt); + /** * Allocate a single page of memory with the given paging structure * @@ -194,6 +201,12 @@ void *kmapaddr(void *phys, void *virt, size_t len, unsigned int flags); */ void *kmapuseraddr(mem_ctx_t ctx, const void *virt, size_t len); +/** + * Gets the physical address for a given vitural address + * @param virt - the vitural address + */ +void *kget_phys(const void *virt); + /** * Unmaps mapped address from the kmapaddr function * @param virt - the vitural address returned from kmapaddr diff --git a/kernel/memory/memory.c b/kernel/memory/memory.c index 295823e..bd3e06b 100644 --- a/kernel/memory/memory.c +++ b/kernel/memory/memory.c @@ -31,6 +31,11 @@ void kunmapaddr(const void *virt) mem_unmapaddr(kernel_mem_ctx, virt); } +void *kget_phys(const void *virt) +{ + return mem_get_phys(kernel_mem_ctx, virt); +} + void *kalloc_page(void) { return mem_alloc_page(kernel_mem_ctx, F_PRESENT | F_WRITEABLE); diff --git a/kernel/memory/paging.c b/kernel/memory/paging.c index 5a0b285..d54a26b 100644 --- a/kernel/memory/paging.c +++ b/kernel/memory/paging.c @@ -5,6 +5,7 @@ #include "physalloc.h" #include "paging.h" #include "memory.h" +#include // PAGE MAP LEVEL 4 ENTRY struct pml4e { @@ -753,15 +754,11 @@ void *mem_mapaddr(mem_ctx_t ctx, void *phys, void *virt, size_t len, void *kmapuseraddr(mem_ctx_t ctx, const void *vADDR, size_t len) { char *pADDR; - volatile struct pte *vPTE; - vPTE = page_locate((volatile struct pml4 *)ctx->pml4, vADDR); - if (vPTE == NULL) + pADDR = mem_get_phys(ctx, vADDR); + if (pADDR == NULL) return NULL; - pADDR = (void *)((uintptr_t)vPTE->address << 12); - pADDR += ((uint64_t)vADDR % PAGE_SIZE); - return kmapaddr(pADDR, NULL, len, F_PRESENT | F_WRITEABLE); } @@ -776,6 +773,20 @@ void mem_unmapaddr(mem_ctx_t ctx, const void *virt) unmap_pages(&kernel_pml4, virt, pages); } +void *mem_get_phys(mem_ctx_t ctx, const void *vADDR) +{ + char *pADDR; + volatile struct pte *vPTE; + + vPTE = page_locate((volatile struct pml4 *)ctx->pml4, vADDR); + if (vPTE == NULL) + return NULL; + + pADDR = (void *)((uintptr_t)vPTE->address << 12); + pADDR += ((uint64_t)vADDR % PAGE_SIZE); + return pADDR; +} + void *mem_alloc_page(mem_ctx_t ctx, unsigned int flags) { return mem_alloc_pages(ctx, 1, flags); diff --git a/kernel/memory/physalloc.c b/kernel/memory/physalloc.c index 01464ee..48d2e3f 100644 --- a/kernel/memory/physalloc.c +++ b/kernel/memory/physalloc.c @@ -88,7 +88,8 @@ void *alloc_phys_pages_exact(size_t pages) if (bitmap == NULL || page_start == NULL) { // temporary bump allocator void *addr = (void *)memory_start; - assert(pages == 1, "caller expects more pages, but is only getting one"); + assert(pages == 1, + "caller expects more pages, but is only getting one"); memory_start += PAGE_SIZE; return addr; } -- cgit v1.2.3-freya