Skip to contentSkip to conversation

arm64/hw_breakpoint: Enable FEAT_Debugv8p9

PATCH In review Anshuman Khandual 5 participants 22 messages Dec 16, 2024 – Apr 13

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

v3 cover arm64/hw_breakpoint: Enable FEAT_Debugv8p9

This series enables FEAT_Debugv8p9 thus extending breakpoint and watchpoint support upto 64. This series is based on v6.13-rc3 although this depends on FEAT_FGT2 FGU series posted earlier, for MDSELR_EL1 handling in various KVM guest configurations.

https://lore.kernel.org/all/[email protected]/

Cc Jonathan Corbet <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Oliver Upton <[email protected]>
Cc James Morse <[email protected]>
Cc Suzuki K Poulose <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Mark Brown <[email protected]>
Cc Mark Rutland <[email protected]>

Changes in V3:

- Marked ID_AA64DFR1_EL1.ABLE as FTR_NONSTRICT in ftr_id_aa64dfr1[]
- Dropped MDCR_EL3.TDA boot requirement from documentation (separate series)
- Dropped MDCR_EL2_EBWE definition as MDCR_EL2 is now defined in tools sysreg
- Used SYS_FIELD_PREP() in read_wb_reg() and write_wb_reg()
- Added MAX_PER_BANK based BUILD_BUG_ON() tests in arch_hw_breakpoint_init()
- Dropped local variables i.e mdsel_bank and index
- Derived bank and index from MAX_PER_BANK as required

Changes in V2:

https://lore.kernel.org/all/[email protected]/

Following changes have been made per review comments from Mark Rutland

- Orr MDCR_EL2_EBWE directly without an intermittent register
- Alphabetically order header files in debug-monitors.c
- Dropped embwe_ref_count mechanism
- Dropped preempt_enable() from AARCH64_DBG_READ
- Dropped preempt_disable() from AARCH64_DBG_WRITE
- Dropped set_bank_index()
- Renamed read/write_wb_reg() as __read/__write_wb_reg()
- Modified read/write_wb_reg() to have MDSELR_E1 based banked read/write
- Added required sysreg tools patches from KVM FEAT_FGT2 series for build

Changes in V1:

https://lore.kernel.org/all/[email protected]/

- Changed FTR_STRICT to FTR_NONSTRICT for the following ID_AA64DFR1_EL1 register fields - ABL_CMPs, DPFZS, PMICNTR, CTX_CMPs, WRPs and BRPs

Changes in RFC V2:

https://lore.kernel.org/linux-arm-kernel/[email protected]/

- This series has been split from RFC V1 dealing only with arm64 breakpoints
- Restored back DBG_MDSCR_MASK definition (unrelated change)
- Added preempt_disable()/enable() blocks between selecting banks and registers

Changes in RFC:

https://lore.kernel.org/all/[email protected]/

Anshuman Khandual (7):
arm64/sysreg: Update register fields for ID_AA64MMFR0_EL1 arm64/sysreg: Add register fields for MDSELR_EL1 arm64/sysreg: Add register fields for HDFGRTR2_EL2 arm64/sysreg: Add register fields for HDFGWTR2_EL2 arm64/cpufeature: Add field details for ID_AA64DFR1_EL1 register arm64/boot: Enable EL2 requirements for FEAT_Debugv8p9 arm64/hw_breakpoint: Enable FEAT_Debugv8p9

 Documentation/arch/arm64/booting.rst    | 18 +++++++
 arch/arm64/include/asm/debug-monitors.h |  1 +
 arch/arm64/include/asm/el2_setup.h      | 26 +++++++++
 arch/arm64/include/asm/hw_breakpoint.h  | 47 +++++++++++++----
 arch/arm64/kernel/cpufeature.c          | 21 ++++++--
 arch/arm64/kernel/debug-monitors.c      | 15 ++++--
 arch/arm64/kernel/hw_breakpoint.c       | 48 ++++++++++++++++-
 arch/arm64/tools/sysreg                 | 70 +++++++++++++++++++++++++
 8 files changed, 225 insertions(+), 21 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v3 1/7 arm64/sysreg: Update register fields for ID_AA64MMFR0_EL1

This updates ID_AA64MMFR0_EL1 register fields as per the definitions based on DDI0601 2024-09.

---
 arch/arm64/tools/sysreg | 2 ++
 1 file changed, 2 insertions(+)
arch/arm64/tools/sysreg +2 -0
index b081b54d6d22..a6cbe0dcd63b 100644
@@ -1591,6 +1591,7 @@ EndEnum
 UnsignedEnum	59:56	FGT
 	0b0000	NI
 	0b0001	IMP
+	0b0010	FGT2
 EndEnum
 Res0	55:48
 UnsignedEnum	47:44	EXS
@@ -1652,6 +1653,7 @@ Enum	3:0	PARANGE
 	0b0100	44
 	0b0101	48
 	0b0110	52
+	0b0111	56
 EndEnum
 EndSysreg
 
Signature · 1 line
2.25.1
#2 lore raw

PATCH v3 2/7 arm64/sysreg: Add register fields for MDSELR_EL1

In reply to Anshuman Khandual

This adds register fields for MDSELR_EL1 as per the definitions based on DDI0601 2024-09.

---
 arch/arm64/tools/sysreg | 11 +++++++++++
 1 file changed, 11 insertions(+)
arch/arm64/tools/sysreg +11 -0
index a6cbe0dcd63b..fe878eb194a0 100644
@@ -93,6 +93,17 @@ Res0	63:32
 Field	31:0	DTRTX
 EndSysreg
 
+Sysreg	MDSELR_EL1	2	0	0	4	2
+Res0	63:6
+Enum	5:4	BANK
+	0b00	BANK_0
+	0b01	BANK_1
+	0b10	BANK_2
+	0b11	BANK_3
+EndEnum
+Res0	3:0
+EndSysreg
+
 Sysreg	OSECCR_EL1	2	0	0	6	2
 Res0	63:32
 Field	31:0	EDECCR
Signature · 1 line
2.25.1
#3 lore raw

PATCH v3 3/7 arm64/sysreg: Add register fields for HDFGRTR2_EL2

In reply to Anshuman Khandual

This adds register fields for HDFGRTR2_EL2 as per the definitions based on DDI0601 2024-09.

---
 arch/arm64/tools/sysreg | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
arch/arm64/tools/sysreg +29 -0
index fe878eb194a0..a9dc5e4f9d97 100644
@@ -2557,6 +2557,35 @@ Field	1	ICIALLU
 Field	0	ICIALLUIS
 EndSysreg
 
+Sysreg HDFGRTR2_EL2	3	4	3	1	0
+Res0	63:25
+Field	24	nPMBMAR_EL1
+Field	23	nMDSTEPOP_EL1
+Field	22	nTRBMPAM_EL1
+Res0	21
+Field	20	nTRCITECR_EL1
+Field	19	nPMSDSFR_EL1
+Field	18	nSPMDEVAFF_EL1
+Field	17	nSPMID
+Field	16	nSPMSCR_EL1
+Field	15	nSPMACCESSR_EL1
+Field	14	nSPMCR_EL0
+Field	13	nSPMOVS
+Field	12	nSPMINTEN
+Field	11	nSPMCNTEN
+Field	10	nSPMSELR_EL0
+Field	9	nSPMEVTYPERn_EL0
+Field	8	nSPMEVCNTRn_EL0
+Field	7	nPMSSCR_EL1
+Field	6	nPMSSDATA
+Field	5	nMDSELR_EL1
+Field	4	nPMUACR_EL1
+Field	3	nPMICFILTR_EL0
+Field	2	nPMICNTR_EL0
+Field	1	nPMIAR_EL1
+Field	0	nPMECR_EL1
+EndSysreg
+
 Sysreg HDFGRTR_EL2	3	4	3	1	4
 Field	63	PMBIDR_EL1
 Field	62	nPMSNEVFR_EL1
Signature · 1 line
2.25.1
#4 lore raw

PATCH v3 4/7 arm64/sysreg: Add register fields for HDFGWTR2_EL2

In reply to Anshuman Khandual

This adds register fields for HDFGWTR2_EL2 as per the definitions based on DDI0601 2024-09.

---
 arch/arm64/tools/sysreg | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
arch/arm64/tools/sysreg +28 -0
index a9dc5e4f9d97..8bf22f3904bd 100644
@@ -2586,6 +2586,34 @@ Field	1	nPMIAR_EL1
 Field	0	nPMECR_EL1
 EndSysreg
 
+Sysreg HDFGWTR2_EL2	3	4	3	1	1
+Res0	63:25
+Field	24	nPMBMAR_EL1
+Field	23	nMDSTEPOP_EL1
+Field	22	nTRBMPAM_EL1
+Field	21	nPMZR_EL0
+Field	20	nTRCITECR_EL1
+Field	19	nPMSDSFR_EL1
+Res0	18:17
+Field	16	nSPMSCR_EL1
+Field	15	nSPMACCESSR_EL1
+Field	14	nSPMCR_EL0
+Field	13	nSPMOVS
+Field	12	nSPMINTEN
+Field	11	nSPMCNTEN
+Field	10	nSPMSELR_EL0
+Field	9	nSPMEVTYPERn_EL0
+Field	8	nSPMEVCNTRn_EL0
+Field	7	nPMSSCR_EL1
+Res0	6
+Field	5	nMDSELR_EL1
+Field	4	nPMUACR_EL1
+Field	3	nPMICFILTR_EL0
+Field	2	nPMICNTR_EL0
+Field	1	nPMIAR_EL1
+Field	0	nPMECR_EL1
+EndSysreg
+
 Sysreg HDFGRTR_EL2	3	4	3	1	4
 Field	63	PMBIDR_EL1
 Field	62	nPMSNEVFR_EL1
Signature · 1 line
2.25.1
#5 lore raw

PATCH v3 5/7 arm64/cpufeature: Add field details for ID_AA64DFR1_EL1 register

In reply to Anshuman Khandual

This adds required field details for ID_AA64DFR1_EL1, and also drops dummy ftr_raz[] array which is now redundant. These register fields will be used to enable increased breakpoint and watchpoint registers via FEAT_Debugv8p9 later. The register fields have been marked as FTR_STRICT, unless there is a known variation in practice.

Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
cc Mark Brown <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Marc Zyngier <[email protected]>

---
Changes in V3:

- Updated the commit message
- Marked ID_AA64DFR1_EL1.ABLE as FTR_NONSTRICT in ftr_id_aa64dfr1[]

 arch/arm64/kernel/cpufeature.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
arch/arm64/kernel/cpufeature.c +16 -5
index 6ce71f444ed8..0dc22fde104e 100644
@@ -534,6 +534,21 @@ static const struct arm64_ftr_bits ftr_id_aa64dfr0[] = {
 	ARM64_FTR_END,
 };
 
+static const struct arm64_ftr_bits ftr_id_aa64dfr1[] = {
+	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64DFR1_EL1_ABL_CMPs_SHIFT, 8, 0),
+	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64DFR1_EL1_DPFZS_SHIFT, 4, 0),
+	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR1_EL1_EBEP_SHIFT, 4, 0),
+	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR1_EL1_ITE_SHIFT, 4, 0),
+	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64DFR1_EL1_ABLE_SHIFT, 4, 0),
+	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64DFR1_EL1_PMICNTR_SHIFT, 4, 0),
+	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR1_EL1_SPMU_SHIFT, 4, 0),
+	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64DFR1_EL1_CTX_CMPs_SHIFT, 8, 0),
+	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64DFR1_EL1_WRPs_SHIFT, 8, 0),
+	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64DFR1_EL1_BRPs_SHIFT, 8, 0),
+	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR1_EL1_SYSPMUID_SHIFT, 8, 0),
+	ARM64_FTR_END,
+};
+
 static const struct arm64_ftr_bits ftr_mvfr0[] = {
 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_EL1_FPRound_SHIFT, 4, 0),
 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_EL1_FPShVec_SHIFT, 4, 0),
@@ -720,10 +735,6 @@ static const struct arm64_ftr_bits ftr_single32[] = {
 	ARM64_FTR_END,
 };
 
