From 2c17a4db1d32a9ace5bdb430a0faf7e28a00110e Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Tue, 16 Sep 2025 00:29:14 -0400 Subject: kern/amd64: mmu: Add mmu_new_vas() routine This commit introduces the ability to create new virtual address spaces for user programs and a zeroed lower half. Signed-off-by: Ian Moffett --- src/sys/arch/amd64/cpu/mmu.c | 32 ++++++++++++++++++++++++++++++++ src/sys/include/vm/mmu.h | 9 +++++++++ 2 files changed, 41 insertions(+) diff --git a/src/sys/arch/amd64/cpu/mmu.c b/src/sys/arch/amd64/cpu/mmu.c index 60fb4f1..05e8b55 100644 --- a/src/sys/arch/amd64/cpu/mmu.c +++ b/src/sys/arch/amd64/cpu/mmu.c @@ -332,6 +332,38 @@ mmu_map_single(struct vm_vas *vas, struct mmu_map *spec, int prot) return 0; } +/* + * Create a new virtual address space + */ +int +mmu_new_vas(struct vm_vas *res) +{ + uint64_t *dest, *src; + paddr_t frame; + + src = PHYS_TO_VIRT(g_kvas.cr3 & PTE_ADDR_MASK); + frame = vm_alloc_frame(1); + if (frame == 0) { + return -ENOMEM; + } + + /* + * Copy only the higher half but zero the + * lower half. + */ + dest = PHYS_TO_VIRT(frame); + for (int i = 0; i < 512; ++i) { + if (i < 256) { + dest[i] = 0; + } else { + dest[i] = src[i]; + } + } + + res->cr3 = VIRT_TO_PHYS(dest); + return 0; +} + /* * Verify that we are in a known state */ diff --git a/src/sys/include/vm/mmu.h b/src/sys/include/vm/mmu.h index 5c1c2bb..908748b 100644 --- a/src/sys/include/vm/mmu.h +++ b/src/sys/include/vm/mmu.h @@ -97,4 +97,13 @@ int mmu_map_single(struct vm_vas *vas, struct mmu_map *spec, int prot); */ int mmu_this_vas(struct vm_vas *vasres_p); +/* + * Create a new virtual address structure with a zeroed + * user porition. + * + * Returns zero on success, otherwise a less than zero + * value on failure. + */ +int mmu_new_vas(struct vm_vas *res); + #endif /* !_MACHINE_MMU_H_ */ -- cgit v1.2.3