Skip to content

Commit a5e0c25

Browse files
sean-jcbonzini
authored andcommitted
KVM: nVMX: Clean up x2APIC MSR handling for L2
Clean up the x2APIC MSR bitmap intereption code for L2, which is the last holdout of open coded bitmap manipulations. Freshen up the SDM/PRM comment, rename the function to make it abundantly clear the funky behavior is x2APIC specific, and explain _why_ vmcs01's bitmap is ignored (the previous comment was flat out wrong for x2APIC behavior). No functional change intended. Signed-off-by: Sean Christopherson <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 0cacb80 commit a5e0c25

File tree

1 file changed

+14
-39
lines changed

1 file changed

+14
-39
lines changed

arch/x86/kvm/vmx/nested.c

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -525,44 +525,19 @@ static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
525525
}
526526

527527
/*
528-
* If a msr is allowed by L0, we should check whether it is allowed by L1.
529-
* The corresponding bit will be cleared unless both of L0 and L1 allow it.
528+
* For x2APIC MSRs, ignore the vmcs01 bitmap. L1 can enable x2APIC without L1
529+
* itself utilizing x2APIC. All MSRs were previously set to be intercepted,
530+
* only the "disable intercept" case needs to be handled.
530531
*/
531-
static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
532-
unsigned long *msr_bitmap_nested,
533-
u32 msr, int type)
532+
static void nested_vmx_disable_intercept_for_x2apic_msr(unsigned long *msr_bitmap_l1,
533+
unsigned long *msr_bitmap_l0,
534+
u32 msr, int type)
534535
{
535-
int f = sizeof(unsigned long);
536+
if (type & MSR_TYPE_R && !vmx_test_msr_bitmap_read(msr_bitmap_l1, msr))
537+
vmx_clear_msr_bitmap_read(msr_bitmap_l0, msr);
536538

537-
/*
538-
* See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
539-
* have the write-low and read-high bitmap offsets the wrong way round.
540-
* We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
541-
*/
542-
if (msr <= 0x1fff) {
543-
if (type & MSR_TYPE_R &&
544-
!test_bit(msr, msr_bitmap_l1 + 0x000 / f))
545-
/* read-low */
546-
__clear_bit(msr, msr_bitmap_nested + 0x000 / f);
547-
548-
if (type & MSR_TYPE_W &&
549-
!test_bit(msr, msr_bitmap_l1 + 0x800 / f))
550-
/* write-low */
551-
__clear_bit(msr, msr_bitmap_nested + 0x800 / f);
552-
553-
} else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
554-
msr &= 0x1fff;
555-
if (type & MSR_TYPE_R &&
556-
!test_bit(msr, msr_bitmap_l1 + 0x400 / f))
557-
/* read-high */
558-
__clear_bit(msr, msr_bitmap_nested + 0x400 / f);
559-
560-
if (type & MSR_TYPE_W &&
561-
!test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
562-
/* write-high */
563-
__clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
564-
565-
}
539+
if (type & MSR_TYPE_W && !vmx_test_msr_bitmap_write(msr_bitmap_l1, msr))
540+
vmx_clear_msr_bitmap_write(msr_bitmap_l0, msr);
566541
}
567542

568543
static inline void enable_x2apic_msr_intercepts(unsigned long *msr_bitmap)
@@ -631,7 +606,7 @@ static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
631606
/*
632607
* To keep the control flow simple, pay eight 8-byte writes (sixteen
633608
* 4-byte writes on 32-bit systems) up front to enable intercepts for
634-
* the x2APIC MSR range and selectively disable them below.
609+
* the x2APIC MSR range and selectively toggle those relevant to L2.
635610
*/
636611
enable_x2apic_msr_intercepts(msr_bitmap_l0);
637612

@@ -650,17 +625,17 @@ static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
650625
}
651626
}
652627

653-
nested_vmx_disable_intercept_for_msr(
628+
nested_vmx_disable_intercept_for_x2apic_msr(
654629
msr_bitmap_l1, msr_bitmap_l0,
655630
X2APIC_MSR(APIC_TASKPRI),
656631
MSR_TYPE_R | MSR_TYPE_W);
657632

658633
if (nested_cpu_has_vid(vmcs12)) {
659-
nested_vmx_disable_intercept_for_msr(
634+
nested_vmx_disable_intercept_for_x2apic_msr(
660635
msr_bitmap_l1, msr_bitmap_l0,
661636
X2APIC_MSR(APIC_EOI),
662637
MSR_TYPE_W);
663-
nested_vmx_disable_intercept_for_msr(
638+
nested_vmx_disable_intercept_for_x2apic_msr(
664639
msr_bitmap_l1, msr_bitmap_l0,
665640
X2APIC_MSR(APIC_SELF_IPI),
666641
MSR_TYPE_W);

0 commit comments

Comments
 (0)