]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
arm64: debug: split hardware watchpoint exception entry
authorAda Couprie Diaz <ada.coupriediaz@arm.com>
Mon, 7 Jul 2025 11:41:06 +0000 (12:41 +0100)
committerWill Deacon <will@kernel.org>
Tue, 8 Jul 2025 12:27:42 +0000 (13:27 +0100)
Currently all debug exceptions share common entry code and are routed
to `do_debug_exception()`, which calls dynamically-registered
handlers for each specific debug exception. This is unfortunate as
different debug exceptions have different entry handling requirements,
and it would be better to handle these distinct requirements earlier.

Hardware watchpoints are the only debug exceptions that will write
FAR_EL1, so we need to preserve it and pass it down.
However, they cannot be used to maliciously train branch predictors, so
we can omit calling `arm64_bp_hardening()`, nor do they need to handle
the Cortex-A76 erratum #1463225, as it only applies to single stepping
exceptions.

As the hardware watchpoint handler only returns 0 and never triggers
the call to `arm64_notify_die()`, we can call it directly from
`entry-common.c`.
Split the hardware watchpoint exception entry and adjust the behaviour
to match the lack of needed mitigations.

Signed-off-by: Ada Couprie Diaz <ada.coupriediaz@arm.com>
Tested-by: Luis Claudio R. Goncalves <lgoncalv@redhat.com>
Reviewed-by: Will Deacon <will@kernel.org>
Acked-by: Mark Rutland <mark.rutland@arm.com>
Link: https://lore.kernel.org/r/20250707114109.35672-11-ada.coupriediaz@arm.com
Signed-off-by: Will Deacon <will@kernel.org>
arch/arm64/include/asm/exception.h
arch/arm64/kernel/entry-common.c
arch/arm64/kernel/hw_breakpoint.c

index c8e7c61b8ac43485fc80fa09403dd577231f39ec..0362fecc5f69cba18749299c3d0d955730c3a160 100644 (file)
@@ -63,8 +63,12 @@ void do_debug_exception(unsigned long addr_if_watchpoint, unsigned long esr,
                        struct pt_regs *regs);
 #ifdef CONFIG_HAVE_HW_BREAKPOINT
 void do_breakpoint(unsigned long esr, struct pt_regs *regs);
+void do_watchpoint(unsigned long addr, unsigned long esr,
+                       struct pt_regs *regs);
 #else
 static inline void do_breakpoint(unsigned long esr, struct pt_regs *regs) {}
+static inline void do_watchpoint(unsigned long addr, unsigned long esr,
+                       struct pt_regs *regs) {}
 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
 void do_el0_softstep(unsigned long esr, struct pt_regs *regs);
 void do_el1_softstep(unsigned long esr, struct pt_regs *regs);
index 7265bef96672ff6905619c8c67fa963a10cab0a8..8a6d951c14cc9b39b32cd7e91b4d581acaa6a355 100644 (file)
@@ -553,6 +553,18 @@ static void noinstr el1_softstp(struct pt_regs *regs, unsigned long esr)
        arm64_exit_el1_dbg(regs);
 }
 
+static void noinstr el1_watchpt(struct pt_regs *regs, unsigned long esr)
+{
+       /* Watchpoints are the only debug exception to write FAR_EL1 */
+       unsigned long far = read_sysreg(far_el1);
+
+       arm64_enter_el1_dbg(regs);
+       debug_exception_enter(regs);
+       do_watchpoint(far, esr, regs);
+       debug_exception_exit(regs);
+       arm64_exit_el1_dbg(regs);
+}
+
 static void noinstr el1_dbg(struct pt_regs *regs, unsigned long esr)
 {
        unsigned long far = read_sysreg(far_el1);
@@ -608,6 +620,8 @@ asmlinkage void noinstr el1h_64_sync_handler(struct pt_regs *regs)
                el1_softstp(regs, esr);
                break;
        case ESR_ELx_EC_WATCHPT_CUR:
+               el1_watchpt(regs, esr);
+               break;
        case ESR_ELx_EC_BRK64:
                el1_dbg(regs, esr);
                break;
@@ -832,6 +846,19 @@ static void noinstr el0_softstp(struct pt_regs *regs, unsigned long esr)
        exit_to_user_mode(regs);
 }
 
