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