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