Skip to contentSkip to conversation

i386-efi and x86_64-efi fixes

PATCH In review Josh Triplett 4 participants 14 messages Nov 13, 2013 – Nov 19, 2013

lore
Share & tools
lore
#0 lore raw

cover i386-efi and x86_64-efi fixes

The first three patches improve the EFI firmware allocator, fixing some bugs in the process.

The last patch fixes many compilation-specific and firmware-specific crashes.

To avoid unnecessary patch conflicts, I've included appropriately formatted ChangeLog entries for each patch separately in each mail, below the commit messages and above the diffstats.

Josh Triplett (4):
efi: Fix firmware memory allocation to round to 4k pages, not 1k
efi: Fix requests to allocate GRUB_MEMORY_AVAILABLE
efi: Support GRUB_MMAP_MALLOC_LOW in the EFI firmware allocator
efi: On x86-64, align the stack to a 16-byte boundary as required by
ABI

 grub-core/kern/x86_64/efi/startup.S |  6 +++++-
 grub-core/mmap/efi/mmap.c           | 23 ++++++++++++++---------
 include/grub/efi/memory.h           |  2 ++
 3 files changed, 21 insertions(+), 10 deletions(-)
Signature · 1 line
1.8.4.3
#1 lore raw

PATCH 1/4 efi: Fix firmware memory allocation to round to 4k pages, not 1k

EFI memory allocation operates in 4k pages, but the firmware memory allocator used 0x3ff in several places rather than 0xfff.
---

ChangeLog entry:

2013-11-13 Josh Triplett <[email protected]>

* grub-core/mmap/efi/mmap.c (grub_mmap_register): Round up/down to 4k page boundaries as expected by firmware rather than 1k boundaries.
(grub_mmap_malign_and_register): Likewise.

 grub-core/mmap/efi/mmap.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
grub-core/mmap/efi/mmap.c +3 -3
index 4f17c8b..a77efe8 100644
@@ -184,8 +184,8 @@ grub_mmap_register (grub_uint64_t start, grub_uint64_t size, int type)
     return 0;
 
   b = grub_efi_system_table->boot_services;
