i386: Using 64-bit boot protocol for 64-bit linux kernel
PATCH v2 1/1 i386: Using 64-bit boot protocol for 64-bit linux kernel
From: Wei Zhang <[email protected]>
Currently GRUB boots linux with 32-bit protocol for 64 bit kernel. Thus if both GRUB and linux kernel are in 64-bit, we'll have to go through 64-bit grub -> 32-bit boot protocol -> 64-bit kernel transitions, and extra instructions have to be executed in the kernel.
Since linux has long ago supported 64-bit boot protocol, we can take advantage of that to directly boot to 64-bit kernel.
To do this, first we determine whether the kernel is 64-bit by xloadflags (since linux boot protocol 2.12), then we build the identity-mapped page table required by the 64-bit kernel, and that's it. The memory needed by the page table is allocated after the protected kernel image proper.
So if we're in 32-bit GRUB to boot a 64-bit kernel, the transition will happen before handing over to the kernel, and if we're in 64-bit GRUB, we don't have to go down to 32-bit and back to 64-bit. The 32-bit kernel boot process will not be affected.
Tested on my 64-bit machine and QEMU.
--- grub-core/loader/i386/linux.c | 93 ++++++++++++++++++++++++++++++++--- include/grub/i386/linux.h | 2 +- 2 files changed, 86 insertions(+), 9 deletions(-)
grub-core/loader/i386/linux.c +85 -8
@@ -63,12 +63,24 @@ GRUB_MOD_LICENSE ("GPLv3+");#define ACCEPTS_PURE_TEXT 1#endif +#define PG_P 0x001 +#define PG_RW 0x002 +#define PG_US 0x004 +#define PG_PS 0x080 +#define PG_G 0x100 +#define LINUX_KERNEL_64 0x0001 +#define LINUX_STARTUP_64 0x200 +#define LINUX_PGT_SIZE_64 (6 << 12) +static grub_dl_t my_mod;static grub_size_t linux_mem_size;static int loaded;static void *prot_mode_mem;static grub_addr_t prot_mode_target; +static int is_64bit =0 ; +static grub_uint8_t *pgtable; +static grub_addr_t pgtable_target;static void *initrd_mem;static grub_addr_t initrd_mem_target;static grub_size_t prot_init_space;
@@ -199,6 +211,11 @@ allocate_pages (grub_size_t prot_size, grub_size_t *align, goto fail; prot_mode_mem =get_virtual_current_address (ch); prot_mode_target =get_physical_target_address (ch); +if (is_64bit) + { + pgtable = (grub_uint8_t *)((grub_addr_t ) prot_mode_mem + prot_size -LINUX_PGT_SIZE_64 ); + pgtable_target = prot_mode_target + prot_size -LINUX_PGT_SIZE_64 ; + } } grub_dprintf ("linux", "prot_mode_mem = %p, prot_mode_target = %lx, prot_size = %x\n",
@@ -398,13 +415,52 @@ grub_linux_boot_mmap_fill (grub_uint64_t addr, grub_uint64_t size,return 0 ; } +/* Fill linux identity map page table. */ +static void +grub_fill_linux64_pgtable (void ) +{ +grub_uint64_t *p4d, *pud, *pmd; +grub_addr_t pudt, pmdt; +int i; + + p4d = (grub_uint64_t *) pgtable; + pud = (grub_uint64_t *) (pgtable +0x1000 ); + pmd = (grub_uint64_t *) (pgtable +0x2000 ); + + pudt = pgtable_target +0x1000 ; + pmdt = pgtable_target +0x2000 ; + +grub_memset (pgtable,0 ,LINUX_PGT_SIZE_64 ); + +/* First entry for the 3rd level page */ + p4d[0 ] = (grub_addr_t) (pudt); + p4d[0 ] |= (PG_P |PG_RW |PG_US ); + +/* Map the whole 4G physical address */ +for (i =0 ; i <4 ; i++) + { + pud[i] = (grub_addr_t) (pmdt + i *0x1000 ); + pud[i] |= (PG_P |PG_RW |PG_US ); + } + +/* Identity map with 2MB pages */ +for (i =0 ; i <2048 ; i++) + { + pmd[i] = (grub_addr_t) (i) *0x200000 ; + pmd[i] |= (PG_P |PG_RW |PG_PS |PG_G ); + } +} +static grub_err_t grub_linux_boot (void ) {grub_err_t err =0 ;const char *modevar;char *tmp; -struct grub_relocator32_state state; +union { +struct grub_relocator32_state _32; +struct grub_relocator64_state _64; + } state;void *real_mode_mem;struct grub_linux_boot_ctx ctx = { .real_mode_target =0
@@ -624,13 +680,24 @@ grub_linux_boot (void) }#endif -/* FIXME. */ -/* asm volatile ("lidt %0" : : "m" (idt_desc)); */ - state.ebp = state.edi = state.ebx =0 ; - state.esi = ctx.real_mode_target; - state.esp = ctx.real_mode_target; - state.eip = ctx.params->code32_start; -return grub_relocator32_boot (relocator, state,0 ); +if (is_64bit) + { + state._64.rsi = ctx.real_mode_target; + state._64.rip = ctx.params->code32_start +LINUX_STARTUP_64 ; +grub_fill_linux64_pgtable (); + state._64.cr3 = (grub_addr_t ) pgtable_target; +return grub_relocator64_boot (relocator, state._64,0x1000 ,0x9a000 ); + } +else + { +/* FIXME. */ +/* asm volatile ("lidt %0" : : "m" (idt_desc)); */ + state._32.ebp = state._32.edi = state._32.ebx =0 ; + state._32.esi = ctx.real_mode_target; + state._32.esp = ctx.real_mode_target; + state._32.eip = ctx.params->code32_start; +return grub_relocator32_boot (relocator, state._32,0 ); + } } static grub_err_t
@@ -748,6 +815,16 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), { min_align = lh.min_alignment; prot_size =grub_le_to_cpu32 (lh.init_size); +if (grub_le_to_cpu16 (lh.version) >=0x020c + && (lh.xloadflags &LINUX_KERNEL_64 )) + { + is_64bit =1 ; + prot_size +=LINUX_PGT_SIZE_64 ; +grub_dprintf ("linux" ,"using 64-bit boot protocol, " +"extra %d bytes will be allocated\n" ,LINUX_PGT_SIZE_64 ); + } +else + is_64bit =0 ; prot_init_space =page_align (prot_size);if (relocatable) preferred_address =grub_le_to_cpu64 (lh.pref_address);
include/grub/i386/linux.h +1 -1
@@ -138,7 +138,7 @@ struct linux_i386_kernel_headergrub_uint32_t kernel_alignment;grub_uint8_t relocatable;grub_uint8_t min_alignment; -grub_uint8_t pad[2 ]; +grub_uint16_t xloadflags;grub_uint32_t cmdline_size;grub_uint32_t hardware_subarch;grub_uint64_t hardware_subarch_data;
Signature · 1 line
2.34.1
Quoted 16 lines · Wei Zhang
From: Wei Zhang <[email protected]>
Currently GRUB boots linux with 32-bit protocol for 64 bit kernel. Thus if both GRUB and linux kernel are in 64-bit, we'll have to go through 64-bit grub -> 32-bit boot protocol -> 64-bit kernel transitions, and extra instructions have to be executed in the kernel.
Since linux has long ago supported 64-bit boot protocol, we can take advantage of that to directly boot to 64-bit kernel.
To do this, first we determine whether the kernel is 64-bit by xloadflags (since linux boot protocol 2.12), then we build the identity-mapped page table required by the 64-bit kernel, and that's it. The memory needed by the page table is allocated after the protected kernel image proper.
Quoted 6 lines
So if we're in 32-bit GRUB to boot a 64-bit kernel, the transition will happen before handing over to the kernel, and if we're in 64-bit GRUB, we don't have to go down to 32-bit and back to 64-bit. The 32-bit kernel boot process will not be affected.
Tested on my 64-bit machine and QEMU.
Could you tell us what target and platform you configure for the GRUB? Or send us full GRUB configure command you use?
Daniel
Quoted 23 lines · Daniel Kiper
Quoted 21 lines · Wei Zhang
From: Wei Zhang <[email protected]>
Currently GRUB boots linux with 32-bit protocol for 64 bit kernel. Thus if both GRUB and linux kernel are in 64-bit, we'll have to go through 64-bit grub -> 32-bit boot protocol -> 64-bit kernel transitions, and extra instructions have to be executed in the kernel.
Since linux has long ago supported 64-bit boot protocol, we can take advantage of that to directly boot to 64-bit kernel.
To do this, first we determine whether the kernel is 64-bit by xloadflags (since linux boot protocol 2.12), then we build the identity-mapped page table required by the 64-bit kernel, and that's it. The memory needed by the page table is allocated after the protected kernel image proper.
So if we're in 32-bit GRUB to boot a 64-bit kernel, the transition will happen before handing over to the kernel, and if we're in 64-bit GRUB, we don't have to go down to 32-bit and back to 64-bit. The 32-bit kernel boot process will not be affected.
Quoted 4 lines
Quoted 1 line
Tested on my 64-bit machine and QEMU.
Could you tell us what target and platform you configure for the GRUB? Or send us full GRUB configure command you use?
Sure, I mainly focused on x86, and my configure command is:
./configure --target=x86_64 --with-platform=efi
I didn't examine other targets, e.g. arm64 and sparc64 etc. But if there are similar issues on those targets too, I'll definitely like to work on those too:)
Thanks for your reply.
Wei Zhang
Quoted 24 lines · Wei Zhang
Quoted 23 lines · Daniel Kiper
Quoted 21 lines · Wei Zhang
From: Wei Zhang <[email protected]>
Currently GRUB boots linux with 32-bit protocol for 64 bit kernel. Thus if both GRUB and linux kernel are in 64-bit, we'll have to go through 64-bit grub -> 32-bit boot protocol -> 64-bit kernel transitions, and extra instructions have to be executed in the kernel.
Since linux has long ago supported 64-bit boot protocol, we can take advantage of that to directly boot to 64-bit kernel.
To do this, first we determine whether the kernel is 64-bit by xloadflags (since linux boot protocol 2.12), then we build the identity-mapped page table required by the 64-bit kernel, and that's it. The memory needed by the page table is allocated after the protected kernel image proper.
So if we're in 32-bit GRUB to boot a 64-bit kernel, the transition will happen before handing over to the kernel, and if we're in 64-bit GRUB, we don't have to go down to 32-bit and back to 64-bit. The 32-bit kernel boot process will not be affected.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Tested on my 64-bit machine and QEMU.
Could you tell us what target and platform you configure for the GRUB? Or send us full GRUB configure command you use?
Sure, I mainly focused on x86, and my configure command is:
./configure --target=x86_64 --with-platform=efi
This is what I expected. I appreciate your work. Sadly this is not a direction which we are heading. So, I have to NAK your patch. However, if you want to improve the EFI boot protocol in the GRUB please take a look at [1] and my comments starting at [2]. I would really want to see the patch set at [1] updated and at some point merged into the GRUB upstream.
If you have any questions please drop us a line.
Daniel
[1] https://lists.gnu.org/archive/html/grub-devel/2021-10/msg00209.html
[2] https://lists.gnu.org/archive/html/grub-devel/2021-11/msg00084.html
Quoted 28 lines · Daniel Kiper
Quoted 26 lines · Wei Zhang
Quoted 25 lines · Daniel Kiper
Quoted 22 lines · Wei Zhang
From: Wei Zhang <[email protected]>
Currently GRUB boots linux with 32-bit protocol for 64 bit kernel. Thus if both GRUB and linux kernel are in 64-bit, we'll have to go through 64-bit grub -> 32-bit boot protocol -> 64-bit kernel transitions, and extra instructions have to be executed in the kernel.
Since linux has long ago supported 64-bit boot protocol, we can take advantage of that to directly boot to 64-bit kernel.
To do this, first we determine whether the kernel is 64-bit by xloadflags (since linux boot protocol 2.12), then we build the identity-mapped page table required by the 64-bit kernel, and that's it. The memory needed by the page table is allocated after the protected kernel image proper.
So if we're in 32-bit GRUB to boot a 64-bit kernel, the transition will happen before handing over to the kernel, and if we're in 64-bit GRUB, we don't have to go down to 32-bit and back to 64-bit. The 32-bit kernel boot process will not be affected.
Tested on my 64-bit machine and QEMU.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Could you tell us what target and platform you configure for the GRUB? Or send us full GRUB configure command you use?
Sure, I mainly focused on x86, and my configure command is:
./configure --target=x86_64 --with-platform=efiThis is what I expected. I appreciate your work. Sadly this is not a direction which we are heading. So, I have to NAK your patch.
That's OK. This is just a random thought I had when I was going through the kernel boot code, and I'm happy that you took a look.
Quoted 5 lines
However,
if you want to improve the EFI boot protocol in the GRUB please take a look at [1] and my comments starting at [2]. I would really want to see the patch set at [1] updated and at some point merged into the GRUB upstream.
I haven't looked into it, but I'll take it if it's not too far beyond my scope:)
Quoted 7 lines
If you have any questions please drop us a line.
Daniel
[1] https://lists.gnu.org/archive/html/grub-devel/2021-10/msg00209.html
[2] https://lists.gnu.org/archive/html/grub-devel/2021-11/msg00084.html
Wei Zhang
Quoted 39 lines · Wei Zhang
Quoted 26 lines · Daniel Kiper
Quoted 25 lines · Wei Zhang
Quoted 24 lines · Daniel Kiper
Quoted 22 lines · Wei Zhang
From: Wei Zhang <[email protected]>
Currently GRUB boots linux with 32-bit protocol for 64 bit kernel. Thus if both GRUB and linux kernel are in 64-bit, we'll have to go through 64-bit grub -> 32-bit boot protocol -> 64-bit kernel transitions, and extra instructions have to be executed in the kernel.
Since linux has long ago supported 64-bit boot protocol, we can take advantage of that to directly boot to 64-bit kernel.
To do this, first we determine whether the kernel is 64-bit by xloadflags (since linux boot protocol 2.12), then we build the identity-mapped page table required by the 64-bit kernel, and that's it. The memory needed by the page table is allocated after the protected kernel image proper.
So if we're in 32-bit GRUB to boot a 64-bit kernel, the transition will happen before handing over to the kernel, and if we're in 64-bit GRUB, we don't have to go down to 32-bit and back to 64-bit. The 32-bit kernel boot process will not be affected.
Tested on my 64-bit machine and QEMU.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Could you tell us what target and platform you configure for the GRUB? Or send us full GRUB configure command you use?
Sure, I mainly focused on x86, and my configure command is:
./configure --target=x86_64 --with-platform=efiThis is what I expected. I appreciate your work. Sadly this is not a direction which we are heading. So, I have to NAK your patch.
That's OK. This is just a random thought I had when I was going through the kernel boot code, and I'm happy that you took a look.
Quoted 7 lines
Quoted 5 lines
However,
if you want to improve the EFI boot protocol in the GRUB please take a look at [1] and my comments starting at [2]. I would really want to see the patch set at [1] updated and at some point merged into the GRUB upstream.I haven't looked into it, but I'll take it if it's not too far beyond my scope:)
FYI, Ard reposted it [1].
Daniel
[1] https://lists.gnu.org/archive/html/grub-devel/2022-08/msg00187.html
Quoted 56 lines · Daniel Kiper <[email protected]>于2022年8月20日 周六19:01写道:
Quoted 1 line · Wei Zhang
On Fri, Aug 12, 2022 at 1:18 AM Daniel Kiper <[email protected]>
Quoted 2 lines · wrote:
Quoted 2 lines · Wei Zhang
Quoted 1 line
On Wed, Aug 3, 2022 at 11:33 PM Daniel Kiper <[email protected]>
Quoted 4 lines · wrote:
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines · Wei Zhang
Quoted 3 lines
From: Wei Zhang <[email protected]>
Currently GRUB boots linux with 32-bit protocol for 64 bit
kernel.
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Thus if both GRUB and linux kernel are in 64-bit, we'll have to
go
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
through 64-bit grub -> 32-bit boot protocol -> 64-bit kernel transitions, and extra instructions have to be executed in the
kernel.
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Since linux has long ago supported 64-bit boot protocol, we can
take
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
advantage of that to directly boot to 64-bit kernel.
To do this, first we determine whether the kernel is 64-bit by xloadflags (since linux boot protocol 2.12), then we build the identity-mapped page table required by the 64-bit kernel, and
that's
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
it. The memory needed by the page table is allocated after the protected kernel image proper.
So if we're in 32-bit GRUB to boot a 64-bit kernel, the
transition
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
will happen before handing over to the kernel, and if we're in
64-bit
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
GRUB, we don't have to go down to 32-bit and back to 64-bit. The 32-bit kernel boot process will not be affected.
Tested on my 64-bit machine and QEMU.
Could you tell us what target and platform you configure for the
GRUB?
Quoted 17 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Or send us full GRUB configure command you use?
Sure, I mainly focused on x86, and my configure command is:
./configure --target=x86_64 --with-platform=efiThis is what I expected. I appreciate your work. Sadly this is not a direction which we are heading. So, I have to NAK your patch.
That's OK. This is just a random thought I had when I was going through the kernel boot code, and I'm happy that you took a look.
Quoted 5 lines
However,
if you want to improve the EFI boot protocol in the GRUB please take a look at [1] and my comments starting at [2]. I would really want to see the patch set at [1] updated and at some point merged into the GRUB upstream.
Quoted 8 lines
Quoted 1 line
I haven't looked into it, but I'll take it if it's not too far beyond my
scope:)
FYI, Ard reposted it [1].
Daniel
[1] https://lists.gnu.org/archive/html/grub-devel/2022-08/msg00187.html
<https://lists.gnu.org/archive/html/grub-devel/2022-08/msg00187.html>
Quoted 1 line
Right, I saw it last night too, thanks.
Signature · 1 line
Wei Zhang