Viridian changes for Hyper-V as a guest
v3 cover Viridian changes for Hyper-V as a guest
Hi,
Here are some changes to get the existing Virdian enlightenments working when running a Windows 11 guest with Hyper-V enabled
Hyper-V also depends on some unimplemented MSRs to boot, but that will be addressed in a separate series.
Thanks,
Ross
Ross Lagerwall (3):
x86/viridian: Workaround Hyper-V GP fault writing to MSR x86/viridian: Implement synthetic timer direct mode tools/libxl: Add support for Viridian stimer direct mode
docs/man/xl.cfg.5.pod.in | 5 ++++ tools/include/libxl.h | 6 ++++ tools/libs/light/libxl_types.idl | 1 + tools/libs/light/libxl_x86.c | 5 ++++ xen/arch/x86/hvm/viridian/synic.c | 9 ++++-- xen/arch/x86/hvm/viridian/time.c | 44 ++++++++++++++++++++++++---- xen/arch/x86/hvm/viridian/viridian.c | 3 ++ xen/include/public/hvm/params.h | 7 ++++- 8 files changed, 71 insertions(+), 9 deletions(-)
Signature · 1 line
2.53.0
PATCH v3 1/3 x86/viridian: Workaround Hyper-V GP fault writing to MSR
The Viridian spec requires the vector to be >= 0x10 when writing to the SINTx MSR, otherwise it should #GP fault.
The spec-defined initial value is 0x10000, i.e. the vector is 0. On startup, for some reason Windows 11 Hyper-V tries to set the MSR to the spec-defined initial value and since the vector is 0, it GP faults. Fix this by doing as Hyper-V does and only fault if the SINT is unmasked.
---
In v3:
Copy Hyper-V behaviour - only check the vector if not masked.
xen/arch/x86/hvm/viridian/synic.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/viridian/synic.c +7 -2
@@ -157,9 +157,14 @@ int viridian_synic_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)if ( !(viridian_feature_mask (d) & HVMPV_synic) )return X86EMUL_EXCEPTION ; -/* Vectors must be in the range 0x10-0xff inclusive */ +/* +* Vectors must be in the range 0x10-0xff inclusive unless the vector +* is masked since Windows 11 Hyper-V (26H1) has been seen to write +* this MSR to its default value, despite not being a spec compliant +* value. +*/ new.as_uint64 = val; -if ( new.vector <0x10 ) +if ( new.vector <0x10 && !new.masked )return X86EMUL_EXCEPTION ;/*
Signature · 1 line
2.53.0
Re: [PATCH v3 1/3] x86/viridian: Workaround Hyper-V GP fault writing to MSR
Quoted 9 lines · Ross Lagerwall
The Viridian spec requires the vector to be >=
0x10when writing to the SINTx MSR, otherwise it should #GP fault.
The spec-defined initial value is0x10000, i.e. the vector is 0. On startup, for some reason Windows 11 Hyper-V tries to set the MSR to the spec-defined initial value and since the vector is 0, it GP faults. Fix this by doing as Hyper-V does and only fault if the SINT is unmasked.Signed-off-by Ross Lagerwall <[email protected]>
PATCH v3 2/3 x86/viridian: Implement synthetic timer direct mode
In reply to Ross LagerwallIn direct mode, the timer asserts an interrupt on expiration rather than using a SynIC message. It is useful to implement this since Windows 11's Hyper-V can only use synthetic timers in direct mode.
To avoid changing behaviour in existing guests, add a new Viridian flag, stimer_direct, to control whether this feature is visible and usable.
At the same time, check that the reserved bits are zero and the APIC vector is only set when using direct mode since this is enforced by Hyper-V (although not mentioned in the spec).
---
In v3:
* For compatibility, keep the existing behaviour when the feature flag is not enabled.
* GP fault if reserved bits are set.
* GP fault if APIC vector is set when !direct.
xen/arch/x86/hvm/viridian/time.c | 44 ++++++++++++++++++++++++---- xen/arch/x86/hvm/viridian/viridian.c | 3 ++ xen/include/public/hvm/params.h | 7 ++++- 3 files changed, 47 insertions(+), 7 deletions(-)
xen/arch/x86/hvm/viridian/time.c +38 -6
@@ -223,6 +223,21 @@ static void start_stimer(struct viridian_stimer *vs)set_timer (&vs->timer, timeout +NOW ()); } +static bool stimer_direct_mode (const struct vcpu *v, +const union hv_stimer_config *config) +{ +return (viridian_feature_mask (v->domain) & HVMPV_stimer_direct) && + config->direct_mode; +} + +static void stimer_deliver_direct (struct vcpu *v,const struct viridian_stimer *vs) +{ +struct vlapic *vlapic =vcpu_vlapic (v); + +if (vlapic_enabled (vlapic) ) +vlapic_set_irq (vlapic, vs->config.apic_vector,0 ); +} +static void poll_stimer (struct vcpu *v,unsigned int stimerx) {struct viridian_vcpu *vv = v->arch.hvm.viridian;
@@ -242,9 +257,11 @@ static void poll_stimer(struct vcpu *v, unsigned int stimerx)if ( !test_bit (stimerx, &vv->stimer_pending) )return ; -if ( !viridian_synic_deliver_timer_msg (v, vs->config.sintx, - stimerx, vs->expiration, -time_ref_count (v->domain)) ) +if (stimer_direct_mode (v, &vs->config) ) +stimer_deliver_direct (v, vs); +else if ( !viridian_synic_deliver_timer_msg (v, vs->config.sintx, + stimerx, vs->expiration, +time_ref_count (v->domain)) )return ;clear_bit (stimerx, &vv->stimer_pending);
@@ -361,6 +378,7 @@ int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)case HV_X64_MSR_STIMER2_CONFIG :case HV_X64_MSR_STIMER3_CONFIG : { +union hv_stimer_config new;unsigned int stimerx = (idx -HV_X64_MSR_STIMER0_CONFIG ) /2 ;struct viridian_stimer *vs = &array_access_nospec (vv->stimer, stimerx);
@@ -368,11 +386,21 @@ int viridian_time_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val)if ( !(viridian_feature_mask (d) & HVMPV_stimer) )return X86EMUL_EXCEPTION ; + new.as_uint64 = val; + +if ( new.reserved_z0 || new.reserved_z1 ) +return X86EMUL_EXCEPTION ; + +if (stimer_direct_mode (v, &new) ? + new.apic_vector <0x10 : new.apic_vector ) +return X86EMUL_EXCEPTION ; +stop_stimer (vs); vs->config.as_uint64 = val; -if ( !vs->config.sintx || !vs->count ) +if ( (!stimer_direct_mode (v, &vs->config) && !vs->config.sintx) || + !vs->count ) vs->config.enable =0 ;if ( vs->config.enable )
@@ -583,8 +611,12 @@ void viridian_time_load_vcpu_ctxt( vs->config.as_uint64 = ctxt->stimer_config_msr[i]; vs->count = ctxt->stimer_count_msr[i]; -if ( !vs->config.sintx || !vs->count ) -/* Reject enabling with a zero sintx or count fields. */ +if ( (!stimer_direct_mode (v, &vs->config) && !vs->config.sintx) || + !vs->count ) +/* +* Reject enabling with a zero sintx (if not using direct mode) or +* zero count field. +*/ vs->config.enable =0 ; } }
xen/arch/x86/hvm/viridian/viridian.c +3 -0
@@ -78,6 +78,7 @@ typedef union _HV_CRASH_CTL_REG_CONTENTS#define CPUID3D_CPU_DYNAMIC_PARTITIONING (1 << 3)#define CPUID3D_CRASH_MSRS (1 << 10)#define CPUID3D_SINT_POLLING (1 << 17) +#define CPUID3D_STIMER_DIRECT_MODE (1 << 19)/* Viridian CPUID leaf 4: Implementation Recommendations. */ #define CPUID4A_HCALL_REMOTE_TLB_FLUSH (1 << 2)
@@ -185,6 +186,8 @@ void cpuid_viridian_leaves(const struct vcpu *v, uint32_t leaf, res->d |=CPUID3D_CRASH_MSRS ;if (viridian_feature_mask (d) & HVMPV_synic ) res->d |=CPUID3D_SINT_POLLING ; +if (viridian_feature_mask (d) & HVMPV_stimer_direct ) + res->d |=CPUID3D_STIMER_DIRECT_MODE ;break ; }
xen/include/public/hvm/params.h +6 -1
@@ -159,6 +159,10 @@#define _HVMPV_cpu_hotplug 12#define HVMPV_cpu_hotplug (1 << _HVMPV_cpu_hotplug) +/* Enable STIMER direct mode */ +#define _HVMPV_stimer_direct 13 +#define HVMPV_stimer_direct (1 << _HVMPV_stimer_direct) +#define HVMPV_feature_mask \ (HVMPV_base_freq | \ HVMPV_no_freq | \
@@ -172,7 +176,8 @@ HVMPV_hcall_ipi | \ HVMPV_ex_processor_masks | \ HVMPV_no_vp_limit | \ - HVMPV_cpu_hotplug) + HVMPV_cpu_hotplug | \ + HVMPV_stimer_direct)#endif
Signature · 1 line
2.53.0
Re: [PATCH v3 2/3] x86/viridian: Implement synthetic timer direct mode
Quoted 4 lines · Ross Lagerwall
In direct mode, the timer asserts an interrupt on expiration rather than using a SynIC message. It is useful to implement this since Windows 11's Hyper-V can only use synthetic timers in direct mode.
Quoted 8 lines
To avoid changing behaviour in existing guests, add a new Viridian flag,
stimer_direct, to control whether this feature is visible and usable.At the same time, check that the reserved bits are zero and the APIC vector is only set when using direct mode since this is enforced by Hyper-V (although not mentioned in the spec).
Signed-off-by Ross Lagerwall <[email protected]>
PATCH v3 3/3 tools/libxl: Add support for Viridian stimer direct mode
In reply to Ross LagerwallAdd support to libxl for using the stimer enlightenments in direct mode.
---
In v3: Fix typo in docs
docs/man/xl.cfg.5.pod.in | 5 +++++ tools/include/libxl.h | 6 ++++++ tools/libs/light/libxl_types.idl | 1 + tools/libs/light/libxl_x86.c | 5 +++++ 4 files changed, 17 insertions(+)
docs/man/xl.cfg.5.pod.in +5 -0
@@ -2496,6 +2496,11 @@ ticks and hence enabling this group will ensure that ticks will be consistent with use of an enlightened time source (B<time_ref_count> or B<reference_tsc>). +=item B<stimer_direct> + +This set is the same as the B<stimer> set but additionally supports +using synthetic timers in direct mode. + =item B<hcall_ipi> This set incorporates use of a hypercall for interprocessor interrupts.
tools/include/libxl.h +6 -0
@@ -381,6 +381,12 @@*/ #define LIBXL_HAVE_VIRIDIAN_HCALL_IPI 1 +/* +* LIBXL_HAVE_VIRIDIAN_STIMER_DIRECT indicates that the 'stimer_direct' value +* is present in the viridian enlightenment enumeration. +*/ +#define LIBXL_HAVE_VIRIDIAN_STIMER_DIRECT 1 +/* * LIBXL_HAVE_BUILDINFO_HVM_ACPI_LAPTOP_SLATE indicates that * libxl_domain_build_info has the u.hvm.acpi_laptop_slate field.
tools/libs/light/libxl_types.idl +1 -0
@@ -260,6 +260,7 @@ libxl_viridian_enlightenment = Enumeration("viridian_enlightenment", [ (10, "ex_processor_masks"), (11, "no_vp_limit"), (12, "cpu_hotplug"), + (13, "stimer_direct"), ]) libxl_hdtype = Enumeration("hdtype", [
tools/libs/light/libxl_x86.c +5 -0
@@ -389,6 +389,11 @@ static int hvm_set_viridian_features(libxl__gc *gc, uint32_t domid,if (libxl_bitmap_test (&enlightenments,LIBXL_VIRIDIAN_ENLIGHTENMENT_CPU_HOTPLUG )) mask |= HVMPV_cpu_hotplug; +if (libxl_bitmap_test (&enlightenments, +LIBXL_VIRIDIAN_ENLIGHTENMENT_STIMER_DIRECT )) + mask |= HVMPV_time_ref_count | HVMPV_synic | HVMPV_stimer | + HVMPV_stimer_direct; +if (mask !=0 && xc_hvm_param_set(CTX->xch, domid,
Signature · 1 line
2.53.0
Re: [PATCH v3 3/3] tools/libxl: Add support for Viridian stimer direct mode
Quoted 64 lines · Ross Lagerwall
Add support to libxl for using the stimer enlightenments in direct mode.
Signed-off-by Ross Lagerwall <[email protected]>---
In v3: Fix typo in docs
docs/man/xl.cfg.5.pod.in | 5 +++++ tools/include/libxl.h | 6 ++++++ tools/libs/light/libxl_types.idl | 1 + tools/libs/light/libxl_x86.c | 5 +++++ 4 files changed, 17 insertions(+)docs/man/xl.cfg.5.pod.in +5
-0@@ -2496,6 +2496,11 @@ ticks and hence enabling this group will ensure that ticks will be consistent with use of an enlightened time source (B<time_ref_count> or B<reference_tsc>). +=item B<stimer_direct> + +This set is the same as the B<stimer> set but additionally supports +using synthetic timers in direct mode. + =item B<hcall_ipi> This set incorporates use of a hypercall for interprocessor interrupts.tools/include/libxl.h +6
-0@@ -381,6 +381,12 @@*/ #define LIBXL_HAVE_VIRIDIAN_HCALL_IPI 1 +/* +* LIBXL_HAVE_VIRIDIAN_STIMER_DIRECT indicates that the 'stimer_direct' value +* is present in the viridian enlightenment enumeration. +*/ +#define LIBXL_HAVE_VIRIDIAN_STIMER_DIRECT 1 +/* * LIBXL_HAVE_BUILDINFO_HVM_ACPI_LAPTOP_SLATE indicates that * libxl_domain_build_info has the u.hvm.acpi_laptop_slate field. tools/libs/light/libxl_types.idl +1
-0@@ -260,6 +260,7 @@ libxl_viridian_enlightenment = Enumeration("viridian_enlightenment", [ (10, "ex_processor_masks"), (11, "no_vp_limit"), (12, "cpu_hotplug"), + (13, "stimer_direct"), ]) libxl_hdtype = Enumeration("hdtype", [tools/libs/light/libxl_x86.c +0
-0@@ -389,6 +389,11 @@ static int hvm_set_viridian_features(libxl__gc *gc, uint32_t domid,if (libxl_bitmap_test (&enlightenments,LIBXL_VIRIDIAN_ENLIGHTENMENT_CPU_HOTPLUG ))
tools/libs/light/libxl_x86.c static int hvm_set_viridian_features(libxl__gc *gc, uint32_t domid, · line 399
Quoted 10 lines
tools/libs/light/libxl_x86.c +5
-0mask |= HVMPV_cpu_hotplug; +if (libxl_bitmap_test (&enlightenments, +LIBXL_VIRIDIAN_ENLIGHTENMENT_STIMER_DIRECT )) + mask |= HVMPV_time_ref_count | HVMPV_synic | HVMPV_stimer | + HVMPV_stimer_direct; +if (mask !=0 && xc_hvm_param_set(CTX->xch, domid,
Ping. Note that the Xen side of this has already been committed.
Thanks,
Ross
Re: [PATCH v3 3/3] tools/libxl: Add support for Viridian stimer direct mode
In reply to Ross LagerwallQuoted 3 lines · Ross Lagerwall
Add support to libxl for using the stimer enlightenments in direct mode.
Signed-off-by Ross Lagerwall <[email protected]>
Thanks,
--
Anthony Perard | Vates XCP-ng Developer
XCP-ng & Xen Orchestra - Vates solutions
web: https://vates.tech