On arm64 we can't restrict memory usage below 0xffffffff as we do
on x86 because there is no guarantee memory in that range exists
at all (an example is Apple Silicon).
Instead, try to allocate in the lower 4GB range first and fall
back to the entire available address space if that did not work.

This fixes a bug on Snapdragon X Elite devices such as the Lenovo
Thinkpad T14s which crashes when accessing memory above 0xffffffff.

Signed-off-by: Tobias Heider <tobias.hei...@canonical.com>
---

 For previous discussion see
 https://lists.gnu.org/archive/html/grub-devel/2025-04/msg00072.html

 In contrast to the previous patch this one affects all archs.
 It seems generally safe not to limit it to arm64 with the fallback
 to the old behaviour but I'd be interested to hear if there are any
 concerns.
 Tested on a Snapdragon X Elite and Macbook M2 Air.

 grub-core/kern/efi/mm.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c
index df238b165..029d975dd 100644
--- a/grub-core/kern/efi/mm.c
+++ b/grub-core/kern/efi/mm.c
@@ -168,9 +168,19 @@ grub_efi_allocate_pages_real (grub_efi_physical_address_t 
address,
 void *
 grub_efi_allocate_any_pages (grub_efi_uintn_t pages)
 {
-  return grub_efi_allocate_pages_real (GRUB_EFI_MAX_USABLE_ADDRESS,
-                                      pages, GRUB_EFI_ALLOCATE_MAX_ADDRESS,
-                                      GRUB_EFI_LOADER_DATA);
+  void *ret;
+
+  ret = grub_efi_allocate_pages_real (0xffffffff,
+                                     pages, GRUB_EFI_ALLOCATE_MAX_ADDRESS,
+                                     GRUB_EFI_LOADER_DATA);
+  if (ret == NULL)
+    {
+      grub_errno = GRUB_ERR_NONE;
+      ret = grub_efi_allocate_pages_real (GRUB_EFI_MAX_USABLE_ADDRESS,
+                                         pages, GRUB_EFI_ALLOCATE_MAX_ADDRESS,
+                                         GRUB_EFI_LOADER_DATA);
+    }
+  return ret;
 }
 
 void *
-- 
2.48.1


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

Reply via email to