From: "Jan Beulich" Subject: [PATCH] x86/HVM: refine SMEP/SMAP tests in HVM_CR4_GUEST_RESERVED_BITS() Date: Thu, 05 Jun 2014 12:58:35 +0100 Message-ID: <5390778B02000078000182FA@mail.emea.novell.com> References: <538F3F2A.7050202@citrix.com> <538F5EA00200007800017E64@mail.emea.novell.com> <538F449B.10807@citrix.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part8BB9457B.2__=" Return-path: In-Reply-To: <538F449B.10807@citrix.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Andrew Cooper , Xen-devel List Cc: Keir Fraser , Feng Wu List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part8BB9457B.2__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Andrew validly points out that the use of the macro on the restore path can't rely on the CPUID bits for the guest already being in place (as their setting by the tool stack in turn requires the other restore operations already having taken place). And even worse, using hvm_cpuid() is invalid here because that function assumes to be used in the context of the vCPU in question. Reverting to the behavior prior to the change from checking cpu_has_sm?p to hvm_vcpu_has_sm?p() would break the other (non-restore) use of the macro. So let's revert to the prior behavior only for the restore path, by adding a respective second parameter to the macro. Obviously the two cpu_has_* uses in the macro should really also be converted to hvm_cpuid() based checks at least for the non-restore path. Reported-by: Andrew Cooper Signed-off-by: Jan Beulich --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -1754,7 +1754,7 @@ static int hvm_load_cpu_ctxt(struct doma return -EINVAL; } =20 - if ( ctxt.cr4 & HVM_CR4_GUEST_RESERVED_BITS(v) ) + if ( ctxt.cr4 & HVM_CR4_GUEST_RESERVED_BITS(v, 1) ) { printk(XENLOG_G_ERR "HVM%d restore: bad CR4 %#" PRIx64 "\n", d->domain_id, ctxt.cr4); @@ -3186,7 +3186,7 @@ int hvm_set_cr4(unsigned long value) struct vcpu *v =3D current; unsigned long old_cr; =20 - if ( value & HVM_CR4_GUEST_RESERVED_BITS(v) ) + if ( value & HVM_CR4_GUEST_RESERVED_BITS(v, 0) ) { HVM_DBG_LOG(DBG_LEVEL_1, "Guest attempts to set reserved bit in CR4: %lx", --- a/xen/include/asm-x86/hvm/hvm.h +++ b/xen/include/asm-x86/hvm/hvm.h @@ -406,19 +406,27 @@ static inline bool_t hvm_vcpu_has_smep(v (X86_CR4_VMXE | X86_CR4_PAE | X86_CR4_MCE)) =20 /* These bits in CR4 cannot be set by the guest. */ -#define HVM_CR4_GUEST_RESERVED_BITS(_v) \ +#define HVM_CR4_GUEST_RESERVED_BITS(v, restore) ({ \ + const struct vcpu *_v =3D (v); \ + bool_t _restore =3D !!(restore); \ + ASSERT((_restore) || _v =3D=3D current); \ (~((unsigned long) \ (X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | \ X86_CR4_DE | X86_CR4_PSE | X86_CR4_PAE | \ X86_CR4_MCE | X86_CR4_PGE | X86_CR4_PCE | \ X86_CR4_OSFXSR | X86_CR4_OSXMMEXCPT | \ - (hvm_vcpu_has_smep() ? X86_CR4_SMEP : 0) | \ - (hvm_vcpu_has_smap() ? X86_CR4_SMAP : 0) | \ + (((_restore) ? cpu_has_smep : \ + hvm_vcpu_has_smep()) ? \ + X86_CR4_SMEP : 0) | \ + (((_restore) ? cpu_has_smap : \ + hvm_vcpu_has_smap()) ? \ + X86_CR4_SMAP : 0) | \ (cpu_has_fsgsbase ? X86_CR4_FSGSBASE : 0) | \ - ((nestedhvm_enabled((_v)->domain) && cpu_has_vmx)\ - ? X86_CR4_VMXE : 0) | \ - (cpu_has_pcid ? X86_CR4_PCIDE : 0) | \ - (cpu_has_xsave ? X86_CR4_OSXSAVE : 0)))) + ((nestedhvm_enabled(_v->domain) && cpu_has_vmx) \ + ? X86_CR4_VMXE : 0) | \ + (cpu_has_pcid ? X86_CR4_PCIDE : 0) | \ + (cpu_has_xsave ? X86_CR4_OSXSAVE : 0)))); \ +}) =20 /* These exceptions must always be intercepted. */ #define HVM_TRAP_MASK ((1U << TRAP_machine_check) | (1U << TRAP_invalid_op= )) --=__Part8BB9457B.2__= Content-Type: text/plain; name="x86-HVM-refine-SMxP-tests.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-HVM-refine-SMxP-tests.patch" x86/HVM: refine SMEP/SMAP tests in HVM_CR4_GUEST_RESERVED_BITS()=0A=0AAndre= w validly points out that the use of the macro on the restore path=0Acan't = rely on the CPUID bits for the guest already being in place (as=0Atheir = setting by the tool stack in turn requires the other restore=0Aoperations = already having taken place). And even worse, using=0Ahvm_cpuid() is = invalid here because that function assumes to be used in=0Athe context of = the vCPU in question.=0A=0AReverting to the behavior prior to the change = from checking=0Acpu_has_sm?p to hvm_vcpu_has_sm?p() would break the other = (non-restore)=0Ause of the macro. So let's revert to the prior behavior = only for the=0Arestore path, by adding a respective second parameter to = the macro.=0A=0AObviously the two cpu_has_* uses in the macro should = really also be=0Aconverted to hvm_cpuid() based checks at least for the = non-restore=0Apath.=0A=0AReported-by: Andrew Cooper =0ASigned-off-by: Jan Beulich =0A=0A--- a/xen/arch/x= 86/hvm/hvm.c=0A+++ b/xen/arch/x86/hvm/hvm.c=0A@@ -1754,7 +1754,7 @@ static = int hvm_load_cpu_ctxt(struct doma=0A return -EINVAL;=0A }=0A = =0A- if ( ctxt.cr4 & HVM_CR4_GUEST_RESERVED_BITS(v) )=0A+ if ( = ctxt.cr4 & HVM_CR4_GUEST_RESERVED_BITS(v, 1) )=0A {=0A = printk(XENLOG_G_ERR "HVM%d restore: bad CR4 %#" PRIx64 "\n",=0A = d->domain_id, ctxt.cr4);=0A@@ -3186,7 +3186,7 @@ int hvm_set_cr4(unsign= ed long value)=0A struct vcpu *v =3D current;=0A unsigned long = old_cr;=0A =0A- if ( value & HVM_CR4_GUEST_RESERVED_BITS(v) )=0A+ if = ( value & HVM_CR4_GUEST_RESERVED_BITS(v, 0) )=0A {=0A = HVM_DBG_LOG(DBG_LEVEL_1,=0A "Guest attempts to set = reserved bit in CR4: %lx",=0A--- a/xen/include/asm-x86/hvm/hvm.h=0A+++ = b/xen/include/asm-x86/hvm/hvm.h=0A@@ -406,19 +406,27 @@ static inline = bool_t hvm_vcpu_has_smep(v=0A (X86_CR4_VMXE | X86_CR4_PAE | X86_CR4_MCE= ))=0A =0A /* These bits in CR4 cannot be set by the guest. */=0A-#define = HVM_CR4_GUEST_RESERVED_BITS(_v) \=0A+#define HVM_CR4_GUEST_= RESERVED_BITS(v, restore) ({ \=0A+ const struct vcpu *_v =3D (v); = \=0A+ bool_t _restore =3D !!(restore); = \=0A+ ASSERT((_restore) || _v =3D=3D current); = \=0A (~((unsigned long) \=0A = (X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | \=0A X86_CR4_DE = | X86_CR4_PSE | X86_CR4_PAE | \=0A X86_CR4_MCE | X86_CR4_PGE = | X86_CR4_PCE | \=0A X86_CR4_OSFXSR | X86_CR4_OSXMMEXCPT | = \=0A- (hvm_vcpu_has_smep() ? X86_CR4_SMEP : 0) | \=0A- = (hvm_vcpu_has_smap() ? X86_CR4_SMAP : 0) | \=0A+ = (((_restore) ? cpu_has_smep : \=0A+ = hvm_vcpu_has_smep()) ? \=0A+ X86_CR4_SMEP : 0) | = \=0A+ (((_restore) ? cpu_has_smap : = \=0A+ hvm_vcpu_has_smap()) ? \=0A+ = X86_CR4_SMAP : 0) | \=0A = (cpu_has_fsgsbase ? X86_CR4_FSGSBASE : 0) | \=0A- ((nestedhvm_en= abled((_v)->domain) && cpu_has_vmx)\=0A- ? X86_CR4_VMX= E : 0) | \=0A- (cpu_has_pcid ? X86_CR4_PCIDE : 0) | = \=0A- (cpu_has_xsave ? X86_CR4_OSXSAVE : 0))))=0A+ = ((nestedhvm_enabled(_v->domain) && cpu_has_vmx) \=0A+ = ? X86_CR4_VMXE : 0) | \=0A+ (cpu_has_pcid ? X86_CR4_PCID= E : 0) | \=0A+ (cpu_has_xsave ? X86_CR4_OSXSAVE : 0)))); = \=0A+})=0A =0A /* These exceptions must always be intercepted. */=0A = #define HVM_TRAP_MASK ((1U << TRAP_machine_check) | (1U << TRAP_invalid_op)= )=0A --=__Part8BB9457B.2__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__Part8BB9457B.2__=--