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