Skip to contentSkip to conversation

x86/emul: Correct the decoding of mov to/from cr/dr

PATCH Superseded Andrew Cooper 2 participants 4 messages Mar 6, 2017

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/emul: Correct the decoding of mov to/from cr/dr

The mov to/from cr/dr behave as if they were encoded with Mod = 3. When encoded with Mod != 3, no displacement or SIB bytes are fetched.

---
CC: Jan Beulich <[email protected]>

---
 tools/tests/x86_emulator/test_x86_emulator.c | 21 +++++++++++++++++++++
 xen/arch/x86/x86_emulate/x86_emulate.c       | 20 +++++++++++++++++++-
 2 files changed, 40 insertions(+), 1 deletion(-)
tools/tests/x86_emulator/test_x86_emulator.c +21 -0
index 37d00f1..b588a8d 100644
@@ -894,6 +894,27 @@ int main(int argc, char **argv)
     }
     printf("okay\n");
 
+    printf("%-40s", "Testing mov %%cr4,%%esi (bad ModRM)...");
+    /*
+     * Mod = 1, Reg = 4, R/M = 6 would normally encode a memory reference of
+     * disp8(%esi), but mov to/from cr/dr are special and behave as if they
+     * were encoded with Mod == 3.
+     */
+    instr[0] = 0x0f; instr[1] = 0x20, instr[2] = 0146;
+    instr[3] = 0; /* Supposed disp8. */
+    regs.esi = 0;
+    regs.eip = (unsigned long)&instr[0];
+    rc = x86_emulate(&ctxt, &emulops);
+    /*
+     * We don't care precicely what gets read from %cr4 into %esi, just so
+     * long as ModRM is treated as a register operand and 0(%esi) isn't
+     * followed as a memory reference.
+     */
+    if ( (rc != X86EMUL_OKAY) ||
+         (regs.eip != (unsigned long)&instr[3]) )
+        goto fail;
+    printf("okay\n");
+
 #define decl_insn(which) extern const unsigned char which[], \
                          which##_end[] asm ( ".L" #which "_end" )
 #define put_insn(which, insn) ".pushsection .test, \"ax\", @progbits\n" \
xen/arch/x86/x86_emulate/x86_emulate.c +19 -1
index ad62420..58be737 100644
@@ -2086,7 +2086,8 @@ x86_decode_twobyte(
         }
         /* fall through */
     case 0x21: case 0x23: /* mov to/from dr */
-        generate_exception_if(lock_prefix || ea.type != OP_REG, EXC_UD);
+        ASSERT(ea.type == OP_REG); /* Early operand adjustment ensures this. */
+        generate_exception_if(lock_prefix, EXC_UD);
         op_bytes = mode_64bit() ? 8 : 4;
         break;
 
@@ -2427,6 +2428,23 @@ x86_decode(
                 break;
             }
         }
+        else if ( ext == ext_0f )
+        {
+            switch ( b )
+            {
+            case 0x20: /* mov cr,reg */
+            case 0x21: /* mov dr,reg */
+            case 0x22: /* mov reg,cr */
+            case 0x23: /* mov reg,dr */
+                /*
+                 * Mov to/from cr/dr ignore the encoding of Mod, and behave as
+                 * if they were encoded as reg/reg instructions.  No futher
+                 * disp/SIB bytes are fetched.
+                 */
+                modrm_mod = 3;
+                break;
+            }
+        }
 
         if ( modrm_mod == 3 )
         {
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 06.03.17 at 11:30, <[email protected]> wrote:

The mov to/from cr/dr behave as if they were encoded with Mod = 3. When encoded with Mod != 3, no displacement or SIB bytes are fetched.

Would mind letting us know how you became aware of this oddity? It's clearly both unexpected and undocumented, and I wonder whether there are any other opcodes behaving the same. In any even I think we want to make at least an attempt at having HW vendors confirm this (and to address the other-opcodes concern), so I'm extending the Cc list.

tools/tests/x86_emulator/test_x86_emulator.c int main(int argc, char **argv) · line 903

Quoted 13 lines
tools/tests/x86_emulator/test_x86_emulator.c +7 -0
@@ -894,6 +894,27 @@ int main(int argc, char **argv)
     }
     printf("okay\n");
 
+    printf("%-40s", "Testing mov %%cr4,%%esi (bad ModRM)...");
+    /*
+     * Mod = 1, Reg = 4, R/M = 6 would normally encode a memory reference of
+     * disp8(%esi), but mov to/from cr/dr are special and behave as if they
+     * were encoded with Mod == 3.
+     */
+    instr[0] = 0x0f; instr[1] = 0x20, instr[2] = 0146;

I can guess why you've done it this way, but I'd prefer if we didn't start using octal numbers. I for one am very used to reading ModRM bytes in their hex representation.

