Skip to contentSkip to conversation

hw/arm/virt: add vhe option for nested virtualization

PATCH In review Haseeb Ashraf 3 participants 9 messages Nov 21 KVM

lore
Share & tools
lore
#0 lore raw

cover hw/arm/virt: add vhe option for nested virtualization

Hi,

The virt board currently enables VHE whenever virtualization=on. There is no way to expose EL2 in nVHE mode to a type-1 guest hypervisor.

This series adds a vhe machine property, enabled by default for compatibility. Setting vhe=off propagates the requested mode to the guest CPU, limits TCG's advertised VHE feature, and requests the KVM HAS_EL2_E2H0 vCPU initialization feature. Unsupported accelerators and KVM hosts fail clearly instead of silently ignoring the option.

Validation was performed on an AmpereOne A192-32X (Ampere-1a) machine:

- KVM with L0 kernels 6.18.29 and 7.2.11:
- Linux 6.12.67 initializes KVM in nVHE mode with vhe=off and VHE mode with vhe=on.
- QNX Hypervisor 8.0.5 detects the requested EL1/nVHE or EL2/VHE mode.
- Xen dom0 boots with vhe=off.
- Xen does not boot with vhe=on.
- TCG:
- QNX Hypervisor 8.0.5 boots to login with vhe=on and vhe=off.
- Xen dom0 boots to login with vhe=off.
- Both AArch64 KVM functional self-tests pass.
- TCG-only, KVM-only, and aarch64-linux-user builds succeed.

Thanks,
Haseeb

Haseeb Ashraf (1):
hw/arm/virt: add vhe option for nested virtualization

#1 lore raw

PATCH 1/1 hw/arm/virt: add vhe option for nested virtualization

From: Haseeb Ashraf <[email protected]>

Add a vhe machine property for the virt board. When virtualization=on, VHE remains enabled by default, but vhe=off allows nVHE/type-1 guest hypervisors to run without VHE.

Propagate the setting to the ARM CPU, limit TCG to nVHE mode, and request the KVM_ARM_VCPU_HAS_EL2_E2H0 vCPU initialization flag when VHE is disabled. Reject vhe=off when the selected accelerator cannot provide the requested behavior. The option has no effect unless virtualization=on is also set.

Document the option, and make the nested virtualization functional test pass a vhe setting matching the kernel mode.

Tested nested virtualization with KVM on arm64 hosts using L0 kernel versions 6.18.29 and 7.2.11. A Linux 6.12.67 guest initialized KVM in nVHE mode with vhe=off and VHE mode with vhe=on. QNX Hypervisor 8.0.5 detected EL1 host hypervisor support with vhe=off and EL2 host hypervisor support with vhe=on. Xen booted dom0 to the login prompt with vhe=off, but did not boot with vhe=on.

Tested with TCG on an AmpereOne A192-32X host. QNX Hypervisor 8.0.5 boots with both vhe settings, and Xen boots its dom0 with vhe=off.

---
 docs/system/arm/virt.rst             | 10 ++++++++
 hw/arm/virt.c                        | 37 ++++++++++++++++++++++++++++
 include/hw/arm/virt.h                |  1 +
 target/arm/cpu.c                     | 15 +++++++++++
 target/arm/cpu.h                     |  2 ++
 target/arm/kvm-stub.c                |  5 ++++
 target/arm/kvm.c                     |  8 ++++++
 target/arm/kvm_arm.h                 | 12 +++++++++
 tests/functional/aarch64/test_kvm.py | 10 +++++---
 9 files changed, 97 insertions(+), 3 deletions(-)
docs/system/arm/virt.rst +10 -0
index 5f6dd17978..31f0878788 100644
@@ -116,6 +116,16 @@ virtualization
   Set ``on``/``off`` to enable/disable emulating a guest CPU which implements the
   Arm Virtualization Extensions. The default is ``off``.
 
+vhe
+  Set ``on``/``off`` to allow/disallow a guest hypervisor to use the Arm
+  Virtualization Host Extensions (``FEAT_VHE``) when ``virtualization`` is
+  ``on``. The default is ``on``, which places no restriction on the selected
+  CPU model (it does not add VHE to a CPU model which lacks it). ``off``
+  exposes EL2 in nVHE mode for a type-1 guest hypervisor: with TCG this hides
+  ``FEAT_VHE``, with KVM it requires host ``E2H0`` support, and it is not
+  supported with HVF. This option has no effect when ``virtualization`` is
+  ``off``.
+
 mte
   Set ``on``/``off`` to enable/disable emulating a guest CPU which implements the
   Arm Memory Tagging Extensions. The default is ``off``.
hw/arm/virt.c +37 -0
index 0b11f7e328..1a987faa19 100644
@@ -3025,6 +3025,12 @@ static void machvirt_init(MachineState *machine)
         exit(1);
     }
 
+    if (vms->virt && !vms->vhe && hvf_enabled()) {
+        error_report("mach-virt: %s does not support disabling VHE",
+                     current_accel_name());
+        exit(1);
+    }
+
     if (vms->mte && hvf_enabled()) {
         error_report("mach-virt: %s does not support providing "
                      "MTE to the guest CPU",
@@ -3069,6 +3075,10 @@ static void machvirt_init(MachineState *machine)
             object_property_set_bool(cpuobj, "has_el2", false, NULL);
         }
 
+        if (vms->virt && object_property_find(cpuobj, "has_vhe")) {
+            object_property_set_bool(cpuobj, "has_vhe", vms->vhe, NULL);
+        }
+
         if (vmc->no_kvm_steal_time &&
             object_property_find(cpuobj, "kvm-steal-time")) {
             object_property_set_bool(cpuobj, "kvm-steal-time", false, NULL);
@@ -3308,6 +3318,20 @@ static void virt_set_virt(Object *obj, bool value, Error **errp)
     hvf_nested_virt_enable(value);
 }
 
+static bool virt_get_vhe(Object *obj, Error **errp)
+{
+    VirtMachineState *vms = VIRT_MACHINE(obj);
+
+    return vms->vhe;
+}
+
+static void virt_set_vhe(Object *obj, bool value, Error **errp)
+{
+    VirtMachineState *vms = VIRT_MACHINE(obj);
+
+    vms->vhe = value;
+}
+
 static bool virt_get_highmem(Object *obj, Error **errp)
 {
     VirtMachineState *vms = VIRT_MACHINE(obj);
@@ -4235,6 +4259,13 @@ static void virt_machine_class_init(ObjectClass *oc, const void *data)
                                           "guest CPU which implements the ARM "
                                           "Virtualization Extensions");
 
+    object_class_property_add_bool(oc, "vhe", virt_get_vhe,
+                                   virt_set_vhe);
+    object_class_property_set_description(oc, "vhe",
+                                          "Set on/off to enable/disable the ARM "
+                                          "Virtualization Host Extensions (applicable "
+                                          "only when virtualization=on)");
+
     object_class_property_add_bool(oc, "highmem", virt_get_highmem,
                                    virt_set_highmem);
     object_class_property_set_description(oc, "highmem",
@@ -4375,6 +4406,12 @@ static void virt_instance_init(Object *obj)
     /* EL2 is also disabled by default, for similar reasons */
     vms->virt = false;
 
+    /*
+     * VHE is unrestricted by default when virtualization=on is set.
+     * With vhe=off, EL2 is limited to nVHE mode.
+     */
+    vms->vhe = true;
+
     /* High memory is enabled by default */
     vms->highmem = true;
     vms->highmem_compact = !vmc->no_highmem_compact;
include/hw/arm/virt.h +1 -0
index 22e66d1a11..bd0065efb1 100644
@@ -174,6 +174,7 @@ struct VirtMachineState {
     bool highmem_redists;
     bool tcg_its;
     bool virt;
+    bool vhe;
     bool ras;
     bool mte;
     bool dtb_randomness;
target/arm/cpu.c +15 -0
index dde03ffdd6..4d272b5388 100644
@@ -1326,6 +1326,9 @@ static const Property arm_cpu_reset_hivecs_property =
 static const Property arm_cpu_has_el2_property =
             DEFINE_PROP_BOOL("has_el2", ARMCPU, has_el2, true);
 
+static const Property arm_cpu_has_vhe_property =
+            DEFINE_PROP_BOOL("has_vhe", ARMCPU, has_vhe, true);
+
 static const Property arm_cpu_has_el3_property =
             DEFINE_PROP_BOOL("has_el3", ARMCPU, has_el3, true);
 
@@ -1622,6 +1625,7 @@ 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);
+        qdev_property_add_static(DEVICE(obj), &arm_cpu_has_vhe_property);
     }
 #endif
 
@@ -1872,6 +1876,13 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
+    if (cpu->has_el2 && !cpu->has_vhe && kvm_enabled() &&
+        !kvm_arm_el2_e2h0_supported()) {
+        error_setg(errp, "KVM does not support disabling VHE (E2H0 "
+                   "unavailable)");
+        return;
+    }
+
 #ifndef CONFIG_USER_ONLY
     /* The NVIC and M-profile CPU are two halves of a single piece of
      * hardware; trying to use one without the other is a command line
@@ -2246,6 +2257,10 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
         unset_feature(env, ARM_FEATURE_EL2);
     }
 
+    if (cpu->has_el2 && !cpu->has_vhe && tcg_enabled()) {
+        FIELD_DP64_IDREG(isar, ID_AA64MMFR1, VH, 0);
+    }
+
     if (!cpu->has_pmu) {
         unset_feature(env, ARM_FEATURE_PMU);
     }
target/arm/cpu.h +2 -0
index b2f393f501..2f94fdae7b 100644
@@ -1022,6 +1022,8 @@ struct ArchCPU {
     bool has_el2;
     /* CPU has security extension */
     bool has_el3;
+    /* CPU has virtualization host extensions */
+    bool has_vhe;
     /* CPU has PMU (Performance Monitor Unit) */
     bool has_pmu;
     /* CPU has VFP */
target/arm/kvm-stub.c +5 -0
index 88cbe8d85c..6eb9e1366a 100644
@@ -42,6 +42,11 @@ bool kvm_arm_el2_supported(void)
     return false;
 }
 
+bool kvm_arm_el2_e2h0_supported(void)
+{
+    return false;
+}
+
 /*
  * These functions should never actually be called without KVM support.
  */
target/arm/kvm.c +8 -0
index df86ad387c..6b95ed1da8 100644
@@ -2003,6 +2003,11 @@ bool kvm_arm_el2_supported(void)
     return kvm_check_extension(kvm_state, KVM_CAP_ARM_EL2);
 }
 
+bool kvm_arm_el2_e2h0_supported(void)
+{
+    return kvm_check_extension(kvm_state, KVM_CAP_ARM_EL2_E2H0);
+}
+
 bool kvm_arm_mte_supported(void)
 {
     return kvm_check_extension(kvm_state, KVM_CAP_ARM_MTE);
@@ -2060,6 +2065,9 @@ static void kvm_arm_vcpu_prepare_init_features(ARMCPU *cpu)
     }
     if (cpu->has_el2 && kvm_arm_el2_supported()) {
         cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_HAS_EL2;
+        if (!cpu->has_vhe) {
+            cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_HAS_EL2_E2H0;
+        }
     }
 }
 
target/arm/kvm_arm.h +12 -0
index 9c7e7f654a..24828e9c07 100644
@@ -182,6 +182,13 @@ bool kvm_arm_mte_supported(void);
  * Returns true if KVM can enable EL2 and false otherwise.
  */
 bool kvm_arm_el2_supported(void);
+
+/**
+ * kvm_arm_el2_e2h0_supported:
+ *
+ * Returns true if KVM supports limiting EL2 to E2H=0, and false otherwise.
+ */
+bool kvm_arm_el2_e2h0_supported(void);
 #else
 
 static inline bool kvm_arm_aarch32_supported(void)
@@ -198,6 +205,11 @@ static inline bool kvm_arm_el2_supported(void)
 {
     return false;
 }
+
+static inline bool kvm_arm_el2_e2h0_supported(void)
+{
+    return false;
+}
 #endif
 
 /**
tests/functional/aarch64/test_kvm.py +7 -3
index c977e8c6d2..502060a109 100755
@@ -40,7 +40,9 @@ def _launch_guest(self, kvm_mode="nvhe"):
                                f"console=ttyAMA0 kvm-arm.mode={kvm_mode}")
 
         self.vm.add_args("-cpu", "max")
-        self.vm.add_args("-machine", "virt,gic-version=3,virtualization=on",
+        vhe = "on" if kvm_mode == "vhe" else "off"
+        self.vm.add_args("-machine",
+                         f"virt,gic-version=3,virtualization=on,vhe={vhe}",
                          '-kernel', kernel_path,
                          '-append', kernel_command_line)
         self.vm.add_args("-smp", "2", "-m", "320")
@@ -48,8 +50,10 @@ def _launch_guest(self, kvm_mode="nvhe"):
         try:
             self.vm.launch()
         except VMLaunchFailure as excp:
-            if "does not support providing Virtualization" in excp.output:
-                self.skipTest("accelerator has no virtualization support")
+            if ("does not support providing Virtualization" in excp.output or
+                    "does not support disabling VHE" in excp.output):
+                self.skipTest("accelerator lacks the required virtualization "
+                              "support")
             else:
                 self.log.info("unhandled launch failure: %s", excp.output)
                 raise excp
Signature · 1 line
2.34.1
#2 lore raw
Quoted 26 lines · Haseeb Ashraf

From: Haseeb Ashraf <[email protected]>

Add a vhe machine property for the virt board. When virtualization=on, VHE remains enabled by default, but vhe=off allows nVHE/type-1 guest hypervisors to run without VHE.

Propagate the setting to the ARM CPU, limit TCG to nVHE mode, and request the KVM_ARM_VCPU_HAS_EL2_E2H0 vCPU initialization flag when VHE is disabled. Reject vhe=off when the selected accelerator cannot provide the requested behavior. The option has no effect unless virtualization=on is also set.

Document the option, and make the nested virtualization functional test pass a vhe setting matching the kernel mode.

Tested nested virtualization with KVM on arm64 hosts using L0 kernel versions 6.18.29 and 7.2.11. A Linux 6.12.67 guest initialized KVM in nVHE mode with vhe=off and VHE mode with vhe=on. QNX Hypervisor 8.0.5 detected EL1 host hypervisor support with vhe=off and EL2 host hypervisor support with vhe=on. Xen booted dom0 to the login prompt with vhe=off, but did not boot with vhe=on.

Quoted 5 lines

Tested with TCG on an AmpereOne A192-32X host. QNX Hypervisor 8.0.5 boots with both vhe settings, and Xen boots its dom0 with vhe=off.

Hi,

Quoted 19 lines

---

docs/system/arm/virt.rst             | 10 ++++++++
hw/arm/virt.c                        | 37 ++++++++++++++++++++++++++++
include/hw/arm/virt.h                |  1 +
target/arm/cpu.c                     | 15 +++++++++++
target/arm/cpu.h                     |  2 ++
target/arm/kvm-stub.c                |  5 ++++
target/arm/kvm.c                     |  8 ++++++
target/arm/kvm_arm.h                 | 12 +++++++++
tests/functional/aarch64/test_kvm.py | 10 +++++---
9 files changed, 97 insertions(+), 3 deletions(-)
docs/system/arm/virt.rst +0 -0
index 5f6dd17978..31f0878788 100644
@@ -116,6 +116,16 @@ virtualization
  Set ``on``/``off`` to enable/disable emulating a guest CPU which implements the
  Arm Virtualization Extensions. The default is ``off``.

docs/system/arm/virt.rst virtualization · line 127

Quoted 10 lines
docs/system/arm/virt.rst +9 -0
 
+vhe
+  Set ``on``/``off`` to allow/disallow a guest hypervisor to use the Arm
+  Virtualization Host Extensions (``FEAT_VHE``) when ``virtualization`` is
+  ``on``. The default is ``on``, which places no restriction on the selected
+  CPU model (it does not add VHE to a CPU model which lacks it). ``off``
+  exposes EL2 in nVHE mode for a type-1 guest hypervisor: with TCG this hides
+  ``FEAT_VHE``, with KVM it requires host ``E2H0`` support, and it is not
+  supported with HVF. This option has no effect when ``virtualization`` is
+  ``off``.

If going this machine property route:

This should be a tristate: on/off/auto

With auto being the default: both nVHE and VHE when the CPU supports that, and neither with no EL2 exposed to the guest.

Quoted 9 lines
docs/system/arm/virt.rst +1 -0
+

mte
Set ``on``/``off`` to enable/disable emulating a guest CPU which implements the Arm Memory Tagging Extensions. The default is ``off``.

hw/arm/virt.c +0 -0
index 0b11f7e328..1a987faa19 100644

hw/arm/virt.c static void machvirt_init(MachineState *machine) · line 3032

Quoted 8 lines
hw/arm/virt.c +5 -0
@@ -3025,6 +3025,12 @@ static void machvirt_init(MachineState *machine)
        exit(1);
    }
 
+    if (vms->virt && !vms->vhe && hvf_enabled()) {
+        error_report("mach-virt: %s does not support disabling VHE",
+                     current_accel_name());
+        exit(1);
+    }

Does not support enabling VHE. The (experimental) patch to enable VHE with HVF is elsewhere on this mailing list.

Currently, Apple only ships nVHE with Hypervisor.framework, with VHE being a pre-release feature.

Quoted 37 lines
hw/arm/virt.c +19 -0
+
    if (vms->mte && hvf_enabled()) {
        error_report("mach-virt: %s does not support providing "
                     "MTE to the guest CPU",
@@ -3069,6 +3075,10 @@ static void machvirt_init(MachineState *machine)
            object_property_set_bool(cpuobj, "has_el2", false, NULL);
        }
 
+        if (vms->virt && object_property_find(cpuobj, "has_vhe")) {
+            object_property_set_bool(cpuobj, "has_vhe", vms->vhe, NULL);
+        }
+
        if (vmc->no_kvm_steal_time &&
            object_property_find(cpuobj, "kvm-steal-time")) {
            object_property_set_bool(cpuobj, "kvm-steal-time", false, NULL);
@@ -3308,6 +3318,20 @@ static void virt_set_virt(Object *obj, bool value, Error **errp)
    hvf_nested_virt_enable(value);
 }
 
+static bool virt_get_vhe(Object *obj, Error **errp)
+{
+    VirtMachineState *vms = VIRT_MACHINE(obj);
+
+    return vms->vhe;
+}
+
+static void virt_set_vhe(Object *obj, bool value, Error **errp)
+{
+    VirtMachineState *vms = VIRT_MACHINE(obj);
+
+    vms->vhe = value;
+}
+
 static bool virt_get_highmem(Object *obj, Error **errp)
 {
    VirtMachineState *vms = VIRT_MACHINE(obj);

hw/arm/virt.c static void virt_machine_class_init(ObjectClass *oc, const void *data) · line 4267

Quoted 9 lines
hw/arm/virt.c +6 -0
@@ -4235,6 +4259,13 @@ static void virt_machine_class_init(ObjectClass *oc, const void *data)
                                          "guest CPU which implements the ARM "
                                          "Virtualization Extensions");
 
+    object_class_property_add_bool(oc, "vhe", virt_get_vhe,
+                                   virt_set_vhe);
+    object_class_property_set_description(oc, "vhe",
+                                          "Set on/off to enable/disable the ARM "
+                                          "Virtualization Host Extensions (applicable "
+                                          "only when virtualization=on)");

As said above, should be a tristate at least.

Quoted 193 lines
hw/arm/virt.c +7 -0
+
    object_class_property_add_bool(oc, "highmem", virt_get_highmem,
                                   virt_set_highmem);
    object_class_property_set_description(oc, "highmem",
@@ -4375,6 +4406,12 @@ static void virt_instance_init(Object *obj)
    /* EL2 is also disabled by default, for similar reasons */
    vms->virt = false;
 
