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