mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] x86: lib: setjmp: fix stack alignment
@ 2023-10-09 11:50 Ahmad Fatoum
  2023-10-09 11:50 ` [PATCH 2/2] x86: setjmp: set base pointer to zero in initjmp Ahmad Fatoum
  2023-10-12 14:27 ` [PATCH 1/2] x86: lib: setjmp: fix stack alignment Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2023-10-09 11:50 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

x86 stack is expected to be 16-byte aligned to allow for instructions
like movaps that involve xmm operands to directly use the stack.

However the 16-byte alignment is what's expected at startup time.
All later functions will have the stack misaligned by a pointer size's
worth because call pushes the return address to the call stack.

Add the missing (mis)alignment. This fixes a segmentation fault observed
using initjmp on x86_64.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 arch/x86/lib/setjmp_32.S | 4 +++-
 arch/x86/lib/setjmp_64.S | 2 ++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/arch/x86/lib/setjmp_32.S b/arch/x86/lib/setjmp_32.S
index 38dcb68c1b59..30db5f989af6 100644
--- a/arch/x86/lib/setjmp_32.S
+++ b/arch/x86/lib/setjmp_32.S
@@ -8,6 +8,7 @@
 #define _REGPARM
 
 #include <linux/linkage.h>
+#include <asm-generic/pointer.h>
 
 .text
 .align 8
@@ -53,7 +54,8 @@ ENDPROC(longjmp)
 ENTRY(initjmp)
 
 	movl %edx, 20(%eax)	/* Return address */
-	movl %ecx, 4(%eax)	/* Post-return %esp! */
+	sub $ASM_SZPTR, %ecx	/* ESP - 4 has to be 16-byte aligned on entry */
+	movl %ecx, 4(%eax)	/* Stack top */
 	xorl %eax, %eax		/* Return value */
 	ret
 
diff --git a/arch/x86/lib/setjmp_64.S b/arch/x86/lib/setjmp_64.S
index 28ea576cd22e..d5cf99a1557f 100644
--- a/arch/x86/lib/setjmp_64.S
+++ b/arch/x86/lib/setjmp_64.S
@@ -6,6 +6,7 @@
  */
 
 #include <linux/linkage.h>
+#include <asm-generic/pointer.h>
 
 .text
 .align 8
@@ -53,6 +54,7 @@ ENDPROC(longjmp)
 ENTRY(initjmp)
 
 	movq	%rsi, (%rdi)	/* Return address */
+	sub	$ASM_SZPTR, %rdx	/* RSP - 8 has to be 16-byte aligned on entry */
 	movq	%rdx, 8(%rdi)	/* Stack top */
 	xorq	%rax, %rax
 	ret
-- 
2.39.2




^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/2] x86: setjmp: set base pointer to zero in initjmp
  2023-10-09 11:50 [PATCH 1/2] x86: lib: setjmp: fix stack alignment Ahmad Fatoum
@ 2023-10-09 11:50 ` Ahmad Fatoum
  2023-10-12 14:27 ` [PATCH 1/2] x86: lib: setjmp: fix stack alignment Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2023-10-09 11:50 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Initializing the frame points to a known-bad value like zero makes it
easier for code unwinding the stack to know when to stop. Thus
initialize the stack pointer in initjmp-initialized jump buffers to
zero.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 arch/x86/lib/setjmp_32.S | 1 +
 arch/x86/lib/setjmp_64.S | 1 +
 2 files changed, 2 insertions(+)

diff --git a/arch/x86/lib/setjmp_32.S b/arch/x86/lib/setjmp_32.S
index 30db5f989af6..5814623f9478 100644
--- a/arch/x86/lib/setjmp_32.S
+++ b/arch/x86/lib/setjmp_32.S
@@ -54,6 +54,7 @@ ENDPROC(longjmp)
 ENTRY(initjmp)
 
 	movl %edx, 20(%eax)	/* Return address */
+	movl   $0, 8(%edx)	/* Base pointer */
 	sub $ASM_SZPTR, %ecx	/* ESP - 4 has to be 16-byte aligned on entry */
 	movl %ecx, 4(%eax)	/* Stack top */
 	xorl %eax, %eax		/* Return value */
