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