Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

16.1 Self-hosted debug

In self-hosted debug, software running on the PE is its own debugger. Debug events become debug exceptions that are taken to an EL1 or EL2 handler, as in GDB under Linux or a debug monitor in firmware. External (halting) debug, where events stop the PE in Debug state, is the subject of 16.2. The two share the same breakpoint and watchpoint hardware, and EDSCR.HDE and the authentication interface decide which model an event uses.

The debug exceptions and their syndromes

ExceptionGenerated byEC (lower EL / same EL)Gate
Breakpoint InstructionBRK #imm160x3C (one value)always generated, even when debug is otherwise disabled
BreakpointDBGBCR<n>_EL1/DBGBVR<n>_EL1 match0x30 / 0x31MDSCR_EL1.MDE
WatchpointDBGWCR<n>_EL1/DBGWVR<n>_EL1 match0x34 / 0x35MDSCR_EL1.MDE
Software StepMDSCR_EL1.SS plus PSTATE.SS0x32 / 0x33MDSCR_EL1.SS
Vector CatchAArch32 EL1 only0x3AMDSCR_EL1.MDE

BRK is a synchronous exception like SVC. It is not subject to the enable rules below, and the imm16 comment field appears in the ISS. Every other debug exception is generated only if debug exceptions are enabled from the current Exception level and Security state. The ASL function that decides this is AArch64.GenerateDebugExceptionsFrom(). In outline:

  1. The debug target Exception level, ELd, is EL2 if EL2 is enabled in the current Security state and either MDCR_EL2.TDE or HCR_EL2.TGE is 1. Otherwise ELd is EL1. In AArch64, debug exceptions other than BRK are never taken to EL3, and none are generated while executing at EL3.
  2. If the OS Lock is locked, or the OS Double Lock is in effect (DoubleLockStatus()), or the PE is halted, nothing is generated.
  3. In Secure state, MDCR_EL3.SDD == 1 disables them at every Secure EL.
  4. From an EL lower than ELd, they are enabled. From ELd itself, they are enabled only if MDSCR_EL1.KDE == 1 and PSTATE.D == 0. From an EL higher than ELd, they are never enabled.

This rule is why a kernel can debug its own EL1 code only with KDE set and PSTATE.D cleared. It is also why a hypervisor that sets MDCR_EL2.TDE takes over every guest debug event, and why EL3 firmware cannot be debugged self-hosted at all. PSTATE.D is set on every exception entry, so a handler for a debug exception cannot recurse into itself by default.

MDSCR_EL1 fields that matter here (bit positions from the System Register XML):

BitFieldMeaning
15MDEenables Breakpoint, Watchpoint and Vector Catch exceptions
14HDEthe self-hosted view of EDSCR.HDE (halting debug enable)
13KDEenables debug exceptions at ELd
12TDCCtraps EL0 access to the DCC registers
0SSSoftware Step enable

Breakpoints

ID_AA64DFR0_EL1.BRPs is the number of breakpoints minus one, and CTX_CMPs is the number that are context-aware, also minus one. The context-aware breakpoints are the highest-numbered ones. Before FEAT_Debugv8p9 there are at most 16 of each. FEAT_Debugv8p9 allows up to 64 and exposes them in banks of 16 through MDSELR_EL1.BANK, with the counts in ID_AA64DFR1_EL1.{BRPs,WRPs,CTX_CMPs}.

DBGBCR<n>_EL1 fields:

  • E (bit 0): enable.
  • PMC, HMC, SSC (with SSCE under RME): select the Exception levels and Security states in which the breakpoint matches. In the common cases, with HMC=0 and SSC=0b00, PMC=0b01 matches EL1 only, 0b10 matches EL0 only and 0b11 matches both. These are the values Linux’s hw_breakpoint uses. For the full table, see the Arm ARM section “Execution conditions for which a breakpoint generates Breakpoint exceptions”.
  • BAS [8:5]: byte-address select, which exists to select halfwords for T32. For an A64 instruction address match, program it to 0b1111. For what other values do, see the DBGBCR<n>_EL1.BAS description. Linux always writes 0xF here.
  • BT: the breakpoint type. 0b0000 is an unlinked address match and 0b0001 is a linked address match. Further types match on Context ID (CONTEXTIDR_EL1), VMID, or CONTEXTIDR_EL2, and are available only on context-aware breakpoints.
  • LBN: for a linked breakpoint, the context-aware breakpoint it is linked to. An address match fires only if the linked context also matches. This is how a debugger sets “break at VA X only in process Y”.

