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