diff --git a/service/ptos/head/mm/pm.h b/service/ptos/head/mm/pm.h index d3868a8..0b56361 100644 --- a/service/ptos/head/mm/pm.h +++ b/service/ptos/head/mm/pm.h @@ -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_ */ diff --git a/service/ptos/ke/Makefile b/service/ptos/ke/Makefile index 31d11ff..5048929 100644 --- a/service/ptos/ke/Makefile +++ b/service/ptos/ke/Makefile @@ -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) diff --git a/service/ptos/ke/init.c b/service/ptos/ke/init.c index fa27c18..ada6f13 100644 --- a/service/ptos/ke/init.c +++ b/service/ptos/ke/init.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -45,4 +46,7 @@ KiKernelInit(VOID) /* Initialize the boot video console */ BootVidInitCons(NULL); + + /* Initialize physical memory */ + MmPmInit(); } diff --git a/service/ptos/mm/pm.c b/service/ptos/mm/pm.c new file mode 100644 index 0000000..ca537cf --- /dev/null +++ b/service/ptos/mm/pm.c @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause. + * + * Description: Physical memory management + * Author: Chloe M. + */ + +#include +#include +#include + +#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(); +}