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

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