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