stos: pmm: Add pageframe descriptor population

Signed-off-by: Chloe M. <chloe@mensia.org>
This commit is contained in:
Chloe M.
2026-06-22 06:33:14 +00:00
parent 52d98c3615
commit ef0588512c
3 changed files with 144 additions and 1 deletions
+115 -1
View File
@@ -9,7 +9,12 @@
#include <ex/trace.h>
#include <ke/bpal.h>
#include <mm/pmm.h>
#include <mm/vmm.h>
#include <hal/page.h>
#include <units.h>
#include <stdef.h>
#define LOAD_DELAY 128
#define DTRACE(Fmt, ...) \
TRACE("[ PMM ]: " Fmt, ##__VA_ARGS__)
@@ -20,6 +25,28 @@
? TypeTab[(TYPE)] \
: "invalid"
/*
* Physical frame descriptor
*
* @Next: Next PFD in list
* @Prev: Previous PFD in list
*/
typedef struct _MM_PFD {
struct _MM_PFD *Next;
struct _MM_PFD *Prev;
} MM_PFD;
/*
* Page frame descriptor list
*
* @First: First entry in list
* @Last: Last entry in list
*/
typedef struct {
MM_PFD *First;
MM_PFD *Last;
} MM_PFD_LIST;
/*
* Memory type constant to human readable
* string table.
@@ -38,6 +65,63 @@ static const char *TypeTab[] = {
/* Globals */
static USIZE UsableMemory = 0;
static MM_PFD_LIST PfdList;
/*
* Append a page frame descriptor to a page frame descriptor list
*
* @List: PFD list to append to
* @Pfd: PFD to append
*/
static VOID
MmPfdAppend(MM_PFD_LIST *List, MM_PFD *Pfd)
{
MM_PFD *Last;
if (List == NULL || Pfd == NULL) {
return;
}
if (List->Last == NULL || List->First == NULL) {
List->Last = Pfd;
List->First = Pfd;
} else {
Last = List->Last;
Pfd->Prev = Last;
Last->Next = Pfd;
List->Last = Pfd;
}
}
/*
* Pop a page frame descriptor from a page frame descriptor list
*
* @List: Page frame descriptor list to pop from
*
* Returns NULL if there are no entries left
*/
static MM_PFD *
MmPfdPop(MM_PFD_LIST *List)
{
MM_PFD *Last, *Prev;
if (List == NULL) {
return NULL;
}
Last = List->Last;
if (Last == NULL) {
return NULL;
}
/* Unlink from the list */
Prev = Last->Prev;
List->Last = List->Last->Prev;
Prev->Next = NULL;
List->Last = Prev;
return Last;
}
/*
* Print the units of a size in a pretty format
@@ -61,13 +145,29 @@ MmPrintUnits(USIZE Length, const CHAR *Title)
}
}
/*
* Display a loading status
*/
static VOID
MmLoadUpdate(VOID)
{
const UCHAR LoadMax = 3;
static UCHAR LoadIdx = 0;
static CHAR LoadProg[3] = {'/', '-', '\\'};
TRACE("\x1b[?25l%c \b\b", LoadProg[LoadIdx++]);
LoadIdx %= LoadMax;
}
static VOID
MmProbeMemory(VOID)
{
BPAL_HANDLE BpalHandle;
MEMMAP_ENTRY Entry;
UPTR EntryEnd;
USIZE Idx;
VOID *Vma, *Base;
USIZE Idx, Offset;
USIZE EntryCount = 0;
ST_STATUS Status;
KeBpalGetHandle(&BpalHandle);
@@ -85,9 +185,23 @@ MmProbeMemory(VOID)
continue;
}
Base = PMA_TO_VMA(Entry.Base);
for (Offset = 0; Offset < Entry.Length; Offset += PAGESIZE) {
Vma = PTR_OFFSET(Base, Offset);
MmPfdAppend(&PfdList, Vma);
EntryCount++;
/* Display a little loader as this takes awhile */
if ((EntryCount & (LOAD_DELAY - 1)) == 0) {
MmLoadUpdate();
}
}
UsableMemory += Entry.Length;
}
TRACE("\b\x1b[?25h");
DTRACE("%d entries populated\n", EntryCount);
MmPrintUnits(UsableMemory, "avl");
}