pmm: Use our custom EFI memory entry type to release EFI memory back instead of relying on our memmap

This commit is contained in:
mintsuki 2021-07-06 08:55:32 +02:00
parent e63931f0dd
commit eadee3e6cb
2 changed files with 48 additions and 11 deletions

View File

@ -344,20 +344,50 @@ void pmm_reclaim_uefi_mem(void) {
void pmm_release_uefi_mem(void) {
EFI_STATUS status;
for (size_t i = 0; i < memmap_entries; i++) {
if (memmap[i].type != MEMMAP_USABLE
&& memmap[i].type != MEMMAP_BOOTLOADER_RECLAIMABLE) {
continue;
EFI_MEMORY_DESCRIPTOR tmp_mmap[1];
efi_mmap_size = sizeof(tmp_mmap);
UINTN mmap_key = 0;
uefi_call_wrapper(gBS->GetMemoryMap, 5,
&efi_mmap_size, tmp_mmap, &mmap_key, &efi_desc_size, &efi_desc_ver);
efi_mmap_size += 4096;
status = uefi_call_wrapper(gBS->FreePool, 1, efi_mmap);
if (status)
goto fail;
status = uefi_call_wrapper(gBS->AllocatePool, 3,
EfiLoaderData, efi_mmap_size, &efi_mmap);
if (status)
goto fail;
status = uefi_call_wrapper(gBS->GetMemoryMap, 5,
&efi_mmap_size, efi_mmap, &mmap_key, &efi_desc_size, &efi_desc_ver);
if (status)
goto fail;
// Go through new EFI memmap and free up bootloader held entries
size_t entry_count = efi_mmap_size / efi_desc_size;
for (size_t i = 0; i < entry_count; i++) {
EFI_MEMORY_DESCRIPTOR *entry = (void *)efi_mmap + i * efi_desc_size;
if (entry->Type == 0x80000000) {
status = uefi_call_wrapper(gBS->FreePages, 2,
entry->PhysicalStart, entry->NumberOfPages);
if (status)
panic("pmm: FreePages failure (%x)", status);
}
status = uefi_call_wrapper(gBS->FreePages, 2,
memmap[i].base, memmap[i].length / 4096);
if (status)
panic("pmm: FreePages failure (%x)", status);
}
allocations_disallowed = true;
return;
fail:
panic("pmm: Failed to release UEFI memory");
}
#endif

View File

@ -9,6 +9,7 @@
#include <lib/fb.h>
#include <lib/uri.h>
#include <lib/print.h>
#include <lib/libc.h>
#include <sys/idt.h>
#include <drivers/vga_textmode.h>
#include <mm/pmm.h>
@ -119,8 +120,14 @@ void chainload(char *config) {
if (!uri_open(image, image_path))
panic("chainload: Failed to open image with path `%s`. Is the path correct?", image_path);
void *ptr = freadall(image, MEMMAP_RESERVED);
void *_ptr = freadall(image, MEMMAP_RESERVED);
size_t image_size = image->size;
void *ptr;
status = uefi_call_wrapper(gBS->AllocatePool, 3,
EfiLoaderData, image_size, &ptr);
if (status)
panic("chainload: Allocation failure");
memcpy(ptr, _ptr, image_size);
term_deinit();