linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: will.deacon@arm.com (Will Deacon)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH 3/7] arm64: Align stack when taking exception from EL1
Date: Wed, 7 Nov 2018 21:59:03 +0000	[thread overview]
Message-ID: <20181107215901.GE12248@brain-police> (raw)
In-Reply-To: <1537970184-44348-4-git-send-email-julien.thierry@arm.com>

On Wed, Sep 26, 2018 at 02:56:20PM +0100, Julien Thierry wrote:
> Arm64 SP needs to be aligned to 16 bytes before being used as base address
> for loads and stores. When taking some valid exceptions from EL1 (e.g. irq,
> dbg, data abort), there is no guarantee that SP_EL1 was aligned when
> taking the exception.
> 
> Pad the stack on EL1 entries when misaligned.
> 
> Signed-off-by: Julien Thierry <julien.thierry@arm.com>
> ---
>  arch/arm64/include/asm/assembler.h |  9 +++++++++
>  arch/arm64/kernel/entry.S          | 32 ++++++++++++++++++++++++++++++--
>  2 files changed, 39 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/arm64/include/asm/assembler.h b/arch/arm64/include/asm/assembler.h
> index 0bcc98d..a0a5415 100644
> --- a/arch/arm64/include/asm/assembler.h
> +++ b/arch/arm64/include/asm/assembler.h
> @@ -701,4 +701,13 @@
>  .Lyield_out_\@ :
>  	.endm
>  
> +/*
> + * Echange content of register xt with sp.

Exchange contents of register Xt with SP

> + */
> +	.macro xchg_sp	xt
> +	add	sp, sp, \xt	// sp' = sp + xt
> +	sub	\xt, sp, \xt	// xt' = sp' - xt = sp + xt - xt = sp
> +	sub	sp, sp, \xt	// sp'' = sp' - xt' = sp + xt - sp = xt
> +	.endm

Having said that, this is clearly inspired by the code in kernel_ventry for
dealing with stack overflow, yet the macro you've got here cannot be reused
there.

Maybe we're better off only macro-ising:

	.macro sp_fetch_add xt
	add	sp, sp, \xt
	sub	\xt, sp, \xt
	.endm

and then letting the callers handle the rest, or build an swizzle_sp macro
on top of it?

> +
>  #endif	/* __ASM_ASSEMBLER_H */
> diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S
> index fc5842b..8fb66e4 100644
> --- a/arch/arm64/kernel/entry.S
> +++ b/arch/arm64/kernel/entry.S
> @@ -59,6 +59,19 @@
>  	.endr
>  	.endm
>  
> +	.macro force_stack_align
> +	xchg_sp	x0
> +	str	x1, [x0] // store x1 far away from S_SP

So this temporarily trashes pt_regs.regs[0] if the stack is already aligned,
right? I think that's fine, but we should comment /that/ rather than vaguely
saying it's far away from S_SP. In fact, having a block comment above this
macro saying exactly what is clobbered would generally be a good idea (esp
as you nobble the flags in your next patch).

> +
> +	// aligned_sp[S_SP] = old_sp
> +	bic	x1, x0, #0xf	// align down to 16-byte
> +	str	x0, [x1, #S_SP]
> +
> +	ldr	x1, [x0]
> +	bic	x0, x0, #0xf	// x0 = aligned_sp
> +	xchg_sp	x0
> +	.endm
> +
>  /*
>   * Bad Abort numbers
>   *-----------------
> @@ -158,6 +171,10 @@ alternative_cb_end
>  	.if	\regsize == 32
>  	mov	w0, w0				// zero upper 32 bits of x0
>  	.endif
> +	.if	\el != 0
> +	force_stack_align
> +	.endif
> +
>  	stp	x0, x1, [sp, #16 * 0]
>  	stp	x2, x3, [sp, #16 * 1]
>  	stp	x4, x5, [sp, #16 * 2]
> @@ -184,7 +201,8 @@ alternative_cb_end
>  	apply_ssbd 1, x22, x23
>  
>  	.else
> -	add	x21, sp, #S_FRAME_SIZE
> +	ldr	x21, [sp, #S_SP]
> +	add	x21, x21, #S_FRAME_SIZE		// adjust stored sp

Can we not just store the corrected SP in force_stack_align, rather than
store something bogus and then have to add the frame size back here?

Will

  reply	other threads:[~2018-11-07 21:59 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-26 13:56 [PATCH 0/7] Ensure stack is aligned for kernel entries Julien Thierry
2018-09-26 13:56 ` [PATCH 1/7] arm64: Add static check for pt_regs alignment Julien Thierry
2018-11-07 21:58   ` Will Deacon
2018-11-08 10:16     ` Mark Rutland
2018-11-08 11:57       ` Julien Thierry
2018-11-08 16:53         ` Will Deacon
2018-11-08 18:35   ` Ard Biesheuvel
2018-09-26 13:56 ` [PATCH 2/7] arm64: sdei: Always use sdei stack for sdei events Julien Thierry
2018-11-07 21:58   ` Will Deacon
2018-11-21 11:15     ` James Morse
2018-09-26 13:56 ` [PATCH 3/7] arm64: Align stack when taking exception from EL1 Julien Thierry
2018-11-07 21:59   ` Will Deacon [this message]
2018-11-08 12:20     ` Julien Thierry
2018-09-26 13:56 ` [PATCH 4/7] arm64: Add fast-path for stack alignment Julien Thierry
2018-11-07 21:59   ` Will Deacon
2018-11-08 12:21     ` Julien Thierry
2018-09-26 13:56 ` [PATCH 5/7] arm64: Do not apply BP hardening for hyp entries from EL2 Julien Thierry
2018-11-07 21:59   ` Will Deacon
2018-11-08 12:23     ` Julien Thierry
2018-09-26 13:56 ` [PATCH 6/7] arm64: Do not apply vector harderning " Julien Thierry
2018-11-07 21:59   ` Will Deacon
2018-11-08 12:31     ` Julien Thierry
2018-09-26 13:56 ` [PATCH 7/7] arm64: kvm: Align stack for exception coming " Julien Thierry
2018-10-19  8:07 ` [PATCH 0/7] Ensure stack is aligned for kernel entries Julien Thierry
2018-11-07 21:58 ` Will Deacon
2018-11-08 12:43   ` Julien Thierry
2018-11-08 13:04 ` Ard Biesheuvel
2018-11-08 13:27   ` Julien Thierry
2018-11-08 14:10     ` Ard Biesheuvel
2018-11-08 14:19       ` Ramana Radhakrishnan
2018-11-08 15:01         ` Julien Thierry
2018-11-08 15:33           ` Ramana Radhakrishnan
2018-11-08 15:41             ` Julien Thierry
2018-11-08 15:30         ` Dave Martin
2018-11-08 15:33           ` Ramana Radhakrishnan
2018-11-08 15:39             ` Dave Martin
2018-11-08 15:44               ` Ard Biesheuvel
2018-11-08 16:57                 ` Ramana Radhakrishnan
2018-11-08 17:14                   ` Ard Biesheuvel
2018-11-22 11:49 ` Julien Thierry
2018-11-22 12:11   ` Ard Biesheuvel
2018-11-22 15:10     ` Julien Thierry
2018-11-22 15:12     ` Will Deacon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181107215901.GE12248@brain-police \
    --to=will.deacon@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).