Skip to contentSkip to conversation

accel/tcg: Use IntervalTree for breakpoints and watchpoints

PATCH Superseded Richard Henderson 3 participants 28 messages Jul 1 RISC-V

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover accel/tcg: Use IntervalTree for breakpoints and watchpoints

Follow-up to [email protected] ("[RFC PATCH 0/2] change cpu->breakpoints to a GTree")

Beyond using interval-tree for breakpoints:

- Pass the matching breakpoint to the target debug hook,
- Allow the target to record an id/index for each BP or WP so that the target doesn't have to search for which item triggered, so that the target doesn't have to search again.
- Convert both BP and WP to IntervalTree.

r~

Richard Henderson (17):
accel/tcg: Pass CPUBreakpoint to debug_check_breakpoint accel/tcg: Constify CPUWatchpoint for debug_check_watchpoint
cpu: Move cpu_breakpoint_test out of line exec/breakpoint: Move BP declarations from hw/core/cpu.h qemu/typedefs: Declare CPUBreakpoint and CPUWatchpoint globally *: Add missing include of exec/breakpoint.h hw/core/cpu: Remove exec/breakpoint.h include exec/breakpoint: Introduce BP_CPU_ID_SHIFT
target/arm: Split is_wp out of bp_wp_matches target/arm: Set ID for watchpoints and breakpoints target/arm: Use bp argument in arm_debug_check_breakpoint target/arm: Use wp argument in arm_debug_check_watchpoint target/riscv: Set ID for watchpoints and breakpoints target/riscv: Use bp argument in riscv_cpu_debug_check_breakpoint target/riscv: Use bp argument in riscv_cpu_debug_check_watchpoint target/xtensa: Use BP_CPU_ID_SHIFT
cpu: Use interval-tree for CPUBreakpoint

 include/accel/tcg/cpu-ops.h         |   4 +-
 include/exec/breakpoint.h           |  39 ++++++++--
 include/hw/core/cpu.h               |  39 +---------
 include/qemu/typedefs.h             |   2 +
 target/arm/internals.h              |   4 +-
 target/ppc/internal.h               |   4 +-
 target/riscv/debug.h                |   4 +-
 target/xtensa/cpu.h                 |   2 +-
 accel/tcg/cpu-exec.c                |  48 ++++++------
 accel/tcg/tcg-accel-ops.c           |   1 +
 cpu-common.c                        |  54 ++++++++-----
 gdbstub/system.c                    |   1 +
 gdbstub/user.c                      |   1 +
 hw/core/cpu-common.c                |   1 -
 linux-user/main.c                   |  13 +++-
 system/watchpoint.c                 |   1 +
 target/arm/tcg/debug.c              |  76 ++++--------------
 target/i386/cpu.c                   |   1 +
 target/i386/kvm/kvm.c               |   1 +
 target/i386/machine.c               |   1 +
 target/i386/tcg/system/bpt_helper.c |   1 +
 target/i386/tcg/tcg-cpu.c           |   2 +-
 target/ppc/cpu.c                    |   1 +
 target/ppc/tcg-excp_helper.c        |   4 +-
 target/riscv/debug.c                | 116 ++++++----------------------
 target/xtensa/dbg_helper.c          |  19 ++---
 target/xtensa/helper.c              |   1 +
 27 files changed, 174 insertions(+), 267 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/17 accel/tcg: Pass CPUBreakpoint to debug_check_breakpoint

For 3 of the 4 targets that use this hook, add the argument and ignore it so far. For riscv, drop the loop over all breakpoints and use the new argument.

---
 include/accel/tcg/cpu-ops.h  |  2 +-
 target/arm/internals.h       |  2 +-
 target/ppc/internal.h        |  2 +-
 target/riscv/debug.h         |  2 +-
 target/xtensa/cpu.h          |  2 +-
 accel/tcg/cpu-exec.c         |  2 +-
 target/arm/tcg/debug.c       |  2 +-
 target/i386/tcg/tcg-cpu.c    |  2 +-
 target/ppc/tcg-excp_helper.c |  2 +-
 target/riscv/debug.c         | 55 +++++++++++++++++-------------------
 target/xtensa/dbg_helper.c   |  2 +-
 11 files changed, 36 insertions(+), 39 deletions(-)
include/accel/tcg/cpu-ops.h +1 -1
index 710da12b82..d2b9079835 100644
@@ -265,7 +265,7 @@ struct TCGCPUOps {
      * @debug_check_breakpoint: return true if the architectural
      * breakpoint whose PC has matched should really fire.
      */
-    bool (*debug_check_breakpoint)(CPUState *cpu);
+    bool (*debug_check_breakpoint)(CPUState *cpu, const CPUBreakpoint *bp);
 
     /**
      * @io_recompile_replay_branch: Callback for cpu_io_recompile.
target/arm/internals.h +1 -1
index fcce3804f3..c78c86a1fc 100644
@@ -680,7 +680,7 @@ void hw_breakpoint_update(ARMCPU *cpu, int n);
 void hw_breakpoint_update_all(ARMCPU *cpu);
 
 /* Callback function for checking if a breakpoint should trigger. */
-bool arm_debug_check_breakpoint(CPUState *cs);
+bool arm_debug_check_breakpoint(CPUState *cs, const CPUBreakpoint *bp);
 
 /* Callback function for checking if a watchpoint should trigger. */
 bool arm_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp);
target/ppc/internal.h +1 -1
index a3bb12afd9..f3e694c4cd 100644
@@ -304,7 +304,7 @@ void ppc_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
                                    int mmu_idx, MemTxAttrs attrs,
                                    MemTxResult response, uintptr_t retaddr);
 void ppc_cpu_debug_excp_handler(CPUState *cs);
-bool ppc_cpu_debug_check_breakpoint(CPUState *cs);
+bool ppc_cpu_debug_check_breakpoint(CPUState *cs, const CPUBreakpoint *bp);
 bool ppc_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp);
 
 G_NORETURN void powerpc_checkstop(CPUPPCState *env, const char *reason);
target/riscv/debug.h +1 -1
index 55a3ac72e6..91690404df 100644
@@ -144,7 +144,7 @@ void tdata_csr_write(CPURISCVState *env, int tdata_index, target_ulong val);
 target_ulong tinfo_csr_read(CPURISCVState *env);
 
 void riscv_cpu_debug_excp_handler(CPUState *cs);
-bool riscv_cpu_debug_check_breakpoint(CPUState *cs);
+bool riscv_cpu_debug_check_breakpoint(CPUState *cs, const CPUBreakpoint *bp);
 bool riscv_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp);
 
 void riscv_trigger_realize(CPURISCVState *env);
target/xtensa/cpu.h +1 -1
index 442e98bd1b..9323f45e4b 100644
@@ -591,7 +591,7 @@ void xtensa_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr, vaddr addr,
                                       int mmu_idx, MemTxAttrs attrs,
                                       MemTxResult response, uintptr_t retaddr);
 hwaddr xtensa_cpu_get_phys_addr_debug(CPUState *cpu, vaddr addr);
-bool xtensa_debug_check_breakpoint(CPUState *cs);
+bool xtensa_debug_check_breakpoint(CPUState *cs, const CPUBreakpoint *bp);
 #endif
 void xtensa_cpu_dump_state(CPUState *cpu, FILE *f, int flags);
 void xtensa_count_regs(const XtensaConfig *config,
accel/tcg/cpu-exec.c +1 -1
index 9c754b0365..66ec656861 100644
@@ -328,7 +328,7 @@ static bool check_for_breakpoints_slow(CPUState *cpu, vaddr pc,
 #else
                 const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
                 assert(tcg_ops->debug_check_breakpoint);
-                match_bp = tcg_ops->debug_check_breakpoint(cpu);
+                match_bp = tcg_ops->debug_check_breakpoint(cpu, bp);
 #endif
             }
 
target/arm/tcg/debug.c +1 -1
index 07a52643e7..565af7b985 100644
@@ -373,7 +373,7 @@ static bool check_watchpoints(ARMCPU *cpu)
     return false;
 }
 
-bool arm_debug_check_breakpoint(CPUState *cs)
+bool arm_debug_check_breakpoint(CPUState *cs, const CPUBreakpoint *bp)
 {
     ARMCPU *cpu = ARM_CPU(cs);
     CPUARMState *env = &cpu->env;
target/i386/tcg/tcg-cpu.c +1 -1
index 6f5dc06b3b..ee534f0a15 100644
@@ -132,7 +132,7 @@ static int x86_cpu_mmu_index(CPUState *cs, bool ifetch)
 }
 
 #ifndef CONFIG_USER_ONLY
