diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2025-01-04 10:53:38 -0500 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-01-04 10:53:38 -0500 |
commit | ae6db9be229bdfdf19fa98eec9871f89b08d14eb (patch) | |
tree | 0e2bf74317ac7f6de728811897662b66d9dadf7e /kernel/mm/memory_map.c | |
parent | cd10fa5a9a84d445bea5bc4bbf903aac1df65053 (diff) |
kernel: mm: Detect memory regions.
Doesn't do much, but lays down the foundation for future memory management
efforts.
Diffstat (limited to 'kernel/mm/memory_map.c')
-rw-r--r-- | kernel/mm/memory_map.c | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/kernel/mm/memory_map.c b/kernel/mm/memory_map.c new file mode 100644 index 0000000..c6aefee --- /dev/null +++ b/kernel/mm/memory_map.c @@ -0,0 +1,78 @@ +/* + * CMOS + * Copyright (C) 2025 Raghuram Subramani <raghus2247@gmail.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdint.h> + +#include <common.h> + +#include <kernel/halt.h> +#include <libk/stdio.h> + +#include <mm/multiboot.h> + +ALWAYS_INLINE static char * +memory_map_fetch_type(multiboot_memory_map_t *mmap) +{ + switch (mmap->type) { + case MULTIBOOT_MEMORY_AVAILABLE: + return "AVAILABLE"; + case MULTIBOOT_MEMORY_RESERVED: + return "RESERVED"; + case MULTIBOOT_MEMORY_ACPI_RECLAIMABLE: + return "ACPI_RECLAIMABLE"; + case MULTIBOOT_MEMORY_NVS: + return "NVS"; + case MULTIBOOT_MEMORY_BADRAM: + return "BADRAM"; + default: + return "UNKNOWN"; + } +} + +void +memory_map_load(multiboot_info_t *multiboot_info) +{ + printk("mm", "Loading Memory Map:"); + + /* https://www.gnu.org/software/grub/manual/multiboot/multiboot.html: + * + * If bit 6 in the ‘flags’ word is set, then the ‘mmap_*’ fields are + * valid, and indicate the address and length of a buffer containing a + * memory map of the machine provided by the BIOS. ‘mmap_addr’ is the + * address, and ‘mmap_length’ is the total size of the buffer. The buffer + * consists of one or more of the following size/structure pairs (‘size’ + * is really used for skipping to the next pair): */ + + if (!(multiboot_info->flags & MULTIBOOT_INFO_ELF_SHDR)) { + printk("mm", "Invalid memory map!"); + halt(); + } + + for (uint32_t i = 0; i < multiboot_info->mmap_length; + i += sizeof(multiboot_memory_map_t)) { + multiboot_memory_map_t *mmap + = (multiboot_memory_map_t *) (multiboot_info->mmap_addr + i); + + printk("mm", + "start: 0x%x | length: 0x%x | size: 0x%x | type: %s", + mmap->addr_low, + mmap->len_low, + mmap->size, + memory_map_fetch_type(mmap)); + } +} |