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