Skip to content

Commit

Permalink
KVM: x86: Ignore CR0.WP toggles in non-paging mode
Browse files Browse the repository at this point in the history
If paging is disabled, there are no permission bits to emulate.
Micro-optimize this case to avoid unnecessary work.

Suggested-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Mathias Krause <minipli@grsecurity.net>
Link: https://lore.kernel.org/r/20230322013731.102955-4-minipli@grsecurity.net
Co-developed-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
  • Loading branch information
minipli-oss authored and sean-jc committed Mar 22, 2023
1 parent 01b3171 commit e40bcf9
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions arch/x86/kvm/x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -908,14 +908,20 @@ void kvm_post_set_cr0(struct kvm_vcpu *vcpu, unsigned long old_cr0, unsigned lon
{
/*
* CR0.WP is incorporated into the MMU role, but only for non-nested,
* indirect shadow MMUs. If TDP is enabled, the MMU's metadata needs
* to be updated, e.g. so that emulating guest translations does the
* right thing, but there's no need to unload the root as CR0.WP
* doesn't affect SPTEs.
* indirect shadow MMUs. If paging is disabled, no updates are needed
* as there are no permission bits to emulate. If TDP is enabled, the
* MMU's metadata needs to be updated, e.g. so that emulating guest
* translations does the right thing, but there's no need to unload the
* root as CR0.WP doesn't affect SPTEs.
*/
if (tdp_enabled && (cr0 ^ old_cr0) == X86_CR0_WP) {
kvm_init_mmu(vcpu);
return;
if ((cr0 ^ old_cr0) == X86_CR0_WP) {
if (!(cr0 & X86_CR0_PG))
return;

if (tdp_enabled) {
kvm_init_mmu(vcpu);
return;
}
}

if ((cr0 ^ old_cr0) & X86_CR0_PG) {
Expand Down

0 comments on commit e40bcf9

Please sign in to comment.