KVM: X86: Raise #GP when clearing CR0_PG in 64 bit mode
PATCH 1/1 KVM: X86: Raise #GP when clearing CR0_PG in 64 bit mode
From: Lai Jiangshan <[email protected]>
In the SDM:
If the logical processor is in 64-bit mode or if CR4.PCIDE = 1, an attempt to clear CR0.PG causes a general-protection exception (#GP). Software should transition to compatibility mode and clear CR4.PCIDE before attempting to disable paging.
--- arch/x86/kvm/x86.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/x86.c +2 -1
@@ -906,7 +906,8 @@ int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) !load_pdptrs(vcpu, kvm_read_cr3(vcpu)))return 1 ; -if (!(cr0 &X86_CR0_PG ) &&kvm_read_cr4_bits (vcpu,X86_CR4_PCIDE )) +if (!(cr0 &X86_CR0_PG ) && + (is_64_bit_mode (vcpu) ||kvm_read_cr4_bits (vcpu,X86_CR4_PCIDE )))return 1 ;static_call (kvm_x86_set_cr0)(vcpu, cr0);
Signature · 1 line
2.19.1.6.gb485710b
Quoted 19 lines · Lai Jiangshan
From: Lai Jiangshan <[email protected]>
In the SDM:
If the logical processor is in 64-bit mode or if CR4.PCIDE = 1, an attempt to clear CR0.PG causes a general-protection exception (#GP). Software should transition to compatibility mode and clear CR4.PCIDE before attempting to disable paging.Signed-off-by Lai Jiangshan <[email protected]>--- arch/x86/kvm/x86.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) · line 913
Quoted 9 lines
arch/x86/kvm/x86.c +2
-1@@ -906,7 +906,8 @@ int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) !load_pdptrs(vcpu, kvm_read_cr3(vcpu)))return 1 ; -if (!(cr0 &X86_CR0_PG ) &&kvm_read_cr4_bits (vcpu,X86_CR4_PCIDE )) +if (!(cr0 &X86_CR0_PG ) && + (is_64_bit_mode (vcpu) ||kvm_read_cr4_bits (vcpu,X86_CR4_PCIDE )))return 1 ;static_call (kvm_x86_set_cr0)(vcpu, cr0);
Queued, thanks.
Paolo
H. Peter Anvin
reviewerQuoted 23 lines · Paolo Bonzini
Quoted 22 lines · Lai Jiangshan
From: Lai Jiangshan <[email protected]>
In the SDM:
If the logical processor is in 64-bit mode or if CR4.PCIDE = 1, an attempt to clear CR0.PG causes a general-protection exception (#GP). Software should transition to compatibility mode and clear CR4.PCIDE before attempting to disable paging.Signed-off-by Lai Jiangshan <[email protected]>---
arch/x86/kvm/x86.c| 3 ++-1 file changed, 2 insertions(+), 1 deletion(-)arch/x86/kvm/x86.c +0
-1@@ -906,7 +906,8 @@ int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) !load_pdptrs(vcpu, kvm_read_cr3(vcpu))) return 1; - if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
Quoted 9 lines
arch/x86/kvm/x86.c int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) · line 913
Quoted 6 lines
arch/x86/kvm/x86.c +2
-0+ if (!(cr0 & X86_CR0_PG) && + (is_64_bit_mode(vcpu) || kvm_read_cr4_bits(vcpu,X86_CR4_PCIDE ))) return 1; static_call(kvm_x86_set_cr0)(vcpu, cr0);Queued, thanks.
Have you actually checked to see what real CPUs do in this case?
-hpa
Sean Christopherson
maintainerQuoted 34 lines · H. Peter Anvin
Quoted 23 lines · Paolo Bonzini
Quoted 22 lines · Lai Jiangshan
From: Lai Jiangshan <[email protected]>
In the SDM:
If the logical processor is in 64-bit mode or if CR4.PCIDE = 1, an attempt to clear CR0.PG causes a general-protection exception (#GP). Software should transition to compatibility mode and clear CR4.PCIDE before attempting to disable paging.Signed-off-by Lai Jiangshan <[email protected]>---
arch/x86/kvm/x86.c| 3 ++-1 file changed, 2 insertions(+), 1 deletion(-)arch/x86/kvm/x86.c +0
-1@@ -906,7 +906,8 @@ int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) !load_pdptrs(vcpu, kvm_read_cr3(vcpu))) return 1; - if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))Quoted 9 lines
arch/x86/kvm/x86.c int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned · line 913
Quoted 6 lines
arch/x86/kvm/x86.c +2
-0+ if (!(cr0 & X86_CR0_PG) && + (is_64_bit_mode(vcpu) || kvm_read_cr4_bits(vcpu,X86_CR4_PCIDE ))) return 1; static_call(kvm_x86_set_cr0)(vcpu, cr0);Queued, thanks.
Quoted 1 line
Have you actually checked to see what real CPUs do in this case?
I have now, and EMR at least behaves as the SDM describes. Why do you ask?
kvm_intel: Clearing CR0.PG faulted (vector = 13)
arch/x86/kvm/vmx/vmx.c +19 -0
@@ -8637,6 +8637,23 @@ void vmx_exit(void)kvm_x86_vendor_exit (); } +static noinline void vmx_disable_paging(void) +{ +unsigned long cr0 =native_read_cr0 (); +long vector =-1 ; + + asmvolatile ("1: mov %1, %%cr0\n\t" +" mov %2, %%cr0\n\t" +"2:" + _ASM_EXTABLE_FAULT(1b, 2b) + : "+a" (vector) + : "r" (cr0 & ~X86_CR0_PG), "r" (cr0) + : "cc", "memory" ); + + pr_warn("Clearing CR0.PG %s (vector = %ld)\n" , + vector <0 ?"succeeded" :"faulted" , vector); +} +int __init vmx_init (void ) {int r, cpu;
@@ -8644,6 +8661,8 @@ int __init vmx_init(void)if (!kvm_is_vmx_supported ())return -EOPNOTSUPP ; +vmx_disable_paging (); +/* * Note, hv_init_evmcs() touches only VMX knobs, i.e. there's nothing * to unwind if a later step fails.
Quoted 17 lines · Sean Christopherson
Quoted 15 lines · H. Peter Anvin
Quoted 14 lines · Paolo Bonzini
Quoted 13 lines · Lai Jiangshan
From: Lai Jiangshan <[email protected]>
In the SDM:
If the logical processor is in 64-bit mode or if CR4.PCIDE = 1, an attempt to clear CR0.PG causes a general-protection exception (#GP). Software should transition to compatibility mode and clear CR4.PCIDE before attempting to disable paging.Signed-off-by Lai Jiangshan <[email protected]>--- arch/x86/kvm/x86.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/x86.c +0
-0@@ -906,7 +906,8 @@ int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) !load_pdptrs(vcpu, kvm_read_cr3(vcpu)))Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/x86.c int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned · line 913
Quoted 8 lines
arch/x86/kvm/x86.c +2
-1return 1 ; -if (!(cr0 &X86_CR0_PG ) &&kvm_read_cr4_bits (vcpu,X86_CR4_PCIDE )) +if (!(cr0 &X86_CR0_PG ) && + (is_64_bit_mode (vcpu) ||kvm_read_cr4_bits (vcpu,X86_CR4_PCIDE )))return 1 ;static_call (kvm_x86_set_cr0)(vcpu, cr0);
Isn't this redundant with the "if (cs_l)" check above?
Quoted 49 lines
Quoted 4 lines
Quoted 2 lines
Queued, thanks.
Have you actually checked to see what real CPUs do in this case?
I have now, and EMR at least behaves as the SDM describes. Why do you ask?
kvm_intel: Clearing CR0.PG faulted (vector = 13)arch/x86/kvm/vmx/vmx.c +19
-0@@ -8637,6 +8637,23 @@ void vmx_exit(void)kvm_x86_vendor_exit (); } +static noinline void vmx_disable_paging(void) +{ +unsigned long cr0 =native_read_cr0 (); +long vector =-1 ; + + asmvolatile ("1: mov %1, %%cr0\n\t" +" mov %2, %%cr0\n\t" +"2:" + _ASM_EXTABLE_FAULT(1b, 2b) + : "+a" (vector) + : "r" (cr0 & ~X86_CR0_PG), "r" (cr0) + : "cc", "memory" ); + + pr_warn("Clearing CR0.PG %s (vector = %ld)\n" , + vector <0 ?"succeeded" :"faulted" , vector); +} +int __init vmx_init (void ) {int r, cpu;@@ -8644,6 +8661,8 @@ int __init vmx_init(void)if (!kvm_is_vmx_supported ())return -EOPNOTSUPP ; +vmx_disable_paging (); +/* * Note, hv_init_evmcs() touches only VMX knobs, i.e. there's nothing * to unwind if a later step fails.
Quoted 28 lines · Jim Mattson
Quoted 26 lines · Sean Christopherson
Quoted 24 lines · H. Peter Anvin
Quoted 23 lines · Paolo Bonzini
Quoted 22 lines · Lai Jiangshan
From: Lai Jiangshan <[email protected]>
In the SDM:
If the logical processor is in 64-bit mode or if CR4.PCIDE = 1, an attempt to clear CR0.PG causes a general-protection exception (#GP). Software should transition to compatibility mode and clear CR4.PCIDE before attempting to disable paging.Signed-off-by Lai Jiangshan <[email protected]>--- arch/x86/kvm/x86.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)arch/x86/kvm/x86.c +0
-1@@ -906,7 +906,8 @@ int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) !load_pdptrs(vcpu, kvm_read_cr3(vcpu)))return 1 ; -if (!(cr0 &X86_CR0_PG ) &&kvm_read_cr4_bits (vcpu,X86_CR4_PCIDE ))
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/x86.c int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned · line 913
Quoted 6 lines
arch/x86/kvm/x86.c +2
-0+if (!(cr0 &X86_CR0_PG ) && + (is_64_bit_mode (vcpu) ||kvm_read_cr4_bits (vcpu,X86_CR4_PCIDE )))return 1 ;static_call (kvm_x86_set_cr0)(vcpu, cr0);Isn't this redundant with the "if (
cs_l)" check above?
Never mind. That's an attempt to set CR0.PG, not to clear it.
Quoted 49 lines
Quoted 49 lines
Quoted 4 lines
Quoted 2 lines
Queued, thanks.
Have you actually checked to see what real CPUs do in this case?
I have now, and EMR at least behaves as the SDM describes. Why do you ask?
kvm_intel: Clearing CR0.PG faulted (vector = 13)arch/x86/kvm/vmx/vmx.c +19
-0@@ -8637,6 +8637,23 @@ void vmx_exit(void)kvm_x86_vendor_exit (); } +static noinline void vmx_disable_paging(void) +{ +unsigned long cr0 =native_read_cr0 (); +long vector =-1 ; + + asmvolatile ("1: mov %1, %%cr0\n\t" +" mov %2, %%cr0\n\t" +"2:" + _ASM_EXTABLE_FAULT(1b, 2b) + : "+a" (vector) + : "r" (cr0 & ~X86_CR0_PG), "r" (cr0) + : "cc", "memory" ); + + pr_warn("Clearing CR0.PG %s (vector = %ld)\n" , + vector <0 ?"succeeded" :"faulted" , vector); +} +int __init vmx_init (void ) {int r, cpu;@@ -8644,6 +8661,8 @@ int __init vmx_init(void)if (!kvm_is_vmx_supported ())return -EOPNOTSUPP ; +vmx_disable_paging (); +/* * Note, hv_init_evmcs() touches only VMX knobs, i.e. there's nothing * to unwind if a later step fails.