+static void noinstr el0_watchpt(struct pt_regs *regs, unsigned long esr)
+{
+       /* Watchpoints are the only debug exception to write FAR_EL1 */
+       unsigned long far = read_sysreg(far_el1);
+
+       enter_from_user_mode(regs);
+       debug_exception_enter(regs);
+       do_watchpoint(far, esr, regs);
+       debug_exception_exit(regs);
+       local_daif_restore(DAIF_PROCCTX);
+       exit_to_user_mode(regs);
+}
+
 static void noinstr el0_dbg(struct pt_regs *regs, unsigned long esr)
 {
        /* Only watchpoints write FAR_EL1, otherwise its UNKNOWN */
@@ -917,6 +944,8 @@ asmlinkage void noinstr el0t_64_sync_handler(struct pt_regs *regs)
                el0_softstp(regs, esr);
                break;
        case ESR_ELx_EC_WATCHPT_LOW:
+               el0_watchpt(regs, esr);
+               break;
        case ESR_ELx_EC_BRK64:
                el0_dbg(regs, esr);
                break;
@@ -1041,6 +1070,8 @@ asmlinkage void noinstr el0t_32_sync_handler(struct pt_regs *regs)
                el0_softstp(regs, esr);
                break;
        case ESR_ELx_EC_WATCHPT_LOW:
+               el0_watchpt(regs, esr);
+               break;
        case ESR_ELx_EC_BKPT32:
                el0_dbg(regs, esr);
                break;
index 8a80e13347c88fe237c4b973c5c31c54bc46b216..ab76b36dce820bd12e01acb1c3946df9f69e8156 100644 (file)
@@ -750,8 +750,7 @@ static int watchpoint_report(struct perf_event *wp, unsigned long addr,
        return step;
 }
 
-static int watchpoint_handler(unsigned long addr, unsigned long esr,
-                             struct pt_regs *regs)
+void do_watchpoint(unsigned long addr, unsigned long esr, struct pt_regs *regs)
 {
        int i, step = 0, *kernel_step, access, closest_match = 0;
        u64 min_dist = -1, dist;
@@ -806,7 +805,7 @@ static int watchpoint_handler(unsigned long addr, unsigned long esr,
        rcu_read_unlock();
 
        if (!step)
-               return 0;
+               return;
 
        /*
         * We always disable EL0 watchpoints because the kernel can
@@ -819,7 +818,7 @@ static int watchpoint_handler(unsigned long addr, unsigned long esr,
 
                /* If we're already stepping a breakpoint, just return. */
                if (debug_info->bps_disabled)
-                       return 0;
+                       return;
 
                if (test_thread_flag(TIF_SINGLESTEP))
                        debug_info->suspended_step = 1;
@@ -830,7 +829,7 @@ static int watchpoint_handler(unsigned long addr, unsigned long esr,
                kernel_step = this_cpu_ptr(&stepping_kernel_bp);
 
                if (*kernel_step != ARM_KERNEL_STEP_NONE)
-                       return 0;
+                       return;
 
                if (kernel_active_single_step()) {
                        *kernel_step = ARM_KERNEL_STEP_SUSPEND;
@@ -839,10 +838,8 @@ static int watchpoint_handler(unsigned long addr, unsigned long esr,
                        kernel_enable_single_step(regs);
                }
        }
-
-       return 0;
 }
-NOKPROBE_SYMBOL(watchpoint_handler);
+NOKPROBE_SYMBOL(do_watchpoint);
 
 /*
  * Handle single-step exception.
@@ -984,10 +981,6 @@ static int __init arch_hw_breakpoint_init(void)
        pr_info("found %d breakpoint and %d watchpoint registers.\n",
                core_num_brps, core_num_wrps);
 
-       /* Register debug fault handlers. */
-       hook_debug_fault_code(DBG_ESR_EVT_HWWP, watchpoint_handler, SIGTRAP,
-                             TRAP_HWBKPT, "hw-watchpoint handler");
-
        /*
         * Reset the breakpoint resources. We assume that a halting
         * debugger will leave the world in a nice state for us.