Skip to contentSkip to conversation

Possible problem emulating movntq, movss

DISCUSSION Razvan Cojocaru 4 participants 11 messages Aug 6, 2014 – Aug 7, 2014

lore
Share & tools
lore
#0 lore raw

Hello,

We found that our HVM guests froze when trying to emulate movntq instructions. The solution seems to be to replace "goto done;" with "break;" at line 4191 (when handling "case 0x7f:") in xen/arch/x86/x86_emulate/x86_emulate.c. Otherwise the writeback part doesn't happen.

If you're happy with the fix I can prepare a patch, otherwise please let me know if we're missing something.

Thanks,
Razvan Cojocaru

#1 lore raw
Quoted 8 lines · Razvan Cojocaru

Hello,

We found that our HVM guests froze when trying to emulate movntq instructions. The solution seems to be to replace "goto done;" with "break;" at line 4191 (when handling "case 0x7f:") in xen/arch/x86/x86_emulate/x86_emulate.c. Otherwise the writeback part doesn't happen.

Quoted 6 lines

If you're happy with the fix I can prepare a patch, otherwise please let me know if we're missing something.

Thanks,
Razvan Cojocaru

I can't comment on the fix right now, but will take a look at it in due course.

If you are making a change, please can you add a testcase for movntq and movss to tools/tests/x86_emulator/test_x86_emulator.c which is the unittest harness for x86_emulate.

~Andrew

#2 lore raw
In reply to Razvan Cojocaru
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 06.08.14 at 10:57, <[email protected]> wrote:

We found that our HVM guests froze when trying to emulate movntq instructions. The solution seems to be to replace "goto done;" with "break;" at line 4191 (when handling "case 0x7f:") in xen/arch/x86/x86_emulate/x86_emulate.c. Otherwise the writeback part doesn't happen.

If you're happy with the fix I can prepare a patch, otherwise please let me know if we're missing something.

No, that doesn't look right: There's nothing left to be written back at that point (registers get updated with the instruction executed via the on-stack stub, and memory gets written with immediately preceding ops->write(). So without you being more specific about _what_ you see going wrong I don't think I can give further advice.

Furthermore what you write is kind of inconsistent: For one, opcode 0x7f is movq/movdq[au] rather than movntdq (admitted they're being handled by the same code block, but you ought to be rather precise here). And then the subject of your mail mentions movss, but the body doesn't at all - is that because you mean the same would apply to that other similar code block?

As to Andrew asking for added tests: movq, movdqu, and vmovdqu are all being tested with both operation directions (covering one of the two code blocks in question), and the set of tests for movsd, movaps, vmovsd, and vmovaps should be sufficient to cover the other of the two code blocks too.

Jan

#3 lore raw
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 06.08.14 at 10:57, <[email protected]> wrote:

We found that our HVM guests froze when trying to emulate movntq instructions. The solution seems to be to replace "goto done;" with "break;" at line 4191 (when handling "case 0x7f:") in xen/arch/x86/x86_emulate/x86_emulate.c. Otherwise the writeback part doesn't happen.

Quoted 8 lines
Quoted 2 lines

If you're happy with the fix I can prepare a patch, otherwise please let me know if we're missing something.

No, that doesn't look right: There's nothing left to be written back at that point (registers get updated with the instruction executed via the on-stack stub, and memory gets written with immediately preceding ops->write(). So without you being more specific about _what_ you see going wrong I don't think I can give further advice.

I understand. My colleague and fellow xen-devel subscriber Andrei Lutas has found the issue and the solution, and will reply with more details.

Quoted 6 lines

Furthermore what you write is kind of inconsistent: For one, opcode 0x7f is movq/movdq[au] rather than movntdq (admitted they're being handled by the same code block, but you ought to be rather precise here). And then the subject of your mail mentions movss, but the body doesn't at all - is that because you mean the same would apply to that other similar code block?

Indeed, my assumptions were exactly those: movq/movdq[au] is handled in the same code block (it is, in fact, arguably the beginning of said code block) as movntdq, and movss seems to be handled in the same manner ("goto done;" vs "break;"), and if I understood Andrei correctly, poses the same problem.

Thanks,
Razvan Cojocaru

#4 lore raw
In reply to Jan Beulich

Hello there,

Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 06.08.14 at 10:57, <[email protected]> wrote:

We found that our HVM guests froze when trying to emulate movntq instructions. The solution seems to be to replace "goto done;" with "break;" at line 4191 (when handling "case 0x7f:") in xen/arch/x86/x86_emulate/x86_emulate.c. Otherwise the writeback part doesn't happen.

Quoted 7 lines
Quoted 2 lines

If you're happy with the fix I can prepare a patch, otherwise please let me know if we're missing something.

No, that doesn't look right: There's nothing left to be written back at that point (registers get updated with the instruction executed via the on-stack stub, and memory gets written with immediately preceding ops->write(). So without you being more specific about _what_ you see going wrong I don't think I can give further advice.

Except for maybe the instruction pointer? That doesn't seem to be updated anywhereexcept during the write-back phase (or maybe I'm missing the spot). The problem is that the guest gets stuck with the instruction pointer pointing to the sameinstruction (in our particular case it is "MOVDQU xmm0, xmmword ptr [rdx + rcx - 0x10]"),entering in an infinite loop (EPT violation - emulate), since the IP doesn't seem to be updated.

Quoted 7 lines

Furthermore what you write is kind of inconsistent: For one, opcode 0x7f is movq/movdq[au] rather than movntdq (admitted they're being handled by the same code block, but you ought to be rather precise here). And then the subject of your mail mentions movss, but the body doesn't at all - is that because you mean the same would apply to that other similar code block?

A quick look reveals that 0x0f 0x2b/0x28/0x29/0x10/0x11 and 0x0f 0xe7/0x6f/0x7f encodings are all affected. While other places may be affected too, these two encoding sets seem to be the only ones where "goto done;" is used unconditionally instead of a "break;" - all otheruses of "goto done;" are made when an error is encountered.

Quoted 8 lines

As to Andrew asking for added tests: movq, movdqu, and vmovdqu are all being tested with both operation directions (covering one of the two code blocks in question), and the set of tests for movsd, movaps, vmovsd, and vmovaps should be sufficient to cover the other of the two code blocks too.

Jan

Best regards,
Andrei.

Quoted 7 lines

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

#5 lore raw
Quoted 47 lines · Andrei LUTAS

Hello there,

Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 06.08.14 at 10:57, <[email protected]> wrote:

We found that our HVM guests froze when trying to emulate movntq instructions. The solution seems to be to replace "goto done;" with "break;" at line 4191 (when handling "case 0x7f:") in xen/arch/x86/x86_emulate/x86_emulate.c. Otherwise the writeback part doesn't happen.

Quoted 8 lines
Quoted 3 lines

If you're happy with the fix I can prepare a patch, otherwise please let
me know if we're missing something.

No, that doesn't look right: There's nothing left to be written back at that point (registers get updated with the instruction executed via the on-stack stub, and memory gets written with immediately preceding ops->write(). So without you being more specific about _what_ you see going wrong I don't think I can give further advice.

Except for maybe the instruction pointer? That doesn't seem to be updated anywhereexcept during the write-back phase (or maybe I'm missing the spot).
The problem is that the guest gets stuck with the instruction pointer pointing to the sameinstruction (in our particular case it is "MOVDQU xmm0, xmmword ptr [rdx + rcx - 0x10]"),entering in an infinite loop (EPT violation - emulate), since the IP doesn't seem to be updated.

Quoted 7 lines

Furthermore what you write is kind of inconsistent: For one, opcode 0x7f is movq/movdq[au] rather than movntdq (admitted they're being handled by the same code block, but you ought to be rather precise here). And then the subject of your mail mentions movss, but the body doesn't at all - is that because you mean the same would apply to that other similar code block?

A quick look reveals that 0x0f 0x2b/0x28/0x29/0x10/0x11 and 0x0f 0xe7/0x6f/0x7f
encodings are all affected. While other places may be affected too, these two
encoding sets seem to be the only ones where "goto done;" is used unconditionally instead of a "break;" - all otheruses of "goto done;" are made when an error is encountered.

Quoted 7 lines

As to Andrew asking for added tests: movq, movdqu, and vmovdqu are all being tested with both operation directions (covering one of the two code blocks in question), and the set of tests for movsd, movaps, vmovsd, and vmovaps should be sufficient to cover the other of the two code blocks too.

Quoted 3 lines
Quoted 1 line

Jan

Best regards,
Andrei.

It would appear that for some instructions, these movxxx included, the test harness does not verify that the instruction pointer has been suitably updated.

It is plausible that this is a real bug and the unit tests are erroneously passing. I would suggest starting by adding instruction pointer checks to the test harness first to confirm whether there is a bug.

~Andrew

#6 lore raw
Quoted 52 lines · Andrew Cooper
Quoted 47 lines · Andrei LUTAS

Hello there,

Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 06.08.14 at 10:57, <[email protected]> wrote:

We found that our HVM guests froze when trying to emulate movntq instructions. The solution seems to be to replace "goto done;" with "break;" at line 4191 (when handling "case 0x7f:") in xen/arch/x86/x86_emulate/x86_emulate.c. Otherwise the writeback part doesn't happen.

Quoted 8 lines
Quoted 3 lines

If you're happy with the fix I can prepare a patch, otherwise please let
me know if we're missing something.

No, that doesn't look right: There's nothing left to be written back at that point (registers get updated with the instruction executed via the on-stack stub, and memory gets written with immediately preceding ops->write(). So without you being more specific about _what_ you see going wrong I don't think I can give further advice.

Except for maybe the instruction pointer? That doesn't seem to be updated anywhereexcept during the write-back phase (or maybe I'm missing the spot).
The problem is that the guest gets stuck with the instruction pointer pointing to the sameinstruction (in our particular case it is "MOVDQU xmm0, xmmword ptr [rdx + rcx - 0x10]"),entering in an infinite loop (EPT violation - emulate), since the IP doesn't seem to be updated.

Quoted 7 lines

Furthermore what you write is kind of inconsistent: For one, opcode 0x7f is movq/movdq[au] rather than movntdq (admitted they're being handled by the same code block, but you ought to be rather precise here). And then the subject of your mail mentions movss, but the body doesn't at all - is that because you mean the same would apply to that other similar code block?

A quick look reveals that 0x0f 0x2b/0x28/0x29/0x10/0x11 and 0x0f 0xe7/0x6f/0x7f
encodings are all affected. While other places may be affected too, these two
encoding sets seem to be the only ones where "goto done;" is used unconditionally instead of a "break;" - all otheruses of "goto done;" are made when an error is encountered.

Quoted 7 lines

As to Andrew asking for added tests: movq, movdqu, and vmovdqu are all being tested with both operation directions (covering one of the two code blocks in question), and the set of tests for movsd, movaps, vmovsd, and vmovaps should be sufficient to cover the other of the two code blocks too.

Quoted 3 lines
Quoted 1 line

Jan

Best regards,
Andrei.

Quoted 7 lines

It would appear that for some instructions, these movxxx included, the test harness does not verify that the instruction pointer has been suitably updated.

It is plausible that this is a real bug and the unit tests are erroneously passing. I would suggest starting by adding instruction pointer checks to the test harness first to confirm whether there is a bug.

Quick and dirty test:

655     printf("%-40s", "Testing movdqu %xmm2,(%ecx)...");
656     if ( stack_exec && cpu_has_sse2 )
657     {
658         extern const unsigned char movdqu_to_mem[];
659
660         asm volatile ( "pcmpeqb %%xmm2, %%xmm2\n"
661                        ".pushsection .test, \"a\", @progbits\n"
662                        "movdqu_to_mem: movdqu %%xmm2, (%0)\n"
663                        ".popsection" :: "c" (NULL) );
664
665         memcpy(instr, movdqu_to_mem, 15);
666         memset(res, 0x55, 64);
667         memset(res + 8, 0xff, 16);
668         regs.eip    = (unsigned long)&instr[0];
669         regs.ecx    = (unsigned long)res;
670         rc = x86_emulate(&ctxt, &emulops);
671         if ( (rc != X86EMUL_OKAY) || memcmp(res, res + 8, 32) )

672 goto fail;
673
674 if ( regs.eip == (unsigned long)&instr[0] )
675 printf("eip has NOT been updated!\n");
676 else
677 printf("okay\n");
678 }
679 else
680 printf("skipped\n");

Output:

Testing movdqu %xmm2,(%ecx)... eip has NOT been updated!

Thanks,
Razvan Cojocaru

#7 lore raw
In reply to Andrei LUTAS
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 06.08.14 at 12:47, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 06.08.14 at 10:57, <[email protected]> wrote:

We found that our HVM guests froze when trying to emulate movntq instructions. The solution seems to be to replace "goto done;" with "break;" at line 4191 (when handling "case 0x7f:") in xen/arch/x86/x86_emulate/x86_emulate.c. Otherwise the writeback part doesn't happen.

Quoted 7 lines
Quoted 2 lines

If you're happy with the fix I can prepare a patch, otherwise please let me know if we're missing something.

No, that doesn't look right: There's nothing left to be written back at that point (registers get updated with the instruction executed via the on-stack stub, and memory gets written with immediately preceding ops->write(). So without you being more specific about _what_ you see going wrong I don't think I can give further advice.

Quoted 6 lines

Except for maybe the instruction pointer? That doesn't seem to be updated anywhereexcept during the write-back phase (or maybe I'm missing the spot). The problem is that the guest gets stuck with the instruction pointer pointing to the sameinstruction (in our particular case it is "MOVDQU xmm0, xmmword ptr [rdx + rcx - 0x10]"),entering in an infinite loop (EPT violation - emulate), since the IP doesn't seem to be updated.

Now that is indeed a problem, but not solved by simply replacing the "goto done" with "break". I'll look into getting you a proper fix.

Jan

#8 lore raw
Quoted 24 lines
Quoted 17 lines
Quoted 2 lines · On 06.08.14 at 14:16, <[email protected]> wrote:
Quoted 1 line · On 06.08.14 at 12:47, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 06.08.14 at 10:57, <[email protected]> wrote:

We found that our HVM guests froze when trying to emulate movntq instructions. The solution seems to be to replace "goto done;" with "break;" at line 4191 (when handling "case 0x7f:") in xen/arch/x86/x86_emulate/x86_emulate.c. Otherwise the writeback part doesn't happen.

Quoted 7 lines
Quoted 2 lines

If you're happy with the fix I can prepare a patch, otherwise please let me know if we're missing something.

No, that doesn't look right: There's nothing left to be written back at that point (registers get updated with the instruction executed via the on-stack stub, and memory gets written with immediately preceding ops->write(). So without you being more specific about _what_ you see going wrong I don't think I can give further advice.

Quoted 6 lines

Except for maybe the instruction pointer? That doesn't seem to be updated anywhereexcept during the write-back phase (or maybe I'm missing the spot). The problem is that the guest gets stuck with the instruction pointer pointing to the sameinstruction (in our particular case it is "MOVDQU xmm0, xmmword ptr [rdx + rcx - 0x10]"),entering in an infinite loop (EPT violation - emulate), since the IP doesn't seem to be updated.

Quoted 2 lines

Now that is indeed a problem, but not solved by simply replacing the "goto done" with "break". I'll look into getting you a proper fix.

Mind giving this one (lightly tested only) a try?

Jan

xen/arch/x86/x86_emulate/x86_emulate.c +16 -17
@@ -720,29 +720,26 @@ do{ uint8_t stub[] = { _bytes, 0xc3 };  
     put_fpu(&fic);                                                      \
 } while (0)
 
-static unsigned long __get_rep_prefix(
-    struct cpu_user_regs *int_regs,
-    struct cpu_user_regs *ext_regs,
+static unsigned long _get_rep_prefix(
+    const struct cpu_user_regs *int_regs,
     int ad_bytes)
 {
-    unsigned long ecx = ((ad_bytes == 2) ? (uint16_t)int_regs->ecx :
-                         (ad_bytes == 4) ? (uint32_t)int_regs->ecx :
-                         int_regs->ecx);
-
-    /* Skip the instruction if no repetitions are required. */
-    if ( ecx == 0 )
-        ext_regs->eip = int_regs->eip;
-
-    return ecx;
+    return (ad_bytes == 2) ? (uint16_t)int_regs->ecx :
+           (ad_bytes == 4) ? (uint32_t)int_regs->ecx :
+           int_regs->ecx;
 }
 
 #define get_rep_prefix() ({                                             \
     unsigned long max_reps = 1;                                         \
     if ( rep_prefix() )                                                 \
-        max_reps = __get_rep_prefix(&_regs, ctxt->regs, ad_bytes);      \
+        max_reps = _get_rep_prefix(&_regs, ad_bytes);                   \
     if ( max_reps == 0 )                                                \
-        goto done;                                                      \
-   max_reps;                                                            \
+    {                                                                   \
+        /* Skip the instruction if no repetitions are required. */      \
+        dst.type = OP_NONE;                                             \
+        goto writeback;                                                 \
+    }                                                                   \
+    max_reps;                                                           \
 })
 
 static void __put_rep_prefix(
@@ -3921,7 +3918,8 @@ x86_emulate(
         if ( !rc && (b & 1) && (ea.type == OP_MEM) )
             rc = ops->write(ea.mem.seg, ea.mem.off, mmvalp,
                             ea.bytes, ctxt);
-        goto done;
+        dst.type = OP_NONE;
+        break;
     }
 
     case 0x20: /* mov cr,reg */
@@ -4188,7 +4186,8 @@ x86_emulate(
         if ( !rc && (b != 0x6f) && (ea.type == OP_MEM) )
             rc = ops->write(ea.mem.seg, ea.mem.off, mmvalp,
                             ea.bytes, ctxt);
-        goto done;
+        dst.type = OP_NONE;
+        break;
     }
 
     case 0x80 ... 0x8f: /* jcc (near) */ {
xen/arch/x86/x86_emulate/x86_emulate.c +16 -17
@@ -720,29 +720,26 @@ do{ uint8_t stub[] = { _bytes, 0xc3 };  
     put_fpu(&fic);                                                      \
 } while (0)
 
-static unsigned long __get_rep_prefix(
-    struct cpu_user_regs *int_regs,
-    struct cpu_user_regs *ext_regs,
+static unsigned long _get_rep_prefix(
+    const struct cpu_user_regs *int_regs,
     int ad_bytes)
 {
-    unsigned long ecx = ((ad_bytes == 2) ? (uint16_t)int_regs->ecx :
-                         (ad_bytes == 4) ? (uint32_t)int_regs->ecx :
-                         int_regs->ecx);
-
-    /* Skip the instruction if no repetitions are required. */
-    if ( ecx == 0 )
-        ext_regs->eip = int_regs->eip;
-
-    return ecx;
+    return (ad_bytes == 2) ? (uint16_t)int_regs->ecx :
+           (ad_bytes == 4) ? (uint32_t)int_regs->ecx :
+           int_regs->ecx;
 }
 
 #define get_rep_prefix() ({                                             \
     unsigned long max_reps = 1;                                         \
     if ( rep_prefix() )                                                 \
-        max_reps = __get_rep_prefix(&_regs, ctxt->regs, ad_bytes);      \
+        max_reps = _get_rep_prefix(&_regs, ad_bytes);                   \
     if ( max_reps == 0 )                                                \
-        goto done;                                                      \
-   max_reps;                                                            \
+    {                                                                   \
+        /* Skip the instruction if no repetitions are required. */      \
+        dst.type = OP_NONE;                                             \
+        goto writeback;                                                 \
+    }                                                                   \
+    max_reps;                                                           \
 })
 
 static void __put_rep_prefix(
@@ -3921,7 +3918,8 @@ x86_emulate(
         if ( !rc && (b & 1) && (ea.type == OP_MEM) )
             rc = ops->write(ea.mem.seg, ea.mem.off, mmvalp,
                             ea.bytes, ctxt);
-        goto done;
+        dst.type = OP_NONE;
+        break;
     }
 
     case 0x20: /* mov cr,reg */
@@ -4188,7 +4186,8 @@ x86_emulate(
         if ( !rc && (b != 0x6f) && (ea.type == OP_MEM) )
             rc = ops->write(ea.mem.seg, ea.mem.off, mmvalp,
                             ea.bytes, ctxt);
-        goto done;
+        dst.type = OP_NONE;
+        break;
     }
 
     case 0x80 ... 0x8f: /* jcc (near) */ {

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

#9 lore raw
Quoted 24 lines · Jan Beulich
Quoted 24 lines
Quoted 23 lines
Quoted 2 lines · On 06.08.14 at 14:16, <[email protected]> wrote:
Quoted 1 line · On 06.08.14 at 12:47, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 06.08.14 at 10:57, <[email protected]> wrote:

We found that our HVM guests froze when trying to emulate movntq instructions. The solution seems to be to replace "goto done;" with "break;" at line 4191 (when handling "case 0x7f:") in xen/arch/x86/x86_emulate/x86_emulate.c. Otherwise the writeback part doesn't happen.

Quoted 7 lines
Quoted 2 lines

If you're happy with the fix I can prepare a patch, otherwise please let me know if we're missing something.

No, that doesn't look right: There's nothing left to be written back at that point (registers get updated with the instruction executed via the on-stack stub, and memory gets written with immediately preceding ops->write(). So without you being more specific about _what_ you see going wrong I don't think I can give further advice.

Except for maybe the instruction pointer? That doesn't seem to be updated anywhereexcept during the write-back phase (or maybe I'm missing the spot). The problem is that the guest gets stuck with the instruction pointer pointing to the sameinstruction (in our particular case it is "MOVDQU xmm0, xmmword ptr [rdx + rcx - 0x10]"),entering in an infinite loop (EPT violation - emulate), since the IP doesn't seem to be updated.

Quoted 4 lines
Quoted 2 lines

Now that is indeed a problem, but not solved by simply replacing the "goto done" with "break". I'll look into getting you a proper fix.

Mind giving this one (lightly tested only) a try?

We've tested this with both a modified version of test_x86_emulator.c (small patch to follow) and our HVM guest, and everything seems to be working.

Thanks,
Razvan Cojocaru

#10 lore raw
In reply to Andrei LUTAS
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 06.08.14 at 12:47, <[email protected]> wrote:

A quick look reveals that 0x0f 0x2b/0x28/0x29/0x10/0x11 and 0x0f 0xe7/0x6f/0x7f
encodings are all affected. While other places may be affected too, these two
encoding sets seem to be the only ones where "goto done;" is used unconditionally instead of a "break;" - all otheruses of "goto done;" are made when an error is encountered.

With two other exceptions: The get_rep_prefix() use seems to be broken too, while the use after the swint label looks to be correct (ops->inject_sw_interrupt() ought to do all necessary updating).

Jan