Skip to content

Commit fc84bc5

Browse files
brooniectmarinas
authored andcommitted
arm64/gcs: Context switch GCS state for EL0
There are two registers controlling the GCS state of EL0, GCSPR_EL0 which is the current GCS pointer and GCSCRE0_EL1 which has enable bits for the specific GCS functionality enabled for EL0. Manage these on context switch and process lifetime events, GCS is reset on exec(). Also ensure that any changes to the GCS memory are visible to other PEs and that changes from other PEs are visible on this one by issuing a GCSB DSYNC when moving to or from a thread with GCS. Since the current GCS configuration of a thread will be visible to userspace we store the configuration in the format used with userspace and provide a helper which configures the system register as needed. On systems that support GCS we always allow access to GCSPR_EL0, this facilitates reporting of GCS faults if userspace implements disabling of GCS on error - the GCS can still be discovered and examined even if GCS has been disabled. Reviewed-by: Catalin Marinas <[email protected]> Reviewed-by: Thiago Jung Bauermann <[email protected]> Signed-off-by: Mark Brown <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Catalin Marinas <[email protected]>
1 parent cfad706 commit fc84bc5

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed

arch/arm64/include/asm/gcs.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,28 @@ static inline u64 gcsss2(void)
4848
return Xt;
4949
}
5050

51+
#ifdef CONFIG_ARM64_GCS
52+
53+
static inline bool task_gcs_el0_enabled(struct task_struct *task)
54+
{
55+
return current->thread.gcs_el0_mode & PR_SHADOW_STACK_ENABLE;
56+
}
57+
58+
void gcs_set_el0_mode(struct task_struct *task);
59+
void gcs_free(struct task_struct *task);
60+
void gcs_preserve_current_state(void);
61+
62+
#else
63+
64+
static inline bool task_gcs_el0_enabled(struct task_struct *task)
65+
{
66+
return false;
67+
}
68+
69+
static inline void gcs_set_el0_mode(struct task_struct *task) { }
70+
static inline void gcs_free(struct task_struct *task) { }
71+
static inline void gcs_preserve_current_state(void) { }
72+
73+
#endif
74+
5175
#endif

arch/arm64/include/asm/processor.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ struct thread_struct {
185185
u64 svcr;
186186
u64 tpidr2_el0;
187187
u64 por_el0;
188+
#ifdef CONFIG_ARM64_GCS
189+
unsigned int gcs_el0_mode;
190+
u64 gcspr_el0;
191+
u64 gcs_base;
192+
u64 gcs_size;
193+
#endif
188194
};
189195

190196
static inline unsigned int thread_get_vl(struct thread_struct *thread,

arch/arm64/kernel/process.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include <asm/cacheflush.h>
5050
#include <asm/exec.h>
5151
#include <asm/fpsimd.h>
52+
#include <asm/gcs.h>
5253
#include <asm/mmu_context.h>
5354
#include <asm/mte.h>
5455
#include <asm/processor.h>
@@ -280,13 +281,33 @@ static void flush_poe(void)
280281
write_sysreg_s(POR_EL0_INIT, SYS_POR_EL0);
281282
}
282283

284+
#ifdef CONFIG_ARM64_GCS
285+
286+
static void flush_gcs(void)
287+
{
288+
if (!system_supports_gcs())
289+
return;
290+
291+
gcs_free(current);
292+
current->thread.gcs_el0_mode = 0;
293+
write_sysreg_s(GCSCRE0_EL1_nTR, SYS_GCSCRE0_EL1);
294+
write_sysreg_s(0, SYS_GCSPR_EL0);
295+
}
296+
297+
#else
298+
299+
static void flush_gcs(void) { }
300+
301+
#endif
302+
283303
void flush_thread(void)
284304
{
285305
fpsimd_flush_thread();
286306
tls_thread_flush();
287307
flush_ptrace_hw_breakpoint(current);
288308
flush_tagged_addr_state();
289309
flush_poe();
310+
flush_gcs();
290311
}
291312

292313
void arch_release_task_struct(struct task_struct *tsk)
@@ -484,6 +505,46 @@ static void entry_task_switch(struct task_struct *next)
484505
__this_cpu_write(__entry_task, next);
485506
}
486507

