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