[U-Boot] [PATCH v3] x86: use EFI calling convention for efi_main on x86_64

UEFI specifies the calling convention used in Microsoft compilers; first arguments of a function are passed in (%rcx, %rdx, %r8, %r9).
All other compilers use System V ABI by default, passing first integer arguments of a function in (%rdi, %rsi, %rdx, %rcx, %r8, %r9).
These ABI also specify different sets of registers that must be preserved across function calls (callee-saved).
GCC allows using the Microsoft calling convention by adding the ms_abi attribute to a function declaration.
Current EFI implementation in U-Boot specifies EFIAPI for efi_main() in the test apps but uses default calling convention in lib/efi. The arguments of efi_main() are also passed as unused arguments to the _relocate() function.
Save efi_main() arguments in the startup code on x86_64; use EFI calling convention for _relocate() on x86_64; remove unused _relocate() arguments; consistently use EFI calling convention for efi_main() everywhere.
v3: Updated patch description.
v2: Added EFIABI to _relocate() declaration.
Ivan Gorinov (1): x86: use EFI calling convention for efi_main on x86_64
arch/x86/lib/crt0_x86_64_efi.S | 21 ++++++++++----------- arch/x86/lib/reloc_x86_64_efi.c | 3 +-- lib/efi/efi_app.c | 3 ++- lib/efi/efi_stub.c | 3 ++- 4 files changed, 15 insertions(+), 15 deletions(-)

