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

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