From c854bc705bb794582fc562d1e29cb3a194fba454 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Mon, 22 Dec 2025 14:41:10 -0500 Subject: mos: bpt: Add boot memory map interface Signed-off-by: Ian Moffett --- mos/sys/bpt/bpt_limine.c | 30 ++++++++++++++++++++++++++++++ mos/sys/inc/kern/bpt.h | 39 +++++++++++++++++++++++++++++++++++++++ mos/sys/kern/kern_bpt.c | 14 ++++++++++++++ 3 files changed, 83 insertions(+) diff --git a/mos/sys/bpt/bpt_limine.c b/mos/sys/bpt/bpt_limine.c index 0decc57..532ce6a 100644 --- a/mos/sys/bpt/bpt_limine.c +++ b/mos/sys/bpt/bpt_limine.c @@ -16,6 +16,13 @@ static volatile struct limine_hhdm_request hhdm_req = { .revision = 0 }; +/* Memory map */ +static struct limine_memmap_response *mem_resp; +static volatile struct limine_memmap_request mem_req = { + .id = LIMINE_MEMMAP_REQUEST, + .revision = 0 +}; + static MOS_STATUS limine_get_vars(BPT_VARS *res) { @@ -27,6 +34,26 @@ limine_get_vars(BPT_VARS *res) return STATUS_SUCCESS; } +static MOS_STATUS +limine_get_mementry(USIZE index, BPT_MEMENTRY *res) +{ + struct limine_memmap_entry *entry; + + if (res == NULL) { + return STATUS_INVALID_ARG; + } + + if (index >= mem_resp->entry_count) { + return STATUS_INVALID_ARG; + } + + entry = mem_resp->entries[index]; + res->base = entry->base; + res->length = entry->length; + res->type = entry->type; /* 1:1 */ + return STATUS_SUCCESS; +} + MOS_STATUS bpt_limine_init(BPT_HOOKS *hooks_res) { @@ -35,6 +62,9 @@ bpt_limine_init(BPT_HOOKS *hooks_res) } hhdm_resp = hhdm_req.response; + mem_resp = mem_req.response; + hooks_res->get_vars = limine_get_vars; + hooks_res->get_mementry = limine_get_mementry; return STATUS_SUCCESS; } diff --git a/mos/sys/inc/kern/bpt.h b/mos/sys/inc/kern/bpt.h index 79d55f0..3e3c3f2 100644 --- a/mos/sys/inc/kern/bpt.h +++ b/mos/sys/inc/kern/bpt.h @@ -9,6 +9,20 @@ #include #include +/* + * Represents memory map entry types + */ +typedef enum { + BPT_MEM_USABLE, + BPT_MEM_RESERVED, + BPT_MEM_ACPI_RECLAIM, + BPT_MEM_ACPI_NVS, + BPT_MEM_BAD, + BPT_MEM_BOOTLOADER, + BPT_MEM_KERNEL, + BPT_MEM_FRAMEBUFFER +} BPT_MEM_TYPE; + /* * Represents boot protocol translation variables that * contain information provided by the bootloader @@ -19,6 +33,19 @@ typedef struct { UPTR kernel_base; } BPT_VARS; +/* + * Represents a memory map entry + * + * @base: Base address of memory area + * @length: Length of memory area in bytes + * @type: Memory entry type + */ +typedef struct { + UPTR base; + USIZE length; + BPT_MEM_TYPE type; +} BPT_MEMENTRY; + /* * Represents boot protocol translation hooks that can * be used to obtain state and variables from the underlying @@ -26,6 +53,7 @@ typedef struct { */ typedef struct { MOS_STATUS(*get_vars)(BPT_VARS *res); + MOS_STATUS(*get_mementry)(USIZE index, BPT_MEMENTRY *res); } BPT_HOOKS; /* @@ -36,6 +64,17 @@ typedef struct { */ MOS_STATUS bpt_get_vars(BPT_VARS *res); +/* + * Obtain a memory map entry + * + * @index: Entry index + * @res: Entry result + * + * Returns STATUS_SUCCESS if the entry index is within + * proper range. + */ +MOS_STATUS bpt_get_mementry(USIZE index, BPT_MEMENTRY *res); + /* * Initialize the boot protocol translation layer */ diff --git a/mos/sys/kern/kern_bpt.c b/mos/sys/kern/kern_bpt.c index 21f1c04..7d3511e 100644 --- a/mos/sys/kern/kern_bpt.c +++ b/mos/sys/kern/kern_bpt.c @@ -34,6 +34,20 @@ bpt_get_vars(BPT_VARS *res) return global_hooks.get_vars(res); } +MOS_STATUS +bpt_get_mementry(USIZE index, BPT_MEMENTRY *res) +{ + if (res == NULL) { + return STATUS_INVALID_ARG; + } + + if (global_hooks.get_mementry == NULL) { + return STATUS_IO_ERROR; + } + + return global_hooks.get_mementry(index, res); +} + MOS_STATUS bpt_init(void) { -- cgit v1.2.3