Skip to content

Commit 2ed41aa

Browse files
Sean Christophersonbonzini
authored andcommitted
KVM: VMX: Intercept guest reserved CR4 bits to inject #GP fault
Intercept CR4 bits that are guest reserved so that KVM correctly injects a #GP fault if the guest attempts to set a reserved bit. If a feature is supported by the CPU but is not exposed to the guest, and its associated CR4 bit is not intercepted by KVM by default, then KVM will fail to inject a #GP if the guest sets the CR4 bit without triggering an exit, e.g. by toggling only the bit in question. Note, KVM doesn't give the guest direct access to any CR4 bits that are also dependent on guest CPUID. Yet. Signed-off-by: Sean Christopherson <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent a6337a3 commit 2ed41aa

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

arch/x86/kvm/vmx/vmx.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4053,13 +4053,16 @@ void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
40534053

40544054
void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
40554055
{
4056-
vmx->vcpu.arch.cr4_guest_owned_bits = KVM_POSSIBLE_CR4_GUEST_BITS;
4056+
struct kvm_vcpu *vcpu = &vmx->vcpu;
4057+
4058+
vcpu->arch.cr4_guest_owned_bits = KVM_POSSIBLE_CR4_GUEST_BITS &
4059+
~vcpu->arch.cr4_guest_rsvd_bits;
40574060
if (!enable_ept)
4058-
vmx->vcpu.arch.cr4_guest_owned_bits &= ~X86_CR4_PGE;
4061+
vcpu->arch.cr4_guest_owned_bits &= ~X86_CR4_PGE;
40594062
if (is_guest_mode(&vmx->vcpu))
4060-
vmx->vcpu.arch.cr4_guest_owned_bits &=
4061-
~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
4062-
vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
4063+
vcpu->arch.cr4_guest_owned_bits &=
4064+
~get_vmcs12(vcpu)->cr4_guest_host_mask;
4065+
vmcs_writel(CR4_GUEST_HOST_MASK, ~vcpu->arch.cr4_guest_owned_bits);
40634066
}
40644067

40654068
u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
@@ -7249,6 +7252,8 @@ static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
72497252
}
72507253
}
72517254

7255+
set_cr4_guest_host_mask(vmx);
7256+
72527257
/* Refresh #PF interception to account for MAXPHYADDR changes. */
72537258
update_exception_bitmap(vcpu);
72547259
}

0 commit comments

Comments
 (0)