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