pmm: General tyding up of pmm code

This commit is contained in:
mintsuki 2022-08-11 09:41:07 +02:00
parent 7a9a04d72f
commit fa6f607769
2 changed files with 54 additions and 102 deletions

View File

@ -15,7 +15,6 @@
#define MEMMAP_KERNEL_AND_MODULES 0x1001
#define MEMMAP_FRAMEBUFFER 0x1002
#define MEMMAP_EFI_RECLAIMABLE 0x2000
#define MEMMAP_EFI_BOOTSERVICES 0x2001
struct meminfo {
size_t uppermem;

View File

@ -84,8 +84,6 @@ static const char *memmap_type(uint32_t type) {
return "Kernel/Modules";
case MEMMAP_EFI_RECLAIMABLE:
return "EFI reclaimable";
case MEMMAP_EFI_BOOTSERVICES:
return "EFI boot services";
default:
return "???";
}
@ -360,7 +358,7 @@ void init_memmap(void) {
our_type = MEMMAP_RESERVED; break;
case EfiBootServicesCode:
case EfiBootServicesData:
our_type = MEMMAP_EFI_BOOTSERVICES; break;
our_type = MEMMAP_EFI_RECLAIMABLE; break;
case EfiACPIReclaimMemory:
our_type = MEMMAP_ACPI_RECLAIMABLE; break;
case EfiACPIMemoryNVS:
@ -439,28 +437,24 @@ static void pmm_reclaim_uefi_mem(struct e820_entry_t *m, size_t *_count) {
}
struct e820_entry_t *recl = ext_mem_alloc(recl_i * sizeof(struct e820_entry_t));
if (m == memmap) {
count = memmap_entries;
}
{
size_t recl_j = 0;
for (size_t i = 0; i < count; i++) {
for (size_t i = 0, j = 0; i < count; i++) {
if (m[i].type == MEMMAP_EFI_RECLAIMABLE) {
recl[recl_j++] = m[i];
}
recl[j++] = m[i];
}
}
another_recl:;
for (size_t ri = 0; ri < recl_i; ri++) {
struct e820_entry_t *r = &recl[ri];
// Punch holes in our EFI reclaimable entry for every EFI area which is
// boot services or conventional that fits within
size_t efi_mmap_entry_count = efi_mmap_size / efi_desc_size;
for (size_t i = 0; i < efi_mmap_entry_count; i++) {
EFI_MEMORY_DESCRIPTOR *entry = (void *)efi_mmap + i * efi_desc_size;
uint64_t base = recl->base;
uint64_t top = base + recl->length;
uint64_t base = r->base;
uint64_t top = base + r->length;
uint64_t efi_base = entry->PhysicalStart;
uint64_t efi_size = entry->NumberOfPages * 4096;
@ -505,7 +499,7 @@ another_recl:;
memmap = m;
memmap_entries = count;
memmap_alloc_range(efi_base, efi_size, our_type, false, true, false, true);
memmap_alloc_range(efi_base, efi_size, our_type, 0, true, false, false);
count = memmap_entries;
@ -514,47 +508,6 @@ another_recl:;
memmap_entries = memmap_entries_save;
}
}
if (--recl_i > 0) {
recl++;
goto another_recl;
}
// Ensure the boot services are still boot services, or free, in
// the EFI memmap, and disallow allocations since our stack and page tables
// are placed in this newly freed memory.
for (size_t i = 0; i < count; i++) {
if (m[i].type != MEMMAP_EFI_BOOTSERVICES)
continue;
// Go through EFI memmap and ensure this entry fits within a boot services
// or conventional entry
size_t entry_count = efi_mmap_size / efi_desc_size;
for (size_t j = 0; j < entry_count; j++) {
EFI_MEMORY_DESCRIPTOR *entry = (void *)efi_mmap + j * efi_desc_size;
switch (entry->Type) {
case EfiBootServicesCode:
case EfiBootServicesData:
case EfiConventionalMemory:
break;
default:
continue;
}
uint64_t base = m[i].base;
uint64_t top = base + m[i].length;
uint64_t efi_base = entry->PhysicalStart;
uint64_t efi_size = entry->NumberOfPages * 4096;
uint64_t efi_top = efi_base + efi_size;
if (!(base >= efi_base && base < efi_top
&& top > efi_base && top <= efi_top))
continue;
m[i].type = MEMMAP_USABLE;
}
}
allocations_disallowed = true;