Skip to contentSkip to conversation

i386/relocator: Align stack in grub_relocator64_efi relocator

PATCH In review Daniel Kiper 3 participants 6 messages Feb 2, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i386/relocator: Align stack in grub_relocator64_efi relocator

Unified Extensible Firmware Interface Specification, Version 2.6, section 2.3.4, x64 Platforms, boot services, says among others:
The stack must be 16-byte aligned. So, do it. Otherwise OS may boot only by chance as it happens right now.

---
 grub-core/lib/i386/relocator64.S |   16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
grub-core/lib/i386/relocator64.S +13 -3
index 75725cf..148f38a 100644
@@ -73,14 +73,22 @@ VARIABLE(grub_relocator64_rsp)
 
 	movq	%rax, %rsp
 
+#ifdef GRUB_MACHINE_EFI
+	jmp	LOCAL(skip_efi_stack_align)
+
 	/*
-	 * Here is grub_relocator64_efi_start() entry point.
-	 * Following code is shared between grub_relocator64_efi_start()
+	 * Here is grub_relocator64_efi_start() entry point. Most of the
+	 * code below is shared between grub_relocator64_efi_start()
 	 * and grub_relocator64_start().
 	 *
-	 * Think twice before changing anything below!!!
+	 * Think twice before changing anything there!!!
 	 */
 VARIABLE(grub_relocator64_efi_start)
+	/* Align the stack as UEFI spec requires. */
+	andq	$~15, %rsp
+
+LOCAL(skip_efi_stack_align):
+#endif
 	/* mov imm64, %rax */
 	.byte 	0x48
 	.byte	0xb8
@@ -128,8 +136,10 @@ LOCAL(jump_addr):
 VARIABLE(grub_relocator64_rip)
 	.quad	0
 
+#ifdef GRUB_MACHINE_EFI
 	/* Here grub_relocator64_efi_start() ends. Ufff... */
 VARIABLE(grub_relocator64_efi_end)
+#endif
 
 #ifndef __x86_64__
 	.p2align	4
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 25 lines · Daniel Kiper

Unified Extensible Firmware Interface Specification, Version 2.6, section 2.3.4, x64 Platforms, boot services, says among others:
The stack must be 16-byte aligned. So, do it. Otherwise OS may boot only by chance as it happens right now.

---
 grub-core/lib/i386/relocator64.S |   16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
grub-core/lib/i386/relocator64.S +4 -2
index 75725cf..148f38a 100644
@@ -73,14 +73,22 @@ VARIABLE(grub_relocator64_rsp)
 
        movq    %rax, %rsp
 
+#ifdef GRUB_MACHINE_EFI
+       jmp     LOCAL(skip_efi_stack_align)
+
        /*
-        * Here is grub_relocator64_efi_start() entry point.
-        * Following code is shared between grub_relocator64_efi_start()
+        * Here is grub_relocator64_efi_start() entry point. Most of the

grub-core/lib/i386/relocator64.S VARIABLE(grub_relocator64_rsp) · line 89

Quoted 10 lines
grub-core/lib/i386/relocator64.S +5 -1
+        * code below is shared between grub_relocator64_efi_start()
         * and grub_relocator64_start().
         *
-        * Think twice before changing anything below!!!
+        * Think twice before changing anything there!!!
         */
 VARIABLE(grub_relocator64_efi_start)
+       /* Align the stack as UEFI spec requires. */
+       andq    $~15, %rsp
+

It sounds like it should be fixed on caller side instead. I mean, caller allocated some memory, we cannot just arbitrary adjust it now. It sounds wrong.

Quoted 19 lines
grub-core/lib/i386/relocator64.S +4 -1
+LOCAL(skip_efi_stack_align):
+#endif
        /* mov imm64, %rax */
        .byte   0x48
        .byte   0xb8
@@ -128,8 +136,10 @@ LOCAL(jump_addr):
 VARIABLE(grub_relocator64_rip)
        .quad   0
 
+#ifdef GRUB_MACHINE_EFI
        /* Here grub_relocator64_efi_start() ends. Ufff... */
 VARIABLE(grub_relocator64_efi_end)
+#endif
 
 #ifndef __x86_64__
        .p2align        4
--

1.7.10.4

#2 lore raw
Quoted 34 lines · On Thu, 2 Feb 2017, 15:43 Andrei Borzenkov <[email protected]> wrote:

Review

Quoted 11 lines · Daniel Kiper

Unified Extensible Firmware Interface Specification, Version 2.6, section 2.3.4, x64 Platforms, boot services, says among others:
The stack must be 16-byte aligned. So, do it. Otherwise OS may boot only by chance as it happens right now.

---
 grub-core/lib/i386/relocator64.S |   16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/grub-core/lib/i386/relocator64.S

Quoted 20 lines

index 75725cf..148f38a 100644

grub-core/lib/i386/relocator64.S +6 -3
@@ -73,14 +73,22 @@ VARIABLE(grub_relocator64_rsp)
 
        movq    %rax, %rsp
 
+#ifdef GRUB_MACHINE_EFI
+       jmp     LOCAL(skip_efi_stack_align)
+
        /*
-        * Here is grub_relocator64_efi_start() entry point.
-        * Following code is shared between grub_relocator64_efi_start()
+        * Here is grub_relocator64_efi_start() entry point. Most of the
+        * code below is shared between grub_relocator64_efi_start()
         * and grub_relocator64_start().
         *
-        * Think twice before changing anything below!!!
+        * Think twice before changing anything there!!!
         */
Quoted 9 lines

grub-core/lib/i386/relocator64.S VARIABLE(grub_relocator64_rsp) · line 89

