Skip to content

Commit 74cdc83

Browse files
minipli-osssean-jc
authored andcommitted
KVM: x86: Make use of kvm_read_cr*_bits() when testing bits
Make use of the kvm_read_cr{0,4}_bits() helper functions when we only want to know the state of certain bits instead of the whole register. This not only makes the intent cleaner, it also avoids a potential VMREAD in case the tested bits aren't guest owned. Signed-off-by: Mathias Krause <minipli@grsecurity.net> Link: https://lore.kernel.org/r/20230322013731.102955-5-minipli@grsecurity.net Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent e40bcf9 commit 74cdc83

2 files changed

Lines changed: 4 additions & 4 deletions

File tree

arch/x86/kvm/pmu.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,9 +540,9 @@ int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)
540540
if (!pmc)
541541
return 1;
542542

543-
if (!(kvm_read_cr4(vcpu) & X86_CR4_PCE) &&
543+
if (!(kvm_read_cr4_bits(vcpu, X86_CR4_PCE)) &&
544544
(static_call(kvm_x86_get_cpl)(vcpu) != 0) &&
545-
(kvm_read_cr0(vcpu) & X86_CR0_PE))
545+
(kvm_read_cr0_bits(vcpu, X86_CR0_PE)))
546546
return 1;
547547

548548
*data = pmc_read_counter(pmc) & mask;

arch/x86/kvm/vmx/vmx.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5517,7 +5517,7 @@ static int handle_cr(struct kvm_vcpu *vcpu)
55175517
break;
55185518
case 3: /* lmsw */
55195519
val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
5520-
trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
5520+
trace_kvm_cr_write(0, (kvm_read_cr0_bits(vcpu, ~0xful) | val));
55215521
kvm_lmsw(vcpu, val);
55225522

55235523
return kvm_skip_emulated_instruction(vcpu);
@@ -7575,7 +7575,7 @@ static u8 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
75757575
if (!kvm_arch_has_noncoherent_dma(vcpu->kvm))
75767576
return (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT) | VMX_EPT_IPAT_BIT;
75777577

7578-
if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
7578+
if (kvm_read_cr0_bits(vcpu, X86_CR0_CD)) {
75797579
if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
75807580
cache = MTRR_TYPE_WRBACK;
75817581
else

0 commit comments

Comments
 (0)