]> git.karo-electronics.de Git - mv-sheeva.git/blobdiff - arch/x86/kvm/emulate.c
KVM: emulator: Propagate fault in far jump emulation
[mv-sheeva.git] / arch / x86 / kvm / emulate.c
index 9f491bfb00fd9cdc25c097553945040d081546f5..3624f202b440aaf1251d5a6c29684c2a56bbca00 100644 (file)
 #define MemAbs      (1<<11)      /* Memory operand is absolute displacement */
 #define String      (1<<12)     /* String instruction (rep capable) */
 #define Stack       (1<<13)     /* Stack instruction (push/pop) */
+#define GroupMask   (7<<14)     /* Opcode uses one of the group mechanisms */
 #define Group       (1<<14)     /* Bits 3:5 of modrm byte extend opcode */
-#define GroupDual   (1<<15)     /* Alternate decoding of mod == 3 */
-#define Prefix      (1<<16)     /* Instruction varies with 66/f2/f3 prefix */
+#define GroupDual   (2<<14)     /* Alternate decoding of mod == 3 */
+#define Prefix      (3<<14)     /* Instruction varies with 66/f2/f3 prefix */
+#define RMExt       (4<<14)     /* Opcode extension in ModRM r/m if mod == 3 */
 #define Sse         (1<<17)     /* SSE Vector instruction */
-#define RMExt       (1<<18)     /* Opcode extension in ModRM r/m if mod == 3 */
 /* Misc flags */
 #define Prot        (1<<21) /* instruction generates #UD if not in prot-mode */
 #define VendorSpecific (1<<22) /* Vendor specific instruction */
@@ -1448,6 +1449,16 @@ static int emulate_popf(struct x86_emulate_ctxt *ctxt,
        return rc;
 }
 
+static int em_popf(struct x86_emulate_ctxt *ctxt)
+{
+       struct decode_cache *c = &ctxt->decode;
+
+       c->dst.type = OP_REG;
+       c->dst.addr.reg = &ctxt->eflags;
+       c->dst.bytes = c->op_bytes;
+       return emulate_popf(ctxt, ctxt->ops, &c->dst.val, c->op_bytes);
+}
+
 static int emulate_push_sreg(struct x86_emulate_ctxt *ctxt,
                             struct x86_emulate_ops *ops, int seg)
 {
@@ -1473,7 +1484,7 @@ static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt,
        return rc;
 }
 
-static int emulate_pusha(struct x86_emulate_ctxt *ctxt)
+static int em_pusha(struct x86_emulate_ctxt *ctxt)
 {
        struct decode_cache *c = &ctxt->decode;
        unsigned long old_esp = c->regs[VCPU_REGS_RSP];
@@ -1494,8 +1505,15 @@ static int emulate_pusha(struct x86_emulate_ctxt *ctxt)
        return rc;
 }
 
-static int emulate_popa(struct x86_emulate_ctxt *ctxt,
-                       struct x86_emulate_ops *ops)
+static int em_pushf(struct x86_emulate_ctxt *ctxt)
+{
+       struct decode_cache *c = &ctxt->decode;
+
+       c->src.val =  (unsigned long)ctxt->eflags;
+       return em_push(ctxt);
+}
+
+static int em_popa(struct x86_emulate_ctxt *ctxt)
 {
        struct decode_cache *c = &ctxt->decode;
        int rc = X86EMUL_CONTINUE;
@@ -1508,7 +1526,7 @@ static int emulate_popa(struct x86_emulate_ctxt *ctxt,
                        --reg;
                }
 
-               rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes);
+               rc = emulate_pop(ctxt, ctxt->ops, &c->regs[reg], c->op_bytes);
                if (rc != X86EMUL_CONTINUE)
                        break;
                --reg;
@@ -2952,7 +2970,7 @@ static int check_perm_out(struct x86_emulate_ctxt *ctxt)
 #define N    D(0)
 #define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) }
 #define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
-#define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) }
+#define GD(_f, _g) { .flags = ((_f) | GroupDual), .u.gdual = (_g) }
 #define I(_f, _e) { .flags = (_f), .u.execute = (_e) }
 #define II(_f, _e, _i) \
        { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i }
@@ -3098,7 +3116,8 @@ static struct opcode opcode_table[256] = {
        /* 0x58 - 0x5F */
        X8(I(DstReg | Stack, em_pop)),
        /* 0x60 - 0x67 */
-       D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64),
+       I(ImplicitOps | Stack | No64, em_pusha),
+       I(ImplicitOps | Stack | No64, em_popa),
        N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ ,
        N, N, N, N,
        /* 0x68 - 0x6F */
@@ -3126,7 +3145,8 @@ static struct opcode opcode_table[256] = {
        /* 0x98 - 0x9F */
        D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd),
        I(SrcImmFAddr | No64, em_call_far), N,
