Skip to contentSkip to conversation

ARM: hw_breakpoint: don't fault when debug is not powered

PATCH New Linus Walleij 3 participants 3 messages Nov 9, 2011

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ARM: hw_breakpoint: don't fault when debug is not powered

From: Rabin Vincent <[email protected]>

If the debug logic is not powered, the processor can unfortunately raise an undefined instruction exception on the instruction that we use to check if the debug logic is powered up or not.

Handle this with an undef hook, so that the kernel doesn't crash on boot on such setups.

---
 arch/arm/kernel/hw_breakpoint.c |   25 +++++++++++++++++++++++++
 1 files changed, 25 insertions(+), 0 deletions(-)
arch/arm/kernel/hw_breakpoint.c +25 -0
index 814a52a9..f46137c 100644
@@ -854,6 +854,25 @@ static int hw_breakpoint_pending(unsigned long addr, unsigned int fsr,
 	return ret;
 }
 
+static int hw_breakpoint_undef(struct pt_regs *regs, unsigned int instr)
+{
+	int reg = (instr >> 12) & 15;
+
+	/* Fake sticky power-down cleared */
+	regs->uregs[reg] = 0;
+	regs->ARM_pc += 4;
+
+	return 0;
+}
+
+static struct undef_hook hw_breakpoint_hook = {
+	.instr_mask	= 0xffff0fff,
+	.instr_val	= 0xee110e95,
+	.cpsr_mask	= MODE_MASK,
+	.cpsr_val	= SVC_MODE,
+	.fn		= hw_breakpoint_undef,
+};
+
 /*
  * One-time initialisation.
  */
@@ -900,6 +919,10 @@ static void reset_ctrl_regs(void *unused)
 		/*
 		 * Ensure sticky power-down is clear (i.e. debug logic is
 		 * powered up).
+		 *
+		 * This could raise an undefined instruction exception.  If it
+		 * does, it is fixed up with an undef hook which constructs
+		 * a fake value with the sticky power-down bit cleared.
 		 */
 		asm volatile("mrc p14, 0, %0, c1, c5, 4" : "=r" (dbg_power));
 		if ((dbg_power & 0x1) == 0)
@@ -980,6 +1003,8 @@ static int __init arch_hw_breakpoint_init(void)
 	core_num_brps = get_num_brps();
 	core_num_wrps = get_num_wrps();
 
+	register_undef_hook(&hw_breakpoint_hook);
+
 	/*
 	 * We need to tread carefully here because DBGSWENABLE may be
 	 * driven low on this core and there isn't an architected way to
Signature · 1 line
1.7.3.2
#1 lore raw

PATCH 1/1 ARM: hw_breakpoint: don't fault when debug is not powered

Quoted 33 lines · Linus Walleij

From: Rabin Vincent <[email protected]>

If the debug logic is not powered, the processor can unfortunately raise an undefined instruction exception on the instruction that we use to check if the debug logic is powered up or not.

Handle this with an undef hook, so that the kernel doesn't crash on boot on such setups.

---
 arch/arm/kernel/hw_breakpoint.c |   25 +++++++++++++++++++++++++
 1 files changed, 25 insertions(+), 0 deletions(-)
arch/arm/kernel/hw_breakpoint.c +9 -0
index 814a52a9..f46137c 100644
@@ -854,6 +854,25 @@ static int hw_breakpoint_pending(unsigned long addr, unsigned int fsr,
 	return ret;
 }
 
+static int hw_breakpoint_undef(struct pt_regs *regs, unsigned int instr)
+{
+	int reg = (instr >> 12) & 15;
+
+	/* Fake sticky power-down cleared */
+	regs->uregs[reg] = 0;
+	regs->ARM_pc += 4;
+
+	return 0;

arch/arm/kernel/hw_breakpoint.c static int hw_breakpoint_pending(unsigned long addr, unsigned int fsr, · line 875

Quoted 10 lines
arch/arm/kernel/hw_breakpoint.c +10 -0
+}
+
+static struct undef_hook hw_breakpoint_hook = {
+	.instr_mask	= 0xffff0fff,
+	.instr_val	= 0xee110e95,
+	.cpsr_mask	= MODE_MASK,
+	.cpsr_val	= SVC_MODE,
+	.fn		= hw_breakpoint_undef,
+};
+

This should work OK on Thumb2 kernels as the encoding for the MRC instruction is the same as on ARM :-) Is it worth a comment to this effect?

Signature · 1 line
Tixy
#2 lore raw

PATCH 1/1 ARM: hw_breakpoint: don't fault when debug is not powered

In reply to Linus Walleij

Hi Linus,

Quoted 8 lines · Linus Walleij

From: Rabin Vincent <[email protected]>

If the debug logic is not powered, the processor can unfortunately raise an undefined instruction exception on the instruction that we use to check if the debug logic is powered up or not.

Handle this with an undef hook, so that the kernel doesn't crash on boot on such setups.

Actually, I've done exactly this already! It got merged in the recent merge window, so please take it for a spin.

Will