-static bool x86_debug_check_breakpoint(CPUState *cs)
+static bool x86_debug_check_breakpoint(CPUState *cs, const CPUBreakpoint *bp)
 {
     X86CPU *cpu = X86_CPU(cs);
     CPUX86State *env = &cpu->env;
target/ppc/tcg-excp_helper.c +1 -1
index b04f07a637..7715cae342 100644
@@ -331,7 +331,7 @@ void ppc_cpu_debug_excp_handler(CPUState *cs)
 #endif
 }
 
-bool ppc_cpu_debug_check_breakpoint(CPUState *cs)
+bool ppc_cpu_debug_check_breakpoint(CPUState *cs, const CPUBreakpoint *bp)
 {
 #if defined(TARGET_PPC64)
     CPUPPCState *env = cpu_env(cs);
target/riscv/debug.c +26 -29

73 lines not on this page · raw message

target/xtensa/dbg_helper.c +1 -1
index 3b91f7c38a..3d1143ad5e 100644
@@ -64,7 +64,7 @@ void HELPER(wsr_ibreaka)(CPUXtensaState *env, uint32_t i, uint32_t v)
     env->sregs[IBREAKA + i] = v;
 }
 
-bool xtensa_debug_check_breakpoint(CPUState *cs)
+bool xtensa_debug_check_breakpoint(CPUState *cs, const CPUBreakpoint *bp)
 {
     CPUXtensaState *env = cpu_env(cs);
     unsigned int i;
Signature · 1 line
2.43.0
Reviewed-by on v1 1/17
#2 lore raw

Re: [PATCH 01/17] accel/tcg: Pass CPUBreakpoint to debug_check_breakpoint

Quoted 5 lines · Richard Henderson

For 3 of the 4 targets that use this hook, add the argument and ignore it so far. For riscv, drop the loop over all breakpoints and use the new argument.

Quoted 13 lines
---
  include/accel/tcg/cpu-ops.h  |  2 +-
  target/arm/internals.h       |  2 +-
  target/ppc/internal.h        |  2 +-
  target/riscv/debug.h         |  2 +-
  target/xtensa/cpu.h          |  2 +-
  accel/tcg/cpu-exec.c         |  2 +-
  target/arm/tcg/debug.c       |  2 +-
  target/i386/tcg/tcg-cpu.c    |  2 +-
  target/ppc/tcg-excp_helper.c |  2 +-
  target/riscv/debug.c         | 55 +++++++++++++++++-------------------
  target/xtensa/dbg_helper.c   |  2 +-
  11 files changed, 36 insertions(+), 39 deletions(-)
#3 lore raw

PATCH 2/17 accel/tcg: Constify CPUWatchpoint for debug_check_watchpoint

In reply to Richard Henderson
---
 include/accel/tcg/cpu-ops.h  | 2 +-
 target/arm/internals.h       | 2 +-
 target/ppc/internal.h        | 2 +-
 target/riscv/debug.h         | 2 +-
 target/arm/tcg/debug.c       | 2 +-
 target/ppc/tcg-excp_helper.c | 2 +-
 target/riscv/debug.c         | 2 +-
 7 files changed, 7 insertions(+), 7 deletions(-)
include/accel/tcg/cpu-ops.h +1 -1
index d2b9079835..1e163176f7 100644
@@ -259,7 +259,7 @@ struct TCGCPUOps {
      * watchpoint whose address has matched should really fire, used by ARM
      * and RISC-V
      */
-    bool (*debug_check_watchpoint)(CPUState *cpu, CPUWatchpoint *wp);
+    bool (*debug_check_watchpoint)(CPUState *cpu, const CPUWatchpoint *wp);
 
     /**
      * @debug_check_breakpoint: return true if the architectural
target/arm/internals.h +1 -1
index c78c86a1fc..7a4f6938b7 100644
@@ -683,7 +683,7 @@ void hw_breakpoint_update_all(ARMCPU *cpu);
 bool arm_debug_check_breakpoint(CPUState *cs, const CPUBreakpoint *bp);
 
 /* Callback function for checking if a watchpoint should trigger. */
-bool arm_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp);
+bool arm_debug_check_watchpoint(CPUState *cs, const CPUWatchpoint *wp);
 
 /* Adjust addresses (in BE32 mode) before testing against watchpoint
  * addresses.
target/ppc/internal.h +1 -1
index f3e694c4cd..fd089ebba5 100644
@@ -305,7 +305,7 @@ void ppc_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
                                    MemTxResult response, uintptr_t retaddr);
 void ppc_cpu_debug_excp_handler(CPUState *cs);
 bool ppc_cpu_debug_check_breakpoint(CPUState *cs, const CPUBreakpoint *bp);
-bool ppc_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp);
+bool ppc_cpu_debug_check_watchpoint(CPUState *cs, const CPUWatchpoint *wp);
 
 G_NORETURN void powerpc_checkstop(CPUPPCState *env, const char *reason);
 void powerpc_excp(PowerPCCPU *cpu, int excp);
target/riscv/debug.h +1 -1
index 91690404df..230d517f01 100644
@@ -145,7 +145,7 @@ target_ulong tinfo_csr_read(CPURISCVState *env);
 
 void riscv_cpu_debug_excp_handler(CPUState *cs);
 bool riscv_cpu_debug_check_breakpoint(CPUState *cs, const CPUBreakpoint *bp);
-bool riscv_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp);
+bool riscv_cpu_debug_check_watchpoint(CPUState *cs, const CPUWatchpoint *wp);
 
 void riscv_trigger_realize(CPURISCVState *env);
 void riscv_trigger_reset_hold(CPURISCVState *env);
target/arm/tcg/debug.c +1 -1
index 565af7b985..ed94b20739 100644
@@ -419,7 +419,7 @@ bool arm_debug_check_breakpoint(CPUState *cs, const CPUBreakpoint *bp)
     return false;
 }
 
-bool arm_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)
+bool arm_debug_check_watchpoint(CPUState *cs, const CPUWatchpoint *wp)
 {
     /*
      * Called by core code when a CPU watchpoint fires; need to check if this
target/ppc/tcg-excp_helper.c +1 -1
index 7715cae342..7fbedbc8e3 100644
@@ -358,7 +358,7 @@ bool ppc_cpu_debug_check_breakpoint(CPUState *cs, const CPUBreakpoint *bp)
     return false;
 }
 
-bool ppc_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)
+bool ppc_cpu_debug_check_watchpoint(CPUState *cs, const CPUWatchpoint *wp)
 {
 #if defined(TARGET_PPC64)
     CPUPPCState *env = cpu_env(cs);
target/riscv/debug.c +1 -1
index 1acec77caa..ad9df09cf7 100644
@@ -983,7 +983,7 @@ bool riscv_cpu_debug_check_breakpoint(CPUState *cs, const CPUBreakpoint *bp)
     return false;
 }
 
-bool riscv_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)
+bool riscv_cpu_debug_check_watchpoint(CPUState *cs, const CPUWatchpoint *wp)
 {
     RISCVCPU *cpu = RISCV_CPU(cs);
     CPURISCVState *env = &cpu->env;
Signature · 1 line
2.43.0
Reviewed-by on v1 2/17
#4 lore raw

Re: [PATCH 02/17] accel/tcg: Constify CPUWatchpoint for debug_check_watchpoint

Quoted 10 lines · Richard Henderson
---
  include/accel/tcg/cpu-ops.h  | 2 +-
  target/arm/internals.h       | 2 +-
  target/ppc/internal.h        | 2 +-
  target/riscv/debug.h         | 2 +-
  target/arm/tcg/debug.c       | 2 +-
  target/ppc/tcg-excp_helper.c | 2 +-
  target/riscv/debug.c         | 2 +-
  7 files changed, 7 insertions(+), 7 deletions(-)
#5 lore raw

PATCH 3/17 cpu: Move cpu_breakpoint_test out of line

In reply to Richard Henderson

Move the function to cpu-common.c, with the other
breakpoint functions.

---
 include/hw/core/cpu.h | 16 +---------------
 cpu-common.c          | 15 +++++++++++++++
 2 files changed, 16 insertions(+), 15 deletions(-)
include/hw/core/cpu.h +1 -15
index 59d601465b..b853824e47 100644
@@ -1157,21 +1157,7 @@ int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
 int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags);
 void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint);
 void cpu_breakpoint_remove_all(CPUState *cpu, int mask);
-
-/* Return true if PC matches an installed breakpoint.  */
-static inline bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask)
-{
-    CPUBreakpoint *bp;
-
-    if (unlikely(!QTAILQ_EMPTY(&cpu->breakpoints))) {
-        QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
-            if (bp->pc == pc && (bp->flags & mask)) {
-                return true;
-            }
-        }
-    }
-    return false;
-}
+bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask);
 
 /**
  * cpu_get_address_space:
cpu-common.c +15 -0
index 988d057d84..adb76b3a78 100644
@@ -388,6 +388,21 @@ void process_queued_cpu_work(CPUState *cpu)
     qemu_cond_broadcast(&qemu_work_cond);
 }
 
+/* Return true if PC matches an installed breakpoint.  */
+bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask)
+{
+    CPUBreakpoint *bp;
+
+    if (unlikely(!QTAILQ_EMPTY(&cpu->breakpoints))) {
+        QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
+            if (bp->pc == pc && (bp->flags & mask)) {
+                return true;
+            }
+        }
+    }
+    return false;
+}
+
 /* Add a breakpoint.  */
 int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
                           CPUBreakpoint **breakpoint)
