Skip to contentSkip to conversation

target/arm: Implement FEAT_WFxT

PATCH In review Peter Maydell 4 participants 11 messages Apr 30, 2024 – May 31, 2024

lore
Share & tools
lore
#0 lore raw

cover target/arm: Implement FEAT_WFxT

FEAT_WFxT introduces new instructions WFIT and WFET, which are like the existing WFI and WFE but allow the guest to pass a timeout value in a register. The instructions will wait for an interrupt/event as usual, but will also stop waiting when the value of CNTVCT_EL0 is greater than or equal to the specified timeout value.

This series implements this and enables it for '-cpu max'. Patch 1 is a tweak to the TCGCPUOps::cpu_exec_halt method so that we can use it in patch 2 for "do some work when we are going to leave the halt state".

thanks
-- PMM

Peter Maydell (2):
accel/tcg: Make TCGCPUOps::cpu_exec_halt return bool for whether to
halt
target/arm: Implement FEAT WFxT and enable for '-cpu max'

 docs/system/arm/emulation.rst       |  1 +
 include/hw/core/tcg-cpu-ops.h       | 11 ++++--
 target/arm/cpu-features.h           |  5 +++
 target/arm/cpu.h                    |  3 ++
 target/arm/helper.h                 |  1 +
 target/arm/internals.h              |  8 +++++
 target/i386/tcg/helper-tcg.h        |  2 +-
 target/arm/tcg/a64.decode           |  4 +++
 accel/tcg/cpu-exec.c                |  7 ++--
 target/arm/cpu.c                    | 38 ++++++++++++++++++++
 target/arm/helper.c                 |  4 +--
 target/arm/machine.c                | 20 +++++++++++
 target/arm/tcg/cpu64.c              |  1 +
 target/arm/tcg/op_helper.c          | 54 +++++++++++++++++++++++++++++
 target/arm/tcg/translate-a64.c      | 41 ++++++++++++++++++++++
 target/i386/tcg/sysemu/seg_helper.c |  3 +-
 16 files changed, 195 insertions(+), 8 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/2 accel/tcg: Make TCGCPUOps::cpu_exec_halt return bool for whether to halt

The TCGCPUOps::cpu_exec_halt method is called from cpu_handle_halt() when the CPU is halted, so that a target CPU emulation can do anything target-specific it needs to do. (At the moment we only use this on i386.)

The current specification of the method doesn't allow the target specific code to do something different if the CPU is about to come out of the halt state, because cpu_handle_halt() only determines this after the method has returned. (If the method called cpu_has_work() itself this would introduce a potential race if an interrupt arrived between the target's method implementation checking and cpu_handle_halt() repeating the check.)

Change the definition of the method so that it returns a bool to tell cpu_handle_halt() whether to stay in halt or not.

We will want this for the Arm target, where FEAT_WFxT wants to do some work only for the case where the CPU is in halt but about to leave it.

---
 include/hw/core/tcg-cpu-ops.h       | 11 +++++++++--
 target/i386/tcg/helper-tcg.h        |  2 +-
 accel/tcg/cpu-exec.c                |  7 +++++--
 target/i386/tcg/sysemu/seg_helper.c |  3 ++-
 4 files changed, 17 insertions(+), 6 deletions(-)
include/hw/core/tcg-cpu-ops.h +9 -2
index dc1f16a9777..f3ac76e6f6d 100644
@@ -111,8 +111,15 @@ struct TCGCPUOps {
     void (*do_interrupt)(CPUState *cpu);
     /** @cpu_exec_interrupt: Callback for processing interrupts in cpu_exec */
     bool (*cpu_exec_interrupt)(CPUState *cpu, int interrupt_request);
-    /** @cpu_exec_halt: Callback for handling halt in cpu_exec */
-    void (*cpu_exec_halt)(CPUState *cpu);
+    /**
+     * @cpu_exec_halt: Callback for handling halt in cpu_exec.
+     *
+     * Return true to indicate that the CPU should now leave halt, false
+     * if it should remain in the halted state.
+     * If this method is not provided, the default is to leave halt
+     * if cpu_has_work() returns true.
+     */
+    bool (*cpu_exec_halt)(CPUState *cpu);
     /**
      * @tlb_fill: Handle a softmmu tlb miss
      *
target/i386/tcg/helper-tcg.h +1 -1
index effc2c1c984..85957943bf3 100644
@@ -39,7 +39,7 @@ QEMU_BUILD_BUG_ON(TCG_PHYS_ADDR_BITS > TARGET_PHYS_ADDR_SPACE_BITS);
  */
 void x86_cpu_do_interrupt(CPUState *cpu);
 #ifndef CONFIG_USER_ONLY
-void x86_cpu_exec_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);
 #endif
