All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH V3] tools/tests: Add EIP check to test_x86_emulator.c
@ 2014-08-07 11:38 Razvan Cojocaru
  2014-08-07 11:50 ` Jan Beulich
  0 siblings, 1 reply; 5+ messages in thread
From: Razvan Cojocaru @ 2014-08-07 11:38 UTC (permalink / raw)
  To: xen-devel
  Cc: ian.campbell, Razvan Cojocaru, stefano.stabellini,
	andrew.cooper3, ian.jackson, JBeulich

The test now also checks that EIP was modified after emulating
instructions after (and including) the "movq %mm3,(%ecx)..."
code block. This patch depends on Jan Beulich's "x86_emulate:
properly do IP updates and other side effects on success" patch
for the tests to succeed.

Changes since V1:
 - Now checking if the value in EIP is correct instead of simply
   checking that EIP has been modified.

Changes since V1:
 - Added brackets for (unsigned long)(&instr[0] + instr_size).
 - Added comment about patch dependencies.

Signed-off-by: Razvan Cojocaru <rcojocaru@bitdefender.com>
Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
 tools/tests/x86_emulator/test_x86_emulator.c |   80 +++++++++++++++++++-------
 1 file changed, 60 insertions(+), 20 deletions(-)

diff --git a/tools/tests/x86_emulator/test_x86_emulator.c b/tools/tests/x86_emulator/test_x86_emulator.c
index 0a00d5a..2d44fe1 100644
--- a/tools/tests/x86_emulator/test_x86_emulator.c
+++ b/tools/tests/x86_emulator/test_x86_emulator.c
@@ -602,20 +602,24 @@ int main(int argc, char **argv)
     printf("%-40s", "Testing movq %mm3,(%ecx)...");
     if ( stack_exec && cpu_has_mmx )
     {
-        extern const unsigned char movq_to_mem[];
+        extern const unsigned char movq_to_mem[], movq_to_mem_end[];
+        unsigned long instr_size;
 
         asm volatile ( "pcmpeqb %%mm3, %%mm3\n"
                        ".pushsection .test, \"a\", @progbits\n"
                        "movq_to_mem: movq %%mm3, (%0)\n"
+                       "movq_to_mem_end:\n"
                        ".popsection" :: "c" (NULL) );
 
+        instr_size = movq_to_mem_end - movq_to_mem;
         memcpy(instr, movq_to_mem, 15);
         memset(res, 0x33, 64);
         memset(res + 8, 0xff, 8);
         regs.eip    = (unsigned long)&instr[0];
         regs.ecx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             (regs.eip != (unsigned long)(&instr[0] + instr_size)) )
             goto fail;
         printf("okay\n");
     }
@@ -625,19 +629,23 @@ int main(int argc, char **argv)
     printf("%-40s", "Testing movq (%edx),%mm5...");
     if ( stack_exec && cpu_has_mmx )
     {
-        extern const unsigned char movq_from_mem[];
+        extern const unsigned char movq_from_mem[], movq_from_mem_end[];
+        unsigned long instr_size;
 
         asm volatile ( "pcmpgtb %%mm5, %%mm5\n"
                        ".pushsection .test, \"a\", @progbits\n"
                        "movq_from_mem: movq (%0), %%mm5\n"
+                       "movq_from_mem_end:\n"
                        ".popsection" :: "d" (NULL) );
 
+        instr_size = movq_from_mem_end - movq_from_mem;
         memcpy(instr, movq_from_mem, 15);
         regs.eip    = (unsigned long)&instr[0];
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY ||
+             (regs.eip != (unsigned long)(&instr[0] + instr_size)) )
             goto fail;
         asm ( "pcmpeqb %%mm3, %%mm3\n\t"
               "pcmpeqb %%mm5, %%mm3\n\t"
@@ -652,20 +660,24 @@ int main(int argc, char **argv)
     printf("%-40s", "Testing movdqu %xmm2,(%ecx)...");
     if ( stack_exec && cpu_has_sse2 )
     {
-        extern const unsigned char movdqu_to_mem[];
+        extern const unsigned char movdqu_to_mem[], movdqu_to_mem_end[];
+        unsigned long instr_size;
 
         asm volatile ( "pcmpeqb %%xmm2, %%xmm2\n"
                        ".pushsection .test, \"a\", @progbits\n"
                        "movdqu_to_mem: movdqu %%xmm2, (%0)\n"
+                       "movdqu_to_mem_end:\n"
                        ".popsection" :: "c" (NULL) );
 
+        instr_size = movdqu_to_mem_end - movdqu_to_mem;
         memcpy(instr, movdqu_to_mem, 15);
         memset(res, 0x55, 64);
         memset(res + 8, 0xff, 16);
         regs.eip    = (unsigned long)&instr[0];
         regs.ecx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             (regs.eip != (unsigned long)(&instr[0] + instr_size)) )
             goto fail;
         printf("okay\n");
     }
@@ -675,19 +687,23 @@ int main(int argc, char **argv)
     printf("%-40s", "Testing movdqu (%edx),%xmm4...");
     if ( stack_exec && cpu_has_sse2 )
     {
-        extern const unsigned char movdqu_from_mem[];
+        extern const unsigned char movdqu_from_mem[], movdqu_from_mem_end[];
+        unsigned long instr_size;
 
         asm volatile ( "pcmpgtb %%xmm4, %%xmm4\n"
                        ".pushsection .test, \"a\", @progbits\n"
                        "movdqu_from_mem: movdqu (%0), %%xmm4\n"
+                       "movdqu_from_mem_end:\n"
                        ".popsection" :: "d" (NULL) );
 
+        instr_size = movdqu_from_mem_end - movdqu_from_mem;
         memcpy(instr, movdqu_from_mem, 15);
         regs.eip    = (unsigned long)&instr[0];
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY ||
+             (regs.eip != (unsigned long)(&instr[0] + instr_size)) )
             goto fail;
         asm ( "pcmpeqb %%xmm2, %%xmm2\n\t"
               "pcmpeqb %%xmm4, %%xmm2\n\t"
@@ -702,13 +718,16 @@ int main(int argc, char **argv)
     printf("%-40s", "Testing vmovdqu %ymm2,(%ecx)...");
     if ( stack_exec && cpu_has_avx )
     {
-        extern const unsigned char vmovdqu_to_mem[];
+        extern const unsigned char vmovdqu_to_mem[], vmovdqu_to_mem_end[];
+        unsigned long instr_size;
 
         asm volatile ( "vpcmpeqb %%xmm2, %%xmm2, %%xmm2\n"
                        ".pushsection .test, \"a\", @progbits\n"
                        "vmovdqu_to_mem: vmovdqu %%ymm2, (%0)\n"
+                       "vmovdqu_to_mem_end:\n"
                        ".popsection" :: "c" (NULL) );
 
+        instr_size = vmovdqu_to_mem_end - vmovdqu_to_mem;
         memcpy(instr, vmovdqu_to_mem, 15);
         memset(res, 0x55, 128);
         memset(res + 16, 0xff, 16);
@@ -716,7 +735,8 @@ int main(int argc, char **argv)
         regs.eip    = (unsigned long)&instr[0];
         regs.ecx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 16, 64) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 16, 64) ||
+             (regs.eip != (unsigned long)(&instr[0] + instr_size)) )
             goto fail;
         printf("okay\n");
     }
@@ -726,7 +746,8 @@ int main(int argc, char **argv)
     printf("%-40s", "Testing vmovdqu (%edx),%ymm4...");
     if ( stack_exec && cpu_has_avx )
     {
-        extern const unsigned char vmovdqu_from_mem[];
+        extern const unsigned char vmovdqu_from_mem[], vmovdqu_from_mem_end[];
+        unsigned long instr_size;
 
 #if 0 /* Don't use AVX2 instructions for now */
         asm volatile ( "vpcmpgtb %%ymm4, %%ymm4, %%ymm4\n"
@@ -736,15 +757,18 @@ int main(int argc, char **argv)
 #endif
                        ".pushsection .test, \"a\", @progbits\n"
                        "vmovdqu_from_mem: vmovdqu (%0), %%ymm4\n"
+                       "vmovdqu_from_mem_end:\n"
                        ".popsection" :: "d" (NULL) );
 
+        instr_size = vmovdqu_from_mem_end - vmovdqu_from_mem;
         memcpy(instr, vmovdqu_from_mem, 15);
         memset(res + 4, 0xff, 16);
         regs.eip    = (unsigned long)&instr[0];
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY ||
+             (regs.eip != (unsigned long)(&instr[0] + instr_size)) )
             goto fail;
 #if 0 /* Don't use AVX2 instructions for now */
         asm ( "vpcmpeqb %%ymm2, %%ymm2, %%ymm2\n\t"
@@ -771,20 +795,24 @@ int main(int argc, char **argv)
     memset(res + 10, 0x66, 8);
     if ( stack_exec && cpu_has_sse2 )
     {
-        extern const unsigned char movsd_to_mem[];
+        extern const unsigned char movsd_to_mem[], movsd_to_mem_end[];
+        unsigned long instr_size;
 
         asm volatile ( "movlpd %0, %%xmm5\n\t"
                        "movhpd %0, %%xmm5\n"
                        ".pushsection .test, \"a\", @progbits\n"
                        "movsd_to_mem: movsd %%xmm5, (%1)\n"
+                       "movsd_to_mem_end:\n"
                        ".popsection" :: "m" (res[10]), "c" (NULL) );
 
+        instr_size = movsd_to_mem_end - movsd_to_mem;
         memcpy(instr, movsd_to_mem, 15);
         regs.eip    = (unsigned long)&instr[0];
         regs.ecx    = (unsigned long)(res + 2);
         regs.edx    = 0;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             (regs.eip != (unsigned long)(&instr[0] + instr_size)) )
             goto fail;
         printf("okay\n");
     }
@@ -797,19 +825,23 @@ int main(int argc, char **argv)
     printf("%-40s", "Testing movaps (%edx),%xmm7...");
     if ( stack_exec && cpu_has_sse )
     {
-        extern const unsigned char movaps_from_mem[];
+        extern const unsigned char movaps_from_mem[], movaps_from_mem_end[];
+        unsigned long instr_size;
 
         asm volatile ( "xorps %%xmm7, %%xmm7\n"
                        ".pushsection .test, \"a\", @progbits\n"
                        "movaps_from_mem: movaps (%0), %%xmm7\n"
+                       "movaps_from_mem_end:\n"
                        ".popsection" :: "d" (NULL) );
 
+        instr_size = movaps_from_mem_end - movaps_from_mem;
         memcpy(instr, movaps_from_mem, 15);
         regs.eip    = (unsigned long)&instr[0];
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY ||
+             (regs.eip != (unsigned long)(&instr[0] + instr_size)) )
             goto fail;
         asm ( "cmpeqps %1, %%xmm7\n\t"
               "movmskps %%xmm7, %0" : "=r" (rc) : "m" (res[8]) );
@@ -825,19 +857,23 @@ int main(int argc, char **argv)
     memset(res + 10, 0x77, 8);
     if ( stack_exec && cpu_has_avx )
     {
-        extern const unsigned char vmovsd_to_mem[];
+        extern const unsigned char vmovsd_to_mem[], vmovsd_to_mem_end[];
+        unsigned long instr_size;
 
         asm volatile ( "vbroadcastsd %0, %%ymm5\n"
                        ".pushsection .test, \"a\", @progbits\n"
                        "vmovsd_to_mem: vmovsd %%xmm5, (%1)\n"
+                       "vmovsd_to_mem_end:\n"
                        ".popsection" :: "m" (res[10]), "c" (NULL) );
 
+        instr_size = vmovsd_to_mem_end - vmovsd_to_mem;
         memcpy(instr, vmovsd_to_mem, 15);
         regs.eip    = (unsigned long)&instr[0];
         regs.ecx    = (unsigned long)(res + 2);
         regs.edx    = 0;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             (regs.eip != (unsigned long)(&instr[0] + instr_size)) )
             goto fail;
         printf("okay\n");
     }
@@ -850,19 +886,23 @@ int main(int argc, char **argv)
     printf("%-40s", "Testing vmovaps (%edx),%ymm7...");
     if ( stack_exec && cpu_has_avx )
     {
-        extern const unsigned char vmovaps_from_mem[];
+        extern const unsigned char vmovaps_from_mem[], vmovaps_from_mem_end[];
+        unsigned long instr_size;
 
         asm volatile ( "vxorps %%ymm7, %%ymm7, %%ymm7\n"
                        ".pushsection .test, \"a\", @progbits\n"
                        "vmovaps_from_mem: vmovaps (%0), %%ymm7\n"
+                       "vmovaps_from_mem_end:\n"
                        ".popsection" :: "d" (NULL) );
 
+        instr_size = vmovaps_from_mem_end - vmovaps_from_mem;
         memcpy(instr, vmovaps_from_mem, 15);
         regs.eip    = (unsigned long)&instr[0];
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY ||
+             (regs.eip != (unsigned long)(&instr[0] + instr_size)) )
             goto fail;
         asm ( "vcmpeqps %1, %%ymm7, %%ymm0\n\t"
               "vmovmskps %%ymm0, %0" : "=r" (rc) : "m" (res[8]) );
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH V3] tools/tests: Add EIP check to test_x86_emulator.c
  2014-08-07 11:38 [PATCH V3] tools/tests: Add EIP check to test_x86_emulator.c Razvan Cojocaru
@ 2014-08-07 11:50 ` Jan Beulich
  2014-08-07 15:46   ` [PATCH] test_x86_emulator: add missing EIP checks Jan Beulich
  0 siblings, 1 reply; 5+ messages in thread
From: Jan Beulich @ 2014-08-07 11:50 UTC (permalink / raw)
  To: Razvan Cojocaru
  Cc: andrew.cooper3, xen-devel, ian.jackson, ian.campbell, stefano.stabellini

>>> On 07.08.14 at 13:38, <rcojocaru@bitdefender.com> wrote:
> The test now also checks that EIP was modified after emulating
> instructions after (and including) the "movq %mm3,(%ecx)..."
> code block. This patch depends on Jan Beulich's "x86_emulate:
> properly do IP updates and other side effects on success" patch
> for the tests to succeed.
> 
> Changes since V1:
>  - Now checking if the value in EIP is correct instead of simply
>    checking that EIP has been modified.
> 
> Changes since V1:
>  - Added brackets for (unsigned long)(&instr[0] + instr_size).

Quite pointlessly, but it's fine either way. And I guess I'll do some
further work on it anyway before committing.

Jan

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH] test_x86_emulator: add missing EIP checks
  2014-08-07 11:50 ` Jan Beulich
