Skip to contentSkip to conversation

target/arm: Introduce host_cpu_feature_supported() API

RFC PATCH Superseded Philippe Mathieu-Daudé 2 participants 29 messages Aug 11, 2025 – Aug 13, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover target/arm: Introduce host_cpu_feature_supported() API

Hi,

Mohamed and myself are working on adding nested virtualization support to HVF Aarch64. Mohamed approach leverages the latest hardware features of the Apple M3+ Silicon chips [1], while mine falls back to emulation [2] when features are not available, as it happens with the M1 and M2 chipsets.

We want to support both methods long term, as they solve different use cases. Therefore I'm looking for a common API for methods added in both series.

In this series we propose the host_cpu_feature_supported() method to check if a feature is supported by the host, allowing fall back to TCG. KVM uses are converted, and an example -- while not really usable without other patch applied -- is provided for HVF.

Does this look reasonable enough to pursue in that direction?

Thanks,

Phil.

[1] https://lore.kernel.org/qemu-devel/[email protected]/
[2] https://lore.kernel.org/qemu-devel/[email protected]/

Mohamed Mediouni (2):
target/arm: Factor hvf_psci_get_target_el() out
target/arm/hvf: Sync registers used at EL2

Philippe Mathieu-Daudé (9):
accel/system: Introduce hwaccel_enabled() helper target/arm: Use generic hwaccel_enabled() to check 'host' cpu type target/arm: Restrict PMU to system mode
target/arm: Introduce arm_hw_accel_cpu_feature_supported() target/arm: Introduce host_cpu_feature_supported() target/arm: Replace kvm_arm_pmu_supported by
host_cpu_feature_supported
target/arm: Replace kvm_arm_el2_supported by
host_cpu_feature_supported
target/arm/hvf: Consider EL2 acceleration for Silicon M3+ chipsets target/arm/hvf: Allow EL2/EL3 emulation on Silicon M1 / M2

 include/system/hw_accel.h | 13 +++++++
 target/arm/cpu.h          | 23 +++++++++++++
 target/arm/kvm_arm.h      | 24 -------------
 hw/arm/virt.c             |  8 +----
 target/arm/arm-qmp-cmds.c |  5 +--
 target/arm/arm_hw_accel.c | 27 +++++++++++++++
 target/arm/cpu.c          | 14 ++++----
 target/arm/cpu64.c        | 11 +++---
 target/arm/hvf/hvf.c      | 71 +++++++++++++++++++++++++++++++++++++--
 target/arm/kvm-stub.c     | 10 ------
 target/arm/kvm.c          | 33 +++++++++++++-----
 target/arm/meson.build    |  2 +-
 12 files changed, 176 insertions(+), 65 deletions(-)
 create mode 100644 target/arm/arm_hw_accel.c
Signature · 1 line
2.49.0
#1 lore raw

RFC PATCH 1/11 accel/system: Introduce hwaccel_enabled() helper

hwaccel_enabled() return whether any hardware accelerator is enabled.

---
 include/system/hw_accel.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
include/system/hw_accel.h +13 -0
index fa9228d5d2d..49556b026e0 100644
@@ -39,4 +39,17 @@ void cpu_synchronize_pre_loadvm(CPUState *cpu);
 void cpu_synchronize_post_reset(CPUState *cpu);
 void cpu_synchronize_post_init(CPUState *cpu);
 
+/**
+ * hwaccel_enabled:
+ *
+ * Returns: %true if a hardware accelerator is enabled, %false otherwise.
+ */
+static inline bool hwaccel_enabled(void)
+{
+    return hvf_enabled()
+        || kvm_enabled()
+        || nvmm_enabled()
+        || whpx_enabled();
+}
+
 #endif /* QEMU_HW_ACCEL_H */
Signature · 1 line
2.49.0
#2 lore raw

RFC PATCH 2/11 target/arm: Use generic hwaccel_enabled() to check 'host' cpu type

In reply to Philippe Mathieu-Daudé

We should be able to use the 'host' CPU with any hardware accelerator.

---
 target/arm/arm-qmp-cmds.c |  5 +++--
 target/arm/cpu.c          |  5 +++--
 target/arm/cpu64.c        | 11 ++++++-----
 3 files changed, 12 insertions(+), 9 deletions(-)
target/arm/arm-qmp-cmds.c +3 -2
index d292c974c44..1142e28cb76 100644
@@ -31,6 +31,7 @@
 #include "qapi/qapi-commands-misc-arm.h"
 #include "qobject/qdict.h"
 #include "qom/qom-qobject.h"
+#include "system/hw_accel.h"
 #include "cpu.h"
 
 static GICCapability *gic_cap_new(int version)
@@ -117,8 +118,8 @@ CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
         return NULL;
     }
 
