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