-static const struct arm64_ftr_bits ftr_raz[] = {
-	ARM64_FTR_END,
-};
-
 #define __ARM64_FTR_REG_OVERRIDE(id_str, id, table, ovr) {	\
 		.sys_id = id,					\
 		.reg = 	&(struct arm64_ftr_reg){		\
@@ -796,7 +807,7 @@ static const struct __ftr_reg_entry {
 
 	/* Op1 = 0, CRn = 0, CRm = 5 */
 	ARM64_FTR_REG(SYS_ID_AA64DFR0_EL1, ftr_id_aa64dfr0),
-	ARM64_FTR_REG(SYS_ID_AA64DFR1_EL1, ftr_raz),
+	ARM64_FTR_REG(SYS_ID_AA64DFR1_EL1, ftr_id_aa64dfr1),
 
 	/* Op1 = 0, CRn = 0, CRm = 6 */
 	ARM64_FTR_REG(SYS_ID_AA64ISAR0_EL1, ftr_id_aa64isar0),
Signature · 1 line
2.25.1
#6 lore raw

PATCH v3 6/7 arm64/boot: Enable EL2 requirements for FEAT_Debugv8p9

In reply to Anshuman Khandual

Fine grained trap control for MDSELR_EL1 register needs to be configured in HDFGRTR2_EL2, and HDFGWTR2_EL2 registers when kernel enters at EL1, but EL2 is also present. This adds a new helper __init_el2_fgt2() initializing this new FEAT_FGT2 based fine grained registers.

MDCR_EL2.EBWE needs to be enabled for additional (beyond 16) breakpoint and watchpoint exceptions when kernel enters at EL1, but EL2 is also present. This updates __init_el2_debug() as required for FEAT_Debugv8p9.

While here, also update booting.rst with MDCR_EL3 and SCR_EL3 requirements.

---
Changes in V3:

- Dropped MDCR_EL3.TDA boot requirement from documentation (separate series)
- Dropped MDCR_EL2_EBWE definition as MDCR_EL2 is now defined in tools sysreg

https://lore.kernel.org/all/[email protected]/

 Documentation/arch/arm64/booting.rst | 18 ++++++++++++++++++
 arch/arm64/include/asm/el2_setup.h   | 26 ++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
Documentation/arch/arm64/booting.rst +18 -0
index 3278fb4bf219..054cfe1cad18 100644
@@ -288,6 +288,12 @@ Before jumping into the kernel, the following conditions must be met:
 
     - SCR_EL3.FGTEn (bit 27) must be initialised to 0b1.
 
+  For CPUs with the Fine Grained Traps (FEAT_FGT2) extension present:
+
+  - If EL3 is present and the kernel is entered at EL2:
+
+    - SCR_EL3.FGTEn2 (bit 59) must be initialised to 0b1.
+
   For CPUs with support for HCRX_EL2 (FEAT_HCX) present:
 
   - If EL3 is present and the kernel is entered at EL2:
@@ -322,6 +328,18 @@ Before jumping into the kernel, the following conditions must be met:
     - ZCR_EL2.LEN must be initialised to the same value for all CPUs the
       kernel will execute on.
 
+  For CPUs with FEAT_Debugv8p9 extension present:
+
+  - If the kernel is entered at EL1 and EL2 is present:
+
+    - HDFGRTR2_EL2.nMDSELR_EL1 (bit 5) must be initialized to 0b1
+    - HDFGWTR2_EL2.nMDSELR_EL1 (bit 5) must be initialized to 0b1
+    - MDCR_EL2.EBWE (bit 43) must be initialized to 0b1
+
+  - If EL3 is present:
+
+    - MDCR_EL3.EBWE (bit 43) must be initialized to 0b1
+
   For CPUs with the Scalable Matrix Extension (FEAT_SME):
 
   - If EL3 is present:
arch/arm64/include/asm/el2_setup.h +26 -0
index 4ef52d7245bb..2fbfe27d38b5 100644
@@ -105,6 +105,13 @@
 						// to own it.
 
 .Lskip_trace_\@:
+	mrs	x1, id_aa64dfr0_el1
+	ubfx	x1, x1, #ID_AA64DFR0_EL1_DebugVer_SHIFT, #4
+	cmp	x1, #ID_AA64DFR0_EL1_DebugVer_V8P9
+	b.lt	.Lskip_dbg_v8p9_\@
+
+	orr	x2, x2, #MDCR_EL2_EBWE
+.Lskip_dbg_v8p9_\@:
 	msr	mdcr_el2, x2			// Configure debug traps
 .endm
 
@@ -244,6 +251,24 @@
 .Lskip_gcs_\@:
 .endm
 
+.macro __init_el2_fgt2
+	mrs	x1, id_aa64mmfr0_el1
+	ubfx	x1, x1, #ID_AA64MMFR0_EL1_FGT_SHIFT, #4
+	cmp	x1, #ID_AA64MMFR0_EL1_FGT_FGT2
+	b.lt	.Lskip_fgt2_\@
+
+	mrs	x1, id_aa64dfr0_el1
+	ubfx	x1, x1, #ID_AA64DFR0_EL1_DebugVer_SHIFT, #4
+	cmp	x1, #ID_AA64DFR0_EL1_DebugVer_V8P9
+	b.lt	.Lskip_dbg_v8p9_\@
+
+	mov_q   x0, HDFGWTR2_EL2_nMDSELR_EL1
+	msr_s	SYS_HDFGWTR2_EL2, x0
+	msr_s	SYS_HDFGRTR2_EL2, x0
+.Lskip_dbg_v8p9_\@:
+.Lskip_fgt2_\@:
+.endm
+
 .macro __init_el2_nvhe_prepare_eret
 	mov	x0, #INIT_PSTATE_EL1
 	msr	spsr_el2, x0
@@ -283,6 +308,7 @@
 	__init_el2_nvhe_idregs
 	__init_el2_cptr
 	__init_el2_fgt
+	__init_el2_fgt2
         __init_el2_gcs
 .endm
 
Signature · 1 line
2.25.1
#7 lore raw

Re: [PATCH V3 6/7] arm64/boot: Enable EL2 requirements for FEAT_Debugv8p9

Quoted 3 lines · Anshuman Khandual

Fine grained trap control for MDSELR_EL1 register needs to be configured in HDFGRTR2_EL2, and HDFGWTR2_EL2 registers when kernel enters at EL1, but EL2 is also present.

Shouldn't this be "when kernel enters at EL2, and EL3 is also present"? Though it is really "If EL3 set FGTEn2 and the kernel is entered in EL2, then FGT2 must be initialized for MDSELR_EL1."

If it was me, I'd just plagarize what was written for prior FGT commits for this code. :)

Quoted 2 lines

This adds a new helper __init_el2_fgt2() initializing this new FEAT_FGT2 based fine grained registers.

"This adds" is the same as saying "This patch/commit adds" which is well documented to avoid. Use imperative "Add a new helper...". Though it is clear from the diff that is what you are doing...

Quoted 83 lines

MDCR_EL2.EBWE needs to be enabled for additional (beyond 16) breakpoint and watchpoint exceptions when kernel enters at EL1, but EL2 is also present. This updates __init_el2_debug() as required for FEAT_Debugv8p9.

While here, also update booting.rst with MDCR_EL3 and SCR_EL3 requirements.

---
Changes in V3:

- Dropped MDCR_EL3.TDA boot requirement from documentation (separate series)
- Dropped MDCR_EL2_EBWE definition as MDCR_EL2 is now defined in tools sysreg

https://lore.kernel.org/all/[email protected]/

 Documentation/arch/arm64/booting.rst | 18 ++++++++++++++++++
 arch/arm64/include/asm/el2_setup.h   | 26 ++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
Documentation/arch/arm64/booting.rst +18 -0
index 3278fb4bf219..054cfe1cad18 100644
@@ -288,6 +288,12 @@ Before jumping into the kernel, the following conditions must be met:
 
     - SCR_EL3.FGTEn (bit 27) must be initialised to 0b1.
 
+  For CPUs with the Fine Grained Traps (FEAT_FGT2) extension present:
+
+  - If EL3 is present and the kernel is entered at EL2:
+
+    - SCR_EL3.FGTEn2 (bit 59) must be initialised to 0b1.
+
   For CPUs with support for HCRX_EL2 (FEAT_HCX) present:
 
   - If EL3 is present and the kernel is entered at EL2:
@@ -322,6 +328,18 @@ Before jumping into the kernel, the following conditions must be met:
     - ZCR_EL2.LEN must be initialised to the same value for all CPUs the
       kernel will execute on.
 
+  For CPUs with FEAT_Debugv8p9 extension present:
+
+  - If the kernel is entered at EL1 and EL2 is present:
+
+    - HDFGRTR2_EL2.nMDSELR_EL1 (bit 5) must be initialized to 0b1
+    - HDFGWTR2_EL2.nMDSELR_EL1 (bit 5) must be initialized to 0b1
+    - MDCR_EL2.EBWE (bit 43) must be initialized to 0b1
+
+  - If EL3 is present:
+
+    - MDCR_EL3.EBWE (bit 43) must be initialized to 0b1
+
   For CPUs with the Scalable Matrix Extension (FEAT_SME):
 
   - If EL3 is present:
arch/arm64/include/asm/el2_setup.h +7 -0
index 4ef52d7245bb..2fbfe27d38b5 100644
@@ -105,6 +105,13 @@
 						// to own it.
 
 .Lskip_trace_\@:
+	mrs	x1, id_aa64dfr0_el1
+	ubfx	x1, x1, #ID_AA64DFR0_EL1_DebugVer_SHIFT, #4
+	cmp	x1, #ID_AA64DFR0_EL1_DebugVer_V8P9
+	b.lt	.Lskip_dbg_v8p9_\@
+
+	orr	x2, x2, #MDCR_EL2_EBWE
+.Lskip_dbg_v8p9_\@:
 	msr	mdcr_el2, x2			// Configure debug traps
 .endm
 

arch/arm64/include/asm/el2_setup.h line 257

Quoted 7 lines
arch/arm64/include/asm/el2_setup.h +4 -0
@@ -244,6 +251,24 @@
 .Lskip_gcs_\@:
 .endm
 
+.macro __init_el2_fgt2
+	mrs	x1, id_aa64mmfr0_el1
+	ubfx	x1, x1, #ID_AA64MMFR0_EL1_FGT_SHIFT, #4
+	cmp	x1, #ID_AA64MMFR0_EL1_FGT_FGT2

We already read this field in __init_el2_fgt, shouldn't we leverage that and move all this there rather than read the feature reg twice.

arch/arm64/include/asm/el2_setup.h line 267

Quoted 10 lines
arch/arm64/include/asm/el2_setup.h +10 -0
+	b.lt	.Lskip_fgt2_\@
+
+	mrs	x1, id_aa64dfr0_el1
+	ubfx	x1, x1, #ID_AA64DFR0_EL1_DebugVer_SHIFT, #4
+	cmp	x1, #ID_AA64DFR0_EL1_DebugVer_V8P9
+	b.lt	.Lskip_dbg_v8p9_\@
+
+	mov_q   x0, HDFGWTR2_EL2_nMDSELR_EL1
+	msr_s	SYS_HDFGWTR2_EL2, x0
+	msr_s	SYS_HDFGRTR2_EL2, x0

If Debug v8.9 is not present, but FGT2 is, shouldn't we write 0 to these registers? That is what we do for FGT.

I just realized I forgot to add FGT2 setup for the PMUv3.9 features I already added in 6.12 and 6.13. So this really needs to land sooner rather than later to add that.

Rob

#8 lore raw

Re: [PATCH V3 6/7] arm64/boot: Enable EL2 requirements for FEAT_Debugv8p9

Quoted 6 lines · Rob Herring
Quoted 3 lines · Anshuman Khandual

Fine grained trap control for MDSELR_EL1 register needs to be configured in HDFGRTR2_EL2, and HDFGWTR2_EL2 registers when kernel enters at EL1, but EL2 is also present.

Shouldn't this be "when kernel enters at EL2, and EL3 is also present"?

AFAICT - HDFGRTR2_EL2 and HDFGWTR2_EL2 registers configure traps into EL2 when accessed from EL1/EL0, provided all required EL3 trap controls are in place as well. These EL2 based trap configs need to be set before the kernel finally enters EL1. Although there is an assumption about EL3 based trap configs being in place, do we need to mention that in commit message as well. Is not updating booting.rst takes care of all EL3 requirements.

Quoted 5 lines

Though it is really "If EL3 set FGTEn2 and the kernel is entered in EL2, then FGT2 must be initialized for MDSELR_EL1."

If it was me, I'd just plagarize what was written for prior FGT commits for this code. :)

There are many commits that changed __init_el2_fgt() with different description formats. Do you have particular one in mind which can be followed here ? :)

Quoted 7 lines
Quoted 3 lines

This adds a new helper __init_el2_fgt2() initializing this new FEAT_FGT2 based fine grained registers.

"This adds" is the same as saying "This patch/commit adds" which is well documented to avoid. Use imperative "Add a new helper...". Though it is clear from the diff that is what you are doing...

Sure, will fix it.

Quoted 88 lines
Quoted 88 lines

MDCR_EL2.EBWE needs to be enabled for additional (beyond 16) breakpoint and watchpoint exceptions when kernel enters at EL1, but EL2 is also present. This updates __init_el2_debug() as required for FEAT_Debugv8p9.

While here, also update booting.rst with MDCR_EL3 and SCR_EL3 requirements.

---
Changes in V3:

- Dropped MDCR_EL3.TDA boot requirement from documentation (separate series)
- Dropped MDCR_EL2_EBWE definition as MDCR_EL2 is now defined in tools sysreg

https://lore.kernel.org/all/[email protected]/

 Documentation/arch/arm64/booting.rst | 18 ++++++++++++++++++
 arch/arm64/include/asm/el2_setup.h   | 26 ++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
Documentation/arch/arm64/booting.rst +18 -0
index 3278fb4bf219..054cfe1cad18 100644
@@ -288,6 +288,12 @@ Before jumping into the kernel, the following conditions must be met:
 
     - SCR_EL3.FGTEn (bit 27) must be initialised to 0b1.
 
+  For CPUs with the Fine Grained Traps (FEAT_FGT2) extension present:
+
+  - If EL3 is present and the kernel is entered at EL2:
+
+    - SCR_EL3.FGTEn2 (bit 59) must be initialised to 0b1.
+
   For CPUs with support for HCRX_EL2 (FEAT_HCX) present:
 
   - If EL3 is present and the kernel is entered at EL2:
@@ -322,6 +328,18 @@ Before jumping into the kernel, the following conditions must be met:
     - ZCR_EL2.LEN must be initialised to the same value for all CPUs the
       kernel will execute on.
 
+  For CPUs with FEAT_Debugv8p9 extension present:
+
+  - If the kernel is entered at EL1 and EL2 is present:
+
+    - HDFGRTR2_EL2.nMDSELR_EL1 (bit 5) must be initialized to 0b1
+    - HDFGWTR2_EL2.nMDSELR_EL1 (bit 5) must be initialized to 0b1
+    - MDCR_EL2.EBWE (bit 43) must be initialized to 0b1
+
+  - If EL3 is present:
+
+    - MDCR_EL3.EBWE (bit 43) must be initialized to 0b1
+
   For CPUs with the Scalable Matrix Extension (FEAT_SME):
 
   - If EL3 is present:
arch/arm64/include/asm/el2_setup.h +7 -0
index 4ef52d7245bb..2fbfe27d38b5 100644
@@ -105,6 +105,13 @@
 						// to own it.
 
 .Lskip_trace_\@:
+	mrs	x1, id_aa64dfr0_el1
+	ubfx	x1, x1, #ID_AA64DFR0_EL1_DebugVer_SHIFT, #4
+	cmp	x1, #ID_AA64DFR0_EL1_DebugVer_V8P9
+	b.lt	.Lskip_dbg_v8p9_\@
+
+	orr	x2, x2, #MDCR_EL2_EBWE
+.Lskip_dbg_v8p9_\@:
 	msr	mdcr_el2, x2			// Configure debug traps
 .endm
 
@@ -244,6 +251,24 @@
 .Lskip_gcs_\@:
 .endm
 
Quoted 7 lines

arch/arm64/include/asm/el2_setup.h line 257

Quoted 4 lines
arch/arm64/include/asm/el2_setup.h +4 -0
+.macro __init_el2_fgt2
+	mrs	x1, id_aa64mmfr0_el1
+	ubfx	x1, x1, #ID_AA64MMFR0_EL1_FGT_SHIFT, #4
+	cmp	x1, #ID_AA64MMFR0_EL1_FGT_FGT2

We already read this field in __init_el2_fgt, shouldn't we leverage that and move all this there rather than read the feature reg twice.

Should not __init_el2_fgt2() remain separate to contain all future FEAT_FGT2 related trap enabled/disable checks ? OTOH reading id_aa64mmfr0_el1 register should improve some performance as well.

Quoted 14 lines

arch/arm64/include/asm/el2_setup.h line 267

Quoted 11 lines
arch/arm64/include/asm/el2_setup.h +10 -0
+	b.lt	.Lskip_fgt2_\@
+
+	mrs	x1, id_aa64dfr0_el1
+	ubfx	x1, x1, #ID_AA64DFR0_EL1_DebugVer_SHIFT, #4
+	cmp	x1, #ID_AA64DFR0_EL1_DebugVer_V8P9
+	b.lt	.Lskip_dbg_v8p9_\@
+
+	mov_q   x0, HDFGWTR2_EL2_nMDSELR_EL1
+	msr_s	SYS_HDFGWTR2_EL2, x0
+	msr_s	SYS_HDFGRTR2_EL2, x0

If Debug v8.9 is not present, but FGT2 is, shouldn't we write 0 to these registers? That is what we do for FGT.

Just to summerize what's being done in __init_el2_fgt()

.macro __init_el2_fgt

	...............
	mov     x0, xzr
	...............

.Lskip_spe_fgt_\@:
msr_s SYS_HDFGRTR_EL2, x0
msr_s SYS_HDFGWTR_EL2, x0
...............
.Lset_fgt_\@:

        msr_s   SYS_HFGRTR_EL2, x0
        msr_s   SYS_HFGWTR_EL2, x0
        msr_s   SYS_HFGITR_EL2, xzr
	...............

If the relevant features are not present and their traps need not be handled, all FEAT_FGT based trap control registers (SYS_HDFGRTR_EL2, SYS_HDFGWTR_EL2, SYS_HFGRTR_EL2, SYS_HFGWTR_EL2 and SYS_HFGITR_EL2) get cleared. Hence same needs to be done for all FEAT_FGT2 register as well. Fair enough, something like the following makes sense ?

.macro __init_el2_fgt2

        mrs     x1, id_aa64mmfr0_el1
        ubfx    x1, x1, #ID_AA64MMFR0_EL1_FGT_SHIFT, #4
        cmp     x1, #ID_AA64MMFR0_EL1_FGT_FGT2
        b.lt    .Lskip_fgt2_\@
        mov     x0, xzr
        mrs     x1, id_aa64dfr0_el1
        ubfx    x1, x1, #ID_AA64DFR0_EL1_DebugVer_SHIFT, #4
        cmp     x1, #ID_AA64DFR0_EL1_DebugVer_V8P9
        b.lt    .Lskip_dbg_v8p9_\@
        orr     x0, x0, #HDFGWTR2_EL2_nMDSELR_EL1
.Lskip_dbg_v8p9_\@:
        msr_s   SYS_HDFGWTR2_EL2, x0
        msr_s   SYS_HDFGRTR2_EL2, x0
        msr_s   SYS_HFGRTR2_EL2, xzr
        msr_s   SYS_HFGWTR2_EL2, xzr
        msr_s   SYS_HFGITR2_EL2, xzr

.Lskip_fgt2_\@:
.endm

Just wondering - following the same principle if clearing these FEAT_FGT2 trap registers would make sense in itself, even without FEAT_Debugv8p9 enablement ?

Something like the commit

commit 31c00d2aeaa2da89361f5b64a64ca831433be5fc

Author: Mark Brown <[email protected]>
Date:   Thu Apr 1 19:09:40 2021 +0100

arm64: Disable fine grained traps on boot

The arm64 FEAT_FGT extension introduces a set of traps to EL2 for accesses to small sets of registers and instructions from EL1 and EL0. Currently Linux makes no use of this feature, ensure that it is not active at boot by disabling the traps during EL2 setup.

Quoted 5 lines

I just realized I forgot to add FGT2 setup for the PMUv3.9 features I already added in 6.12 and 6.13. So this really needs to land sooner rather than later to add that.

Not sure if I got this correctly. Are you suggesting to carve out __init_el2_fgt2() from the series and post separately with PMUv3.9 requirements and fallback clearing for all FEAT_FGT2 trap config registers as mentioned above ?

#9 lore raw

Re: [PATCH V3 6/7] arm64/boot: Enable EL2 requirements for FEAT_Debugv8p9

Quoted 7 lines · Anshuman Khandual
Quoted 5 lines · Rob Herring
Quoted 3 lines · Anshuman Khandual

Fine grained trap control for MDSELR_EL1 register needs to be configured in HDFGRTR2_EL2, and HDFGWTR2_EL2 registers when kernel enters at EL1, but EL2 is also present.

Quoted 8 lines
Quoted 1 line

Shouldn't this be "when kernel enters at EL2, and EL3 is also present"?

AFAICT - HDFGRTR2_EL2 and HDFGWTR2_EL2 registers configure traps into EL2 when accessed from EL1/EL0, provided all required EL3 trap controls are in place as well. These EL2 based trap configs need to be set before the kernel finally enters EL1. Although there is an assumption about EL3 based trap configs being in place, do we need to mention that in commit message as well. Is not updating booting.rst takes care of all EL3 requirements.

My point is just I read 'kernel enters at EL1' as meaning the kernel booted at EL1 and EL2 is not accessible. Maybe should reworded as 'before/if the kernel drops to EL1'

Quoted 115 lines
Quoted 5 lines

Though it is really "If EL3 set FGTEn2 and the kernel is entered in EL2, then FGT2 must be initialized for MDSELR_EL1."

If it was me, I'd just plagarize what was written for prior FGT commits for this code. :)

There are many commits that changed __init_el2_fgt() with different description formats. Do you have particular one in mind which can be followed here ? :)