diff --git a/arch/x86/lib/setjmp_64.S b/arch/x86/lib/setjmp_64.S
index d5cf99a1557f..bfa152149975 100644
--- a/arch/x86/lib/setjmp_64.S
+++ b/arch/x86/lib/setjmp_64.S
@@ -54,6 +54,7 @@ ENDPROC(longjmp)
 ENTRY(initjmp)
 
 	movq	%rsi, (%rdi)	/* Return address */
+	movq	$0,  16(%rdi)	/* Base pointer */
 	sub	$ASM_SZPTR, %rdx	/* RSP - 8 has to be 16-byte aligned on entry */
 	movq	%rdx, 8(%rdi)	/* Stack top */
 	xorq	%rax, %rax
-- 
2.39.2




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/2] x86: lib: setjmp: fix stack alignment
  2023-10-09 11:50 [PATCH 1/2] x86: lib: setjmp: fix stack alignment Ahmad Fatoum
  2023-10-09 11:50 ` [PATCH 2/2] x86: setjmp: set base pointer to zero in initjmp Ahmad Fatoum
@ 2023-10-12 14:27 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2023-10-12 14:27 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On Mon, Oct 09, 2023 at 01:50:50PM +0200, Ahmad Fatoum wrote:
> x86 stack is expected to be 16-byte aligned to allow for instructions
> like movaps that involve xmm operands to directly use the stack.
> 
> However the 16-byte alignment is what's expected at startup time.
> All later functions will have the stack misaligned by a pointer size's
> worth because call pushes the return address to the call stack.
> 
> Add the missing (mis)alignment. This fixes a segmentation fault observed
> using initjmp on x86_64.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
>  arch/x86/lib/setjmp_32.S | 4 +++-
>  arch/x86/lib/setjmp_64.S | 2 ++
>  2 files changed, 5 insertions(+), 1 deletion(-)

Applied, thanks

Sascha

> 
> diff --git a/arch/x86/lib/setjmp_32.S b/arch/x86/lib/setjmp_32.S
> index 38dcb68c1b59..30db5f989af6 100644
> --- a/arch/x86/lib/setjmp_32.S
> +++ b/arch/x86/lib/setjmp_32.S
> @@ -8,6 +8,7 @@
>  #define _REGPARM
>  
>  #include <linux/linkage.h>
> +#include <asm-generic/pointer.h>
>  
>  .text
>  .align 8
> @@ -53,7 +54,8 @@ ENDPROC(longjmp)
>  ENTRY(initjmp)
>  
>  	movl %edx, 20(%eax)	/* Return address */
> -	movl %ecx, 4(%eax)	/* Post-return %esp! */
> +	sub $ASM_SZPTR, %ecx	/* ESP - 4 has to be 16-byte aligned on entry */
> +	movl %ecx, 4(%eax)	/* Stack top */
>  	xorl %eax, %eax		/* Return value */
>  	ret
>  
> diff --git a/arch/x86/lib/setjmp_64.S b/arch/x86/lib/setjmp_64.S
> index 28ea576cd22e..d5cf99a1557f 100644
> --- a/arch/x86/lib/setjmp_64.S
> +++ b/arch/x86/lib/setjmp_64.S
> @@ -6,6 +6,7 @@
>   */
>  
>  #include <linux/linkage.h>
> +#include <asm-generic/pointer.h>
>  
>  .text
>  .align 8
> @@ -53,6 +54,7 @@ ENDPROC(longjmp)
>  ENTRY(initjmp)
>  
>  	movq	%rsi, (%rdi)	/* Return address */
> +	sub	$ASM_SZPTR, %rdx	/* RSP - 8 has to be 16-byte aligned on entry */
>  	movq	%rdx, 8(%rdi)	/* Stack top */
>  	xorq	%rax, %rax
>  	ret
> -- 
> 2.39.2
> 
> 
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-10-12 14:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-09 11:50 [PATCH 1/2] x86: lib: setjmp: fix stack alignment Ahmad Fatoum
2023-10-09 11:50 ` [PATCH 2/2] x86: setjmp: set base pointer to zero in initjmp Ahmad Fatoum
2023-10-12 14:27 ` [PATCH 1/2] x86: lib: setjmp: fix stack alignment Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox