hw/arm/virt: add vhe option for nested virtualization
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
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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
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_E2H0vCPU 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.
Suggested-by Volodymyr Babchuk <[email protected]>Signed-off-by Haseeb Ashraf <[email protected]>
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@@ -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
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@@ -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@@ -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@@ -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@@ -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@@ -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@@ -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@@ -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 excpSignature · 1 line
2.34.1
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_E2H0vCPU 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.
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_E2H0vCPU 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.
v2 cover arm/kvm: select an nVHE-only EL2 with SYSREG_ID_AA64MMFR1_EL1_VH
In reply to Haseeb AshrafFrom: 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=0Following 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(-)
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
@@ -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
@@ -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
@@ -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
RFC PATCH v2 2/3 target/arm/kvm: Let SYSREG_ID_AA64MMFR1_EL1_VH select an nVHE EL2
In reply to Haseeb AshrafFrom: 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
@@ -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
@@ -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
@@ -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
RFC PATCH v2 3/3 tests/functional: Add a KVM selftest for an nVHE-only EL2
In reply to Haseeb AshrafFrom: 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
@@ -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
Re: [RFC PATCH v2 0/3] arm/kvm: select an nVHE-only EL2 with SYSREG_ID_AA64MMFR1_EL1_VH
In reply to Haseeb AshrafHi Haseeb,
Quoted 27 lines · 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_E2H0is 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=0Following 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, andkvm_feature_idx_to_idregs_idx()must sit afterkvm_arm_sve_get_vls()instead of inside it. The fixes belong to Eric's series and are not part of these three patches.
Quoted 8 lines
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?
Well I am a bit embarrassed to answer at that point since the series is not fully reviewed and this addition blurs some lines.
We have a way to introspect which fields that are let writable by KVM. We don't have any way to understand which bit can be set at VCPU INIT. So hardcoding the exception is the only way I see. If this tends to reproduce we can effectively have a table for such fields but I am not sure this would be needed at that point.
Overall it complexifies the logic & description of those properties because we said they apply at the end of the init chain and now we start to have a mix. We said SYSREG props were not supposed to replace legacy composite options. Now I guess it paves the way to have SYSREG props that do stuff at vcpu init and latter which is less obvious. > - Only VH is updated in QEMU's copy of the ID registers. Other fields
Quoted 2 lines
which KVM derives from E2H0, for example
ID_AA64MMFR4_EL1, are not refreshed. Is that acceptable, or should QEMU re-read the ID
what do you mean by other derived IDREG fields are not refreshed. Isn't it done automatically by KVM when you apply the VCPU init to reflect the above changes?
Adding Khushit in the loop.
Eric > registers after vCPU initialization?
Quoted 62 lines
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 useSYSREG_ID_AA64MMFR1_EL1_VHto select an nVHE-only EL2.
- Exposed the property only when KVM advertisesKVM_CAP_ARM_EL2_E2H0.- Derived
KVM_ARM_VCPU_HAS_EL2_E2H0from 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 rejectsSYSREG_ID_AA64MMFR1_EL1_VHbecause 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,
HaseebHaseeb Ashraf (3):
target/arm/kvm: Addkvm_arm_el2_e2h0_supported()target/arm/kvm: LetSYSREG_ID_AA64MMFR1_EL1_VHselect an nVHE EL2 tests/functional: Add a KVM selftest for an nVHE-only EL2docs/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(-)
Re: [RFC PATCH v2 0/3] arm/kvm: select an nVHE-only EL2 with SYSREG_ID_AA64MMFR1_EL1_VH
Hi Eric,
Thanks for the feedback.
I agree that exposing VH as an exception is appropriate for now. KVM does not expose VH through its writable ID-register interface; it is selected by KVM_ARM_VCPU_HAS_EL2_E2H0 during vCPU initialization. Keeping this explicit avoids prematurely adding a generic framework mechanism for which no other field currently has a requirement.
On the second point, my wording was imprecise. KVM updates the guest-visible ID registers automatically after vCPU initialization, and the guest sees the derived ID_AA64MMFR4_EL1 value. I referred only to QEMU's cached isar.idregs[] copy, where the framework currently deposits the explicitly requested VH field but does not re-read fields derived by KVM.
Thanks,
Haseeb
v3 cover arm/kvm: select an nVHE-only EL2 with SYSREG_ID_AA64MMFR1_EL1_VH
In reply to Haseeb AshrafFrom: 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. This RFC lets an nVHE-only guest hypervisor request that mode through:
-M virt,virtualization=on -accel kvm \
-cpu host,SYSREG_ID_AA64MMFR1_EL1_VH=0The series builds on Eric Auger's remaining v11 SYSREG framework. It is based on the rebased prerequisite commit corresponding to Eric's arm-cpu-model-v11-rc tip e32893ddcbe643d47919f55ae33ff71be2609a77. This dependency is intentional: a version which works without that framework would have to introduce a separate machine or CPU property, which was the approach rejected by Marc in v1.
References:
- Eric's v11 series:
https://patchew.org/QEMU/[email protected]/ - Eric's v11-rc branch: https://github.com/eauger/qemu/commits/arm-cpu-model-v11-rc - My v1: https://patchew.org/QEMU/[email protected]/
Changes since v2:
- Apply init-driven SYSREG properties before creating the scratch vCPU, so full CPU-model expansion reflects the requested init mode.
- Refresh ID_AA64MMFR1_EL1 and ID_AA64MMFR4_EL1 from KVM after vCPU initialization, keeping QEMU's cached model and QMP expansion in sync with the guest-visible registers.
Eric correctly pointed out that KVM already updates the guest-visible derived ID registers. The v2 gap was limited to QEMU's cache/model expansion view; v3 fixes that without changing the guest-visible behavior. With VH=0, query-cpu-model-expansion now reports ID_AA64MMFR4_EL1.NV_frac=0; with VH=1 it reports 1.
For the init-driven property question, this series keeps VH as an explicit exception because KVM_CAP_ARM_EL2_E2H0 currently exposes no writable-ID-register equivalent. A generic framework table can be considered if more such fields arise; there is no need to add a broader abstraction for this single case.
This RFC therefore also asks maintainers to confirm that using a SYSREG_<REG>_<FIELD> property for a value selected at vCPU init is an acceptable extension of Eric's framework. If that model is not accepted, this nVHE functionality should wait for the framework direction to settle. The standalone-property implementation from v1 is available as a fallback for the implementation discussion, but I would prefer not to make that upstream direction the default again.
TCG and HVF are unchanged by design.
Testing on an AmpereOne A192-32X with host kernel 6.18.29-arm64:
- TCG-only and KVM-only builds succeeded.
- TCG rejects SYSREG_ID_AA64MMFR1_EL1_VH.
- arm-cpu-features passed.
- All three AArch64 KVM functional selftests passed.
- QMP full expansion reports the KVM-derived MMFR4 state for both
VH=0 and VH=1.
- Linux 6.12.67 initialized KVM in nVHE mode with VH=0 and VHE mode with VH=1; QNX Hypervisor 8.0.5 reached login with both values.
- Xen dom0 reached login with VH=0 and did not boot with VH=1.
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/arm-qmp-cmds.c | 38 +++++++++++++++++ target/arm/cpu-idregs.h | 31 ++++++++++++++ target/arm/cpu64.c | 51 +++++++++++++++++----- target/arm/kvm-stub.c | 10 +++++ target/arm/kvm.c | 63 ++++++++++++++++++++++++++-- target/arm/kvm_arm.h | 21 ++++++++++ tests/functional/aarch64/test_kvm.py | 22 +++++++++- 8 files changed, 242 insertions(+), 16 deletions(-)
Signature · 1 line
2.34.1
RFC PATCH v3 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
@@ -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
@@ -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
@@ -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
RFC PATCH v3 2/3 target/arm/kvm: Let SYSREG_ID_AA64MMFR1_EL1_VH select an nVHE EL2
In reply to Haseeb AshrafFrom: 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. Apply this init-driven property before creating the scratch vCPU and use it when initializing real vCPUs. Setting VH=0 without virtualization=on is rejected.
KVM derives other ID register fields from the selected init mode. Re-read ID_AA64MMFR1_EL1 and ID_AA64MMFR4_EL1 after vCPU initialization so QEMU's cached CPU model and query-cpu-model-expansion agree with the values the guest sees.
---
v3:
- Apply init-driven properties before creating the scratch vCPU.
- Refresh MMFR1 and MMFR4 from KVM after vCPU initialization.
docs/system/arm/cpu-features.rst | 22 ++++++++++++ target/arm/arm-qmp-cmds.c | 38 +++++++++++++++++++++ target/arm/cpu-idregs.h | 31 +++++++++++++++++ target/arm/cpu64.c | 51 ++++++++++++++++++++++------ target/arm/kvm-stub.c | 5 +++ target/arm/kvm.c | 58 ++++++++++++++++++++++++++++++-- target/arm/kvm_arm.h | 8 +++++ 7 files changed, 199 insertions(+), 14 deletions(-)
docs/system/arm/cpu-features.rst +22 -0
@@ -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/arm-qmp-cmds.c +38 -0
@@ -98,6 +98,7 @@ qmp_query_arm_cpu_model_expansion(CpuModelExpansionType type,Visitor *visitor =NULL ;Error *err =NULL ;bool scratch_created = false; +bool e2h0_requested = false;const char *name;int fdarray[3 ];int fd =-1 ;
@@ -173,6 +174,38 @@ qmp_query_arm_cpu_model_expansion(CpuModelExpansionType type, goto bail_out; } +/* +* Init-driven SYSREG properties must be visible while the scratch +* vCPU is initialized, not only when the remaining properties are +* applied below. +*/ +for (const QDictEntry *entry =qdict_first (qdict_in); + entry !=NULL ; entry =qdict_next (qdict_in, entry)) { +const char *key =qdict_entry_key (entry); +QObject *val_obj =qdict_entry_value (entry); +Visitor *v; +uint64_t value; + +if (!strcmp (key,"SYSREG_ID_AA64MMFR1_EL1_VH" )) { + v =qobject_input_visitor_new (val_obj); +if (!visit_type_uint64 (v, key, &value, errp)) { +visit_free (v); + goto bail_out; + } +visit_free (v); + e2h0_requested = value ==0 ; + }else { +continue ; + } + + v =qobject_input_visitor_new (val_obj); +if (!object_property_set (obj, key, v, errp)) { +visit_free (v); + goto bail_out; + } +visit_free (v); + } +/** * Test SYSREG option settings * In full mode, build a scratch vcpu that reflects composite legacy
@@ -237,6 +270,11 @@ qmp_query_arm_cpu_model_expansion(CpuModelExpansionType type,visit_free (v); } +if (scratch_created && e2h0_requested && +kvm_arm_refresh_e2h0_idregs (ARM_CPU (obj), fd, errp)) { + goto bail_out; + } +visit_check_struct (visitor, &err);if (err) { goto bail_out;
target/arm/cpu-idregs.h +31 -0
@@ -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-stub.c +5 -0
@@ -47,6 +47,11 @@ bool kvm_arm_el2_e2h0_supported(void)return false; } +int kvm_arm_refresh_e2h0_idregs (ARMCPU *cpu,int fd,Error **errp) +{ +return 0 ; +} +/* * These functions should never actually be called without KVM support. */
target/arm/kvm.c +55 -3
83 lines not on this page · raw message
target/arm/kvm_arm.h +8 -0
@@ -208,6 +208,8 @@ bool kvm_arm_el2_supported(void);* KVM_ARM_VCPU_HAS_EL2_E2H0, and false otherwise. */ bool kvm_arm_el2_e2h0_supported (void ); + +int kvm_arm_refresh_e2h0_idregs (ARMCPU *cpu,int fd,Error **errp);#else static inline bool kvm_arm_aarch32_supported (void )
@@ -229,6 +231,12 @@ static inline bool kvm_arm_el2_e2h0_supported(void) {return false; } + +static inline int kvm_arm_refresh_e2h0_idregs (ARMCPU *cpu,int fd, +Error **errp) +{ +return 0 ; +}#endif /**
Signature · 1 line
2.34.1
Re: [RFC PATCH v3 2/3] target/arm/kvm: Let SYSREG_ID_AA64MMFR1_EL1_VH select an nVHE EL2
Quoted 371 lines · Haseeb Ashraf
!-------------------------------------------------------------------|
CAUTION: External Email|-------------------------------------------------------------------!
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_E2H0is requested atKVM_ARM_VCPU_INITtime. With that featureFEAT_VHEis 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 requestsKVM_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. Apply this init-driven property before creating the scratch vCPU and use it when initializing real vCPUs. Setting VH=0 without virtualization=on is rejected.KVM derives other ID register fields from the selected init mode. Re-read
ID_AA64MMFR1_EL1andID_AA64MMFR4_EL1after vCPU initialization so QEMU's cached CPU model and query-cpu-model-expansion agree with the values the guest sees.Suggested-by Marc Zyngier <[email protected]>Suggested-by Volodymyr Babchuk <[email protected]>Signed-off-by Haseeb Ashraf <[email protected]>---
v3:
- Apply init-driven properties before creating the scratch vCPU.
- Refresh MMFR1 and MMFR4 from KVM after vCPU initialization.docs/system/arm/cpu-features.rst | 22 ++++++++++++ target/arm/arm-qmp-cmds.c | 38 +++++++++++++++++++++ target/arm/cpu-idregs.h | 31 +++++++++++++++++ target/arm/cpu64.c | 51 ++++++++++++++++++++++------ target/arm/kvm-stub.c | 5 +++ target/arm/kvm.c | 58 ++++++++++++++++++++++++++++++-- target/arm/kvm_arm.h | 8 +++++ 7 files changed, 199 insertions(+), 14 deletions(-)docs/system/arm/cpu-features.rst +0
-0@@ -579,6 +579,28 @@ either not writable with the specific host kernel, unknown to this QEMUbuild, or intentionally not exposed because it overlaps a legacy composite option.
docs/system/arm/cpu-features.rst +22
-0+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/arm-qmp-cmds.c +38
-0@@ -98,6 +98,7 @@ qmp_query_arm_cpu_model_expansion(CpuModelExpansionType type,Visitor *visitor =NULL ;Error *err =NULL ;bool scratch_created = false; +bool e2h0_requested = false;const char *name;int fdarray[3 ];int fd =-1 ;@@ -173,6 +174,38 @@ qmp_query_arm_cpu_model_expansion(CpuModelExpansionType type, goto bail_out; } +/* +* Init-driven SYSREG properties must be visible while the scratch +* vCPU is initialized, not only when the remaining properties are +* applied below. +*/ +for (const QDictEntry *entry =qdict_first (qdict_in); + entry !=NULL ; entry =qdict_next (qdict_in, entry)) { +const char *key =qdict_entry_key (entry); +QObject *val_obj =qdict_entry_value (entry); +Visitor *v; +uint64_t value; + +if (!strcmp (key,"SYSREG_ID_AA64MMFR1_EL1_VH" )) { + v =qobject_input_visitor_new (val_obj); +if (!visit_type_uint64 (v, key, &value, errp)) { +visit_free (v); + goto bail_out; + } +visit_free (v); + e2h0_requested = value ==0 ; + }else { +continue ; + } + + v =qobject_input_visitor_new (val_obj); +if (!object_property_set (obj, key, v, errp)) { +visit_free (v); + goto bail_out; + } +visit_free (v); + } +/** * Test SYSREG option settings * In full mode, build a scratch vcpu that reflects composite legacy @@ -237,6 +270,11 @@ qmp_query_arm_cpu_model_expansion(CpuModelExpansionType type,visit_free (v); } +if (scratch_created && e2h0_requested && +kvm_arm_refresh_e2h0_idregs (ARM_CPU (obj), fd, errp)) { + goto bail_out; + } +visit_check_struct (visitor, &err);if (err) { goto bail_out;target/arm/cpu-idregs.h +31
-0@@ -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@@ -835,6 +835,21 @@ static ARM64SysRegField *arm_sysreg_get_field(ARM64SysReg *reg, int bit)return NULL ; } +bool arm_cpu_sysreg_prop_get (ARMCPU *cpu,int reg_idx,int shift, +uint64_t *value) +{ +gpointer value_ptr; +uint64_t key =IDREG_FIELD_KEY (reg_idx, shift); + +if (!g_hash_table_lookup_extended (cpu->sysreg_props, +GUINT_TO_POINTER (key),NULL , + &value_ptr)) { +return false; + } + *value =GPOINTER_TO_UINT (value_ptr); +return true; +} +/** * arm_cpu_validate_sysreg_prop: * @@ -879,6 +894,13 @@ bool arm_cpu_validate_sysreg_prop(ARMCPU *cpu, ARM64SysRegField *field,error_setg (errp,"%s is non null whereas pmu is unset" , propname);return false; } +/* nVHE-only EL2, selected at vcpu init time */ + }else if (!strcmp (propname,"SYSREG_ID_AA64MMFR1_EL1_VH" )) { +if (value ==0 && !cpu->has_el2) { +error_setg (errp,"%s=0 requires -machine virtualization=on" , + propname); +return false; + }/* aarch64=off */ }else if (!arm_feature (&cpu->env,ARM_FEATURE_AARCH64 )) { if ((!strcmp(propname, "SYSREG_ID_AA64PFR0_EL1_EL0") ||@@ -988,17 +1010,9 @@ static void get_sysreg_prop(Object *obj, Visitor *v,int index = field->index;int lower = field->shift;int length = field->length; -gpointer value_ptr;uint64_t value; -uint64_t key =IDREG_FIELD_KEY (index, lower); -gboolean exists; - - exists =g_hash_table_lookup_extended (cpu->sysreg_props, -GUINT_TO_POINTER (key),NULL , - &value_ptr); -if (exists) { - value =GPOINTER_TO_UINT (value_ptr); - }else { + +if (!arm_cpu_sysreg_prop_get (cpu, index, lower, &value)) {uint64_t mask =MAKE_64BIT_MASK (lower, length);uint64_t *idregs = cpu->isar.idregs;@@ -1028,6 +1042,20 @@ static const char * const aarch64_sysreg_prop_skip_list[] = { "SYSREG_ID_AA64ISAR2_EL1_GPA3", }; +/* +* Fields which KVM does not let userspace write with KVM_SET_ONE_REG, +* but which QEMU can still select through a vcpu init feature, see +* kvm_arm_vcpu_prepare_init_features(). ID_AA64MMFR1_EL1.VH is hidden +* by KVM_ARM_VCPU_HAS_EL2_E2H0, which requests an nVHE-only EL2. +*/ +bool arm_cpu_sysreg_field_init_driven (ARM64SysRegField *field) +{ +/* Compare the field first: this runs for every ID register field */ +return field->index ==ID_AA64MMFR1_EL1_IDX && + field->shift ==R_ID_AA64MMFR1_VH_SHIFT && +kvm_enabled () &&kvm_arm_el2_e2h0_supported (); +} +static bool aarch64_sysreg_prop_skipped (const char *prop_name) {for (int i =0 ; i <ARRAY_SIZE (aarch64_sysreg_prop_skip_list); i++) {@@ -1084,7 +1112,8 @@ aarch64_add_one_idreg_properties(Object *obj, ARM64SysReg *reg, } map = map & ~field_mask; -if (writable_only && !field_writable_mask) { +if (writable_only && !field_writable_mask && + !arm_cpu_sysreg_field_init_driven (field)) {continue ; }target/arm/kvm-stub.c +5
-0@@ -47,6 +47,11 @@ bool kvm_arm_el2_e2h0_supported(void)return false; } +int kvm_arm_refresh_e2h0_idregs (ARMCPU *cpu,int fd,Error **errp) +{ +return 0 ; +} +/* * These functions should never actually be called without KVM support. */ target/arm/kvm.c +45
-3@@ -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] =@@ -2183,6 +2189,30 @@ bool kvm_arm_el2_e2h0_supported(void)return kvm_check_extension (kvm_state,KVM_CAP_ARM_EL2_E2H0 ); } +int kvm_arm_refresh_e2h0_idregs (ARMCPU *cpu,int fd,Error **errp) +{ +static const ARMIDRegisterIdx indices[] = { +ID_AA64MMFR1_EL1_IDX , +ID_AA64MMFR4_EL1_IDX , + }; + +for (int i =0 ; i <ARRAY_SIZE (indices); i++) { +ARMIDRegisterIdx index = indices[i]; +int ret; + + ret =read_sys_reg64 (fd, &cpu->isar.idregs[index], +idregs_sysreg_to_kvm_reg ( + id_register_sysreg[index])); +if (ret) { +error_setg (errp,"failed to refresh %s after vCPU init" , + arm64_id_regs[index].name); +return ret; + } + } + +return 0 ; +} +bool kvm_arm_mte_supported (void ) {return kvm_check_extension (kvm_state,KVM_CAP_ARM_MTE );@@ -2239,7 +2269,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 ; + } } }@@ -2286,6 +2328,16 @@ int kvm_arch_init_vcpu(CPUState *cs, Error **errp)return ret; }
target/arm/kvm.c int kvm_arch_init_vcpu(CPUState *cs, Error **errp) · line 2340
Quoted 10 lines
target/arm/kvm.c +10
-0+uint64_t vh; +if (arm_cpu_sysreg_prop_get (cpu,ID_AA64MMFR1_EL1_IDX , +R_ID_AA64MMFR1_VH_SHIFT , &vh) && + vh ==0 ) { + ret =kvm_arm_refresh_e2h0_idregs (cpu,CPU (cpu)->kvm_fd, errp); +if (ret) { +return ret; + } + } +
There should not be a need to explicitly refreshing this specific registers.
I think this should be handled by Eric’s series by refreshing all the ID registers after write-back.
Eric, what is your view on this?
Thanks,
Khushit
Quoted 32 lines
target/arm/kvm.c +0
-0if (cpu_isar_feature (aa64_sve, cpu)) { ret =kvm_arm_sve_set_vls (cpu);if (ret) {target/arm/kvm_arm.h +8
-0@@ -208,6 +208,8 @@ bool kvm_arm_el2_supported(void);* KVM_ARM_VCPU_HAS_EL2_E2H0, and false otherwise. */ bool kvm_arm_el2_e2h0_supported (void ); + +int kvm_arm_refresh_e2h0_idregs (ARMCPU *cpu,int fd,Error **errp);#else static inline bool kvm_arm_aarch32_supported (void )@@ -229,6 +231,12 @@ static inline bool kvm_arm_el2_e2h0_supported(void) {return false; } + +static inline int kvm_arm_refresh_e2h0_idregs (ARMCPU *cpu,int fd, +Error **errp) +{ +return 0 ; +}#endif /** Signature · 1 line
2.34.1
RFC PATCH v3 3/3 tests/functional: Add a KVM selftest for an nVHE-only EL2
In reply to Haseeb AshrafFrom: 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.
---
v3:
- No changes from v2.
tests/functional/aarch64/test_kvm.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-)
tests/functional/aarch64/test_kvm.py +20 -2
@@ -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
Re: [RFC PATCH v3 0/3] arm/kvm: select an nVHE-only EL2 with SYSREG_ID_AA64MMFR1_EL1_VH
In reply to Haseeb AshrafQuoted 50 lines · Haseeb Ashraf
!-------------------------------------------------------------------|
CAUTION: External Email|-------------------------------------------------------------------!
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_E2H0is requested at vCPU initialization. This RFC lets an nVHE-only guest hypervisor request that mode through:-M virt,virtualization=on -accel kvm \ -cpu host,SYSREG_ID_AA64MMFR1_EL1_VH=0The series builds on Eric Auger's remaining v11 SYSREG framework. It is based on the rebased prerequisite commit corresponding to Eric's arm-cpu-model-v11-rc tip
e32893ddcbe643d47919f55ae33ff71be2609a77. This dependency is intentional: a version which works without that framework would have to introduce a separate machine or CPU property, which was the approach rejected by Marc in v1.References:
- Eric's v11 series:
https://urldefense.proofpoint.com/v2/url?u=https-3A__patchew.org_QEMU_20261030131406.461599-2D1-2Deric.auger-40redhat.com_&d=DwIDAg&c=s883GpUCOChKOHiocYtGcg&r=PGWMyignA0NiDmTlyP7vOTHozBws_VN86yrVmSMkBp0&m=x45rO9ktlL_N_DqruwZCqdxIKJMqZVba_77ghKIlKoGk02-X8HO-rG_CnCF5WE52&s=5PdtSrSqg2BFk_IuGIK9Crkvyj98oCgRTmTB9Qpw5Bo&e= - Eric's v11-rc branch: https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_eauger_qemu_commits_arm-2Dcpu-2Dmodel-2Dv11-2Drc&d=DwIDAg&c=s883GpUCOChKOHiocYtGcg&r=PGWMyignA0NiDmTlyP7vOTHozBws_VN86yrVmSMkBp0&m=x45rO9ktlL_N_DqruwZCqdxIKJMqZVba_77ghKIlKoGk02-X8HO-rG_CnCF5WE52&s=xs9AECI1g2J6lF0ajI3MWrseWIxJik2nbNg61Sser4I&e= - My v1: https://urldefense.proofpoint.com/v2/url?u=https-3A__patchew.org_QEMU_20261121095636.289814-2D1-2Dhaseebashraf091-40gmail.com_&d=DwIDAg&c=s883GpUCOChKOHiocYtGcg&r=PGWMyignA0NiDmTlyP7vOTHozBws_VN86yrVmSMkBp0&m=x45rO9ktlL_N_DqruwZCqdxIKJMqZVba_77ghKIlKoGk02-X8HO-rG_CnCF5WE52&s=cxnvVEn7tWyGO1QnnlIwe3YKIqfxSwt6Ies6eCTJEzM&e=Changes since v2:
- Apply init-driven SYSREG properties before creating the scratch vCPU, so full CPU-model expansion reflects the requested init mode.
- RefreshID_AA64MMFR1_EL1andID_AA64MMFR4_EL1from KVM after vCPU initialization, keeping QEMU's cached model and QMP expansion in sync with the guest-visible registers.Eric correctly pointed out that KVM already updates the guest-visible derived ID registers. The v2 gap was limited to QEMU's cache/model expansion view; v3 fixes that without changing the guest-visible behavior. With VH=0, query-cpu-model-expansion now reports ID_AA64MMFR4_EL1.NV_frac=0; with VH=1 it reports 1.
Quoted 5 lines
For the init-driven property question, this series keeps VH as an explicit exception because
KVM_CAP_ARM_EL2_E2H0currently exposes no writable-ID-register equivalent. A generic framework table can be considered if more such fields arise; there is no need to add a broader abstraction for this single case.
There are many such fields like PAuth (APA, GPA, …) and SVE. They are not part of the SYSREG_ framework as there already exists boolean properties for them.
(Another way to do this is having -cpu …,vhe=on/off property)
Quoted 43 lines
This RFC therefore also asks maintainers to confirm that using a SYSREG_<REG>_<FIELD> property for a value selected at vCPU init is an acceptable extension of Eric's framework. If that model is not accepted, this nVHE functionality should wait for the framework direction to settle. The standalone-property implementation from v1 is available as a fallback for the implementation discussion, but I would prefer not to make that upstream direction the default again.
TCG and HVF are unchanged by design.
Testing on an AmpereOne A192-32X with host kernel 6.18.29-arm64:
- TCG-only and KVM-only builds succeeded.
- TCG rejectsSYSREG_ID_AA64MMFR1_EL1_VH.
- arm-cpu-features passed.
- All three AArch64 KVM functional selftests passed.
- QMP full expansion reports the KVM-derived MMFR4 state for both VH=0 and VH=1.
- Linux 6.12.67 initialized KVM in nVHE mode with VH=0 and VHE mode with VH=1; QNX Hypervisor 8.0.5 reached login with both values.
- Xen dom0 reached login with VH=0 and did not boot with VH=1.Thanks,
HaseebHaseeb Ashraf (3):
target/arm/kvm: Addkvm_arm_el2_e2h0_supported()target/arm/kvm: LetSYSREG_ID_AA64MMFR1_EL1_VHselect an nVHE EL2 tests/functional: Add a KVM selftest for an nVHE-only EL2docs/system/arm/cpu-features.rst | 22 ++++++++++ target/arm/arm-qmp-cmds.c | 38 +++++++++++++++++ target/arm/cpu-idregs.h | 31 ++++++++++++++ target/arm/cpu64.c | 51 +++++++++++++++++----- target/arm/kvm-stub.c | 10 +++++ target/arm/kvm.c | 63 ++++++++++++++++++++++++++-- target/arm/kvm_arm.h | 21 ++++++++++ tests/functional/aarch64/test_kvm.py | 22 +++++++++- 8 files changed, 242 insertions(+), 16 deletions(-)Signature · 1 line
2.34.1
Re: [RFC PATCH v3 0/3] arm/kvm: select an nVHE-only EL2 with SYSREG_ID_AA64MMFR1_EL1_VH
Hi Khushit,
Quoted 4 lines · Khushit Shah
There are many such fields like PAuth (APA, GPA, …) and SVE. They are not part of the
SYSREG_framework as there already exists boolean properties for them.
(Another way to do this is having -cpu …,vhe=on/off property)
The v1 series was closer to what you are suggesting: it added a standalone vhe=on/off property and used it to select the requested KVM vCPU initialization mode.
Could you please also review the v1 implementation?
https://lore.kernel.org/qemu-devel/[email protected]/
I changed direction for v2 following Marc's feedback against adding a separate property.
Thanks,
Haseeb