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