accel/tcg: Make halt-to-exec transition explicit and remove cpu_exec_halt
v2 cover accel/tcg: Make halt-to-exec transition explicit and remove cpu_exec_halt
Since v1:
- Collected Richard R-b tags (sometimes commit description updated).
- Documented the BQL and synchronization requirements of has_work().
- Renamed the callbacks to poll_during_halt() and leaving_halt().
- Addressed Peter's comments by removing racy halt-work assertions, documenting the BQL contract, and explaining why x86 #DB delivery through do_interrupt_all() is safe without the BQL.
- Addressed Paolo's comments by making ARM PSCI state updates atomic, kicking remote vCPUs after power-state changes, and ensuring x86 APIC polling runs before cpu_has_work() with the BQL held only around APIC access.
- Sanitized s390_cpu_get_state() for atomic accesses without BQL.
This series was inspired by a previous thread on the list [*].
Refactor the CPU halt-to-execution transition logic in TCG as something more explicit and composable.
Core problem: TCGCPUOps::cpu_exec_halt callback mixed concerns, it checked for work, processed async events, and handled state transitions all in one place.
Solution: introduces two dedicated callbacks:
* poll_during_halt(): Process target-specific async events before checking for work. Called early in cpu_exec().
* leaving_halt(): Perform target-specific state updates when transitioning from halt to execution.
This separation allows the generic cpu_exec() code to orchestrate the flow cleanly (process events, check for work, transition state).
Only 2 targets need to be migrated (x86 and ARM) then we can remove the redundant cpu_exec_halt() hook.
The changes are expected to be purely refactoring with no functional impact.
Philippe Mathieu-Daudé (17):
system: Document has_work() synchronization requirements target/s390x: Use s390_cpu_get_state() consistently target/s390x: Make s390_cpu_set_state() return void target/s390x: Use S390CpuState for CPU state APIs target/s390x: Access S390CpuState atomically target/arm: Un-inline arm_set_cpu_power_state() target/arm: Kick vCPU after updating PSCI power state target/arm: Access PSCI state atomically
target/arm: Read halt state atomically in arm_cpu_has_work() accel/tcg: Refactor poll-while-halted flow in cpu_exec() accel/tcg: Introduce poll_during_halt() and leaving_halt() target/arm: Split leaving-halt handling from arm_cpu_exec_halt() target/arm: Convert cpu_exec_halt() to leaving_halt() target/i386: Split APIC polling from x86_cpu_exec_halt() target/i386: Split leaving-halt handling from x86_cpu_exec_halt() target/i386: Use generic halt transition handlers accel/tcg: Remove the redundant cpu_exec_halt() hook
include/accel/tcg/cpu-ops.h | 36 ++++++++++++++++------------- include/hw/core/sysemu-cpu-ops.h | 6 +++-- target/arm/internals.h | 11 +-------- target/i386/tcg/helper-tcg.h | 3 ++- target/loongarch/internals.h | 1 - target/riscv/internals.h | 5 ---- target/s390x/cpu.h | 9 ++++---- target/s390x/kvm/kvm_s390x.h | 2 +- target/s390x/s390x-internal.h | 1 - accel/tcg/cpu-exec.c | 20 ++++++++++------ hw/intc/s390_flic.c | 7 ++++-- target/alpha/cpu.c | 1 - target/arm/arm-powerctl.c | 13 +++++++++++ target/arm/cpu.c | 31 +++++++++++-------------- target/arm/tcg/cpu-v7m.c | 1 - target/avr/cpu.c | 1 - target/hexagon/cpu.c | 1 - target/hppa/cpu.c | 1 - target/i386/tcg/system/seg_helper.c | 17 +++++++++----- target/i386/tcg/tcg-cpu.c | 3 ++- target/loongarch/cpu.c | 2 +- target/loongarch/tcg/tcg_cpu.c | 1 - target/m68k/cpu.c | 1 - target/microblaze/cpu.c | 1 - target/mips/cpu.c | 1 - target/or1k/cpu.c | 1 - target/ppc/cpu_init.c | 1 - target/riscv/cpu.c | 2 +- target/riscv/tcg/tcg-cpu.c | 1 - target/rx/cpu.c | 1 - target/s390x/cpu-system.c | 26 ++++++++++++--------- target/s390x/cpu.c | 3 +-- target/s390x/kvm/kvm.c | 4 ++-- target/s390x/kvm/stubs.c | 2 +- target/s390x/machine.c | 2 +- target/s390x/sigp.c | 9 ++++++-- target/sh4/cpu.c | 1 - target/sparc/cpu.c | 1 - target/tricore/cpu.c | 1 - target/xtensa/cpu.c | 1 - 40 files changed, 119 insertions(+), 113 deletions(-)
Signature · 1 line
2.53.0
PATCH v2 1/17 system: Document has_work() synchronization requirements
Document that has_work() may be called with or without the BQL.
Require implementations to remain idempotent and avoid consuming work, and to synchronize state shared with other threads without acquiring the BQL unconditionally.
--- include/hw/core/sysemu-cpu-ops.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
include/hw/core/sysemu-cpu-ops.h +4 -2
@@ -19,8 +19,10 @@ typedef struct SysemuCPUOps {/** * @has_work: Callback for checking if there is work to do. * -* This function should be idempotent (i.e. not change state) as -* it will likely be queried multiple times before a CPU resumes. +* This callback may be called with or without the BQL. It must be +* idempotent, must not consume work, and must not assume that the BQL +* is held or acquire it unconditionally. State shared with other +* threads must use appropriate synchronization. */ bool (*has_work)(CPUState *cpu);/* MANDATORY NON-NULL */ /**
Signature · 1 line
2.53.0
Re: [PATCH v2 01/17] system: Document has_work() synchronization requirements
Quoted 11 lines · Philippe Mathieu-Daudé
Document that
has_work()may be called with or without the BQL.Require implementations to remain idempotent and avoid consuming work, and to synchronize state shared with other threads without acquiring the BQL unconditionally.
Suggested-by Peter Maydell<[email protected]>Signed-off-by Philippe Mathieu-Daudé<[email protected]>--- include/hw/core/sysemu-cpu-ops.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
r~
PATCH v2 2/17 target/s390x: Use s390_cpu_get_state() consistently
In reply to Philippe Mathieu-DaudéReplace direct reads of env::cpu_state with s390_cpu_get_state(). This keeps CPU state reads consistent across CPU transitions.
--- target/s390x/cpu-system.c | 2 +- target/s390x/cpu.c | 2 +- target/s390x/kvm/kvm.c | 2 +- target/s390x/machine.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-)
target/s390x/cpu-system.c +1 -1
@@ -263,7 +263,7 @@ unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu) cpu_state);exit (1 ); } -if (kvm_enabled () && cpu->env.cpu_state != cpu_state) { +if (kvm_enabled () &&s390_cpu_get_state (cpu) != cpu_state) {kvm_s390_set_cpu_state (cpu, cpu_state); } cpu->env.cpu_state = cpu_state;
target/s390x/cpu.c +1 -1
@@ -130,7 +130,7 @@ static void s390_query_cpu_fast(CPUState *cpu, CpuInfoFast *value) {S390CPU *s390_cpu =S390_CPU (cpu); - value->u.s390x.cpu_state = s390_cpu->env.cpu_state; + value->u.s390x.cpu_state =s390_cpu_get_state (s390_cpu);#if !defined(CONFIG_USER_ONLY )if (s390_has_topology ()) { value->u.s390x.has_dedicated = true;
target/s390x/kvm/kvm.c +1 -1
@@ -407,7 +407,7 @@ int kvm_arch_init_vcpu(CPUState *cs) {unsigned int max_cpus =MACHINE (qdev_get_machine ())->smp.max_cpus;S390CPU *cpu =S390_CPU (cs); -kvm_s390_set_cpu_state (cpu, cpu->env.cpu_state); +kvm_s390_set_cpu_state (cpu,s390_cpu_get_state (cpu)); cpu->irqstate =g_malloc0 (VCPU_IRQ_BUF_SIZE (max_cpus));return 0 ; }
target/s390x/machine.c +1 -1
@@ -32,7 +32,7 @@ static int cpu_post_load(void *opaque, int version_id)* than via cpu_synchronize_state, we need update kvm here. */ if (kvm_enabled ()) { -kvm_s390_set_cpu_state (cpu, cpu->env.cpu_state); +kvm_s390_set_cpu_state (cpu,s390_cpu_get_state (cpu));return kvm_s390_vcpu_interrupt_post_load (cpu); }
Signature · 1 line
2.53.0
Re: [PATCH v2 02/17] target/s390x: Use s390_cpu_get_state() consistently
Quoted 10 lines · Philippe Mathieu-Daudé
Replace direct reads of
env::cpu_statewiths390_cpu_get_state(). This keeps CPU state reads consistent across CPU transitions.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- target/s390x/cpu-system.c | 2 +- target/s390x/cpu.c | 2 +- target/s390x/kvm/kvm.c | 2 +- target/s390x/machine.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-)
PATCH v2 3/17 target/s390x: Make s390_cpu_set_state() return void
In reply to Philippe Mathieu-DaudéThe return value of s390_cpu_set_state() is only used by the STOP interrupt handler. Make the setter return void and count running CPUs directly at that call site.
--- target/s390x/cpu.h | 5 ++--- target/s390x/cpu-system.c | 4 +--- target/s390x/sigp.c | 3 ++- 3 files changed, 5 insertions(+), 7 deletions(-)
target/s390x/cpu.h +2 -3
@@ -847,11 +847,10 @@ void s390_do_cpu_set_diag318(CPUState *cs, run_on_cpu_data arg);int s390_assign_subch_ioeventfd (EventNotifier *notifier,uint32_t sch_id,int vq,bool assign);#ifndef CONFIG_USER_ONLY -unsigned int s390_cpu_set_state (uint8_t cpu_state,S390CPU *cpu); +void s390_cpu_set_state (uint8_t cpu_state,S390CPU *cpu);#else -static inline unsigned int s390_cpu_set_state (uint8_t cpu_state,S390CPU *cpu) +static inline void s390_cpu_set_state (uint8_t cpu_state,S390CPU *cpu) { -return 0 ; }#endif /* CONFIG_USER_ONLY */ static inline uint8_t s390_cpu_get_state (const S390CPU *cpu)
target/s390x/cpu-system.c +1 -3
@@ -236,7 +236,7 @@ void s390_cpu_unhalt(S390CPU *cpu) } } -unsigned int s390_cpu_set_state (uint8_t cpu_state,S390CPU *cpu) +void s390_cpu_set_state (uint8_t cpu_state,S390CPU *cpu) {trace_cpu_set_state (CPU (cpu)->cpu_index, cpu_state);
@@ -267,8 +267,6 @@ unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu)kvm_s390_set_cpu_state (cpu, cpu_state); } cpu->env.cpu_state = cpu_state; - -return s390_count_running_cpus (); }void s390_cmma_reset (void )
target/s390x/sigp.c +2 -1
@@ -617,7 +617,8 @@ void do_stop_interrupt(CPUS390XState *env)s390_store_status (cpu,S390_STORE_STATUS_DEF_ADDR , true); } env->sigp_order =0 ; -if (s390_cpu_set_state (S390_CPU_STATE_STOPPED , cpu) ==0 ) { +s390_cpu_set_state (S390_CPU_STATE_STOPPED , cpu); +if (s390_count_running_cpus () ==0 ) {qemu_system_shutdown_request (SHUTDOWN_CAUSE_GUEST_SHUTDOWN ); } env->pending_int &= ~INTERRUPT_STOP ;
Signature · 1 line
2.53.0
Re: [PATCH v2 03/17] target/s390x: Make s390_cpu_set_state() return void
Quoted 10 lines · Philippe Mathieu-Daudé
The return value of
s390_cpu_set_state()is only used by the STOP interrupt handler. Make the setter return void and count running CPUs directly at that call site.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- target/s390x/cpu.h | 5 ++--- target/s390x/cpu-system.c | 4 +--- target/s390x/sigp.c | 3 ++- 3 files changed, 5 insertions(+), 7 deletions(-)
PATCH v2 4/17 target/s390x: Use S390CpuState for CPU state APIs
In reply to Philippe Mathieu-DaudéUse the QAPI S390CpuState enum for CPU state accessors instead of uint8_t. Handle state checks with switch statements and assert unexpected states in SIGP restart and stop handling.
---
FIXME: g_assert_not_reached -> break?
--- target/s390x/cpu.h | 6 +++--- target/s390x/kvm/kvm_s390x.h | 2 +- hw/intc/s390_flic.c | 7 +++++-- target/s390x/cpu-system.c | 18 ++++++++++++------ target/s390x/kvm/kvm.c | 2 +- target/s390x/kvm/stubs.c | 2 +- target/s390x/sigp.c | 6 +++++- 7 files changed, 28 insertions(+), 15 deletions(-)
target/s390x/cpu.h +3 -3
@@ -847,13 +847,13 @@ void s390_do_cpu_set_diag318(CPUState *cs, run_on_cpu_data arg);int s390_assign_subch_ioeventfd (EventNotifier *notifier,uint32_t sch_id,int vq,bool assign);#ifndef CONFIG_USER_ONLY -void s390_cpu_set_state (uint8_t cpu_state,S390CPU *cpu); +void s390_cpu_set_state (S390CpuState cpu_state,S390CPU *cpu);#else -static inline void s390_cpu_set_state (uint8_t cpu_state,S390CPU *cpu) +static inline void s390_cpu_set_state (S390CpuState cpu_state,S390CPU *cpu) { }#endif /* CONFIG_USER_ONLY */ -static inline uint8_t s390_cpu_get_state (const S390CPU *cpu) +static inline S390CpuState s390_cpu_get_state (const S390CPU *cpu) {return cpu->env.cpu_state; }
target/s390x/kvm/kvm_s390x.h +1 -1
@@ -22,7 +22,7 @@ int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,int kvm_s390_mem_op_pv (S390CPU *cpu,vaddr addr,void *hostbuf,int len,bool is_write);void kvm_s390_program_interrupt (S390CPU *cpu,uint16_t code); -int kvm_s390_set_cpu_state (S390CPU *cpu,uint8_t cpu_state); +int kvm_s390_set_cpu_state (S390CPU *cpu,S390CpuState cpu_state);void kvm_s390_vcpu_interrupt_pre_save (S390CPU *cpu);int kvm_s390_vcpu_interrupt_post_load (S390CPU *cpu);int kvm_s390_get_hpage (void );
hw/intc/s390_flic.c +5 -2
@@ -193,8 +193,11 @@ static void qemu_s390_flic_notify(uint32_t type)cpu_set_interrupt (cs,CPU_INTERRUPT_HARD );/* ignore CPUs that are not sleeping */ -if (s390_cpu_get_state (cpu) !=S390_CPU_STATE_OPERATING && -s390_cpu_get_state (cpu) !=S390_CPU_STATE_LOAD ) { +switch (s390_cpu_get_state (cpu)) { +case S390_CPU_STATE_LOAD : +case S390_CPU_STATE_OPERATING : +break ; +default :continue ; }
target/s390x/cpu-system.c +12 -6
@@ -44,8 +44,11 @@ bool s390_cpu_has_work(CPUState *cs)S390CPU *cpu =S390_CPU (cs);/* STOPPED cpus can never wake up */ -if (s390_cpu_get_state (cpu) !=S390_CPU_STATE_LOAD && -s390_cpu_get_state (cpu) !=S390_CPU_STATE_OPERATING ) { +switch (s390_cpu_get_state (cpu)) { +case S390_CPU_STATE_LOAD : +case S390_CPU_STATE_OPERATING : +break ; +default :return false; }
@@ -202,12 +205,15 @@ unsigned s390_count_running_cpus(void)int nr_running =0 ;CPU_FOREACH (cpu) { -uint8_t state =S390_CPU (cpu)->env.cpu_state; -if (state ==S390_CPU_STATE_OPERATING || - state ==S390_CPU_STATE_LOAD ) { +switch (s390_cpu_get_state (S390_CPU (cpu))) { +case S390_CPU_STATE_LOAD : +case S390_CPU_STATE_OPERATING :if (!disabled_wait (cpu)) { nr_running++; } +break ; +default : +break ; } }
@@ -236,7 +242,7 @@ void s390_cpu_unhalt(S390CPU *cpu) } } -void s390_cpu_set_state (uint8_t cpu_state,S390CPU *cpu) +void s390_cpu_set_state (S390CpuState cpu_state,S390CPU *cpu) {trace_cpu_set_state (CPU (cpu)->cpu_index, cpu_state);
target/s390x/kvm/kvm.c +1 -1
@@ -2007,7 +2007,7 @@ int kvm_s390_get_ri(void)return cap_ri; } -int kvm_s390_set_cpu_state (S390CPU *cpu,uint8_t cpu_state) +int kvm_s390_set_cpu_state (S390CPU *cpu,S390CpuState cpu_state) {struct kvm_mp_state mp_state = {};int ret;
target/s390x/kvm/stubs.c +1 -1
@@ -128,7 +128,7 @@ int kvm_s390_mem_op_pv(S390CPU *cpu, vaddr addr, void *hostbuf, int len,g_assert_not_reached (); } -int kvm_s390_set_cpu_state (S390CPU *cpu,uint8_t cpu_state) +int kvm_s390_set_cpu_state (S390CPU *cpu,S390CpuState cpu_state) {g_assert_not_reached (); }
target/s390x/sigp.c +5 -1
@@ -39,7 +39,7 @@ static void set_sigp_status(SigpInfo *si, uint64_t status)static void sigp_sense (S390CPU *dst_cpu,SigpInfo *si) { -uint8_t state =s390_cpu_get_state (dst_cpu); +S390CpuState state =s390_cpu_get_state (dst_cpu);bool ext_call = dst_cpu->env.pending_int &INTERRUPT_EXTERNAL_CALL ;uint64_t status =0 ;
@@ -221,6 +221,8 @@ static void sigp_stop_and_store_status(CPUState *cs, run_on_cpu_data arg)cpu_synchronize_state (cs);s390_store_status (cpu,S390_STORE_STATUS_DEF_ADDR , true);break ; +default : +g_assert_not_reached (); } si->cc =SIGP_CC_ORDER_CODE_ACCEPTED ; }
@@ -362,6 +364,8 @@ static void sigp_restart(CPUState *cs, run_on_cpu_data arg)case S390_CPU_STATE_OPERATING :cpu_inject_restart (cpu);break ; +default : +g_assert_not_reached (); } si->cc =SIGP_CC_ORDER_CODE_ACCEPTED ; }
Signature · 1 line
2.53.0
Re: [PATCH v2 04/17] target/s390x: Use S390CpuState for CPU state APIs
Quoted 7 lines · Philippe Mathieu-Daudé
Use the QAPI S390CpuState enum for CPU state accessors instead of
uint8_t. Handle state checks with switch statements and assert unexpected states in SIGP restart and stop handling.Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
FIXME:g_assert_not_reached-> break?
Yes, please.
Those orders sent against any other cpu state should be NOPs, rather than a fatal assertion.
Quoted 9 lines
--- target/s390x/cpu.h | 6 +++--- target/s390x/kvm/kvm_s390x.h | 2 +- hw/intc/s390_flic.c | 7 +++++-- target/s390x/cpu-system.c | 18 ++++++++++++------ target/s390x/kvm/kvm.c | 2 +- target/s390x/kvm/stubs.c | 2 +- target/s390x/sigp.c | 6 +++++- 7 files changed, 28 insertions(+), 15 deletions(-)
The rest of this looks fine to me.
Quoted 156 lines
target/s390x/cpu.h +3
-3@@ -847,13 +847,13 @@ void s390_do_cpu_set_diag318(CPUState *cs, run_on_cpu_data arg);int s390_assign_subch_ioeventfd (EventNotifier *notifier,uint32_t sch_id,int vq,bool assign);#ifndef CONFIG_USER_ONLY -void s390_cpu_set_state (uint8_t cpu_state,S390CPU *cpu); +void s390_cpu_set_state (S390CpuState cpu_state,S390CPU *cpu);#else -static inline void s390_cpu_set_state (uint8_t cpu_state,S390CPU *cpu) +static inline void s390_cpu_set_state (S390CpuState cpu_state,S390CPU *cpu) { }#endif /* CONFIG_USER_ONLY */ -static inline uint8_t s390_cpu_get_state (const S390CPU *cpu) +static inline S390CpuState s390_cpu_get_state (const S390CPU *cpu) {return cpu->env.cpu_state; }target/s390x/kvm/kvm_s390x.h +1
-1@@ -22,7 +22,7 @@ int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,int kvm_s390_mem_op_pv (S390CPU *cpu,vaddr addr,void *hostbuf,int len,bool is_write);void kvm_s390_program_interrupt (S390CPU *cpu,uint16_t code); -int kvm_s390_set_cpu_state (S390CPU *cpu,uint8_t cpu_state); +int kvm_s390_set_cpu_state (S390CPU *cpu,S390CpuState cpu_state);void kvm_s390_vcpu_interrupt_pre_save (S390CPU *cpu);int kvm_s390_vcpu_interrupt_post_load (S390CPU *cpu);int kvm_s390_get_hpage (void );hw/intc/s390_flic.c +5
-2@@ -193,8 +193,11 @@ static void qemu_s390_flic_notify(uint32_t type)cpu_set_interrupt (cs,CPU_INTERRUPT_HARD );/* ignore CPUs that are not sleeping */ -if (s390_cpu_get_state (cpu) !=S390_CPU_STATE_OPERATING && -s390_cpu_get_state (cpu) !=S390_CPU_STATE_LOAD ) { +switch (s390_cpu_get_state (cpu)) { +case S390_CPU_STATE_LOAD : +case S390_CPU_STATE_OPERATING : +break ; +default :continue ; }target/s390x/cpu-system.c +12
-6@@ -44,8 +44,11 @@ bool s390_cpu_has_work(CPUState *cs)S390CPU *cpu =S390_CPU (cs);/* STOPPED cpus can never wake up */ -if (s390_cpu_get_state (cpu) !=S390_CPU_STATE_LOAD && -s390_cpu_get_state (cpu) !=S390_CPU_STATE_OPERATING ) { +switch (s390_cpu_get_state (cpu)) { +case S390_CPU_STATE_LOAD : +case S390_CPU_STATE_OPERATING : +break ; +default :return false; }@@ -202,12 +205,15 @@ unsigned s390_count_running_cpus(void)int nr_running =0 ;CPU_FOREACH (cpu) { -uint8_t state =S390_CPU (cpu)->env.cpu_state; -if (state ==S390_CPU_STATE_OPERATING || - state ==S390_CPU_STATE_LOAD ) { +switch (s390_cpu_get_state (S390_CPU (cpu))) { +case S390_CPU_STATE_LOAD : +case S390_CPU_STATE_OPERATING :if (!disabled_wait (cpu)) { nr_running++; } +break ; +default : +break ; } }@@ -236,7 +242,7 @@ void s390_cpu_unhalt(S390CPU *cpu) } } -void s390_cpu_set_state (uint8_t cpu_state,S390CPU *cpu) +void s390_cpu_set_state (S390CpuState cpu_state,S390CPU *cpu) {trace_cpu_set_state (CPU (cpu)->cpu_index, cpu_state);target/s390x/kvm/kvm.c +1
-1@@ -2007,7 +2007,7 @@ int kvm_s390_get_ri(void)return cap_ri; } -int kvm_s390_set_cpu_state (S390CPU *cpu,uint8_t cpu_state) +int kvm_s390_set_cpu_state (S390CPU *cpu,S390CpuState cpu_state) {struct kvm_mp_state mp_state = {};int ret;target/s390x/kvm/stubs.c +1
-1@@ -128,7 +128,7 @@ int kvm_s390_mem_op_pv(S390CPU *cpu, vaddr addr, void *hostbuf, int len,g_assert_not_reached (); } -int kvm_s390_set_cpu_state (S390CPU *cpu,uint8_t cpu_state) +int kvm_s390_set_cpu_state (S390CPU *cpu,S390CpuState cpu_state) {g_assert_not_reached (); }target/s390x/sigp.c +5
-1@@ -39,7 +39,7 @@ static void set_sigp_status(SigpInfo *si, uint64_t status)static void sigp_sense (S390CPU *dst_cpu,SigpInfo *si) { -uint8_t state =s390_cpu_get_state (dst_cpu); +S390CpuState state =s390_cpu_get_state (dst_cpu);bool ext_call = dst_cpu->env.pending_int &INTERRUPT_EXTERNAL_CALL ;uint64_t status =0 ;@@ -221,6 +221,8 @@ static void sigp_stop_and_store_status(CPUState *cs, run_on_cpu_data arg)cpu_synchronize_state (cs);s390_store_status (cpu,S390_STORE_STATUS_DEF_ADDR , true);break ; +default : +g_assert_not_reached (); } si->cc =SIGP_CC_ORDER_CODE_ACCEPTED ; }@@ -362,6 +364,8 @@ static void sigp_restart(CPUState *cs, run_on_cpu_data arg)case S390_CPU_STATE_OPERATING :cpu_inject_restart (cpu);break ; +default : +g_assert_not_reached (); } si->cc =SIGP_CC_ORDER_CODE_ACCEPTED ; }
PATCH v2 5/17 target/s390x: Access S390CpuState atomically
In reply to Philippe Mathieu-DaudéUse qatomic_read/set() in the S390 CPU state accessors. This avoids non-atomic reads in s390_cpu_has_work().
--- target/s390x/cpu.h | 2 +- target/s390x/cpu-system.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
target/s390x/cpu.h +1 -1
@@ -855,7 +855,7 @@ static inline void s390_cpu_set_state(S390CpuState cpu_state, S390CPU *cpu)#endif /* CONFIG_USER_ONLY */ static inline S390CpuState s390_cpu_get_state (const S390CPU *cpu) { -return cpu->env.cpu_state; +return qatomic_read (&cpu->env.cpu_state); }
target/s390x/cpu-system.c +1 -1
@@ -272,7 +272,7 @@ void s390_cpu_set_state(S390CpuState cpu_state, S390CPU *cpu)if (kvm_enabled () &&s390_cpu_get_state (cpu) != cpu_state) {kvm_s390_set_cpu_state (cpu, cpu_state); } - cpu->env.cpu_state = cpu_state; +qatomic_set (&cpu->env.cpu_state, cpu_state); }void s390_cmma_reset (void )
Signature · 1 line
2.53.0
Re: [PATCH v2 05/17] target/s390x: Access S390CpuState atomically
Quoted 3 lines · Philippe Mathieu-Daudé
Use qatomic_read/set() in the S390 CPU state accessors. This avoids non-atomic reads in
s390_cpu_has_work().
Quoted 5 lines
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- target/s390x/cpu.h | 2 +- target/s390x/cpu-system.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
Re: [PATCH v2 05/17] target/s390x: Access S390CpuState atomically
In reply to Philippe Mathieu-DaudéQuoted 8 lines · Philippe Mathieu-Daudé
Use qatomic_read/set() in the S390 CPU state accessors. This avoids non-atomic reads in
s390_cpu_has_work().Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- target/s390x/cpu.h | 2 +- target/s390x/cpu-system.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
PATCH v2 6/17 target/arm: Un-inline arm_set_cpu_power_state()
In reply to Philippe Mathieu-DaudéMove arm_set_cpu_power_state() from the header to arm-powerctl.c, keeping the power-state helper with the other power-control code. Restrict the call in arm_cpu_reset_hold() to system emulation.
--- target/arm/internals.h | 8 +------- target/arm/arm-powerctl.c | 7 +++++++ target/arm/cpu.c | 4 ++-- 3 files changed, 10 insertions(+), 9 deletions(-)
target/arm/internals.h +1 -7
@@ -2082,12 +2082,6 @@ bool arm_cpu_match_cpreg_mig_tolerance(ARMCPU *cpu, uint64_t kvmidx,/** * arm_set_cpu_power_state() - set power state synced with halt_reason */ -static inline void arm_set_cpu_power_state (ARMCPU *cpu,ARMPSCIState state) -{ -CPUARMState *env = &cpu->env; - - cpu->power_state = state; - env->halt_reason = state ==PSCI_OFF ?HALT_PSCI :NOT_HALTED ; -} +void arm_set_cpu_power_state (ARMCPU *cpu,ARMPSCIState state);#endif
target/arm/arm-powerctl.c +7 -0
@@ -45,6 +45,13 @@ struct CpuOnInfo {bool target_aa64; }; +void arm_set_cpu_power_state (ARMCPU *cpu,ARMPSCIState state) +{ +CPUARMState *env = &cpu->env; + + cpu->power_state = state; + env->halt_reason = state ==PSCI_OFF ?HALT_PSCI :NOT_HALTED ; +}static void arm_set_cpu_on_async_work (CPUState *target_cpu_state,run_on_cpu_data data)
target/arm/cpu.c +2 -2
@@ -351,8 +351,6 @@ static void arm_cpu_reset_hold(Object *obj, ResetType type) env->vfp.xregs[ARM_VFP_MVFR1 ] = cpu->isar.mvfr1; env->vfp.xregs[ARM_VFP_MVFR2 ] = cpu->isar.mvfr2; -arm_set_cpu_power_state (cpu, cs->start_powered_off ?PSCI_OFF :PSCI_ON ); -if (arm_feature (env,ARM_FEATURE_AARCH64 )) {/* 64 bit CPUs always start in 64 bit mode */ env->aarch64 = true;
@@ -671,6 +669,8 @@ static void arm_cpu_reset_hold(Object *obj, ResetType type)arm_set_ah_fp_behaviours (&env->vfp.fp_status[FPST_AH_F16 ]);#ifndef CONFIG_USER_ONLY +arm_set_cpu_power_state (cpu, cs->start_powered_off ?PSCI_OFF :PSCI_ON ); +if (kvm_enabled ()) {kvm_arm_reset_vcpu (cpu); }
Signature · 1 line
2.53.0
Re: [PATCH v2 06/17] target/arm: Un-inline arm_set_cpu_power_state()
Quoted 10 lines · Philippe Mathieu-Daudé
Move
arm_set_cpu_power_state()from the header toarm-powerctl.c, keeping the power-state helper with the other power-control code. Restrict the call inarm_cpu_reset_hold()to system emulation.Signed-off-by Philippe Mathieu-Daudé<[email protected]>--- target/arm/internals.h | 8 +------- target/arm/arm-powerctl.c | 7 +++++++ target/arm/cpu.c | 4 ++-- 3 files changed, 10 insertions(+), 9 deletions(-)
r~
PATCH v2 7/17 target/arm: Kick vCPU after updating PSCI power state
In reply to Philippe Mathieu-DaudéPSCI can update the power state of a vCPU other than the caller. Kick that vCPU after changing its state so it observes the update promptly.
--- target/arm/arm-powerctl.c | 5 +++++ 1 file changed, 5 insertions(+)
target/arm/arm-powerctl.c +5 -0
@@ -48,9 +48,14 @@ struct CpuOnInfo {void arm_set_cpu_power_state (ARMCPU *cpu,ARMPSCIState state) {CPUARMState *env = &cpu->env; +CPUState *cs =CPU (cpu); cpu->power_state = state; env->halt_reason = state ==PSCI_OFF ?HALT_PSCI :NOT_HALTED ; + +if (!qemu_cpu_is_self (cs)) { +qemu_cpu_kick (cs); + } } static void arm_set_cpu_on_async_work(CPUState *target_cpu_state,
Signature · 1 line
2.53.0
PATCH v2 8/17 target/arm: Access PSCI state atomically
In reply to Philippe Mathieu-DaudéUse qatomic_set() when updating ARMCPU::power_state and CPUARMState::halt_reason. Read ARMCPU::power_state atomically in arm_cpu_has_work(), which can run without the BQL held.
--- target/arm/arm-powerctl.c | 5 +++-- target/arm/cpu.c | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-)
target/arm/arm-powerctl.c +3 -2
@@ -50,8 +50,9 @@ void arm_set_cpu_power_state(ARMCPU *cpu, ARMPSCIState state)CPUARMState *env = &cpu->env;CPUState *cs =CPU (cpu); - cpu->power_state = state; - env->halt_reason = state ==PSCI_OFF ?HALT_PSCI :NOT_HALTED ; +qatomic_set (&cpu->power_state, state); +qatomic_set (&env->halt_reason, state ==PSCI_OFF ?HALT_PSCI + :NOT_HALTED );if (!qemu_cpu_is_self (cs)) {qemu_cpu_kick (cs);
target/arm/cpu.c +1 -1
@@ -149,7 +149,7 @@ static bool arm_cpu_has_work(CPUState *cs)* Only another PSCI call can wake the CPU up in which case the * power_state would be set by arm_set_cpu_on_and_reset_async_work() */ -if (cpu->power_state ==PSCI_OFF ) { +if (qatomic_read (&cpu->power_state) ==PSCI_OFF ) {g_assert (cpu->env.halt_reason ==HALT_PSCI );return false; }
Signature · 1 line
2.53.0
Re: [PATCH v2 08/17] target/arm: Access PSCI state atomically
Quoted 9 lines · Philippe Mathieu-Daudé
Use
qatomic_set()when updatingARMCPU::power_stateandCPUARMState::halt_reason. ReadARMCPU::power_stateatomically inarm_cpu_has_work(), which can run without the BQL held.Signed-off-by Philippe Mathieu-Daudé<[email protected]>--- target/arm/arm-powerctl.c | 5 +++-- target/arm/cpu.c | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-)
r~
PATCH v2 9/17 target/arm: Read halt state atomically in arm_cpu_has_work()
In reply to Philippe Mathieu-Daudéarm_cpu_has_work() runs without the BQL held. Use atomic reads for halt_reason and event_register to safely inspect state updated by other CPU contexts.
--- target/arm/cpu.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
target/arm/cpu.c +3 -2
@@ -144,20 +144,21 @@ int arm_cpu_mmu_index(CPUState *cs, bool ifetch)static bool arm_cpu_has_work (CPUState *cs) {ARMCPU *cpu =ARM_CPU (cs); +ARMHaltReason halt_reason =qatomic_read (&cpu->env.halt_reason);/* * Only another PSCI call can wake the CPU up in which case the * power_state would be set by arm_set_cpu_on_and_reset_async_work() */ if (qatomic_read (&cpu->power_state) ==PSCI_OFF ) { -g_assert (cpu->env.halt_reason ==HALT_PSCI ); +g_assert (halt_reason ==HALT_PSCI );return false; }/* * A wake-up event should only wake us if we are halted on a WFE */ -if (cpu->env.halt_reason ==HALT_WFE && cpu->env.event_register) { +if (halt_reason ==HALT_WFE &&qatomic_read (&cpu->env.event_register)) {return true; }
Signature · 1 line
2.53.0
Re: [PATCH v2 09/17] target/arm: Read halt state atomically in arm_cpu_has_work()
Quoted 8 lines · Philippe Mathieu-Daudé
arm_cpu_has_work()runs without the BQL held. Use atomic reads forhalt_reasonandevent_registerto safely inspect state updated by other CPU contexts.Signed-off-by Philippe Mathieu-Daudé<[email protected]>--- target/arm/cpu.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
r~
PATCH v2 10/17 accel/tcg: Refactor poll-while-halted flow in cpu_exec()
In reply to Philippe Mathieu-DaudéInvert cpu_handle_halt()'s logic to read in cpu_exec() as "if halted, poll for async events and find work; if no work, return". Rename as cpu_poll_while_halted().
--- accel/tcg/cpu-exec.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
accel/tcg/cpu-exec.c +4 -4
@@ -654,18 +654,18 @@ static inline void tb_add_jump(TranslationBlock *tb, int n, }#ifndef CONFIG_USER_ONLY -static inline bool cpu_handle_halt (CPUState *cpu) +static bool cpu_poll_while_halted (CPUState *cpu) {const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;bool leave_halt = tcg_ops->cpu_exec_halt (cpu);if (!leave_halt) { -return true; +return false; } cpu->halted =0 ;/* allow execution */ -return false; +return true; }#endif /* !CONFIG_USER_ONLY */
@@ -1027,7 +1027,7 @@ int cpu_exec(CPUState *cpu)#ifndef CONFIG_USER_ONLY if (cpu->halted) { -if (cpu_handle_halt (cpu)) { +if (!cpu_poll_while_halted (cpu)) {return EXCP_HALTED ; }assert (!cpu->halted);
Signature · 1 line
2.53.0
PATCH v2 11/17 accel/tcg: Introduce poll_during_halt() and leaving_halt()
In reply to Philippe Mathieu-DaudéSplit target-specific halt handling into an optional poll_during_halt() callback before cpu_has_work() and a leaving_halt() callback after work is found.
Keep cpu_exec_halt() as a legacy alternative for targets that have not migrated. Require each target to provide either cpu_exec_halt() or leaving_halt(), but not both.
--- include/accel/tcg/cpu-ops.h | 24 +++++++++++++++++++++++- accel/tcg/cpu-exec.c | 21 +++++++++++++++++---- 2 files changed, 40 insertions(+), 5 deletions(-)
include/accel/tcg/cpu-ops.h +23 -1
@@ -169,6 +169,27 @@ struct TCGCPUOps {*/ vaddr (*untagged_addr)(CPUState *cs,vaddr addr);#else +/** +* @poll_during_halt: Poll target-specific events while the CPU is halted. +* @cpu: vCPU context +* +* Called when the CPU is halted to handle target-specific asynchronous +* event processing before instruction execution begins. +* The caller does not hold the BQL. +*/ +void (*poll_during_halt)(CPUState *cpu); +/** +* @leaving_halt: Perform target-specific cleanup before resuming +* execution. +* @cpu: vCPU context +* +* Called after a halted CPU has detected pending work and is about to +* resume execution from the halted state. This callback performs any +* necessary target-specific state transitions or synchronization before +* instruction execution resumes. The caller does not hold the BQL. +* Either this callback or @cpu_exec_halt must be provided (but not both). +*/ +void (*leaving_halt)(CPUState *cpu);/** @do_interrupt: Callback for interrupt handling. */ void (*do_interrupt)(CPUState *cpu);/** @cpu_exec_interrupt: Callback for processing interrupts in cpu_exec */
@@ -185,7 +206,8 @@ struct TCGCPUOps {* if it should remain in the halted state. (This should generally * be the same value that cpu_has_work() would return.) * -* This method must be provided. If the target does not need to +* Either @leaving_halt or this method must be provided, but not both. +* If the target does not need to * do anything special for halt, the same function used for its * SysemuCPUOps::has_work method can be used here, as they have the * same function signature.
accel/tcg/cpu-exec.c +17 -4
@@ -48,6 +48,7 @@#include "internal-common.h" #if !defined(CONFIG_USER_ONLY )#include "accel/tcg/iommu.h" +#include "hw/core/sysemu-cpu-ops.h" #endif /* -icount align implementation. */
@@ -657,10 +658,22 @@ static inline void tb_add_jump(TranslationBlock *tb, int n,static bool cpu_poll_while_halted (CPUState *cpu) {const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops; -bool leave_halt = tcg_ops->cpu_exec_halt (cpu); -if (!leave_halt) { -return false; +if (tcg_ops->leaving_halt) { +assert (!tcg_ops->cpu_exec_halt); +if (tcg_ops->poll_during_halt) { + tcg_ops->poll_during_halt (cpu); + } +if (!cpu_has_work (cpu)) { +return false; + } + tcg_ops->leaving_halt (cpu); + }else { +assert (!tcg_ops->poll_during_halt); +assert (cpu->cc->sysemu_ops->has_work == tcg_ops->cpu_exec_halt); +if (!tcg_ops->cpu_exec_halt (cpu)) { +return false; + } } cpu->halted =0 ;/* allow execution */
@@ -1059,7 +1072,7 @@ bool tcg_exec_realizefn(CPUState *cpu, Error **errp)/* Check mandatory TCGCPUOps handlers */ const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;#ifndef CONFIG_USER_ONLY -assert (tcg_ops->cpu_exec_halt); +assert (tcg_ops->cpu_exec_halt || tcg_ops->leaving_halt);assert (tcg_ops->cpu_exec_interrupt);assert (tcg_ops->cpu_exec_reset);assert (tcg_ops->pointer_wrap);
Signature · 1 line
2.53.0
PATCH v2 12/17 target/arm: Split leaving-halt handling from arm_cpu_exec_halt()
In reply to Philippe Mathieu-DaudéMove WFxT timer cleanup and halt-reason reset into arm_cpu_leaving_halt(). Keep arm_cpu_exec_halt() focused on deciding whether the CPU can leave the halted state.
--- target/arm/cpu.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
target/arm/cpu.c +13 -7
@@ -872,18 +872,24 @@ static bool arm_cpu_internal_is_big_endian(CPUState *cs) }#ifdef CONFIG_TCG +static void arm_cpu_leaving_halt (CPUState *cs) +{ +ARMCPU *cpu =ARM_CPU (cs); + +/* We're about to come out of WFI/WFE: disable the WFxT timer */ +if (cpu->wfxt_timer) { +timer_del (cpu->wfxt_timer); + } +/* clear the halt reason */ + cpu->env.halt_reason =NOT_HALTED ; +} +bool arm_cpu_exec_halt (CPUState *cs) {bool leave_halt =cpu_has_work (cs);if (leave_halt) { -/* We're about to come out of WFI/WFE: disable the WFxT timer */ -ARMCPU *cpu =ARM_CPU (cs); -if (cpu->wfxt_timer) { -timer_del (cpu->wfxt_timer); - } -/* clear the halt reason */ - cpu->env.halt_reason =NOT_HALTED ; +arm_cpu_leaving_halt (cs); }return leave_halt; }
Signature · 1 line
2.53.0
PATCH v2 13/17 target/arm: Convert cpu_exec_halt() to leaving_halt()
In reply to Philippe Mathieu-Daudéarm_cpu_exec_halt() now merely checks cpu_has_work() and conditionally call arm_cpu_leaving_halt(), which is now the generic flow in cpu_has_work_after_processing_async_events().
Register the leaving_halt handler to use the generic target-agnostic flow and remove the arm_cpu_exec_halt() wrapper.
--- target/arm/internals.h | 3 --- target/arm/cpu.c | 12 +----------- target/arm/tcg/cpu-v7m.c | 1 - 3 files changed, 1 insertion(+), 15 deletions(-)
target/arm/internals.h +0 -3
@@ -431,9 +431,6 @@ void arm_restore_state_to_opc(CPUState *cs,#ifdef CONFIG_TCG TCGTBCPUState arm_get_tb_cpu_state (CPUState *cs);void arm_cpu_synchronize_from_tb (CPUState *cs,const TranslationBlock *tb); - -/* Our implementation of TCGCPUOps::cpu_exec_halt */ -bool arm_cpu_exec_halt (CPUState *cs);int arm_cpu_mmu_index (CPUState *cs,bool ifetch);#endif /* CONFIG_TCG */
target/arm/cpu.c +1 -11
@@ -883,16 +883,6 @@ static void arm_cpu_leaving_halt(CPUState *cs)/* clear the halt reason */ cpu->env.halt_reason = NOT_HALTED; } - -bool arm_cpu_exec_halt (CPUState *cs) -{ -bool leave_halt =cpu_has_work (cs); - -if (leave_halt) { -arm_cpu_leaving_halt (cs); - } -return leave_halt; -}#endif /*
@@ -2617,7 +2607,7 @@ static const TCGCPUOps arm_tcg_ops = { .tlb_fill_align = arm_cpu_tlb_fill_align, .pointer_wrap = aprofile_pointer_wrap, .cpu_exec_interrupt = arm_cpu_exec_interrupt, - .cpu_exec_halt = arm_cpu_exec_halt, + .leaving_halt = arm_cpu_leaving_halt, .cpu_exec_reset = cpu_reset, .do_interrupt = arm_cpu_do_interrupt, .do_transaction_failed = arm_cpu_do_transaction_failed,
target/arm/tcg/cpu-v7m.c +0 -1
@@ -302,7 +302,6 @@ static const TCGCPUOps arm_v7m_tcg_ops = { .tlb_fill_align = arm_cpu_tlb_fill_align, .pointer_wrap = cpu_pointer_wrap_uint32, .cpu_exec_interrupt = arm_v7m_cpu_exec_interrupt, - .cpu_exec_halt = arm_cpu_exec_halt, .cpu_exec_reset = cpu_reset, .do_interrupt = arm_v7m_cpu_do_interrupt, .do_transaction_failed = arm_cpu_do_transaction_failed,
Signature · 1 line
2.53.0
PATCH v2 14/17 target/i386: Split APIC polling from x86_cpu_exec_halt()
In reply to Philippe Mathieu-DaudéMove APIC polling into x86_cpu_poll_during_halt() while keeping the remaining halt handling in x86_cpu_exec_halt().
--- target/i386/tcg/helper-tcg.h | 1 + target/i386/tcg/system/seg_helper.c | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-)
target/i386/tcg/helper-tcg.h +1 -0
@@ -37,6 +37,7 @@*/ void x86_cpu_do_interrupt (CPUState *cpu);#ifndef CONFIG_USER_ONLY +void x86_cpu_poll_during_halt (CPUState *cpu);bool x86_cpu_exec_halt (CPUState *cpu);bool x86_need_replay_interrupt (int interrupt_request);bool x86_cpu_exec_interrupt (CPUState *cpu,int int_req);
target/i386/tcg/system/seg_helper.c +9 -2
@@ -128,10 +128,9 @@ void x86_cpu_do_interrupt(CPUState *cs) } } -bool x86_cpu_exec_halt (CPUState *cpu) +void x86_cpu_poll_during_halt (CPUState *cpu) {X86CPU *x86_cpu =X86_CPU (cpu); -CPUX86State *env = &x86_cpu->env;if (cpu_test_interrupt (cpu,CPU_INTERRUPT_POLL )) {bql_lock ();
@@ -139,6 +138,14 @@ bool x86_cpu_exec_halt(CPUState *cpu)cpu_reset_interrupt (cpu,CPU_INTERRUPT_POLL );bql_unlock (); } +} + +bool x86_cpu_exec_halt (CPUState *cpu) +{ +X86CPU *x86_cpu =X86_CPU (cpu); +CPUX86State *env = &x86_cpu->env; + +x86_cpu_poll_during_halt (cpu);if (!cpu_has_work (cpu)) {return false;
Signature · 1 line
2.53.0
PATCH v2 15/17 target/i386: Split leaving-halt handling from x86_cpu_exec_halt()
In reply to Philippe Mathieu-DaudéMove debug exception handling performed when leaving HLT into x86_cpu_leaving_halt(). Keep x86_cpu_exec_halt() responsible for checking whether the CPU can resume.
Document that #DB IRQ delivery through do_interrupt_all() runs in the vCPU thread and does not require BQL protection.
--- target/i386/tcg/helper-tcg.h | 1 + target/i386/tcg/system/seg_helper.c | 25 +++++++++++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-)
target/i386/tcg/helper-tcg.h +1 -0
@@ -39,6 +39,7 @@ void x86_cpu_do_interrupt(CPUState *cpu);#ifndef CONFIG_USER_ONLY void x86_cpu_poll_during_halt (CPUState *cpu);bool x86_cpu_exec_halt (CPUState *cpu); +void x86_cpu_leaving_halt (CPUState *cpu);bool x86_need_replay_interrupt (int interrupt_request);bool x86_cpu_exec_interrupt (CPUState *cpu,int int_req);#endif
target/i386/tcg/system/seg_helper.c +17 -8
@@ -140,22 +140,31 @@ void x86_cpu_poll_during_halt(CPUState *cpu) } } -bool x86_cpu_exec_halt (CPUState *cpu) +void x86_cpu_leaving_halt (CPUState *cpu) {X86CPU *x86_cpu =X86_CPU (cpu); -CPUX86State *env = &x86_cpu->env; - -x86_cpu_poll_during_halt (cpu); - -if (!cpu_has_work (cpu)) { -return false; - } +CPUX86State *env =cpu_env (cpu);/* Complete HLT instruction. */ if (env->eflags &TF_MASK ) { env->dr[6 ] |=DR6_BS ; +/* +* do_interrupt_all() is the normal TCG exception-delivery path +* and runs in the same vCPU thread context, so #DB delivery does +* not require BQL protection here. +*/ do_interrupt_all (x86_cpu,EXCP01_DB ,0 ,0 , env->eip,0 ); } +} + +bool x86_cpu_exec_halt (CPUState *cpu) +{ +if (!cpu_has_work (cpu)) { +return false; + } + +x86_cpu_leaving_halt (cpu); +return true; }
Signature · 1 line
2.53.0
PATCH v2 16/17 target/i386: Use generic halt transition handlers
In reply to Philippe Mathieu-DaudéRegister x86_cpu_poll_during_halt() and x86_cpu_leaving_halt() with the generic TCG halt flow. Remove the target-specific x86_cpu_exec_halt() wrapper.
--- target/i386/tcg/helper-tcg.h | 1 - target/i386/tcg/system/seg_helper.c | 11 ----------- target/i386/tcg/tcg-cpu.c | 3 ++- 3 files changed, 2 insertions(+), 13 deletions(-)
target/i386/tcg/helper-tcg.h +0 -1
@@ -38,7 +38,6 @@void x86_cpu_do_interrupt (CPUState *cpu);#ifndef CONFIG_USER_ONLY void x86_cpu_poll_during_halt (CPUState *cpu); -bool x86_cpu_exec_halt (CPUState *cpu);void x86_cpu_leaving_halt (CPUState *cpu);bool x86_need_replay_interrupt (int interrupt_request);bool x86_cpu_exec_interrupt (CPUState *cpu,int int_req);
target/i386/tcg/system/seg_helper.c +0 -11
@@ -157,17 +157,6 @@ void x86_cpu_leaving_halt(CPUState *cpu) } } -bool x86_cpu_exec_halt (CPUState *cpu) -{ -if (!cpu_has_work (cpu)) { -return false; - } - -x86_cpu_leaving_halt (cpu); - -return true; -} -bool x86_need_replay_interrupt (int interrupt_request) {/*
target/i386/tcg/tcg-cpu.c +2 -1
@@ -180,7 +180,8 @@ const TCGCPUOps x86_tcg_ops = { .tlb_fill = x86_cpu_tlb_fill, .pointer_wrap = x86_pointer_wrap, .do_interrupt = x86_cpu_do_interrupt, - .cpu_exec_halt = x86_cpu_exec_halt, + .poll_during_halt = x86_cpu_poll_during_halt, + .leaving_halt = x86_cpu_leaving_halt, .cpu_exec_interrupt = x86_cpu_exec_interrupt, .cpu_exec_reset = x86_cpu_exec_reset, .do_unaligned_access = x86_cpu_do_unaligned_access,
Signature · 1 line
2.53.0
PATCH v2 17/17 accel/tcg: Remove the redundant cpu_exec_halt() hook
In reply to Philippe Mathieu-DaudéAll remaining users of cpu_exec_halt simply alias cpu_has_work(). Call cpu_has_work() directly from the generic halt flow and remove the redundant hook registrations and interface definition.
Make affected cpu_has_work() helpers private where possible.
--- include/accel/tcg/cpu-ops.h | 18 ------------------ target/loongarch/internals.h | 1 - target/riscv/internals.h | 5 ----- target/s390x/s390x-internal.h | 1 - accel/tcg/cpu-exec.c | 23 ++++++++--------------- target/alpha/cpu.c | 1 - target/avr/cpu.c | 1 - target/hexagon/cpu.c | 1 - target/hppa/cpu.c | 1 - target/loongarch/cpu.c | 2 +- target/loongarch/tcg/tcg_cpu.c | 1 - target/m68k/cpu.c | 1 - target/microblaze/cpu.c | 1 - target/mips/cpu.c | 1 - target/or1k/cpu.c | 1 - target/ppc/cpu_init.c | 1 - target/riscv/cpu.c | 2 +- target/riscv/tcg/tcg-cpu.c | 1 - target/rx/cpu.c | 1 - target/s390x/cpu-system.c | 2 +- target/s390x/cpu.c | 1 - target/sh4/cpu.c | 1 - target/sparc/cpu.c | 1 - target/tricore/cpu.c | 1 - target/xtensa/cpu.c | 1 - 25 files changed, 11 insertions(+), 60 deletions(-)
include/accel/tcg/cpu-ops.h +0 -18
@@ -187,7 +187,6 @@ struct TCGCPUOps {* resume execution from the halted state. This callback performs any * necessary target-specific state transitions or synchronization before * instruction execution resumes. The caller does not hold the BQL. -* Either this callback or @cpu_exec_halt must be provided (but not both). */ void (*leaving_halt)(CPUState *cpu);/** @do_interrupt: Callback for interrupt handling. */
@@ -196,23 +195,6 @@ struct TCGCPUOps {bool (*cpu_exec_interrupt)(CPUState *cpu,int interrupt_request);/** @cpu_exec_reset: Callback for reset in cpu_exec. */ void (*cpu_exec_reset)(CPUState *cpu); -/** -* @cpu_exec_halt: Callback for handling halt in cpu_exec. -* -* The target CPU should do any special processing here that it needs -* to do when the CPU is in the halted state. -* -* Return true to indicate that the CPU should now leave halt, false -* if it should remain in the halted state. (This should generally -* be the same value that cpu_has_work() would return.) -* -* Either @leaving_halt or this method must be provided, but not both. -* If the target does not need to -* do anything special for halt, the same function used for its -* SysemuCPUOps::has_work method can be used here, as they have the -* same function signature. -*/ -bool (*cpu_exec_halt)(CPUState *cpu);/** * @tlb_fill_align: Handle a softmmu tlb miss * @cpu: cpu context
target/loongarch/internals.h +0 -1
@@ -38,7 +38,6 @@ void cpu_loongarch_timer_cb(void *opaque);uint64_t cpu_loongarch_get_timer_counter (CPUTimerState *timer);uint64_t cpu_loongarch_get_timer_ticks (CPUTimerState *timer);void cpu_loongarch_set_timer_config (CPUTimerState *timer,uint64_t value); -bool loongarch_cpu_has_work (CPUState *cs);bool cpu_loongarch_hw_interrupts_pending (CPULoongArchState *env);#endif /* !CONFIG_USER_ONLY */
target/riscv/internals.h +0 -5
@@ -201,11 +201,6 @@ static inline target_ulong get_xepc_mask(CPURISCVState *env) } } -#ifndef CONFIG_USER_ONLY -/* Our implementation of SysemuCPUOps::has_work */ -bool riscv_cpu_has_work (CPUState *cs); -#endif -/* Zjpm addr masking routine */ static inline target_ulong adjust_addr_body(CPURISCVState *env, target_ulong addr,
target/s390x/s390x-internal.h +0 -1
@@ -248,7 +248,6 @@ bool s390_cpu_system_realize(DeviceState *dev, Error **errp);void s390_cpu_finalize (Object *obj);void s390_cpu_system_class_init (CPUClass *cc);void s390_cpu_machine_reset_cb (void *opaque); -bool s390_cpu_has_work (CPUState *cs);#endif /* CONFIG_USER_ONLY */
accel/tcg/cpu-exec.c +8 -15
@@ -48,7 +48,6 @@#include "internal-common.h" #if !defined(CONFIG_USER_ONLY )#include "accel/tcg/iommu.h" -#include "hw/core/sysemu-cpu-ops.h" #endif /* -icount align implementation. */
@@ -659,21 +658,16 @@ static bool cpu_poll_while_halted(CPUState *cpu) {const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops; +if (tcg_ops->poll_during_halt) { + tcg_ops->poll_during_halt (cpu); + } + +if (!cpu_has_work (cpu)) { +return false; + } +if (tcg_ops->leaving_halt) { -assert (!tcg_ops->cpu_exec_halt); -if (tcg_ops->poll_during_halt) { - tcg_ops->poll_during_halt (cpu); - } -if (!cpu_has_work (cpu)) { -return false; - } tcg_ops->leaving_halt (cpu); - }else { -assert (!tcg_ops->poll_during_halt); -assert (cpu->cc->sysemu_ops->has_work == tcg_ops->cpu_exec_halt); -if (!tcg_ops->cpu_exec_halt (cpu)) { -return false; - } } cpu->halted =0 ;/* allow execution */
@@ -1072,7 +1066,6 @@ bool tcg_exec_realizefn(CPUState *cpu, Error **errp)/* Check mandatory TCGCPUOps handlers */ const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;#ifndef CONFIG_USER_ONLY -assert (tcg_ops->cpu_exec_halt || tcg_ops->leaving_halt);assert (tcg_ops->cpu_exec_interrupt);assert (tcg_ops->cpu_exec_reset);assert (tcg_ops->pointer_wrap);
target/alpha/cpu.c +0 -1
@@ -265,7 +265,6 @@ static const TCGCPUOps alpha_tcg_ops = { .tlb_fill = alpha_cpu_tlb_fill, .pointer_wrap = cpu_pointer_wrap_notreached, .cpu_exec_interrupt = alpha_cpu_exec_interrupt, - .cpu_exec_halt = alpha_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = alpha_cpu_do_interrupt, .do_transaction_failed = alpha_cpu_do_transaction_failed,
target/avr/cpu.c +0 -1
@@ -246,7 +246,6 @@ static const TCGCPUOps avr_tcg_ops = { .restore_state_to_opc = avr_restore_state_to_opc, .mmu_index = avr_cpu_mmu_index, .cpu_exec_interrupt = avr_cpu_exec_interrupt, - .cpu_exec_halt = avr_cpu_has_work, .cpu_exec_reset = cpu_reset, .tlb_fill = avr_cpu_tlb_fill, .do_interrupt = avr_cpu_do_interrupt,
target/hexagon/cpu.c +0 -1
@@ -783,7 +783,6 @@ static const TCGCPUOps hexagon_tcg_ops = { .cpu_exec_reset = cpu_reset, .tlb_fill = hexagon_tlb_fill, .do_unaligned_access = hexagon_cpu_do_unaligned_access, - .cpu_exec_halt = hexagon_cpu_has_work, .do_interrupt = hexagon_cpu_do_interrupt,#endif /* !CONFIG_USER_ONLY */ };
target/hppa/cpu.c +0 -1
@@ -270,7 +270,6 @@ static const TCGCPUOps hppa_tcg_ops = { .tlb_fill_align = hppa_cpu_tlb_fill_align, .pointer_wrap = cpu_pointer_wrap_notreached, .cpu_exec_interrupt = hppa_cpu_exec_interrupt, - .cpu_exec_halt = hppa_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = hppa_cpu_do_interrupt, .do_unaligned_access = hppa_cpu_do_unaligned_access,
target/loongarch/cpu.c +1 -1
@@ -123,7 +123,7 @@ bool cpu_loongarch_hw_interrupts_pending(CPULoongArchState *env)#endif #ifndef CONFIG_USER_ONLY -bool loongarch_cpu_has_work (CPUState *cs) +static bool loongarch_cpu_has_work (CPUState *cs) {bool has_work = false;
target/loongarch/tcg/tcg_cpu.c +0 -1
@@ -327,7 +327,6 @@ const TCGCPUOps loongarch_tcg_ops = { .tlb_fill = loongarch_cpu_tlb_fill, .pointer_wrap = loongarch_pointer_wrap, .cpu_exec_interrupt = loongarch_cpu_exec_interrupt, - .cpu_exec_halt = loongarch_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = loongarch_cpu_do_interrupt, .do_transaction_failed = loongarch_cpu_do_transaction_failed,
target/m68k/cpu.c +0 -1
@@ -710,7 +710,6 @@ static const TCGCPUOps m68k_tcg_ops = { .tlb_fill = m68k_cpu_tlb_fill, .pointer_wrap = cpu_pointer_wrap_uint32, .cpu_exec_interrupt = m68k_cpu_exec_interrupt, - .cpu_exec_halt = m68k_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = m68k_cpu_do_interrupt, .do_transaction_failed = m68k_cpu_transaction_failed,
target/microblaze/cpu.c +0 -1
@@ -448,7 +448,6 @@ static const TCGCPUOps mb_tcg_ops = { .tlb_fill = mb_cpu_tlb_fill, .pointer_wrap = cpu_pointer_wrap_uint32, .cpu_exec_interrupt = mb_cpu_exec_interrupt, - .cpu_exec_halt = mb_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = mb_cpu_do_interrupt, .do_transaction_failed = mb_cpu_transaction_failed,
target/mips/cpu.c +0 -1
@@ -716,7 +716,6 @@ static const TCGCPUOps mips_tcg_ops = { .tlb_fill = mips_cpu_tlb_fill, .pointer_wrap = mips_pointer_wrap, .cpu_exec_interrupt = mips_cpu_exec_interrupt, - .cpu_exec_halt = mips_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = mips_cpu_do_interrupt, .do_transaction_failed = mips_cpu_do_transaction_failed,
target/or1k/cpu.c +0 -1
@@ -266,7 +266,6 @@ static const TCGCPUOps openrisc_tcg_ops = { .tlb_fill = openrisc_cpu_tlb_fill, .pointer_wrap = cpu_pointer_wrap_uint32, .cpu_exec_interrupt = openrisc_cpu_exec_interrupt, - .cpu_exec_halt = openrisc_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = openrisc_cpu_do_interrupt,#endif /* !CONFIG_USER_ONLY */
target/ppc/cpu_init.c +0 -1
@@ -7496,7 +7496,6 @@ static const TCGCPUOps ppc_tcg_ops = { .tlb_fill = ppc_cpu_tlb_fill, .pointer_wrap = ppc_pointer_wrap, .cpu_exec_interrupt = ppc_cpu_exec_interrupt, - .cpu_exec_halt = ppc_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = ppc_cpu_do_interrupt, .cpu_exec_enter = ppc_cpu_exec_enter,
target/riscv/cpu.c +1 -1
@@ -953,7 +953,7 @@ int riscv_cpu_vsirq_pending(CPURISCVState *env) (irqs | irqs_f_vs), env->hviprio); } -bool riscv_cpu_has_work (CPUState *cs) +static bool riscv_cpu_has_work (CPUState *cs) {RISCVCPU *cpu =RISCV_CPU (cs);CPURISCVState *env = &cpu->env;
target/riscv/tcg/tcg-cpu.c +0 -1
@@ -286,7 +286,6 @@ const TCGCPUOps riscv_tcg_ops = { .tlb_fill = riscv_cpu_tlb_fill, .pointer_wrap = riscv_pointer_wrap, .cpu_exec_interrupt = riscv_cpu_exec_interrupt, - .cpu_exec_halt = riscv_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = riscv_cpu_do_interrupt, .do_transaction_failed = riscv_cpu_do_transaction_failed,
target/rx/cpu.c +0 -1
@@ -227,7 +227,6 @@ static const TCGCPUOps rx_tcg_ops = { .pointer_wrap = cpu_pointer_wrap_uint32, .cpu_exec_interrupt = rx_cpu_exec_interrupt, - .cpu_exec_halt = rx_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = rx_cpu_do_interrupt, };
target/s390x/cpu-system.c +1 -1
@@ -39,7 +39,7 @@#include "system/tcg.h" #include "hw/core/sysemu-cpu-ops.h" -bool s390_cpu_has_work (CPUState *cs) +static bool s390_cpu_has_work (CPUState *cs) {S390CPU *cpu =S390_CPU (cs);
target/s390x/cpu.c +0 -1
@@ -384,7 +384,6 @@ static const TCGCPUOps s390_tcg_ops = { .tlb_fill = s390_cpu_tlb_fill, .pointer_wrap = s390_pointer_wrap, .cpu_exec_interrupt = s390_cpu_exec_interrupt, - .cpu_exec_halt = s390_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = s390_cpu_do_interrupt, .debug_excp_handler = s390x_cpu_debug_excp_handler,
target/sh4/cpu.c +0 -1
@@ -312,7 +312,6 @@ static const TCGCPUOps superh_tcg_ops = { .tlb_fill = superh_cpu_tlb_fill, .pointer_wrap = cpu_pointer_wrap_notreached, .cpu_exec_interrupt = superh_cpu_exec_interrupt, - .cpu_exec_halt = superh_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = superh_cpu_do_interrupt, .do_unaligned_access = superh_cpu_do_unaligned_access,
target/sparc/cpu.c +0 -1
@@ -1074,7 +1074,6 @@ static const TCGCPUOps sparc_tcg_ops = { .tlb_fill = sparc_cpu_tlb_fill, .pointer_wrap = sparc_pointer_wrap, .cpu_exec_interrupt = sparc_cpu_exec_interrupt, - .cpu_exec_halt = sparc_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = sparc_cpu_do_interrupt, .do_transaction_failed = sparc_cpu_do_transaction_failed,
target/tricore/cpu.c +0 -1
@@ -223,7 +223,6 @@ static const TCGCPUOps tricore_tcg_ops = { .tlb_fill = tricore_cpu_tlb_fill, .pointer_wrap = cpu_pointer_wrap_uint32, .cpu_exec_interrupt = tricore_cpu_exec_interrupt, - .cpu_exec_halt = tricore_cpu_has_work, .cpu_exec_reset = cpu_reset, };
target/xtensa/cpu.c +0 -1
@@ -325,7 +325,6 @@ static const TCGCPUOps xtensa_tcg_ops = { .tlb_fill = xtensa_cpu_tlb_fill, .pointer_wrap = cpu_pointer_wrap_uint32, .cpu_exec_interrupt = xtensa_cpu_exec_interrupt, - .cpu_exec_halt = xtensa_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = xtensa_cpu_do_interrupt, .do_transaction_failed = xtensa_cpu_do_transaction_failed,
Signature · 1 line
2.53.0
Quoted 13 lines · Philippe Mathieu-Daudé
Since v1:
- Collected Richard R-b tags (sometimes commit description updated).
- Documented the BQL and synchronization requirements ofhas_work().
- Renamed the callbacks topoll_during_halt()andleaving_halt().
- Addressed Peter's comments by removing racy halt-work assertions, documenting the BQL contract, and explaining why x86 #DB delivery throughdo_interrupt_all()is safe without the BQL.
- Addressed Paolo's comments by making ARM PSCI state updates atomic, kicking remote vCPUs after power-state changes, and ensuring x86 APIC polling runs beforecpu_has_work()with the BQL held only around APIC access.
- Sanitizeds390_cpu_get_state()for atomic accesses without BQL.
Quoted 1 line
This series was inspired by a previous thread on the list [*].
I just realized I missed Paolo's comment on riscv :( Depending on how this series review goes I'll amend or respin.
https://lore.kernel.org/qemu-devel/CABgObfZQb8FwDubg=pZk6nRSrUgdMxoDpRwG3Kd=GwpQeccLhg@mail.gmail.com/