multiboot2: enable quirk-modules-after-kernel
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 particular usage, this quirk is useful to load the modules to higher address.
This patch makes the quirk to multiboot2 by:
- remove "ifndef GRUB_USE_MULTIBOOT2" that is related to this logic.
- define separate variables for both multiboot and multiboot2, e.g. grub_multiboot_quirks, highest_load, etc.
- in grub_multiboot2_load(): calculate the highest load address of raw image and assign it to grub_multiboot2_highest_load.
- the existing code in multiboot_elfxx.c is able to calculate the highest_load for mutiboot2 already.
At the end, the adjusted GRUB_MULTIBOOT(highest_load) is used as the lowest address to allocate memory for loading modules.
--- 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
Dear Zide,
Thank you for posting version 2 of your patch. (Please add v2 to the patch tag next time.)
Maybe use the commit message summary below.
Quoted 1 line
multiboot2: Implement quirk-modules-after-kernel
Am 03.04.20 um 09:35 schrieb Zide Chen:
Quoted 1 line
In multiboot2, currently there is no way to control where to load the
Maybe start with: In contrast to Mulitboot (v1), in Mulitboot 2, there is currently …
Quoted 4 lines
modules. In case of user wants to reserve low address for particular usage, this quirk is useful to load the modules to higher address.
This patch makes the quirk to multiboot2 by:
So, implement the quirk quirk-modules-after-kernel to load modules after the kernel for Multiboot 2 by reusing the implementation for Multiboot (v1).
Quoted 10 lines
- remove "ifndef
GRUB_USE_MULTIBOOT2" that is related to this logic.
- define separate variables for both multiboot and multiboot2, e.g.grub_multiboot_quirks,highest_load, etc.
- ingrub_multiboot2_load(): calculate the highest load address of raw image and assign it togrub_multiboot2_highest_load.
- the existing code inmultiboot_elfxx.cis able to calculate thehighest_loadfor mutiboot2 already.At the end, the adjusted
GRUB_MULTIBOOT(highest_load)is used as the lowest address to allocate memory for loading modules.
Tested with VMware. ;-)
Quoted 170 lines
Signed-off-by Zide Chen <[email protected]>--- 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@@ -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@@ -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@@ -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@@ -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@@ -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 +7
-0@@ -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[]);
include/grub/multiboot2.h grub_multiboot2_load_elf (mbi_load_data_t *mld); · line 112
Quoted 7 lines
include/grub/multiboot2.h +1
-0@@ -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 */
Does this need to be documented somewhere? (Multiboot 2 does not seem to be separately documented in docs/grub.texi, so maybe not.
It’d be great if you added a separate second patch on top, adding a Multiboot 2 example with this quirk to the documentation.
Please post a v3 patch with the improved commit message, and add the line below to it.
Kind regards,
Paul
Hi Paul,
Than k you very much for your comments! Will revise the commit messages base on your comments. I have tested on VirtualBox but will test on VMWare before sending out V3.
Best Regards,
Zide
Quoted 43 lines
-----Original Message-----
From: Paul Menzel <[email protected]>
Sent: Monday, April 6, 2020 1:57 PM
To: Chen, Zide <[email protected]>
Cc: The development of GNU GRUB <[email protected]>
Subject: Re: [PATCHv2] multiboot2: enable quirk-modules-after-kernelDear Zide,
Thank you for posting version 2 of your patch. (Please add v2 to the patch tag next time.)
Maybe use the commit message summary below.
Quoted 1 line
multiboot2: Implement quirk-modules-after-kernel
Am 03.04.20 um 09:35 schrieb Zide Chen:
Quoted 1 line
In multiboot2, currently there is no way to control where to load the
Maybe start with: In contrast to Mulitboot (v1), in Mulitboot 2, there is currently …
Quoted 4 lines
modules. In case of user wants to reserve low address for particular usage, this quirk is useful to load the modules to higher address.
This patch makes the quirk to multiboot2 by:
So, implement the quirk quirk-modules-after-kernel to load modules after the kernel for Multiboot 2 by reusing the implementation for Multiboot (v1).
Quoted 10 lines
- remove "ifndef
GRUB_USE_MULTIBOOT2" that is related to this logic.
- define separate variables for both multiboot and multiboot2, e.g.grub_multiboot_quirks,highest_load, etc.
- ingrub_multiboot2_load(): calculate the highest load address of raw image and assign it togrub_multiboot2_highest_load.
- the existing code inmultiboot_elfxx.cis able to calculate thehighest_loadfor mutiboot2 already.At the end, the adjusted
GRUB_MULTIBOOT(highest_load)is used as the lowest address to allocate memory for loading modules.Tested with VMware. ;-)
Hi Paul,
I installed VMWare ESXi, but realized that it's not booted by GRUB.
I have tested on a KabyLake NUC, using Multiboot2 to load ACRN hypervisor, and confirmed that either the OS kernel is an ELF or raw binary, with this patch, grub_cmd_module () calculates the lowest_addr correctly and allocates higher address for modules as expected:
err = grub_relocator_alloc_chunk_align (GRUB_MULTIBOOT (relocator), &ch,
lowest_addr, (0xffffffff - size) + 1,
size, MULTIBOOT_MOD_ALIGN,
GRUB_RELOCATOR_PREFERENCE_NONE, 1);This patch is supposed to have no impact to existing Multiboot, and I have played on Multiboot for sanity tests as well.
Best Regards,
Zide
Quoted 63 lines
-----Original Message-----
From: Grub-devel <[email protected]> On Behalf Of Chen, Zide
Sent: Monday, April 6, 2020 5:41 PM
To: Paul Menzel <[email protected]>
Cc: The development of GNU GRUB <[email protected]>
Subject: RE: [PATCHv2] multiboot2: enable quirk-modules-after-kernelHi Paul,
Thank you very much for your comments! Will revise the commit messages base on your comments. I have tested on VirtualBox but will test on VMWare before sending out V3.
Best Regards,
ZideQuoted 39 lines
-----Original Message-----
From: Paul Menzel <[email protected]>
Sent: Monday, April 6, 2020 1:57 PM
To: Chen, Zide <[email protected]>
Cc: The development of GNU GRUB <[email protected]>
Subject: Re: [PATCHv2] multiboot2: enable quirk-modules-after-kernelDear Zide,
Thank you for posting version 2 of your patch. (Please add v2 to the patch tag next time.)
Maybe use the commit message summary below.
Quoted 1 line
multiboot2: Implement quirk-modules-after-kernel
Am 03.04.20 um 09:35 schrieb Zide Chen:
Quoted 1 line
In multiboot2, currently there is no way to control where to load the
Maybe start with: In contrast to Mulitboot (v1), in Mulitboot 2, there is currently …
Quoted 4 lines
modules. In case of user wants to reserve low address for particular usage, this quirk is useful to load the modules to higher address.
This patch makes the quirk to multiboot2 by:
So, implement the quirk quirk-modules-after-kernel to load modules after the kernel for Multiboot 2 by reusing the implementation for Multiboot (v1).
Quoted 8 lines
- remove "ifndef
GRUB_USE_MULTIBOOT2" that is related to this logic.
- define separate variables for both multiboot and multiboot2, e.g.grub_multiboot_quirks,highest_load, etc.
- ingrub_multiboot2_load(): calculate the highest load address of raw image and assign it togrub_multiboot2_highest_load.
- the existing code inmultiboot_elfxx.cis able to calculate thehighest_loadfor mutiboot2 already.Quoted 4 lines
Quoted 2 lines
At the end, the adjusted
GRUB_MULTIBOOT(highest_load)is used as the lowest address to allocate memory for loading modules.Tested with VMware. ;-)
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel