From: ard.biesheuvel@linaro.org (Ard Biesheuvel) To: linux-arm-kernel@lists.infradead.org Subject: [RFC PATCH 07/10] arm64: kernel: switch to register x18 as a task struct pointer Date: Thu, 13 Jul 2017 13:27:50 +0100 [thread overview] Message-ID: <CAKv+Gu8gFSSYV5-11bZOMnQGMcvx0gSDnTpibciEPMv_3HpAUA@mail.gmail.com> (raw) In-Reply-To: <20170713104116.GD3966@e103592.cambridge.arm.com> On 13 July 2017 at 11:41, Dave Martin <Dave.Martin@arm.com> wrote: > On Wed, Jul 12, 2017 at 03:44:20PM +0100, Ard Biesheuvel wrote: >> In order to free up sp_el0, which we will need to deal with faulting >> stack accesses when using virtually mapped stacks, switch to register >> x18 as the task struct register. This is permitted by the AAPCS64 ABI, >> and simplifies many references to 'current', given that they no longer >> involve a MSR instruction to access SP_EL0. >> >> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> > > [...] > >> diff --git a/arch/arm64/include/asm/current.h b/arch/arm64/include/asm/current.h >> index f6580d4afb0e..b4e3acff699c 100644 >> --- a/arch/arm64/include/asm/current.h >> +++ b/arch/arm64/include/asm/current.h >> @@ -13,11 +13,9 @@ struct task_struct; >> */ >> static __always_inline struct task_struct *get_current(void) >> { >> - unsigned long sp_el0; >> + register unsigned long tsk asm ("x18"); >> >> - asm ("mrs %0, sp_el0" : "=r" (sp_el0)); >> - >> - return (struct task_struct *)sp_el0; >> + return (struct task_struct *)tsk; > > Nit: > > You're explicitly returning an uninitialised variable here: the asm > annotation doesn't change the fact that tsk lifetime is that of the > function. Sufficiently aggressive GCC can probably optimise the whole > thing (and any caller) away as undefined behaviour. > > The GCC docs say > > "The only supported use for [specifying registers for local variables] > is to specify registers for input and output operands when calling > Extended 'asm'". > Ah ok, so it needs to live outside of the function, just like current_stack_pointer. > > As an alternative, you could make tsk a global register variable. I > don't know whether it should be volatile or not in that case -- > probably not, since it's constant for a given thread. > > Alternatively, the following should work: > > unsigned long ret; > > asm ("mrs %0, x18" : "=r" (ret)); > > return ret; > > (with -ffixed-x18, naturally). > Indeed (assuming you meant mov not mrs)
WARNING: multiple messages have this Message-ID (diff)
From: Ard Biesheuvel <ard.biesheuvel@linaro.org> To: Dave Martin <Dave.Martin@arm.com> Cc: "linux-arm-kernel@lists.infradead.org" <linux-arm-kernel@lists.infradead.org>, Kernel Hardening <kernel-hardening@lists.openwall.com>, Mark Rutland <mark.rutland@arm.com>, Catalin Marinas <catalin.marinas@arm.com>, Will Deacon <will.deacon@arm.com>, Laura Abbott <labbott@fedoraproject.org> Subject: [kernel-hardening] Re: [RFC PATCH 07/10] arm64: kernel: switch to register x18 as a task struct pointer Date: Thu, 13 Jul 2017 13:27:50 +0100 [thread overview] Message-ID: <CAKv+Gu8gFSSYV5-11bZOMnQGMcvx0gSDnTpibciEPMv_3HpAUA@mail.gmail.com> (raw) In-Reply-To: <20170713104116.GD3966@e103592.cambridge.arm.com> On 13 July 2017 at 11:41, Dave Martin <Dave.Martin@arm.com> wrote: > On Wed, Jul 12, 2017 at 03:44:20PM +0100, Ard Biesheuvel wrote: >> In order to free up sp_el0, which we will need to deal with faulting >> stack accesses when using virtually mapped stacks, switch to register >> x18 as the task struct register. This is permitted by the AAPCS64 ABI, >> and simplifies many references to 'current', given that they no longer >> involve a MSR instruction to access SP_EL0. >> >> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> > > [...] > >> diff --git a/arch/arm64/include/asm/current.h b/arch/arm64/include/asm/current.h >> index f6580d4afb0e..b4e3acff699c 100644 >> --- a/arch/arm64/include/asm/current.h >> +++ b/arch/arm64/include/asm/current.h >> @@ -13,11 +13,9 @@ struct task_struct; >> */ >> static __always_inline struct task_struct *get_current(void) >> { >> - unsigned long sp_el0; >> + register unsigned long tsk asm ("x18"); >> >> - asm ("mrs %0, sp_el0" : "=r" (sp_el0)); >> - >> - return (struct task_struct *)sp_el0; >> + return (struct task_struct *)tsk; > > Nit: > > You're explicitly returning an uninitialised variable here: the asm > annotation doesn't change the fact that tsk lifetime is that of the > function. Sufficiently aggressive GCC can probably optimise the whole > thing (and any caller) away as undefined behaviour. > > The GCC docs say > > "The only supported use for [specifying registers for local variables] > is to specify registers for input and output operands when calling > Extended 'asm'". > Ah ok, so it needs to live outside of the function, just like current_stack_pointer. > > As an alternative, you could make tsk a global register variable. I > don't know whether it should be volatile or not in that case -- > probably not, since it's constant for a given thread. > > Alternatively, the following should work: > > unsigned long ret; > > asm ("mrs %0, x18" : "=r" (ret)); > > return ret; > > (with -ffixed-x18, naturally). > Indeed (assuming you meant mov not mrs)
next prev parent reply other threads:[~2017-07-13 12:27 UTC|newest] Thread overview: 44+ messages / expand[flat|nested] mbox.gz Atom feed top 2017-07-12 14:44 [RFC PATCH 00/10] arm64: allow virtually mapped stacks to be enabled Ard Biesheuvel 2017-07-12 14:44 ` [kernel-hardening] " Ard Biesheuvel 2017-07-12 14:44 ` [RFC PATCH 01/10] arm64/lib: copy_page: use consistent prefetch stride Ard Biesheuvel 2017-07-12 14:44 ` [kernel-hardening] " Ard Biesheuvel 2017-07-12 14:44 ` [RFC PATCH 02/10] arm64/lib: copy_page: avoid x18 register in assembler code Ard Biesheuvel 2017-07-12 14:44 ` [kernel-hardening] " Ard Biesheuvel 2017-07-12 14:44 ` [RFC PATCH 03/10] arm64: crypto: avoid register x18 in scalar AES code Ard Biesheuvel 2017-07-12 14:44 ` [kernel-hardening] " Ard Biesheuvel 2017-07-12 14:44 ` [RFC PATCH 04/10] arm64: kvm: stop treating register x18 as caller save Ard Biesheuvel 2017-07-12 14:44 ` [kernel-hardening] " Ard Biesheuvel 2017-07-12 14:44 ` [RFC PATCH 05/10] arm64: kernel: avoid x18 as an arbitrary temp register Ard Biesheuvel 2017-07-12 14:44 ` [kernel-hardening] " Ard Biesheuvel 2017-07-12 14:44 ` [RFC PATCH 06/10] arm64: kbuild: reserve reg x18 from general allocation by the compiler Ard Biesheuvel 2017-07-12 14:44 ` [kernel-hardening] " Ard Biesheuvel 2017-07-12 14:44 ` [RFC PATCH 07/10] arm64: kernel: switch to register x18 as a task struct pointer Ard Biesheuvel 2017-07-12 14:44 ` [kernel-hardening] " Ard Biesheuvel 2017-07-13 10:41 ` Dave Martin 2017-07-13 10:41 ` [kernel-hardening] " Dave Martin 2017-07-13 12:27 ` Ard Biesheuvel [this message] 2017-07-13 12:27 ` Ard Biesheuvel 2017-07-13 14:11 ` Dave Martin 2017-07-13 14:11 ` [kernel-hardening] " Dave Martin 2017-07-12 14:44 ` [RFC PATCH 08/10] arm64/kernel: dump entire stack if sp points elsewhere Ard Biesheuvel 2017-07-12 14:44 ` [kernel-hardening] " Ard Biesheuvel 2017-07-12 14:44 ` [RFC PATCH 09/10] arm64: mm: add C level handling for stack overflows Ard Biesheuvel 2017-07-12 14:44 ` [kernel-hardening] " Ard Biesheuvel 2017-07-12 14:44 ` [RFC PATCH 10/10] arm64: kernel: add support for virtually mapped stacks Ard Biesheuvel 2017-07-12 14:44 ` [kernel-hardening] " Ard Biesheuvel 2017-07-12 22:59 ` Mark Rutland 2017-07-12 22:59 ` [kernel-hardening] " Mark Rutland 2017-07-13 9:12 ` Mark Rutland 2017-07-13 9:12 ` Mark Rutland 2017-07-13 10:35 ` Dave Martin 2017-07-13 10:35 ` [kernel-hardening] " Dave Martin 2017-07-12 20:12 ` [RFC PATCH 00/10] arm64: allow virtually mapped stacks to be enabled Laura Abbott 2017-07-12 20:12 ` [kernel-hardening] " Laura Abbott 2017-07-12 20:49 ` Ard Biesheuvel 2017-07-12 20:49 ` [kernel-hardening] " Ard Biesheuvel 2017-07-12 21:32 ` Andy Lutomirski 2017-07-12 21:32 ` [kernel-hardening] " Andy Lutomirski 2017-07-12 22:47 ` Mark Rutland 2017-07-12 22:47 ` [kernel-hardening] " Mark Rutland 2017-07-13 6:51 ` Ard Biesheuvel 2017-07-13 6:51 ` [kernel-hardening] " Ard Biesheuvel
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=CAKv+Gu8gFSSYV5-11bZOMnQGMcvx0gSDnTpibciEPMv_3HpAUA@mail.gmail.com \ --to=ard.biesheuvel@linaro.org \ --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: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.