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