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