Signature · 1 line
2.43.0
#7 lore raw

PATCH 4/17 exec/breakpoint: Move BP declarations from hw/core/cpu.h

In reply to Richard Henderson
---
 include/exec/breakpoint.h | 21 +++++++++++++++++++++
 include/hw/core/cpu.h     | 21 ---------------------
 2 files changed, 21 insertions(+), 21 deletions(-)
include/exec/breakpoint.h +21 -0
index 95f0482e6d..4013d9a07c 100644
@@ -12,6 +12,20 @@
 #include "exec/vaddr.h"
 #include "exec/memattrs.h"
 
+/* Breakpoint/watchpoint flags */
+#define BP_MEM_READ           0x01
+#define BP_MEM_WRITE          0x02
+#define BP_MEM_ACCESS         (BP_MEM_READ | BP_MEM_WRITE)
+#define BP_STOP_BEFORE_ACCESS 0x04
+/* 0x08 currently unused */
+#define BP_GDB                0x10
+#define BP_CPU                0x20
+#define BP_ANY                (BP_GDB | BP_CPU)
+#define BP_HIT_SHIFT          6
+#define BP_WATCHPOINT_HIT_READ  (BP_MEM_READ << BP_HIT_SHIFT)
+#define BP_WATCHPOINT_HIT_WRITE (BP_MEM_WRITE << BP_HIT_SHIFT)
+#define BP_WATCHPOINT_HIT       (BP_MEM_ACCESS << BP_HIT_SHIFT)
+
 typedef struct CPUBreakpoint {
     vaddr pc;
     int flags; /* BP_* */
@@ -27,4 +41,11 @@ typedef struct CPUWatchpoint {
     QTAILQ_ENTRY(CPUWatchpoint) entry;
 } CPUWatchpoint;
 
+int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
+                          CPUBreakpoint **breakpoint);
+int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags);
+void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint);
+void cpu_breakpoint_remove_all(CPUState *cpu, int mask);
+bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask);
+
 #endif
include/hw/core/cpu.h +0 -21
index b853824e47..e05e261883 100644
@@ -1138,27 +1138,6 @@ void qemu_init_vcpu(CPUState *cpu);
  */
 void cpu_single_step(CPUState *cpu, int enabled);
 
-/* Breakpoint/watchpoint flags */
-#define BP_MEM_READ           0x01
-#define BP_MEM_WRITE          0x02
-#define BP_MEM_ACCESS         (BP_MEM_READ | BP_MEM_WRITE)
-#define BP_STOP_BEFORE_ACCESS 0x04
-/* 0x08 currently unused */
-#define BP_GDB                0x10
-#define BP_CPU                0x20
-#define BP_ANY                (BP_GDB | BP_CPU)
-#define BP_HIT_SHIFT          6
-#define BP_WATCHPOINT_HIT_READ  (BP_MEM_READ << BP_HIT_SHIFT)
-#define BP_WATCHPOINT_HIT_WRITE (BP_MEM_WRITE << BP_HIT_SHIFT)
-#define BP_WATCHPOINT_HIT       (BP_MEM_ACCESS << BP_HIT_SHIFT)
-
-int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
-                          CPUBreakpoint **breakpoint);
-int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags);
-void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint);
-void cpu_breakpoint_remove_all(CPUState *cpu, int mask);
-bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask);
-
 /**
  * cpu_get_address_space:
  * @cpu: CPU to get address space from
Signature · 1 line
2.43.0
#8 lore raw

Re: [PATCH 04/17] exec/breakpoint: Move BP declarations from hw/core/cpu.h

Quoted 17 lines · Richard Henderson
---
  include/exec/breakpoint.h | 21 +++++++++++++++++++++
  include/hw/core/cpu.h     | 21 ---------------------
  2 files changed, 21 insertions(+), 21 deletions(-)
include/exec/breakpoint.h +3 -0
index 95f0482e6d..4013d9a07c 100644
@@ -12,6 +12,20 @@
  #include "exec/vaddr.h"
  #include "exec/memattrs.h"
 
+/* Breakpoint/watchpoint flags */
+#define BP_MEM_READ           0x01
+#define BP_MEM_WRITE          0x02

include/exec/breakpoint.h line 27

Quoted 10 lines
include/exec/breakpoint.h +10 -0
+#define BP_MEM_ACCESS         (BP_MEM_READ | BP_MEM_WRITE)
+#define BP_STOP_BEFORE_ACCESS 0x04
+/* 0x08 currently unused */
+#define BP_GDB                0x10
+#define BP_CPU                0x20
+#define BP_ANY                (BP_GDB | BP_CPU)
+#define BP_HIT_SHIFT          6
+#define BP_WATCHPOINT_HIT_READ  (BP_MEM_READ << BP_HIT_SHIFT)
+#define BP_WATCHPOINT_HIT_WRITE (BP_MEM_WRITE << BP_HIT_SHIFT)
+#define BP_WATCHPOINT_HIT       (BP_MEM_ACCESS << BP_HIT_SHIFT)

See https://lore.kernel.org/qemu-devel/[email protected]/ ;)

#9 lore raw

Re: [PATCH 04/17] exec/breakpoint: Move BP declarations from hw/core/cpu.h

Quoted 21 lines · Philippe Mathieu-Daudé
Quoted 20 lines · Richard Henderson

---
  include/exec/breakpoint.h | 21 +++++++++++++++++++++ include/hw/core/cpu.h     | 21 ---------------------

  2 files changed, 21 insertions(+), 21 deletions(-)
include/exec/breakpoint.h +7 -0
index 95f0482e6d..4013d9a07c 100644
@@ -12,6 +12,20 @@
   #include "exec/vaddr.h"
   #include "exec/memattrs.h"
+/* Breakpoint/watchpoint flags */
+#define BP_MEM_READ           0x01
+#define BP_MEM_WRITE          0x02
+#define BP_MEM_ACCESS         (BP_MEM_READ | BP_MEM_WRITE)
+#define BP_STOP_BEFORE_ACCESS 0x04
+/* 0x08 currently unused */
+#define BP_GDB                0x10
Quoted 8 lines

include/exec/breakpoint.h line 27

Quoted 6 lines
include/exec/breakpoint.h +6 -0
+#define BP_CPU                0x20
+#define BP_ANY                (BP_GDB | BP_CPU)
+#define BP_HIT_SHIFT          6
+#define BP_WATCHPOINT_HIT_READ  (BP_MEM_READ << BP_HIT_SHIFT)
+#define BP_WATCHPOINT_HIT_WRITE (BP_MEM_WRITE << BP_HIT_SHIFT)
+#define BP_WATCHPOINT_HIT       (BP_MEM_ACCESS << BP_HIT_SHIFT)

See https://lore.kernel.org/qemu-devel/[email protected]/ ;)

I noticed your series just after I posted mine. :-) I'll have a look through and probably rebase on yours.

r~

#10 lore raw

PATCH 5/17 qemu/typedefs: Declare CPUBreakpoint and CPUWatchpoint globally

In reply to Richard Henderson
---
 include/exec/breakpoint.h | 8 ++++----
 include/qemu/typedefs.h   | 2 ++
 2 files changed, 6 insertions(+), 4 deletions(-)
include/exec/breakpoint.h +4 -4
index 4013d9a07c..fc91343875 100644
@@ -26,20 +26,20 @@
 #define BP_WATCHPOINT_HIT_WRITE (BP_MEM_WRITE << BP_HIT_SHIFT)
 #define BP_WATCHPOINT_HIT       (BP_MEM_ACCESS << BP_HIT_SHIFT)
 
-typedef struct CPUBreakpoint {
+struct CPUBreakpoint {
     vaddr pc;
     int flags; /* BP_* */
     QTAILQ_ENTRY(CPUBreakpoint) entry;
-} CPUBreakpoint;
+};
 
-typedef struct CPUWatchpoint {
+struct CPUWatchpoint {
     vaddr vaddr;
     vaddr len;
     vaddr hitaddr;
     MemTxAttrs hitattrs;
     int flags; /* BP_* */
     QTAILQ_ENTRY(CPUWatchpoint) entry;
-} CPUWatchpoint;
+};
 
 int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
                           CPUBreakpoint **breakpoint);
include/qemu/typedefs.h +2 -0
index 2344c92182..919fe8ebff 100644
@@ -39,9 +39,11 @@ typedef struct Chardev Chardev;
 typedef struct Clock Clock;
 typedef struct ConfidentialGuestSupport ConfidentialGuestSupport;
 typedef struct CPUArchState CPUArchState;
+typedef struct CPUBreakpoint CPUBreakpoint;
 typedef struct CPUPluginState CPUPluginState;
 typedef struct CPUState CPUState;
 typedef struct CPUTLBEntryFull CPUTLBEntryFull;
+typedef struct CPUWatchpoint CPUWatchpoint;
 typedef struct DeviceState DeviceState;
 typedef struct DirtyBitmapSnapshot DirtyBitmapSnapshot;
 typedef struct DisasContextBase DisasContextBase;
Signature · 1 line
2.43.0
#11 lore raw

PATCH 6/17 *: Add missing include of exec/breakpoint.h

In reply to Richard Henderson
---
 accel/tcg/tcg-accel-ops.c           | 1 +
 cpu-common.c                        | 1 +
 gdbstub/system.c                    | 1 +
 gdbstub/user.c                      | 1 +
 system/watchpoint.c                 | 1 +
 target/i386/cpu.c                   | 1 +
 target/i386/kvm/kvm.c               | 1 +
 target/i386/machine.c               | 1 +
 target/i386/tcg/system/bpt_helper.c | 1 +
 target/ppc/cpu.c                    | 1 +
 target/xtensa/dbg_helper.c          | 1 +
 target/xtensa/helper.c              | 1 +
 12 files changed, 12 insertions(+)
accel/tcg/tcg-accel-ops.c +1 -0
index c179cd4ade..c153bb3355 100644
@@ -31,6 +31,7 @@
 #include "accel/tcg/cpu-loop.h"
 #include "system/tcg.h"
 #include "system/replay.h"
+#include "exec/breakpoint.h"
 #include "exec/icount.h"
 #include "qemu/main-loop.h"
 #include "qemu/guest-random.h"
cpu-common.c +1 -0
index adb76b3a78..11ab32d64d 100644
@@ -20,6 +20,7 @@
 #include "qemu/osdep.h"
 #include "qemu/main-loop.h"
 #include "exec/cpu-common.h"
+#include "exec/breakpoint.h"
 #include "hw/core/cpu.h"
 #include "qemu/lockable.h"
 #include "trace/trace-root.h"
gdbstub/system.c +1 -0
index 2063b63b2f..6ff24312bf 100644
@@ -18,6 +18,7 @@
 #include "gdbstub/syscalls.h"
 #include "gdbstub/commands.h"
 #include "exec/hwaddr.h"
+#include "exec/breakpoint.h"
 #include "accel/accel-ops.h"
 #include "accel/accel-cpu-ops.h"
 #include "system/address-spaces.h"
gdbstub/user.c +1 -0
index 97eb13e796..05fe18f906 100644
@@ -14,6 +14,7 @@
 #include "qemu/cutils.h"
 #include "qemu/sockets.h"
 #include "qapi/error.h"
+#include "exec/breakpoint.h"
 #include "exec/hwaddr.h"
 #include "exec/gdbstub.h"
 #include "gdbstub/commands.h"
system/watchpoint.c +1 -0
index 21d0bb36ca..4c2543a7c9 100644
@@ -21,6 +21,7 @@
 #include "qemu/error-report.h"
 #include "exec/cputlb.h"
 #include "exec/target_page.h"
+#include "exec/breakpoint.h"
 #include "exec/watchpoint.h"
 #include "hw/core/cpu.h"
 
target/i386/cpu.c +1 -0
index 5805d33ab9..422ca139af 100644
@@ -39,6 +39,7 @@
 #include "standard-headers/asm-x86/kvm_para.h"
 #include "hw/core/qdev-properties.h"
 #include "hw/i386/topology.h"
+#include "exec/breakpoint.h"
 #include "exec/watchpoint.h"
 #ifndef CONFIG_USER_ONLY
 #include "confidential-guest.h"
target/i386/kvm/kvm.c +1 -0
index 64cc421abe..739e21793d 100644
@@ -70,6 +70,7 @@
 #include "migration/blocker.h"
 #include "exec/memattrs.h"
 #include "exec/target_page.h"
+#include "exec/breakpoint.h"
 #include "trace.h"
 
 #include CONFIG_DEVICES
target/i386/machine.c +1 -0
index 1f5c517e56..2b9cf3878d 100644
@@ -7,6 +7,7 @@
 #include "hw/i386/x86.h"
 #include "kvm/kvm_i386.h"
 #include "hw/xen/xen.h"
+#include "exec/breakpoint.h"
 #include "exec/watchpoint.h"
 #include "system/kvm.h"
 #include "system/kvm_xen.h"
target/i386/tcg/system/bpt_helper.c +1 -0
index 2feea69c8a..3b1ecd8735 100644
@@ -21,6 +21,7 @@
 #include "accel/tcg/cpu-loop.h"
 #include "cpu.h"
 #include "exec/helper-proto.h"
+#include "exec/breakpoint.h"
 #include "exec/watchpoint.h"
 #include "tcg/helper-tcg.h"
 
target/ppc/cpu.c +1 -0
index 41edb18643..0bd6ebe39b 100644
@@ -22,6 +22,7 @@
 #include "cpu-models.h"
 #include "cpu-qom.h"
 #include "exec/log.h"
+#include "exec/breakpoint.h"
 #include "exec/watchpoint.h"
 #include "fpu/softfloat-helpers.h"
 #include "mmu-hash64.h"
target/xtensa/dbg_helper.c +1 -0
index 3d1143ad5e..160cdd798c 100644
@@ -30,6 +30,7 @@
 #include "cpu.h"
 #include "exec/helper-proto.h"
 #include "qemu/host-utils.h"
+#include "exec/breakpoint.h"
 #include "exec/watchpoint.h"
 #include "system/address-spaces.h"
 
target/xtensa/helper.c +1 -0
index 5a0e865514..0db5502287 100644
@@ -30,6 +30,7 @@
 #include "cpu.h"
 #include "exec/cputlb.h"
 #include "exec/target_page.h"
+#include "exec/breakpoint.h"
 #include "gdbstub/helpers.h"
 #include "exec/helper-proto.h"
 #include "accel/tcg/cpu-loop.h"
Signature · 1 line
2.43.0
#12 lore raw

PATCH 7/17 hw/core/cpu: Remove exec/breakpoint.h include

In reply to Richard Henderson

This is no longer required in the generic header.

---
 include/hw/core/cpu.h | 1 -
 1 file changed, 1 deletion(-)
include/hw/core/cpu.h +0 -1
index e05e261883..ae8daa1159 100644
@@ -22,7 +22,6 @@
 
 #include "hw/core/qdev.h"
 #include "disas/dis-asm.h"
-#include "exec/breakpoint.h"
 #include "exec/hwaddr.h"
 #include "exec/vaddr.h"
 #include "exec/memattrs.h"
Signature · 1 line
2.43.0
#13 lore raw

PATCH 8/17 exec/breakpoint: Introduce BP_CPU_ID_SHIFT

In reply to Richard Henderson

Allow the target to store an identifier with
breakpoints and watchpoints. Change "flags" to
use uint32_t instead of "int".

---
 include/exec/breakpoint.h | 10 ++++++----
 cpu-common.c              |  4 ++--
 2 files changed, 8 insertions(+), 6 deletions(-)
include/exec/breakpoint.h +6 -4
index fc91343875..93876f3304 100644
@@ -25,10 +25,12 @@
 #define BP_WATCHPOINT_HIT_READ  (BP_MEM_READ << BP_HIT_SHIFT)
 #define BP_WATCHPOINT_HIT_WRITE (BP_MEM_WRITE << BP_HIT_SHIFT)
 #define BP_WATCHPOINT_HIT       (BP_MEM_ACCESS << BP_HIT_SHIFT)
+/* Bits [31:16] are reserved as an identifier for BP_CPU. */
+#define BP_CPU_ID_SHIFT       16
 
 struct CPUBreakpoint {
     vaddr pc;
-    int flags; /* BP_* */
+    uint32_t flags; /* BP_* */
     QTAILQ_ENTRY(CPUBreakpoint) entry;
 };
 
@@ -37,13 +39,13 @@ struct CPUWatchpoint {
     vaddr len;
     vaddr hitaddr;
     MemTxAttrs hitattrs;
-    int flags; /* BP_* */
+    uint32_t flags; /* BP_* */
     QTAILQ_ENTRY(CPUWatchpoint) entry;
 };
 
-int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
+int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, uint32_t flags,
                           CPUBreakpoint **breakpoint);
-int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags);
+int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, uint32_t flags);
 void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint);
 void cpu_breakpoint_remove_all(CPUState *cpu, int mask);
 bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask);
cpu-common.c +2 -2
index 11ab32d64d..0b54e8a497 100644
@@ -405,7 +405,7 @@ bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask)
 }
 
 /* Add a breakpoint.  */
-int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
+int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, uint32_t flags,
                           CPUBreakpoint **breakpoint)
 {
     CPUBreakpoint *bp;
@@ -435,7 +435,7 @@ int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
 }
 
 /* Remove a specific breakpoint.  */
-int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags)
+int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, uint32_t flags)
 {
     CPUBreakpoint *bp;
 
Signature · 1 line
2.43.0
#14 lore raw

Re: [PATCH 08/17] exec/breakpoint: Introduce BP_CPU_ID_SHIFT

Quoted 29 lines · Richard Henderson

Allow the target to store an identifier with
breakpoints and watchpoints. Change "flags" to
use uint32_t instead of "int".

---
  include/exec/breakpoint.h | 10 ++++++----
  cpu-common.c              |  4 ++--
  2 files changed, 8 insertions(+), 6 deletions(-)
include/exec/breakpoint.h +3 -1
index fc91343875..93876f3304 100644
@@ -25,10 +25,12 @@
  #define BP_WATCHPOINT_HIT_READ  (BP_MEM_READ << BP_HIT_SHIFT)
  #define BP_WATCHPOINT_HIT_WRITE (BP_MEM_WRITE << BP_HIT_SHIFT)
  #define BP_WATCHPOINT_HIT       (BP_MEM_ACCESS << BP_HIT_SHIFT)
+/* Bits [31:16] are reserved as an identifier for BP_CPU. */
+#define BP_CPU_ID_SHIFT       16
 
  struct CPUBreakpoint {
      vaddr pc;
-    int flags; /* BP_* */
+    uint32_t flags; /* BP_* */
      QTAILQ_ENTRY(CPUBreakpoint) entry;
  };
 

include/exec/breakpoint.h struct CPUWatchpoint { · line 44

Quoted 7 lines
include/exec/breakpoint.h +1 -1
@@ -37,13 +39,13 @@ struct CPUWatchpoint {
      vaddr len;
      vaddr hitaddr;
      MemTxAttrs hitattrs;
-    int flags; /* BP_* */
+    uint32_t flags; /* BP_* */
      QTAILQ_ENTRY(CPUWatchpoint) entry;
  };

See https://lore.kernel.org/qemu-devel/[email protected]/ ;)

#15 lore raw

Re: [PATCH 08/17] exec/breakpoint: Introduce BP_CPU_ID_SHIFT

Hi Richard,

Quoted 11 lines · Philippe Mathieu-Daudé
Quoted 10 lines · Richard Henderson

Allow the target to store an identifier with
breakpoints and watchpoints.  Change "flags" to
use uint32_t instead of "int".

---
  include/exec/breakpoint.h | 10 ++++++----
  cpu-common.c              |  4 ++--

  2 files changed, 8 insertions(+), 6 deletions(-)
Quoted 10 lines

include/exec/breakpoint.h line 29

Quoted 10 lines
include/exec/breakpoint.h +2 -0
index fc91343875..93876f3304 100644
@@ -25,10 +25,12 @@
   #define BP_WATCHPOINT_HIT_READ  (BP_MEM_READ << BP_HIT_SHIFT)
   #define BP_WATCHPOINT_HIT_WRITE (BP_MEM_WRITE << BP_HIT_SHIFT)
   #define BP_WATCHPOINT_HIT       (BP_MEM_ACCESS << BP_HIT_SHIFT)
+/* Bits [31:16] are reserved as an identifier for BP_CPU. */
+#define BP_CPU_ID_SHIFT       16

While I understand the benefits of storing the ID in the structure, I'm not keen in having it packed with the BP flags, it hinders the code logic. Since there is no point in optimizing this, lets simply add an index field. And convert the API introducing _by_index() helpers. I suppose you plan to remove cpu_breakpoint_remove_by_ref() at the end.

Quoted 18 lines
Quoted 7 lines
include/exec/breakpoint.h +1 -1
   struct CPUBreakpoint {
       vaddr pc;
-    int flags; /* BP_* */
+    uint32_t flags; /* BP_* */
       QTAILQ_ENTRY(CPUBreakpoint) entry;
   };

include/exec/breakpoint.h struct CPUWatchpoint { · line 44

Quoted 7 lines
include/exec/breakpoint.h +1 -1
@@ -37,13 +39,13 @@ struct CPUWatchpoint {
       vaddr len;
       vaddr hitaddr;
       MemTxAttrs hitattrs;
-    int flags; /* BP_* */
+    uint32_t flags; /* BP_* */
       QTAILQ_ENTRY(CPUWatchpoint) entry;
   };

See https://lore.kernel.org/qemu-devel/20260630220100.1289-19- [email protected]/ ;)

#16 lore raw

Re: [PATCH 08/17] exec/breakpoint: Introduce BP_CPU_ID_SHIFT

Quoted 23 lines · Philippe Mathieu-Daudé

Hi Richard,

Quoted 20 lines · Philippe Mathieu-Daudé
Quoted 19 lines · Richard Henderson

Allow the target to store an identifier with
breakpoints and watchpoints.  Change "flags" to
use uint32_t instead of "int".

---
  include/exec/breakpoint.h | 10 ++++++----
  cpu-common.c              |  4 ++--

  2 files changed, 8 insertions(+), 6 deletions(-)
include/exec/breakpoint.h +1 -0
index fc91343875..93876f3304 100644
@@ -25,10 +25,12 @@
   #define BP_WATCHPOINT_HIT_READ  (BP_MEM_READ << BP_HIT_SHIFT)
   #define BP_WATCHPOINT_HIT_WRITE (BP_MEM_WRITE << BP_HIT_SHIFT)
   #define BP_WATCHPOINT_HIT       (BP_MEM_ACCESS << BP_HIT_SHIFT)
+/* Bits [31:16] are reserved as an identifier for BP_CPU. */
Quoted 8 lines
Quoted 1 line

include/exec/breakpoint.h line 29

Quoted 1 line
include/exec/breakpoint.h +1 -0
+#define BP_CPU_ID_SHIFT       16

While I understand the benefits of storing the ID in the structure, I'm not keen in having it packed with the BP flags, it hinders the code logic. Since there is no point in optimizing this, lets simply add an index field. And convert the API introducing _by_index() helpers. I suppose you plan to remove cpu_breakpoint_remove_by_ref() at the end.

At first I thought packing the id into the flags would work well with cpu_breakpoint_remove, and it's "bp->flags == flags" test. But it seems all backends use cpu_breakpoint_remove_by_ref and that continues to be easier to use.

So I might just add an index parameter to the "insert" functions and leave it at that, not bothering with any "_by_index" api.

r~

#17 lore raw

PATCH 9/17 target/arm: Split is_wp out of bp_wp_matches

In reply to Richard Henderson

This function is only called twice, once for each of breakpoint and watchpoint. Hoist the separate bp/wp code into the respective callers.

---
 target/arm/tcg/debug.c | 46 ++++++++++++++++--------------------------
 1 file changed, 17 insertions(+), 29 deletions(-)
target/arm/tcg/debug.c +17 -29
index ed94b20739..51d6748427 100644
@@ -252,41 +252,16 @@ static bool linked_bp_matches(ARMCPU *cpu, int lbn)
     return contextidr == (uint32_t)env->cp15.dbgbvr[lbn];
 }
 
-static bool bp_wp_matches(ARMCPU *cpu, int n, bool is_wp)
+static bool bp_wp_matches(ARMCPU *cpu, uint64_t cr, int access_el)
 {
     CPUARMState *env = &cpu->env;
-    uint64_t cr;
     int pac, hmc, ssc, wt, lbn;
     /*
      * Note that for watchpoints the check is against the CPU security
      * state, not the S/NS attribute on the offending data access.
      */
     bool is_secure = arm_is_secure(env);
-    int access_el = arm_current_el(env);
 
-    if (is_wp) {
-        CPUWatchpoint *wp = env->cpu_watchpoint[n];
-
-        if (!wp || !(wp->flags & BP_WATCHPOINT_HIT)) {
-            return false;
-        }
-        cr = env->cp15.dbgwcr[n];
-        if (wp->hitattrs.user) {
-            /*
-             * The LDRT/STRT/LDT/STT "unprivileged access" instructions should
-             * match watchpoints as if they were accesses done at EL0, even if
-             * the CPU is at EL1 or higher.
-             */
-            access_el = 0;
-        }
-    } else {
-        uint64_t pc = is_a64(env) ? env->pc : env->regs[15];
-
-        if (!env->cpu_breakpoint[n] || env->cpu_breakpoint[n]->pc != pc) {
-            return false;
-        }
-        cr = env->cp15.dbgbcr[n];
-    }
     /*
      * The WATCHPOINT_HIT flag guarantees us that the watchpoint is
      * enabled and that the address and access type match; for breakpoints
@@ -366,8 +341,19 @@ static bool check_watchpoints(ARMCPU *cpu)
     }
 
     for (n = 0; n < ARRAY_SIZE(env->cpu_watchpoint); n++) {
-        if (bp_wp_matches(cpu, n, true)) {
-            return true;
+        CPUWatchpoint *wp = env->cpu_watchpoint[n];
+
+        if (wp && (wp->flags & BP_WATCHPOINT_HIT)) {
+            /*
+             * The LDRT/STRT/LDT/STT "unprivileged access" instructions should
+             * match watchpoints as if they were accesses done at EL0, even if
+             * the CPU is at EL1 or higher.
+             */
+            int access_el = wp->hitattrs.user ? 0 : arm_current_el(env);
+
+            if (bp_wp_matches(cpu, env->cp15.dbgwcr[n], access_el)) {
+                return true;
+            }
         }
     }
     return false;
@@ -412,7 +398,9 @@ bool arm_debug_check_breakpoint(CPUState *cs, const CPUBreakpoint *bp)
      */
 
     for (n = 0; n < ARRAY_SIZE(env->cpu_breakpoint); n++) {
-        if (bp_wp_matches(cpu, n, false)) {
+        if (env->cpu_breakpoint[n] &&
+            env->cpu_breakpoint[n]->pc != pc &&
+            bp_wp_matches(cpu, env->cp15.dbgbcr[n], arm_current_el(env))) {
             return true;
         }
     }
Signature · 1 line
2.43.0
#18 lore raw

PATCH 10/17 target/arm: Set ID for watchpoints and breakpoints

In reply to Richard Henderson
---
 target/arm/tcg/debug.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/arm/tcg/debug.c +2 -2
index 51d6748427..8bcce4a1b0 100644
@@ -538,7 +538,7 @@ void hw_watchpoint_update(ARMCPU *cpu, int n)
     vaddr wvr = env->cp15.dbgwvr[n];
     uint64_t wcr = env->cp15.dbgwcr[n];
     int mask;
-    int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
+    int flags = BP_CPU | BP_STOP_BEFORE_ACCESS | (n << BP_CPU_ID_SHIFT);
 
     if (env->cpu_watchpoint[n]) {
         cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
@@ -644,7 +644,7 @@ void hw_breakpoint_update(ARMCPU *cpu, int n)
     uint64_t bcr = env->cp15.dbgbcr[n];
     vaddr addr;
     int bt;
-    int flags = BP_CPU;
+    int flags = BP_CPU | (n << BP_CPU_ID_SHIFT);
 
     if (env->cpu_breakpoint[n]) {
         cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
Signature · 1 line
2.43.0
#19 lore raw

PATCH 11/17 target/arm: Use bp argument in arm_debug_check_breakpoint

In reply to Richard Henderson

Use the matched breakpoint instead of looping over all.

---
 target/arm/tcg/debug.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
target/arm/tcg/debug.c +2 -9
index 8bcce4a1b0..fc63d35d98 100644
@@ -396,15 +396,8 @@ bool arm_debug_check_breakpoint(CPUState *cs, const CPUBreakpoint *bp)
      * TODO: We would need to look up the page for PC and verify that
      * it is present and executable.
      */
-
-    for (n = 0; n < ARRAY_SIZE(env->cpu_breakpoint); n++) {
-        if (env->cpu_breakpoint[n] &&
-            env->cpu_breakpoint[n]->pc != pc &&
-            bp_wp_matches(cpu, env->cp15.dbgbcr[n], arm_current_el(env))) {
-            return true;
-        }
-    }
-    return false;
+    n = bp->flags >> BP_CPU_ID_SHIFT;
+    return bp_wp_matches(cpu, env->cp15.dbgbcr[n], arm_current_el(env));
 }
 
 bool arm_debug_check_watchpoint(CPUState *cs, const CPUWatchpoint *wp)
Signature · 1 line
2.43.0
#20 lore raw

PATCH 12/17 target/arm: Use wp argument in arm_debug_check_watchpoint

In reply to Richard Henderson

Merge check_watchpoints and drop the loop over all watchpoints.

---
 target/arm/tcg/debug.c | 43 +++++++++---------------------------------
 1 file changed, 9 insertions(+), 34 deletions(-)
target/arm/tcg/debug.c +9 -34
index fc63d35d98..e87020de90 100644
@@ -326,39 +326,6 @@ static bool bp_wp_matches(ARMCPU *cpu, uint64_t cr, int access_el)
     return true;
 }
 
-static bool check_watchpoints(ARMCPU *cpu)
-{
-    CPUARMState *env = &cpu->env;
-    int n;
-
-    /*
-     * If watchpoints are disabled globally or we can't take debug
-     * exceptions here then watchpoint firings are ignored.
-     */
-    if (extract32(env->cp15.mdscr_el1, 15, 1) == 0
-        || !arm_generate_debug_exceptions(env)) {
-        return false;
-    }
-
-    for (n = 0; n < ARRAY_SIZE(env->cpu_watchpoint); n++) {
-        CPUWatchpoint *wp = env->cpu_watchpoint[n];
-
-        if (wp && (wp->flags & BP_WATCHPOINT_HIT)) {
-            /*
-             * The LDRT/STRT/LDT/STT "unprivileged access" instructions should
-             * match watchpoints as if they were accesses done at EL0, even if
-             * the CPU is at EL1 or higher.
-             */
-            int access_el = wp->hitattrs.user ? 0 : arm_current_el(env);
-
-            if (bp_wp_matches(cpu, env->cp15.dbgwcr[n], access_el)) {
-                return true;
-            }
-        }
-    }
-    return false;
-}
-
 bool arm_debug_check_breakpoint(CPUState *cs, const CPUBreakpoint *bp)
 {
     ARMCPU *cpu = ARM_CPU(cs);
@@ -407,8 +374,16 @@ bool arm_debug_check_watchpoint(CPUState *cs, const CPUWatchpoint *wp)
      * is also an architectural watchpoint match.
      */
     ARMCPU *cpu = ARM_CPU(cs);
+    CPUARMState *env = &cpu->env;
+    int n = wp->flags >> BP_CPU_ID_SHIFT;
+    /*
+     * The LDRT/STRT/LDT/STT "unprivileged access" instructions should
+     * match watchpoints as if they were accesses done at EL0, even if
+     * the CPU is at EL1 or higher.
+     */
+    int access_el = wp->hitattrs.user ? 0 : arm_current_el(env);
 
-    return check_watchpoints(cpu);
+    return bp_wp_matches(cpu, env->cp15.dbgwcr[n], access_el);
 }
 
 /*
Signature · 1 line
2.43.0
#21 lore raw

PATCH 13/17 target/riscv: Set ID for watchpoints and breakpoints

In reply to Richard Henderson
---
 target/riscv/debug.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/riscv/debug.c +2 -2
index ad9df09cf7..5f76dd4b7a 100644
@@ -479,7 +479,7 @@ static void type2_breakpoint_insert(CPURISCVState *env, target_ulong index)
     target_ulong addr = env->tdata2[index];
     bool enabled = type2_breakpoint_enabled(ctrl);
     CPUState *cs = env_cpu(env);
-    int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
+    int flags = BP_CPU | BP_STOP_BEFORE_ACCESS | (index << BP_CPU_ID_SHIFT);
     uint32_t size, def_size;
 
     if (!enabled) {
@@ -604,7 +604,7 @@ static void type6_breakpoint_insert(CPURISCVState *env, target_ulong index)
     target_ulong addr = env->tdata2[index];
     bool enabled = type6_breakpoint_enabled(ctrl);
     CPUState *cs = env_cpu(env);
-    int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
+    int flags = BP_CPU | BP_STOP_BEFORE_ACCESS | (index << BP_CPU_ID_SHIFT);
     uint32_t size;
 
     if (!enabled) {
Signature · 1 line
2.43.0
#22 lore raw

PATCH 14/17 target/riscv: Use bp argument in riscv_cpu_debug_check_breakpoint

In reply to Richard Henderson

Use the ID of the breakpoint instead of a loop over all.

---
 target/riscv/debug.c | 34 ++++++++++------------------------
 1 file changed, 10 insertions(+), 24 deletions(-)
target/riscv/debug.c +10 -24
index 5f76dd4b7a..f6ffc62f1a 100644
@@ -943,41 +943,27 @@ bool riscv_cpu_debug_check_breakpoint(CPUState *cs, const CPUBreakpoint *bp)
 {
     RISCVCPU *cpu = RISCV_CPU(cs);
     CPURISCVState *env = &cpu->env;
-    target_ulong ctrl;
-    target_ulong pc;
-    int trigger_type;
-    int i;
+    int i = bp->flags >> BP_CPU_ID_SHIFT;
+    trigger_type_t trigger_type = get_trigger_type(env, i);
 
-    for (i = 0; i < RV_MAX_TRIGGERS; i++) {
-        trigger_type = get_trigger_type(env, i);
-
-        if (!trigger_common_match(env, trigger_type, i)) {
-            continue;
-        }
+    if (trigger_common_match(env, trigger_type, i)) {
+        target_ulong ctrl = 0;
 
         switch (trigger_type) {
         case TRIGGER_TYPE_AD_MATCH:
-            ctrl = env->tdata1[i];
-            pc = env->tdata2[i];
-
-            if ((ctrl & TYPE2_EXEC) && (bp->pc == pc)) {
-                env->badaddr = pc;
-                return true;
-            }
+            ctrl = env->tdata1[i] & TYPE2_EXEC;
             break;
         case TRIGGER_TYPE_AD_MATCH6:
-            ctrl = env->tdata1[i];
-            pc = env->tdata2[i];
-
-            if ((ctrl & TYPE6_EXEC) && (bp->pc == pc)) {
-                env->badaddr = pc;
-                return true;
-            }
+            ctrl = env->tdata1[i] & TYPE6_EXEC;
             break;
         default:
             /* other trigger types are not supported or irrelevant */
             break;
         }
+        if (ctrl) {
+            env->badaddr = env->tdata2[i];
+            return true;
+        }
     }
 
     return false;
Signature · 1 line
2.43.0
#23 lore raw

PATCH 15/17 target/riscv: Use bp argument in riscv_cpu_debug_check_watchpoint

In reply to Richard Henderson

Use the ID of the watchpoint instead of a loop over all.

This fixes a bug in which addr was compared against wp->vaddr, so that we never recognized watchpoints that didn't trigger at the start of the region.

---
 target/riscv/debug.c | 55 +++-----------------------------------------
 1 file changed, 3 insertions(+), 52 deletions(-)
target/riscv/debug.c +3 -52
index f6ffc62f1a..433050bd94 100644
@@ -973,59 +973,10 @@ bool riscv_cpu_debug_check_watchpoint(CPUState *cs, const CPUWatchpoint *wp)
 {
     RISCVCPU *cpu = RISCV_CPU(cs);
     CPURISCVState *env = &cpu->env;
-    target_ulong ctrl;
-    target_ulong addr;
-    int trigger_type;
-    int flags;
-    int i;
+    int i = wp->flags >> BP_CPU_ID_SHIFT;
+    trigger_type_t trigger_type = get_trigger_type(env, i);
 
-    for (i = 0; i < RV_MAX_TRIGGERS; i++) {
-        trigger_type = get_trigger_type(env, i);
-
-        if (!trigger_common_match(env, trigger_type, i)) {
-            continue;
-        }
-
-        switch (trigger_type) {
-        case TRIGGER_TYPE_AD_MATCH:
-            ctrl = env->tdata1[i];
-            addr = env->tdata2[i];
-            flags = 0;
-
-            if (ctrl & TYPE2_LOAD) {
-                flags |= BP_MEM_READ;
-            }
-            if (ctrl & TYPE2_STORE) {
-                flags |= BP_MEM_WRITE;
-            }
-
-            if ((wp->flags & flags) && (wp->vaddr == addr)) {
-                return true;
-            }
-            break;
-        case TRIGGER_TYPE_AD_MATCH6:
-            ctrl = env->tdata1[i];
-            addr = env->tdata2[i];
-            flags = 0;
-
-            if (ctrl & TYPE6_LOAD) {
-                flags |= BP_MEM_READ;
-            }
-            if (ctrl & TYPE6_STORE) {
-                flags |= BP_MEM_WRITE;
-            }
-
-            if ((wp->flags & flags) && (wp->vaddr == addr)) {
-                return true;
-            }
-            break;
-        default:
-            /* other trigger types are not supported */
-            break;
-        }
-    }
-
-    return false;
+    return trigger_common_match(env, trigger_type, i);
 }
 
 void riscv_trigger_realize(CPURISCVState *env)
Signature · 1 line
2.43.0
#24 lore raw
In reply to Richard Henderson
Quoted 5 lines · Richard Henderson

Follow-up to [email protected] ("[RFC PATCH 0/2] change cpu->breakpoints to a GTree")

Beyond using interval-tree for breakpoints:

Quoted 8 lines

- Pass the matching breakpoint to the target debug hook,
- Allow the target to record an id/index for each BP or WP so that the target doesn't have to search for which item triggered, so that the target doesn't have to search again.
- Convert both BP and WP to IntervalTree.

r~

Bah. I was mid-rebase for this, and it's missing the CPUWatchpoint work. I'll send a v2 tommorrow.

r~

Quoted 50 lines

Richard Henderson (17):
accel/tcg: Pass CPUBreakpoint to debug_check_breakpoint accel/tcg: Constify CPUWatchpoint for debug_check_watchpoint
cpu: Move cpu_breakpoint_test out of line exec/breakpoint: Move BP declarations from hw/core/cpu.h qemu/typedefs: Declare CPUBreakpoint and CPUWatchpoint globally *: Add missing include of exec/breakpoint.h hw/core/cpu: Remove exec/breakpoint.h include exec/breakpoint: Introduce BP_CPU_ID_SHIFT
target/arm: Split is_wp out of bp_wp_matches target/arm: Set ID for watchpoints and breakpoints target/arm: Use bp argument in arm_debug_check_breakpoint target/arm: Use wp argument in arm_debug_check_watchpoint target/riscv: Set ID for watchpoints and breakpoints target/riscv: Use bp argument in riscv_cpu_debug_check_breakpoint target/riscv: Use bp argument in riscv_cpu_debug_check_watchpoint target/xtensa: Use BP_CPU_ID_SHIFT
cpu: Use interval-tree for CPUBreakpoint

  include/accel/tcg/cpu-ops.h         |   4 +-
  include/exec/breakpoint.h           |  39 ++++++++--
  include/hw/core/cpu.h               |  39 +---------
  include/qemu/typedefs.h             |   2 +
  target/arm/internals.h              |   4 +-
  target/ppc/internal.h               |   4 +-
  target/riscv/debug.h                |   4 +-
  target/xtensa/cpu.h                 |   2 +-
  accel/tcg/cpu-exec.c                |  48 ++++++------
  accel/tcg/tcg-accel-ops.c           |   1 +
  cpu-common.c                        |  54 ++++++++-----
  gdbstub/system.c                    |   1 +
  gdbstub/user.c                      |   1 +
  hw/core/cpu-common.c                |   1 -
  linux-user/main.c                   |  13 +++-
  system/watchpoint.c                 |   1 +
  target/arm/tcg/debug.c              |  76 ++++--------------
  target/i386/cpu.c                   |   1 +
  target/i386/kvm/kvm.c               |   1 +
  target/i386/machine.c               |   1 +
  target/i386/tcg/system/bpt_helper.c |   1 +
  target/i386/tcg/tcg-cpu.c           |   2 +-
  target/ppc/cpu.c                    |   1 +
  target/ppc/tcg-excp_helper.c        |   4 +-
  target/riscv/debug.c                | 116 ++++++----------------------
  target/xtensa/dbg_helper.c          |  19 ++---
  target/xtensa/helper.c              |   1 +
  27 files changed, 174 insertions(+), 267 deletions(-)
