Skip to contentSkip to conversation

multiboot2: enable quirk-modules-after-kernel

PATCH In review Zide Chen 2 participants 4 messages Mar 26, 2020 – Apr 3, 2020

lore
Share & tools
lore

Zide Chen

author
#0 lore raw

PATCH 1/1 multiboot2: enable quirk-modules-after-kernel

In multiboot2, currently there is no way to control where to load the modules. In case of user wants to reserve low address for specific usage, this quirk is useful.

---
 grub-core/loader/i386/multiboot_mbi.c |  1 -
 grub-core/loader/multiboot.c          | 23 ++++++++++++-----------
 grub-core/loader/multiboot_elfxx.c    |  7 ++++---
 grub-core/loader/multiboot_mbi2.c     |  2 ++
 include/grub/multiboot.h              |  1 +
 include/grub/multiboot2.h             |  8 ++++++++
 6 files changed, 27 insertions(+), 15 deletions(-)
grub-core/loader/i386/multiboot_mbi.c +0 -1
index ad3cc292fd18..18aaac847011 100644
@@ -63,7 +63,6 @@ static int bootdev_set;
 static grub_size_t elf_sec_num, elf_sec_entsize;
 static unsigned elf_sec_shstrndx;
 static void *elf_sections;
-grub_multiboot_quirks_t grub_multiboot_quirks;
 
 static grub_err_t
 load_kernel (grub_file_t file, const char *filename,
grub-core/loader/multiboot.c +12 -11
index 4a98d7082598..f9f66378dfb2 100644
@@ -32,6 +32,8 @@
 #include <grub/multiboot2.h>
 #define GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER GRUB_MULTIBOOT2_CONSOLE_FRAMEBUFFER
 #define GRUB_MULTIBOOT_CONSOLE_EGA_TEXT GRUB_MULTIBOOT2_CONSOLE_EGA_TEXT
+#define GRUB_MULTIBOOT_QUIRKS_NONE GRUB_MULTIBOOT2_QUIRKS_NONE
+#define GRUB_MULTIBOOT_QUIRK_MODULES_AFTER_KERNEL GRUB_MULTIBOOT2_QUIRK_MODULES_AFTER_KERNEL
 #define GRUB_MULTIBOOT(x) grub_multiboot2_ ## x
 #else
 #include <grub/multiboot.h>
@@ -210,7 +212,8 @@ grub_multiboot_unload (void)
   return GRUB_ERR_NONE;
 }
 
-static grub_uint64_t highest_load;
+GRUB_MULTIBOOT (quirks_t) GRUB_MULTIBOOT (quirks);
+grub_uint64_t GRUB_MULTIBOOT (highest_load);
 
 #define MULTIBOOT_LOAD_ELF64
 #include "multiboot_elfxx.c"
