Skip to contentSkip to conversation

arm64: Use BRK instruction for generic BUG traps

PATCH New Dave Martin 1 participant 10 messages Jul 24, 2015 arm64

lore
Share & tools
lore
#0 lore raw

v3 cover arm64: Use BRK instruction for generic BUG traps

Final repost to address outstanding minor comments.

Note that I have dropped tags from the following non-trivially changed patches:

* patch 7/9 arm64/debug: Add missing #includes

(Merged in remnants of dropped patch "arm64: esr.h type fixes and cleanup")

* patch 6/9 arm64/debug: Simplify BRK insn opcode declarations

(Added truncating type casts for ESR value assignment, to silence compiler warnings.)

Changes since v2:

* Get rid of broad-brush type annotation changes in <asm/esr.h>. They are replaced with a couple of explicit casts where the affected constants are used. The only purpose of this is to silence harmless compiler warnings.

Changes since v1:

* Modified BRK immediate for BUG so that it doesn't overlap the range allocated for KGDB.

* Typo fixes.

* Don't assume that BUG() kills the thread, so that catching BUGs in kgdb works again.

* Separate header for the BRK immediates removed, at Will's request. I've retained the other refactoring since it contains useful tidy- ups, but some of that could go away if desired.

Original cover letter:

Currently, the minimal default BUG() implementation from asm-generic is used for arm64.

This series uses the BRK software breakpoint instruction to generate a trap instead, similarly to most other arches, with the generic BUG code generating the dmesg boilerplate. This eliminates a fair amount of inlined code at BUG() and WARN() sites.

This work makes it look increasingly desirable to collect BRK immediates together in one place. Patches 1-7 do some refactoring to prepare for this, and patch 8 moves the definitions to a fresh header, <asm/brk.h>.

Patch 9 provides the BRK-based GENERIC_BUG support for arm64.

A side-effect of this change is that WARNs are now generated via a different bit of generic code (lib/bug.c:report_bug()) that no longer prints a backtrace (compare kernel/panic.c:warn_slowpath_common()).) I will post a separate mini-RFC series to address that in the generic code. Patch 10 hacks a backtrace back into the arm64 arch code in the meantime.

[...]

Dave Martin (9):
arm64/debug: Eliminate magic number for size of BRK instruction arm64/debug: Mask off all reserved bits from generated ESR values arm64/debug: Eliminate magic number from ESR template definition arm64/debug: More consistent naming for the BRK ESR template macro arm64/debug: Move BRK ESR template macro into <asm/esr.h> arm64/debug: Simplify BRK insn opcode declarations arm64/debug: Add missing #includes
arm64/BUG: Use BRK instruction for generic BUG traps arm64/BUG: Show explicit backtrace for WARNs

 arch/arm64/Kconfig                      |    8 ++++
 arch/arm64/include/asm/bug.h            |   64 +++++++++++++++++++++++++++++++
 arch/arm64/include/asm/debug-monitors.h |   38 +++++++-----------
 arch/arm64/include/asm/esr.h            |    9 +++++
 arch/arm64/kernel/kgdb.c                |   12 +++---
 arch/arm64/kernel/traps.c               |   61 ++++++++++++++++++++++++++++-
 arch/arm64/mm/fault.c                   |   12 +++++-
 7 files changed, 170 insertions(+), 34 deletions(-)
 create mode 100644 arch/arm64/include/asm/bug.h
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH v3 1/9 arm64/debug: Eliminate magic number for size of BRK instruction

The size of an A64 BRK instruction is the same as the size of all other A64 instructions, because all A64 instructions are the same size.

BREAK_INSTR_SIZE is retained for readibility, but it should not be an independent constant from AARCH64_INSN_SIZE.

---
 arch/arm64/include/asm/debug-monitors.h |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm64/include/asm/debug-monitors.h +3 -1
index 40ec68a..f3d2dbd 100644
@@ -18,6 +18,8 @@
 
 #ifdef __KERNEL__
 
+#include <asm/insn.h>
+
 /* Low-level stepping controls. */
 #define DBG_MDSCR_SS		(1 << 0)
 #define DBG_SPSR_SS		(1 << 21)
@@ -38,7 +40,7 @@
 /*
  * Break point instruction encoding
  */
