Blame SOURCES/0498-x86-efi-Make-our-own-allocator-for-kernel-stuff.patch

b9d01e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b9d01e
From: Peter Jones <pjones@redhat.com>
b9d01e
Date: Wed, 12 Sep 2018 16:03:55 -0400
b9d01e
Subject: [PATCH] x86-efi: Make our own allocator for kernel stuff
b9d01e
b9d01e
This helps enable allocations above 4GB.
b9d01e
b9d01e
Signed-off-by: Peter Jones <pjones@redhat.com>
b9d01e
(cherry picked from commit cfea4ae780f8860d472cd2d5a9765ec2fe2adc83)
b9d01e
---
b9d01e
 grub-core/loader/i386/efi/linux.c | 167 +++++++++++++++++++++-----------------
b9d01e
 1 file changed, 94 insertions(+), 73 deletions(-)
b9d01e
b9d01e
diff --git a/grub-core/loader/i386/efi/linux.c b/grub-core/loader/i386/efi/linux.c
b9d01e
index c9650561f..5eed2014c 100644
b9d01e
--- a/grub-core/loader/i386/efi/linux.c
b9d01e
+++ b/grub-core/loader/i386/efi/linux.c
b9d01e
@@ -49,6 +49,65 @@ static char *linux_cmdline;
b9d01e
 
b9d01e
 #define BYTES_TO_PAGES(bytes)   (((bytes) + 0xfff) >> 12)
b9d01e
 
b9d01e
+struct allocation_choice {
b9d01e
+    grub_efi_physical_address_t addr;
b9d01e
+    grub_efi_allocate_type_t alloc_type;
b9d01e
+};
b9d01e
+
b9d01e
+static struct allocation_choice max_addresses[] =
b9d01e
+  {
b9d01e
+    { GRUB_EFI_MAX_ALLOCATION_ADDRESS, GRUB_EFI_ALLOCATE_MAX_ADDRESS },
b9d01e
+    { GRUB_EFI_MAX_ALLOCATION_ADDRESS, GRUB_EFI_ALLOCATE_MAX_ADDRESS },
b9d01e
+    { GRUB_EFI_MAX_ALLOCATION_ADDRESS, GRUB_EFI_ALLOCATE_MAX_ADDRESS },
b9d01e
+    { 0, 0 }
b9d01e
+  };
b9d01e
+
b9d01e
+static inline void
b9d01e
+kernel_free(void *addr, grub_efi_uintn_t size)
b9d01e
+{
b9d01e
+  if (addr && size)
b9d01e
+    grub_efi_free_pages ((grub_efi_physical_address_t)(grub_addr_t)addr,
b9d01e
+			 BYTES_TO_PAGES(size));
b9d01e
+}
b9d01e
+
b9d01e
+static void *
b9d01e
+kernel_alloc(grub_efi_uintn_t size, const char * const errmsg)
b9d01e
+{
b9d01e
+  void *addr = 0;
b9d01e
+  unsigned int i;
b9d01e
+  grub_efi_physical_address_t prev_max = 0;
b9d01e
+
b9d01e
+  for (i = 0; max_addresses[i].addr != 0 && addr == 0; i++)
b9d01e
+    {
b9d01e
+      grub_uint64_t max = max_addresses[i].addr;
b9d01e
+      grub_efi_uintn_t pages;
b9d01e
+
b9d01e
+      if (max == prev_max)
b9d01e
+	continue;
b9d01e
+
b9d01e
+      pages = BYTES_TO_PAGES(size);
b9d01e
+      grub_dprintf ("linux", "Trying to allocate %lu pages from %p\n",
b9d01e
+		    pages, (void *)max);
b9d01e
+
b9d01e
+      prev_max = max;
b9d01e
+      addr = grub_efi_allocate_pages_real (max, pages,
b9d01e
+					   max_addresses[i].alloc_type,
b9d01e
+					   GRUB_EFI_LOADER_DATA);
b9d01e
+      if (addr)
b9d01e
+	grub_dprintf ("linux", "Allocated at %p\n", addr);
b9d01e
+    }
b9d01e
+
b9d01e
+  while (grub_error_pop ())
b9d01e
+    {
b9d01e
+      ;
b9d01e
+    }
b9d01e
+
b9d01e
+  if (addr == NULL)
b9d01e
+    grub_error (GRUB_ERR_OUT_OF_MEMORY, "%s", errmsg);
b9d01e
+
b9d01e
+  return addr;
b9d01e
+}
b9d01e
+
b9d01e
 static grub_err_t
