Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * x86_emulate.c |
| 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 |
| 12 | * |
| 13 | * Avi Kivity <avi@qumranet.com> |
| 14 | * Yaniv Kamay <yaniv@qumranet.com> |
| 15 | * |
| 16 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 17 | * the COPYING file in the top-level directory. |
| 18 | * |
| 19 | * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4 |
| 20 | */ |
| 21 | |
| 22 | #ifndef __KERNEL__ |
| 23 | #include <stdio.h> |
| 24 | #include <stdint.h> |
| 25 | #include <public/xen.h> |
| 26 | #define DPRINTF(_f, _a ...) printf( _f , ## _a ) |
| 27 | #else |
| 28 | #include "kvm.h" |
| 29 | #define DPRINTF(x...) do {} while (0) |
| 30 | #endif |
| 31 | #include "x86_emulate.h" |
| 32 | #include <linux/module.h> |
| 33 | |
| 34 | /* |
| 35 | * Opcode effective-address decode tables. |
| 36 | * Note that we only emulate instructions that have at least one memory |
| 37 | * operand (excluding implicit stack references). We assume that stack |
| 38 | * references and instruction fetches will never occur in special memory |
| 39 | * areas that require emulation. So, for example, 'mov <imm>,<reg>' need |
| 40 | * not be handled. |
| 41 | */ |
| 42 | |
| 43 | /* Operand sizes: 8-bit operands or specified/overridden size. */ |
| 44 | #define ByteOp (1<<0) /* 8-bit operands. */ |
| 45 | /* Destination operand type. */ |
| 46 | #define ImplicitOps (1<<1) /* Implicit in opcode. No generic decode. */ |
| 47 | #define DstReg (2<<1) /* Register operand. */ |
| 48 | #define DstMem (3<<1) /* Memory operand. */ |
| 49 | #define DstMask (3<<1) |
| 50 | /* Source operand type. */ |
| 51 | #define SrcNone (0<<3) /* No source operand. */ |
| 52 | #define SrcImplicit (0<<3) /* Source operand is implicit in the opcode. */ |
| 53 | #define SrcReg (1<<3) /* Register operand. */ |
| 54 | #define SrcMem (2<<3) /* Memory operand. */ |
| 55 | #define SrcMem16 (3<<3) /* Memory operand (16-bit). */ |
| 56 | #define SrcMem32 (4<<3) /* Memory operand (32-bit). */ |
| 57 | #define SrcImm (5<<3) /* Immediate operand. */ |
| 58 | #define SrcImmByte (6<<3) /* 8-bit sign-extended immediate operand. */ |
| 59 | #define SrcMask (7<<3) |
| 60 | /* Generic ModRM decode. */ |
| 61 | #define ModRM (1<<6) |
| 62 | /* Destination is only written; never read. */ |
| 63 | #define Mov (1<<7) |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 64 | #define BitOp (1<<8) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 65 | |
| 66 | static u8 opcode_table[256] = { |
| 67 | /* 0x00 - 0x07 */ |
| 68 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, |
| 69 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, |
| 70 | 0, 0, 0, 0, |
| 71 | /* 0x08 - 0x0F */ |
| 72 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, |
| 73 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, |
| 74 | 0, 0, 0, 0, |
| 75 | /* 0x10 - 0x17 */ |
| 76 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, |
| 77 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, |
| 78 | 0, 0, 0, 0, |
| 79 | /* 0x18 - 0x1F */ |
| 80 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, |
| 81 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, |
| 82 | 0, 0, 0, 0, |
| 83 | /* 0x20 - 0x27 */ |
| 84 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, |
| 85 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, |
Nitin A Kamble | 19eb938 | 2007-08-17 15:17:41 +0300 | [diff] [blame] | 86 | SrcImmByte, SrcImm, 0, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 87 | /* 0x28 - 0x2F */ |
| 88 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, |
| 89 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, |
| 90 | 0, 0, 0, 0, |
| 91 | /* 0x30 - 0x37 */ |
| 92 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, |
| 93 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, |
| 94 | 0, 0, 0, 0, |
| 95 | /* 0x38 - 0x3F */ |
| 96 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, |
| 97 | ByteOp | DstReg | SrcMem | ModRM, DstReg | SrcMem | ModRM, |
| 98 | 0, 0, 0, 0, |
| 99 | /* 0x40 - 0x4F */ |
| 100 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
Nitin A Kamble | 7f0aaee | 2007-06-19 11:16:04 +0300 | [diff] [blame] | 101 | /* 0x50 - 0x57 */ |
Nitin A Kamble | 7e77816 | 2007-08-19 11:07:06 +0300 | [diff] [blame] | 102 | ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps, |
| 103 | ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps, |
Nitin A Kamble | 7f0aaee | 2007-06-19 11:16:04 +0300 | [diff] [blame] | 104 | /* 0x58 - 0x5F */ |
| 105 | ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps, |
| 106 | ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps, |
Nitin A Kamble | 7d31691 | 2007-08-28 17:58:52 -0700 | [diff] [blame] | 107 | /* 0x60 - 0x67 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 108 | 0, 0, 0, DstReg | SrcMem32 | ModRM | Mov /* movsxd (x86/64) */ , |
Nitin A Kamble | 7d31691 | 2007-08-28 17:58:52 -0700 | [diff] [blame] | 109 | 0, 0, 0, 0, |
| 110 | /* 0x68 - 0x6F */ |
| 111 | 0, 0, ImplicitOps|Mov, 0, |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 112 | SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* insb, insw/insd */ |
| 113 | SrcNone | ByteOp | ImplicitOps, SrcNone | ImplicitOps, /* outsb, outsw/outsd */ |
Nitin A Kamble | 55bebde | 2007-09-15 10:25:41 +0300 | [diff] [blame] | 114 | /* 0x70 - 0x77 */ |
| 115 | ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps, |
| 116 | ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps, |
| 117 | /* 0x78 - 0x7F */ |
| 118 | ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps, |
| 119 | ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 120 | /* 0x80 - 0x87 */ |
| 121 | ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImm | ModRM, |
| 122 | ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM, |
| 123 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, |
| 124 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, |
| 125 | /* 0x88 - 0x8F */ |
| 126 | ByteOp | DstMem | SrcReg | ModRM | Mov, DstMem | SrcReg | ModRM | Mov, |
| 127 | ByteOp | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 128 | 0, ModRM | DstReg, 0, DstMem | SrcNone | ModRM | Mov, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 129 | /* 0x90 - 0x9F */ |
Nitin A Kamble | 535eabc | 2007-09-15 10:45:05 +0300 | [diff] [blame] | 130 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps, ImplicitOps, 0, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 131 | /* 0xA0 - 0xA7 */ |
| 132 | ByteOp | DstReg | SrcMem | Mov, DstReg | SrcMem | Mov, |
| 133 | ByteOp | DstMem | SrcReg | Mov, DstMem | SrcReg | Mov, |
| 134 | ByteOp | ImplicitOps | Mov, ImplicitOps | Mov, |
| 135 | ByteOp | ImplicitOps, ImplicitOps, |
| 136 | /* 0xA8 - 0xAF */ |
| 137 | 0, 0, ByteOp | ImplicitOps | Mov, ImplicitOps | Mov, |
| 138 | ByteOp | ImplicitOps | Mov, ImplicitOps | Mov, |
| 139 | ByteOp | ImplicitOps, ImplicitOps, |
| 140 | /* 0xB0 - 0xBF */ |
| 141 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 142 | /* 0xC0 - 0xC7 */ |
Nitin A Kamble | d9413cd | 2007-06-19 11:21:15 +0300 | [diff] [blame] | 143 | ByteOp | DstMem | SrcImm | ModRM, DstMem | SrcImmByte | ModRM, |
| 144 | 0, ImplicitOps, 0, 0, |
| 145 | ByteOp | DstMem | SrcImm | ModRM | Mov, DstMem | SrcImm | ModRM | Mov, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 146 | /* 0xC8 - 0xCF */ |
| 147 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 148 | /* 0xD0 - 0xD7 */ |
| 149 | ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM, |
| 150 | ByteOp | DstMem | SrcImplicit | ModRM, DstMem | SrcImplicit | ModRM, |
| 151 | 0, 0, 0, 0, |
| 152 | /* 0xD8 - 0xDF */ |
| 153 | 0, 0, 0, 0, 0, 0, 0, 0, |
Nitin A Kamble | 098c937 | 2007-08-19 11:00:36 +0300 | [diff] [blame] | 154 | /* 0xE0 - 0xE7 */ |
| 155 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 156 | /* 0xE8 - 0xEF */ |
Nitin A Kamble | f6eed39 | 2007-08-28 18:08:37 -0700 | [diff] [blame] | 157 | ImplicitOps, SrcImm|ImplicitOps, 0, SrcImmByte|ImplicitOps, 0, 0, 0, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 158 | /* 0xF0 - 0xF7 */ |
| 159 | 0, 0, 0, 0, |
Avi Kivity | 72d6e5a | 2007-06-05 16:15:51 +0300 | [diff] [blame] | 160 | ImplicitOps, 0, |
| 161 | ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 162 | /* 0xF8 - 0xFF */ |
| 163 | 0, 0, 0, 0, |
| 164 | 0, 0, ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM |
| 165 | }; |
| 166 | |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 167 | static u16 twobyte_table[256] = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 168 | /* 0x00 - 0x0F */ |
| 169 | 0, SrcMem | ModRM | DstReg, 0, 0, 0, 0, ImplicitOps, 0, |
Avi Kivity | 651a3e2 | 2007-10-28 16:09:18 +0200 | [diff] [blame] | 170 | ImplicitOps, ImplicitOps, 0, 0, 0, ImplicitOps | ModRM, 0, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 171 | /* 0x10 - 0x1F */ |
| 172 | 0, 0, 0, 0, 0, 0, 0, 0, ImplicitOps | ModRM, 0, 0, 0, 0, 0, 0, 0, |
| 173 | /* 0x20 - 0x2F */ |
| 174 | ModRM | ImplicitOps, ModRM, ModRM | ImplicitOps, ModRM, 0, 0, 0, 0, |
| 175 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 176 | /* 0x30 - 0x3F */ |
Avi Kivity | 35f3f28 | 2007-07-17 14:20:30 +0300 | [diff] [blame] | 177 | ImplicitOps, 0, ImplicitOps, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 178 | /* 0x40 - 0x47 */ |
| 179 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
| 180 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
| 181 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
| 182 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
| 183 | /* 0x48 - 0x4F */ |
| 184 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
| 185 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
| 186 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
| 187 | DstReg | SrcMem | ModRM | Mov, DstReg | SrcMem | ModRM | Mov, |
| 188 | /* 0x50 - 0x5F */ |
| 189 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 190 | /* 0x60 - 0x6F */ |
| 191 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 192 | /* 0x70 - 0x7F */ |
| 193 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 194 | /* 0x80 - 0x8F */ |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 195 | ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps, |
| 196 | ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps, |
| 197 | ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps, |
| 198 | ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 199 | /* 0x90 - 0x9F */ |
| 200 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 201 | /* 0xA0 - 0xA7 */ |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 202 | 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 203 | /* 0xA8 - 0xAF */ |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 204 | 0, 0, 0, DstMem | SrcReg | ModRM | BitOp, 0, 0, 0, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 205 | /* 0xB0 - 0xB7 */ |
| 206 | ByteOp | DstMem | SrcReg | ModRM, DstMem | SrcReg | ModRM, 0, |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 207 | DstMem | SrcReg | ModRM | BitOp, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 208 | 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov, |
| 209 | DstReg | SrcMem16 | ModRM | Mov, |
| 210 | /* 0xB8 - 0xBF */ |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 211 | 0, 0, DstMem | SrcImmByte | ModRM, DstMem | SrcReg | ModRM | BitOp, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 212 | 0, 0, ByteOp | DstReg | SrcMem | ModRM | Mov, |
| 213 | DstReg | SrcMem16 | ModRM | Mov, |
| 214 | /* 0xC0 - 0xCF */ |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 215 | 0, 0, 0, DstMem | SrcReg | ModRM | Mov, 0, 0, 0, ImplicitOps | ModRM, |
| 216 | 0, 0, 0, 0, 0, 0, 0, 0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 217 | /* 0xD0 - 0xDF */ |
| 218 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 219 | /* 0xE0 - 0xEF */ |
| 220 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 221 | /* 0xF0 - 0xFF */ |
| 222 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 223 | }; |
| 224 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 225 | /* EFLAGS bit definitions. */ |
| 226 | #define EFLG_OF (1<<11) |
| 227 | #define EFLG_DF (1<<10) |
| 228 | #define EFLG_SF (1<<7) |
| 229 | #define EFLG_ZF (1<<6) |
| 230 | #define EFLG_AF (1<<4) |
| 231 | #define EFLG_PF (1<<2) |
| 232 | #define EFLG_CF (1<<0) |
| 233 | |
| 234 | /* |
| 235 | * Instruction emulation: |
| 236 | * Most instructions are emulated directly via a fragment of inline assembly |
| 237 | * code. This allows us to save/restore EFLAGS and thus very easily pick up |
| 238 | * any modified flags. |
| 239 | */ |
| 240 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 241 | #if defined(CONFIG_X86_64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 242 | #define _LO32 "k" /* force 32-bit operand */ |
| 243 | #define _STK "%%rsp" /* stack pointer */ |
| 244 | #elif defined(__i386__) |
| 245 | #define _LO32 "" /* force 32-bit operand */ |
| 246 | #define _STK "%%esp" /* stack pointer */ |
| 247 | #endif |
| 248 | |
| 249 | /* |
| 250 | * These EFLAGS bits are restored from saved value during emulation, and |
| 251 | * any changes are written back to the saved value after emulation. |
| 252 | */ |
| 253 | #define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF) |
| 254 | |
| 255 | /* Before executing instruction: restore necessary bits in EFLAGS. */ |
| 256 | #define _PRE_EFLAGS(_sav, _msk, _tmp) \ |
| 257 | /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); */ \ |
| 258 | "push %"_sav"; " \ |
| 259 | "movl %"_msk",%"_LO32 _tmp"; " \ |
| 260 | "andl %"_LO32 _tmp",("_STK"); " \ |
| 261 | "pushf; " \ |
| 262 | "notl %"_LO32 _tmp"; " \ |
| 263 | "andl %"_LO32 _tmp",("_STK"); " \ |
| 264 | "pop %"_tmp"; " \ |
| 265 | "orl %"_LO32 _tmp",("_STK"); " \ |
| 266 | "popf; " \ |
| 267 | /* _sav &= ~msk; */ \ |
| 268 | "movl %"_msk",%"_LO32 _tmp"; " \ |
| 269 | "notl %"_LO32 _tmp"; " \ |
| 270 | "andl %"_LO32 _tmp",%"_sav"; " |
| 271 | |
| 272 | /* After executing instruction: write-back necessary bits in EFLAGS. */ |
| 273 | #define _POST_EFLAGS(_sav, _msk, _tmp) \ |
| 274 | /* _sav |= EFLAGS & _msk; */ \ |
| 275 | "pushf; " \ |
| 276 | "pop %"_tmp"; " \ |
| 277 | "andl %"_msk",%"_LO32 _tmp"; " \ |
| 278 | "orl %"_LO32 _tmp",%"_sav"; " |
| 279 | |
| 280 | /* Raw emulation: instruction has two explicit operands. */ |
| 281 | #define __emulate_2op_nobyte(_op,_src,_dst,_eflags,_wx,_wy,_lx,_ly,_qx,_qy) \ |
| 282 | do { \ |
| 283 | unsigned long _tmp; \ |
| 284 | \ |
| 285 | switch ((_dst).bytes) { \ |
| 286 | case 2: \ |
| 287 | __asm__ __volatile__ ( \ |
| 288 | _PRE_EFLAGS("0","4","2") \ |
| 289 | _op"w %"_wx"3,%1; " \ |
| 290 | _POST_EFLAGS("0","4","2") \ |
| 291 | : "=m" (_eflags), "=m" ((_dst).val), \ |
| 292 | "=&r" (_tmp) \ |
| 293 | : _wy ((_src).val), "i" (EFLAGS_MASK) ); \ |
| 294 | break; \ |
| 295 | case 4: \ |
| 296 | __asm__ __volatile__ ( \ |
| 297 | _PRE_EFLAGS("0","4","2") \ |
| 298 | _op"l %"_lx"3,%1; " \ |
| 299 | _POST_EFLAGS("0","4","2") \ |
| 300 | : "=m" (_eflags), "=m" ((_dst).val), \ |
| 301 | "=&r" (_tmp) \ |
| 302 | : _ly ((_src).val), "i" (EFLAGS_MASK) ); \ |
| 303 | break; \ |
| 304 | case 8: \ |
| 305 | __emulate_2op_8byte(_op, _src, _dst, \ |
| 306 | _eflags, _qx, _qy); \ |
| 307 | break; \ |
| 308 | } \ |
| 309 | } while (0) |
| 310 | |
| 311 | #define __emulate_2op(_op,_src,_dst,_eflags,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \ |
| 312 | do { \ |
| 313 | unsigned long _tmp; \ |
| 314 | switch ( (_dst).bytes ) \ |
| 315 | { \ |
| 316 | case 1: \ |
| 317 | __asm__ __volatile__ ( \ |
| 318 | _PRE_EFLAGS("0","4","2") \ |
| 319 | _op"b %"_bx"3,%1; " \ |
| 320 | _POST_EFLAGS("0","4","2") \ |
| 321 | : "=m" (_eflags), "=m" ((_dst).val), \ |
| 322 | "=&r" (_tmp) \ |
| 323 | : _by ((_src).val), "i" (EFLAGS_MASK) ); \ |
| 324 | break; \ |
| 325 | default: \ |
| 326 | __emulate_2op_nobyte(_op, _src, _dst, _eflags, \ |
| 327 | _wx, _wy, _lx, _ly, _qx, _qy); \ |
| 328 | break; \ |
| 329 | } \ |
| 330 | } while (0) |
| 331 | |
| 332 | /* Source operand is byte-sized and may be restricted to just %cl. */ |
| 333 | #define emulate_2op_SrcB(_op, _src, _dst, _eflags) \ |
| 334 | __emulate_2op(_op, _src, _dst, _eflags, \ |
| 335 | "b", "c", "b", "c", "b", "c", "b", "c") |
| 336 | |
| 337 | /* Source operand is byte, word, long or quad sized. */ |
| 338 | #define emulate_2op_SrcV(_op, _src, _dst, _eflags) \ |
| 339 | __emulate_2op(_op, _src, _dst, _eflags, \ |
| 340 | "b", "q", "w", "r", _LO32, "r", "", "r") |
| 341 | |
| 342 | /* Source operand is word, long or quad sized. */ |
| 343 | #define emulate_2op_SrcV_nobyte(_op, _src, _dst, _eflags) \ |
| 344 | __emulate_2op_nobyte(_op, _src, _dst, _eflags, \ |
| 345 | "w", "r", _LO32, "r", "", "r") |
| 346 | |
| 347 | /* Instruction has only one explicit operand (no source operand). */ |
| 348 | #define emulate_1op(_op, _dst, _eflags) \ |
| 349 | do { \ |
| 350 | unsigned long _tmp; \ |
| 351 | \ |
| 352 | switch ( (_dst).bytes ) \ |
| 353 | { \ |
| 354 | case 1: \ |
| 355 | __asm__ __volatile__ ( \ |
| 356 | _PRE_EFLAGS("0","3","2") \ |
| 357 | _op"b %1; " \ |
| 358 | _POST_EFLAGS("0","3","2") \ |
| 359 | : "=m" (_eflags), "=m" ((_dst).val), \ |
| 360 | "=&r" (_tmp) \ |
| 361 | : "i" (EFLAGS_MASK) ); \ |
| 362 | break; \ |
| 363 | case 2: \ |
| 364 | __asm__ __volatile__ ( \ |
| 365 | _PRE_EFLAGS("0","3","2") \ |
| 366 | _op"w %1; " \ |
| 367 | _POST_EFLAGS("0","3","2") \ |
| 368 | : "=m" (_eflags), "=m" ((_dst).val), \ |
| 369 | "=&r" (_tmp) \ |
| 370 | : "i" (EFLAGS_MASK) ); \ |
| 371 | break; \ |
| 372 | case 4: \ |
| 373 | __asm__ __volatile__ ( \ |
| 374 | _PRE_EFLAGS("0","3","2") \ |
| 375 | _op"l %1; " \ |
| 376 | _POST_EFLAGS("0","3","2") \ |
| 377 | : "=m" (_eflags), "=m" ((_dst).val), \ |
| 378 | "=&r" (_tmp) \ |
| 379 | : "i" (EFLAGS_MASK) ); \ |
| 380 | break; \ |
| 381 | case 8: \ |
| 382 | __emulate_1op_8byte(_op, _dst, _eflags); \ |
| 383 | break; \ |
| 384 | } \ |
| 385 | } while (0) |
| 386 | |
| 387 | /* Emulate an instruction with quadword operands (x86/64 only). */ |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 388 | #if defined(CONFIG_X86_64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 389 | #define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy) \ |
| 390 | do { \ |
| 391 | __asm__ __volatile__ ( \ |
| 392 | _PRE_EFLAGS("0","4","2") \ |
| 393 | _op"q %"_qx"3,%1; " \ |
| 394 | _POST_EFLAGS("0","4","2") \ |
| 395 | : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \ |
| 396 | : _qy ((_src).val), "i" (EFLAGS_MASK) ); \ |
| 397 | } while (0) |
| 398 | |
| 399 | #define __emulate_1op_8byte(_op, _dst, _eflags) \ |
| 400 | do { \ |
| 401 | __asm__ __volatile__ ( \ |
| 402 | _PRE_EFLAGS("0","3","2") \ |
| 403 | _op"q %1; " \ |
| 404 | _POST_EFLAGS("0","3","2") \ |
| 405 | : "=m" (_eflags), "=m" ((_dst).val), "=&r" (_tmp) \ |
| 406 | : "i" (EFLAGS_MASK) ); \ |
| 407 | } while (0) |
| 408 | |
| 409 | #elif defined(__i386__) |
| 410 | #define __emulate_2op_8byte(_op, _src, _dst, _eflags, _qx, _qy) |
| 411 | #define __emulate_1op_8byte(_op, _dst, _eflags) |
| 412 | #endif /* __i386__ */ |
| 413 | |
| 414 | /* Fetch next part of the instruction being emulated. */ |
| 415 | #define insn_fetch(_type, _size, _eip) \ |
| 416 | ({ unsigned long _x; \ |
| 417 | rc = ops->read_std((unsigned long)(_eip) + ctxt->cs_base, &_x, \ |
Laurent Vivier | cebff02 | 2007-07-30 13:35:24 +0300 | [diff] [blame] | 418 | (_size), ctxt->vcpu); \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 419 | if ( rc != 0 ) \ |
| 420 | goto done; \ |
| 421 | (_eip) += (_size); \ |
| 422 | (_type)_x; \ |
| 423 | }) |
| 424 | |
| 425 | /* Access/update address held in a register, based on addressing mode. */ |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 426 | #define address_mask(reg) \ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 427 | ((c->ad_bytes == sizeof(unsigned long)) ? \ |
| 428 | (reg) : ((reg) & ((1UL << (c->ad_bytes << 3)) - 1))) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 429 | #define register_address(base, reg) \ |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 430 | ((base) + address_mask(reg)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 431 | #define register_address_increment(reg, inc) \ |
| 432 | do { \ |
| 433 | /* signed type ensures sign extension to long */ \ |
| 434 | int _inc = (inc); \ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 435 | if (c->ad_bytes == sizeof(unsigned long)) \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 436 | (reg) += _inc; \ |
| 437 | else \ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 438 | (reg) = ((reg) & \ |
| 439 | ~((1UL << (c->ad_bytes << 3)) - 1)) | \ |
| 440 | (((reg) + _inc) & \ |
| 441 | ((1UL << (c->ad_bytes << 3)) - 1)); \ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 442 | } while (0) |
| 443 | |
Nitin A Kamble | 098c937 | 2007-08-19 11:00:36 +0300 | [diff] [blame] | 444 | #define JMP_REL(rel) \ |
| 445 | do { \ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 446 | register_address_increment(c->eip, rel); \ |
Nitin A Kamble | 098c937 | 2007-08-19 11:00:36 +0300 | [diff] [blame] | 447 | } while (0) |
| 448 | |
Rusty Russell | 1e3c5cb | 2007-07-17 23:16:11 +1000 | [diff] [blame] | 449 | /* |
| 450 | * Given the 'reg' portion of a ModRM byte, and a register block, return a |
| 451 | * pointer into the block that addresses the relevant register. |
| 452 | * @highbyte_regs specifies whether to decode AH,CH,DH,BH. |
| 453 | */ |
| 454 | static void *decode_register(u8 modrm_reg, unsigned long *regs, |
| 455 | int highbyte_regs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 456 | { |
| 457 | void *p; |
| 458 | |
| 459 | p = ®s[modrm_reg]; |
| 460 | if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8) |
| 461 | p = (unsigned char *)®s[modrm_reg & 3] + 1; |
| 462 | return p; |
| 463 | } |
| 464 | |
| 465 | static int read_descriptor(struct x86_emulate_ctxt *ctxt, |
| 466 | struct x86_emulate_ops *ops, |
| 467 | void *ptr, |
| 468 | u16 *size, unsigned long *address, int op_bytes) |
| 469 | { |
| 470 | int rc; |
| 471 | |
| 472 | if (op_bytes == 2) |
| 473 | op_bytes = 3; |
| 474 | *address = 0; |
Laurent Vivier | cebff02 | 2007-07-30 13:35:24 +0300 | [diff] [blame] | 475 | rc = ops->read_std((unsigned long)ptr, (unsigned long *)size, 2, |
| 476 | ctxt->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 477 | if (rc) |
| 478 | return rc; |
Laurent Vivier | cebff02 | 2007-07-30 13:35:24 +0300 | [diff] [blame] | 479 | rc = ops->read_std((unsigned long)ptr + 2, address, op_bytes, |
| 480 | ctxt->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 481 | return rc; |
| 482 | } |
| 483 | |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 484 | static int test_cc(unsigned int condition, unsigned int flags) |
| 485 | { |
| 486 | int rc = 0; |
| 487 | |
| 488 | switch ((condition & 15) >> 1) { |
| 489 | case 0: /* o */ |
| 490 | rc |= (flags & EFLG_OF); |
| 491 | break; |
| 492 | case 1: /* b/c/nae */ |
| 493 | rc |= (flags & EFLG_CF); |
| 494 | break; |
| 495 | case 2: /* z/e */ |
| 496 | rc |= (flags & EFLG_ZF); |
| 497 | break; |
| 498 | case 3: /* be/na */ |
| 499 | rc |= (flags & (EFLG_CF|EFLG_ZF)); |
| 500 | break; |
| 501 | case 4: /* s */ |
| 502 | rc |= (flags & EFLG_SF); |
| 503 | break; |
| 504 | case 5: /* p/pe */ |
| 505 | rc |= (flags & EFLG_PF); |
| 506 | break; |
| 507 | case 7: /* le/ng */ |
| 508 | rc |= (flags & EFLG_ZF); |
| 509 | /* fall through */ |
| 510 | case 6: /* l/nge */ |
| 511 | rc |= (!(flags & EFLG_SF) != !(flags & EFLG_OF)); |
| 512 | break; |
| 513 | } |
| 514 | |
| 515 | /* Odd condition identifiers (lsb == 1) have inverted sense. */ |
| 516 | return (!!rc ^ (condition & 1)); |
| 517 | } |
| 518 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 519 | int |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 520 | 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] | 521 | { |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 522 | struct decode_cache *c = &ctxt->decode; |
| 523 | u8 sib, rex_prefix = 0; |
| 524 | unsigned int i; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 525 | int rc = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 526 | int mode = ctxt->mode; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 527 | int index_reg = 0, base_reg = 0, scale, rip_relative = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 528 | |
| 529 | /* Shadow copy of register state. Committed on successful emulation. */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 530 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 531 | memset(c, 0, sizeof(struct decode_cache)); |
| 532 | c->eip = ctxt->vcpu->rip; |
| 533 | memcpy(c->regs, ctxt->vcpu->regs, sizeof c->regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 534 | |
| 535 | switch (mode) { |
| 536 | case X86EMUL_MODE_REAL: |
| 537 | case X86EMUL_MODE_PROT16: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 538 | c->op_bytes = c->ad_bytes = 2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 539 | break; |
| 540 | case X86EMUL_MODE_PROT32: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 541 | c->op_bytes = c->ad_bytes = 4; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 542 | break; |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 543 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 544 | case X86EMUL_MODE_PROT64: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 545 | c->op_bytes = 4; |
| 546 | c->ad_bytes = 8; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 547 | break; |
| 548 | #endif |
| 549 | default: |
| 550 | return -1; |
| 551 | } |
| 552 | |
| 553 | /* Legacy prefixes. */ |
| 554 | for (i = 0; i < 8; i++) { |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 555 | switch (c->b = insn_fetch(u8, 1, c->eip)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 556 | case 0x66: /* operand-size override */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 557 | c->op_bytes ^= 6; /* switch between 2/4 bytes */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 558 | break; |
| 559 | case 0x67: /* address-size override */ |
| 560 | if (mode == X86EMUL_MODE_PROT64) |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 561 | /* switch between 4/8 bytes */ |
| 562 | c->ad_bytes ^= 12; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 563 | else |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 564 | /* switch between 2/4 bytes */ |
| 565 | c->ad_bytes ^= 6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 566 | break; |
| 567 | case 0x2e: /* CS override */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 568 | c->override_base = &ctxt->cs_base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 569 | break; |
| 570 | case 0x3e: /* DS override */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 571 | c->override_base = &ctxt->ds_base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 572 | break; |
| 573 | case 0x26: /* ES override */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 574 | c->override_base = &ctxt->es_base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 575 | break; |
| 576 | case 0x64: /* FS override */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 577 | c->override_base = &ctxt->fs_base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 578 | break; |
| 579 | case 0x65: /* GS override */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 580 | c->override_base = &ctxt->gs_base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 581 | break; |
| 582 | case 0x36: /* SS override */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 583 | c->override_base = &ctxt->ss_base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 584 | break; |
| 585 | case 0xf0: /* LOCK */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 586 | c->lock_prefix = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 587 | break; |
Laurent Vivier | ae6200b | 2007-09-20 11:17:24 +0200 | [diff] [blame] | 588 | case 0xf2: /* REPNE/REPNZ */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 589 | case 0xf3: /* REP/REPE/REPZ */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 590 | c->rep_prefix = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 591 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 592 | default: |
| 593 | goto done_prefixes; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | done_prefixes: |
| 598 | |
| 599 | /* REX prefix. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 600 | if ((mode == X86EMUL_MODE_PROT64) && ((c->b & 0xf0) == 0x40)) { |
| 601 | rex_prefix = c->b; |
| 602 | if (c->b & 8) |
| 603 | c->op_bytes = 8; /* REX.W */ |
| 604 | c->modrm_reg = (c->b & 4) << 1; /* REX.R */ |
| 605 | index_reg = (c->b & 2) << 2; /* REX.X */ |
| 606 | c->modrm_rm = base_reg = (c->b & 1) << 3; /* REG.B */ |
| 607 | c->b = insn_fetch(u8, 1, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | /* Opcode byte(s). */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 611 | c->d = opcode_table[c->b]; |
| 612 | if (c->d == 0) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 613 | /* Two-byte opcode? */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 614 | if (c->b == 0x0f) { |
| 615 | c->twobyte = 1; |
| 616 | c->b = insn_fetch(u8, 1, c->eip); |
| 617 | c->d = twobyte_table[c->b]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | /* Unrecognised? */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 621 | if (c->d == 0) { |
| 622 | DPRINTF("Cannot emulate %02x\n", c->b); |
| 623 | return -1; |
| 624 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | /* ModRM and SIB bytes. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 628 | if (c->d & ModRM) { |
| 629 | c->modrm = insn_fetch(u8, 1, c->eip); |
| 630 | c->modrm_mod |= (c->modrm & 0xc0) >> 6; |
| 631 | c->modrm_reg |= (c->modrm & 0x38) >> 3; |
| 632 | c->modrm_rm |= (c->modrm & 0x07); |
| 633 | c->modrm_ea = 0; |
| 634 | c->use_modrm_ea = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 635 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 636 | if (c->modrm_mod == 3) { |
| 637 | c->modrm_val = *(unsigned long *) |
| 638 | decode_register(c->modrm_rm, c->regs, c->d & ByteOp); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 639 | goto modrm_done; |
| 640 | } |
| 641 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 642 | if (c->ad_bytes == 2) { |
| 643 | unsigned bx = c->regs[VCPU_REGS_RBX]; |
| 644 | unsigned bp = c->regs[VCPU_REGS_RBP]; |
| 645 | unsigned si = c->regs[VCPU_REGS_RSI]; |
| 646 | unsigned di = c->regs[VCPU_REGS_RDI]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 647 | |
| 648 | /* 16-bit ModR/M decode. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 649 | switch (c->modrm_mod) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 650 | case 0: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 651 | if (c->modrm_rm == 6) |
| 652 | c->modrm_ea += |
| 653 | insn_fetch(u16, 2, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 654 | break; |
| 655 | case 1: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 656 | c->modrm_ea += insn_fetch(s8, 1, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 657 | break; |
| 658 | case 2: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 659 | c->modrm_ea += insn_fetch(u16, 2, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 660 | break; |
| 661 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 662 | switch (c->modrm_rm) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 663 | case 0: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 664 | c->modrm_ea += bx + si; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 665 | break; |
| 666 | case 1: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 667 | c->modrm_ea += bx + di; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 668 | break; |
| 669 | case 2: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 670 | c->modrm_ea += bp + si; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 671 | break; |
| 672 | case 3: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 673 | c->modrm_ea += bp + di; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 674 | break; |
| 675 | case 4: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 676 | c->modrm_ea += si; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 677 | break; |
| 678 | case 5: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 679 | c->modrm_ea += di; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 680 | break; |
| 681 | case 6: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 682 | if (c->modrm_mod != 0) |
| 683 | c->modrm_ea += bp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 684 | break; |
| 685 | case 7: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 686 | c->modrm_ea += bx; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 687 | break; |
| 688 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 689 | if (c->modrm_rm == 2 || c->modrm_rm == 3 || |
| 690 | (c->modrm_rm == 6 && c->modrm_mod != 0)) |
| 691 | if (!c->override_base) |
| 692 | c->override_base = &ctxt->ss_base; |
| 693 | c->modrm_ea = (u16)c->modrm_ea; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 694 | } else { |
| 695 | /* 32/64-bit ModR/M decode. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 696 | switch (c->modrm_rm) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 697 | case 4: |
| 698 | case 12: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 699 | sib = insn_fetch(u8, 1, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 700 | index_reg |= (sib >> 3) & 7; |
| 701 | base_reg |= sib & 7; |
| 702 | scale = sib >> 6; |
| 703 | |
| 704 | switch (base_reg) { |
| 705 | case 5: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 706 | if (c->modrm_mod != 0) |
| 707 | c->modrm_ea += |
| 708 | c->regs[base_reg]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 709 | else |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 710 | c->modrm_ea += |
| 711 | insn_fetch(s32, 4, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 712 | break; |
| 713 | default: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 714 | c->modrm_ea += c->regs[base_reg]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 715 | } |
| 716 | switch (index_reg) { |
| 717 | case 4: |
| 718 | break; |
| 719 | default: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 720 | c->modrm_ea += |
| 721 | c->regs[index_reg] << scale; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 722 | |
| 723 | } |
| 724 | break; |
| 725 | case 5: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 726 | if (c->modrm_mod != 0) |
| 727 | c->modrm_ea += c->regs[c->modrm_rm]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 728 | else if (mode == X86EMUL_MODE_PROT64) |
| 729 | rip_relative = 1; |
| 730 | break; |
| 731 | default: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 732 | c->modrm_ea += c->regs[c->modrm_rm]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 733 | break; |
| 734 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 735 | switch (c->modrm_mod) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 736 | case 0: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 737 | if (c->modrm_rm == 5) |
| 738 | c->modrm_ea += |
| 739 | insn_fetch(s32, 4, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 740 | break; |
| 741 | case 1: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 742 | c->modrm_ea += insn_fetch(s8, 1, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 743 | break; |
| 744 | case 2: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 745 | c->modrm_ea += insn_fetch(s32, 4, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 746 | break; |
| 747 | } |
| 748 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 749 | if (!c->override_base) |
| 750 | c->override_base = &ctxt->ds_base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 751 | if (mode == X86EMUL_MODE_PROT64 && |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 752 | c->override_base != &ctxt->fs_base && |
| 753 | c->override_base != &ctxt->gs_base) |
| 754 | c->override_base = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 755 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 756 | if (c->override_base) |
| 757 | c->modrm_ea += *c->override_base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 758 | |
| 759 | if (rip_relative) { |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 760 | c->modrm_ea += c->eip; |
| 761 | switch (c->d & SrcMask) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 762 | case SrcImmByte: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 763 | c->modrm_ea += 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 764 | break; |
| 765 | case SrcImm: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 766 | if (c->d & ByteOp) |
| 767 | c->modrm_ea += 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 768 | else |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 769 | if (c->op_bytes == 8) |
| 770 | c->modrm_ea += 4; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 771 | else |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 772 | c->modrm_ea += c->op_bytes; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 773 | } |
| 774 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 775 | if (c->ad_bytes != 8) |
| 776 | c->modrm_ea = (u32)c->modrm_ea; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 777 | modrm_done: |
| 778 | ; |
| 779 | } |
| 780 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 781 | /* |
| 782 | * Decode and fetch the source operand: register, memory |
| 783 | * or immediate. |
| 784 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 785 | switch (c->d & SrcMask) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 786 | case SrcNone: |
| 787 | break; |
| 788 | case SrcReg: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 789 | c->src.type = OP_REG; |
| 790 | if (c->d & ByteOp) { |
| 791 | c->src.ptr = |
| 792 | decode_register(c->modrm_reg, c->regs, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 793 | (rex_prefix == 0)); |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 794 | c->src.val = c->src.orig_val = *(u8 *)c->src.ptr; |
| 795 | c->src.bytes = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 796 | } else { |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 797 | c->src.ptr = |
| 798 | decode_register(c->modrm_reg, c->regs, 0); |
| 799 | switch ((c->src.bytes = c->op_bytes)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 800 | case 2: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 801 | c->src.val = c->src.orig_val = |
| 802 | *(u16 *) c->src.ptr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 803 | break; |
| 804 | case 4: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 805 | c->src.val = c->src.orig_val = |
| 806 | *(u32 *) c->src.ptr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 807 | break; |
| 808 | case 8: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 809 | c->src.val = c->src.orig_val = |
| 810 | *(u64 *) c->src.ptr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 811 | break; |
| 812 | } |
| 813 | } |
| 814 | break; |
| 815 | case SrcMem16: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 816 | c->src.bytes = 2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 817 | goto srcmem_common; |
| 818 | case SrcMem32: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 819 | c->src.bytes = 4; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 820 | goto srcmem_common; |
| 821 | case SrcMem: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 822 | c->src.bytes = (c->d & ByteOp) ? 1 : |
| 823 | c->op_bytes; |
Rusty Russell | b85b9ee9 | 2007-09-09 14:12:54 +0300 | [diff] [blame] | 824 | /* Don't fetch the address for invlpg: it could be unmapped. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 825 | if (c->twobyte && c->b == 0x01 |
| 826 | && c->modrm_reg == 7) |
Rusty Russell | b85b9ee9 | 2007-09-09 14:12:54 +0300 | [diff] [blame] | 827 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 828 | srcmem_common: |
Aurelien Jarno | 4e62417 | 2007-10-17 19:30:41 +0200 | [diff] [blame] | 829 | /* |
| 830 | * For instructions with a ModR/M byte, switch to register |
| 831 | * access if Mod = 3. |
| 832 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 833 | if ((c->d & ModRM) && c->modrm_mod == 3) { |
| 834 | c->src.type = OP_REG; |
Aurelien Jarno | 4e62417 | 2007-10-17 19:30:41 +0200 | [diff] [blame] | 835 | break; |
| 836 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 837 | c->src.type = OP_MEM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 838 | break; |
| 839 | case SrcImm: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 840 | c->src.type = OP_IMM; |
| 841 | c->src.ptr = (unsigned long *)c->eip; |
| 842 | c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 843 | if (c->src.bytes == 8) |
| 844 | c->src.bytes = 4; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 845 | /* NB. Immediates are sign-extended as necessary. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 846 | switch (c->src.bytes) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 847 | case 1: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 848 | c->src.val = insn_fetch(s8, 1, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 849 | break; |
| 850 | case 2: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 851 | c->src.val = insn_fetch(s16, 2, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 852 | break; |
| 853 | case 4: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 854 | c->src.val = insn_fetch(s32, 4, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 855 | break; |
| 856 | } |
| 857 | break; |
| 858 | case SrcImmByte: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 859 | c->src.type = OP_IMM; |
| 860 | c->src.ptr = (unsigned long *)c->eip; |
| 861 | c->src.bytes = 1; |
| 862 | c->src.val = insn_fetch(s8, 1, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 863 | break; |
| 864 | } |
| 865 | |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 866 | /* Decode and fetch the destination operand: register or memory. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 867 | switch (c->d & DstMask) { |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 868 | case ImplicitOps: |
| 869 | /* Special instructions do their own operand decoding. */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 870 | return 0; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 871 | case DstReg: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 872 | c->dst.type = OP_REG; |
| 873 | if ((c->d & ByteOp) |
| 874 | && !(c->twobyte && |
| 875 | (c->b == 0xb6 || c->b == 0xb7))) { |
| 876 | c->dst.ptr = |
| 877 | decode_register(c->modrm_reg, c->regs, |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 878 | (rex_prefix == 0)); |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 879 | c->dst.val = *(u8 *) c->dst.ptr; |
| 880 | c->dst.bytes = 1; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 881 | } else { |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 882 | c->dst.ptr = |
| 883 | decode_register(c->modrm_reg, c->regs, 0); |
| 884 | switch ((c->dst.bytes = c->op_bytes)) { |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 885 | case 2: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 886 | c->dst.val = *(u16 *)c->dst.ptr; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 887 | break; |
| 888 | case 4: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 889 | c->dst.val = *(u32 *)c->dst.ptr; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 890 | break; |
| 891 | case 8: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 892 | c->dst.val = *(u64 *)c->dst.ptr; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 893 | break; |
| 894 | } |
| 895 | } |
| 896 | break; |
| 897 | case DstMem: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 898 | if ((c->d & ModRM) && c->modrm_mod == 3) { |
| 899 | c->dst.type = OP_REG; |
Aurelien Jarno | 4e62417 | 2007-10-17 19:30:41 +0200 | [diff] [blame] | 900 | break; |
| 901 | } |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 902 | c->dst.type = OP_MEM; |
| 903 | break; |
| 904 | } |
| 905 | |
| 906 | done: |
| 907 | return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0; |
| 908 | } |
| 909 | |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 910 | static inline void emulate_push(struct x86_emulate_ctxt *ctxt) |
| 911 | { |
| 912 | struct decode_cache *c = &ctxt->decode; |
| 913 | |
| 914 | c->dst.type = OP_MEM; |
| 915 | c->dst.bytes = c->op_bytes; |
| 916 | c->dst.val = c->src.val; |
| 917 | register_address_increment(c->regs[VCPU_REGS_RSP], -c->op_bytes); |
| 918 | c->dst.ptr = (void *) register_address(ctxt->ss_base, |
| 919 | c->regs[VCPU_REGS_RSP]); |
| 920 | } |
| 921 | |
| 922 | static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt, |
| 923 | struct x86_emulate_ops *ops) |
| 924 | { |
| 925 | struct decode_cache *c = &ctxt->decode; |
| 926 | int rc; |
| 927 | |
| 928 | /* 64-bit mode: POP always pops a 64-bit operand. */ |
| 929 | |
| 930 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
| 931 | c->dst.bytes = 8; |
| 932 | |
| 933 | rc = ops->read_std(register_address(ctxt->ss_base, |
| 934 | c->regs[VCPU_REGS_RSP]), |
| 935 | &c->dst.val, c->dst.bytes, ctxt->vcpu); |
| 936 | if (rc != 0) |
| 937 | return rc; |
| 938 | |
| 939 | register_address_increment(c->regs[VCPU_REGS_RSP], c->dst.bytes); |
| 940 | |
| 941 | return 0; |
| 942 | } |
| 943 | |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 944 | static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 945 | { |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 946 | struct decode_cache *c = &ctxt->decode; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 947 | switch (c->modrm_reg) { |
| 948 | case 0: /* rol */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 949 | emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 950 | break; |
| 951 | case 1: /* ror */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 952 | emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 953 | break; |
| 954 | case 2: /* rcl */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 955 | emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 956 | break; |
| 957 | case 3: /* rcr */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 958 | emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 959 | break; |
| 960 | case 4: /* sal/shl */ |
| 961 | case 6: /* sal/shl */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 962 | emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 963 | break; |
| 964 | case 5: /* shr */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 965 | emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 966 | break; |
| 967 | case 7: /* sar */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 968 | emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 969 | break; |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt, |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 974 | struct x86_emulate_ops *ops) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 975 | { |
| 976 | struct decode_cache *c = &ctxt->decode; |
| 977 | int rc = 0; |
| 978 | |
| 979 | switch (c->modrm_reg) { |
| 980 | case 0 ... 1: /* test */ |
| 981 | /* |
| 982 | * Special case in Grp3: test has an immediate |
| 983 | * source operand. |
| 984 | */ |
| 985 | c->src.type = OP_IMM; |
| 986 | c->src.ptr = (unsigned long *)c->eip; |
| 987 | c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 988 | if (c->src.bytes == 8) |
| 989 | c->src.bytes = 4; |
| 990 | switch (c->src.bytes) { |
| 991 | case 1: |
| 992 | c->src.val = insn_fetch(s8, 1, c->eip); |
| 993 | break; |
| 994 | case 2: |
| 995 | c->src.val = insn_fetch(s16, 2, c->eip); |
| 996 | break; |
| 997 | case 4: |
| 998 | c->src.val = insn_fetch(s32, 4, c->eip); |
| 999 | break; |
| 1000 | } |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1001 | emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1002 | break; |
| 1003 | case 2: /* not */ |
| 1004 | c->dst.val = ~c->dst.val; |
| 1005 | break; |
| 1006 | case 3: /* neg */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1007 | emulate_1op("neg", c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1008 | break; |
| 1009 | default: |
| 1010 | DPRINTF("Cannot emulate %02x\n", c->b); |
| 1011 | rc = X86EMUL_UNHANDLEABLE; |
| 1012 | break; |
| 1013 | } |
| 1014 | done: |
| 1015 | return rc; |
| 1016 | } |
| 1017 | |
| 1018 | static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt, |
| 1019 | struct x86_emulate_ops *ops, |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1020 | int *no_wb) |
| 1021 | { |
| 1022 | struct decode_cache *c = &ctxt->decode; |
| 1023 | int rc; |
| 1024 | |
| 1025 | switch (c->modrm_reg) { |
| 1026 | case 0: /* inc */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1027 | emulate_1op("inc", c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1028 | break; |
| 1029 | case 1: /* dec */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1030 | emulate_1op("dec", c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1031 | break; |
| 1032 | case 4: /* jmp abs */ |
| 1033 | if (c->b == 0xff) |
| 1034 | c->eip = c->dst.val; |
| 1035 | else { |
| 1036 | DPRINTF("Cannot emulate %02x\n", c->b); |
| 1037 | return X86EMUL_UNHANDLEABLE; |
| 1038 | } |
| 1039 | break; |
| 1040 | case 6: /* push */ |
| 1041 | |
| 1042 | /* 64-bit mode: PUSH always pushes a 64-bit operand. */ |
| 1043 | |
| 1044 | if (ctxt->mode == X86EMUL_MODE_PROT64) { |
| 1045 | c->dst.bytes = 8; |
| 1046 | rc = ops->read_std((unsigned long)c->dst.ptr, |
| 1047 | &c->dst.val, 8, ctxt->vcpu); |
| 1048 | if (rc != 0) |
| 1049 | return rc; |
| 1050 | } |
| 1051 | register_address_increment(c->regs[VCPU_REGS_RSP], |
| 1052 | -c->dst.bytes); |
| 1053 | rc = ops->write_emulated(register_address(ctxt->ss_base, |
| 1054 | c->regs[VCPU_REGS_RSP]), &c->dst.val, |
| 1055 | c->dst.bytes, ctxt->vcpu); |
| 1056 | if (rc != 0) |
| 1057 | return rc; |
| 1058 | *no_wb = 1; |
| 1059 | break; |
| 1060 | default: |
| 1061 | DPRINTF("Cannot emulate %02x\n", c->b); |
| 1062 | return X86EMUL_UNHANDLEABLE; |
| 1063 | } |
| 1064 | return 0; |
| 1065 | } |
| 1066 | |
| 1067 | static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt, |
| 1068 | struct x86_emulate_ops *ops, |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1069 | unsigned long cr2) |
| 1070 | { |
| 1071 | struct decode_cache *c = &ctxt->decode; |
| 1072 | u64 old, new; |
| 1073 | int rc; |
| 1074 | |
| 1075 | rc = ops->read_emulated(cr2, &old, 8, ctxt->vcpu); |
| 1076 | if (rc != 0) |
| 1077 | return rc; |
| 1078 | |
| 1079 | if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) || |
| 1080 | ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) { |
| 1081 | |
| 1082 | c->regs[VCPU_REGS_RAX] = (u32) (old >> 0); |
| 1083 | c->regs[VCPU_REGS_RDX] = (u32) (old >> 32); |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1084 | ctxt->eflags &= ~EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1085 | |
| 1086 | } else { |
| 1087 | new = ((u64)c->regs[VCPU_REGS_RCX] << 32) | |
| 1088 | (u32) c->regs[VCPU_REGS_RBX]; |
| 1089 | |
| 1090 | rc = ops->cmpxchg_emulated(cr2, &old, &new, 8, ctxt->vcpu); |
| 1091 | if (rc != 0) |
| 1092 | return rc; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1093 | ctxt->eflags |= EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1094 | } |
| 1095 | return 0; |
| 1096 | } |
| 1097 | |
| 1098 | static inline int writeback(struct x86_emulate_ctxt *ctxt, |
| 1099 | struct x86_emulate_ops *ops) |
| 1100 | { |
| 1101 | int rc; |
| 1102 | struct decode_cache *c = &ctxt->decode; |
| 1103 | |
| 1104 | switch (c->dst.type) { |
| 1105 | case OP_REG: |
| 1106 | /* The 4-byte case *is* correct: |
| 1107 | * in 64-bit mode we zero-extend. |
| 1108 | */ |
| 1109 | switch (c->dst.bytes) { |
| 1110 | case 1: |
| 1111 | *(u8 *)c->dst.ptr = (u8)c->dst.val; |
| 1112 | break; |
| 1113 | case 2: |
| 1114 | *(u16 *)c->dst.ptr = (u16)c->dst.val; |
| 1115 | break; |
| 1116 | case 4: |
| 1117 | *c->dst.ptr = (u32)c->dst.val; |
| 1118 | break; /* 64b: zero-ext */ |
| 1119 | case 8: |
| 1120 | *c->dst.ptr = c->dst.val; |
| 1121 | break; |
| 1122 | } |
| 1123 | break; |
| 1124 | case OP_MEM: |
| 1125 | if (c->lock_prefix) |
| 1126 | rc = ops->cmpxchg_emulated( |
| 1127 | (unsigned long)c->dst.ptr, |
| 1128 | &c->dst.orig_val, |
| 1129 | &c->dst.val, |
| 1130 | c->dst.bytes, |
| 1131 | ctxt->vcpu); |
| 1132 | else |
| 1133 | rc = ops->write_emulated( |
| 1134 | (unsigned long)c->dst.ptr, |
| 1135 | &c->dst.val, |
| 1136 | c->dst.bytes, |
| 1137 | ctxt->vcpu); |
| 1138 | if (rc != 0) |
| 1139 | return rc; |
| 1140 | default: |
| 1141 | break; |
| 1142 | } |
| 1143 | return 0; |
| 1144 | } |
| 1145 | |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1146 | int |
Laurent Vivier | 1be3aa4 | 2007-09-18 11:27:27 +0200 | [diff] [blame] | 1147 | 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] | 1148 | { |
| 1149 | unsigned long cr2 = ctxt->cr2; |
| 1150 | int no_wb = 0; |
| 1151 | u64 msr_data; |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1152 | unsigned long saved_eip = 0; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1153 | struct decode_cache *c = &ctxt->decode; |
Laurent Vivier | 1be3aa4 | 2007-09-18 11:27:27 +0200 | [diff] [blame] | 1154 | int rc = 0; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1155 | |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1156 | /* Shadow copy of register state. Committed on successful emulation. |
| 1157 | * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't |
| 1158 | * modify them. |
| 1159 | */ |
| 1160 | |
| 1161 | memcpy(c->regs, ctxt->vcpu->regs, sizeof c->regs); |
| 1162 | saved_eip = c->eip; |
| 1163 | |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1164 | if ((c->d & ModRM) && (c->modrm_mod != 3)) |
| 1165 | cr2 = c->modrm_ea; |
| 1166 | |
| 1167 | if (c->src.type == OP_MEM) { |
| 1168 | c->src.ptr = (unsigned long *)cr2; |
| 1169 | c->src.val = 0; |
| 1170 | if ((rc = ops->read_emulated((unsigned long)c->src.ptr, |
| 1171 | &c->src.val, |
| 1172 | c->src.bytes, |
| 1173 | ctxt->vcpu)) != 0) |
| 1174 | goto done; |
| 1175 | c->src.orig_val = c->src.val; |
| 1176 | } |
| 1177 | |
| 1178 | if ((c->d & DstMask) == ImplicitOps) |
| 1179 | goto special_insn; |
| 1180 | |
| 1181 | |
| 1182 | if (c->dst.type == OP_MEM) { |
| 1183 | c->dst.ptr = (unsigned long *)cr2; |
| 1184 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 1185 | c->dst.val = 0; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1186 | if (c->d & BitOp) { |
| 1187 | unsigned long mask = ~(c->dst.bytes * 8 - 1); |
Avi Kivity | df513e2 | 2007-03-28 20:04:16 +0200 | [diff] [blame] | 1188 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1189 | c->dst.ptr = (void *)c->dst.ptr + |
| 1190 | (c->src.val & mask) / 8; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1191 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1192 | if (!(c->d & Mov) && |
| 1193 | /* optimisation - avoid slow emulated read */ |
| 1194 | ((rc = ops->read_emulated((unsigned long)c->dst.ptr, |
| 1195 | &c->dst.val, |
| 1196 | c->dst.bytes, ctxt->vcpu)) != 0)) |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1197 | goto done; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1198 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1199 | c->dst.orig_val = c->dst.val; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1200 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1201 | if (c->twobyte) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1202 | goto twobyte_insn; |
| 1203 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1204 | switch (c->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1205 | case 0x00 ... 0x05: |
| 1206 | add: /* add */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1207 | emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1208 | break; |
| 1209 | case 0x08 ... 0x0d: |
| 1210 | or: /* or */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1211 | emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1212 | break; |
| 1213 | case 0x10 ... 0x15: |
| 1214 | adc: /* adc */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1215 | emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1216 | break; |
| 1217 | case 0x18 ... 0x1d: |
| 1218 | sbb: /* sbb */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1219 | emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1220 | break; |
Nitin A Kamble | 19eb938 | 2007-08-17 15:17:41 +0300 | [diff] [blame] | 1221 | case 0x20 ... 0x23: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1222 | and: /* and */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1223 | emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1224 | break; |
Nitin A Kamble | 19eb938 | 2007-08-17 15:17:41 +0300 | [diff] [blame] | 1225 | case 0x24: /* and al imm8 */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1226 | c->dst.type = OP_REG; |
| 1227 | c->dst.ptr = &c->regs[VCPU_REGS_RAX]; |
| 1228 | c->dst.val = *(u8 *)c->dst.ptr; |
| 1229 | c->dst.bytes = 1; |
| 1230 | c->dst.orig_val = c->dst.val; |
Nitin A Kamble | 19eb938 | 2007-08-17 15:17:41 +0300 | [diff] [blame] | 1231 | goto and; |
| 1232 | case 0x25: /* and ax imm16, or eax imm32 */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1233 | c->dst.type = OP_REG; |
| 1234 | c->dst.bytes = c->op_bytes; |
| 1235 | c->dst.ptr = &c->regs[VCPU_REGS_RAX]; |
| 1236 | if (c->op_bytes == 2) |
| 1237 | c->dst.val = *(u16 *)c->dst.ptr; |
Nitin A Kamble | 19eb938 | 2007-08-17 15:17:41 +0300 | [diff] [blame] | 1238 | else |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1239 | c->dst.val = *(u32 *)c->dst.ptr; |
| 1240 | c->dst.orig_val = c->dst.val; |
Nitin A Kamble | 19eb938 | 2007-08-17 15:17:41 +0300 | [diff] [blame] | 1241 | goto and; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1242 | case 0x28 ... 0x2d: |
| 1243 | sub: /* sub */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1244 | emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1245 | break; |
| 1246 | case 0x30 ... 0x35: |
| 1247 | xor: /* xor */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1248 | emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1249 | break; |
| 1250 | case 0x38 ... 0x3d: |
| 1251 | cmp: /* cmp */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1252 | emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1253 | break; |
| 1254 | case 0x63: /* movsxd */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1255 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1256 | goto cannot_emulate; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1257 | c->dst.val = (s32) c->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1258 | break; |
| 1259 | case 0x80 ... 0x83: /* Grp1 */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1260 | switch (c->modrm_reg) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1261 | case 0: |
| 1262 | goto add; |
| 1263 | case 1: |
| 1264 | goto or; |
| 1265 | case 2: |
| 1266 | goto adc; |
| 1267 | case 3: |
| 1268 | goto sbb; |
| 1269 | case 4: |
| 1270 | goto and; |
| 1271 | case 5: |
| 1272 | goto sub; |
| 1273 | case 6: |
| 1274 | goto xor; |
| 1275 | case 7: |
| 1276 | goto cmp; |
| 1277 | } |
| 1278 | break; |
| 1279 | case 0x84 ... 0x85: |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1280 | emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1281 | break; |
| 1282 | case 0x86 ... 0x87: /* xchg */ |
| 1283 | /* Write back the register source. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1284 | switch (c->dst.bytes) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1285 | case 1: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1286 | *(u8 *) c->src.ptr = (u8) c->dst.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1287 | break; |
| 1288 | case 2: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1289 | *(u16 *) c->src.ptr = (u16) c->dst.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1290 | break; |
| 1291 | case 4: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1292 | *c->src.ptr = (u32) c->dst.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1293 | break; /* 64b reg: zero-extend */ |
| 1294 | case 8: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1295 | *c->src.ptr = c->dst.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1296 | break; |
| 1297 | } |
| 1298 | /* |
| 1299 | * Write back the memory destination with implicit LOCK |
| 1300 | * prefix. |
| 1301 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1302 | c->dst.val = c->src.val; |
| 1303 | c->lock_prefix = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1304 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1305 | case 0x88 ... 0x8b: /* mov */ |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 1306 | goto mov; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 1307 | case 0x8d: /* lea r16/r32, m */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1308 | c->dst.val = c->modrm_val; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 1309 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1310 | case 0x8f: /* pop (sole member of Grp1a) */ |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1311 | rc = emulate_grp1a(ctxt, ops); |
| 1312 | if (rc != 0) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1313 | goto done; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1314 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 1315 | case 0xa0 ... 0xa1: /* mov */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1316 | c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX]; |
| 1317 | c->dst.val = c->src.val; |
| 1318 | /* skip src displacement */ |
| 1319 | c->eip += c->ad_bytes; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 1320 | break; |
| 1321 | case 0xa2 ... 0xa3: /* mov */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1322 | c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX]; |
| 1323 | /* skip c->dst displacement */ |
| 1324 | c->eip += c->ad_bytes; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 1325 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1326 | case 0xc0 ... 0xc1: |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1327 | emulate_grp2(ctxt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1328 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 1329 | case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */ |
| 1330 | mov: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1331 | c->dst.val = c->src.val; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 1332 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1333 | case 0xd0 ... 0xd1: /* Grp2 */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1334 | c->src.val = 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1335 | emulate_grp2(ctxt); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1336 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1337 | case 0xd2 ... 0xd3: /* Grp2 */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1338 | c->src.val = c->regs[VCPU_REGS_RCX]; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1339 | emulate_grp2(ctxt); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1340 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1341 | case 0xf6 ... 0xf7: /* Grp3 */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1342 | rc = emulate_grp3(ctxt, ops); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1343 | if (rc != 0) |
| 1344 | goto done; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1345 | break; |
| 1346 | case 0xfe ... 0xff: /* Grp4/Grp5 */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1347 | rc = emulate_grp45(ctxt, ops, &no_wb); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1348 | if (rc != 0) |
| 1349 | goto done; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1350 | break; |
| 1351 | } |
| 1352 | |
| 1353 | writeback: |
Luca Tettamanti | 02c03a3 | 2007-06-19 22:41:20 +0200 | [diff] [blame] | 1354 | if (!no_wb) { |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1355 | rc = writeback(ctxt, ops); |
| 1356 | if (rc != 0) |
| 1357 | goto done; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | /* Commit shadow register state. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1361 | memcpy(ctxt->vcpu->regs, c->regs, sizeof c->regs); |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1362 | ctxt->vcpu->rip = c->eip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1363 | |
| 1364 | done: |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1365 | if (rc == X86EMUL_UNHANDLEABLE) { |
| 1366 | c->eip = saved_eip; |
| 1367 | return -1; |
| 1368 | } |
| 1369 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1370 | |
| 1371 | special_insn: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1372 | if (c->twobyte) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1373 | goto twobyte_special_insn; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1374 | switch (c->b) { |
Nitin A Kamble | 7e77816 | 2007-08-19 11:07:06 +0300 | [diff] [blame] | 1375 | case 0x50 ... 0x57: /* push reg */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1376 | if (c->op_bytes == 2) |
| 1377 | c->src.val = (u16) c->regs[c->b & 0x7]; |
Nitin A Kamble | 7e77816 | 2007-08-19 11:07:06 +0300 | [diff] [blame] | 1378 | else |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1379 | c->src.val = (u32) c->regs[c->b & 0x7]; |
| 1380 | c->dst.type = OP_MEM; |
| 1381 | c->dst.bytes = c->op_bytes; |
| 1382 | c->dst.val = c->src.val; |
| 1383 | register_address_increment(c->regs[VCPU_REGS_RSP], |
| 1384 | -c->op_bytes); |
| 1385 | c->dst.ptr = (void *) register_address( |
| 1386 | ctxt->ss_base, c->regs[VCPU_REGS_RSP]); |
Nitin A Kamble | 7e77816 | 2007-08-19 11:07:06 +0300 | [diff] [blame] | 1387 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 1388 | case 0x58 ... 0x5f: /* pop reg */ |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1389 | c->dst.ptr = (unsigned long *)&c->regs[c->b & 0x7]; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 1390 | pop_instruction: |
| 1391 | if ((rc = ops->read_std(register_address(ctxt->ss_base, |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1392 | c->regs[VCPU_REGS_RSP]), c->dst.ptr, |
| 1393 | c->op_bytes, ctxt->vcpu)) != 0) |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 1394 | goto done; |
| 1395 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1396 | register_address_increment(c->regs[VCPU_REGS_RSP], |
| 1397 | c->op_bytes); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 1398 | no_wb = 1; /* Disable writeback. */ |
| 1399 | break; |
Avi Kivity | 1e35d3c | 2007-10-26 14:16:56 +0200 | [diff] [blame] | 1400 | case 0x6a: /* push imm8 */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1401 | c->src.val = 0L; |
| 1402 | c->src.val = insn_fetch(s8, 1, c->eip); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1403 | emulate_push(ctxt); |
Avi Kivity | 1e35d3c | 2007-10-26 14:16:56 +0200 | [diff] [blame] | 1404 | break; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 1405 | case 0x6c: /* insb */ |
| 1406 | case 0x6d: /* insw/insd */ |
Laurent Vivier | 3090dd7 | 2007-08-05 10:43:32 +0300 | [diff] [blame] | 1407 | if (kvm_emulate_pio_string(ctxt->vcpu, NULL, |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1408 | 1, |
| 1409 | (c->d & ByteOp) ? 1 : c->op_bytes, |
| 1410 | c->rep_prefix ? |
| 1411 | address_mask(c->regs[VCPU_REGS_RCX]) : 1, |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1412 | (ctxt->eflags & EFLG_DF), |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 1413 | register_address(ctxt->es_base, |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1414 | c->regs[VCPU_REGS_RDI]), |
| 1415 | c->rep_prefix, |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1416 | c->regs[VCPU_REGS_RDX]) == 0) { |
| 1417 | c->eip = saved_eip; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 1418 | return -1; |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1419 | } |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 1420 | return 0; |
| 1421 | case 0x6e: /* outsb */ |
| 1422 | case 0x6f: /* outsw/outsd */ |
Laurent Vivier | 3090dd7 | 2007-08-05 10:43:32 +0300 | [diff] [blame] | 1423 | if (kvm_emulate_pio_string(ctxt->vcpu, NULL, |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1424 | 0, |
| 1425 | (c->d & ByteOp) ? 1 : c->op_bytes, |
| 1426 | c->rep_prefix ? |
| 1427 | address_mask(c->regs[VCPU_REGS_RCX]) : 1, |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1428 | (ctxt->eflags & EFLG_DF), |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1429 | register_address(c->override_base ? |
| 1430 | *c->override_base : |
| 1431 | ctxt->ds_base, |
| 1432 | c->regs[VCPU_REGS_RSI]), |
| 1433 | c->rep_prefix, |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1434 | c->regs[VCPU_REGS_RDX]) == 0) { |
| 1435 | c->eip = saved_eip; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 1436 | return -1; |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1437 | } |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 1438 | return 0; |
Nitin A Kamble | 55bebde | 2007-09-15 10:25:41 +0300 | [diff] [blame] | 1439 | case 0x70 ... 0x7f: /* jcc (short) */ { |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1440 | int rel = insn_fetch(s8, 1, c->eip); |
Nitin A Kamble | 55bebde | 2007-09-15 10:25:41 +0300 | [diff] [blame] | 1441 | |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1442 | if (test_cc(c->b, ctxt->eflags)) |
Nitin A Kamble | 55bebde | 2007-09-15 10:25:41 +0300 | [diff] [blame] | 1443 | JMP_REL(rel); |
| 1444 | break; |
| 1445 | } |
Nitin A Kamble | fd2a760 | 2007-08-28 18:22:47 -0700 | [diff] [blame] | 1446 | case 0x9c: /* pushf */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1447 | c->src.val = (unsigned long) ctxt->eflags; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1448 | emulate_push(ctxt); |
| 1449 | break; |
Nitin A Kamble | 535eabc | 2007-09-15 10:45:05 +0300 | [diff] [blame] | 1450 | case 0x9d: /* popf */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1451 | c->dst.ptr = (unsigned long *) &ctxt->eflags; |
Nitin A Kamble | 535eabc | 2007-09-15 10:45:05 +0300 | [diff] [blame] | 1452 | goto pop_instruction; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 1453 | case 0xc3: /* ret */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1454 | c->dst.ptr = &c->eip; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 1455 | goto pop_instruction; |
| 1456 | case 0xf4: /* hlt */ |
| 1457 | ctxt->vcpu->halt_request = 1; |
| 1458 | goto done; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 1459 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1460 | if (c->rep_prefix) { |
| 1461 | if (c->regs[VCPU_REGS_RCX] == 0) { |
| 1462 | ctxt->vcpu->rip = c->eip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1463 | goto done; |
| 1464 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1465 | c->regs[VCPU_REGS_RCX]--; |
| 1466 | c->eip = ctxt->vcpu->rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1467 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1468 | switch (c->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1469 | case 0xa4 ... 0xa5: /* movs */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1470 | c->dst.type = OP_MEM; |
| 1471 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 1472 | c->dst.ptr = (unsigned long *)register_address( |
| 1473 | ctxt->es_base, |
| 1474 | c->regs[VCPU_REGS_RDI]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1475 | if ((rc = ops->read_emulated(register_address( |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1476 | c->override_base ? *c->override_base : |
| 1477 | ctxt->ds_base, |
| 1478 | c->regs[VCPU_REGS_RSI]), |
| 1479 | &c->dst.val, |
| 1480 | c->dst.bytes, ctxt->vcpu)) != 0) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1481 | goto done; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1482 | register_address_increment(c->regs[VCPU_REGS_RSI], |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1483 | (ctxt->eflags & EFLG_DF) ? -c->dst.bytes |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1484 | : c->dst.bytes); |
| 1485 | register_address_increment(c->regs[VCPU_REGS_RDI], |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1486 | (ctxt->eflags & EFLG_DF) ? -c->dst.bytes |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1487 | : c->dst.bytes); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1488 | break; |
| 1489 | case 0xa6 ... 0xa7: /* cmps */ |
| 1490 | DPRINTF("Urk! I don't handle CMPS.\n"); |
| 1491 | goto cannot_emulate; |
| 1492 | case 0xaa ... 0xab: /* stos */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1493 | c->dst.type = OP_MEM; |
| 1494 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 1495 | c->dst.ptr = (unsigned long *)cr2; |
| 1496 | c->dst.val = c->regs[VCPU_REGS_RAX]; |
| 1497 | register_address_increment(c->regs[VCPU_REGS_RDI], |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1498 | (ctxt->eflags & EFLG_DF) ? -c->dst.bytes |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1499 | : c->dst.bytes); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1500 | break; |
| 1501 | case 0xac ... 0xad: /* lods */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1502 | c->dst.type = OP_REG; |
| 1503 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 1504 | c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX]; |
| 1505 | if ((rc = ops->read_emulated(cr2, &c->dst.val, |
| 1506 | c->dst.bytes, |
Laurent Vivier | cebff02 | 2007-07-30 13:35:24 +0300 | [diff] [blame] | 1507 | ctxt->vcpu)) != 0) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1508 | goto done; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1509 | register_address_increment(c->regs[VCPU_REGS_RSI], |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1510 | (ctxt->eflags & EFLG_DF) ? -c->dst.bytes |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1511 | : c->dst.bytes); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1512 | break; |
| 1513 | case 0xae ... 0xaf: /* scas */ |
| 1514 | DPRINTF("Urk! I don't handle SCAS.\n"); |
| 1515 | goto cannot_emulate; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 1516 | case 0xe8: /* call (near) */ { |
| 1517 | long int rel; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1518 | switch (c->op_bytes) { |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 1519 | case 2: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1520 | rel = insn_fetch(s16, 2, c->eip); |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 1521 | break; |
| 1522 | case 4: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1523 | rel = insn_fetch(s32, 4, c->eip); |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 1524 | break; |
| 1525 | case 8: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1526 | rel = insn_fetch(s64, 8, c->eip); |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 1527 | break; |
| 1528 | default: |
| 1529 | DPRINTF("Call: Invalid op_bytes\n"); |
| 1530 | goto cannot_emulate; |
| 1531 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1532 | c->src.val = (unsigned long) c->eip; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 1533 | JMP_REL(rel); |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1534 | c->op_bytes = c->ad_bytes; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1535 | emulate_push(ctxt); |
| 1536 | break; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 1537 | } |
| 1538 | case 0xe9: /* jmp rel */ |
| 1539 | case 0xeb: /* jmp rel short */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1540 | JMP_REL(c->src.val); |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 1541 | no_wb = 1; /* Disable writeback. */ |
| 1542 | break; |
| 1543 | |
Nitin A Kamble | 7f0aaee | 2007-06-19 11:16:04 +0300 | [diff] [blame] | 1544 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1545 | } |
| 1546 | goto writeback; |
| 1547 | |
| 1548 | twobyte_insn: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1549 | switch (c->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1550 | case 0x01: /* lgdt, lidt, lmsw */ |
Aurelien Jarno | d37c855 | 2007-07-25 10:19:54 +0200 | [diff] [blame] | 1551 | /* Disable writeback. */ |
| 1552 | no_wb = 1; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1553 | switch (c->modrm_reg) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1554 | u16 size; |
| 1555 | unsigned long address; |
| 1556 | |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 1557 | case 0: /* vmcall */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1558 | if (c->modrm_mod != 3 || c->modrm_rm != 1) |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 1559 | goto cannot_emulate; |
| 1560 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1561 | rc = kvm_fix_hypercall(ctxt->vcpu); |
| 1562 | if (rc) |
| 1563 | goto done; |
| 1564 | |
| 1565 | kvm_emulate_hypercall(ctxt->vcpu); |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 1566 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1567 | case 2: /* lgdt */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1568 | rc = read_descriptor(ctxt, ops, c->src.ptr, |
| 1569 | &size, &address, c->op_bytes); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1570 | if (rc) |
| 1571 | goto done; |
| 1572 | realmode_lgdt(ctxt->vcpu, size, address); |
| 1573 | break; |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 1574 | case 3: /* lidt/vmmcall */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1575 | if (c->modrm_mod == 3 && c->modrm_rm == 1) { |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1576 | rc = kvm_fix_hypercall(ctxt->vcpu); |
| 1577 | if (rc) |
| 1578 | goto done; |
| 1579 | kvm_emulate_hypercall(ctxt->vcpu); |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 1580 | } else { |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1581 | rc = read_descriptor(ctxt, ops, c->src.ptr, |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 1582 | &size, &address, |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1583 | c->op_bytes); |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 1584 | if (rc) |
| 1585 | goto done; |
| 1586 | realmode_lidt(ctxt->vcpu, size, address); |
| 1587 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1588 | break; |
| 1589 | case 4: /* smsw */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1590 | if (c->modrm_mod != 3) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1591 | goto cannot_emulate; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1592 | *(u16 *)&c->regs[c->modrm_rm] |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1593 | = realmode_get_cr(ctxt->vcpu, 0); |
| 1594 | break; |
| 1595 | case 6: /* lmsw */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1596 | if (c->modrm_mod != 3) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1597 | goto cannot_emulate; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1598 | realmode_lmsw(ctxt->vcpu, (u16)c->modrm_val, |
| 1599 | &ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1600 | break; |
| 1601 | case 7: /* invlpg*/ |
| 1602 | emulate_invlpg(ctxt->vcpu, cr2); |
| 1603 | break; |
| 1604 | default: |
| 1605 | goto cannot_emulate; |
| 1606 | } |
| 1607 | break; |
| 1608 | case 0x21: /* mov from dr to reg */ |
Avi Kivity | bac27d3 | 2007-08-05 10:16:11 +0300 | [diff] [blame] | 1609 | no_wb = 1; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1610 | if (c->modrm_mod != 3) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1611 | goto cannot_emulate; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1612 | rc = emulator_get_dr(ctxt, c->modrm_reg, &c->regs[c->modrm_rm]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1613 | break; |
| 1614 | case 0x23: /* mov from reg to dr */ |
Avi Kivity | bac27d3 | 2007-08-05 10:16:11 +0300 | [diff] [blame] | 1615 | no_wb = 1; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1616 | if (c->modrm_mod != 3) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1617 | goto cannot_emulate; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1618 | rc = emulator_set_dr(ctxt, c->modrm_reg, |
| 1619 | c->regs[c->modrm_rm]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1620 | break; |
| 1621 | case 0x40 ... 0x4f: /* cmov */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1622 | c->dst.val = c->dst.orig_val = c->src.val; |
Avi Kivity | e324345 | 2007-07-20 12:30:58 +0300 | [diff] [blame] | 1623 | no_wb = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1624 | /* |
| 1625 | * First, assume we're decoding an even cmov opcode |
| 1626 | * (lsb == 0). |
| 1627 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1628 | switch ((c->b & 15) >> 1) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1629 | case 0: /* cmovo */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1630 | no_wb = (ctxt->eflags & EFLG_OF) ? 0 : 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1631 | break; |
| 1632 | case 1: /* cmovb/cmovc/cmovnae */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1633 | no_wb = (ctxt->eflags & EFLG_CF) ? 0 : 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1634 | break; |
| 1635 | case 2: /* cmovz/cmove */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1636 | no_wb = (ctxt->eflags & EFLG_ZF) ? 0 : 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1637 | break; |
| 1638 | case 3: /* cmovbe/cmovna */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1639 | no_wb = (ctxt->eflags & (EFLG_CF | EFLG_ZF)) ? 0 : 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1640 | break; |
| 1641 | case 4: /* cmovs */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1642 | no_wb = (ctxt->eflags & EFLG_SF) ? 0 : 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1643 | break; |
| 1644 | case 5: /* cmovp/cmovpe */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1645 | no_wb = (ctxt->eflags & EFLG_PF) ? 0 : 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1646 | break; |
| 1647 | case 7: /* cmovle/cmovng */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1648 | no_wb = (ctxt->eflags & EFLG_ZF) ? 0 : 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1649 | /* fall through */ |
| 1650 | case 6: /* cmovl/cmovnge */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1651 | no_wb &= (!(ctxt->eflags & EFLG_SF) != |
| 1652 | !(ctxt->eflags & EFLG_OF)) ? 0 : 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1653 | break; |
| 1654 | } |
| 1655 | /* Odd cmov opcodes (lsb == 1) have inverted sense. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1656 | no_wb ^= c->b & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1657 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 1658 | case 0xa3: |
| 1659 | bt: /* bt */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1660 | /* only subword offset */ |
| 1661 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1662 | emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 1663 | break; |
| 1664 | case 0xab: |
| 1665 | bts: /* bts */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1666 | /* only subword offset */ |
| 1667 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1668 | emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 1669 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1670 | case 0xb0 ... 0xb1: /* cmpxchg */ |
| 1671 | /* |
| 1672 | * Save real source value, then compare EAX against |
| 1673 | * destination. |
| 1674 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1675 | c->src.orig_val = c->src.val; |
| 1676 | c->src.val = c->regs[VCPU_REGS_RAX]; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1677 | emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags); |
| 1678 | if (ctxt->eflags & EFLG_ZF) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1679 | /* Success: write back to memory. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1680 | c->dst.val = c->src.orig_val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1681 | } else { |
| 1682 | /* Failure: write the value we saw to EAX. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1683 | c->dst.type = OP_REG; |
| 1684 | c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1685 | } |
| 1686 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1687 | case 0xb3: |
| 1688 | btr: /* btr */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1689 | /* only subword offset */ |
| 1690 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1691 | emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1692 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1693 | case 0xb6 ... 0xb7: /* movzx */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1694 | c->dst.bytes = c->op_bytes; |
| 1695 | c->dst.val = (c->d & ByteOp) ? (u8) c->src.val |
| 1696 | : (u16) c->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1697 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1698 | case 0xba: /* Grp8 */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1699 | switch (c->modrm_reg & 3) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1700 | case 0: |
| 1701 | goto bt; |
| 1702 | case 1: |
| 1703 | goto bts; |
| 1704 | case 2: |
| 1705 | goto btr; |
| 1706 | case 3: |
| 1707 | goto btc; |
| 1708 | } |
| 1709 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 1710 | case 0xbb: |
| 1711 | btc: /* btc */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1712 | /* only subword offset */ |
| 1713 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1714 | emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 1715 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1716 | case 0xbe ... 0xbf: /* movsx */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1717 | c->dst.bytes = c->op_bytes; |
| 1718 | c->dst.val = (c->d & ByteOp) ? (s8) c->src.val : |
| 1719 | (s16) c->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1720 | break; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 1721 | case 0xc3: /* movnti */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1722 | c->dst.bytes = c->op_bytes; |
| 1723 | c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val : |
| 1724 | (u64) c->src.val; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 1725 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1726 | } |
| 1727 | goto writeback; |
| 1728 | |
| 1729 | twobyte_special_insn: |
| 1730 | /* Disable writeback. */ |
Luca Tettamanti | 02c03a3 | 2007-06-19 22:41:20 +0200 | [diff] [blame] | 1731 | no_wb = 1; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1732 | switch (c->b) { |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 1733 | case 0x06: |
| 1734 | emulate_clts(ctxt->vcpu); |
| 1735 | break; |
Avi Kivity | 651a3e2 | 2007-10-28 16:09:18 +0200 | [diff] [blame] | 1736 | case 0x08: /* invd */ |
| 1737 | break; |
Avi Kivity | 687fdbf | 2007-05-24 11:17:33 +0300 | [diff] [blame] | 1738 | case 0x09: /* wbinvd */ |
| 1739 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1740 | case 0x0d: /* GrpP (prefetch) */ |
| 1741 | case 0x18: /* Grp16 (prefetch/nop) */ |
| 1742 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1743 | case 0x20: /* mov cr, reg */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1744 | if (c->modrm_mod != 3) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1745 | goto cannot_emulate; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1746 | c->regs[c->modrm_rm] = |
| 1747 | realmode_get_cr(ctxt->vcpu, c->modrm_reg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1748 | break; |
| 1749 | case 0x22: /* mov reg, cr */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1750 | if (c->modrm_mod != 3) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1751 | goto cannot_emulate; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1752 | realmode_set_cr(ctxt->vcpu, |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1753 | c->modrm_reg, c->modrm_val, &ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1754 | break; |
Avi Kivity | 35f3f28 | 2007-07-17 14:20:30 +0300 | [diff] [blame] | 1755 | case 0x30: |
| 1756 | /* wrmsr */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1757 | msr_data = (u32)c->regs[VCPU_REGS_RAX] |
| 1758 | | ((u64)c->regs[VCPU_REGS_RDX] << 32); |
| 1759 | rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data); |
Avi Kivity | 35f3f28 | 2007-07-17 14:20:30 +0300 | [diff] [blame] | 1760 | if (rc) { |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1761 | kvm_x86_ops->inject_gp(ctxt->vcpu, 0); |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1762 | c->eip = ctxt->vcpu->rip; |
Avi Kivity | 35f3f28 | 2007-07-17 14:20:30 +0300 | [diff] [blame] | 1763 | } |
| 1764 | rc = X86EMUL_CONTINUE; |
| 1765 | break; |
| 1766 | case 0x32: |
| 1767 | /* rdmsr */ |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1768 | rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data); |
Avi Kivity | 35f3f28 | 2007-07-17 14:20:30 +0300 | [diff] [blame] | 1769 | if (rc) { |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1770 | kvm_x86_ops->inject_gp(ctxt->vcpu, 0); |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1771 | c->eip = ctxt->vcpu->rip; |
Avi Kivity | 35f3f28 | 2007-07-17 14:20:30 +0300 | [diff] [blame] | 1772 | } else { |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1773 | c->regs[VCPU_REGS_RAX] = (u32)msr_data; |
| 1774 | c->regs[VCPU_REGS_RDX] = msr_data >> 32; |
Avi Kivity | 35f3f28 | 2007-07-17 14:20:30 +0300 | [diff] [blame] | 1775 | } |
| 1776 | rc = X86EMUL_CONTINUE; |
| 1777 | break; |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 1778 | case 0x80 ... 0x8f: /* jnz rel, etc*/ { |
| 1779 | long int rel; |
| 1780 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1781 | switch (c->op_bytes) { |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 1782 | case 2: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1783 | rel = insn_fetch(s16, 2, c->eip); |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 1784 | break; |
| 1785 | case 4: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1786 | rel = insn_fetch(s32, 4, c->eip); |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 1787 | break; |
| 1788 | case 8: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1789 | rel = insn_fetch(s64, 8, c->eip); |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 1790 | break; |
| 1791 | default: |
| 1792 | DPRINTF("jnz: Invalid op_bytes\n"); |
| 1793 | goto cannot_emulate; |
| 1794 | } |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1795 | if (test_cc(c->b, ctxt->eflags)) |
Nitin A Kamble | bbe9abb | 2007-09-15 10:23:07 +0300 | [diff] [blame] | 1796 | JMP_REL(rel); |
| 1797 | break; |
| 1798 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1799 | case 0xc7: /* Grp9 (cmpxchg8b) */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame^] | 1800 | rc = emulate_grp9(ctxt, ops, cr2); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1801 | if (rc != 0) |
| 1802 | goto done; |
| 1803 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1804 | } |
| 1805 | goto writeback; |
| 1806 | |
| 1807 | cannot_emulate: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1808 | DPRINTF("Cannot emulate %02x\n", c->b); |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1809 | c->eip = saved_eip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1810 | return -1; |
| 1811 | } |