-#define BREAK_INSTR_SIZE		4
+#define BREAK_INSTR_SIZE		AARCH64_INSN_SIZE
 
 /*
  * ESR values expected for dynamic and compile time BRK instruction
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH v3 2/9 arm64/debug: Mask off all reserved bits from generated ESR values

In reply to Dave Martin

There are only 16 comment bits in a BRK instruction, which correspond to ESR bits 15:0. Bits 24:16 of the ESR are RES0, and might have weird meanings in the future.

This code inserts 16 bits of comment in the ESR value instead of 20 (almost certainly a typo in the original code).

---
 arch/arm64/include/asm/debug-monitors.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/include/asm/debug-monitors.h +1 -1
index f3d2dbd..ab7d5a8 100644
@@ -45,7 +45,7 @@
 /*
  * ESR values expected for dynamic and compile time BRK instruction
  */
-#define DBG_ESR_VAL_BRK(x)	(0xf2000000 | ((x) & 0xfffff))
+#define DBG_ESR_VAL_BRK(x)	(0xf2000000 | ((x) & 0xffff))
 
 /*
  * #imm16 values used for BRK instruction generation
Signature · 1 line
1.7.10.4
#3 lore raw

PATCH v3 3/9 arm64/debug: Eliminate magic number from ESR template definition

In reply to Dave Martin

<asm/esr.h> has perfectly good constants for defining ESR values already. Let's use them.

---
 arch/arm64/include/asm/debug-monitors.h |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm64/include/asm/debug-monitors.h +3 -1
index ab7d5a8..ff09058 100644
@@ -18,6 +18,7 @@
 
 #ifdef __KERNEL__
 
+#include <asm/esr.h>
 #include <asm/insn.h>
 
 /* Low-level stepping controls. */
@@ -45,7 +46,8 @@
 /*
  * ESR values expected for dynamic and compile time BRK instruction
  */
-#define DBG_ESR_VAL_BRK(x)	(0xf2000000 | ((x) & 0xffff))
+#define DBG_ESR_VAL_BRK(x) \
+	((ESR_ELx_EC_BRK64 << ESR_ELx_EC_SHIFT) | ESR_ELx_IL | ((x) & 0xffff))
 
 /*
  * #imm16 values used for BRK instruction generation
Signature · 1 line
1.7.10.4
#4 lore raw

PATCH v3 4/9 arm64/debug: More consistent naming for the BRK ESR template macro

In reply to Dave Martin

The naming of DBG_ESR_VAL_BRK is inconsistent with the way other similar macros are named.

This patch makes the naming more consistent, and appends "64" as a reminder that this ESR pattern only matches from AArch64 state.

---
 arch/arm64/include/asm/debug-monitors.h |    5 +++--
 arch/arm64/kernel/kgdb.c                |    4 ++--
 2 files changed, 5 insertions(+), 4 deletions(-)
arch/arm64/include/asm/debug-monitors.h +3 -2
index ff09058..bb97e9d 100644
@@ -46,8 +46,9 @@
 /*
  * ESR values expected for dynamic and compile time BRK instruction
  */
-#define DBG_ESR_VAL_BRK(x) \
-	((ESR_ELx_EC_BRK64 << ESR_ELx_EC_SHIFT) | ESR_ELx_IL | ((x) & 0xffff))
+#define ESR_ELx_VAL_BRK64(imm)					\
+	((ESR_ELx_EC_BRK64 << ESR_ELx_EC_SHIFT) | ESR_ELx_IL |	\
+	 ((imm) & 0xffff))
 
 /*
  * #imm16 values used for BRK instruction generation
arch/arm64/kernel/kgdb.c +2 -2
index a0d10c5..a5a838e 100644
@@ -235,13 +235,13 @@ static int kgdb_step_brk_fn(struct pt_regs *regs, unsigned int esr)
 
 static struct break_hook kgdb_brkpt_hook = {
 	.esr_mask	= 0xffffffff,
-	.esr_val	= DBG_ESR_VAL_BRK(KGDB_DYN_DBG_BRK_IMM),
+	.esr_val	= ESR_ELx_VAL_BRK64(KGDB_DYN_DBG_BRK_IMM),
 	.fn		= kgdb_brk_fn
 };
 
 static struct break_hook kgdb_compiled_brkpt_hook = {
 	.esr_mask	= 0xffffffff,
-	.esr_val	= DBG_ESR_VAL_BRK(KGDB_COMPILED_DBG_BRK_IMM),
+	.esr_val	= ESR_ELx_VAL_BRK64(KGDB_COMPILED_DBG_BRK_IMM),
 	.fn		= kgdb_compiled_brk_fn
 };
 
Signature · 1 line
1.7.10.4
#5 lore raw

PATCH v3 5/9 arm64/debug: Move BRK ESR template macro into <asm/esr.h>

In reply to Dave Martin

It makes sense to keep all the architectural exception syndrome definitions in the same place.

---
 arch/arm64/include/asm/debug-monitors.h |    7 -------
 arch/arm64/include/asm/esr.h            |    7 +++++++
 2 files changed, 7 insertions(+), 7 deletions(-)
arch/arm64/include/asm/debug-monitors.h +0 -7
index bb97e9d..e28b1dd 100644
@@ -44,13 +44,6 @@
 #define BREAK_INSTR_SIZE		AARCH64_INSN_SIZE
 
 /*
- * ESR values expected for dynamic and compile time BRK instruction
- */
-#define ESR_ELx_VAL_BRK64(imm)					\
-	((ESR_ELx_EC_BRK64 << ESR_ELx_EC_SHIFT) | ESR_ELx_IL |	\
-	 ((imm) & 0xffff))
-
-/*
  * #imm16 values used for BRK instruction generation
  * Allowed values for kgbd are 0x400 - 0x7ff
  * 0x100: for triggering a fault on purpose (reserved)
arch/arm64/include/asm/esr.h +7 -0
index 7052245..1b44cf6 100644
@@ -99,6 +99,13 @@
 #define ESR_ELx_WFx_ISS_WFE	(UL(1) << 0)
 #define ESR_ELx_xVC_IMM_MASK	((1UL << 16) - 1)
 
+/* ESR value templates for specific events */
+
+/* BRK instruction trap from AArch64 state */
+#define ESR_ELx_VAL_BRK64(imm)					\
+	((ESR_ELx_EC_BRK64 << ESR_ELx_EC_SHIFT) | ESR_ELx_IL |	\
+	 ((imm) & 0xffff))
+
 #ifndef __ASSEMBLY__
 #include <asm/types.h>
 