+    /*
+     * VHE is unrestricted by default when virtualization=on is set.
+     * With vhe=off, EL2 is limited to nVHE mode.
+     */
+    vms->vhe = true;
+
    /* High memory is enabled by default */
    vms->highmem = true;
    vms->highmem_compact = !vmc->no_highmem_compact;
include/hw/arm/virt.h +1 -0
index 22e66d1a11..bd0065efb1 100644
@@ -174,6 +174,7 @@ struct VirtMachineState {
    bool highmem_redists;
    bool tcg_its;
    bool virt;
+    bool vhe;
    bool ras;
    bool mte;
    bool dtb_randomness;
target/arm/cpu.c +0 -0
index dde03ffdd6..4d272b5388 100644
@@ -1326,6 +1326,9 @@ static const Property arm_cpu_reset_hivecs_property =

static const Property arm_cpu_has_el2_property =

            DEFINE_PROP_BOOL("has_el2", ARMCPU, has_el2, true);
target/arm/cpu.c +3 -0
+static const Property arm_cpu_has_vhe_property =
+            DEFINE_PROP_BOOL("has_vhe", ARMCPU, has_vhe, true);
+

static const Property arm_cpu_has_el3_property =

            DEFINE_PROP_BOOL("has_el3", ARMCPU, has_el3, true);
target/arm/cpu.c +12 -0
@@ -1622,6 +1625,7 @@ 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);
+        qdev_property_add_static(DEVICE(obj), &arm_cpu_has_vhe_property);
    }
 #endif
 
@@ -1872,6 +1876,13 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
        return;
    }
 
+    if (cpu->has_el2 && !cpu->has_vhe && kvm_enabled() &&
+        !kvm_arm_el2_e2h0_supported()) {
+        error_setg(errp, "KVM does not support disabling VHE (E2H0 "
+                   "unavailable)");
+        return;
+    }
+
 #ifndef CONFIG_USER_ONLY
    /* The NVIC and M-profile CPU are two halves of a single piece of
     * hardware; trying to use one without the other is a command line
@@ -2246,6 +2257,10 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
        unset_feature(env, ARM_FEATURE_EL2);
    }
 
+    if (cpu->has_el2 && !cpu->has_vhe && tcg_enabled()) {
+        FIELD_DP64_IDREG(isar, ID_AA64MMFR1, VH, 0);
+    }
+
    if (!cpu->has_pmu) {
        unset_feature(env, ARM_FEATURE_PMU);
    }
target/arm/cpu.h +2 -0
index b2f393f501..2f94fdae7b 100644
@@ -1022,6 +1022,8 @@ struct ArchCPU {
    bool has_el2;
    /* CPU has security extension */
    bool has_el3;
+    /* CPU has virtualization host extensions */
+    bool has_vhe;
    /* CPU has PMU (Performance Monitor Unit) */
    bool has_pmu;
    /* CPU has VFP */
target/arm/kvm-stub.c +5 -0
index 88cbe8d85c..6eb9e1366a 100644
@@ -42,6 +42,11 @@ bool kvm_arm_el2_supported(void)
    return false;
 }
 
+bool kvm_arm_el2_e2h0_supported(void)
+{
+    return false;
+}
+
 /*
 * These functions should never actually be called without KVM support.
 */
target/arm/kvm.c +8 -0
index df86ad387c..6b95ed1da8 100644
@@ -2003,6 +2003,11 @@ bool kvm_arm_el2_supported(void)
    return kvm_check_extension(kvm_state, KVM_CAP_ARM_EL2);
 }
 
