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