Quoted 23 lines
xen/arch/x86/x86_emulate/x86_emulate.c +9 -1
@@ -2086,7 +2086,8 @@ x86_decode_twobyte(
         }
         /* fall through */
     case 0x21: case 0x23: /* mov to/from dr */
-        generate_exception_if(lock_prefix || ea.type != OP_REG, EXC_UD);
+        ASSERT(ea.type == OP_REG); /* Early operand adjustment ensures this. */
+        generate_exception_if(lock_prefix, EXC_UD);
         op_bytes = mode_64bit() ? 8 : 4;
         break;
 
@@ -2427,6 +2428,23 @@ x86_decode(
                 break;
             }
         }
+        else if ( ext == ext_0f )
+        {
+            switch ( b )
+            {
+            case 0x20: /* mov cr,reg */
+            case 0x21: /* mov dr,reg */
+            case 0x22: /* mov reg,cr */

xen/arch/x86/x86_emulate/x86_emulate.c x86_decode( · line 2447

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +10 -0
+            case 0x23: /* mov reg,dr */
+                /*
+                 * Mov to/from cr/dr ignore the encoding of Mod, and behave as
+                 * if they were encoded as reg/reg instructions.  No futher
+                 * disp/SIB bytes are fetched.
+                 */
+                modrm_mod = 3;
+                break;
+            }
+        }

Just like done by
https://lists.xenproject.org/archives/html/xen-devel/2017-03/msg00588.html
this calls for the outer if/else to be morphed into a switch().

Jan

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

#2 lore raw
Quoted 5 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 06.03.17 at 11:30, <[email protected]> wrote:

The mov to/from cr/dr behave as if they were encoded with Mod = 3. When encoded with Mod != 3, no displacement or SIB bytes are fetched.

Would mind letting us know how you became aware of this oddity? It's clearly both unexpected

Most definitely.

Quoted 1 line

and undocumented,

Actually, the contrary. It is explicitly called out in both Intel and AMDs instruction manual, which is why I noticed it.

I have confirmed that hardware doesn't fetch disp or SIB bytes on my oldest and newest Intel and AMD boxes.

Quoted 1 line

and I wonder whether there are any other opcodes behaving the same.

Not that I have spotted.

Quoted 10 lines

In any even I think we want to make at least an attempt at having HW vendors confirm this (and to address the other-opcodes concern), so I'm extending the Cc list.

Quoted 6 lines
tools/tests/x86_emulator/test_x86_emulator.c +0 -0
@@ -894,6 +894,27 @@ int main(int argc, char **argv)
     }
     printf("okay\n");
 
Quoted 10 lines

tools/tests/x86_emulator/test_x86_emulator.c int main(int argc, char **argv) · line 903

Quoted 7 lines
tools/tests/x86_emulator/test_x86_emulator.c +7 -0
+    printf("%-40s", "Testing mov %%cr4,%%esi (bad ModRM)...");
+    /*
+     * Mod = 1, Reg = 4, R/M = 6 would normally encode a memory reference of
+     * disp8(%esi), but mov to/from cr/dr are special and behave as if they
+     * were encoded with Mod == 3.
+     */
+    instr[0] = 0x0f; instr[1] = 0x20, instr[2] = 0146;

I can guess why you've done it this way, but I'd prefer if we didn't start using octal numbers. I for one am very used to reading ModRM bytes in their hex representation.

The argument ought to be for which representation is easier. I'd accept an argument for consistency with the rest of the code, but are you seriously saying you think that hex is easier to read/manipulate than octal for ModRM/SIB bytes?

Quoted 29 lines
Quoted 29 lines
xen/arch/x86/x86_emulate/x86_emulate.c +14 -1
@@ -2086,7 +2086,8 @@ x86_decode_twobyte(
         }
         /* fall through */
     case 0x21: case 0x23: /* mov to/from dr */
-        generate_exception_if(lock_prefix || ea.type != OP_REG, EXC_UD);
+        ASSERT(ea.type == OP_REG); /* Early operand adjustment ensures this. */
+        generate_exception_if(lock_prefix, EXC_UD);
         op_bytes = mode_64bit() ? 8 : 4;
         break;
 
@@ -2427,6 +2428,23 @@ x86_decode(
                 break;
             }
         }
+        else if ( ext == ext_0f )
+        {
+            switch ( b )
+            {
+            case 0x20: /* mov cr,reg */
+            case 0x21: /* mov dr,reg */
+            case 0x22: /* mov reg,cr */
+            case 0x23: /* mov reg,dr */
+                /*
+                 * Mov to/from cr/dr ignore the encoding of Mod, and behave as
+                 * if they were encoded as reg/reg instructions.  No futher
+                 * disp/SIB bytes are fetched.
Quoted 8 lines

xen/arch/x86/x86_emulate/x86_emulate.c x86_decode( · line 2447

Quoted 5 lines
xen/arch/x86/x86_emulate/x86_emulate.c +5 -0
+                 */
+                modrm_mod = 3;
+                break;
+            }
+        }

