[xen stable-4.8] x86/emul: Fix the handling of unimplemented Grp7 instructions

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[xen stable-4.8] x86/emul: Fix the handling of unimplemented Grp7 instructions

patchbot
commit d623d820c8ab55aca3e555aef3a90ab12f2d6573
Author:     Andrew Cooper <[hidden email]>
AuthorDate: Fri Oct 6 15:05:03 2017 +0200
Commit:     Jan Beulich <[hidden email]>
CommitDate: Fri Oct 6 15:05:03 2017 +0200

    x86/emul: Fix the handling of unimplemented Grp7 instructions
   
    Grp7 is abnormally complicated to decode, even by x86's standards, with
    {s,l}msw being the problematic cases.
   
    Previously, any value which fell through the first switch statement (looking
    for instructions with entirely implicit operands) would be interpreted by the
    second switch statement (handling instructions with memory operands).
   
    Unimplemented instructions would then hit the #UD case for having a non-memory
    operand, rather than taking the cannot_emulate path.
   
    Consolidate the two switch statements into a single one, using ranges to cover
    the instructions with memory operands.
   
    Reported-by: Petre Pircalabu <[hidden email]>
    Signed-off-by: Andrew Cooper <[hidden email]>
    Reviewed-by: Jan Beulich <[hidden email]>
    master commit: 4d3f0fde471e7588ce512eaff1abdab209d8cd4b
    master date: 2017-09-05 12:58:47 +0100
---
 xen/arch/x86/x86_emulate/x86_emulate.c | 53 ++++++++++++++++++++--------------
 1 file changed, 32 insertions(+), 21 deletions(-)

diff --git a/xen/arch/x86/x86_emulate/x86_emulate.c b/xen/arch/x86/x86_emulate/x86_emulate.c
index 3dc6f10..0efad4f 100644
--- a/xen/arch/x86/x86_emulate/x86_emulate.c
+++ b/xen/arch/x86/x86_emulate/x86_emulate.c
@@ -4370,9 +4370,12 @@ x86_emulate(
         }
         break;
 
-    case X86EMUL_OPC(0x0f, 0x01): /* Grp7 */ {
+    case X86EMUL_OPC(0x0f, 0x01): /* Grp7 */
+    {
         unsigned long base, limit, cr0, cr0w;
 
+        seg = (modrm_reg & 1) ? x86_seg_idtr : x86_seg_gdtr;
+
         switch( modrm )
         {
 #ifdef __XEN__
@@ -4388,7 +4391,7 @@ x86_emulate(
                                   handle_xsetbv(_regs._ecx,
                                                 _regs._eax | (_regs.rdx << 32)),
                                   EXC_GP, 0);
-            goto no_writeback;
+            break;
         }
 #endif
 
@@ -4398,7 +4401,7 @@ x86_emulate(
             fail_if(!ops->vmfunc);
             if ( (rc = ops->vmfunc(ctxt)) != X86EMUL_OKAY )
                 goto done;
-            goto no_writeback;
+            break;
 
         case 0xd5: /* xend */
             generate_exception_if(vex.pfx, EXC_UD, -1);
@@ -4412,7 +4415,7 @@ x86_emulate(
                                   EXC_UD, -1);
             /* Neither HLE nor RTM can be active when we get here. */
             _regs.eflags |= EFLG_ZF;
-            goto no_writeback;
+            break;
 
         case 0xdf: /* invlpga */
             generate_exception_if(!in_protmode(ctxt, ops), EXC_UD, -1);
@@ -4421,7 +4424,7 @@ x86_emulate(
             if ( (rc = ops->invlpg(x86_seg_none, truncate_ea(_regs.eax),
                                    ctxt)) )
                 goto done;
-            goto no_writeback;
+            break;
 
         case 0xf9: /* rdtscp */
         {
@@ -4471,17 +4474,17 @@ x86_emulate(
                 base += sizeof(zero);
                 limit -= sizeof(zero);
             }
-            goto no_writeback;
-        }
+            break;
         }
 
-        seg = (modrm_reg & 1) ? x86_seg_idtr : x86_seg_gdtr;
+#define _GRP7(mod, reg) \
+            (((mod) << 6) | ((reg) << 3)) ... (((mod) << 6) | ((reg) << 3) | 7)
+#define GRP7_MEM(reg) _GRP7(0, reg): case _GRP7(1, reg): case _GRP7(2, reg)
+#define GRP7_ALL(reg) GRP7_MEM(reg): case _GRP7(3, reg)
 
-        switch ( modrm_reg & 7 )
-        {
-        case 0: /* sgdt */
-        case 1: /* sidt */
-            generate_exception_if(ea.type != OP_MEM, EXC_UD, -1);
+        case GRP7_MEM(0): /* sgdt */
+        case GRP7_MEM(1): /* sidt */
+            ASSERT(ea.type == OP_MEM);
             generate_exception_if(umip_active(ctxt, ops), EXC_GP, 0);
             fail_if(ops->read_segment == NULL);
             if ( (rc = ops->read_segment(seg, &sreg, ctxt)) )
@@ -4499,10 +4502,11 @@ x86_emulate(
                                   op_bytes, ctxt)) != X86EMUL_OKAY )
                 goto done;
             break;
-        case 2: /* lgdt */
-        case 3: /* lidt */
+
+        case GRP7_MEM(2): /* lgdt */
+        case GRP7_MEM(3): /* lidt */
+            ASSERT(ea.type == OP_MEM);
             generate_exception_if(!mode_ring0(), EXC_GP, 0);
-            generate_exception_if(ea.type != OP_MEM, EXC_UD, -1);
             fail_if(ops->write_segment == NULL);
             memset(&sreg, 0, sizeof(sreg));
             if ( (rc = read_ulong(ea.mem.seg, ea.mem.off+0,
@@ -4518,7 +4522,8 @@ x86_emulate(
             if ( (rc = ops->write_segment(seg, &sreg, ctxt)) )
                 goto done;
             break;
-        case 4: /* smsw */
+
+        case GRP7_ALL(4): /* smsw */
             generate_exception_if(umip_active(ctxt, ops), EXC_GP, 0);
             ea.bytes = (ea.type == OP_MEM) ? 2 : op_bytes;
             dst = ea;
@@ -4527,7 +4532,8 @@ x86_emulate(
                 goto done;
             d |= Mov; /* force writeback */
             break;
-        case 6: /* lmsw */
+
+        case GRP7_ALL(6): /* lmsw */
             fail_if(ops->read_cr == NULL);
             fail_if(ops->write_cr == NULL);
             generate_exception_if(!mode_ring0(), EXC_GP, 0);
@@ -4543,13 +4549,19 @@ x86_emulate(
             if ( (rc = ops->write_cr(0, cr0, ctxt)) )
                 goto done;
             break;
-        case 7: /* invlpg */
+
+        case GRP7_MEM(7): /* invlpg */
+            ASSERT(ea.type == OP_MEM);
             generate_exception_if(!mode_ring0(), EXC_GP, 0);
-            generate_exception_if(ea.type != OP_MEM, EXC_UD, -1);
             fail_if(ops->invlpg == NULL);
             if ( (rc = ops->invlpg(ea.mem.seg, ea.mem.off, ctxt)) )
                 goto done;
             break;
+
+#undef GRP7_ALL
+#undef GRP7_MEM
+#undef _GRP7
+
         default:
             goto cannot_emulate;
         }
@@ -5477,7 +5489,6 @@ x86_emulate(
         break;
     }
 
- no_writeback:
     /* Commit shadow register state. */
     _regs.eflags &= ~EFLG_RF;
 
--
generated by git-patchbot for /home/xen/git/xen.git#stable-4.8

_______________________________________________
Xen-changelog mailing list
[hidden email]
https://lists.xenproject.org/xen-changelog