From ffe001821c044ed0cbc9c36fcd49af8bb6b75bce Mon Sep 17 00:00:00 2001 From: Chloe M Date: Sat, 11 Jul 2026 20:26:24 -0400 Subject: [PATCH] ptos: vm: Initialize valloc range + zero pagemap lvls Signed-off-by: Chloe M --- service/ptos/arch/amd64/cpu/mmu.c | 1 + service/ptos/head/mm/vm.h | 5 +++ service/ptos/ke/init.c | 4 +++ service/ptos/mm/vm.c | 53 +++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+) diff --git a/service/ptos/arch/amd64/cpu/mmu.c b/service/ptos/arch/amd64/cpu/mmu.c index f876052..c79f7ce 100644 --- a/service/ptos/arch/amd64/cpu/mmu.c +++ b/service/ptos/arch/amd64/cpu/mmu.c @@ -13,6 +13,7 @@ #include #include #include +#include /* * Valid paging structure levels diff --git a/service/ptos/head/mm/vm.h b/service/ptos/head/mm/vm.h index 0751e57..198ea0b 100644 --- a/service/ptos/head/mm/vm.h +++ b/service/ptos/head/mm/vm.h @@ -67,4 +67,9 @@ PT_STATUS MmMapRange( MM_PROT Prot, MMU_PAGESIZE PageSize ); +/* + * Initialize virtual memory + */ +VOID MmVmInit(VOID); + #endif /* !_MM_VM_H_ */ diff --git a/service/ptos/ke/init.c b/service/ptos/ke/init.c index 1a97700..d24aaa4 100644 --- a/service/ptos/ke/init.c +++ b/service/ptos/ke/init.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -55,4 +56,7 @@ KiKernelInit(VOID) /* Phase 1 init of bootstrap core */ HalKpcrP1Init(&BootstrapCore); + + /* Initialize virtual memory */ + MmVmInit(); } diff --git a/service/ptos/mm/vm.c b/service/ptos/mm/vm.c index 34caf69..3f86674 100644 --- a/service/ptos/mm/vm.c +++ b/service/ptos/mm/vm.c @@ -7,9 +7,22 @@ */ #include +#include +#include #include +#include +#include #include +#define DTRACE(Fmt, ...) \ + TRACE("[ VM ]: " Fmt, ##__VA_ARGS__) + +/* + * Number of pages we start with that can be valloc'd + * total. + */ +#define INITIAL_VALLOC_PAGES 128 + /* Use to safely convert page size */ #define PAGE_SIZE(PS) \ ((PS) >= NELEM(PageSizeTable)) \ @@ -21,6 +34,9 @@ static USIZE PageSizeTable[] = { [PAGESIZE_4K] = 0x1000 }; +/* Bump pointer */ +static UPTR VallocBump = VALLOC_BASE; + PT_STATUS MmUnmapRange(MMU_VAS *Vas, const MM_RANGE *Range, MMU_PAGESIZE PageSize) { PT_STATUS Status; @@ -86,3 +102,40 @@ MmMapRange(MMU_VAS *Vas, const MM_RANGE *Range, MM_PROT Prot, MMU_PAGESIZE PageS return STATUS_SUCCESS; } + +VOID +MmVmInit(VOID) +{ + PT_STATUS Status; + MMU_VAS VasCurrent; + UPTR VirtualStart; + UPTR VirtualEnd, Off; + UPTR PhysicalBase; + USIZE NumPages; + MM_PFN Pfn; + + HalMmuReadVas(&VasCurrent); + VirtualStart = VallocBump; + VallocBump += INITIAL_VALLOC_PAGES * PAGESIZE; + VirtualEnd = VallocBump; + NumPages = (VirtualEnd - VirtualStart) / PAGESIZE; + + for (Off = 0; Off < NumPages * PAGESIZE; Off += PAGESIZE) { + Pfn = MmRequestFrame(); + PhysicalBase = PFN_TO_ADDRESS(Pfn); + + Status = HalMmuMapPage( + &VasCurrent, + VirtualStart + Off, + PhysicalBase + Off, + PAGE_READ | PAGE_WRITE, + PAGESIZE_4K + ); + + if (PT_ERROR(Status)) { + KeBugCheck(BUGCHECK_OOM, "failed to map valloc pages\n"); + } + } + + DTRACE("initialized %d valloc pages\n", NumPages); +}