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

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