-       DI(ImplicitOps | Stack, pushf), DI(ImplicitOps | Stack, popf), N, N,
+       II(ImplicitOps | Stack, em_pushf, pushf),
+       II(ImplicitOps | Stack, em_popf, popf), N, N,
        /* 0xA0 - 0xA7 */
        I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov),
        I2bv(DstMem | SrcAcc | Mov | MemAbs, em_mov),
@@ -3318,9 +3338,9 @@ x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
        struct decode_cache *c = &ctxt->decode;
        int rc = X86EMUL_CONTINUE;
        int mode = ctxt->mode;
-       int def_op_bytes, def_ad_bytes, dual, goffset, simd_prefix;
+       int def_op_bytes, def_ad_bytes, goffset, simd_prefix;
        bool op_prefix = false;
-       struct opcode opcode, *g_mod012, *g_mod3;
+       struct opcode opcode;
        struct operand memop = { .type = OP_NONE };
 
        c->eip = ctxt->eip;
@@ -3414,44 +3434,43 @@ done_prefixes:
        }
        c->d = opcode.flags;
 
-       if (c->d & Group) {
-               dual = c->d & GroupDual;
-               c->modrm = insn_fetch(u8, 1, c->eip);
-               --c->eip;
-
-               if (c->d & GroupDual) {
-                       g_mod012 = opcode.u.gdual->mod012;
-                       g_mod3 = opcode.u.gdual->mod3;
-               } else
-                       g_mod012 = g_mod3 = opcode.u.group;
-
-               c->d &= ~(Group | GroupDual);
-
-               goffset = (c->modrm >> 3) & 7;
-
-               if ((c->modrm >> 6) == 3)
-                       opcode = g_mod3[goffset];
-               else
-                       opcode = g_mod012[goffset];
-
-               if (opcode.flags & RMExt) {
+       while (c->d & GroupMask) {
+               switch (c->d & GroupMask) {
+               case Group:
+                       c->modrm = insn_fetch(u8, 1, c->eip);
+                       --c->eip;
+                       goffset = (c->modrm >> 3) & 7;
+                       opcode = opcode.u.group[goffset];
+                       break;
+               case GroupDual:
+                       c->modrm = insn_fetch(u8, 1, c->eip);
+                       --c->eip;
+                       goffset = (c->modrm >> 3) & 7;
+                       if ((c->modrm >> 6) == 3)
+                               opcode = opcode.u.gdual->mod3[goffset];
+                       else
+                               opcode = opcode.u.gdual->mod012[goffset];
+                       break;
+               case RMExt:
                        goffset = c->modrm & 7;
                        opcode = opcode.u.group[goffset];
-               }
-
-               c->d |= opcode.flags;
-       }
-
-       if (c->d & Prefix) {
-               if (c->rep_prefix && op_prefix)
+                       break;
+               case Prefix:
+                       if (c->rep_prefix && op_prefix)
+                               return X86EMUL_UNHANDLEABLE;
+                       simd_prefix = op_prefix ? 0x66 : c->rep_prefix;
+                       switch (simd_prefix) {
+                       case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
+                       case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
+                       case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
+                       case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
+                       }
+                       break;
+               default:
                        return X86EMUL_UNHANDLEABLE;
-               simd_prefix = op_prefix ? 0x66 : c->rep_prefix;
-               switch (simd_prefix) {
-               case 0x00: opcode = opcode.u.gprefix->pfx_no; break;
-               case 0x66: opcode = opcode.u.gprefix->pfx_66; break;
-               case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break;
-               case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break;
                }
+
+               c->d &= ~GroupMask;
                c->d |= opcode.flags;
        }
 
@@ -3823,12 +3842,6 @@ special_insn:
        case 0x48 ... 0x4f: /* dec r16/r32 */
                emulate_1op("dec", c->dst, ctxt->eflags);
                break;
-       case 0x60:      /* pusha */
-               rc = emulate_pusha(ctxt);
-               break;
-       case 0x61:      /* popa */
-               rc = emulate_popa(ctxt, ops);
-               break;
        case 0x63:              /* movsxd */
                if (ctxt->mode != X86EMUL_MODE_PROT64)
                        goto cannot_emulate;
@@ -3906,16 +3919,6 @@ special_insn:
                case 8: c->dst.val = (s32)c->dst.val; break;
                }
                break;
-       case 0x9c: /* pushf */
-               c->src.val =  (unsigned long) ctxt->eflags;
-               rc = em_push(ctxt);
-               break;
-       case 0x9d: /* popf */
-               c->dst.type = OP_REG;
-               c->dst.addr.reg = &ctxt->eflags;
-               c->dst.bytes = c->op_bytes;
-               rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes);
-               break;
        case 0xa8 ... 0xa9:     /* test ax, imm */
                goto test;
        case 0xc0 ... 0xc1:
@@ -3990,7 +3993,8 @@ special_insn:
        jump_far:
                memcpy(&sel, c->src.valptr + c->op_bytes, 2);
 
-               if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
+               rc = load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS);
+               if (rc != X86EMUL_CONTINUE)
                        goto done;
 
                c->eip = 0;