[PATCH v2 1/1] x86_64: Fix stack aligment for x86-64 SysV ABI

Matheus Pecoraro matpecor at gmail.com
Mon Apr 1 19:29:01 UTC 2024


Makes the code in bsp/x86_64/amd64/start/start.S and the stack frame
setup by _CPU_Context_Initialize align the stack properly according to
what is expected by the x86-64 SysV ABI

Updates #2898
Closes #5004
---
 bsps/x86_64/amd64/clock/eficlock.c            | 28 ++-----------------
 bsps/x86_64/amd64/start/start.S               |  1 -
 .../cpu/x86_64/x86_64-context-initialize.c    |  4 +--
 3 files changed, 5 insertions(+), 28 deletions(-)

diff --git a/bsps/x86_64/amd64/clock/eficlock.c b/bsps/x86_64/amd64/clock/eficlock.c
index f5ff9278f6..a841f909c1 100644
--- a/bsps/x86_64/amd64/clock/eficlock.c
+++ b/bsps/x86_64/amd64/clock/eficlock.c
@@ -41,35 +41,13 @@ Clock_isr( void* );
 #error "EFIAPI not defined!"
 #endif
 
-
-/* no-sse attribute helps with enforcing GCC to generate code which does not use
-   SSE instructions requiring 16 byte alligned access on unaligned data hence
-   producing perfect GP fault. The code with SSE enabled looks:
-
-EFIAPI void
-efi_clock_tick_notify(EFI_EVENT e, VOID* ctx)
-{
-  10b2ba:       55                      push   rbp
-  10b2bb:       48 89 e5                mov    rbp,rsp
-  10b2be:       57                      push   rdi
-  10b2bf:       56                      push   rsi
-  10b2c0:       48 81 ec a0 00 00 00    sub    rsp,0xa0
-  10b2c7:       8b 02                   mov    eax,DWORD PTR [rdx]
-->10b2c9:       0f 29 b5 50 ff ff ff    movaps XMMWORD PTR [rbp-0xb0],xmm6
-  10b2d0:       0f 29 bd 60 ff ff ff    movaps XMMWORD PTR [rbp-0xa0],xmm7
-  10b2d7:       83 c0 01                add    eax,0x1
-
-and we get GP @ 10b2c9.
-
-CAVEAT: This function is to be called from the UEFI which means it needs to callable
-by using MS ABI!
-
+/*
+ * CAVEAT: This function is to be called from the UEFI which means it
+ * needs to be callable by using MS ABI!
  */
-__attribute__((target("no-sse")))
 EFIAPI void
 efi_clock_tick_notify(EFI_EVENT e, VOID* ctx);
 
-__attribute__((target("no-sse")))
 EFIAPI void
 efi_clock_tick_notify(EFI_EVENT e, VOID* ctx)
 {
diff --git a/bsps/x86_64/amd64/start/start.S b/bsps/x86_64/amd64/start/start.S
index b8120473be..f1f2f059b9 100644
--- a/bsps/x86_64/amd64/start/start.S
+++ b/bsps/x86_64/amd64/start/start.S
@@ -40,7 +40,6 @@
 _start:
 	.cfi_startproc
 	movabsq	$_ISR_Stack_area_end, %rsp
-	subq	$8, %rsp
 	.cfi_def_cfa_offset 16
 	xorl	%edi, %edi
 #ifdef BSP_MULTIBOOT_SUPPORT
diff --git a/cpukit/score/cpu/x86_64/x86_64-context-initialize.c b/cpukit/score/cpu/x86_64/x86_64-context-initialize.c
index ee25fcfa2d..0df7cff1bc 100644
--- a/cpukit/score/cpu/x86_64/x86_64-context-initialize.c
+++ b/cpukit/score/cpu/x86_64/x86_64-context-initialize.c
@@ -87,8 +87,8 @@ void _CPU_Context_Initialize(
 
   _stack  = ((uintptr_t) stack_area_begin) + stack_area_size;
   _stack &= ~(CPU_STACK_ALIGNMENT - 1);
-  _stack -= sizeof(uintptr_t); /* fake return address for entry_point's frame;
-                                * this allows rsp+8 to be an aligned boundary */
+  _stack -= CPU_STACK_ALIGNMENT; /* fake return address for entry_point's frame;
+                                  * this allows rsp+8 to be an aligned boundary */
   *((void (**)(void)) _stack) = entry_point;
 
   the_context->rbp     = (void *) 0;
-- 
2.43.2



More information about the devel mailing list