@ 2014-08-07 15:46   ` Jan Beulich
  2014-08-07 15:52     ` Andrew Cooper
  2014-08-07 16:02     ` Jan Beulich
  0 siblings, 2 replies; 5+ messages in thread
From: Jan Beulich @ 2014-08-07 15:46 UTC (permalink / raw)
  To: xen-devel
  Cc: Keir Fraser, ian.campbell, Razvan Cojocaru, stefano.stabellini,
	andrew.cooper3, ian.jackson

[-- Attachment #1: Type: text/plain, Size: 12827 bytes --]

The MMX, SSE, and AVX instruction tests didn't validate that EIP got
properly updated by the emulator (which indeed it failed to do).

At once macroize the resepctive code quite a bit, hopefully making it
easier to add further tests when the need arises.

Signed-off-by: Jan Beulich <jbeulich@suse.com>
---
I guess I'll append this to the actual emulator fix.

--- a/tools/tests/x86_emulator/test_x86_emulator.c
+++ b/tools/tests/x86_emulator/test_x86_emulator.c
@@ -599,23 +599,32 @@ int main(int argc, char **argv)
     printf("skipped\n");
 #endif
 
+#define decl_insn(which) extern const unsigned char which[], which##_len[]
+#define put_insn(which, insn) ".pushsection .test, \"ax\", @progbits\n" \
+                              #which ": " insn "\n"                     \
+                              ".equ " #which "_len, .-" #which "\n"     \
+                              ".popsection"
+#define set_insn(which) (regs.eip = (unsigned long)memcpy(instr, which, \
+                                             (unsigned long)which##_len))
+#define check_eip(which) (regs.eip == (unsigned long)instr + \
+                                      (unsigned long)which##_len)
+
     printf("%-40s", "Testing movq %mm3,(%ecx)...");
     if ( stack_exec && cpu_has_mmx )
     {
-        extern const unsigned char movq_to_mem[];
+        decl_insn(movq_to_mem);
 
         asm volatile ( "pcmpeqb %%mm3, %%mm3\n"
-                       ".pushsection .test, \"a\", @progbits\n"
-                       "movq_to_mem: movq %%mm3, (%0)\n"
-                       ".popsection" :: "c" (NULL) );
+                       put_insn(movq_to_mem, "movq %%mm3, (%0)")
+                       :: "c" (NULL) );
 
-        memcpy(instr, movq_to_mem, 15);
+        set_insn(movq_to_mem);
         memset(res, 0x33, 64);
         memset(res + 8, 0xff, 8);
-        regs.eip    = (unsigned long)&instr[0];
         regs.ecx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             !check_eip(movq_to_mem) )
             goto fail;
         printf("okay\n");
     }
@@ -625,19 +634,17 @@ int main(int argc, char **argv)
     printf("%-40s", "Testing movq (%edx),%mm5...");
     if ( stack_exec && cpu_has_mmx )
     {
-        extern const unsigned char movq_from_mem[];
+        decl_insn(movq_from_mem);
 
         asm volatile ( "pcmpgtb %%mm5, %%mm5\n"
-                       ".pushsection .test, \"a\", @progbits\n"
-                       "movq_from_mem: movq (%0), %%mm5\n"
-                       ".popsection" :: "d" (NULL) );
+                       put_insn(movq_from_mem, "movq (%0), %%mm5")
+                       :: "d" (NULL) );
 
-        memcpy(instr, movq_from_mem, 15);
-        regs.eip    = (unsigned long)&instr[0];
+        set_insn(movq_from_mem);
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY || !check_eip(movq_from_mem) )
             goto fail;
         asm ( "pcmpeqb %%mm3, %%mm3\n\t"
               "pcmpeqb %%mm5, %%mm3\n\t"
@@ -652,20 +659,19 @@ int main(int argc, char **argv)
     printf("%-40s", "Testing movdqu %xmm2,(%ecx)...");
     if ( stack_exec && cpu_has_sse2 )
     {
-        extern const unsigned char movdqu_to_mem[];
+        decl_insn(movdqu_to_mem);
 
         asm volatile ( "pcmpeqb %%xmm2, %%xmm2\n"
-                       ".pushsection .test, \"a\", @progbits\n"
-                       "movdqu_to_mem: movdqu %%xmm2, (%0)\n"
-                       ".popsection" :: "c" (NULL) );
+                       put_insn(movdqu_to_mem, "movdqu %%xmm2, (%0)")
+                       :: "c" (NULL) );
 
-        memcpy(instr, movdqu_to_mem, 15);
+        set_insn(movdqu_to_mem);
         memset(res, 0x55, 64);
         memset(res + 8, 0xff, 16);
-        regs.eip    = (unsigned long)&instr[0];
         regs.ecx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             !check_eip(movdqu_to_mem) )
             goto fail;
         printf("okay\n");
     }
@@ -675,19 +681,17 @@ int main(int argc, char **argv)
     printf("%-40s", "Testing movdqu (%edx),%xmm4...");
     if ( stack_exec && cpu_has_sse2 )
     {
-        extern const unsigned char movdqu_from_mem[];
+        decl_insn(movdqu_from_mem);
 
         asm volatile ( "pcmpgtb %%xmm4, %%xmm4\n"
-                       ".pushsection .test, \"a\", @progbits\n"
-                       "movdqu_from_mem: movdqu (%0), %%xmm4\n"
-                       ".popsection" :: "d" (NULL) );
+                       put_insn(movdqu_from_mem, "movdqu (%0), %%xmm4")
+                       :: "d" (NULL) );
 
-        memcpy(instr, movdqu_from_mem, 15);
-        regs.eip    = (unsigned long)&instr[0];
+        set_insn(movdqu_from_mem);
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY || !check_eip(movdqu_from_mem) )
             goto fail;
         asm ( "pcmpeqb %%xmm2, %%xmm2\n\t"
               "pcmpeqb %%xmm4, %%xmm2\n\t"
@@ -702,21 +706,20 @@ int main(int argc, char **argv)
     printf("%-40s", "Testing vmovdqu %ymm2,(%ecx)...");
     if ( stack_exec && cpu_has_avx )
     {
-        extern const unsigned char vmovdqu_to_mem[];
+        decl_insn(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) );
+                       put_insn(vmovdqu_to_mem, "vmovdqu %%ymm2, (%0)")
+                       :: "c" (NULL) );
 
-        memcpy(instr, vmovdqu_to_mem, 15);
+        set_insn(vmovdqu_to_mem);
         memset(res, 0x55, 128);
         memset(res + 16, 0xff, 16);
         memset(res + 20, 0x00, 16);
-        regs.eip    = (unsigned long)&instr[0];
         regs.ecx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 16, 64) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 16, 64) ||
+             !check_eip(vmovdqu_to_mem) )
             goto fail;
         printf("okay\n");
     }
@@ -726,7 +729,7 @@ int main(int argc, char **argv)
     printf("%-40s", "Testing vmovdqu (%edx),%ymm4...");
     if ( stack_exec && cpu_has_avx )
     {
-        extern const unsigned char vmovdqu_from_mem[];
+        decl_insn(vmovdqu_from_mem);
 
 #if 0 /* Don't use AVX2 instructions for now */
         asm volatile ( "vpcmpgtb %%ymm4, %%ymm4, %%ymm4\n"
@@ -734,17 +737,15 @@ int main(int argc, char **argv)
         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) );
+                       put_insn(vmovdqu_from_mem, "vmovdqu (%0), %%ymm4")
+                       :: "d" (NULL) );
 
-        memcpy(instr, vmovdqu_from_mem, 15);
+        set_insn(vmovdqu_from_mem);
         memset(res + 4, 0xff, 16);
-        regs.eip    = (unsigned long)&instr[0];
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY || !check_eip(vmovdqu_from_mem) )
             goto fail;
 #if 0 /* Don't use AVX2 instructions for now */
         asm ( "vpcmpeqb %%ymm2, %%ymm2, %%ymm2\n\t"
@@ -771,20 +772,19 @@ int main(int argc, char **argv)
     memset(res + 10, 0x66, 8);
     if ( stack_exec && cpu_has_sse2 )
     {
-        extern const unsigned char movsd_to_mem[];
+        decl_insn(movsd_to_mem);
 
         asm volatile ( "movlpd %0, %%xmm5\n\t"
                        "movhpd %0, %%xmm5\n"
-                       ".pushsection .test, \"a\", @progbits\n"
-                       "movsd_to_mem: movsd %%xmm5, (%1)\n"
-                       ".popsection" :: "m" (res[10]), "c" (NULL) );
+                       put_insn(movsd_to_mem, "movsd %%xmm5, (%1)")
+                       :: "m" (res[10]), "c" (NULL) );
 
-        memcpy(instr, movsd_to_mem, 15);
-        regs.eip    = (unsigned long)&instr[0];
+        set_insn(movsd_to_mem);
         regs.ecx    = (unsigned long)(res + 2);
         regs.edx    = 0;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             !check_eip(movsd_to_mem) )
             goto fail;
         printf("okay\n");
     }
@@ -797,19 +797,17 @@ int main(int argc, char **argv)
     printf("%-40s", "Testing movaps (%edx),%xmm7...");
     if ( stack_exec && cpu_has_sse )
     {
-        extern const unsigned char movaps_from_mem[];
+        decl_insn(movaps_from_mem);
 
         asm volatile ( "xorps %%xmm7, %%xmm7\n"
-                       ".pushsection .test, \"a\", @progbits\n"
-                       "movaps_from_mem: movaps (%0), %%xmm7\n"
-                       ".popsection" :: "d" (NULL) );
+                       put_insn(movaps_from_mem, "movaps (%0), %%xmm7")
+                       :: "d" (NULL) );
 
-        memcpy(instr, movaps_from_mem, 15);
-        regs.eip    = (unsigned long)&instr[0];
+        set_insn(movaps_from_mem);
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY || !check_eip(movaps_from_mem) )
             goto fail;
         asm ( "cmpeqps %1, %%xmm7\n\t"
               "movmskps %%xmm7, %0" : "=r" (rc) : "m" (res[8]) );
@@ -825,19 +823,18 @@ int main(int argc, char **argv)
     memset(res + 10, 0x77, 8);
     if ( stack_exec && cpu_has_avx )
     {
-        extern const unsigned char vmovsd_to_mem[];
+        decl_insn(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) );
+                       put_insn(vmovsd_to_mem, "vmovsd %%xmm5, (%1)")
+                       :: "m" (res[10]), "c" (NULL) );
 
-        memcpy(instr, vmovsd_to_mem, 15);
-        regs.eip    = (unsigned long)&instr[0];
+        set_insn(vmovsd_to_mem);
         regs.ecx    = (unsigned long)(res + 2);
         regs.edx    = 0;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             !check_eip(vmovsd_to_mem) )
             goto fail;
         printf("okay\n");
     }
@@ -850,19 +847,17 @@ int main(int argc, char **argv)
     printf("%-40s", "Testing vmovaps (%edx),%ymm7...");
     if ( stack_exec && cpu_has_avx )
     {
-        extern const unsigned char vmovaps_from_mem[];
+        decl_insn(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) );
+                       put_insn(vmovaps_from_mem, "vmovaps (%0), %%ymm7")
+                       :: "d" (NULL) );
 
-        memcpy(instr, vmovaps_from_mem, 15);
-        regs.eip    = (unsigned long)&instr[0];
+        set_insn(vmovaps_from_mem);
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY || !check_eip(vmovaps_from_mem) )
             goto fail;
         asm ( "vcmpeqps %1, %%ymm7, %%ymm0\n\t"
               "vmovmskps %%ymm0, %0" : "=r" (rc) : "m" (res[8]) );
@@ -873,6 +868,11 @@ int main(int argc, char **argv)
     else
         printf("skipped\n");
 
+#undef decl_insn
+#undef put_insn
+#undef set_insn
+#undef check_eip
+
     for ( j = 1; j <= 2; j++ )
     {
 #if defined(__i386__)



[-- Attachment #2: x86emul-test-IP-updates.patch --]
[-- Type: text/plain, Size: 12868 bytes --]

test_x86_emulator: add missing EIP checks

The MMX, SSE, and AVX instruction tests didn't validate that EIP got
properly updated by the emulator (which indeed it failed to do).

At once macroize the resepctive code quite a bit, hopefully making it
easier to add further tests when the need arises.

Signed-off-by: Jan Beulich <jbeulich@suse.com>
---
I guess I'll append this to the actual emulator fix.

--- a/tools/tests/x86_emulator/test_x86_emulator.c
+++ b/tools/tests/x86_emulator/test_x86_emulator.c
@@ -599,23 +599,32 @@ int main(int argc, char **argv)
     printf("skipped\n");
 #endif
 
+#define decl_insn(which) extern const unsigned char which[], which##_len[]
+#define put_insn(which, insn) ".pushsection .test, \"ax\", @progbits\n" \
+                              #which ": " insn "\n"                     \
+                              ".equ " #which "_len, .-" #which "\n"     \
+                              ".popsection"
+#define set_insn(which) (regs.eip = (unsigned long)memcpy(instr, which, \
+                                             (unsigned long)which##_len))
+#define check_eip(which) (regs.eip == (unsigned long)instr + \
+                                      (unsigned long)which##_len)
+
     printf("%-40s", "Testing movq %mm3,(%ecx)...");
     if ( stack_exec && cpu_has_mmx )
     {
-        extern const unsigned char movq_to_mem[];
+        decl_insn(movq_to_mem);
 
         asm volatile ( "pcmpeqb %%mm3, %%mm3\n"
-                       ".pushsection .test, \"a\", @progbits\n"
-                       "movq_to_mem: movq %%mm3, (%0)\n"
-                       ".popsection" :: "c" (NULL) );
+                       put_insn(movq_to_mem, "movq %%mm3, (%0)")
+                       :: "c" (NULL) );
 
-        memcpy(instr, movq_to_mem, 15);
+        set_insn(movq_to_mem);
         memset(res, 0x33, 64);
         memset(res + 8, 0xff, 8);
-        regs.eip    = (unsigned long)&instr[0];
         regs.ecx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             !check_eip(movq_to_mem) )
             goto fail;
         printf("okay\n");
     }
@@ -625,19 +634,17 @@ int main(int argc, char **argv)
     printf("%-40s", "Testing movq (%edx),%mm5...");
     if ( stack_exec && cpu_has_mmx )
     {
-        extern const unsigned char movq_from_mem[];
+        decl_insn(movq_from_mem);
 
         asm volatile ( "pcmpgtb %%mm5, %%mm5\n"
-                       ".pushsection .test, \"a\", @progbits\n"
-                       "movq_from_mem: movq (%0), %%mm5\n"
-                       ".popsection" :: "d" (NULL) );
+                       put_insn(movq_from_mem, "movq (%0), %%mm5")
+                       :: "d" (NULL) );
 
-        memcpy(instr, movq_from_mem, 15);
-        regs.eip    = (unsigned long)&instr[0];
+        set_insn(movq_from_mem);
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY || !check_eip(movq_from_mem) )
             goto fail;
         asm ( "pcmpeqb %%mm3, %%mm3\n\t"
               "pcmpeqb %%mm5, %%mm3\n\t"
@@ -652,20 +659,19 @@ int main(int argc, char **argv)
     printf("%-40s", "Testing movdqu %xmm2,(%ecx)...");
     if ( stack_exec && cpu_has_sse2 )
     {
-        extern const unsigned char movdqu_to_mem[];
+        decl_insn(movdqu_to_mem);
 
         asm volatile ( "pcmpeqb %%xmm2, %%xmm2\n"
-                       ".pushsection .test, \"a\", @progbits\n"
-                       "movdqu_to_mem: movdqu %%xmm2, (%0)\n"
-                       ".popsection" :: "c" (NULL) );
+                       put_insn(movdqu_to_mem, "movdqu %%xmm2, (%0)")
+                       :: "c" (NULL) );
 
-        memcpy(instr, movdqu_to_mem, 15);
+        set_insn(movdqu_to_mem);
         memset(res, 0x55, 64);
         memset(res + 8, 0xff, 16);
-        regs.eip    = (unsigned long)&instr[0];
         regs.ecx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             !check_eip(movdqu_to_mem) )
             goto fail;
         printf("okay\n");
     }
@@ -675,19 +681,17 @@ int main(int argc, char **argv)
     printf("%-40s", "Testing movdqu (%edx),%xmm4...");
     if ( stack_exec && cpu_has_sse2 )
     {
-        extern const unsigned char movdqu_from_mem[];
+        decl_insn(movdqu_from_mem);
 
         asm volatile ( "pcmpgtb %%xmm4, %%xmm4\n"
-                       ".pushsection .test, \"a\", @progbits\n"
-                       "movdqu_from_mem: movdqu (%0), %%xmm4\n"
-                       ".popsection" :: "d" (NULL) );
+                       put_insn(movdqu_from_mem, "movdqu (%0), %%xmm4")
+                       :: "d" (NULL) );
 
-        memcpy(instr, movdqu_from_mem, 15);
-        regs.eip    = (unsigned long)&instr[0];
+        set_insn(movdqu_from_mem);
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY || !check_eip(movdqu_from_mem) )
             goto fail;
         asm ( "pcmpeqb %%xmm2, %%xmm2\n\t"
               "pcmpeqb %%xmm4, %%xmm2\n\t"
@@ -702,21 +706,20 @@ int main(int argc, char **argv)
     printf("%-40s", "Testing vmovdqu %ymm2,(%ecx)...");
     if ( stack_exec && cpu_has_avx )
     {
-        extern const unsigned char vmovdqu_to_mem[];
+        decl_insn(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) );
+                       put_insn(vmovdqu_to_mem, "vmovdqu %%ymm2, (%0)")
+                       :: "c" (NULL) );
 
-        memcpy(instr, vmovdqu_to_mem, 15);
+        set_insn(vmovdqu_to_mem);
         memset(res, 0x55, 128);
         memset(res + 16, 0xff, 16);
         memset(res + 20, 0x00, 16);
-        regs.eip    = (unsigned long)&instr[0];
         regs.ecx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 16, 64) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 16, 64) ||
+             !check_eip(vmovdqu_to_mem) )
             goto fail;
         printf("okay\n");
     }
@@ -726,7 +729,7 @@ int main(int argc, char **argv)
     printf("%-40s", "Testing vmovdqu (%edx),%ymm4...");
     if ( stack_exec && cpu_has_avx )
     {
-        extern const unsigned char vmovdqu_from_mem[];
+        decl_insn(vmovdqu_from_mem);
 
 #if 0 /* Don't use AVX2 instructions for now */
         asm volatile ( "vpcmpgtb %%ymm4, %%ymm4, %%ymm4\n"
@@ -734,17 +737,15 @@ int main(int argc, char **argv)
         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) );
+                       put_insn(vmovdqu_from_mem, "vmovdqu (%0), %%ymm4")
+                       :: "d" (NULL) );
 
-        memcpy(instr, vmovdqu_from_mem, 15);
+        set_insn(vmovdqu_from_mem);
         memset(res + 4, 0xff, 16);
-        regs.eip    = (unsigned long)&instr[0];
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY || !check_eip(vmovdqu_from_mem) )
             goto fail;
 #if 0 /* Don't use AVX2 instructions for now */
         asm ( "vpcmpeqb %%ymm2, %%ymm2, %%ymm2\n\t"
@@ -771,20 +772,19 @@ int main(int argc, char **argv)
     memset(res + 10, 0x66, 8);
     if ( stack_exec && cpu_has_sse2 )
     {
-        extern const unsigned char movsd_to_mem[];
+        decl_insn(movsd_to_mem);
 
         asm volatile ( "movlpd %0, %%xmm5\n\t"
                        "movhpd %0, %%xmm5\n"
-                       ".pushsection .test, \"a\", @progbits\n"
-                       "movsd_to_mem: movsd %%xmm5, (%1)\n"
-                       ".popsection" :: "m" (res[10]), "c" (NULL) );
+                       put_insn(movsd_to_mem, "movsd %%xmm5, (%1)")
+                       :: "m" (res[10]), "c" (NULL) );
 
-        memcpy(instr, movsd_to_mem, 15);
-        regs.eip    = (unsigned long)&instr[0];
+        set_insn(movsd_to_mem);
         regs.ecx    = (unsigned long)(res + 2);
         regs.edx    = 0;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             !check_eip(movsd_to_mem) )
             goto fail;
         printf("okay\n");
     }
@@ -797,19 +797,17 @@ int main(int argc, char **argv)
     printf("%-40s", "Testing movaps (%edx),%xmm7...");
     if ( stack_exec && cpu_has_sse )
     {
-        extern const unsigned char movaps_from_mem[];
+        decl_insn(movaps_from_mem);
 
         asm volatile ( "xorps %%xmm7, %%xmm7\n"
-                       ".pushsection .test, \"a\", @progbits\n"
-                       "movaps_from_mem: movaps (%0), %%xmm7\n"
-                       ".popsection" :: "d" (NULL) );
+                       put_insn(movaps_from_mem, "movaps (%0), %%xmm7")
+                       :: "d" (NULL) );
 
-        memcpy(instr, movaps_from_mem, 15);
-        regs.eip    = (unsigned long)&instr[0];
+        set_insn(movaps_from_mem);
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY || !check_eip(movaps_from_mem) )
             goto fail;
         asm ( "cmpeqps %1, %%xmm7\n\t"
               "movmskps %%xmm7, %0" : "=r" (rc) : "m" (res[8]) );
@@ -825,19 +823,18 @@ int main(int argc, char **argv)
     memset(res + 10, 0x77, 8);
     if ( stack_exec && cpu_has_avx )
     {
-        extern const unsigned char vmovsd_to_mem[];
+        decl_insn(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) );
+                       put_insn(vmovsd_to_mem, "vmovsd %%xmm5, (%1)")
+                       :: "m" (res[10]), "c" (NULL) );
 
-        memcpy(instr, vmovsd_to_mem, 15);
-        regs.eip    = (unsigned long)&instr[0];
+        set_insn(vmovsd_to_mem);
         regs.ecx    = (unsigned long)(res + 2);
         regs.edx    = 0;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
+        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
+             !check_eip(vmovsd_to_mem) )
             goto fail;
         printf("okay\n");
     }
@@ -850,19 +847,17 @@ int main(int argc, char **argv)
     printf("%-40s", "Testing vmovaps (%edx),%ymm7...");
     if ( stack_exec && cpu_has_avx )
     {
-        extern const unsigned char vmovaps_from_mem[];
+        decl_insn(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) );
+                       put_insn(vmovaps_from_mem, "vmovaps (%0), %%ymm7")
+                       :: "d" (NULL) );
 
-        memcpy(instr, vmovaps_from_mem, 15);
-        regs.eip    = (unsigned long)&instr[0];
+        set_insn(vmovaps_from_mem);
         regs.ecx    = 0;
         regs.edx    = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
-        if ( rc != X86EMUL_OKAY )
+        if ( rc != X86EMUL_OKAY || !check_eip(vmovaps_from_mem) )
             goto fail;
         asm ( "vcmpeqps %1, %%ymm7, %%ymm0\n\t"
               "vmovmskps %%ymm0, %0" : "=r" (rc) : "m" (res[8]) );
@@ -873,6 +868,11 @@ int main(int argc, char **argv)
     else
         printf("skipped\n");
 
+#undef decl_insn
+#undef put_insn
+#undef set_insn
+#undef check_eip
+
     for ( j = 1; j <= 2; j++ )
     {
 #if defined(__i386__)

[-- Attachment #3: Type: text/plain, Size: 126 bytes --]

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] test_x86_emulator: add missing EIP checks
  2014-08-07 15:46   ` [PATCH] test_x86_emulator: add missing EIP checks Jan Beulich