accel/tcg/cpu-exec.c +5 -2
index 5c70748060a..550f93b19ce 100644
@@ -669,11 +669,14 @@ static inline bool cpu_handle_halt(CPUState *cpu)
 #ifndef CONFIG_USER_ONLY
     if (cpu->halted) {
         const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
+        bool leave_halt;
 
         if (tcg_ops->cpu_exec_halt) {
-            tcg_ops->cpu_exec_halt(cpu);
+            leave_halt = tcg_ops->cpu_exec_halt(cpu);
+        } else {
+            leave_halt = cpu_has_work(cpu);
         }
-        if (!cpu_has_work(cpu)) {
+        if (!leave_halt) {
             return true;
         }
 
target/i386/tcg/sysemu/seg_helper.c +2 -1
index 2db8083748e..9ba94deb3aa 100644
@@ -128,7 +128,7 @@ void x86_cpu_do_interrupt(CPUState *cs)
     }
 }
 
-void x86_cpu_exec_halt(CPUState *cpu)
+bool x86_cpu_exec_halt(CPUState *cpu)
 {
     if (cpu->interrupt_request & CPU_INTERRUPT_POLL) {
         X86CPU *x86_cpu = X86_CPU(cpu);
@@ -138,6 +138,7 @@ void x86_cpu_exec_halt(CPUState *cpu)
         cpu_reset_interrupt(cpu, CPU_INTERRUPT_POLL);
         bql_unlock();
     }
+    return cpu_has_work(cpu);
 }
 
 bool x86_need_replay_interrupt(int interrupt_request)
Signature · 1 line
2.34.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] accel/tcg: Make TCGCPUOps::cpu_exec_halt return bool for whether to halt

Quoted 20 lines · Peter Maydell

The TCGCPUOps::cpu_exec_halt method is called from cpu_handle_halt() when the CPU is halted, so that a target CPU emulation can do anything target-specific it needs to do. (At the moment we only use this on i386.)

The current specification of the method doesn't allow the target specific code to do something different if the CPU is about to come out of the halt state, because cpu_handle_halt() only determines this after the method has returned. (If the method called cpu_has_work() itself this would introduce a potential race if an interrupt arrived between the target's method implementation checking and cpu_handle_halt() repeating the check.)

Change the definition of the method so that it returns a bool to tell cpu_handle_halt() whether to stay in halt or not.

We will want this for the Arm target, where FEAT_WFxT wants to do some work only for the case where the CPU is in halt but about to leave it.

Quoted 7 lines
---
  include/hw/core/tcg-cpu-ops.h       | 11 +++++++++--
  target/i386/tcg/helper-tcg.h        |  2 +-
  accel/tcg/cpu-exec.c                |  7 +++++--
  target/i386/tcg/sysemu/seg_helper.c |  3 ++-
  4 files changed, 17 insertions(+), 6 deletions(-)
#3 lore raw

Re: [PATCH 1/2] accel/tcg: Make TCGCPUOps::cpu_exec_halt return bool for whether to halt

In reply to Peter Maydell
Quoted 37 lines · Peter Maydell

The TCGCPUOps::cpu_exec_halt method is called from cpu_handle_halt() when the CPU is halted, so that a target CPU emulation can do anything target-specific it needs to do. (At the moment we only use this on i386.)

The current specification of the method doesn't allow the target specific code to do something different if the CPU is about to come out of the halt state, because cpu_handle_halt() only determines this after the method has returned. (If the method called cpu_has_work() itself this would introduce a potential race if an interrupt arrived between the target's method implementation checking and cpu_handle_halt() repeating the check.)

Change the definition of the method so that it returns a bool to tell cpu_handle_halt() whether to stay in halt or not.

We will want this for the Arm target, where FEAT_WFxT wants to do some work only for the case where the CPU is in halt but about to leave it.