@@ -291,32 +294,32 @@ grub_cmd_multiboot (grub_command_t cmd __attribute__ ((unused)),
 
   grub_loader_unset ();
 
-  highest_load = 0;
+  GRUB_MULTIBOOT (highest_load) = 0;
 
-#ifndef GRUB_USE_MULTIBOOT2
-  grub_multiboot_quirks = GRUB_MULTIBOOT_QUIRKS_NONE;
+  GRUB_MULTIBOOT (quirks) = GRUB_MULTIBOOT_QUIRKS_NONE;
   int option_found = 0;
 
   do
     {
       option_found = 0;
+#ifndef GRUB_USE_MULTIBOOT2
       if (argc != 0 && grub_strcmp (argv[0], "--quirk-bad-kludge") == 0)
 	{
 	  argc--;
 	  argv++;
 	  option_found = 1;
-	  grub_multiboot_quirks |= GRUB_MULTIBOOT_QUIRK_BAD_KLUDGE;
+	  GRUB_MULTIBOOT (quirks) |= GRUB_MULTIBOOT_QUIRK_BAD_KLUDGE;
 	}
+#endif
 
       if (argc != 0 && grub_strcmp (argv[0], "--quirk-modules-after-kernel") == 0)
 	{
 	  argc--;
 	  argv++;
 	  option_found = 1;
-	  grub_multiboot_quirks |= GRUB_MULTIBOOT_QUIRK_MODULES_AFTER_KERNEL;
+	  GRUB_MULTIBOOT (quirks) |= GRUB_MULTIBOOT_QUIRK_MODULES_AFTER_KERNEL;
 	}
     } while (option_found);
-#endif
 
   if (argc == 0)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
@@ -392,11 +395,9 @@ grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
   if (! file)
     return grub_errno;
 
-#ifndef GRUB_USE_MULTIBOOT2
   lowest_addr = 0x100000;
-  if (grub_multiboot_quirks & GRUB_MULTIBOOT_QUIRK_MODULES_AFTER_KERNEL)
-    lowest_addr = ALIGN_UP (highest_load + 1048576, 4096);
-#endif
+  if (GRUB_MULTIBOOT (quirks) & GRUB_MULTIBOOT_QUIRK_MODULES_AFTER_KERNEL)
+    lowest_addr = ALIGN_UP (GRUB_MULTIBOOT (highest_load) + 1048576, 4096);
 
   size = grub_file_size (file);
   if (size)
grub-core/loader/multiboot_elfxx.c +4 -3
index 70cd1db513e6..8adce7b3489b 100644
@@ -89,17 +89,18 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld)
     if (phdr(i)->p_type == PT_LOAD)
       {
 	mld->link_base_addr = grub_min (mld->link_base_addr, phdr(i)->p_paddr);
-	highest_load = grub_max (highest_load, phdr(i)->p_paddr + phdr(i)->p_memsz);
+	GRUB_MULTIBOOT (highest_load) = grub_max (GRUB_MULTIBOOT (highest_load),
+			phdr(i)->p_paddr + phdr(i)->p_memsz);
       }
 
 #ifdef MULTIBOOT_LOAD_ELF64
