i386-efi and x86_64-efi fixes
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
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
@@ -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
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.
PATCH 2/4 efi: Fix requests to allocate GRUB_MEMORY_AVAILABLE
In reply to Josh TriplettEFI 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
@@ -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
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. MapGRUB_MEMORY_AVAILABLEtoGRUB_EFI_LOADER_CODEinstead.
---ChangeLog entry:
Quoted 9 lines
2013-11-13 Josh Triplett <[email protected]>
*
grub-core/mmap/efi/mmap.c(make_efi_memtype): MapGRUB_MEMORY_AVAILABLEtoGRUB_EFI_LOADER_CODErather thanGRUB_EFI_CONVENTIONAL_MEMORY. EFI firmware refuses to allocate memory of typeGRUB_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@@ -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 ;
PATCH 3/4 efi: Support GRUB_MMAP_MALLOC_LOW in the EFI firmware allocator
In reply to Josh TriplettEFI 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
@@ -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
@@ -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
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_LOWby 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 forGRUB_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@@ -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@@ -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);
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_LOWby 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_LOWand 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.
Re: [PATCH 3/4] efi: Support GRUB_MMAP_MALLOC_LOW in the EFI firmware allocator
In reply to Josh TriplettQuoted 2 lines · Josh Triplett
EFI supports allocating memory below a specified address; use that to implement
GRUB_MMAP_MALLOC_LOWby 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 forGRUB_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@@ -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@@ -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);
PATCH 4/4 efi: On x86-64, align the stack to a 16-byte boundary as required by ABI
In reply to Josh TriplettThe 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
@@ -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
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
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
-0ret
Re: [PATCH 4/4] efi: On x86-64, align the stack to a 16-byte boundary as required by ABI
In reply to Josh TriplettQuoted 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 callinggrub_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
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
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.
Re: [PATCH 4/4] efi: On x86-64, align the stack to a 16-byte boundary as required by ABI
In reply to Josh TriplettQuoted 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 callinggrub_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.