---
 include/hw/core/tcg-cpu-ops.h       | 11 +++++++++--
 target/i386/tcg/helper-tcg.h        |  2 +-
 accel/tcg/cpu-exec.c                |  7 +++++--
 target/i386/tcg/sysemu/seg_helper.c |  3 ++-
 4 files changed, 17 insertions(+), 6 deletions(-)
include/hw/core/tcg-cpu-ops.h +0 -1
index dc1f16a9777..f3ac76e6f6d 100644
@@ -111,8 +111,15 @@ struct TCGCPUOps {
     void (*do_interrupt)(CPUState *cpu);
     /** @cpu_exec_interrupt: Callback for processing interrupts in cpu_exec */
     bool (*cpu_exec_interrupt)(CPUState *cpu, int interrupt_request);
-    /** @cpu_exec_halt: Callback for handling halt in cpu_exec */

include/hw/core/tcg-cpu-ops.h struct TCGCPUOps { · line 122

Quoted 10 lines
include/hw/core/tcg-cpu-ops.h +9 -1
-    void (*cpu_exec_halt)(CPUState *cpu);
+    /**
+     * @cpu_exec_halt: Callback for handling halt in cpu_exec.
+     *
+     * Return true to indicate that the CPU should now leave halt, false
+     * if it should remain in the halted state.
+     * If this method is not provided, the default is to leave halt
+     * if cpu_has_work() returns true.
+     */
+    bool (*cpu_exec_halt)(CPUState *cpu);

Would it be too much to rename the method to cpu_exec_leave_halt() to make it clearer on use the sense of the return value?

Quoted 51 lines
include/hw/core/tcg-cpu-ops.h +0 -0
     /**
      * @tlb_fill: Handle a softmmu tlb miss
      *
target/i386/tcg/helper-tcg.h +1 -1
index effc2c1c984..85957943bf3 100644
@@ -39,7 +39,7 @@ QEMU_BUILD_BUG_ON(TCG_PHYS_ADDR_BITS > TARGET_PHYS_ADDR_SPACE_BITS);
  */
 void x86_cpu_do_interrupt(CPUState *cpu);
 #ifndef CONFIG_USER_ONLY
-void x86_cpu_exec_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);
 #endif
accel/tcg/cpu-exec.c +5 -2
index 5c70748060a..550f93b19ce 100644
@@ -669,11 +669,14 @@ static inline bool cpu_handle_halt(CPUState *cpu)
 #ifndef CONFIG_USER_ONLY
     if (cpu->halted) {
         const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
+        bool leave_halt;
 
         if (tcg_ops->cpu_exec_halt) {
-            tcg_ops->cpu_exec_halt(cpu);
+            leave_halt = tcg_ops->cpu_exec_halt(cpu);
+        } else {
+            leave_halt = cpu_has_work(cpu);
         }
-        if (!cpu_has_work(cpu)) {
+        if (!leave_halt) {
             return true;
         }
 
target/i386/tcg/sysemu/seg_helper.c +1 -1
index 2db8083748e..9ba94deb3aa 100644
@@ -128,7 +128,7 @@ void x86_cpu_do_interrupt(CPUState *cs)
     }
 }
 
-void x86_cpu_exec_halt(CPUState *cpu)
+bool x86_cpu_exec_halt(CPUState *cpu)
 {
     if (cpu->interrupt_request & CPU_INTERRUPT_POLL) {
         X86CPU *x86_cpu = X86_CPU(cpu);

target/i386/tcg/sysemu/seg_helper.c void x86_cpu_exec_halt(CPUState *cpu) · line 141

Quoted 4 lines
target/i386/tcg/sysemu/seg_helper.c +1 -0
@@ -138,6 +138,7 @@ void x86_cpu_exec_halt(CPUState *cpu)
         cpu_reset_interrupt(cpu, CPU_INTERRUPT_POLL);
         bql_unlock();
     }
+    return cpu_has_work(cpu);

The x86 version is essentially being called for side effects. Do we want to document this usage in the method?

Quoted 3 lines
target/i386/tcg/sysemu/seg_helper.c +0 -0
 }
 
 bool x86_need_replay_interrupt(int interrupt_request)
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#4 lore raw

Re: [PATCH 1/2] accel/tcg: Make TCGCPUOps::cpu_exec_halt return bool for whether to halt

Quoted 45 lines · Alex Bennée
Quoted 42 lines · Peter Maydell

The TCGCPUOps::cpu_exec_halt method is called from cpu_handle_halt() when the CPU is halted, so that a target CPU emulation can do anything target-specific it needs to do. (At the moment we only use this on i386.)

The current specification of the method doesn't allow the target specific code to do something different if the CPU is about to come out of the halt state, because cpu_handle_halt() only determines this after the method has returned. (If the method called cpu_has_work() itself this would introduce a potential race if an interrupt arrived between the target's method implementation checking and cpu_handle_halt() repeating the check.)