DBGBVR<n>_EL1 holds the word-aligned instruction VA. The upper bits follow the sign-extension rules for the implemented VA size. On a Breakpoint exception, ELR_ELx holds the address of the breakpointed instruction. That instruction has not executed, and the handler must disable the breakpoint or step over it before returning.

Watchpoints

DBGWCR<n>_EL1/DBGWVR<n>_EL1 match data accesses:

  • LSC: 0b01 loads, 0b10 stores, 0b11 both.
  • BAS: a byte mask over the doubleword-aligned (DBGWVR[2:0] == 0) address. 0xFF watches all 8 bytes.
  • MASK: when non-zero, watches a naturally aligned 2^MASK-byte region (the minimum is 8 bytes). It is mutually exclusive with a partial BAS.
  • WT, LBN: link the watchpoint to a context-aware breakpoint for context-qualified watching.
  • PAC, HMC, SSC: the same EL and Security filtering as for breakpoints.

A Watchpoint exception is synchronous and precise with respect to the accessing instruction. ELR_ELx points at the load or store, which has not completed architecturally. For a single-access store, the watched memory has not been updated, so the handler can inspect the old value. For instructions that make several accesses, such as STP or DC ZVA, check “Watchpoint exceptions” in the Arm ARM for whether the other accesses may already have been performed before relying on this. ESR_ELx.ISS.WnR gives the direction, and FAR_ELx holds an address from the access, not necessarily the watched address. A DC ZVA or an LDP that only partly overlaps the watched bytes still triggers, and FAR_ELx can point anywhere in the accessed range that the architecture allows. Some cache maintenance instructions, DC IVAC among them, can also trigger a store watchpoint. For which instructions do and which are only permitted to, see “Watchpoint exceptions” in the Arm ARM.

Bring-up note: DBGWVR/DBGBVR compare virtual addresses in the translation regime of the matching EL. If you set a watchpoint before SCTLR_EL1.M is set and then enable the MMU with a non-identity mapping, the watchpoint now watches a different physical location. Watchpoints do match with the MMU off (VA == PA), which is handy for catching stray writes into a page-table pool during early boot.

Software step

Software step is a small state machine on MDSCR_EL1.SS and PSTATE.SS:

inactive          MDSCR_EL1.SS == 0, or debug exceptions disabled from the current EL
active-not-pending  MDSCR_EL1.SS == 1 and PSTATE.SS == 1: the PE executes ONE instruction
active-pending    MDSCR_EL1.SS == 1 and PSTATE.SS == 0: Software Step exception is taken
                  before the next instruction

The debugger’s handler (at ELd) sets SPSR_ELx.SS = 1 (bit 21) and executes ERET to the debuggee. The PE then executes one instruction, PSTATE.SS becomes 0, and the step exception is taken with ELR_ELx pointing to the next instruction. Things to know:

  • If the stepped instruction itself causes an exception to ELd, the handler is not stepped, because it runs at ELd with PSTATE.D == 1. The step state travels in SPSR_ELx.SS. After an exception-generating instruction such as SVC, which counts as stepped, the saved SS is 0, so the step exception is taken right after the handler’s ERET. After a fault, where the instruction did not complete, the instruction is stepped again when it re-executes. For the exact rules, see “Software Step exceptions” in the Arm ARM. ESR_ELx.ISS.ISV qualifies the EX bit described next.
  • ESR_ELx.ISS.EX reports that the stepped instruction was a Load-Exclusive. Stepping through an LDXR/STXR loop can livelock, because the step exception clears the exclusive monitor (an exception return is permitted to clear it). Debuggers step over the whole loop with a breakpoint after it. This is one reason to prefer LSE atomics (10.2).
  • With KDE = 1, a kernel can single-step itself at EL1 (EC 0x33). This is how kprobes or kgdb step.

