Skip to content

Commit fa71e95

Browse files
Sean Christophersonbonzini
authored andcommitted
KVM: VMX: Use KVM_POSSIBLE_CR*_GUEST_BITS to initialize guest/host masks
Use the "common" KVM_POSSIBLE_CR*_GUEST_BITS defines to initialize the CR0/CR4 guest host masks instead of duplicating most of the CR4 mask and open coding the CR0 mask. SVM doesn't utilize the masks, i.e. the masks are effectively VMX specific even if they're not named as such. This avoids duplicate code, better documents the guest owned CR0 bit, and eliminates the need for a build-time assertion to keep VMX and x86 synchronized. Signed-off-by: Sean Christopherson <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 7c83d09 commit fa71e95

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

arch/x86/kvm/vmx/nested.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4109,7 +4109,7 @@ static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
41094109
* CR0_GUEST_HOST_MASK is already set in the original vmcs01
41104110
* (KVM doesn't change it);
41114111
*/
4112-
vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
4112+
vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
41134113
vmx_set_cr0(vcpu, vmcs12->host_cr0);
41144114

41154115
/* Same as above - no reason to call set_cr4_guest_host_mask(). */
@@ -4259,7 +4259,7 @@ static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
42594259
*/
42604260
vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
42614261

4262-
vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
4262+
vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
42634263
vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
42644264

42654265
vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);

arch/x86/kvm/vmx/vmx.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,6 @@ module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
133133
#define KVM_VM_CR0_ALWAYS_ON \
134134
(KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
135135
X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
136-
#define KVM_CR4_GUEST_OWNED_BITS \
137-
(X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
138-
| X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
139136

140137
#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
141138
#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
@@ -4034,11 +4031,9 @@ void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
40344031

40354032
void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
40364033
{
4037-
BUILD_BUG_ON(KVM_CR4_GUEST_OWNED_BITS & ~KVM_POSSIBLE_CR4_GUEST_BITS);
4038-
4039-
vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
4040-
if (enable_ept)
4041-
vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
4034+
vmx->vcpu.arch.cr4_guest_owned_bits = KVM_POSSIBLE_CR4_GUEST_BITS;
4035+
if (!enable_ept)
4036+
vmx->vcpu.arch.cr4_guest_owned_bits &= ~X86_CR4_PGE;
40424037
if (is_guest_mode(&vmx->vcpu))
40434038
vmx->vcpu.arch.cr4_guest_owned_bits &=
40444039
~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
@@ -4335,8 +4330,8 @@ static void init_vmcs(struct vcpu_vmx *vmx)
43354330
/* 22.2.1, 20.8.1 */
43364331
vm_entry_controls_set(vmx, vmx_vmentry_ctrl());
43374332

4338-
vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
4339-
vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
4333+
vmx->vcpu.arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
4334+
vmcs_writel(CR0_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr0_guest_owned_bits);
43404335

43414336
set_cr4_guest_host_mask(vmx);
43424337

0 commit comments

Comments
 (0)