-    if (!kvm_enabled() && !strcmp(model->name, "host")) {
-        error_setg(errp, "The CPU type '%s' requires KVM", model->name);
+    if (!hwaccel_enabled() && !strcmp(model->name, "host")) {
+        error_setg(errp, "The CPU type 'host' requires hardware accelerator");
         return NULL;
     }
 
target/arm/cpu.c +3 -2
index e2b2337399c..d9a8f62934d 100644
@@ -1984,8 +1984,9 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
      * this is the first point where we can report it.
      */
     if (cpu->host_cpu_probe_failed) {
-        if (!kvm_enabled() && !hvf_enabled()) {
-            error_setg(errp, "The 'host' CPU type can only be used with KVM or HVF");
+        if (!hwaccel_enabled()) {
+            error_setg(errp, "The 'host' CPU type can only be used with "
+                             "hardware accelator such KVM/HVF");
         } else {
             error_setg(errp, "Failed to retrieve host CPU features");
         }
target/arm/cpu64.c +6 -5
index 26cf7e6dfa2..034bbc504cd 100644
@@ -26,6 +26,7 @@
 #include "qemu/units.h"
 #include "system/kvm.h"
 #include "system/hvf.h"
+#include "system/hw_accel.h"
 #include "system/qtest.h"
 #include "system/tcg.h"
 #include "kvm_arm.h"
@@ -522,7 +523,7 @@ void arm_cpu_pauth_finalize(ARMCPU *cpu, Error **errp)
     isar2 = FIELD_DP64(isar2, ID_AA64ISAR2, APA3, 0);
     isar2 = FIELD_DP64(isar2, ID_AA64ISAR2, GPA3, 0);
 
-    if (kvm_enabled() || hvf_enabled()) {
+    if (hwaccel_enabled()) {
         /*
          * Exit early if PAuth is enabled and fall through to disable it.
          * The algorithm selection properties are not present.
@@ -599,10 +600,10 @@ void aarch64_add_pauth_properties(Object *obj)
 
     /* Default to PAUTH on, with the architected algorithm on TCG. */
     qdev_property_add_static(DEVICE(obj), &arm_cpu_pauth_property);
-    if (kvm_enabled() || hvf_enabled()) {
+    if (hwaccel_enabled()) {
         /*
          * Mirror PAuth support from the probed sysregs back into the
-         * property for KVM or hvf. Is it just a bit backward? Yes it is!
+         * property for HW accel. Is it just a bit backward? Yes it is!
          * Note that prop_pauth is true whether the host CPU supports the
          * architected QARMA5 algorithm or the IMPDEF one. We don't
          * provide the separate pauth-impdef property for KVM or hvf,
@@ -780,8 +781,8 @@ static void aarch64_host_initfn(Object *obj)
 
 static void aarch64_max_initfn(Object *obj)
 {
-    if (kvm_enabled() || hvf_enabled()) {
-        /* With KVM or HVF, '-cpu max' is identical to '-cpu host' */
+    if (hwaccel_enabled()) {
+        /* When hardware acceleration enabled, '-cpu max' is identical to '-cpu host' */
         aarch64_host_initfn(obj);
         return;
     }
Signature · 1 line
2.49.0
#3 lore raw

RFC PATCH 3/11 target/arm: Restrict PMU to system mode

In reply to Philippe Mathieu-Daudé
---
 target/arm/cpu.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
target/arm/cpu.c +3 -2
index d9a8f62934d..1dc2a8330d8 100644
@@ -1551,7 +1551,6 @@ static const Property arm_cpu_pmsav7_dregion_property =
             DEFINE_PROP_UNSIGNED_NODEFAULT("pmsav7-dregion", ARMCPU,
                                            pmsav7_dregion,
                                            qdev_prop_uint32, uint32_t);
-#endif
 
 static bool arm_get_pmu(Object *obj, Error **errp)
 {
@@ -1576,6 +1575,8 @@ static void arm_set_pmu(Object *obj, bool value, Error **errp)
     cpu->has_pmu = value;
 }
 
+#endif
+
 static bool aarch64_cpu_get_aarch64(Object *obj, Error **errp)
 {
     ARMCPU *cpu = ARM_CPU(obj);
@@ -1771,12 +1772,12 @@ static void arm_cpu_post_init(Object *obj)
     if (arm_feature(&cpu->env, ARM_FEATURE_EL2)) {
         qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el2_property);
     }
-#endif
 
     if (arm_feature(&cpu->env, ARM_FEATURE_PMU)) {
         cpu->has_pmu = true;
         object_property_add_bool(obj, "pmu", arm_get_pmu, arm_set_pmu);
     }
+#endif
 
     /*
      * Allow user to turn off VFP and Neon support, but only for TCG --
Signature · 1 line
2.49.0
Reviewed-by on v1 3/11
#4 lore raw

Re: [RFC PATCH 03/11] target/arm: Restrict PMU to system mode

Quoted 4 lines · Philippe Mathieu-Daudé
---
  target/arm/cpu.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)

r~

Quoted 36 lines
target/arm/cpu.c +3 -2
index d9a8f62934d..1dc2a8330d8 100644
@@ -1551,7 +1551,6 @@ static const Property arm_cpu_pmsav7_dregion_property =
              DEFINE_PROP_UNSIGNED_NODEFAULT("pmsav7-dregion", ARMCPU,
                                             pmsav7_dregion,
                                             qdev_prop_uint32, uint32_t);
-#endif
 
  static bool arm_get_pmu(Object *obj, Error **errp)
  {
@@ -1576,6 +1575,8 @@ static void arm_set_pmu(Object *obj, bool value, Error **errp)
      cpu->has_pmu = value;
  }
 
+#endif
+
  static bool aarch64_cpu_get_aarch64(Object *obj, Error **errp)
  {
      ARMCPU *cpu = ARM_CPU(obj);
@@ -1771,12 +1772,12 @@ static void arm_cpu_post_init(Object *obj)
      if (arm_feature(&cpu->env, ARM_FEATURE_EL2)) {
          qdev_property_add_static(DEVICE(obj), &arm_cpu_has_el2_property);
      }
-#endif
 
      if (arm_feature(&cpu->env, ARM_FEATURE_PMU)) {
          cpu->has_pmu = true;
          object_property_add_bool(obj, "pmu", arm_get_pmu, arm_set_pmu);
      }
+#endif
 
      /*
       * Allow user to turn off VFP and Neon support, but only for TCG --
#5 lore raw

RFC PATCH 4/11 target/arm: Factor hvf_psci_get_target_el() out

In reply to Philippe Mathieu-Daudé

From: Mohamed Mediouni <[email protected]>

Factor hvf_psci_get_target_el() out so it will be easier to allow switching to other EL later.

---
 target/arm/hvf/hvf.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
target/arm/hvf/hvf.c +5 -2
index 47b0cd3a351..81dc4df686d 100644
@@ -1107,6 +1107,10 @@ static void hvf_psci_cpu_off(ARMCPU *arm_cpu)
     assert(ret == QEMU_ARM_POWERCTL_RET_SUCCESS);
 }
 
+static int hvf_psci_get_target_el(void)
+{
+    return 1;
+}
 /*
  * Handle a PSCI call.
  *
@@ -1128,7 +1132,6 @@ static bool hvf_handle_psci_call(CPUState *cpu)
     CPUState *target_cpu_state;
     ARMCPU *target_cpu;
     target_ulong entry;
-    int target_el = 1;
     int32_t ret = 0;
 
     trace_hvf_psci_call(param[0], param[1], param[2], param[3],
@@ -1182,7 +1185,7 @@ static bool hvf_handle_psci_call(CPUState *cpu)
         entry = param[2];
         context_id = param[3];
         ret = arm_set_cpu_on(mpidr, entry, context_id,
-                             target_el, target_aarch64);
+                             hvf_psci_get_target_el(), target_aarch64);
         break;
     case QEMU_PSCI_0_1_FN_CPU_OFF:
     case QEMU_PSCI_0_2_FN_CPU_OFF:
Signature · 1 line
2.49.0
Reviewed-by on v1 4/11
#6 lore raw

Re: [RFC PATCH 04/11] target/arm: Factor hvf_psci_get_target_el() out

Quoted 10 lines · Philippe Mathieu-Daudé

From: Mohamed Mediouni <[email protected]>

Factor hvf_psci_get_target_el() out so it will be easier to allow switching to other EL later.

---
  target/arm/hvf/hvf.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)

r~

#7 lore raw

RFC PATCH 5/11 target/arm: Introduce arm_hw_accel_cpu_feature_supported()

In reply to Philippe Mathieu-Daudé

Introduce arm_hw_accel_cpu_feature_supported() helper, an accelerator implementation to return whether a ARM feature is supported by host hardware. Allow optional fallback on emulation.

---
 target/arm/cpu.h     | 12 ++++++++++++
 target/arm/hvf/hvf.c | 20 ++++++++++++++++++++
 target/arm/kvm.c     | 22 ++++++++++++++++++++++
 3 files changed, 54 insertions(+)
target/arm/cpu.h +12 -0
index dc9b6dce4c9..5136c4caabf 100644
@@ -2507,6 +2507,18 @@ static inline ARMSecuritySpace arm_secure_to_space(bool secure)
 }
 
 #if !defined(CONFIG_USER_ONLY)
+
+/**
+ * arm_hw_accel_cpu_feature_supported:
+ * @feat: Feature to test for support
+ * @can_emulate: Whether Allow to fall back to emulation if @feat is not
+ *               supported by hardware accelerator
+ *
+ * Hardware accelerator implementation of cpu_feature_supported().
+ */
+bool arm_hw_accel_cpu_feature_supported(enum arm_features feat,
+                                        bool can_emulate);
+
 /**
  * arm_security_space_below_el3:
  * @env: cpu context
target/arm/hvf/hvf.c +20 -0
index 81dc4df686d..5174973991f 100644
@@ -964,6 +964,26 @@ uint32_t hvf_arm_get_max_ipa_bit_size(void)
     return round_down_to_parange_bit_size(max_ipa_size);
 }
 
+bool arm_hw_accel_cpu_feature_supported(enum arm_features feat, bool can_emulate)
+{
+    if (!hvf_enabled()) {
+        return false;
+    }
+    switch (feat) {
+    case ARM_FEATURE_V8:
+    case ARM_FEATURE_NEON:
+    case ARM_FEATURE_AARCH64:
+    case ARM_FEATURE_PMU:
+    case ARM_FEATURE_GENERIC_TIMER:
+        return true;
+    case ARM_FEATURE_EL2:
+    case ARM_FEATURE_EL3:
+        return false;
+    default:
+        g_assert_not_reached();
+    }
+}
+
 void hvf_arm_set_cpu_features_from_host(ARMCPU *cpu)
 {
     if (!arm_host_cpu_features.dtb_compatible) {
target/arm/kvm.c +22 -0
index 66723448554..82853e68d8d 100644
@@ -1771,6 +1771,28 @@ void kvm_arm_steal_time_finalize(ARMCPU *cpu, Error **errp)
     }
 }
 
+bool arm_hw_accel_cpu_feature_supported(enum arm_features feat, bool can_emulate)
+{
+    if (!kvm_enabled()) {
+        return false;
+    }
+    switch (feat) {
+    case ARM_FEATURE_V8:
+    case ARM_FEATURE_NEON:
+    case ARM_FEATURE_AARCH64:
+    case ARM_FEATURE_GENERIC_TIMER:
+        return true;
+    case ARM_FEATURE_PMU:
+        return kvm_arm_pmu_supported();
+    case ARM_FEATURE_EL2:
+        return kvm_arm_el2_supported();
+    case ARM_FEATURE_EL3:
+        return false;
+    default:
+        g_assert_not_reached();
+    }
+}
+
 bool kvm_arm_aarch32_supported(void)
 {
     return kvm_check_extension(kvm_state, KVM_CAP_ARM_EL1_32BIT);
Signature · 1 line
2.49.0
#8 lore raw

Re: [RFC PATCH 05/11] target/arm: Introduce arm_hw_accel_cpu_feature_supported()

Quoted 21 lines · Philippe Mathieu-Daudé

Introduce arm_hw_accel_cpu_feature_supported() helper, an accelerator implementation to return whether a ARM feature is supported by host hardware. Allow optional fallback on emulation.

---
  target/arm/cpu.h     | 12 ++++++++++++
  target/arm/hvf/hvf.c | 20 ++++++++++++++++++++
  target/arm/kvm.c     | 22 ++++++++++++++++++++++
  3 files changed, 54 insertions(+)
target/arm/cpu.h +1 -0
index dc9b6dce4c9..5136c4caabf 100644
@@ -2507,6 +2507,18 @@ static inline ARMSecuritySpace arm_secure_to_space(bool secure)
  }
 
  #if !defined(CONFIG_USER_ONLY)
+

target/arm/cpu.h static inline ARMSecuritySpace arm_secure_to_space(bool secure) · line 2520

Quoted 10 lines
target/arm/cpu.h +10 -0
+/**
+ * arm_hw_accel_cpu_feature_supported:
+ * @feat: Feature to test for support
+ * @can_emulate: Whether Allow to fall back to emulation if @feat is not
+ *               supported by hardware accelerator
+ *
+ * Hardware accelerator implementation of cpu_feature_supported().
+ */
+bool arm_hw_accel_cpu_feature_supported(enum arm_features feat,
+                                        bool can_emulate);

internals.h.

I guess this is ok. If we ignore xen, which is a thing of its own, we don't currently have more than one accelerator per OS. That hasn't always been true for x86, where we had 2 or 3 at once for Windows.

r~

#9 lore raw

Re: [RFC PATCH 05/11] target/arm: Introduce arm_hw_accel_cpu_feature_supported()

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

Introduce arm_hw_accel_cpu_feature_supported() helper, an accelerator implementation to return whether a ARM feature is supported by host hardware. Allow optional fallback on emulation.

---
  target/arm/cpu.h     | 12 ++++++++++++ target/arm/hvf/hvf.c | 20 ++++++++++++++++++++ target/arm/kvm.c     | 22 ++++++++++++++++++++++

  3 files changed, 54 insertions(+)
target/arm/cpu.h +1 -0
index dc9b6dce4c9..5136c4caabf 100644
@@ -2507,6 +2507,18 @@ static inline ARMSecuritySpace 
 arm_secure_to_space(bool secure)
   }
   #if !defined(CONFIG_USER_ONLY)
+

target/arm/cpu.h static inline ARMSecuritySpace · line 2520

Quoted 10 lines
target/arm/cpu.h +10 -0
+/**
+ * arm_hw_accel_cpu_feature_supported:
+ * @feat: Feature to test for support
+ * @can_emulate: Whether Allow to fall back to emulation if @feat is not
+ *               supported by hardware accelerator
+ *
+ * Hardware accelerator implementation of cpu_feature_supported().
+ */
+bool arm_hw_accel_cpu_feature_supported(enum arm_features feat,
+                                        bool can_emulate);

internals.h.

Quoted 1 line

I guess this is ok.  If we ignore xen, which is a thing of its own, we

Does Xen manage vCPUs runtime?

Quoted 1 line

don't currently have more than one accelerator per OS.  That hasn't always been true for x86, where we had 2 or 3 at once for Windows.

I felt than adding as per-target per-accel methods would be over engineering at this point, but I can give it a try if preferred.

#10 lore raw

Re: [RFC PATCH 05/11] target/arm: Introduce arm_hw_accel_cpu_feature_supported()

In reply to Richard Henderson
Quoted 26 lines · Richard Henderson
Quoted 25 lines · Philippe Mathieu-Daudé

Introduce arm_hw_accel_cpu_feature_supported() helper, an accelerator implementation to return whether a ARM feature is supported by host hardware. Allow optional fallback on emulation.

---
  target/arm/cpu.h     | 12 ++++++++++++ target/arm/hvf/hvf.c | 20 ++++++++++++++++++++ target/arm/kvm.c     | 22 ++++++++++++++++++++++

  3 files changed, 54 insertions(+)
target/arm/cpu.h +5 -0
index dc9b6dce4c9..5136c4caabf 100644
@@ -2507,6 +2507,18 @@ static inline ARMSecuritySpace 
 arm_secure_to_space(bool secure)
   }
   #if !defined(CONFIG_USER_ONLY)
+
+/**
+ * arm_hw_accel_cpu_feature_supported:
+ * @feat: Feature to test for support
+ * @can_emulate: Whether Allow to fall back to emulation if @feat is not
Quoted 8 lines

target/arm/cpu.h static inline ARMSecuritySpace arm_secure_to_space(bool secure) · line 2520

Quoted 6 lines
target/arm/cpu.h +6 -0
+ *               supported by hardware accelerator
+ *
+ * Hardware accelerator implementation of cpu_feature_supported().
+ */
+bool arm_hw_accel_cpu_feature_supported(enum arm_features feat,
+                                        bool can_emulate);

internals.h.

OK, but since hw/intc/arm_gicv3_hvf.c calls host_cpu_feature_supported, the other one has to stay in "cpu.h" (see
https://lore.kernel.org/qemu-devel/[email protected]/).

#11 lore raw

RFC PATCH 6/11 target/arm: Introduce host_cpu_feature_supported()

In reply to Philippe Mathieu-Daudé

host_cpu_feature_supported() is the generic method which dispatch to the host accelerator implementation, taking care to cache supported features.

---
 target/arm/cpu.h          | 11 +++++++++++
 target/arm/arm_hw_accel.c | 27 +++++++++++++++++++++++++++
 target/arm/meson.build    |  2 +-
 3 files changed, 39 insertions(+), 1 deletion(-)
 create mode 100644 target/arm/arm_hw_accel.c
target/arm/cpu.h +11 -0
index 5136c4caabf..aff60cef6da 100644
@@ -2508,6 +2508,16 @@ static inline ARMSecuritySpace arm_secure_to_space(bool secure)
 
 #if !defined(CONFIG_USER_ONLY)
 
+/**
+ * host_cpu_feature_supported:
+ * @feat: Feature to test for support
+ * @can_emulate: Whether Allow to fall back to emulation if @feat is not
+ *               supported by hardware accelerator
+ *
+ * Hardware accelerator implementation of cpu_feature_supported().
+ */
+bool host_cpu_feature_supported(enum arm_features feature, bool can_emulate);
+
 /**
  * arm_hw_accel_cpu_feature_supported:
  * @feat: Feature to test for support
@@ -2515,6 +2525,7 @@ static inline ARMSecuritySpace arm_secure_to_space(bool secure)
  *               supported by hardware accelerator
  *
  * Hardware accelerator implementation of cpu_feature_supported().
+ * Common code should use the generic host_cpu_feature_supported() equivalent.
  */
 bool arm_hw_accel_cpu_feature_supported(enum arm_features feat,
                                         bool can_emulate);
target/arm/arm_hw_accel.c new file +27 -0
new file mode 100644
index 00000000000..3a8ff007599
@@ -0,0 +1,27 @@
+/*
+ * QEMU helpers for ARM hardware accelerators
+ *
+ *  Copyright (c) Linaro
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "cpu.h"
+
+bool host_cpu_feature_supported(enum arm_features feat, bool can_emulate)
+{
+#if defined(CONFIG_KVM) || defined(CONFIG_HVF)
+    static enum { F_UNKN, F_SUPP, F_UNSUPP } supported[64] = { };
+
+    assert(feat < ARRAY_SIZE(supported));
+    if (supported[feat] == F_UNKN) {
+        supported[feat] = arm_hw_accel_cpu_feature_supported(feat, can_emulate);
+    }
+    return supported[feat] == F_SUPP;
+#elif defined(CONFIG_TCG)
+    return can_emulate;
+#else
+#error
+#endif
+}
target/arm/meson.build +1 -1
index 07d9271aa4d..37718c85666 100644
@@ -11,7 +11,7 @@ arm_ss.add(when: 'TARGET_AARCH64', if_true: files(
 arm_system_ss = ss.source_set()
 arm_common_system_ss = ss.source_set()
 arm_system_ss.add(files(
-  'arm-qmp-cmds.c',
+  'arm-qmp-cmds.c', 'arm_hw_accel.c',
 ))
 arm_system_ss.add(when: 'CONFIG_KVM', if_true: files('hyp_gdbstub.c', 'kvm.c'))
 arm_system_ss.add(when: 'CONFIG_HVF', if_true: files('hyp_gdbstub.c'))
Signature · 1 line
2.49.0
#12 lore raw

Re: [RFC PATCH 06/11] target/arm: Introduce host_cpu_feature_supported()

Quoted 6 lines · Philippe Mathieu-Daudé
target/arm/arm_hw_accel.c new file +6 -0
+bool host_cpu_feature_supported(enum arm_features feat, bool can_emulate)
+{
+#if defined(CONFIG_KVM) || defined(CONFIG_HVF)
+    static enum { F_UNKN, F_SUPP, F_UNSUPP } supported[64] = { };
+
+    assert(feat < ARRAY_SIZE(supported));

target/arm/arm_hw_accel.c line 27

Quoted 10 lines
target/arm/arm_hw_accel.c new file +10 -0
+    if (supported[feat] == F_UNKN) {
+        supported[feat] = arm_hw_accel_cpu_feature_supported(feat, can_emulate);
+    }
+    return supported[feat] == F_SUPP;
+#elif defined(CONFIG_TCG)
+    return can_emulate;
+#else
+#error
+#endif
+}

How much startup time is saved by this cache?
This feels like premature optimization.

r~

#13 lore raw

Re: [RFC PATCH 06/11] target/arm: Introduce host_cpu_feature_supported()

Quoted 14 lines · Richard Henderson
Quoted 13 lines · Philippe Mathieu-Daudé

+bool host_cpu_feature_supported(enum arm_features feat, bool can_emulate)

target/arm/arm_hw_accel.c new file +7 -0
+{
+#if defined(CONFIG_KVM) || defined(CONFIG_HVF)
+    static enum { F_UNKN, F_SUPP, F_UNSUPP } supported[64] = { };
+
+    assert(feat < ARRAY_SIZE(supported));
+    if (supported[feat] == F_UNKN) {
+        supported[feat] = arm_hw_accel_cpu_feature_supported(feat, 

can_emulate);
+    }
+    return supported[feat] == F_SUPP;
+#elif defined(CONFIG_TCG)

Quoted 8 lines

target/arm/arm_hw_accel.c line 27

Quoted 5 lines

+    return can_emulate;

target/arm/arm_hw_accel.c new file +4 -0
+#else
+#error
+#endif
+}

How much startup time is saved by this cache?
This feels like premature optimization.

It is called for each PSCI call here:

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

+static int hvf_psci_get_target_el(void)
+{
+ if (hvf_arm_el2_enabled()) {
+ return 2;
+ }
+ return 1;
+}

But as you mentioned in patch #9 "by the time we get [here] we have already detected the host cpu features and have updated env to match", so checking with:

arm_feature(env, ARM_FEATURE_EL2)

should be sufficient.

#14 lore raw

RFC PATCH 7/11 target/arm: Replace kvm_arm_pmu_supported by host_cpu_feature_supported

In reply to Philippe Mathieu-Daudé

Use the generic host_cpu_feature_supported() helper to check for the PMU feature support. This will allow to expand to non-KVM accelerators such HVF.

---
 target/arm/kvm_arm.h  | 13 -------------
 target/arm/cpu.c      |  4 ++--
 target/arm/kvm-stub.c |  5 -----
 target/arm/kvm.c      |  9 ++-------
 4 files changed, 4 insertions(+), 27 deletions(-)
target/arm/kvm_arm.h +0 -13
index 6a9b6374a6d..364578c50d6 100644
@@ -177,14 +177,6 @@ void kvm_arm_steal_time_finalize(ARMCPU *cpu, Error **errp);
  */
 bool kvm_arm_aarch32_supported(void);
 
-/**
- * kvm_arm_pmu_supported:
- *
- * Returns: true if KVM can enable the PMU
- * and false otherwise.
- */
-bool kvm_arm_pmu_supported(void);
-
 /**
  * kvm_arm_sve_supported:
  *
@@ -212,11 +204,6 @@ static inline bool kvm_arm_aarch32_supported(void)
     return false;
 }
 
-static inline bool kvm_arm_pmu_supported(void)
-{
-    return false;
-}
-
 static inline bool kvm_arm_sve_supported(void)
 {
     return false;
target/arm/cpu.c +2 -2
index 1dc2a8330d8..c78a3c9cda8 100644
@@ -1564,8 +1564,8 @@ static void arm_set_pmu(Object *obj, bool value, Error **errp)
     ARMCPU *cpu = ARM_CPU(obj);
 
     if (value) {
-        if (kvm_enabled() && !kvm_arm_pmu_supported()) {
-            error_setg(errp, "'pmu' feature not supported by KVM on this host");
+        if (host_cpu_feature_supported(ARM_FEATURE_PMU, false)) {
+            error_setg(errp, "'pmu' feature not supported by this host accelerator");
             return;
         }
         set_feature(&cpu->env, ARM_FEATURE_PMU);
target/arm/kvm-stub.c +0 -5
index c93462c5b9b..3beb336416d 100644
@@ -32,11 +32,6 @@ bool kvm_arm_aarch32_supported(void)
     return false;
 }
 
-bool kvm_arm_pmu_supported(void)
-{
-    return false;
-}
-
 bool kvm_arm_sve_supported(void)
 {
     return false;
target/arm/kvm.c +2 -7
index 82853e68d8d..0fe0f89f931 100644
@@ -288,7 +288,7 @@ static bool kvm_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
                              1 << KVM_ARM_VCPU_PTRAUTH_GENERIC);
     }
 
-    if (kvm_arm_pmu_supported()) {
+    if (host_cpu_feature_supported(ARM_FEATURE_PMU, false)) {
         init.features[0] |= 1 << KVM_ARM_VCPU_PMU_V3;
         pmu_supported = true;
         features |= 1ULL << ARM_FEATURE_PMU;
@@ -506,11 +506,6 @@ void kvm_arm_add_vcpu_properties(ARMCPU *cpu)
                                     "Set off to disable KVM steal time.");
 }
 
-bool kvm_arm_pmu_supported(void)
-{
-    return kvm_check_extension(kvm_state, KVM_CAP_ARM_PMU_V3);
-}
-
 int kvm_arm_get_max_vm_ipa_size(MachineState *ms, bool *fixed_ipa)
 {
     KVMState *s = KVM_STATE(ms->accelerator);
@@ -1783,7 +1778,7 @@ bool arm_hw_accel_cpu_feature_supported(enum arm_features feat, bool can_emulate
     case ARM_FEATURE_GENERIC_TIMER:
         return true;
     case ARM_FEATURE_PMU:
-        return kvm_arm_pmu_supported();
+        return kvm_check_extension(kvm_state, KVM_CAP_ARM_PMU_V3);
     case ARM_FEATURE_EL2:
         return kvm_arm_el2_supported();
     case ARM_FEATURE_EL3:
Signature · 1 line
2.49.0
#15 lore raw

Re: [RFC PATCH 07/11] target/arm: Replace kvm_arm_pmu_supported by host_cpu_feature_supported

target/arm/kvm.c static bool kvm_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf) · line 291

Quoted 7 lines · Philippe Mathieu-Daudé

+++ b/target/arm/kvm.c

target/arm/kvm.c +1 -1
@@ -288,7 +288,7 @@ static bool kvm_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
                               1 << KVM_ARM_VCPU_PTRAUTH_GENERIC);
      }
 
-    if (kvm_arm_pmu_supported()) {
+    if (host_cpu_feature_supported(ARM_FEATURE_PMU, false)) {

Why is false correct here? Alternately, in the next patch, why is it correct to pass true for the EL2 test?

What is the purpose of the can_emulate parameter at all?

r~

#16 lore raw

Re: [RFC PATCH 07/11] target/arm: Replace kvm_arm_pmu_supported by host_cpu_feature_supported

Quoted 10 lines · Richard Henderson

target/arm/kvm.c static bool · line 291

Quoted 7 lines · Philippe Mathieu-Daudé

+++ b/target/arm/kvm.c

target/arm/kvm.c +1 -1
@@ -288,7 +288,7 @@ static bool 
 kvm_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
                                1 << KVM_ARM_VCPU_PTRAUTH_GENERIC);
       }
-    if (kvm_arm_pmu_supported()) {
+    if (host_cpu_feature_supported(ARM_FEATURE_PMU, false)) {

Why is false correct here?  Alternately, in the next patch, why is it correct to pass true for the EL2 test?

I think I copied to KVM the HVF use, adapted on top of:
https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 2 lines

What is the purpose of the can_emulate parameter at all?

When using split-accel on pre-M3, we might emulate EL2:

       |   feat            |    can_emulate   |    retval
       +   ----            +      -----       +     ----M1/M2  | 
ARM_FEATURE_EL2         false            false
M1/M2  |  ARM_FEATURE_EL2         true             true
M3/M4  |  ARM_FEATURE_EL2         any              true
#17 lore raw

Re: [RFC PATCH 07/11] target/arm: Replace kvm_arm_pmu_supported by host_cpu_feature_supported

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

target/arm/kvm.c static bool · line 291

Quoted 7 lines · Philippe Mathieu-Daudé

+++ b/target/arm/kvm.c

target/arm/kvm.c +1 -1
@@ -288,7 +288,7 @@ static bool 
 kvm_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
                                1 << KVM_ARM_VCPU_PTRAUTH_GENERIC);
       }
-    if (kvm_arm_pmu_supported()) {
+    if (host_cpu_feature_supported(ARM_FEATURE_PMU, false)) {

Why is false correct here?  Alternately, in the next patch, why is it correct to pass true for the EL2 test?

I think I copied to KVM the HVF use, adapted on top of:
https://lore.kernel.org/qemu-devel/20250808070137.48716-12- [email protected]/

Quoted 1 line
Quoted 8 lines
Quoted 1 line

What is the purpose of the can_emulate parameter at all?

When using split-accel on pre-M3, we might emulate EL2:

       |   feat            |    can_emulate   |    retval
       +   ----            +      -----       +     ----
M1/M2  | ARM_FEATURE_EL2         false            false> M1/M2  |  ARM_FEATURE_EL2         true             true M3/M4  |  ARM_FEATURE_EL2         any              true

For example in hvf.c:

static bool hvf_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)

{
    ...
    if (host_cpu_feature_supported(ARM_FEATURE_EL2, true)) {
        ahcf->features |= 1ULL << ARM_FEATURE_EL2;
    }

and then only when split-accel is not enabled:

hv_return_t hvf_arch_vm_create(MachineState *ms, uint32_t pa_range)

{
    ...
    if (host_cpu_feature_supported(ARM_FEATURE_EL2, false)) {
        ret = hv_vm_config_set_el2_enabled(config, true);
        if (ret != HV_SUCCESS) {
            goto cleanup;
        }
    }
#18 lore raw

Re: [RFC PATCH 07/11] target/arm: Replace kvm_arm_pmu_supported by host_cpu_feature_supported

Quoted 38 lines · Philippe Mathieu-Daudé
Quoted 16 lines · Philippe Mathieu-Daudé
Quoted 10 lines · Richard Henderson

target/arm/kvm.c static bool · line 291

Quoted 7 lines · Philippe Mathieu-Daudé

+++ b/target/arm/kvm.c

target/arm/kvm.c +1 -1
@@ -288,7 +288,7 @@ static bool 
 kvm_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
                                1 << KVM_ARM_VCPU_PTRAUTH_GENERIC);
       }
-    if (kvm_arm_pmu_supported()) {
+    if (host_cpu_feature_supported(ARM_FEATURE_PMU, false)) {

Why is false correct here?  Alternately, in the next patch, why is it correct to pass true for the EL2 test?

I think I copied to KVM the HVF use, adapted on top of:
https://lore.kernel.org/qemu-devel/20250808070137.48716-12- [email protected]/

Quoted 1 line
Quoted 7 lines
Quoted 1 line

What is the purpose of the can_emulate parameter at all?

When using split-accel on pre-M3, we might emulate EL2:

        |   feat            |    can_emulate   |    retval +   ----            +      -----       +     ---- M1/M2  |  ARM_FEATURE_EL2         false            false> M1/M2  |

ARM_FEATURE_EL2         true             true

Quoted 1 line

M3/M4  |  ARM_FEATURE_EL2         any              true

For example in hvf.c:

static bool hvf_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf) {
    ...
    if (host_cpu_feature_supported(ARM_FEATURE_EL2, true)) { ahcf->features |= 1ULL << ARM_FEATURE_EL2;
    }

and then only when split-accel is not enabled:

Quoted 10 lines

hv_return_t hvf_arch_vm_create(MachineState *ms, uint32_t pa_range) {
    ...
    if (host_cpu_feature_supported(ARM_FEATURE_EL2, false)) { ret = hv_vm_config_set_el2_enabled(config, true); if (ret != HV_SUCCESS) {
            goto cleanup;
        }
    }

What I'm looking for:

- Is this feature supported BY HW?

-> hw_init_feature

- Is this feature supported BY SW?

-> sw_init_feature

- Is this feature supported BY ANY?

-> do smth with feature

With split-accel, this isn't specific to HVF/ARM.

I can use a tri-state enum { ANY, HW, SW }.

#19 lore raw

Re: [RFC PATCH 07/11] target/arm: Replace kvm_arm_pmu_supported by host_cpu_feature_supported

Quoted 50 lines · Philippe Mathieu-Daudé
Quoted 29 lines
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

-    if (kvm_arm_pmu_supported()) {
+    if (host_cpu_feature_supported(ARM_FEATURE_PMU, false)) {

Why is false correct here?  Alternately, in the next patch, why is it correct to pass true for the EL2 test?

I think I copied to KVM the HVF use, adapted on top of:
https://lore.kernel.org/qemu-devel/20250808070137.48716-12- [email protected]/

Quoted 1 line
Quoted 6 lines
Quoted 1 line

What is the purpose of the can_emulate parameter at all?

When using split-accel on pre-M3, we might emulate EL2:

        |   feat            |    can_emulate   |    retval +   ----            +      -----       +     ----

 > M1/M2  |  ARM_FEATURE_EL2         false            false> M1/M2  | ARM_FEATURE_EL2         true             true

Quoted 1 line

M3/M4  |  ARM_FEATURE_EL2         any              true

For example in hvf.c:

static bool hvf_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf) {
     ...
     if (host_cpu_feature_supported(ARM_FEATURE_EL2, true)) { ahcf->features |= 1ULL << ARM_FEATURE_EL2;
     }

and then only when split-accel is not enabled:

Quoted 10 lines

hv_return_t hvf_arch_vm_create(MachineState *ms, uint32_t pa_range) {
     ...
     if (host_cpu_feature_supported(ARM_FEATURE_EL2, false)) { ret = hv_vm_config_set_el2_enabled(config, true); if (ret != HV_SUCCESS) {
             goto cleanup;
         }
     }

What I'm looking for:

- Is this feature supported BY HW?

  -> hw_init_feature

- Is this feature supported BY SW?

  -> sw_init_feature

Quoted 7 lines

- Is this feature supported BY ANY?

  -> do smth with feature

With split-accel, this isn't specific to HVF/ARM.

I can use a tri-state enum { ANY, HW, SW }.

My point, I guess, is: tcg_enabled() appears to be the only correct setting for can_emulate, and since that's the case, it's clearer to not have the parameter and simply test can_emulate within any subroutines.

r~

#20 lore raw

Re: [RFC PATCH 07/11] target/arm: Replace kvm_arm_pmu_supported by host_cpu_feature_supported

Quoted 53 lines · Richard Henderson
Quoted 52 lines · Philippe Mathieu-Daudé
Quoted 29 lines
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

-    if (kvm_arm_pmu_supported()) {
+    if (host_cpu_feature_supported(ARM_FEATURE_PMU, false)) {

Why is false correct here?  Alternately, in the next patch, why is it correct to pass true for the EL2 test?

I think I copied to KVM the HVF use, adapted on top of:
https://lore.kernel.org/qemu-devel/20250808070137.48716-12- [email protected]/

Quoted 1 line
Quoted 6 lines
Quoted 1 line

What is the purpose of the can_emulate parameter at all?

When using split-accel on pre-M3, we might emulate EL2:

        |   feat            |    can_emulate   |    retval +   ----            +      -----       +     ----

 > M1/M2  |  ARM_FEATURE_EL2         false            false> M1/M2  | ARM_FEATURE_EL2         true             true

Quoted 1 line

M3/M4  |  ARM_FEATURE_EL2         any              true

For example in hvf.c:

static bool hvf_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf) {
     ...
     if (host_cpu_feature_supported(ARM_FEATURE_EL2, true)) { ahcf->features |= 1ULL << ARM_FEATURE_EL2;
     }

and then only when split-accel is not enabled:

Quoted 10 lines

hv_return_t hvf_arch_vm_create(MachineState *ms, uint32_t pa_range) {
     ...
     if (host_cpu_feature_supported(ARM_FEATURE_EL2, false)) { ret = hv_vm_config_set_el2_enabled(config, true); if (ret != HV_SUCCESS) {
             goto cleanup;
         }
     }

What I'm looking for:

- Is this feature supported BY HW?

   -> hw_init_feature

- Is this feature supported BY SW?

   -> sw_init_feature

- Is this feature supported BY ANY?

Quoted 7 lines
Quoted 5 lines

   -> do smth with feature

With split-accel, this isn't specific to HVF/ARM.

I can use a tri-state enum { ANY, HW, SW }.

My point, I guess, is:  tcg_enabled() appears to be the only correct setting for can_emulate, and since that's the case, it's clearer to not have the parameter and simply test can_emulate within any subroutines.

Got it!

#21 lore raw

RFC PATCH 8/11 target/arm: Replace kvm_arm_el2_supported by host_cpu_feature_supported

In reply to Philippe Mathieu-Daudé

Use the generic host_cpu_feature_supported() helper to check for the EL2 feature support. This will allow to expand to non-KVM accelerators such HVF.

---
 target/arm/kvm_arm.h  | 11 -----------
 hw/arm/virt.c         |  8 +-------
 target/arm/kvm-stub.c |  5 -----
 target/arm/kvm.c      |  6 +++---
 4 files changed, 4 insertions(+), 26 deletions(-)
target/arm/kvm_arm.h +0 -11
index 364578c50d6..7e5755d76b2 100644
@@ -191,12 +191,6 @@ bool kvm_arm_sve_supported(void);
  */
 bool kvm_arm_mte_supported(void);
 
-/**
- * kvm_arm_el2_supported:
- *
- * Returns true if KVM can enable EL2 and false otherwise.
- */
-bool kvm_arm_el2_supported(void);
 #else
 
 static inline bool kvm_arm_aarch32_supported(void)
@@ -213,11 +207,6 @@ static inline bool kvm_arm_mte_supported(void)
 {
     return false;
 }
-
-static inline bool kvm_arm_el2_supported(void)
-{
-    return false;
-}
 #endif
 
 /**
hw/arm/virt.c +1 -7
index ef6be3660f5..c2f71ecbfa7 100644
@@ -2267,13 +2267,7 @@ static void machvirt_init(MachineState *machine)
         exit(1);
     }
 
-    if (vms->virt && kvm_enabled() && !kvm_arm_el2_supported()) {
-        error_report("mach-virt: host kernel KVM does not support providing "
-                     "Virtualization extensions to the guest CPU");
-        exit(1);
-    }
-
-    if (vms->virt && !kvm_enabled() && !tcg_enabled() && !qtest_enabled()) {
+    if (vms->virt && !host_cpu_feature_supported(ARM_FEATURE_EL2, true)) {
         error_report("mach-virt: %s does not support providing "
                      "Virtualization extensions to the guest CPU",
                      current_accel_name());
target/arm/kvm-stub.c +0 -5
index 3beb336416d..35afcc7d6f9 100644
@@ -42,11 +42,6 @@ bool kvm_arm_mte_supported(void)
     return false;
 }
 
-bool kvm_arm_el2_supported(void)
-{
-    return false;
-}
-
 /*
  * These functions should never actually be called without KVM support.
  */
target/arm/kvm.c +3 -3
index 0fe0f89f931..a9f05bfa7ea 100644
@@ -274,7 +274,7 @@ static bool kvm_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
     /*
      * Ask for EL2 if supported.
      */
-    el2_supported = kvm_arm_el2_supported();
+    el2_supported = host_cpu_feature_supported(ARM_FEATURE_EL2, false);
     if (el2_supported) {
         init.features[0] |= 1 << KVM_ARM_VCPU_HAS_EL2;
     }
@@ -1780,7 +1780,7 @@ bool arm_hw_accel_cpu_feature_supported(enum arm_features feat, bool can_emulate
     case ARM_FEATURE_PMU:
         return kvm_check_extension(kvm_state, KVM_CAP_ARM_PMU_V3);
     case ARM_FEATURE_EL2:
-        return kvm_arm_el2_supported();
+        return kvm_check_extension(kvm_state, KVM_CAP_ARM_EL2);
     case ARM_FEATURE_EL3:
         return false;
     default:
@@ -1918,7 +1918,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
         cpu->kvm_init_features[0] |= (1 << KVM_ARM_VCPU_PTRAUTH_ADDRESS |
                                       1 << KVM_ARM_VCPU_PTRAUTH_GENERIC);
     }
-    if (cpu->has_el2 && kvm_arm_el2_supported()) {
+    if (cpu->has_el2 && host_cpu_feature_supported(ARM_FEATURE_EL2, false)) {
         cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_HAS_EL2;
     }
 
Signature · 1 line
2.49.0
#22 lore raw

RFC PATCH 9/11 target/arm/hvf: Sync registers used at EL2

In reply to Philippe Mathieu-Daudé

From: Mohamed Mediouni <[email protected]>

When starting up the VM at EL2, more sysregs are available. Sync the state of those.

[PMD: Adapted to host_cpu_feature_supported() API]
---
 target/arm/hvf/hvf.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
target/arm/hvf/hvf.c +34 -0
index 5174973991f..778dc3cedf7 100644
@@ -400,6 +400,7 @@ static const struct hvf_reg_match hvf_fpreg_match[] = {
 struct hvf_sreg_match {
     int reg;
     uint32_t key;
+    bool el2;
     uint32_t cp_idx;
 };
 
@@ -545,6 +546,27 @@ static struct hvf_sreg_match hvf_sreg_match[] = {
     { HV_SYS_REG_CNTV_CTL_EL0, HVF_SYSREG(14, 3, 3, 3, 1) },
     { HV_SYS_REG_CNTV_CVAL_EL0, HVF_SYSREG(14, 3, 3, 3, 2) },
     { HV_SYS_REG_SP_EL1, HVF_SYSREG(4, 1, 3, 4, 0) },
+    /* EL2 */
+    { HV_SYS_REG_CPTR_EL2, HVF_SYSREG(1, 1, 3, 4, 2), .el2 = true },
+    { HV_SYS_REG_ELR_EL2, HVF_SYSREG(4, 0, 3, 4, 1), .el2 = true },
+    { HV_SYS_REG_ESR_EL2, HVF_SYSREG(5, 2, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_FAR_EL2, HVF_SYSREG(6, 0, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_HCR_EL2, HVF_SYSREG(1, 1, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_HPFAR_EL2, HVF_SYSREG(6, 0, 3, 4, 4), .el2 = true },
+    { HV_SYS_REG_MAIR_EL2, HVF_SYSREG(10, 2, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_MDCR_EL2, HVF_SYSREG(1, 1, 3, 4, 1), .el2 = true },
+    { HV_SYS_REG_SCTLR_EL2, HVF_SYSREG(1, 0, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_SPSR_EL2, HVF_SYSREG(4, 0, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_SP_EL2, HVF_SYSREG(4, 1, 3, 6, 0), .el2 = true},
+    { HV_SYS_REG_TCR_EL2, HVF_SYSREG(2, 0, 3, 4, 2), .el2 = true },
+    { HV_SYS_REG_TPIDR_EL2, HVF_SYSREG(13, 0, 3, 4, 2), .el2 = true },
+    { HV_SYS_REG_TTBR0_EL2, HVF_SYSREG(2, 0, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_TTBR1_EL2, HVF_SYSREG(2, 0, 3, 4, 1), .el2 = true },
+    { HV_SYS_REG_VBAR_EL2, HVF_SYSREG(12, 0, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_VMPIDR_EL2, HVF_SYSREG(0, 0, 3, 4, 5), .el2 = true },
+    { HV_SYS_REG_VPIDR_EL2, HVF_SYSREG(0, 0, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_VTCR_EL2, HVF_SYSREG(2, 1, 3, 4, 2), .el2 = true },
+    { HV_SYS_REG_VTTBR_EL2, HVF_SYSREG(2, 1, 3, 4, 0), .el2 = true },
 };
 
 int hvf_get_registers(CPUState *cpu)
@@ -588,6 +610,12 @@ int hvf_get_registers(CPUState *cpu)
             continue;
         }
 
+        if (hvf_sreg_match[i].el2
+            && arm_feature(env, ARM_FEATURE_EL2)
+            && !host_cpu_feature_supported(ARM_FEATURE_EL2, false)) {
+            continue;
+        }
+
         if (cpu->accel->guest_debug_enabled) {
             /* Handle debug registers */
             switch (hvf_sreg_match[i].reg) {
@@ -725,6 +753,12 @@ int hvf_put_registers(CPUState *cpu)
             continue;
         }
 
+        if (hvf_sreg_match[i].el2
+            && arm_feature(env, ARM_FEATURE_EL2)
+            && !host_cpu_feature_supported(ARM_FEATURE_EL2, false)) {
+            continue;
+        }
+
         if (cpu->accel->guest_debug_enabled) {
             /* Handle debug registers */
             switch (hvf_sreg_match[i].reg) {
Signature · 1 line
2.49.0
#23 lore raw

Re: [RFC PATCH 09/11] target/arm/hvf: Sync registers used at EL2

Quoted 38 lines · Philippe Mathieu-Daudé

From: Mohamed Mediouni <[email protected]>

When starting up the VM at EL2, more sysregs are available. Sync the state of those.

[PMD: Adapted to host_cpu_feature_supported() API]
---
  target/arm/hvf/hvf.c | 34 ++++++++++++++++++++++++++++++++++
  1 file changed, 34 insertions(+)
target/arm/hvf/hvf.c +12 -0
index 5174973991f..778dc3cedf7 100644
@@ -400,6 +400,7 @@ static const struct hvf_reg_match hvf_fpreg_match[] = {
  struct hvf_sreg_match {
      int reg;
      uint32_t key;
+    bool el2;
      uint32_t cp_idx;
  };
 
@@ -545,6 +546,27 @@ static struct hvf_sreg_match hvf_sreg_match[] = {
      { HV_SYS_REG_CNTV_CTL_EL0, HVF_SYSREG(14, 3, 3, 3, 1) },
      { HV_SYS_REG_CNTV_CVAL_EL0, HVF_SYSREG(14, 3, 3, 3, 2) },
      { HV_SYS_REG_SP_EL1, HVF_SYSREG(4, 1, 3, 4, 0) },
+    /* EL2 */
+    { HV_SYS_REG_CPTR_EL2, HVF_SYSREG(1, 1, 3, 4, 2), .el2 = true },
+    { HV_SYS_REG_ELR_EL2, HVF_SYSREG(4, 0, 3, 4, 1), .el2 = true },
+    { HV_SYS_REG_ESR_EL2, HVF_SYSREG(5, 2, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_FAR_EL2, HVF_SYSREG(6, 0, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_HCR_EL2, HVF_SYSREG(1, 1, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_HPFAR_EL2, HVF_SYSREG(6, 0, 3, 4, 4), .el2 = true },
+    { HV_SYS_REG_MAIR_EL2, HVF_SYSREG(10, 2, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_MDCR_EL2, HVF_SYSREG(1, 1, 3, 4, 1), .el2 = true },
+    { HV_SYS_REG_SCTLR_EL2, HVF_SYSREG(1, 0, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_SPSR_EL2, HVF_SYSREG(4, 0, 3, 4, 0), .el2 = true },

target/arm/hvf/hvf.c static struct hvf_sreg_match hvf_sreg_match[] = { · line 569

Quoted 10 lines
target/arm/hvf/hvf.c +10 -0
+    { HV_SYS_REG_SP_EL2, HVF_SYSREG(4, 1, 3, 6, 0), .el2 = true},
+    { HV_SYS_REG_TCR_EL2, HVF_SYSREG(2, 0, 3, 4, 2), .el2 = true },
+    { HV_SYS_REG_TPIDR_EL2, HVF_SYSREG(13, 0, 3, 4, 2), .el2 = true },
+    { HV_SYS_REG_TTBR0_EL2, HVF_SYSREG(2, 0, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_TTBR1_EL2, HVF_SYSREG(2, 0, 3, 4, 1), .el2 = true },
+    { HV_SYS_REG_VBAR_EL2, HVF_SYSREG(12, 0, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_VMPIDR_EL2, HVF_SYSREG(0, 0, 3, 4, 5), .el2 = true },
+    { HV_SYS_REG_VPIDR_EL2, HVF_SYSREG(0, 0, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_VTCR_EL2, HVF_SYSREG(2, 1, 3, 4, 2), .el2 = true },
+    { HV_SYS_REG_VTTBR_EL2, HVF_SYSREG(2, 1, 3, 4, 0), .el2 = true },

Note that el2 == opc1 in {4,5}.

target/arm/hvf/hvf.c int hvf_get_registers(CPUState *cpu) · line 615

Quoted 7 lines
target/arm/hvf/hvf.c +3 -0
@@ -588,6 +610,12 @@ int hvf_get_registers(CPUState *cpu)
              continue;
          }
 
+        if (hvf_sreg_match[i].el2
+            && arm_feature(env, ARM_FEATURE_EL2)
+            && !host_cpu_feature_supported(ARM_FEATURE_EL2, false)) {

Why are you checking both arm_feature and host_cpu_feature_supported?
Surely by the time we get to hvf_get/put_registers we have already detected the host cpu features and have updated env to match.

r~

#24 lore raw

Re: [RFC PATCH 09/11] target/arm/hvf: Sync registers used at EL2

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

From: Mohamed Mediouni <[email protected]>

When starting up the VM at EL2, more sysregs are available. Sync the state of those.

[PMD: Adapted to host_cpu_feature_supported() API]

---
  target/arm/hvf/hvf.c | 34 ++++++++++++++++++++++++++++++++++

  1 file changed, 34 insertions(+)
Quoted 16 lines
Quoted 13 lines
target/arm/hvf/hvf.c +1 -0
index 5174973991f..778dc3cedf7 100644
@@ -400,6 +400,7 @@ static const struct hvf_reg_match 
 hvf_fpreg_match[] = {
   struct hvf_sreg_match {
       int reg;
       uint32_t key;
+    bool el2;
       uint32_t cp_idx;
   };

target/arm/hvf/hvf.c static struct hvf_sreg_match hvf_sreg_match[] = { · line 548

Quoted 3 lines
target/arm/hvf/hvf.c +0 -0
@@ -545,6 +546,27 @@ static struct hvf_sreg_match hvf_sreg_match[] = {
       { HV_SYS_REG_CNTV_CTL_EL0, HVF_SYSREG(14, 3, 3, 3, 1) },
       { HV_SYS_REG_CNTV_CVAL_EL0, HVF_SYSREG(14, 3, 3, 3, 2) },
       { HV_SYS_REG_SP_EL1, HVF_SYSREG(4, 1, 3, 4, 0) },

[*]

Quoted 22 lines
Quoted 11 lines
target/arm/hvf/hvf.c +11 -0
+    /* EL2 */
+    { HV_SYS_REG_CPTR_EL2, HVF_SYSREG(1, 1, 3, 4, 2), .el2 = true },
+    { HV_SYS_REG_ELR_EL2, HVF_SYSREG(4, 0, 3, 4, 1), .el2 = true },
+    { HV_SYS_REG_ESR_EL2, HVF_SYSREG(5, 2, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_FAR_EL2, HVF_SYSREG(6, 0, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_HCR_EL2, HVF_SYSREG(1, 1, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_HPFAR_EL2, HVF_SYSREG(6, 0, 3, 4, 4), .el2 = true },
+    { HV_SYS_REG_MAIR_EL2, HVF_SYSREG(10, 2, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_MDCR_EL2, HVF_SYSREG(1, 1, 3, 4, 1), .el2 = true },
+    { HV_SYS_REG_SCTLR_EL2, HVF_SYSREG(1, 0, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_SPSR_EL2, HVF_SYSREG(4, 0, 3, 4, 0), .el2 = true },

target/arm/hvf/hvf.c static struct hvf_sreg_match hvf_sreg_match[] = { · line 569

Quoted 10 lines
target/arm/hvf/hvf.c +10 -0
+    { HV_SYS_REG_SP_EL2, HVF_SYSREG(4, 1, 3, 6, 0), .el2 = true},
+    { HV_SYS_REG_TCR_EL2, HVF_SYSREG(2, 0, 3, 4, 2), .el2 = true },
+    { HV_SYS_REG_TPIDR_EL2, HVF_SYSREG(13, 0, 3, 4, 2), .el2 = true },
+    { HV_SYS_REG_TTBR0_EL2, HVF_SYSREG(2, 0, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_TTBR1_EL2, HVF_SYSREG(2, 0, 3, 4, 1), .el2 = true },
+    { HV_SYS_REG_VBAR_EL2, HVF_SYSREG(12, 0, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_VMPIDR_EL2, HVF_SYSREG(0, 0, 3, 4, 5), .el2 = true },
+    { HV_SYS_REG_VPIDR_EL2, HVF_SYSREG(0, 0, 3, 4, 0), .el2 = true },
+    { HV_SYS_REG_VTCR_EL2, HVF_SYSREG(2, 1, 3, 4, 2), .el2 = true },
+    { HV_SYS_REG_VTTBR_EL2, HVF_SYSREG(2, 1, 3, 4, 0), .el2 = true },
Quoted 1 line

Note that el2 == opc1 in {4,5}.

Does this also work with HV_SYS_REG_SP_EL1 (see [*])?
I suppose so, after reading commit 67d10fc4737 ("target/arm: Trap sysreg accesses for FEAT_NV") addition:

/**
* arm_cpreg_trap_in_nv: Return true if cpreg traps in nested
* virtualization
*
* Return true if this cpreg is one which should be trapped to EL2
* if it is executed at EL1 when nested virtualization is enabled
* via HCR_EL2.NV.
*/
static inline bool arm_cpreg_traps_in_nv(const ARMCPRegInfo *ri) {
/*
* The Arm ARM defines the registers to be trapped in terms of
* their names (I_TZTZL). However the underlying principle is "if
* it would UNDEF at EL1 but work at EL2 then it should trap", and
* the way the encoding of sysregs and system instructions is done
* means that the right set of registers is exactly those where
* the opc1 field is 4 or 5. (You can see this also in the assert
* we do that the opc1 field and the permissions mask line up in
* define_one_arm_cp_reg_with_opaque().)
* Checking the opc1 field is easier for us and avoids the problem
* that we do not consistently use the right architectural names
* for all sysregs, since we treat the name field as largely for
* debug.
*
* However we do this check, it is going to be at least potentially
* fragile to future new sysregs, but this seems the least likely
* to break.
*
* In particular, note that the released sysreg XML defines that
* the FEAT_MEC sysregs and instructions do not follow this FEAT_NV
* trapping rule, so we will need to add an ARM_CP_* flag to
* indicate "register does not trap on NV" to handle those if/when
* we implement FEAT_MEC.
*/
return ri->opc1 == 4 || ri->opc1 == 5;
}

From v2:

Quoted 10 lines

target/arm/hvf/hvf.c int hvf_get_registers(CPUState *cpu) · line 635

Quoted 7 lines
target/arm/hvf/hvf.c +4 -0
@@ -608,6 +630,11 @@ int hvf_get_registers(CPUState *cpu)
              continue;
          }
+        if (hvf_sreg_match[i].el2
+            && !host_cpu_feature_supported(ARM_FEATURE_EL2)) {
+            continue;
+        }

I thought you were going to change this to arm_feature(&cpu->env, ARM_FEATURE_EL2).

We want to use HVF to get these registers on M3 / M4, but not on M1 / M2 when split-accel in use where we emulate them.

Expected logic:

    if (
        // this is an EL2 register ...
        hvf_reg_traps_in_nv(i)
        // ... and CPU doesn't provide EL2 ...
        && (!arm_feature(&cpu->env, ARM_FEATURE_EL2)
            // ... or we are emulating EL2 (split-accel) ...
            || tcg_enabled())
    ) {
        // ... then do not get the register with the HVF API ...
        continue;
    }
    // ... otherwise by default use HVF.
    hv_vcpu_get_sys_reg(cpu->accel->fd, hvf_sreg_match[i].reg, &val);
#25 lore raw

RFC PATCH 10/11 target/arm/hvf: Consider EL2 acceleration for Silicon M3+ chipsets

In reply to Philippe Mathieu-Daudé

---
Just a proof-of-concept...

---
 target/arm/hvf/hvf.c | 6 ++++++
 1 file changed, 6 insertions(+)
target/arm/hvf/hvf.c +6 -0
index 778dc3cedf7..d74f576b103 100644
@@ -1000,6 +1000,9 @@ uint32_t hvf_arm_get_max_ipa_bit_size(void)
 
 bool arm_hw_accel_cpu_feature_supported(enum arm_features feat, bool can_emulate)
 {
+    hv_return_t ret;
+    bool supported;
+
     if (!hvf_enabled()) {
         return false;
     }
@@ -1011,6 +1014,9 @@ bool arm_hw_accel_cpu_feature_supported(enum arm_features feat, bool can_emulate
     case ARM_FEATURE_GENERIC_TIMER:
         return true;
     case ARM_FEATURE_EL2:
+        ret = hv_vm_config_get_el2_supported(&supported);
+        assert_hvf_ok(ret);
+        return supported;
     case ARM_FEATURE_EL3:
         return false;
     default:
Signature · 1 line
2.49.0
#26 lore raw

RFC PATCH 11/11 target/arm/hvf: Allow EL2/EL3 emulation on Silicon M1 / M2

In reply to Philippe Mathieu-Daudé

---
Another API PoC.

---
 target/arm/hvf/hvf.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
target/arm/hvf/hvf.c +5 -1
index d74f576b103..0519903c928 100644
@@ -17,6 +17,7 @@
 #include "system/hvf.h"
 #include "system/hvf_int.h"
 #include "system/hw_accel.h"
+#include "system/tcg.h"
 #include "hvf_arm.h"
 #include "cpregs.h"
 #include "cpu-sysregs.h"
@@ -1014,11 +1015,14 @@ bool arm_hw_accel_cpu_feature_supported(enum arm_features feat, bool can_emulate
     case ARM_FEATURE_GENERIC_TIMER:
         return true;
     case ARM_FEATURE_EL2:
+        if (can_emulate) {
+            return true;
+        }
         ret = hv_vm_config_get_el2_supported(&supported);
         assert_hvf_ok(ret);
         return supported;
     case ARM_FEATURE_EL3:
-        return false;
+        return can_emulate && tcg_enabled();
     default:
         g_assert_not_reached();
     }
Signature · 1 line
2.49.0
#27 lore raw

Re: [RFC PATCH 11/11] target/arm/hvf: Allow EL2/EL3 emulation on Silicon M1 / M2

target/arm/hvf/hvf.c bool arm_hw_accel_cpu_feature_supported(enum arm_features feat, bool can_emulate · line 1025

Quoted 13 lines · Philippe Mathieu-Daudé
target/arm/hvf/hvf.c +4 -1
@@ -1014,11 +1015,14 @@ bool arm_hw_accel_cpu_feature_supported(enum arm_features feat, bool can_emulate
      case ARM_FEATURE_GENERIC_TIMER:
          return true;
      case ARM_FEATURE_EL2:
+        if (can_emulate) {
+            return true;
+        }
          ret = hv_vm_config_get_el2_supported(&supported);
          assert_hvf_ok(ret);
          return supported;
      case ARM_FEATURE_EL3:
-        return false;
+        return can_emulate && tcg_enabled();

Surely the el2 portion needs the tcg_enabled check as well.

r~

#28 lore raw

Re: [RFC PATCH 11/11] target/arm/hvf: Allow EL2/EL3 emulation on Silicon M1 / M2

Quoted 17 lines · Richard Henderson
Quoted 6 lines · Philippe Mathieu-Daudé
(quoted diff) +0 -0
@@ -1014,11 +1015,14 @@ bool arm_hw_accel_cpu_feature_supported(enum 

arm_features feat, bool can_emulate
      case ARM_FEATURE_GENERIC_TIMER:
          return true;
      case ARM_FEATURE_EL2:
+        if (can_emulate) {

Quoted 8 lines

+            return true;
+        }
          ret = hv_vm_config_get_el2_supported(&supported); assert_hvf_ok(ret);
          return supported;
      case ARM_FEATURE_EL3:
-        return false;
+        return can_emulate && tcg_enabled();

Surely the el2 portion needs the tcg_enabled check as well.

Oops, indeed.