kvm: x86: Suppress CR3_PCID_INVD bit only when PCIDs are enabled

This change “kvm: x86: Suppress CR3_PCID_INVD bit only when PCIDs are enabled” in Linux kernel is authored by Junaid Shahid <junaids [at] google.com> on Fri May 4 11:37:13 2018 -0700.

kvm: x86: Suppress CR3_PCID_INVD bit only when PCIDs are enabled

If the PCIDE bit is not set in CR4, then the MSb of CR3 is a reserved
bit. If the guest tries to set it, that should cause a #GP fault. So
mask out the bit only when the PCIDE bit is set.

Signed-off-by: Junaid Shahid <junaids@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

This Linux change may have been applied to various maintained Linux releases and you can find Linux releases including commit c19986f.

There are 5 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 arch/x86/kvm/x86.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 44bd4a2..37dd9a9 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -843,7 +843,10 @@ int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
 int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
 {
 #ifdef CONFIG_X86_64
-	cr3 &= ~CR3_PCID_INVD;
+	bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
+
+	if (pcid_enabled)
+		cr3 &= ~CR3_PCID_INVD;
 #endif
 
 	if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {

The commit for this change in Linux stable tree is c19986f (patch).

Leave a Reply

Your email address will not be published. Required fields are marked *