b9d01e
 grub_linuxefi_boot (void)
b9d01e
 {
b9d01e
@@ -64,19 +123,12 @@ grub_linuxefi_unload (void)
b9d01e
 {
b9d01e
   grub_dl_unref (my_mod);
b9d01e
   loaded = 0;
b9d01e
-  if (initrd_mem)
b9d01e
-    grub_efi_free_pages ((grub_efi_physical_address_t)(grub_addr_t)initrd_mem,
b9d01e
-			 BYTES_TO_PAGES(params->ramdisk_size));
b9d01e
-  if (linux_cmdline)
b9d01e
-    grub_efi_free_pages ((grub_efi_physical_address_t)(grub_addr_t)
b9d01e
-			 linux_cmdline,
b9d01e
-			 BYTES_TO_PAGES(params->cmdline_size + 1));
b9d01e
-  if (kernel_mem)
b9d01e
-    grub_efi_free_pages ((grub_efi_physical_address_t)(grub_addr_t)kernel_mem,
b9d01e
-			 BYTES_TO_PAGES(kernel_size));
b9d01e
-  if (params)
b9d01e
-    grub_efi_free_pages ((grub_efi_physical_address_t)(grub_addr_t)params,
b9d01e
-			 BYTES_TO_PAGES(16384));
b9d01e
+
b9d01e
+  kernel_free(initrd_mem, params->ramdisk_size);
b9d01e
+  kernel_free(linux_cmdline, params->cmdline_size + 1);
b9d01e
+  kernel_free(kernel_mem, kernel_size);
b9d01e
+  kernel_free(params, sizeof(*params));
b9d01e
+
b9d01e
   return GRUB_ERR_NONE;
b9d01e
 }
b9d01e
 
b9d01e
@@ -157,19 +209,13 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
b9d01e
 	}
b9d01e
     }
b9d01e
 
b9d01e
-  initrd_mem = grub_efi_allocate_pages_max (GRUB_EFI_MAX_ALLOCATION_ADDRESS, BYTES_TO_PAGES(size));
b9d01e
-  if (!initrd_mem)
b9d01e
-    initrd_mem = grub_efi_allocate_pages_max (GRUB_EFI_MAX_USABLE_ADDRESS, BYTES_TO_PAGES(size));
b9d01e
-  if (!initrd_mem)
b9d01e
-    {
b9d01e
-      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("can't allocate initrd"));
b9d01e
-      goto fail;
b9d01e
-    }
b9d01e
-
b9d01e
-  grub_dprintf ("linux", "initrd_mem = %lx\n", (unsigned long) initrd_mem);
b9d01e
+  initrd_mem = kernel_alloc(size, N_("can't allocate initrd"));
b9d01e
+  if (initrd_mem == NULL)
b9d01e
+    goto fail;
b9d01e
+  grub_dprintf ("linux", "initrd_mem = %p\n", initrd_mem);
b9d01e
 
b9d01e
   params->ramdisk_size = size;
b9d01e
-  params->ramdisk_image = (grub_uint32_t)(grub_addr_t) initrd_mem;
b9d01e
+  params->ramdisk_image = initrd_mem;
b9d01e
 
b9d01e
   ptr = initrd_mem;
b9d01e
 
