From: "Jan Beulich" Subject: Re: XSAVE IRC thread Date: Fri, 03 May 2013 15:58:40 +0100 Message-ID: <5183ECC002000078000D3274@nat28.tlf.novell.com> References: <517FCF4F.70209@citrix.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartDAEA05B0.1__=" Return-path: In-Reply-To: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Ben Guthro , Mark Roddy Cc: xen-devel 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. --=__PartDAEA05B0.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline >>> On 30.04.13 at 18:24, Mark Roddy wrote: > The two values are pointers to the before and after regions: >=20 > 1: kd> dd 841c4880 > 841c4880 0000027f 00000000 6e83506e 0000001b > 841c4890 01d113d8 00000023 00001f80 0000ffff > 841c48a0 00000000 00000000 00000000 00000000 > 841c48b0 00000000 00000000 00000000 00000000 > 841c48c0 00000000 00000000 00000000 00000000 > 841c48d0 00000000 00000000 00000000 00000000 > 841c48e0 00000000 00000000 00000000 00000000 > 841c48f0 00000000 00000000 00000000 00000000 >=20 > 1: kd> dd 841c4600=20 > 841c4600 0000027f 00000000 6e83506e 00000000 > 841c4610 01d113d8 00000000 00001f80 0000ffff > 841c4620 00000000 00000000 00000000 00000000 > 841c4630 00000000 00000000 00000000 00000000 > 841c4640 00000000 00000000 00000000 00000000 > 841c4650 00000000 00000000 00000000 00000000 > 841c4660 00000000 00000000 00000000 00000000 > 841c4670 00000000 00000000 00000000 00000000 >=20 > I don't know if that helps, but obviously there are differences, two = of=20 > them, at offsets 0xc and 0x12. >=20 > " Interrupt Service Routine 88CAC91C has changed extended thread = context. > Context saved before executing ISR: 841C4880. Context saved after = executing=20 > ISR: 841C4600." >=20 > 841C4880 is before and 841C4600 is after. Attached a patch that I think should address this problem. It's against the tip of the staging tree, and doesn't apply without adjustment to 4.2 (and making it work for 4.1 would be quite a bit more work) - please let me know whether that's sufficient for you testing this, or whether you need me to do any backporting. I didn't verify this with any Windows, but since the same issue can - if one is looking for it - be observed on PV Linux, I did verify the patch to help there. I'd like to note though that while this is expected to help with 32-bit guests, and with a 64-bit guest kernels doing such checking after using the respective save (and possibly restore) instructions with a 64-bit operand size, the hypervisor has no way of knowing whether the context actually belongs to a 32-bit process while the guest is in kernel (64-bit) mode. That means that from a 32-bit app's perspective, inconsistencies could still be observed under certain conditions (but the case where the hypervisor side save happens after a VM exit from user mode should also work with that patch). I don't see any way to hide that, other than running on CPUs that don't save/restore the selector values at all anymore (Intel at least has a feature bit for this). Jan --=__PartDAEA05B0.1__= Content-Type: text/plain; name="x86-FPU-preserve-selectors.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-FPU-preserve-selectors.patch" --- a/xen/arch/x86/i387.c=0A+++ b/xen/arch/x86/i387.c=0A@@ -55,28 +55,54 = @@ static inline void fpu_fxrstor(struct vc=0A * possibility, which = may occur if the block was passed to us by control=0A * tools, by = silently clearing the block.=0A */=0A- asm volatile (=0A- = /* See above for why the operands/constraints are this way. */=0A- = "1: " REX64_PREFIX "fxrstor (%2)\n"=0A- ".section .fixup,\"ax\" = \n"=0A- "2: push %%"__OP"ax \n"=0A- " push %%"__OP"cx= \n"=0A- " push %%"__OP"di \n"=0A- " lea = %0,%%"__OP"di \n"=0A- " mov %1,%%ecx \n"=0A- " = xor %%eax,%%eax \n"=0A- " rep ; stosl \n"=0A- = " pop %%"__OP"di \n"=0A- " pop %%"__OP"cx = \n"=0A- " pop %%"__OP"ax \n"=0A- " jmp 1b = \n"=0A- ".previous \n"=0A- _ASM_EXTABLE= (1b, 2b)=0A- : =0A- : "m" (*fpu_ctxt),=0A- "i" = (sizeof(v->arch.xsave_area->fpu_sse)/4)=0A- ,"cdaSDb" (fpu_ctxt)= =0A- );=0A+ switch ( __builtin_expect(fpu_ctxt[FPU_WORD_SIZE_OFFS= ET], 8) )=0A+ {=0A+ default:=0A+ asm volatile (=0A+ = /* See below for why the operands/constraints are this way. */=0A+ = "1: " REX64_PREFIX "fxrstor (%2)\n"=0A+ ".section .fixup,\"a= x\" \n"=0A+ "2: push %%"__OP"ax \n"=0A+ " = push %%"__OP"cx \n"=0A+ " push %%"__OP"di \n"=0A+ = " mov %2,%%"__OP"di \n"=0A+ " mov %1,%%ecx = \n"=0A+ " xor %%eax,%%eax \n"=0A+ " = rep ; stosl \n"=0A+ " pop %%"__OP"di \n"=0A+ = " pop %%"__OP"cx \n"=0A+ " pop %%"__OP"ax= \n"=0A+ " jmp 1b \n"=0A+ = ".previous \n"=0A+ _ASM_EXTABLE(1b, 2b)=0A+ = :=0A+ : "m" (*fpu_ctxt),=0A+ "i" (sizeof(v->a= rch.xsave_area->fpu_sse)/4),=0A+ "cdaSDb" (fpu_ctxt) );=0A+ = break;=0A+ case 4: case 2:=0A+ asm volatile (=0A+ = "1: fxrstor (%2)\n"=0A+ ".section .fixup,\"ax\" \n"=0A+ = "2: push %%"__OP"ax \n"=0A+ " push %%"__OP"cx = \n"=0A+ " push %%"__OP"di \n"=0A+ " mov = %2,%%"__OP"di \n"=0A+ " mov %1,%%ecx \n"=0A+ = " xor %%eax,%%eax \n"=0A+ " rep ; stosl = \n"=0A+ " pop %%"__OP"di \n"=0A+ " pop = %%"__OP"cx \n"=0A+ " pop %%"__OP"ax \n"=0A+ = " jmp 1b \n"=0A+ ".previous = \n"=0A+ _ASM_EXTABLE(1b, 2b)=0A+ :=0A+ = : "m" (*fpu_ctxt),=0A+ "i" (sizeof(v->arch.xsave_area->fpu_sse= )/4),=0A+ "r" (fpu_ctxt) );=0A+ break;=0A+ }=0A = }=0A =0A /* Restore x87 extended state */=0A@@ -105,15 +131,24 @@ static = inline void fpu_xsave(struct vcpu=0A static inline void fpu_fxsave(struct = vcpu *v)=0A {=0A char *fpu_ctxt =3D v->arch.fpu_ctxt;=0A+ int = word_size =3D guest_word_size(v);=0A =0A- /*=0A- * The only way to = force fxsaveq on a wide range of gas versions. On =0A- * older = versions the rex64 prefix works only if we force an=0A- * addressing = mode that doesn't require extended registers.=0A- */=0A- asm = volatile (=0A- REX64_PREFIX "fxsave (%1)"=0A- : "=3Dm" = (*fpu_ctxt) : "cdaSDb" (fpu_ctxt) );=0A+ switch ( __builtin_expect(word_= size, 8) )=0A+ {=0A+ default:=0A+ /*=0A+ * The only = way to force fxsaveq on a wide range of gas versions.=0A+ * On = older versions the rex64 prefix works only if we force an=0A+ * = addressing mode that doesn't require extended registers.=0A+ = */=0A+ asm volatile ( REX64_PREFIX "fxsave (%1)"=0A+ = : "=3Dm" (*fpu_ctxt) : "cdaSDb" (fpu_ctxt) );=0A+ break;=0A+ = case 4: case 2:=0A+ asm volatile ( "fxsave %0" : "=3Dm" = (*fpu_ctxt) );=0A+ break;=0A+ }=0A+ fpu_ctxt[FPU_WORD_SIZE_OFF= SET] =3D word_size;=0A =0A /* Clear exception flags if FSW.ES is = set. */=0A if ( unlikely(fpu_ctxt[2] & 0x80) )=0A@@ -253,6 +288,39 @@ = void vcpu_destroy_fpu(struct vcpu *v)=0A xfree(v->arch.fpu_ctxt);= =0A }=0A =0A+int guest_word_size(struct vcpu *v)=0A+{=0A+ int = mode;=0A+=0A+ if ( !is_hvm_vcpu(v) )=0A+ {=0A+ if ( is_pv_32bi= t_vcpu(v) )=0A+ return 4;=0A+=0A+ asm ( "1: lar %1,%0 = \n"=0A+ " jnz 2f \n"=0A+ "3: = \n"=0A+ ".section .fixup,\"ax\"\n"=0A+ = "2: xor %0,%0 \n"=0A+ " jmp 3b = \n"=0A+ ".previous \n"=0A+ = _ASM_EXTABLE(1b, 2b)=0A+ : "=3Dr" (mode)=0A+ : = "m" (guest_cpu_user_regs()->cs) );=0A+=0A+ return !(mode & = _SEGMENT_S) || (mode & _SEGMENT_L) ? 8 : 4;=0A+ }=0A+=0A+ switch ( = mode =3D hvm_guest_x86_mode(v) )=0A+ {=0A+ case 0: /* real mode = */=0A+ case 1: /* virtual mode */=0A+ return 2;=0A+ }=0A+=0A+ = return mode;=0A+}=0A+=0A /*=0A * Local variables:=0A * mode: C=0A--- = a/xen/arch/x86/xstate.c=0A+++ b/xen/arch/x86/xstate.c=0A@@ -56,32 +56,53 = @@ void xsave(struct vcpu *v, uint64_t mask=0A struct xsave_struct = *ptr =3D v->arch.xsave_area;=0A uint32_t hmask =3D mask >> 32;=0A = uint32_t lmask =3D mask;=0A+ int word_size =3D guest_word_size(v);=0A = =0A- if ( cpu_has_xsaveopt )=0A- asm volatile (=0A- = ".byte " REX_PREFIX "0x0f,0xae,0x37"=0A- :=0A- : "a" = (lmask), "d" (hmask), "D"(ptr)=0A- : "memory" );=0A- = else=0A- asm volatile (=0A- ".byte " REX_PREFIX = "0x0f,0xae,0x27"=0A- :=0A- : "a" (lmask), "d" = (hmask), "D"(ptr)=0A- : "memory" );=0A+ switch ( __builtin_ex= pect(word_size, 8) )=0A+ {=0A+ default:=0A+ if ( cpu_has_xsave= opt )=0A+ asm volatile ( ".byte 0x48,0x0f,0xae,0x37"=0A+ = : "=3Dm" (*ptr)=0A+ : "a" = (lmask), "d" (hmask), "D" (ptr) );=0A+ else=0A+ asm = volatile ( ".byte 0x48,0x0f,0xae,0x27"=0A+ : = "=3Dm" (*ptr)=0A+ : "a" (lmask), "d" (hmask), = "D" (ptr) );=0A+ break;=0A+ case 4: case 2:=0A+ if ( = cpu_has_xsaveopt )=0A+ asm volatile ( ".byte 0x0f,0xae,0x37"=0A+= : "=3Dm" (*ptr)=0A+ : = "a" (lmask), "d" (hmask), "D" (ptr) );=0A+ else=0A+ asm = volatile ( ".byte 0x0f,0xae,0x27"=0A+ : "=3Dm" = (*ptr)=0A+ : "a" (lmask), "d" (hmask), "D" (ptr) = );=0A+ break;=0A+ }=0A+ ptr->fpu_sse.x[FPU_WORD_SIZE_OFFSET] = =3D word_size;=0A }=0A =0A void xrstor(struct vcpu *v, uint64_t mask)=0A = {=0A uint32_t hmask =3D mask >> 32;=0A uint32_t lmask =3D = mask;=0A-=0A struct xsave_struct *ptr =3D v->arch.xsave_area;=0A =0A- = asm volatile (=0A- ".byte " REX_PREFIX "0x0f,0xae,0x2f"=0A- = :=0A- : "m" (*ptr), "a" (lmask), "d" (hmask), "D"(ptr) );=0A+ = switch ( __builtin_expect(ptr->fpu_sse.x[FPU_WORD_SIZE_OFFSET], 8) )=0A+ = {=0A+ default:=0A+ asm volatile ( ".byte 0x48,0x0f,0xae,0x2f"=0A= + :=0A+ : "m" (*ptr), "a" = (lmask), "d" (hmask), "D" (ptr) );=0A+ break;=0A+ case 4: case = 2:=0A+ asm volatile ( ".byte 0x0f,0xae,0x2f"=0A+ = :=0A+ : "m" (*ptr), "a" (lmask), "d" (hmask), "D" = (ptr) );=0A+ break;=0A+ }=0A }=0A =0A bool_t xsave_enabled(const = struct vcpu *v)=0A--- a/xen/include/asm-x86/xstate.h=0A+++ b/xen/include/as= m-x86/xstate.h=0A@@ -34,8 +34,6 @@=0A #define XSTATE_NONLAZY (XSTATE_LWP)= =0A #define XSTATE_LAZY (XSTATE_ALL & ~XSTATE_NONLAZY)=0A =0A-#define = REX_PREFIX "0x48, "=0A-=0A /* extended state variables */=0A DECLARE_PE= R_CPU(uint64_t, xcr0);=0A =0A@@ -88,4 +86,14 @@ void xstate_free_save_area(= struct vcpu *=0A int xstate_alloc_save_area(struct vcpu *v);=0A void = xstate_init(void);=0A =0A+/* Byte offset within the FXSAVE (portion) of = the stored word size. */=0A+#define FPU_WORD_SIZE_OFFSET 511=0A+=0A+/*=0A+ = * Used EXCLUSIVELY to determine the needed operand size override on=0A+ * = XSAVE/FXSAVE. Any other use would need to make sure that the context=0A+ * = is suitable for all operations this involves.=0A+ */=0A+int guest_word_size= (struct vcpu *);=0A+=0A #endif /* __ASM_XSTATE_H */=0A --=__PartDAEA05B0.1__= 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 --=__PartDAEA05B0.1__=--