From 8221e333958cf7dc4817c2f932a4ca6291e612a8 Mon Sep 17 00:00:00 2001 From: "Chloe M." Date: Sun, 12 Jul 2026 06:26:18 +0000 Subject: [PATCH] ptos: vad: Add VAD insertions and create VALLOC VADs Signed-off-by: Chloe M. --- service/ptos/head/mm/vad.h | 19 +++++++++++++- service/ptos/mm/vad.c | 51 ++++++++++++++++++++++++++++++++++++++ service/ptos/mm/vm.c | 32 +++++++++++++++++++++--- 3 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 service/ptos/mm/vad.c diff --git a/service/ptos/head/mm/vad.h b/service/ptos/head/mm/vad.h index 060f433..e069397 100644 --- a/service/ptos/head/mm/vad.h +++ b/service/ptos/head/mm/vad.h @@ -9,8 +9,9 @@ #ifndef _MM_VAD_H_ #define _MM_VAD_H_ 1 +#include #include -#include +#include #include /* @@ -37,4 +38,20 @@ typedef struct { USIZE Elements; } MM_VAD_TREE; +/* + * Initialize a virtual address descriptor tree + * + * @Tree: Tree to initialize + */ +VOID MmVadTreeInit(MM_VAD_TREE *Tree); + +/* + * Insert a virtual address descriptor into a virtual address + * descriptor tree. + * + * @Tree: VAD tree to be inserted into + * @Vad: VAD to insert + */ +PT_STATUS MmVadTreeInsert(MM_VAD_TREE *Tree, MM_VAD *Vad); + #endif /* !_MM_VAD_H_ */ diff --git a/service/ptos/mm/vad.c b/service/ptos/mm/vad.c new file mode 100644 index 0000000..5470f96 --- /dev/null +++ b/service/ptos/mm/vad.c @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause. + * + * Description: Virtual address descriptor management + * Author: Chloe M. + */ + +#include + +/* Prototypes */ +static LONG VadCmp(MM_VAD *V0, MM_VAD *V1); + +/* RBT generation */ +RB_PROTOTYPE(VadTree, _MM_VAD, TreeLink, VadCmp); +RB_GENERATE(VadTree, _MM_VAD, TreeLink, VadCmp); + +static LONG +VadCmp(MM_VAD *V0, MM_VAD *V1) +{ + MM_RANGE *Range0, *Range1; + + Range0 = &V0->Range; + Range1 = &V1->Range; + + return (Range0->VirtualBase < Range1->VirtualBase) + ? -1 + : Range0->VirtualBase > Range1->VirtualBase; +} + +VOID +MmVadTreeInit(MM_VAD_TREE *Tree) +{ + if (Tree == NULL) { + return; + } + + Tree->Elements = 0; + RB_INIT(&Tree->Head); +} + +PT_STATUS +MmVadTreeInsert(MM_VAD_TREE *Tree, MM_VAD *Vad) +{ + if (Tree == NULL || Vad == NULL) { + return STATUS_INVALID_PARAM; + } + + RB_INSERT(VadTree, &Tree->Head, Vad); + return STATUS_SUCCESS; +} diff --git a/service/ptos/mm/vm.c b/service/ptos/mm/vm.c index 3f86674..bc0f289 100644 --- a/service/ptos/mm/vm.c +++ b/service/ptos/mm/vm.c @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -34,8 +35,10 @@ static USIZE PageSizeTable[] = { [PAGESIZE_4K] = 0x1000 }; -/* Bump pointer */ +/* Bump pointer and vad tree */ static UPTR VallocBump = VALLOC_BASE; +static MM_VAD_TREE VadTree; + PT_STATUS MmUnmapRange(MMU_VAS *Vas, const MM_RANGE *Range, MMU_PAGESIZE PageSize) { @@ -113,8 +116,12 @@ MmVmInit(VOID) UPTR PhysicalBase; USIZE NumPages; MM_PFN Pfn; + MM_RANGE *CurrentRange; + MM_VAD *CurrentVad; HalMmuReadVas(&VasCurrent); + MmVadTreeInit(&VadTree); + VirtualStart = VallocBump; VallocBump += INITIAL_VALLOC_PAGES * PAGESIZE; VirtualEnd = VallocBump; @@ -122,19 +129,36 @@ MmVmInit(VOID) for (Off = 0; Off < NumPages * PAGESIZE; Off += PAGESIZE) { Pfn = MmRequestFrame(); - PhysicalBase = PFN_TO_ADDRESS(Pfn); + if (Pfn == 0) { + KeBugCheck(BUGCHECK_OOM, "out of memory\n"); + } + PhysicalBase = PFN_TO_ADDRESS(Pfn); Status = HalMmuMapPage( &VasCurrent, VirtualStart + Off, - PhysicalBase + Off, - PAGE_READ | PAGE_WRITE, + PhysicalBase, + PAGE_READ| PAGE_WRITE, PAGESIZE_4K ); if (PT_ERROR(Status)) { KeBugCheck(BUGCHECK_OOM, "failed to map valloc pages\n"); } + + CurrentVad = (MM_VAD *)(VirtualStart + Off); + CurrentRange = &CurrentVad->Range; + CurrentRange->VirtualBase = VirtualStart + Off; + CurrentRange->PhysicalBase = PhysicalBase; + CurrentRange->Length = PAGESIZE; + Status = MmVadTreeInsert( + &VadTree, + CurrentVad + ); + + if (PT_ERROR(Status)) { + KeBugCheck(BUGCHECK_MISC, "failed to initialize vad tree\n"); + } } DTRACE("initialized %d valloc pages\n", NumPages);