b9d01e
@@ -230,7 +276,6 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
b9d01e
   filelen = grub_file_size (file);
b9d01e
 
b9d01e
   kernel = grub_malloc(filelen);
b9d01e
-
b9d01e
   if (!kernel)
b9d01e
     {
b9d01e
       grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate kernel buffer"));
b9d01e
@@ -289,7 +334,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
b9d01e
       goto fail;
b9d01e
     }
b9d01e
 
b9d01e
-#if defined(__x86_64__) || defined(__aarch64__)
b9d01e
+#if defined(__x86_64__)
b9d01e
   grub_dprintf ("linux", "checking lh->xloadflags\n");
b9d01e
   if (!(lh->xloadflags & LINUX_XLF_KERNEL_64))
b9d01e
     {
b9d01e
@@ -308,17 +353,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
b9d01e
     }
b9d01e
 #endif
b9d01e
 
b9d01e
-  params = grub_efi_allocate_pages_max (GRUB_EFI_MAX_ALLOCATION_ADDRESS,
b9d01e
-					BYTES_TO_PAGES(sizeof(*params)));
b9d01e
+  params = kernel_alloc (sizeof(*params), "cannot allocate kernel parameters");
b9d01e
   if (!params)
b9d01e
-    params = grub_efi_allocate_pages_max (GRUB_EFI_MAX_USABLE_ADDRESS,
b9d01e
-					  BYTES_TO_PAGES(sizeof(*params)));
b9d01e
-  if (! params)
b9d01e
-    {
b9d01e
-      grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate kernel parameters");
b9d01e
-      goto fail;
b9d01e
-    }
b9d01e
-
b9d01e
+    goto fail;
b9d01e
   grub_dprintf ("linux", "params = %p\n", params);
b9d01e
 
b9d01e
   grub_memset (params, 0, sizeof(*params));
b9d01e
@@ -337,19 +374,10 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
b9d01e
   grub_dprintf ("linux", "new lh is at %p\n", lh);
b9d01e
 
b9d01e
   grub_dprintf ("linux", "setting up cmdline\n");
b9d01e
-  linux_cmdline = grub_efi_allocate_pages_max(GRUB_EFI_MAX_ALLOCATION_ADDRESS,
b9d01e
-					      BYTES_TO_PAGES(lh->cmdline_size + 1));
b9d01e
+  linux_cmdline = kernel_alloc (lh->cmdline_size + 1, N_("can't allocate cmdline"));
b9d01e
   if (!linux_cmdline)
b9d01e
-    linux_cmdline = grub_efi_allocate_pages_max(GRUB_EFI_MAX_USABLE_ADDRESS,
b9d01e
-						BYTES_TO_PAGES(lh->cmdline_size + 1));
b9d01e
-  if (!linux_cmdline)
b9d01e
-    {
b9d01e
-      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("can't allocate cmdline"));
b9d01e
-      goto fail;
b9d01e
-    }
b9d01e
-
b9d01e
-  grub_dprintf ("linux", "linux_cmdline = %lx\n",
b9d01e
-		(unsigned long)linux_cmdline);
b9d01e
+    goto fail;
b9d01e
+  grub_dprintf ("linux", "linux_cmdline = %p\n", linux_cmdline);
b9d01e
 
b9d01e
   grub_memcpy (linux_cmdline, LINUX_IMAGE, sizeof (LINUX_IMAGE));
b9d01e
   grub_create_loader_cmdline (argc, argv,
b9d01e
@@ -358,27 +386,24 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
b9d01e
 			      GRUB_VERIFY_KERNEL_CMDLINE);
b9d01e
 
b9d01e
   grub_dprintf ("linux", "cmdline:%s\n", linux_cmdline);
