Use memory region map to discover the memory

This commit is contained in:
2025-12-08 01:28:09 +01:00
parent 807f067101
commit 0de8cca8bb
9 changed files with 431 additions and 26 deletions

View File

@ -39,6 +39,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <mm/pmm.h>
#include <mm/bba.h>
#include <mm/liballoc.h>
#include <mm/mregmap.h>
#include <config.h>
extern byte_t __kernel_end;
@ -46,19 +47,47 @@ extern byte_t __kernel_end;
static void dump_multiboot_header(void) {
struct multiboot *mb = multiboot_get();
dbgf("multiboot=%p\n", mb);
dbgf("multiboot=%p, flags=%08x\n", mb, mb->flags);
dbgf("multiboot sanity dump:\n");
dbgf("flags=%08x, mem_low=%08x, mem_up=%08x\n",
mb->flags, mb->mem_low, mb->mem_up);
dbgf("flags=%08x, mem_lower=%08x, mem_upper=%08x\n",
mb->flags, mb->mem_lower, mb->mem_upper);
}
static void mregmap_init1(void) {
struct multiboot *mb = multiboot_get();
struct multiboot_mmap_entry *mmap;
for (mmap = (struct multiboot_mmap_entry *)(mb->mmap_addr + VIRT_BASE);
(uptr_t)mmap < (mb->mmap_addr + VIRT_BASE + mb->mmap_length);
mmap = (struct multiboot_mmap_entry *)((uptr_t)mmap + mmap->size + sizeof(mmap->size))) {
dbgf("mmap entry: size=%08x, addr=%08x, len=%08x, type=%u\n",
mmap->size, mmap->addr1, mmap->len1, mmap->type);
static int type_map[] = {
[1] = MREG_AVAIL_RAM,
[2] = MREG_I386_PC_RESERVED,
[3] = MREG_ACPI,
[4] = MREG_MEM_HBRNT,
[5] = MREG_DEFCT_RMOD,
};
struct mreg mreg = {
.start = (uptr_t)mmap->addr1,
.size = (usize_t)mmap->len1,
.type = type_map[mmap->type],
};
mregmap_add_region(&mreg);
}
}
static void pmm_init1(void) {
struct multiboot *mb = multiboot_get();
dbgf("__kernel_end=%p, %p\n", &__kernel_end, (uptr_t)&__kernel_end - VIRT_BASE);
struct mreg *avail_ram = mregmap_first_avail();
usize_t usable_ram_bytes = mb->mem_up * 1024;
dbgf("usable ram = %zu\n", usable_ram_bytes);
usize_t usable_ram_bytes = avail_ram->size;
dbgf("usable ram = %08x\n", usable_ram_bytes);
uptr_t ram_start = ALIGN_UP((uptr_t)&__kernel_end - VIRT_BASE, 0x1000);
uptr_t ram_end = ALIGN_DOWN(0x100000 + usable_ram_bytes, 0x1000);
@ -89,6 +118,7 @@ void bootmain(void *mbootptr) {
#endif
dump_multiboot_header();
mregmap_init1();
pmm_init1();
bba_init();
mm_init();