Quoted 7 lines
Quoted 3 lines

This adds a new helper __init_el2_fgt2() initializing this new FEAT_FGT2 based fine grained registers.

"This adds" is the same as saying "This patch/commit adds" which is well documented to avoid. Use imperative "Add a new helper...". Though it is clear from the diff that is what you are doing...

Sure, will fix it.

Quoted 88 lines
Quoted 88 lines

MDCR_EL2.EBWE needs to be enabled for additional (beyond 16) breakpoint and watchpoint exceptions when kernel enters at EL1, but EL2 is also present. This updates __init_el2_debug() as required for FEAT_Debugv8p9.

While here, also update booting.rst with MDCR_EL3 and SCR_EL3 requirements.

---
Changes in V3:

- Dropped MDCR_EL3.TDA boot requirement from documentation (separate series)
- Dropped MDCR_EL2_EBWE definition as MDCR_EL2 is now defined in tools sysreg

https://lore.kernel.org/all/[email protected]/

 Documentation/arch/arm64/booting.rst | 18 ++++++++++++++++++
 arch/arm64/include/asm/el2_setup.h   | 26 ++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
Documentation/arch/arm64/booting.rst +18 -0
index 3278fb4bf219..054cfe1cad18 100644
@@ -288,6 +288,12 @@ Before jumping into the kernel, the following conditions must be met:
 
     - SCR_EL3.FGTEn (bit 27) must be initialised to 0b1.
 
+  For CPUs with the Fine Grained Traps (FEAT_FGT2) extension present:
+
+  - If EL3 is present and the kernel is entered at EL2:
+
+    - SCR_EL3.FGTEn2 (bit 59) must be initialised to 0b1.
+
   For CPUs with support for HCRX_EL2 (FEAT_HCX) present:
 
   - If EL3 is present and the kernel is entered at EL2:
@@ -322,6 +328,18 @@ Before jumping into the kernel, the following conditions must be met:
     - ZCR_EL2.LEN must be initialised to the same value for all CPUs the
       kernel will execute on.
 
+  For CPUs with FEAT_Debugv8p9 extension present:
+
+  - If the kernel is entered at EL1 and EL2 is present:
+
+    - HDFGRTR2_EL2.nMDSELR_EL1 (bit 5) must be initialized to 0b1
+    - HDFGWTR2_EL2.nMDSELR_EL1 (bit 5) must be initialized to 0b1
+    - MDCR_EL2.EBWE (bit 43) must be initialized to 0b1
+
+  - If EL3 is present:
+
+    - MDCR_EL3.EBWE (bit 43) must be initialized to 0b1
+
   For CPUs with the Scalable Matrix Extension (FEAT_SME):
 
   - If EL3 is present:
arch/arm64/include/asm/el2_setup.h +7 -0
index 4ef52d7245bb..2fbfe27d38b5 100644
@@ -105,6 +105,13 @@
                                             // to own it.
 
 .Lskip_trace_\@:
+    mrs     x1, id_aa64dfr0_el1
+    ubfx    x1, x1, #ID_AA64DFR0_EL1_DebugVer_SHIFT, #4
+    cmp     x1, #ID_AA64DFR0_EL1_DebugVer_V8P9
+    b.lt    .Lskip_dbg_v8p9_\@
+
+    orr     x2, x2, #MDCR_EL2_EBWE
+.Lskip_dbg_v8p9_\@:
     msr     mdcr_el2, x2                    // Configure debug traps
 .endm
 
@@ -244,6 +251,24 @@
 .Lskip_gcs_\@:
 .endm
 
Quoted 7 lines

arch/arm64/include/asm/el2_setup.h line 257

Quoted 4 lines
arch/arm64/include/asm/el2_setup.h +4 -0
+.macro __init_el2_fgt2
+    mrs     x1, id_aa64mmfr0_el1
+    ubfx    x1, x1, #ID_AA64MMFR0_EL1_FGT_SHIFT, #4
+    cmp     x1, #ID_AA64MMFR0_EL1_FGT_FGT2

We already read this field in __init_el2_fgt, shouldn't we leverage that and move all this there rather than read the feature reg twice.

Quoted 3 lines

