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