Signature · 1 line
1.7.10.4
#6 lore raw

PATCH v3 6/9 arm64/debug: Simplify BRK insn opcode declarations

In reply to Dave Martin

The way the KGDB_DYN_BRK_INS_BYTEx macros are declared is more complex than it needs to be. Also, the macros are only used in one place, which is arch-specific anyway.

This patch refactors the macros to simplify them, and exposes an argument so that we can have a single macro instead of 4.

As a side effect, this patch also fixes some anomalous spellings of "KGDB".

These changes alter the compile types of some integer constants that are harmless but trigger truncation warnings in gcc when assigning to 32-bit variables. This patch adds an explicit cast for the affected cases.

---
 arch/arm64/include/asm/debug-monitors.h |   23 ++++-------------------
 arch/arm64/kernel/kgdb.c                |   12 ++++++------
 2 files changed, 10 insertions(+), 25 deletions(-)
arch/arm64/include/asm/debug-monitors.h +4 -19
index e28b1dd..6a17fb8 100644
@@ -66,25 +66,10 @@
  */
 #define AARCH64_BREAK_FAULT	(AARCH64_BREAK_MON | (FAULT_BRK_IMM << 5))
 
-/*
- * Extract byte from BRK instruction
- */
-#define KGDB_DYN_DBG_BRK_INS_BYTE(x) \
-	((((AARCH64_BREAK_MON) & 0xffe0001f) >> (x * 8)) & 0xff)
-
-/*
- * Extract byte from BRK #imm16
- */
-#define KGBD_DYN_DBG_BRK_IMM_BYTE(x) \
-	(((((KGDB_DYN_DBG_BRK_IMM) & 0xffff) << 5) >> (x * 8)) & 0xff)
-
-#define KGDB_DYN_DBG_BRK_BYTE(x) \
-	(KGDB_DYN_DBG_BRK_INS_BYTE(x) | KGBD_DYN_DBG_BRK_IMM_BYTE(x))
-
-#define  KGDB_DYN_BRK_INS_BYTE0  KGDB_DYN_DBG_BRK_BYTE(0)
-#define  KGDB_DYN_BRK_INS_BYTE1  KGDB_DYN_DBG_BRK_BYTE(1)
-#define  KGDB_DYN_BRK_INS_BYTE2  KGDB_DYN_DBG_BRK_BYTE(2)
-#define  KGDB_DYN_BRK_INS_BYTE3  KGDB_DYN_DBG_BRK_BYTE(3)
+#define AARCH64_BREAK_KGDB_DYN_DBG	\
+	(AARCH64_BREAK_MON | (KGDB_DYN_DBG_BRK_IMM << 5))
+#define KGDB_DYN_BRK_INS_BYTE(x)	\
+	((AARCH64_BREAK_KGDB_DYN_DBG >> (8 * (x))) & 0xff)
 
 #define CACHE_FLUSH_IS_SAFE		1
 
