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