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