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