27b9d726ff
Signed-off-by: Chloe M <chloe@mensia.org>
82 lines
1.8 KiB
C
82 lines
1.8 KiB
C
/*
|
|
* 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();
|
|
}
|