UEFI specifies the calling convention used in Microsoft compilers; first arguments of a function are passed in (%rcx, %rdx, %r8, %r9).
All other compilers use System V ABI by default, passing first integer arguments of a function in (%rdi, %rsi, %rdx, %rcx, %r8, %r9).
These ABI also specify different sets of registers that must be preserved across function calls (callee-saved).
GCC allows using the Microsoft calling convention by adding the ms_abi attribute to a function declaration.
Current EFI implementation in U-Boot specifies EFIAPI for efi_main() in the test apps but uses default calling convention in lib/efi. The arguments of efi_main() are also passed as unused arguments to the _relocate() function.
Save efi_main() arguments in the startup code on x86_64; use EFI calling convention for _relocate() on x86_64; remove unused _relocate() arguments; consistently use EFI calling convention for efi_main() everywhere.
Signed-off-by: Ivan Gorinov ivan.gorinov@intel.com --- arch/x86/lib/crt0_x86_64_efi.S | 21 ++++++++++----------- arch/x86/lib/reloc_x86_64_efi.c | 3 +-- lib/efi/efi_app.c | 3 ++- lib/efi/efi_stub.c | 3 ++- 4 files changed, 15 insertions(+), 15 deletions(-)
diff --git a/arch/x86/lib/crt0_x86_64_efi.S b/arch/x86/lib/crt0_x86_64_efi.S index 989799f..3abb5e3 100644 --- a/arch/x86/lib/crt0_x86_64_efi.S +++ b/arch/x86/lib/crt0_x86_64_efi.S @@ -3,7 +3,7 @@ * crt0-efi-x86_64.S - x86_64 EFI startup code. * Copyright (C) 1999 Hewlett-Packard Co. * Contributed by David Mosberger davidm@hpl.hp.com. - * Copyright (C) 2005 Intel Co. + * Copyright (C) 2005 Intel Corporation * Contributed by Fenghua Yu fenghua.yu@intel.com. * * All rights reserved. @@ -14,26 +14,25 @@ .globl _start _start: subq $8, %rsp + pushq %rcx pushq %rdx
-0: - lea image_base(%rip), %rdi - lea _DYNAMIC(%rip), %rsi + lea image_base(%rip), %rcx + lea _DYNAMIC(%rip), %rdx
- popq %rcx - popq %rdx - pushq %rcx - pushq %rdx call _relocate
- popq %rdi - popq %rsi + popq %rdx + popq %rcx + + testq %rax, %rax + jnz _exit
call efi_main +_exit: addq $8, %rsp
-.exit: ret
/* diff --git a/arch/x86/lib/reloc_x86_64_efi.c b/arch/x86/lib/reloc_x86_64_efi.c index 34c5b2e..59d6f8d 100644 --- a/arch/x86/lib/reloc_x86_64_efi.c +++ b/arch/x86/lib/reloc_x86_64_efi.c @@ -14,8 +14,7 @@ #include <elf.h> #include <asm/elf.h>
-efi_status_t _relocate(long ldbase, Elf64_Dyn *dyn, efi_handle_t image, - struct efi_system_table *systab) +efi_status_t EFIAPI _relocate(long ldbase, Elf64_Dyn *dyn) { long relsz = 0, relent = 0; Elf64_Rel *rel = 0; diff --git a/lib/efi/efi_app.c b/lib/efi/efi_app.c index c828093..3eb8eeb 100644 --- a/lib/efi/efi_app.c +++ b/lib/efi/efi_app.c @@ -96,7 +96,8 @@ static void free_memory(struct efi_priv *priv) * U-Boot. If it returns, EFI will continue. Another way to get back to EFI * is via reset_cpu(). */ -efi_status_t efi_main(efi_handle_t image, struct efi_system_table *sys_table) +efi_status_t EFIAPI efi_main(efi_handle_t image, + struct efi_system_table *sys_table) { struct efi_priv local_priv, *priv = &local_priv; efi_status_t ret; diff --git a/lib/efi/efi_stub.c b/lib/efi/efi_stub.c index 3138739..399d16b 100644 --- a/lib/efi/efi_stub.c +++ b/lib/efi/efi_stub.c @@ -268,7 +268,8 @@ static void add_entry_addr(struct efi_priv *priv, enum efi_entry_t type, * This function is called by our EFI start-up code. It handles running * U-Boot. If it returns, EFI will continue. */ -efi_status_t efi_main(efi_handle_t image, struct efi_system_table *sys_table) +efi_status_t EFIAPI efi_main(efi_handle_t image, + struct efi_system_table *sys_table) { struct efi_priv local_priv, *priv = &local_priv; struct efi_boot_services *boot = sys_table->boottime;

On Thu, May 31, 2018 at 9:46 PM, Ivan Gorinov ivan.gorinov@intel.com wrote:
UEFI specifies the calling convention used in Microsoft compilers; first arguments of a function are passed in (%rcx, %rdx, %r8, %r9).
All other compilers use System V ABI by default, passing first integer arguments of a function in (%rdi, %rsi, %rdx, %rcx, %r8, %r9).
These ABI also specify different sets of registers that must be preserved across function calls (callee-saved).
GCC allows using the Microsoft calling convention by adding the ms_abi attribute to a function declaration.
Current EFI implementation in U-Boot specifies EFIAPI for efi_main() in the test apps but uses default calling convention in lib/efi. The arguments of efi_main() are also passed as unused arguments to the _relocate() function.
Save efi_main() arguments in the startup code on x86_64; use EFI calling convention for _relocate() on x86_64; remove unused _relocate() arguments; consistently use EFI calling convention for efi_main() everywhere.
Thanks, FWIW,
Reviewed-by: Andy Shevchenko andy.shevchenko@gmail.com
Signed-off-by: Ivan Gorinov ivan.gorinov@intel.com
arch/x86/lib/crt0_x86_64_efi.S | 21 ++++++++++----------- arch/x86/lib/reloc_x86_64_efi.c | 3 +-- lib/efi/efi_app.c | 3 ++- lib/efi/efi_stub.c | 3 ++- 4 files changed, 15 insertions(+), 15 deletions(-)
diff --git a/arch/x86/lib/crt0_x86_64_efi.S b/arch/x86/lib/crt0_x86_64_efi.S index 989799f..3abb5e3 100644 --- a/arch/x86/lib/crt0_x86_64_efi.S +++ b/arch/x86/lib/crt0_x86_64_efi.S @@ -3,7 +3,7 @@
- crt0-efi-x86_64.S - x86_64 EFI startup code.
- Copyright (C) 1999 Hewlett-Packard Co.
- Contributed by David Mosberger davidm@hpl.hp.com.
- Copyright (C) 2005 Intel Co.
- Copyright (C) 2005 Intel Corporation
- Contributed by Fenghua Yu fenghua.yu@intel.com.
- All rights reserved.
@@ -14,26 +14,25 @@ .globl _start _start: subq $8, %rsp
pushq %rcx pushq %rdx
-0:
lea image_base(%rip), %rdi
lea _DYNAMIC(%rip), %rsi
lea image_base(%rip), %rcx
lea _DYNAMIC(%rip), %rdx
popq %rcx
popq %rdx
pushq %rcx
pushq %rdx call _relocate
popq %rdi
popq %rsi
popq %rdx
popq %rcx
testq %rax, %rax
jnz _exit call efi_main
+_exit: addq $8, %rsp
-.exit: ret
/*
diff --git a/arch/x86/lib/reloc_x86_64_efi.c b/arch/x86/lib/reloc_x86_64_efi.c index 34c5b2e..59d6f8d 100644 --- a/arch/x86/lib/reloc_x86_64_efi.c +++ b/arch/x86/lib/reloc_x86_64_efi.c @@ -14,8 +14,7 @@ #include <elf.h> #include <asm/elf.h>
-efi_status_t _relocate(long ldbase, Elf64_Dyn *dyn, efi_handle_t image,
struct efi_system_table *systab)
+efi_status_t EFIAPI _relocate(long ldbase, Elf64_Dyn *dyn) { long relsz = 0, relent = 0; Elf64_Rel *rel = 0; diff --git a/lib/efi/efi_app.c b/lib/efi/efi_app.c index c828093..3eb8eeb 100644 --- a/lib/efi/efi_app.c +++ b/lib/efi/efi_app.c @@ -96,7 +96,8 @@ static void free_memory(struct efi_priv *priv)
- U-Boot. If it returns, EFI will continue. Another way to get back to EFI
- is via reset_cpu().
*/ -efi_status_t efi_main(efi_handle_t image, struct efi_system_table *sys_table) +efi_status_t EFIAPI efi_main(efi_handle_t image,
struct efi_system_table *sys_table)
{ struct efi_priv local_priv, *priv = &local_priv; efi_status_t ret; diff --git a/lib/efi/efi_stub.c b/lib/efi/efi_stub.c index 3138739..399d16b 100644 --- a/lib/efi/efi_stub.c +++ b/lib/efi/efi_stub.c @@ -268,7 +268,8 @@ static void add_entry_addr(struct efi_priv *priv, enum efi_entry_t type,
- This function is called by our EFI start-up code. It handles running
- U-Boot. If it returns, EFI will continue.
*/ -efi_status_t efi_main(efi_handle_t image, struct efi_system_table *sys_table) +efi_status_t EFIAPI efi_main(efi_handle_t image,
struct efi_system_table *sys_table)
{ struct efi_priv local_priv, *priv = &local_priv; struct efi_boot_services *boot = sys_table->boottime; -- 2.7.4
U-Boot mailing list U-Boot@lists.denx.de https://lists.denx.de/listinfo/u-boot

On 31.05.18 20:46, Ivan Gorinov wrote:
UEFI specifies the calling convention used in Microsoft compilers; first arguments of a function are passed in (%rcx, %rdx, %r8, %r9).
All other compilers use System V ABI by default, passing first integer arguments of a function in (%rdi, %rsi, %rdx, %rcx, %r8, %r9).
These ABI also specify different sets of registers that must be preserved across function calls (callee-saved).
GCC allows using the Microsoft calling convention by adding the ms_abi attribute to a function declaration.
Current EFI implementation in U-Boot specifies EFIAPI for efi_main() in the test apps but uses default calling convention in lib/efi. The arguments of efi_main() are also passed as unused arguments to the _relocate() function.
Save efi_main() arguments in the startup code on x86_64; use EFI calling convention for _relocate() on x86_64; remove unused _relocate() arguments; consistently use EFI calling convention for efi_main() everywhere.
Signed-off-by: Ivan Gorinov ivan.gorinov@intel.com
Looks good to me, but I haven't double-checked all registers to the respective calling conventions. I'll leave that to Bin :)
Alex

Hi Ivan,
On Fri, Jun 1, 2018 at 2:46 AM, Ivan Gorinov ivan.gorinov@intel.com wrote:
UEFI specifies the calling convention used in Microsoft compilers; first arguments of a function are passed in (%rcx, %rdx, %r8, %r9).
All other compilers use System V ABI by default, passing first integer arguments of a function in (%rdi, %rsi, %rdx, %rcx, %r8, %r9).
These ABI also specify different sets of registers that must be preserved across function calls (callee-saved).
GCC allows using the Microsoft calling convention by adding the ms_abi attribute to a function declaration.
Current EFI implementation in U-Boot specifies EFIAPI for efi_main() in the test apps but uses default calling convention in lib/efi. The arguments of efi_main() are also passed as unused arguments to the _relocate() function.
Save efi_main() arguments in the startup code on x86_64; use EFI calling convention for _relocate() on x86_64; remove unused _relocate() arguments;
This should be a separate patch to remove unused _relocate arguments for all arches (arm/x86/risc-v)
consistently use EFI calling convention for efi_main() everywhere.
Signed-off-by: Ivan Gorinov ivan.gorinov@intel.com
arch/x86/lib/crt0_x86_64_efi.S | 21 ++++++++++----------- arch/x86/lib/reloc_x86_64_efi.c | 3 +-- lib/efi/efi_app.c | 3 ++- lib/efi/efi_stub.c | 3 ++- 4 files changed, 15 insertions(+), 15 deletions(-)
diff --git a/arch/x86/lib/crt0_x86_64_efi.S b/arch/x86/lib/crt0_x86_64_efi.S index 989799f..3abb5e3 100644 --- a/arch/x86/lib/crt0_x86_64_efi.S +++ b/arch/x86/lib/crt0_x86_64_efi.S @@ -3,7 +3,7 @@
- crt0-efi-x86_64.S - x86_64 EFI startup code.
- Copyright (C) 1999 Hewlett-Packard Co.
- Contributed by David Mosberger davidm@hpl.hp.com.
- Copyright (C) 2005 Intel Co.
- Copyright (C) 2005 Intel Corporation
- Contributed by Fenghua Yu fenghua.yu@intel.com.
- All rights reserved.
@@ -14,26 +14,25 @@ .globl _start _start: subq $8, %rsp
pushq %rcx pushq %rdx
-0:
lea image_base(%rip), %rdi
lea _DYNAMIC(%rip), %rsi
lea image_base(%rip), %rcx
lea _DYNAMIC(%rip), %rdx
popq %rcx
popq %rdx
pushq %rcx
pushq %rdx call _relocate
popq %rdi
popq %rsi
popq %rdx
popq %rcx
testq %rax, %rax
jnz _exit call efi_main
+_exit:
nits: keep the .exit label name, as the x86 version uses the same name, for consistency
addq $8, %rsp
-.exit: ret
/*
diff --git a/arch/x86/lib/reloc_x86_64_efi.c b/arch/x86/lib/reloc_x86_64_efi.c index 34c5b2e..59d6f8d 100644 --- a/arch/x86/lib/reloc_x86_64_efi.c +++ b/arch/x86/lib/reloc_x86_64_efi.c @@ -14,8 +14,7 @@ #include <elf.h> #include <asm/elf.h>
-efi_status_t _relocate(long ldbase, Elf64_Dyn *dyn, efi_handle_t image,
struct efi_system_table *systab)
+efi_status_t EFIAPI _relocate(long ldbase, Elf64_Dyn *dyn) { long relsz = 0, relent = 0; Elf64_Rel *rel = 0; diff --git a/lib/efi/efi_app.c b/lib/efi/efi_app.c index c828093..3eb8eeb 100644 --- a/lib/efi/efi_app.c +++ b/lib/efi/efi_app.c @@ -96,7 +96,8 @@ static void free_memory(struct efi_priv *priv)
- U-Boot. If it returns, EFI will continue. Another way to get back to EFI
- is via reset_cpu().
*/ -efi_status_t efi_main(efi_handle_t image, struct efi_system_table *sys_table) +efi_status_t EFIAPI efi_main(efi_handle_t image,
struct efi_system_table *sys_table)
{ struct efi_priv local_priv, *priv = &local_priv; efi_status_t ret; diff --git a/lib/efi/efi_stub.c b/lib/efi/efi_stub.c index 3138739..399d16b 100644 --- a/lib/efi/efi_stub.c +++ b/lib/efi/efi_stub.c @@ -268,7 +268,8 @@ static void add_entry_addr(struct efi_priv *priv, enum efi_entry_t type,
- This function is called by our EFI start-up code. It handles running
- U-Boot. If it returns, EFI will continue.
*/ -efi_status_t efi_main(efi_handle_t image, struct efi_system_table *sys_table) +efi_status_t EFIAPI efi_main(efi_handle_t image,
struct efi_system_table *sys_table)
{ struct efi_priv local_priv, *priv = &local_priv; struct efi_boot_services *boot = sys_table->boottime; --
Regards, Bin
participants (4)
-
Alexander Graf
-
Andy Shevchenko
-
Bin Meng
-
Ivan Gorinov