508+
#ifdef CONFIG_ARM64_GCS
509+
510+
void gcs_preserve_current_state(void)
511+
{
512+
current->thread.gcspr_el0 = read_sysreg_s(SYS_GCSPR_EL0);
513+
}
514+
515+
static void gcs_thread_switch(struct task_struct *next)
516+
{
517+
if (!system_supports_gcs())
518+
return;
519+
520+
/* GCSPR_EL0 is always readable */
521+
gcs_preserve_current_state();
522+
write_sysreg_s(next->thread.gcspr_el0, SYS_GCSPR_EL0);
523+
524+
if (current->thread.gcs_el0_mode != next->thread.gcs_el0_mode)
525+
gcs_set_el0_mode(next);
526+
527+
/*
528+
* Ensure that GCS memory effects of the 'prev' thread are
529+
* ordered before other memory accesses with release semantics
530+
* (or preceded by a DMB) on the current PE. In addition, any
531+
* memory accesses with acquire semantics (or succeeded by a
532+
* DMB) are ordered before GCS memory effects of the 'next'
533+
* thread. This will ensure that the GCS memory effects are
534+
* visible to other PEs in case of migration.
535+
*/
536+
if (task_gcs_el0_enabled(current) || task_gcs_el0_enabled(next))
537+
gcsb_dsync();
538+
}
539+
540+
#else
541+
542+
static void gcs_thread_switch(struct task_struct *next)
543+
{
544+
}
545+
546+
#endif
547+
487548
/*
488549
* Handle sysreg updates for ARM erratum 1418040 which affects the 32bit view of
489550
* CNTVCT, various other errata which require trapping all CNTVCT{,_EL0}
@@ -580,6 +641,7 @@ struct task_struct *__switch_to(struct task_struct *prev,
580641
cntkctl_thread_switch(prev, next);
581642
ptrauth_thread_switch_user(next);
582643
permission_overlay_switch(next);
644+
gcs_thread_switch(next);
583645

584646
/*
585647
* Complete any pending TLB or cache maintenance on this CPU in case

arch/arm64/mm/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ obj-$(CONFIG_TRANS_TABLE) += trans_pgd.o
1111
obj-$(CONFIG_TRANS_TABLE) += trans_pgd-asm.o
1212
obj-$(CONFIG_DEBUG_VIRTUAL) += physaddr.o
1313
obj-$(CONFIG_ARM64_MTE) += mteswap.o
14+
obj-$(CONFIG_ARM64_GCS) += gcs.o
1415
KASAN_SANITIZE_physaddr.o += n
1516

1617
obj-$(CONFIG_KASAN) += kasan_init.o

arch/arm64/mm/gcs.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
3+
#include <linux/mm.h>
4+
#include <linux/mman.h>
5+
#include <linux/syscalls.h>
6+
#include <linux/types.h>
7+
8+
#include <asm/cpufeature.h>
9+
#include <asm/page.h>
10+
11+
/*
12+
* Apply the GCS mode configured for the specified task to the
13+
* hardware.
14+
*/
15+
void gcs_set_el0_mode(struct task_struct *task)
16+
{
17+
u64 gcscre0_el1 = GCSCRE0_EL1_nTR;
18+
19+
if (task->thread.gcs_el0_mode & PR_SHADOW_STACK_ENABLE)
20+
gcscre0_el1 |= GCSCRE0_EL1_RVCHKEN | GCSCRE0_EL1_PCRSEL;
21+
22+
if (task->thread.gcs_el0_mode & PR_SHADOW_STACK_WRITE)
23+
gcscre0_el1 |= GCSCRE0_EL1_STREn;
24+
25+
if (task->thread.gcs_el0_mode & PR_SHADOW_STACK_PUSH)
26+
gcscre0_el1 |= GCSCRE0_EL1_PUSHMEn;
27+
28+
write_sysreg_s(gcscre0_el1, SYS_GCSCRE0_EL1);
29+
}
30+
31+
void gcs_free(struct task_struct *task)
32+
{
33+
if (!system_supports_gcs())
34+
return;
35+
36+
if (task->thread.gcs_base)
37+
vm_munmap(task->thread.gcs_base, task->thread.gcs_size);
38+
39+
task->thread.gcspr_el0 = 0;
40+
task->thread.gcs_base = 0;
41+
task->thread.gcs_size = 0;
42+
}

0 commit comments

Comments
 (0)