Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
Avi Kivity | 56e8231 | 2009-08-12 15:04:37 +0300 | [diff] [blame] | 2 | * emulate.c |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3 | * |
| 4 | * Generic x86 (32-bit and 64-bit) instruction decoder and emulator. |
| 5 | * |
| 6 | * Copyright (c) 2005 Keir Fraser |
| 7 | * |
| 8 | * Linux coding style, mod r/m decoder, segment base fixes, real-mode |
Rusty Russell | dcc0766 | 2007-07-17 23:16:56 +1000 | [diff] [blame] | 9 | * privileged instructions: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10 | * |
| 11 | * Copyright (C) 2006 Qumranet |
Avi Kivity | 221d059 | 2010-05-23 18:37:00 +0300 | [diff] [blame] | 12 | * Copyright 2010 Red Hat, Inc. and/or its affilates. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13 | * |
| 14 | * Avi Kivity <avi@qumranet.com> |
| 15 | * Yaniv Kamay <yaniv@qumranet.com> |
| 16 | * |
| 17 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 18 | * the COPYING file in the top-level directory. |
| 19 | * |
| 20 | * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4 |
| 21 | */ |
| 22 | |
| 23 | #ifndef __KERNEL__ |
| 24 | #include <stdio.h> |
| 25 | #include <stdint.h> |
| 26 | #include <public/xen.h> |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 27 | #define DPRINTF(_f, _a ...) printf(_f , ## _a) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 28 | #else |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 29 | #include <linux/kvm_host.h> |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 30 | #include "kvm_cache_regs.h" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 31 | #define DPRINTF(x...) do {} while (0) |
| 32 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 33 | #include <linux/module.h> |
Avi Kivity | 56e8231 | 2009-08-12 15:04:37 +0300 | [diff] [blame] | 34 | #include <asm/kvm_emulate.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 35 | |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 36 | #include "x86.h" |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 37 | #include "tss.h" |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 38 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 39 | /* |
| 40 | * Opcode effective-address decode tables. |
| 41 | * Note that we only emulate instructions that have at least one memory |
| 42 | * operand (excluding implicit stack references). We assume that stack |
| 43 | * references and instruction fetches will never occur in special memory |
| 44 | * areas that require emulation. So, for example, 'mov <imm>,<reg>' need |
| 45 | * not be handled. |
| 46 | */ |
| 47 | |
| 48 | /* Operand sizes: 8-bit operands or specified/overridden size. */ |
Avi Kivity | 2ce4953 | 2010-07-26 14:37:46 +0300 | [diff] [blame] | 49 | #define ByteOp (1<<16) /* 8-bit operands. */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 50 | /* Destination operand type. */ |
Avi Kivity | 2ce4953 | 2010-07-26 14:37:46 +0300 | [diff] [blame] | 51 | #define ImplicitOps (1<<17) /* Implicit in opcode. No generic decode. */ |
| 52 | #define DstReg (2<<17) /* Register operand. */ |
| 53 | #define DstMem (3<<17) /* Memory operand. */ |
| 54 | #define DstAcc (4<<17) /* Destination Accumulator */ |
| 55 | #define DstDI (5<<17) /* Destination is in ES:(E)DI */ |
| 56 | #define DstMem64 (6<<17) /* 64bit memory operand */ |
| 57 | #define DstMask (7<<17) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 58 | /* Source operand type. */ |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 59 | #define SrcNone (0<<4) /* No source operand. */ |
| 60 | #define SrcImplicit (0<<4) /* Source operand is implicit in the opcode. */ |
| 61 | #define SrcReg (1<<4) /* Register operand. */ |
| 62 | #define SrcMem (2<<4) /* Memory operand. */ |
| 63 | #define SrcMem16 (3<<4) /* Memory operand (16-bit). */ |
| 64 | #define SrcMem32 (4<<4) /* Memory operand (32-bit). */ |
| 65 | #define SrcImm (5<<4) /* Immediate operand. */ |
| 66 | #define SrcImmByte (6<<4) /* 8-bit sign-extended immediate operand. */ |
Guillaume Thouvenin | bfcadf8 | 2008-12-04 14:27:38 +0100 | [diff] [blame] | 67 | #define SrcOne (7<<4) /* Implied '1' */ |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 68 | #define SrcImmUByte (8<<4) /* 8-bit unsigned immediate operand. */ |
Avi Kivity | c9eaf20f | 2009-05-18 16:13:45 +0300 | [diff] [blame] | 69 | #define SrcImmU (9<<4) /* Immediate operand, unsigned */ |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 70 | #define SrcSI (0xa<<4) /* Source is in the DS:RSI */ |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 71 | #define SrcImmFAddr (0xb<<4) /* Source is immediate far address */ |
| 72 | #define SrcMemFAddr (0xc<<4) /* Source is far address in memory */ |
Wei Yongjun | 5d55f29 | 2010-07-07 17:43:35 +0800 | [diff] [blame] | 73 | #define SrcAcc (0xd<<4) /* Source Accumulator */ |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 74 | #define SrcMask (0xf<<4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 75 | /* Generic ModRM decode. */ |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 76 | #define ModRM (1<<8) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 77 | /* Destination is only written; never read. */ |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 78 | #define Mov (1<<9) |
| 79 | #define BitOp (1<<10) |
| 80 | #define MemAbs (1<<11) /* Memory operand is absolute displacement */ |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 81 | #define String (1<<12) /* String instruction (rep capable) */ |
| 82 | #define Stack (1<<13) /* Stack instruction (push/pop) */ |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 83 | #define Group (1<<14) /* Bits 3:5 of modrm byte extend opcode */ |
| 84 | #define GroupDual (1<<15) /* Alternate decoding of mod == 3 */ |
Avi Kivity | 2ce4953 | 2010-07-26 14:37:46 +0300 | [diff] [blame] | 85 | #define GroupMask 0x0f /* Group number stored in bits 0:3 */ |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 86 | /* Misc flags */ |
Avi Kivity | 047a481 | 2010-07-26 14:37:47 +0300 | [diff] [blame] | 87 | #define Undefined (1<<25) /* No Such Instruction */ |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 88 | #define Lock (1<<26) /* lock prefix is allowed for the instruction */ |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 89 | #define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */ |
Mohammed Gamal | d8769fe | 2009-08-23 14:24:25 +0300 | [diff] [blame] | 90 | #define No64 (1<<28) |
Guillaume Thouvenin | 0dc8d10 | 2008-12-04 14:26:42 +0100 | [diff] [blame] | 91 | /* Source 2 operand type */ |
| 92 | #define Src2None (0<<29) |
| 93 | #define Src2CL (1<<29) |
| 94 | #define Src2ImmByte (2<<29) |
| 95 | #define Src2One (3<<29) |
| 96 | #define Src2Mask (7<<29) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 97 | |
Avi Kivity | ea9ef04 | 2010-07-29 15:11:34 +0300 | [diff] [blame] | 98 | #define X2(x) x, x |
| 99 | #define X3(x) X2(x), x |
Avi Kivity | 83babbc | 2010-07-26 14:37:39 +0300 | [diff] [blame] | 100 | #define X4(x) X2(x), X2(x) |
Avi Kivity | ea9ef04 | 2010-07-29 15:11:34 +0300 | [diff] [blame] | 101 | #define X5(x) X4(x), x |
Avi Kivity | 83babbc | 2010-07-26 14:37:39 +0300 | [diff] [blame] | 102 | #define X6(x) X4(x), X2(x) |
| 103 | #define X7(x) X4(x), X3(x) |
| 104 | #define X8(x) X4(x), X4(x) |
| 105 | #define X16(x) X8(x), X8(x) |
| 106 | |
Avi Kivity | 43bb19c | 2008-01-18 12:46:50 +0200 | [diff] [blame] | 107 | enum { |
Avi Kivity | 793d5a8 | 2010-07-29 15:11:38 +0300 | [diff] [blame] | 108 | NoGrp, Group1, Group1A, Group3, Group4, Group5, Group7, Group8, Group9, |
Avi Kivity | 43bb19c | 2008-01-18 12:46:50 +0200 | [diff] [blame] | 109 | }; |
| 110 | |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 111 | struct opcode { |
| 112 | u32 flags; |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame^] | 113 | union { |
| 114 | struct opcode *group; |
| 115 | struct group_dual *gdual; |
| 116 | } u; |
| 117 | }; |
| 118 | |
| 119 | struct group_dual { |
| 120 | struct opcode mod012[8]; |
| 121 | struct opcode mod3[8]; |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 122 | }; |
| 123 | |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 124 | #define D(_y) { .flags = (_y) } |
| 125 | #define N D(0) |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame^] | 126 | #define G(_f, _g) { .flags = ((_f) | Group), .u.group = (_g) } |
| 127 | #define GD(_f, _g) { .flags = ((_f) | Group | GroupDual), .u.gdual = (_g) } |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 128 | |
Avi Kivity | 42a1c52 | 2010-07-29 15:11:37 +0300 | [diff] [blame] | 129 | static struct opcode group_table[] = { |
| 130 | [Group1*8] = |
| 131 | X7(D(Lock)), N, |
| 132 | [Group1A*8] = |
| 133 | D(DstMem | SrcNone | ModRM | Mov | Stack), N, N, N, N, N, N, N, |
| 134 | [Group3*8] = |
| 135 | D(DstMem | SrcImm | ModRM), D(DstMem | SrcImm | ModRM), |
| 136 | D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock), |
| 137 | X4(D(Undefined)), |
| 138 | [Group4*8] = |
| 139 | D(ByteOp | DstMem | SrcNone | ModRM | Lock), D(ByteOp | DstMem | SrcNone | ModRM | Lock), |
| 140 | N, N, N, N, N, N, |
| 141 | [Group5*8] = |
| 142 | D(DstMem | SrcNone | ModRM | Lock), D(DstMem | SrcNone | ModRM | Lock), |
| 143 | D(SrcMem | ModRM | Stack), N, |
| 144 | D(SrcMem | ModRM | Stack), D(SrcMemFAddr | ModRM | ImplicitOps), |
| 145 | D(SrcMem | ModRM | Stack), N, |
| 146 | [Group7*8] = |
| 147 | N, N, D(ModRM | SrcMem | Priv), D(ModRM | SrcMem | Priv), |
| 148 | D(SrcNone | ModRM | DstMem | Mov), N, |
| 149 | D(SrcMem16 | ModRM | Mov | Priv), D(SrcMem | ModRM | ByteOp | Priv), |
| 150 | [Group8*8] = |
| 151 | N, N, N, N, |
| 152 | D(DstMem | SrcImmByte | ModRM), D(DstMem | SrcImmByte | ModRM | Lock), |
| 153 | D(DstMem | SrcImmByte | ModRM | Lock), D(DstMem | SrcImmByte | ModRM | Lock), |
| 154 | [Group9*8] = |
| 155 | N, D(DstMem64 | ModRM | Lock), N, N, N, N, N, N, |
| 156 | }; |
| 157 | |
| 158 | static struct opcode group2_table[] = { |
| 159 | [Group7*8] = |
| 160 | D(SrcNone | ModRM | Priv), N, N, D(SrcNone | ModRM | Priv), |
| 161 | D(SrcNone | ModRM | DstMem | Mov), N, |
| 162 | D(SrcMem16 | ModRM | Mov | Priv), N, |
| 163 | [Group9*8] = |
| 164 | N, N, N, N, N, N, N, N, |
| 165 | }; |
| 166 | |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 167 | static struct opcode opcode_table[256] = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 168 | /* 0x00 - 0x07 */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 169 | D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock), |
| 170 | D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM), |
| 171 | D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm), |
| 172 | D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 173 | /* 0x08 - 0x0F */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 174 | D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock), |
| 175 | D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM), |
| 176 | D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm), |
| 177 | D(ImplicitOps | Stack | No64), N, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 178 | /* 0x10 - 0x17 */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 179 | D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock), |
| 180 | D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM), |
| 181 | D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm), |
| 182 | D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 183 | /* 0x18 - 0x1F */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 184 | D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock), |
| 185 | D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM), |
| 186 | D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm), |
| 187 | D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 188 | /* 0x20 - 0x27 */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 189 | D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock), |
| 190 | D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM), |
| 191 | D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 192 | /* 0x28 - 0x2F */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 193 | D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock), |
| 194 | D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM), |
| 195 | D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 196 | /* 0x30 - 0x37 */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 197 | D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock), |
| 198 | D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM), |
| 199 | D(ByteOp | DstAcc | SrcImmByte), D(DstAcc | SrcImm), N, N, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 200 | /* 0x38 - 0x3F */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 201 | D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM), |
| 202 | D(ByteOp | DstReg | SrcMem | ModRM), D(DstReg | SrcMem | ModRM), |
| 203 | D(ByteOp | DstAcc | SrcImm), D(DstAcc | SrcImm), |
| 204 | N, N, |
Avi Kivity | 749358a | 2010-07-26 14:37:40 +0300 | [diff] [blame] | 205 | /* 0x40 - 0x4F */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 206 | X16(D(DstReg)), |
Nitin A Kamble | 7f0aaee | 2007-06-19 11:16:04 +0300 | [diff] [blame] | 207 | /* 0x50 - 0x57 */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 208 | X8(D(SrcReg | Stack)), |
Nitin A Kamble | 7f0aaee | 2007-06-19 11:16:04 +0300 | [diff] [blame] | 209 | /* 0x58 - 0x5F */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 210 | X8(D(DstReg | Stack)), |
Nitin A Kamble | 7d31691 | 2007-08-28 17:58:52 -0700 | [diff] [blame] | 211 | /* 0x60 - 0x67 */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 212 | D(ImplicitOps | Stack | No64), D(ImplicitOps | Stack | No64), |
| 213 | N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ , |
| 214 | N, N, N, N, |
Nitin A Kamble | 7d31691 | 2007-08-28 17:58:52 -0700 | [diff] [blame] | 215 | /* 0x68 - 0x6F */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 216 | D(SrcImm | Mov | Stack), N, D(SrcImmByte | Mov | Stack), N, |
| 217 | D(DstDI | ByteOp | Mov | String), D(DstDI | Mov | String), /* insb, insw/insd */ |
| 218 | D(SrcSI | ByteOp | ImplicitOps | String), D(SrcSI | ImplicitOps | String), /* outsb, outsw/outsd */ |
Avi Kivity | b3ab340 | 2010-07-26 14:37:42 +0300 | [diff] [blame] | 219 | /* 0x70 - 0x7F */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 220 | X16(D(SrcImmByte)), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 221 | /* 0x80 - 0x87 */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 222 | D(ByteOp | DstMem | SrcImm | ModRM | Group | Group1), |
| 223 | D(DstMem | SrcImm | ModRM | Group | Group1), |
| 224 | D(ByteOp | DstMem | SrcImm | ModRM | No64 | Group | Group1), |
| 225 | D(DstMem | SrcImmByte | ModRM | Group | Group1), |
| 226 | D(ByteOp | DstMem | SrcReg | ModRM), D(DstMem | SrcReg | ModRM), |
| 227 | D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 228 | /* 0x88 - 0x8F */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 229 | D(ByteOp | DstMem | SrcReg | ModRM | Mov), D(DstMem | SrcReg | ModRM | Mov), |
| 230 | D(ByteOp | DstReg | SrcMem | ModRM | Mov), D(DstReg | SrcMem | ModRM | Mov), |
| 231 | D(DstMem | SrcNone | ModRM | Mov), D(ModRM | DstReg), |
| 232 | D(ImplicitOps | SrcMem16 | ModRM), D(Group | Group1A), |
Mohammed Gamal | b13354f | 2008-06-15 19:37:38 +0300 | [diff] [blame] | 233 | /* 0x90 - 0x97 */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 234 | D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg), D(DstReg), |
Mohammed Gamal | b13354f | 2008-06-15 19:37:38 +0300 | [diff] [blame] | 235 | /* 0x98 - 0x9F */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 236 | N, N, D(SrcImmFAddr | No64), N, |
| 237 | D(ImplicitOps | Stack), D(ImplicitOps | Stack), N, N, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 238 | /* 0xA0 - 0xA7 */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 239 | D(ByteOp | DstAcc | SrcMem | Mov | MemAbs), D(DstAcc | SrcMem | Mov | MemAbs), |
| 240 | D(ByteOp | DstMem | SrcAcc | Mov | MemAbs), D(DstMem | SrcAcc | Mov | MemAbs), |
| 241 | D(ByteOp | SrcSI | DstDI | Mov | String), D(SrcSI | DstDI | Mov | String), |
| 242 | D(ByteOp | SrcSI | DstDI | String), D(SrcSI | DstDI | String), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 243 | /* 0xA8 - 0xAF */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 244 | D(DstAcc | SrcImmByte | ByteOp), D(DstAcc | SrcImm), D(ByteOp | DstDI | Mov | String), D(DstDI | Mov | String), |
| 245 | D(ByteOp | SrcSI | DstAcc | Mov | String), D(SrcSI | DstAcc | Mov | String), |
| 246 | D(ByteOp | DstDI | String), D(DstDI | String), |
Mohammed Gamal | a5e2e82 | 2008-08-27 05:02:56 +0300 | [diff] [blame] | 247 | /* 0xB0 - 0xB7 */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 248 | X8(D(ByteOp | DstReg | SrcImm | Mov)), |
Mohammed Gamal | a5e2e82 | 2008-08-27 05:02:56 +0300 | [diff] [blame] | 249 | /* 0xB8 - 0xBF */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 250 | X8(D(DstReg | SrcImm | Mov)), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 251 | /* 0xC0 - 0xC7 */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 252 | D(ByteOp | DstMem | SrcImm | ModRM), D(DstMem | SrcImmByte | ModRM), |
| 253 | N, D(ImplicitOps | Stack), N, N, |
| 254 | D(ByteOp | DstMem | SrcImm | ModRM | Mov), D(DstMem | SrcImm | ModRM | Mov), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 255 | /* 0xC8 - 0xCF */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 256 | N, N, N, D(ImplicitOps | Stack), |
| 257 | D(ImplicitOps), D(SrcImmByte), D(ImplicitOps | No64), D(ImplicitOps), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 258 | /* 0xD0 - 0xD7 */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 259 | D(ByteOp | DstMem | SrcImplicit | ModRM), D(DstMem | SrcImplicit | ModRM), |
| 260 | D(ByteOp | DstMem | SrcImplicit | ModRM), D(DstMem | SrcImplicit | ModRM), |
| 261 | N, N, N, N, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 262 | /* 0xD8 - 0xDF */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 263 | N, N, N, N, N, N, N, N, |
Nitin A Kamble | 098c937 | 2007-08-19 11:00:36 +0300 | [diff] [blame] | 264 | /* 0xE0 - 0xE7 */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 265 | N, N, N, N, |
| 266 | D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc), |
| 267 | D(ByteOp | SrcImmUByte | DstAcc), D(SrcImmUByte | DstAcc), |
Nitin A Kamble | 098c937 | 2007-08-19 11:00:36 +0300 | [diff] [blame] | 268 | /* 0xE8 - 0xEF */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 269 | D(SrcImm | Stack), D(SrcImm | ImplicitOps), |
| 270 | D(SrcImmFAddr | No64), D(SrcImmByte | ImplicitOps), |
| 271 | D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc), |
| 272 | D(SrcNone | ByteOp | DstAcc), D(SrcNone | DstAcc), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 273 | /* 0xF0 - 0xF7 */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 274 | N, N, N, N, |
| 275 | D(ImplicitOps | Priv), D(ImplicitOps), D(ByteOp | Group | Group3), D(Group | Group3), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 276 | /* 0xF8 - 0xFF */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 277 | D(ImplicitOps), N, D(ImplicitOps), D(ImplicitOps), |
| 278 | D(ImplicitOps), D(ImplicitOps), D(Group | Group4), D(Group | Group5), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 279 | }; |
| 280 | |
Avi Kivity | d65b1de | 2010-07-29 15:11:35 +0300 | [diff] [blame] | 281 | static struct opcode twobyte_table[256] = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 282 | /* 0x00 - 0x0F */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 283 | N, D(Group | GroupDual | Group7), N, N, |
| 284 | N, D(ImplicitOps), D(ImplicitOps | Priv), N, |
| 285 | D(ImplicitOps | Priv), D(ImplicitOps | Priv), N, N, |
| 286 | N, D(ImplicitOps | ModRM), N, N, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 287 | /* 0x10 - 0x1F */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 288 | N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 289 | /* 0x20 - 0x2F */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 290 | D(ModRM | ImplicitOps | Priv), D(ModRM | Priv), |
| 291 | D(ModRM | ImplicitOps | Priv), D(ModRM | Priv), |
| 292 | N, N, N, N, |
| 293 | N, N, N, N, N, N, N, N, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 294 | /* 0x30 - 0x3F */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 295 | D(ImplicitOps | Priv), N, D(ImplicitOps | Priv), N, |
| 296 | D(ImplicitOps), D(ImplicitOps | Priv), N, N, |
| 297 | N, N, N, N, N, N, N, N, |
Avi Kivity | be8eacd | 2010-07-26 14:37:44 +0300 | [diff] [blame] | 298 | /* 0x40 - 0x4F */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 299 | X16(D(DstReg | SrcMem | ModRM | Mov)), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 300 | /* 0x50 - 0x5F */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 301 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 302 | /* 0x60 - 0x6F */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 303 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 304 | /* 0x70 - 0x7F */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 305 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 306 | /* 0x80 - 0x8F */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 307 | X16(D(SrcImm)), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 308 | /* 0x90 - 0x9F */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 309 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 310 | /* 0xA0 - 0xA7 */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 311 | D(ImplicitOps | Stack), D(ImplicitOps | Stack), |
| 312 | N, D(DstMem | SrcReg | ModRM | BitOp), |
| 313 | D(DstMem | SrcReg | Src2ImmByte | ModRM), |
| 314 | D(DstMem | SrcReg | Src2CL | ModRM), N, N, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 315 | /* 0xA8 - 0xAF */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 316 | D(ImplicitOps | Stack), D(ImplicitOps | Stack), |
| 317 | N, D(DstMem | SrcReg | ModRM | BitOp | Lock), |
| 318 | D(DstMem | SrcReg | Src2ImmByte | ModRM), |
| 319 | D(DstMem | SrcReg | Src2CL | ModRM), |
| 320 | D(ModRM), N, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 321 | /* 0xB0 - 0xB7 */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 322 | D(ByteOp | DstMem | SrcReg | ModRM | Lock), D(DstMem | SrcReg | ModRM | Lock), |
| 323 | N, D(DstMem | SrcReg | ModRM | BitOp | Lock), |
| 324 | N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov), |
| 325 | D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 326 | /* 0xB8 - 0xBF */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 327 | N, N, |
| 328 | D(Group | Group8), D(DstMem | SrcReg | ModRM | BitOp | Lock), |
| 329 | N, N, D(ByteOp | DstReg | SrcMem | ModRM | Mov), |
| 330 | D(DstReg | SrcMem16 | ModRM | Mov), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 331 | /* 0xC0 - 0xCF */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 332 | N, N, N, D(DstMem | SrcReg | ModRM | Mov), |
| 333 | N, N, N, D(Group | GroupDual | Group9), |
| 334 | N, N, N, N, N, N, N, N, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 335 | /* 0xD0 - 0xDF */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 336 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 337 | /* 0xE0 - 0xEF */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 338 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 339 | /* 0xF0 - 0xFF */ |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 340 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 341 | }; |
| 342 | |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 343 | #undef D |
| 344 | #undef N |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame^] | 345 | #undef G |
| 346 | #undef GD |
Avi Kivity | fd85331 | 2010-07-29 15:11:36 +0300 | [diff] [blame] | 347 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 348 | /* EFLAGS bit definitions. */ |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 349 | #define EFLG_ID (1<<21) |
| 350 | #define EFLG_VIP (1<<20) |
| 351 | #define EFLG_VIF (1<<19) |
| 352 | #define EFLG_AC (1<<18) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 353 | #define EFLG_VM (1<<17) |
| 354 | #define EFLG_RF (1<<16) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 355 | #define EFLG_IOPL (3<<12) |
| 356 | #define EFLG_NT (1<<14) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 357 | #define EFLG_OF (1<<11) |
| 358 | #define EFLG_DF (1<<10) |
Andre Przywara | b1d8614 | 2009-06-17 15:50:32 +0200 | [diff] [blame] | 359 | #define EFLG_IF (1<<9) |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 360 | #define EFLG_TF (1<<8) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 361 | #define EFLG_SF (1<<7) |
| 362 | #define EFLG_ZF (1<<6) |
| 363 | #define EFLG_AF (1<<4) |
| 364 | #define EFLG_PF (1<<2) |
| 365 | #define EFLG_CF (1<<0) |
| 366 | |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 367 | #define EFLG_RESERVED_ZEROS_MASK 0xffc0802a |
| 368 | #define EFLG_RESERVED_ONE_MASK 2 |
| 369 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 370 | /* |
| 371 | * Instruction emulation: |
| 372 | * Most instructions are emulated directly via a fragment of inline assembly |
| 373 | * code. This allows us to save/restore EFLAGS and thus very easily pick up |
| 374 | * any modified flags. |
| 375 | */ |
| 376 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 377 | #if defined(CONFIG_X86_64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 378 | #define _LO32 "k" /* force 32-bit operand */ |
| 379 | #define _STK "%%rsp" /* stack pointer */ |
| 380 | #elif defined(__i386__) |
| 381 | #define _LO32 "" /* force 32-bit operand */ |
| 382 | #define _STK "%%esp" /* stack pointer */ |
| 383 | #endif |
| 384 | |
| 385 | /* |
| 386 | * These EFLAGS bits are restored from saved value during emulation, and |
| 387 | * any changes are written back to the saved value after emulation. |
| 388 | */ |
| 389 | #define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF) |
| 390 | |
| 391 | /* Before executing instruction: restore necessary bits in EFLAGS. */ |
Avi Kivity | e934c9c | 2007-12-06 16:15:02 +0200 | [diff] [blame] | 392 | #define _PRE_EFLAGS(_sav, _msk, _tmp) \ |
| 393 | /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \ |
| 394 | "movl %"_sav",%"_LO32 _tmp"; " \ |
| 395 | "push %"_tmp"; " \ |
| 396 | "push %"_tmp"; " \ |
| 397 | "movl %"_msk",%"_LO32 _tmp"; " \ |
| 398 | "andl %"_LO32 _tmp",("_STK"); " \ |
| 399 | "pushf; " \ |
| 400 | "notl %"_LO32 _tmp"; " \ |
| 401 | "andl %"_LO32 _tmp",("_STK"); " \ |
| 402 | "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \ |
| 403 | "pop %"_tmp"; " \ |
| 404 | "orl %"_LO32 _tmp",("_STK"); " \ |
| 405 | "popf; " \ |
| 406 | "pop %"_sav"; " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 407 | |
| 408 | /* After executing instruction: write-back necessary bits in EFLAGS. */ |
| 409 | #define _POST_EFLAGS(_sav, _msk, _tmp) \ |
| 410 | /* _sav |= EFLAGS & _msk; */ \ |
| 411 | "pushf; " \ |
| 412 | "pop %"_tmp"; " \ |
| 413 | "andl %"_msk",%"_LO32 _tmp"; " \ |
| 414 | "orl %"_LO32 _tmp",%"_sav"; " |
| 415 | |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 416 | #ifdef CONFIG_X86_64 |
| 417 | #define ON64(x) x |
| 418 | #else |
| 419 | #define ON64(x) |
| 420 | #endif |
| 421 | |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 422 | #define ____emulate_2op(_op, _src, _dst, _eflags, _x, _y, _suffix) \ |
| 423 | do { \ |
| 424 | __asm__ __volatile__ ( \ |
| 425 | _PRE_EFLAGS("0", "4", "2") \ |
| 426 | _op _suffix " %"_x"3,%1; " \ |
| 427 | _POST_EFLAGS("0", "4", "2") \ |
| 428 | : "=m" (_eflags), "=m" ((_dst).val), \ |
| 429 | "=&r" (_tmp) \ |
| 430 | : _y ((_src).val), "i" (EFLAGS_MASK)); \ |
Avi Kivity | f3fd92f | 2008-11-29 20:38:12 +0200 | [diff] [blame] | 431 | } while (0) |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 432 | |
| 433 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 434 | /* Raw emulation: instruction has two explicit operands. */ |
| 435 | #define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 436 | do { \ |
| 437 | unsigned long _tmp; \ |
| 438 | \ |
| 439 | switch ((_dst).bytes) { \ |
| 440 | case 2: \ |
| 441 | ____emulate_2op(_op,_src,_dst,_eflags,_wx,_wy,"w"); \ |
| 442 | break; \ |
| 443 | case 4: \ |
| 444 | ____emulate_2op(_op,_src,_dst,_eflags,_lx,_ly,"l"); \ |
| 445 | break; \ |
| 446 | case 8: \ |
| 447 | ON64(____emulate_2op(_op,_src,_dst,_eflags,_qx,_qy,"q")); \ |
| 448 | break; \ |
| 449 | } \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 450 | } while (0) |
| 451 | |
| 452 | #define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \ |
| 453 | do { \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 454 | unsigned long _tmp; \ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 455 | switch ((_dst).bytes) { \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 456 | case 1: \ |
Avi Kivity | 6b7ad61 | 2008-11-26 15:30:45 +0200 | [diff] [blame] | 457 | ____emulate_2op(_op,_src,_dst,_eflags,_bx,_by,"b"); \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 458 | break; \ |
| 459 | default: \ |
| 460 | __emulate_2op_nobyte(_op, _src, _dst, _eflags, \ |
| 461 | _wx, _wy, _lx, _ly, _qx, _qy); \ |
| 462 | break; \ |
| 463 | } \ |
| 464 | } while (0) |
| 465 | |
| 466 | /* Source operand is byte-sized and may be restricted to just %cl. */ |
| 467 | #define emulate_2op_SrcB(_op, _src, _dst, _eflags) \ |
| 468 | __emulate_2op(_op, _src, _dst, _eflags, \ |
| 469 | "b", "c", "b", "c", "b", "c", "b", "c") |
| 470 | |
| 471 | /* Source operand is byte, word, long or quad sized. */ |
| 472 | #define emulate_2op_SrcV(_op, _src, _dst, _eflags) \ |
| 473 | __emulate_2op(_op, _src, _dst, _eflags, \ |
| 474 | "b", "q", "w", "r", _LO32, "r", "", "r") |
| 475 | |
| 476 | /* Source operand is word, long or quad sized. */ |
| 477 | #define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \ |
| 478 | __emulate_2op_nobyte(_op, _src, _dst, _eflags, \ |
| 479 | "w", "r", _LO32, "r", "", "r") |
| 480 | |
Guillaume Thouvenin | d175226 | 2008-12-04 14:29:00 +0100 | [diff] [blame] | 481 | /* Instruction has three operands and one operand is stored in ECX register */ |
| 482 | #define __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, _suffix, _type) \ |
| 483 | do { \ |
| 484 | unsigned long _tmp; \ |
| 485 | _type _clv = (_cl).val; \ |
| 486 | _type _srcv = (_src).val; \ |
| 487 | _type _dstv = (_dst).val; \ |
| 488 | \ |
| 489 | __asm__ __volatile__ ( \ |
| 490 | _PRE_EFLAGS("0", "5", "2") \ |
| 491 | _op _suffix " %4,%1 \n" \ |
| 492 | _POST_EFLAGS("0", "5", "2") \ |
| 493 | : "=m" (_eflags), "+r" (_dstv), "=&r" (_tmp) \ |
| 494 | : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \ |
| 495 | ); \ |
| 496 | \ |
| 497 | (_cl).val = (unsigned long) _clv; \ |
| 498 | (_src).val = (unsigned long) _srcv; \ |
| 499 | (_dst).val = (unsigned long) _dstv; \ |
| 500 | } while (0) |
| 501 | |
| 502 | #define emulate_2op_cl(_op, _cl, _src, _dst, _eflags) \ |
| 503 | do { \ |
| 504 | switch ((_dst).bytes) { \ |
| 505 | case 2: \ |
| 506 | __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \ |
| 507 | "w", unsigned short); \ |
| 508 | break; \ |
| 509 | case 4: \ |
| 510 | __emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \ |
| 511 | "l", unsigned int); \ |
| 512 | break; \ |
| 513 | case 8: \ |
| 514 | ON64(__emulate_2op_cl(_op, _cl, _src, _dst, _eflags, \ |
| 515 | "q", unsigned long)); \ |
| 516 | break; \ |
| 517 | } \ |
| 518 | } while (0) |
| 519 | |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 520 | #define __emulate_1op(_op, _dst, _eflags, _suffix) \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 521 | do { \ |
| 522 | unsigned long _tmp; \ |
| 523 | \ |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 524 | __asm__ __volatile__ ( \ |
| 525 | _PRE_EFLAGS("0", "3", "2") \ |
| 526 | _op _suffix " %1; " \ |
| 527 | _POST_EFLAGS("0", "3", "2") \ |
| 528 | : "=m" (_eflags), "+m" ((_dst).val), \ |
| 529 | "=&r" (_tmp) \ |
| 530 | : "i" (EFLAGS_MASK)); \ |
| 531 | } while (0) |
| 532 | |
| 533 | /* Instruction has only one explicit operand (no source operand). */ |
| 534 | #define emulate_1op(_op, _dst, _eflags) \ |
| 535 | do { \ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 536 | switch ((_dst).bytes) { \ |
Avi Kivity | dda96d8 | 2008-11-26 15:14:10 +0200 | [diff] [blame] | 537 | case 1: __emulate_1op(_op, _dst, _eflags, "b"); break; \ |
| 538 | case 2: __emulate_1op(_op, _dst, _eflags, "w"); break; \ |
| 539 | case 4: __emulate_1op(_op, _dst, _eflags, "l"); break; \ |
| 540 | case 8: ON64(__emulate_1op(_op, _dst, _eflags, "q")); break; \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 541 | } \ |
| 542 | } while (0) |
| 543 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 544 | /* Fetch next part of the instruction being emulated. */ |
| 545 | #define insn_fetch(_type, _size, _eip) \ |
| 546 | ({ unsigned long _x; \ |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 547 | rc = do_insn_fetch(ctxt, ops, (_eip), &_x, (_size)); \ |
Gleb Natapov | af5b4f7 | 2010-03-15 16:38:30 +0200 | [diff] [blame] | 548 | if (rc != X86EMUL_CONTINUE) \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 549 | goto done; \ |
| 550 | (_eip) += (_size); \ |
| 551 | (_type)_x; \ |
| 552 | }) |
| 553 | |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 554 | #define insn_fetch_arr(_arr, _size, _eip) \ |
| 555 | ({ rc = do_insn_fetch(ctxt, ops, (_eip), _arr, (_size)); \ |
| 556 | if (rc != X86EMUL_CONTINUE) \ |
| 557 | goto done; \ |
| 558 | (_eip) += (_size); \ |
| 559 | }) |
| 560 | |
Harvey Harrison | ddcb288 | 2008-02-18 11:12:48 -0800 | [diff] [blame] | 561 | static inline unsigned long ad_mask(struct decode_cache *c) |
| 562 | { |
| 563 | return (1UL << (c->ad_bytes << 3)) - 1; |
| 564 | } |
| 565 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 566 | /* Access/update address held in a register, based on addressing mode. */ |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 567 | static inline unsigned long |
| 568 | address_mask(struct decode_cache *c, unsigned long reg) |
| 569 | { |
| 570 | if (c->ad_bytes == sizeof(unsigned long)) |
| 571 | return reg; |
| 572 | else |
| 573 | return reg & ad_mask(c); |
| 574 | } |
| 575 | |
| 576 | static inline unsigned long |
| 577 | register_address(struct decode_cache *c, unsigned long base, unsigned long reg) |
| 578 | { |
| 579 | return base + address_mask(c, reg); |
| 580 | } |
| 581 | |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 582 | static inline void |
| 583 | register_address_increment(struct decode_cache *c, unsigned long *reg, int inc) |
| 584 | { |
| 585 | if (c->ad_bytes == sizeof(unsigned long)) |
| 586 | *reg += inc; |
| 587 | else |
| 588 | *reg = (*reg & ~ad_mask(c)) | ((*reg + inc) & ad_mask(c)); |
| 589 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 590 | |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 591 | static inline void jmp_rel(struct decode_cache *c, int rel) |
| 592 | { |
| 593 | register_address_increment(c, &c->eip, rel); |
| 594 | } |
Nitin A Kamble | 098c937 | 2007-08-19 11:00:36 +0300 | [diff] [blame] | 595 | |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 596 | static void set_seg_override(struct decode_cache *c, int seg) |
| 597 | { |
| 598 | c->has_seg_override = true; |
| 599 | c->seg_override = seg; |
| 600 | } |
| 601 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 602 | static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, |
| 603 | struct x86_emulate_ops *ops, int seg) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 604 | { |
| 605 | if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS) |
| 606 | return 0; |
| 607 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 608 | return ops->get_cached_segment_base(seg, ctxt->vcpu); |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 612 | struct x86_emulate_ops *ops, |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 613 | struct decode_cache *c) |
| 614 | { |
| 615 | if (!c->has_seg_override) |
| 616 | return 0; |
| 617 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 618 | return seg_base(ctxt, ops, c->seg_override); |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 619 | } |
| 620 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 621 | static unsigned long es_base(struct x86_emulate_ctxt *ctxt, |
| 622 | struct x86_emulate_ops *ops) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 623 | { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 624 | return seg_base(ctxt, ops, VCPU_SREG_ES); |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 625 | } |
| 626 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 627 | static unsigned long ss_base(struct x86_emulate_ctxt *ctxt, |
| 628 | struct x86_emulate_ops *ops) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 629 | { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 630 | return seg_base(ctxt, ops, VCPU_SREG_SS); |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 631 | } |
| 632 | |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 633 | static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec, |
| 634 | u32 error, bool valid) |
| 635 | { |
| 636 | ctxt->exception = vec; |
| 637 | ctxt->error_code = error; |
| 638 | ctxt->error_code_valid = valid; |
| 639 | ctxt->restart = false; |
| 640 | } |
| 641 | |
| 642 | static void emulate_gp(struct x86_emulate_ctxt *ctxt, int err) |
| 643 | { |
| 644 | emulate_exception(ctxt, GP_VECTOR, err, true); |
| 645 | } |
| 646 | |
| 647 | static void emulate_pf(struct x86_emulate_ctxt *ctxt, unsigned long addr, |
| 648 | int err) |
| 649 | { |
| 650 | ctxt->cr2 = addr; |
| 651 | emulate_exception(ctxt, PF_VECTOR, err, true); |
| 652 | } |
| 653 | |
| 654 | static void emulate_ud(struct x86_emulate_ctxt *ctxt) |
| 655 | { |
| 656 | emulate_exception(ctxt, UD_VECTOR, 0, false); |
| 657 | } |
| 658 | |
| 659 | static void emulate_ts(struct x86_emulate_ctxt *ctxt, int err) |
| 660 | { |
| 661 | emulate_exception(ctxt, TS_VECTOR, err, true); |
| 662 | } |
| 663 | |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 664 | static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt, |
| 665 | struct x86_emulate_ops *ops, |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 666 | unsigned long eip, u8 *dest) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 667 | { |
| 668 | struct fetch_cache *fc = &ctxt->decode.fetch; |
| 669 | int rc; |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 670 | int size, cur_size; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 671 | |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 672 | if (eip == fc->end) { |
| 673 | cur_size = fc->end - fc->start; |
| 674 | size = min(15UL - cur_size, PAGE_SIZE - offset_in_page(eip)); |
| 675 | rc = ops->fetch(ctxt->cs_base + eip, fc->data + cur_size, |
| 676 | size, ctxt->vcpu, NULL); |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 677 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 678 | return rc; |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 679 | fc->end += size; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 680 | } |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 681 | *dest = fc->data[eip - fc->start]; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 682 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | static int do_insn_fetch(struct x86_emulate_ctxt *ctxt, |
| 686 | struct x86_emulate_ops *ops, |
| 687 | unsigned long eip, void *dest, unsigned size) |
| 688 | { |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 689 | int rc; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 690 | |
Avi Kivity | eb3c79e | 2009-11-24 15:20:15 +0200 | [diff] [blame] | 691 | /* x86 instructions are limited to 15 bytes. */ |
Gleb Natapov | 063db06 | 2010-03-18 15:20:06 +0200 | [diff] [blame] | 692 | if (eip + size - ctxt->eip > 15) |
Avi Kivity | eb3c79e | 2009-11-24 15:20:15 +0200 | [diff] [blame] | 693 | return X86EMUL_UNHANDLEABLE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 694 | while (size--) { |
| 695 | rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++); |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 696 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 697 | return rc; |
| 698 | } |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 699 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 700 | } |
| 701 | |
Rusty Russell | 1e3c5cb | 2007-07-17 23:16:11 +1000 | [diff] [blame] | 702 | /* |
| 703 | * Given the 'reg' portion of a ModRM byte, and a register block, return a |
| 704 | * pointer into the block that addresses the relevant register. |
| 705 | * @highbyte_regs specifies whether to decode AH,CH,DH,BH. |
| 706 | */ |
| 707 | static void *decode_register(u8 modrm_reg, unsigned long *regs, |
| 708 | int highbyte_regs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 709 | { |
| 710 | void *p; |
| 711 | |
| 712 | p = ®s[modrm_reg]; |
| 713 | if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8) |
| 714 | p = (unsigned char *)®s[modrm_reg & 3] + 1; |
| 715 | return p; |
| 716 | } |
| 717 | |
| 718 | static int read_descriptor(struct x86_emulate_ctxt *ctxt, |
| 719 | struct x86_emulate_ops *ops, |
| 720 | void *ptr, |
| 721 | u16 *size, unsigned long *address, int op_bytes) |
| 722 | { |
| 723 | int rc; |
| 724 | |
| 725 | if (op_bytes == 2) |
| 726 | op_bytes = 3; |
| 727 | *address = 0; |
Laurent Vivier | cebff02 | 2007-07-30 13:35:24 +0300 | [diff] [blame] | 728 | rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2, |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 729 | ctxt->vcpu, NULL); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 730 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 731 | return rc; |
Laurent Vivier | cebff02 | 2007-07-30 13:35:24 +0300 | [diff] [blame] | 732 | rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes, |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 733 | ctxt->vcpu, NULL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 734 | return rc; |
| 735 | } |
| 736 | |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 737 | static int test_cc(unsigned int condition, unsigned int flags) |
| 738 | { |
| 739 | int rc = 0; |
| 740 | |
| 741 | switch ((condition & 15) >> 1) { |
| 742 | case 0: /* o */ |
| 743 | rc |= (flags & EFLG_OF); |
| 744 | break; |
| 745 | case 1: /* b/c/nae */ |
| 746 | rc |= (flags & EFLG_CF); |
| 747 | break; |
| 748 | case 2: /* z/e */ |
| 749 | rc |= (flags & EFLG_ZF); |
| 750 | break; |
| 751 | case 3: /* be/na */ |
| 752 | rc |= (flags & (EFLG_CF|EFLG_ZF)); |
| 753 | break; |
| 754 | case 4: /* s */ |
| 755 | rc |= (flags & EFLG_SF); |
| 756 | break; |
| 757 | case 5: /* p/pe */ |
| 758 | rc |= (flags & EFLG_PF); |
| 759 | break; |
| 760 | case 7: /* le/ng */ |
| 761 | rc |= (flags & EFLG_ZF); |
| 762 | /* fall through */ |
| 763 | case 6: /* l/nge */ |
| 764 | rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF)); |
| 765 | break; |
| 766 | } |
| 767 | |
| 768 | /* Odd condition identifiers (lsb == 1) have inverted sense. */ |
| 769 | return (!!rc ^ (condition & 1)); |
| 770 | } |
| 771 | |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 772 | static void decode_register_operand(struct operand *op, |
| 773 | struct decode_cache *c, |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 774 | int inhibit_bytereg) |
| 775 | { |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 776 | unsigned reg = c->modrm_reg; |
Avi Kivity | 9f1ef3f | 2007-10-31 11:21:06 +0200 | [diff] [blame] | 777 | int highbyte_regs = c->rex_prefix == 0; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 778 | |
| 779 | if (!(c->d & ModRM)) |
| 780 | reg = (c->b & 7) | ((c->rex_prefix & 1) << 3); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 781 | op->type = OP_REG; |
| 782 | if ((c->d & ByteOp) && !inhibit_bytereg) { |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 783 | op->ptr = decode_register(reg, c->regs, highbyte_regs); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 784 | op->val = *(u8 *)op->ptr; |
| 785 | op->bytes = 1; |
| 786 | } else { |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 787 | op->ptr = decode_register(reg, c->regs, 0); |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 788 | op->bytes = c->op_bytes; |
| 789 | switch (op->bytes) { |
| 790 | case 2: |
| 791 | op->val = *(u16 *)op->ptr; |
| 792 | break; |
| 793 | case 4: |
| 794 | op->val = *(u32 *)op->ptr; |
| 795 | break; |
| 796 | case 8: |
| 797 | op->val = *(u64 *) op->ptr; |
| 798 | break; |
| 799 | } |
| 800 | } |
| 801 | op->orig_val = op->val; |
| 802 | } |
| 803 | |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 804 | static int decode_modrm(struct x86_emulate_ctxt *ctxt, |
| 805 | struct x86_emulate_ops *ops) |
| 806 | { |
| 807 | struct decode_cache *c = &ctxt->decode; |
| 808 | u8 sib; |
Avi Kivity | f5b4edc | 2008-06-15 22:09:11 -0700 | [diff] [blame] | 809 | int index_reg = 0, base_reg = 0, scale; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 810 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 811 | |
| 812 | if (c->rex_prefix) { |
| 813 | c->modrm_reg = (c->rex_prefix & 4) << 1; /* REX.R */ |
| 814 | index_reg = (c->rex_prefix & 2) << 2; /* REX.X */ |
| 815 | c->modrm_rm = base_reg = (c->rex_prefix & 1) << 3; /* REG.B */ |
| 816 | } |
| 817 | |
| 818 | c->modrm = insn_fetch(u8, 1, c->eip); |
| 819 | c->modrm_mod |= (c->modrm & 0xc0) >> 6; |
| 820 | c->modrm_reg |= (c->modrm & 0x38) >> 3; |
| 821 | c->modrm_rm |= (c->modrm & 0x07); |
| 822 | c->modrm_ea = 0; |
| 823 | c->use_modrm_ea = 1; |
| 824 | |
| 825 | if (c->modrm_mod == 3) { |
Avi Kivity | 107d6d2 | 2008-05-05 14:58:26 +0300 | [diff] [blame] | 826 | c->modrm_ptr = decode_register(c->modrm_rm, |
| 827 | c->regs, c->d & ByteOp); |
| 828 | c->modrm_val = *(unsigned long *)c->modrm_ptr; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 829 | return rc; |
| 830 | } |
| 831 | |
| 832 | if (c->ad_bytes == 2) { |
| 833 | unsigned bx = c->regs[VCPU_REGS_RBX]; |
| 834 | unsigned bp = c->regs[VCPU_REGS_RBP]; |
| 835 | unsigned si = c->regs[VCPU_REGS_RSI]; |
| 836 | unsigned di = c->regs[VCPU_REGS_RDI]; |
| 837 | |
| 838 | /* 16-bit ModR/M decode. */ |
| 839 | switch (c->modrm_mod) { |
| 840 | case 0: |
| 841 | if (c->modrm_rm == 6) |
| 842 | c->modrm_ea += insn_fetch(u16, 2, c->eip); |
| 843 | break; |
| 844 | case 1: |
| 845 | c->modrm_ea += insn_fetch(s8, 1, c->eip); |
| 846 | break; |
| 847 | case 2: |
| 848 | c->modrm_ea += insn_fetch(u16, 2, c->eip); |
| 849 | break; |
| 850 | } |
| 851 | switch (c->modrm_rm) { |
| 852 | case 0: |
| 853 | c->modrm_ea += bx + si; |
| 854 | break; |
| 855 | case 1: |
| 856 | c->modrm_ea += bx + di; |
| 857 | break; |
| 858 | case 2: |
| 859 | c->modrm_ea += bp + si; |
| 860 | break; |
| 861 | case 3: |
| 862 | c->modrm_ea += bp + di; |
| 863 | break; |
| 864 | case 4: |
| 865 | c->modrm_ea += si; |
| 866 | break; |
| 867 | case 5: |
| 868 | c->modrm_ea += di; |
| 869 | break; |
| 870 | case 6: |
| 871 | if (c->modrm_mod != 0) |
| 872 | c->modrm_ea += bp; |
| 873 | break; |
| 874 | case 7: |
| 875 | c->modrm_ea += bx; |
| 876 | break; |
| 877 | } |
| 878 | if (c->modrm_rm == 2 || c->modrm_rm == 3 || |
| 879 | (c->modrm_rm == 6 && c->modrm_mod != 0)) |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 880 | if (!c->has_seg_override) |
| 881 | set_seg_override(c, VCPU_SREG_SS); |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 882 | c->modrm_ea = (u16)c->modrm_ea; |
| 883 | } else { |
| 884 | /* 32/64-bit ModR/M decode. */ |
Avi Kivity | 84411d8 | 2008-06-15 21:53:26 -0700 | [diff] [blame] | 885 | if ((c->modrm_rm & 7) == 4) { |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 886 | sib = insn_fetch(u8, 1, c->eip); |
| 887 | index_reg |= (sib >> 3) & 7; |
| 888 | base_reg |= sib & 7; |
| 889 | scale = sib >> 6; |
| 890 | |
Avi Kivity | dc71d0f | 2008-06-15 21:23:17 -0700 | [diff] [blame] | 891 | if ((base_reg & 7) == 5 && c->modrm_mod == 0) |
| 892 | c->modrm_ea += insn_fetch(s32, 4, c->eip); |
| 893 | else |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 894 | c->modrm_ea += c->regs[base_reg]; |
Avi Kivity | dc71d0f | 2008-06-15 21:23:17 -0700 | [diff] [blame] | 895 | if (index_reg != 4) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 896 | c->modrm_ea += c->regs[index_reg] << scale; |
Avi Kivity | 84411d8 | 2008-06-15 21:53:26 -0700 | [diff] [blame] | 897 | } else if ((c->modrm_rm & 7) == 5 && c->modrm_mod == 0) { |
| 898 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
Avi Kivity | f5b4edc | 2008-06-15 22:09:11 -0700 | [diff] [blame] | 899 | c->rip_relative = 1; |
Avi Kivity | 84411d8 | 2008-06-15 21:53:26 -0700 | [diff] [blame] | 900 | } else |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 901 | c->modrm_ea += c->regs[c->modrm_rm]; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 902 | switch (c->modrm_mod) { |
| 903 | case 0: |
| 904 | if (c->modrm_rm == 5) |
| 905 | c->modrm_ea += insn_fetch(s32, 4, c->eip); |
| 906 | break; |
| 907 | case 1: |
| 908 | c->modrm_ea += insn_fetch(s8, 1, c->eip); |
| 909 | break; |
| 910 | case 2: |
| 911 | c->modrm_ea += insn_fetch(s32, 4, c->eip); |
| 912 | break; |
| 913 | } |
| 914 | } |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 915 | done: |
| 916 | return rc; |
| 917 | } |
| 918 | |
| 919 | static int decode_abs(struct x86_emulate_ctxt *ctxt, |
| 920 | struct x86_emulate_ops *ops) |
| 921 | { |
| 922 | struct decode_cache *c = &ctxt->decode; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 923 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 924 | |
| 925 | switch (c->ad_bytes) { |
| 926 | case 2: |
| 927 | c->modrm_ea = insn_fetch(u16, 2, c->eip); |
| 928 | break; |
| 929 | case 4: |
| 930 | c->modrm_ea = insn_fetch(u32, 4, c->eip); |
| 931 | break; |
| 932 | case 8: |
| 933 | c->modrm_ea = insn_fetch(u64, 8, c->eip); |
| 934 | break; |
| 935 | } |
| 936 | done: |
| 937 | return rc; |
| 938 | } |
| 939 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 940 | int |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 941 | x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 942 | { |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 943 | struct decode_cache *c = &ctxt->decode; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 944 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 945 | int mode = ctxt->mode; |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame^] | 946 | int def_op_bytes, def_ad_bytes, group, dual, goffset; |
| 947 | struct opcode opcode, *g_mod012, *g_mod3; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 948 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 949 | /* we cannot decode insn before we complete previous rep insn */ |
| 950 | WARN_ON(ctxt->restart); |
| 951 | |
Gleb Natapov | 063db06 | 2010-03-18 15:20:06 +0200 | [diff] [blame] | 952 | c->eip = ctxt->eip; |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 953 | c->fetch.start = c->fetch.end = c->eip; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 954 | ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 955 | |
| 956 | switch (mode) { |
| 957 | case X86EMUL_MODE_REAL: |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 958 | case X86EMUL_MODE_VM86: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 959 | case X86EMUL_MODE_PROT16: |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 960 | def_op_bytes = def_ad_bytes = 2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 961 | break; |
| 962 | case X86EMUL_MODE_PROT32: |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 963 | def_op_bytes = def_ad_bytes = 4; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 964 | break; |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 965 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 966 | case X86EMUL_MODE_PROT64: |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 967 | def_op_bytes = 4; |
| 968 | def_ad_bytes = 8; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 969 | break; |
| 970 | #endif |
| 971 | default: |
| 972 | return -1; |
| 973 | } |
| 974 | |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 975 | c->op_bytes = def_op_bytes; |
| 976 | c->ad_bytes = def_ad_bytes; |
| 977 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 978 | /* Legacy prefixes. */ |
Laurent Vivier | b4c6abf | 2007-09-25 13:36:40 +0200 | [diff] [blame] | 979 | for (;;) { |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 980 | switch (c->b = insn_fetch(u8, 1, c->eip)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 981 | case 0x66: /* operand-size override */ |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 982 | /* switch between 2/4 bytes */ |
| 983 | c->op_bytes = def_op_bytes ^ 6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 984 | break; |
| 985 | case 0x67: /* address-size override */ |
| 986 | if (mode == X86EMUL_MODE_PROT64) |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 987 | /* switch between 4/8 bytes */ |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 988 | c->ad_bytes = def_ad_bytes ^ 12; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 989 | else |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 990 | /* switch between 2/4 bytes */ |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 991 | c->ad_bytes = def_ad_bytes ^ 6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 992 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 993 | case 0x26: /* ES override */ |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 994 | case 0x2e: /* CS override */ |
| 995 | case 0x36: /* SS override */ |
| 996 | case 0x3e: /* DS override */ |
| 997 | set_seg_override(c, (c->b >> 3) & 3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 998 | break; |
| 999 | case 0x64: /* FS override */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1000 | case 0x65: /* GS override */ |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 1001 | set_seg_override(c, c->b & 7); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1002 | break; |
Laurent Vivier | b4c6abf | 2007-09-25 13:36:40 +0200 | [diff] [blame] | 1003 | case 0x40 ... 0x4f: /* REX */ |
| 1004 | if (mode != X86EMUL_MODE_PROT64) |
| 1005 | goto done_prefixes; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1006 | c->rex_prefix = c->b; |
Laurent Vivier | b4c6abf | 2007-09-25 13:36:40 +0200 | [diff] [blame] | 1007 | continue; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1008 | case 0xf0: /* LOCK */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1009 | c->lock_prefix = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1010 | break; |
Laurent Vivier | ae6200b | 2007-09-20 11:17:24 +0200 | [diff] [blame] | 1011 | case 0xf2: /* REPNE/REPNZ */ |
Guillaume Thouvenin | 90e0a28 | 2007-11-22 11:32:09 +0100 | [diff] [blame] | 1012 | c->rep_prefix = REPNE_PREFIX; |
| 1013 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1014 | case 0xf3: /* REP/REPE/REPZ */ |
Guillaume Thouvenin | 90e0a28 | 2007-11-22 11:32:09 +0100 | [diff] [blame] | 1015 | c->rep_prefix = REPE_PREFIX; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1016 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1017 | default: |
| 1018 | goto done_prefixes; |
| 1019 | } |
Laurent Vivier | b4c6abf | 2007-09-25 13:36:40 +0200 | [diff] [blame] | 1020 | |
| 1021 | /* Any legacy prefix after a REX prefix nullifies its effect. */ |
| 1022 | |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1023 | c->rex_prefix = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | done_prefixes: |
| 1027 | |
| 1028 | /* REX prefix. */ |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1029 | if (c->rex_prefix) |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1030 | if (c->rex_prefix & 8) |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1031 | c->op_bytes = 8; /* REX.W */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1032 | |
| 1033 | /* Opcode byte(s). */ |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame^] | 1034 | opcode = opcode_table[c->b]; |
| 1035 | if (opcode.flags == 0) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1036 | /* Two-byte opcode? */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1037 | if (c->b == 0x0f) { |
| 1038 | c->twobyte = 1; |
| 1039 | c->b = insn_fetch(u8, 1, c->eip); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame^] | 1040 | opcode = twobyte_table[c->b]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1041 | } |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 1042 | } |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame^] | 1043 | c->d = opcode.flags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1044 | |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 1045 | if (c->d & Group) { |
| 1046 | group = c->d & GroupMask; |
Avi Kivity | 52811d7 | 2010-07-26 14:37:48 +0300 | [diff] [blame] | 1047 | dual = c->d & GroupDual; |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 1048 | c->modrm = insn_fetch(u8, 1, c->eip); |
| 1049 | --c->eip; |
| 1050 | |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame^] | 1051 | if (group) { |
| 1052 | g_mod012 = g_mod3 = &group_table[group * 8]; |
| 1053 | if (c->d & GroupDual) |
| 1054 | g_mod3 = &group2_table[group * 8]; |
| 1055 | } else { |
| 1056 | if (c->d & GroupDual) { |
| 1057 | g_mod012 = opcode.u.gdual->mod012; |
| 1058 | g_mod3 = opcode.u.gdual->mod3; |
| 1059 | } else |
| 1060 | g_mod012 = g_mod3 = opcode.u.group; |
| 1061 | } |
| 1062 | |
Avi Kivity | 52811d7 | 2010-07-26 14:37:48 +0300 | [diff] [blame] | 1063 | c->d &= ~(Group | GroupDual | GroupMask); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame^] | 1064 | |
| 1065 | goffset = (c->modrm >> 3) & 7; |
| 1066 | |
| 1067 | if ((c->modrm >> 6) == 3) |
| 1068 | opcode = g_mod3[goffset]; |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 1069 | else |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame^] | 1070 | opcode = g_mod012[goffset]; |
| 1071 | c->d |= opcode.flags; |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | /* Unrecognised? */ |
Avi Kivity | 047a481 | 2010-07-26 14:37:47 +0300 | [diff] [blame] | 1075 | if (c->d == 0 || (c->d & Undefined)) { |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 1076 | DPRINTF("Cannot emulate %02x\n", c->b); |
| 1077 | return -1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1078 | } |
| 1079 | |
Avi Kivity | 6e3d5df | 2007-12-06 18:14:14 +0200 | [diff] [blame] | 1080 | if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack)) |
| 1081 | c->op_bytes = 8; |
| 1082 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1083 | /* ModRM and SIB bytes. */ |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1084 | if (c->d & ModRM) |
| 1085 | rc = decode_modrm(ctxt, ops); |
| 1086 | else if (c->d & MemAbs) |
| 1087 | rc = decode_abs(ctxt, ops); |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1088 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1089 | goto done; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1090 | |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 1091 | if (!c->has_seg_override) |
| 1092 | set_seg_override(c, VCPU_SREG_DS); |
Avi Kivity | c7e75a3 | 2007-10-28 16:34:25 +0200 | [diff] [blame] | 1093 | |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 1094 | if (!(!c->twobyte && c->b == 0x8d)) |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1095 | c->modrm_ea += seg_override_base(ctxt, ops, c); |
Avi Kivity | c7e75a3 | 2007-10-28 16:34:25 +0200 | [diff] [blame] | 1096 | |
| 1097 | if (c->ad_bytes != 8) |
| 1098 | c->modrm_ea = (u32)c->modrm_ea; |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 1099 | |
| 1100 | if (c->rip_relative) |
| 1101 | c->modrm_ea += c->eip; |
| 1102 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1103 | /* |
| 1104 | * Decode and fetch the source operand: register, memory |
| 1105 | * or immediate. |
| 1106 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1107 | switch (c->d & SrcMask) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1108 | case SrcNone: |
| 1109 | break; |
| 1110 | case SrcReg: |
Avi Kivity | 9f1ef3f | 2007-10-31 11:21:06 +0200 | [diff] [blame] | 1111 | decode_register_operand(&c->src, c, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1112 | break; |
| 1113 | case SrcMem16: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1114 | c->src.bytes = 2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1115 | goto srcmem_common; |
| 1116 | case SrcMem32: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1117 | c->src.bytes = 4; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1118 | goto srcmem_common; |
| 1119 | case SrcMem: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1120 | c->src.bytes = (c->d & ByteOp) ? 1 : |
| 1121 | c->op_bytes; |
Rusty Russell | b85b9ee9 | 2007-09-09 14:12:54 +0300 | [diff] [blame] | 1122 | /* Don't fetch the address for invlpg: it could be unmapped. */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1123 | if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7) |
Rusty Russell | b85b9ee9 | 2007-09-09 14:12:54 +0300 | [diff] [blame] | 1124 | break; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1125 | srcmem_common: |
Aurelien Jarno | 4e62417 | 2007-10-17 19:30:41 +0200 | [diff] [blame] | 1126 | /* |
| 1127 | * For instructions with a ModR/M byte, switch to register |
| 1128 | * access if Mod = 3. |
| 1129 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1130 | if ((c->d & ModRM) && c->modrm_mod == 3) { |
| 1131 | c->src.type = OP_REG; |
Avi Kivity | 66b8550 | 2008-04-14 23:27:07 +0300 | [diff] [blame] | 1132 | c->src.val = c->modrm_val; |
Avi Kivity | 107d6d2 | 2008-05-05 14:58:26 +0300 | [diff] [blame] | 1133 | c->src.ptr = c->modrm_ptr; |
Aurelien Jarno | 4e62417 | 2007-10-17 19:30:41 +0200 | [diff] [blame] | 1134 | break; |
| 1135 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1136 | c->src.type = OP_MEM; |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 1137 | c->src.ptr = (unsigned long *)c->modrm_ea; |
| 1138 | c->src.val = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1139 | break; |
| 1140 | case SrcImm: |
Avi Kivity | c9eaf20f | 2009-05-18 16:13:45 +0300 | [diff] [blame] | 1141 | case SrcImmU: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1142 | c->src.type = OP_IMM; |
| 1143 | c->src.ptr = (unsigned long *)c->eip; |
| 1144 | c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 1145 | if (c->src.bytes == 8) |
| 1146 | c->src.bytes = 4; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1147 | /* NB. Immediates are sign-extended as necessary. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1148 | switch (c->src.bytes) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1149 | case 1: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1150 | c->src.val = insn_fetch(s8, 1, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1151 | break; |
| 1152 | case 2: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1153 | c->src.val = insn_fetch(s16, 2, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1154 | break; |
| 1155 | case 4: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1156 | c->src.val = insn_fetch(s32, 4, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1157 | break; |
| 1158 | } |
Avi Kivity | c9eaf20f | 2009-05-18 16:13:45 +0300 | [diff] [blame] | 1159 | if ((c->d & SrcMask) == SrcImmU) { |
| 1160 | switch (c->src.bytes) { |
| 1161 | case 1: |
| 1162 | c->src.val &= 0xff; |
| 1163 | break; |
| 1164 | case 2: |
| 1165 | c->src.val &= 0xffff; |
| 1166 | break; |
| 1167 | case 4: |
| 1168 | c->src.val &= 0xffffffff; |
| 1169 | break; |
| 1170 | } |
| 1171 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1172 | break; |
| 1173 | case SrcImmByte: |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 1174 | case SrcImmUByte: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1175 | c->src.type = OP_IMM; |
| 1176 | c->src.ptr = (unsigned long *)c->eip; |
| 1177 | c->src.bytes = 1; |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 1178 | if ((c->d & SrcMask) == SrcImmByte) |
| 1179 | c->src.val = insn_fetch(s8, 1, c->eip); |
| 1180 | else |
| 1181 | c->src.val = insn_fetch(u8, 1, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1182 | break; |
Wei Yongjun | 5d55f29 | 2010-07-07 17:43:35 +0800 | [diff] [blame] | 1183 | case SrcAcc: |
| 1184 | c->src.type = OP_REG; |
| 1185 | c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 1186 | c->src.ptr = &c->regs[VCPU_REGS_RAX]; |
| 1187 | switch (c->src.bytes) { |
| 1188 | case 1: |
| 1189 | c->src.val = *(u8 *)c->src.ptr; |
| 1190 | break; |
| 1191 | case 2: |
| 1192 | c->src.val = *(u16 *)c->src.ptr; |
| 1193 | break; |
| 1194 | case 4: |
| 1195 | c->src.val = *(u32 *)c->src.ptr; |
| 1196 | break; |
| 1197 | case 8: |
| 1198 | c->src.val = *(u64 *)c->src.ptr; |
| 1199 | break; |
| 1200 | } |
| 1201 | break; |
Guillaume Thouvenin | bfcadf8 | 2008-12-04 14:27:38 +0100 | [diff] [blame] | 1202 | case SrcOne: |
| 1203 | c->src.bytes = 1; |
| 1204 | c->src.val = 1; |
| 1205 | break; |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 1206 | case SrcSI: |
| 1207 | c->src.type = OP_MEM; |
| 1208 | c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 1209 | c->src.ptr = (unsigned long *) |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1210 | register_address(c, seg_override_base(ctxt, ops, c), |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 1211 | c->regs[VCPU_REGS_RSI]); |
| 1212 | c->src.val = 0; |
| 1213 | break; |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 1214 | case SrcImmFAddr: |
| 1215 | c->src.type = OP_IMM; |
| 1216 | c->src.ptr = (unsigned long *)c->eip; |
| 1217 | c->src.bytes = c->op_bytes + 2; |
| 1218 | insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip); |
| 1219 | break; |
| 1220 | case SrcMemFAddr: |
| 1221 | c->src.type = OP_MEM; |
| 1222 | c->src.ptr = (unsigned long *)c->modrm_ea; |
| 1223 | c->src.bytes = c->op_bytes + 2; |
| 1224 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1225 | } |
| 1226 | |
Guillaume Thouvenin | 0dc8d10 | 2008-12-04 14:26:42 +0100 | [diff] [blame] | 1227 | /* |
| 1228 | * Decode and fetch the second source operand: register, memory |
| 1229 | * or immediate. |
| 1230 | */ |
| 1231 | switch (c->d & Src2Mask) { |
| 1232 | case Src2None: |
| 1233 | break; |
| 1234 | case Src2CL: |
| 1235 | c->src2.bytes = 1; |
| 1236 | c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8; |
| 1237 | break; |
| 1238 | case Src2ImmByte: |
| 1239 | c->src2.type = OP_IMM; |
| 1240 | c->src2.ptr = (unsigned long *)c->eip; |
| 1241 | c->src2.bytes = 1; |
| 1242 | c->src2.val = insn_fetch(u8, 1, c->eip); |
| 1243 | break; |
| 1244 | case Src2One: |
| 1245 | c->src2.bytes = 1; |
| 1246 | c->src2.val = 1; |
| 1247 | break; |
| 1248 | } |
| 1249 | |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1250 | /* Decode and fetch the destination operand: register or memory. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1251 | switch (c->d & DstMask) { |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1252 | case ImplicitOps: |
| 1253 | /* Special instructions do their own operand decoding. */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1254 | return 0; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1255 | case DstReg: |
Avi Kivity | 9f1ef3f | 2007-10-31 11:21:06 +0200 | [diff] [blame] | 1256 | decode_register_operand(&c->dst, c, |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1257 | c->twobyte && (c->b == 0xb6 || c->b == 0xb7)); |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1258 | break; |
| 1259 | case DstMem: |
Gleb Natapov | 6550e1f | 2010-03-21 13:08:21 +0200 | [diff] [blame] | 1260 | case DstMem64: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1261 | if ((c->d & ModRM) && c->modrm_mod == 3) { |
Guillaume Thouvenin | 89c6963 | 2008-05-27 10:22:20 +0200 | [diff] [blame] | 1262 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1263 | c->dst.type = OP_REG; |
Avi Kivity | 66b8550 | 2008-04-14 23:27:07 +0300 | [diff] [blame] | 1264 | c->dst.val = c->dst.orig_val = c->modrm_val; |
Avi Kivity | 107d6d2 | 2008-05-05 14:58:26 +0300 | [diff] [blame] | 1265 | c->dst.ptr = c->modrm_ptr; |
Aurelien Jarno | 4e62417 | 2007-10-17 19:30:41 +0200 | [diff] [blame] | 1266 | break; |
| 1267 | } |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1268 | c->dst.type = OP_MEM; |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 1269 | c->dst.ptr = (unsigned long *)c->modrm_ea; |
Gleb Natapov | 6550e1f | 2010-03-21 13:08:21 +0200 | [diff] [blame] | 1270 | if ((c->d & DstMask) == DstMem64) |
| 1271 | c->dst.bytes = 8; |
| 1272 | else |
| 1273 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 1274 | c->dst.val = 0; |
| 1275 | if (c->d & BitOp) { |
| 1276 | unsigned long mask = ~(c->dst.bytes * 8 - 1); |
| 1277 | |
| 1278 | c->dst.ptr = (void *)c->dst.ptr + |
| 1279 | (c->src.val & mask) / 8; |
| 1280 | } |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1281 | break; |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 1282 | case DstAcc: |
| 1283 | c->dst.type = OP_REG; |
Gleb Natapov | d6d367d | 2010-03-15 16:38:28 +0200 | [diff] [blame] | 1284 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 1285 | c->dst.ptr = &c->regs[VCPU_REGS_RAX]; |
Gleb Natapov | d6d367d | 2010-03-15 16:38:28 +0200 | [diff] [blame] | 1286 | switch (c->dst.bytes) { |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 1287 | case 1: |
| 1288 | c->dst.val = *(u8 *)c->dst.ptr; |
| 1289 | break; |
| 1290 | case 2: |
| 1291 | c->dst.val = *(u16 *)c->dst.ptr; |
| 1292 | break; |
| 1293 | case 4: |
| 1294 | c->dst.val = *(u32 *)c->dst.ptr; |
| 1295 | break; |
Gleb Natapov | d6d367d | 2010-03-15 16:38:28 +0200 | [diff] [blame] | 1296 | case 8: |
| 1297 | c->dst.val = *(u64 *)c->dst.ptr; |
| 1298 | break; |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 1299 | } |
| 1300 | c->dst.orig_val = c->dst.val; |
| 1301 | break; |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 1302 | case DstDI: |
| 1303 | c->dst.type = OP_MEM; |
| 1304 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 1305 | c->dst.ptr = (unsigned long *) |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1306 | register_address(c, es_base(ctxt, ops), |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 1307 | c->regs[VCPU_REGS_RDI]); |
| 1308 | c->dst.val = 0; |
| 1309 | break; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | done: |
| 1313 | return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0; |
| 1314 | } |
| 1315 | |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1316 | static int read_emulated(struct x86_emulate_ctxt *ctxt, |
| 1317 | struct x86_emulate_ops *ops, |
| 1318 | unsigned long addr, void *dest, unsigned size) |
| 1319 | { |
| 1320 | int rc; |
| 1321 | struct read_cache *mc = &ctxt->decode.mem_read; |
Gleb Natapov | 8fe681e | 2010-04-28 19:15:37 +0300 | [diff] [blame] | 1322 | u32 err; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1323 | |
| 1324 | while (size) { |
| 1325 | int n = min(size, 8u); |
| 1326 | size -= n; |
| 1327 | if (mc->pos < mc->end) |
| 1328 | goto read_cached; |
| 1329 | |
Gleb Natapov | 8fe681e | 2010-04-28 19:15:37 +0300 | [diff] [blame] | 1330 | rc = ops->read_emulated(addr, mc->data + mc->end, n, &err, |
| 1331 | ctxt->vcpu); |
| 1332 | if (rc == X86EMUL_PROPAGATE_FAULT) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 1333 | emulate_pf(ctxt, addr, err); |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1334 | if (rc != X86EMUL_CONTINUE) |
| 1335 | return rc; |
| 1336 | mc->end += n; |
| 1337 | |
| 1338 | read_cached: |
| 1339 | memcpy(dest, mc->data + mc->pos, n); |
| 1340 | mc->pos += n; |
| 1341 | dest += n; |
| 1342 | addr += n; |
| 1343 | } |
| 1344 | return X86EMUL_CONTINUE; |
| 1345 | } |
| 1346 | |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1347 | static int pio_in_emulated(struct x86_emulate_ctxt *ctxt, |
| 1348 | struct x86_emulate_ops *ops, |
| 1349 | unsigned int size, unsigned short port, |
| 1350 | void *dest) |
| 1351 | { |
| 1352 | struct read_cache *rc = &ctxt->decode.io_read; |
| 1353 | |
| 1354 | if (rc->pos == rc->end) { /* refill pio read ahead */ |
| 1355 | struct decode_cache *c = &ctxt->decode; |
| 1356 | unsigned int in_page, n; |
| 1357 | unsigned int count = c->rep_prefix ? |
| 1358 | address_mask(c, c->regs[VCPU_REGS_RCX]) : 1; |
| 1359 | in_page = (ctxt->eflags & EFLG_DF) ? |
| 1360 | offset_in_page(c->regs[VCPU_REGS_RDI]) : |
| 1361 | PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]); |
| 1362 | n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size, |
| 1363 | count); |
| 1364 | if (n == 0) |
| 1365 | n = 1; |
| 1366 | rc->pos = rc->end = 0; |
| 1367 | if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu)) |
| 1368 | return 0; |
| 1369 | rc->end = n * size; |
| 1370 | } |
| 1371 | |
| 1372 | memcpy(dest, rc->data + rc->pos, size); |
| 1373 | rc->pos += size; |
| 1374 | return 1; |
| 1375 | } |
| 1376 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1377 | static u32 desc_limit_scaled(struct desc_struct *desc) |
| 1378 | { |
| 1379 | u32 limit = get_desc_limit(desc); |
| 1380 | |
| 1381 | return desc->g ? (limit << 12) | 0xfff : limit; |
| 1382 | } |
| 1383 | |
| 1384 | static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt, |
| 1385 | struct x86_emulate_ops *ops, |
| 1386 | u16 selector, struct desc_ptr *dt) |
| 1387 | { |
| 1388 | if (selector & 1 << 2) { |
| 1389 | struct desc_struct desc; |
| 1390 | memset (dt, 0, sizeof *dt); |
| 1391 | if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu)) |
| 1392 | return; |
| 1393 | |
| 1394 | dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */ |
| 1395 | dt->address = get_desc_base(&desc); |
| 1396 | } else |
| 1397 | ops->get_gdt(dt, ctxt->vcpu); |
| 1398 | } |
| 1399 | |
| 1400 | /* allowed just for 8 bytes segments */ |
| 1401 | static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1402 | struct x86_emulate_ops *ops, |
| 1403 | u16 selector, struct desc_struct *desc) |
| 1404 | { |
| 1405 | struct desc_ptr dt; |
| 1406 | u16 index = selector >> 3; |
| 1407 | int ret; |
| 1408 | u32 err; |
| 1409 | ulong addr; |
| 1410 | |
| 1411 | get_descriptor_table_ptr(ctxt, ops, selector, &dt); |
| 1412 | |
| 1413 | if (dt.size < index * 8 + 7) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 1414 | emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1415 | return X86EMUL_PROPAGATE_FAULT; |
| 1416 | } |
| 1417 | addr = dt.address + index * 8; |
| 1418 | ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err); |
| 1419 | if (ret == X86EMUL_PROPAGATE_FAULT) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 1420 | emulate_pf(ctxt, addr, err); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1421 | |
| 1422 | return ret; |
| 1423 | } |
| 1424 | |
| 1425 | /* allowed just for 8 bytes segments */ |
| 1426 | static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1427 | struct x86_emulate_ops *ops, |
| 1428 | u16 selector, struct desc_struct *desc) |
| 1429 | { |
| 1430 | struct desc_ptr dt; |
| 1431 | u16 index = selector >> 3; |
| 1432 | u32 err; |
| 1433 | ulong addr; |
| 1434 | int ret; |
| 1435 | |
| 1436 | get_descriptor_table_ptr(ctxt, ops, selector, &dt); |
| 1437 | |
| 1438 | if (dt.size < index * 8 + 7) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 1439 | emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1440 | return X86EMUL_PROPAGATE_FAULT; |
| 1441 | } |
| 1442 | |
| 1443 | addr = dt.address + index * 8; |
| 1444 | ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err); |
| 1445 | if (ret == X86EMUL_PROPAGATE_FAULT) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 1446 | emulate_pf(ctxt, addr, err); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1447 | |
| 1448 | return ret; |
| 1449 | } |
| 1450 | |
| 1451 | static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1452 | struct x86_emulate_ops *ops, |
| 1453 | u16 selector, int seg) |
| 1454 | { |
| 1455 | struct desc_struct seg_desc; |
| 1456 | u8 dpl, rpl, cpl; |
| 1457 | unsigned err_vec = GP_VECTOR; |
| 1458 | u32 err_code = 0; |
| 1459 | bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */ |
| 1460 | int ret; |
| 1461 | |
| 1462 | memset(&seg_desc, 0, sizeof seg_desc); |
| 1463 | |
| 1464 | if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) |
| 1465 | || ctxt->mode == X86EMUL_MODE_REAL) { |
| 1466 | /* set real mode segment descriptor */ |
| 1467 | set_desc_base(&seg_desc, selector << 4); |
| 1468 | set_desc_limit(&seg_desc, 0xffff); |
| 1469 | seg_desc.type = 3; |
| 1470 | seg_desc.p = 1; |
| 1471 | seg_desc.s = 1; |
| 1472 | goto load; |
| 1473 | } |
| 1474 | |
| 1475 | /* NULL selector is not valid for TR, CS and SS */ |
| 1476 | if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR) |
| 1477 | && null_selector) |
| 1478 | goto exception; |
| 1479 | |
| 1480 | /* TR should be in GDT only */ |
| 1481 | if (seg == VCPU_SREG_TR && (selector & (1 << 2))) |
| 1482 | goto exception; |
| 1483 | |
| 1484 | if (null_selector) /* for NULL selector skip all following checks */ |
| 1485 | goto load; |
| 1486 | |
| 1487 | ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc); |
| 1488 | if (ret != X86EMUL_CONTINUE) |
| 1489 | return ret; |
| 1490 | |
| 1491 | err_code = selector & 0xfffc; |
| 1492 | err_vec = GP_VECTOR; |
| 1493 | |
| 1494 | /* can't load system descriptor into segment selecor */ |
| 1495 | if (seg <= VCPU_SREG_GS && !seg_desc.s) |
| 1496 | goto exception; |
| 1497 | |
| 1498 | if (!seg_desc.p) { |
| 1499 | err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR; |
| 1500 | goto exception; |
| 1501 | } |
| 1502 | |
| 1503 | rpl = selector & 3; |
| 1504 | dpl = seg_desc.dpl; |
| 1505 | cpl = ops->cpl(ctxt->vcpu); |
| 1506 | |
| 1507 | switch (seg) { |
| 1508 | case VCPU_SREG_SS: |
| 1509 | /* |
| 1510 | * segment is not a writable data segment or segment |
| 1511 | * selector's RPL != CPL or segment selector's RPL != CPL |
| 1512 | */ |
| 1513 | if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl) |
| 1514 | goto exception; |
| 1515 | break; |
| 1516 | case VCPU_SREG_CS: |
| 1517 | if (!(seg_desc.type & 8)) |
| 1518 | goto exception; |
| 1519 | |
| 1520 | if (seg_desc.type & 4) { |
| 1521 | /* conforming */ |
| 1522 | if (dpl > cpl) |
| 1523 | goto exception; |
| 1524 | } else { |
| 1525 | /* nonconforming */ |
| 1526 | if (rpl > cpl || dpl != cpl) |
| 1527 | goto exception; |
| 1528 | } |
| 1529 | /* CS(RPL) <- CPL */ |
| 1530 | selector = (selector & 0xfffc) | cpl; |
| 1531 | break; |
| 1532 | case VCPU_SREG_TR: |
| 1533 | if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9)) |
| 1534 | goto exception; |
| 1535 | break; |
| 1536 | case VCPU_SREG_LDTR: |
| 1537 | if (seg_desc.s || seg_desc.type != 2) |
| 1538 | goto exception; |
| 1539 | break; |
| 1540 | default: /* DS, ES, FS, or GS */ |
| 1541 | /* |
| 1542 | * segment is not a data or readable code segment or |
| 1543 | * ((segment is a data or nonconforming code segment) |
| 1544 | * and (both RPL and CPL > DPL)) |
| 1545 | */ |
| 1546 | if ((seg_desc.type & 0xa) == 0x8 || |
| 1547 | (((seg_desc.type & 0xc) != 0xc) && |
| 1548 | (rpl > dpl && cpl > dpl))) |
| 1549 | goto exception; |
| 1550 | break; |
| 1551 | } |
| 1552 | |
| 1553 | if (seg_desc.s) { |
| 1554 | /* mark segment as accessed */ |
| 1555 | seg_desc.type |= 1; |
| 1556 | ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc); |
| 1557 | if (ret != X86EMUL_CONTINUE) |
| 1558 | return ret; |
| 1559 | } |
| 1560 | load: |
| 1561 | ops->set_segment_selector(selector, seg, ctxt->vcpu); |
| 1562 | ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu); |
| 1563 | return X86EMUL_CONTINUE; |
| 1564 | exception: |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 1565 | emulate_exception(ctxt, err_vec, err_code, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1566 | return X86EMUL_PROPAGATE_FAULT; |
| 1567 | } |
| 1568 | |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1569 | static inline int writeback(struct x86_emulate_ctxt *ctxt, |
| 1570 | struct x86_emulate_ops *ops) |
| 1571 | { |
| 1572 | int rc; |
| 1573 | struct decode_cache *c = &ctxt->decode; |
| 1574 | u32 err; |
| 1575 | |
| 1576 | switch (c->dst.type) { |
| 1577 | case OP_REG: |
| 1578 | /* The 4-byte case *is* correct: |
| 1579 | * in 64-bit mode we zero-extend. |
| 1580 | */ |
| 1581 | switch (c->dst.bytes) { |
| 1582 | case 1: |
| 1583 | *(u8 *)c->dst.ptr = (u8)c->dst.val; |
| 1584 | break; |
| 1585 | case 2: |
| 1586 | *(u16 *)c->dst.ptr = (u16)c->dst.val; |
| 1587 | break; |
| 1588 | case 4: |
| 1589 | *c->dst.ptr = (u32)c->dst.val; |
| 1590 | break; /* 64b: zero-ext */ |
| 1591 | case 8: |
| 1592 | *c->dst.ptr = c->dst.val; |
| 1593 | break; |
| 1594 | } |
| 1595 | break; |
| 1596 | case OP_MEM: |
| 1597 | if (c->lock_prefix) |
| 1598 | rc = ops->cmpxchg_emulated( |
| 1599 | (unsigned long)c->dst.ptr, |
| 1600 | &c->dst.orig_val, |
| 1601 | &c->dst.val, |
| 1602 | c->dst.bytes, |
| 1603 | &err, |
| 1604 | ctxt->vcpu); |
| 1605 | else |
| 1606 | rc = ops->write_emulated( |
| 1607 | (unsigned long)c->dst.ptr, |
| 1608 | &c->dst.val, |
| 1609 | c->dst.bytes, |
| 1610 | &err, |
| 1611 | ctxt->vcpu); |
| 1612 | if (rc == X86EMUL_PROPAGATE_FAULT) |
| 1613 | emulate_pf(ctxt, |
| 1614 | (unsigned long)c->dst.ptr, err); |
| 1615 | if (rc != X86EMUL_CONTINUE) |
| 1616 | return rc; |
| 1617 | break; |
| 1618 | case OP_NONE: |
| 1619 | /* no writeback */ |
| 1620 | break; |
| 1621 | default: |
| 1622 | break; |
| 1623 | } |
| 1624 | return X86EMUL_CONTINUE; |
| 1625 | } |
| 1626 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1627 | static inline void emulate_push(struct x86_emulate_ctxt *ctxt, |
| 1628 | struct x86_emulate_ops *ops) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1629 | { |
| 1630 | struct decode_cache *c = &ctxt->decode; |
| 1631 | |
| 1632 | c->dst.type = OP_MEM; |
| 1633 | c->dst.bytes = c->op_bytes; |
| 1634 | c->dst.val = c->src.val; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 1635 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1636 | c->dst.ptr = (void *) register_address(c, ss_base(ctxt, ops), |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1637 | c->regs[VCPU_REGS_RSP]); |
| 1638 | } |
| 1639 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1640 | static int emulate_pop(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1641 | struct x86_emulate_ops *ops, |
| 1642 | void *dest, int len) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1643 | { |
| 1644 | struct decode_cache *c = &ctxt->decode; |
| 1645 | int rc; |
| 1646 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1647 | rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops), |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1648 | c->regs[VCPU_REGS_RSP]), |
| 1649 | dest, len); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1650 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1651 | return rc; |
| 1652 | |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1653 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], len); |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1654 | return rc; |
| 1655 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1656 | |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1657 | static int emulate_popf(struct x86_emulate_ctxt *ctxt, |
| 1658 | struct x86_emulate_ops *ops, |
| 1659 | void *dest, int len) |
| 1660 | { |
| 1661 | int rc; |
| 1662 | unsigned long val, change_mask; |
| 1663 | int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 1664 | int cpl = ops->cpl(ctxt->vcpu); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1665 | |
| 1666 | rc = emulate_pop(ctxt, ops, &val, len); |
| 1667 | if (rc != X86EMUL_CONTINUE) |
| 1668 | return rc; |
| 1669 | |
| 1670 | change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF |
| 1671 | | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID; |
| 1672 | |
| 1673 | switch(ctxt->mode) { |
| 1674 | case X86EMUL_MODE_PROT64: |
| 1675 | case X86EMUL_MODE_PROT32: |
| 1676 | case X86EMUL_MODE_PROT16: |
| 1677 | if (cpl == 0) |
| 1678 | change_mask |= EFLG_IOPL; |
| 1679 | if (cpl <= iopl) |
| 1680 | change_mask |= EFLG_IF; |
| 1681 | break; |
| 1682 | case X86EMUL_MODE_VM86: |
| 1683 | if (iopl < 3) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 1684 | emulate_gp(ctxt, 0); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1685 | return X86EMUL_PROPAGATE_FAULT; |
| 1686 | } |
| 1687 | change_mask |= EFLG_IF; |
| 1688 | break; |
| 1689 | default: /* real mode */ |
| 1690 | change_mask |= (EFLG_IOPL | EFLG_IF); |
| 1691 | break; |
| 1692 | } |
| 1693 | |
| 1694 | *(unsigned long *)dest = |
| 1695 | (ctxt->eflags & ~change_mask) | (val & change_mask); |
| 1696 | |
| 1697 | return rc; |
| 1698 | } |
| 1699 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1700 | static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt, |
| 1701 | struct x86_emulate_ops *ops, int seg) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1702 | { |
| 1703 | struct decode_cache *c = &ctxt->decode; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1704 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1705 | c->src.val = ops->get_segment_selector(seg, ctxt->vcpu); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1706 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1707 | emulate_push(ctxt, ops); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt, |
| 1711 | struct x86_emulate_ops *ops, int seg) |
| 1712 | { |
| 1713 | struct decode_cache *c = &ctxt->decode; |
| 1714 | unsigned long selector; |
| 1715 | int rc; |
| 1716 | |
| 1717 | rc = emulate_pop(ctxt, ops, &selector, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1718 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1719 | return rc; |
| 1720 | |
Gleb Natapov | 2e87302 | 2010-03-18 15:20:18 +0200 | [diff] [blame] | 1721 | rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1722 | return rc; |
| 1723 | } |
| 1724 | |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1725 | static int emulate_pusha(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1726 | struct x86_emulate_ops *ops) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1727 | { |
| 1728 | struct decode_cache *c = &ctxt->decode; |
| 1729 | unsigned long old_esp = c->regs[VCPU_REGS_RSP]; |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1730 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1731 | int reg = VCPU_REGS_RAX; |
| 1732 | |
| 1733 | while (reg <= VCPU_REGS_RDI) { |
| 1734 | (reg == VCPU_REGS_RSP) ? |
| 1735 | (c->src.val = old_esp) : (c->src.val = c->regs[reg]); |
| 1736 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1737 | emulate_push(ctxt, ops); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1738 | |
| 1739 | rc = writeback(ctxt, ops); |
| 1740 | if (rc != X86EMUL_CONTINUE) |
| 1741 | return rc; |
| 1742 | |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1743 | ++reg; |
| 1744 | } |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1745 | |
| 1746 | /* Disable writeback. */ |
| 1747 | c->dst.type = OP_NONE; |
| 1748 | |
| 1749 | return rc; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | static int emulate_popa(struct x86_emulate_ctxt *ctxt, |
| 1753 | struct x86_emulate_ops *ops) |
| 1754 | { |
| 1755 | struct decode_cache *c = &ctxt->decode; |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1756 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1757 | int reg = VCPU_REGS_RDI; |
| 1758 | |
| 1759 | while (reg >= VCPU_REGS_RAX) { |
| 1760 | if (reg == VCPU_REGS_RSP) { |
| 1761 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], |
| 1762 | c->op_bytes); |
| 1763 | --reg; |
| 1764 | } |
| 1765 | |
| 1766 | rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1767 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1768 | break; |
| 1769 | --reg; |
| 1770 | } |
| 1771 | return rc; |
| 1772 | } |
| 1773 | |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1774 | static int emulate_iret_real(struct x86_emulate_ctxt *ctxt, |
| 1775 | struct x86_emulate_ops *ops) |
| 1776 | { |
| 1777 | struct decode_cache *c = &ctxt->decode; |
| 1778 | int rc = X86EMUL_CONTINUE; |
| 1779 | unsigned long temp_eip = 0; |
| 1780 | unsigned long temp_eflags = 0; |
| 1781 | unsigned long cs = 0; |
| 1782 | unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF | |
| 1783 | EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF | |
| 1784 | EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */ |
| 1785 | unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP; |
| 1786 | |
| 1787 | /* TODO: Add stack limit check */ |
| 1788 | |
| 1789 | rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes); |
| 1790 | |
| 1791 | if (rc != X86EMUL_CONTINUE) |
| 1792 | return rc; |
| 1793 | |
| 1794 | if (temp_eip & ~0xffff) { |
| 1795 | emulate_gp(ctxt, 0); |
| 1796 | return X86EMUL_PROPAGATE_FAULT; |
| 1797 | } |
| 1798 | |
| 1799 | rc = emulate_pop(ctxt, ops, &cs, c->op_bytes); |
| 1800 | |
| 1801 | if (rc != X86EMUL_CONTINUE) |
| 1802 | return rc; |
| 1803 | |
| 1804 | rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes); |
| 1805 | |
| 1806 | if (rc != X86EMUL_CONTINUE) |
| 1807 | return rc; |
| 1808 | |
| 1809 | rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS); |
| 1810 | |
| 1811 | if (rc != X86EMUL_CONTINUE) |
| 1812 | return rc; |
| 1813 | |
| 1814 | c->eip = temp_eip; |
| 1815 | |
| 1816 | |
| 1817 | if (c->op_bytes == 4) |
| 1818 | ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask)); |
| 1819 | else if (c->op_bytes == 2) { |
| 1820 | ctxt->eflags &= ~0xffff; |
| 1821 | ctxt->eflags |= temp_eflags; |
| 1822 | } |
| 1823 | |
| 1824 | ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */ |
| 1825 | ctxt->eflags |= EFLG_RESERVED_ONE_MASK; |
| 1826 | |
| 1827 | return rc; |
| 1828 | } |
| 1829 | |
| 1830 | static inline int emulate_iret(struct x86_emulate_ctxt *ctxt, |
| 1831 | struct x86_emulate_ops* ops) |
| 1832 | { |
| 1833 | switch(ctxt->mode) { |
| 1834 | case X86EMUL_MODE_REAL: |
| 1835 | return emulate_iret_real(ctxt, ops); |
| 1836 | case X86EMUL_MODE_VM86: |
| 1837 | case X86EMUL_MODE_PROT16: |
| 1838 | case X86EMUL_MODE_PROT32: |
| 1839 | case X86EMUL_MODE_PROT64: |
| 1840 | default: |
| 1841 | /* iret from protected mode unimplemented yet */ |
| 1842 | return X86EMUL_UNHANDLEABLE; |
| 1843 | } |
| 1844 | } |
| 1845 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1846 | static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt, |
| 1847 | struct x86_emulate_ops *ops) |
| 1848 | { |
| 1849 | struct decode_cache *c = &ctxt->decode; |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1850 | |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1851 | return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1852 | } |
| 1853 | |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1854 | static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1855 | { |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1856 | struct decode_cache *c = &ctxt->decode; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1857 | switch (c->modrm_reg) { |
| 1858 | case 0: /* rol */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1859 | emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1860 | break; |
| 1861 | case 1: /* ror */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1862 | emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1863 | break; |
| 1864 | case 2: /* rcl */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1865 | emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1866 | break; |
| 1867 | case 3: /* rcr */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1868 | emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1869 | break; |
| 1870 | case 4: /* sal/shl */ |
| 1871 | case 6: /* sal/shl */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1872 | emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1873 | break; |
| 1874 | case 5: /* shr */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1875 | emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1876 | break; |
| 1877 | case 7: /* sar */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1878 | emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1879 | break; |
| 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt, |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1884 | struct x86_emulate_ops *ops) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1885 | { |
| 1886 | struct decode_cache *c = &ctxt->decode; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1887 | |
| 1888 | switch (c->modrm_reg) { |
| 1889 | case 0 ... 1: /* test */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1890 | emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1891 | break; |
| 1892 | case 2: /* not */ |
| 1893 | c->dst.val = ~c->dst.val; |
| 1894 | break; |
| 1895 | case 3: /* neg */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1896 | emulate_1op("neg", c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1897 | break; |
| 1898 | default: |
Gleb Natapov | aca06a8 | 2010-03-18 15:20:15 +0200 | [diff] [blame] | 1899 | return 0; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1900 | } |
Gleb Natapov | aca06a8 | 2010-03-18 15:20:15 +0200 | [diff] [blame] | 1901 | return 1; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1902 | } |
| 1903 | |
| 1904 | static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt, |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 1905 | struct x86_emulate_ops *ops) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1906 | { |
| 1907 | struct decode_cache *c = &ctxt->decode; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1908 | |
| 1909 | switch (c->modrm_reg) { |
| 1910 | case 0: /* inc */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1911 | emulate_1op("inc", c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1912 | break; |
| 1913 | case 1: /* dec */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1914 | emulate_1op("dec", c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1915 | break; |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 1916 | case 2: /* call near abs */ { |
| 1917 | long int old_eip; |
| 1918 | old_eip = c->eip; |
| 1919 | c->eip = c->src.val; |
| 1920 | c->src.val = old_eip; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1921 | emulate_push(ctxt, ops); |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 1922 | break; |
| 1923 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1924 | case 4: /* jmp abs */ |
Avi Kivity | fd60754 | 2008-01-18 13:12:26 +0200 | [diff] [blame] | 1925 | c->eip = c->src.val; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1926 | break; |
| 1927 | case 6: /* push */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1928 | emulate_push(ctxt, ops); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1929 | break; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1930 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1931 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 1935 | struct x86_emulate_ops *ops) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1936 | { |
| 1937 | struct decode_cache *c = &ctxt->decode; |
Avi Kivity | 16518d5 | 2010-08-26 14:31:30 +0300 | [diff] [blame] | 1938 | u64 old = c->dst.orig_val64; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1939 | |
| 1940 | if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) || |
| 1941 | ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) { |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1942 | c->regs[VCPU_REGS_RAX] = (u32) (old >> 0); |
| 1943 | c->regs[VCPU_REGS_RDX] = (u32) (old >> 32); |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1944 | ctxt->eflags &= ~EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1945 | } else { |
Avi Kivity | 16518d5 | 2010-08-26 14:31:30 +0300 | [diff] [blame] | 1946 | c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) | |
| 1947 | (u32) c->regs[VCPU_REGS_RBX]; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1948 | |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1949 | ctxt->eflags |= EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1950 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1951 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1952 | } |
| 1953 | |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1954 | static int emulate_ret_far(struct x86_emulate_ctxt *ctxt, |
| 1955 | struct x86_emulate_ops *ops) |
| 1956 | { |
| 1957 | struct decode_cache *c = &ctxt->decode; |
| 1958 | int rc; |
| 1959 | unsigned long cs; |
| 1960 | |
| 1961 | rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1962 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1963 | return rc; |
| 1964 | if (c->op_bytes == 4) |
| 1965 | c->eip = (u32)c->eip; |
| 1966 | rc = emulate_pop(ctxt, ops, &cs, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1967 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1968 | return rc; |
Gleb Natapov | 2e87302 | 2010-03-18 15:20:18 +0200 | [diff] [blame] | 1969 | rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS); |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1970 | return rc; |
| 1971 | } |
| 1972 | |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1973 | static inline void |
| 1974 | setup_syscalls_segments(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1975 | struct x86_emulate_ops *ops, struct desc_struct *cs, |
| 1976 | struct desc_struct *ss) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1977 | { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1978 | memset(cs, 0, sizeof(struct desc_struct)); |
| 1979 | ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu); |
| 1980 | memset(ss, 0, sizeof(struct desc_struct)); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1981 | |
| 1982 | cs->l = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1983 | set_desc_base(cs, 0); /* flat segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1984 | cs->g = 1; /* 4kb granularity */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1985 | set_desc_limit(cs, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1986 | cs->type = 0x0b; /* Read, Execute, Accessed */ |
| 1987 | cs->s = 1; |
| 1988 | cs->dpl = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1989 | cs->p = 1; |
| 1990 | cs->d = 1; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1991 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1992 | set_desc_base(ss, 0); /* flat segment */ |
| 1993 | set_desc_limit(ss, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1994 | ss->g = 1; /* 4kb granularity */ |
| 1995 | ss->s = 1; |
| 1996 | ss->type = 0x03; /* Read/Write, Accessed */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1997 | ss->d = 1; /* 32bit stack segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1998 | ss->dpl = 0; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1999 | ss->p = 1; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | static int |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2003 | emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2004 | { |
| 2005 | struct decode_cache *c = &ctxt->decode; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2006 | struct desc_struct cs, ss; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2007 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2008 | u16 cs_sel, ss_sel; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2009 | |
| 2010 | /* syscall is not available in real mode */ |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 2011 | if (ctxt->mode == X86EMUL_MODE_REAL || |
| 2012 | ctxt->mode == X86EMUL_MODE_VM86) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2013 | emulate_ud(ctxt); |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 2014 | return X86EMUL_PROPAGATE_FAULT; |
| 2015 | } |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2016 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2017 | setup_syscalls_segments(ctxt, ops, &cs, &ss); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2018 | |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2019 | ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2020 | msr_data >>= 32; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2021 | cs_sel = (u16)(msr_data & 0xfffc); |
| 2022 | ss_sel = (u16)(msr_data + 8); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2023 | |
| 2024 | if (is_long_mode(ctxt->vcpu)) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2025 | cs.d = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2026 | cs.l = 1; |
| 2027 | } |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2028 | ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu); |
| 2029 | ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu); |
| 2030 | ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu); |
| 2031 | ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2032 | |
| 2033 | c->regs[VCPU_REGS_RCX] = c->eip; |
| 2034 | if (is_long_mode(ctxt->vcpu)) { |
| 2035 | #ifdef CONFIG_X86_64 |
| 2036 | c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF; |
| 2037 | |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2038 | ops->get_msr(ctxt->vcpu, |
| 2039 | ctxt->mode == X86EMUL_MODE_PROT64 ? |
| 2040 | MSR_LSTAR : MSR_CSTAR, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2041 | c->eip = msr_data; |
| 2042 | |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2043 | ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2044 | ctxt->eflags &= ~(msr_data | EFLG_RF); |
| 2045 | #endif |
| 2046 | } else { |
| 2047 | /* legacy mode */ |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2048 | ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2049 | c->eip = (u32)msr_data; |
| 2050 | |
| 2051 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
| 2052 | } |
| 2053 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2054 | return X86EMUL_CONTINUE; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2055 | } |
| 2056 | |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2057 | static int |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2058 | emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2059 | { |
| 2060 | struct decode_cache *c = &ctxt->decode; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2061 | struct desc_struct cs, ss; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2062 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2063 | u16 cs_sel, ss_sel; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2064 | |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2065 | /* inject #GP if in real mode */ |
| 2066 | if (ctxt->mode == X86EMUL_MODE_REAL) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2067 | emulate_gp(ctxt, 0); |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 2068 | return X86EMUL_PROPAGATE_FAULT; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | /* XXX sysenter/sysexit have not been tested in 64bit mode. |
| 2072 | * Therefore, we inject an #UD. |
| 2073 | */ |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 2074 | if (ctxt->mode == X86EMUL_MODE_PROT64) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2075 | emulate_ud(ctxt); |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 2076 | return X86EMUL_PROPAGATE_FAULT; |
| 2077 | } |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2078 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2079 | setup_syscalls_segments(ctxt, ops, &cs, &ss); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2080 | |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2081 | ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2082 | switch (ctxt->mode) { |
| 2083 | case X86EMUL_MODE_PROT32: |
| 2084 | if ((msr_data & 0xfffc) == 0x0) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2085 | emulate_gp(ctxt, 0); |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2086 | return X86EMUL_PROPAGATE_FAULT; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2087 | } |
| 2088 | break; |
| 2089 | case X86EMUL_MODE_PROT64: |
| 2090 | if (msr_data == 0x0) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2091 | emulate_gp(ctxt, 0); |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2092 | return X86EMUL_PROPAGATE_FAULT; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2093 | } |
| 2094 | break; |
| 2095 | } |
| 2096 | |
| 2097 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2098 | cs_sel = (u16)msr_data; |
| 2099 | cs_sel &= ~SELECTOR_RPL_MASK; |
| 2100 | ss_sel = cs_sel + 8; |
| 2101 | ss_sel &= ~SELECTOR_RPL_MASK; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2102 | if (ctxt->mode == X86EMUL_MODE_PROT64 |
| 2103 | || is_long_mode(ctxt->vcpu)) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2104 | cs.d = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2105 | cs.l = 1; |
| 2106 | } |
| 2107 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2108 | ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu); |
| 2109 | ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu); |
| 2110 | ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu); |
| 2111 | ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2112 | |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2113 | ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2114 | c->eip = msr_data; |
| 2115 | |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2116 | ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2117 | c->regs[VCPU_REGS_RSP] = msr_data; |
| 2118 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2119 | return X86EMUL_CONTINUE; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2120 | } |
| 2121 | |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2122 | static int |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2123 | emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2124 | { |
| 2125 | struct decode_cache *c = &ctxt->decode; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2126 | struct desc_struct cs, ss; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2127 | u64 msr_data; |
| 2128 | int usermode; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2129 | u16 cs_sel, ss_sel; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2130 | |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2131 | /* inject #GP if in real mode or Virtual 8086 mode */ |
| 2132 | if (ctxt->mode == X86EMUL_MODE_REAL || |
| 2133 | ctxt->mode == X86EMUL_MODE_VM86) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2134 | emulate_gp(ctxt, 0); |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 2135 | return X86EMUL_PROPAGATE_FAULT; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2136 | } |
| 2137 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2138 | setup_syscalls_segments(ctxt, ops, &cs, &ss); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2139 | |
| 2140 | if ((c->rex_prefix & 0x8) != 0x0) |
| 2141 | usermode = X86EMUL_MODE_PROT64; |
| 2142 | else |
| 2143 | usermode = X86EMUL_MODE_PROT32; |
| 2144 | |
| 2145 | cs.dpl = 3; |
| 2146 | ss.dpl = 3; |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2147 | ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2148 | switch (usermode) { |
| 2149 | case X86EMUL_MODE_PROT32: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2150 | cs_sel = (u16)(msr_data + 16); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2151 | if ((msr_data & 0xfffc) == 0x0) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2152 | emulate_gp(ctxt, 0); |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2153 | return X86EMUL_PROPAGATE_FAULT; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2154 | } |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2155 | ss_sel = (u16)(msr_data + 24); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2156 | break; |
| 2157 | case X86EMUL_MODE_PROT64: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2158 | cs_sel = (u16)(msr_data + 32); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2159 | if (msr_data == 0x0) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2160 | emulate_gp(ctxt, 0); |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2161 | return X86EMUL_PROPAGATE_FAULT; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2162 | } |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2163 | ss_sel = cs_sel + 8; |
| 2164 | cs.d = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2165 | cs.l = 1; |
| 2166 | break; |
| 2167 | } |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2168 | cs_sel |= SELECTOR_RPL_MASK; |
| 2169 | ss_sel |= SELECTOR_RPL_MASK; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2170 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2171 | ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu); |
| 2172 | ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu); |
| 2173 | ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu); |
| 2174 | ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2175 | |
Gleb Natapov | bdb475a | 2010-04-28 19:15:41 +0300 | [diff] [blame] | 2176 | c->eip = c->regs[VCPU_REGS_RDX]; |
| 2177 | c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX]; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2178 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2179 | return X86EMUL_CONTINUE; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2180 | } |
| 2181 | |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 2182 | static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt, |
| 2183 | struct x86_emulate_ops *ops) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2184 | { |
| 2185 | int iopl; |
| 2186 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 2187 | return false; |
| 2188 | if (ctxt->mode == X86EMUL_MODE_VM86) |
| 2189 | return true; |
| 2190 | iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 2191 | return ops->cpl(ctxt->vcpu) > iopl; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt, |
| 2195 | struct x86_emulate_ops *ops, |
| 2196 | u16 port, u16 len) |
| 2197 | { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2198 | struct desc_struct tr_seg; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2199 | int r; |
| 2200 | u16 io_bitmap_ptr; |
| 2201 | u8 perm, bit_idx = port & 0x7; |
| 2202 | unsigned mask = (1 << len) - 1; |
| 2203 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2204 | ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu); |
| 2205 | if (!tr_seg.p) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2206 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2207 | if (desc_limit_scaled(&tr_seg) < 103) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2208 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2209 | r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2, |
| 2210 | ctxt->vcpu, NULL); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2211 | if (r != X86EMUL_CONTINUE) |
| 2212 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2213 | if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2214 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2215 | r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8, |
| 2216 | &perm, 1, ctxt->vcpu, NULL); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2217 | if (r != X86EMUL_CONTINUE) |
| 2218 | return false; |
| 2219 | if ((perm >> bit_idx) & mask) |
| 2220 | return false; |
| 2221 | return true; |
| 2222 | } |
| 2223 | |
| 2224 | static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt, |
| 2225 | struct x86_emulate_ops *ops, |
| 2226 | u16 port, u16 len) |
| 2227 | { |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 2228 | if (emulator_bad_iopl(ctxt, ops)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2229 | if (!emulator_io_port_access_allowed(ctxt, ops, port, len)) |
| 2230 | return false; |
| 2231 | return true; |
| 2232 | } |
| 2233 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2234 | static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt, |
| 2235 | struct x86_emulate_ops *ops, |
| 2236 | struct tss_segment_16 *tss) |
| 2237 | { |
| 2238 | struct decode_cache *c = &ctxt->decode; |
| 2239 | |
| 2240 | tss->ip = c->eip; |
| 2241 | tss->flag = ctxt->eflags; |
| 2242 | tss->ax = c->regs[VCPU_REGS_RAX]; |
| 2243 | tss->cx = c->regs[VCPU_REGS_RCX]; |
| 2244 | tss->dx = c->regs[VCPU_REGS_RDX]; |
| 2245 | tss->bx = c->regs[VCPU_REGS_RBX]; |
| 2246 | tss->sp = c->regs[VCPU_REGS_RSP]; |
| 2247 | tss->bp = c->regs[VCPU_REGS_RBP]; |
| 2248 | tss->si = c->regs[VCPU_REGS_RSI]; |
| 2249 | tss->di = c->regs[VCPU_REGS_RDI]; |
| 2250 | |
| 2251 | tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu); |
| 2252 | tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu); |
| 2253 | tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu); |
| 2254 | tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu); |
| 2255 | tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu); |
| 2256 | } |
| 2257 | |
| 2258 | static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt, |
| 2259 | struct x86_emulate_ops *ops, |
| 2260 | struct tss_segment_16 *tss) |
| 2261 | { |
| 2262 | struct decode_cache *c = &ctxt->decode; |
| 2263 | int ret; |
| 2264 | |
| 2265 | c->eip = tss->ip; |
| 2266 | ctxt->eflags = tss->flag | 2; |
| 2267 | c->regs[VCPU_REGS_RAX] = tss->ax; |
| 2268 | c->regs[VCPU_REGS_RCX] = tss->cx; |
| 2269 | c->regs[VCPU_REGS_RDX] = tss->dx; |
| 2270 | c->regs[VCPU_REGS_RBX] = tss->bx; |
| 2271 | c->regs[VCPU_REGS_RSP] = tss->sp; |
| 2272 | c->regs[VCPU_REGS_RBP] = tss->bp; |
| 2273 | c->regs[VCPU_REGS_RSI] = tss->si; |
| 2274 | c->regs[VCPU_REGS_RDI] = tss->di; |
| 2275 | |
| 2276 | /* |
| 2277 | * SDM says that segment selectors are loaded before segment |
| 2278 | * descriptors |
| 2279 | */ |
| 2280 | ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu); |
| 2281 | ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu); |
| 2282 | ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu); |
| 2283 | ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu); |
| 2284 | ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu); |
| 2285 | |
| 2286 | /* |
| 2287 | * Now load segment descriptors. If fault happenes at this stage |
| 2288 | * it is handled in a context of new task |
| 2289 | */ |
| 2290 | ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR); |
| 2291 | if (ret != X86EMUL_CONTINUE) |
| 2292 | return ret; |
| 2293 | ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES); |
| 2294 | if (ret != X86EMUL_CONTINUE) |
| 2295 | return ret; |
| 2296 | ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS); |
| 2297 | if (ret != X86EMUL_CONTINUE) |
| 2298 | return ret; |
| 2299 | ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS); |
| 2300 | if (ret != X86EMUL_CONTINUE) |
| 2301 | return ret; |
| 2302 | ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS); |
| 2303 | if (ret != X86EMUL_CONTINUE) |
| 2304 | return ret; |
| 2305 | |
| 2306 | return X86EMUL_CONTINUE; |
| 2307 | } |
| 2308 | |
| 2309 | static int task_switch_16(struct x86_emulate_ctxt *ctxt, |
| 2310 | struct x86_emulate_ops *ops, |
| 2311 | u16 tss_selector, u16 old_tss_sel, |
| 2312 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2313 | { |
| 2314 | struct tss_segment_16 tss_seg; |
| 2315 | int ret; |
| 2316 | u32 err, new_tss_base = get_desc_base(new_desc); |
| 2317 | |
| 2318 | ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu, |
| 2319 | &err); |
| 2320 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
| 2321 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2322 | emulate_pf(ctxt, old_tss_base, err); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2323 | return ret; |
| 2324 | } |
| 2325 | |
| 2326 | save_state_to_tss16(ctxt, ops, &tss_seg); |
| 2327 | |
| 2328 | ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu, |
| 2329 | &err); |
| 2330 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
| 2331 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2332 | emulate_pf(ctxt, old_tss_base, err); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2333 | return ret; |
| 2334 | } |
| 2335 | |
| 2336 | ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu, |
| 2337 | &err); |
| 2338 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
| 2339 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2340 | emulate_pf(ctxt, new_tss_base, err); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2341 | return ret; |
| 2342 | } |
| 2343 | |
| 2344 | if (old_tss_sel != 0xffff) { |
| 2345 | tss_seg.prev_task_link = old_tss_sel; |
| 2346 | |
| 2347 | ret = ops->write_std(new_tss_base, |
| 2348 | &tss_seg.prev_task_link, |
| 2349 | sizeof tss_seg.prev_task_link, |
| 2350 | ctxt->vcpu, &err); |
| 2351 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
| 2352 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2353 | emulate_pf(ctxt, new_tss_base, err); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2354 | return ret; |
| 2355 | } |
| 2356 | } |
| 2357 | |
| 2358 | return load_state_from_tss16(ctxt, ops, &tss_seg); |
| 2359 | } |
| 2360 | |
| 2361 | static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt, |
| 2362 | struct x86_emulate_ops *ops, |
| 2363 | struct tss_segment_32 *tss) |
| 2364 | { |
| 2365 | struct decode_cache *c = &ctxt->decode; |
| 2366 | |
| 2367 | tss->cr3 = ops->get_cr(3, ctxt->vcpu); |
| 2368 | tss->eip = c->eip; |
| 2369 | tss->eflags = ctxt->eflags; |
| 2370 | tss->eax = c->regs[VCPU_REGS_RAX]; |
| 2371 | tss->ecx = c->regs[VCPU_REGS_RCX]; |
| 2372 | tss->edx = c->regs[VCPU_REGS_RDX]; |
| 2373 | tss->ebx = c->regs[VCPU_REGS_RBX]; |
| 2374 | tss->esp = c->regs[VCPU_REGS_RSP]; |
| 2375 | tss->ebp = c->regs[VCPU_REGS_RBP]; |
| 2376 | tss->esi = c->regs[VCPU_REGS_RSI]; |
| 2377 | tss->edi = c->regs[VCPU_REGS_RDI]; |
| 2378 | |
| 2379 | tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu); |
| 2380 | tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu); |
| 2381 | tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu); |
| 2382 | tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu); |
| 2383 | tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu); |
| 2384 | tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu); |
| 2385 | tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu); |
| 2386 | } |
| 2387 | |
| 2388 | static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, |
| 2389 | struct x86_emulate_ops *ops, |
| 2390 | struct tss_segment_32 *tss) |
| 2391 | { |
| 2392 | struct decode_cache *c = &ctxt->decode; |
| 2393 | int ret; |
| 2394 | |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 2395 | if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2396 | emulate_gp(ctxt, 0); |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 2397 | return X86EMUL_PROPAGATE_FAULT; |
| 2398 | } |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2399 | c->eip = tss->eip; |
| 2400 | ctxt->eflags = tss->eflags | 2; |
| 2401 | c->regs[VCPU_REGS_RAX] = tss->eax; |
| 2402 | c->regs[VCPU_REGS_RCX] = tss->ecx; |
| 2403 | c->regs[VCPU_REGS_RDX] = tss->edx; |
| 2404 | c->regs[VCPU_REGS_RBX] = tss->ebx; |
| 2405 | c->regs[VCPU_REGS_RSP] = tss->esp; |
| 2406 | c->regs[VCPU_REGS_RBP] = tss->ebp; |
| 2407 | c->regs[VCPU_REGS_RSI] = tss->esi; |
| 2408 | c->regs[VCPU_REGS_RDI] = tss->edi; |
| 2409 | |
| 2410 | /* |
| 2411 | * SDM says that segment selectors are loaded before segment |
| 2412 | * descriptors |
| 2413 | */ |
| 2414 | ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu); |
| 2415 | ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu); |
| 2416 | ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu); |
| 2417 | ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu); |
| 2418 | ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu); |
| 2419 | ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu); |
| 2420 | ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu); |
| 2421 | |
| 2422 | /* |
| 2423 | * Now load segment descriptors. If fault happenes at this stage |
| 2424 | * it is handled in a context of new task |
| 2425 | */ |
| 2426 | ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR); |
| 2427 | if (ret != X86EMUL_CONTINUE) |
| 2428 | return ret; |
| 2429 | ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES); |
| 2430 | if (ret != X86EMUL_CONTINUE) |
| 2431 | return ret; |
| 2432 | ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS); |
| 2433 | if (ret != X86EMUL_CONTINUE) |
| 2434 | return ret; |
| 2435 | ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS); |
| 2436 | if (ret != X86EMUL_CONTINUE) |
| 2437 | return ret; |
| 2438 | ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS); |
| 2439 | if (ret != X86EMUL_CONTINUE) |
| 2440 | return ret; |
| 2441 | ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS); |
| 2442 | if (ret != X86EMUL_CONTINUE) |
| 2443 | return ret; |
| 2444 | ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS); |
| 2445 | if (ret != X86EMUL_CONTINUE) |
| 2446 | return ret; |
| 2447 | |
| 2448 | return X86EMUL_CONTINUE; |
| 2449 | } |
| 2450 | |
| 2451 | static int task_switch_32(struct x86_emulate_ctxt *ctxt, |
| 2452 | struct x86_emulate_ops *ops, |
| 2453 | u16 tss_selector, u16 old_tss_sel, |
| 2454 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2455 | { |
| 2456 | struct tss_segment_32 tss_seg; |
| 2457 | int ret; |
| 2458 | u32 err, new_tss_base = get_desc_base(new_desc); |
| 2459 | |
| 2460 | ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu, |
| 2461 | &err); |
| 2462 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
| 2463 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2464 | emulate_pf(ctxt, old_tss_base, err); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2465 | return ret; |
| 2466 | } |
| 2467 | |
| 2468 | save_state_to_tss32(ctxt, ops, &tss_seg); |
| 2469 | |
| 2470 | ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu, |
| 2471 | &err); |
| 2472 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
| 2473 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2474 | emulate_pf(ctxt, old_tss_base, err); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2475 | return ret; |
| 2476 | } |
| 2477 | |
| 2478 | ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu, |
| 2479 | &err); |
| 2480 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
| 2481 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2482 | emulate_pf(ctxt, new_tss_base, err); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2483 | return ret; |
| 2484 | } |
| 2485 | |
| 2486 | if (old_tss_sel != 0xffff) { |
| 2487 | tss_seg.prev_task_link = old_tss_sel; |
| 2488 | |
| 2489 | ret = ops->write_std(new_tss_base, |
| 2490 | &tss_seg.prev_task_link, |
| 2491 | sizeof tss_seg.prev_task_link, |
| 2492 | ctxt->vcpu, &err); |
| 2493 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
| 2494 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2495 | emulate_pf(ctxt, new_tss_base, err); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2496 | return ret; |
| 2497 | } |
| 2498 | } |
| 2499 | |
| 2500 | return load_state_from_tss32(ctxt, ops, &tss_seg); |
| 2501 | } |
| 2502 | |
| 2503 | static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2504 | struct x86_emulate_ops *ops, |
| 2505 | u16 tss_selector, int reason, |
| 2506 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2507 | { |
| 2508 | struct desc_struct curr_tss_desc, next_tss_desc; |
| 2509 | int ret; |
| 2510 | u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu); |
| 2511 | ulong old_tss_base = |
Gleb Natapov | 5951c44 | 2010-04-28 19:15:29 +0300 | [diff] [blame] | 2512 | ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu); |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2513 | u32 desc_limit; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2514 | |
| 2515 | /* FIXME: old_tss_base == ~0 ? */ |
| 2516 | |
| 2517 | ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc); |
| 2518 | if (ret != X86EMUL_CONTINUE) |
| 2519 | return ret; |
| 2520 | ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc); |
| 2521 | if (ret != X86EMUL_CONTINUE) |
| 2522 | return ret; |
| 2523 | |
| 2524 | /* FIXME: check that next_tss_desc is tss */ |
| 2525 | |
| 2526 | if (reason != TASK_SWITCH_IRET) { |
| 2527 | if ((tss_selector & 3) > next_tss_desc.dpl || |
| 2528 | ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2529 | emulate_gp(ctxt, 0); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2530 | return X86EMUL_PROPAGATE_FAULT; |
| 2531 | } |
| 2532 | } |
| 2533 | |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2534 | desc_limit = desc_limit_scaled(&next_tss_desc); |
| 2535 | if (!next_tss_desc.p || |
| 2536 | ((desc_limit < 0x67 && (next_tss_desc.type & 8)) || |
| 2537 | desc_limit < 0x2b)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2538 | emulate_ts(ctxt, tss_selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2539 | return X86EMUL_PROPAGATE_FAULT; |
| 2540 | } |
| 2541 | |
| 2542 | if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) { |
| 2543 | curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */ |
| 2544 | write_segment_descriptor(ctxt, ops, old_tss_sel, |
| 2545 | &curr_tss_desc); |
| 2546 | } |
| 2547 | |
| 2548 | if (reason == TASK_SWITCH_IRET) |
| 2549 | ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT; |
| 2550 | |
| 2551 | /* set back link to prev task only if NT bit is set in eflags |
| 2552 | note that old_tss_sel is not used afetr this point */ |
| 2553 | if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE) |
| 2554 | old_tss_sel = 0xffff; |
| 2555 | |
| 2556 | if (next_tss_desc.type & 8) |
| 2557 | ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel, |
| 2558 | old_tss_base, &next_tss_desc); |
| 2559 | else |
| 2560 | ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel, |
| 2561 | old_tss_base, &next_tss_desc); |
Jan Kiszka | 0760d44 | 2010-04-14 15:50:57 +0200 | [diff] [blame] | 2562 | if (ret != X86EMUL_CONTINUE) |
| 2563 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2564 | |
| 2565 | if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) |
| 2566 | ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT; |
| 2567 | |
| 2568 | if (reason != TASK_SWITCH_IRET) { |
| 2569 | next_tss_desc.type |= (1 << 1); /* set busy flag */ |
| 2570 | write_segment_descriptor(ctxt, ops, tss_selector, |
| 2571 | &next_tss_desc); |
| 2572 | } |
| 2573 | |
| 2574 | ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu); |
| 2575 | ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu); |
| 2576 | ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu); |
| 2577 | |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2578 | if (has_error_code) { |
| 2579 | struct decode_cache *c = &ctxt->decode; |
| 2580 | |
| 2581 | c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2; |
| 2582 | c->lock_prefix = 0; |
| 2583 | c->src.val = (unsigned long) error_code; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2584 | emulate_push(ctxt, ops); |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2585 | } |
| 2586 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2587 | return ret; |
| 2588 | } |
| 2589 | |
| 2590 | int emulator_task_switch(struct x86_emulate_ctxt *ctxt, |
| 2591 | struct x86_emulate_ops *ops, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2592 | u16 tss_selector, int reason, |
| 2593 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2594 | { |
| 2595 | struct decode_cache *c = &ctxt->decode; |
| 2596 | int rc; |
| 2597 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2598 | c->eip = ctxt->eip; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2599 | c->dst.type = OP_NONE; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2600 | |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2601 | rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason, |
| 2602 | has_error_code, error_code); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2603 | |
| 2604 | if (rc == X86EMUL_CONTINUE) { |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2605 | rc = writeback(ctxt, ops); |
Gleb Natapov | 95c5588 | 2010-04-28 19:15:39 +0300 | [diff] [blame] | 2606 | if (rc == X86EMUL_CONTINUE) |
| 2607 | ctxt->eip = c->eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2608 | } |
| 2609 | |
Gleb Natapov | 19d0443 | 2010-04-15 12:29:50 +0300 | [diff] [blame] | 2610 | return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2611 | } |
| 2612 | |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2613 | static void string_addr_inc(struct x86_emulate_ctxt *ctxt, unsigned long base, |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 2614 | int reg, struct operand *op) |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2615 | { |
| 2616 | struct decode_cache *c = &ctxt->decode; |
| 2617 | int df = (ctxt->eflags & EFLG_DF) ? -1 : 1; |
| 2618 | |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 2619 | register_address_increment(c, &c->regs[reg], df * op->bytes); |
| 2620 | op->ptr = (unsigned long *)register_address(c, base, c->regs[reg]); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2621 | } |
| 2622 | |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2623 | int |
Laurent Vivier | 1be3aa4 | 2007-09-18 11:27:27 +0200 | [diff] [blame] | 2624 | x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2625 | { |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2626 | u64 msr_data; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2627 | struct decode_cache *c = &ctxt->decode; |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2628 | int rc = X86EMUL_CONTINUE; |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 2629 | int saved_dst_type = c->dst.type; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2630 | |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 2631 | ctxt->decode.mem_read.pos = 0; |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 2632 | |
Gleb Natapov | 1161624 | 2010-02-11 14:43:14 +0200 | [diff] [blame] | 2633 | if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2634 | emulate_ud(ctxt); |
Gleb Natapov | 1161624 | 2010-02-11 14:43:14 +0200 | [diff] [blame] | 2635 | goto done; |
| 2636 | } |
| 2637 | |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 2638 | /* LOCK prefix is allowed only with some instructions */ |
Gleb Natapov | a41ffb75 | 2010-03-18 15:20:14 +0200 | [diff] [blame] | 2639 | if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2640 | emulate_ud(ctxt); |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 2641 | goto done; |
| 2642 | } |
| 2643 | |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 2644 | /* Privileged instruction can be executed only in CPL=0 */ |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 2645 | if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2646 | emulate_gp(ctxt, 0); |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 2647 | goto done; |
| 2648 | } |
| 2649 | |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 2650 | if (c->rep_prefix && (c->d & String)) { |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 2651 | ctxt->restart = true; |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 2652 | /* All REP prefixes have the same first termination condition */ |
Gleb Natapov | c73e197 | 2010-03-15 16:38:29 +0200 | [diff] [blame] | 2653 | if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) { |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 2654 | string_done: |
| 2655 | ctxt->restart = false; |
Gleb Natapov | 95c5588 | 2010-04-28 19:15:39 +0300 | [diff] [blame] | 2656 | ctxt->eip = c->eip; |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 2657 | goto done; |
| 2658 | } |
| 2659 | /* The second termination condition only applies for REPE |
| 2660 | * and REPNE. Test if the repeat string operation prefix is |
| 2661 | * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the |
| 2662 | * corresponding termination condition according to: |
| 2663 | * - if REPE/REPZ and ZF = 0 then done |
| 2664 | * - if REPNE/REPNZ and ZF = 1 then done |
| 2665 | */ |
| 2666 | if ((c->b == 0xa6) || (c->b == 0xa7) || |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 2667 | (c->b == 0xae) || (c->b == 0xaf)) { |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 2668 | if ((c->rep_prefix == REPE_PREFIX) && |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 2669 | ((ctxt->eflags & EFLG_ZF) == 0)) |
| 2670 | goto string_done; |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 2671 | if ((c->rep_prefix == REPNE_PREFIX) && |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 2672 | ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) |
| 2673 | goto string_done; |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 2674 | } |
Gleb Natapov | 063db06 | 2010-03-18 15:20:06 +0200 | [diff] [blame] | 2675 | c->eip = ctxt->eip; |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 2676 | } |
| 2677 | |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2678 | if (c->src.type == OP_MEM) { |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 2679 | rc = read_emulated(ctxt, ops, (unsigned long)c->src.ptr, |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 2680 | c->src.valptr, c->src.bytes); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 2681 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2682 | goto done; |
Avi Kivity | 16518d5 | 2010-08-26 14:31:30 +0300 | [diff] [blame] | 2683 | c->src.orig_val64 = c->src.val64; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2684 | } |
| 2685 | |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 2686 | if (c->src2.type == OP_MEM) { |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 2687 | rc = read_emulated(ctxt, ops, (unsigned long)c->src2.ptr, |
| 2688 | &c->src2.val, c->src2.bytes); |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 2689 | if (rc != X86EMUL_CONTINUE) |
| 2690 | goto done; |
| 2691 | } |
| 2692 | |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2693 | if ((c->d & DstMask) == ImplicitOps) |
| 2694 | goto special_insn; |
| 2695 | |
| 2696 | |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 2697 | if ((c->dst.type == OP_MEM) && !(c->d & Mov)) { |
| 2698 | /* optimisation - avoid slow emulated read if Mov */ |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 2699 | rc = read_emulated(ctxt, ops, (unsigned long)c->dst.ptr, |
| 2700 | &c->dst.val, c->dst.bytes); |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 2701 | if (rc != X86EMUL_CONTINUE) |
| 2702 | goto done; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 2703 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2704 | c->dst.orig_val = c->dst.val; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 2705 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2706 | special_insn: |
| 2707 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2708 | if (c->twobyte) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2709 | goto twobyte_insn; |
| 2710 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2711 | switch (c->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2712 | case 0x00 ... 0x05: |
| 2713 | add: /* add */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2714 | emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2715 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2716 | case 0x06: /* push es */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2717 | emulate_push_sreg(ctxt, ops, VCPU_SREG_ES); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2718 | break; |
| 2719 | case 0x07: /* pop es */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2720 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2721 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2722 | goto done; |
| 2723 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2724 | case 0x08 ... 0x0d: |
| 2725 | or: /* or */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2726 | emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2727 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2728 | case 0x0e: /* push cs */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2729 | emulate_push_sreg(ctxt, ops, VCPU_SREG_CS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2730 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2731 | case 0x10 ... 0x15: |
| 2732 | adc: /* adc */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2733 | emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2734 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2735 | case 0x16: /* push ss */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2736 | emulate_push_sreg(ctxt, ops, VCPU_SREG_SS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2737 | break; |
| 2738 | case 0x17: /* pop ss */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2739 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2740 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2741 | goto done; |
| 2742 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2743 | case 0x18 ... 0x1d: |
| 2744 | sbb: /* sbb */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2745 | emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2746 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2747 | case 0x1e: /* push ds */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2748 | emulate_push_sreg(ctxt, ops, VCPU_SREG_DS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2749 | break; |
| 2750 | case 0x1f: /* pop ds */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2751 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2752 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2753 | goto done; |
| 2754 | break; |
Guillaume Thouvenin | aa3a816 | 2008-09-12 13:52:18 +0200 | [diff] [blame] | 2755 | case 0x20 ... 0x25: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2756 | and: /* and */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2757 | emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2758 | break; |
| 2759 | case 0x28 ... 0x2d: |
| 2760 | sub: /* sub */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2761 | emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2762 | break; |
| 2763 | case 0x30 ... 0x35: |
| 2764 | xor: /* xor */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2765 | emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2766 | break; |
| 2767 | case 0x38 ... 0x3d: |
| 2768 | cmp: /* cmp */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2769 | emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2770 | break; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 2771 | case 0x40 ... 0x47: /* inc r16/r32 */ |
| 2772 | emulate_1op("inc", c->dst, ctxt->eflags); |
| 2773 | break; |
| 2774 | case 0x48 ... 0x4f: /* dec r16/r32 */ |
| 2775 | emulate_1op("dec", c->dst, ctxt->eflags); |
| 2776 | break; |
| 2777 | case 0x50 ... 0x57: /* push reg */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2778 | emulate_push(ctxt, ops); |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 2779 | break; |
| 2780 | case 0x58 ... 0x5f: /* pop reg */ |
| 2781 | pop_instruction: |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 2782 | rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2783 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 2784 | goto done; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 2785 | break; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 2786 | case 0x60: /* pusha */ |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 2787 | rc = emulate_pusha(ctxt, ops); |
| 2788 | if (rc != X86EMUL_CONTINUE) |
| 2789 | goto done; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 2790 | break; |
| 2791 | case 0x61: /* popa */ |
| 2792 | rc = emulate_popa(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2793 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 2794 | goto done; |
| 2795 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2796 | case 0x63: /* movsxd */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2797 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2798 | goto cannot_emulate; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2799 | c->dst.val = (s32) c->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2800 | break; |
Avi Kivity | 91ed7a0 | 2008-05-29 14:38:38 +0300 | [diff] [blame] | 2801 | case 0x68: /* push imm */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2802 | case 0x6a: /* push imm8 */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2803 | emulate_push(ctxt, ops); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2804 | break; |
| 2805 | case 0x6c: /* insb */ |
| 2806 | case 0x6d: /* insw/insd */ |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 2807 | c->dst.bytes = min(c->dst.bytes, 4u); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2808 | if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX], |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 2809 | c->dst.bytes)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2810 | emulate_gp(ctxt, 0); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2811 | goto done; |
| 2812 | } |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 2813 | if (!pio_in_emulated(ctxt, ops, c->dst.bytes, |
| 2814 | c->regs[VCPU_REGS_RDX], &c->dst.val)) |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 2815 | goto done; /* IO is needed, skip writeback */ |
| 2816 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2817 | case 0x6e: /* outsb */ |
| 2818 | case 0x6f: /* outsw/outsd */ |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 2819 | c->src.bytes = min(c->src.bytes, 4u); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2820 | if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX], |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 2821 | c->src.bytes)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2822 | emulate_gp(ctxt, 0); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2823 | goto done; |
| 2824 | } |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 2825 | ops->pio_out_emulated(c->src.bytes, c->regs[VCPU_REGS_RDX], |
| 2826 | &c->src.val, 1, ctxt->vcpu); |
| 2827 | |
| 2828 | c->dst.type = OP_NONE; /* nothing to writeback */ |
| 2829 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 2830 | case 0x70 ... 0x7f: /* jcc (short) */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2831 | if (test_cc(c->b, ctxt->eflags)) |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 2832 | jmp_rel(c, c->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2833 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2834 | case 0x80 ... 0x83: /* Grp1 */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2835 | switch (c->modrm_reg) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2836 | case 0: |
| 2837 | goto add; |
| 2838 | case 1: |
| 2839 | goto or; |
| 2840 | case 2: |
| 2841 | goto adc; |
| 2842 | case 3: |
| 2843 | goto sbb; |
| 2844 | case 4: |
| 2845 | goto and; |
| 2846 | case 5: |
| 2847 | goto sub; |
| 2848 | case 6: |
| 2849 | goto xor; |
| 2850 | case 7: |
| 2851 | goto cmp; |
| 2852 | } |
| 2853 | break; |
| 2854 | case 0x84 ... 0x85: |
Mohammed Gamal | dfb507c | 2010-05-11 22:22:40 +0300 | [diff] [blame] | 2855 | test: |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2856 | emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2857 | break; |
| 2858 | case 0x86 ... 0x87: /* xchg */ |
Mohammed Gamal | b13354f | 2008-06-15 19:37:38 +0300 | [diff] [blame] | 2859 | xchg: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2860 | /* Write back the register source. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2861 | switch (c->dst.bytes) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2862 | case 1: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2863 | *(u8 *) c->src.ptr = (u8) c->dst.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2864 | break; |
| 2865 | case 2: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2866 | *(u16 *) c->src.ptr = (u16) c->dst.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2867 | break; |
| 2868 | case 4: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2869 | *c->src.ptr = (u32) c->dst.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2870 | break; /* 64b reg: zero-extend */ |
| 2871 | case 8: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2872 | *c->src.ptr = c->dst.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2873 | break; |
| 2874 | } |
| 2875 | /* |
| 2876 | * Write back the memory destination with implicit LOCK |
| 2877 | * prefix. |
| 2878 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2879 | c->dst.val = c->src.val; |
| 2880 | c->lock_prefix = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2881 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2882 | case 0x88 ... 0x8b: /* mov */ |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 2883 | goto mov; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2884 | case 0x8c: /* mov r/m, sreg */ |
| 2885 | if (c->modrm_reg > VCPU_SREG_GS) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2886 | emulate_ud(ctxt); |
Gleb Natapov | 5e3ae6c | 2010-03-18 15:20:07 +0200 | [diff] [blame] | 2887 | goto done; |
Guillaume Thouvenin | 38d5bc6 | 2008-05-27 15:13:28 +0200 | [diff] [blame] | 2888 | } |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2889 | c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu); |
Guillaume Thouvenin | 38d5bc6 | 2008-05-27 15:13:28 +0200 | [diff] [blame] | 2890 | break; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 2891 | case 0x8d: /* lea r16/r32, m */ |
Avi Kivity | f9b7aab | 2008-04-14 23:46:37 +0300 | [diff] [blame] | 2892 | c->dst.val = c->modrm_ea; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 2893 | break; |
Guillaume Thouvenin | 4257198 | 2008-05-27 14:49:15 +0200 | [diff] [blame] | 2894 | case 0x8e: { /* mov seg, r/m16 */ |
| 2895 | uint16_t sel; |
Guillaume Thouvenin | 4257198 | 2008-05-27 14:49:15 +0200 | [diff] [blame] | 2896 | |
| 2897 | sel = c->src.val; |
Gleb Natapov | 8b9f441 | 2010-02-18 12:14:59 +0200 | [diff] [blame] | 2898 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 2899 | if (c->modrm_reg == VCPU_SREG_CS || |
| 2900 | c->modrm_reg > VCPU_SREG_GS) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2901 | emulate_ud(ctxt); |
Gleb Natapov | 8b9f441 | 2010-02-18 12:14:59 +0200 | [diff] [blame] | 2902 | goto done; |
| 2903 | } |
| 2904 | |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 2905 | if (c->modrm_reg == VCPU_SREG_SS) |
Gleb Natapov | 95cb229 | 2010-04-28 19:15:43 +0300 | [diff] [blame] | 2906 | ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 2907 | |
Gleb Natapov | 2e87302 | 2010-03-18 15:20:18 +0200 | [diff] [blame] | 2908 | rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg); |
Guillaume Thouvenin | 4257198 | 2008-05-27 14:49:15 +0200 | [diff] [blame] | 2909 | |
| 2910 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2911 | break; |
| 2912 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2913 | case 0x8f: /* pop (sole member of Grp1a) */ |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2914 | rc = emulate_grp1a(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2915 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2916 | goto done; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2917 | break; |
Mohammed Gamal | b13354f | 2008-06-15 19:37:38 +0300 | [diff] [blame] | 2918 | case 0x90: /* nop / xchg r8,rax */ |
Gleb Natapov | b8a9894 | 2010-04-28 19:15:25 +0300 | [diff] [blame] | 2919 | if (c->dst.ptr == (unsigned long *)&c->regs[VCPU_REGS_RAX]) { |
| 2920 | c->dst.type = OP_NONE; /* nop */ |
Mohammed Gamal | b13354f | 2008-06-15 19:37:38 +0300 | [diff] [blame] | 2921 | break; |
| 2922 | } |
| 2923 | case 0x91 ... 0x97: /* xchg reg,rax */ |
Gleb Natapov | f0c13ef | 2010-04-28 19:15:24 +0300 | [diff] [blame] | 2924 | c->src.type = OP_REG; |
| 2925 | c->src.bytes = c->op_bytes; |
Mohammed Gamal | b13354f | 2008-06-15 19:37:38 +0300 | [diff] [blame] | 2926 | c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX]; |
| 2927 | c->src.val = *(c->src.ptr); |
| 2928 | goto xchg; |
Nitin A Kamble | fd2a760 | 2007-08-28 18:22:47 -0700 | [diff] [blame] | 2929 | case 0x9c: /* pushf */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2930 | c->src.val = (unsigned long) ctxt->eflags; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2931 | emulate_push(ctxt, ops); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2932 | break; |
Nitin A Kamble | 535eabc | 2007-09-15 10:45:05 +0300 | [diff] [blame] | 2933 | case 0x9d: /* popf */ |
Avi Kivity | 2b48cc7 | 2008-11-29 20:36:13 +0200 | [diff] [blame] | 2934 | c->dst.type = OP_REG; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2935 | c->dst.ptr = (unsigned long *) &ctxt->eflags; |
Avi Kivity | 2b48cc7 | 2008-11-29 20:36:13 +0200 | [diff] [blame] | 2936 | c->dst.bytes = c->op_bytes; |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 2937 | rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes); |
| 2938 | if (rc != X86EMUL_CONTINUE) |
| 2939 | goto done; |
| 2940 | break; |
Wei Yongjun | 5d55f29 | 2010-07-07 17:43:35 +0800 | [diff] [blame] | 2941 | case 0xa0 ... 0xa3: /* mov */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2942 | case 0xa4 ... 0xa5: /* movs */ |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2943 | goto mov; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2944 | case 0xa6 ... 0xa7: /* cmps */ |
Guillaume Thouvenin | d7e5117 | 2007-11-26 13:49:09 +0100 | [diff] [blame] | 2945 | c->dst.type = OP_NONE; /* Disable writeback. */ |
Guillaume Thouvenin | d7e5117 | 2007-11-26 13:49:09 +0100 | [diff] [blame] | 2946 | DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2947 | goto cmp; |
Mohammed Gamal | dfb507c | 2010-05-11 22:22:40 +0300 | [diff] [blame] | 2948 | case 0xa8 ... 0xa9: /* test ax, imm */ |
| 2949 | goto test; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2950 | case 0xaa ... 0xab: /* stos */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2951 | c->dst.val = c->regs[VCPU_REGS_RAX]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2952 | break; |
| 2953 | case 0xac ... 0xad: /* lods */ |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2954 | goto mov; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2955 | case 0xae ... 0xaf: /* scas */ |
| 2956 | DPRINTF("Urk! I don't handle SCAS.\n"); |
| 2957 | goto cannot_emulate; |
Mohammed Gamal | a5e2e82 | 2008-08-27 05:02:56 +0300 | [diff] [blame] | 2958 | case 0xb0 ... 0xbf: /* mov r, imm */ |
Guillaume Thouvenin | 615ac12 | 2008-05-27 10:19:16 +0200 | [diff] [blame] | 2959 | goto mov; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2960 | case 0xc0 ... 0xc1: |
| 2961 | emulate_grp2(ctxt); |
| 2962 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2963 | case 0xc3: /* ret */ |
Avi Kivity | cf5de4f | 2008-11-28 00:14:07 +0200 | [diff] [blame] | 2964 | c->dst.type = OP_REG; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2965 | c->dst.ptr = &c->eip; |
Avi Kivity | cf5de4f | 2008-11-28 00:14:07 +0200 | [diff] [blame] | 2966 | c->dst.bytes = c->op_bytes; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2967 | goto pop_instruction; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2968 | case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */ |
| 2969 | mov: |
| 2970 | c->dst.val = c->src.val; |
| 2971 | break; |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2972 | case 0xcb: /* ret far */ |
| 2973 | rc = emulate_ret_far(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2974 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2975 | goto done; |
| 2976 | break; |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2977 | case 0xcf: /* iret */ |
| 2978 | rc = emulate_iret(ctxt, ops); |
| 2979 | |
| 2980 | if (rc != X86EMUL_CONTINUE) |
| 2981 | goto done; |
| 2982 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2983 | case 0xd0 ... 0xd1: /* Grp2 */ |
| 2984 | c->src.val = 1; |
| 2985 | emulate_grp2(ctxt); |
| 2986 | break; |
| 2987 | case 0xd2 ... 0xd3: /* Grp2 */ |
| 2988 | c->src.val = c->regs[VCPU_REGS_RCX]; |
| 2989 | emulate_grp2(ctxt); |
| 2990 | break; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 2991 | case 0xe4: /* inb */ |
| 2992 | case 0xe5: /* in */ |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 2993 | goto do_io_in; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 2994 | case 0xe6: /* outb */ |
| 2995 | case 0xe7: /* out */ |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 2996 | goto do_io_out; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 2997 | case 0xe8: /* call (near) */ { |
Gleb Natapov | d53c477 | 2009-04-12 13:36:36 +0300 | [diff] [blame] | 2998 | long int rel = c->src.val; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2999 | c->src.val = (unsigned long) c->eip; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 3000 | jmp_rel(c, rel); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3001 | emulate_push(ctxt, ops); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 3002 | break; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 3003 | } |
| 3004 | case 0xe9: /* jmp rel */ |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 3005 | goto jmp; |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 3006 | case 0xea: { /* jmp far */ |
| 3007 | unsigned short sel; |
Gleb Natapov | ea79849d | 2010-02-25 16:36:43 +0200 | [diff] [blame] | 3008 | jump_far: |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 3009 | memcpy(&sel, c->src.valptr + c->op_bytes, 2); |
| 3010 | |
| 3011 | if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS)) |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 3012 | goto done; |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 3013 | |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 3014 | c->eip = 0; |
| 3015 | memcpy(&c->eip, c->src.valptr, c->op_bytes); |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 3016 | break; |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 3017 | } |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 3018 | case 0xeb: |
| 3019 | jmp: /* jmp rel short */ |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 3020 | jmp_rel(c, c->src.val); |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 3021 | c->dst.type = OP_NONE; /* Disable writeback. */ |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 3022 | break; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 3023 | case 0xec: /* in al,dx */ |
| 3024 | case 0xed: /* in (e/r)ax,dx */ |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3025 | c->src.val = c->regs[VCPU_REGS_RDX]; |
| 3026 | do_io_in: |
| 3027 | c->dst.bytes = min(c->dst.bytes, 4u); |
| 3028 | if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3029 | emulate_gp(ctxt, 0); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 3030 | goto done; |
| 3031 | } |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 3032 | if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val, |
| 3033 | &c->dst.val)) |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3034 | goto done; /* IO is needed */ |
| 3035 | break; |
Wei Yongjun | ce7a0ad | 2010-07-06 16:50:21 +0800 | [diff] [blame] | 3036 | case 0xee: /* out dx,al */ |
| 3037 | case 0xef: /* out dx,(e/r)ax */ |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3038 | c->src.val = c->regs[VCPU_REGS_RDX]; |
| 3039 | do_io_out: |
| 3040 | c->dst.bytes = min(c->dst.bytes, 4u); |
| 3041 | if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3042 | emulate_gp(ctxt, 0); |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3043 | goto done; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 3044 | } |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3045 | ops->pio_out_emulated(c->dst.bytes, c->src.val, &c->dst.val, 1, |
| 3046 | ctxt->vcpu); |
| 3047 | c->dst.type = OP_NONE; /* Disable writeback. */ |
Guillaume Thouvenin | e93f36b | 2008-10-28 10:51:30 +0100 | [diff] [blame] | 3048 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3049 | case 0xf4: /* hlt */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3050 | ctxt->vcpu->arch.halt_request = 1; |
Mohammed Gamal | 19fdfa0 | 2008-07-06 16:51:26 +0300 | [diff] [blame] | 3051 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3052 | case 0xf5: /* cmc */ |
| 3053 | /* complement carry flag from eflags reg */ |
| 3054 | ctxt->eflags ^= EFLG_CF; |
| 3055 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 3056 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3057 | case 0xf6 ... 0xf7: /* Grp3 */ |
Gleb Natapov | aca06a8 | 2010-03-18 15:20:15 +0200 | [diff] [blame] | 3058 | if (!emulate_grp3(ctxt, ops)) |
| 3059 | goto cannot_emulate; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3060 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3061 | case 0xf8: /* clc */ |
| 3062 | ctxt->eflags &= ~EFLG_CF; |
| 3063 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 3064 | break; |
| 3065 | case 0xfa: /* cli */ |
Wei Yongjun | 07cbc6c | 2010-07-06 16:54:19 +0800 | [diff] [blame] | 3066 | if (emulator_bad_iopl(ctxt, ops)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3067 | emulate_gp(ctxt, 0); |
Wei Yongjun | 07cbc6c | 2010-07-06 16:54:19 +0800 | [diff] [blame] | 3068 | goto done; |
| 3069 | } else { |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 3070 | ctxt->eflags &= ~X86_EFLAGS_IF; |
| 3071 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 3072 | } |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3073 | break; |
| 3074 | case 0xfb: /* sti */ |
Wei Yongjun | 07cbc6c | 2010-07-06 16:54:19 +0800 | [diff] [blame] | 3075 | if (emulator_bad_iopl(ctxt, ops)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3076 | emulate_gp(ctxt, 0); |
Wei Yongjun | 07cbc6c | 2010-07-06 16:54:19 +0800 | [diff] [blame] | 3077 | goto done; |
| 3078 | } else { |
Gleb Natapov | 95cb229 | 2010-04-28 19:15:43 +0300 | [diff] [blame] | 3079 | ctxt->interruptibility = KVM_X86_SHADOW_INT_STI; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 3080 | ctxt->eflags |= X86_EFLAGS_IF; |
| 3081 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 3082 | } |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3083 | break; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 3084 | case 0xfc: /* cld */ |
| 3085 | ctxt->eflags &= ~EFLG_DF; |
| 3086 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 3087 | break; |
| 3088 | case 0xfd: /* std */ |
| 3089 | ctxt->eflags |= EFLG_DF; |
| 3090 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 3091 | break; |
Gleb Natapov | ea79849d | 2010-02-25 16:36:43 +0200 | [diff] [blame] | 3092 | case 0xfe: /* Grp4 */ |
| 3093 | grp45: |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3094 | rc = emulate_grp45(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3095 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3096 | goto done; |
| 3097 | break; |
Gleb Natapov | ea79849d | 2010-02-25 16:36:43 +0200 | [diff] [blame] | 3098 | case 0xff: /* Grp5 */ |
| 3099 | if (c->modrm_reg == 5) |
| 3100 | goto jump_far; |
| 3101 | goto grp45; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 3102 | default: |
| 3103 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3104 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3105 | |
| 3106 | writeback: |
| 3107 | rc = writeback(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3108 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3109 | goto done; |
| 3110 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 3111 | /* |
| 3112 | * restore dst type in case the decoding will be reused |
| 3113 | * (happens for string instruction ) |
| 3114 | */ |
| 3115 | c->dst.type = saved_dst_type; |
| 3116 | |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 3117 | if ((c->d & SrcMask) == SrcSI) |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3118 | string_addr_inc(ctxt, seg_override_base(ctxt, ops, c), |
| 3119 | VCPU_REGS_RSI, &c->src); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 3120 | |
| 3121 | if ((c->d & DstMask) == DstDI) |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3122 | string_addr_inc(ctxt, es_base(ctxt, ops), VCPU_REGS_RDI, |
| 3123 | &c->dst); |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 3124 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 3125 | if (c->rep_prefix && (c->d & String)) { |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 3126 | struct read_cache *rc = &ctxt->decode.io_read; |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 3127 | register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1); |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 3128 | /* |
| 3129 | * Re-enter guest when pio read ahead buffer is empty or, |
| 3130 | * if it is not used, after each 1024 iteration. |
| 3131 | */ |
| 3132 | if ((rc->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) || |
| 3133 | (rc->end != 0 && rc->end == rc->pos)) |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 3134 | ctxt->restart = false; |
| 3135 | } |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 3136 | /* |
| 3137 | * reset read cache here in case string instruction is restared |
| 3138 | * without decoding |
| 3139 | */ |
| 3140 | ctxt->decode.mem_read.end = 0; |
Gleb Natapov | 95c5588 | 2010-04-28 19:15:39 +0300 | [diff] [blame] | 3141 | ctxt->eip = c->eip; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3142 | |
| 3143 | done: |
Gleb Natapov | cb404fe | 2010-03-18 15:20:25 +0200 | [diff] [blame] | 3144 | return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3145 | |
| 3146 | twobyte_insn: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3147 | switch (c->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3148 | case 0x01: /* lgdt, lidt, lmsw */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3149 | switch (c->modrm_reg) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3150 | u16 size; |
| 3151 | unsigned long address; |
| 3152 | |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3153 | case 0: /* vmcall */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3154 | if (c->modrm_mod != 3 || c->modrm_rm != 1) |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3155 | goto cannot_emulate; |
| 3156 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 3157 | rc = kvm_fix_hypercall(ctxt->vcpu); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3158 | if (rc != X86EMUL_CONTINUE) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 3159 | goto done; |
| 3160 | |
Avi Kivity | 33e3885 | 2008-05-21 15:34:25 +0300 | [diff] [blame] | 3161 | /* Let the processor re-execute the fixed hypercall */ |
Gleb Natapov | 063db06 | 2010-03-18 15:20:06 +0200 | [diff] [blame] | 3162 | c->eip = ctxt->eip; |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 3163 | /* Disable writeback. */ |
| 3164 | c->dst.type = OP_NONE; |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3165 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3166 | case 2: /* lgdt */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3167 | rc = read_descriptor(ctxt, ops, c->src.ptr, |
| 3168 | &size, &address, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3169 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3170 | goto done; |
| 3171 | realmode_lgdt(ctxt->vcpu, size, address); |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 3172 | /* Disable writeback. */ |
| 3173 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3174 | break; |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3175 | case 3: /* lidt/vmmcall */ |
Avi Kivity | 2b3d2a2 | 2008-12-23 19:46:01 +0200 | [diff] [blame] | 3176 | if (c->modrm_mod == 3) { |
| 3177 | switch (c->modrm_rm) { |
| 3178 | case 1: |
| 3179 | rc = kvm_fix_hypercall(ctxt->vcpu); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3180 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 2b3d2a2 | 2008-12-23 19:46:01 +0200 | [diff] [blame] | 3181 | goto done; |
| 3182 | break; |
| 3183 | default: |
| 3184 | goto cannot_emulate; |
| 3185 | } |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3186 | } else { |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3187 | rc = read_descriptor(ctxt, ops, c->src.ptr, |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3188 | &size, &address, |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3189 | c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3190 | if (rc != X86EMUL_CONTINUE) |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3191 | goto done; |
| 3192 | realmode_lidt(ctxt->vcpu, size, address); |
| 3193 | } |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 3194 | /* Disable writeback. */ |
| 3195 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3196 | break; |
| 3197 | case 4: /* smsw */ |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 3198 | c->dst.bytes = 2; |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 3199 | c->dst.val = ops->get_cr(0, ctxt->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3200 | break; |
| 3201 | case 6: /* lmsw */ |
Gleb Natapov | 93a152b | 2010-03-18 15:20:04 +0200 | [diff] [blame] | 3202 | ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0ful) | |
| 3203 | (c->src.val & 0x0f), ctxt->vcpu); |
Avi Kivity | dc7457e | 2008-04-30 16:13:36 +0300 | [diff] [blame] | 3204 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3205 | break; |
Gleb Natapov | 6e1e5ff | 2010-03-18 15:20:08 +0200 | [diff] [blame] | 3206 | case 5: /* not defined */ |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3207 | emulate_ud(ctxt); |
Gleb Natapov | 6e1e5ff | 2010-03-18 15:20:08 +0200 | [diff] [blame] | 3208 | goto done; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3209 | case 7: /* invlpg*/ |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 3210 | emulate_invlpg(ctxt->vcpu, c->modrm_ea); |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 3211 | /* Disable writeback. */ |
| 3212 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3213 | break; |
| 3214 | default: |
| 3215 | goto cannot_emulate; |
| 3216 | } |
| 3217 | break; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3218 | case 0x05: /* syscall */ |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 3219 | rc = emulate_syscall(ctxt, ops); |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 3220 | if (rc != X86EMUL_CONTINUE) |
| 3221 | goto done; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 3222 | else |
| 3223 | goto writeback; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3224 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3225 | case 0x06: |
| 3226 | emulate_clts(ctxt->vcpu); |
| 3227 | c->dst.type = OP_NONE; |
| 3228 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3229 | case 0x09: /* wbinvd */ |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 3230 | kvm_emulate_wbinvd(ctxt->vcpu); |
| 3231 | c->dst.type = OP_NONE; |
| 3232 | break; |
| 3233 | case 0x08: /* invd */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3234 | case 0x0d: /* GrpP (prefetch) */ |
| 3235 | case 0x18: /* Grp16 (prefetch/nop) */ |
| 3236 | c->dst.type = OP_NONE; |
| 3237 | break; |
| 3238 | case 0x20: /* mov cr, reg */ |
Gleb Natapov | 6aebfa6 | 2010-03-18 15:20:10 +0200 | [diff] [blame] | 3239 | switch (c->modrm_reg) { |
| 3240 | case 1: |
| 3241 | case 5 ... 7: |
| 3242 | case 9 ... 15: |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3243 | emulate_ud(ctxt); |
Gleb Natapov | 6aebfa6 | 2010-03-18 15:20:10 +0200 | [diff] [blame] | 3244 | goto done; |
| 3245 | } |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 3246 | c->regs[c->modrm_rm] = ops->get_cr(c->modrm_reg, ctxt->vcpu); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3247 | c->dst.type = OP_NONE; /* no writeback */ |
| 3248 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3249 | case 0x21: /* mov from dr to reg */ |
Gleb Natapov | 1e470be | 2010-03-18 15:20:11 +0200 | [diff] [blame] | 3250 | if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) && |
| 3251 | (c->modrm_reg == 4 || c->modrm_reg == 5)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3252 | emulate_ud(ctxt); |
Gleb Natapov | 1e470be | 2010-03-18 15:20:11 +0200 | [diff] [blame] | 3253 | goto done; |
| 3254 | } |
Gleb Natapov | 35aa537 | 2010-04-28 19:15:27 +0300 | [diff] [blame] | 3255 | ops->get_dr(c->modrm_reg, &c->regs[c->modrm_rm], ctxt->vcpu); |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 3256 | c->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3257 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3258 | case 0x22: /* mov reg, cr */ |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 3259 | if (ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3260 | emulate_gp(ctxt, 0); |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 3261 | goto done; |
| 3262 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3263 | c->dst.type = OP_NONE; |
| 3264 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3265 | case 0x23: /* mov from reg to dr */ |
Gleb Natapov | 1e470be | 2010-03-18 15:20:11 +0200 | [diff] [blame] | 3266 | if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) && |
| 3267 | (c->modrm_reg == 4 || c->modrm_reg == 5)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3268 | emulate_ud(ctxt); |
Gleb Natapov | 1e470be | 2010-03-18 15:20:11 +0200 | [diff] [blame] | 3269 | goto done; |
| 3270 | } |
Gleb Natapov | 35aa537 | 2010-04-28 19:15:27 +0300 | [diff] [blame] | 3271 | |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 3272 | if (ops->set_dr(c->modrm_reg, c->regs[c->modrm_rm] & |
| 3273 | ((ctxt->mode == X86EMUL_MODE_PROT64) ? |
| 3274 | ~0ULL : ~0U), ctxt->vcpu) < 0) { |
| 3275 | /* #UD condition is already handled by the code above */ |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3276 | emulate_gp(ctxt, 0); |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 3277 | goto done; |
| 3278 | } |
| 3279 | |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 3280 | c->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3281 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3282 | case 0x30: |
| 3283 | /* wrmsr */ |
| 3284 | msr_data = (u32)c->regs[VCPU_REGS_RAX] |
| 3285 | | ((u64)c->regs[VCPU_REGS_RDX] << 32); |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 3286 | if (ops->set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3287 | emulate_gp(ctxt, 0); |
Gleb Natapov | fd52536 | 2010-03-18 15:20:13 +0200 | [diff] [blame] | 3288 | goto done; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3289 | } |
| 3290 | rc = X86EMUL_CONTINUE; |
| 3291 | c->dst.type = OP_NONE; |
| 3292 | break; |
| 3293 | case 0x32: |
| 3294 | /* rdmsr */ |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 3295 | if (ops->get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3296 | emulate_gp(ctxt, 0); |
Gleb Natapov | fd52536 | 2010-03-18 15:20:13 +0200 | [diff] [blame] | 3297 | goto done; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3298 | } else { |
| 3299 | c->regs[VCPU_REGS_RAX] = (u32)msr_data; |
| 3300 | c->regs[VCPU_REGS_RDX] = msr_data >> 32; |
| 3301 | } |
| 3302 | rc = X86EMUL_CONTINUE; |
| 3303 | c->dst.type = OP_NONE; |
| 3304 | break; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3305 | case 0x34: /* sysenter */ |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 3306 | rc = emulate_sysenter(ctxt, ops); |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 3307 | if (rc != X86EMUL_CONTINUE) |
| 3308 | goto done; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 3309 | else |
| 3310 | goto writeback; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3311 | break; |
| 3312 | case 0x35: /* sysexit */ |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 3313 | rc = emulate_sysexit(ctxt, ops); |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 3314 | if (rc != X86EMUL_CONTINUE) |
| 3315 | goto done; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 3316 | else |
| 3317 | goto writeback; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3318 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3319 | case 0x40 ... 0x4f: /* cmov */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3320 | c->dst.val = c->dst.orig_val = c->src.val; |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 3321 | if (!test_cc(c->b, ctxt->eflags)) |
| 3322 | c->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3323 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 3324 | case 0x80 ... 0x8f: /* jnz rel, etc*/ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3325 | if (test_cc(c->b, ctxt->eflags)) |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 3326 | jmp_rel(c, c->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3327 | c->dst.type = OP_NONE; |
| 3328 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3329 | case 0xa0: /* push fs */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3330 | emulate_push_sreg(ctxt, ops, VCPU_SREG_FS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3331 | break; |
| 3332 | case 0xa1: /* pop fs */ |
| 3333 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3334 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3335 | goto done; |
| 3336 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3337 | case 0xa3: |
| 3338 | bt: /* bt */ |
Qing He | e4f8e03 | 2007-09-24 17:22:13 +0800 | [diff] [blame] | 3339 | c->dst.type = OP_NONE; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3340 | /* only subword offset */ |
| 3341 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3342 | emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3343 | break; |
Guillaume Thouvenin | 9bf8ea4 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 3344 | case 0xa4: /* shld imm8, r, r/m */ |
| 3345 | case 0xa5: /* shld cl, r, r/m */ |
| 3346 | emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags); |
| 3347 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3348 | case 0xa8: /* push gs */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3349 | emulate_push_sreg(ctxt, ops, VCPU_SREG_GS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3350 | break; |
| 3351 | case 0xa9: /* pop gs */ |
| 3352 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3353 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3354 | goto done; |
| 3355 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3356 | case 0xab: |
| 3357 | bts: /* bts */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3358 | /* only subword offset */ |
| 3359 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3360 | emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3361 | break; |
Guillaume Thouvenin | 9bf8ea4 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 3362 | case 0xac: /* shrd imm8, r, r/m */ |
| 3363 | case 0xad: /* shrd cl, r, r/m */ |
| 3364 | emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags); |
| 3365 | break; |
Glauber Costa | 2a7c5b8 | 2008-07-10 17:08:15 -0300 | [diff] [blame] | 3366 | case 0xae: /* clflush */ |
| 3367 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3368 | case 0xb0 ... 0xb1: /* cmpxchg */ |
| 3369 | /* |
| 3370 | * Save real source value, then compare EAX against |
| 3371 | * destination. |
| 3372 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3373 | c->src.orig_val = c->src.val; |
| 3374 | c->src.val = c->regs[VCPU_REGS_RAX]; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3375 | emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags); |
| 3376 | if (ctxt->eflags & EFLG_ZF) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3377 | /* Success: write back to memory. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3378 | c->dst.val = c->src.orig_val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3379 | } else { |
| 3380 | /* Failure: write the value we saw to EAX. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3381 | c->dst.type = OP_REG; |
| 3382 | c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3383 | } |
| 3384 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3385 | case 0xb3: |
| 3386 | btr: /* btr */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3387 | /* only subword offset */ |
| 3388 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3389 | emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3390 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3391 | case 0xb6 ... 0xb7: /* movzx */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3392 | c->dst.bytes = c->op_bytes; |
| 3393 | c->dst.val = (c->d & ByteOp) ? (u8) c->src.val |
| 3394 | : (u16) c->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3395 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3396 | case 0xba: /* Grp8 */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3397 | switch (c->modrm_reg & 3) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3398 | case 0: |
| 3399 | goto bt; |
| 3400 | case 1: |
| 3401 | goto bts; |
| 3402 | case 2: |
| 3403 | goto btr; |
| 3404 | case 3: |
| 3405 | goto btc; |
| 3406 | } |
| 3407 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3408 | case 0xbb: |
| 3409 | btc: /* btc */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3410 | /* only subword offset */ |
| 3411 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3412 | emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3413 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3414 | case 0xbe ... 0xbf: /* movsx */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3415 | c->dst.bytes = c->op_bytes; |
| 3416 | c->dst.val = (c->d & ByteOp) ? (s8) c->src.val : |
| 3417 | (s16) c->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3418 | break; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 3419 | case 0xc3: /* movnti */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3420 | c->dst.bytes = c->op_bytes; |
| 3421 | c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val : |
| 3422 | (u64) c->src.val; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 3423 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3424 | case 0xc7: /* Grp9 (cmpxchg8b) */ |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 3425 | rc = emulate_grp9(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3426 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 3427 | goto done; |
| 3428 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 3429 | default: |
| 3430 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3431 | } |
| 3432 | goto writeback; |
| 3433 | |
| 3434 | cannot_emulate: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3435 | DPRINTF("Cannot emulate %02x\n", c->b); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3436 | return -1; |
| 3437 | } |