arm64: debug: unmask PSTATE.D earlier
Subsystems (2)
PATCH 1/3 arm64: debug: unmask PSTATE.D earlier
Clearing PSTATE.D is one of the requirements for generating a debug exception. The arm64 booting protocol requires that PSTATE.D is set, since many of the debug registers (for example, the hw_breakpoint registers) are UNKNOWN out of reset and could potentially generate spurious, fatal debug exceptions in early boot code if PSTATE.D was clear. Once the debug registers have been safely initialised, PSTATE.D is cleared, however this is currently broken for two reasons:
(1) The boot CPU clears PSTATE.D in a postcore_initcall and secondary CPUs clear PSTATE.D in secondary_start_kernel. Since the initcall runs after SMP (and the scheduler) have been initialised, there is no guarantee that it is actually running on the boot CPU. In this case, the boot CPU is left with PSTATE.D set and is not capable of generating debug exceptions.
(2) In a preemptible kernel, we may explicitly schedule on the IRQ return path to EL1. If an IRQ occurs with PSTATE.D set in the idle thread, then we may schedule the kthread_init thread, run the postcore_initcall to clear PSTATE.D and then context switch back to the idle thread before returning from the IRQ. The exception return patch will then restore PSTATE.D from the stack, and set it again.
This patch fixes the problem by moving the clearing of PSTATE.D earlier to proc.S. This has the desirable effect of clearing it in one place for all CPUs, long before we have to worry about the scheduler or any exception handling. We ensure that the previous reset of MDSCR_EL1 has completed before unmasking the exception, so that any spurious exceptions resulting from UNKNOWN debug registers are not generated.
Without this patch applied, the kprobes selftests have been seen to fail under KVM, where we end up attempting to step the OOL instruction buffer with PSTATE.D set and therefore fail to complete the step.
--- arch/arm64/kernel/debug-monitors.c | 1 - arch/arm64/kernel/smp.c | 1 - arch/arm64/mm/proc.S | 2 ++ 3 files changed, 2 insertions(+), 2 deletions(-)
arch/arm64/kernel/debug-monitors.c +0 -1
@@ -156,7 +156,6 @@ static int debug_monitors_init(void)/* Clear the OS lock. */ on_each_cpu (clear_os_lock,NULL ,1 );isb (); -local_dbg_enable ();/* Register hotplug handler. */ __register_cpu_notifier (&os_lock_nb);
arch/arm64/kernel/smp.c +0 -1
@@ -267,7 +267,6 @@ asmlinkage void secondary_start_kernel(void)set_cpu_online (cpu, true);complete (&cpu_running); -local_dbg_enable ();local_irq_enable ();local_async_enable ();
arch/arm64/mm/proc.S +2 -0
@@ -180,6 +180,8 @@ ENTRY(__cpu_setup) msr cpacr_el1, x0 // Enable FP/ASIMD mov x0, #1 << 12 // Reset mdscr_el1 and disable msr mdscr_el1, x0 // access to the DCC from EL0 + isb // Unmask debug exceptions now, + msr daifclr, #8 // since this is per-cpu reset_pmuserenr_el0 x0 // Disable PMU access from EL0 /* * Memory region attributes for LPAE:
Signature · 1 line
2.1.4
Quoted 15 lines · Will Deacon
Clearing PSTATE.D is one of the requirements for generating a debug exception. The arm64 booting protocol requires that PSTATE.D is set, since many of the debug registers (for example, the
hw_breakpointregisters) are UNKNOWN out of reset and could potentially generate spurious, fatal debug exceptions in early boot code if PSTATE.D was clear. Once the debug registers have been safely initialised, PSTATE.D is cleared, however this is currently broken for two reasons:(1) The boot CPU clears PSTATE.D in a
postcore_initcalland secondary CPUs clear PSTATE.D insecondary_start_kernel. Since the initcall runs after SMP (and the scheduler) have been initialised, there is no guarantee that it is actually running on the boot CPU. In this case, the boot CPU is left with PSTATE.D set and is not capable of generating debug exceptions.
Quoted 7 lines
(2) In a preemptible kernel, we may explicitly schedule on the IRQ return path to EL1. If an IRQ occurs with PSTATE.D set in the idle thread, then we may schedule the
kthread_initthread, run thepostcore_initcallto clear PSTATE.D and then context switch back to the idle thread before returning from the IRQ. The exception return patch will then restore PSTATE.D from the stack, and set it again.
Nit: s/patch/path/
Quoted 8 lines
This patch fixes the problem by moving the clearing of PSTATE.D earlier to
proc.S. This has the desirable effect of clearing it in one place for all CPUs, long before we have to worry about the scheduler or any exception handling. We ensure that the previous reset ofMDSCR_EL1has completed before unmasking the exception, so that any spurious exceptions resulting from UNKNOWN debug registers are not generated.
Quoted 7 lines
Without this patch applied, the kprobes selftests have been seen to fail under KVM, where we end up attempting to step the OOL instruction buffer with PSTATE.D set and therefore fail to complete the step.
Cc <[email protected]>Reported-by Catalin Marinas <[email protected]>Signed-off-by Will Deacon <[email protected]>
Otherwise, this looks good to me. Unifying the primary/secondary init is a nice cleanup regardless.
FWIW:
Thanks,
Mark.
Quoted 46 lines
--- arch/arm64/kernel/debug-monitors.c | 1 - arch/arm64/kernel/smp.c | 1 - arch/arm64/mm/proc.S | 2 ++ 3 files changed, 2 insertions(+), 2 deletions(-)arch/arm64/kernel/debug-monitors.c +0
-1@@ -156,7 +156,6 @@ static int debug_monitors_init(void)/* Clear the OS lock. */ on_each_cpu (clear_os_lock,NULL ,1 );isb (); -local_dbg_enable ();/* Register hotplug handler. */ __register_cpu_notifier (&os_lock_nb);arch/arm64/kernel/smp.c +0
-1@@ -267,7 +267,6 @@ asmlinkage void secondary_start_kernel(void)set_cpu_online (cpu, true);complete (&cpu_running); -local_dbg_enable ();local_irq_enable ();local_async_enable ();arch/arm64/mm/proc.S +2
-0@@ -180,6 +180,8 @@ ENTRY(__cpu_setup) msr cpacr_el1, x0 // Enable FP/ASIMD mov x0, #1 << 12 // Reset mdscr_el1 and disable msr mdscr_el1, x0 // access to the DCC from EL0 + isb // Unmask debug exceptions now, + msr daifclr, #8 // since this is per-cpu reset_pmuserenr_el0 x0 // Disable PMU access from EL0 /* * Memory region attributes for LPAE:Signature · 1 line
2.1.4
Quoted 30 lines · Will Deacon
Clearing PSTATE.D is one of the requirements for generating a debug exception. The arm64 booting protocol requires that PSTATE.D is set, since many of the debug registers (for example, the
hw_breakpointregisters) are UNKNOWN out of reset and could potentially generate spurious, fatal debug exceptions in early boot code if PSTATE.D was clear. Once the debug registers have been safely initialised, PSTATE.D is cleared, however this is currently broken for two reasons:(1) The boot CPU clears PSTATE.D in a
postcore_initcalland secondary CPUs clear PSTATE.D insecondary_start_kernel. Since the initcall runs after SMP (and the scheduler) have been initialised, there is no guarantee that it is actually running on the boot CPU. In this case, the boot CPU is left with PSTATE.D set and is not capable of generating debug exceptions.(2) In a preemptible kernel, we may explicitly schedule on the IRQ return path to EL1. If an IRQ occurs with PSTATE.D set in the idle thread, then we may schedule the
kthread_initthread, run thepostcore_initcallto clear PSTATE.D and then context switch back to the idle thread before returning from the IRQ. The exception return patch will then restore PSTATE.D from the stack, and set it again.This patch fixes the problem by moving the clearing of PSTATE.D earlier to
proc.S. This has the desirable effect of clearing it in one place for all CPUs, long before we have to worry about the scheduler or any exception handling. We ensure that the previous reset ofMDSCR_EL1has completed before unmasking the exception, so that any spurious exceptions resulting from UNKNOWN debug registers are not generated.
Quoted 7 lines
Without this patch applied, the kprobes selftests have been seen to fail under KVM, where we end up attempting to step the OOL instruction buffer with PSTATE.D set and therefore fail to complete the step.
Cc <[email protected]>Reported-by Catalin Marinas <[email protected]>Signed-off-by Will Deacon <[email protected]>
Gave it a spin on both Juno and Seattle, both host and KVM guest, and everything went smoothly, so:
Thanks,
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Quoted 60 lines · Will Deacon
Clearing PSTATE.D is one of the requirements for generating a debug exception. The arm64 booting protocol requires that PSTATE.D is set, since many of the debug registers (for example, the
hw_breakpointregisters) are UNKNOWN out of reset and could potentially generate spurious, fatal debug exceptions in early boot code if PSTATE.D was clear. Once the debug registers have been safely initialised, PSTATE.D is cleared, however this is currently broken for two reasons:(1) The boot CPU clears PSTATE.D in a
postcore_initcalland secondary CPUs clear PSTATE.D insecondary_start_kernel. Since the initcall runs after SMP (and the scheduler) have been initialised, there is no guarantee that it is actually running on the boot CPU. In this case, the boot CPU is left with PSTATE.D set and is not capable of generating debug exceptions.(2) In a preemptible kernel, we may explicitly schedule on the IRQ return path to EL1. If an IRQ occurs with PSTATE.D set in the idle thread, then we may schedule the
kthread_initthread, run thepostcore_initcallto clear PSTATE.D and then context switch back to the idle thread before returning from the IRQ. The exception return patch will then restore PSTATE.D from the stack, and set it again.This patch fixes the problem by moving the clearing of PSTATE.D earlier to
proc.S. This has the desirable effect of clearing it in one place for all CPUs, long before we have to worry about the scheduler or any exception handling. We ensure that the previous reset ofMDSCR_EL1has completed before unmasking the exception, so that any spurious exceptions resulting from UNKNOWN debug registers are not generated.Without this patch applied, the kprobes selftests have been seen to fail under KVM, where we end up attempting to step the OOL instruction buffer with PSTATE.D set and therefore fail to complete the step.
Cc <[email protected]>Reported-by Catalin Marinas <[email protected]>Signed-off-by Will Deacon <[email protected]>--- arch/arm64/kernel/debug-monitors.c | 1 - arch/arm64/kernel/smp.c | 1 - arch/arm64/mm/proc.S | 2 ++ 3 files changed, 2 insertions(+), 2 deletions(-)arch/arm64/kernel/debug-monitors.c +0
-1@@ -156,7 +156,6 @@ static int debug_monitors_init(void)/* Clear the OS lock. */ on_each_cpu (clear_os_lock,NULL ,1 );isb (); -local_dbg_enable ();/* Register hotplug handler. */ __register_cpu_notifier (&os_lock_nb);arch/arm64/kernel/smp.c +0
-0
arch/arm64/kernel/smp.c asmlinkage void secondary_start_kernel(void) · line 270
Quoted 4 lines
arch/arm64/kernel/smp.c +0
-1@@ -267,7 +267,6 @@ asmlinkage void secondary_start_kernel(void)set_cpu_online (cpu, true);complete (&cpu_running); -local_dbg_enable ();
The above look to be the only uses of local_dbg_enable() - do we still need to keep the definition?
Robin.
Quoted 17 lines
arch/arm64/kernel/smp.c +0
-0local_irq_enable ();local_async_enable ();arch/arm64/mm/proc.S +2
-0@@ -180,6 +180,8 @@ ENTRY(__cpu_setup) msr cpacr_el1, x0 // Enable FP/ASIMD mov x0, #1 << 12 // Reset mdscr_el1 and disable msr mdscr_el1, x0 // access to the DCC from EL0 + isb // Unmask debug exceptions now, + msr daifclr, #8 // since this is per-cpu reset_pmuserenr_el0 x0 // Disable PMU access from EL0 /* * Memory region attributes for LPAE: