@samitouri / QOSamiQemu / commits / 0a8bc0f251

cpu: Introduce cpu_single_stepping() helper

Access CPUState::@singlestep_enabled field with a helper. Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com> Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Message-ID: <20260705215729.62196-31-philmd@oss.qualcomm.com>

Philippe Mathieu-Daudé committed Jun 26, 2026 at 14:18 UTC 0a8bc0f25151d8d83e193bffc4811b4631460fc5
15 files changed +33 -22
accel/kvm/kvm-all.c
+1 -1
@@ -3812,7 +3812,7 @@ int kvm_update_guest_debug(CPUState *cpu, unsigned long reinject_trap)
3812
3813 data.dbg.control = reinject_trap;
3814
3815 - if (cpu->singlestep_enabled) {
3815 + if (cpu_single_stepping(cpu)) {
3816 data.dbg.control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP;
3817
3818 if (cpu->singlestep_enabled & SSTEP_NOIRQ) {
accel/tcg/cpu-exec-common.c
+1 -1
@@ -47,7 +47,7 @@ uint32_t curr_cflags(CPUState *cpu)
47 * For singlestep and -d nochain, suppress goto_tb so that
48 * we can log -d cpu,exec after every TB.
49 */
50 - if (unlikely(cpu->singlestep_enabled)) {
50 + if (unlikely(cpu_single_stepping(cpu))) {
51 cflags |= CF_NO_GOTO_TB | CF_NO_GOTO_PTR | CF_SINGLE_STEP | 1;
52 } else if (qatomic_read(&one_insn_per_tb)) {
53 cflags |= CF_NO_GOTO_TB | 1;
accel/tcg/cpu-exec.c
+4 -4
@@ -308,7 +308,7 @@ static bool check_for_breakpoints_slow(CPUState *cpu, vaddr pc,
308 * so that one could (gdb) singlestep into the guest kernel's
309 * architectural breakpoint handler.
310 */
311 - if (cpu->singlestep_enabled) {
311 + if (cpu_single_stepping(cpu)) {
312 return false;
313 }
314
@@ -485,7 +485,7 @@ cpu_tb_exec(CPUState *cpu, TranslationBlock *itb, int *tb_exit)
485 * raise a debug exception. Single-step with another exception
486 * is handled in cpu_handle_exception.
487 */
488 - if (unlikely(cpu->singlestep_enabled) && cpu->exception_index == -1) {
488 + if (unlikely(cpu_single_stepping(cpu)) && cpu->exception_index == -1) {
489 cpu->exception_index = EXCP_DEBUG;
490 cpu_loop_exit(cpu);
491 }
@@ -732,7 +732,7 @@ static inline bool cpu_handle_exception(CPUState *cpu, int *ret)
732 bql_unlock();
733 cpu->exception_index = -1;
734
735 - if (unlikely(cpu->singlestep_enabled)) {
735 + if (unlikely(cpu_single_stepping(cpu))) {
736 /*
737 * After processing the exception, ensure an EXCP_DEBUG is
738 * raised when single-stepping so that GDB doesn't miss the
@@ -849,7 +849,7 @@ static inline bool cpu_handle_interrupt(CPUState *cpu,
849 * raised when single-stepping so that GDB doesn't miss the
850 * next instruction.
851 */
852 - if (unlikely(cpu->singlestep_enabled)) {
852 + if (unlikely(cpu_single_stepping(cpu))) {
853 cpu->exception_index = EXCP_DEBUG;
854 bql_unlock();
855 return true;
include/hw/core/cpu.h
+11
@@ -1138,6 +1138,17 @@ void qemu_init_vcpu(CPUState *cpu);
1138 */
1139 void cpu_single_step(CPUState *cpu, int enabled);
1140
1141 +/**
1142 + * cpu_single_stepping:
1143 + * @cpu: The vCPU to check
1144 + *
1145 + * Returns whether the vCPU has single-stepping enabled.
1146 + */
1147 +static inline bool cpu_single_stepping(const CPUState *cpu)
1148 +{
1149 + return cpu->singlestep_enabled;
1150 +}
1151 +
1152 int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
1153 CPUBreakpoint **breakpoint);
1154 int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags);
linux-user/riscv/cpu_loop.c
+1 -1
@@ -68,7 +68,7 @@ void cpu_loop(CPURISCVState *env)
68 } else if (ret != -QEMU_ESIGRETURN && ret != -QEMU_ESETPC) {
69 env->gpr[xA0] = ret;
70 }
71 - if (cs->singlestep_enabled) {
71 + if (cpu_single_stepping(cs)) {
72 goto gdbstep;
73 }
74 break;
linux-user/s390x/cpu_loop.c
+1 -1
@@ -87,7 +87,7 @@ void cpu_loop(CPUS390XState *env)
87 env->regs[2] = ret;
88 }
89
90 - if (unlikely(cs->singlestep_enabled)) {
90 + if (unlikely(cpu_single_stepping(cs))) {
91 /*
92 * cpu_tb_exec() did not raise EXCP_DEBUG, because it has seen
93 * that EXCP_SVC was already pending.
system/cpus.c
+1 -1
@@ -344,7 +344,7 @@ bool cpu_can_run(CPUState *cpu)
344 void cpu_handle_guest_debug(CPUState *cpu)
345 {
346 if (replay_running_debug()) {
347 - if (!cpu->singlestep_enabled) {
347 + if (!cpu_single_stepping(cpu)) {
348 /*
349 * Report about the breakpoint and
350 * make a single step to skip it
target/arm/hvf/hvf.c
+4 -4
@@ -2340,7 +2340,7 @@ static int hvf_handle_exception(CPUState *cpu, hv_vcpu_exit_exception_t *excp)
2340 case EC_SOFTWARESTEP: {
2341 ret = EXCP_DEBUG;
2342
2343 - if (!cpu->singlestep_enabled) {
2343 + if (!cpu_single_stepping(cpu)) {
2344 error_report("EC_SOFTWARESTEP but single-stepping not enabled");
2345 }
2346 break;
@@ -2549,7 +2549,7 @@ static int hvf_handle_exception(CPUState *cpu, hv_vcpu_exit_exception_t *excp)
2549 assert_hvf_ok(r);
2550
2551 /* Handle single-stepping over instructions which trigger a VM exit */
2552 - if (cpu->singlestep_enabled) {
2552 + if (cpu_single_stepping(cpu)) {
2553 ret = EXCP_DEBUG;
2554 }
2555 }
@@ -2868,7 +2868,7 @@ void hvf_arch_update_guest_debug(CPUState *cpu)
2868 CPUARMState *env = &arm_cpu->env;
2869
2870 /* Check whether guest debugging is enabled */
2871 - cpu->accel->guest_debug_enabled = cpu->singlestep_enabled ||
2871 + cpu->accel->guest_debug_enabled = cpu_single_stepping(cpu) ||
2872 hvf_sw_breakpoints_active(cpu) ||
2873 hvf_arm_hw_debug_active(cpu);
2874
@@ -2882,7 +2882,7 @@ void hvf_arch_update_guest_debug(CPUState *cpu)
2882 cpu_synchronize_state(cpu);
2883
2884 /* Enable/disable single-stepping */
2885 - if (cpu->singlestep_enabled) {
2885 + if (cpu_single_stepping(cpu)) {
2886 env->cp15.mdscr_el1 =
2887 deposit64(env->cp15.mdscr_el1, MDSCR_EL1_SS_SHIFT, 1, 1);
2888 pstate_write(env, pstate_read(env) | PSTATE_SS);
target/arm/kvm.c
+1 -1
@@ -1492,7 +1492,7 @@ static bool kvm_arm_handle_debug(ARMCPU *cpu,
1492
1493 switch (hsr_ec) {
1494 case EC_SOFTWARESTEP:
1495 - if (cs->singlestep_enabled) {
1495 + if (cpu_single_stepping(cs)) {
1496 return true;
1497 } else {
1498 /*
target/i386/kvm/kvm.c
+1 -1
@@ -6250,7 +6250,7 @@ static int kvm_handle_debug(X86CPU *cpu,
6250
6251 if (arch_info->exception == EXCP01_DB) {
6252 if (arch_info->dr6 & DR6_BS) {
6253 - if (cs->singlestep_enabled) {
6253 + if (cpu_single_stepping(cs)) {
6254 ret = EXCP_DEBUG;
6255 }
6256 } else {
target/i386/whpx/whpx-all.c
+3 -3
@@ -2266,7 +2266,7 @@ int whpx_vcpu_run(CPUState *cpu)
2266 }
2267 }
2268
2269 - if (exclusive_step_mode != WHPX_STEP_NONE || cpu->singlestep_enabled) {
2269 + if (exclusive_step_mode != WHPX_STEP_NONE || cpu_single_stepping(cpu)) {
2270 whpx_vcpu_configure_single_stepping(cpu, true, NULL);
2271 }
2272
@@ -2283,7 +2283,7 @@ int whpx_vcpu_run(CPUState *cpu)
2283 break;
2284 }
2285
2286 - if (exclusive_step_mode != WHPX_STEP_NONE || cpu->singlestep_enabled) {
2286 + if (exclusive_step_mode != WHPX_STEP_NONE || cpu_single_stepping(cpu)) {
2287 whpx_vcpu_configure_single_stepping(cpu,
2288 false,
2289 &vcpu->exit_ctx.VpContext.Rflags);
@@ -2648,7 +2648,7 @@ int whpx_vcpu_run(CPUState *cpu)
2648 cpu->exception_index = EXCP_DEBUG;
2649 } else if ((vcpu->exit_ctx.VpException.ExceptionType ==
2650 WHvX64ExceptionTypeDebugTrapOrFault) &&
2651 - !cpu->singlestep_enabled) {
2651 + !cpu_single_stepping(cpu)) {
2652 /*
2653 * Just finished stepping over a breakpoint, but the
2654 * gdb does not expect us to do single-stepping.
target/loongarch/kvm/kvm.c
+1 -1
@@ -1440,7 +1440,7 @@ static bool kvm_loongarch_handle_debug(CPUState *cs, struct kvm_run *run)
1440 CPULoongArchState *env = &cpu->env;
1441
1442 kvm_cpu_synchronize_state(cs);
1443 - if (cs->singlestep_enabled) {
1443 + if (cpu_single_stepping(cs)) {
1444 return true;
1445 }
1446
target/microblaze/translate.c
+1 -1
@@ -1772,7 +1772,7 @@ static void mb_tr_tb_stop(DisasContextBase *dcb, CPUState *cs)
1772 }
1773
1774 /* Finish DISAS_EXIT_* */
1775 - if (unlikely(cs->singlestep_enabled)) {
1775 + if (unlikely(cpu_single_stepping(cs))) {
1776 gen_raise_exception(dc, EXCP_DEBUG);
1777 } else {
1778 tcg_gen_exit_tb(NULL, 0);
target/ppc/kvm.c
+1 -1
@@ -1615,7 +1615,7 @@ static int kvm_handle_debug(PowerPCCPU *cpu, struct kvm_run *run)
1615 CPUPPCState *env = &cpu->env;
1616 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1617
1618 - if (cs->singlestep_enabled) {
1618 + if (cpu_single_stepping(cs)) {
1619 return kvm_handle_singlestep();
1620 }
1621
target/s390x/kvm/kvm.c
+1 -1
@@ -1874,7 +1874,7 @@ static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1874 }
1875 break;
1876 case KVM_SINGLESTEP:
1877 - if (cs->singlestep_enabled) {
1877 + if (cpu_single_stepping(cs)) {
1878 ret = EXCP_DEBUG;
1879 }
1880 break;