-  if (highest_load >= 0x100000000)
+  if (GRUB_MULTIBOOT(highest_load) >= 0x100000000)
     return grub_error (GRUB_ERR_BAD_OS, "segment crosses 4 GiB border");
 #endif
 
   if (mld->relocatable)
     {
-      load_size = highest_load - mld->link_base_addr;
+      load_size = GRUB_MULTIBOOT(highest_load) - mld->link_base_addr;
 
       grub_dprintf ("multiboot_loader", "align=0x%lx, preference=0x%x, "
 		    "load_size=0x%x, avoid_efi_boot_services=%d\n",
grub-core/loader/multiboot_mbi2.c +2 -0
index 18e766c7b31c..3883732df531 100644
@@ -315,8 +315,10 @@ grub_multiboot2_load (grub_file_t file, const char *filename)
 	  grub_free (mld.buffer);
 	  return err;
 	}
+
       mld.link_base_addr = load_addr;
       mld.load_base_addr = get_physical_target_address (ch);
+      grub_multiboot2_highest_load = mld.load_base_addr + code_size;
       source = get_virtual_current_address (ch);
 
       grub_dprintf ("multiboot_loader", "link_base_addr=0x%x, load_base_addr=0x%x, "
include/grub/multiboot.h +1 -0
index bd0a9873e6c1..3d7c5ad29318 100644
@@ -107,6 +107,7 @@ grub_multiboot_load_elf (mbi_load_data_t *mld);
 extern grub_size_t grub_multiboot_pure_size;
 extern grub_size_t grub_multiboot_alloc_mbi;
 extern grub_uint32_t grub_multiboot_payload_eip;
+extern grub_uint64_t grub_multiboot_highest_load;
 
 
 #endif /* ! GRUB_MULTIBOOT_HEADER */
include/grub/multiboot2.h +8 -0
index 502d34ef1804..fa74508afcda 100644
@@ -27,6 +27,13 @@
 #include <grub/types.h>
 #include <grub/err.h>
 
+typedef enum
+  {
+    GRUB_MULTIBOOT2_QUIRKS_NONE = 0,
+    GRUB_MULTIBOOT2_QUIRK_MODULES_AFTER_KERNEL = 1
+  } grub_multiboot2_quirks_t;
+extern grub_multiboot2_quirks_t grub_multiboot2_quirks;
+
 extern struct grub_relocator *grub_multiboot2_relocator;
 
 void grub_multiboot2 (int argc, char *argv[]);
@@ -99,6 +106,7 @@ grub_multiboot2_load_elf (mbi_load_data_t *mld);
 extern grub_size_t grub_multiboot2_pure_size;
 extern grub_size_t grub_multiboot2_alloc_mbi;
 extern grub_uint32_t grub_multiboot2_payload_eip;
+extern grub_uint64_t grub_multiboot2_highest_load;
 
 
 #endif /* ! GRUB_MULTIBOOT_HEADER */
Signature · 1 line
2.17.1
author
#1 lore raw

Hi,

It will be very helpful to upstream this feature for our use case. Could you please help to review this patch?

Best Regards,
Zide

Quoted 190 lines

-----Original Message-----
From: Chen, Zide <[email protected]>
Sent: Thursday, March 26, 2020 3:46 PM
To: [email protected]
Cc: Chen, Zide <[email protected]>
Subject: [PATCH] multiboot2: enable quirk-modules-after-kernel

In multiboot2, currently there is no way to control where to load the modules. In case of user wants to reserve low address for specific usage, this quirk is useful.

---
 grub-core/loader/i386/multiboot_mbi.c |  1 -
 grub-core/loader/multiboot.c          | 23 ++++++++++++-----------
 grub-core/loader/multiboot_elfxx.c    |  7 ++++---
 grub-core/loader/multiboot_mbi2.c     |  2 ++
 include/grub/multiboot.h              |  1 +
 include/grub/multiboot2.h             |  8 ++++++++
 6 files changed, 27 insertions(+), 15 deletions(-)
grub-core/loader/i386/multiboot_mbi.c +0 -1
index ad3cc292fd18..18aaac847011 100644
@@ -63,7 +63,6 @@ static int bootdev_set;
 static grub_size_t elf_sec_num, elf_sec_entsize;
 static unsigned elf_sec_shstrndx;
 static void *elf_sections;
-grub_multiboot_quirks_t grub_multiboot_quirks;
 
 static grub_err_t
 load_kernel (grub_file_t file, const char *filename,
grub-core/loader/multiboot.c +12 -11
index 4a98d7082598..f9f66378dfb2 100644
@@ -32,6 +32,8 @@
 #include <grub/multiboot2.h>
 #define GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER GRUB_MULTIBOOT2_CONSOLE_FRAMEBUFFER
 #define GRUB_MULTIBOOT_CONSOLE_EGA_TEXT GRUB_MULTIBOOT2_CONSOLE_EGA_TEXT
+#define GRUB_MULTIBOOT_QUIRKS_NONE GRUB_MULTIBOOT2_QUIRKS_NONE
+#define GRUB_MULTIBOOT_QUIRK_MODULES_AFTER_KERNEL GRUB_MULTIBOOT2_QUIRK_MODULES_AFTER_KERNEL
 #define GRUB_MULTIBOOT(x) grub_multiboot2_ ## x
 #else
 #include <grub/multiboot.h>
@@ -210,7 +212,8 @@ grub_multiboot_unload (void)
   return GRUB_ERR_NONE;
 }
 
-static grub_uint64_t highest_load;
+GRUB_MULTIBOOT (quirks_t) GRUB_MULTIBOOT (quirks);
+grub_uint64_t GRUB_MULTIBOOT (highest_load);
 
 #define MULTIBOOT_LOAD_ELF64
 #include "multiboot_elfxx.c"
@@ -291,32 +294,32 @@ grub_cmd_multiboot (grub_command_t cmd __attribute__ ((unused)),
 
   grub_loader_unset ();
 
-  highest_load = 0;
+  GRUB_MULTIBOOT (highest_load) = 0;
 
-#ifndef GRUB_USE_MULTIBOOT2
-  grub_multiboot_quirks = GRUB_MULTIBOOT_QUIRKS_NONE;
+  GRUB_MULTIBOOT (quirks) = GRUB_MULTIBOOT_QUIRKS_NONE;
   int option_found = 0;
 
   do
     {
       option_found = 0;
+#ifndef GRUB_USE_MULTIBOOT2
       if (argc != 0 && grub_strcmp (argv[0], "--quirk-bad-kludge") == 0)
 	{
 	  argc--;
 	  argv++;
 	  option_found = 1;
-	  grub_multiboot_quirks |= GRUB_MULTIBOOT_QUIRK_BAD_KLUDGE;
+	  GRUB_MULTIBOOT (quirks) |= GRUB_MULTIBOOT_QUIRK_BAD_KLUDGE;
 	}
+#endif
 
       if (argc != 0 && grub_strcmp (argv[0], "--quirk-modules-after-kernel") == 0)
 	{
 	  argc--;
 	  argv++;
 	  option_found = 1;
-	  grub_multiboot_quirks |= GRUB_MULTIBOOT_QUIRK_MODULES_AFTER_KERNEL;
+	  GRUB_MULTIBOOT (quirks) |= GRUB_MULTIBOOT_QUIRK_MODULES_AFTER_KERNEL;
 	}
     } while (option_found);
-#endif
 
   if (argc == 0)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
@@ -392,11 +395,9 @@ grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
   if (! file)
     return grub_errno;
 
-#ifndef GRUB_USE_MULTIBOOT2
   lowest_addr = 0x100000;
-  if (grub_multiboot_quirks & GRUB_MULTIBOOT_QUIRK_MODULES_AFTER_KERNEL)
-    lowest_addr = ALIGN_UP (highest_load + 1048576, 4096);
-#endif
+  if (GRUB_MULTIBOOT (quirks) & GRUB_MULTIBOOT_QUIRK_MODULES_AFTER_KERNEL)
+    lowest_addr = ALIGN_UP (GRUB_MULTIBOOT (highest_load) + 1048576, 4096);
 
   size = grub_file_size (file);
   if (size)
grub-core/loader/multiboot_elfxx.c +4 -3
index 70cd1db513e6..8adce7b3489b 100644
@@ -89,17 +89,18 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld)
     if (phdr(i)->p_type == PT_LOAD)
       {
 	mld->link_base_addr = grub_min (mld->link_base_addr, phdr(i)->p_paddr);
-	highest_load = grub_max (highest_load, phdr(i)->p_paddr + phdr(i)->p_memsz);
+	GRUB_MULTIBOOT (highest_load) = grub_max (GRUB_MULTIBOOT (highest_load),
+			phdr(i)->p_paddr + phdr(i)->p_memsz);
       }
 
 #ifdef MULTIBOOT_LOAD_ELF64
-  if (highest_load >= 0x100000000)
+  if (GRUB_MULTIBOOT(highest_load) >= 0x100000000)
     return grub_error (GRUB_ERR_BAD_OS, "segment crosses 4 GiB border");
 #endif
 
   if (mld->relocatable)
     {
-      load_size = highest_load - mld->link_base_addr;
+      load_size = GRUB_MULTIBOOT(highest_load) - mld->link_base_addr;
 
       grub_dprintf ("multiboot_loader", "align=0x%lx, preference=0x%x, "
 		    "load_size=0x%x, avoid_efi_boot_services=%d\n",
grub-core/loader/multiboot_mbi2.c +2 -0
index 18e766c7b31c..3883732df531 100644
@@ -315,8 +315,10 @@ grub_multiboot2_load (grub_file_t file, const char *filename)
 	  grub_free (mld.buffer);
 	  return err;
 	}
+
       mld.link_base_addr = load_addr;
       mld.load_base_addr = get_physical_target_address (ch);
+      grub_multiboot2_highest_load = mld.load_base_addr + code_size;
       source = get_virtual_current_address (ch);
 
       grub_dprintf ("multiboot_loader", "link_base_addr=0x%x, load_base_addr=0x%x, "
include/grub/multiboot.h +1 -0
index bd0a9873e6c1..3d7c5ad29318 100644
@@ -107,6 +107,7 @@ grub_multiboot_load_elf (mbi_load_data_t *mld);
 extern grub_size_t grub_multiboot_pure_size;
 extern grub_size_t grub_multiboot_alloc_mbi;
 extern grub_uint32_t grub_multiboot_payload_eip;
+extern grub_uint64_t grub_multiboot_highest_load;
 
 
 #endif /* ! GRUB_MULTIBOOT_HEADER */
include/grub/multiboot2.h +8 -1
index 502d34ef1804..fa74508afcda 100644
@@ -27,6 +27,13 @@
 #include <grub/types.h>
 #include <grub/err.h>
 
+typedef enum
+  {
+    GRUB_MULTIBOOT2_QUIRKS_NONE = 0,
+    GRUB_MULTIBOOT2_QUIRK_MODULES_AFTER_KERNEL = 1
+  } grub_multiboot2_quirks_t;
+extern grub_multiboot2_quirks_t grub_multiboot2_quirks;
+
 extern struct grub_relocator *grub_multiboot2_relocator;
 
 void grub_multiboot2 (int argc, char *argv[]);
@@ -99,6 +106,7 @@ grub_multiboot2_load_elf (mbi_load_data_t *mld);
 extern grub_size_t grub_multiboot2_pure_size;
 extern grub_size_t grub_multiboot2_alloc_mbi;
 extern grub_uint32_t grub_multiboot2_payload_eip;
+extern grub_uint64_t grub_multiboot2_highest_load;
 
 
 #endif /* ! GRUB_MULTIBOOT_HEADER */
--

2.17.1

#2 lore raw
In reply to Zide Chen

Dear Zide,

Thank you very much for your patch.

Am 26.03.20 um 23:46 schrieb Zide Chen:

Quoted 3 lines

In multiboot2, currently there is no way to control where to load the modules. In case of user wants to reserve low address for specific usage, this quirk is useful.

It’d be great if you elaborated, where the quirk comes from, and how the the change can be tested to reproduce your issue.

Lastly, it’d be great to also describe your implementation in the commit message, so reviewers can verify that too.

[…]

Kind regards,

Paul

author
#3 lore raw

Hi Paul,

Thank you very much for your reply!

We are using Grub to boot ACRN hypervisor. It could run into problem to launch large guest Linux kernel.

For example:
multiboot2 /boot/acrn.bin
module2 /boot/bzImage

In this case, ACRN image is loaded to address 256M according to ACRN's multiboot2 address tag, and the guest VM's bzImage is loaded to address 1M because Grub allocates memory for multiboot2 module from the lowest address 0. When ACRN is launching the guest Linux, it prefers to relocate guest bzImage to address 16M and it could have problem if the original bzImage is larger than 15MB which overlaps with address 16M.

ACRN has various choices to resolve this problem. Our first choice is to ask boot loader to load the modules after the ACRN image since ACRN is a lightweight hypervisor and it's always better to have simpler logic and smaller footprint.

BTW, I will update the patch with more descriptions as well.

Best Regards,
Zide

Quoted 29 lines

-----Original Message-----
From: Paul Menzel <[email protected]>
Sent: Thursday, April 2, 2020 4:40 AM
To: Chen, Zide <[email protected]>
Cc: The development of GNU GRUB <[email protected]>
Subject: Re: [PATCH] multiboot2: enable quirk-modules-after-kernel

Dear Zide,

Thank you very much for your patch.

Am 26.03.20 um 23:46 schrieb Zide Chen:

Quoted 3 lines

In multiboot2, currently there is no way to control where to load the modules. In case of user wants to reserve low address for specific usage, this quirk is useful.

It’d be great if you elaborated, where the quirk comes from, and how the the change can be tested to reproduce your issue.

Lastly, it’d be great to also describe your implementation in the commit message, so reviewers can verify that too.

[…]

Kind regards,

Paul