Change the definition of the method so that it returns a bool to tell cpu_handle_halt() whether to stay in halt or not.

We will want this for the Arm target, where FEAT_WFxT wants to do some work only for the case where the CPU is in halt but about to leave it.

---
 include/hw/core/tcg-cpu-ops.h       | 11 +++++++++--
 target/i386/tcg/helper-tcg.h        |  2 +-
 accel/tcg/cpu-exec.c                |  7 +++++--
 target/i386/tcg/sysemu/seg_helper.c |  3 ++-
 4 files changed, 17 insertions(+), 6 deletions(-)
include/hw/core/tcg-cpu-ops.h +4 -2
index dc1f16a9777..f3ac76e6f6d 100644
@@ -111,8 +111,15 @@ struct TCGCPUOps {
     void (*do_interrupt)(CPUState *cpu);
     /** @cpu_exec_interrupt: Callback for processing interrupts in cpu_exec */
     bool (*cpu_exec_interrupt)(CPUState *cpu, int interrupt_request);
-    /** @cpu_exec_halt: Callback for handling halt in cpu_exec */
-    void (*cpu_exec_halt)(CPUState *cpu);
+    /**
+     * @cpu_exec_halt: Callback for handling halt in cpu_exec.
+     *
+     * Return true to indicate that the CPU should now leave halt, false
Quoted 8 lines

include/hw/core/tcg-cpu-ops.h struct TCGCPUOps { · line 122

Quoted 5 lines
include/hw/core/tcg-cpu-ops.h +5 -0
+     * if it should remain in the halted state.
+     * If this method is not provided, the default is to leave halt
+     * if cpu_has_work() returns true.
+     */
+    bool (*cpu_exec_halt)(CPUState *cpu);

Would it be too much to rename the method to cpu_exec_leave_halt() to make it clearer on use the sense of the return value?

We could, but that makes it sound like it's a method to say "should we leave halt?", which ...

Quoted 5 lines
Quoted 5 lines
include/hw/core/tcg-cpu-ops.h +1 -1
-void x86_cpu_exec_halt(CPUState *cpu)
+bool x86_cpu_exec_halt(CPUState *cpu)
 {
     if (cpu->interrupt_request & CPU_INTERRUPT_POLL) {
         X86CPU *x86_cpu = X86_CPU(cpu);
Quoted 8 lines

include/hw/core/tcg-cpu-ops.h void x86_cpu_exec_halt(CPUState *cpu) · line 141

Quoted 5 lines
include/hw/core/tcg-cpu-ops.h +1 -0
@@ -138,6 +138,7 @@ void x86_cpu_exec_halt(CPUState *cpu)
         cpu_reset_interrupt(cpu, CPU_INTERRUPT_POLL);
         bql_unlock();
     }
+    return cpu_has_work(cpu);

The x86 version is essentially being called for side effects. Do we want to document this usage in the method?

...is not how the x86 target is using it, as you note.

thanks
-- PMM

#5 lore raw

Re: [PATCH 1/2] accel/tcg: Make TCGCPUOps::cpu_exec_halt return bool for whether to halt

In reply to Alex Bennée
Quoted 2 lines · Alex Bennée

The x86 version is essentially being called for side effects. Do we want to document this usage in the method?

I plan to take these two patches into target-arm.next, with a slightly beefed up doc comment:

/**
* @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.
*
* If this method is not provided, the default is to do nothing, and
* to leave halt if cpu_has_work() returns true.
*/

which hopefully makes it a little clearer to the reader that "do stuff you need to do when in halt" is the primary purpose of the method.

I'll do the follow-on tidyup rth suggests once these have made it into git.

thanks
-- PMM

Reviewed-by on v1 1/2
#6 lore raw

Re: [PATCH 1/2] accel/tcg: Make TCGCPUOps::cpu_exec_halt return bool for whether to halt

In reply to Peter Maydell
Quoted 20 lines · Peter Maydell

The TCGCPUOps::cpu_exec_halt method is called from cpu_handle_halt() when the CPU is halted, so that a target CPU emulation can do anything target-specific it needs to do. (At the moment we only use this on i386.)

The current specification of the method doesn't allow the target specific code to do something different if the CPU is about to come out of the halt state, because cpu_handle_halt() only determines this after the method has returned. (If the method called cpu_has_work() itself this would introduce a potential race if an interrupt arrived between the target's method implementation checking and cpu_handle_halt() repeating the check.)

Change the definition of the method so that it returns a bool to tell cpu_handle_halt() whether to stay in halt or not.

We will want this for the Arm target, where FEAT_WFxT wants to do some work only for the case where the CPU is in halt but about to leave it.

Quoted 7 lines
---
  include/hw/core/tcg-cpu-ops.h       | 11 +++++++++--
  target/i386/tcg/helper-tcg.h        |  2 +-
  accel/tcg/cpu-exec.c                |  7 +++++--
  target/i386/tcg/sysemu/seg_helper.c |  3 ++-
  4 files changed, 17 insertions(+), 6 deletions(-)

I like Alex's suggested rename.

Quoted 8 lines
accel/tcg/cpu-exec.c +1 -0
@@ -669,11 +669,14 @@ static inline bool cpu_handle_halt(CPUState *cpu)
  #ifndef CONFIG_USER_ONLY
      if (cpu->halted) {
          const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;
+        bool leave_halt;
 

accel/tcg/cpu-exec.c static inline bool cpu_handle_halt(CPUState *cpu) · line 681

Quoted 10 lines
accel/tcg/cpu-exec.c +4 -2
          if (tcg_ops->cpu_exec_halt) {
-            tcg_ops->cpu_exec_halt(cpu);
+            leave_halt = tcg_ops->cpu_exec_halt(cpu);
+        } else {
+            leave_halt = cpu_has_work(cpu);
          }
-        if (!cpu_has_work(cpu)) {
+        if (!leave_halt) {
              return true;
          }

As a followup, I would also suggest making implementation of the hook mandatory.
We already require the has_work hook to be set; it would simply be a matter of copying the function pointer to the second slot.

Also, the assert in cpu_has_work could be moved to startup, as Phil has started to do with some of the other hooks.

r~

#7 lore raw

PATCH 2/2 target/arm: Implement FEAT WFxT and enable for '-cpu max'

In reply to Peter Maydell

FEAT_WFxT introduces new instructions WFIT and WFET, which are like the existing WFI and WFE but allow the guest to pass a timeout value in a register. The instructions will wait for an interrupt/event as usual, but will also stop waiting when the value of CNTVCT_EL0 is greater than or equal to the specified timeout value.

We implement WFIT by setting up a timer to expire at the right point; when the timer expires it sets the EXITTB interrupt, which will cause the CPU to leave the halted state. If we come out of halt for some other reason, we unset the pending timer.

We implement WFET as a nop, which is architecturally permitted and matches the way we currently make WFE a nop.

---
 docs/system/arm/emulation.rst  |  1 +
 target/arm/cpu-features.h      |  5 ++++
 target/arm/cpu.h               |  3 ++
 target/arm/helper.h            |  1 +
 target/arm/internals.h         |  8 +++++
 target/arm/tcg/a64.decode      |  4 +++
 target/arm/cpu.c               | 38 ++++++++++++++++++++++++
 target/arm/helper.c            |  4 +--
 target/arm/machine.c           | 20 +++++++++++++
 target/arm/tcg/cpu64.c         |  1 +
 target/arm/tcg/op_helper.c     | 54 ++++++++++++++++++++++++++++++++++
 target/arm/tcg/translate-a64.c | 41 ++++++++++++++++++++++++++
 12 files changed, 178 insertions(+), 2 deletions(-)
docs/system/arm/emulation.rst +1 -0
index a9ae7ede9fc..d283c985d14 100644
@@ -108,6 +108,7 @@ the following architecture extensions:
 - FEAT_UAO (Unprivileged Access Override control)
 - FEAT_VHE (Virtualization Host Extensions)
 - FEAT_VMID16 (16-bit VMID)
+- FEAT_WFxT (WFE and WFI instructions with timeout)
 - FEAT_XNX (Translation table stage 2 Unprivileged Execute-never)
 - SVE (The Scalable Vector Extension)
 - SVE2 (The Scalable Vector Extension v2)
target/arm/cpu-features.h +5 -0
index b300d0446d8..c59ca104fe1 100644
@@ -571,6 +571,11 @@ static inline bool isar_feature_aa64_i8mm(const ARMISARegisters *id)
     return FIELD_EX64(id->id_aa64isar1, ID_AA64ISAR1, I8MM) != 0;
 }
 
+static inline bool isar_feature_aa64_wfxt(const ARMISARegisters *id)
+{
+    return FIELD_EX64(id->id_aa64isar2, ID_AA64ISAR2, WFXT) >= 2;
+}
+
 static inline bool isar_feature_aa64_hbc(const ARMISARegisters *id)
 {
     return FIELD_EX64(id->id_aa64isar2, ID_AA64ISAR2, BC) != 0;
target/arm/cpu.h +3 -0
index 97997dbd087..e8e6024fe30 100644
@@ -868,6 +868,9 @@ struct ArchCPU {
      * pmu_op_finish() - it does not need other handling during migration
      */
     QEMUTimer *pmu_timer;
+    /* Timer used for WFxT timeouts */
+    QEMUTimer *wfxt_timer;
+
     /* GPIO outputs for generic timer */
     qemu_irq gt_timer_outputs[NUM_GTIMERS];
     /* GPIO output for GICv3 maintenance interrupt signal */
target/arm/helper.h +1 -0
index 2b027333053..a85de78c8fc 100644
@@ -53,6 +53,7 @@ DEF_HELPER_2(exception_pc_alignment, noreturn, env, tl)
 DEF_HELPER_1(setend, void, env)
 DEF_HELPER_2(wfi, void, env, i32)
 DEF_HELPER_1(wfe, void, env)
+DEF_HELPER_2(wfit, void, env, i64)
 DEF_HELPER_1(yield, void, env)
 DEF_HELPER_1(pre_hvc, void, env)
 DEF_HELPER_2(pre_smc, void, env, i32)
target/arm/internals.h +8 -0
index b53f5e8ff2a..bd32883890d 100644
@@ -1760,4 +1760,12 @@ bool check_watchpoint_in_range(int i, target_ulong addr);
 CPUWatchpoint *find_hw_watchpoint(CPUState *cpu, target_ulong addr);
 int insert_hw_watchpoint(target_ulong addr, target_ulong len, int type);
 int delete_hw_watchpoint(target_ulong addr, target_ulong len, int type);
+
+/* Return the current value of the system counter in ticks */
+uint64_t gt_get_countervalue(CPUARMState *env);
+/*
+ * Return the currently applicable offset between the system counter
+ * and CNTVCT_EL0 (this will be either 0 or the value of CNTVOFF_EL2).
+ */
+uint64_t gt_virt_cnt_offset(CPUARMState *env);
 #endif
target/arm/tcg/a64.decode +4 -0
index 0e7656fd158..7aea5cba5ea 100644
@@ -183,6 +183,10 @@ ERETA           1101011 0100 11111 00001 m:1 11111 11111 &reta  # ERETAA, ERETAB
   NOP           1101 0101 0000 0011 0010 ---- --- 11111
 }
 
+# System instructions with register argument
+WFET            1101 0101 0000 0011 0001 0000 000 rd:5
+WFIT            1101 0101 0000 0011 0001 0000 001 rd:5
+
 # Barriers
 
 CLREX           1101 0101 0000 0011 0011 ---- 010 11111
target/arm/cpu.c +38 -0
index a152def2413..006092a6b12 100644
@@ -1132,6 +1132,33 @@ static bool arm_cpu_virtio_is_big_endian(CPUState *cs)
     return arm_cpu_data_is_big_endian(env);
 }
 
+static 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);
+        }
+    }
+    return leave_halt;
+}
+
+static void arm_wfxt_timer_cb(void *opaque)
+{
+    ARMCPU *cpu = opaque;
+    CPUState *cs = CPU(cpu);
+
+    /*
+     * We expect the CPU to be halted; this will cause arm_cpu_is_work()
+     * to return true (so we will come out of halt even with no other
+     * pending interrupt), and the TCG accelerator's cpu_exec_interrupt()
+     * function auto-clears the CPU_INTERRUPT_EXITTB flag for us.
+     */
+    cpu_interrupt(cs, CPU_INTERRUPT_EXITTB);
+}
 #endif
 
 static void arm_disas_set_info(CPUState *cpu, disassemble_info *info)