+bool kvm_arm_el2_e2h0_supported(void)
+{
+    return kvm_check_extension(kvm_state, KVM_CAP_ARM_EL2_E2H0);
+}
+
 bool kvm_arm_mte_supported(void)
 {
    return kvm_check_extension(kvm_state, KVM_CAP_ARM_MTE);
@@ -2060,6 +2065,9 @@ static void kvm_arm_vcpu_prepare_init_features(ARMCPU *cpu)
    }
    if (cpu->has_el2 && kvm_arm_el2_supported()) {
        cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_HAS_EL2;
+        if (!cpu->has_vhe) {
+            cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_HAS_EL2_E2H0;
+        }
    }
 }
 
target/arm/kvm_arm.h +12 -0
index 9c7e7f654a..24828e9c07 100644
@@ -182,6 +182,13 @@ bool kvm_arm_mte_supported(void);
 * Returns true if KVM can enable EL2 and false otherwise.
 */
 bool kvm_arm_el2_supported(void);
+
+/**
+ * kvm_arm_el2_e2h0_supported:
+ *
+ * Returns true if KVM supports limiting EL2 to E2H=0, and false otherwise.
+ */
+bool kvm_arm_el2_e2h0_supported(void);
 #else
 
 static inline bool kvm_arm_aarch32_supported(void)
@@ -198,6 +205,11 @@ static inline bool kvm_arm_el2_supported(void)
 {
    return false;
 }
+
+static inline bool kvm_arm_el2_e2h0_supported(void)
+{
+    return false;
+}
 #endif
 
 /**
tests/functional/aarch64/test_kvm.py +7 -3
index c977e8c6d2..502060a109 100755
@@ -40,7 +40,9 @@ def _launch_guest(self, kvm_mode="nvhe"):
                               f"console=ttyAMA0 kvm-arm.mode={kvm_mode}")
 
        self.vm.add_args("-cpu", "max")
-        self.vm.add_args("-machine", "virt,gic-version=3,virtualization=on",
+        vhe = "on" if kvm_mode == "vhe" else "off"
+        self.vm.add_args("-machine",
+                         f"virt,gic-version=3,virtualization=on,vhe={vhe}",
                         '-kernel', kernel_path,
                         '-append', kernel_command_line)
        self.vm.add_args("-smp", "2", "-m", "320")
@@ -48,8 +50,10 @@ def _launch_guest(self, kvm_mode="nvhe"):
        try:
            self.vm.launch()
        except VMLaunchFailure as excp:
-            if "does not support providing Virtualization" in excp.output:
-                self.skipTest("accelerator has no virtualization support")
+            if ("does not support providing Virtualization" in excp.output or
+                    "does not support disabling VHE" in excp.output):
+                self.skipTest("accelerator lacks the required virtualization "
+                              "support")
            else:
                self.log.info("unhandled launch failure: %s", excp.output)
                raise excp
Signature · 1 line
2.34.1
#3 lore raw
In reply to Haseeb Ashraf
Quoted 11 lines · Haseeb Ashraf

Hi,

The virt board currently enables VHE whenever virtualization=on. There is no way to expose EL2 in nVHE mode to a type-1 guest hypervisor.

This series adds a vhe machine property, enabled by default for compatibility. Setting vhe=off propagates the requested mode to the guest CPU, limits TCG's advertised VHE feature, and requests the KVM HAS_EL2_E2H0 vCPU initialization feature. Unsupported accelerators and KVM hosts fail clearly instead of silently ignoring the option.

+ Eric

Adding yet another random property for something that is a feature of the emulated CPU strikes me as going in the wrong direction.

I'd rather see this being directly driven by Eric's feature framework as posted at [1], which lets *any* feature being selected by the user. The KVM feature flag can then be derived from what the user has selected (and matched against what the host can provide).

Thanks,

	M.

[1] https://lore.kernel.org/r/[email protected]

Signature · 1 line
Without deviation from the norm, progress is not possible.
#4 lore raw
Quoted 23 lines · Marc Zyngier
Quoted 11 lines · Haseeb Ashraf

Hi,

The virt board currently enables VHE whenever virtualization=on. There is no way to expose EL2 in nVHE mode to a type-1 guest hypervisor.

This series adds a vhe machine property, enabled by default for compatibility. Setting vhe=off propagates the requested mode to the guest CPU, limits TCG's advertised VHE feature, and requests the KVM HAS_EL2_E2H0 vCPU initialization feature. Unsupported accelerators and KVM hosts fail clearly instead of silently ignoring the option.

+ Eric

Adding yet another random property for something that is a feature of the emulated CPU strikes me as going in the wrong direction.

Quoted 6 lines

I'd rather see this being directly driven by Eric's feature framework as posted at [1], which lets *any* feature being selected by the user. The KVM feature flag can then be derived from what the user has selected (and matched against what the host can provide).

Thanks,

Hi,

Relying on CPU features for nested virt comes with complexity for the HVF side, where the nested virt configuration is part of the accelerator not a vCPU.

And it’s too late at vCPU creation time to re-configure this, that said I’ve been considering whether it’d be wise to build a hack in HVF and WHP* to defer VM creation until creation of the first vCPU but that adds other problems…

* currently, we have this behaviour on x86 there where nested virt is always set as enabled because of this, and -cpu host,vmx=on is only disabling exposing the CPUID bit, not nested virt by itself.

Quoted 8 lines

M.

[1] https://lore.kernel.org/r/[email protected]

Signature · 1 line
Without deviation from the norm, progress is not possible.
#5 lore raw

v2 cover arm/kvm: select an nVHE-only EL2 with SYSREG_ID_AA64MMFR1_EL1_VH

In reply to Haseeb Ashraf

From: Haseeb Ashraf <[email protected]>

Hi,

KVM presents a VHE-only EL2 (HCR_EL2.E2H RES1) to a nested guest unless KVM_ARM_VCPU_HAS_EL2_E2H0 is requested at vCPU initialization. Guest hypervisors which only support nVHE, such as Xen, cannot run in that mode. This series lets the user select an nVHE-only EL2 with:

  -M virt,virtualization=on -accel kvm \
      -cpu host,SYSREG_ID_AA64MMFR1_EL1_VH=0

Following Marc's feedback on v1, this is built on Eric Auger's SYSREG_<REG>_<FIELD> framework instead of a new machine or CPU property. It is an RFC because that framework is not merged yet.

Dependencies
------------

The series applies on top of the remaining patches of Eric's v11 [1], which are on his arm-cpu-model-v11-rc branch [2], rebased on current master. On current master those patches need two mechanical build
fixes: object_property_add() now takes &uint64_type_info, and kvm_feature_idx_to_idregs_idx() must sit after kvm_arm_sve_get_vls() instead of inside it. The fixes belong to Eric's series and are not part of these three patches.

Questions
---------

- KVM does not let userspace write ID_AA64MMFR1_EL1.VH, so the generic "writable fields only" rule would not create the property. Patch 2 treats VH as selected through a vCPU init feature and exposes it as an exception. Is that acceptable, or would you prefer a generic representation of such fields in the framework?
- Only VH is updated in QEMU's copy of the ID registers. Other fields which KVM derives from E2H0, for example ID_AA64MMFR4_EL1, are not refreshed. Is that acceptable, or should QEMU re-read the ID registers after vCPU initialization?

Scope
-----

TCG and HVF are unchanged by design. The property is available for the host (and max) CPU model with KVM, when the host kernel advertises KVM_CAP_ARM_EL2_E2H0. Xen and QNX already boot with TCG, where HCR_EL2.E2H is under the control of the guest.

Changes since v1 [3]:

- Removed the dedicated virt machine and ARM CPU "vhe" properties.
- Switched to Eric's SYSREG ID-register framework and use SYSREG_ID_AA64MMFR1_EL1_VH to select an nVHE-only EL2.
- Exposed the property only when KVM advertises

    KVM_CAP_ARM_EL2_E2H0.

- Derived KVM_ARM_VCPU_HAS_EL2_E2H0 from VH and rejected VH=0 when virtualization is disabled.
- Added documentation and a KVM functional selftest which verifies that the guest hypervisor initializes in nVHE mode.

Testing
-------

Validation was performed on an AmpereOne A192-32X (Ampere-1a) running host kernel 6.18.29+3-arm64:

- TCG-only, KVM-only and aarch64-linux-user builds succeeded.
- TCG rejects SYSREG_ID_AA64MMFR1_EL1_VH because the property does not exist for that accelerator.
- The existing AArch64 KVM nVHE and VHE selftests pass under TCG.
- The arm-cpu-features qtest passes.
- Linux 6.12.67 initialized KVM in nVHE mode with VH=0 and in VHE mode with VH=1; both boots reached login.
- QNX Hypervisor 8.0.5 reached login with both values and reported the corresponding ID register state.
- Xen dom0 reached login with VH=0. With VH=1, Xen did not boot.
- The complete AArch64 KVM functional suite passed, including the new nVHE-only selftest.

Not tested: live migration, and hosts other than AmpereOne.

[1] https://patchew.org/QEMU/[email protected]/
[2] https://github.com/eauger/qemu/commits/arm-cpu-model-v11-rc
[3] https://patchew.org/QEMU/[email protected]/

Thanks,
Haseeb

Haseeb Ashraf (3):
target/arm/kvm: Add kvm_arm_el2_e2h0_supported() target/arm/kvm: Let SYSREG_ID_AA64MMFR1_EL1_VH select an nVHE EL2 tests/functional: Add a KVM selftest for an nVHE-only EL2

 docs/system/arm/cpu-features.rst     | 22 ++++++++++++
 target/arm/cpu-idregs.h              | 31 +++++++++++++++++
 target/arm/cpu64.c                   | 51 ++++++++++++++++++++++------
 target/arm/kvm-stub.c                |  5 +++
 target/arm/kvm.c                     | 29 ++++++++++++++--
 target/arm/kvm_arm.h                 | 13 +++++++
 tests/functional/aarch64/test_kvm.py | 22 ++++++++++--
 7 files changed, 157 insertions(+), 16 deletions(-)
#6 lore raw

RFC PATCH v2 1/3 target/arm/kvm: Add kvm_arm_el2_e2h0_supported()

From: Haseeb Ashraf <[email protected]>

Add a helper reporting whether KVM can limit a nested guest to an nVHE-only EL2 (HCR_EL2.E2H RES0) through the KVM_ARM_VCPU_HAS_EL2_E2H0 vCPU feature. KVM advertises this with KVM_CAP_ARM_EL2_E2H0.

The next patch uses it to let the user select an nVHE-only EL2.

---
 target/arm/kvm-stub.c |  5 +++++
 target/arm/kvm.c      |  5 +++++
 target/arm/kvm_arm.h  | 13 +++++++++++++
 3 files changed, 23 insertions(+)
target/arm/kvm-stub.c +5 -0
index c9387f5c8c..88e35909c4 100644
@@ -42,6 +42,11 @@ bool kvm_arm_el2_supported(void)
     return false;
 }
 
+bool kvm_arm_el2_e2h0_supported(void)
+{
+    return false;
+}
+
 /*
  * These functions should never actually be called without KVM support.
  */
target/arm/kvm.c +5 -0
index 36a5faf537..8ef5597c68 100644
@@ -2178,6 +2178,11 @@ bool kvm_arm_el2_supported(void)
     return kvm_check_extension(kvm_state, KVM_CAP_ARM_EL2);
 }
 