The OS Lock

The OS Lock exists so that an OS can save and restore the debug registers across a core power-down without an external debugger changing them underneath it.

  • OSLAR_EL1.OSLK (WO, bit 0): write 1 to lock and 0 to unlock.
  • OSLSR_EL1.OSLK (bit 1) reads the state. OSLSR_EL1.OSLM (bits [3,0]) reads 0b10 (“implemented”), the only permitted value in Armv8.

While the lock is set:

  • Breakpoint, Watchpoint, Software Step and Vector Catch exceptions are not generated. BRK still is.
  • External debugger accesses to most debug registers return an error response, including EDSCR, EDITR and DBGBCR<n>_EL1 through the external interface. Software at EL1 or above can still read and write them through system register accesses in order to save and restore them.
  • A debugger can arm EDECR.OSUCE (OS Unlock Catch) to halt the PE as soon as software unlocks.

The OS Lock is set on Cold reset (OSLSR_EL1.OSLK resets to 1). Linux clears it on every CPU during early CPU setup, including on the resume path (older kernels did this in clear_os_lock() in debug-monitors.c; recent kernels do it in the early boot/resume code). A bare-metal or RTOS image that never writes OSLAR_EL1 keeps self-hosted debug disabled, and an external debugger sees error responses on the core’s debug registers until it writes OSLAR_EL1 itself through the external interface at offset 0x300.

Traps: MDCR_EL3.TDOSA and MDCR_EL2.TDOSA trap OSLAR_EL1, OSLSR_EL1, OSDLR_EL1 and DBGPRCR_EL1 (EC 0x18). MDCR_ELx.TDA traps the breakpoint, watchpoint and MDSCR_EL1 accesses. These let a hypervisor virtualise guest debug.

The OS Double Lock (FEAT_DoubleLock)

OSDLR_EL1.DLK (bit 0) is the “really powering down now” lock. ASL defines:

DoubleLockStatus() = OSDLR_EL1.DLK == 1 && DBGPRCR_EL1.CORENPDRQ == 0 && !Halted()

When it is in effect, no debug events are generated and external debug accesses to most of the core’s debug registers (including EDSCR and the breakpoint registers) return an error. This is meant to be the last step before the power controller removes Core power, so that no debug APB transaction is caught in flight across the power boundary. DBGPRCR_EL1.CORENPDRQ (Core No Power-Down Request, driven by a debugger through EDPRCR) overrides it. In that case the power controller emulates the power-down, which is how a debugger keeps a core alive across CPU_SUSPEND.

Version status: ID_AA64DFR0_EL1.DoubleLock (bits [39:36], a signed field) is 0b0000 (implemented) or 0b1111 (not implemented, with OSDLR_EL1 RAZ/WI).

  • In Armv8.0 the double lock is mandatory.
  • With FEAT_Debugv8p2 it becomes optional.
  • With FEAT_DoPD (Debug over Powerdown, which moves all external debug registers into the Core power domain) it is not permitted.
  • Following DDI0487 A2, Armv9-A implementations do not implement FEAT_DoubleLock.

Code that executes MSR OSDLR_EL1, x therefore has to check the ID field. On a part without the feature the write is ignored, so do not rely on the lock for power-down safety there.

Security note: self-hosted debug exceptions in Secure state are a way to observe Secure execution. TF-A sets MDCR_EL3.SDD = 1 (Secure self-hosted debug disabled, except BRK) and, for AArch32 Secure EL1, SPD32 = 0b10. When you review a secure-world image, check that no path clears SDD in production builds, and that MDCR_EL3.TDA/TDOSA handling does not let the Normal world reprogram a breakpoint that matches in Secure state. With SSC/HMC a breakpoint can be configured to match Secure EL1, and SDD is what stops it from firing.

