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