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