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