From: "Jan Beulich" Subject: [PATCH] x86: AVX instruction emulation fixes Date: Wed, 28 Aug 2013 09:32:45 +0100 Message-ID: <521DD1CD02000078000EF000@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part2F1C6DBD.0__=" Return-path: Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1VEbBM-000121-KT for xen-devel@lists.xenproject.org; Wed, 28 Aug 2013 08:32:53 +0000 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: Keir Fraser 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. --=__Part2F1C6DBD.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline - we used the C4/C5 (first prefix) byte instead of the apparent ModR/M one as the second prefix byte - early decoding normalized vex.reg, thus corrupting it for the main consumer (copy_REX_VEX()), resulting in #UD on the two-operand instructions we emulate Also add respective test cases to the testing utility plus - fix get_fpu() (the fall-through order was inverted) - add cpu_has_avx2, even if it's currently unused (as in the new test cases I decided to refrain from using AVX2 instructions in order to be able to actually run all the tests on the hardware I have) - slightly tweak cpu_has_avx to more consistently express the outputs we don't care about (sinking them all into the same variable) Signed-off-by: Jan Beulich --- a/tools/tests/x86_emulator/test_x86_emulator.c +++ b/tools/tests/x86_emulator/test_x86_emulator.c @@ -94,13 +94,25 @@ static inline uint64_t xgetbv(uint32_t x } =20 #define cpu_has_avx ({ \ - unsigned int eax =3D 1, ecx =3D 0, edx; \ - cpuid(&eax, &edx, &ecx, &edx, NULL); \ + unsigned int eax =3D 1, ecx =3D 0; \ + cpuid(&eax, &eax, &ecx, &eax, NULL); \ if ( !(ecx & (1U << 27)) || ((xgetbv(0) & 6) !=3D 6) ) \ ecx =3D 0; \ (ecx & (1U << 28)) !=3D 0; \ }) =20 +#define cpu_has_avx2 ({ \ + unsigned int eax =3D 1, ebx, ecx =3D 0; \ + cpuid(&eax, &ebx, &ecx, &eax, NULL); \ + if ( !(ecx & (1U << 27)) || ((xgetbv(0) & 6) !=3D 6) ) \ + ebx =3D 0; \ + else { \ + eax =3D 7, ecx =3D 0; \ + cpuid(&eax, &ebx, &ecx, &eax, NULL); \ + } \ + (ebx & (1U << 5)) !=3D 0; \ +}) + int get_fpu( void (*exception_callback)(void *, struct cpu_user_regs *), void *exception_callback_arg, @@ -111,14 +123,14 @@ int get_fpu( { case X86EMUL_FPU_fpu: break; - case X86EMUL_FPU_ymm: - if ( cpu_has_avx ) + case X86EMUL_FPU_mmx: + if ( cpu_has_mmx ) break; case X86EMUL_FPU_xmm: if ( cpu_has_sse ) break; - case X86EMUL_FPU_mmx: - if ( cpu_has_mmx ) + case X86EMUL_FPU_ymm: + if ( cpu_has_avx ) break; default: return X86EMUL_UNHANDLEABLE; @@ -629,6 +641,73 @@ int main(int argc, char **argv) else printf("skipped\n"); =20 + printf("%-40s", "Testing vmovdqu %ymm2,(%ecx)..."); + if ( stack_exec && cpu_has_avx ) + { + extern const unsigned char vmovdqu_to_mem[]; + + asm volatile ( "vpcmpeqb %%xmm2, %%xmm2, %%xmm2\n" + ".pushsection .test, \"a\", @progbits\n" + "vmovdqu_to_mem: vmovdqu %%ymm2, (%0)\n" + ".popsection" :: "c" (NULL) ); + + memcpy(instr, vmovdqu_to_mem, 15); + memset(res, 0x55, 128); + memset(res + 16, 0xff, 16); + memset(res + 20, 0x00, 16); + regs.eip =3D (unsigned long)&instr[0]; + regs.ecx =3D (unsigned long)res; + rc =3D x86_emulate(&ctxt, &emulops); + if ( (rc !=3D X86EMUL_OKAY) || memcmp(res, res + 16, 64) ) + goto fail; + printf("okay\n"); + } + else + printf("skipped\n"); + + printf("%-40s", "Testing vmovdqu (%edx),%ymm4..."); + if ( stack_exec && cpu_has_avx ) + { + extern const unsigned char vmovdqu_from_mem[]; + +#if 0 /* Don't use AVX2 instructions for now */ + asm volatile ( "vpcmpgtb %%ymm4, %%ymm4, %%ymm4\n" +#else + asm volatile ( "vpcmpgtb %%xmm4, %%xmm4, %%xmm4\n\t" + "vinsertf128 $1, %%xmm4, %%ymm4, %%ymm4\n" +#endif + ".pushsection .test, \"a\", @progbits\n" + "vmovdqu_from_mem: vmovdqu (%0), %%ymm4\n" + ".popsection" :: "d" (NULL) ); + + memcpy(instr, vmovdqu_from_mem, 15); + memset(res + 4, 0xff, 16); + regs.eip =3D (unsigned long)&instr[0]; + regs.ecx =3D 0; + regs.edx =3D (unsigned long)res; + rc =3D x86_emulate(&ctxt, &emulops); + if ( rc !=3D X86EMUL_OKAY ) + goto fail; +#if 0 /* Don't use AVX2 instructions for now */ + asm ( "vpcmpeqb %%ymm2, %%ymm2, %%ymm2\n\t" + "vpcmpeqb %%ymm4, %%ymm2, %%ymm0\n\t" + "vpmovmskb %%ymm1, %0" : "=3Dr" (rc) ); +#else + asm ( "vextractf128 $1, %%ymm4, %%xmm3\n\t" + "vpcmpeqb %%xmm2, %%xmm2, %%xmm2\n\t" + "vpcmpeqb %%xmm4, %%xmm2, %%xmm0\n\t" + "vpcmpeqb %%xmm3, %%xmm2, %%xmm1\n\t" + "vpmovmskb %%xmm0, %0\n\t" + "vpmovmskb %%xmm1, %1" : "=3Dr" (rc), "=3Dr" (i) ); + rc |=3D i << 16; +#endif + if ( rc !=3D 0xffffffff ) + goto fail; + printf("okay\n"); + } + else + printf("skipped\n"); + printf("%-40s", "Testing movsd %xmm5,(%ecx)..."); memset(res, 0x77, 64); memset(res + 10, 0x66, 8); @@ -683,6 +762,59 @@ int main(int argc, char **argv) else printf("skipped\n"); =20 + printf("%-40s", "Testing vmovsd %xmm5,(%ecx)..."); + memset(res, 0x88, 64); + memset(res + 10, 0x77, 8); + if ( stack_exec && cpu_has_avx ) + { + extern const unsigned char vmovsd_to_mem[]; + + asm volatile ( "vbroadcastsd %0, %%ymm5\n" + ".pushsection .test, \"a\", @progbits\n" + "vmovsd_to_mem: vmovsd %%xmm5, (%1)\n" + ".popsection" :: "m" (res[10]), "c" (NULL) ); + + memcpy(instr, vmovsd_to_mem, 15); + regs.eip =3D (unsigned long)&instr[0]; + regs.ecx =3D (unsigned long)(res + 2); + regs.edx =3D 0; + rc =3D x86_emulate(&ctxt, &emulops); + if ( (rc !=3D X86EMUL_OKAY) || memcmp(res, res + 8, 32) ) + goto fail; + printf("okay\n"); + } + else + { + printf("skipped\n"); + memset(res + 2, 0x77, 8); + } + + printf("%-40s", "Testing vmovaps (%edx),%ymm7..."); + if ( stack_exec && cpu_has_avx ) + { + extern const unsigned char vmovaps_from_mem[]; + + asm volatile ( "vxorps %%ymm7, %%ymm7, %%ymm7\n" + ".pushsection .test, \"a\", @progbits\n" + "vmovaps_from_mem: vmovaps (%0), %%ymm7\n" + ".popsection" :: "d" (NULL) ); + + memcpy(instr, vmovaps_from_mem, 15); + regs.eip =3D (unsigned long)&instr[0]; + regs.ecx =3D 0; + regs.edx =3D (unsigned long)res; + rc =3D x86_emulate(&ctxt, &emulops); + if ( rc !=3D X86EMUL_OKAY ) + goto fail; + asm ( "vcmpeqps %1, %%ymm7, %%ymm0\n\t" + "vmovmskps %%ymm0, %0" : "=3Dr" (rc) : "m" (res[8]) ); + if ( rc !=3D 0xff ) + goto fail; + printf("okay\n"); + } + else + printf("skipped\n"); + for ( j =3D 1; j <=3D 2; j++ ) { #if defined(__i386__) --- a/xen/arch/x86/x86_emulate/x86_emulate.c +++ b/xen/arch/x86/x86_emulate/x86_emulate.c @@ -1454,10 +1454,10 @@ x86_emulate( /* VEX */ generate_exception_if(rex_prefix || vex.pfx, EXC_UD, -1); =20 - vex.raw[0] =3D b; + vex.raw[0] =3D modrm; if ( b & 1 ) { - vex.raw[1] =3D b; + vex.raw[1] =3D modrm; vex.opcx =3D vex_0f; vex.x =3D 1; vex.b =3D 1; @@ -1479,10 +1479,7 @@ x86_emulate( } } } - vex.reg ^=3D 0xf; - if ( !mode_64bit() ) - vex.reg &=3D 0x7; - else if ( !vex.r ) + if ( mode_64bit() && !vex.r ) rex_prefix |=3D REX_R; =20 fail_if(vex.opcx !=3D vex_0f); @@ -3899,8 +3896,9 @@ x86_emulate( else { fail_if((vex.opcx !=3D vex_0f) || - (vex.reg && ((ea.type =3D=3D OP_MEM) || - !(vex.pfx & VEX_PREFIX_SCALAR_MASK)))); + ((vex.reg !=3D 0xf) && + ((ea.type =3D=3D OP_MEM) || + !(vex.pfx & VEX_PREFIX_SCALAR_MASK)))); vcpu_must_have_avx(); get_fpu(X86EMUL_FPU_ymm, &fic); ea.bytes =3D 16 << vex.l; @@ -4168,7 +4166,7 @@ x86_emulate( } else { - fail_if((vex.opcx !=3D vex_0f) || vex.reg || + fail_if((vex.opcx !=3D vex_0f) || (vex.reg !=3D 0xf) || ((vex.pfx !=3D vex_66) && (vex.pfx !=3D vex_f3))); vcpu_must_have_avx(); get_fpu(X86EMUL_FPU_ymm, &fic); --=__Part2F1C6DBD.0__= Content-Type: text/plain; name="x86-emul-test-AVX.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-emul-test-AVX.patch" x86: AVX instruction emulation fixes=0A=0A- we used the C4/C5 (first = prefix) byte instead of the apparent ModR/M=0A one as the second prefix = byte=0A- early decoding normalized vex.reg, thus corrupting it for the = main=0A consumer (copy_REX_VEX()), resulting in #UD on the two-operand=0A = instructions we emulate=0A=0AAlso add respective test cases to the = testing utility plus=0A- fix get_fpu() (the fall-through order was = inverted)=0A- add cpu_has_avx2, even if it's currently unused (as in the = new test=0A cases I decided to refrain from using AVX2 instructions in = order to=0A be able to actually run all the tests on the hardware I = have)=0A- slightly tweak cpu_has_avx to more consistently express the = outputs=0A we don't care about (sinking them all into the same = variable)=0A=0ASigned-off-by: Jan Beulich =0A=0A--- = a/tools/tests/x86_emulator/test_x86_emulator.c=0A+++ b/tools/tests/x86_emul= ator/test_x86_emulator.c=0A@@ -94,13 +94,25 @@ static inline uint64_t = xgetbv(uint32_t x=0A }=0A =0A #define cpu_has_avx ({ \=0A- unsigned int = eax =3D 1, ecx =3D 0, edx; \=0A- cpuid(&eax, &edx, &ecx, &edx, NULL); = \=0A+ unsigned int eax =3D 1, ecx =3D 0; \=0A+ cpuid(&eax, &eax, = &ecx, &eax, NULL); \=0A if ( !(ecx & (1U << 27)) || ((xgetbv(0) & 6) = !=3D 6) ) \=0A ecx =3D 0; \=0A (ecx & (1U << 28)) !=3D 0; \=0A = })=0A =0A+#define cpu_has_avx2 ({ \=0A+ unsigned int eax =3D 1, ebx, = ecx =3D 0; \=0A+ cpuid(&eax, &ebx, &ecx, &eax, NULL); \=0A+ if ( = !(ecx & (1U << 27)) || ((xgetbv(0) & 6) !=3D 6) ) \=0A+ ebx =3D 0; = \=0A+ else { \=0A+ eax =3D 7, ecx =3D 0; \=0A+ cpuid(&eax,= &ebx, &ecx, &eax, NULL); \=0A+ } \=0A+ (ebx & (1U << 5)) !=3D 0; = \=0A+})=0A+=0A int get_fpu(=0A void (*exception_callback)(void *, = struct cpu_user_regs *),=0A void *exception_callback_arg,=0A@@ -111,14 = +123,14 @@ int get_fpu(=0A {=0A case X86EMUL_FPU_fpu:=0A = break;=0A- case X86EMUL_FPU_ymm:=0A- if ( cpu_has_avx )=0A+ = case X86EMUL_FPU_mmx:=0A+ if ( cpu_has_mmx )=0A = break;=0A case X86EMUL_FPU_xmm:=0A if ( cpu_has_sse )=0A = break;=0A- case X86EMUL_FPU_mmx:=0A- if ( cpu_has_mmx = )=0A+ case X86EMUL_FPU_ymm:=0A+ if ( cpu_has_avx )=0A = break;=0A default:=0A return X86EMUL_UNHANDLEABLE;=0A@@ = -629,6 +641,73 @@ int main(int argc, char **argv)=0A else=0A = printf("skipped\n");=0A =0A+ printf("%-40s", "Testing vmovdqu %ymm2,(%ec= x)...");=0A+ if ( stack_exec && cpu_has_avx )=0A+ {=0A+ = extern const unsigned char vmovdqu_to_mem[];=0A+=0A+ asm volatile ( = "vpcmpeqb %%xmm2, %%xmm2, %%xmm2\n"=0A+ ".pushsection= .test, \"a\", @progbits\n"=0A+ "vmovdqu_to_mem: = vmovdqu %%ymm2, (%0)\n"=0A+ ".popsection" :: "c" = (NULL) );=0A+=0A+ memcpy(instr, vmovdqu_to_mem, 15);=0A+ = memset(res, 0x55, 128);=0A+ memset(res + 16, 0xff, 16);=0A+ = memset(res + 20, 0x00, 16);=0A+ regs.eip =3D (unsigned long)&inst= r[0];=0A+ regs.ecx =3D (unsigned long)res;=0A+ rc =3D = x86_emulate(&ctxt, &emulops);=0A+ if ( (rc !=3D X86EMUL_OKAY) || = memcmp(res, res + 16, 64) )=0A+ goto fail;=0A+ printf("ok= ay\n");=0A+ }=0A+ else=0A+ printf("skipped\n");=0A+=0A+ = printf("%-40s", "Testing vmovdqu (%edx),%ymm4...");=0A+ if ( stack_exec = && cpu_has_avx )=0A+ {=0A+ extern const unsigned char vmovdqu_fro= m_mem[];=0A+=0A+#if 0 /* Don't use AVX2 instructions for now */=0A+ = asm volatile ( "vpcmpgtb %%ymm4, %%ymm4, %%ymm4\n"=0A+#else=0A+ asm = volatile ( "vpcmpgtb %%xmm4, %%xmm4, %%xmm4\n\t"=0A+ = "vinsertf128 $1, %%xmm4, %%ymm4, %%ymm4\n"=0A+#endif=0A+ = ".pushsection .test, \"a\", @progbits\n"=0A+ = "vmovdqu_from_mem: vmovdqu (%0), %%ymm4\n"=0A+ = ".popsection" :: "d" (NULL) );=0A+=0A+ memcpy(instr, vmovdqu_from_me= m, 15);=0A+ memset(res + 4, 0xff, 16);=0A+ regs.eip =3D = (unsigned long)&instr[0];=0A+ regs.ecx =3D 0;=0A+ = regs.edx =3D (unsigned long)res;=0A+ rc =3D x86_emulate(&ctxt, = &emulops);=0A+ if ( rc !=3D X86EMUL_OKAY )=0A+ goto = fail;=0A+#if 0 /* Don't use AVX2 instructions for now */=0A+ asm ( = "vpcmpeqb %%ymm2, %%ymm2, %%ymm2\n\t"=0A+ "vpcmpeqb %%ymm4, = %%ymm2, %%ymm0\n\t"=0A+ "vpmovmskb %%ymm1, %0" : "=3Dr" (rc) = );=0A+#else=0A+ asm ( "vextractf128 $1, %%ymm4, %%xmm3\n\t"=0A+ = "vpcmpeqb %%xmm2, %%xmm2, %%xmm2\n\t"=0A+ "vpcmpeqb = %%xmm4, %%xmm2, %%xmm0\n\t"=0A+ "vpcmpeqb %%xmm3, %%xmm2, = %%xmm1\n\t"=0A+ "vpmovmskb %%xmm0, %0\n\t"=0A+ = "vpmovmskb %%xmm1, %1" : "=3Dr" (rc), "=3Dr" (i) );=0A+ rc |=3D i = << 16;=0A+#endif=0A+ if ( rc !=3D 0xffffffff )=0A+ goto = fail;=0A+ printf("okay\n");=0A+ }=0A+ else=0A+ = printf("skipped\n");=0A+=0A printf("%-40s", "Testing movsd %xmm5,(%ecx)= ...");=0A memset(res, 0x77, 64);=0A memset(res + 10, 0x66, = 8);=0A@@ -683,6 +762,59 @@ int main(int argc, char **argv)=0A else=0A = printf("skipped\n");=0A =0A+ printf("%-40s", "Testing vmovsd = %xmm5,(%ecx)...");=0A+ memset(res, 0x88, 64);=0A+ memset(res + 10, = 0x77, 8);=0A+ if ( stack_exec && cpu_has_avx )=0A+ {=0A+ = extern const unsigned char vmovsd_to_mem[];=0A+=0A+ asm volatile ( = "vbroadcastsd %0, %%ymm5\n"=0A+ ".pushsection .test, = \"a\", @progbits\n"=0A+ "vmovsd_to_mem: vmovsd = %%xmm5, (%1)\n"=0A+ ".popsection" :: "m" (res[10]), = "c" (NULL) );=0A+=0A+ memcpy(instr, vmovsd_to_mem, 15);=0A+ = regs.eip =3D (unsigned long)&instr[0];=0A+ regs.ecx =3D = (unsigned long)(res + 2);=0A+ regs.edx =3D 0;=0A+ rc =3D = x86_emulate(&ctxt, &emulops);=0A+ if ( (rc !=3D X86EMUL_OKAY) || = memcmp(res, res + 8, 32) )=0A+ goto fail;=0A+ printf("oka= y\n");=0A+ }=0A+ else=0A+ {=0A+ printf("skipped\n");=0A+ = memset(res + 2, 0x77, 8);=0A+ }=0A+=0A+ printf("%-40s", = "Testing vmovaps (%edx),%ymm7...");=0A+ if ( stack_exec && cpu_has_avx = )=0A+ {=0A+ extern const unsigned char vmovaps_from_mem[];=0A+=0A= + asm volatile ( "vxorps %%ymm7, %%ymm7, %%ymm7\n"=0A+ = ".pushsection .test, \"a\", @progbits\n"=0A+ = "vmovaps_from_mem: vmovaps (%0), %%ymm7\n"=0A+ = ".popsection" :: "d" (NULL) );=0A+=0A+ memcpy(instr, vmovaps_from_me= m, 15);=0A+ regs.eip =3D (unsigned long)&instr[0];=0A+ = regs.ecx =3D 0;=0A+ regs.edx =3D (unsigned long)res;=0A+ = rc =3D x86_emulate(&ctxt, &emulops);=0A+ if ( rc !=3D X86EMUL_OKAY= )=0A+ goto fail;=0A+ asm ( "vcmpeqps %1, %%ymm7, = %%ymm0\n\t"=0A+ "vmovmskps %%ymm0, %0" : "=3Dr" (rc) : "m" = (res[8]) );=0A+ if ( rc !=3D 0xff )=0A+ goto fail;=0A+ = printf("okay\n");=0A+ }=0A+ else=0A+ printf("skipped\n");= =0A+=0A for ( j =3D 1; j <=3D 2; j++ )=0A {=0A #if defined(__i386__= )=0A--- a/xen/arch/x86/x86_emulate/x86_emulate.c=0A+++ b/xen/arch/x86/x86_e= mulate/x86_emulate.c=0A@@ -1454,10 +1454,10 @@ x86_emulate(=0A = /* VEX */=0A generate_exception_if(rex_prefix || = vex.pfx, EXC_UD, -1);=0A =0A- vex.raw[0] =3D b;=0A+ = vex.raw[0] =3D modrm;=0A if ( b & 1 )=0A = {=0A- vex.raw[1] =3D b;=0A+ = vex.raw[1] =3D modrm;=0A vex.opcx =3D vex_0f;=0A = vex.x =3D 1;=0A vex.b =3D 1;=0A@@ = -1479,10 +1479,7 @@ x86_emulate(=0A }=0A = }=0A }=0A- vex.reg ^=3D 0xf;=0A- = if ( !mode_64bit() )=0A- vex.reg &=3D = 0x7;=0A- else if ( !vex.r )=0A+ if ( = mode_64bit() && !vex.r )=0A rex_prefix |=3D REX_R;=0A = =0A fail_if(vex.opcx !=3D vex_0f);=0A@@ -3899,8 +3896,9 @@ = x86_emulate(=0A else=0A {=0A fail_if((vex.opcx = !=3D vex_0f) ||=0A- (vex.reg && ((ea.type =3D=3D = OP_MEM) ||=0A- !(vex.pfx & VEX_PREFIX_SCALA= R_MASK))));=0A+ ((vex.reg !=3D 0xf) &&=0A+ = ((ea.type =3D=3D OP_MEM) ||=0A+ !(vex.pfx & = VEX_PREFIX_SCALAR_MASK))));=0A vcpu_must_have_avx();=0A = get_fpu(X86EMUL_FPU_ymm, &fic);=0A ea.bytes =3D 16 << = vex.l;=0A@@ -4168,7 +4166,7 @@ x86_emulate(=0A }=0A = else=0A {=0A- fail_if((vex.opcx !=3D vex_0f) || vex.reg = ||=0A+ fail_if((vex.opcx !=3D vex_0f) || (vex.reg !=3D 0xf) = ||=0A ((vex.pfx !=3D vex_66) && (vex.pfx !=3D = vex_f3)));=0A vcpu_must_have_avx();=0A get_fpu(X86E= MUL_FPU_ymm, &fic);=0A --=__Part2F1C6DBD.0__= 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 --=__Part2F1C6DBD.0__=--