Quoted 4 lines
grub-core/lib/i386/relocator64.S +3 -0
 VARIABLE(grub_relocator64_efi_start)
+       /* Align the stack as UEFI spec requires. */
+       andq    $~15, %rsp
+

It sounds like it should be fixed on caller side instead. I mean, caller allocated some memory, we cannot just arbitrary adjust it now. It sounds wrong.

Unlike normal relocator, EFI relocator didn't use .RSP field but instead uses EFI stack, so there is no other place to fix this.

Quoted 21 lines
Quoted 20 lines
grub-core/lib/i386/relocator64.S +4 -1
+LOCAL(skip_efi_stack_align):
+#endif
        /* mov imm64, %rax */
        .byte   0x48
        .byte   0xb8
@@ -128,8 +136,10 @@ LOCAL(jump_addr):
 VARIABLE(grub_relocator64_rip)
        .quad   0
 
+#ifdef GRUB_MACHINE_EFI
        /* Here grub_relocator64_efi_start() ends. Ufff... */
 VARIABLE(grub_relocator64_efi_end)
+#endif
 
 #ifndef __x86_64__
        .p2align        4
--

1.7.10.4

#3 lore raw
Quoted 47 lines · Vladimir 'phcoder' Serbinenko
Quoted 35 lines · On Thu, 2 Feb 2017, 15:43 Andrei Borzenkov <[email protected]> wrote:
Quoted 32 lines · Daniel Kiper

Unified Extensible Firmware Interface Specification, Version 2.6, section 2.3.4, x64 Platforms, boot services, says among others:
The stack must be 16-byte aligned. So, do it. Otherwise OS may boot only by chance as it happens right now.

---
 grub-core/lib/i386/relocator64.S |   16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
grub-core/lib/i386/relocator64.S +6 -3
index 75725cf..148f38a 100644
@@ -73,14 +73,22 @@ VARIABLE(grub_relocator64_rsp)
 
        movq    %rax, %rsp
 
+#ifdef GRUB_MACHINE_EFI
+       jmp     LOCAL(skip_efi_stack_align)
+
        /*
-        * Here is grub_relocator64_efi_start() entry point.
-        * Following code is shared between grub_relocator64_efi_start()
+        * Here is grub_relocator64_efi_start() entry point. Most of the
+        * code below is shared between grub_relocator64_efi_start()
         * and grub_relocator64_start().
         *
-        * Think twice before changing anything below!!!
+        * Think twice before changing anything there!!!
         */
Quoted 8 lines

grub-core/lib/i386/relocator64.S VARIABLE(grub_relocator64_rsp) · line 89

Quoted 4 lines
grub-core/lib/i386/relocator64.S +3 -0
 VARIABLE(grub_relocator64_efi_start)
+       /* Align the stack as UEFI spec requires. */
+       andq    $~15, %rsp
+

It sounds like it should be fixed on caller side instead. I mean, caller allocated some memory, we cannot just arbitrary adjust it now. It sounds wrong.

Quoted 3 lines

Unlike normal relocator, EFI relocator didn't use .RSP field but instead uses EFI stack, so there is no other place to fix this.

Quoted 1 line

OK, I see.

#4 lore raw
In reply to Vladimir 'phcoder' Serbinenko
Quoted 45 lines · Vladimir 'phcoder' Serbinenko
Quoted 34 lines · On Thu, 2 Feb 2017, 15:43 Andrei Borzenkov <[email protected]> wrote:

Review

Quoted 11 lines · Daniel Kiper

Unified Extensible Firmware Interface Specification, Version 2.6, section 2.3.4, x64 Platforms, boot services, says among others:
The stack must be 16-byte aligned. So, do it. Otherwise OS may boot only by chance as it happens right now.

---
 grub-core/lib/i386/relocator64.S |   16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/grub-core/lib/i386/relocator64.S

Quoted 20 lines

index 75725cf..148f38a 100644

grub-core/lib/i386/relocator64.S +6 -3
@@ -73,14 +73,22 @@ VARIABLE(grub_relocator64_rsp)
 
        movq    %rax, %rsp
 
+#ifdef GRUB_MACHINE_EFI
+       jmp     LOCAL(skip_efi_stack_align)
+
        /*
-        * Here is grub_relocator64_efi_start() entry point.
-        * Following code is shared between grub_relocator64_efi_start()
+        * Here is grub_relocator64_efi_start() entry point. Most of the
+        * code below is shared between grub_relocator64_efi_start()
         * and grub_relocator64_start().
         *
-        * Think twice before changing anything below!!!
+        * Think twice before changing anything there!!!
         */
Quoted 9 lines

grub-core/lib/i386/relocator64.S VARIABLE(grub_relocator64_rsp) · line 89

Quoted 4 lines
grub-core/lib/i386/relocator64.S +3 -0
 VARIABLE(grub_relocator64_efi_start)
+       /* Align the stack as UEFI spec requires. */
+       andq    $~15, %rsp
+

It sounds like it should be fixed on caller side instead. I mean, caller allocated some memory, we cannot just arbitrary adjust it now. It sounds wrong.

Quoted 2 lines

Unlike normal relocator, EFI relocator didn't use .RSP field but instead uses EFI stack, so there is no other place to fix this.

The other option is call instead of jmp below but it seems to me a less reliable thing.

Daniel

#5 lore raw
In reply to Daniel Kiper
Quoted 6 lines · Daniel Kiper

Unified Extensible Firmware Interface Specification, Version 2.6, section 2.3.4, x64 Platforms, boot services, says among others:
The stack must be 16-byte aligned. So, do it. Otherwise OS may boot only by chance as it happens right now.

Commited!

Daniel