Validation note: a minimal self-hosted debug test at EL1 sets MDSCR_EL1.{MDE,KDE}, clears PSTATE.D (msr daifclr, #8), programs DBGBVR0_EL1 with a function address and DBGBCR0_EL1 = (0xF << 5) | (0b01 << 1) | 1 (BAS = 0b1111, EL1, enabled), executes ISB, and calls the function. Expect EC 0x31 with ELR_EL1 equal to the function address. The same test with OSLAR_EL1 = 1 must take no exception, and the same test with KDE = 0 must take none either. Without the ISB after programming, the breakpoint is permitted to fire but is not guaranteed to. If it never fires even with the ISB, the usual cause is an OS Lock that was never cleared.

Checkpoint

Q16.1.1 EL1 code sets MDSCR_EL1.MDE = 1, programs and enables a breakpoint that matches at EL1, executes ISB, and calls the target. HCR_EL2.TGE == 0, MDCR_EL2.TDE == 0, and the OS Lock is unlocked. No exception is taken. Which explanation is consistent with the architecture?

  • A. Breakpoints are only generated for EL0 code, so EL1 must use BRK instead
  • B. The ISB is not enough. A DSB SY is required for a DBGBCR write to take effect
  • C. MDSCR_EL1.KDE is 0 or PSTATE.D is 1, so debug exceptions are disabled at the debug target EL itself
  • D. Breakpoint exceptions are always routed to EL3, which ignores them
Answer

C. ELd is EL1 here, and debug exceptions from ELd require both KDE == 1 and PSTATE.D == 0. Option B is tempting because barrier questions usually have that shape, but writes to debug system registers are made visible to subsequent instructions by a context synchronization event, and ISB is one. DSB adds nothing here. EL1 breakpoints exist, which rules out A, and AArch64 never takes non-BRK debug exceptions to EL3, which rules out D.

Read more: Arm ARM (DDI0487), “Enabling debug exceptions from the current Exception level and Security state”, MDSCR_EL1

Q16.1.2 A bring-up team’s RTOS image boots on a new core. The external debugger connects over the DAP but reports errors whenever it tries to program a hardware breakpoint, even though authentication signals permit invasive debug. What is the most likely cause?

  • A. The OS Lock is still set from Cold reset, so external accesses to the breakpoint registers return an error response
  • B. The OS Double Lock is set from Cold reset and blocks all external access
  • C. MDSCR_EL1.MDE is 0, which makes the breakpoint registers read-only externally
  • D. The breakpoint registers can only be written by software running at EL1
Answer

A. OSLSR_EL1.OSLK resets to 1 on Cold reset, and while it is set, external accesses to DBGBCR<n>_EL1 and similar registers generate an error response. Linux clears it, but a bare-metal image often does not. The debugger can clear it through the external OSLAR_EL1. B is wrong because OSDLR_EL1.DLK resets to 0, and on Armv9 parts the double lock is not implemented at all. MDE controls only self-hosted exception generation, which rules out C.

Read more: Arm ARM (DDI0487), OSLSR_EL1, OSLAR_EL1, “The OS Lock”

Q16.1.3 A debugger single-steps EL0 code under Linux and reaches an LDXR/STXR retry loop. Each step completes, yet the loop never exits. Why?

  • A. Software step is not permitted on Load-Exclusive instructions and silently skips them
  • B. PSTATE.SS is cleared by STXR, so the step exception never arrives
  • C. The step exception is taken at EL1, which is above ELd, so it is ignored
  • D. Taking the step exception and returning between LDXR and STXR is permitted to clear the local exclusive monitor, so STXR keeps failing
Answer

D. Each step exception is an exception entry and return between the LDXR and the STXR, and an exception return is permitted to clear the exclusive monitor, so the store-exclusive fails every time. This is why ESR_ELx.ISS.EX flags a stepped Load-Exclusive: it lets the debugger switch to a breakpoint after the loop. Option A is wrong because the instruction is stepped normally. Option C misreads the routing: the exception is taken to EL1 (ELd) from EL0, which is the normal case.

Read more: Arm ARM (DDI0487), “Software Step exceptions”, “Stepping a Load-Exclusive instruction”