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); |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 650 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 651 | return rc; |
| 652 | fc->start = linear; |
| 653 | fc->end = linear + size; |
| 654 | } |
| 655 | *dest = fc->data[linear - fc->start]; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 656 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 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 | { |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 663 | int rc; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 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++); |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 671 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 672 | return rc; |
| 673 | } |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 674 | return X86EMUL_CONTINUE; |
Avi Kivity | 6226686 | 2007-11-20 13:15:52 +0200 | [diff] [blame] | 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); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 705 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 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; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 785 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 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; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 898 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 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; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 919 | int rc = X86EMUL_CONTINUE; |
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 | |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 1018 | if (c->d & Group) { |
| 1019 | group = c->d & GroupMask; |
| 1020 | c->modrm = insn_fetch(u8, 1, c->eip); |
| 1021 | --c->eip; |
| 1022 | |
| 1023 | group = (group << 3) + ((c->modrm >> 3) & 7); |
| 1024 | if ((c->d & GroupDual) && (c->modrm >> 6) == 3) |
| 1025 | c->d = group2_table[group]; |
| 1026 | else |
| 1027 | c->d = group_table[group]; |
| 1028 | } |
| 1029 | |
| 1030 | /* Unrecognised? */ |
| 1031 | if (c->d == 0) { |
| 1032 | DPRINTF("Cannot emulate %02x\n", c->b); |
| 1033 | return -1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1034 | } |
| 1035 | |
Avi Kivity | 6e3d5df | 2007-12-06 18:14:14 +0200 | [diff] [blame] | 1036 | if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack)) |
| 1037 | c->op_bytes = 8; |
| 1038 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1039 | /* ModRM and SIB bytes. */ |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1040 | if (c->d & ModRM) |
| 1041 | rc = decode_modrm(ctxt, ops); |
| 1042 | else if (c->d & MemAbs) |
| 1043 | rc = decode_abs(ctxt, ops); |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1044 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1045 | goto done; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1046 | |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 1047 | if (!c->has_seg_override) |
| 1048 | set_seg_override(c, VCPU_SREG_DS); |
Avi Kivity | c7e75a3 | 2007-10-28 16:34:25 +0200 | [diff] [blame] | 1049 | |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 1050 | if (!(!c->twobyte && c->b == 0x8d)) |
| 1051 | c->modrm_ea += seg_override_base(ctxt, c); |
Avi Kivity | c7e75a3 | 2007-10-28 16:34:25 +0200 | [diff] [blame] | 1052 | |
| 1053 | if (c->ad_bytes != 8) |
| 1054 | c->modrm_ea = (u32)c->modrm_ea; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1055 | /* |
| 1056 | * Decode and fetch the source operand: register, memory |
| 1057 | * or immediate. |
| 1058 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1059 | switch (c->d & SrcMask) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1060 | case SrcNone: |
| 1061 | break; |
| 1062 | case SrcReg: |
Avi Kivity | 9f1ef3f | 2007-10-31 11:21:06 +0200 | [diff] [blame] | 1063 | decode_register_operand(&c->src, c, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1064 | break; |
| 1065 | case SrcMem16: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1066 | c->src.bytes = 2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1067 | goto srcmem_common; |
| 1068 | case SrcMem32: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1069 | c->src.bytes = 4; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1070 | goto srcmem_common; |
| 1071 | case SrcMem: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1072 | c->src.bytes = (c->d & ByteOp) ? 1 : |
| 1073 | c->op_bytes; |
Rusty Russell | b85b9ee9 | 2007-09-09 14:12:54 +0300 | [diff] [blame] | 1074 | /* Don't fetch the address for invlpg: it could be unmapped. */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1075 | if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7) |
Rusty Russell | b85b9ee9 | 2007-09-09 14:12:54 +0300 | [diff] [blame] | 1076 | break; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1077 | srcmem_common: |
Aurelien Jarno | 4e62417 | 2007-10-17 19:30:41 +0200 | [diff] [blame] | 1078 | /* |
| 1079 | * For instructions with a ModR/M byte, switch to register |
| 1080 | * access if Mod = 3. |
| 1081 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1082 | if ((c->d & ModRM) && c->modrm_mod == 3) { |
| 1083 | c->src.type = OP_REG; |
Avi Kivity | 66b8550 | 2008-04-14 23:27:07 +0300 | [diff] [blame] | 1084 | c->src.val = c->modrm_val; |
Avi Kivity | 107d6d2 | 2008-05-05 14:58:26 +0300 | [diff] [blame] | 1085 | c->src.ptr = c->modrm_ptr; |
Aurelien Jarno | 4e62417 | 2007-10-17 19:30:41 +0200 | [diff] [blame] | 1086 | break; |
| 1087 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1088 | c->src.type = OP_MEM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1089 | break; |
| 1090 | case SrcImm: |
Avi Kivity | c9eaf20f | 2009-05-18 16:13:45 +0300 | [diff] [blame] | 1091 | case SrcImmU: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1092 | c->src.type = OP_IMM; |
| 1093 | c->src.ptr = (unsigned long *)c->eip; |
| 1094 | c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 1095 | if (c->src.bytes == 8) |
| 1096 | c->src.bytes = 4; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1097 | /* NB. Immediates are sign-extended as necessary. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1098 | switch (c->src.bytes) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1099 | case 1: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1100 | c->src.val = insn_fetch(s8, 1, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1101 | break; |
| 1102 | case 2: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1103 | c->src.val = insn_fetch(s16, 2, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1104 | break; |
| 1105 | case 4: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1106 | c->src.val = insn_fetch(s32, 4, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1107 | break; |
| 1108 | } |
Avi Kivity | c9eaf20f | 2009-05-18 16:13:45 +0300 | [diff] [blame] | 1109 | if ((c->d & SrcMask) == SrcImmU) { |
| 1110 | switch (c->src.bytes) { |
| 1111 | case 1: |
| 1112 | c->src.val &= 0xff; |
| 1113 | break; |
| 1114 | case 2: |
| 1115 | c->src.val &= 0xffff; |
| 1116 | break; |
| 1117 | case 4: |
| 1118 | c->src.val &= 0xffffffff; |
| 1119 | break; |
| 1120 | } |
| 1121 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1122 | break; |
| 1123 | case SrcImmByte: |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 1124 | case SrcImmUByte: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1125 | c->src.type = OP_IMM; |
| 1126 | c->src.ptr = (unsigned long *)c->eip; |
| 1127 | c->src.bytes = 1; |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 1128 | if ((c->d & SrcMask) == SrcImmByte) |
| 1129 | c->src.val = insn_fetch(s8, 1, c->eip); |
| 1130 | else |
| 1131 | c->src.val = insn_fetch(u8, 1, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1132 | break; |
Guillaume Thouvenin | bfcadf8 | 2008-12-04 14:27:38 +0100 | [diff] [blame] | 1133 | case SrcOne: |
| 1134 | c->src.bytes = 1; |
| 1135 | c->src.val = 1; |
| 1136 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1137 | } |
| 1138 | |
Guillaume Thouvenin | 0dc8d10 | 2008-12-04 14:26:42 +0100 | [diff] [blame] | 1139 | /* |
| 1140 | * Decode and fetch the second source operand: register, memory |
| 1141 | * or immediate. |
| 1142 | */ |
| 1143 | switch (c->d & Src2Mask) { |
| 1144 | case Src2None: |
| 1145 | break; |
| 1146 | case Src2CL: |
| 1147 | c->src2.bytes = 1; |
| 1148 | c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8; |
| 1149 | break; |
| 1150 | case Src2ImmByte: |
| 1151 | c->src2.type = OP_IMM; |
| 1152 | c->src2.ptr = (unsigned long *)c->eip; |
| 1153 | c->src2.bytes = 1; |
| 1154 | c->src2.val = insn_fetch(u8, 1, c->eip); |
| 1155 | break; |
Gleb Natapov | a5f868b | 2009-04-12 13:36:14 +0300 | [diff] [blame] | 1156 | case Src2Imm16: |
| 1157 | c->src2.type = OP_IMM; |
| 1158 | c->src2.ptr = (unsigned long *)c->eip; |
| 1159 | c->src2.bytes = 2; |
| 1160 | c->src2.val = insn_fetch(u16, 2, c->eip); |
| 1161 | break; |
Guillaume Thouvenin | 0dc8d10 | 2008-12-04 14:26:42 +0100 | [diff] [blame] | 1162 | case Src2One: |
| 1163 | c->src2.bytes = 1; |
| 1164 | c->src2.val = 1; |
| 1165 | break; |
| 1166 | } |
| 1167 | |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1168 | /* Decode and fetch the destination operand: register or memory. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1169 | switch (c->d & DstMask) { |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1170 | case ImplicitOps: |
| 1171 | /* Special instructions do their own operand decoding. */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1172 | return 0; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1173 | case DstReg: |
Avi Kivity | 9f1ef3f | 2007-10-31 11:21:06 +0200 | [diff] [blame] | 1174 | decode_register_operand(&c->dst, c, |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1175 | c->twobyte && (c->b == 0xb6 || c->b == 0xb7)); |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1176 | break; |
| 1177 | case DstMem: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1178 | if ((c->d & ModRM) && c->modrm_mod == 3) { |
Guillaume Thouvenin | 89c6963 | 2008-05-27 10:22:20 +0200 | [diff] [blame] | 1179 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1180 | c->dst.type = OP_REG; |
Avi Kivity | 66b8550 | 2008-04-14 23:27:07 +0300 | [diff] [blame] | 1181 | c->dst.val = c->dst.orig_val = c->modrm_val; |
Avi Kivity | 107d6d2 | 2008-05-05 14:58:26 +0300 | [diff] [blame] | 1182 | c->dst.ptr = c->modrm_ptr; |
Aurelien Jarno | 4e62417 | 2007-10-17 19:30:41 +0200 | [diff] [blame] | 1183 | break; |
| 1184 | } |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1185 | c->dst.type = OP_MEM; |
| 1186 | break; |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 1187 | case DstAcc: |
| 1188 | c->dst.type = OP_REG; |
| 1189 | c->dst.bytes = c->op_bytes; |
| 1190 | c->dst.ptr = &c->regs[VCPU_REGS_RAX]; |
| 1191 | switch (c->op_bytes) { |
| 1192 | case 1: |
| 1193 | c->dst.val = *(u8 *)c->dst.ptr; |
| 1194 | break; |
| 1195 | case 2: |
| 1196 | c->dst.val = *(u16 *)c->dst.ptr; |
| 1197 | break; |
| 1198 | case 4: |
| 1199 | c->dst.val = *(u32 *)c->dst.ptr; |
| 1200 | break; |
| 1201 | } |
| 1202 | c->dst.orig_val = c->dst.val; |
| 1203 | break; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1204 | } |
| 1205 | |
Avi Kivity | f5b4edc | 2008-06-15 22:09:11 -0700 | [diff] [blame] | 1206 | if (c->rip_relative) |
| 1207 | c->modrm_ea += c->eip; |
| 1208 | |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1209 | done: |
| 1210 | return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0; |
| 1211 | } |
| 1212 | |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1213 | static inline void emulate_push(struct x86_emulate_ctxt *ctxt) |
| 1214 | { |
| 1215 | struct decode_cache *c = &ctxt->decode; |
| 1216 | |
| 1217 | c->dst.type = OP_MEM; |
| 1218 | c->dst.bytes = c->op_bytes; |
| 1219 | c->dst.val = c->src.val; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 1220 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes); |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 1221 | c->dst.ptr = (void *) register_address(c, ss_base(ctxt), |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1222 | c->regs[VCPU_REGS_RSP]); |
| 1223 | } |
| 1224 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1225 | static int emulate_pop(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1226 | struct x86_emulate_ops *ops, |
| 1227 | void *dest, int len) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1228 | { |
| 1229 | struct decode_cache *c = &ctxt->decode; |
| 1230 | int rc; |
| 1231 | |
Avi Kivity | 781d0ed | 2008-11-27 18:00:28 +0200 | [diff] [blame] | 1232 | rc = ops->read_emulated(register_address(c, ss_base(ctxt), |
| 1233 | c->regs[VCPU_REGS_RSP]), |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1234 | dest, len, ctxt->vcpu); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1235 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1236 | return rc; |
| 1237 | |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1238 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], len); |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1239 | return rc; |
| 1240 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1241 | |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1242 | static int emulate_popf(struct x86_emulate_ctxt *ctxt, |
| 1243 | struct x86_emulate_ops *ops, |
| 1244 | void *dest, int len) |
| 1245 | { |
| 1246 | int rc; |
| 1247 | unsigned long val, change_mask; |
| 1248 | int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
| 1249 | int cpl = kvm_x86_ops->get_cpl(ctxt->vcpu); |
| 1250 | |
| 1251 | rc = emulate_pop(ctxt, ops, &val, len); |
| 1252 | if (rc != X86EMUL_CONTINUE) |
| 1253 | return rc; |
| 1254 | |
| 1255 | change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF |
| 1256 | | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID; |
| 1257 | |
| 1258 | switch(ctxt->mode) { |
| 1259 | case X86EMUL_MODE_PROT64: |
| 1260 | case X86EMUL_MODE_PROT32: |
| 1261 | case X86EMUL_MODE_PROT16: |
| 1262 | if (cpl == 0) |
| 1263 | change_mask |= EFLG_IOPL; |
| 1264 | if (cpl <= iopl) |
| 1265 | change_mask |= EFLG_IF; |
| 1266 | break; |
| 1267 | case X86EMUL_MODE_VM86: |
| 1268 | if (iopl < 3) { |
| 1269 | kvm_inject_gp(ctxt->vcpu, 0); |
| 1270 | return X86EMUL_PROPAGATE_FAULT; |
| 1271 | } |
| 1272 | change_mask |= EFLG_IF; |
| 1273 | break; |
| 1274 | default: /* real mode */ |
| 1275 | change_mask |= (EFLG_IOPL | EFLG_IF); |
| 1276 | break; |
| 1277 | } |
| 1278 | |
| 1279 | *(unsigned long *)dest = |
| 1280 | (ctxt->eflags & ~change_mask) | (val & change_mask); |
| 1281 | |
| 1282 | return rc; |
| 1283 | } |
| 1284 | |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1285 | static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt, int seg) |
| 1286 | { |
| 1287 | struct decode_cache *c = &ctxt->decode; |
| 1288 | struct kvm_segment segment; |
| 1289 | |
| 1290 | kvm_x86_ops->get_segment(ctxt->vcpu, &segment, seg); |
| 1291 | |
| 1292 | c->src.val = segment.selector; |
| 1293 | emulate_push(ctxt); |
| 1294 | } |
| 1295 | |
| 1296 | static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt, |
| 1297 | struct x86_emulate_ops *ops, int seg) |
| 1298 | { |
| 1299 | struct decode_cache *c = &ctxt->decode; |
| 1300 | unsigned long selector; |
| 1301 | int rc; |
| 1302 | |
| 1303 | rc = emulate_pop(ctxt, ops, &selector, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 1304 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1305 | return rc; |
| 1306 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 1307 | rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)selector, seg); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1308 | return rc; |
| 1309 | } |
| 1310 | |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1311 | static void emulate_pusha(struct x86_emulate_ctxt *ctxt) |
| 1312 | { |
| 1313 | struct decode_cache *c = &ctxt->decode; |
| 1314 | unsigned long old_esp = c->regs[VCPU_REGS_RSP]; |
| 1315 | int reg = VCPU_REGS_RAX; |
| 1316 | |
| 1317 | while (reg <= VCPU_REGS_RDI) { |
| 1318 | (reg == VCPU_REGS_RSP) ? |
| 1319 | (c->src.val = old_esp) : (c->src.val = c->regs[reg]); |
| 1320 | |
| 1321 | emulate_push(ctxt); |
| 1322 | ++reg; |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | static int emulate_popa(struct x86_emulate_ctxt *ctxt, |
| 1327 | struct x86_emulate_ops *ops) |
| 1328 | { |
| 1329 | struct decode_cache *c = &ctxt->decode; |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 1330 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1331 | int reg = VCPU_REGS_RDI; |
| 1332 | |
| 1333 | while (reg >= VCPU_REGS_RAX) { |
| 1334 | if (reg == VCPU_REGS_RSP) { |
| 1335 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], |
| 1336 | c->op_bytes); |
| 1337 | --reg; |
| 1338 | } |
| 1339 | |
| 1340 | rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 1341 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1342 | break; |
| 1343 | --reg; |
| 1344 | } |
| 1345 | return rc; |
| 1346 | } |
| 1347 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1348 | static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt, |
| 1349 | struct x86_emulate_ops *ops) |
| 1350 | { |
| 1351 | struct decode_cache *c = &ctxt->decode; |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1352 | |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 1353 | return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1354 | } |
| 1355 | |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1356 | static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1357 | { |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1358 | struct decode_cache *c = &ctxt->decode; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1359 | switch (c->modrm_reg) { |
| 1360 | case 0: /* rol */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1361 | emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1362 | break; |
| 1363 | case 1: /* ror */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1364 | emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1365 | break; |
| 1366 | case 2: /* rcl */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1367 | emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1368 | break; |
| 1369 | case 3: /* rcr */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1370 | emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1371 | break; |
| 1372 | case 4: /* sal/shl */ |
| 1373 | case 6: /* sal/shl */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1374 | emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1375 | break; |
| 1376 | case 5: /* shr */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1377 | emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1378 | break; |
| 1379 | case 7: /* sar */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1380 | emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1381 | break; |
| 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt, |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1386 | struct x86_emulate_ops *ops) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1387 | { |
| 1388 | struct decode_cache *c = &ctxt->decode; |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 1389 | int rc = X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1390 | |
| 1391 | switch (c->modrm_reg) { |
| 1392 | case 0 ... 1: /* test */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1393 | emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1394 | break; |
| 1395 | case 2: /* not */ |
| 1396 | c->dst.val = ~c->dst.val; |
| 1397 | break; |
| 1398 | case 3: /* neg */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1399 | emulate_1op("neg", c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1400 | break; |
| 1401 | default: |
| 1402 | DPRINTF("Cannot emulate %02x\n", c->b); |
| 1403 | rc = X86EMUL_UNHANDLEABLE; |
| 1404 | break; |
| 1405 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1406 | return rc; |
| 1407 | } |
| 1408 | |
| 1409 | static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt, |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 1410 | struct x86_emulate_ops *ops) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1411 | { |
| 1412 | struct decode_cache *c = &ctxt->decode; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1413 | |
| 1414 | switch (c->modrm_reg) { |
| 1415 | case 0: /* inc */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1416 | emulate_1op("inc", c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1417 | break; |
| 1418 | case 1: /* dec */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1419 | emulate_1op("dec", c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1420 | break; |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 1421 | case 2: /* call near abs */ { |
| 1422 | long int old_eip; |
| 1423 | old_eip = c->eip; |
| 1424 | c->eip = c->src.val; |
| 1425 | c->src.val = old_eip; |
| 1426 | emulate_push(ctxt); |
| 1427 | break; |
| 1428 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1429 | case 4: /* jmp abs */ |
Avi Kivity | fd60754 | 2008-01-18 13:12:26 +0200 | [diff] [blame] | 1430 | c->eip = c->src.val; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1431 | break; |
| 1432 | case 6: /* push */ |
Avi Kivity | fd60754 | 2008-01-18 13:12:26 +0200 | [diff] [blame] | 1433 | emulate_push(ctxt); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1434 | break; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1435 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 1436 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt, |
| 1440 | struct x86_emulate_ops *ops, |
Sheng Yang | e8d8d7f | 2007-11-16 16:29:15 +0800 | [diff] [blame] | 1441 | unsigned long memop) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1442 | { |
| 1443 | struct decode_cache *c = &ctxt->decode; |
| 1444 | u64 old, new; |
| 1445 | int rc; |
| 1446 | |
Sheng Yang | e8d8d7f | 2007-11-16 16:29:15 +0800 | [diff] [blame] | 1447 | rc = ops->read_emulated(memop, &old, 8, ctxt->vcpu); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1448 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1449 | return rc; |
| 1450 | |
| 1451 | if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) || |
| 1452 | ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) { |
| 1453 | |
| 1454 | c->regs[VCPU_REGS_RAX] = (u32) (old >> 0); |
| 1455 | c->regs[VCPU_REGS_RDX] = (u32) (old >> 32); |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1456 | ctxt->eflags &= ~EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1457 | |
| 1458 | } else { |
| 1459 | new = ((u64)c->regs[VCPU_REGS_RCX] << 32) | |
| 1460 | (u32) c->regs[VCPU_REGS_RBX]; |
| 1461 | |
Sheng Yang | e8d8d7f | 2007-11-16 16:29:15 +0800 | [diff] [blame] | 1462 | rc = ops->cmpxchg_emulated(memop, &old, &new, 8, ctxt->vcpu); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1463 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1464 | return rc; |
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 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 1467 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1468 | } |
| 1469 | |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1470 | static int emulate_ret_far(struct x86_emulate_ctxt *ctxt, |
| 1471 | struct x86_emulate_ops *ops) |
| 1472 | { |
| 1473 | struct decode_cache *c = &ctxt->decode; |
| 1474 | int rc; |
| 1475 | unsigned long cs; |
| 1476 | |
| 1477 | rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 1478 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1479 | return rc; |
| 1480 | if (c->op_bytes == 4) |
| 1481 | c->eip = (u32)c->eip; |
| 1482 | rc = emulate_pop(ctxt, ops, &cs, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 1483 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1484 | return rc; |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 1485 | rc = kvm_load_segment_descriptor(ctxt->vcpu, (u16)cs, VCPU_SREG_CS); |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1486 | return rc; |
| 1487 | } |
| 1488 | |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1489 | static inline int writeback(struct x86_emulate_ctxt *ctxt, |
| 1490 | struct x86_emulate_ops *ops) |
| 1491 | { |
| 1492 | int rc; |
| 1493 | struct decode_cache *c = &ctxt->decode; |
| 1494 | |
| 1495 | switch (c->dst.type) { |
| 1496 | case OP_REG: |
| 1497 | /* The 4-byte case *is* correct: |
| 1498 | * in 64-bit mode we zero-extend. |
| 1499 | */ |
| 1500 | switch (c->dst.bytes) { |
| 1501 | case 1: |
| 1502 | *(u8 *)c->dst.ptr = (u8)c->dst.val; |
| 1503 | break; |
| 1504 | case 2: |
| 1505 | *(u16 *)c->dst.ptr = (u16)c->dst.val; |
| 1506 | break; |
| 1507 | case 4: |
| 1508 | *c->dst.ptr = (u32)c->dst.val; |
| 1509 | break; /* 64b: zero-ext */ |
| 1510 | case 8: |
| 1511 | *c->dst.ptr = c->dst.val; |
| 1512 | break; |
| 1513 | } |
| 1514 | break; |
| 1515 | case OP_MEM: |
| 1516 | if (c->lock_prefix) |
| 1517 | rc = ops->cmpxchg_emulated( |
| 1518 | (unsigned long)c->dst.ptr, |
| 1519 | &c->dst.orig_val, |
| 1520 | &c->dst.val, |
| 1521 | c->dst.bytes, |
| 1522 | ctxt->vcpu); |
| 1523 | else |
| 1524 | rc = ops->write_emulated( |
| 1525 | (unsigned long)c->dst.ptr, |
| 1526 | &c->dst.val, |
| 1527 | c->dst.bytes, |
| 1528 | ctxt->vcpu); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1529 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1530 | return rc; |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 1531 | break; |
| 1532 | case OP_NONE: |
| 1533 | /* no writeback */ |
| 1534 | break; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1535 | default: |
| 1536 | break; |
| 1537 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 1538 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1539 | } |
| 1540 | |
Jaswinder Singh Rajput | a3f9d39 | 2009-06-18 16:53:25 +0530 | [diff] [blame] | 1541 | static void toggle_interruptibility(struct x86_emulate_ctxt *ctxt, u32 mask) |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 1542 | { |
| 1543 | u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(ctxt->vcpu, mask); |
| 1544 | /* |
| 1545 | * an sti; sti; sequence only disable interrupts for the first |
| 1546 | * instruction. So, if the last instruction, be it emulated or |
| 1547 | * not, left the system with the INT_STI flag enabled, it |
| 1548 | * means that the last instruction is an sti. We should not |
| 1549 | * leave the flag on in this case. The same goes for mov ss |
| 1550 | */ |
| 1551 | if (!(int_shadow & mask)) |
| 1552 | ctxt->interruptibility = mask; |
| 1553 | } |
| 1554 | |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1555 | static inline void |
| 1556 | setup_syscalls_segments(struct x86_emulate_ctxt *ctxt, |
| 1557 | struct kvm_segment *cs, struct kvm_segment *ss) |
| 1558 | { |
| 1559 | memset(cs, 0, sizeof(struct kvm_segment)); |
| 1560 | kvm_x86_ops->get_segment(ctxt->vcpu, cs, VCPU_SREG_CS); |
| 1561 | memset(ss, 0, sizeof(struct kvm_segment)); |
| 1562 | |
| 1563 | cs->l = 0; /* will be adjusted later */ |
| 1564 | cs->base = 0; /* flat segment */ |
| 1565 | cs->g = 1; /* 4kb granularity */ |
| 1566 | cs->limit = 0xffffffff; /* 4GB limit */ |
| 1567 | cs->type = 0x0b; /* Read, Execute, Accessed */ |
| 1568 | cs->s = 1; |
| 1569 | cs->dpl = 0; /* will be adjusted later */ |
| 1570 | cs->present = 1; |
| 1571 | cs->db = 1; |
| 1572 | |
| 1573 | ss->unusable = 0; |
| 1574 | ss->base = 0; /* flat segment */ |
| 1575 | ss->limit = 0xffffffff; /* 4GB limit */ |
| 1576 | ss->g = 1; /* 4kb granularity */ |
| 1577 | ss->s = 1; |
| 1578 | ss->type = 0x03; /* Read/Write, Accessed */ |
| 1579 | ss->db = 1; /* 32bit stack segment */ |
| 1580 | ss->dpl = 0; |
| 1581 | ss->present = 1; |
| 1582 | } |
| 1583 | |
| 1584 | static int |
| 1585 | emulate_syscall(struct x86_emulate_ctxt *ctxt) |
| 1586 | { |
| 1587 | struct decode_cache *c = &ctxt->decode; |
| 1588 | struct kvm_segment cs, ss; |
| 1589 | u64 msr_data; |
| 1590 | |
| 1591 | /* syscall is not available in real mode */ |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 1592 | if (ctxt->mode == X86EMUL_MODE_REAL || ctxt->mode == X86EMUL_MODE_VM86) |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 1593 | return X86EMUL_UNHANDLEABLE; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1594 | |
| 1595 | setup_syscalls_segments(ctxt, &cs, &ss); |
| 1596 | |
| 1597 | kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data); |
| 1598 | msr_data >>= 32; |
| 1599 | cs.selector = (u16)(msr_data & 0xfffc); |
| 1600 | ss.selector = (u16)(msr_data + 8); |
| 1601 | |
| 1602 | if (is_long_mode(ctxt->vcpu)) { |
| 1603 | cs.db = 0; |
| 1604 | cs.l = 1; |
| 1605 | } |
| 1606 | kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS); |
| 1607 | kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS); |
| 1608 | |
| 1609 | c->regs[VCPU_REGS_RCX] = c->eip; |
| 1610 | if (is_long_mode(ctxt->vcpu)) { |
| 1611 | #ifdef CONFIG_X86_64 |
| 1612 | c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF; |
| 1613 | |
| 1614 | kvm_x86_ops->get_msr(ctxt->vcpu, |
| 1615 | ctxt->mode == X86EMUL_MODE_PROT64 ? |
| 1616 | MSR_LSTAR : MSR_CSTAR, &msr_data); |
| 1617 | c->eip = msr_data; |
| 1618 | |
| 1619 | kvm_x86_ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data); |
| 1620 | ctxt->eflags &= ~(msr_data | EFLG_RF); |
| 1621 | #endif |
| 1622 | } else { |
| 1623 | /* legacy mode */ |
| 1624 | kvm_x86_ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data); |
| 1625 | c->eip = (u32)msr_data; |
| 1626 | |
| 1627 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
| 1628 | } |
| 1629 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 1630 | return X86EMUL_CONTINUE; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1631 | } |
| 1632 | |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1633 | static int |
| 1634 | emulate_sysenter(struct x86_emulate_ctxt *ctxt) |
| 1635 | { |
| 1636 | struct decode_cache *c = &ctxt->decode; |
| 1637 | struct kvm_segment cs, ss; |
| 1638 | u64 msr_data; |
| 1639 | |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 1640 | /* inject #GP if in real mode */ |
| 1641 | if (ctxt->mode == X86EMUL_MODE_REAL) { |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1642 | kvm_inject_gp(ctxt->vcpu, 0); |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 1643 | return X86EMUL_UNHANDLEABLE; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | /* XXX sysenter/sysexit have not been tested in 64bit mode. |
| 1647 | * Therefore, we inject an #UD. |
| 1648 | */ |
| 1649 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 1650 | return X86EMUL_UNHANDLEABLE; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1651 | |
| 1652 | setup_syscalls_segments(ctxt, &cs, &ss); |
| 1653 | |
| 1654 | kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data); |
| 1655 | switch (ctxt->mode) { |
| 1656 | case X86EMUL_MODE_PROT32: |
| 1657 | if ((msr_data & 0xfffc) == 0x0) { |
| 1658 | kvm_inject_gp(ctxt->vcpu, 0); |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 1659 | return X86EMUL_PROPAGATE_FAULT; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1660 | } |
| 1661 | break; |
| 1662 | case X86EMUL_MODE_PROT64: |
| 1663 | if (msr_data == 0x0) { |
| 1664 | kvm_inject_gp(ctxt->vcpu, 0); |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 1665 | return X86EMUL_PROPAGATE_FAULT; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1666 | } |
| 1667 | break; |
| 1668 | } |
| 1669 | |
| 1670 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
| 1671 | cs.selector = (u16)msr_data; |
| 1672 | cs.selector &= ~SELECTOR_RPL_MASK; |
| 1673 | ss.selector = cs.selector + 8; |
| 1674 | ss.selector &= ~SELECTOR_RPL_MASK; |
| 1675 | if (ctxt->mode == X86EMUL_MODE_PROT64 |
| 1676 | || is_long_mode(ctxt->vcpu)) { |
| 1677 | cs.db = 0; |
| 1678 | cs.l = 1; |
| 1679 | } |
| 1680 | |
| 1681 | kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS); |
| 1682 | kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS); |
| 1683 | |
| 1684 | kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data); |
| 1685 | c->eip = msr_data; |
| 1686 | |
| 1687 | kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data); |
| 1688 | c->regs[VCPU_REGS_RSP] = msr_data; |
| 1689 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 1690 | return X86EMUL_CONTINUE; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 1691 | } |
| 1692 | |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1693 | static int |
| 1694 | emulate_sysexit(struct x86_emulate_ctxt *ctxt) |
| 1695 | { |
| 1696 | struct decode_cache *c = &ctxt->decode; |
| 1697 | struct kvm_segment cs, ss; |
| 1698 | u64 msr_data; |
| 1699 | int usermode; |
| 1700 | |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 1701 | /* inject #GP if in real mode or Virtual 8086 mode */ |
| 1702 | if (ctxt->mode == X86EMUL_MODE_REAL || |
| 1703 | ctxt->mode == X86EMUL_MODE_VM86) { |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1704 | kvm_inject_gp(ctxt->vcpu, 0); |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 1705 | return X86EMUL_UNHANDLEABLE; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1706 | } |
| 1707 | |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1708 | setup_syscalls_segments(ctxt, &cs, &ss); |
| 1709 | |
| 1710 | if ((c->rex_prefix & 0x8) != 0x0) |
| 1711 | usermode = X86EMUL_MODE_PROT64; |
| 1712 | else |
| 1713 | usermode = X86EMUL_MODE_PROT32; |
| 1714 | |
| 1715 | cs.dpl = 3; |
| 1716 | ss.dpl = 3; |
| 1717 | kvm_x86_ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data); |
| 1718 | switch (usermode) { |
| 1719 | case X86EMUL_MODE_PROT32: |
| 1720 | cs.selector = (u16)(msr_data + 16); |
| 1721 | if ((msr_data & 0xfffc) == 0x0) { |
| 1722 | kvm_inject_gp(ctxt->vcpu, 0); |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 1723 | return X86EMUL_PROPAGATE_FAULT; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1724 | } |
| 1725 | ss.selector = (u16)(msr_data + 24); |
| 1726 | break; |
| 1727 | case X86EMUL_MODE_PROT64: |
| 1728 | cs.selector = (u16)(msr_data + 32); |
| 1729 | if (msr_data == 0x0) { |
| 1730 | kvm_inject_gp(ctxt->vcpu, 0); |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 1731 | return X86EMUL_PROPAGATE_FAULT; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1732 | } |
| 1733 | ss.selector = cs.selector + 8; |
| 1734 | cs.db = 0; |
| 1735 | cs.l = 1; |
| 1736 | break; |
| 1737 | } |
| 1738 | cs.selector |= SELECTOR_RPL_MASK; |
| 1739 | ss.selector |= SELECTOR_RPL_MASK; |
| 1740 | |
| 1741 | kvm_x86_ops->set_segment(ctxt->vcpu, &cs, VCPU_SREG_CS); |
| 1742 | kvm_x86_ops->set_segment(ctxt->vcpu, &ss, VCPU_SREG_SS); |
| 1743 | |
| 1744 | c->eip = ctxt->vcpu->arch.regs[VCPU_REGS_RDX]; |
| 1745 | c->regs[VCPU_REGS_RSP] = ctxt->vcpu->arch.regs[VCPU_REGS_RCX]; |
| 1746 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 1747 | return X86EMUL_CONTINUE; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 1748 | } |
| 1749 | |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 1750 | static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt) |
| 1751 | { |
| 1752 | int iopl; |
| 1753 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 1754 | return false; |
| 1755 | if (ctxt->mode == X86EMUL_MODE_VM86) |
| 1756 | return true; |
| 1757 | iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
| 1758 | return kvm_x86_ops->get_cpl(ctxt->vcpu) > iopl; |
| 1759 | } |
| 1760 | |
| 1761 | static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt, |
| 1762 | struct x86_emulate_ops *ops, |
| 1763 | u16 port, u16 len) |
| 1764 | { |
| 1765 | struct kvm_segment tr_seg; |
| 1766 | int r; |
| 1767 | u16 io_bitmap_ptr; |
| 1768 | u8 perm, bit_idx = port & 0x7; |
| 1769 | unsigned mask = (1 << len) - 1; |
| 1770 | |
| 1771 | kvm_get_segment(ctxt->vcpu, &tr_seg, VCPU_SREG_TR); |
| 1772 | if (tr_seg.unusable) |
| 1773 | return false; |
| 1774 | if (tr_seg.limit < 103) |
| 1775 | return false; |
| 1776 | r = ops->read_std(tr_seg.base + 102, &io_bitmap_ptr, 2, ctxt->vcpu, |
| 1777 | NULL); |
| 1778 | if (r != X86EMUL_CONTINUE) |
| 1779 | return false; |
| 1780 | if (io_bitmap_ptr + port/8 > tr_seg.limit) |
| 1781 | return false; |
| 1782 | r = ops->read_std(tr_seg.base + io_bitmap_ptr + port/8, &perm, 1, |
| 1783 | ctxt->vcpu, NULL); |
| 1784 | if (r != X86EMUL_CONTINUE) |
| 1785 | return false; |
| 1786 | if ((perm >> bit_idx) & mask) |
| 1787 | return false; |
| 1788 | return true; |
| 1789 | } |
| 1790 | |
| 1791 | static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt, |
| 1792 | struct x86_emulate_ops *ops, |
| 1793 | u16 port, u16 len) |
| 1794 | { |
| 1795 | if (emulator_bad_iopl(ctxt)) |
| 1796 | if (!emulator_io_port_access_allowed(ctxt, ops, port, len)) |
| 1797 | return false; |
| 1798 | return true; |
| 1799 | } |
| 1800 | |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1801 | int |
Laurent Vivier | 1be3aa4 | 2007-09-18 11:27:27 +0200 | [diff] [blame] | 1802 | 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] | 1803 | { |
Sheng Yang | e8d8d7f | 2007-11-16 16:29:15 +0800 | [diff] [blame] | 1804 | unsigned long memop = 0; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1805 | u64 msr_data; |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1806 | unsigned long saved_eip = 0; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1807 | struct decode_cache *c = &ctxt->decode; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 1808 | unsigned int port; |
| 1809 | int io_dir_in; |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 1810 | int rc = X86EMUL_CONTINUE; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1811 | |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 1812 | ctxt->interruptibility = 0; |
| 1813 | |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1814 | /* Shadow copy of register state. Committed on successful emulation. |
| 1815 | * NOTE: we can copy them from vcpu as x86_decode_insn() doesn't |
| 1816 | * modify them. |
| 1817 | */ |
| 1818 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1819 | memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs); |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1820 | saved_eip = c->eip; |
| 1821 | |
Gleb Natapov | 1161624 | 2010-02-11 14:43:14 +0200 | [diff] [blame] | 1822 | if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) { |
| 1823 | kvm_queue_exception(ctxt->vcpu, UD_VECTOR); |
| 1824 | goto done; |
| 1825 | } |
| 1826 | |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 1827 | /* LOCK prefix is allowed only with some instructions */ |
| 1828 | if (c->lock_prefix && !(c->d & Lock)) { |
| 1829 | kvm_queue_exception(ctxt->vcpu, UD_VECTOR); |
| 1830 | goto done; |
| 1831 | } |
| 1832 | |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 1833 | /* Privileged instruction can be executed only in CPL=0 */ |
| 1834 | if ((c->d & Priv) && kvm_x86_ops->get_cpl(ctxt->vcpu)) { |
| 1835 | kvm_inject_gp(ctxt->vcpu, 0); |
| 1836 | goto done; |
| 1837 | } |
| 1838 | |
Avi Kivity | c7e75a3 | 2007-10-28 16:34:25 +0200 | [diff] [blame] | 1839 | if (((c->d & ModRM) && (c->modrm_mod != 3)) || (c->d & MemAbs)) |
Sheng Yang | e8d8d7f | 2007-11-16 16:29:15 +0800 | [diff] [blame] | 1840 | memop = c->modrm_ea; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1841 | |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 1842 | if (c->rep_prefix && (c->d & String)) { |
| 1843 | /* All REP prefixes have the same first termination condition */ |
| 1844 | if (c->regs[VCPU_REGS_RCX] == 0) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1845 | kvm_rip_write(ctxt->vcpu, c->eip); |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 1846 | goto done; |
| 1847 | } |
| 1848 | /* The second termination condition only applies for REPE |
| 1849 | * and REPNE. Test if the repeat string operation prefix is |
| 1850 | * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the |
| 1851 | * corresponding termination condition according to: |
| 1852 | * - if REPE/REPZ and ZF = 0 then done |
| 1853 | * - if REPNE/REPNZ and ZF = 1 then done |
| 1854 | */ |
| 1855 | if ((c->b == 0xa6) || (c->b == 0xa7) || |
| 1856 | (c->b == 0xae) || (c->b == 0xaf)) { |
| 1857 | if ((c->rep_prefix == REPE_PREFIX) && |
| 1858 | ((ctxt->eflags & EFLG_ZF) == 0)) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1859 | kvm_rip_write(ctxt->vcpu, c->eip); |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 1860 | goto done; |
| 1861 | } |
| 1862 | if ((c->rep_prefix == REPNE_PREFIX) && |
| 1863 | ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1864 | kvm_rip_write(ctxt->vcpu, c->eip); |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 1865 | goto done; |
| 1866 | } |
| 1867 | } |
| 1868 | c->regs[VCPU_REGS_RCX]--; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1869 | c->eip = kvm_rip_read(ctxt->vcpu); |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 1870 | } |
| 1871 | |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1872 | if (c->src.type == OP_MEM) { |
Sheng Yang | e8d8d7f | 2007-11-16 16:29:15 +0800 | [diff] [blame] | 1873 | c->src.ptr = (unsigned long *)memop; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1874 | c->src.val = 0; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1875 | rc = ops->read_emulated((unsigned long)c->src.ptr, |
| 1876 | &c->src.val, |
| 1877 | c->src.bytes, |
| 1878 | ctxt->vcpu); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1879 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1880 | goto done; |
| 1881 | c->src.orig_val = c->src.val; |
| 1882 | } |
| 1883 | |
| 1884 | if ((c->d & DstMask) == ImplicitOps) |
| 1885 | goto special_insn; |
| 1886 | |
| 1887 | |
| 1888 | if (c->dst.type == OP_MEM) { |
Sheng Yang | e8d8d7f | 2007-11-16 16:29:15 +0800 | [diff] [blame] | 1889 | c->dst.ptr = (unsigned long *)memop; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1890 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 1891 | c->dst.val = 0; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1892 | if (c->d & BitOp) { |
| 1893 | unsigned long mask = ~(c->dst.bytes * 8 - 1); |
Avi Kivity | df513e2 | 2007-03-28 20:04:16 +0200 | [diff] [blame] | 1894 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1895 | c->dst.ptr = (void *)c->dst.ptr + |
| 1896 | (c->src.val & mask) / 8; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1897 | } |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1898 | if (!(c->d & Mov)) { |
| 1899 | /* optimisation - avoid slow emulated read */ |
| 1900 | rc = ops->read_emulated((unsigned long)c->dst.ptr, |
| 1901 | &c->dst.val, |
| 1902 | c->dst.bytes, |
| 1903 | ctxt->vcpu); |
| 1904 | if (rc != X86EMUL_CONTINUE) |
| 1905 | goto done; |
| 1906 | } |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1907 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1908 | c->dst.orig_val = c->dst.val; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1909 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 1910 | special_insn: |
| 1911 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1912 | if (c->twobyte) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1913 | goto twobyte_insn; |
| 1914 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1915 | switch (c->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1916 | case 0x00 ... 0x05: |
| 1917 | add: /* add */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1918 | emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1919 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1920 | case 0x06: /* push es */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1921 | emulate_push_sreg(ctxt, VCPU_SREG_ES); |
| 1922 | break; |
| 1923 | case 0x07: /* pop es */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1924 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 1925 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1926 | goto done; |
| 1927 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1928 | case 0x08 ... 0x0d: |
| 1929 | or: /* or */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1930 | emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1931 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1932 | case 0x0e: /* push cs */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1933 | emulate_push_sreg(ctxt, VCPU_SREG_CS); |
| 1934 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1935 | case 0x10 ... 0x15: |
| 1936 | adc: /* adc */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1937 | emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1938 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1939 | case 0x16: /* push ss */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1940 | emulate_push_sreg(ctxt, VCPU_SREG_SS); |
| 1941 | break; |
| 1942 | case 0x17: /* pop ss */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1943 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 1944 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1945 | goto done; |
| 1946 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1947 | case 0x18 ... 0x1d: |
| 1948 | sbb: /* sbb */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1949 | emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1950 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1951 | case 0x1e: /* push ds */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1952 | emulate_push_sreg(ctxt, VCPU_SREG_DS); |
| 1953 | break; |
| 1954 | case 0x1f: /* pop ds */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1955 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 1956 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1957 | goto done; |
| 1958 | break; |
Guillaume Thouvenin | aa3a816 | 2008-09-12 13:52:18 +0200 | [diff] [blame] | 1959 | case 0x20 ... 0x25: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1960 | and: /* and */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1961 | emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1962 | break; |
| 1963 | case 0x28 ... 0x2d: |
| 1964 | sub: /* sub */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1965 | emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1966 | break; |
| 1967 | case 0x30 ... 0x35: |
| 1968 | xor: /* xor */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1969 | emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1970 | break; |
| 1971 | case 0x38 ... 0x3d: |
| 1972 | cmp: /* cmp */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1973 | emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1974 | break; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1975 | case 0x40 ... 0x47: /* inc r16/r32 */ |
| 1976 | emulate_1op("inc", c->dst, ctxt->eflags); |
| 1977 | break; |
| 1978 | case 0x48 ... 0x4f: /* dec r16/r32 */ |
| 1979 | emulate_1op("dec", c->dst, ctxt->eflags); |
| 1980 | break; |
| 1981 | case 0x50 ... 0x57: /* push reg */ |
Guillaume Thouvenin | 2786b01 | 2008-09-22 16:08:06 +0200 | [diff] [blame] | 1982 | emulate_push(ctxt); |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1983 | break; |
| 1984 | case 0x58 ... 0x5f: /* pop reg */ |
| 1985 | pop_instruction: |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1986 | rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 1987 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1988 | goto done; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1989 | break; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1990 | case 0x60: /* pusha */ |
| 1991 | emulate_pusha(ctxt); |
| 1992 | break; |
| 1993 | case 0x61: /* popa */ |
| 1994 | rc = emulate_popa(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 1995 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1996 | goto done; |
| 1997 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1998 | case 0x63: /* movsxd */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1999 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2000 | goto cannot_emulate; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2001 | c->dst.val = (s32) c->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2002 | break; |
Avi Kivity | 91ed7a0 | 2008-05-29 14:38:38 +0300 | [diff] [blame] | 2003 | case 0x68: /* push imm */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2004 | case 0x6a: /* push imm8 */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2005 | emulate_push(ctxt); |
| 2006 | break; |
| 2007 | case 0x6c: /* insb */ |
| 2008 | case 0x6d: /* insw/insd */ |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2009 | if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX], |
| 2010 | (c->d & ByteOp) ? 1 : c->op_bytes)) { |
| 2011 | kvm_inject_gp(ctxt->vcpu, 0); |
| 2012 | goto done; |
| 2013 | } |
| 2014 | if (kvm_emulate_pio_string(ctxt->vcpu, |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2015 | 1, |
| 2016 | (c->d & ByteOp) ? 1 : c->op_bytes, |
| 2017 | c->rep_prefix ? |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 2018 | address_mask(c, c->regs[VCPU_REGS_RCX]) : 1, |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2019 | (ctxt->eflags & EFLG_DF), |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 2020 | register_address(c, es_base(ctxt), |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2021 | c->regs[VCPU_REGS_RDI]), |
| 2022 | c->rep_prefix, |
| 2023 | c->regs[VCPU_REGS_RDX]) == 0) { |
| 2024 | c->eip = saved_eip; |
| 2025 | return -1; |
| 2026 | } |
| 2027 | return 0; |
| 2028 | case 0x6e: /* outsb */ |
| 2029 | case 0x6f: /* outsw/outsd */ |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2030 | if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX], |
| 2031 | (c->d & ByteOp) ? 1 : c->op_bytes)) { |
| 2032 | kvm_inject_gp(ctxt->vcpu, 0); |
| 2033 | goto done; |
| 2034 | } |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2035 | if (kvm_emulate_pio_string(ctxt->vcpu, |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2036 | 0, |
| 2037 | (c->d & ByteOp) ? 1 : c->op_bytes, |
| 2038 | c->rep_prefix ? |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 2039 | address_mask(c, c->regs[VCPU_REGS_RCX]) : 1, |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2040 | (ctxt->eflags & EFLG_DF), |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 2041 | register_address(c, |
| 2042 | seg_override_base(ctxt, c), |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2043 | c->regs[VCPU_REGS_RSI]), |
| 2044 | c->rep_prefix, |
| 2045 | c->regs[VCPU_REGS_RDX]) == 0) { |
| 2046 | c->eip = saved_eip; |
| 2047 | return -1; |
| 2048 | } |
| 2049 | return 0; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 2050 | case 0x70 ... 0x7f: /* jcc (short) */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2051 | if (test_cc(c->b, ctxt->eflags)) |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 2052 | jmp_rel(c, c->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2053 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2054 | case 0x80 ... 0x83: /* Grp1 */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2055 | switch (c->modrm_reg) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2056 | case 0: |
| 2057 | goto add; |
| 2058 | case 1: |
| 2059 | goto or; |
| 2060 | case 2: |
| 2061 | goto adc; |
| 2062 | case 3: |
| 2063 | goto sbb; |
| 2064 | case 4: |
| 2065 | goto and; |
| 2066 | case 5: |
| 2067 | goto sub; |
| 2068 | case 6: |
| 2069 | goto xor; |
| 2070 | case 7: |
| 2071 | goto cmp; |
| 2072 | } |
| 2073 | break; |
| 2074 | case 0x84 ... 0x85: |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2075 | emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2076 | break; |
| 2077 | case 0x86 ... 0x87: /* xchg */ |
Mohammed Gamal | b13354f | 2008-06-15 19:37:38 +0300 | [diff] [blame] | 2078 | xchg: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2079 | /* Write back the register source. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2080 | switch (c->dst.bytes) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2081 | case 1: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2082 | *(u8 *) c->src.ptr = (u8) c->dst.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2083 | break; |
| 2084 | case 2: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2085 | *(u16 *) c->src.ptr = (u16) c->dst.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2086 | break; |
| 2087 | case 4: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2088 | *c->src.ptr = (u32) c->dst.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2089 | break; /* 64b reg: zero-extend */ |
| 2090 | case 8: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2091 | *c->src.ptr = c->dst.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2092 | break; |
| 2093 | } |
| 2094 | /* |
| 2095 | * Write back the memory destination with implicit LOCK |
| 2096 | * prefix. |
| 2097 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2098 | c->dst.val = c->src.val; |
| 2099 | c->lock_prefix = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2100 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2101 | case 0x88 ... 0x8b: /* mov */ |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 2102 | goto mov; |
Guillaume Thouvenin | 38d5bc6 | 2008-05-27 15:13:28 +0200 | [diff] [blame] | 2103 | case 0x8c: { /* mov r/m, sreg */ |
| 2104 | struct kvm_segment segreg; |
| 2105 | |
| 2106 | if (c->modrm_reg <= 5) |
| 2107 | kvm_get_segment(ctxt->vcpu, &segreg, c->modrm_reg); |
| 2108 | else { |
| 2109 | printk(KERN_INFO "0x8c: Invalid segreg in modrm byte 0x%02x\n", |
| 2110 | c->modrm); |
| 2111 | goto cannot_emulate; |
| 2112 | } |
| 2113 | c->dst.val = segreg.selector; |
| 2114 | break; |
| 2115 | } |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 2116 | case 0x8d: /* lea r16/r32, m */ |
Avi Kivity | f9b7aab | 2008-04-14 23:46:37 +0300 | [diff] [blame] | 2117 | c->dst.val = c->modrm_ea; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 2118 | break; |
Guillaume Thouvenin | 4257198 | 2008-05-27 14:49:15 +0200 | [diff] [blame] | 2119 | case 0x8e: { /* mov seg, r/m16 */ |
| 2120 | uint16_t sel; |
Guillaume Thouvenin | 4257198 | 2008-05-27 14:49:15 +0200 | [diff] [blame] | 2121 | |
| 2122 | sel = c->src.val; |
Gleb Natapov | 8b9f441 | 2010-02-18 12:14:59 +0200 | [diff] [blame] | 2123 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 2124 | if (c->modrm_reg == VCPU_SREG_CS || |
| 2125 | c->modrm_reg > VCPU_SREG_GS) { |
Gleb Natapov | 8b9f441 | 2010-02-18 12:14:59 +0200 | [diff] [blame] | 2126 | kvm_queue_exception(ctxt->vcpu, UD_VECTOR); |
| 2127 | goto done; |
| 2128 | } |
| 2129 | |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 2130 | if (c->modrm_reg == VCPU_SREG_SS) |
| 2131 | toggle_interruptibility(ctxt, X86_SHADOW_INT_MOV_SS); |
| 2132 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 2133 | rc = kvm_load_segment_descriptor(ctxt->vcpu, sel, c->modrm_reg); |
Guillaume Thouvenin | 4257198 | 2008-05-27 14:49:15 +0200 | [diff] [blame] | 2134 | |
| 2135 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2136 | break; |
| 2137 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2138 | case 0x8f: /* pop (sole member of Grp1a) */ |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2139 | rc = emulate_grp1a(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 2140 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2141 | goto done; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2142 | break; |
Mohammed Gamal | b13354f | 2008-06-15 19:37:38 +0300 | [diff] [blame] | 2143 | case 0x90: /* nop / xchg r8,rax */ |
| 2144 | if (!(c->rex_prefix & 1)) { /* nop */ |
| 2145 | c->dst.type = OP_NONE; |
| 2146 | break; |
| 2147 | } |
| 2148 | case 0x91 ... 0x97: /* xchg reg,rax */ |
| 2149 | c->src.type = c->dst.type = OP_REG; |
| 2150 | c->src.bytes = c->dst.bytes = c->op_bytes; |
| 2151 | c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX]; |
| 2152 | c->src.val = *(c->src.ptr); |
| 2153 | goto xchg; |
Nitin A Kamble | fd2a760 | 2007-08-28 18:22:47 -0700 | [diff] [blame] | 2154 | case 0x9c: /* pushf */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2155 | c->src.val = (unsigned long) ctxt->eflags; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2156 | emulate_push(ctxt); |
| 2157 | break; |
Nitin A Kamble | 535eabc | 2007-09-15 10:45:05 +0300 | [diff] [blame] | 2158 | case 0x9d: /* popf */ |
Avi Kivity | 2b48cc7 | 2008-11-29 20:36:13 +0200 | [diff] [blame] | 2159 | c->dst.type = OP_REG; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2160 | c->dst.ptr = (unsigned long *) &ctxt->eflags; |
Avi Kivity | 2b48cc7 | 2008-11-29 20:36:13 +0200 | [diff] [blame] | 2161 | c->dst.bytes = c->op_bytes; |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 2162 | rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes); |
| 2163 | if (rc != X86EMUL_CONTINUE) |
| 2164 | goto done; |
| 2165 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2166 | case 0xa0 ... 0xa1: /* mov */ |
| 2167 | c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX]; |
| 2168 | c->dst.val = c->src.val; |
| 2169 | break; |
| 2170 | case 0xa2 ... 0xa3: /* mov */ |
| 2171 | c->dst.val = (unsigned long)c->regs[VCPU_REGS_RAX]; |
| 2172 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2173 | case 0xa4 ... 0xa5: /* movs */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2174 | c->dst.type = OP_MEM; |
| 2175 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 2176 | c->dst.ptr = (unsigned long *)register_address(c, |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 2177 | es_base(ctxt), |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2178 | c->regs[VCPU_REGS_RDI]); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 2179 | rc = ops->read_emulated(register_address(c, |
| 2180 | seg_override_base(ctxt, c), |
| 2181 | c->regs[VCPU_REGS_RSI]), |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2182 | &c->dst.val, |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 2183 | c->dst.bytes, ctxt->vcpu); |
| 2184 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2185 | goto done; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 2186 | register_address_increment(c, &c->regs[VCPU_REGS_RSI], |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2187 | (ctxt->eflags & EFLG_DF) ? -c->dst.bytes |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2188 | : c->dst.bytes); |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 2189 | register_address_increment(c, &c->regs[VCPU_REGS_RDI], |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2190 | (ctxt->eflags & EFLG_DF) ? -c->dst.bytes |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2191 | : c->dst.bytes); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2192 | break; |
| 2193 | case 0xa6 ... 0xa7: /* cmps */ |
Guillaume Thouvenin | d7e5117 | 2007-11-26 13:49:09 +0100 | [diff] [blame] | 2194 | c->src.type = OP_NONE; /* Disable writeback. */ |
| 2195 | c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 2196 | c->src.ptr = (unsigned long *)register_address(c, |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 2197 | seg_override_base(ctxt, c), |
Guillaume Thouvenin | d7e5117 | 2007-11-26 13:49:09 +0100 | [diff] [blame] | 2198 | c->regs[VCPU_REGS_RSI]); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 2199 | rc = ops->read_emulated((unsigned long)c->src.ptr, |
| 2200 | &c->src.val, |
| 2201 | c->src.bytes, |
| 2202 | ctxt->vcpu); |
| 2203 | if (rc != X86EMUL_CONTINUE) |
Guillaume Thouvenin | d7e5117 | 2007-11-26 13:49:09 +0100 | [diff] [blame] | 2204 | goto done; |
| 2205 | |
| 2206 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2207 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 2208 | c->dst.ptr = (unsigned long *)register_address(c, |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 2209 | es_base(ctxt), |
Guillaume Thouvenin | d7e5117 | 2007-11-26 13:49:09 +0100 | [diff] [blame] | 2210 | c->regs[VCPU_REGS_RDI]); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 2211 | rc = ops->read_emulated((unsigned long)c->dst.ptr, |
| 2212 | &c->dst.val, |
| 2213 | c->dst.bytes, |
| 2214 | ctxt->vcpu); |
| 2215 | if (rc != X86EMUL_CONTINUE) |
Guillaume Thouvenin | d7e5117 | 2007-11-26 13:49:09 +0100 | [diff] [blame] | 2216 | goto done; |
| 2217 | |
| 2218 | DPRINTF("cmps: mem1=0x%p mem2=0x%p\n", c->src.ptr, c->dst.ptr); |
| 2219 | |
| 2220 | emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags); |
| 2221 | |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 2222 | register_address_increment(c, &c->regs[VCPU_REGS_RSI], |
Guillaume Thouvenin | d7e5117 | 2007-11-26 13:49:09 +0100 | [diff] [blame] | 2223 | (ctxt->eflags & EFLG_DF) ? -c->src.bytes |
| 2224 | : c->src.bytes); |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 2225 | register_address_increment(c, &c->regs[VCPU_REGS_RDI], |
Guillaume Thouvenin | d7e5117 | 2007-11-26 13:49:09 +0100 | [diff] [blame] | 2226 | (ctxt->eflags & EFLG_DF) ? -c->dst.bytes |
| 2227 | : c->dst.bytes); |
| 2228 | |
| 2229 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2230 | case 0xaa ... 0xab: /* stos */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2231 | c->dst.type = OP_MEM; |
| 2232 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Harvey Harrison | e470677 | 2008-02-19 07:40:38 -0800 | [diff] [blame] | 2233 | c->dst.ptr = (unsigned long *)register_address(c, |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 2234 | es_base(ctxt), |
Sheng Yang | a7e6c88 | 2007-11-15 14:52:28 +0800 | [diff] [blame] | 2235 | c->regs[VCPU_REGS_RDI]); |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2236 | c->dst.val = c->regs[VCPU_REGS_RAX]; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 2237 | register_address_increment(c, &c->regs[VCPU_REGS_RDI], |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2238 | (ctxt->eflags & EFLG_DF) ? -c->dst.bytes |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2239 | : c->dst.bytes); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2240 | break; |
| 2241 | case 0xac ... 0xad: /* lods */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2242 | c->dst.type = OP_REG; |
| 2243 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 2244 | c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX]; |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 2245 | rc = ops->read_emulated(register_address(c, |
| 2246 | seg_override_base(ctxt, c), |
| 2247 | c->regs[VCPU_REGS_RSI]), |
| 2248 | &c->dst.val, |
| 2249 | c->dst.bytes, |
| 2250 | ctxt->vcpu); |
| 2251 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2252 | goto done; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 2253 | register_address_increment(c, &c->regs[VCPU_REGS_RSI], |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2254 | (ctxt->eflags & EFLG_DF) ? -c->dst.bytes |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2255 | : c->dst.bytes); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2256 | break; |
| 2257 | case 0xae ... 0xaf: /* scas */ |
| 2258 | DPRINTF("Urk! I don't handle SCAS.\n"); |
| 2259 | goto cannot_emulate; |
Mohammed Gamal | a5e2e82 | 2008-08-27 05:02:56 +0300 | [diff] [blame] | 2260 | case 0xb0 ... 0xbf: /* mov r, imm */ |
Guillaume Thouvenin | 615ac12 | 2008-05-27 10:19:16 +0200 | [diff] [blame] | 2261 | goto mov; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2262 | case 0xc0 ... 0xc1: |
| 2263 | emulate_grp2(ctxt); |
| 2264 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2265 | case 0xc3: /* ret */ |
Avi Kivity | cf5de4f | 2008-11-28 00:14:07 +0200 | [diff] [blame] | 2266 | c->dst.type = OP_REG; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2267 | c->dst.ptr = &c->eip; |
Avi Kivity | cf5de4f | 2008-11-28 00:14:07 +0200 | [diff] [blame] | 2268 | c->dst.bytes = c->op_bytes; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2269 | goto pop_instruction; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2270 | case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */ |
| 2271 | mov: |
| 2272 | c->dst.val = c->src.val; |
| 2273 | break; |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2274 | case 0xcb: /* ret far */ |
| 2275 | rc = emulate_ret_far(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 2276 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2277 | goto done; |
| 2278 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2279 | case 0xd0 ... 0xd1: /* Grp2 */ |
| 2280 | c->src.val = 1; |
| 2281 | emulate_grp2(ctxt); |
| 2282 | break; |
| 2283 | case 0xd2 ... 0xd3: /* Grp2 */ |
| 2284 | c->src.val = c->regs[VCPU_REGS_RCX]; |
| 2285 | emulate_grp2(ctxt); |
| 2286 | break; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 2287 | case 0xe4: /* inb */ |
| 2288 | case 0xe5: /* in */ |
Gleb Natapov | 84ce66a | 2009-04-12 13:36:46 +0300 | [diff] [blame] | 2289 | port = c->src.val; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 2290 | io_dir_in = 1; |
| 2291 | goto do_io; |
| 2292 | case 0xe6: /* outb */ |
| 2293 | case 0xe7: /* out */ |
Gleb Natapov | 84ce66a | 2009-04-12 13:36:46 +0300 | [diff] [blame] | 2294 | port = c->src.val; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 2295 | io_dir_in = 0; |
| 2296 | goto do_io; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 2297 | case 0xe8: /* call (near) */ { |
Gleb Natapov | d53c477 | 2009-04-12 13:36:36 +0300 | [diff] [blame] | 2298 | long int rel = c->src.val; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2299 | c->src.val = (unsigned long) c->eip; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 2300 | jmp_rel(c, rel); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2301 | emulate_push(ctxt); |
| 2302 | break; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 2303 | } |
| 2304 | case 0xe9: /* jmp rel */ |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 2305 | goto jmp; |
Gleb Natapov | 782b877 | 2009-04-12 13:36:25 +0300 | [diff] [blame] | 2306 | case 0xea: /* jmp far */ |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 2307 | if (kvm_load_segment_descriptor(ctxt->vcpu, c->src2.val, |
| 2308 | VCPU_SREG_CS)) |
| 2309 | goto done; |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 2310 | |
Gleb Natapov | 782b877 | 2009-04-12 13:36:25 +0300 | [diff] [blame] | 2311 | c->eip = c->src.val; |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 2312 | break; |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 2313 | case 0xeb: |
| 2314 | jmp: /* jmp rel short */ |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 2315 | jmp_rel(c, c->src.val); |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 2316 | c->dst.type = OP_NONE; /* Disable writeback. */ |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 2317 | break; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 2318 | case 0xec: /* in al,dx */ |
| 2319 | case 0xed: /* in (e/r)ax,dx */ |
| 2320 | port = c->regs[VCPU_REGS_RDX]; |
| 2321 | io_dir_in = 1; |
| 2322 | goto do_io; |
| 2323 | case 0xee: /* out al,dx */ |
| 2324 | case 0xef: /* out (e/r)ax,dx */ |
| 2325 | port = c->regs[VCPU_REGS_RDX]; |
| 2326 | io_dir_in = 0; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2327 | do_io: |
| 2328 | if (!emulator_io_permited(ctxt, ops, port, |
| 2329 | (c->d & ByteOp) ? 1 : c->op_bytes)) { |
| 2330 | kvm_inject_gp(ctxt->vcpu, 0); |
| 2331 | goto done; |
| 2332 | } |
| 2333 | if (kvm_emulate_pio(ctxt->vcpu, io_dir_in, |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 2334 | (c->d & ByteOp) ? 1 : c->op_bytes, |
| 2335 | port) != 0) { |
| 2336 | c->eip = saved_eip; |
| 2337 | goto cannot_emulate; |
| 2338 | } |
Guillaume Thouvenin | e93f36b | 2008-10-28 10:51:30 +0100 | [diff] [blame] | 2339 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2340 | case 0xf4: /* hlt */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2341 | ctxt->vcpu->arch.halt_request = 1; |
Mohammed Gamal | 19fdfa0 | 2008-07-06 16:51:26 +0300 | [diff] [blame] | 2342 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2343 | case 0xf5: /* cmc */ |
| 2344 | /* complement carry flag from eflags reg */ |
| 2345 | ctxt->eflags ^= EFLG_CF; |
| 2346 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2347 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2348 | case 0xf6 ... 0xf7: /* Grp3 */ |
| 2349 | rc = emulate_grp3(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 2350 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2351 | goto done; |
| 2352 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2353 | case 0xf8: /* clc */ |
| 2354 | ctxt->eflags &= ~EFLG_CF; |
| 2355 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2356 | break; |
| 2357 | case 0xfa: /* cli */ |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2358 | if (emulator_bad_iopl(ctxt)) |
| 2359 | kvm_inject_gp(ctxt->vcpu, 0); |
| 2360 | else { |
| 2361 | ctxt->eflags &= ~X86_EFLAGS_IF; |
| 2362 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2363 | } |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2364 | break; |
| 2365 | case 0xfb: /* sti */ |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2366 | if (emulator_bad_iopl(ctxt)) |
| 2367 | kvm_inject_gp(ctxt->vcpu, 0); |
| 2368 | else { |
| 2369 | toggle_interruptibility(ctxt, X86_SHADOW_INT_STI); |
| 2370 | ctxt->eflags |= X86_EFLAGS_IF; |
| 2371 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2372 | } |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2373 | break; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 2374 | case 0xfc: /* cld */ |
| 2375 | ctxt->eflags &= ~EFLG_DF; |
| 2376 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2377 | break; |
| 2378 | case 0xfd: /* std */ |
| 2379 | ctxt->eflags |= EFLG_DF; |
| 2380 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2381 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2382 | case 0xfe ... 0xff: /* Grp4/Grp5 */ |
| 2383 | rc = emulate_grp45(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 2384 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2385 | goto done; |
| 2386 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2387 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2388 | |
| 2389 | writeback: |
| 2390 | rc = writeback(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 2391 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2392 | goto done; |
| 2393 | |
| 2394 | /* Commit shadow register state. */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2395 | memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2396 | kvm_rip_write(ctxt->vcpu, c->eip); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2397 | |
| 2398 | done: |
| 2399 | if (rc == X86EMUL_UNHANDLEABLE) { |
| 2400 | c->eip = saved_eip; |
| 2401 | return -1; |
| 2402 | } |
| 2403 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2404 | |
| 2405 | twobyte_insn: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2406 | switch (c->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2407 | case 0x01: /* lgdt, lidt, lmsw */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2408 | switch (c->modrm_reg) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2409 | u16 size; |
| 2410 | unsigned long address; |
| 2411 | |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 2412 | case 0: /* vmcall */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2413 | if (c->modrm_mod != 3 || c->modrm_rm != 1) |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 2414 | goto cannot_emulate; |
| 2415 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2416 | rc = kvm_fix_hypercall(ctxt->vcpu); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 2417 | if (rc != X86EMUL_CONTINUE) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2418 | goto done; |
| 2419 | |
Avi Kivity | 33e3885 | 2008-05-21 15:34:25 +0300 | [diff] [blame] | 2420 | /* Let the processor re-execute the fixed hypercall */ |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2421 | c->eip = kvm_rip_read(ctxt->vcpu); |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 2422 | /* Disable writeback. */ |
| 2423 | c->dst.type = OP_NONE; |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 2424 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2425 | case 2: /* lgdt */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2426 | rc = read_descriptor(ctxt, ops, c->src.ptr, |
| 2427 | &size, &address, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 2428 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2429 | goto done; |
| 2430 | realmode_lgdt(ctxt->vcpu, size, address); |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 2431 | /* Disable writeback. */ |
| 2432 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2433 | break; |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 2434 | case 3: /* lidt/vmmcall */ |
Avi Kivity | 2b3d2a2 | 2008-12-23 19:46:01 +0200 | [diff] [blame] | 2435 | if (c->modrm_mod == 3) { |
| 2436 | switch (c->modrm_rm) { |
| 2437 | case 1: |
| 2438 | rc = kvm_fix_hypercall(ctxt->vcpu); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 2439 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 2b3d2a2 | 2008-12-23 19:46:01 +0200 | [diff] [blame] | 2440 | goto done; |
| 2441 | break; |
| 2442 | default: |
| 2443 | goto cannot_emulate; |
| 2444 | } |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 2445 | } else { |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2446 | rc = read_descriptor(ctxt, ops, c->src.ptr, |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 2447 | &size, &address, |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2448 | c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 2449 | if (rc != X86EMUL_CONTINUE) |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 2450 | goto done; |
| 2451 | realmode_lidt(ctxt->vcpu, size, address); |
| 2452 | } |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 2453 | /* Disable writeback. */ |
| 2454 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2455 | break; |
| 2456 | case 4: /* smsw */ |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 2457 | c->dst.bytes = 2; |
| 2458 | c->dst.val = realmode_get_cr(ctxt->vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2459 | break; |
| 2460 | case 6: /* lmsw */ |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 2461 | realmode_lmsw(ctxt->vcpu, (u16)c->src.val, |
| 2462 | &ctxt->eflags); |
Avi Kivity | dc7457e | 2008-04-30 16:13:36 +0300 | [diff] [blame] | 2463 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2464 | break; |
| 2465 | case 7: /* invlpg*/ |
Sheng Yang | e8d8d7f | 2007-11-16 16:29:15 +0800 | [diff] [blame] | 2466 | emulate_invlpg(ctxt->vcpu, memop); |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 2467 | /* Disable writeback. */ |
| 2468 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2469 | break; |
| 2470 | default: |
| 2471 | goto cannot_emulate; |
| 2472 | } |
| 2473 | break; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 2474 | case 0x05: /* syscall */ |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2475 | rc = emulate_syscall(ctxt); |
| 2476 | if (rc != X86EMUL_CONTINUE) |
| 2477 | goto done; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2478 | else |
| 2479 | goto writeback; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 2480 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2481 | case 0x06: |
| 2482 | emulate_clts(ctxt->vcpu); |
| 2483 | c->dst.type = OP_NONE; |
| 2484 | break; |
| 2485 | case 0x08: /* invd */ |
| 2486 | case 0x09: /* wbinvd */ |
| 2487 | case 0x0d: /* GrpP (prefetch) */ |
| 2488 | case 0x18: /* Grp16 (prefetch/nop) */ |
| 2489 | c->dst.type = OP_NONE; |
| 2490 | break; |
| 2491 | case 0x20: /* mov cr, reg */ |
| 2492 | if (c->modrm_mod != 3) |
| 2493 | goto cannot_emulate; |
| 2494 | c->regs[c->modrm_rm] = |
| 2495 | realmode_get_cr(ctxt->vcpu, c->modrm_reg); |
| 2496 | c->dst.type = OP_NONE; /* no writeback */ |
| 2497 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2498 | case 0x21: /* mov from dr to reg */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2499 | if (c->modrm_mod != 3) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2500 | goto cannot_emulate; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2501 | 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] | 2502 | if (rc) |
| 2503 | goto cannot_emulate; |
| 2504 | c->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2505 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2506 | case 0x22: /* mov reg, cr */ |
| 2507 | if (c->modrm_mod != 3) |
| 2508 | goto cannot_emulate; |
| 2509 | realmode_set_cr(ctxt->vcpu, |
| 2510 | c->modrm_reg, c->modrm_val, &ctxt->eflags); |
| 2511 | c->dst.type = OP_NONE; |
| 2512 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2513 | case 0x23: /* mov from reg to dr */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2514 | if (c->modrm_mod != 3) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2515 | goto cannot_emulate; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2516 | rc = emulator_set_dr(ctxt, c->modrm_reg, |
| 2517 | c->regs[c->modrm_rm]); |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 2518 | if (rc) |
| 2519 | goto cannot_emulate; |
| 2520 | c->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2521 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2522 | case 0x30: |
| 2523 | /* wrmsr */ |
| 2524 | msr_data = (u32)c->regs[VCPU_REGS_RAX] |
| 2525 | | ((u64)c->regs[VCPU_REGS_RDX] << 32); |
| 2526 | rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data); |
| 2527 | if (rc) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 2528 | kvm_inject_gp(ctxt->vcpu, 0); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2529 | c->eip = kvm_rip_read(ctxt->vcpu); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2530 | } |
| 2531 | rc = X86EMUL_CONTINUE; |
| 2532 | c->dst.type = OP_NONE; |
| 2533 | break; |
| 2534 | case 0x32: |
| 2535 | /* rdmsr */ |
| 2536 | rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data); |
| 2537 | if (rc) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 2538 | kvm_inject_gp(ctxt->vcpu, 0); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2539 | c->eip = kvm_rip_read(ctxt->vcpu); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2540 | } else { |
| 2541 | c->regs[VCPU_REGS_RAX] = (u32)msr_data; |
| 2542 | c->regs[VCPU_REGS_RDX] = msr_data >> 32; |
| 2543 | } |
| 2544 | rc = X86EMUL_CONTINUE; |
| 2545 | c->dst.type = OP_NONE; |
| 2546 | break; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 2547 | case 0x34: /* sysenter */ |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2548 | rc = emulate_sysenter(ctxt); |
| 2549 | if (rc != X86EMUL_CONTINUE) |
| 2550 | goto done; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2551 | else |
| 2552 | goto writeback; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 2553 | break; |
| 2554 | case 0x35: /* sysexit */ |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2555 | rc = emulate_sysexit(ctxt); |
| 2556 | if (rc != X86EMUL_CONTINUE) |
| 2557 | goto done; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2558 | else |
| 2559 | goto writeback; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 2560 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2561 | case 0x40 ... 0x4f: /* cmov */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2562 | c->dst.val = c->dst.orig_val = c->src.val; |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 2563 | if (!test_cc(c->b, ctxt->eflags)) |
| 2564 | c->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2565 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 2566 | case 0x80 ... 0x8f: /* jnz rel, etc*/ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2567 | if (test_cc(c->b, ctxt->eflags)) |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 2568 | jmp_rel(c, c->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2569 | c->dst.type = OP_NONE; |
| 2570 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2571 | case 0xa0: /* push fs */ |
| 2572 | emulate_push_sreg(ctxt, VCPU_SREG_FS); |
| 2573 | break; |
| 2574 | case 0xa1: /* pop fs */ |
| 2575 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 2576 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2577 | goto done; |
| 2578 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 2579 | case 0xa3: |
| 2580 | bt: /* bt */ |
Qing He | e4f8e03 | 2007-09-24 17:22:13 +0800 | [diff] [blame] | 2581 | c->dst.type = OP_NONE; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2582 | /* only subword offset */ |
| 2583 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2584 | emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 2585 | break; |
Guillaume Thouvenin | 9bf8ea4 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 2586 | case 0xa4: /* shld imm8, r, r/m */ |
| 2587 | case 0xa5: /* shld cl, r, r/m */ |
| 2588 | emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags); |
| 2589 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2590 | case 0xa8: /* push gs */ |
| 2591 | emulate_push_sreg(ctxt, VCPU_SREG_GS); |
| 2592 | break; |
| 2593 | case 0xa9: /* pop gs */ |
| 2594 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 2595 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2596 | goto done; |
| 2597 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 2598 | case 0xab: |
| 2599 | bts: /* bts */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2600 | /* only subword offset */ |
| 2601 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2602 | emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 2603 | break; |
Guillaume Thouvenin | 9bf8ea4 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 2604 | case 0xac: /* shrd imm8, r, r/m */ |
| 2605 | case 0xad: /* shrd cl, r, r/m */ |
| 2606 | emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags); |
| 2607 | break; |
Glauber Costa | 2a7c5b8 | 2008-07-10 17:08:15 -0300 | [diff] [blame] | 2608 | case 0xae: /* clflush */ |
| 2609 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2610 | case 0xb0 ... 0xb1: /* cmpxchg */ |
| 2611 | /* |
| 2612 | * Save real source value, then compare EAX against |
| 2613 | * destination. |
| 2614 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2615 | c->src.orig_val = c->src.val; |
| 2616 | c->src.val = c->regs[VCPU_REGS_RAX]; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2617 | emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags); |
| 2618 | if (ctxt->eflags & EFLG_ZF) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2619 | /* Success: write back to memory. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2620 | c->dst.val = c->src.orig_val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2621 | } else { |
| 2622 | /* Failure: write the value we saw to EAX. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2623 | c->dst.type = OP_REG; |
| 2624 | c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2625 | } |
| 2626 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2627 | case 0xb3: |
| 2628 | btr: /* btr */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2629 | /* only subword offset */ |
| 2630 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2631 | emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2632 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2633 | case 0xb6 ... 0xb7: /* movzx */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2634 | c->dst.bytes = c->op_bytes; |
| 2635 | c->dst.val = (c->d & ByteOp) ? (u8) c->src.val |
| 2636 | : (u16) c->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2637 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2638 | case 0xba: /* Grp8 */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2639 | switch (c->modrm_reg & 3) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2640 | case 0: |
| 2641 | goto bt; |
| 2642 | case 1: |
| 2643 | goto bts; |
| 2644 | case 2: |
| 2645 | goto btr; |
| 2646 | case 3: |
| 2647 | goto btc; |
| 2648 | } |
| 2649 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 2650 | case 0xbb: |
| 2651 | btc: /* btc */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2652 | /* only subword offset */ |
| 2653 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2654 | emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 2655 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2656 | case 0xbe ... 0xbf: /* movsx */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2657 | c->dst.bytes = c->op_bytes; |
| 2658 | c->dst.val = (c->d & ByteOp) ? (s8) c->src.val : |
| 2659 | (s16) c->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2660 | break; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 2661 | case 0xc3: /* movnti */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2662 | c->dst.bytes = c->op_bytes; |
| 2663 | c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val : |
| 2664 | (u64) c->src.val; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 2665 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2666 | case 0xc7: /* Grp9 (cmpxchg8b) */ |
Sheng Yang | e8d8d7f | 2007-11-16 16:29:15 +0800 | [diff] [blame] | 2667 | rc = emulate_grp9(ctxt, ops, memop); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame^] | 2668 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2669 | goto done; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2670 | c->dst.type = OP_NONE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2671 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2672 | } |
| 2673 | goto writeback; |
| 2674 | |
| 2675 | cannot_emulate: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2676 | DPRINTF("Cannot emulate %02x\n", c->b); |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 2677 | c->eip = saved_eip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2678 | return -1; |
| 2679 | } |