+bool kvm_arm_el2_e2h0_supported(void)
+{
+    return kvm_check_extension(kvm_state, KVM_CAP_ARM_EL2_E2H0);
+}
+
 bool kvm_arm_mte_supported(void)
 {
     return kvm_check_extension(kvm_state, KVM_CAP_ARM_MTE);
target/arm/kvm_arm.h +13 -0
index 2e7e02e4a4..0916a5fdb4 100644
@@ -200,6 +200,14 @@ bool kvm_arm_mte_supported(void);
  * Returns true if KVM can enable EL2 and false otherwise.
  */
 bool kvm_arm_el2_supported(void);
+
+/**
+ * kvm_arm_el2_e2h0_supported:
+ *
+ * Returns true if KVM can limit EL2 to nVHE (HCR_EL2.E2H RES0) through
+ * KVM_ARM_VCPU_HAS_EL2_E2H0, and false otherwise.
+ */
+bool kvm_arm_el2_e2h0_supported(void);
 #else
 
 static inline bool kvm_arm_aarch32_supported(void)
@@ -216,6 +224,11 @@ static inline bool kvm_arm_el2_supported(void)
 {
     return false;
 }
+
+static inline bool kvm_arm_el2_e2h0_supported(void)
+{
+    return false;
+}
 #endif
 
 /**
Signature · 1 line
2.34.1
#7 lore raw

RFC PATCH v2 2/3 target/arm/kvm: Let SYSREG_ID_AA64MMFR1_EL1_VH select an nVHE EL2

In reply to Haseeb Ashraf

From: Haseeb Ashraf <[email protected]>

KVM runs a nested guest with a VHE-only EL2 (HCR_EL2.E2H RES1) unless KVM_ARM_VCPU_HAS_EL2_E2H0 is requested at KVM_ARM_VCPU_INIT time. With that feature FEAT_VHE is hidden: ID_AA64MMFR1_EL1.VH reads 0 and HCR_EL2.E2H is RES0. Guest hypervisors which only support nVHE, such as Xen, can only run in the latter mode.

Do not add a dedicated machine or CPU option for it. Derive the vCPU init feature from the ID register field selected by the user instead:
SYSREG_ID_AA64MMFR1_EL1_VH=0 requests KVM_ARM_VCPU_HAS_EL2_E2H0.

KVM does not let userspace write ID_AA64MMFR1_EL1.VH, so the generic "writable fields only" rule would not create the property. Expose it explicitly when the host supports KVM_CAP_ARM_EL2_E2H0. As KVM already reports the requested value after vCPU init, applying the property does not need a KVM_SET_ONE_REG for this field, and the query-cpu-model-expansion dry run on the scratch vCPU skips it. Setting VH=0 without virtualization=on is rejected.

Only ID_AA64MMFR1_EL1.VH is updated in QEMU's copy of the ID registers. Other fields which KVM derives from E2H0 (e.g. ID_AA64MMFR4_EL1) are not refreshed.

---

v2:
- Use Eric Auger's SYSREG framework instead of a dedicated vhe machine/CPU property.
- Expose the init-driven VH field only when KVM supports E2H0.
- Add the virtualization=on consistency check and document VH=0.

 docs/system/arm/cpu-features.rst | 22 ++++++++++++++
 target/arm/cpu-idregs.h          | 31 +++++++++++++++++++
 target/arm/cpu64.c               | 51 +++++++++++++++++++++++++-------
 target/arm/kvm.c                 | 24 +++++++++++++--
 4 files changed, 114 insertions(+), 14 deletions(-)
docs/system/arm/cpu-features.rst +22 -0
index 13454783cf..cde26c04b9 100644
@@ -579,6 +579,28 @@ either not writable with the specific host kernel, unknown to this QEMU
 build, or intentionally not exposed because it overlaps a legacy composite
 option.
 
+Selecting an nVHE-only EL2
+--------------------------
+
+With ``-machine virt,virtualization=on``, KVM presents a VHE-only EL2 to
+the guest (``HCR_EL2.E2H`` is RES1) by default.  A guest hypervisor which
+only supports nVHE, such as a type-1 hypervisor like Xen, needs VHE to be
+hidden.  This is done by setting ``ID_AA64MMFR1_EL1.VH`` to 0::
+
+  $ qemu-system-aarch64 -M virt,virtualization=on -accel kvm \
+        -cpu host,SYSREG_ID_AA64MMFR1_EL1_VH=0
+
+QEMU then requests ``KVM_ARM_VCPU_HAS_EL2_E2H0`` when initializing the
+vCPUs, so that ``HCR_EL2.E2H`` is RES0 for the guest.  Leaving the
+property unset, or setting it to 1, keeps the default VHE-only EL2.
+Setting it to 0 requires ``virtualization=on``.
+
+KVM does not allow userspace to write ``ID_AA64MMFR1_EL1.VH`` directly, so
+this is an exception to the rule that only writable fields get a
+property: the ``SYSREG_ID_AA64MMFR1_EL1_VH`` property is exposed when the
+host kernel supports ``KVM_CAP_ARM_EL2_E2H0``.  Without this support the
+property does not exist.
+
 A note on compatibility
 -----------------------
 
target/arm/cpu-idregs.h +31 -0
index 465ec83c97..76d7b94858 100644
@@ -73,4 +73,35 @@ int arm_cpu_apply_sysreg_props(ARMCPU *cpu, ARMSysregPropApplyFn accel_apply_fn,
 bool arm_cpu_validate_sysreg_prop(ARMCPU *cpu, ARM64SysRegField *field,
                                   uint64_t value, Error **errp);
 
+/**
+ * arm_cpu_sysreg_prop_get:
+ *
+ * Look up the value the user set through a SYSREG_<IDREG>_<FIELD>
+ * property, before it is applied to the vcpu.  Accelerators use it to
+ * derive vcpu init settings from the requested ID register values.
+ *
+ * @cpu: vcpu the property belongs to
+ * @reg_idx: ID register index (<REG>_EL1_IDX)
+ * @shift: lowest bit of the field
+ * @value: set to the requested field value when the property was set
+ *
+ * returns true if the property was set by the user
+ */
+bool arm_cpu_sysreg_prop_get(ARMCPU *cpu, int reg_idx, int shift,
+                             uint64_t *value);
+
+/**
+ * arm_cpu_sysreg_field_init_driven:
+ *
+ * Some fields cannot be written with the accelerator once the vcpu exists
+ * but are selected through a vcpu init feature instead (for instance
+ * ID_AA64MMFR1_EL1.VH with KVM_ARM_VCPU_HAS_EL2_E2H0).  Their SYSREG
+ * property is exposed even if the field is not writable.
+ *
+ * @field: ID register field
+ *
+ * returns true if @field is selected at vcpu init time on this host
+ */
+bool arm_cpu_sysreg_field_init_driven(ARM64SysRegField *field);
+
 #endif
target/arm/cpu64.c +40 -11

82 lines not on this page · raw message

target/arm/kvm.c +21 -3
index 8ef5597c68..bb4ca7e8d1 100644
@@ -1324,9 +1324,15 @@ bool kvm_idreg_write_scratch_vcpu(ARMCPU *cpu, int cpufd,
         return false;
     }
 
-    ret = kvm_arm_override_idreg_field(cpufd, field, newfv, errp);
-    if (ret) {
-        return false;
+    /*
+     * Init-driven fields (e.g. VH) are selected through a vcpu init
+     * feature, which the scratch vcpu may not have: do not write them.
+     */
+    if (!arm_cpu_sysreg_field_init_driven(field)) {
+        ret = kvm_arm_override_idreg_field(cpufd, field, newfv, errp);
+        if (ret) {
+            return false;
+        }
     }
 
     cpu->isar.idregs[field->index] =
@@ -2239,7 +2245,19 @@ static void kvm_arm_vcpu_prepare_init_features(ARMCPU *cpu)
                                       1 << KVM_ARM_VCPU_PTRAUTH_GENERIC);
     }
     if (cpu->has_el2 && kvm_arm_el2_supported()) {
+        uint64_t vh;
+
         cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_HAS_EL2;
+        /*
+         * KVM exposes a VHE-only EL2 by default.  An nVHE-only EL2 can
+         * only be selected at vcpu init time, so derive it from the
+         * requested ID_AA64MMFR1_EL1.VH value.
+         */
+        if (arm_cpu_sysreg_prop_get(cpu, ID_AA64MMFR1_EL1_IDX,
+                                    R_ID_AA64MMFR1_VH_SHIFT, &vh) &&
+            vh == 0) {
+            cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_HAS_EL2_E2H0;
+        }
     }
 }
 