b9d01e
-  grub_dprintf ("linux", "setting lh->cmd_line_ptr\n");
b9d01e
-  lh->cmd_line_ptr = (grub_uint32_t)(grub_addr_t)linux_cmdline;
b9d01e
+  grub_dprintf ("linux", "setting lh->cmd_line_ptr to 0x%08x\n",
b9d01e
+		linux_cmdline);
b9d01e
+  lh->cmd_line_ptr = linux_cmdline;
b9d01e
 
b9d01e
   handover_offset = lh->handover_offset;
b9d01e
-  grub_dprintf("linux", "handover_offset: %08x\n", handover_offset);
b9d01e
+  grub_dprintf("linux", "handover_offset: 0x%08x\n", handover_offset);
b9d01e
 
b9d01e
   start = (lh->setup_sects + 1) * 512;
b9d01e
 
b9d01e
-  kernel_mem = grub_efi_allocate_pages_max(lh->pref_address,
b9d01e
-					   BYTES_TO_PAGES(lh->init_size));
b9d01e
-  if (!kernel_mem)
b9d01e
-    kernel_mem = grub_efi_allocate_pages_max(GRUB_EFI_MAX_ALLOCATION_ADDRESS,
b9d01e
-					     BYTES_TO_PAGES(lh->init_size));
b9d01e
-  if (!kernel_mem)
b9d01e
-    kernel_mem = grub_efi_allocate_pages_max(GRUB_EFI_MAX_USABLE_ADDRESS,
b9d01e
-					     BYTES_TO_PAGES(lh->init_size));
b9d01e
-  if (!kernel_mem)
b9d01e
+  grub_dprintf ("linux", "lh->pref_address: %p\n", (void *)(grub_addr_t)lh->pref_address);
b9d01e
+  if (lh->pref_address < (grub_uint64_t)GRUB_EFI_MAX_ALLOCATION_ADDRESS)
b9d01e
     {
b9d01e
-      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("can't allocate kernel"));
b9d01e
-      goto fail;
b9d01e
+      max_addresses[0].addr = lh->pref_address;
b9d01e
+      max_addresses[0].alloc_type = GRUB_EFI_ALLOCATE_ADDRESS;
b9d01e
     }
b9d01e
+  kernel_mem = kernel_alloc (lh->init_size, N_("can't allocate kernel"));
b9d01e
+  if (!kernel_mem)
b9d01e
+    goto fail;
b9d01e
   grub_dprintf("linux", "kernel_mem = %p\n", kernel_mem);
b9d01e
 
b9d01e
   grub_loader_set (grub_linuxefi_boot, grub_linuxefi_unload, 0);
b9d01e
@@ -413,18 +438,14 @@ fail:
b9d01e
       loaded = 0;
b9d01e
     }
b9d01e
 
b9d01e
-  if (linux_cmdline && lh && !loaded)
b9d01e
-    grub_efi_free_pages ((grub_efi_physical_address_t)(grub_addr_t)
b9d01e
-			 linux_cmdline,
b9d01e
-			 BYTES_TO_PAGES(lh->cmdline_size + 1));
b9d01e
+  if (!loaded)
b9d01e
+    {
b9d01e
+      if (lh)
b9d01e
+	kernel_free (linux_cmdline, lh->cmdline_size + 1);
b9d01e
 
b9d01e
-  if (kernel_mem && !loaded)
b9d01e
-    grub_efi_free_pages ((grub_efi_physical_address_t)(grub_addr_t)kernel_mem,
b9d01e
-			 BYTES_TO_PAGES(kernel_size));
b9d01e
-
b9d01e
-  if (params && !loaded)
b9d01e
-    grub_efi_free_pages ((grub_efi_physical_address_t)(grub_addr_t)params,
b9d01e
-			 BYTES_TO_PAGES(16384));
b9d01e
+      kernel_free (kernel_mem, kernel_size);
b9d01e
+      kernel_free (params, sizeof(*params));
b9d01e
+    }
b9d01e
 
b9d01e
   return grub_errno;
b9d01e
 }