From: "Jan Beulich" Subject: [PATCH 4/4] x86/fxsave: bring in line with recent xsave adjustments Date: Tue, 04 Jun 2013 09:10:10 +0100 Message-ID: <51ADBD0202000078000DAF0F@nat28.tlf.novell.com> References: <51ADBA6102000078000DAEAA@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartC6F637F2.2__=" Return-path: In-Reply-To: <51ADBA6102000078000DAEAA@nat28.tlf.novell.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: xen-devel Cc: George Dunlap , Keir Fraser , suravee.suthikulpanit@amd.com 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. --=__PartC6F637F2.2__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Defer the FIP/FDP pointer reset needed on AMD CPUs to the restore path, and switch from using EMMS to FFREE here too (to be resistant against eventual future CPUs without MMX support). Also switch from using an almost typeless pointer in fpu_fxrstor() to a properly typed one, thus telling the compiler the truth about which memory gets accessed. Signed-off-by: Jan Beulich --- a/xen/arch/x86/i387.c +++ b/xen/arch/x86/i387.c @@ -52,14 +52,30 @@ static inline void fpu_xrstor(struct vcp /* Restor x87 FPU, MMX, SSE and SSE2 state */ static inline void fpu_fxrstor(struct vcpu *v) { - const char *fpu_ctxt =3D v->arch.fpu_ctxt; + const typeof(v->arch.xsave_area->fpu_sse) *fpu_ctxt =3D v->arch.fpu_ct= xt; + + /* + * AMD CPUs don't save/restore FDP/FIP/FOP unless an exception + * is pending. Clear the x87 state here by setting it to fixed + * values. The hypervisor data segment can be sometimes 0 and + * sometimes new user value. Both should be ok. Use the FPU saved + * data block as a safe address because it should be in L1. + */ + if ( !(fpu_ctxt->fsw & 0x0080) && + boot_cpu_data.x86_vendor =3D=3D X86_VENDOR_AMD ) + { + asm volatile ( "fnclex\n\t" + "ffree %%st(7)\n\t" /* clear stack tag */ + "fildl %0" /* load to clear state */ + : : "m" (*fpu_ctxt) ); + } =20 /* * FXRSTOR can fault if passed a corrupted data block. We handle this * possibility, which may occur if the block was passed to us by = control * tools or through VCPUOP_initialise, by silently clearing the = block. */ - switch ( __builtin_expect(fpu_ctxt[FPU_WORD_SIZE_OFFSET], 8) ) + switch ( __builtin_expect(fpu_ctxt->x[FPU_WORD_SIZE_OFFSET], 8) ) { default: asm volatile ( @@ -80,8 +96,7 @@ static inline void fpu_fxrstor(struct vc ".previous \n" _ASM_EXTABLE(1b, 2b) : - : "m" (*fpu_ctxt), - "i" (sizeof(v->arch.xsave_area->fpu_sse) / 4), + : "m" (*fpu_ctxt), "i" (sizeof(*fpu_ctxt) / 4), "cdaSDb" (fpu_ctxt) ); break; case 4: case 2: @@ -102,8 +117,7 @@ static inline void fpu_fxrstor(struct vc ".previous \n" _ASM_EXTABLE(1b, 2b) : - : "m" (*fpu_ctxt), - "i" (sizeof(v->arch.xsave_area->fpu_sse) / 4) ); + : "m" (*fpu_ctxt), "i" (sizeof(*fpu_ctxt) / 4) ); break; } } @@ -156,7 +170,7 @@ static inline void fpu_fxsave(struct vcp */ if ( !(fpu_ctxt->fsw & 0x0080) && boot_cpu_data.x86_vendor =3D=3D X86_VENDOR_AMD ) - word_size =3D -1; + return; =20 if ( word_size > 0 && !((fpu_ctxt->fip.addr | fpu_ctxt->fdp.addr) >> 32) ) @@ -177,25 +191,6 @@ static inline void fpu_fxsave(struct vcp =20 if ( word_size >=3D 0 ) fpu_ctxt->x[FPU_WORD_SIZE_OFFSET] =3D word_size; - =20 - /* Clear exception flags if FSW.ES is set. */ - if ( unlikely(fpu_ctxt->fsw & 0x0080) ) - asm volatile ("fnclex"); - =20 - /* - * AMD CPUs don't save/restore FDP/FIP/FOP unless an exception - * is pending. Clear the x87 state here by setting it to fixed - * values. The hypervisor data segment can be sometimes 0 and - * sometimes new user value. Both should be ok. Use the FPU saved - * data block as a safe address because it should be in L1. - */ - if ( boot_cpu_data.x86_vendor =3D=3D X86_VENDOR_AMD ) - { - asm volatile ( - "emms\n\t" /* clear stack tags */ - "fildl %0" /* load to clear state */ - : : "m" (*fpu_ctxt) ); - } } =20 /* Save x87 FPU state */ --=__PartC6F637F2.2__= Content-Type: text/plain; name="x86-fxrstor-reset-ptrs.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-fxrstor-reset-ptrs.patch" x86/fxsave: bring in line with recent xsave adjustments=0A=0ADefer the = FIP/FDP pointer reset needed on AMD CPUs to the restore path,=0Aand switch = from using EMMS to FFREE here too (to be resistant against=0Aeventual = future CPUs without MMX support). Also switch from using an=0Aalmost = typeless pointer in fpu_fxrstor() to a properly typed one, thus=0Atelling = the compiler the truth about which memory gets accessed.=0A=0ASigned-off-by= : Jan Beulich =0A=0A--- a/xen/arch/x86/i387.c=0A+++ = b/xen/arch/x86/i387.c=0A@@ -52,14 +52,30 @@ static inline void fpu_xrstor(s= truct vcp=0A /* Restor x87 FPU, MMX, SSE and SSE2 state */=0A static = inline void fpu_fxrstor(struct vcpu *v)=0A {=0A- const char *fpu_ctxt = =3D v->arch.fpu_ctxt;=0A+ const typeof(v->arch.xsave_area->fpu_sse) = *fpu_ctxt =3D v->arch.fpu_ctxt;=0A+=0A+ /*=0A+ * AMD CPUs don't = save/restore FDP/FIP/FOP unless an exception=0A+ * is pending. Clear = the x87 state here by setting it to fixed=0A+ * values. The hypervisor = data segment can be sometimes 0 and=0A+ * sometimes new user value. = Both should be ok. Use the FPU saved=0A+ * data block as a safe = address because it should be in L1.=0A+ */=0A+ if ( !(fpu_ctxt->fsw = & 0x0080) &&=0A+ boot_cpu_data.x86_vendor =3D=3D X86_VENDOR_AMD = )=0A+ {=0A+ asm volatile ( "fnclex\n\t"=0A+ = "ffree %%st(7)\n\t" /* clear stack tag */=0A+ = "fildl %0" /* load to clear state */=0A+ : = : "m" (*fpu_ctxt) );=0A+ }=0A =0A /*=0A * FXRSTOR can fault if = passed a corrupted data block. We handle this=0A * possibility, which = may occur if the block was passed to us by control=0A * tools or = through VCPUOP_initialise, by silently clearing the block.=0A */=0A- = switch ( __builtin_expect(fpu_ctxt[FPU_WORD_SIZE_OFFSET], 8) )=0A+ = switch ( __builtin_expect(fpu_ctxt->x[FPU_WORD_SIZE_OFFSET], 8) )=0A = {=0A default:=0A asm volatile (=0A@@ -80,8 +96,7 @@ static = inline void fpu_fxrstor(struct vc=0A ".previous = \n"=0A _ASM_EXTABLE(1b, 2b)=0A :=0A- : = "m" (*fpu_ctxt),=0A- "i" (sizeof(v->arch.xsave_area->fpu_sse) = / 4),=0A+ : "m" (*fpu_ctxt), "i" (sizeof(*fpu_ctxt) / 4),=0A = "cdaSDb" (fpu_ctxt) );=0A break;=0A case 4: case = 2:=0A@@ -102,8 +117,7 @@ static inline void fpu_fxrstor(struct vc=0A = ".previous \n"=0A _ASM_EXTABLE(1b, 2b)=0A = :=0A- : "m" (*fpu_ctxt),=0A- "i" = (sizeof(v->arch.xsave_area->fpu_sse) / 4) );=0A+ : "m" = (*fpu_ctxt), "i" (sizeof(*fpu_ctxt) / 4) );=0A break;=0A }=0A = }=0A@@ -156,7 +170,7 @@ static inline void fpu_fxsave(struct vcp=0A = */=0A if ( !(fpu_ctxt->fsw & 0x0080) &&=0A = boot_cpu_data.x86_vendor =3D=3D X86_VENDOR_AMD )=0A- word_size = =3D -1;=0A+ return;=0A =0A if ( word_size > 0 &&=0A = !((fpu_ctxt->fip.addr | fpu_ctxt->fdp.addr) >> 32) )=0A@@ -177,25 = +191,6 @@ static inline void fpu_fxsave(struct vcp=0A =0A if ( = word_size >=3D 0 )=0A fpu_ctxt->x[FPU_WORD_SIZE_OFFSET] =3D = word_size;=0A- =0A- /* Clear exception flags if FSW.ES is set. = */=0A- if ( unlikely(fpu_ctxt->fsw & 0x0080) )=0A- asm volatile = ("fnclex");=0A- =0A- /*=0A- * AMD CPUs don't save/restore = FDP/FIP/FOP unless an exception=0A- * is pending. Clear the x87 state = here by setting it to fixed=0A- * values. The hypervisor data segment = can be sometimes 0 and=0A- * sometimes new user value. Both should be = ok. Use the FPU saved=0A- * data block as a safe address because it = should be in L1.=0A- */=0A- if ( boot_cpu_data.x86_vendor =3D=3D = X86_VENDOR_AMD )=0A- {=0A- asm volatile (=0A- = "emms\n\t" /* clear stack tags */=0A- "fildl %0" /* load to = clear state */=0A- : : "m" (*fpu_ctxt) );=0A- }=0A }=0A =0A = /* Save x87 FPU state */=0A --=__PartC6F637F2.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 --=__PartC6F637F2.2__=--