-  address = start & (~0x3ffULL);
-  pages = (end - address  + 0x3ff) >> 12;
+  address = start & (~0xfffULL);
+  pages = (end - address + 0xfff) >> 12;
   status = efi_call_2 (b->free_pages, address, pages);
   if (status != GRUB_EFI_SUCCESS && status != GRUB_EFI_NOT_FOUND)
     {
@@ -263,7 +263,7 @@ grub_mmap_malign_and_register (grub_uint64_t align __attribute__ ((unused)),
   atype = GRUB_EFI_ALLOCATE_ANY_PAGES;
 #endif
 
-  pages = (size + 0x3ff) >> 12;
+  pages = (size + 0xfff) >> 12;
   status = efi_call_4 (b->allocate_pages, atype,
 		       make_efi_memtype (type), pages, &address);
   if (status != GRUB_EFI_SUCCESS)
Signature · 1 line
1.8.4.3
#2 lore raw

Re: [PATCH 1/4] efi: Fix firmware memory allocation to round to 4k pages, not 1k

Quoted 6 lines · Josh Triplett

2013-11-13 Josh Triplett <[email protected]>

* grub-core/mmap/efi/mmap.c (grub_mmap_register): Round up/down to 4k page boundaries as expected by firmware rather than 1k boundaries.
(grub_mmap_malign_and_register): Likewise.

Committed, thanks.

#3 lore raw

PATCH 2/4 efi: Fix requests to allocate GRUB_MEMORY_AVAILABLE

In reply to Josh Triplett

EFI firmware refuses to allocate memory of type GRUB_EFI_CONVENTIONAL_MEMORY, because that indicates a block of available memory that other allocations (or the OS) would then step on. Map GRUB_MEMORY_AVAILABLE to GRUB_EFI_LOADER_CODE instead.
---

ChangeLog entry:

2013-11-13 Josh Triplett <[email protected]>

* grub-core/mmap/efi/mmap.c (make_efi_memtype): Map GRUB_MEMORY_AVAILABLE to GRUB_EFI_LOADER_CODE rather than GRUB_EFI_CONVENTIONAL_MEMORY. EFI firmware refuses to allocate memory of type GRUB_EFI_CONVENTIONAL_MEMORY, because that indicates a block of available memory that other allocations (or the OS) would then step on.

 grub-core/mmap/efi/mmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/mmap/efi/mmap.c +1 -1
index a77efe8..e6cd185 100644
@@ -146,7 +146,7 @@ make_efi_memtype (int type)
       return GRUB_EFI_UNUSABLE_MEMORY;
 
     case GRUB_MEMORY_AVAILABLE:
-      return GRUB_EFI_CONVENTIONAL_MEMORY;
+      return GRUB_EFI_LOADER_CODE;
 
     case GRUB_MEMORY_ACPI:
       return GRUB_EFI_ACPI_RECLAIM_MEMORY;
Signature · 1 line
1.8.4.3
#4 lore raw

Re: [PATCH 2/4] efi: Fix requests to allocate GRUB_MEMORY_AVAILABLE

Quoted 8 lines · Josh Triplett

EFI firmware refuses to allocate memory of type GRUB_EFI_CONVENTIONAL_MEMORY, because that indicates a block of available memory that other allocations (or the OS) would then step on. Map GRUB_MEMORY_AVAILABLE to GRUB_EFI_LOADER_CODE instead.
---

ChangeLog entry:

Quoted 9 lines

2013-11-13 Josh Triplett <[email protected]>

* grub-core/mmap/efi/mmap.c (make_efi_memtype): Map GRUB_MEMORY_AVAILABLE to GRUB_EFI_LOADER_CODE rather than GRUB_EFI_CONVENTIONAL_MEMORY. EFI firmware refuses to allocate memory of type GRUB_EFI_CONVENTIONAL_MEMORY, because that indicates a block of available memory that other allocations (or the OS) would then step on.

"allocating" with type GRUB_MEMORY_AVAILABLE isn't actually allocating but declaring a new chunk of available memory not discovered by firmware. Up until now we've never actually needed it

Quoted 17 lines
 grub-core/mmap/efi/mmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/mmap/efi/mmap.c +1 -1
index a77efe8..e6cd185 100644
@@ -146,7 +146,7 @@ make_efi_memtype (int type)
       return GRUB_EFI_UNUSABLE_MEMORY;
 
     case GRUB_MEMORY_AVAILABLE:
-      return GRUB_EFI_CONVENTIONAL_MEMORY;
+      return GRUB_EFI_LOADER_CODE;
 
     case GRUB_MEMORY_ACPI:
       return GRUB_EFI_ACPI_RECLAIM_MEMORY;
#5 lore raw

PATCH 3/4 efi: Support GRUB_MMAP_MALLOC_LOW in the EFI firmware allocator

In reply to Josh Triplett

EFI supports allocating memory below a specified address; use that to implement GRUB_MMAP_MALLOC_LOW by requesting memory below 1M.
---

ChangeLog entry:

2013-11-13 Josh Triplett <[email protected]>

* include/grub/efi/memory.h (GRUB_MMAP_MALLOC_LOW): Define.
* grub-core/mmap/efi/mmap.c (grub_mmap_malign_and_register): Add support for GRUB_MMAP_MALLOC_LOW, to allocate memory below 1M via the EFI firmware.

 grub-core/mmap/efi/mmap.c | 15 ++++++++++-----
 include/grub/efi/memory.h |  2 ++
 2 files changed, 12 insertions(+), 5 deletions(-)
grub-core/mmap/efi/mmap.c +10 -5
index e6cd185..64ad05c 100644
@@ -239,9 +239,9 @@ void *
 grub_mmap_malign_and_register (grub_uint64_t align __attribute__ ((unused)),
 			       grub_uint64_t size,
 			       int *handle, int type,
-			       int flags __attribute__ ((unused)))
+			       int flags)
 {
-  grub_efi_physical_address_t address;
+  grub_efi_physical_address_t address, max_address;
   grub_efi_boot_services_t *b;
   grub_efi_uintn_t pages;
   grub_efi_status_t status;
@@ -254,13 +254,18 @@ grub_mmap_malign_and_register (grub_uint64_t align __attribute__ ((unused)),
 
   b = grub_efi_system_table->boot_services;
 
-  address = 0xffffffff;
+  if (flags & GRUB_MMAP_MALLOC_LOW)
+      max_address = 0xfffff;
+  else
+      max_address = 0xffffffff;
+  address = max_address;
 
 #if GRUB_TARGET_SIZEOF_VOID_P < 8
   /* Limit the memory access to less than 4GB for 32-bit platforms.  */
   atype = GRUB_EFI_ALLOCATE_MAX_ADDRESS;
 #else
-  atype = GRUB_EFI_ALLOCATE_ANY_PAGES;
+  atype = (flags & GRUB_MMAP_MALLOC_LOW) ? GRUB_EFI_ALLOCATE_MAX_ADDRESS
+                                         : GRUB_EFI_ALLOCATE_ANY_PAGES;
 #endif
 
   pages = (size + 0xfff) >> 12;
@@ -276,7 +281,7 @@ grub_mmap_malign_and_register (grub_uint64_t align __attribute__ ((unused)),
     {
       /* Uggh, the address 0 was allocated... This is too annoying,
 	 so reallocate another one.  */
-      address = 0xffffffff;
+      address = max_address;
       status = efi_call_4 (b->allocate_pages, atype,
 			   make_efi_memtype (type), pages, &address);
       grub_efi_free_pages (0, pages);
include/grub/efi/memory.h +2 -0
index 20526b1..b4940af 100644
@@ -24,6 +24,8 @@
 
 #define GRUB_MMAP_REGISTER_BY_FIRMWARE  1
 
+#define GRUB_MMAP_MALLOC_LOW 1
+
 grub_err_t grub_machine_mmap_register (grub_uint64_t start, grub_uint64_t size,
 				       int type, int handle);
 grub_err_t grub_machine_mmap_unregister (int handle);
Signature · 1 line
1.8.4.3
#6 lore raw

Re: [PATCH 3/4] efi: Support GRUB_MMAP_MALLOC_LOW in the EFI firmware allocator

В Tue, 12 Nov 2013 18:26:39 -0800
Josh Triplett <[email protected]> пишет:

Quoted 2 lines

EFI supports allocating memory below a specified address; use that to implement GRUB_MMAP_MALLOC_LOW by requesting memory below 1M.

Out of curiosity - why would you need it on EFI? Your patch does not add any consumer of GRUB_MMAP_MALLOC_LOW and on EFI notion of low memory should not exist?

Quoted 74 lines

---

ChangeLog entry:

2013-11-13 Josh Triplett <[email protected]>

* include/grub/efi/memory.h (GRUB_MMAP_MALLOC_LOW): Define.
* grub-core/mmap/efi/mmap.c (grub_mmap_malign_and_register): Add support for GRUB_MMAP_MALLOC_LOW, to allocate memory below 1M via the EFI firmware.

 grub-core/mmap/efi/mmap.c | 15 ++++++++++-----
 include/grub/efi/memory.h |  2 ++
 2 files changed, 12 insertions(+), 5 deletions(-)
grub-core/mmap/efi/mmap.c +10 -5
index e6cd185..64ad05c 100644
@@ -239,9 +239,9 @@ void *
 grub_mmap_malign_and_register (grub_uint64_t align __attribute__ ((unused)),
 			       grub_uint64_t size,
 			       int *handle, int type,
-			       int flags __attribute__ ((unused)))
+			       int flags)
 {
-  grub_efi_physical_address_t address;
+  grub_efi_physical_address_t address, max_address;
   grub_efi_boot_services_t *b;
   grub_efi_uintn_t pages;
   grub_efi_status_t status;
@@ -254,13 +254,18 @@ grub_mmap_malign_and_register (grub_uint64_t align __attribute__ ((unused)),
 
   b = grub_efi_system_table->boot_services;
 
-  address = 0xffffffff;
+  if (flags & GRUB_MMAP_MALLOC_LOW)
+      max_address = 0xfffff;
+  else
+      max_address = 0xffffffff;
+  address = max_address;
 
 #if GRUB_TARGET_SIZEOF_VOID_P < 8
   /* Limit the memory access to less than 4GB for 32-bit platforms.  */
   atype = GRUB_EFI_ALLOCATE_MAX_ADDRESS;
 #else
-  atype = GRUB_EFI_ALLOCATE_ANY_PAGES;
+  atype = (flags & GRUB_MMAP_MALLOC_LOW) ? GRUB_EFI_ALLOCATE_MAX_ADDRESS
+                                         : GRUB_EFI_ALLOCATE_ANY_PAGES;
 #endif
 
   pages = (size + 0xfff) >> 12;
@@ -276,7 +281,7 @@ grub_mmap_malign_and_register (grub_uint64_t align __attribute__ ((unused)),
     {
       /* Uggh, the address 0 was allocated... This is too annoying,
 	 so reallocate another one.  */
-      address = 0xffffffff;
+      address = max_address;
       status = efi_call_4 (b->allocate_pages, atype,
 			   make_efi_memtype (type), pages, &address);
       grub_efi_free_pages (0, pages);
include/grub/efi/memory.h +2 -0
index 20526b1..b4940af 100644
@@ -24,6 +24,8 @@
 
 #define GRUB_MMAP_REGISTER_BY_FIRMWARE  1
 
+#define GRUB_MMAP_MALLOC_LOW 1
+
 grub_err_t grub_machine_mmap_register (grub_uint64_t start, grub_uint64_t size,
 				       int type, int handle);
 grub_err_t grub_machine_mmap_unregister (int handle);
#7 lore raw

Re: [PATCH 3/4] efi: Support GRUB_MMAP_MALLOC_LOW in the EFI firmware allocator

Quoted 10 lines · Andrey Borzenkov

В Tue, 12 Nov 2013 18:26:39 -0800
Josh Triplett <[email protected]> пишет:

Quoted 2 lines

EFI supports allocating memory below a specified address; use that to implement GRUB_MMAP_MALLOC_LOW by requesting memory below 1M.

Out of curiosity - why would you need it on EFI? Your patch does not add any consumer of GRUB_MMAP_MALLOC_LOW and on EFI notion of low memory should not exist?

As discussed on IRC, it's for starting other CPUs, for this you need a vector in low memory as cpus always start in real mode.

#8 lore raw

Re: [PATCH 3/4] efi: Support GRUB_MMAP_MALLOC_LOW in the EFI firmware allocator

In reply to Josh Triplett
Quoted 2 lines · Josh Triplett

EFI supports allocating memory below a specified address; use that to implement GRUB_MMAP_MALLOC_LOW by requesting memory below 1M.

As discussed on the IRC, using mmap isn't right way to allocate transient low memory space. This would merit a separate functions (alloc_low_page and free_low_page) which uses EFI calls on EFI and returns the scratch address GRUB_MEMORY_MACHINE_SCRATCH_ADDR on other platforms. These would be subjected to following rules:
- Always gives only 4k
- Consumer has to ensure the transient nature of allocation since low memory is scarce.

Quoted 75 lines

---

ChangeLog entry:

2013-11-13 Josh Triplett <[email protected]>

* include/grub/efi/memory.h (GRUB_MMAP_MALLOC_LOW): Define.
* grub-core/mmap/efi/mmap.c (grub_mmap_malign_and_register): Add support for GRUB_MMAP_MALLOC_LOW, to allocate memory below 1M via the EFI firmware.

 grub-core/mmap/efi/mmap.c | 15 ++++++++++-----
 include/grub/efi/memory.h |  2 ++
 2 files changed, 12 insertions(+), 5 deletions(-)
grub-core/mmap/efi/mmap.c +10 -5
index e6cd185..64ad05c 100644
@@ -239,9 +239,9 @@ void *
 grub_mmap_malign_and_register (grub_uint64_t align __attribute__ ((unused)),
 			       grub_uint64_t size,
 			       int *handle, int type,
-			       int flags __attribute__ ((unused)))
+			       int flags)
 {
-  grub_efi_physical_address_t address;
+  grub_efi_physical_address_t address, max_address;
   grub_efi_boot_services_t *b;
   grub_efi_uintn_t pages;
   grub_efi_status_t status;
@@ -254,13 +254,18 @@ grub_mmap_malign_and_register (grub_uint64_t align __attribute__ ((unused)),
 
   b = grub_efi_system_table->boot_services;
 
-  address = 0xffffffff;
+  if (flags & GRUB_MMAP_MALLOC_LOW)
+      max_address = 0xfffff;
+  else
+      max_address = 0xffffffff;
+  address = max_address;
 
 #if GRUB_TARGET_SIZEOF_VOID_P < 8
   /* Limit the memory access to less than 4GB for 32-bit platforms.  */
   atype = GRUB_EFI_ALLOCATE_MAX_ADDRESS;
 #else
-  atype = GRUB_EFI_ALLOCATE_ANY_PAGES;
+  atype = (flags & GRUB_MMAP_MALLOC_LOW) ? GRUB_EFI_ALLOCATE_MAX_ADDRESS
+                                         : GRUB_EFI_ALLOCATE_ANY_PAGES;
 #endif
 
   pages = (size + 0xfff) >> 12;
@@ -276,7 +281,7 @@ grub_mmap_malign_and_register (grub_uint64_t align __attribute__ ((unused)),
     {
       /* Uggh, the address 0 was allocated... This is too annoying,
 	 so reallocate another one.  */
-      address = 0xffffffff;
+      address = max_address;
       status = efi_call_4 (b->allocate_pages, atype,
 			   make_efi_memtype (type), pages, &address);
       grub_efi_free_pages (0, pages);
include/grub/efi/memory.h +2 -0
index 20526b1..b4940af 100644
@@ -24,6 +24,8 @@
 
 #define GRUB_MMAP_REGISTER_BY_FIRMWARE  1
 
+#define GRUB_MMAP_MALLOC_LOW 1
+
 grub_err_t grub_machine_mmap_register (grub_uint64_t start, grub_uint64_t size,
 				       int type, int handle);
 grub_err_t grub_machine_mmap_unregister (int handle);
#9 lore raw

PATCH 4/4 efi: On x86-64, align the stack to a 16-byte boundary as required by ABI

In reply to Josh Triplett

The x86-64 ABI specification requires a 16-byte-aligned stack. In some cases, GCC emits code that assumes this alignment, which crashes if not aligned. The EFI firmware is also entitled to assume that stack alignment without checking, and some firmware does make that assumption.
---

ChangeLog entry:

2013-11-13 Josh Triplett <[email protected]>

* grub-core/kern/x86_64/efi/startup.S (_start): Align the stack to a 16-byte boundary, as required by the x86-64 ABI, before calling grub_main. In some cases, GCC emits code that assumes this alignment, which crashes if not aligned. The EFI firmware is also entitled to assume that stack alignment without checking.

 grub-core/kern/x86_64/efi/startup.S | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
grub-core/kern/x86_64/efi/startup.S +5 -1
index f86f019..94bd6ae 100644
@@ -29,7 +29,11 @@ start:
 _start:
 	movq	%rcx, EXT_C(grub_efi_image_handle)(%rip)
 	movq	%rdx, EXT_C(grub_efi_system_table)(%rip)
-
+	mov	%rsp, %rax
+	subq	$8, %rsp
+	and	$~0xf, %rsp
+	mov	%rax, (%rsp)
 	call	EXT_C(grub_main)
+	mov	(%rsp), %rsp
 	ret
 
Signature · 1 line
1.8.4.3
#10 lore raw

Re: [PATCH 4/4] efi: On x86-64, align the stack to a 16-byte boundary as required by ABI

Quoted 6 lines
grub-core/kern/x86_64/efi/startup.S +0 -0
index f86f019..94bd6ae 100644

grub-core/kern/x86_64/efi/startup.S start: · line 37

Quoted 10 lines
grub-core/kern/x86_64/efi/startup.S +5 -1
@@ -29,7 +29,11 @@ start:
 _start:
 	movq	%rcx, EXT_C(grub_efi_image_handle)(%rip)
 	movq	%rdx, EXT_C(grub_efi_system_table)(%rip)
-
+	mov	%rsp, %rax
+	subq	$8, %rsp
+	and	$~0xf, %rsp
+	mov	%rax, (%rsp)
 	call	EXT_C(grub_main)
+	mov	(%rsp), %rsp

This would be useful if we ever returned but we never do (we call boot_services->exit instead). Could you instead prepare a patch which does stack alignment and removed the "ret" putting instead a comment /* Doesn't return. */ ?

Quoted 3 lines
grub-core/kern/x86_64/efi/startup.S +0 -0
 	ret
 
#11 lore raw

Re: [PATCH 4/4] efi: On x86-64, align the stack to a 16-byte boundary as required by ABI

In reply to Josh Triplett
Quoted 24 lines · Josh Triplett

The x86-64 ABI specification requires a 16-byte-aligned stack. In some cases, GCC emits code that assumes this alignment, which crashes if not aligned. The EFI firmware is also entitled to assume that stack alignment without checking, and some firmware does make that assumption.
---

ChangeLog entry:

2013-11-13 Josh Triplett <[email protected]>

* grub-core/kern/x86_64/efi/startup.S (_start): Align the stack to a 16-byte boundary, as required by the x86-64 ABI, before calling grub_main. In some cases, GCC emits code that assumes this alignment, which crashes if not aligned. The EFI firmware is also entitled to assume that stack alignment without checking.

 grub-core/kern/x86_64/efi/startup.S | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
grub-core/kern/x86_64/efi/startup.S +0 -0
index f86f019..94bd6ae 100644

grub-core/kern/x86_64/efi/startup.S start: · line 37

Quoted 10 lines
grub-core/kern/x86_64/efi/startup.S +5 -1
@@ -29,7 +29,11 @@ start:
 _start:
        movq    %rcx, EXT_C(grub_efi_image_handle)(%rip)
        movq    %rdx, EXT_C(grub_efi_system_table)(%rip)
-
+       mov     %rsp, %rax
+       subq    $8, %rsp
+       and     $~0xf, %rsp
+       mov     %rax, (%rsp)
        call    EXT_C(grub_main)
+       mov     (%rsp), %rsp

You can assume that the firmware followed the alignment convention, so you just need to subtract 8 from the stack before calling, and add it back after. Since rcx is not an output, how about:
push %rcx
call EXT_C(grub_main)
pop %rcx

Or, use sub/add. Code might be larger, but would be more readable.

As far as Vladimir's comment about never returning, it seems like it would be better to keep the path safe. But, either way, the comment seems like a good idea.

-Jordan

#12 lore raw

Re: [PATCH 4/4] efi: On x86-64, align the stack to a 16-byte boundary as required by ABI

Quoted 1 line · Jordan Justen

You can assume that the firmware followed the alignment convention,

Never assume that firmware followed anything unless you have to.

#13 lore raw

Re: [PATCH 4/4] efi: On x86-64, align the stack to a 16-byte boundary as required by ABI

In reply to Josh Triplett
Quoted 7 lines · Josh Triplett

2013-11-13 Josh Triplett <[email protected]>

* grub-core/kern/x86_64/efi/startup.S (_start): Align the stack to a 16-byte boundary, as required by the x86-64 ABI, before calling grub_main. In some cases, GCC emits code that assumes this alignment, which crashes if not aligned. The EFI firmware is also entitled to assume that stack alignment without checking.

Committed with changes, thanks.