Should not __init_el2_fgt2() remain separate to contain all future FEAT_FGT2 related trap enabled/disable checks ? OTOH reading id_aa64mmfr0_el1 register should improve some performance as well.

That's the tradeoff. I'll defer to others whether a single id register read here is preferred.

Quoted 55 lines
Quoted 5 lines
Quoted 5 lines
arch/arm64/include/asm/el2_setup.h +5 -0
+    b.lt    .Lskip_fgt2_\@
+
+    mrs     x1, id_aa64dfr0_el1
+    ubfx    x1, x1, #ID_AA64DFR0_EL1_DebugVer_SHIFT, #4
+    cmp     x1, #ID_AA64DFR0_EL1_DebugVer_V8P9
Quoted 8 lines

arch/arm64/include/asm/el2_setup.h line 267

Quoted 5 lines
arch/arm64/include/asm/el2_setup.h +5 -0
+    b.lt    .Lskip_dbg_v8p9_\@
+
+    mov_q   x0, HDFGWTR2_EL2_nMDSELR_EL1
+    msr_s   SYS_HDFGWTR2_EL2, x0
+    msr_s   SYS_HDFGRTR2_EL2, x0

If Debug v8.9 is not present, but FGT2 is, shouldn't we write 0 to these registers? That is what we do for FGT.

Just to summerize what's being done in __init_el2_fgt()

.macro __init_el2_fgt

        ...............
        mov     x0, xzr
        ...............

.Lskip_spe_fgt_\@:
msr_s SYS_HDFGRTR_EL2, x0
msr_s SYS_HDFGWTR_EL2, x0
...............
.Lset_fgt_\@:

        msr_s   SYS_HFGRTR_EL2, x0
        msr_s   SYS_HFGWTR_EL2, x0
        msr_s   SYS_HFGITR_EL2, xzr
        ...............

If the relevant features are not present and their traps need not be handled, all FEAT_FGT based trap control registers (SYS_HDFGRTR_EL2, SYS_HDFGWTR_EL2, SYS_HFGRTR_EL2, SYS_HFGWTR_EL2 and SYS_HFGITR_EL2) get cleared. Hence same needs to be done for all FEAT_FGT2 register as well. Fair enough, something like the following makes sense ?

.macro __init_el2_fgt2

        mrs     x1, id_aa64mmfr0_el1
        ubfx    x1, x1, #ID_AA64MMFR0_EL1_FGT_SHIFT, #4
        cmp     x1, #ID_AA64MMFR0_EL1_FGT_FGT2
        b.lt    .Lskip_fgt2_\@
        mov     x0, xzr
        mrs     x1, id_aa64dfr0_el1
        ubfx    x1, x1, #ID_AA64DFR0_EL1_DebugVer_SHIFT, #4
        cmp     x1, #ID_AA64DFR0_EL1_DebugVer_V8P9
        b.lt    .Lskip_dbg_v8p9_\@
        orr     x0, x0, #HDFGWTR2_EL2_nMDSELR_EL1
.Lskip_dbg_v8p9_\@:
        msr_s   SYS_HDFGWTR2_EL2, x0
        msr_s   SYS_HDFGRTR2_EL2, x0
        msr_s   SYS_HFGRTR2_EL2, xzr
        msr_s   SYS_HFGWTR2_EL2, xzr
        msr_s   SYS_HFGITR2_EL2, xzr
Quoted 2 lines

.Lskip_fgt2_\@:
.endm

Looks good.

Quoted 3 lines

Just wondering - following the same principle if clearing these FEAT_FGT2 trap registers would make sense in itself, even without FEAT_Debugv8p9 enablement ?

Probably and there's a Jira for it. Though there was some debate about whether anything was needed before any users. But that's kind of a mute point anyway because I need it for PMU now, too.

Quoted 16 lines

Something like the commit

commit 31c00d2aeaa2da89361f5b64a64ca831433be5fc

Author: Mark Brown <[email protected]>
Date:   Thu Apr 1 19:09:40 2021 +0100

arm64: Disable fine grained traps on boot

The arm64 FEAT_FGT extension introduces a set of traps to EL2 for accesses to small sets of registers and instructions from EL1 and EL0. Currently Linux makes no use of this feature, ensure that it is not active at boot by disabling the traps during EL2 setup.

Quoted 2 lines
Quoted 6 lines
Quoted 3 lines

I just realized I forgot to add FGT2 setup for the PMUv3.9 features I already added in 6.12 and 6.13. So this really needs to land sooner rather than later to add that.

Not sure if I got this correctly. Are you suggesting to carve out __init_el2_fgt2() from the series and post separately with PMUv3.9 requirements and fallback clearing for all FEAT_FGT2 trap config registers as mentioned above ?

Yes, as it needs to not be held up by any of the debug issues Mark raised. Also, it may need to be back ported to 6.12. And for that we'd want the PMU parts, but not the Debug. I still have to figure out what needs to be done on the KVM side.

Rob

#10 lore raw

Re: [PATCH V3 6/7] arm64/boot: Enable EL2 requirements for FEAT_Debugv8p9

[....]

Quoted 18 lines · Rob Herring
Quoted 18 lines

Something like the commit

commit 31c00d2aeaa2da89361f5b64a64ca831433be5fc

Author: Mark Brown <[email protected]>
Date:   Thu Apr 1 19:09:40 2021 +0100

arm64: Disable fine grained traps on boot

The arm64 FEAT_FGT extension introduces a set of traps to EL2 for accesses to small sets of registers and instructions from EL1 and EL0. Currently Linux makes no use of this feature, ensure that it is not active at boot by disabling the traps during EL2 setup.

Quoted 4 lines

I just realized I forgot to add FGT2 setup for the PMUv3.9 features I already added in 6.12 and 6.13. So this really needs to land sooner rather than later to add that.

Quoted 7 lines
Quoted 3 lines

Not sure if I got this correctly. Are you suggesting to carve out __init_el2_fgt2() from the series and post separately with PMUv3.9 requirements and fallback clearing for all FEAT_FGT2 trap config registers as mentioned above ?

Yes, as it needs to not be held up by any of the debug issues Mark raised. Also, it may need to be back ported to 6.12. And for that we'd want the PMU parts, but not the Debug. I still have to figure out what needs to be done on the KVM side.

Hi Rob,

I did go through all the five FEAT_FGT2 trap control registers and it seems like the following are the controls available for FEAT_PMUv3p9 based registers. Although PMZR_EL0 does not get used in kernel right now but still might be a good idea to include anyway. Please let me know, if I might have missed something else related to FEAT_PMUv3p9.

HDFGRTR2_EL2_nPMUACR_EL1 (mrs PMUACR_EL1)
HDFGWTR2_EL2_nPMUACR_EL1 (msr PMUACR_EL1)
HDFGWTR2_EL2_nPMZR_EL0 (msr PMZR_EL0)

Following will be the change required for __init_el2_fgt2() along with all the tools sysreg updates required for the mentioned registers here.

arch/arm64/include/asm/el2_setup.h +26 -0
@@ -233,6 +233,31 @@
 .Lskip_fgt_\@:
 .endm
 
+.macro __init_el2_fgt2
+       mrs     x1, id_aa64mmfr0_el1
+       ubfx    x1, x1, #ID_AA64MMFR0_EL1_FGT_SHIFT, #4
+       cmp     x1, #ID_AA64MMFR0_EL1_FGT_FGT2
+       b.lt    .Lskip_fgt2_\@
+
+       mov     x0, xzr
+       mov     x2, xzr
+       mrs     x1, id_aa64dfr0_el1
+       ubfx    x1, x1, #ID_AA64DFR0_EL1_PMUVer_SHIFT, #4
+       cmp     x1, #ID_AA64DFR0_EL1_PMUVer_V3P9
+       b.lt    .Lskip_pmuv3p9_\@
+
+       orr     x0, x0, #HDFGRTR2_EL2_nPMUACR_EL1
+       orr     x2, x2, #HDFGWTR2_EL2_nPMUACR_EL1
+       orr     x2, x2, #HDFGWTR2_EL2_nPMZR_EL0
+.Lskip_pmuv3p9_\@:
+       msr_s   SYS_HDFGRTR2_EL2, x0
+       msr_s   SYS_HDFGWTR2_EL2, x2
+       msr_s   SYS_HFGRTR2_EL2, xzr
+       msr_s   SYS_HFGWTR2_EL2, xzr
+       msr_s   SYS_HFGITR2_EL2, xzr
+.Lskip_fgt2_\@:
+.endm
+
 .macro __init_el2_gcs
        mrs_s   x1, SYS_ID_AA64PFR1_EL1
        ubfx    x1, x1, #ID_AA64PFR1_EL1_GCS_SHIFT, #4
@@ -283,6 +308,7 @@
        __init_el2_nvhe_idregs
        __init_el2_cptr
        __init_el2_fgt
+       __init_el2_fgt2
         __init_el2_gcs
 .endm
maintainer
#11 lore raw

Re: [PATCH V3 6/7] arm64/boot: Enable EL2 requirements for FEAT_Debugv8p9

In reply to Rob Herring
Quoted 19 lines · Rob Herring
Quoted 7 lines · Anshuman Khandual
Quoted 5 lines · Rob Herring
Quoted 3 lines · Anshuman Khandual

Fine grained trap control for MDSELR_EL1 register needs to be configured in HDFGRTR2_EL2, and HDFGWTR2_EL2 registers when kernel enters at EL1, but EL2 is also present.

Quoted 8 lines
Quoted 1 line

Shouldn't this be "when kernel enters at EL2, and EL3 is also present"?

AFAICT - HDFGRTR2_EL2 and HDFGWTR2_EL2 registers configure traps into EL2 when accessed from EL1/EL0, provided all required EL3 trap controls are in place as well. These EL2 based trap configs need to be set before the kernel finally enters EL1. Although there is an assumption about EL3 based trap configs being in place, do we need to mention that in commit message as well. Is not updating booting.rst takes care of all EL3 requirements.

Quoted 3 lines

My point is just I read 'kernel enters at EL1' as meaning the kernel booted at EL1 and EL2 is not accessible. Maybe should reworded as 'before/if the kernel drops to EL1'

The EL2->EL1 downgrade is internal to the kernel. If we get it wrong, that's our fault, and there isn't anything to document. This document is about booting the kernel at any NS EL, so only the "external" requirements matter.

But I don't think we should be prescriptive about the state of these registers, as long as the potential traps are correctly handled.

For the above, I'd rather have something like:

"The MDSELR_EL1 register must be freely accessible when the kernel is
entered at EL1 and that higher ELs are present in the system."

On its own, that should be enough. You can subsequently add:

"This includes HDFGRTR2_EL2, HDFGWTR2_EL2 and MDCR_EL2 when EL2 is
present, as well as MDCR_EL3 when EL3 is present".

but that's paraphrasing the architecture, and is definitely incomplete (see the MDSELR_EL1 pseudocode for reference).

Quoted 71 lines
Quoted 71 lines
Quoted 6 lines

Though it is really "If EL3 set FGTEn2 and the kernel is entered in EL2, then FGT2 must be initialized for MDSELR_EL1."

If it was me, I'd just plagarize what was written for prior FGT commits for this code. :)

There are many commits that changed __init_el2_fgt() with different description formats. Do you have particular one in mind which can be followed here ? :)

Quoted 7 lines
Quoted 3 lines

This adds a new helper __init_el2_fgt2() initializing this new FEAT_FGT2 based fine grained registers.

"This adds" is the same as saying "This patch/commit adds" which is well documented to avoid. Use imperative "Add a new helper...". Though it is clear from the diff that is what you are doing...

Sure, will fix it.

Quoted 51 lines
Quoted 51 lines

MDCR_EL2.EBWE needs to be enabled for additional (beyond 16) breakpoint and watchpoint exceptions when kernel enters at EL1, but EL2 is also present. This updates __init_el2_debug() as required for FEAT_Debugv8p9.

While here, also update booting.rst with MDCR_EL3 and SCR_EL3 requirements.

---
Changes in V3:

- Dropped MDCR_EL3.TDA boot requirement from documentation (separate series)
- Dropped MDCR_EL2_EBWE definition as MDCR_EL2 is now defined in tools sysreg

https://lore.kernel.org/all/[email protected]/

 Documentation/arch/arm64/booting.rst | 18 ++++++++++++++++++
 arch/arm64/include/asm/el2_setup.h   | 26 ++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
Documentation/arch/arm64/booting.rst +6 -0
index 3278fb4bf219..054cfe1cad18 100644
@@ -288,6 +288,12 @@ Before jumping into the kernel, the following conditions must be met:
 
     - SCR_EL3.FGTEn (bit 27) must be initialised to 0b1.
 
+  For CPUs with the Fine Grained Traps (FEAT_FGT2) extension present:
+
+  - If EL3 is present and the kernel is entered at EL2:
+
+    - SCR_EL3.FGTEn2 (bit 59) must be initialised to 0b1.
+
   For CPUs with support for HCRX_EL2 (FEAT_HCX) present:
 
   - If EL3 is present and the kernel is entered at EL2:
