Register the host ARMCPU type for every target and hide it with
is_available. aarch64_host_initfn picks kvm, hvf, whpx, or nitro
from TargetKconfig instead of CONFIG_* ifdefs.

- Add ARMCPUInfo.is_available and pass it from arm_cpu_register
- target_has_arm_host_cpu covers kvm, hvf, whpx, and nitro
- Add hvf and whpx set_cpu_features_from_host stubs

Signed-off-by: Yonggang Luo <[email protected]>
---
 target/arm/cpu.c            |  1 +
 target/arm/cpu.h            |  1 +
 target/arm/cpu64.c          | 43 +++++++++++++++++++++----------------
 target/arm/hvf/hvf-stub.c   | 14 ++++++++++++
 target/arm/hvf/meson.build  |  1 +
 target/arm/whpx/whpx-stub.c |  5 +++++
 6 files changed, 47 insertions(+), 18 deletions(-)
 create mode 100644 target/arm/hvf/hvf-stub.c

diff --git a/target/arm/cpu.c b/target/arm/cpu.c
index c138c4c0587..9b498632c86 100644
--- a/target/arm/cpu.c
+++ b/target/arm/cpu.c
@@ -2685,6 +2685,7 @@ void arm_cpu_register(const ARMCPUInfo *info)
         .instance_init = arm_cpu_instance_init,
         .class_init = info->class_init ?: cpu_register_class_init,
         .class_data = info,
+        .is_available = info->is_available,
     };
 
     type_info.name = g_strdup_printf("%s-" TYPE_ARM_CPU, info->name);
diff --git a/target/arm/cpu.h b/target/arm/cpu.h
index e3f931dba26..87de7e9ff09 100644
--- a/target/arm/cpu.h
+++ b/target/arm/cpu.h
@@ -1177,6 +1177,7 @@ typedef struct ARMCPUInfo {
     const char *deprecation_note;
     void (*initfn)(Object *obj);
     void (*class_init)(ObjectClass *oc, const void *data);
+    TypeIsAvailable *is_available;
 } ARMCPUInfo;
 
 /**
diff --git a/target/arm/cpu64.c b/target/arm/cpu64.c
index 03f3d55f6ba..168639ac2f4 100644
--- a/target/arm/cpu64.c
+++ b/target/arm/cpu64.c
@@ -20,6 +20,8 @@
 
 #include "qemu/osdep.h"
 #include "qapi/error.h"
+#include "qemu/target-info.h"
+#include "qemu/target-kconfig-has.h"
 #include "cpu.h"
 #include "cpregs.h"
 #include "qemu/module.h"
@@ -757,37 +759,42 @@ static void aarch64_a53_initfn(Object *obj)
 void aarch64_host_initfn(Object *obj)
 {
     ARMCPU *cpu = ARM_CPU(obj);
+    const TargetInfo *ti = target_info();
 
-#if defined(CONFIG_NITRO)
-    if (nitro_enabled()) {
+    if (target_has_kconfig_nitro(ti) && nitro_enabled()) {
         /* The nitro accel uses -cpu host, but does not actually consume it */
         return;
     }
-#endif
 
-#if defined(CONFIG_KVM)
-    kvm_arm_set_cpreg_mig_tolerances(cpu);
-    kvm_arm_set_cpu_features_from_host(cpu);
-    aarch64_add_sve_properties(obj);
-#elif defined(CONFIG_HVF)
-    hvf_arm_set_cpu_features_from_host(cpu);
-#elif defined(CONFIG_WHPX)
-    whpx_arm_set_cpu_features_from_host(cpu);
-#else
-    cpu->host_cpu_probe_failed = true;
-#endif
+    if (target_has_kconfig_kvm(ti)) {
+        kvm_arm_set_cpreg_mig_tolerances(cpu);
+        kvm_arm_set_cpu_features_from_host(cpu);
+        aarch64_add_sve_properties(obj);
+    } else if (target_has_kconfig_hvf(ti)) {
+        hvf_arm_set_cpu_features_from_host(cpu);
+    } else if (target_has_kconfig_whpx(ti)) {
+        whpx_arm_set_cpu_features_from_host(cpu);
+    } else {
+        cpu->host_cpu_probe_failed = true;
+    }
     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
         aarch64_add_pauth_properties(obj);
     }
 }
 
+static bool target_has_arm_host_cpu(const TargetInfo *ti)
+{
+    return target_has_kconfig_kvm(ti)
+        || target_has_kconfig_hvf(ti)
+        || target_has_kconfig_whpx(ti)
+        || target_has_kconfig_nitro(ti);
+}
+
 static const ARMCPUInfo aarch64_cpus[] = {
     { .name = "cortex-a57",         .initfn = aarch64_a57_initfn },
     { .name = "cortex-a53",         .initfn = aarch64_a53_initfn },
-#if defined(CONFIG_KVM) || defined(CONFIG_HVF) || defined(CONFIG_WHPX) || \
-    defined(CONFIG_NITRO)
-    { .name = "host",               .initfn = aarch64_host_initfn },
-#endif
+    { .name = "host",               .initfn = aarch64_host_initfn,
+      .is_available = target_has_arm_host_cpu },
 };
 
 static void aarch64_cpu_register_types(void)
diff --git a/target/arm/hvf/hvf-stub.c b/target/arm/hvf/hvf-stub.c
new file mode 100644
index 00000000000..3c8148316c9
--- /dev/null
+++ b/target/arm/hvf/hvf-stub.c
@@ -0,0 +1,14 @@
+/*
+ * QEMU HVF ARM specific function stubs
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "hvf_arm.h"
+
+void hvf_arm_set_cpu_features_from_host(ARMCPU *cpu)
+{
+    g_assert_not_reached();
+}
diff --git a/target/arm/hvf/meson.build b/target/arm/hvf/meson.build
index afc509a470e..055fab56fb6 100644
--- a/target/arm/hvf/meson.build
+++ b/target/arm/hvf/meson.build
@@ -1,3 +1,4 @@
 arm_system_ss.add(when: [hvf, 'CONFIG_HVF'], if_true: files(
   'hvf.c',
 ))
+arm_stubs_ss.add(files('hvf-stub.c'))
diff --git a/target/arm/whpx/whpx-stub.c b/target/arm/whpx/whpx-stub.c
index 32e434a5f6a..3d1cbb33a28 100644
--- a/target/arm/whpx/whpx-stub.c
+++ b/target/arm/whpx/whpx-stub.c
@@ -13,3 +13,8 @@ uint32_t whpx_arm_get_ipa_bit_size(void)
 {
     g_assert_not_reached();
 }
+
+void whpx_arm_set_cpu_features_from_host(ARMCPU *cpu)
+{
+    g_assert_not_reached();
+}
-- 
2.52.0.windows.1


Reply via email to