whpx: i386: Some more changes
v2 cover whpx: i386: Some more changes
This applies on top of the "whpx: i386: bug fixes, feature probing and CPUID" series.
"target: i386: HLT type that ignores EFLAGS.IF" is probably the most worthwhile patch here, and that not-quite-an-HLT might be what's needed to reduce CPU idle on KVM on recent Windows versions too..
v1 -> v2:
Oops, my v1 had a bug in HV_X64_MSR_GUEST_IDLE emulation, We should not inject interrupts after it ignoring EFLAGS.IF. That matters just for the wake from HLT.
Trapping one more CPUID for x2APIC system topology, and some more x2APIC awareness. For the kernel-irqchip=off case, keep the hypervisor aware of the initial APIC ID we're hoosing anyways.
Based-on: <[email protected]>
Mohamed Mediouni (10):
whpx: i386: set apicbase value only on success
whpx: i386: unknown MSR configurability
whpx: i386: enable GuestIdleReg enlightenment
whpx: i386: tighten APIC base validity check
whpx: i386: ignore vpassist when kernel-irqchip=off
target: i386: HLT type that ignores EFLAGS.IF
whpx: i386: add HV_X64_MSR_GUEST_IDLE when !kernel-irqchip
whpx: i386: one more CPUID
whpx: i386: some x2APIC awareness
whpx: i386: set WHvX64RegisterInitialApicId
accel/whpx/whpx-common.c | 38 +++++++++++++++++ include/system/whpx-internal.h | 1 + target/i386/cpu.c | 10 +++-- target/i386/cpu.h | 2 + target/i386/whpx/whpx-all.c | 77 +++++++++++++++++++++++++++++++--- target/i386/whpx/whpx-apic.c | 12 +++++- 6 files changed, 129 insertions(+), 11 deletions(-)
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v2 1/10 whpx: i386: set apicbase value only on success
--- target/i386/whpx/whpx-all.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
target/i386/whpx/whpx-all.c +2 -1
@@ -2001,8 +2001,9 @@ int whpx_vcpu_run(CPUState *cpu)int msr_ret =cpu_set_apic_base (X86_CPU (cpu)->apic_state, val);if (msr_ret <0 ) {x86_emul_raise_exception (&X86_CPU (cpu)->env,EXCP0D_GPF ,0 ); + }else { +whpx_set_reg (cpu, WHvX64RegisterApicBase, reg); } -whpx_set_reg (cpu, WHvX64RegisterApicBase, reg); } }
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v2 2/10 whpx: i386: unknown MSR configurability
In reply to Mohamed MediouniAdd an option to inject back a GPF for unknown MSRs.
Keep it on by default for now as Linux expects accesses to some AMD-specific MSRs to always succeed when on an AMD host.
--- accel/whpx/whpx-common.c | 38 ++++++++++++++++++++++++++++++++++ include/system/whpx-internal.h | 1 + target/i386/whpx/whpx-all.c | 4 ++++ 3 files changed, 43 insertions(+)
accel/whpx/whpx-common.c +38 -0
@@ -505,6 +505,38 @@ static void whpx_set_hyperv(Object *obj, Visitor *v, } } +static void whpx_set_unknown_msr (Object *obj,Visitor *v, +const char *name,void *opaque, +Error **errp) +{ +struct whpx_state *whpx = &whpx_global; +OnOffAuto mode; + +if (!visit_type_OnOffAuto (v, name, &mode, errp)) { +return ; + } + +switch (mode) { +case ON_OFF_AUTO_ON : + whpx->ignore_unknown_msr = true; +break ; + +case ON_OFF_AUTO_OFF : + whpx->ignore_unknown_msr = false; +break ; + +case ON_OFF_AUTO_AUTO : + whpx->ignore_unknown_msr = true; +break ; +default : +/* +* The value was checked in visit_type_OnOffAuto() above. If +* we get here, then something is wrong in QEMU. +*/ +abort (); + } +} +static void whpx_cpu_accel_class_init (ObjectClass *oc,const void *data) {AccelCPUClass *acc =ACCEL_CPU_CLASS (oc);
@@ -538,6 +570,11 @@ static void whpx_accel_class_init(ObjectClass *oc, const void *data) NULL, NULL);object_class_property_set_description (oc,"hyperv" ,"Configure Hyper-V enlightenments" ); +object_class_property_add (oc,"ignore-unknown-msr" ,"OnOffAuto" , +NULL , whpx_set_unknown_msr, +NULL ,NULL ); +object_class_property_set_description (oc,"ignore-unknown-msr" , +"Configure unknown MSR behavior" ); }static void whpx_accel_instance_init (Object *obj)
@@ -552,6 +589,7 @@ static void whpx_accel_instance_init(Object *obj) whpx->hyperv_enlightenments_required = false;/* Value determined at whpx_accel_init */ whpx->hyperv_enlightenments_enabled = false; + whpx->ignore_unknown_msr = true; }static const TypeInfo whpx_accel_type = {
include/system/whpx-internal.h +1 -0
@@ -47,6 +47,7 @@ struct whpx_state {bool hyperv_enlightenments_required;bool hyperv_enlightenments_enabled; +bool ignore_unknown_msr; };extern struct whpx_state whpx_global;
target/i386/whpx/whpx-all.c +4 -0
@@ -2048,6 +2048,10 @@ int whpx_vcpu_run(CPUState *cpu) vcpu->exit_ctx.MsrAccess.AccessInfo.IsWrite); } +if (!is_known_msr && !whpx->ignore_unknown_msr) { +x86_emul_raise_exception (&X86_CPU (cpu)->env,EXCP0D_GPF ,0 ); + } + hr = whp_dispatch.WHvSetVirtualProcessorRegisters( whpx->partition, cpu->cpu_index,
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v2 3/10 whpx: i386: enable GuestIdleReg enlightenment
In reply to Mohamed MediouniThis corresponds to HV_X64_MSR_GUEST_IDLE (0x400000f0).
This enlightenment is only available by the HV when using the Hyper-V LAPIC.
--- target/i386/whpx/whpx-all.c | 1 + 1 file changed, 1 insertion(+)
target/i386/whpx/whpx-all.c +1 -0
@@ -2686,6 +2686,7 @@ int whpx_accel_init(AccelState *as, MachineState *ms) synthetic_features.Bank0.AccessIntrCtrlRegs =1 ; synthetic_features.Bank0.SyntheticClusterIpi =1 ; synthetic_features.Bank0.DirectSyntheticTimers =1 ; + synthetic_features.Bank0.AccessGuestIdleReg =1 ;/* * These technically work without the Hyper-V LAPIC * but behave oddly for multi-core VMs.
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v2 4/10 whpx: i386: tighten APIC base validity check
In reply to Mohamed Mediouni--- target/i386/whpx/whpx-all.c | 3 +++ 1 file changed, 3 insertions(+)
target/i386/whpx/whpx-all.c +3 -0
@@ -1993,6 +1993,9 @@ int whpx_vcpu_run(CPUState *cpu)if (vcpu->exit_ctx.MsrAccess.MsrNumber ==MSR_IA32_APICBASE ) { is_known_msr =1 ; +if (val &MSR_IA32_APICBASE_RESERVED ) { +x86_emul_raise_exception (&X86_CPU (cpu)->env,EXCP0D_GPF ,0 ); + }if (!vcpu->exit_ctx.MsrAccess.AccessInfo.IsWrite) {/* Read path unreachable on Hyper-V */ abort ();
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v2 5/10 whpx: i386: ignore vpassist when kernel-irqchip=off
In reply to Mohamed MediouniLinux tries to set vpassist even when none of the enlightenments using it are available.
So ignore the page it sets.
--- target/i386/whpx/whpx-all.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
target/i386/whpx/whpx-all.c +13 -0
@@ -51,6 +51,7 @@#define HYPERV_APIC_BUS_FREQUENCY (200000000ULL)/* for kernel-irqchip=off */ #define HV_X64_MSR_APIC_FREQUENCY 0x40000023 +#define HV_X64_MSR_VP_ASSIST_PAGE 0x40000073static bool is_modern_os = true;
@@ -2033,6 +2034,18 @@ int whpx_vcpu_run(CPUState *cpu) } } } + +/* +* Linux tries to use it anyway even when not exposed. +* Ignore the write as the VP assist page is not used. +*/ +if (vcpu->exit_ctx.MsrAccess.MsrNumber ==HV_X64_MSR_VP_ASSIST_PAGE + && vcpu->exit_ctx.MsrAccess.AccessInfo.IsWrite + && !whpx_irqchip_in_kernel () + && whpx->hyperv_enlightenments_enabled) { + is_known_msr =1 ; + } +/* * For all unsupported MSR access we: * ignore writes
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v2 6/10 target: i386: HLT type that ignores EFLAGS.IF
In reply to Mohamed MediouniThe TLFS says:
Quoted 5 lines
A partition which possesses the AccessGuestIdleMsr privilege may trigger entry into the virtual processor idle sleep state through a read to the hypervisor-defined MSR
HV_X64_MSR_GUEST_IDLE. The virtual processor will be woken when an interrupt arrives, regardless of whether the interrupt is enabled on the virtual processor or not.
Meanwhile, Windows 24H2+ calls this MSR anyway without the privilege being set.
Add the infrastructure to support it on the generic QEMU side.
--- target/i386/cpu.c | 10 ++++++---- target/i386/cpu.h | 2 ++ 2 files changed, 8 insertions(+), 4 deletions(-)
target/i386/cpu.c +6 -4
@@ -10482,13 +10482,15 @@ int x86_cpu_pending_interrupt(CPUState *cs, int interrupt_request) (((env->hflags2 &HF2_VINTR_MASK ) && (env->hflags2 &HF2_HIF_MASK )) || (!(env->hflags2 &HF2_VINTR_MASK ) && - (env->eflags &IF_MASK && - !(env->hflags & HF_INHIBIT_IRQ_MASK))))) { + ((env->eflags &IF_MASK && + !(env->hflags &HF_INHIBIT_IRQ_MASK )) + || env->hflags2 & HF2_HYPERV_HLT_MASK)))) { return CPU_INTERRUPT_HARD; } else if (env->hflags2 & HF2_VGIF_MASK) {if ((interrupt_request &CPU_INTERRUPT_VIRQ ) && - (env->eflags &IF_MASK ) && - !(env->hflags &HF_INHIBIT_IRQ_MASK )) { + ((env->eflags &IF_MASK && + !(env->hflags &HF_INHIBIT_IRQ_MASK )) + || env->hflags2 &HF2_HYPERV_HLT_MASK )) {return CPU_INTERRUPT_VIRQ ; } }
target/i386/cpu.h +2 -0
@@ -225,6 +225,7 @@ typedef enum X86Seg {#define HF2_NPT_SHIFT 6/* Nested Paging enabled */ #define HF2_IGNNE_SHIFT 7/* Ignore CR0.NE=0 */ #define HF2_VGIF_SHIFT 8/* Can take VIRQ*/ +#define HF2_HYPERV_HLT_SHIFT 9/* Hyper-V HV_X64_MSR_GUEST_IDLE */ #define HF2_GIF_MASK (1 << HF2_GIF_SHIFT)#define HF2_HIF_MASK (1 << HF2_HIF_SHIFT)
@@ -235,6 +236,7 @@ typedef enum X86Seg {#define HF2_NPT_MASK (1 << HF2_NPT_SHIFT)#define HF2_IGNNE_MASK (1 << HF2_IGNNE_SHIFT)#define HF2_VGIF_MASK (1 << HF2_VGIF_SHIFT) +#define HF2_HYPERV_HLT_MASK (1 << HF2_HYPERV_HLT_SHIFT)#define CR0_PE_SHIFT 0#define CR0_MP_SHIFT 1
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v2 7/10 whpx: i386: add HV_X64_MSR_GUEST_IDLE when !kernel-irqchip
In reply to Mohamed MediouniAdd support for an oddball HV_X64_MSR_GUEST_IDLE not-quite-an-HLT that wakes the vCPU even if EFLAGS.IF is set.
--- target/i386/whpx/whpx-all.c | 46 ++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-)
target/i386/whpx/whpx-all.c +43 -3
@@ -52,6 +52,7 @@/* for kernel-irqchip=off */ #define HV_X64_MSR_APIC_FREQUENCY 0x40000023#define HV_X64_MSR_VP_ASSIST_PAGE 0x40000073 +#define HV_X64_MSR_GUEST_IDLE 0x400000f0static bool is_modern_os = true;
@@ -1543,13 +1544,16 @@ static vaddr whpx_vcpu_get_pc(CPUState *cpu, bool exit_context_valid) } } -static int whpx_handle_halt (CPUState *cpu) +static int whpx_handle_halt_generic (CPUState *cpu) { +X86CPU *x86_cpu =X86_CPU (cpu); +CPUX86State *env = &x86_cpu->env; +int ret =0 ;bql_lock ();if (!(cpu_test_interrupt (cpu,CPU_INTERRUPT_HARD ) && - (cpu_env (cpu)->eflags &IF_MASK )) && + ((cpu_env (cpu)->eflags &IF_MASK ) || env->hflags2 &HF2_HYPERV_HLT_MASK )) && !cpu_test_interrupt (cpu,CPU_INTERRUPT_NMI )) { cpu->exception_index =EXCP_HLT ; cpu->halted = true;
@@ -1560,6 +1564,27 @@ static int whpx_handle_halt(CPUState *cpu)return ret; } +static int whpx_handle_halt (CPUState *cpu) +{ +int ret =0 ; + + ret =whpx_handle_halt_generic (cpu); + +return ret; +} + +static int whpx_handle_hyperv_guestidle (CPUState *cpu) +{ +X86CPU *x86_cpu =X86_CPU (cpu); +CPUX86State *env = &x86_cpu->env; +int ret =0 ; + + env->hflags2 |=HF2_HYPERV_HLT_MASK ; + ret =whpx_handle_halt_generic (cpu); + +return ret; +} +static void whpx_vcpu_kick_out_of_hlt (CPUState *cpu) {WHV_REGISTER_VALUE reg;
@@ -1763,9 +1788,10 @@ static void whpx_vcpu_process_async_events(CPUState *cpu) }if ((cpu_test_interrupt (cpu,CPU_INTERRUPT_HARD ) && - (env->eflags &IF_MASK )) || + ((env->eflags &IF_MASK ) || env->hflags2 &HF2_HYPERV_HLT_MASK )) ||cpu_test_interrupt (cpu,CPU_INTERRUPT_NMI )) { cpu->halted = false; + env->hflags2 &= ~HF2_HYPERV_HLT_MASK ; }if (cpu_test_interrupt (cpu,CPU_INTERRUPT_SIPI )) {
@@ -2035,6 +2061,20 @@ int whpx_vcpu_run(CPUState *cpu) } } +/* +* Windows and Linux both use this MSR. +* Windows 11 25H2 uses it even when not advertised. +*/ +if (vcpu->exit_ctx.MsrAccess.MsrNumber ==HV_X64_MSR_GUEST_IDLE + && !vcpu->exit_ctx.MsrAccess.AccessInfo.IsWrite + && !whpx_irqchip_in_kernel () + && whpx->hyperv_enlightenments_enabled) { + is_known_msr =1 ; +whpx_bump_rip (cpu, &vcpu->exit_ctx); + ret =whpx_handle_hyperv_guestidle (cpu); +break ; + } +/* * Linux tries to use it anyway even when not exposed. * Ignore the write as the VP assist page is not used.
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v2 8/10 whpx: i386: one more CPUID
In reply to Mohamed MediouniTrap cpuid_0xb as it has x2APIC topology information.
--- target/i386/whpx/whpx-all.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/i386/whpx/whpx-all.c +1 -1
@@ -2527,7 +2527,7 @@ int whpx_accel_init(AccelState *as, MachineState *ms)WHV_PROCESSOR_FEATURES_BANKS processor_features;WHV_PROCESSOR_PERFMON_FEATURES perfmon_features; -UINT32 cpuidExitList[] = {0x0 ,0x1 ,0x6 ,0x7 ,0x14 ,0x24 ,0x29 ,0x1E , +UINT32 cpuidExitList[] = {0x0 ,0x1 ,0x6 ,0x7 ,0xb ,0x14 ,0x24 ,0x29 ,0x1E ,0x40000000 ,0x40000001 ,0x40000010 ,0x80000000 ,0x80000001 ,0x80000002 ,0x80000003 ,0x80000004 ,0x80000007 ,0x80000008 ,0x8000000A ,0x80000021 ,0x80000022 ,0xC0000000 ,0xC0000001 };
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v2 9/10 whpx: i386: some x2APIC awareness
In reply to Mohamed Mediouni--- target/i386/whpx/whpx-apic.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
target/i386/whpx/whpx-apic.c +10 -2
@@ -33,7 +33,11 @@ static void whpx_put_apic_state(APICCommonState *s,int i;memset (kapic,0 ,sizeof (*kapic)); - kapic->fields[0x2 ].data = s->id <<24 ; +if (s->apicbase &MSR_IA32_APICBASE_EXTD ) { + kapic->fields[0x2 ].data = s->initial_apic_id; + }else { + kapic->fields[0x2 ].data = s->id <<24 ; + } kapic->fields[0x3 ].data = s->version | ((APIC_LVT_NB -1 ) <<16 ); kapic->fields[0x8 ].data = s->tpr; kapic->fields[0xd ].data = s->log_dest <<24 ;
@@ -61,7 +65,11 @@ static void whpx_get_apic_state(APICCommonState *s, {int i, v; - s->id = kapic->fields[0x2 ].data >>24 ; +if (s->apicbase &MSR_IA32_APICBASE_EXTD ) { +assert (kapic->fields[0x2 ].data == s->initial_apic_id); + }else { + s->id = kapic->fields[0x2 ].data >>24 ; + } s->tpr = kapic->fields[0x8 ].data; s->arb_id = kapic->fields[0x9 ].data; s->log_dest = kapic->fields[0xd ].data >>24 ;
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v2 10/10 whpx: i386: set WHvX64RegisterInitialApicId
In reply to Mohamed MediouniKeep Hyper-V aware of the initial APIC ID chosen.
--- target/i386/whpx/whpx-all.c | 5 +++++ 1 file changed, 5 insertions(+)
target/i386/whpx/whpx-all.c +5 -0
@@ -2398,6 +2398,11 @@ int whpx_init_vcpu(CPUState *cpu) goto error; } +if (!whpx_irqchip_in_kernel ()) { +WHV_REGISTER_VALUE apic_id = {.Reg64 = x86_cpu->apic_state->initial_apic_id}; +whpx_set_reg (cpu, WHvX64RegisterInitialApicId, apic_id); + } +/* * vcpu's TSC frequency is either specified by user, or use the value * provided by Hyper-V if the former is not present. In the latter case, we
Signature · 1 line
2.50.1 (Apple Git-155)