Skip to content

Commit 2c49db4

Browse files
Binbin Wusean-jc
authored andcommitted
KVM: x86: Add & use kvm_vcpu_is_legal_cr3() to check CR3's legality
Add and use kvm_vcpu_is_legal_cr3() to check CR3's legality to provide a clear distinction between CR3 and GPA checks. This will allow exempting bits from kvm_vcpu_is_legal_cr3() without affecting general GPA checks, e.g. for upcoming features that will use high bits in CR3 for feature enabling. No functional change intended. Signed-off-by: Binbin Wu <binbin.wu@linux.intel.com> Tested-by: Xuelian Guo <xuelian.guo@intel.com> Link: https://lore.kernel.org/r/20230913124227.12574-7-binbin.wu@linux.intel.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent a130066 commit 2c49db4

4 files changed

Lines changed: 11 additions & 6 deletions

File tree

arch/x86/kvm/cpuid.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,9 @@ static __always_inline bool guest_can_use(struct kvm_vcpu *vcpu,
279279
vcpu->arch.governed_features.enabled);
280280
}
281281

282+
static inline bool kvm_vcpu_is_legal_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
283+
{
284+
return kvm_vcpu_is_legal_gpa(vcpu, cr3);
285+
}
286+
282287
#endif

arch/x86/kvm/svm/nested.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ static bool __nested_vmcb_check_save(struct kvm_vcpu *vcpu,
311311
if ((save->efer & EFER_LME) && (save->cr0 & X86_CR0_PG)) {
312312
if (CC(!(save->cr4 & X86_CR4_PAE)) ||
313313
CC(!(save->cr0 & X86_CR0_PE)) ||
314-
CC(kvm_vcpu_is_illegal_gpa(vcpu, save->cr3)))
314+
CC(!kvm_vcpu_is_legal_cr3(vcpu, save->cr3)))
315315
return false;
316316
}
317317

@@ -520,7 +520,7 @@ static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu)
520520
static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
521521
bool nested_npt, bool reload_pdptrs)
522522
{
523-
if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3)))
523+
if (CC(!kvm_vcpu_is_legal_cr3(vcpu, cr3)))
524524
return -EINVAL;
525525

526526
if (reload_pdptrs && !nested_npt && is_pae_paging(vcpu) &&

arch/x86/kvm/vmx/nested.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
10851085
bool nested_ept, bool reload_pdptrs,
10861086
enum vm_entry_failure_code *entry_failure_code)
10871087
{
1088-
if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3))) {
1088+
if (CC(!kvm_vcpu_is_legal_cr3(vcpu, cr3))) {
10891089
*entry_failure_code = ENTRY_FAIL_DEFAULT;
10901090
return -EINVAL;
10911091
}
@@ -2912,7 +2912,7 @@ static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu,
29122912

29132913
if (CC(!nested_host_cr0_valid(vcpu, vmcs12->host_cr0)) ||
29142914
CC(!nested_host_cr4_valid(vcpu, vmcs12->host_cr4)) ||
2915-
CC(kvm_vcpu_is_illegal_gpa(vcpu, vmcs12->host_cr3)))
2915+
CC(!kvm_vcpu_is_legal_cr3(vcpu, vmcs12->host_cr3)))
29162916
return -EINVAL;
29172917

29182918
if (CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu)) ||

arch/x86/kvm/x86.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,7 @@ int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
12841284
* stuff CR3, e.g. for RSM emulation, and there is no guarantee that
12851285
* the current vCPU mode is accurate.
12861286
*/
1287-
if (kvm_vcpu_is_illegal_gpa(vcpu, cr3))
1287+
if (!kvm_vcpu_is_legal_cr3(vcpu, cr3))
12881288
return 1;
12891289

12901290
if (is_pae_paging(vcpu) && !load_pdptrs(vcpu, cr3))
@@ -11612,7 +11612,7 @@ static bool kvm_is_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1161211612
*/
1161311613
if (!(sregs->cr4 & X86_CR4_PAE) || !(sregs->efer & EFER_LMA))
1161411614
return false;
11615-
if (kvm_vcpu_is_illegal_gpa(vcpu, sregs->cr3))
11615+
if (!kvm_vcpu_is_legal_cr3(vcpu, sregs->cr3))
1161611616
return false;
1161711617
} else {
1161811618
/*

0 commit comments

Comments
 (0)