arch/arm64/kernel/kgdb.c +6 -6
index a5a838e..bcac81e 100644
@@ -235,13 +235,13 @@ static int kgdb_step_brk_fn(struct pt_regs *regs, unsigned int esr)
 
 static struct break_hook kgdb_brkpt_hook = {
 	.esr_mask	= 0xffffffff,
-	.esr_val	= ESR_ELx_VAL_BRK64(KGDB_DYN_DBG_BRK_IMM),
+	.esr_val	= (u32)ESR_ELx_VAL_BRK64(KGDB_DYN_DBG_BRK_IMM),
 	.fn		= kgdb_brk_fn
 };
 
 static struct break_hook kgdb_compiled_brkpt_hook = {
 	.esr_mask	= 0xffffffff,
-	.esr_val	= ESR_ELx_VAL_BRK64(KGDB_COMPILED_DBG_BRK_IMM),
+	.esr_val	= (u32)ESR_ELx_VAL_BRK64(KGDB_COMPILED_DBG_BRK_IMM),
 	.fn		= kgdb_compiled_brk_fn
 };
 
@@ -328,9 +328,9 @@ void kgdb_arch_exit(void)
  */
 struct kgdb_arch arch_kgdb_ops = {
 	.gdb_bpt_instr = {
-		KGDB_DYN_BRK_INS_BYTE0,
-		KGDB_DYN_BRK_INS_BYTE1,
-		KGDB_DYN_BRK_INS_BYTE2,
-		KGDB_DYN_BRK_INS_BYTE3,
+		KGDB_DYN_BRK_INS_BYTE(0),
+		KGDB_DYN_BRK_INS_BYTE(1),
+		KGDB_DYN_BRK_INS_BYTE(2),
+		KGDB_DYN_BRK_INS_BYTE(3),
 	}
 };
Signature · 1 line
1.7.10.4
#7 lore raw

PATCH v3 7/9 arm64/debug: Add missing #includes

In reply to Dave Martin

<asm/debug-monitors.h> relies on <asm/ptrace.h>, but doesn't declare this dependency. This becomes a problem once debug-monitors.h starts getting included all over the place to get the BRK immedates.

The missing include of <asm/memory.h> (for UL()) in <asm/esr.h> is also added. The series no longer relies on this, but I spotted it during development and it may as well get fixed.

No functional change.

---
 arch/arm64/include/asm/debug-monitors.h |    3 +++
 arch/arm64/include/asm/esr.h            |    2 ++
 2 files changed, 5 insertions(+)
arch/arm64/include/asm/debug-monitors.h +3 -0
index 6a17fb8..777c36a 100644
@@ -18,8 +18,11 @@
 
 #ifdef __KERNEL__
 
+#include <linux/errno.h>
+#include <linux/types.h>
 #include <asm/esr.h>
 #include <asm/insn.h>
+#include <asm/ptrace.h>
 
 /* Low-level stepping controls. */
 #define DBG_MDSCR_SS		(1 << 0)
arch/arm64/include/asm/esr.h +2 -0
index 1b44cf6..77eeb2c 100644
@@ -18,6 +18,8 @@
 #ifndef __ASM_ESR_H
 #define __ASM_ESR_H
 
+#include <asm/memory.h>
+
 #define ESR_ELx_EC_UNKNOWN	(0x00)
 #define ESR_ELx_EC_WFx		(0x01)
 /* Unallocated EC: 0x02 */
Signature · 1 line
1.7.10.4
#8 lore raw

PATCH v3 8/9 arm64/BUG: Use BRK instruction for generic BUG traps

In reply to Dave Martin

Currently, the minimal default BUG() implementation from asm- generic is used for arm64.

This patch uses the BRK software breakpoint instruction to generate a trap instead, similarly to most other arches, with the generic BUG code generating the dmesg boilerplate.

This allows bug metadata to be moved to a separate table and reduces the amount of inline code at BUG and WARN sites. This also avoids clobbering any registers before they can be dumped.

To mitigate the size of the bug table further, this patch makes use of the existing infrastructure for encoding addresses within the bug table as 32-bit offsets instead of absolute pointers. (Note that this limits the kernel size to 2GB.)

Traps are registered at arch_initcall time for aarch64, but BUG has minimal real dependencies and it is desirable to be able to generate bug splats as early as possible. This patch redirects all debug exceptions caused by BRK directly to bug_handler() until the full debug exception support has been initialised.

