Skip to content

Commit d49f7d7

Browse files
Marc Zyngierctmarinas
authored andcommitted
arm64: Move handling of erratum 1418040 into C code
Instead of dealing with erratum 1418040 on each entry and exit, let's move the handling to __switch_to() instead, which has several advantages: - It can be applied when it matters (switching between 32 and 64 bit tasks). - It is written in C (yay!) - It can rely on static keys rather than alternatives Signed-off-by: Marc Zyngier <[email protected]> Tested-by: Sai Prakash Ranjan <[email protected]> Reviewed-by: Stephen Boyd <[email protected]> Acked-by: Will Deacon <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Catalin Marinas <[email protected]>
1 parent 9123e3a commit d49f7d7

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

arch/arm64/kernel/entry.S

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -170,19 +170,6 @@ alternative_cb_end
170170
stp x28, x29, [sp, #16 * 14]
171171

172172
.if \el == 0
173-
.if \regsize == 32
174-
/*
175-
* If we're returning from a 32-bit task on a system affected by
176-
* 1418040 then re-enable userspace access to the virtual counter.
177-
*/
178-
#ifdef CONFIG_ARM64_ERRATUM_1418040
179-
alternative_if ARM64_WORKAROUND_1418040
180-
mrs x0, cntkctl_el1
181-
orr x0, x0, #2 // ARCH_TIMER_USR_VCT_ACCESS_EN
182-
msr cntkctl_el1, x0
183-
alternative_else_nop_endif
184-
#endif
185-
.endif
186173
clear_gp_regs
187174
mrs x21, sp_el0
188175
ldr_this_cpu tsk, __entry_task, x20
@@ -294,14 +281,6 @@ alternative_else_nop_endif
294281
tst x22, #PSR_MODE32_BIT // native task?
295282
b.eq 3f
296283

297-
#ifdef CONFIG_ARM64_ERRATUM_1418040
298-
alternative_if ARM64_WORKAROUND_1418040
299-
mrs x0, cntkctl_el1
300-
bic x0, x0, #2 // ARCH_TIMER_USR_VCT_ACCESS_EN
301-
msr cntkctl_el1, x0
302-
alternative_else_nop_endif
303-
#endif
304-
305284
#ifdef CONFIG_ARM64_ERRATUM_845719
306285
alternative_if ARM64_WORKAROUND_845719
307286
#ifdef CONFIG_PID_IN_CONTEXTIDR

arch/arm64/kernel/process.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,39 @@ static void entry_task_switch(struct task_struct *next)
515515
__this_cpu_write(__entry_task, next);
516516
}
517517

518+
/*
519+
* ARM erratum 1418040 handling, affecting the 32bit view of CNTVCT.
520+
* Assuming the virtual counter is enabled at the beginning of times:
521+
*
522+
* - disable access when switching from a 64bit task to a 32bit task
523+
* - enable access when switching from a 32bit task to a 64bit task
524+
*/
525+
static void erratum_1418040_thread_switch(struct task_struct *prev,
526+
struct task_struct *next)
527+
{
528+
bool prev32, next32;
529+
u64 val;
530+
531+
if (!(IS_ENABLED(CONFIG_ARM64_ERRATUM_1418040) &&
532+
cpus_have_const_cap(ARM64_WORKAROUND_1418040)))
533+
return;
534+
535+
prev32 = is_compat_thread(task_thread_info(prev));
536+
next32 = is_compat_thread(task_thread_info(next));
537+
538+
if (prev32 == next32)
539+
return;
540+
541+
val = read_sysreg(cntkctl_el1);
542+
543+
if (!next32)
544+
val |= ARCH_TIMER_USR_VCT_ACCESS_EN;
545+
else
546+
val &= ~ARCH_TIMER_USR_VCT_ACCESS_EN;
547+
548+
write_sysreg(val, cntkctl_el1);
549+
}
550+
518551
/*
519552
* Thread switching.
520553
*/
@@ -530,6 +563,7 @@ __notrace_funcgraph struct task_struct *__switch_to(struct task_struct *prev,
530563
entry_task_switch(next);
531564
uao_thread_switch(next);
532565
ssbs_thread_switch(next);
566+
erratum_1418040_thread_switch(prev, next);
533567

534568
/*
535569
* Complete any pending TLB or cache maintenance on this CPU in case

0 commit comments

Comments
 (0)