@ 2014-08-07 15:52     ` Andrew Cooper
  2014-08-07 16:02     ` Jan Beulich
  1 sibling, 0 replies; 5+ messages in thread
From: Andrew Cooper @ 2014-08-07 15:52 UTC (permalink / raw)
  To: Jan Beulich, xen-devel
  Cc: ian.jackson, Keir Fraser, ian.campbell, Razvan Cojocaru,
	stefano.stabellini


[-- Attachment #1.1: Type: text/plain, Size: 13395 bytes --]

On 07/08/14 16:46, Jan Beulich wrote:
> The MMX, SSE, and AVX instruction tests didn't validate that EIP got
> properly updated by the emulator (which indeed it failed to do).
>
> At once macroize the resepctive code quite a bit, hopefully making it
> easier to add further tests when the need arises.
>
> Signed-off-by: Jan Beulich <jbeulich@suse.com>

This is much neater than before.

Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>

> ---
> I guess I'll append this to the actual emulator fix.
>
> --- a/tools/tests/x86_emulator/test_x86_emulator.c
> +++ b/tools/tests/x86_emulator/test_x86_emulator.c
> @@ -599,23 +599,32 @@ int main(int argc, char **argv)
>      printf("skipped\n");
>  #endif
>  
> +#define decl_insn(which) extern const unsigned char which[], which##_len[]
> +#define put_insn(which, insn) ".pushsection .test, \"ax\", @progbits\n" \
> +                              #which ": " insn "\n"                     \
> +                              ".equ " #which "_len, .-" #which "\n"     \
> +                              ".popsection"
> +#define set_insn(which) (regs.eip = (unsigned long)memcpy(instr, which, \
> +                                             (unsigned long)which##_len))
> +#define check_eip(which) (regs.eip == (unsigned long)instr + \
> +                                      (unsigned long)which##_len)
> +
>      printf("%-40s", "Testing movq %mm3,(%ecx)...");
>      if ( stack_exec && cpu_has_mmx )
>      {
> -        extern const unsigned char movq_to_mem[];
> +        decl_insn(movq_to_mem);
>  
>          asm volatile ( "pcmpeqb %%mm3, %%mm3\n"
> -                       ".pushsection .test, \"a\", @progbits\n"
> -                       "movq_to_mem: movq %%mm3, (%0)\n"
> -                       ".popsection" :: "c" (NULL) );
> +                       put_insn(movq_to_mem, "movq %%mm3, (%0)")
> +                       :: "c" (NULL) );
>  
> -        memcpy(instr, movq_to_mem, 15);
> +        set_insn(movq_to_mem);
>          memset(res, 0x33, 64);
>          memset(res + 8, 0xff, 8);
> -        regs.eip    = (unsigned long)&instr[0];
>          regs.ecx    = (unsigned long)res;
>          rc = x86_emulate(&ctxt, &emulops);
> -        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
> +        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
> +             !check_eip(movq_to_mem) )
>              goto fail;
>          printf("okay\n");
>      }
> @@ -625,19 +634,17 @@ int main(int argc, char **argv)
>      printf("%-40s", "Testing movq (%edx),%mm5...");
>      if ( stack_exec && cpu_has_mmx )
>      {
> -        extern const unsigned char movq_from_mem[];
> +        decl_insn(movq_from_mem);
>  
>          asm volatile ( "pcmpgtb %%mm5, %%mm5\n"
> -                       ".pushsection .test, \"a\", @progbits\n"
> -                       "movq_from_mem: movq (%0), %%mm5\n"
> -                       ".popsection" :: "d" (NULL) );
> +                       put_insn(movq_from_mem, "movq (%0), %%mm5")
> +                       :: "d" (NULL) );
>  
> -        memcpy(instr, movq_from_mem, 15);
> -        regs.eip    = (unsigned long)&instr[0];
> +        set_insn(movq_from_mem);
>          regs.ecx    = 0;
>          regs.edx    = (unsigned long)res;
>          rc = x86_emulate(&ctxt, &emulops);
> -        if ( rc != X86EMUL_OKAY )
> +        if ( rc != X86EMUL_OKAY || !check_eip(movq_from_mem) )
>              goto fail;
>          asm ( "pcmpeqb %%mm3, %%mm3\n\t"
>                "pcmpeqb %%mm5, %%mm3\n\t"
> @@ -652,20 +659,19 @@ int main(int argc, char **argv)
>      printf("%-40s", "Testing movdqu %xmm2,(%ecx)...");
>      if ( stack_exec && cpu_has_sse2 )
>      {
> -        extern const unsigned char movdqu_to_mem[];
> +        decl_insn(movdqu_to_mem);
>  
>          asm volatile ( "pcmpeqb %%xmm2, %%xmm2\n"
> -                       ".pushsection .test, \"a\", @progbits\n"
> -                       "movdqu_to_mem: movdqu %%xmm2, (%0)\n"
> -                       ".popsection" :: "c" (NULL) );
> +                       put_insn(movdqu_to_mem, "movdqu %%xmm2, (%0)")
> +                       :: "c" (NULL) );
>  
> -        memcpy(instr, movdqu_to_mem, 15);
> +        set_insn(movdqu_to_mem);
>          memset(res, 0x55, 64);
>          memset(res + 8, 0xff, 16);
> -        regs.eip    = (unsigned long)&instr[0];
>          regs.ecx    = (unsigned long)res;
>          rc = x86_emulate(&ctxt, &emulops);
> -        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
> +        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
> +             !check_eip(movdqu_to_mem) )
>              goto fail;
>          printf("okay\n");
>      }
> @@ -675,19 +681,17 @@ int main(int argc, char **argv)
>      printf("%-40s", "Testing movdqu (%edx),%xmm4...");
>      if ( stack_exec && cpu_has_sse2 )
>      {
> -        extern const unsigned char movdqu_from_mem[];
> +        decl_insn(movdqu_from_mem);
>  
>          asm volatile ( "pcmpgtb %%xmm4, %%xmm4\n"
> -                       ".pushsection .test, \"a\", @progbits\n"
> -                       "movdqu_from_mem: movdqu (%0), %%xmm4\n"
> -                       ".popsection" :: "d" (NULL) );
> +                       put_insn(movdqu_from_mem, "movdqu (%0), %%xmm4")
> +                       :: "d" (NULL) );
>  
> -        memcpy(instr, movdqu_from_mem, 15);
> -        regs.eip    = (unsigned long)&instr[0];
> +        set_insn(movdqu_from_mem);
>          regs.ecx    = 0;
>          regs.edx    = (unsigned long)res;
>          rc = x86_emulate(&ctxt, &emulops);
> -        if ( rc != X86EMUL_OKAY )
> +        if ( rc != X86EMUL_OKAY || !check_eip(movdqu_from_mem) )
>              goto fail;
>          asm ( "pcmpeqb %%xmm2, %%xmm2\n\t"
>                "pcmpeqb %%xmm4, %%xmm2\n\t"
> @@ -702,21 +706,20 @@ int main(int argc, char **argv)
>      printf("%-40s", "Testing vmovdqu %ymm2,(%ecx)...");
>      if ( stack_exec && cpu_has_avx )
>      {
> -        extern const unsigned char vmovdqu_to_mem[];
> +        decl_insn(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) );
> +                       put_insn(vmovdqu_to_mem, "vmovdqu %%ymm2, (%0)")
> +                       :: "c" (NULL) );
>  
> -        memcpy(instr, vmovdqu_to_mem, 15);
> +        set_insn(vmovdqu_to_mem);
>          memset(res, 0x55, 128);
>          memset(res + 16, 0xff, 16);
>          memset(res + 20, 0x00, 16);
> -        regs.eip    = (unsigned long)&instr[0];
>          regs.ecx    = (unsigned long)res;
>          rc = x86_emulate(&ctxt, &emulops);
> -        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 16, 64) )
> +        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 16, 64) ||
> +             !check_eip(vmovdqu_to_mem) )
>              goto fail;
>          printf("okay\n");
>      }
> @@ -726,7 +729,7 @@ int main(int argc, char **argv)
>      printf("%-40s", "Testing vmovdqu (%edx),%ymm4...");
>      if ( stack_exec && cpu_has_avx )
>      {
> -        extern const unsigned char vmovdqu_from_mem[];
> +        decl_insn(vmovdqu_from_mem);
>  
>  #if 0 /* Don't use AVX2 instructions for now */
>          asm volatile ( "vpcmpgtb %%ymm4, %%ymm4, %%ymm4\n"
> @@ -734,17 +737,15 @@ int main(int argc, char **argv)
>          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) );
> +                       put_insn(vmovdqu_from_mem, "vmovdqu (%0), %%ymm4")
> +                       :: "d" (NULL) );
>  
> -        memcpy(instr, vmovdqu_from_mem, 15);
> +        set_insn(vmovdqu_from_mem);
>          memset(res + 4, 0xff, 16);
> -        regs.eip    = (unsigned long)&instr[0];
>          regs.ecx    = 0;
>          regs.edx    = (unsigned long)res;
>          rc = x86_emulate(&ctxt, &emulops);
> -        if ( rc != X86EMUL_OKAY )
> +        if ( rc != X86EMUL_OKAY || !check_eip(vmovdqu_from_mem) )
>              goto fail;
>  #if 0 /* Don't use AVX2 instructions for now */
>          asm ( "vpcmpeqb %%ymm2, %%ymm2, %%ymm2\n\t"
> @@ -771,20 +772,19 @@ int main(int argc, char **argv)
>      memset(res + 10, 0x66, 8);
>      if ( stack_exec && cpu_has_sse2 )
>      {
> -        extern const unsigned char movsd_to_mem[];
> +        decl_insn(movsd_to_mem);
>  
>          asm volatile ( "movlpd %0, %%xmm5\n\t"
>                         "movhpd %0, %%xmm5\n"
> -                       ".pushsection .test, \"a\", @progbits\n"
> -                       "movsd_to_mem: movsd %%xmm5, (%1)\n"
> -                       ".popsection" :: "m" (res[10]), "c" (NULL) );
> +                       put_insn(movsd_to_mem, "movsd %%xmm5, (%1)")
> +                       :: "m" (res[10]), "c" (NULL) );
>  
> -        memcpy(instr, movsd_to_mem, 15);
> -        regs.eip    = (unsigned long)&instr[0];
> +        set_insn(movsd_to_mem);
>          regs.ecx    = (unsigned long)(res + 2);
>          regs.edx    = 0;
>          rc = x86_emulate(&ctxt, &emulops);
> -        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
> +        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
> +             !check_eip(movsd_to_mem) )
>              goto fail;
>          printf("okay\n");
>      }
> @@ -797,19 +797,17 @@ int main(int argc, char **argv)
>      printf("%-40s", "Testing movaps (%edx),%xmm7...");
>      if ( stack_exec && cpu_has_sse )
>      {
> -        extern const unsigned char movaps_from_mem[];
> +        decl_insn(movaps_from_mem);
>  
>          asm volatile ( "xorps %%xmm7, %%xmm7\n"
> -                       ".pushsection .test, \"a\", @progbits\n"
> -                       "movaps_from_mem: movaps (%0), %%xmm7\n"
> -                       ".popsection" :: "d" (NULL) );
> +                       put_insn(movaps_from_mem, "movaps (%0), %%xmm7")
> +                       :: "d" (NULL) );
>  
> -        memcpy(instr, movaps_from_mem, 15);
> -        regs.eip    = (unsigned long)&instr[0];
> +        set_insn(movaps_from_mem);
>          regs.ecx    = 0;
>          regs.edx    = (unsigned long)res;
>          rc = x86_emulate(&ctxt, &emulops);
> -        if ( rc != X86EMUL_OKAY )
> +        if ( rc != X86EMUL_OKAY || !check_eip(movaps_from_mem) )
>              goto fail;
>          asm ( "cmpeqps %1, %%xmm7\n\t"
>                "movmskps %%xmm7, %0" : "=r" (rc) : "m" (res[8]) );
> @@ -825,19 +823,18 @@ int main(int argc, char **argv)
>      memset(res + 10, 0x77, 8);
>      if ( stack_exec && cpu_has_avx )
>      {
> -        extern const unsigned char vmovsd_to_mem[];
> +        decl_insn(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) );
> +                       put_insn(vmovsd_to_mem, "vmovsd %%xmm5, (%1)")
> +                       :: "m" (res[10]), "c" (NULL) );
>  
> -        memcpy(instr, vmovsd_to_mem, 15);
> -        regs.eip    = (unsigned long)&instr[0];
> +        set_insn(vmovsd_to_mem);
>          regs.ecx    = (unsigned long)(res + 2);
>          regs.edx    = 0;
>          rc = x86_emulate(&ctxt, &emulops);
> -        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )
> +        if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) ||
> +             !check_eip(vmovsd_to_mem) )
>              goto fail;
>          printf("okay\n");
>      }
> @@ -850,19 +847,17 @@ int main(int argc, char **argv)
>      printf("%-40s", "Testing vmovaps (%edx),%ymm7...");
>      if ( stack_exec && cpu_has_avx )
>      {
> -        extern const unsigned char vmovaps_from_mem[];
> +        decl_insn(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) );
> +                       put_insn(vmovaps_from_mem, "vmovaps (%0), %%ymm7")
> +                       :: "d" (NULL) );
>  
> -        memcpy(instr, vmovaps_from_mem, 15);
> -        regs.eip    = (unsigned long)&instr[0];
> +        set_insn(vmovaps_from_mem);
>          regs.ecx    = 0;
>          regs.edx    = (unsigned long)res;
>          rc = x86_emulate(&ctxt, &emulops);
> -        if ( rc != X86EMUL_OKAY )
> +        if ( rc != X86EMUL_OKAY || !check_eip(vmovaps_from_mem) )
>              goto fail;
>          asm ( "vcmpeqps %1, %%ymm7, %%ymm0\n\t"
>                "vmovmskps %%ymm0, %0" : "=r" (rc) : "m" (res[8]) );
> @@ -873,6 +868,11 @@ int main(int argc, char **argv)
>      else
>          printf("skipped\n");
>  
> +#undef decl_insn
> +#undef put_insn
> +#undef set_insn
> +#undef check_eip
> +
>      for ( j = 1; j <= 2; j++ )
>      {
>  #if defined(__i386__)
>
>
>
>
> _______________________________________________
> Xen-devel mailing list
> Xen-devel@lists.xen.org
> http://lists.xen.org/xen-devel


[-- Attachment #1.2: Type: text/html, Size: 13934 bytes --]

[-- Attachment #2: Type: text/plain, Size: 126 bytes --]

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] test_x86_emulator: add missing EIP checks
  2014-08-07 15:46   ` [PATCH] test_x86_emulator: add missing EIP checks Jan Beulich
  2014-08-07 15:52     ` Andrew Cooper
@ 2014-08-07 16:02     ` Jan Beulich
  1 sibling, 0 replies; 5+ messages in thread
From: Jan Beulich @ 2014-08-07 16:02 UTC (permalink / raw)
  To: ian.campbell, ian.jackson, stefano.stabellini; +Cc: xen-devel, Keir Fraser

>>> On 07.08.14 at 17:46, <JBeulich@suse.com> wrote:
> The MMX, SSE, and AVX instruction tests didn't validate that EIP got
> properly updated by the emulator (which indeed it failed to do).
> 
> At once macroize the resepctive code quite a bit, hopefully making it
> easier to add further tests when the need arises.
> 
> Signed-off-by: Jan Beulich <jbeulich@suse.com>
> ---
> I guess I'll append this to the actual emulator fix.

Btw, tools maintainers: Would you mind me naming myself the
maintainer of tools/tests/x86_emulator/?

Jan

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-08-07 16:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-07 11:38 [PATCH V3] tools/tests: Add EIP check to test_x86_emulator.c Razvan Cojocaru
2014-08-07 11:50 ` Jan Beulich
2014-08-07 15:46   ` [PATCH] test_x86_emulator: add missing EIP checks Jan Beulich
2014-08-07 15:52     ` Andrew Cooper
2014-08-07 16:02     ` Jan Beulich

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.