Skip to content

Commit b899c13

Browse files
Krish Sadhukhanbonzini
authored andcommitted
KVM: x86: Create mask for guest CR4 reserved bits in kvm_update_cpuid()
Instead of creating the mask for guest CR4 reserved bits in kvm_valid_cr4(), do it in kvm_update_cpuid() so that it can be reused instead of creating it each time kvm_valid_cr4() is called. Suggested-by: Paolo Bonzini <[email protected]> Signed-off-by: Krish Sadhukhan <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent d42e3fa commit b899c13

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ struct kvm_vcpu_arch {
545545
unsigned long cr3;
546546
unsigned long cr4;
547547
unsigned long cr4_guest_owned_bits;
548+
unsigned long cr4_guest_rsvd_bits;
548549
unsigned long cr8;
549550
u32 host_pkru;
550551
u32 pkru;

arch/x86/kvm/cpuid.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ int kvm_update_cpuid(struct kvm_vcpu *vcpu)
128128
kvm_mmu_reset_context(vcpu);
129129

130130
kvm_pmu_refresh(vcpu);
131+
vcpu->arch.cr4_guest_rsvd_bits =
132+
__cr4_reserved_bits(guest_cpuid_has, vcpu);
131133
return 0;
132134
}
133135

arch/x86/kvm/x86.c

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -955,33 +955,12 @@ int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
955955
}
956956
EXPORT_SYMBOL_GPL(kvm_set_xcr);
957957

958-
#define __cr4_reserved_bits(__cpu_has, __c) \
959-
({ \
960-
u64 __reserved_bits = CR4_RESERVED_BITS; \
961-
\
962-
if (!__cpu_has(__c, X86_FEATURE_XSAVE)) \
963-
__reserved_bits |= X86_CR4_OSXSAVE; \
964-
if (!__cpu_has(__c, X86_FEATURE_SMEP)) \
965-
__reserved_bits |= X86_CR4_SMEP; \
966-
if (!__cpu_has(__c, X86_FEATURE_SMAP)) \
967-
__reserved_bits |= X86_CR4_SMAP; \
968-
if (!__cpu_has(__c, X86_FEATURE_FSGSBASE)) \
969-
__reserved_bits |= X86_CR4_FSGSBASE; \
970-
if (!__cpu_has(__c, X86_FEATURE_PKU)) \
971-
__reserved_bits |= X86_CR4_PKE; \
972-
if (!__cpu_has(__c, X86_FEATURE_LA57)) \
973-
__reserved_bits |= X86_CR4_LA57; \
974-
if (!__cpu_has(__c, X86_FEATURE_UMIP)) \
975-
__reserved_bits |= X86_CR4_UMIP; \
976-
__reserved_bits; \
977-
})
978-
979958
static int kvm_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
980959
{
981960
if (cr4 & cr4_reserved_bits)
982961
return -EINVAL;
983962

984-
if (cr4 & __cr4_reserved_bits(guest_cpuid_has, vcpu))
963+
if (cr4 & vcpu->arch.cr4_guest_rsvd_bits)
985964
return -EINVAL;
986965

987966
return 0;

arch/x86/kvm/x86.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,4 +373,25 @@ bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu);
373373

374374
#define KVM_MSR_RET_INVALID 2
375375

376+
#define __cr4_reserved_bits(__cpu_has, __c) \
377+
({ \
378+
u64 __reserved_bits = CR4_RESERVED_BITS; \
379+
\
380+
if (!__cpu_has(__c, X86_FEATURE_XSAVE)) \
381+
__reserved_bits |= X86_CR4_OSXSAVE; \
382+
if (!__cpu_has(__c, X86_FEATURE_SMEP)) \
383+
__reserved_bits |= X86_CR4_SMEP; \
384+
if (!__cpu_has(__c, X86_FEATURE_SMAP)) \
385+
__reserved_bits |= X86_CR4_SMAP; \
386+
if (!__cpu_has(__c, X86_FEATURE_FSGSBASE)) \
387+
__reserved_bits |= X86_CR4_FSGSBASE; \
388+
if (!__cpu_has(__c, X86_FEATURE_PKU)) \
389+
__reserved_bits |= X86_CR4_PKE; \
390+
if (!__cpu_has(__c, X86_FEATURE_LA57)) \
391+
__reserved_bits |= X86_CR4_LA57; \
392+
if (!__cpu_has(__c, X86_FEATURE_UMIP)) \
393+
__reserved_bits |= X86_CR4_UMIP; \
394+
__reserved_bits; \
395+
})
396+
376397
#endif

0 commit comments

Comments
 (0)