Just like done by
https://lists.xenproject.org/archives/html/xen-devel/2017-03/msg00588.html
this calls for the outer if/else to be morphed into a switch().

I did that first, but it was rather complicated to read. I can switch back to that if you'd prefer.

~Andrew

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

#3 lore raw
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 06.03.17 at 14:30, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 06.03.17 at 11:30, <[email protected]> wrote:

The mov to/from cr/dr behave as if they were encoded with Mod = 3. When encoded with Mod != 3, no displacement or SIB bytes are fetched.

Would mind letting us know how you became aware of this oddity? It's clearly both unexpected

Quoted 6 lines

Most definitely.

Quoted 1 line

and undocumented,

Actually, the contrary. It is explicitly called out in both Intel and AMDs instruction manual, which is why I noticed it.

Ah, well hidden in the middle of text. Using standard opcode representation for non-standard encodings is far from helpful - that's what I look at mainly (and the text becomes important only when there _are_ oddities there).

Quoted 2 lines

I have confirmed that hardware doesn't fetch disp or SIB bytes on my oldest and newest Intel and AMD boxes.

So did I.

Quoted 3 lines
Quoted 1 line

and I wonder whether there are any other opcodes behaving the same.

Not that I have spotted.

Neither me, but there must be something in the decoder special casing those insns, and it's easily imaginable for such special cases to also apply elsewhere.

Quoted 17 lines
Quoted 6 lines
Quoted 6 lines
tools/tests/x86_emulator/test_x86_emulator.c +0 -0
@@ -894,6 +894,27 @@ int main(int argc, char **argv)
     }
     printf("okay\n");
 
Quoted 10 lines

tools/tests/x86_emulator/test_x86_emulator.c int main(int argc, char **argv) · line 903

Quoted 7 lines
tools/tests/x86_emulator/test_x86_emulator.c +7 -0
+    printf("%-40s", "Testing mov %%cr4,%%esi (bad ModRM)...");
+    /*
+     * Mod = 1, Reg = 4, R/M = 6 would normally encode a memory reference of
+     * disp8(%esi), but mov to/from cr/dr are special and behave as if they
+     * were encoded with Mod == 3.
+     */
+    instr[0] = 0x0f; instr[1] = 0x20, instr[2] = 0146;

I can guess why you've done it this way, but I'd prefer if we didn't start using octal numbers. I for one am very used to reading ModRM bytes in their hex representation.

Quoted 4 lines

The argument ought to be for which representation is easier. I'd accept an argument for consistency with the rest of the code, but are you seriously saying you think that hex is easier to read/manipulate than octal for ModRM/SIB bytes?

Yes, and very seriously in fact. Nevertheless I admit that this is a matter of what one is used to. The only alternative to hex I'd freely accept would be to use bit fields to fill the pieces individually.

Quoted 37 lines
Quoted 28 lines
Quoted 28 lines
xen/arch/x86/x86_emulate/x86_emulate.c +14 -1
@@ -2086,7 +2086,8 @@ x86_decode_twobyte(
         }
         /* fall through */
     case 0x21: case 0x23: /* mov to/from dr */
-        generate_exception_if(lock_prefix || ea.type != OP_REG, EXC_UD);
+        ASSERT(ea.type == OP_REG); /* Early operand adjustment ensures this. */
+        generate_exception_if(lock_prefix, EXC_UD);
         op_bytes = mode_64bit() ? 8 : 4;
         break;
 
@@ -2427,6 +2428,23 @@ x86_decode(
                 break;
             }
         }
+        else if ( ext == ext_0f )
+        {
+            switch ( b )
+            {
+            case 0x20: /* mov cr,reg */
+            case 0x21: /* mov dr,reg */
+            case 0x22: /* mov reg,cr */
+            case 0x23: /* mov reg,dr */
+                /*
+                 * Mov to/from cr/dr ignore the encoding of Mod, and behave as
+                 * if they were encoded as reg/reg instructions.  No futher
+                 * disp/SIB bytes are fetched.
Quoted 8 lines

xen/arch/x86/x86_emulate/x86_emulate.c x86_decode( · line 2447

Quoted 5 lines
xen/arch/x86/x86_emulate/x86_emulate.c +5 -0
+                 */
+                modrm_mod = 3;
+                break;
+            }
+        }

Just like done by
https://lists.xenproject.org/archives/html/xen-devel/2017-03/msg00588.html
this calls for the outer if/else to be morphed into a switch().

Quoted 2 lines

I did that first, but it was rather complicated to read. I can switch back to that if you'd prefer.

Well, if you don't here then that other patch will in any case.

Jan

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