@@ -322,6 +328,18 @@ Before jumping into the kernel, the following conditions must be met:
     - ZCR_EL2.LEN must be initialised to the same value for all CPUs the
       kernel will execute on.
 
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Documentation/arch/arm64/booting.rst Before jumping into the kernel, the following conditions must be met: · line 336

Quoted 6 lines
Documentation/arch/arm64/booting.rst +6 -0
+  For CPUs with FEAT_Debugv8p9 extension present:
+
+  - If the kernel is entered at EL1 and EL2 is present:
+
+    - HDFGRTR2_EL2.nMDSELR_EL1 (bit 5) must be initialized to 0b1
+    - HDFGWTR2_EL2.nMDSELR_EL1 (bit 5) must be initialized to 0b1

But then you don't say anything about SCR_EL3.FGTEn2. And what if the hypervisor wants to trap things in order to emulate on lazy switch things?

The more I look at those, the more I think these requirements are not saying what we want to express, which is that some registers must be accessible one way or another.

Quoted 39 lines
Quoted 39 lines
Quoted 38 lines
Quoted 28 lines
Documentation/arch/arm64/booting.rst +6 -0
+    - MDCR_EL2.EBWE (bit 43) must be initialized to 0b1
+
+  - If EL3 is present:
+
+    - MDCR_EL3.EBWE (bit 43) must be initialized to 0b1
+
   For CPUs with the Scalable Matrix Extension (FEAT_SME):
 
   - If EL3 is present:
arch/arm64/include/asm/el2_setup.h +7 -0
index 4ef52d7245bb..2fbfe27d38b5 100644
@@ -105,6 +105,13 @@
                                             // to own it.
 
 .Lskip_trace_\@:
+    mrs     x1, id_aa64dfr0_el1
+    ubfx    x1, x1, #ID_AA64DFR0_EL1_DebugVer_SHIFT, #4
+    cmp     x1, #ID_AA64DFR0_EL1_DebugVer_V8P9
+    b.lt    .Lskip_dbg_v8p9_\@
+
+    orr     x2, x2, #MDCR_EL2_EBWE
+.Lskip_dbg_v8p9_\@:
     msr     mdcr_el2, x2                    // Configure debug traps
 .endm
 

arch/arm64/include/asm/el2_setup.h line 257

Quoted 7 lines
arch/arm64/include/asm/el2_setup.h +4 -0
@@ -244,6 +251,24 @@
 .Lskip_gcs_\@:
 .endm
 
+.macro __init_el2_fgt2
+    mrs     x1, id_aa64mmfr0_el1
+    ubfx    x1, x1, #ID_AA64MMFR0_EL1_FGT_SHIFT, #4
+    cmp     x1, #ID_AA64MMFR0_EL1_FGT_FGT2

We already read this field in __init_el2_fgt, shouldn't we leverage that and move all this there rather than read the feature reg twice.

Quoted 6 lines
Quoted 3 lines

Should not __init_el2_fgt2() remain separate to contain all future FEAT_FGT2 related trap enabled/disable checks ? OTOH reading id_aa64mmfr0_el1 register should improve some performance as well.

That's the tradeoff. I'll defer to others whether a single id register read here is preferred.

Are we *really* talking about *performance* here? For something that is executed once per CPU boot? Just keep the damn thing readable, if at all possible...

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#12 lore raw

Re: [PATCH V3 6/7] arm64/boot: Enable EL2 requirements for FEAT_Debugv8p9

Quoted 2 lines · Marc Zyngier

But I don't think we should be prescriptive about the state of these registers, as long as the potential traps are correctly handled.

The rest of the document is written in terms of explicit register values, if we're changing approach we should probably update all the existing requirements to be written in the same way since having a mix of approaches tends to be a big red flag that there should be different handling. Consistency will make the document clearer and easier for people to work with.

FWIW there is an explict note in the document about the fact that it's an as-if rule:

|                                                Where the values documented
| disable traps it is permissible for these traps to be enabled so long as
| those traps are handled transparently by higher exception levels as though
| the values documented were set.

from when I raised this before.

#13 lore raw

PATCH v3 7/7 arm64/hw_breakpoint: Enable FEAT_Debugv8p9

In reply to Anshuman Khandual

Currently there can be maximum 16 breakpoints, and 16 watchpoints available on a given platform - as detected from ID_AA64DFR0_EL1.[BRPs|WRPs] register fields. But these breakpoint, and watchpoints can be extended further up to 64 via a new arch feature FEAT_Debugv8p9.

This first enables banked access for the breakpoint and watchpoint register set via MDSELR_EL1, extended exceptions via MDSCR_EL1.EMBWE and determining available breakpoints and watchpoints in the platform from ID_AA64DFR1_EL1, when FEAT_Debugv8p9 is enabled.

---
Changes in V3:

- Used SYS_FIELD_PREP() in read_wb_reg() and write_wb_reg()
- Added MAX_PER_BANK based BUILD_BUG_ON() tests in arch_hw_breakpoint_init()
- Dropped local variables i.e mdsel_bank and index
- Derived bank and index from MAX_PER_BANK as required

 arch/arm64/include/asm/debug-monitors.h |  1 +
 arch/arm64/include/asm/hw_breakpoint.h  | 47 ++++++++++++++++++------
 arch/arm64/kernel/debug-monitors.c      | 15 +++++---
 arch/arm64/kernel/hw_breakpoint.c       | 48 +++++++++++++++++++++++--
 4 files changed, 95 insertions(+), 16 deletions(-)
arch/arm64/include/asm/debug-monitors.h +1 -0
index 8f6ba31b8658..56b89a582a0d 100644
@@ -20,6 +20,7 @@
 #define DBG_MDSCR_KDE		(1 << 13)
 #define DBG_MDSCR_MDE		(1 << 15)
 #define DBG_MDSCR_MASK		~(DBG_MDSCR_KDE | DBG_MDSCR_MDE)
+#define DBG_MDSCR_EMBWE		(1UL << 32)
 
 #define	DBG_ESR_EVT(x)		(((x) >> 27) & 0x7)
 
arch/arm64/include/asm/hw_breakpoint.h +37 -10

70 lines not on this page · raw message

arch/arm64/kernel/debug-monitors.c +11 -4
index 58f047de3e1c..50779c68f11e 100644
@@ -21,6 +21,7 @@
 #include <asm/cputype.h>
 #include <asm/daifflags.h>
 #include <asm/debug-monitors.h>
+#include <asm/hw_breakpoint.h>
 #include <asm/system_misc.h>
 #include <asm/traps.h>
 
@@ -34,7 +35,7 @@ u8 debug_monitors_arch(void)
 /*
  * MDSCR access routines.
  */
-static void mdscr_write(u32 mdscr)
+static void mdscr_write(u64 mdscr)
 {
 	unsigned long flags;
 	flags = local_daif_save();
@@ -43,7 +44,7 @@ static void mdscr_write(u32 mdscr)
 }
 NOKPROBE_SYMBOL(mdscr_write);
 
-static u32 mdscr_read(void)
+static u64 mdscr_read(void)
 {
 	return read_sysreg(mdscr_el1);
 }
@@ -79,7 +80,7 @@ static DEFINE_PER_CPU(int, kde_ref_count);
 
 void enable_debug_monitors(enum dbg_active_el el)
 {
-	u32 mdscr, enable = 0;
+	u64 mdscr, enable = 0;
 
 	WARN_ON(preemptible());
 
@@ -90,6 +91,9 @@ void enable_debug_monitors(enum dbg_active_el el)
 	    this_cpu_inc_return(kde_ref_count) == 1)
 		enable |= DBG_MDSCR_KDE;
 