Signature · 1 line
2.34.1
#8 lore raw

RFC PATCH v2 3/3 tests/functional: Add a KVM selftest for an nVHE-only EL2

In reply to Haseeb Ashraf

From: Haseeb Ashraf <[email protected]>

Run the existing kvm-unit-tests smoke test with VHE hidden from the guest hypervisor through SYSREG_ID_AA64MMFR1_EL1_VH=0. The SYSREG_* properties are available only for the host CPU model with KVM, so select the accelerator and CPU model explicitly, and check that the guest kernel initialized its hypervisor in nVHE mode. The test is skipped when the host kernel cannot limit EL2 to nVHE.

---

v2:
- Select KVM and the host CPU model explicitly.
- Verify the guest kernel reports nVHE mode.

 tests/functional/aarch64/test_kvm.py | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
tests/functional/aarch64/test_kvm.py +20 -2
index c977e8c6d2..b7a4aceb66 100755
@@ -30,7 +30,7 @@ class Aarch64VirtKVMTests(LinuxKernelTest):
     # base of tests
     KUT_BASE = "/usr/share/kvm-unit-tests/"
 
-    def _launch_guest(self, kvm_mode="nvhe"):
+    def _launch_guest(self, kvm_mode="nvhe", cpu="max", accel=None):
 
         self.set_machine('virt')
         kernel_path = self.ASSET_KVM_TEST_KERNEL.fetch()
@@ -39,7 +39,9 @@ def _launch_guest(self, kvm_mode="nvhe"):
         kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
                                f"console=ttyAMA0 kvm-arm.mode={kvm_mode}")
 
-        self.vm.add_args("-cpu", "max")
+        if accel:
+            self.vm.add_args("-accel", accel)
+        self.vm.add_args("-cpu", cpu)
         self.vm.add_args("-machine", "virt,gic-version=3,virtualization=on",
                          '-kernel', kernel_path,
                          '-append', kernel_command_line)
@@ -50,6 +52,9 @@ def _launch_guest(self, kvm_mode="nvhe"):
         except VMLaunchFailure as excp:
             if "does not support providing Virtualization" in excp.output:
                 self.skipTest("accelerator has no virtualization support")
+            elif ("SYSREG_ID_AA64MMFR1_EL1_VH" in excp.output and
+                  "not found" in excp.output):
+                self.skipTest("host KVM cannot limit EL2 to nVHE")
             else:
                 self.log.info("unhandled launch failure: %s", excp.output)
                 raise excp
@@ -73,5 +78,18 @@ def test_aarch64_vhe_selftest(self):
         self._launch_guest("vhe")
         self._smoketest_kvm()
 
+    def test_aarch64_nvhe_only_selftest(self):
+        # Hiding VHE from the guest hypervisor is a KVM feature: the
+        # SYSREG_* properties only exist for the host CPU model with KVM.
+        self.require_accelerator("kvm")
+        self._launch_guest("nvhe", "host,SYSREG_ID_AA64MMFR1_EL1_VH=0",
+                           accel="kvm")
+        # The guest kernel must run its hypervisor in nVHE mode
+        ec_and_wait(self, "dmesg | grep -Eq "
+                    "'Hyp (nVHE )?mode initialized successfully' && "
+                    "! dmesg | grep -q ' VHE mode initialized'",
+                    self.OK_CMD)
+        self._smoketest_kvm()
+
 if __name__ == '__main__':
     LinuxKernelTest.main()
Signature · 1 line
2.34.1