---
 arch/arm64/Kconfig                      |    8 ++++
 arch/arm64/include/asm/bug.h            |   64 +++++++++++++++++++++++++++++++
 arch/arm64/include/asm/debug-monitors.h |    2 +
 arch/arm64/kernel/traps.c               |   59 +++++++++++++++++++++++++++-
 arch/arm64/mm/fault.c                   |   12 +++++-
 5 files changed, 142 insertions(+), 3 deletions(-)
 create mode 100644 arch/arm64/include/asm/bug.h
arch/arm64/Kconfig +8 -0
index 318175f..b918286 100644
@@ -113,6 +113,14 @@ config TRACE_IRQFLAGS_SUPPORT
 config RWSEM_XCHGADD_ALGORITHM
 	def_bool y
 
+config GENERIC_BUG
+	def_bool y
+	depends on BUG
+
+config GENERIC_BUG_RELATIVE_POINTERS
+	def_bool y
+	depends on GENERIC_BUG
+
 config GENERIC_HWEIGHT
 	def_bool y
 
arch/arm64/include/asm/bug.h new file +64 -0

64 lines not on this page · raw message

arch/arm64/include/asm/debug-monitors.h +2 -0
index 777c36a..e3f2bad 100644
@@ -52,10 +52,12 @@
  * 0x100: for triggering a fault on purpose (reserved)
  * 0x400: for dynamic BRK instruction
  * 0x401: for compile time BRK instruction
+ * 0x800: kernel-mode BUG() and WARN() traps
  */
 #define FAULT_BRK_IMM			0x100
 #define KGDB_DYN_DBG_BRK_IMM		0x400
 #define KGDB_COMPILED_DBG_BRK_IMM	0x401
+#define BUG_BRK_IMM			0x800
 
 /*
  * BRK instruction encoding
arch/arm64/kernel/traps.c +58 -1

79 lines not on this page · raw message

arch/arm64/mm/fault.c +10 -2
index 94d98cd..49da50d 100644
@@ -492,14 +492,22 @@ asmlinkage void __exception do_sp_pc_abort(unsigned long addr,
 	arm64_notify_die("Oops - SP/PC alignment exception", regs, &info, esr);
 }
 
-static struct fault_info debug_fault_info[] = {
+int __init early_brk64(unsigned long addr, unsigned int esr,
+		       struct pt_regs *regs);
+
+/*
+ * __refdata because early_brk64 is __init, but the reference to it is
+ * clobbered at arch_initcall time.
+ * See traps.c and debug-monitors.c:debug_traps_init().
+ */
+static struct fault_info __refdata debug_fault_info[] = {
 	{ do_bad,	SIGTRAP,	TRAP_HWBKPT,	"hardware breakpoint"	},
 	{ do_bad,	SIGTRAP,	TRAP_HWBKPT,	"hardware single-step"	},
 	{ do_bad,	SIGTRAP,	TRAP_HWBKPT,	"hardware watchpoint"	},
 	{ do_bad,	SIGBUS,		0,		"unknown 3"		},
 	{ do_bad,	SIGTRAP,	TRAP_BRKPT,	"aarch32 BKPT"		},
 	{ do_bad,	SIGTRAP,	0,		"aarch32 vector catch"	},
-	{ do_bad,	SIGTRAP,	TRAP_BRKPT,	"aarch64 BRK"		},
+	{ early_brk64,	SIGTRAP,	TRAP_BRKPT,	"aarch64 BRK"		},
 	{ do_bad,	SIGBUS,		0,		"unknown 7"		},
 };
 
Signature · 1 line
1.7.10.4
#9 lore raw

PATCH v3 9/9 arm64/BUG: Show explicit backtrace for WARNs

In reply to Dave Martin

The generic slowpath WARN implementation prints a backtrace, but the report_bug() based implementation does not, opting to print the registers instead which is generally not as useful.

Ideally, report_bug() should be fixed to make the behaviour more consistent, but in the meantime this patch generates a backtrace directly from the arm64 backend instead so that this functionality is not lost with the migration to report_bug().

As a side-effect, the backtrace will be outside the oops end marker, but that's hard to avoid without modifying generic code.

This patch can go away if report_bug() grows the ability in the future to generate a backtrace directly or call an arch hook at the appropriate time.

---
 arch/arm64/kernel/traps.c |    2 ++
 1 file changed, 2 insertions(+)
arch/arm64/kernel/traps.c +2 -0
index b10f4bf..bc3dfc5 100644
@@ -487,6 +487,8 @@ static int bug_handler(struct pt_regs *regs, unsigned int esr)
 		break;
 
 	case BUG_TRAP_TYPE_WARN:
+		/* Ideally, report_bug() should backtrace for us... but no. */
+		dump_backtrace(regs, NULL);
 		break;
 
 	default:
Signature · 1 line
1.7.10.4