whpx: i386: Some more changes
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..
Based-on: <[email protected]>
Mohamed Mediouni (7):
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
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 | 70 +++++++++++++++++++++++++++++++--- 5 files changed, 112 insertions(+), 9 deletions(-)
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH 1/7 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 2/7 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 3/7 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 4/7 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 5/7 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 6/7 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 7/7 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, 42 insertions(+), 4 deletions(-)
target/i386/whpx/whpx-all.c +42 -4
@@ -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;
@@ -1631,7 +1656,8 @@ static void whpx_vcpu_pre_run(CPUState *cpu)/* Get pending hard interruption or replay one that was overwritten */ if (!whpx_irqchip_in_kernel ()) {if (!vcpu->interruption_pending && - vcpu->interruptable && (env->eflags &IF_MASK ) + vcpu->interruptable && + ((env->eflags &IF_MASK ) || env->hflags2 &HF2_HYPERV_HLT_MASK ) && (vcpu->tpr < irr || irr ==0 )) {assert (!new_int.InterruptionPending);if (cpu_test_interrupt (cpu,CPU_INTERRUPT_HARD )) {
@@ -1763,9 +1789,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 +2062,17 @@ int whpx_vcpu_run(CPUState *cpu) } } +/* Windows and Linux both use this MSR. */ +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)