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 |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 946 | x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 947 | { |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 948 | struct decode_cache *c = &ctxt->decode; |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 949 | int rc = X86EMUL_CONTINUE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 950 | int mode = ctxt->mode; |
Avi Kivity | 3885d53 | 2010-07-29 15:11:48 +0300 | [diff] [blame] | 951 | int def_op_bytes, def_ad_bytes, dual, goffset; |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 952 | struct opcode opcode, *g_mod012, *g_mod3; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 953 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 954 | /* we cannot decode insn before we complete previous rep insn */ |
| 955 | WARN_ON(ctxt->restart); |
| 956 | |
Gleb Natapov | 063db06 | 2010-03-18 15:20:06 +0200 | [diff] [blame] | 957 | c->eip = ctxt->eip; |
Avi Kivity | 2fb53ad | 2010-04-11 13:05:15 +0300 | [diff] [blame] | 958 | c->fetch.start = c->fetch.end = c->eip; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 959 | ctxt->cs_base = seg_base(ctxt, ops, VCPU_SREG_CS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 960 | |
| 961 | switch (mode) { |
| 962 | case X86EMUL_MODE_REAL: |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 963 | case X86EMUL_MODE_VM86: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 964 | case X86EMUL_MODE_PROT16: |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 965 | def_op_bytes = def_ad_bytes = 2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 966 | break; |
| 967 | case X86EMUL_MODE_PROT32: |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 968 | def_op_bytes = def_ad_bytes = 4; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 969 | break; |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 970 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 971 | case X86EMUL_MODE_PROT64: |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 972 | def_op_bytes = 4; |
| 973 | def_ad_bytes = 8; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 974 | break; |
| 975 | #endif |
| 976 | default: |
| 977 | return -1; |
| 978 | } |
| 979 | |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 980 | c->op_bytes = def_op_bytes; |
| 981 | c->ad_bytes = def_ad_bytes; |
| 982 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 983 | /* Legacy prefixes. */ |
Laurent Vivier | b4c6abf | 2007-09-25 13:36:40 +0200 | [diff] [blame] | 984 | for (;;) { |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 985 | switch (c->b = insn_fetch(u8, 1, c->eip)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 986 | case 0x66: /* operand-size override */ |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 987 | /* switch between 2/4 bytes */ |
| 988 | c->op_bytes = def_op_bytes ^ 6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 989 | break; |
| 990 | case 0x67: /* address-size override */ |
| 991 | if (mode == X86EMUL_MODE_PROT64) |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 992 | /* switch between 4/8 bytes */ |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 993 | c->ad_bytes = def_ad_bytes ^ 12; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 994 | else |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 995 | /* switch between 2/4 bytes */ |
Avi Kivity | f21b8bf | 2007-11-22 14:16:12 +0200 | [diff] [blame] | 996 | c->ad_bytes = def_ad_bytes ^ 6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 997 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 998 | case 0x26: /* ES override */ |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 999 | case 0x2e: /* CS override */ |
| 1000 | case 0x36: /* SS override */ |
| 1001 | case 0x3e: /* DS override */ |
| 1002 | set_seg_override(c, (c->b >> 3) & 3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1003 | break; |
| 1004 | case 0x64: /* FS override */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1005 | case 0x65: /* GS override */ |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 1006 | set_seg_override(c, c->b & 7); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1007 | break; |
Laurent Vivier | b4c6abf | 2007-09-25 13:36:40 +0200 | [diff] [blame] | 1008 | case 0x40 ... 0x4f: /* REX */ |
| 1009 | if (mode != X86EMUL_MODE_PROT64) |
| 1010 | goto done_prefixes; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1011 | c->rex_prefix = c->b; |
Laurent Vivier | b4c6abf | 2007-09-25 13:36:40 +0200 | [diff] [blame] | 1012 | continue; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1013 | case 0xf0: /* LOCK */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1014 | c->lock_prefix = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1015 | break; |
Laurent Vivier | ae6200b | 2007-09-20 11:17:24 +0200 | [diff] [blame] | 1016 | case 0xf2: /* REPNE/REPNZ */ |
Guillaume Thouvenin | 90e0a28 | 2007-11-22 11:32:09 +0100 | [diff] [blame] | 1017 | c->rep_prefix = REPNE_PREFIX; |
| 1018 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1019 | case 0xf3: /* REP/REPE/REPZ */ |
Guillaume Thouvenin | 90e0a28 | 2007-11-22 11:32:09 +0100 | [diff] [blame] | 1020 | c->rep_prefix = REPE_PREFIX; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1021 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1022 | default: |
| 1023 | goto done_prefixes; |
| 1024 | } |
Laurent Vivier | b4c6abf | 2007-09-25 13:36:40 +0200 | [diff] [blame] | 1025 | |
| 1026 | /* Any legacy prefix after a REX prefix nullifies its effect. */ |
| 1027 | |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1028 | c->rex_prefix = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | done_prefixes: |
| 1032 | |
| 1033 | /* REX prefix. */ |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1034 | if (c->rex_prefix) |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 1035 | if (c->rex_prefix & 8) |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1036 | c->op_bytes = 8; /* REX.W */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1037 | |
| 1038 | /* Opcode byte(s). */ |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 1039 | opcode = opcode_table[c->b]; |
| 1040 | if (opcode.flags == 0) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1041 | /* Two-byte opcode? */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1042 | if (c->b == 0x0f) { |
| 1043 | c->twobyte = 1; |
| 1044 | c->b = insn_fetch(u8, 1, c->eip); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 1045 | opcode = twobyte_table[c->b]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1046 | } |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 1047 | } |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 1048 | c->d = opcode.flags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1049 | |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 1050 | if (c->d & Group) { |
Avi Kivity | 52811d7 | 2010-07-26 14:37:48 +0300 | [diff] [blame] | 1051 | dual = c->d & GroupDual; |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 1052 | c->modrm = insn_fetch(u8, 1, c->eip); |
| 1053 | --c->eip; |
| 1054 | |
Avi Kivity | 3885d53 | 2010-07-29 15:11:48 +0300 | [diff] [blame] | 1055 | if (c->d & GroupDual) { |
| 1056 | g_mod012 = opcode.u.gdual->mod012; |
| 1057 | g_mod3 = opcode.u.gdual->mod3; |
| 1058 | } else |
| 1059 | g_mod012 = g_mod3 = opcode.u.group; |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 1060 | |
Avi Kivity | 3885d53 | 2010-07-29 15:11:48 +0300 | [diff] [blame] | 1061 | c->d &= ~(Group | GroupDual); |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 1062 | |
| 1063 | goffset = (c->modrm >> 3) & 7; |
| 1064 | |
| 1065 | if ((c->modrm >> 6) == 3) |
| 1066 | opcode = g_mod3[goffset]; |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 1067 | else |
Avi Kivity | 120df89 | 2010-07-29 15:11:39 +0300 | [diff] [blame] | 1068 | opcode = g_mod012[goffset]; |
| 1069 | c->d |= opcode.flags; |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | /* Unrecognised? */ |
Avi Kivity | 047a481 | 2010-07-26 14:37:47 +0300 | [diff] [blame] | 1073 | if (c->d == 0 || (c->d & Undefined)) { |
Avi Kivity | e09d082 | 2008-01-18 12:38:59 +0200 | [diff] [blame] | 1074 | DPRINTF("Cannot emulate %02x\n", c->b); |
| 1075 | return -1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1076 | } |
| 1077 | |
Avi Kivity | 6e3d5df | 2007-12-06 18:14:14 +0200 | [diff] [blame] | 1078 | if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack)) |
| 1079 | c->op_bytes = 8; |
| 1080 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1081 | /* ModRM and SIB bytes. */ |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1082 | if (c->d & ModRM) |
| 1083 | rc = decode_modrm(ctxt, ops); |
| 1084 | else if (c->d & MemAbs) |
| 1085 | rc = decode_abs(ctxt, ops); |
Takuya Yoshikawa | 3e2815e | 2010-02-12 15:53:59 +0900 | [diff] [blame] | 1086 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 1c73ef665 | 2007-11-01 06:31:28 +0200 | [diff] [blame] | 1087 | goto done; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1088 | |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 1089 | if (!c->has_seg_override) |
| 1090 | set_seg_override(c, VCPU_SREG_DS); |
Avi Kivity | c7e75a3 | 2007-10-28 16:34:25 +0200 | [diff] [blame] | 1091 | |
Avi Kivity | 7a5b56d | 2008-06-22 16:22:51 +0300 | [diff] [blame] | 1092 | if (!(!c->twobyte && c->b == 0x8d)) |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1093 | c->modrm_ea += seg_override_base(ctxt, ops, c); |
Avi Kivity | c7e75a3 | 2007-10-28 16:34:25 +0200 | [diff] [blame] | 1094 | |
| 1095 | if (c->ad_bytes != 8) |
| 1096 | c->modrm_ea = (u32)c->modrm_ea; |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 1097 | |
| 1098 | if (c->rip_relative) |
| 1099 | c->modrm_ea += c->eip; |
| 1100 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1101 | /* |
| 1102 | * Decode and fetch the source operand: register, memory |
| 1103 | * or immediate. |
| 1104 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1105 | switch (c->d & SrcMask) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1106 | case SrcNone: |
| 1107 | break; |
| 1108 | case SrcReg: |
Avi Kivity | 9f1ef3f | 2007-10-31 11:21:06 +0200 | [diff] [blame] | 1109 | decode_register_operand(&c->src, c, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1110 | break; |
| 1111 | case SrcMem16: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1112 | c->src.bytes = 2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1113 | goto srcmem_common; |
| 1114 | case SrcMem32: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1115 | c->src.bytes = 4; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1116 | goto srcmem_common; |
| 1117 | case SrcMem: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1118 | c->src.bytes = (c->d & ByteOp) ? 1 : |
| 1119 | c->op_bytes; |
Rusty Russell | b85b9ee9 | 2007-09-09 14:12:54 +0300 | [diff] [blame] | 1120 | /* Don't fetch the address for invlpg: it could be unmapped. */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1121 | if (c->twobyte && c->b == 0x01 && c->modrm_reg == 7) |
Rusty Russell | b85b9ee9 | 2007-09-09 14:12:54 +0300 | [diff] [blame] | 1122 | break; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1123 | srcmem_common: |
Aurelien Jarno | 4e62417 | 2007-10-17 19:30:41 +0200 | [diff] [blame] | 1124 | /* |
| 1125 | * For instructions with a ModR/M byte, switch to register |
| 1126 | * access if Mod = 3. |
| 1127 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1128 | if ((c->d & ModRM) && c->modrm_mod == 3) { |
| 1129 | c->src.type = OP_REG; |
Avi Kivity | 66b8550 | 2008-04-14 23:27:07 +0300 | [diff] [blame] | 1130 | c->src.val = c->modrm_val; |
Avi Kivity | 107d6d2 | 2008-05-05 14:58:26 +0300 | [diff] [blame] | 1131 | c->src.ptr = c->modrm_ptr; |
Aurelien Jarno | 4e62417 | 2007-10-17 19:30:41 +0200 | [diff] [blame] | 1132 | break; |
| 1133 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1134 | c->src.type = OP_MEM; |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 1135 | c->src.ptr = (unsigned long *)c->modrm_ea; |
| 1136 | c->src.val = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1137 | break; |
| 1138 | case SrcImm: |
Avi Kivity | c9eaf20f | 2009-05-18 16:13:45 +0300 | [diff] [blame] | 1139 | case SrcImmU: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1140 | c->src.type = OP_IMM; |
| 1141 | c->src.ptr = (unsigned long *)c->eip; |
| 1142 | c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 1143 | if (c->src.bytes == 8) |
| 1144 | c->src.bytes = 4; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1145 | /* NB. Immediates are sign-extended as necessary. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1146 | switch (c->src.bytes) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1147 | case 1: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1148 | c->src.val = insn_fetch(s8, 1, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1149 | break; |
| 1150 | case 2: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1151 | c->src.val = insn_fetch(s16, 2, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1152 | break; |
| 1153 | case 4: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1154 | c->src.val = insn_fetch(s32, 4, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1155 | break; |
| 1156 | } |
Avi Kivity | c9eaf20f | 2009-05-18 16:13:45 +0300 | [diff] [blame] | 1157 | if ((c->d & SrcMask) == SrcImmU) { |
| 1158 | switch (c->src.bytes) { |
| 1159 | case 1: |
| 1160 | c->src.val &= 0xff; |
| 1161 | break; |
| 1162 | case 2: |
| 1163 | c->src.val &= 0xffff; |
| 1164 | break; |
| 1165 | case 4: |
| 1166 | c->src.val &= 0xffffffff; |
| 1167 | break; |
| 1168 | } |
| 1169 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1170 | break; |
| 1171 | case SrcImmByte: |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 1172 | case SrcImmUByte: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1173 | c->src.type = OP_IMM; |
| 1174 | c->src.ptr = (unsigned long *)c->eip; |
| 1175 | c->src.bytes = 1; |
Gleb Natapov | 341de7e | 2009-04-12 13:36:41 +0300 | [diff] [blame] | 1176 | if ((c->d & SrcMask) == SrcImmByte) |
| 1177 | c->src.val = insn_fetch(s8, 1, c->eip); |
| 1178 | else |
| 1179 | c->src.val = insn_fetch(u8, 1, c->eip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1180 | break; |
Wei Yongjun | 5d55f29 | 2010-07-07 17:43:35 +0800 | [diff] [blame] | 1181 | case SrcAcc: |
| 1182 | c->src.type = OP_REG; |
| 1183 | c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 1184 | c->src.ptr = &c->regs[VCPU_REGS_RAX]; |
| 1185 | switch (c->src.bytes) { |
| 1186 | case 1: |
| 1187 | c->src.val = *(u8 *)c->src.ptr; |
| 1188 | break; |
| 1189 | case 2: |
| 1190 | c->src.val = *(u16 *)c->src.ptr; |
| 1191 | break; |
| 1192 | case 4: |
| 1193 | c->src.val = *(u32 *)c->src.ptr; |
| 1194 | break; |
| 1195 | case 8: |
| 1196 | c->src.val = *(u64 *)c->src.ptr; |
| 1197 | break; |
| 1198 | } |
| 1199 | break; |
Guillaume Thouvenin | bfcadf8 | 2008-12-04 14:27:38 +0100 | [diff] [blame] | 1200 | case SrcOne: |
| 1201 | c->src.bytes = 1; |
| 1202 | c->src.val = 1; |
| 1203 | break; |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 1204 | case SrcSI: |
| 1205 | c->src.type = OP_MEM; |
| 1206 | c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 1207 | c->src.ptr = (unsigned long *) |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1208 | register_address(c, seg_override_base(ctxt, ops, c), |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 1209 | c->regs[VCPU_REGS_RSI]); |
| 1210 | c->src.val = 0; |
| 1211 | break; |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 1212 | case SrcImmFAddr: |
| 1213 | c->src.type = OP_IMM; |
| 1214 | c->src.ptr = (unsigned long *)c->eip; |
| 1215 | c->src.bytes = c->op_bytes + 2; |
| 1216 | insn_fetch_arr(c->src.valptr, c->src.bytes, c->eip); |
| 1217 | break; |
| 1218 | case SrcMemFAddr: |
| 1219 | c->src.type = OP_MEM; |
| 1220 | c->src.ptr = (unsigned long *)c->modrm_ea; |
| 1221 | c->src.bytes = c->op_bytes + 2; |
| 1222 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1223 | } |
| 1224 | |
Guillaume Thouvenin | 0dc8d10 | 2008-12-04 14:26:42 +0100 | [diff] [blame] | 1225 | /* |
| 1226 | * Decode and fetch the second source operand: register, memory |
| 1227 | * or immediate. |
| 1228 | */ |
| 1229 | switch (c->d & Src2Mask) { |
| 1230 | case Src2None: |
| 1231 | break; |
| 1232 | case Src2CL: |
| 1233 | c->src2.bytes = 1; |
| 1234 | c->src2.val = c->regs[VCPU_REGS_RCX] & 0x8; |
| 1235 | break; |
| 1236 | case Src2ImmByte: |
| 1237 | c->src2.type = OP_IMM; |
| 1238 | c->src2.ptr = (unsigned long *)c->eip; |
| 1239 | c->src2.bytes = 1; |
| 1240 | c->src2.val = insn_fetch(u8, 1, c->eip); |
| 1241 | break; |
| 1242 | case Src2One: |
| 1243 | c->src2.bytes = 1; |
| 1244 | c->src2.val = 1; |
| 1245 | break; |
| 1246 | } |
| 1247 | |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1248 | /* Decode and fetch the destination operand: register or memory. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1249 | switch (c->d & DstMask) { |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1250 | case ImplicitOps: |
| 1251 | /* Special instructions do their own operand decoding. */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1252 | return 0; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1253 | case DstReg: |
Avi Kivity | 9f1ef3f | 2007-10-31 11:21:06 +0200 | [diff] [blame] | 1254 | decode_register_operand(&c->dst, c, |
Avi Kivity | 3c118e2 | 2007-10-31 10:27:04 +0200 | [diff] [blame] | 1255 | c->twobyte && (c->b == 0xb6 || c->b == 0xb7)); |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 1256 | break; |
| 1257 | case DstMem: |
Gleb Natapov | 6550e1f | 2010-03-21 13:08:21 +0200 | [diff] [blame] | 1258 | case DstMem64: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1259 | if ((c->d & ModRM) && c->modrm_mod == 3) { |
Guillaume Thouvenin | 89c6963 | 2008-05-27 10:22:20 +0200 | [diff] [blame] | 1260 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 1261 | c->dst.type = OP_REG; |
Avi Kivity | 66b8550 | 2008-04-14 23:27:07 +0300 | [diff] [blame] | 1262 | c->dst.val = c->dst.orig_val = c->modrm_val; |
Avi Kivity | 107d6d2 | 2008-05-05 14:58:26 +0300 | [diff] [blame] | 1263 | c->dst.ptr = c->modrm_ptr; |
Aurelien Jarno | 4e62417 | 2007-10-17 19:30:41 +0200 | [diff] [blame] | 1264 | break; |
| 1265 | } |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1266 | c->dst.type = OP_MEM; |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 1267 | c->dst.ptr = (unsigned long *)c->modrm_ea; |
Gleb Natapov | 6550e1f | 2010-03-21 13:08:21 +0200 | [diff] [blame] | 1268 | if ((c->d & DstMask) == DstMem64) |
| 1269 | c->dst.bytes = 8; |
| 1270 | else |
| 1271 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 1272 | c->dst.val = 0; |
| 1273 | if (c->d & BitOp) { |
| 1274 | unsigned long mask = ~(c->dst.bytes * 8 - 1); |
| 1275 | |
| 1276 | c->dst.ptr = (void *)c->dst.ptr + |
| 1277 | (c->src.val & mask) / 8; |
| 1278 | } |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1279 | break; |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 1280 | case DstAcc: |
| 1281 | c->dst.type = OP_REG; |
Gleb Natapov | d6d367d | 2010-03-15 16:38:28 +0200 | [diff] [blame] | 1282 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 1283 | c->dst.ptr = &c->regs[VCPU_REGS_RAX]; |
Gleb Natapov | d6d367d | 2010-03-15 16:38:28 +0200 | [diff] [blame] | 1284 | switch (c->dst.bytes) { |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 1285 | case 1: |
| 1286 | c->dst.val = *(u8 *)c->dst.ptr; |
| 1287 | break; |
| 1288 | case 2: |
| 1289 | c->dst.val = *(u16 *)c->dst.ptr; |
| 1290 | break; |
| 1291 | case 4: |
| 1292 | c->dst.val = *(u32 *)c->dst.ptr; |
| 1293 | break; |
Gleb Natapov | d6d367d | 2010-03-15 16:38:28 +0200 | [diff] [blame] | 1294 | case 8: |
| 1295 | c->dst.val = *(u64 *)c->dst.ptr; |
| 1296 | break; |
Guillaume Thouvenin | 9c9fddd | 2008-09-12 13:50:25 +0200 | [diff] [blame] | 1297 | } |
| 1298 | c->dst.orig_val = c->dst.val; |
| 1299 | break; |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 1300 | case DstDI: |
| 1301 | c->dst.type = OP_MEM; |
| 1302 | c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes; |
| 1303 | c->dst.ptr = (unsigned long *) |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1304 | register_address(c, es_base(ctxt, ops), |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 1305 | c->regs[VCPU_REGS_RDI]); |
| 1306 | c->dst.val = 0; |
| 1307 | break; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | done: |
| 1311 | return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0; |
| 1312 | } |
| 1313 | |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1314 | static int read_emulated(struct x86_emulate_ctxt *ctxt, |
| 1315 | struct x86_emulate_ops *ops, |
| 1316 | unsigned long addr, void *dest, unsigned size) |
| 1317 | { |
| 1318 | int rc; |
| 1319 | struct read_cache *mc = &ctxt->decode.mem_read; |
Gleb Natapov | 8fe681e | 2010-04-28 19:15:37 +0300 | [diff] [blame] | 1320 | u32 err; |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1321 | |
| 1322 | while (size) { |
| 1323 | int n = min(size, 8u); |
| 1324 | size -= n; |
| 1325 | if (mc->pos < mc->end) |
| 1326 | goto read_cached; |
| 1327 | |
Gleb Natapov | 8fe681e | 2010-04-28 19:15:37 +0300 | [diff] [blame] | 1328 | rc = ops->read_emulated(addr, mc->data + mc->end, n, &err, |
| 1329 | ctxt->vcpu); |
| 1330 | if (rc == X86EMUL_PROPAGATE_FAULT) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 1331 | emulate_pf(ctxt, addr, err); |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1332 | if (rc != X86EMUL_CONTINUE) |
| 1333 | return rc; |
| 1334 | mc->end += n; |
| 1335 | |
| 1336 | read_cached: |
| 1337 | memcpy(dest, mc->data + mc->pos, n); |
| 1338 | mc->pos += n; |
| 1339 | dest += n; |
| 1340 | addr += n; |
| 1341 | } |
| 1342 | return X86EMUL_CONTINUE; |
| 1343 | } |
| 1344 | |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 1345 | static int pio_in_emulated(struct x86_emulate_ctxt *ctxt, |
| 1346 | struct x86_emulate_ops *ops, |
| 1347 | unsigned int size, unsigned short port, |
| 1348 | void *dest) |
| 1349 | { |
| 1350 | struct read_cache *rc = &ctxt->decode.io_read; |
| 1351 | |
| 1352 | if (rc->pos == rc->end) { /* refill pio read ahead */ |
| 1353 | struct decode_cache *c = &ctxt->decode; |
| 1354 | unsigned int in_page, n; |
| 1355 | unsigned int count = c->rep_prefix ? |
| 1356 | address_mask(c, c->regs[VCPU_REGS_RCX]) : 1; |
| 1357 | in_page = (ctxt->eflags & EFLG_DF) ? |
| 1358 | offset_in_page(c->regs[VCPU_REGS_RDI]) : |
| 1359 | PAGE_SIZE - offset_in_page(c->regs[VCPU_REGS_RDI]); |
| 1360 | n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size, |
| 1361 | count); |
| 1362 | if (n == 0) |
| 1363 | n = 1; |
| 1364 | rc->pos = rc->end = 0; |
| 1365 | if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu)) |
| 1366 | return 0; |
| 1367 | rc->end = n * size; |
| 1368 | } |
| 1369 | |
| 1370 | memcpy(dest, rc->data + rc->pos, size); |
| 1371 | rc->pos += size; |
| 1372 | return 1; |
| 1373 | } |
| 1374 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1375 | static u32 desc_limit_scaled(struct desc_struct *desc) |
| 1376 | { |
| 1377 | u32 limit = get_desc_limit(desc); |
| 1378 | |
| 1379 | return desc->g ? (limit << 12) | 0xfff : limit; |
| 1380 | } |
| 1381 | |
| 1382 | static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt, |
| 1383 | struct x86_emulate_ops *ops, |
| 1384 | u16 selector, struct desc_ptr *dt) |
| 1385 | { |
| 1386 | if (selector & 1 << 2) { |
| 1387 | struct desc_struct desc; |
| 1388 | memset (dt, 0, sizeof *dt); |
| 1389 | if (!ops->get_cached_descriptor(&desc, VCPU_SREG_LDTR, ctxt->vcpu)) |
| 1390 | return; |
| 1391 | |
| 1392 | dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */ |
| 1393 | dt->address = get_desc_base(&desc); |
| 1394 | } else |
| 1395 | ops->get_gdt(dt, ctxt->vcpu); |
| 1396 | } |
| 1397 | |
| 1398 | /* allowed just for 8 bytes segments */ |
| 1399 | static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1400 | struct x86_emulate_ops *ops, |
| 1401 | u16 selector, struct desc_struct *desc) |
| 1402 | { |
| 1403 | struct desc_ptr dt; |
| 1404 | u16 index = selector >> 3; |
| 1405 | int ret; |
| 1406 | u32 err; |
| 1407 | ulong addr; |
| 1408 | |
| 1409 | get_descriptor_table_ptr(ctxt, ops, selector, &dt); |
| 1410 | |
| 1411 | if (dt.size < index * 8 + 7) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 1412 | emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1413 | return X86EMUL_PROPAGATE_FAULT; |
| 1414 | } |
| 1415 | addr = dt.address + index * 8; |
| 1416 | ret = ops->read_std(addr, desc, sizeof *desc, ctxt->vcpu, &err); |
| 1417 | if (ret == X86EMUL_PROPAGATE_FAULT) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 1418 | emulate_pf(ctxt, addr, err); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1419 | |
| 1420 | return ret; |
| 1421 | } |
| 1422 | |
| 1423 | /* allowed just for 8 bytes segments */ |
| 1424 | static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1425 | struct x86_emulate_ops *ops, |
| 1426 | u16 selector, struct desc_struct *desc) |
| 1427 | { |
| 1428 | struct desc_ptr dt; |
| 1429 | u16 index = selector >> 3; |
| 1430 | u32 err; |
| 1431 | ulong addr; |
| 1432 | int ret; |
| 1433 | |
| 1434 | get_descriptor_table_ptr(ctxt, ops, selector, &dt); |
| 1435 | |
| 1436 | if (dt.size < index * 8 + 7) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 1437 | emulate_gp(ctxt, selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1438 | return X86EMUL_PROPAGATE_FAULT; |
| 1439 | } |
| 1440 | |
| 1441 | addr = dt.address + index * 8; |
| 1442 | ret = ops->write_std(addr, desc, sizeof *desc, ctxt->vcpu, &err); |
| 1443 | if (ret == X86EMUL_PROPAGATE_FAULT) |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 1444 | emulate_pf(ctxt, addr, err); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1445 | |
| 1446 | return ret; |
| 1447 | } |
| 1448 | |
| 1449 | static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
| 1450 | struct x86_emulate_ops *ops, |
| 1451 | u16 selector, int seg) |
| 1452 | { |
| 1453 | struct desc_struct seg_desc; |
| 1454 | u8 dpl, rpl, cpl; |
| 1455 | unsigned err_vec = GP_VECTOR; |
| 1456 | u32 err_code = 0; |
| 1457 | bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */ |
| 1458 | int ret; |
| 1459 | |
| 1460 | memset(&seg_desc, 0, sizeof seg_desc); |
| 1461 | |
| 1462 | if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) |
| 1463 | || ctxt->mode == X86EMUL_MODE_REAL) { |
| 1464 | /* set real mode segment descriptor */ |
| 1465 | set_desc_base(&seg_desc, selector << 4); |
| 1466 | set_desc_limit(&seg_desc, 0xffff); |
| 1467 | seg_desc.type = 3; |
| 1468 | seg_desc.p = 1; |
| 1469 | seg_desc.s = 1; |
| 1470 | goto load; |
| 1471 | } |
| 1472 | |
| 1473 | /* NULL selector is not valid for TR, CS and SS */ |
| 1474 | if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR) |
| 1475 | && null_selector) |
| 1476 | goto exception; |
| 1477 | |
| 1478 | /* TR should be in GDT only */ |
| 1479 | if (seg == VCPU_SREG_TR && (selector & (1 << 2))) |
| 1480 | goto exception; |
| 1481 | |
| 1482 | if (null_selector) /* for NULL selector skip all following checks */ |
| 1483 | goto load; |
| 1484 | |
| 1485 | ret = read_segment_descriptor(ctxt, ops, selector, &seg_desc); |
| 1486 | if (ret != X86EMUL_CONTINUE) |
| 1487 | return ret; |
| 1488 | |
| 1489 | err_code = selector & 0xfffc; |
| 1490 | err_vec = GP_VECTOR; |
| 1491 | |
| 1492 | /* can't load system descriptor into segment selecor */ |
| 1493 | if (seg <= VCPU_SREG_GS && !seg_desc.s) |
| 1494 | goto exception; |
| 1495 | |
| 1496 | if (!seg_desc.p) { |
| 1497 | err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR; |
| 1498 | goto exception; |
| 1499 | } |
| 1500 | |
| 1501 | rpl = selector & 3; |
| 1502 | dpl = seg_desc.dpl; |
| 1503 | cpl = ops->cpl(ctxt->vcpu); |
| 1504 | |
| 1505 | switch (seg) { |
| 1506 | case VCPU_SREG_SS: |
| 1507 | /* |
| 1508 | * segment is not a writable data segment or segment |
| 1509 | * selector's RPL != CPL or segment selector's RPL != CPL |
| 1510 | */ |
| 1511 | if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl) |
| 1512 | goto exception; |
| 1513 | break; |
| 1514 | case VCPU_SREG_CS: |
| 1515 | if (!(seg_desc.type & 8)) |
| 1516 | goto exception; |
| 1517 | |
| 1518 | if (seg_desc.type & 4) { |
| 1519 | /* conforming */ |
| 1520 | if (dpl > cpl) |
| 1521 | goto exception; |
| 1522 | } else { |
| 1523 | /* nonconforming */ |
| 1524 | if (rpl > cpl || dpl != cpl) |
| 1525 | goto exception; |
| 1526 | } |
| 1527 | /* CS(RPL) <- CPL */ |
| 1528 | selector = (selector & 0xfffc) | cpl; |
| 1529 | break; |
| 1530 | case VCPU_SREG_TR: |
| 1531 | if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9)) |
| 1532 | goto exception; |
| 1533 | break; |
| 1534 | case VCPU_SREG_LDTR: |
| 1535 | if (seg_desc.s || seg_desc.type != 2) |
| 1536 | goto exception; |
| 1537 | break; |
| 1538 | default: /* DS, ES, FS, or GS */ |
| 1539 | /* |
| 1540 | * segment is not a data or readable code segment or |
| 1541 | * ((segment is a data or nonconforming code segment) |
| 1542 | * and (both RPL and CPL > DPL)) |
| 1543 | */ |
| 1544 | if ((seg_desc.type & 0xa) == 0x8 || |
| 1545 | (((seg_desc.type & 0xc) != 0xc) && |
| 1546 | (rpl > dpl && cpl > dpl))) |
| 1547 | goto exception; |
| 1548 | break; |
| 1549 | } |
| 1550 | |
| 1551 | if (seg_desc.s) { |
| 1552 | /* mark segment as accessed */ |
| 1553 | seg_desc.type |= 1; |
| 1554 | ret = write_segment_descriptor(ctxt, ops, selector, &seg_desc); |
| 1555 | if (ret != X86EMUL_CONTINUE) |
| 1556 | return ret; |
| 1557 | } |
| 1558 | load: |
| 1559 | ops->set_segment_selector(selector, seg, ctxt->vcpu); |
| 1560 | ops->set_cached_descriptor(&seg_desc, seg, ctxt->vcpu); |
| 1561 | return X86EMUL_CONTINUE; |
| 1562 | exception: |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 1563 | emulate_exception(ctxt, err_vec, err_code, true); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 1564 | return X86EMUL_PROPAGATE_FAULT; |
| 1565 | } |
| 1566 | |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1567 | static inline int writeback(struct x86_emulate_ctxt *ctxt, |
| 1568 | struct x86_emulate_ops *ops) |
| 1569 | { |
| 1570 | int rc; |
| 1571 | struct decode_cache *c = &ctxt->decode; |
| 1572 | u32 err; |
| 1573 | |
| 1574 | switch (c->dst.type) { |
| 1575 | case OP_REG: |
| 1576 | /* The 4-byte case *is* correct: |
| 1577 | * in 64-bit mode we zero-extend. |
| 1578 | */ |
| 1579 | switch (c->dst.bytes) { |
| 1580 | case 1: |
| 1581 | *(u8 *)c->dst.ptr = (u8)c->dst.val; |
| 1582 | break; |
| 1583 | case 2: |
| 1584 | *(u16 *)c->dst.ptr = (u16)c->dst.val; |
| 1585 | break; |
| 1586 | case 4: |
| 1587 | *c->dst.ptr = (u32)c->dst.val; |
| 1588 | break; /* 64b: zero-ext */ |
| 1589 | case 8: |
| 1590 | *c->dst.ptr = c->dst.val; |
| 1591 | break; |
| 1592 | } |
| 1593 | break; |
| 1594 | case OP_MEM: |
| 1595 | if (c->lock_prefix) |
| 1596 | rc = ops->cmpxchg_emulated( |
| 1597 | (unsigned long)c->dst.ptr, |
| 1598 | &c->dst.orig_val, |
| 1599 | &c->dst.val, |
| 1600 | c->dst.bytes, |
| 1601 | &err, |
| 1602 | ctxt->vcpu); |
| 1603 | else |
| 1604 | rc = ops->write_emulated( |
| 1605 | (unsigned long)c->dst.ptr, |
| 1606 | &c->dst.val, |
| 1607 | c->dst.bytes, |
| 1608 | &err, |
| 1609 | ctxt->vcpu); |
| 1610 | if (rc == X86EMUL_PROPAGATE_FAULT) |
| 1611 | emulate_pf(ctxt, |
| 1612 | (unsigned long)c->dst.ptr, err); |
| 1613 | if (rc != X86EMUL_CONTINUE) |
| 1614 | return rc; |
| 1615 | break; |
| 1616 | case OP_NONE: |
| 1617 | /* no writeback */ |
| 1618 | break; |
| 1619 | default: |
| 1620 | break; |
| 1621 | } |
| 1622 | return X86EMUL_CONTINUE; |
| 1623 | } |
| 1624 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1625 | static inline void emulate_push(struct x86_emulate_ctxt *ctxt, |
| 1626 | struct x86_emulate_ops *ops) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1627 | { |
| 1628 | struct decode_cache *c = &ctxt->decode; |
| 1629 | |
| 1630 | c->dst.type = OP_MEM; |
| 1631 | c->dst.bytes = c->op_bytes; |
| 1632 | c->dst.val = c->src.val; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 1633 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1634 | c->dst.ptr = (void *) register_address(c, ss_base(ctxt, ops), |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1635 | c->regs[VCPU_REGS_RSP]); |
| 1636 | } |
| 1637 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1638 | static int emulate_pop(struct x86_emulate_ctxt *ctxt, |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1639 | struct x86_emulate_ops *ops, |
| 1640 | void *dest, int len) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1641 | { |
| 1642 | struct decode_cache *c = &ctxt->decode; |
| 1643 | int rc; |
| 1644 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1645 | rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops), |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 1646 | c->regs[VCPU_REGS_RSP]), |
| 1647 | dest, len); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 1648 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1649 | return rc; |
| 1650 | |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 1651 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], len); |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1652 | return rc; |
| 1653 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1654 | |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1655 | static int emulate_popf(struct x86_emulate_ctxt *ctxt, |
| 1656 | struct x86_emulate_ops *ops, |
| 1657 | void *dest, int len) |
| 1658 | { |
| 1659 | int rc; |
| 1660 | unsigned long val, change_mask; |
| 1661 | int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 1662 | int cpl = ops->cpl(ctxt->vcpu); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1663 | |
| 1664 | rc = emulate_pop(ctxt, ops, &val, len); |
| 1665 | if (rc != X86EMUL_CONTINUE) |
| 1666 | return rc; |
| 1667 | |
| 1668 | change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF |
| 1669 | | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID; |
| 1670 | |
| 1671 | switch(ctxt->mode) { |
| 1672 | case X86EMUL_MODE_PROT64: |
| 1673 | case X86EMUL_MODE_PROT32: |
| 1674 | case X86EMUL_MODE_PROT16: |
| 1675 | if (cpl == 0) |
| 1676 | change_mask |= EFLG_IOPL; |
| 1677 | if (cpl <= iopl) |
| 1678 | change_mask |= EFLG_IF; |
| 1679 | break; |
| 1680 | case X86EMUL_MODE_VM86: |
| 1681 | if (iopl < 3) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 1682 | emulate_gp(ctxt, 0); |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 1683 | return X86EMUL_PROPAGATE_FAULT; |
| 1684 | } |
| 1685 | change_mask |= EFLG_IF; |
| 1686 | break; |
| 1687 | default: /* real mode */ |
| 1688 | change_mask |= (EFLG_IOPL | EFLG_IF); |
| 1689 | break; |
| 1690 | } |
| 1691 | |
| 1692 | *(unsigned long *)dest = |
| 1693 | (ctxt->eflags & ~change_mask) | (val & change_mask); |
| 1694 | |
| 1695 | return rc; |
| 1696 | } |
| 1697 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1698 | static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt, |
| 1699 | struct x86_emulate_ops *ops, int seg) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1700 | { |
| 1701 | struct decode_cache *c = &ctxt->decode; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1702 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1703 | c->src.val = ops->get_segment_selector(seg, ctxt->vcpu); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1704 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1705 | emulate_push(ctxt, ops); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt, |
| 1709 | struct x86_emulate_ops *ops, int seg) |
| 1710 | { |
| 1711 | struct decode_cache *c = &ctxt->decode; |
| 1712 | unsigned long selector; |
| 1713 | int rc; |
| 1714 | |
| 1715 | rc = emulate_pop(ctxt, ops, &selector, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1716 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1717 | return rc; |
| 1718 | |
Gleb Natapov | 2e87302 | 2010-03-18 15:20:18 +0200 | [diff] [blame] | 1719 | rc = load_segment_descriptor(ctxt, ops, (u16)selector, seg); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 1720 | return rc; |
| 1721 | } |
| 1722 | |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1723 | static int emulate_pusha(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1724 | struct x86_emulate_ops *ops) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1725 | { |
| 1726 | struct decode_cache *c = &ctxt->decode; |
| 1727 | unsigned long old_esp = c->regs[VCPU_REGS_RSP]; |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1728 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1729 | int reg = VCPU_REGS_RAX; |
| 1730 | |
| 1731 | while (reg <= VCPU_REGS_RDI) { |
| 1732 | (reg == VCPU_REGS_RSP) ? |
| 1733 | (c->src.val = old_esp) : (c->src.val = c->regs[reg]); |
| 1734 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1735 | emulate_push(ctxt, ops); |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1736 | |
| 1737 | rc = writeback(ctxt, ops); |
| 1738 | if (rc != X86EMUL_CONTINUE) |
| 1739 | return rc; |
| 1740 | |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1741 | ++reg; |
| 1742 | } |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 1743 | |
| 1744 | /* Disable writeback. */ |
| 1745 | c->dst.type = OP_NONE; |
| 1746 | |
| 1747 | return rc; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1748 | } |
| 1749 | |
| 1750 | static int emulate_popa(struct x86_emulate_ctxt *ctxt, |
| 1751 | struct x86_emulate_ops *ops) |
| 1752 | { |
| 1753 | struct decode_cache *c = &ctxt->decode; |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1754 | int rc = X86EMUL_CONTINUE; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1755 | int reg = VCPU_REGS_RDI; |
| 1756 | |
| 1757 | while (reg >= VCPU_REGS_RAX) { |
| 1758 | if (reg == VCPU_REGS_RSP) { |
| 1759 | register_address_increment(c, &c->regs[VCPU_REGS_RSP], |
| 1760 | c->op_bytes); |
| 1761 | --reg; |
| 1762 | } |
| 1763 | |
| 1764 | rc = emulate_pop(ctxt, ops, &c->regs[reg], c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1765 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 1766 | break; |
| 1767 | --reg; |
| 1768 | } |
| 1769 | return rc; |
| 1770 | } |
| 1771 | |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 1772 | static int emulate_iret_real(struct x86_emulate_ctxt *ctxt, |
| 1773 | struct x86_emulate_ops *ops) |
| 1774 | { |
| 1775 | struct decode_cache *c = &ctxt->decode; |
| 1776 | int rc = X86EMUL_CONTINUE; |
| 1777 | unsigned long temp_eip = 0; |
| 1778 | unsigned long temp_eflags = 0; |
| 1779 | unsigned long cs = 0; |
| 1780 | unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF | |
| 1781 | EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF | |
| 1782 | EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */ |
| 1783 | unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP; |
| 1784 | |
| 1785 | /* TODO: Add stack limit check */ |
| 1786 | |
| 1787 | rc = emulate_pop(ctxt, ops, &temp_eip, c->op_bytes); |
| 1788 | |
| 1789 | if (rc != X86EMUL_CONTINUE) |
| 1790 | return rc; |
| 1791 | |
| 1792 | if (temp_eip & ~0xffff) { |
| 1793 | emulate_gp(ctxt, 0); |
| 1794 | return X86EMUL_PROPAGATE_FAULT; |
| 1795 | } |
| 1796 | |
| 1797 | rc = emulate_pop(ctxt, ops, &cs, c->op_bytes); |
| 1798 | |
| 1799 | if (rc != X86EMUL_CONTINUE) |
| 1800 | return rc; |
| 1801 | |
| 1802 | rc = emulate_pop(ctxt, ops, &temp_eflags, c->op_bytes); |
| 1803 | |
| 1804 | if (rc != X86EMUL_CONTINUE) |
| 1805 | return rc; |
| 1806 | |
| 1807 | rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS); |
| 1808 | |
| 1809 | if (rc != X86EMUL_CONTINUE) |
| 1810 | return rc; |
| 1811 | |
| 1812 | c->eip = temp_eip; |
| 1813 | |
| 1814 | |
| 1815 | if (c->op_bytes == 4) |
| 1816 | ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask)); |
| 1817 | else if (c->op_bytes == 2) { |
| 1818 | ctxt->eflags &= ~0xffff; |
| 1819 | ctxt->eflags |= temp_eflags; |
| 1820 | } |
| 1821 | |
| 1822 | ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */ |
| 1823 | ctxt->eflags |= EFLG_RESERVED_ONE_MASK; |
| 1824 | |
| 1825 | return rc; |
| 1826 | } |
| 1827 | |
| 1828 | static inline int emulate_iret(struct x86_emulate_ctxt *ctxt, |
| 1829 | struct x86_emulate_ops* ops) |
| 1830 | { |
| 1831 | switch(ctxt->mode) { |
| 1832 | case X86EMUL_MODE_REAL: |
| 1833 | return emulate_iret_real(ctxt, ops); |
| 1834 | case X86EMUL_MODE_VM86: |
| 1835 | case X86EMUL_MODE_PROT16: |
| 1836 | case X86EMUL_MODE_PROT32: |
| 1837 | case X86EMUL_MODE_PROT64: |
| 1838 | default: |
| 1839 | /* iret from protected mode unimplemented yet */ |
| 1840 | return X86EMUL_UNHANDLEABLE; |
| 1841 | } |
| 1842 | } |
| 1843 | |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1844 | static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt, |
| 1845 | struct x86_emulate_ops *ops) |
| 1846 | { |
| 1847 | struct decode_cache *c = &ctxt->decode; |
Avi Kivity | faa5a3a | 2008-11-27 17:36:41 +0200 | [diff] [blame] | 1848 | |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1849 | return emulate_pop(ctxt, ops, &c->dst.val, c->dst.bytes); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1850 | } |
| 1851 | |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1852 | static inline void emulate_grp2(struct x86_emulate_ctxt *ctxt) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1853 | { |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1854 | struct decode_cache *c = &ctxt->decode; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1855 | switch (c->modrm_reg) { |
| 1856 | case 0: /* rol */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1857 | emulate_2op_SrcB("rol", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1858 | break; |
| 1859 | case 1: /* ror */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1860 | emulate_2op_SrcB("ror", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1861 | break; |
| 1862 | case 2: /* rcl */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1863 | emulate_2op_SrcB("rcl", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1864 | break; |
| 1865 | case 3: /* rcr */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1866 | emulate_2op_SrcB("rcr", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1867 | break; |
| 1868 | case 4: /* sal/shl */ |
| 1869 | case 6: /* sal/shl */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1870 | emulate_2op_SrcB("sal", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1871 | break; |
| 1872 | case 5: /* shr */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1873 | emulate_2op_SrcB("shr", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1874 | break; |
| 1875 | case 7: /* sar */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1876 | emulate_2op_SrcB("sar", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1877 | break; |
| 1878 | } |
| 1879 | } |
| 1880 | |
| 1881 | static inline int emulate_grp3(struct x86_emulate_ctxt *ctxt, |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1882 | struct x86_emulate_ops *ops) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1883 | { |
| 1884 | struct decode_cache *c = &ctxt->decode; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1885 | |
| 1886 | switch (c->modrm_reg) { |
| 1887 | case 0 ... 1: /* test */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1888 | emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1889 | break; |
| 1890 | case 2: /* not */ |
| 1891 | c->dst.val = ~c->dst.val; |
| 1892 | break; |
| 1893 | case 3: /* neg */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1894 | emulate_1op("neg", c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1895 | break; |
| 1896 | default: |
Gleb Natapov | aca06a8 | 2010-03-18 15:20:15 +0200 | [diff] [blame] | 1897 | return 0; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1898 | } |
Gleb Natapov | aca06a8 | 2010-03-18 15:20:15 +0200 | [diff] [blame] | 1899 | return 1; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt, |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 1903 | struct x86_emulate_ops *ops) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1904 | { |
| 1905 | struct decode_cache *c = &ctxt->decode; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1906 | |
| 1907 | switch (c->modrm_reg) { |
| 1908 | case 0: /* inc */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1909 | emulate_1op("inc", c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1910 | break; |
| 1911 | case 1: /* dec */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1912 | emulate_1op("dec", c->dst, ctxt->eflags); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1913 | break; |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 1914 | case 2: /* call near abs */ { |
| 1915 | long int old_eip; |
| 1916 | old_eip = c->eip; |
| 1917 | c->eip = c->src.val; |
| 1918 | c->src.val = old_eip; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1919 | emulate_push(ctxt, ops); |
Mohammed Gamal | d19292e | 2008-09-08 21:47:19 +0300 | [diff] [blame] | 1920 | break; |
| 1921 | } |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1922 | case 4: /* jmp abs */ |
Avi Kivity | fd60754 | 2008-01-18 13:12:26 +0200 | [diff] [blame] | 1923 | c->eip = c->src.val; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1924 | break; |
| 1925 | case 6: /* push */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1926 | emulate_push(ctxt, ops); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1927 | break; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1928 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1929 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1930 | } |
| 1931 | |
| 1932 | static inline int emulate_grp9(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 1933 | struct x86_emulate_ops *ops) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1934 | { |
| 1935 | struct decode_cache *c = &ctxt->decode; |
Avi Kivity | 16518d5 | 2010-08-26 14:31:30 +0300 | [diff] [blame] | 1936 | u64 old = c->dst.orig_val64; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1937 | |
| 1938 | if (((u32) (old >> 0) != (u32) c->regs[VCPU_REGS_RAX]) || |
| 1939 | ((u32) (old >> 32) != (u32) c->regs[VCPU_REGS_RDX])) { |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1940 | c->regs[VCPU_REGS_RAX] = (u32) (old >> 0); |
| 1941 | c->regs[VCPU_REGS_RDX] = (u32) (old >> 32); |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1942 | ctxt->eflags &= ~EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1943 | } else { |
Avi Kivity | 16518d5 | 2010-08-26 14:31:30 +0300 | [diff] [blame] | 1944 | c->dst.val64 = ((u64)c->regs[VCPU_REGS_RCX] << 32) | |
| 1945 | (u32) c->regs[VCPU_REGS_RBX]; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1946 | |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 1947 | ctxt->eflags |= EFLG_ZF; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1948 | } |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1949 | return X86EMUL_CONTINUE; |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 1950 | } |
| 1951 | |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1952 | static int emulate_ret_far(struct x86_emulate_ctxt *ctxt, |
| 1953 | struct x86_emulate_ops *ops) |
| 1954 | { |
| 1955 | struct decode_cache *c = &ctxt->decode; |
| 1956 | int rc; |
| 1957 | unsigned long cs; |
| 1958 | |
| 1959 | rc = emulate_pop(ctxt, ops, &c->eip, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1960 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1961 | return rc; |
| 1962 | if (c->op_bytes == 4) |
| 1963 | c->eip = (u32)c->eip; |
| 1964 | rc = emulate_pop(ctxt, ops, &cs, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 1965 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1966 | return rc; |
Gleb Natapov | 2e87302 | 2010-03-18 15:20:18 +0200 | [diff] [blame] | 1967 | rc = load_segment_descriptor(ctxt, ops, (u16)cs, VCPU_SREG_CS); |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 1968 | return rc; |
| 1969 | } |
| 1970 | |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1971 | static inline void |
| 1972 | setup_syscalls_segments(struct x86_emulate_ctxt *ctxt, |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1973 | struct x86_emulate_ops *ops, struct desc_struct *cs, |
| 1974 | struct desc_struct *ss) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1975 | { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1976 | memset(cs, 0, sizeof(struct desc_struct)); |
| 1977 | ops->get_cached_descriptor(cs, VCPU_SREG_CS, ctxt->vcpu); |
| 1978 | memset(ss, 0, sizeof(struct desc_struct)); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1979 | |
| 1980 | cs->l = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1981 | set_desc_base(cs, 0); /* flat segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1982 | cs->g = 1; /* 4kb granularity */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1983 | set_desc_limit(cs, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1984 | cs->type = 0x0b; /* Read, Execute, Accessed */ |
| 1985 | cs->s = 1; |
| 1986 | cs->dpl = 0; /* will be adjusted later */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1987 | cs->p = 1; |
| 1988 | cs->d = 1; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1989 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1990 | set_desc_base(ss, 0); /* flat segment */ |
| 1991 | set_desc_limit(ss, 0xfffff); /* 4GB limit */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1992 | ss->g = 1; /* 4kb granularity */ |
| 1993 | ss->s = 1; |
| 1994 | ss->type = 0x03; /* Read/Write, Accessed */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1995 | ss->d = 1; /* 32bit stack segment */ |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1996 | ss->dpl = 0; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 1997 | ss->p = 1; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 1998 | } |
| 1999 | |
| 2000 | static int |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2001 | emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2002 | { |
| 2003 | struct decode_cache *c = &ctxt->decode; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2004 | struct desc_struct cs, ss; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2005 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2006 | u16 cs_sel, ss_sel; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2007 | |
| 2008 | /* syscall is not available in real mode */ |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 2009 | if (ctxt->mode == X86EMUL_MODE_REAL || |
| 2010 | ctxt->mode == X86EMUL_MODE_VM86) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2011 | emulate_ud(ctxt); |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 2012 | return X86EMUL_PROPAGATE_FAULT; |
| 2013 | } |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2014 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2015 | setup_syscalls_segments(ctxt, ops, &cs, &ss); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2016 | |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2017 | ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2018 | msr_data >>= 32; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2019 | cs_sel = (u16)(msr_data & 0xfffc); |
| 2020 | ss_sel = (u16)(msr_data + 8); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2021 | |
| 2022 | if (is_long_mode(ctxt->vcpu)) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2023 | cs.d = 0; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2024 | cs.l = 1; |
| 2025 | } |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2026 | ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu); |
| 2027 | ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu); |
| 2028 | ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu); |
| 2029 | ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2030 | |
| 2031 | c->regs[VCPU_REGS_RCX] = c->eip; |
| 2032 | if (is_long_mode(ctxt->vcpu)) { |
| 2033 | #ifdef CONFIG_X86_64 |
| 2034 | c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF; |
| 2035 | |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2036 | ops->get_msr(ctxt->vcpu, |
| 2037 | ctxt->mode == X86EMUL_MODE_PROT64 ? |
| 2038 | MSR_LSTAR : MSR_CSTAR, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2039 | c->eip = msr_data; |
| 2040 | |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2041 | ops->get_msr(ctxt->vcpu, MSR_SYSCALL_MASK, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2042 | ctxt->eflags &= ~(msr_data | EFLG_RF); |
| 2043 | #endif |
| 2044 | } else { |
| 2045 | /* legacy mode */ |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2046 | ops->get_msr(ctxt->vcpu, MSR_STAR, &msr_data); |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2047 | c->eip = (u32)msr_data; |
| 2048 | |
| 2049 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
| 2050 | } |
| 2051 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2052 | return X86EMUL_CONTINUE; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 2053 | } |
| 2054 | |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2055 | static int |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2056 | emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2057 | { |
| 2058 | struct decode_cache *c = &ctxt->decode; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2059 | struct desc_struct cs, ss; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2060 | u64 msr_data; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2061 | u16 cs_sel, ss_sel; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2062 | |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2063 | /* inject #GP if in real mode */ |
| 2064 | if (ctxt->mode == X86EMUL_MODE_REAL) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2065 | emulate_gp(ctxt, 0); |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 2066 | return X86EMUL_PROPAGATE_FAULT; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | /* XXX sysenter/sysexit have not been tested in 64bit mode. |
| 2070 | * Therefore, we inject an #UD. |
| 2071 | */ |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 2072 | if (ctxt->mode == X86EMUL_MODE_PROT64) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2073 | emulate_ud(ctxt); |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 2074 | return X86EMUL_PROPAGATE_FAULT; |
| 2075 | } |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2076 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2077 | setup_syscalls_segments(ctxt, ops, &cs, &ss); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2078 | |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2079 | ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2080 | switch (ctxt->mode) { |
| 2081 | case X86EMUL_MODE_PROT32: |
| 2082 | if ((msr_data & 0xfffc) == 0x0) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2083 | emulate_gp(ctxt, 0); |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2084 | return X86EMUL_PROPAGATE_FAULT; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2085 | } |
| 2086 | break; |
| 2087 | case X86EMUL_MODE_PROT64: |
| 2088 | if (msr_data == 0x0) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2089 | emulate_gp(ctxt, 0); |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2090 | return X86EMUL_PROPAGATE_FAULT; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2091 | } |
| 2092 | break; |
| 2093 | } |
| 2094 | |
| 2095 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2096 | cs_sel = (u16)msr_data; |
| 2097 | cs_sel &= ~SELECTOR_RPL_MASK; |
| 2098 | ss_sel = cs_sel + 8; |
| 2099 | ss_sel &= ~SELECTOR_RPL_MASK; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2100 | if (ctxt->mode == X86EMUL_MODE_PROT64 |
| 2101 | || is_long_mode(ctxt->vcpu)) { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2102 | cs.d = 0; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2103 | cs.l = 1; |
| 2104 | } |
| 2105 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2106 | ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu); |
| 2107 | ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu); |
| 2108 | ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu); |
| 2109 | ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2110 | |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2111 | ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_EIP, &msr_data); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2112 | c->eip = msr_data; |
| 2113 | |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2114 | ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_ESP, &msr_data); |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2115 | c->regs[VCPU_REGS_RSP] = msr_data; |
| 2116 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2117 | return X86EMUL_CONTINUE; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 2118 | } |
| 2119 | |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2120 | static int |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2121 | emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2122 | { |
| 2123 | struct decode_cache *c = &ctxt->decode; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2124 | struct desc_struct cs, ss; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2125 | u64 msr_data; |
| 2126 | int usermode; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2127 | u16 cs_sel, ss_sel; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2128 | |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 2129 | /* inject #GP if in real mode or Virtual 8086 mode */ |
| 2130 | if (ctxt->mode == X86EMUL_MODE_REAL || |
| 2131 | ctxt->mode == X86EMUL_MODE_VM86) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2132 | emulate_gp(ctxt, 0); |
Gleb Natapov | 2e901c4 | 2010-03-18 15:20:12 +0200 | [diff] [blame] | 2133 | return X86EMUL_PROPAGATE_FAULT; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2134 | } |
| 2135 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2136 | setup_syscalls_segments(ctxt, ops, &cs, &ss); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2137 | |
| 2138 | if ((c->rex_prefix & 0x8) != 0x0) |
| 2139 | usermode = X86EMUL_MODE_PROT64; |
| 2140 | else |
| 2141 | usermode = X86EMUL_MODE_PROT32; |
| 2142 | |
| 2143 | cs.dpl = 3; |
| 2144 | ss.dpl = 3; |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 2145 | ops->get_msr(ctxt->vcpu, MSR_IA32_SYSENTER_CS, &msr_data); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2146 | switch (usermode) { |
| 2147 | case X86EMUL_MODE_PROT32: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2148 | cs_sel = (u16)(msr_data + 16); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2149 | if ((msr_data & 0xfffc) == 0x0) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2150 | emulate_gp(ctxt, 0); |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2151 | return X86EMUL_PROPAGATE_FAULT; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2152 | } |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2153 | ss_sel = (u16)(msr_data + 24); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2154 | break; |
| 2155 | case X86EMUL_MODE_PROT64: |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2156 | cs_sel = (u16)(msr_data + 32); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2157 | if (msr_data == 0x0) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2158 | emulate_gp(ctxt, 0); |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2159 | return X86EMUL_PROPAGATE_FAULT; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2160 | } |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2161 | ss_sel = cs_sel + 8; |
| 2162 | cs.d = 0; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2163 | cs.l = 1; |
| 2164 | break; |
| 2165 | } |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2166 | cs_sel |= SELECTOR_RPL_MASK; |
| 2167 | ss_sel |= SELECTOR_RPL_MASK; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2168 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2169 | ops->set_cached_descriptor(&cs, VCPU_SREG_CS, ctxt->vcpu); |
| 2170 | ops->set_segment_selector(cs_sel, VCPU_SREG_CS, ctxt->vcpu); |
| 2171 | ops->set_cached_descriptor(&ss, VCPU_SREG_SS, ctxt->vcpu); |
| 2172 | ops->set_segment_selector(ss_sel, VCPU_SREG_SS, ctxt->vcpu); |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2173 | |
Gleb Natapov | bdb475a | 2010-04-28 19:15:41 +0300 | [diff] [blame] | 2174 | c->eip = c->regs[VCPU_REGS_RDX]; |
| 2175 | c->regs[VCPU_REGS_RSP] = c->regs[VCPU_REGS_RCX]; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2176 | |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 2177 | return X86EMUL_CONTINUE; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 2178 | } |
| 2179 | |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 2180 | static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt, |
| 2181 | struct x86_emulate_ops *ops) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2182 | { |
| 2183 | int iopl; |
| 2184 | if (ctxt->mode == X86EMUL_MODE_REAL) |
| 2185 | return false; |
| 2186 | if (ctxt->mode == X86EMUL_MODE_VM86) |
| 2187 | return true; |
| 2188 | iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 2189 | return ops->cpl(ctxt->vcpu) > iopl; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2190 | } |
| 2191 | |
| 2192 | static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt, |
| 2193 | struct x86_emulate_ops *ops, |
| 2194 | u16 port, u16 len) |
| 2195 | { |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2196 | struct desc_struct tr_seg; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2197 | int r; |
| 2198 | u16 io_bitmap_ptr; |
| 2199 | u8 perm, bit_idx = port & 0x7; |
| 2200 | unsigned mask = (1 << len) - 1; |
| 2201 | |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2202 | ops->get_cached_descriptor(&tr_seg, VCPU_SREG_TR, ctxt->vcpu); |
| 2203 | if (!tr_seg.p) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2204 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2205 | if (desc_limit_scaled(&tr_seg) < 103) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2206 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2207 | r = ops->read_std(get_desc_base(&tr_seg) + 102, &io_bitmap_ptr, 2, |
| 2208 | ctxt->vcpu, NULL); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2209 | if (r != X86EMUL_CONTINUE) |
| 2210 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2211 | if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2212 | return false; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2213 | r = ops->read_std(get_desc_base(&tr_seg) + io_bitmap_ptr + port/8, |
| 2214 | &perm, 1, ctxt->vcpu, NULL); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2215 | if (r != X86EMUL_CONTINUE) |
| 2216 | return false; |
| 2217 | if ((perm >> bit_idx) & mask) |
| 2218 | return false; |
| 2219 | return true; |
| 2220 | } |
| 2221 | |
| 2222 | static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt, |
| 2223 | struct x86_emulate_ops *ops, |
| 2224 | u16 port, u16 len) |
| 2225 | { |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 2226 | if (emulator_bad_iopl(ctxt, ops)) |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2227 | if (!emulator_io_port_access_allowed(ctxt, ops, port, len)) |
| 2228 | return false; |
| 2229 | return true; |
| 2230 | } |
| 2231 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2232 | static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt, |
| 2233 | struct x86_emulate_ops *ops, |
| 2234 | struct tss_segment_16 *tss) |
| 2235 | { |
| 2236 | struct decode_cache *c = &ctxt->decode; |
| 2237 | |
| 2238 | tss->ip = c->eip; |
| 2239 | tss->flag = ctxt->eflags; |
| 2240 | tss->ax = c->regs[VCPU_REGS_RAX]; |
| 2241 | tss->cx = c->regs[VCPU_REGS_RCX]; |
| 2242 | tss->dx = c->regs[VCPU_REGS_RDX]; |
| 2243 | tss->bx = c->regs[VCPU_REGS_RBX]; |
| 2244 | tss->sp = c->regs[VCPU_REGS_RSP]; |
| 2245 | tss->bp = c->regs[VCPU_REGS_RBP]; |
| 2246 | tss->si = c->regs[VCPU_REGS_RSI]; |
| 2247 | tss->di = c->regs[VCPU_REGS_RDI]; |
| 2248 | |
| 2249 | tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu); |
| 2250 | tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu); |
| 2251 | tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu); |
| 2252 | tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu); |
| 2253 | tss->ldt = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu); |
| 2254 | } |
| 2255 | |
| 2256 | static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt, |
| 2257 | struct x86_emulate_ops *ops, |
| 2258 | struct tss_segment_16 *tss) |
| 2259 | { |
| 2260 | struct decode_cache *c = &ctxt->decode; |
| 2261 | int ret; |
| 2262 | |
| 2263 | c->eip = tss->ip; |
| 2264 | ctxt->eflags = tss->flag | 2; |
| 2265 | c->regs[VCPU_REGS_RAX] = tss->ax; |
| 2266 | c->regs[VCPU_REGS_RCX] = tss->cx; |
| 2267 | c->regs[VCPU_REGS_RDX] = tss->dx; |
| 2268 | c->regs[VCPU_REGS_RBX] = tss->bx; |
| 2269 | c->regs[VCPU_REGS_RSP] = tss->sp; |
| 2270 | c->regs[VCPU_REGS_RBP] = tss->bp; |
| 2271 | c->regs[VCPU_REGS_RSI] = tss->si; |
| 2272 | c->regs[VCPU_REGS_RDI] = tss->di; |
| 2273 | |
| 2274 | /* |
| 2275 | * SDM says that segment selectors are loaded before segment |
| 2276 | * descriptors |
| 2277 | */ |
| 2278 | ops->set_segment_selector(tss->ldt, VCPU_SREG_LDTR, ctxt->vcpu); |
| 2279 | ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu); |
| 2280 | ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu); |
| 2281 | ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu); |
| 2282 | ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu); |
| 2283 | |
| 2284 | /* |
| 2285 | * Now load segment descriptors. If fault happenes at this stage |
| 2286 | * it is handled in a context of new task |
| 2287 | */ |
| 2288 | ret = load_segment_descriptor(ctxt, ops, tss->ldt, VCPU_SREG_LDTR); |
| 2289 | if (ret != X86EMUL_CONTINUE) |
| 2290 | return ret; |
| 2291 | ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES); |
| 2292 | if (ret != X86EMUL_CONTINUE) |
| 2293 | return ret; |
| 2294 | ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS); |
| 2295 | if (ret != X86EMUL_CONTINUE) |
| 2296 | return ret; |
| 2297 | ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS); |
| 2298 | if (ret != X86EMUL_CONTINUE) |
| 2299 | return ret; |
| 2300 | ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS); |
| 2301 | if (ret != X86EMUL_CONTINUE) |
| 2302 | return ret; |
| 2303 | |
| 2304 | return X86EMUL_CONTINUE; |
| 2305 | } |
| 2306 | |
| 2307 | static int task_switch_16(struct x86_emulate_ctxt *ctxt, |
| 2308 | struct x86_emulate_ops *ops, |
| 2309 | u16 tss_selector, u16 old_tss_sel, |
| 2310 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2311 | { |
| 2312 | struct tss_segment_16 tss_seg; |
| 2313 | int ret; |
| 2314 | u32 err, new_tss_base = get_desc_base(new_desc); |
| 2315 | |
| 2316 | ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu, |
| 2317 | &err); |
| 2318 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
| 2319 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2320 | emulate_pf(ctxt, old_tss_base, err); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2321 | return ret; |
| 2322 | } |
| 2323 | |
| 2324 | save_state_to_tss16(ctxt, ops, &tss_seg); |
| 2325 | |
| 2326 | ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu, |
| 2327 | &err); |
| 2328 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
| 2329 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2330 | emulate_pf(ctxt, old_tss_base, err); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2331 | return ret; |
| 2332 | } |
| 2333 | |
| 2334 | ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu, |
| 2335 | &err); |
| 2336 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
| 2337 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2338 | emulate_pf(ctxt, new_tss_base, err); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2339 | return ret; |
| 2340 | } |
| 2341 | |
| 2342 | if (old_tss_sel != 0xffff) { |
| 2343 | tss_seg.prev_task_link = old_tss_sel; |
| 2344 | |
| 2345 | ret = ops->write_std(new_tss_base, |
| 2346 | &tss_seg.prev_task_link, |
| 2347 | sizeof tss_seg.prev_task_link, |
| 2348 | ctxt->vcpu, &err); |
| 2349 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
| 2350 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2351 | emulate_pf(ctxt, new_tss_base, err); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2352 | return ret; |
| 2353 | } |
| 2354 | } |
| 2355 | |
| 2356 | return load_state_from_tss16(ctxt, ops, &tss_seg); |
| 2357 | } |
| 2358 | |
| 2359 | static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt, |
| 2360 | struct x86_emulate_ops *ops, |
| 2361 | struct tss_segment_32 *tss) |
| 2362 | { |
| 2363 | struct decode_cache *c = &ctxt->decode; |
| 2364 | |
| 2365 | tss->cr3 = ops->get_cr(3, ctxt->vcpu); |
| 2366 | tss->eip = c->eip; |
| 2367 | tss->eflags = ctxt->eflags; |
| 2368 | tss->eax = c->regs[VCPU_REGS_RAX]; |
| 2369 | tss->ecx = c->regs[VCPU_REGS_RCX]; |
| 2370 | tss->edx = c->regs[VCPU_REGS_RDX]; |
| 2371 | tss->ebx = c->regs[VCPU_REGS_RBX]; |
| 2372 | tss->esp = c->regs[VCPU_REGS_RSP]; |
| 2373 | tss->ebp = c->regs[VCPU_REGS_RBP]; |
| 2374 | tss->esi = c->regs[VCPU_REGS_RSI]; |
| 2375 | tss->edi = c->regs[VCPU_REGS_RDI]; |
| 2376 | |
| 2377 | tss->es = ops->get_segment_selector(VCPU_SREG_ES, ctxt->vcpu); |
| 2378 | tss->cs = ops->get_segment_selector(VCPU_SREG_CS, ctxt->vcpu); |
| 2379 | tss->ss = ops->get_segment_selector(VCPU_SREG_SS, ctxt->vcpu); |
| 2380 | tss->ds = ops->get_segment_selector(VCPU_SREG_DS, ctxt->vcpu); |
| 2381 | tss->fs = ops->get_segment_selector(VCPU_SREG_FS, ctxt->vcpu); |
| 2382 | tss->gs = ops->get_segment_selector(VCPU_SREG_GS, ctxt->vcpu); |
| 2383 | tss->ldt_selector = ops->get_segment_selector(VCPU_SREG_LDTR, ctxt->vcpu); |
| 2384 | } |
| 2385 | |
| 2386 | static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, |
| 2387 | struct x86_emulate_ops *ops, |
| 2388 | struct tss_segment_32 *tss) |
| 2389 | { |
| 2390 | struct decode_cache *c = &ctxt->decode; |
| 2391 | int ret; |
| 2392 | |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 2393 | if (ops->set_cr(3, tss->cr3, ctxt->vcpu)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2394 | emulate_gp(ctxt, 0); |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 2395 | return X86EMUL_PROPAGATE_FAULT; |
| 2396 | } |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2397 | c->eip = tss->eip; |
| 2398 | ctxt->eflags = tss->eflags | 2; |
| 2399 | c->regs[VCPU_REGS_RAX] = tss->eax; |
| 2400 | c->regs[VCPU_REGS_RCX] = tss->ecx; |
| 2401 | c->regs[VCPU_REGS_RDX] = tss->edx; |
| 2402 | c->regs[VCPU_REGS_RBX] = tss->ebx; |
| 2403 | c->regs[VCPU_REGS_RSP] = tss->esp; |
| 2404 | c->regs[VCPU_REGS_RBP] = tss->ebp; |
| 2405 | c->regs[VCPU_REGS_RSI] = tss->esi; |
| 2406 | c->regs[VCPU_REGS_RDI] = tss->edi; |
| 2407 | |
| 2408 | /* |
| 2409 | * SDM says that segment selectors are loaded before segment |
| 2410 | * descriptors |
| 2411 | */ |
| 2412 | ops->set_segment_selector(tss->ldt_selector, VCPU_SREG_LDTR, ctxt->vcpu); |
| 2413 | ops->set_segment_selector(tss->es, VCPU_SREG_ES, ctxt->vcpu); |
| 2414 | ops->set_segment_selector(tss->cs, VCPU_SREG_CS, ctxt->vcpu); |
| 2415 | ops->set_segment_selector(tss->ss, VCPU_SREG_SS, ctxt->vcpu); |
| 2416 | ops->set_segment_selector(tss->ds, VCPU_SREG_DS, ctxt->vcpu); |
| 2417 | ops->set_segment_selector(tss->fs, VCPU_SREG_FS, ctxt->vcpu); |
| 2418 | ops->set_segment_selector(tss->gs, VCPU_SREG_GS, ctxt->vcpu); |
| 2419 | |
| 2420 | /* |
| 2421 | * Now load segment descriptors. If fault happenes at this stage |
| 2422 | * it is handled in a context of new task |
| 2423 | */ |
| 2424 | ret = load_segment_descriptor(ctxt, ops, tss->ldt_selector, VCPU_SREG_LDTR); |
| 2425 | if (ret != X86EMUL_CONTINUE) |
| 2426 | return ret; |
| 2427 | ret = load_segment_descriptor(ctxt, ops, tss->es, VCPU_SREG_ES); |
| 2428 | if (ret != X86EMUL_CONTINUE) |
| 2429 | return ret; |
| 2430 | ret = load_segment_descriptor(ctxt, ops, tss->cs, VCPU_SREG_CS); |
| 2431 | if (ret != X86EMUL_CONTINUE) |
| 2432 | return ret; |
| 2433 | ret = load_segment_descriptor(ctxt, ops, tss->ss, VCPU_SREG_SS); |
| 2434 | if (ret != X86EMUL_CONTINUE) |
| 2435 | return ret; |
| 2436 | ret = load_segment_descriptor(ctxt, ops, tss->ds, VCPU_SREG_DS); |
| 2437 | if (ret != X86EMUL_CONTINUE) |
| 2438 | return ret; |
| 2439 | ret = load_segment_descriptor(ctxt, ops, tss->fs, VCPU_SREG_FS); |
| 2440 | if (ret != X86EMUL_CONTINUE) |
| 2441 | return ret; |
| 2442 | ret = load_segment_descriptor(ctxt, ops, tss->gs, VCPU_SREG_GS); |
| 2443 | if (ret != X86EMUL_CONTINUE) |
| 2444 | return ret; |
| 2445 | |
| 2446 | return X86EMUL_CONTINUE; |
| 2447 | } |
| 2448 | |
| 2449 | static int task_switch_32(struct x86_emulate_ctxt *ctxt, |
| 2450 | struct x86_emulate_ops *ops, |
| 2451 | u16 tss_selector, u16 old_tss_sel, |
| 2452 | ulong old_tss_base, struct desc_struct *new_desc) |
| 2453 | { |
| 2454 | struct tss_segment_32 tss_seg; |
| 2455 | int ret; |
| 2456 | u32 err, new_tss_base = get_desc_base(new_desc); |
| 2457 | |
| 2458 | ret = ops->read_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu, |
| 2459 | &err); |
| 2460 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
| 2461 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2462 | emulate_pf(ctxt, old_tss_base, err); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2463 | return ret; |
| 2464 | } |
| 2465 | |
| 2466 | save_state_to_tss32(ctxt, ops, &tss_seg); |
| 2467 | |
| 2468 | ret = ops->write_std(old_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu, |
| 2469 | &err); |
| 2470 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
| 2471 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2472 | emulate_pf(ctxt, old_tss_base, err); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2473 | return ret; |
| 2474 | } |
| 2475 | |
| 2476 | ret = ops->read_std(new_tss_base, &tss_seg, sizeof tss_seg, ctxt->vcpu, |
| 2477 | &err); |
| 2478 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
| 2479 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2480 | emulate_pf(ctxt, new_tss_base, err); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2481 | return ret; |
| 2482 | } |
| 2483 | |
| 2484 | if (old_tss_sel != 0xffff) { |
| 2485 | tss_seg.prev_task_link = old_tss_sel; |
| 2486 | |
| 2487 | ret = ops->write_std(new_tss_base, |
| 2488 | &tss_seg.prev_task_link, |
| 2489 | sizeof tss_seg.prev_task_link, |
| 2490 | ctxt->vcpu, &err); |
| 2491 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
| 2492 | /* FIXME: need to provide precise fault address */ |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2493 | emulate_pf(ctxt, new_tss_base, err); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2494 | return ret; |
| 2495 | } |
| 2496 | } |
| 2497 | |
| 2498 | return load_state_from_tss32(ctxt, ops, &tss_seg); |
| 2499 | } |
| 2500 | |
| 2501 | static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2502 | struct x86_emulate_ops *ops, |
| 2503 | u16 tss_selector, int reason, |
| 2504 | bool has_error_code, u32 error_code) |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2505 | { |
| 2506 | struct desc_struct curr_tss_desc, next_tss_desc; |
| 2507 | int ret; |
| 2508 | u16 old_tss_sel = ops->get_segment_selector(VCPU_SREG_TR, ctxt->vcpu); |
| 2509 | ulong old_tss_base = |
Gleb Natapov | 5951c44 | 2010-04-28 19:15:29 +0300 | [diff] [blame] | 2510 | ops->get_cached_segment_base(VCPU_SREG_TR, ctxt->vcpu); |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2511 | u32 desc_limit; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2512 | |
| 2513 | /* FIXME: old_tss_base == ~0 ? */ |
| 2514 | |
| 2515 | ret = read_segment_descriptor(ctxt, ops, tss_selector, &next_tss_desc); |
| 2516 | if (ret != X86EMUL_CONTINUE) |
| 2517 | return ret; |
| 2518 | ret = read_segment_descriptor(ctxt, ops, old_tss_sel, &curr_tss_desc); |
| 2519 | if (ret != X86EMUL_CONTINUE) |
| 2520 | return ret; |
| 2521 | |
| 2522 | /* FIXME: check that next_tss_desc is tss */ |
| 2523 | |
| 2524 | if (reason != TASK_SWITCH_IRET) { |
| 2525 | if ((tss_selector & 3) > next_tss_desc.dpl || |
| 2526 | ops->cpl(ctxt->vcpu) > next_tss_desc.dpl) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2527 | emulate_gp(ctxt, 0); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2528 | return X86EMUL_PROPAGATE_FAULT; |
| 2529 | } |
| 2530 | } |
| 2531 | |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 2532 | desc_limit = desc_limit_scaled(&next_tss_desc); |
| 2533 | if (!next_tss_desc.p || |
| 2534 | ((desc_limit < 0x67 && (next_tss_desc.type & 8)) || |
| 2535 | desc_limit < 0x2b)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2536 | emulate_ts(ctxt, tss_selector & 0xfffc); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2537 | return X86EMUL_PROPAGATE_FAULT; |
| 2538 | } |
| 2539 | |
| 2540 | if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) { |
| 2541 | curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */ |
| 2542 | write_segment_descriptor(ctxt, ops, old_tss_sel, |
| 2543 | &curr_tss_desc); |
| 2544 | } |
| 2545 | |
| 2546 | if (reason == TASK_SWITCH_IRET) |
| 2547 | ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT; |
| 2548 | |
| 2549 | /* set back link to prev task only if NT bit is set in eflags |
| 2550 | note that old_tss_sel is not used afetr this point */ |
| 2551 | if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE) |
| 2552 | old_tss_sel = 0xffff; |
| 2553 | |
| 2554 | if (next_tss_desc.type & 8) |
| 2555 | ret = task_switch_32(ctxt, ops, tss_selector, old_tss_sel, |
| 2556 | old_tss_base, &next_tss_desc); |
| 2557 | else |
| 2558 | ret = task_switch_16(ctxt, ops, tss_selector, old_tss_sel, |
| 2559 | old_tss_base, &next_tss_desc); |
Jan Kiszka | 0760d44 | 2010-04-14 15:50:57 +0200 | [diff] [blame] | 2560 | if (ret != X86EMUL_CONTINUE) |
| 2561 | return ret; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2562 | |
| 2563 | if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) |
| 2564 | ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT; |
| 2565 | |
| 2566 | if (reason != TASK_SWITCH_IRET) { |
| 2567 | next_tss_desc.type |= (1 << 1); /* set busy flag */ |
| 2568 | write_segment_descriptor(ctxt, ops, tss_selector, |
| 2569 | &next_tss_desc); |
| 2570 | } |
| 2571 | |
| 2572 | ops->set_cr(0, ops->get_cr(0, ctxt->vcpu) | X86_CR0_TS, ctxt->vcpu); |
| 2573 | ops->set_cached_descriptor(&next_tss_desc, VCPU_SREG_TR, ctxt->vcpu); |
| 2574 | ops->set_segment_selector(tss_selector, VCPU_SREG_TR, ctxt->vcpu); |
| 2575 | |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2576 | if (has_error_code) { |
| 2577 | struct decode_cache *c = &ctxt->decode; |
| 2578 | |
| 2579 | c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2; |
| 2580 | c->lock_prefix = 0; |
| 2581 | c->src.val = (unsigned long) error_code; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2582 | emulate_push(ctxt, ops); |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2583 | } |
| 2584 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2585 | return ret; |
| 2586 | } |
| 2587 | |
| 2588 | int emulator_task_switch(struct x86_emulate_ctxt *ctxt, |
| 2589 | struct x86_emulate_ops *ops, |
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 | { |
| 2593 | struct decode_cache *c = &ctxt->decode; |
| 2594 | int rc; |
| 2595 | |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2596 | c->eip = ctxt->eip; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2597 | c->dst.type = OP_NONE; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2598 | |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2599 | rc = emulator_do_task_switch(ctxt, ops, tss_selector, reason, |
| 2600 | has_error_code, error_code); |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2601 | |
| 2602 | if (rc == X86EMUL_CONTINUE) { |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2603 | rc = writeback(ctxt, ops); |
Gleb Natapov | 95c5588 | 2010-04-28 19:15:39 +0300 | [diff] [blame] | 2604 | if (rc == X86EMUL_CONTINUE) |
| 2605 | ctxt->eip = c->eip; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2606 | } |
| 2607 | |
Gleb Natapov | 19d0443 | 2010-04-15 12:29:50 +0300 | [diff] [blame] | 2608 | return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0; |
Gleb Natapov | 38ba30b | 2010-03-18 15:20:17 +0200 | [diff] [blame] | 2609 | } |
| 2610 | |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2611 | 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] | 2612 | int reg, struct operand *op) |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2613 | { |
| 2614 | struct decode_cache *c = &ctxt->decode; |
| 2615 | int df = (ctxt->eflags & EFLG_DF) ? -1 : 1; |
| 2616 | |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 2617 | register_address_increment(c, &c->regs[reg], df * op->bytes); |
| 2618 | op->ptr = (unsigned long *)register_address(c, base, c->regs[reg]); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2619 | } |
| 2620 | |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2621 | int |
Laurent Vivier | 1be3aa4 | 2007-09-18 11:27:27 +0200 | [diff] [blame] | 2622 | x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2623 | { |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2624 | u64 msr_data; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2625 | struct decode_cache *c = &ctxt->decode; |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2626 | int rc = X86EMUL_CONTINUE; |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 2627 | int saved_dst_type = c->dst.type; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2628 | |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 2629 | ctxt->decode.mem_read.pos = 0; |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 2630 | |
Gleb Natapov | 1161624 | 2010-02-11 14:43:14 +0200 | [diff] [blame] | 2631 | if (ctxt->mode == X86EMUL_MODE_PROT64 && (c->d & No64)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2632 | emulate_ud(ctxt); |
Gleb Natapov | 1161624 | 2010-02-11 14:43:14 +0200 | [diff] [blame] | 2633 | goto done; |
| 2634 | } |
| 2635 | |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 2636 | /* LOCK prefix is allowed only with some instructions */ |
Gleb Natapov | a41ffb75 | 2010-03-18 15:20:14 +0200 | [diff] [blame] | 2637 | if (c->lock_prefix && (!(c->d & Lock) || c->dst.type != OP_MEM)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2638 | emulate_ud(ctxt); |
Gleb Natapov | d380a5e | 2010-02-10 14:21:36 +0200 | [diff] [blame] | 2639 | goto done; |
| 2640 | } |
| 2641 | |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 2642 | /* Privileged instruction can be executed only in CPL=0 */ |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 2643 | if ((c->d & Priv) && ops->cpl(ctxt->vcpu)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2644 | emulate_gp(ctxt, 0); |
Gleb Natapov | e92805a | 2010-02-10 14:21:35 +0200 | [diff] [blame] | 2645 | goto done; |
| 2646 | } |
| 2647 | |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 2648 | if (c->rep_prefix && (c->d & String)) { |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 2649 | ctxt->restart = true; |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 2650 | /* All REP prefixes have the same first termination condition */ |
Gleb Natapov | c73e197 | 2010-03-15 16:38:29 +0200 | [diff] [blame] | 2651 | if (address_mask(c, c->regs[VCPU_REGS_RCX]) == 0) { |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 2652 | string_done: |
| 2653 | ctxt->restart = false; |
Gleb Natapov | 95c5588 | 2010-04-28 19:15:39 +0300 | [diff] [blame] | 2654 | ctxt->eip = c->eip; |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 2655 | goto done; |
| 2656 | } |
| 2657 | /* The second termination condition only applies for REPE |
| 2658 | * and REPNE. Test if the repeat string operation prefix is |
| 2659 | * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the |
| 2660 | * corresponding termination condition according to: |
| 2661 | * - if REPE/REPZ and ZF = 0 then done |
| 2662 | * - if REPNE/REPNZ and ZF = 1 then done |
| 2663 | */ |
| 2664 | if ((c->b == 0xa6) || (c->b == 0xa7) || |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 2665 | (c->b == 0xae) || (c->b == 0xaf)) { |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 2666 | if ((c->rep_prefix == REPE_PREFIX) && |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 2667 | ((ctxt->eflags & EFLG_ZF) == 0)) |
| 2668 | goto string_done; |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 2669 | if ((c->rep_prefix == REPNE_PREFIX) && |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 2670 | ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) |
| 2671 | goto string_done; |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 2672 | } |
Gleb Natapov | 063db06 | 2010-03-18 15:20:06 +0200 | [diff] [blame] | 2673 | c->eip = ctxt->eip; |
Avi Kivity | b9fa9d6 | 2007-11-27 19:05:37 +0200 | [diff] [blame] | 2674 | } |
| 2675 | |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2676 | if (c->src.type == OP_MEM) { |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 2677 | rc = read_emulated(ctxt, ops, (unsigned long)c->src.ptr, |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 2678 | c->src.valptr, c->src.bytes); |
Takuya Yoshikawa | b60d513 | 2010-01-20 16:47:21 +0900 | [diff] [blame] | 2679 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2680 | goto done; |
Avi Kivity | 16518d5 | 2010-08-26 14:31:30 +0300 | [diff] [blame] | 2681 | c->src.orig_val64 = c->src.val64; |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2682 | } |
| 2683 | |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 2684 | if (c->src2.type == OP_MEM) { |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 2685 | rc = read_emulated(ctxt, ops, (unsigned long)c->src2.ptr, |
| 2686 | &c->src2.val, c->src2.bytes); |
Gleb Natapov | e35b7b9 | 2010-02-25 16:36:42 +0200 | [diff] [blame] | 2687 | if (rc != X86EMUL_CONTINUE) |
| 2688 | goto done; |
| 2689 | } |
| 2690 | |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2691 | if ((c->d & DstMask) == ImplicitOps) |
| 2692 | goto special_insn; |
| 2693 | |
| 2694 | |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 2695 | if ((c->dst.type == OP_MEM) && !(c->d & Mov)) { |
| 2696 | /* optimisation - avoid slow emulated read if Mov */ |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 2697 | rc = read_emulated(ctxt, ops, (unsigned long)c->dst.ptr, |
| 2698 | &c->dst.val, c->dst.bytes); |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 2699 | if (rc != X86EMUL_CONTINUE) |
| 2700 | goto done; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 2701 | } |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2702 | c->dst.orig_val = c->dst.val; |
Avi Kivity | 038e51d | 2007-01-22 20:40:40 -0800 | [diff] [blame] | 2703 | |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2704 | special_insn: |
| 2705 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2706 | if (c->twobyte) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2707 | goto twobyte_insn; |
| 2708 | |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2709 | switch (c->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2710 | case 0x00 ... 0x05: |
| 2711 | add: /* add */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2712 | emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2713 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2714 | case 0x06: /* push es */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2715 | emulate_push_sreg(ctxt, ops, VCPU_SREG_ES); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2716 | break; |
| 2717 | case 0x07: /* pop es */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2718 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2719 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2720 | goto done; |
| 2721 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2722 | case 0x08 ... 0x0d: |
| 2723 | or: /* or */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2724 | emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2725 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2726 | case 0x0e: /* push cs */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2727 | emulate_push_sreg(ctxt, ops, VCPU_SREG_CS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2728 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2729 | case 0x10 ... 0x15: |
| 2730 | adc: /* adc */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2731 | emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2732 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2733 | case 0x16: /* push ss */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2734 | emulate_push_sreg(ctxt, ops, VCPU_SREG_SS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2735 | break; |
| 2736 | case 0x17: /* pop ss */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2737 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2738 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2739 | goto done; |
| 2740 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2741 | case 0x18 ... 0x1d: |
| 2742 | sbb: /* sbb */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2743 | emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2744 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2745 | case 0x1e: /* push ds */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2746 | emulate_push_sreg(ctxt, ops, VCPU_SREG_DS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2747 | break; |
| 2748 | case 0x1f: /* pop ds */ |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2749 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2750 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 2751 | goto done; |
| 2752 | break; |
Guillaume Thouvenin | aa3a816 | 2008-09-12 13:52:18 +0200 | [diff] [blame] | 2753 | case 0x20 ... 0x25: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2754 | and: /* and */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2755 | emulate_2op_SrcV("and", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2756 | break; |
| 2757 | case 0x28 ... 0x2d: |
| 2758 | sub: /* sub */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2759 | emulate_2op_SrcV("sub", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2760 | break; |
| 2761 | case 0x30 ... 0x35: |
| 2762 | xor: /* xor */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2763 | emulate_2op_SrcV("xor", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2764 | break; |
| 2765 | case 0x38 ... 0x3d: |
| 2766 | cmp: /* cmp */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2767 | emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2768 | break; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 2769 | case 0x40 ... 0x47: /* inc r16/r32 */ |
| 2770 | emulate_1op("inc", c->dst, ctxt->eflags); |
| 2771 | break; |
| 2772 | case 0x48 ... 0x4f: /* dec r16/r32 */ |
| 2773 | emulate_1op("dec", c->dst, ctxt->eflags); |
| 2774 | break; |
| 2775 | case 0x50 ... 0x57: /* push reg */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2776 | emulate_push(ctxt, ops); |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 2777 | break; |
| 2778 | case 0x58 ... 0x5f: /* pop reg */ |
| 2779 | pop_instruction: |
Avi Kivity | 350f69d | 2009-01-05 11:12:40 +0200 | [diff] [blame] | 2780 | rc = emulate_pop(ctxt, ops, &c->dst.val, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2781 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 2782 | goto done; |
Avi Kivity | 33615aa | 2007-10-31 11:15:56 +0200 | [diff] [blame] | 2783 | break; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 2784 | case 0x60: /* pusha */ |
Wei Yongjun | c37eda1 | 2010-06-15 09:03:33 +0800 | [diff] [blame] | 2785 | rc = emulate_pusha(ctxt, ops); |
| 2786 | if (rc != X86EMUL_CONTINUE) |
| 2787 | goto done; |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 2788 | break; |
| 2789 | case 0x61: /* popa */ |
| 2790 | rc = emulate_popa(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2791 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | abcf14b | 2009-09-01 15:28:11 +0200 | [diff] [blame] | 2792 | goto done; |
| 2793 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2794 | case 0x63: /* movsxd */ |
Laurent Vivier | 8b4caf6 | 2007-09-18 11:27:19 +0200 | [diff] [blame] | 2795 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2796 | goto cannot_emulate; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2797 | c->dst.val = (s32) c->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2798 | break; |
Avi Kivity | 91ed7a0 | 2008-05-29 14:38:38 +0300 | [diff] [blame] | 2799 | case 0x68: /* push imm */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2800 | case 0x6a: /* push imm8 */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2801 | emulate_push(ctxt, ops); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2802 | break; |
| 2803 | case 0x6c: /* insb */ |
| 2804 | case 0x6d: /* insw/insd */ |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 2805 | c->dst.bytes = min(c->dst.bytes, 4u); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2806 | if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX], |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 2807 | c->dst.bytes)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2808 | emulate_gp(ctxt, 0); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2809 | goto done; |
| 2810 | } |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 2811 | if (!pio_in_emulated(ctxt, ops, c->dst.bytes, |
| 2812 | c->regs[VCPU_REGS_RDX], &c->dst.val)) |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 2813 | goto done; /* IO is needed, skip writeback */ |
| 2814 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2815 | case 0x6e: /* outsb */ |
| 2816 | case 0x6f: /* outsw/outsd */ |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 2817 | c->src.bytes = min(c->src.bytes, 4u); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2818 | if (!emulator_io_permited(ctxt, ops, c->regs[VCPU_REGS_RDX], |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 2819 | c->src.bytes)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2820 | emulate_gp(ctxt, 0); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 2821 | goto done; |
| 2822 | } |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 2823 | ops->pio_out_emulated(c->src.bytes, c->regs[VCPU_REGS_RDX], |
| 2824 | &c->src.val, 1, ctxt->vcpu); |
| 2825 | |
| 2826 | c->dst.type = OP_NONE; /* nothing to writeback */ |
| 2827 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 2828 | case 0x70 ... 0x7f: /* jcc (short) */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2829 | if (test_cc(c->b, ctxt->eflags)) |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 2830 | jmp_rel(c, c->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2831 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2832 | case 0x80 ... 0x83: /* Grp1 */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2833 | switch (c->modrm_reg) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2834 | case 0: |
| 2835 | goto add; |
| 2836 | case 1: |
| 2837 | goto or; |
| 2838 | case 2: |
| 2839 | goto adc; |
| 2840 | case 3: |
| 2841 | goto sbb; |
| 2842 | case 4: |
| 2843 | goto and; |
| 2844 | case 5: |
| 2845 | goto sub; |
| 2846 | case 6: |
| 2847 | goto xor; |
| 2848 | case 7: |
| 2849 | goto cmp; |
| 2850 | } |
| 2851 | break; |
| 2852 | case 0x84 ... 0x85: |
Mohammed Gamal | dfb507c | 2010-05-11 22:22:40 +0300 | [diff] [blame] | 2853 | test: |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2854 | emulate_2op_SrcV("test", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2855 | break; |
| 2856 | case 0x86 ... 0x87: /* xchg */ |
Mohammed Gamal | b13354f | 2008-06-15 19:37:38 +0300 | [diff] [blame] | 2857 | xchg: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2858 | /* Write back the register source. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2859 | switch (c->dst.bytes) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2860 | case 1: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2861 | *(u8 *) c->src.ptr = (u8) c->dst.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2862 | break; |
| 2863 | case 2: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2864 | *(u16 *) c->src.ptr = (u16) c->dst.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2865 | break; |
| 2866 | case 4: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2867 | *c->src.ptr = (u32) c->dst.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2868 | break; /* 64b reg: zero-extend */ |
| 2869 | case 8: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2870 | *c->src.ptr = c->dst.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2871 | break; |
| 2872 | } |
| 2873 | /* |
| 2874 | * Write back the memory destination with implicit LOCK |
| 2875 | * prefix. |
| 2876 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2877 | c->dst.val = c->src.val; |
| 2878 | c->lock_prefix = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2879 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2880 | case 0x88 ... 0x8b: /* mov */ |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 2881 | goto mov; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2882 | case 0x8c: /* mov r/m, sreg */ |
| 2883 | if (c->modrm_reg > VCPU_SREG_GS) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2884 | emulate_ud(ctxt); |
Gleb Natapov | 5e3ae6c | 2010-03-18 15:20:07 +0200 | [diff] [blame] | 2885 | goto done; |
Guillaume Thouvenin | 38d5bc6 | 2008-05-27 15:13:28 +0200 | [diff] [blame] | 2886 | } |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2887 | c->dst.val = ops->get_segment_selector(c->modrm_reg, ctxt->vcpu); |
Guillaume Thouvenin | 38d5bc6 | 2008-05-27 15:13:28 +0200 | [diff] [blame] | 2888 | break; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 2889 | case 0x8d: /* lea r16/r32, m */ |
Avi Kivity | f9b7aab | 2008-04-14 23:46:37 +0300 | [diff] [blame] | 2890 | c->dst.val = c->modrm_ea; |
Nitin A Kamble | 7e0b54b | 2007-09-15 10:35:36 +0300 | [diff] [blame] | 2891 | break; |
Guillaume Thouvenin | 4257198 | 2008-05-27 14:49:15 +0200 | [diff] [blame] | 2892 | case 0x8e: { /* mov seg, r/m16 */ |
| 2893 | uint16_t sel; |
Guillaume Thouvenin | 4257198 | 2008-05-27 14:49:15 +0200 | [diff] [blame] | 2894 | |
| 2895 | sel = c->src.val; |
Gleb Natapov | 8b9f441 | 2010-02-18 12:14:59 +0200 | [diff] [blame] | 2896 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 2897 | if (c->modrm_reg == VCPU_SREG_CS || |
| 2898 | c->modrm_reg > VCPU_SREG_GS) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 2899 | emulate_ud(ctxt); |
Gleb Natapov | 8b9f441 | 2010-02-18 12:14:59 +0200 | [diff] [blame] | 2900 | goto done; |
| 2901 | } |
| 2902 | |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 2903 | if (c->modrm_reg == VCPU_SREG_SS) |
Gleb Natapov | 95cb229 | 2010-04-28 19:15:43 +0300 | [diff] [blame] | 2904 | ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 2905 | |
Gleb Natapov | 2e87302 | 2010-03-18 15:20:18 +0200 | [diff] [blame] | 2906 | rc = load_segment_descriptor(ctxt, ops, sel, c->modrm_reg); |
Guillaume Thouvenin | 4257198 | 2008-05-27 14:49:15 +0200 | [diff] [blame] | 2907 | |
| 2908 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 2909 | break; |
| 2910 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2911 | case 0x8f: /* pop (sole member of Grp1a) */ |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2912 | rc = emulate_grp1a(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2913 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2914 | goto done; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2915 | break; |
Mohammed Gamal | b13354f | 2008-06-15 19:37:38 +0300 | [diff] [blame] | 2916 | case 0x90: /* nop / xchg r8,rax */ |
Gleb Natapov | b8a9894 | 2010-04-28 19:15:25 +0300 | [diff] [blame] | 2917 | if (c->dst.ptr == (unsigned long *)&c->regs[VCPU_REGS_RAX]) { |
| 2918 | c->dst.type = OP_NONE; /* nop */ |
Mohammed Gamal | b13354f | 2008-06-15 19:37:38 +0300 | [diff] [blame] | 2919 | break; |
| 2920 | } |
| 2921 | case 0x91 ... 0x97: /* xchg reg,rax */ |
Gleb Natapov | f0c13ef | 2010-04-28 19:15:24 +0300 | [diff] [blame] | 2922 | c->src.type = OP_REG; |
| 2923 | c->src.bytes = c->op_bytes; |
Mohammed Gamal | b13354f | 2008-06-15 19:37:38 +0300 | [diff] [blame] | 2924 | c->src.ptr = (unsigned long *) &c->regs[VCPU_REGS_RAX]; |
| 2925 | c->src.val = *(c->src.ptr); |
| 2926 | goto xchg; |
Nitin A Kamble | fd2a760 | 2007-08-28 18:22:47 -0700 | [diff] [blame] | 2927 | case 0x9c: /* pushf */ |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2928 | c->src.val = (unsigned long) ctxt->eflags; |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2929 | emulate_push(ctxt, ops); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 2930 | break; |
Nitin A Kamble | 535eabc | 2007-09-15 10:45:05 +0300 | [diff] [blame] | 2931 | case 0x9d: /* popf */ |
Avi Kivity | 2b48cc7 | 2008-11-29 20:36:13 +0200 | [diff] [blame] | 2932 | c->dst.type = OP_REG; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 2933 | c->dst.ptr = (unsigned long *) &ctxt->eflags; |
Avi Kivity | 2b48cc7 | 2008-11-29 20:36:13 +0200 | [diff] [blame] | 2934 | c->dst.bytes = c->op_bytes; |
Gleb Natapov | d4c6a15 | 2010-02-10 14:21:34 +0200 | [diff] [blame] | 2935 | rc = emulate_popf(ctxt, ops, &c->dst.val, c->op_bytes); |
| 2936 | if (rc != X86EMUL_CONTINUE) |
| 2937 | goto done; |
| 2938 | break; |
Wei Yongjun | 5d55f29 | 2010-07-07 17:43:35 +0800 | [diff] [blame] | 2939 | case 0xa0 ... 0xa3: /* mov */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2940 | case 0xa4 ... 0xa5: /* movs */ |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2941 | goto mov; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2942 | case 0xa6 ... 0xa7: /* cmps */ |
Guillaume Thouvenin | d7e5117 | 2007-11-26 13:49:09 +0100 | [diff] [blame] | 2943 | c->dst.type = OP_NONE; /* Disable writeback. */ |
Guillaume Thouvenin | d7e5117 | 2007-11-26 13:49:09 +0100 | [diff] [blame] | 2944 | 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] | 2945 | goto cmp; |
Mohammed Gamal | dfb507c | 2010-05-11 22:22:40 +0300 | [diff] [blame] | 2946 | case 0xa8 ... 0xa9: /* test ax, imm */ |
| 2947 | goto test; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2948 | case 0xaa ... 0xab: /* stos */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2949 | c->dst.val = c->regs[VCPU_REGS_RAX]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2950 | break; |
| 2951 | case 0xac ... 0xad: /* lods */ |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 2952 | goto mov; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2953 | case 0xae ... 0xaf: /* scas */ |
| 2954 | DPRINTF("Urk! I don't handle SCAS.\n"); |
| 2955 | goto cannot_emulate; |
Mohammed Gamal | a5e2e82 | 2008-08-27 05:02:56 +0300 | [diff] [blame] | 2956 | case 0xb0 ... 0xbf: /* mov r, imm */ |
Guillaume Thouvenin | 615ac12 | 2008-05-27 10:19:16 +0200 | [diff] [blame] | 2957 | goto mov; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2958 | case 0xc0 ... 0xc1: |
| 2959 | emulate_grp2(ctxt); |
| 2960 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2961 | case 0xc3: /* ret */ |
Avi Kivity | cf5de4f | 2008-11-28 00:14:07 +0200 | [diff] [blame] | 2962 | c->dst.type = OP_REG; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2963 | c->dst.ptr = &c->eip; |
Avi Kivity | cf5de4f | 2008-11-28 00:14:07 +0200 | [diff] [blame] | 2964 | c->dst.bytes = c->op_bytes; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 2965 | goto pop_instruction; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2966 | case 0xc6 ... 0xc7: /* mov (sole member of Grp11) */ |
| 2967 | mov: |
| 2968 | c->dst.val = c->src.val; |
| 2969 | break; |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2970 | case 0xcb: /* ret far */ |
| 2971 | rc = emulate_ret_far(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 2972 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | a77ab5e | 2009-01-05 13:27:34 +0200 | [diff] [blame] | 2973 | goto done; |
| 2974 | break; |
Mohammed Gamal | 62bd430 | 2010-07-28 12:38:40 +0300 | [diff] [blame] | 2975 | case 0xcf: /* iret */ |
| 2976 | rc = emulate_iret(ctxt, ops); |
| 2977 | |
| 2978 | if (rc != X86EMUL_CONTINUE) |
| 2979 | goto done; |
| 2980 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 2981 | case 0xd0 ... 0xd1: /* Grp2 */ |
| 2982 | c->src.val = 1; |
| 2983 | emulate_grp2(ctxt); |
| 2984 | break; |
| 2985 | case 0xd2 ... 0xd3: /* Grp2 */ |
| 2986 | c->src.val = c->regs[VCPU_REGS_RCX]; |
| 2987 | emulate_grp2(ctxt); |
| 2988 | break; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 2989 | case 0xe4: /* inb */ |
| 2990 | case 0xe5: /* in */ |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 2991 | goto do_io_in; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 2992 | case 0xe6: /* outb */ |
| 2993 | case 0xe7: /* out */ |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 2994 | goto do_io_out; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 2995 | case 0xe8: /* call (near) */ { |
Gleb Natapov | d53c477 | 2009-04-12 13:36:36 +0300 | [diff] [blame] | 2996 | long int rel = c->src.val; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 2997 | c->src.val = (unsigned long) c->eip; |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 2998 | jmp_rel(c, rel); |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 2999 | emulate_push(ctxt, ops); |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 3000 | break; |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 3001 | } |
| 3002 | case 0xe9: /* jmp rel */ |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 3003 | goto jmp; |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 3004 | case 0xea: { /* jmp far */ |
| 3005 | unsigned short sel; |
Gleb Natapov | ea79849d | 2010-02-25 16:36:43 +0200 | [diff] [blame] | 3006 | jump_far: |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 3007 | memcpy(&sel, c->src.valptr + c->op_bytes, 2); |
| 3008 | |
| 3009 | if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS)) |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 3010 | goto done; |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 3011 | |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 3012 | c->eip = 0; |
| 3013 | memcpy(&c->eip, c->src.valptr, c->op_bytes); |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 3014 | break; |
Gleb Natapov | 414e627 | 2010-04-28 19:15:26 +0300 | [diff] [blame] | 3015 | } |
Guillaume Thouvenin | 954cd36 | 2008-05-27 10:19:08 +0200 | [diff] [blame] | 3016 | case 0xeb: |
| 3017 | jmp: /* jmp rel short */ |
Harvey Harrison | 7a957275 | 2008-02-19 07:40:41 -0800 | [diff] [blame] | 3018 | jmp_rel(c, c->src.val); |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 3019 | c->dst.type = OP_NONE; /* Disable writeback. */ |
Nitin A Kamble | 1a52e05 | 2007-09-18 16:34:25 -0700 | [diff] [blame] | 3020 | break; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 3021 | case 0xec: /* in al,dx */ |
| 3022 | case 0xed: /* in (e/r)ax,dx */ |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3023 | c->src.val = c->regs[VCPU_REGS_RDX]; |
| 3024 | do_io_in: |
| 3025 | c->dst.bytes = min(c->dst.bytes, 4u); |
| 3026 | if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3027 | emulate_gp(ctxt, 0); |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 3028 | goto done; |
| 3029 | } |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 3030 | if (!pio_in_emulated(ctxt, ops, c->dst.bytes, c->src.val, |
| 3031 | &c->dst.val)) |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3032 | goto done; /* IO is needed */ |
| 3033 | break; |
Wei Yongjun | ce7a0ad | 2010-07-06 16:50:21 +0800 | [diff] [blame] | 3034 | case 0xee: /* out dx,al */ |
| 3035 | case 0xef: /* out dx,(e/r)ax */ |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3036 | c->src.val = c->regs[VCPU_REGS_RDX]; |
| 3037 | do_io_out: |
| 3038 | c->dst.bytes = min(c->dst.bytes, 4u); |
| 3039 | if (!emulator_io_permited(ctxt, ops, c->src.val, c->dst.bytes)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3040 | emulate_gp(ctxt, 0); |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3041 | goto done; |
Mohammed Gamal | a6a3034 | 2008-09-06 17:22:29 +0300 | [diff] [blame] | 3042 | } |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3043 | ops->pio_out_emulated(c->dst.bytes, c->src.val, &c->dst.val, 1, |
| 3044 | ctxt->vcpu); |
| 3045 | c->dst.type = OP_NONE; /* Disable writeback. */ |
Guillaume Thouvenin | e93f36b | 2008-10-28 10:51:30 +0100 | [diff] [blame] | 3046 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3047 | case 0xf4: /* hlt */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3048 | ctxt->vcpu->arch.halt_request = 1; |
Mohammed Gamal | 19fdfa0 | 2008-07-06 16:51:26 +0300 | [diff] [blame] | 3049 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3050 | case 0xf5: /* cmc */ |
| 3051 | /* complement carry flag from eflags reg */ |
| 3052 | ctxt->eflags ^= EFLG_CF; |
| 3053 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 3054 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3055 | case 0xf6 ... 0xf7: /* Grp3 */ |
Gleb Natapov | aca06a8 | 2010-03-18 15:20:15 +0200 | [diff] [blame] | 3056 | if (!emulate_grp3(ctxt, ops)) |
| 3057 | goto cannot_emulate; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3058 | break; |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3059 | case 0xf8: /* clc */ |
| 3060 | ctxt->eflags &= ~EFLG_CF; |
| 3061 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 3062 | break; |
| 3063 | case 0xfa: /* cli */ |
Wei Yongjun | 07cbc6c | 2010-07-06 16:54:19 +0800 | [diff] [blame] | 3064 | if (emulator_bad_iopl(ctxt, ops)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3065 | emulate_gp(ctxt, 0); |
Wei Yongjun | 07cbc6c | 2010-07-06 16:54:19 +0800 | [diff] [blame] | 3066 | goto done; |
| 3067 | } else { |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 3068 | ctxt->eflags &= ~X86_EFLAGS_IF; |
| 3069 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 3070 | } |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3071 | break; |
| 3072 | case 0xfb: /* sti */ |
Wei Yongjun | 07cbc6c | 2010-07-06 16:54:19 +0800 | [diff] [blame] | 3073 | if (emulator_bad_iopl(ctxt, ops)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3074 | emulate_gp(ctxt, 0); |
Wei Yongjun | 07cbc6c | 2010-07-06 16:54:19 +0800 | [diff] [blame] | 3075 | goto done; |
| 3076 | } else { |
Gleb Natapov | 95cb229 | 2010-04-28 19:15:43 +0300 | [diff] [blame] | 3077 | ctxt->interruptibility = KVM_X86_SHADOW_INT_STI; |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 3078 | ctxt->eflags |= X86_EFLAGS_IF; |
| 3079 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 3080 | } |
Avi Kivity | 111de5d | 2007-11-27 19:14:21 +0200 | [diff] [blame] | 3081 | break; |
Mohammed Gamal | fb4616f | 2008-09-01 04:52:24 +0300 | [diff] [blame] | 3082 | case 0xfc: /* cld */ |
| 3083 | ctxt->eflags &= ~EFLG_DF; |
| 3084 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 3085 | break; |
| 3086 | case 0xfd: /* std */ |
| 3087 | ctxt->eflags |= EFLG_DF; |
| 3088 | c->dst.type = OP_NONE; /* Disable writeback. */ |
| 3089 | break; |
Gleb Natapov | ea79849d | 2010-02-25 16:36:43 +0200 | [diff] [blame] | 3090 | case 0xfe: /* Grp4 */ |
| 3091 | grp45: |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3092 | rc = emulate_grp45(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3093 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3094 | goto done; |
| 3095 | break; |
Gleb Natapov | ea79849d | 2010-02-25 16:36:43 +0200 | [diff] [blame] | 3096 | case 0xff: /* Grp5 */ |
| 3097 | if (c->modrm_reg == 5) |
| 3098 | goto jump_far; |
| 3099 | goto grp45; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 3100 | default: |
| 3101 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3102 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3103 | |
| 3104 | writeback: |
| 3105 | rc = writeback(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3106 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3107 | goto done; |
| 3108 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 3109 | /* |
| 3110 | * restore dst type in case the decoding will be reused |
| 3111 | * (happens for string instruction ) |
| 3112 | */ |
| 3113 | c->dst.type = saved_dst_type; |
| 3114 | |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 3115 | if ((c->d & SrcMask) == SrcSI) |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3116 | string_addr_inc(ctxt, seg_override_base(ctxt, ops, c), |
| 3117 | VCPU_REGS_RSI, &c->src); |
Gleb Natapov | a682e35 | 2010-03-18 15:20:21 +0200 | [diff] [blame] | 3118 | |
| 3119 | if ((c->d & DstMask) == DstDI) |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3120 | string_addr_inc(ctxt, es_base(ctxt, ops), VCPU_REGS_RDI, |
| 3121 | &c->dst); |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 3122 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 3123 | if (c->rep_prefix && (c->d & String)) { |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 3124 | struct read_cache *rc = &ctxt->decode.io_read; |
Gleb Natapov | d927112 | 2010-03-18 15:20:22 +0200 | [diff] [blame] | 3125 | register_address_increment(c, &c->regs[VCPU_REGS_RCX], -1); |
Gleb Natapov | 7b262e9 | 2010-03-18 15:20:27 +0200 | [diff] [blame] | 3126 | /* |
| 3127 | * Re-enter guest when pio read ahead buffer is empty or, |
| 3128 | * if it is not used, after each 1024 iteration. |
| 3129 | */ |
| 3130 | if ((rc->end == 0 && !(c->regs[VCPU_REGS_RCX] & 0x3ff)) || |
| 3131 | (rc->end != 0 && rc->end == rc->pos)) |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 3132 | ctxt->restart = false; |
| 3133 | } |
Gleb Natapov | 9de4157 | 2010-04-28 19:15:22 +0300 | [diff] [blame] | 3134 | /* |
| 3135 | * reset read cache here in case string instruction is restared |
| 3136 | * without decoding |
| 3137 | */ |
| 3138 | ctxt->decode.mem_read.end = 0; |
Gleb Natapov | 95c5588 | 2010-04-28 19:15:39 +0300 | [diff] [blame] | 3139 | ctxt->eip = c->eip; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3140 | |
| 3141 | done: |
Gleb Natapov | cb404fe | 2010-03-18 15:20:25 +0200 | [diff] [blame] | 3142 | return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3143 | |
| 3144 | twobyte_insn: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3145 | switch (c->b) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3146 | case 0x01: /* lgdt, lidt, lmsw */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3147 | switch (c->modrm_reg) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3148 | u16 size; |
| 3149 | unsigned long address; |
| 3150 | |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3151 | case 0: /* vmcall */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3152 | if (c->modrm_mod != 3 || c->modrm_rm != 1) |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3153 | goto cannot_emulate; |
| 3154 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 3155 | rc = kvm_fix_hypercall(ctxt->vcpu); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3156 | if (rc != X86EMUL_CONTINUE) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 3157 | goto done; |
| 3158 | |
Avi Kivity | 33e3885 | 2008-05-21 15:34:25 +0300 | [diff] [blame] | 3159 | /* Let the processor re-execute the fixed hypercall */ |
Gleb Natapov | 063db06 | 2010-03-18 15:20:06 +0200 | [diff] [blame] | 3160 | c->eip = ctxt->eip; |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 3161 | /* Disable writeback. */ |
| 3162 | c->dst.type = OP_NONE; |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3163 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3164 | case 2: /* lgdt */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3165 | rc = read_descriptor(ctxt, ops, c->src.ptr, |
| 3166 | &size, &address, c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3167 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3168 | goto done; |
| 3169 | realmode_lgdt(ctxt->vcpu, size, address); |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 3170 | /* Disable writeback. */ |
| 3171 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3172 | break; |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3173 | case 3: /* lidt/vmmcall */ |
Avi Kivity | 2b3d2a2 | 2008-12-23 19:46:01 +0200 | [diff] [blame] | 3174 | if (c->modrm_mod == 3) { |
| 3175 | switch (c->modrm_rm) { |
| 3176 | case 1: |
| 3177 | rc = kvm_fix_hypercall(ctxt->vcpu); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3178 | if (rc != X86EMUL_CONTINUE) |
Avi Kivity | 2b3d2a2 | 2008-12-23 19:46:01 +0200 | [diff] [blame] | 3179 | goto done; |
| 3180 | break; |
| 3181 | default: |
| 3182 | goto cannot_emulate; |
| 3183 | } |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3184 | } else { |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3185 | rc = read_descriptor(ctxt, ops, c->src.ptr, |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3186 | &size, &address, |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3187 | c->op_bytes); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3188 | if (rc != X86EMUL_CONTINUE) |
Anthony Liguori | aca7f96 | 2007-09-17 14:57:49 -0500 | [diff] [blame] | 3189 | goto done; |
| 3190 | realmode_lidt(ctxt->vcpu, size, address); |
| 3191 | } |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 3192 | /* Disable writeback. */ |
| 3193 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3194 | break; |
| 3195 | case 4: /* smsw */ |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 3196 | c->dst.bytes = 2; |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 3197 | c->dst.val = ops->get_cr(0, ctxt->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3198 | break; |
| 3199 | case 6: /* lmsw */ |
Gleb Natapov | 93a152b | 2010-03-18 15:20:04 +0200 | [diff] [blame] | 3200 | ops->set_cr(0, (ops->get_cr(0, ctxt->vcpu) & ~0x0ful) | |
| 3201 | (c->src.val & 0x0f), ctxt->vcpu); |
Avi Kivity | dc7457e | 2008-04-30 16:13:36 +0300 | [diff] [blame] | 3202 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3203 | break; |
Gleb Natapov | 6e1e5ff | 2010-03-18 15:20:08 +0200 | [diff] [blame] | 3204 | case 5: /* not defined */ |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3205 | emulate_ud(ctxt); |
Gleb Natapov | 6e1e5ff | 2010-03-18 15:20:08 +0200 | [diff] [blame] | 3206 | goto done; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3207 | case 7: /* invlpg*/ |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 3208 | emulate_invlpg(ctxt->vcpu, c->modrm_ea); |
Avi Kivity | 16286d0 | 2008-04-14 14:40:50 +0300 | [diff] [blame] | 3209 | /* Disable writeback. */ |
| 3210 | c->dst.type = OP_NONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3211 | break; |
| 3212 | default: |
| 3213 | goto cannot_emulate; |
| 3214 | } |
| 3215 | break; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3216 | case 0x05: /* syscall */ |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 3217 | rc = emulate_syscall(ctxt, ops); |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 3218 | if (rc != X86EMUL_CONTINUE) |
| 3219 | goto done; |
Andre Przywara | e66bb2c | 2009-06-18 12:56:00 +0200 | [diff] [blame] | 3220 | else |
| 3221 | goto writeback; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3222 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3223 | case 0x06: |
| 3224 | emulate_clts(ctxt->vcpu); |
| 3225 | c->dst.type = OP_NONE; |
| 3226 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3227 | case 0x09: /* wbinvd */ |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 3228 | kvm_emulate_wbinvd(ctxt->vcpu); |
| 3229 | c->dst.type = OP_NONE; |
| 3230 | break; |
| 3231 | case 0x08: /* invd */ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3232 | case 0x0d: /* GrpP (prefetch) */ |
| 3233 | case 0x18: /* Grp16 (prefetch/nop) */ |
| 3234 | c->dst.type = OP_NONE; |
| 3235 | break; |
| 3236 | case 0x20: /* mov cr, reg */ |
Gleb Natapov | 6aebfa6 | 2010-03-18 15:20:10 +0200 | [diff] [blame] | 3237 | switch (c->modrm_reg) { |
| 3238 | case 1: |
| 3239 | case 5 ... 7: |
| 3240 | case 9 ... 15: |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3241 | emulate_ud(ctxt); |
Gleb Natapov | 6aebfa6 | 2010-03-18 15:20:10 +0200 | [diff] [blame] | 3242 | goto done; |
| 3243 | } |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 3244 | 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] | 3245 | c->dst.type = OP_NONE; /* no writeback */ |
| 3246 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3247 | case 0x21: /* mov from dr to reg */ |
Gleb Natapov | 1e470be | 2010-03-18 15:20:11 +0200 | [diff] [blame] | 3248 | if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) && |
| 3249 | (c->modrm_reg == 4 || c->modrm_reg == 5)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3250 | emulate_ud(ctxt); |
Gleb Natapov | 1e470be | 2010-03-18 15:20:11 +0200 | [diff] [blame] | 3251 | goto done; |
| 3252 | } |
Gleb Natapov | 35aa537 | 2010-04-28 19:15:27 +0300 | [diff] [blame] | 3253 | 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] | 3254 | c->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3255 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3256 | case 0x22: /* mov reg, cr */ |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 3257 | if (ops->set_cr(c->modrm_reg, c->modrm_val, ctxt->vcpu)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3258 | emulate_gp(ctxt, 0); |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 3259 | goto done; |
| 3260 | } |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3261 | c->dst.type = OP_NONE; |
| 3262 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3263 | case 0x23: /* mov from reg to dr */ |
Gleb Natapov | 1e470be | 2010-03-18 15:20:11 +0200 | [diff] [blame] | 3264 | if ((ops->get_cr(4, ctxt->vcpu) & X86_CR4_DE) && |
| 3265 | (c->modrm_reg == 4 || c->modrm_reg == 5)) { |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3266 | emulate_ud(ctxt); |
Gleb Natapov | 1e470be | 2010-03-18 15:20:11 +0200 | [diff] [blame] | 3267 | goto done; |
| 3268 | } |
Gleb Natapov | 35aa537 | 2010-04-28 19:15:27 +0300 | [diff] [blame] | 3269 | |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 3270 | if (ops->set_dr(c->modrm_reg, c->regs[c->modrm_rm] & |
| 3271 | ((ctxt->mode == X86EMUL_MODE_PROT64) ? |
| 3272 | ~0ULL : ~0U), ctxt->vcpu) < 0) { |
| 3273 | /* #UD condition is already handled by the code above */ |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3274 | emulate_gp(ctxt, 0); |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 3275 | goto done; |
| 3276 | } |
| 3277 | |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 3278 | c->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3279 | break; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3280 | case 0x30: |
| 3281 | /* wrmsr */ |
| 3282 | msr_data = (u32)c->regs[VCPU_REGS_RAX] |
| 3283 | | ((u64)c->regs[VCPU_REGS_RDX] << 32); |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 3284 | 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] | 3285 | emulate_gp(ctxt, 0); |
Gleb Natapov | fd52536 | 2010-03-18 15:20:13 +0200 | [diff] [blame] | 3286 | goto done; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3287 | } |
| 3288 | rc = X86EMUL_CONTINUE; |
| 3289 | c->dst.type = OP_NONE; |
| 3290 | break; |
| 3291 | case 0x32: |
| 3292 | /* rdmsr */ |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 3293 | 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] | 3294 | emulate_gp(ctxt, 0); |
Gleb Natapov | fd52536 | 2010-03-18 15:20:13 +0200 | [diff] [blame] | 3295 | goto done; |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3296 | } else { |
| 3297 | c->regs[VCPU_REGS_RAX] = (u32)msr_data; |
| 3298 | c->regs[VCPU_REGS_RDX] = msr_data >> 32; |
| 3299 | } |
| 3300 | rc = X86EMUL_CONTINUE; |
| 3301 | c->dst.type = OP_NONE; |
| 3302 | break; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3303 | case 0x34: /* sysenter */ |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 3304 | rc = emulate_sysenter(ctxt, ops); |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 3305 | if (rc != X86EMUL_CONTINUE) |
| 3306 | goto done; |
Andre Przywara | 8c60435 | 2009-06-18 12:56:01 +0200 | [diff] [blame] | 3307 | else |
| 3308 | goto writeback; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3309 | break; |
| 3310 | case 0x35: /* sysexit */ |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 3311 | rc = emulate_sysexit(ctxt, ops); |
Takuya Yoshikawa | e54cfa9 | 2010-02-18 12:15:02 +0200 | [diff] [blame] | 3312 | if (rc != X86EMUL_CONTINUE) |
| 3313 | goto done; |
Andre Przywara | 4668f05 | 2009-06-18 12:56:02 +0200 | [diff] [blame] | 3314 | else |
| 3315 | goto writeback; |
Andre Przywara | e99f050 | 2009-06-17 15:50:33 +0200 | [diff] [blame] | 3316 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3317 | case 0x40 ... 0x4f: /* cmov */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3318 | c->dst.val = c->dst.orig_val = c->src.val; |
Laurent Vivier | a01af5e | 2007-09-24 11:10:56 +0200 | [diff] [blame] | 3319 | if (!test_cc(c->b, ctxt->eflags)) |
| 3320 | c->dst.type = OP_NONE; /* no writeback */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3321 | break; |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 3322 | case 0x80 ... 0x8f: /* jnz rel, etc*/ |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3323 | if (test_cc(c->b, ctxt->eflags)) |
Gleb Natapov | b2833e3 | 2009-04-12 13:36:30 +0300 | [diff] [blame] | 3324 | jmp_rel(c, c->src.val); |
Avi Kivity | 018a98d | 2007-11-27 19:30:56 +0200 | [diff] [blame] | 3325 | c->dst.type = OP_NONE; |
| 3326 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3327 | case 0xa0: /* push fs */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3328 | emulate_push_sreg(ctxt, ops, VCPU_SREG_FS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3329 | break; |
| 3330 | case 0xa1: /* pop fs */ |
| 3331 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3332 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3333 | goto done; |
| 3334 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3335 | case 0xa3: |
| 3336 | bt: /* bt */ |
Qing He | e4f8e03 | 2007-09-24 17:22:13 +0800 | [diff] [blame] | 3337 | c->dst.type = OP_NONE; |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3338 | /* only subword offset */ |
| 3339 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3340 | emulate_2op_SrcV_nobyte("bt", c->src, c->dst, ctxt->eflags); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3341 | break; |
Guillaume Thouvenin | 9bf8ea4 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 3342 | case 0xa4: /* shld imm8, r, r/m */ |
| 3343 | case 0xa5: /* shld cl, r, r/m */ |
| 3344 | emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags); |
| 3345 | break; |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3346 | case 0xa8: /* push gs */ |
Gleb Natapov | 79168fd | 2010-04-28 19:15:30 +0300 | [diff] [blame] | 3347 | emulate_push_sreg(ctxt, ops, VCPU_SREG_GS); |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3348 | break; |
| 3349 | case 0xa9: /* pop gs */ |
| 3350 | rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3351 | if (rc != X86EMUL_CONTINUE) |
Mohammed Gamal | 0934ac9 | 2009-08-23 14:24:24 +0300 | [diff] [blame] | 3352 | goto done; |
| 3353 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3354 | case 0xab: |
| 3355 | bts: /* bts */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3356 | /* only subword offset */ |
| 3357 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3358 | emulate_2op_SrcV_nobyte("bts", c->src, c->dst, ctxt->eflags); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3359 | break; |
Guillaume Thouvenin | 9bf8ea4 | 2008-12-04 14:30:13 +0100 | [diff] [blame] | 3360 | case 0xac: /* shrd imm8, r, r/m */ |
| 3361 | case 0xad: /* shrd cl, r, r/m */ |
| 3362 | emulate_2op_cl("shrd", c->src2, c->src, c->dst, ctxt->eflags); |
| 3363 | break; |
Glauber Costa | 2a7c5b8 | 2008-07-10 17:08:15 -0300 | [diff] [blame] | 3364 | case 0xae: /* clflush */ |
| 3365 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3366 | case 0xb0 ... 0xb1: /* cmpxchg */ |
| 3367 | /* |
| 3368 | * Save real source value, then compare EAX against |
| 3369 | * destination. |
| 3370 | */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3371 | c->src.orig_val = c->src.val; |
| 3372 | c->src.val = c->regs[VCPU_REGS_RAX]; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3373 | emulate_2op_SrcV("cmp", c->src, c->dst, ctxt->eflags); |
| 3374 | if (ctxt->eflags & EFLG_ZF) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3375 | /* Success: write back to memory. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3376 | c->dst.val = c->src.orig_val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3377 | } else { |
| 3378 | /* Failure: write the value we saw to EAX. */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3379 | c->dst.type = OP_REG; |
| 3380 | c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3381 | } |
| 3382 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3383 | case 0xb3: |
| 3384 | btr: /* btr */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3385 | /* only subword offset */ |
| 3386 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3387 | emulate_2op_SrcV_nobyte("btr", c->src, c->dst, ctxt->eflags); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3388 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3389 | case 0xb6 ... 0xb7: /* movzx */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3390 | c->dst.bytes = c->op_bytes; |
| 3391 | c->dst.val = (c->d & ByteOp) ? (u8) c->src.val |
| 3392 | : (u16) c->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3393 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3394 | case 0xba: /* Grp8 */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3395 | switch (c->modrm_reg & 3) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3396 | case 0: |
| 3397 | goto bt; |
| 3398 | case 1: |
| 3399 | goto bts; |
| 3400 | case 2: |
| 3401 | goto btr; |
| 3402 | case 3: |
| 3403 | goto btc; |
| 3404 | } |
| 3405 | break; |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3406 | case 0xbb: |
| 3407 | btc: /* btc */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3408 | /* only subword offset */ |
| 3409 | c->src.val &= (c->dst.bytes << 3) - 1; |
Laurent Vivier | 05f086f | 2007-09-24 11:10:55 +0200 | [diff] [blame] | 3410 | emulate_2op_SrcV_nobyte("btc", c->src, c->dst, ctxt->eflags); |
Nitin A Kamble | 7de7524 | 2007-09-15 10:13:07 +0300 | [diff] [blame] | 3411 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3412 | case 0xbe ... 0xbf: /* movsx */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3413 | c->dst.bytes = c->op_bytes; |
| 3414 | c->dst.val = (c->d & ByteOp) ? (s8) c->src.val : |
| 3415 | (s16) c->src.val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3416 | break; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 3417 | case 0xc3: /* movnti */ |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3418 | c->dst.bytes = c->op_bytes; |
| 3419 | c->dst.val = (c->op_bytes == 4) ? (u32) c->src.val : |
| 3420 | (u64) c->src.val; |
Sheng Yang | a012e65 | 2007-10-15 14:24:20 +0800 | [diff] [blame] | 3421 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3422 | case 0xc7: /* Grp9 (cmpxchg8b) */ |
Gleb Natapov | 69f55cb | 2010-03-18 15:20:20 +0200 | [diff] [blame] | 3423 | rc = emulate_grp9(ctxt, ops); |
Takuya Yoshikawa | 1b30eaa | 2010-02-12 15:57:56 +0900 | [diff] [blame] | 3424 | if (rc != X86EMUL_CONTINUE) |
Laurent Vivier | 8cdbd2c | 2007-09-24 11:10:54 +0200 | [diff] [blame] | 3425 | goto done; |
| 3426 | break; |
Avi Kivity | 91269b8 | 2010-07-25 14:51:16 +0300 | [diff] [blame] | 3427 | default: |
| 3428 | goto cannot_emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3429 | } |
| 3430 | goto writeback; |
| 3431 | |
| 3432 | cannot_emulate: |
Laurent Vivier | e4e03de | 2007-09-18 11:52:50 +0200 | [diff] [blame] | 3433 | DPRINTF("Cannot emulate %02x\n", c->b); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3434 | return -1; |
| 3435 | } |