/* SPDX-License-Identifier: GPL-2.0-only */ #include #include static struct mem_zone zones[128]; static size_t n_zones; static const char *zone_type_strings[] = {"USABLE", "RESERVED", "ACPI_RECLAIMABLE", "ACPI_NVS", "BAD_MEMORY", "BOOTLOADER_RECLAIMABLE", "EXECUTABLE_AND_MODULES", "FRAMEBUFFER", "RESERVED_MAPPED"}; size_t mem_get_zones(struct mem_zone **ret) { *ret = zones; return n_zones; } static void mem_init_zones() { struct limine_memmap_response *map = kern_map_res; bool last_usable = false; struct mem_zone *last; static const char *grid[] = {"Entry", "Physical base", "Length", "Type"}; int grid_width = kprintf("| %-5s | %-18s | %-18s | %-22s |\n", grid[0], grid[1], grid[2], grid[3]); kprintf("|"); for (int i = 0; i < grid_width - 3; i++) { kprintf("-"); } kprintf("|\n"); for (int i = 0; i < map->entry_count; i++) { struct limine_memmap_entry *ent = kern_map_res->entries[i]; if (ARRAY_SIZE(zone_type_strings) > ent->type) kprintf("| %-5d | 0x%-16x | 0x%-16x | %-22s |\n", i, ent->base, ent->length, zone_type_strings[ent->type]); bool usable; switch (ent->type) { case LIMINE_MEMMAP_USABLE: usable = true; break; default: usable = false; break; } if (ent->base == 0) usable = false; if (usable) { struct mem_zone *zone; vaddr_t zone_vbase = (vaddr_t)TO_VIRT(ent->base); if (last_usable && last->virt_base + last->len == zone_vbase) { zone = &zones[n_zones - 1]; zone->len += ent->length; } else { zone = &zones[n_zones]; zone->virt_base = zone_vbase; zone->len = ent->length; n_zones += 1; } last = zone; } else { last = NULL; } last_usable = usable; } } void mem_init() { mem_init_zones(); }