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