:: commit 0a27376a2b329438f6ed0c47de0fb81abe11eec0

48cf <32851089+48cf@users.noreply.github.com> — 2023-12-02 22:20

parents: 1ae036c3fe

misc/efi: Call SetVirtualAddressMap when exiting boot services

diff --git a/common/lib/misc.c b/common/lib/misc.c
index 18d1848c..f015e639 100644
--- a/common/lib/misc.c
+++ b/common/lib/misc.c
@@ -151,6 +151,7 @@ error:
 #endif
 
 no_unwind bool efi_boot_services_exited = false;
+no_unwind bool efi_no_va_map = false;
 
 bool efi_exit_boot_services(void) {
     EFI_STATUS status;
@@ -199,6 +200,21 @@ retry:
         goto retry;
     }
 
+    for (size_t i = 0; i < efi_mmap_size / efi_desc_size; i++) {
+        EFI_MEMORY_DESCRIPTOR *md = (void *)efi_mmap + i * efi_desc_size;
+        if ((md->Attribute & EFI_MEMORY_RUNTIME) == 0) {
+            continue;
+        }
+
+        md->VirtualStart = md->PhysicalStart;
+    }
+
+    status = gRT->SetVirtualAddressMap(efi_mmap_size, efi_desc_size, efi_desc_ver, efi_mmap);
+    if (status != EFI_SUCCESS) {
+        print("efi: Failed to set virtual address map\n");
+        efi_no_va_map = true;
+    }
+
 #if defined(__x86_64__) || defined(__i386__)
     asm volatile ("cli" ::: "memory");
 #elif defined (__aarch64__)
diff --git a/common/lib/misc.h b/common/lib/misc.h
index 495c7984..a9fd5fe0 100644
--- a/common/lib/misc.h
+++ b/common/lib/misc.h
@@ -25,6 +25,8 @@ extern UINTN efi_mmap_size, efi_desc_size;
 extern UINT32 efi_desc_ver;
 
 extern bool efi_boot_services_exited;
+extern bool efi_no_va_map;
+
 bool efi_exit_boot_services(void);
 #endif
 
tab: 248 wrap: offon