Skip to contentSkip to conversation

x86/emulate: add support for {, v}movq xmm, xmm/m64

PATCH Superseded Mihai Donțu 2 participants 7 messages Jul 18, 2016 – Jul 19, 2016

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 x86/emulate: add support for {, v}movq xmm, xmm/m64

---
Changed since v1:
* added a test for vmovq
* made the tests depend on SSE and AVX, respectively
* added emulator support for vmovq (0xd6 forces the operand size to 64bit)

---
 tools/tests/x86_emulator/test_x86_emulator.c | 44 ++++++++++++++++++++++++++++
 xen/arch/x86/x86_emulate/x86_emulate.c       |  9 +++---
 2 files changed, 49 insertions(+), 4 deletions(-)
tools/tests/x86_emulator/test_x86_emulator.c +44 -0
index c7f572a..8994149 100644
@@ -697,6 +697,50 @@ int main(int argc, char **argv)
     else
         printf("skipped\n");
 
+    printf("%-40s", "Testing movq %%xmm0,32(%%eax)...");
+    if ( stack_exec && cpu_has_sse )
+    {
+        decl_insn(movq_to_mem2);
+
+        asm volatile ( "pcmpgtb %%xmm0, %%xmm0\n"
+                       put_insn(movq_to_mem2, "movq %%xmm0, 32(%%eax)")
+                       :: );
+
+        *((unsigned long *)res + 4) = 0xbdbdbdbdbdbdbdbd;
+        set_insn(movq_to_mem2);
+        regs.eax = (unsigned long)res;
+        rc = x86_emulate(&ctxt, &emulops);
+        if ( rc != X86EMUL_OKAY || !check_eip(movq_to_mem2) )
+            goto fail;
+        if ( *((unsigned long *)res + 4) )
+            goto fail;
+        printf("okay\n");
+    }
+    else
+        printf("skipped\n");
+
+    printf("%-40s", "Testing vmovq %%xmm1,32(%%eax)...");
+    if ( stack_exec && cpu_has_avx )
+    {
+        decl_insn(vmovq_to_mem);
+
+        asm volatile ( "pcmpgtb %%xmm1, %%xmm1\n"
+                       put_insn(vmovq_to_mem, "vmovq %%xmm1, 32(%%eax)")
+                       :: );
+
+        *((unsigned long *)res + 4) = 0xbdbdbdbdbdbdbdbd;
+        set_insn(vmovq_to_mem);
+        regs.eax = (unsigned long)res;
+        rc = x86_emulate(&ctxt, &emulops);
+        if ( rc != X86EMUL_OKAY || !check_eip(vmovq_to_mem) )
+            goto fail;
+        if ( *((unsigned long *)res + 4) )
+            goto fail;
+        printf("okay\n");
+    }
+    else
+        printf("skipped\n");
+
     printf("%-40s", "Testing movdqu %xmm2,(%ecx)...");
     if ( stack_exec && cpu_has_sse2 )
     {
xen/arch/x86/x86_emulate/x86_emulate.c +5 -4
index fe594ba..0301235 100644
@@ -245,7 +245,7 @@ static uint8_t twobyte_table[256] = {
     ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
     ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
     /* 0xD0 - 0xDF */
-    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+    0, 0, 0, 0, 0, 0, ImplicitOps|ModRM, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     /* 0xE0 - 0xEF */
     0, 0, 0, 0, 0, 0, 0, ImplicitOps|ModRM, 0, 0, 0, 0, 0, 0, 0, 0,
     /* 0xF0 - 0xFF */
@@ -4412,6 +4412,7 @@ x86_emulate(
     case 0x7f: /* movq mm,mm/m64 */
                /* {,v}movdq{a,u} xmm,xmm/m128 */
                /* vmovdq{a,u} ymm,ymm/m256 */
+    case 0xd6: /* {,v}movq xmm,xmm/m64 */
     {
         uint8_t *buf = get_stub(stub);
         struct fpu_insn_ctxt fic = { .insn_bytes = 5 };
@@ -4429,9 +4430,9 @@ x86_emulate(
             case vex_66:
             case vex_f3:
                 host_and_vcpu_must_have(sse2);
-                buf[0] = 0x66; /* movdqa */
+                buf[0] = 0x66; /* SSE */
                 get_fpu(X86EMUL_FPU_xmm, &fic);
-                ea.bytes = 16;
+                ea.bytes = (b == 0xd6 ? 8 : 16);
                 break;
             case vex_none:
                 if ( b != 0xe7 )
@@ -4451,7 +4452,7 @@ x86_emulate(
                     ((vex.pfx != vex_66) && (vex.pfx != vex_f3)));
             host_and_vcpu_must_have(avx);
             get_fpu(X86EMUL_FPU_ymm, &fic);
-            ea.bytes = 16 << vex.l;
+            ea.bytes = (b == 0xd6 ? 8 : 16 << vex.l);
         }
         if ( ea.type == OP_MEM )
         {
Signature · 7 lines
2.9.2


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw

PATCH v2 2/3 x86/emulate: add support of emulating SSE2 instruction {, v}movd mm, m32

Found that Windows driver was using a SSE2 instruction MOVD.

---
Picked from the XenServer 7 patch queue, as suggested by Andrew Cooper

---
 xen/arch/x86/x86_emulate/x86_emulate.c | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)
xen/arch/x86/x86_emulate/x86_emulate.c +27 -3
index 0301235..2a56a67 100644
@@ -204,7 +204,7 @@ static uint8_t twobyte_table[256] = {
     /* 0x60 - 0x6F */
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps|ModRM,
     /* 0x70 - 0x7F */
-    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps|ModRM,
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps|ModRM, ImplicitOps|ModRM,
     /* 0x80 - 0x87 */
     ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
     ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
@@ -4409,6 +4409,10 @@ x86_emulate(
     case 0x6f: /* movq mm/m64,mm */
                /* {,v}movdq{a,u} xmm/m128,xmm */
                /* vmovdq{a,u} ymm/m256,ymm */
+    case 0x7e: /* movd mm,r/m32 */
+               /* movq mm,r/m64 */
+               /* {,v}movd xmm,r/m32 */
+               /* {,v}movq xmm,r/m64 */
     case 0x7f: /* movq mm,mm/m64 */
                /* {,v}movdq{a,u} xmm,xmm/m128 */
                /* vmovdq{a,u} ymm,ymm/m256 */
@@ -4432,7 +4436,17 @@ x86_emulate(
                 host_and_vcpu_must_have(sse2);
                 buf[0] = 0x66; /* SSE */
                 get_fpu(X86EMUL_FPU_xmm, &fic);
-                ea.bytes = (b == 0xd6 ? 8 : 16);
+                switch ( b )
+                {
+                case 0x7e:
+                    ea.bytes = 4;
+                    break;
+                case 0xd6:
+                    ea.bytes = 8;
+                    break;
+                default:
+                    ea.bytes = 16;
+                }
                 break;
             case vex_none:
                 if ( b != 0xe7 )
@@ -4452,7 +4466,17 @@ x86_emulate(
                     ((vex.pfx != vex_66) && (vex.pfx != vex_f3)));
             host_and_vcpu_must_have(avx);
             get_fpu(X86EMUL_FPU_ymm, &fic);
-            ea.bytes = (b == 0xd6 ? 8 : 16 << vex.l);
+            switch ( b )
+            {
+            case 0x7e:
+                ea.bytes = 4;
+                break;
+            case 0xd6:
+                ea.bytes = 8;
+                break;
+            default:
+                ea.bytes = 16 << vex.l;
+            }
         }
         if ( ea.type == OP_MEM )
         {

--
2.9.2

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#2 lore raw

Re: [PATCH v2 2/3] x86/emulate: add support of emulating SSE2 instruction {, v}movd mm, m32

xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 4415

Quoted 8 lines · Mihai Donțu
xen/arch/x86/x86_emulate/x86_emulate.c +4 -0
@@ -4409,6 +4409,10 @@ x86_emulate(
     case 0x6f: /* movq mm/m64,mm */
                /* {,v}movdq{a,u} xmm/m128,xmm */
                /* vmovdq{a,u} ymm/m256,ymm */
+    case 0x7e: /* movd mm,r/m32 */
+               /* movq mm,r/m64 */
+               /* {,v}movd xmm,r/m32 */
+               /* {,v}movq xmm,r/m64 */

This exposes a vulnerability where a guest can clobber local state in x86_emulate, by specifying registers such as %ebx as the destination.

You must either
1) Move this case up above the fail_if(ea.type != OP_MEM); check, or
2) modify the stub logic to convert a GPR destination to a memory address pointing into _regs.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#3 lore raw

Re: [PATCH v2 2/3] x86/emulate: add support of emulating SSE2 instruction {, v}movd mm, m32

Quoted 13 lines · On Monday 18 July 2016 15:57:09 Andrew Cooper wrote:

xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 4415

Quoted 8 lines · Mihai Donțu
xen/arch/x86/x86_emulate/x86_emulate.c +4 -0
@@ -4409,6 +4409,10 @@ x86_emulate(
     case 0x6f: /* movq mm/m64,mm */
                /* {,v}movdq{a,u} xmm/m128,xmm */
                /* vmovdq{a,u} ymm/m256,ymm */
+    case 0x7e: /* movd mm,r/m32 */
+               /* movq mm,r/m64 */
+               /* {,v}movd xmm,r/m32 */
+               /* {,v}movq xmm,r/m64 */  

This exposes a vulnerability where a guest can clobber local state in x86_emulate, by specifying registers such as %ebx as the destination.

Quoted 4 lines

You must either
1) Move this case up above the fail_if(ea.type != OP_MEM); check, or
2) modify the stub logic to convert a GPR destination to a memory address pointing into _regs.

I'm taking a look at (2) as it feels like the best approach. If I'm not making any good progress in the coming days, I'll fallback to (1).

Thank you,

Signature · 1 line
Mihai DONȚU

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#4 lore raw

PATCH v2 3/3 x86/emulate: added tests for {, v}movd mm, m32

In reply to Mihai Donțu
---
 tools/tests/x86_emulator/test_x86_emulator.c | 42 ++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
tools/tests/x86_emulator/test_x86_emulator.c +42 -0
index 8994149..e2bd7ce 100644
@@ -650,6 +650,48 @@ int main(int argc, char **argv)
 #define check_eip(which) (regs.eip == (unsigned long)instr + \
                                       (unsigned long)which##_len)
 
+    printf("%-40s", "Testing movd %%mm3,32(%%eax)...");
+    if ( stack_exec && cpu_has_mmx )
+    {
+        decl_insn(movd_to_mem32);
+
+        asm volatile ( "pcmpeqb %%mm3, %%mm3\n"
+                       put_insn(movd_to_mem32, "movd %%mm3, 32(%%eax)")
+                       :: );
+
+        *(res + 8) = 0xbdbdbdbd;
+        set_insn(movd_to_mem32);
+        regs.eax = (unsigned long)res;
+        rc = x86_emulate(&ctxt, &emulops);
+        if ( (rc != X86EMUL_OKAY) || *(res + 8) != 0xffffffff ||
+             !check_eip(movd_to_mem32) )
+            goto fail;
+        printf("okay\n");
+    }
+    else
+        printf("skipped\n");
+
+    printf("%-40s", "Testing vmovd %%xmm1,32(%%eax)...");
+    if ( stack_exec && cpu_has_avx )
+    {
+        decl_insn(vmovd_to_mem32);
+
+        asm volatile ( "pcmpeqb %%xmm1, %%xmm1\n"
+                       put_insn(vmovd_to_mem32, "vmovd %%xmm1, 32(%%eax)")
+                       :: );
+
+        *(res + 8) = 0xbdbdbdbd;
+        set_insn(vmovd_to_mem32);
+        regs.eax = (unsigned long)res;
+        rc = x86_emulate(&ctxt, &emulops);
+        if ( (rc != X86EMUL_OKAY) || *(res + 8) != 0xffffffff ||
+             !check_eip(vmovd_to_mem32) )
+            goto fail;
+        printf("okay\n");
+    }
+    else
+        printf("skipped\n");
+
     printf("%-40s", "Testing movq %mm3,(%ecx)...");
     if ( stack_exec && cpu_has_mmx )
     {
Signature · 7 lines
2.9.2


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v2 1/3
#6 lore raw
In reply to Mihai Donțu
Quoted 101 lines · Mihai Donțu

---
Changed since v1:
* added a test for vmovq
* made the tests depend on SSE and AVX, respectively
* added emulator support for vmovq (0xd6 forces the operand size to 64bit)

---
 tools/tests/x86_emulator/test_x86_emulator.c | 44 ++++++++++++++++++++++++++++
 xen/arch/x86/x86_emulate/x86_emulate.c       |  9 +++---
 2 files changed, 49 insertions(+), 4 deletions(-)
tools/tests/x86_emulator/test_x86_emulator.c +44 -0
index c7f572a..8994149 100644
@@ -697,6 +697,50 @@ int main(int argc, char **argv)
     else
         printf("skipped\n");
 
+    printf("%-40s", "Testing movq %%xmm0,32(%%eax)...");
+    if ( stack_exec && cpu_has_sse )
+    {
+        decl_insn(movq_to_mem2);
+
+        asm volatile ( "pcmpgtb %%xmm0, %%xmm0\n"
+                       put_insn(movq_to_mem2, "movq %%xmm0, 32(%%eax)")
+                       :: );
+
+        *((unsigned long *)res + 4) = 0xbdbdbdbdbdbdbdbd;
+        set_insn(movq_to_mem2);
+        regs.eax = (unsigned long)res;
+        rc = x86_emulate(&ctxt, &emulops);
+        if ( rc != X86EMUL_OKAY || !check_eip(movq_to_mem2) )
+            goto fail;
+        if ( *((unsigned long *)res + 4) )
+            goto fail;
+        printf("okay\n");
+    }
+    else
+        printf("skipped\n");
+
+    printf("%-40s", "Testing vmovq %%xmm1,32(%%eax)...");
+    if ( stack_exec && cpu_has_avx )
+    {
+        decl_insn(vmovq_to_mem);
+
+        asm volatile ( "pcmpgtb %%xmm1, %%xmm1\n"
+                       put_insn(vmovq_to_mem, "vmovq %%xmm1, 32(%%eax)")
+                       :: );
+
+        *((unsigned long *)res + 4) = 0xbdbdbdbdbdbdbdbd;
+        set_insn(vmovq_to_mem);
+        regs.eax = (unsigned long)res;
+        rc = x86_emulate(&ctxt, &emulops);
+        if ( rc != X86EMUL_OKAY || !check_eip(vmovq_to_mem) )
+            goto fail;
+        if ( *((unsigned long *)res + 4) )
+            goto fail;
+        printf("okay\n");
+    }
+    else
+        printf("skipped\n");
+
     printf("%-40s", "Testing movdqu %xmm2,(%ecx)...");
     if ( stack_exec && cpu_has_sse2 )
     {
xen/arch/x86/x86_emulate/x86_emulate.c +4 -3
index fe594ba..0301235 100644
@@ -245,7 +245,7 @@ static uint8_t twobyte_table[256] = {
     ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
     ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps,
     /* 0xD0 - 0xDF */
-    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+    0, 0, 0, 0, 0, 0, ImplicitOps|ModRM, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     /* 0xE0 - 0xEF */
     0, 0, 0, 0, 0, 0, 0, ImplicitOps|ModRM, 0, 0, 0, 0, 0, 0, 0, 0,
     /* 0xF0 - 0xFF */
@@ -4412,6 +4412,7 @@ x86_emulate(
     case 0x7f: /* movq mm,mm/m64 */
                /* {,v}movdq{a,u} xmm,xmm/m128 */
                /* vmovdq{a,u} ymm,ymm/m256 */
+    case 0xd6: /* {,v}movq xmm,xmm/m64 */
     {
         uint8_t *buf = get_stub(stub);
         struct fpu_insn_ctxt fic = { .insn_bytes = 5 };
@@ -4429,9 +4430,9 @@ x86_emulate(
             case vex_66:
             case vex_f3:
                 host_and_vcpu_must_have(sse2);
-                buf[0] = 0x66; /* movdqa */
+                buf[0] = 0x66; /* SSE */
                 get_fpu(X86EMUL_FPU_xmm, &fic);
-                ea.bytes = 16;
+                ea.bytes = (b == 0xd6 ? 8 : 16);
                 break;
             case vex_none:
                 if ( b != 0xe7 )

xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 4455

Quoted 5 lines
xen/arch/x86/x86_emulate/x86_emulate.c +1 -1
@@ -4451,7 +4452,7 @@ x86_emulate(
                     ((vex.pfx != vex_66) && (vex.pfx != vex_f3)));
             host_and_vcpu_must_have(avx);
             get_fpu(X86EMUL_FPU_ymm, &fic);
-            ea.bytes = 16 << vex.l;
+            ea.bytes = (b == 0xd6 ? 8 : 16 << vex.l);

It would be helpful to bracket (16 << vex.l) to make it more obvious as to what the operator precedent is.

Otherwise,

I can fix this up on commit if there are no other issues in the series.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel