ptos: vad: Add VAD insertions and create VALLOC VADs

Signed-off-by: Chloe M. <chloe@mensia.org>
This commit is contained in:
2026-07-12 06:26:18 +00:00
parent 03c7b160e8
commit 8221e33395
3 changed files with 97 additions and 5 deletions
+18 -1
View File
@@ -9,8 +9,9 @@
#ifndef _MM_VAD_H_
#define _MM_VAD_H_ 1
#include <ptapi/status.h>
#include <ptdef.h>
#include <mm/vmm.h>
#include <mm/vm.h>
#include <tree.h>
/*
@@ -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_ */
+51
View File
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2026, Chloe M.
* Provided under the BSD-3 clause.
*
* Description: Virtual address descriptor management
* Author: Chloe M.
*/
#include <mm/vad.h>
/* 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;
}
+28 -4
View File
@@ -8,6 +8,7 @@
#include <mm/vm.h>
#include <mm/pm.h>
#include <mm/vad.h>
#include <ex/trace.h>
#include <hal/page.h>
#include <hal/mmu.h>
@@ -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);