@@ -1874,6 +1901,9 @@ static void arm_cpu_finalizefn(Object *obj)
     if (cpu->pmu_timer) {
         timer_free(cpu->pmu_timer);
     }
+    if (cpu->wfxt_timer) {
+        timer_free(cpu->wfxt_timer);
+    }
 #endif
 }
 
@@ -2357,6 +2387,13 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
 #endif
     }
 
+#ifndef CONFIG_USER_ONLY
+    if (tcg_enabled() && cpu_isar_feature(aa64_wfxt, cpu)) {
+        cpu->wfxt_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
+                                       arm_wfxt_timer_cb, cpu);
+    }
+#endif
+
     if (tcg_enabled()) {
         /*
          * Don't report some architectural features in the ID registers
@@ -2611,6 +2648,7 @@ static const TCGCPUOps arm_tcg_ops = {
 #else
     .tlb_fill = arm_cpu_tlb_fill,
     .cpu_exec_interrupt = arm_cpu_exec_interrupt,
+    .cpu_exec_halt = arm_cpu_exec_halt,
     .do_interrupt = arm_cpu_do_interrupt,
     .do_transaction_failed = arm_cpu_do_transaction_failed,
     .do_unaligned_access = arm_cpu_do_unaligned_access,
target/arm/helper.c +2 -2
index 6b224826fbb..f7bb2d6ba8a 100644
@@ -2658,7 +2658,7 @@ static CPAccessResult gt_stimer_access(CPUARMState *env,
     }
 }
 
-static uint64_t gt_get_countervalue(CPUARMState *env)
+uint64_t gt_get_countervalue(CPUARMState *env)
 {
     ARMCPU *cpu = env_archcpu(env);
 
@@ -2793,7 +2793,7 @@ static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
     return gt_get_countervalue(env) - gt_phys_cnt_offset(env);
 }
 
-static uint64_t gt_virt_cnt_offset(CPUARMState *env)
+uint64_t gt_virt_cnt_offset(CPUARMState *env)
 {
     uint64_t hcr;
 
target/arm/machine.c +20 -0
index b2b39b24755..0a722ca7e75 100644
@@ -242,6 +242,25 @@ static const VMStateDescription vmstate_irq_line_state = {
     }
 };
 
+static bool wfxt_timer_needed(void *opaque)
+{
+    ARMCPU *cpu = opaque;
+
+    /* We'll only have the timer object if FEAT_WFxT is implemented */
+    return cpu->wfxt_timer;
+}
+
+static const VMStateDescription vmstate_wfxt_timer = {
+    .name = "cpu/wfxt-timer",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .needed = wfxt_timer_needed,
+    .fields = (const VMStateField[]) {
+        VMSTATE_TIMER_PTR(wfxt_timer, ARMCPU),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
 static bool m_needed(void *opaque)
 {
     ARMCPU *cpu = opaque;
@@ -957,6 +976,7 @@ const VMStateDescription vmstate_arm_cpu = {
 #endif
         &vmstate_serror,
         &vmstate_irq_line_state,
+        &vmstate_wfxt_timer,
         NULL
     }
 };
target/arm/tcg/cpu64.c +1 -0
index 62c4663512b..47ea51d3aa5 100644
@@ -1150,6 +1150,7 @@ void aarch64_max_tcg_initfn(Object *obj)
     t = cpu->isar.id_aa64isar2;
     t = FIELD_DP64(t, ID_AA64ISAR2, MOPS, 1);     /* FEAT_MOPS */
     t = FIELD_DP64(t, ID_AA64ISAR2, BC, 1);      /* FEAT_HBC */
+    t = FIELD_DP64(t, ID_AA64ISAR2, WFXT, 2);     /* FEAT_WFxT */
     cpu->isar.id_aa64isar2 = t;
 
     t = cpu->isar.id_aa64pfr0;
target/arm/tcg/op_helper.c +54 -0

60 lines not on this page · raw message

target/arm/tcg/translate-a64.c +41 -0

47 lines not on this page · raw message

Signature · 1 line
2.34.1
Reviewed-by on v1 2/2
#8 lore raw

Re: [PATCH 2/2] target/arm: Implement FEAT WFxT and enable for '-cpu max'

target/arm/tcg/op_helper.c void HELPER(wfi)(CPUARMState *env, uint32_t insn_len) · line 459

Quoted 12 lines · Peter Maydell
target/arm/tcg/op_helper.c +12 -0
+    if (uadd64_overflow(timeout, offset, &nexttick)) {
+        nexttick = UINT64_MAX;
+    }
+    if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
+        /*
+         * If the timeout is too long for the signed 64-bit range
+         * of a QEMUTimer, let it expire early.
+         */
+        timer_mod_ns(cpu->wfxt_timer, INT64_MAX);
+    } else {
+        timer_mod(cpu->wfxt_timer, nexttick);
+    }

The use of both UINT64_MAX and INT64_MAX is confusing. Perhaps

    if (uadd64_overflow(timeout, offset, &nexttick) ||
        nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
        nexttick = INT64_MAX;
    }
    timer_mod(cpu->wfxt_timer, nexttick);

Anyway,

r~

#9 lore raw

Re: [PATCH 2/2] target/arm: Implement FEAT WFxT and enable for '-cpu max'

Quoted 15 lines · Richard Henderson

target/arm/tcg/op_helper.c void HELPER(wfi)(CPUARMState *env, uint32_t insn_len) · line 459

Quoted 12 lines · Peter Maydell
target/arm/tcg/op_helper.c +12 -0
+    if (uadd64_overflow(timeout, offset, &nexttick)) {
+        nexttick = UINT64_MAX;
+    }
+    if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
+        /*
+         * If the timeout is too long for the signed 64-bit range
+         * of a QEMUTimer, let it expire early.
+         */
+        timer_mod_ns(cpu->wfxt_timer, INT64_MAX);
+    } else {
+        timer_mod(cpu->wfxt_timer, nexttick);
+    }
Quoted 7 lines

