Skip to content

Commit ccd4770

Browse files
npigginmpe
authored andcommitted
powerpc/64s: Fix HV NMI vs HV interrupt recoverability test
HV interrupts that use HSRR registers do not enter with MSR[RI] clear, but their entry code is not recoverable vs NMI, due to shared use of HSPRG1 as a scratch register to save r13. This means that a system reset or machine check that hits in HSRR interrupt entry can cause r13 to be silently corrupted. Fix this by marking NMIs non-recoverable if they land in HV interrupt ranges. Signed-off-by: Nicholas Piggin <npiggin@gmail.com> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
1 parent 3b4d07d commit ccd4770

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed

arch/powerpc/include/asm/asm-prototypes.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ int exit_vmx_usercopy(void);
5151
int enter_vmx_ops(void);
5252
void *exit_vmx_ops(void *dest);
5353

54+
/* Exceptions */
55+
#ifdef CONFIG_PPC_POWERNV
56+
extern unsigned long real_trampolines_start;
57+
extern unsigned long real_trampolines_end;
58+
extern unsigned long virt_trampolines_start;
59+
extern unsigned long virt_trampolines_end;
60+
#endif
61+
5462
/* Traps */
5563
long machine_check_early(struct pt_regs *regs);
5664
long hmi_exception_realmode(struct pt_regs *regs);

arch/powerpc/include/asm/nmi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ extern void arch_trigger_cpumask_backtrace(const cpumask_t *mask,
1414
#define arch_trigger_cpumask_backtrace arch_trigger_cpumask_backtrace
1515
#endif
1616

17+
extern void hv_nmi_check_nonrecoverable(struct pt_regs *regs);
18+
1719
#endif /* _ASM_NMI_H */

arch/powerpc/kernel/exceptions-64s.S

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ OPEN_FIXED_SECTION(real_vectors, 0x0100, 0x1900)
6868
OPEN_FIXED_SECTION(real_trampolines, 0x1900, 0x4000)
6969
OPEN_FIXED_SECTION(virt_vectors, 0x4000, 0x5900)
7070
OPEN_FIXED_SECTION(virt_trampolines, 0x5900, 0x7000)
71+
72+
#ifdef CONFIG_PPC_POWERNV
73+
.globl real_trampolines_start
74+
.globl real_trampolines_end
75+
.globl virt_trampolines_start
76+
.globl virt_trampolines_end
77+
#endif
78+
7179
#if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
7280
/*
7381
* Data area reserved for FWNMI option.

arch/powerpc/kernel/mce.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include <asm/machdep.h>
3333
#include <asm/mce.h>
34+
#include <asm/nmi.h>
3435

3536
static DEFINE_PER_CPU(int, mce_nest_count);
3637
static DEFINE_PER_CPU(struct machine_check_event[MAX_MC_EVT], mce_event);
@@ -490,6 +491,8 @@ long machine_check_early(struct pt_regs *regs)
490491
{
491492
long handled = 0;
492493

494+
hv_nmi_check_nonrecoverable(regs);
495+
493496
/*
494497
* See if platform is capable of handling machine check.
495498
*/

arch/powerpc/kernel/traps.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,70 @@ void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
369369
force_sig_fault(signr, code, (void __user *)addr, current);
370370
}
371371

372+
/*
373+
* The interrupt architecture has a quirk in that the HV interrupts excluding
374+
* the NMIs (0x100 and 0x200) do not clear MSR[RI] at entry. The first thing
375+
* that an interrupt handler must do is save off a GPR into a scratch register,
376+
* and all interrupts on POWERNV (HV=1) use the HSPRG1 register as scratch.
377+
* Therefore an NMI can clobber an HV interrupt's live HSPRG1 without noticing
378+
* that it is non-reentrant, which leads to random data corruption.
379+
*
380+
* The solution is for NMI interrupts in HV mode to check if they originated
381+
* from these critical HV interrupt regions. If so, then mark them not
382+
* recoverable.
383+
*
384+
* An alternative would be for HV NMIs to use SPRG for scratch to avoid the
385+
* HSPRG1 clobber, however this would cause guest SPRG to be clobbered. Linux
386+
* guests should always have MSR[RI]=0 when its scratch SPRG is in use, so
387+
* that would work. However any other guest OS that may have the SPRG live
388+
* and MSR[RI]=1 could encounter silent corruption.
389+
*
390+
* Builds that do not support KVM could take this second option to increase
391+
* the recoverability of NMIs.
392+
*/
393+
void hv_nmi_check_nonrecoverable(struct pt_regs *regs)
394+
{
395+
#ifdef CONFIG_PPC_POWERNV
396+
unsigned long kbase = (unsigned long)_stext;
397+
unsigned long nip = regs->nip;
398+
399+
if (!(regs->msr & MSR_RI))
400+
return;
401+
if (!(regs->msr & MSR_HV))
402+
return;
403+
if (regs->msr & MSR_PR)
404+
return;
405+
406+
/*
407+
* Now test if the interrupt has hit a range that may be using
408+
* HSPRG1 without having RI=0 (i.e., an HSRR interrupt). The
409+
* problem ranges all run un-relocated. Test real and virt modes
410+
* at the same time by droping the high bit of the nip (virt mode
411+
* entry points still have the +0x4000 offset).
412+
*/
413+
nip &= ~0xc000000000000000ULL;
414+
if ((nip >= 0x500 && nip < 0x600) || (nip >= 0x4500 && nip < 0x4600))
415+
goto nonrecoverable;
416+
if ((nip >= 0x980 && nip < 0xa00) || (nip >= 0x4980 && nip < 0x4a00))
417+
goto nonrecoverable;
418+
if ((nip >= 0xe00 && nip < 0xec0) || (nip >= 0x4e00 && nip < 0x4ec0))
419+
goto nonrecoverable;
420+
if ((nip >= 0xf80 && nip < 0xfa0) || (nip >= 0x4f80 && nip < 0x4fa0))
421+
goto nonrecoverable;
422+
/* Trampoline code runs un-relocated so subtract kbase. */
423+
if (nip >= real_trampolines_start - kbase &&
424+
nip < real_trampolines_end - kbase)
425+
goto nonrecoverable;
426+
if (nip >= virt_trampolines_start - kbase &&
427+
nip < virt_trampolines_end - kbase)
428+
goto nonrecoverable;
429+
return;
430+
431+
nonrecoverable:
432+
regs->msr &= ~MSR_RI;
433+
#endif
434+
}
435+
372436
void system_reset_exception(struct pt_regs *regs)
373437
{
374438
/*
@@ -379,6 +443,8 @@ void system_reset_exception(struct pt_regs *regs)
379443
if (!nested)
380444
nmi_enter();
381445

446+
hv_nmi_check_nonrecoverable(regs);
447+
382448
__this_cpu_inc(irq_stat.sreset_irqs);
383449

384450
/* See if any machine dependent calls */

0 commit comments

Comments
 (0)