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