The use of both UINT64_MAX and INT64_MAX is confusing. Perhaps

     if (uadd64_overflow(timeout, offset, &nexttick) ||
         nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
         nexttick = INT64_MAX;
     }
     timer_mod(cpu->wfxt_timer, nexttick);

I'm following here the pattern of the logic in gt_recalc_timer() (which could admittedly also be considered confusing...).

Also note that timer_mod_ns() and timer_mod() aren't the same thing. The latter calls timer_mod_ns() on its argument multiplied by ts->scale, so if you pass it INT64_MAX the multiply is liable to overflow.

thanks
-- PMM

#10 lore raw

Re: [PATCH 2/2] target/arm: Implement FEAT WFxT and enable for '-cpu max'

In reply to Peter Maydell
Quoted 30 lines · Peter Maydell

FEAT_WFxT introduces new instructions WFIT and WFET, which are like the existing WFI and WFE but allow the guest to pass a timeout value in a register. The instructions will wait for an interrupt/event as usual, but will also stop waiting when the value of CNTVCT_EL0 is greater than or equal to the specified timeout value.

We implement WFIT by setting up a timer to expire at the right point; when the timer expires it sets the EXITTB interrupt, which will cause the CPU to leave the halted state. If we come out of halt for some other reason, we unset the pending timer.

We implement WFET as a nop, which is architecturally permitted and matches the way we currently make WFE a nop.

---

target/arm/cpu.c +3 -0
index a152def2413..006092a6b12 100644
@@ -1132,6 +1132,33 @@ static bool arm_cpu_virtio_is_big_endian(CPUState *cs)
     return arm_cpu_data_is_big_endian(env);
 }
 
+static bool arm_cpu_exec_halt(CPUState *cs)
+{
+    bool leave_halt = cpu_has_work(cs);

target/arm/cpu.c static bool arm_cpu_virtio_is_big_endian(CPUState *cs) · line 1147

Quoted 10 lines
target/arm/cpu.c +10 -0
+
+    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);
+        }
+    }
+    return leave_halt;
+}

I noticed in my pre-pullreq testing that this function needs an #ifdef CONFIG_TCG around it, since otherwise the compiler complains that it is defined but never used if we're building only-Xen or only-KVM. I'll add that when I put this in target-arm.next, rather than respinning.

thanks
-- PMM