@@ -981,7 +981,7 @@ static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
981981 __func__ , i , e .index , e .reserved );
982982 goto fail ;
983983 }
984- if (kvm_set_msr (vcpu , e .index , e .value )) {
984+ if (kvm_set_msr_with_filter (vcpu , e .index , e .value )) {
985985 pr_debug_ratelimited (
986986 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n" ,
987987 __func__ , i , e .index , e .value );
@@ -1017,7 +1017,7 @@ static bool nested_vmx_get_vmexit_msr_value(struct kvm_vcpu *vcpu,
10171017 }
10181018 }
10191019
1020- if (kvm_get_msr (vcpu , msr_index , data )) {
1020+ if (kvm_get_msr_with_filter (vcpu , msr_index , data )) {
10211021 pr_debug_ratelimited ("%s cannot read MSR (0x%x)\n" , __func__ ,
10221022 msr_index );
10231023 return false;
@@ -1112,9 +1112,9 @@ static void prepare_vmx_msr_autostore_list(struct kvm_vcpu *vcpu,
11121112 /*
11131113 * Emulated VMEntry does not fail here. Instead a less
11141114 * accurate value will be returned by
1115- * nested_vmx_get_vmexit_msr_value() using kvm_get_msr()
1116- * instead of reading the value from the vmcs02 VMExit
1117- * MSR-store area.
1115+ * nested_vmx_get_vmexit_msr_value() by reading KVM's
1116+ * internal MSR state instead of reading the value from
1117+ * the vmcs02 VMExit MSR-store area.
11181118 */
11191119 pr_warn_ratelimited (
11201120 "Not enough msr entries in msr_autostore. Can't add msr %x\n" ,
@@ -2341,10 +2341,12 @@ static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs0
23412341
23422342 /* Posted interrupts setting is only taken from vmcs12. */
23432343 vmx -> nested .pi_pending = false;
2344- if (nested_cpu_has_posted_intr (vmcs12 ))
2344+ if (nested_cpu_has_posted_intr (vmcs12 )) {
23452345 vmx -> nested .posted_intr_nv = vmcs12 -> posted_intr_nv ;
2346- else
2346+ } else {
2347+ vmx -> nested .posted_intr_nv = -1 ;
23472348 exec_control &= ~PIN_BASED_POSTED_INTR ;
2349+ }
23482350 pin_controls_set (vmx , exec_control );
23492351
23502352 /*
@@ -2494,6 +2496,7 @@ static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
24942496
24952497 if (!hv_evmcs || !(hv_evmcs -> hv_clean_fields &
24962498 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2 )) {
2499+
24972500 vmcs_write16 (GUEST_ES_SELECTOR , vmcs12 -> guest_es_selector );
24982501 vmcs_write16 (GUEST_CS_SELECTOR , vmcs12 -> guest_cs_selector );
24992502 vmcs_write16 (GUEST_SS_SELECTOR , vmcs12 -> guest_ss_selector );
@@ -2531,7 +2534,7 @@ static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
25312534 vmcs_writel (GUEST_GDTR_BASE , vmcs12 -> guest_gdtr_base );
25322535 vmcs_writel (GUEST_IDTR_BASE , vmcs12 -> guest_idtr_base );
25332536
2534- vmx -> segment_cache . bitmask = 0 ;
2537+ vmx_segment_cache_clear ( vmx ) ;
25352538 }
25362539
25372540 if (!hv_evmcs || !(hv_evmcs -> hv_clean_fields &
@@ -4308,11 +4311,52 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
43084311 }
43094312
43104313 if (kvm_cpu_has_interrupt (vcpu ) && !vmx_interrupt_blocked (vcpu )) {
4314+ int irq ;
4315+
43114316 if (block_nested_events )
43124317 return - EBUSY ;
43134318 if (!nested_exit_on_intr (vcpu ))
43144319 goto no_vmexit ;
4315- nested_vmx_vmexit (vcpu , EXIT_REASON_EXTERNAL_INTERRUPT , 0 , 0 );
4320+
4321+ if (!nested_exit_intr_ack_set (vcpu )) {
4322+ nested_vmx_vmexit (vcpu , EXIT_REASON_EXTERNAL_INTERRUPT , 0 , 0 );
4323+ return 0 ;
4324+ }
4325+
4326+ irq = kvm_cpu_get_extint (vcpu );
4327+ if (irq != -1 ) {
4328+ nested_vmx_vmexit (vcpu , EXIT_REASON_EXTERNAL_INTERRUPT ,
4329+ INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR | irq , 0 );
4330+ return 0 ;
4331+ }
4332+
4333+ irq = kvm_apic_has_interrupt (vcpu );
4334+ if (WARN_ON_ONCE (irq < 0 ))
4335+ goto no_vmexit ;
4336+
4337+ /*
4338+ * If the IRQ is L2's PI notification vector, process posted
4339+ * interrupts for L2 instead of injecting VM-Exit, as the
4340+ * detection/morphing architecturally occurs when the IRQ is
4341+ * delivered to the CPU. Note, only interrupts that are routed
4342+ * through the local APIC trigger posted interrupt processing,
4343+ * and enabling posted interrupts requires ACK-on-exit.
4344+ */
4345+ if (irq == vmx -> nested .posted_intr_nv ) {
4346+ vmx -> nested .pi_pending = true;
4347+ kvm_apic_clear_irr (vcpu , irq );
4348+ goto no_vmexit ;
4349+ }
4350+
4351+ nested_vmx_vmexit (vcpu , EXIT_REASON_EXTERNAL_INTERRUPT ,
4352+ INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR | irq , 0 );
4353+
4354+ /*
4355+ * ACK the interrupt _after_ emulating VM-Exit, as the IRQ must
4356+ * be marked as in-service in vmcs01.GUEST_INTERRUPT_STATUS.SVI
4357+ * if APICv is active.
4358+ */
4359+ kvm_apic_ack_interrupt (vcpu , irq );
43164360 return 0 ;
43174361 }
43184362
@@ -4830,7 +4874,7 @@ static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
48304874 goto vmabort ;
48314875 }
48324876
4833- if (kvm_set_msr (vcpu , h .index , h .value )) {
4877+ if (kvm_set_msr_with_filter (vcpu , h .index , h .value )) {
48344878 pr_debug_ratelimited (
48354879 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n" ,
48364880 __func__ , j , h .index , h .value );
@@ -4993,14 +5037,6 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,
49935037 vcpu -> arch .mp_state = KVM_MP_STATE_RUNNABLE ;
49945038
49955039 if (likely (!vmx -> fail )) {
4996- if ((u16 )vm_exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
4997- nested_exit_intr_ack_set (vcpu )) {
4998- int irq = kvm_cpu_get_interrupt (vcpu );
4999- WARN_ON (irq < 0 );
5000- vmcs12 -> vm_exit_intr_info = irq |
5001- INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR ;
5002- }
5003-
50045040 if (vm_exit_reason != -1 )
50055041 trace_kvm_nested_vmexit_inject (vmcs12 -> vm_exit_reason ,
50065042 vmcs12 -> exit_qualification ,
0 commit comments