From 181b0fdf61a805a6833d7b57c8c20f674275270e Mon Sep 17 00:00:00 2001 From: "Chloe M." Date: Tue, 14 Jul 2026 21:25:51 +0000 Subject: [PATCH] ptos/amd64+hal: Add virtual address space forking Signed-off-by: Chloe M. --- service/ptos/arch/amd64/cpu/mmu.c | 34 +++++++++++++++++++++++++++++++ service/ptos/head/hal/mmu.h | 9 ++++++++ service/ptos/mm/vm.c | 13 +++++++++++- 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/service/ptos/arch/amd64/cpu/mmu.c b/service/ptos/arch/amd64/cpu/mmu.c index c79f7ce..7428b0b 100644 --- a/service/ptos/arch/amd64/cpu/mmu.c +++ b/service/ptos/arch/amd64/cpu/mmu.c @@ -260,3 +260,37 @@ HalMmuUnmapPage(MMU_VAS *Vas, UPTR VirtualBase, MMU_PAGESIZE PageSize) MdTlbFlushSingle(VirtualBase); return STATUS_SUCCESS; } + +PT_STATUS +HalMmuForkVas(const MMU_VAS *Source, MMU_VAS *Result) +{ + const UPTR *Src; + UPTR *Dest; + UPTR DestPhysBase; + MM_PFN DestPfn; + + if (Source == NULL || Result == NULL) { + return STATUS_INVALID_PARAM; + } + + DestPfn = MmRequestFrame(); + if (DestPfn == PFN_ERROR) { + return STATUS_NO_MEMORY; + } + + DestPhysBase = PFN_TO_ADDRESS(DestPfn); + Src = PMA_TO_VMA((Source->Cr3 & PTE_ADDR_MASK)); + Dest = PMA_TO_VMA(DestPhysBase); + + /* Begin copy */ + for (USIZE Idx = 0; Idx < 512; ++Idx) { + if (Idx < 256) { + Dest[Idx] = 0; + } else { + Dest[Idx] = Src[Idx]; + } + } + + Result->Cr3 = DestPhysBase; + return STATUS_SUCCESS; +} diff --git a/service/ptos/head/hal/mmu.h b/service/ptos/head/hal/mmu.h index 7879dfc..e928b2b 100644 --- a/service/ptos/head/hal/mmu.h +++ b/service/ptos/head/hal/mmu.h @@ -38,6 +38,15 @@ VOID HalMmuReadVas(MMU_VAS *Result); */ VOID HalMmuWriteVas(const MMU_VAS *Vas); +/* + * Fork a virtual address space with the user-portion zeroed + * out. + * + * @Source: Source VAS to fork from + * @Result: Result is written here + */ +PT_STATUS HalMmuForkVas(const MMU_VAS *Source, MMU_VAS *Result); + /* * Map a single page of memory * diff --git a/service/ptos/mm/vm.c b/service/ptos/mm/vm.c index 2c3132e..450d5a2 100644 --- a/service/ptos/mm/vm.c +++ b/service/ptos/mm/vm.c @@ -35,6 +35,9 @@ static USIZE PageSizeTable[] = { [PAGESIZE_4K] = 0x1000 }; +/* Kernel VAS */ +static MMU_VAS KVas; + /* Bump pointer and vad tree */ static UPTR VallocBump = VALLOC_BASE; static MM_VAD_TREE VadTree; @@ -138,7 +141,15 @@ MmVmInit(VOID) HalMmuReadVas(&VasCurrent); MmVadTreeInit(&VadTree); + Status = HalMmuForkVas(&VasCurrent, &KVas); + if (PT_ERROR(Status)) { + KeBugCheck( + BUGCHECK_OOM, + "failed to allocate kernel vas\n" + ); + } + HalMmuWriteVas(&KVas); VirtualStart = VallocBump; VallocBump += INITIAL_VALLOC_PAGES * PAGESIZE; VirtualEnd = VallocBump; @@ -152,7 +163,7 @@ MmVmInit(VOID) PhysicalBase = PFN_TO_ADDRESS(Pfn); Status = HalMmuMapPage( - &VasCurrent, + &KVas, VirtualStart + Off, PhysicalBase, PAGE_READ| PAGE_WRITE,