+	if (is_debug_v8p9_enabled())
+		enable |= DBG_MDSCR_EMBWE;
+
 	if (enable && debug_enabled) {
 		mdscr = mdscr_read();
 		mdscr |= enable;
@@ -100,7 +104,7 @@ NOKPROBE_SYMBOL(enable_debug_monitors);
 
 void disable_debug_monitors(enum dbg_active_el el)
 {
-	u32 mdscr, disable = 0;
+	u64 mdscr, disable = 0;
 
 	WARN_ON(preemptible());
 
@@ -111,6 +115,9 @@ void disable_debug_monitors(enum dbg_active_el el)
 	    this_cpu_dec_return(kde_ref_count) == 0)
 		disable &= ~DBG_MDSCR_KDE;
 
+	if (is_debug_v8p9_enabled())
+		disable &= ~DBG_MDSCR_EMBWE;
+
 	if (disable) {
 		mdscr = mdscr_read();
 		mdscr &= disable;
arch/arm64/kernel/hw_breakpoint.c +46 -2

74 lines not on this page · raw message

Signature · 1 line
2.25.1
maintainer
#14 lore raw
Quoted 9 lines · Anshuman Khandual

Currently there can be maximum 16 breakpoints, and 16 watchpoints available on a given platform - as detected from ID_AA64DFR0_EL1.[BRPs|WRPs] register fields. But these breakpoint, and watchpoints can be extended further up to 64 via a new arch feature FEAT_Debugv8p9.

This first enables banked access for the breakpoint and watchpoint register set via MDSELR_EL1, extended exceptions via MDSCR_EL1.EMBWE and determining available breakpoints and watchpoints in the platform from ID_AA64DFR1_EL1, when FEAT_Debugv8p9 is enabled.

[...]

Quoted 12 lines
arch/arm64/kernel/hw_breakpoint.c +12 -0
+static u64 read_wb_reg(int reg, int n)
+{
+	unsigned long flags;
+	u64 val;
+
+	if (!is_debug_v8p9_enabled())
+		return __read_wb_reg(reg, n);
+
+	/*
+	 * Bank selection in MDSELR_EL1, followed by an indexed read from
+	 * breakpoint (or watchpoint) registers cannot be interrupted, as
+	 * that might cause misread from the wrong targets instead. Hence

arch/arm64/kernel/hw_breakpoint.c static u64 read_wb_reg(int reg, int n) · line 143

Quoted 10 lines
arch/arm64/kernel/hw_breakpoint.c +9 -0
+	 * this requires mutual exclusion.
+	 */
+	local_irq_save(flags);
+	write_sysreg_s(SYS_FIELD_PREP(MDSELR_EL1, BANK, n / MAX_PER_BANK), SYS_MDSELR_EL1);
+	isb();
+	val = __read_wb_reg(reg, n % MAX_PER_BANK);
+	local_irq_restore(flags);
+	return val;
+}
 NOKPROBE_SYMBOL(read_wb_reg);

I don't believe that disabling interrupts here is sufficient. On the last version I asked about the case of racing with a watchpoint handler:

| For example, what prevents watchpoint_handler() from firing in the
| middle of arch_install_hw_breakpoint() or
| arch_uninstall_hw_breakpoint()?

... and disabling interrupts cannot prevent that, because local_irq_{save,restore}() do not affect the behaviour of watchpoints or breakpoints.

Please can you try to answer the questions I asked last time, i.e.

| What prevents a race with an exception handler? e.g. 
| 
| * Does the structure of the code prevent that somehow?
| 
| * What context(s) does this code execute in?
|   - Are debug exceptions always masked?
|   - Do we disable breakpoints/watchpoints around (some) manipulation of
|     the relevant registers?

... and the question form the earlier comment, i.e.

| Is the existing code correct?

I suspect that the existing code might not have the necessary mutual exclusion in all cases, but it's difficult to trigger an issue by accident. Is there any way a handler could race with some other manipulation of watchpoints/breakpoints such that some data structure gets corrupted, or such that the kernel deadlocks?

Mark.

#15 lore raw
Quoted 6 lines · Mark Rutland
Quoted 5 lines · Anshuman Khandual

Currently there can be maximum 16 breakpoints, and 16 watchpoints available on a given platform - as detected from ID_AA64DFR0_EL1.[BRPs|WRPs] register fields. But these breakpoint, and watchpoints can be extended further up to 64 via a new arch feature FEAT_Debugv8p9.

Quoted 6 lines
Quoted 4 lines

This first enables banked access for the breakpoint and watchpoint register set via MDSELR_EL1, extended exceptions via MDSCR_EL1.EMBWE and determining available breakpoints and watchpoints in the platform from ID_AA64DFR1_EL1, when FEAT_Debugv8p9 is enabled.

[...]

Well, this series has landed on my plate...

Quoted 26 lines
Quoted 12 lines
arch/arm64/kernel/hw_breakpoint.c +12 -0
+static u64 read_wb_reg(int reg, int n)
+{
+	unsigned long flags;
+	u64 val;
+
+	if (!is_debug_v8p9_enabled())
+		return __read_wb_reg(reg, n);
+
+	/*
+	 * Bank selection in MDSELR_EL1, followed by an indexed read from
+	 * breakpoint (or watchpoint) registers cannot be interrupted, as
+	 * that might cause misread from the wrong targets instead. Hence

arch/arm64/kernel/hw_breakpoint.c static u64 read_wb_reg(int reg, int n) · line 143

Quoted 10 lines
arch/arm64/kernel/hw_breakpoint.c +9 -0
+	 * this requires mutual exclusion.
+	 */
+	local_irq_save(flags);
+	write_sysreg_s(SYS_FIELD_PREP(MDSELR_EL1, BANK, n / MAX_PER_BANK), SYS_MDSELR_EL1);
+	isb();
+	val = __read_wb_reg(reg, n % MAX_PER_BANK);
+	local_irq_restore(flags);
+	return val;
+}
 NOKPROBE_SYMBOL(read_wb_reg);

I don't believe that disabling interrupts here is sufficient. On the last version I asked about the case of racing with a watchpoint handler:

Quoted 7 lines
| For example, what prevents watchpoint_handler() from firing in the
| middle of arch_install_hw_breakpoint() or
| arch_uninstall_hw_breakpoint()?

... and disabling interrupts cannot prevent that, because local_irq_{save,restore}() do not affect the behaviour of watchpoints or breakpoints.

I think the answer is we just need NOKPROBE_SYMBOL() annotation on hw_breakpoint_control() (what arch_install_hw_breakpoint() and arch_uninstall_hw_breakpoint() wrap). We also need that on __read_wb_reg and __read_wb_reg though I would think those are folded into the calling functions by the compiler. Interestly, the x86 code doesn't use the annotation at all.

I initially thought the IRQ disabling is also still needed as IRQ handlers can trigger breakpoints. However, the x86 version of arch_install_hw_breakpoint() contains a lockdep_assert_irqs_disabled(), so it seems for that case interrupts are already disabled. And in debug exceptions, we disable interrupts. So I think the interrupt disabling can be dropped.

Quoted 5 lines

Please can you try to answer the questions I asked last time, i.e.

| What prevents a race with an exception handler? e.g. 
| 
| * Does the structure of the code prevent that somehow?

If you can't set a breakpoint/watchpoint in NOKPROBE_SYMBOL() annotated code, you can't race.

However, there's no such annotation for data. It looks like the kernel policy is "don't do that" or disable all breakpoints/watchpoints.

Quoted 3 lines
| 
| * What context(s) does this code execute in?
|   - Are debug exceptions always masked?

No.

Quoted 2 lines

| - Do we disable breakpoints/watchpoints around (some) manipulation of
| the relevant registers?

Yes, with NOKPROBE_SYMBOL().

Rob

maintainer
#16 lore raw
Quoted 7 lines · Rob Herring
Quoted 6 lines · Mark Rutland
Quoted 5 lines · Anshuman Khandual

Currently there can be maximum 16 breakpoints, and 16 watchpoints available on a given platform - as detected from ID_AA64DFR0_EL1.[BRPs|WRPs] register fields. But these breakpoint, and watchpoints can be extended further up to 64 via a new arch feature FEAT_Debugv8p9.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

This first enables banked access for the breakpoint and watchpoint register set via MDSELR_EL1, extended exceptions via MDSCR_EL1.EMBWE and determining available breakpoints and watchpoints in the platform from ID_AA64DFR1_EL1, when FEAT_Debugv8p9 is enabled.

[...]

Well, this series has landed on my plate...

Sorry about that; thanks for taking a look!

Quoted 30 lines
Quoted 30 lines
Quoted 12 lines
arch/arm64/kernel/hw_breakpoint.c +12 -0
+static u64 read_wb_reg(int reg, int n)
+{
+	unsigned long flags;
+	u64 val;
+
+	if (!is_debug_v8p9_enabled())
+		return __read_wb_reg(reg, n);
+
+	/*
+	 * Bank selection in MDSELR_EL1, followed by an indexed read from
+	 * breakpoint (or watchpoint) registers cannot be interrupted, as
+	 * that might cause misread from the wrong targets instead. Hence

arch/arm64/kernel/hw_breakpoint.c static u64 read_wb_reg(int reg, int n) · line 143

Quoted 10 lines
arch/arm64/kernel/hw_breakpoint.c +9 -0
+	 * this requires mutual exclusion.
+	 */
+	local_irq_save(flags);
+	write_sysreg_s(SYS_FIELD_PREP(MDSELR_EL1, BANK, n / MAX_PER_BANK), SYS_MDSELR_EL1);
+	isb();
+	val = __read_wb_reg(reg, n % MAX_PER_BANK);
+	local_irq_restore(flags);
+	return val;
+}
 NOKPROBE_SYMBOL(read_wb_reg);

I don't believe that disabling interrupts here is sufficient. On the last version I asked about the case of racing with a watchpoint handler:

| For example, what prevents watchpoint_handler() from firing in the
| middle of arch_install_hw_breakpoint() or
| arch_uninstall_hw_breakpoint()?
Quoted 7 lines
Quoted 3 lines

... and disabling interrupts cannot prevent that, because local_irq_{save,restore}() do not affect the behaviour of watchpoints or breakpoints.

I think the answer is we just need NOKPROBE_SYMBOL() annotation on hw_breakpoint_control() (what arch_install_hw_breakpoint() and arch_uninstall_hw_breakpoint() wrap).

Ok. I couldn'y spot where we prevent placing HW breakpoints on NOKPROBE_SYMBOL() functions, but if we do enforce that, something like that sounds ok.

I suspect we'd need to make that noinstr to also prevent ftrace instrumentation, unless ftrace also inhibits itself for NOKPROBE_SYMBOL() functions.

Quoted 4 lines

We also need that on __read_wb_reg
and __read_wb_reg though I would think those are folded into the calling functions by the compiler. Interestly, the x86 code doesn't use the annotation at all.

IIUC, it looks like they *can* take debug NMIs during arch_install_hw_breakpoint() and arch_uninstall_hw_breakpoint(), which is why they have ordering constraints for modifying the percpu 'cpu_dr7' variable, and their actual DR7 register (which IIUC has the enable controls for each HW breakpoint).

That said, the use of 'bp_per_reg' looks suspect given their arch_install_hw_breakpoint() and arch_uninstall_hw_breakpoint() modify that non-atomically.

We could consider allowing breakpoints on those functions, but I'm not sure whether that's possible for us, and (as noted below) it might be better to transiently disable breakpoints/watchpoints.

IIRC on x86, breakpoint exceptions are taken *after* execution of the instruction that triggered them, so the handler doesn't have to manipulate single-step, and can safely ignore a breakpoint exception without the risk of getting stuck taking the breakpoint repeatedly.

Quoted 6 lines

I initially thought the IRQ disabling is also still needed as IRQ handlers can trigger breakpoints. However, the x86 version of arch_install_hw_breakpoint() contains a lockdep_assert_irqs_disabled(), so it seems for that case interrupts are already disabled. And in debug exceptions, we disable interrupts. So I think the interrupt disabling can be dropped.

I'd expect that the core perf code disables interrupts before calling arch_install_hw_breakpoint() or arch_uninstall_hw_breakpoint(), and this would be necessary for perf to serialise against IPIs that manipulate the perf_event_context.

I agree that when we actually take the breakpoint, we'll mask all exceptions, and so it's not necessary to mask IRQs there.

So a first step is probably to add that lockdep assert.

Quoted 8 lines
Quoted 5 lines

Please can you try to answer the questions I asked last time, i.e.

| What prevents a race with an exception handler? e.g. 
| 
| * Does the structure of the code prevent that somehow?

If you can't set a breakpoint/watchpoint in NOKPROBE_SYMBOL() annotated code, you can't race.

As above, I agree (with caveats), but I couldn't spot where this is enforced.

Quoted 2 lines

However, there's no such annotation for data. It looks like the kernel policy is "don't do that" or disable all breakpoints/watchpoints.

If we have to transiently disable watchpoints/breakpoints when manipulating the relevant HW registers, that sounds fine to me.

Quoted 10 lines
Quoted 3 lines
| 
| * What context(s) does this code execute in?
|   - Are debug exceptions always masked?

No.

Quoted 2 lines

| - Do we disable breakpoints/watchpoints around (some) manipulation of
| the relevant registers?

Yes, with NOKPROBE_SYMBOL().

Thanks for digging into this; much appreciated!

Mark.

#17 lore raw
Quoted 54 lines · Mark Rutland
Quoted 7 lines · Rob Herring
Quoted 6 lines · Mark Rutland
Quoted 5 lines · Anshuman Khandual

Currently there can be maximum 16 breakpoints, and 16 watchpoints available on a given platform - as detected from ID_AA64DFR0_EL1.[BRPs|WRPs] register fields. But these breakpoint, and watchpoints can be extended further up to 64 via a new arch feature FEAT_Debugv8p9.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

This first enables banked access for the breakpoint and watchpoint register set via MDSELR_EL1, extended exceptions via MDSCR_EL1.EMBWE and determining available breakpoints and watchpoints in the platform from ID_AA64DFR1_EL1, when FEAT_Debugv8p9 is enabled.

[...]

Well, this series has landed on my plate...

Sorry about that; thanks for taking a look!

Quoted 34 lines
Quoted 33 lines
Quoted 12 lines
arch/arm64/kernel/hw_breakpoint.c +12 -0
+static u64 read_wb_reg(int reg, int n)
+{
+ unsigned long flags;
+ u64 val;
+
+ if (!is_debug_v8p9_enabled())
+         return __read_wb_reg(reg, n);
+
+ /*
+  * Bank selection in MDSELR_EL1, followed by an indexed read from
+  * breakpoint (or watchpoint) registers cannot be interrupted, as
+  * that might cause misread from the wrong targets instead. Hence

arch/arm64/kernel/hw_breakpoint.c static u64 read_wb_reg(int reg, int n) · line 143

Quoted 10 lines
arch/arm64/kernel/hw_breakpoint.c +9 -0
+  * this requires mutual exclusion.
+  */
+ local_irq_save(flags);
+ write_sysreg_s(SYS_FIELD_PREP(MDSELR_EL1, BANK, n / MAX_PER_BANK), SYS_MDSELR_EL1);
+ isb();
+ val = __read_wb_reg(reg, n % MAX_PER_BANK);
+ local_irq_restore(flags);
+ return val;
+}
 NOKPROBE_SYMBOL(read_wb_reg);

I don't believe that disabling interrupts here is sufficient. On the last version I asked about the case of racing with a watchpoint handler:

| For example, what prevents watchpoint_handler() from firing in the
| middle of arch_install_hw_breakpoint() or
| arch_uninstall_hw_breakpoint()?

... and disabling interrupts cannot prevent that, because local_irq_{save,restore}() do not affect the behaviour of watchpoints or breakpoints.

Quoted 7 lines
Quoted 3 lines

I think the answer is we just need NOKPROBE_SYMBOL() annotation on hw_breakpoint_control() (what arch_install_hw_breakpoint() and arch_uninstall_hw_breakpoint() wrap).

Ok. I couldn'y spot where we prevent placing HW breakpoints on NOKPROBE_SYMBOL() functions, but if we do enforce that, something like that sounds ok.

Uh, actually you are right. I think we need the equivalent to this in arch_build_bp_info():

    case HW_BREAKPOINT_X:
        /*
         * We don't allow kernel breakpoints in places that are not
         * acceptable for kprobes.  On non-kprobes kernels, we don't
         * allow kernel breakpoints at all.
         */
        if (attr->bp_addr >= TASK_SIZE_MAX) {
            if (within_kprobe_blacklist(attr->bp_addr))
                return -EINVAL;
        }

The 2nd sentence is enforced by within_kprobe_blacklist() returning true for !CONFIG_KPROBES. Seems like a reasonable restriction, but it would be a change. Unfortunately, we can't move this to non-arch code as some arches support h/w BP without supporting kprobes.

Quoted 4 lines

I suspect we'd need to make that noinstr to also prevent ftrace instrumentation, unless ftrace also inhibits itself for NOKPROBE_SYMBOL() functions.

If we use NOKPROBE_SYMBOL() along with nokprobe_inline (which just forces inlining functions), then ftrace could only be used on the entry or exit of arch_install_hw_breakpoint()/arch_uninstall_hw_breakpoint() which I think would be fine.

Quoted 12 lines
Quoted 5 lines

We also need that on __read_wb_reg
and __read_wb_reg though I would think those are folded into the calling functions by the compiler. Interestly, the x86 code doesn't use the annotation at all.

IIUC, it looks like they *can* take debug NMIs during arch_install_hw_breakpoint() and arch_uninstall_hw_breakpoint(), which is why they have ordering constraints for modifying the percpu 'cpu_dr7' variable, and their actual DR7 register (which IIUC has the enable controls for each HW breakpoint).

Quoted 3 lines

That said, the use of 'bp_per_reg' looks suspect given their arch_install_hw_breakpoint() and arch_uninstall_hw_breakpoint() modify that non-atomically.

You don't believe the comment saying counter->ctx->lock is held?

Quoted 3 lines

We could consider allowing breakpoints on those functions, but I'm not sure whether that's possible for us, and (as noted below) it might be better to transiently disable breakpoints/watchpoints.

Even if possible, is it all that useful? Easier to just disable than reason whether it would work or not IMO.

Quoted 31 lines

IIRC on x86, breakpoint exceptions are taken *after* execution of the instruction that triggered them, so the handler doesn't have to manipulate single-step, and can safely ignore a breakpoint exception without the risk of getting stuck taking the breakpoint repeatedly.

Quoted 6 lines

I initially thought the IRQ disabling is also still needed as IRQ handlers can trigger breakpoints. However, the x86 version of arch_install_hw_breakpoint() contains a lockdep_assert_irqs_disabled(), so it seems for that case interrupts are already disabled. And in debug exceptions, we disable interrupts. So I think the interrupt disabling can be dropped.

I'd expect that the core perf code disables interrupts before calling arch_install_hw_breakpoint() or arch_uninstall_hw_breakpoint(), and this would be necessary for perf to serialise against IPIs that manipulate the perf_event_context.

I agree that when we actually take the breakpoint, we'll mask all exceptions, and so it's not necessary to mask IRQs there.

So a first step is probably to add that lockdep assert.

Quoted 8 lines
Quoted 5 lines

Please can you try to answer the questions I asked last time, i.e.

| What prevents a race with an exception handler? e.g.
|
| * Does the structure of the code prevent that somehow?

If you can't set a breakpoint/watchpoint in NOKPROBE_SYMBOL() annotated code, you can't race.

Quoted 8 lines

As above, I agree (with caveats), but I couldn't spot where this is enforced.

Quoted 2 lines

However, there's no such annotation for data. It looks like the kernel policy is "don't do that" or disable all breakpoints/watchpoints.

If we have to transiently disable watchpoints/breakpoints when manipulating the relevant HW registers, that sounds fine to me.

For wp/bp_on_reg, the ordering is 'data access, h/w accesses'. I think we just need a barrier to enforce that ordering so the data access (and then watchpoint) don't trigger in the middle of the h/w accesses. Any guidance on the flavor of dsb here? (And is there any guarantee that the access is visible to the watchpoint h/w after a dsb completes?)

Rob

maintainer
#18 lore raw
Quoted 10 lines · Rob Herring
Quoted 7 lines · Mark Rutland
Quoted 2 lines · Rob Herring
Quoted 1 line · Mark Rutland

On Mon, Dec 16, 2024 at 09:38:31AM +0530, Anshuman Khandual wrote:

That said, the use of 'bp_per_reg' looks suspect given their arch_install_hw_breakpoint() and arch_uninstall_hw_breakpoint() modify that non-atomically.

You don't believe the comment saying counter->ctx->lock is held?

Sorry, my concern here was that hw_breakpoint_handler() (which cannot hold the lock) consumes bp_per_reg[], and could race with the non-atomic modification in arch_install_hw_breakpoint() or arch_uninstall_hw_breakpoint().

I've sent a more elaborate mail to x86 folk, with that and another issue caused by taking a breakpoint under arch_uninstall_hw_breakpoint():

https://lore.kernel.org/lkml/[email protected]/

I think we have a similar latent issue where we can take an breakpoint or watchpoint ad infinitum, described in more detail at the end of this mail.

[...]

Quoted 13 lines
Quoted 13 lines
Quoted 6 lines
Quoted 3 lines
| What prevents a race with an exception handler? e.g.
|
| * Does the structure of the code prevent that somehow?

If you can't set a breakpoint/watchpoint in NOKPROBE_SYMBOL() annotated code, you can't race.

As above, I agree (with caveats), but I couldn't spot where this is enforced.

Quoted 2 lines

However, there's no such annotation for data. It looks like the kernel policy is "don't do that" or disable all breakpoints/watchpoints.

Quoted 6 lines
Quoted 2 lines

If we have to transiently disable watchpoints/breakpoints when manipulating the relevant HW registers, that sounds fine to me.

For wp/bp_on_reg, the ordering is 'data access, h/w accesses'. I think we just need a barrier to enforce that ordering so the data access (and then watchpoint) don't trigger in the middle of the h/w accesses.

I assume that by 'h/w accesses' you mean MSRs to the system registers controlling breakpoints/watchpoints. Ordering-wise, I don't believe memory barriers are necessary here (explain in more detail below). However, I also think there's a latent issue here that might bite us with the new banking, described at the end of this mail.

Both breakpoint and watchpoint exceptions are synchronous, meanning that they can only be taken from the specific instruction that triggered them. However, updates to the watchpoint control registers *do* need a context synchronization event before they're guarnateed to take effect.

For a sequence:

    // Initially:
    // - MDSCR, MDCR, DAIF.D permit debug exceptions at CurrentEL
    // - No watchpoints enabled
    
    0x000:  LDR <val>, [<addr>]
    0x004:  MSR DBGWVR<n>_EL1, <addr>
    0x008:  MSR DBGWCR<n>_EL1, <configure_and_enable>
    0x010:  LDR <val>, [<addr>]
    0x014:  ISB
    0x018:  LDR <val>, [<addr>]

... we know:

    (a) The LDR at 0x000 *cannot* trigger the watchpoint.
    (b) The LDR at 0x010 *might* trigger the matchpoint.
    (c) The LDR at 0x018 *must* trigger the watchpoint.

For C code, we can enforce this order with barrier(), e.g.

	val = *addr;
	barrier();
	write_sysreg(addr, DBGWVR<n>_EL1);
	write_sysreg(configure_and_enable, DBGWCR<n>_EL1);
	isb();

... where the compiler cannot re-order the memory access (or write_sysreg(), or isb()) across the barrier(), and as isb() has a memory clobber, the same is true for isb().

Likewise, for the inverse sequence:

    // Initially:
    // - MDSCR, MDCR, DAIF.D permit debug exceptions at CurrentEL
    // - Watchpoint configured and enabled for <addr>

    0x100:  LDR <val>, [<addr>]
    0x104:  MSR DBGWCR<n>_EL1, <disable>
    0x108:  LDR <val>, [<addr>]
    0x110:  ISB
    0x114:  LDR <val>, [<addr>]

... we know:

    (a) The LDR at 0x100 *must* trigger the watchpoint.
    (b) The LDR at 0x108 *might* trigger the watchpoint.
    (c) The LDR at 0x114 *cannot* trigger the watchpoint.
Quoted 3 lines

Any guidance on the flavor of dsb here? (And is there any guarantee that the access is visible to the watchpoint h/w after a dsb completes?)

Hopefully the above was sufficient?

As mentioned above, I think we have a latent issue where we can take a breakpoint or watchpoint under arch_uninstall_hw_breakpoint(), where we have:

    arch_uninstall_hw_breakpoint(bp) {
        ...
	hw_breakpoint_control(bp, HW_BREAKPOINT_UNINSTALL) {
	    ...
	    hw_breakpoint_slot_setup(slots, max_slots, bp, HW_BREAKPOINT_UNINSTALL) {
	         ...
		 *slot = NULL;
		 ...
	    }
	    ...
	    write_wb_reg(ctrl_reg, i, 0) {
	        ...
	    	write_sysreg(0, ...);
		isb();
		...
	    }
	}
    }

The HW breakpoint/watchpoint associated with 'bp' could be triggered between setting '*slot' to NULL and the ISB. If that happens, then do_breakpoint() won't find 'bp', and will return *without* disabling the HW breakpoint or attempting to step.

If that first exception was taken *before* the MSR in write_sysreg(), then nothing has changed, and the breakpoint/watchpoint will then be taken again ad infinitum.

If that first exception was taken *after* the MSR in write_sysreg(), the context synchronization provided by exception entry/return will prevent it from being taken again.

Building v6.19 and testing (with pseudo-NMI enabled):

| #  grep write_wb_reg /proc/kallsyms
| ffff80008004b980 t write_wb_reg
| # ./perf-6.19 stat -a -C 1 -e 'mem:0xffff80008004b980/4:xk' true
| rcu: INFO: rcu_preempt detected stalls on CPUs/tasks:
| rcu:     1-...0: (1 GPs behind) idle=2334/1/0x4000000000000000 softirq=140/140 fqs=2623
| rcu:     (detected by 0, t=5252 jiffies, g=-859, q=7 ncpus=2)
| Sending NMI from CPU 0 to CPUs 1:
| NMI backtrace for cpu 1
| CPU: 1 UID: 0 PID: 139 Comm: perf-6.19 Not tainted 6.19.0 #1 PREEMPT 
| Hardware name: linux,dummy-virt (DT)
| pstate: 60400005 (nZCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
| pc : write_wb_reg+0x0/0x250
| lr : hw_breakpoint_control+0x164/0x248
| sp : ffff800082d73980
| pmr: 000000c0
| x29: ffff800082d73980 x28: ffff000004692dc0 x27: ffff8000802708a0
| x26: ffff00000473c000 x25: 0000000000000000 x24: 0000000000000000
| x23: ffff00000473c000 x22: 0000000000000000 x21: ffff800082d73b70
| x20: ffff00007fbd7b28 x19: ffff00000473c000 x18: 0000000000000000
| x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
| x14: 0000000000000000 x13: 0000000000000000 x12: 0000000000000000
| x11: 0000000000000000 x10: 0000000000000000 x9 : 0000000000000000
| x8 : 0000000000000001 x7 : 0000000000000001 x6 : 0000000000000010
| x5 : 0000000000000001 x4 : 0000000000000006 x3 : ffff00000473c000
| x2 : 0000000000000000 x1 : 0000000000000000 x0 : 0000000000000010
| Call trace:
|  write_wb_reg+0x0/0x250 (P)
|  arch_uninstall_hw_breakpoint+0x14/0x20
|  hw_breakpoint_del+0x10/0x20
|  event_sched_out+0x8c/0x160
|  group_sched_out+0x44/0xa0
|  __perf_event_disable.part.0+0x114/0x120
|  __perf_event_disable+0x1c/0x2c
|  event_function+0x84/0xd8
|  remote_function+0x50/0x64
|  generic_exec_single+0x88/0xf0
|  smp_call_function_single+0x90/0x1c8
|  event_function_call+0x1d8/0x1e4
|  _perf_event_disable+0x44/0x6c
|  perf_event_for_each_child+0x38/0x84
|  _perf_ioctl+0x19c/0xab8
|  perf_ioctl+0x50/0x80
|  __arm64_sys_ioctl+0xa4/0x100
|  invoke_syscall.constprop.0+0x40/0xf0
|  el0_svc_common.constprop.0+0x38/0xd8
|  do_el0_svc+0x1c/0x28
|  el0_svc+0x38/0x148
|  el0t_64_sync_handler+0xa0/0xe4
|  el0t_64_sync+0x1ac/0x1b0

I think that ideally when diabling a breakpoint/watchpoint, we'd program the control register *before* manipulating the slot. The existing structure of the code is rather unhelpful for fixing that.

While this does fall into "don't do that" territory, I do think we should aim for the kernel to survive rather than fall into a livelock. I'll take another look into this shortly -- I think we need a wider cleanup.

Mark.

#19 lore raw
Quoted 73 lines · Mark Rutland
Quoted 10 lines · Rob Herring
Quoted 7 lines · Mark Rutland
Quoted 2 lines · Rob Herring
Quoted 1 line · Mark Rutland

On Mon, Dec 16, 2024 at 09:38:31AM +0530, Anshuman Khandual wrote:

That said, the use of 'bp_per_reg' looks suspect given their arch_install_hw_breakpoint() and arch_uninstall_hw_breakpoint() modify that non-atomically.

You don't believe the comment saying counter->ctx->lock is held?

Sorry, my concern here was that hw_breakpoint_handler() (which cannot hold the lock) consumes bp_per_reg[], and could race with the non-atomic modification in arch_install_hw_breakpoint() or arch_uninstall_hw_breakpoint().

I've sent a more elaborate mail to x86 folk, with that and another issue caused by taking a breakpoint under arch_uninstall_hw_breakpoint():

https://lore.kernel.org/lkml/[email protected]/

I think we have a similar latent issue where we can take an breakpoint or watchpoint ad infinitum, described in more detail at the end of this mail.

[...]

Quoted 13 lines
Quoted 13 lines
Quoted 6 lines
Quoted 3 lines
| What prevents a race with an exception handler? e.g.
|
| * Does the structure of the code prevent that somehow?

If you can't set a breakpoint/watchpoint in NOKPROBE_SYMBOL() annotated code, you can't race.

As above, I agree (with caveats), but I couldn't spot where this is enforced.

Quoted 2 lines

However, there's no such annotation for data. It looks like the kernel policy is "don't do that" or disable all breakpoints/watchpoints.

Quoted 6 lines
Quoted 2 lines

If we have to transiently disable watchpoints/breakpoints when manipulating the relevant HW registers, that sounds fine to me.

For wp/bp_on_reg, the ordering is 'data access, h/w accesses'. I think we just need a barrier to enforce that ordering so the data access (and then watchpoint) don't trigger in the middle of the h/w accesses.

I assume that by 'h/w accesses' you mean MSRs to the system registers controlling breakpoints/watchpoints. Ordering-wise, I don't believe memory barriers are necessary here (explain in more detail below). However, I also think there's a latent issue here that might bite us with the new banking, described at the end of this mail.

Both breakpoint and watchpoint exceptions are synchronous, meanning that they can only be taken from the specific instruction that triggered them. However, updates to the watchpoint control registers *do* need a context synchronization event before they're guarnateed to take effect.

For a sequence:

    // Initially:
    // - MDSCR, MDCR, DAIF.D permit debug exceptions at CurrentEL
    // - No watchpoints enabled

    0x000:  LDR <val>, [<addr>]
    0x004:  MSR DBGWVR<n>_EL1, <addr>
    0x008:  MSR DBGWCR<n>_EL1, <configure_and_enable>
    0x010:  LDR <val>, [<addr>]
    0x014:  ISB
    0x018:  LDR <val>, [<addr>]
Quoted 3 lines

... we know:

    (a) The LDR at 0x000 *cannot* trigger the watchpoint.

Why not? Can't the LDR complete after the MSR? Is ordering ensured between those? Surely the watchpoint triggers on completion of the load and that wouldn't stall the PE from doing the MSR(s)?

Quoted 75 lines
    (b) The LDR at 0x010 *might* trigger the matchpoint.
    (c) The LDR at 0x018 *must* trigger the watchpoint.

For C code, we can enforce this order with barrier(), e.g.

        val = *addr;
        barrier();
        write_sysreg(addr, DBGWVR<n>_EL1);
        write_sysreg(configure_and_enable, DBGWCR<n>_EL1);
        isb();

... where the compiler cannot re-order the memory access (or write_sysreg(), or isb()) across the barrier(), and as isb() has a memory clobber, the same is true for isb().

Likewise, for the inverse sequence:

    // Initially:
    // - MDSCR, MDCR, DAIF.D permit debug exceptions at CurrentEL
    // - Watchpoint configured and enabled for <addr>

    0x100:  LDR <val>, [<addr>]
    0x104:  MSR DBGWCR<n>_EL1, <disable>
    0x108:  LDR <val>, [<addr>]
    0x110:  ISB
    0x114:  LDR <val>, [<addr>]

... we know:

    (a) The LDR at 0x100 *must* trigger the watchpoint.
    (b) The LDR at 0x108 *might* trigger the watchpoint.
    (c) The LDR at 0x114 *cannot* trigger the watchpoint.
Quoted 3 lines

Any guidance on the flavor of dsb here? (And is there any guarantee that the access is visible to the watchpoint h/w after a dsb completes?)

Hopefully the above was sufficient?

As mentioned above, I think we have a latent issue where we can take a breakpoint or watchpoint under arch_uninstall_hw_breakpoint(), where we have:

    arch_uninstall_hw_breakpoint(bp) {
        ...
        hw_breakpoint_control(bp, HW_BREAKPOINT_UNINSTALL) {
            ...
            hw_breakpoint_slot_setup(slots, max_slots, bp, HW_BREAKPOINT_UNINSTALL) {
                 ...
                 *slot = NULL;
                 ...
            }
            ...
            write_wb_reg(ctrl_reg, i, 0) {
                ...
                write_sysreg(0, ...);
                isb();
                ...
            }
        }
    }

The HW breakpoint/watchpoint associated with 'bp' could be triggered between setting '*slot' to NULL and the ISB. If that happens, then do_breakpoint() won't find 'bp', and will return *without* disabling the HW breakpoint or attempting to step.

If that first exception was taken *before* the MSR in write_sysreg(), then nothing has changed, and the breakpoint/watchpoint will then be taken again ad infinitum.

If that first exception was taken *after* the MSR in write_sysreg(), the context synchronization provided by exception entry/return will prevent it from being taken again.

Quoted 5 lines

Building v6.19 and testing (with pseudo-NMI enabled):

| #  grep write_wb_reg /proc/kallsyms
| ffff80008004b980 t write_wb_reg
| # ./perf-6.19 stat -a -C 1 -e 'mem:0xffff80008004b980/4:xk' true

I'll give it a try with v4, but that should be prevented with my changes to exclude kprobe regions.

Rob

maintainer
#20 lore raw
Quoted 19 lines · Rob Herring
Quoted 18 lines · Mark Rutland

Both breakpoint and watchpoint exceptions are synchronous, meanning that they can only be taken from the specific instruction that triggered them. However, updates to the watchpoint control registers *do* need a context synchronization event before they're guarnateed to take effect.

For a sequence:

    // Initially:
    // - MDSCR, MDCR, DAIF.D permit debug exceptions at CurrentEL
    // - No watchpoints enabled

    0x000:  LDR <val>, [<addr>]
    0x004:  MSR DBGWVR<n>_EL1, <addr>
    0x008:  MSR DBGWCR<n>_EL1, <configure_and_enable>
    0x010:  LDR <val>, [<addr>]
    0x014:  ISB
    0x018:  LDR <val>, [<addr>]
Quoted 5 lines
Quoted 3 lines

... we know:

    (a) The LDR at 0x000 *cannot* trigger the watchpoint.

Why not?

Because:

(a) As noted above, both breakpoint and watchpoint exceptions are synchronous. They can only be taken from the specific *instruction* that triggered them, and the exception must be consistent with no subsequent instructions having been executed.

In ARM DDI 0487 M.a.a, see:

- Section D1.4 ("Exceptions"), and in particular RFQHGR, RTNVSL.
- Section D2.8 ("Breakpoint exceptions")
- Section D2.9 ("Watchpoint exceptions")

(b) Generally, writes to system registers cannot affect earlier instructions.

In ARM DDI 0487 M.a.a, see: Section D24.1.2.2 ("Synchronization requirements for AArch64 System registers").

Note in particular the statement: "Direct writes to System registers are not allowed to affect any instructions appearing in program order before the direct write."

Quoted 1 line

Can't the LDR complete after the MSR?

The memory effects of the LDR at 0x000 could complete after the MSRs at 0x004 and 0x008 are executed.

However, any watchpoints/breakpoint triggered by the LDR at 0x000 must be taken *before* the next instruction (i.e. the MSR at 0x004) can be architecturally executed, and any such execption must be consistent with th PE state prior to that next instruction being executed.

Note that this doesn't forbid the PE from speculating the MSR and other subsequent instructions; it just has to maintain program order (so later instructions can't affect earlier instructions), and must be able to discard anything that was speculated past taking an exception.

Quoted 3 lines

Is ordering ensured between those? Surely the watchpoint triggers on completion of the load and that wouldn't stall the PE from doing the MSR(s)?

Hopefully the above covers these concerns?

Quoted 76 lines
Quoted 76 lines
    (b) The LDR at 0x010 *might* trigger the matchpoint.
    (c) The LDR at 0x018 *must* trigger the watchpoint.

For C code, we can enforce this order with barrier(), e.g.

        val = *addr;
        barrier();
        write_sysreg(addr, DBGWVR<n>_EL1);
        write_sysreg(configure_and_enable, DBGWCR<n>_EL1);
        isb();

... where the compiler cannot re-order the memory access (or write_sysreg(), or isb()) across the barrier(), and as isb() has a memory clobber, the same is true for isb().

Likewise, for the inverse sequence:

    // Initially:
    // - MDSCR, MDCR, DAIF.D permit debug exceptions at CurrentEL
    // - Watchpoint configured and enabled for <addr>

    0x100:  LDR <val>, [<addr>]
    0x104:  MSR DBGWCR<n>_EL1, <disable>
    0x108:  LDR <val>, [<addr>]
    0x110:  ISB
    0x114:  LDR <val>, [<addr>]

... we know:

    (a) The LDR at 0x100 *must* trigger the watchpoint.
    (b) The LDR at 0x108 *might* trigger the watchpoint.
    (c) The LDR at 0x114 *cannot* trigger the watchpoint.
Quoted 3 lines

Any guidance on the flavor of dsb here? (And is there any guarantee that the access is visible to the watchpoint h/w after a dsb completes?)

Hopefully the above was sufficient?

As mentioned above, I think we have a latent issue where we can take a breakpoint or watchpoint under arch_uninstall_hw_breakpoint(), where we have:

    arch_uninstall_hw_breakpoint(bp) {
        ...
        hw_breakpoint_control(bp, HW_BREAKPOINT_UNINSTALL) {
            ...
            hw_breakpoint_slot_setup(slots, max_slots, bp, HW_BREAKPOINT_UNINSTALL) {
                 ...
                 *slot = NULL;
                 ...
            }
            ...
            write_wb_reg(ctrl_reg, i, 0) {
                ...
                write_sysreg(0, ...);
                isb();
                ...
            }
        }
    }

The HW breakpoint/watchpoint associated with 'bp' could be triggered between setting '*slot' to NULL and the ISB. If that happens, then do_breakpoint() won't find 'bp', and will return *without* disabling the HW breakpoint or attempting to step.

If that first exception was taken *before* the MSR in write_sysreg(), then nothing has changed, and the breakpoint/watchpoint will then be taken again ad infinitum.

If that first exception was taken *after* the MSR in write_sysreg(), the context synchronization provided by exception entry/return will prevent it from being taken again.

Quoted 8 lines
Quoted 5 lines

Building v6.19 and testing (with pseudo-NMI enabled):

| #  grep write_wb_reg /proc/kallsyms
| ffff80008004b980 t write_wb_reg
| # ./perf-6.19 stat -a -C 1 -e 'mem:0xffff80008004b980/4:xk' true

I'll give it a try with v4, but that should be prevented with my changes to exclude kprobe regions.

That'll work for breakpoints specifically, but not for watchpoints, and I think for that we either have to dynamically disable watchpoint/breakpoint exceptions (e.g. via DAIF.D), or write the code to be race-aware. I strongly suspect that the latter will be simpler.

I think we can solve that with some wider cleanup, e.g. having arch_uninstall_hw_breakpoint() disable the breakpoint/watchpoint *before* setting its slot to NULL, but we'll need to take some care (e.g. to save/restore MDSELR).

I strongly suspect that we can defer implementing support for EMBWE until we've done a more general cleanup, but I'll need to go do some reading first.

Mark.

#21 lore raw
In reply to Anshuman Khandual
Quoted 30 lines · Anshuman Khandual

Currently there can be maximum 16 breakpoints, and 16 watchpoints available on a given platform - as detected from ID_AA64DFR0_EL1.[BRPs|WRPs] register fields. But these breakpoint, and watchpoints can be extended further up to 64 via a new arch feature FEAT_Debugv8p9.

This first enables banked access for the breakpoint and watchpoint register set via MDSELR_EL1, extended exceptions via MDSCR_EL1.EMBWE and determining available breakpoints and watchpoints in the platform from ID_AA64DFR1_EL1, when FEAT_Debugv8p9 is enabled.

---
Changes in V3:

- Used SYS_FIELD_PREP() in read_wb_reg() and write_wb_reg()
- Added MAX_PER_BANK based BUILD_BUG_ON() tests in arch_hw_breakpoint_init()
- Dropped local variables i.e mdsel_bank and index
- Derived bank and index from MAX_PER_BANK as required

 arch/arm64/include/asm/debug-monitors.h |  1 +
 arch/arm64/include/asm/hw_breakpoint.h  | 47 ++++++++++++++++++------
 arch/arm64/kernel/debug-monitors.c      | 15 +++++---
 arch/arm64/kernel/hw_breakpoint.c       | 48 +++++++++++++++++++++++--
 4 files changed, 95 insertions(+), 16 deletions(-)

[...]

arch/arm64/kernel/debug-monitors.c void enable_debug_monitors(enum dbg_active_el el) · line 95

Quoted 13 lines

void enable_debug_monitors(enum dbg_active_el el) {

arch/arm64/kernel/debug-monitors.c +3 -1
-	u32 mdscr, enable = 0;
+	u64 mdscr, enable = 0;
 
 	WARN_ON(preemptible());
@@ -90,6 +91,9 @@ void enable_debug_monitors(enum dbg_active_el el)
 	    this_cpu_inc_return(kde_ref_count) == 1)
 		enable |= DBG_MDSCR_KDE;
 
+	if (is_debug_v8p9_enabled())
+		enable |= DBG_MDSCR_EMBWE;

Just to record my own findings, this is in the wrong place. This will cause the below code to run every time, not just on the first enable or kernel or user mode breakpoints. it needs to be moved into the below if.

Quoted 4 lines
arch/arm64/kernel/debug-monitors.c +1 -0
+
 	if (enable && debug_enabled) {
 		mdscr = mdscr_read();
 		mdscr |= enable;