#25 lore raw

PATCH 16/17 target/xtensa: Use BP_CPU_ID_SHIFT

In reply to Richard Henderson

Set the id when installing the breakpoint, and drop the loop when checking the breakpoint.

---
 target/xtensa/dbg_helper.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
target/xtensa/dbg_helper.c +6 -10
index 160cdd798c..5b957474ea 100644
@@ -44,7 +44,8 @@ void HELPER(wsr_ibreakenable)(CPUXtensaState *env, uint32_t v)
         if (change & (1 << i)) {
             if (v & (1 << i)) {
                 cpu_breakpoint_insert(cs, env->sregs[IBREAKA + i],
-                                      BP_CPU, &env->cpu_breakpoint[i]);
+                                      BP_CPU | (i << BP_CPU_ID_SHIFT),
+                                      &env->cpu_breakpoint[i]);
             } else {
                 cpu_breakpoint_remove_by_ref(cs, env->cpu_breakpoint[i]);
                 env->cpu_breakpoint[i] = NULL;
@@ -60,7 +61,8 @@ void HELPER(wsr_ibreaka)(CPUXtensaState *env, uint32_t i, uint32_t v)
         CPUState *cs = env_cpu(env);
 
         cpu_breakpoint_remove_by_ref(cs, env->cpu_breakpoint[i]);
-        cpu_breakpoint_insert(cs, v, BP_CPU, &env->cpu_breakpoint[i]);
+        cpu_breakpoint_insert(cs, v, BP_CPU | (i << BP_CPU_ID_SHIFT),
+                              &env->cpu_breakpoint[i]);
     }
     env->sregs[IBREAKA + i] = v;
 }
@@ -68,18 +70,12 @@ void HELPER(wsr_ibreaka)(CPUXtensaState *env, uint32_t i, uint32_t v)
 bool xtensa_debug_check_breakpoint(CPUState *cs, const CPUBreakpoint *bp)
 {
     CPUXtensaState *env = cpu_env(cs);
-    unsigned int i;
+    unsigned int i = bp->flags >> BP_CPU_ID_SHIFT;
 
     if (xtensa_get_cintlevel(env) >= env->config->debug_level) {
         return false;
     }
-    for (i = 0; i < env->config->nibreak; ++i) {
-        if (env->sregs[IBREAKENABLE] & (1 << i) &&
-            env->sregs[IBREAKA + i] == env->pc) {
-            return true;
-        }
-    }
-    return false;
+    return (env->sregs[IBREAKENABLE] >> i) & 1;
 }
 
 static void set_dbreak(CPUXtensaState *env, unsigned i, uint32_t dbreaka,
Signature · 1 line
2.43.0
#26 lore raw

PATCH 17/17 cpu: Use interval-tree for CPUBreakpoint

In reply to Richard Henderson

Use a balanced binary tree rather than a simple list for breakpoints. Using an interval tree makes it easy to probe for all breakpoints on a virtual page.

---
 include/exec/breakpoint.h |  6 ++---
 include/hw/core/cpu.h     |  3 ++-
 accel/tcg/cpu-exec.c      | 48 +++++++++++++++++++--------------------
 cpu-common.c              | 46 +++++++++++++++++++------------------
 hw/core/cpu-common.c      |  1 -
 linux-user/main.c         | 13 +++++++----
 6 files changed, 61 insertions(+), 56 deletions(-)
include/exec/breakpoint.h +3 -3
index 93876f3304..d0b5669fa1 100644
@@ -8,6 +8,7 @@
 #ifndef EXEC_BREAKPOINT_H
 #define EXEC_BREAKPOINT_H
 
+#include "qemu/interval-tree.h"
 #include "qemu/queue.h"
 #include "exec/vaddr.h"
 #include "exec/memattrs.h"
@@ -29,9 +30,8 @@
 #define BP_CPU_ID_SHIFT       16
 
 struct CPUBreakpoint {
-    vaddr pc;
-    uint32_t flags; /* BP_* */
-    QTAILQ_ENTRY(CPUBreakpoint) entry;
+    IntervalTreeNode itree;     /* start == last == pc */
+    uint32_t flags;             /* BP_* */
 };
 
 struct CPUWatchpoint {
include/hw/core/cpu.h +2 -1
index ae8daa1159..b0cc1b8fa7 100644
@@ -30,6 +30,7 @@
 #include "qapi/qapi-types-machine.h"
 #include "qapi/qapi-types-run-state.h"
 #include "qemu/bitmap.h"
+#include "qemu/interval-tree.h"
 #include "qemu/rcu_queue.h"
 #include "qemu/queue.h"
 #include "qemu/lockcnt.h"
@@ -525,7 +526,7 @@ struct CPUState {
     QTAILQ_ENTRY(CPUState) node;
 
     /* ice debug support */
-    QTAILQ_HEAD(, CPUBreakpoint) breakpoints;
+    IntervalTreeRoot breakpoints;
 
     QTAILQ_HEAD(, CPUWatchpoint) watchpoints;
     CPUWatchpoint *watchpoint_hit;
accel/tcg/cpu-exec.c +23 -25

77 lines not on this page · raw message

cpu-common.c +24 -22

90 lines not on this page · raw message

hw/core/cpu-common.c +0 -1
index e314f916f8..5c2d7ed42c 100644
@@ -322,7 +322,6 @@ static void cpu_common_initfn(Object *obj)
     qemu_mutex_init(&cpu->work_mutex);
     qemu_lockcnt_init(&cpu->in_ioctl_lock);
     QSIMPLEQ_INIT(&cpu->work_list);
-    QTAILQ_INIT(&cpu->breakpoints);
     QTAILQ_INIT(&cpu->watchpoints);
 
     cpu_exec_initfn(cpu);
linux-user/main.c +9 -4
index c08c73fd80..dd7f267d46 100644
@@ -239,7 +239,6 @@ CPUArchState *cpu_copy(CPUArchState *env)
     CPUState *cpu = env_cpu(env);
     CPUState *new_cpu = cpu_create(cpu_type);
     CPUArchState *new_env = cpu_env(new_cpu);
-    CPUBreakpoint *bp;
 
     /* Reset non arch specific state */
     cpu_reset(new_cpu);
@@ -258,9 +257,15 @@ CPUArchState *cpu_copy(CPUArchState *env)
     /* Clone all break/watchpoints.
        Note: Once we support ptrace with hw-debug register access, make sure
        BP_CPU break/watchpoints are handled correctly on clone. */
-    QTAILQ_INIT(&new_cpu->breakpoints);
-    QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
-        cpu_breakpoint_insert(new_cpu, bp->pc, bp->flags, NULL);
+    if (!interval_tree_is_empty(&cpu->breakpoints)) {
+        IntervalTreeNode *n;
+
+        memset(&new_cpu->breakpoints, 0, sizeof(new_cpu->breakpoints));
+        for (n = interval_tree_iter_first(&cpu->breakpoints, 0, -1); n;
+             n = interval_tree_iter_next(n, 0, -1)) {
+            CPUBreakpoint *bp = container_of(n, CPUBreakpoint, itree);
+            cpu_breakpoint_insert(new_cpu, bp->itree.start, bp->flags, NULL);
+        }
     }
 
     return new_env;
Signature · 1 line
2.43.0
#27 lore raw

Re: [PATCH 17/17] cpu: Use interval-tree for CPUBreakpoint

Quoted 5 lines · Richard Henderson

-    if (match_page) {
+    n = interval_tree_iter_first(&cpu->breakpoints, +                                 pc & TARGET_PAGE_MASK, +                                 pc | ~TARGET_PAGE_MASK); +    if (n) {

This logic seems great. I tested this patch series, on the ppc kernel breakpoints usecase. It seems to perform slightly better than GTree implementation.

Maybe this can open the way to have break-range support from gdb?

Thanks
~Shivang