ptos: mm: Add physical memory groundwork

Signed-off-by: Chloe M <chloe@mensia.org>
This commit is contained in:
2026-07-10 18:26:29 -04:00
parent 5026a758ec
commit 27b9d726ff
4 changed files with 91 additions and 0 deletions
+5
View File
@@ -24,4 +24,9 @@ typedef USIZE MM_PFN;
#define ADDRESS_TO_PFN(ADDR) \
((ADDR) >> LOG2_PAGESIZE)
/*
* Initialize the physical memory management
*/
VOID MmPmInit(VOID);
#endif /* !_MM_PM_H_ */
+1
View File
@@ -13,6 +13,7 @@ CFILES = $(shell find . -name "*.c")
CFILES += $(shell find ../lib -name "*.c")
CFILES += $(shell find ../xt -name "*.c")
CFILES += $(shell find ../drivers -name "*.c")
CFILES += $(shell find ../mm -name "*.c")
DFILES = $(CFILES:.c=.d)
OFILES = $(CFILES:.c=.o)
+4
View File
@@ -9,6 +9,7 @@
#include <ptdef.h>
#include <ke/bpal.h>
#include <ex/trace.h>
#include <mm/pm.h>
#include <hal/serial.h>
#include <drivers/bootvid/fbio.h>
#include <machine/pio.h>
@@ -45,4 +46,7 @@ KiKernelInit(VOID)
/* Initialize the boot video console */
BootVidInitCons(NULL);
/* Initialize physical memory */
MmPmInit();
}
+81
View File
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2026, Chloe M.
* Provided under the BSD-3 clause.
*
* Description: Physical memory management
* Author: Chloe M.
*/
#include <mm/pm.h>
#include <ke/bpal.h>
#include <ex/trace.h>
#define DTRACE(Fmt, ...) \
TRACE("[ PM ]: " Fmt, ##__VA_ARGS__)
/* Safe macro to get region string */
#define PM_REGION(TYPE) \
((TYPE) >= NELEM(PmRegionTable)) \
? "bad type" \
: PmRegionTable[(TYPE)]
/*
* Table of known memory regions
*/
static const CHAR *PmRegionTable[] = {
[MEMORY_USABLE] = "available",
[MEMORY_RESERVED] = "reserved",
[MEMORY_ACPI_RECLAIM] = "acpi reclaimable",
[MEMORY_ACPI_NVS] = "acpi nv-sleep",
[MEMORY_BAD] = "bad",
[MEMORY_BOOTLOADER] = "bootloader",
[MEMORY_KERNEL] = "system",
[MEMORY_FRAMEBUFFER] = "vram",
[MEMORY_ACPI_TABLES] = "acpi tables"
};
/*
* Probe physical memory for usable regions and list
* installed memory.
*/
static VOID
PmProbe(VOID)
{
PT_STATUS Status;
BPAL_HANDLE BpalHandle;
MEMMAP_ENTRY Entry;
UPTR Start, End;
USIZE Index;
/*
* We obtain memory map entries from the BPAL layer as it is provided
* by the bootloader which got it from firmware.
*/
KeBpalGetHandle(&BpalHandle);
/* Iterate each memory entry */
for (Index = 0;; ++Index) {
Status = BpalHandle.MemEntryIdx(Index, &Entry);
/* Are we out of entries to fetch? */
if (PT_ERROR(Status)) {
break;
}
Start = Entry.Base;
End = Start + Entry.Length;
DTRACE(
"%p - %p : %s\n",
Start,
End,
PM_REGION(Entry.Type)
);
}
}
VOID
MmPmInit(VOID)
{
DTRACE("probing physical memory...\n");
PmProbe();
}