]>
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> |
b7d491e7 | 27 | #include <linux/stringify.h> |
6aa8b732 | 28 | |
3eeb3288 | 29 | #include "x86.h" |
38ba30ba | 30 | #include "tss.h" |
e99f0507 | 31 | |
a9945549 AK |
32 | /* |
33 | * Operand types | |
34 | */ | |
b1ea50b2 AK |
35 | #define OpNone 0ull |
36 | #define OpImplicit 1ull /* No generic decode */ | |
37 | #define OpReg 2ull /* Register */ | |
38 | #define OpMem 3ull /* Memory */ | |
39 | #define OpAcc 4ull /* Accumulator: AL/AX/EAX/RAX */ | |
40 | #define OpDI 5ull /* ES:DI/EDI/RDI */ | |
41 | #define OpMem64 6ull /* Memory, 64-bit */ | |
42 | #define OpImmUByte 7ull /* Zero-extended 8-bit immediate */ | |
43 | #define OpDX 8ull /* DX register */ | |
4dd6a57d AK |
44 | #define OpCL 9ull /* CL register (for shifts) */ |
45 | #define OpImmByte 10ull /* 8-bit sign extended immediate */ | |
46 | #define OpOne 11ull /* Implied 1 */ | |
5e2c6883 | 47 | #define OpImm 12ull /* Sign extended up to 32-bit immediate */ |
0fe59128 AK |
48 | #define OpMem16 13ull /* Memory operand (16-bit). */ |
49 | #define OpMem32 14ull /* Memory operand (32-bit). */ | |
50 | #define OpImmU 15ull /* Immediate operand, zero extended */ | |
51 | #define OpSI 16ull /* SI/ESI/RSI */ | |
52 | #define OpImmFAddr 17ull /* Immediate far address */ | |
53 | #define OpMemFAddr 18ull /* Far address in memory */ | |
54 | #define OpImmU16 19ull /* Immediate operand, 16 bits, zero extended */ | |
c191a7a0 AK |
55 | #define OpES 20ull /* ES */ |
56 | #define OpCS 21ull /* CS */ | |
57 | #define OpSS 22ull /* SS */ | |
58 | #define OpDS 23ull /* DS */ | |
59 | #define OpFS 24ull /* FS */ | |
60 | #define OpGS 25ull /* GS */ | |
28867cee | 61 | #define OpMem8 26ull /* 8-bit zero extended memory operand */ |
5e2c6883 | 62 | #define OpImm64 27ull /* Sign extended 16/32/64-bit immediate */ |
0fe59128 AK |
63 | |
64 | #define OpBits 5 /* Width of operand field */ | |
b1ea50b2 | 65 | #define OpMask ((1ull << OpBits) - 1) |
a9945549 | 66 | |
6aa8b732 AK |
67 | /* |
68 | * Opcode effective-address decode tables. | |
69 | * Note that we only emulate instructions that have at least one memory | |
70 | * operand (excluding implicit stack references). We assume that stack | |
71 | * references and instruction fetches will never occur in special memory | |
72 | * areas that require emulation. So, for example, 'mov <imm>,<reg>' need | |
73 | * not be handled. | |
74 | */ | |
75 | ||
76 | /* Operand sizes: 8-bit operands or specified/overridden size. */ | |
ab85b12b | 77 | #define ByteOp (1<<0) /* 8-bit operands. */ |
6aa8b732 | 78 | /* Destination operand type. */ |
a9945549 AK |
79 | #define DstShift 1 |
80 | #define ImplicitOps (OpImplicit << DstShift) | |
81 | #define DstReg (OpReg << DstShift) | |
82 | #define DstMem (OpMem << DstShift) | |
83 | #define DstAcc (OpAcc << DstShift) | |
84 | #define DstDI (OpDI << DstShift) | |
85 | #define DstMem64 (OpMem64 << DstShift) | |
86 | #define DstImmUByte (OpImmUByte << DstShift) | |
87 | #define DstDX (OpDX << DstShift) | |
88 | #define DstMask (OpMask << DstShift) | |
6aa8b732 | 89 | /* Source operand type. */ |
0fe59128 AK |
90 | #define SrcShift 6 |
91 | #define SrcNone (OpNone << SrcShift) | |
92 | #define SrcReg (OpReg << SrcShift) | |
93 | #define SrcMem (OpMem << SrcShift) | |
94 | #define SrcMem16 (OpMem16 << SrcShift) | |
95 | #define SrcMem32 (OpMem32 << SrcShift) | |
96 | #define SrcImm (OpImm << SrcShift) | |
97 | #define SrcImmByte (OpImmByte << SrcShift) | |
98 | #define SrcOne (OpOne << SrcShift) | |
99 | #define SrcImmUByte (OpImmUByte << SrcShift) | |
100 | #define SrcImmU (OpImmU << SrcShift) | |
101 | #define SrcSI (OpSI << SrcShift) | |
102 | #define SrcImmFAddr (OpImmFAddr << SrcShift) | |
103 | #define SrcMemFAddr (OpMemFAddr << SrcShift) | |
104 | #define SrcAcc (OpAcc << SrcShift) | |
105 | #define SrcImmU16 (OpImmU16 << SrcShift) | |
5e2c6883 | 106 | #define SrcImm64 (OpImm64 << SrcShift) |
0fe59128 | 107 | #define SrcDX (OpDX << SrcShift) |
28867cee | 108 | #define SrcMem8 (OpMem8 << SrcShift) |
0fe59128 | 109 | #define SrcMask (OpMask << SrcShift) |
221192bd MT |
110 | #define BitOp (1<<11) |
111 | #define MemAbs (1<<12) /* Memory operand is absolute displacement */ | |
112 | #define String (1<<13) /* String instruction (rep capable) */ | |
113 | #define Stack (1<<14) /* Stack instruction (push/pop) */ | |
114 | #define GroupMask (7<<15) /* Opcode uses one of the group mechanisms */ | |
115 | #define Group (1<<15) /* Bits 3:5 of modrm byte extend opcode */ | |
116 | #define GroupDual (2<<15) /* Alternate decoding of mod == 3 */ | |
117 | #define Prefix (3<<15) /* Instruction varies with 66/f2/f3 prefix */ | |
118 | #define RMExt (4<<15) /* Opcode extension in ModRM r/m if mod == 3 */ | |
045a282c | 119 | #define Escape (5<<15) /* Escape to coprocessor instruction */ |
221192bd | 120 | #define Sse (1<<18) /* SSE Vector instruction */ |
20c29ff2 AK |
121 | /* Generic ModRM decode. */ |
122 | #define ModRM (1<<19) | |
123 | /* Destination is only written; never read. */ | |
124 | #define Mov (1<<20) | |
d8769fed | 125 | /* Misc flags */ |
8ea7d6ae | 126 | #define Prot (1<<21) /* instruction generates #UD if not in prot-mode */ |
d867162c | 127 | #define VendorSpecific (1<<22) /* Vendor specific instruction */ |
5a506b12 | 128 | #define NoAccess (1<<23) /* Don't access memory (lea/invlpg/verr etc) */ |
7f9b4b75 | 129 | #define Op3264 (1<<24) /* Operand is 64b in long mode, 32b otherwise */ |
047a4818 | 130 | #define Undefined (1<<25) /* No Such Instruction */ |
d380a5e4 | 131 | #define Lock (1<<26) /* lock prefix is allowed for the instruction */ |
e92805ac | 132 | #define Priv (1<<27) /* instruction generates #GP if current CPL != 0 */ |
d8769fed | 133 | #define No64 (1<<28) |
d5ae7ce8 | 134 | #define PageTable (1 << 29) /* instruction used to write page table */ |
0dc8d10f | 135 | /* Source 2 operand type */ |
d5ae7ce8 | 136 | #define Src2Shift (30) |
4dd6a57d AK |
137 | #define Src2None (OpNone << Src2Shift) |
138 | #define Src2CL (OpCL << Src2Shift) | |
139 | #define Src2ImmByte (OpImmByte << Src2Shift) | |
140 | #define Src2One (OpOne << Src2Shift) | |
141 | #define Src2Imm (OpImm << Src2Shift) | |
c191a7a0 AK |
142 | #define Src2ES (OpES << Src2Shift) |
143 | #define Src2CS (OpCS << Src2Shift) | |
144 | #define Src2SS (OpSS << Src2Shift) | |
145 | #define Src2DS (OpDS << Src2Shift) | |
146 | #define Src2FS (OpFS << Src2Shift) | |
147 | #define Src2GS (OpGS << Src2Shift) | |
4dd6a57d | 148 | #define Src2Mask (OpMask << Src2Shift) |
cbe2c9d3 | 149 | #define Mmx ((u64)1 << 40) /* MMX Vector instruction */ |
1c11b376 AK |
150 | #define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */ |
151 | #define Unaligned ((u64)1 << 42) /* Explicitly unaligned (e.g. MOVDQU) */ | |
152 | #define Avx ((u64)1 << 43) /* Advanced Vector Extensions */ | |
e28bbd44 | 153 | #define Fastop ((u64)1 << 44) /* Use opcode::u.fastop */ |
b6744dc3 | 154 | #define NoWrite ((u64)1 << 45) /* No writeback */ |
6aa8b732 | 155 | |
d0e53325 AK |
156 | #define X2(x...) x, x |
157 | #define X3(x...) X2(x), x | |
158 | #define X4(x...) X2(x), X2(x) | |
159 | #define X5(x...) X4(x), x | |
160 | #define X6(x...) X4(x), X2(x) | |
161 | #define X7(x...) X4(x), X3(x) | |
162 | #define X8(x...) X4(x), X4(x) | |
163 | #define X16(x...) X8(x), X8(x) | |
83babbca | 164 | |
e28bbd44 AK |
165 | #define NR_FASTOP (ilog2(sizeof(ulong)) + 1) |
166 | #define FASTOP_SIZE 8 | |
167 | ||
168 | /* | |
169 | * fastop functions have a special calling convention: | |
170 | * | |
171 | * dst: [rdx]:rax (in/out) | |
172 | * src: rbx (in/out) | |
173 | * src2: rcx (in) | |
174 | * flags: rflags (in/out) | |
175 | * | |
176 | * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for | |
177 | * different operand sizes can be reached by calculation, rather than a jump | |
178 | * table (which would be bigger than the code). | |
179 | * | |
180 | * fastop functions are declared as taking a never-defined fastop parameter, | |
181 | * so they can't be called from C directly. | |
182 | */ | |
183 | ||
184 | struct fastop; | |
185 | ||
d65b1dee | 186 | struct opcode { |
b1ea50b2 AK |
187 | u64 flags : 56; |
188 | u64 intercept : 8; | |
120df890 | 189 | union { |
ef65c889 | 190 | int (*execute)(struct x86_emulate_ctxt *ctxt); |
fd0a0d82 MK |
191 | const struct opcode *group; |
192 | const struct group_dual *gdual; | |
193 | const struct gprefix *gprefix; | |
045a282c | 194 | const struct escape *esc; |
e28bbd44 | 195 | void (*fastop)(struct fastop *fake); |
120df890 | 196 | } u; |
d09beabd | 197 | int (*check_perm)(struct x86_emulate_ctxt *ctxt); |
120df890 AK |
198 | }; |
199 | ||
200 | struct group_dual { | |
201 | struct opcode mod012[8]; | |
202 | struct opcode mod3[8]; | |
d65b1dee AK |
203 | }; |
204 | ||
0d7cdee8 AK |
205 | struct gprefix { |
206 | struct opcode pfx_no; | |
207 | struct opcode pfx_66; | |
208 | struct opcode pfx_f2; | |
209 | struct opcode pfx_f3; | |
210 | }; | |
211 | ||
045a282c GN |
212 | struct escape { |
213 | struct opcode op[8]; | |
214 | struct opcode high[64]; | |
215 | }; | |
216 | ||
6aa8b732 | 217 | /* EFLAGS bit definitions. */ |
d4c6a154 GN |
218 | #define EFLG_ID (1<<21) |
219 | #define EFLG_VIP (1<<20) | |
220 | #define EFLG_VIF (1<<19) | |
221 | #define EFLG_AC (1<<18) | |
b1d86143 AP |
222 | #define EFLG_VM (1<<17) |
223 | #define EFLG_RF (1<<16) | |
d4c6a154 GN |
224 | #define EFLG_IOPL (3<<12) |
225 | #define EFLG_NT (1<<14) | |
6aa8b732 AK |
226 | #define EFLG_OF (1<<11) |
227 | #define EFLG_DF (1<<10) | |
b1d86143 | 228 | #define EFLG_IF (1<<9) |
d4c6a154 | 229 | #define EFLG_TF (1<<8) |
6aa8b732 AK |
230 | #define EFLG_SF (1<<7) |
231 | #define EFLG_ZF (1<<6) | |
232 | #define EFLG_AF (1<<4) | |
233 | #define EFLG_PF (1<<2) | |
234 | #define EFLG_CF (1<<0) | |
235 | ||
62bd430e MG |
236 | #define EFLG_RESERVED_ZEROS_MASK 0xffc0802a |
237 | #define EFLG_RESERVED_ONE_MASK 2 | |
238 | ||
dd856efa AK |
239 | static ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr) |
240 | { | |
241 | if (!(ctxt->regs_valid & (1 << nr))) { | |
242 | ctxt->regs_valid |= 1 << nr; | |
243 | ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr); | |
244 | } | |
245 | return ctxt->_regs[nr]; | |
246 | } | |
247 | ||
248 | static ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr) | |
249 | { | |
250 | ctxt->regs_valid |= 1 << nr; | |
251 | ctxt->regs_dirty |= 1 << nr; | |
252 | return &ctxt->_regs[nr]; | |
253 | } | |
254 | ||
255 | static ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr) | |
256 | { | |
257 | reg_read(ctxt, nr); | |
258 | return reg_write(ctxt, nr); | |
259 | } | |
260 | ||
261 | static void writeback_registers(struct x86_emulate_ctxt *ctxt) | |
262 | { | |
263 | unsigned reg; | |
264 | ||
265 | for_each_set_bit(reg, (ulong *)&ctxt->regs_dirty, 16) | |
266 | ctxt->ops->write_gpr(ctxt, reg, ctxt->_regs[reg]); | |
267 | } | |
268 | ||
269 | static void invalidate_registers(struct x86_emulate_ctxt *ctxt) | |
270 | { | |
271 | ctxt->regs_dirty = 0; | |
272 | ctxt->regs_valid = 0; | |
273 | } | |
274 | ||
6aa8b732 AK |
275 | /* |
276 | * Instruction emulation: | |
277 | * Most instructions are emulated directly via a fragment of inline assembly | |
278 | * code. This allows us to save/restore EFLAGS and thus very easily pick up | |
279 | * any modified flags. | |
280 | */ | |
281 | ||
05b3e0c2 | 282 | #if defined(CONFIG_X86_64) |
6aa8b732 AK |
283 | #define _LO32 "k" /* force 32-bit operand */ |
284 | #define _STK "%%rsp" /* stack pointer */ | |
285 | #elif defined(__i386__) | |
286 | #define _LO32 "" /* force 32-bit operand */ | |
287 | #define _STK "%%esp" /* stack pointer */ | |
288 | #endif | |
289 | ||
290 | /* | |
291 | * These EFLAGS bits are restored from saved value during emulation, and | |
292 | * any changes are written back to the saved value after emulation. | |
293 | */ | |
294 | #define EFLAGS_MASK (EFLG_OF|EFLG_SF|EFLG_ZF|EFLG_AF|EFLG_PF|EFLG_CF) | |
295 | ||
296 | /* Before executing instruction: restore necessary bits in EFLAGS. */ | |
e934c9c1 AK |
297 | #define _PRE_EFLAGS(_sav, _msk, _tmp) \ |
298 | /* EFLAGS = (_sav & _msk) | (EFLAGS & ~_msk); _sav &= ~_msk; */ \ | |
299 | "movl %"_sav",%"_LO32 _tmp"; " \ | |
300 | "push %"_tmp"; " \ | |
301 | "push %"_tmp"; " \ | |
302 | "movl %"_msk",%"_LO32 _tmp"; " \ | |
303 | "andl %"_LO32 _tmp",("_STK"); " \ | |
304 | "pushf; " \ | |
305 | "notl %"_LO32 _tmp"; " \ | |
306 | "andl %"_LO32 _tmp",("_STK"); " \ | |
307 | "andl %"_LO32 _tmp","__stringify(BITS_PER_LONG/4)"("_STK"); " \ | |
308 | "pop %"_tmp"; " \ | |
309 | "orl %"_LO32 _tmp",("_STK"); " \ | |
310 | "popf; " \ | |
311 | "pop %"_sav"; " | |
6aa8b732 AK |
312 | |
313 | /* After executing instruction: write-back necessary bits in EFLAGS. */ | |
314 | #define _POST_EFLAGS(_sav, _msk, _tmp) \ | |
315 | /* _sav |= EFLAGS & _msk; */ \ | |
316 | "pushf; " \ | |
317 | "pop %"_tmp"; " \ | |
318 | "andl %"_msk",%"_LO32 _tmp"; " \ | |
319 | "orl %"_LO32 _tmp",%"_sav"; " | |
320 | ||
dda96d8f AK |
321 | #ifdef CONFIG_X86_64 |
322 | #define ON64(x) x | |
323 | #else | |
324 | #define ON64(x) | |
325 | #endif | |
326 | ||
a31b9cea | 327 | #define ____emulate_2op(ctxt, _op, _x, _y, _suffix, _dsttype) \ |
6b7ad61f AK |
328 | do { \ |
329 | __asm__ __volatile__ ( \ | |
330 | _PRE_EFLAGS("0", "4", "2") \ | |
331 | _op _suffix " %"_x"3,%1; " \ | |
332 | _POST_EFLAGS("0", "4", "2") \ | |
a31b9cea AK |
333 | : "=m" ((ctxt)->eflags), \ |
334 | "+q" (*(_dsttype*)&(ctxt)->dst.val), \ | |
6b7ad61f | 335 | "=&r" (_tmp) \ |
a31b9cea | 336 | : _y ((ctxt)->src.val), "i" (EFLAGS_MASK)); \ |
f3fd92fb | 337 | } while (0) |
6b7ad61f AK |
338 | |
339 | ||
6aa8b732 | 340 | /* Raw emulation: instruction has two explicit operands. */ |
a31b9cea | 341 | #define __emulate_2op_nobyte(ctxt,_op,_wx,_wy,_lx,_ly,_qx,_qy) \ |
6b7ad61f AK |
342 | do { \ |
343 | unsigned long _tmp; \ | |
344 | \ | |
a31b9cea | 345 | switch ((ctxt)->dst.bytes) { \ |
6b7ad61f | 346 | case 2: \ |
a31b9cea | 347 | ____emulate_2op(ctxt,_op,_wx,_wy,"w",u16); \ |
6b7ad61f AK |
348 | break; \ |
349 | case 4: \ | |
a31b9cea | 350 | ____emulate_2op(ctxt,_op,_lx,_ly,"l",u32); \ |
6b7ad61f AK |
351 | break; \ |
352 | case 8: \ | |
a31b9cea | 353 | ON64(____emulate_2op(ctxt,_op,_qx,_qy,"q",u64)); \ |
6b7ad61f AK |
354 | break; \ |
355 | } \ | |
6aa8b732 AK |
356 | } while (0) |
357 | ||
a31b9cea | 358 | #define __emulate_2op(ctxt,_op,_bx,_by,_wx,_wy,_lx,_ly,_qx,_qy) \ |
6aa8b732 | 359 | do { \ |
6b7ad61f | 360 | unsigned long _tmp; \ |
a31b9cea | 361 | switch ((ctxt)->dst.bytes) { \ |
6aa8b732 | 362 | case 1: \ |
a31b9cea | 363 | ____emulate_2op(ctxt,_op,_bx,_by,"b",u8); \ |
6aa8b732 AK |
364 | break; \ |
365 | default: \ | |
a31b9cea | 366 | __emulate_2op_nobyte(ctxt, _op, \ |
6aa8b732 AK |
367 | _wx, _wy, _lx, _ly, _qx, _qy); \ |
368 | break; \ | |
369 | } \ | |
370 | } while (0) | |
371 | ||
372 | /* Source operand is byte-sized and may be restricted to just %cl. */ | |
a31b9cea AK |
373 | #define emulate_2op_SrcB(ctxt, _op) \ |
374 | __emulate_2op(ctxt, _op, "b", "c", "b", "c", "b", "c", "b", "c") | |
6aa8b732 AK |
375 | |
376 | /* Source operand is byte, word, long or quad sized. */ | |
a31b9cea AK |
377 | #define emulate_2op_SrcV(ctxt, _op) \ |
378 | __emulate_2op(ctxt, _op, "b", "q", "w", "r", _LO32, "r", "", "r") | |
6aa8b732 AK |
379 | |
380 | /* Source operand is word, long or quad sized. */ | |
a31b9cea AK |
381 | #define emulate_2op_SrcV_nobyte(ctxt, _op) \ |
382 | __emulate_2op_nobyte(ctxt, _op, "w", "r", _LO32, "r", "", "r") | |
6aa8b732 | 383 | |
d175226a | 384 | /* Instruction has three operands and one operand is stored in ECX register */ |
29053a60 | 385 | #define __emulate_2op_cl(ctxt, _op, _suffix, _type) \ |
7295261c AK |
386 | do { \ |
387 | unsigned long _tmp; \ | |
761441b9 AK |
388 | _type _clv = (ctxt)->src2.val; \ |
389 | _type _srcv = (ctxt)->src.val; \ | |
390 | _type _dstv = (ctxt)->dst.val; \ | |
7295261c AK |
391 | \ |
392 | __asm__ __volatile__ ( \ | |
393 | _PRE_EFLAGS("0", "5", "2") \ | |
394 | _op _suffix " %4,%1 \n" \ | |
395 | _POST_EFLAGS("0", "5", "2") \ | |
761441b9 | 396 | : "=m" ((ctxt)->eflags), "+r" (_dstv), "=&r" (_tmp) \ |
7295261c AK |
397 | : "c" (_clv) , "r" (_srcv), "i" (EFLAGS_MASK) \ |
398 | ); \ | |
399 | \ | |
761441b9 AK |
400 | (ctxt)->src2.val = (unsigned long) _clv; \ |
401 | (ctxt)->src2.val = (unsigned long) _srcv; \ | |
402 | (ctxt)->dst.val = (unsigned long) _dstv; \ | |
d175226a GT |
403 | } while (0) |
404 | ||
761441b9 | 405 | #define emulate_2op_cl(ctxt, _op) \ |
7295261c | 406 | do { \ |
761441b9 | 407 | switch ((ctxt)->dst.bytes) { \ |
7295261c | 408 | case 2: \ |
29053a60 | 409 | __emulate_2op_cl(ctxt, _op, "w", u16); \ |
7295261c AK |
410 | break; \ |
411 | case 4: \ | |
29053a60 | 412 | __emulate_2op_cl(ctxt, _op, "l", u32); \ |
7295261c AK |
413 | break; \ |
414 | case 8: \ | |
29053a60 | 415 | ON64(__emulate_2op_cl(ctxt, _op, "q", ulong)); \ |
7295261c AK |
416 | break; \ |
417 | } \ | |
d175226a GT |
418 | } while (0) |
419 | ||
d1eef45d | 420 | #define __emulate_1op(ctxt, _op, _suffix) \ |
6aa8b732 AK |
421 | do { \ |
422 | unsigned long _tmp; \ | |
423 | \ | |
dda96d8f AK |
424 | __asm__ __volatile__ ( \ |
425 | _PRE_EFLAGS("0", "3", "2") \ | |
426 | _op _suffix " %1; " \ | |
427 | _POST_EFLAGS("0", "3", "2") \ | |
d1eef45d | 428 | : "=m" ((ctxt)->eflags), "+m" ((ctxt)->dst.val), \ |
dda96d8f AK |
429 | "=&r" (_tmp) \ |
430 | : "i" (EFLAGS_MASK)); \ | |
431 | } while (0) | |
432 | ||
433 | /* Instruction has only one explicit operand (no source operand). */ | |
d1eef45d | 434 | #define emulate_1op(ctxt, _op) \ |
dda96d8f | 435 | do { \ |
d1eef45d AK |
436 | switch ((ctxt)->dst.bytes) { \ |
437 | case 1: __emulate_1op(ctxt, _op, "b"); break; \ | |
438 | case 2: __emulate_1op(ctxt, _op, "w"); break; \ | |
439 | case 4: __emulate_1op(ctxt, _op, "l"); break; \ | |
440 | case 8: ON64(__emulate_1op(ctxt, _op, "q")); break; \ | |
6aa8b732 AK |
441 | } \ |
442 | } while (0) | |
443 | ||
b7d491e7 AK |
444 | #define FOP_ALIGN ".align " __stringify(FASTOP_SIZE) " \n\t" |
445 | #define FOP_RET "ret \n\t" | |
446 | ||
447 | #define FOP_START(op) \ | |
448 | extern void em_##op(struct fastop *fake); \ | |
449 | asm(".pushsection .text, \"ax\" \n\t" \ | |
450 | ".global em_" #op " \n\t" \ | |
451 | FOP_ALIGN \ | |
452 | "em_" #op ": \n\t" | |
453 | ||
454 | #define FOP_END \ | |
455 | ".popsection") | |
456 | ||
0bdea068 AK |
457 | #define FOPNOP() FOP_ALIGN FOP_RET |
458 | ||
b7d491e7 AK |
459 | #define FOP1E(op, dst) \ |
460 | FOP_ALIGN #op " %" #dst " \n\t" FOP_RET | |
461 | ||
462 | #define FASTOP1(op) \ | |
463 | FOP_START(op) \ | |
464 | FOP1E(op##b, al) \ | |
465 | FOP1E(op##w, ax) \ | |
466 | FOP1E(op##l, eax) \ | |
467 | ON64(FOP1E(op##q, rax)) \ | |
468 | FOP_END | |
469 | ||
f7857f35 AK |
470 | #define FOP2E(op, dst, src) \ |
471 | FOP_ALIGN #op " %" #src ", %" #dst " \n\t" FOP_RET | |
472 | ||
473 | #define FASTOP2(op) \ | |
474 | FOP_START(op) \ | |
475 | FOP2E(op##b, al, bl) \ | |
476 | FOP2E(op##w, ax, bx) \ | |
477 | FOP2E(op##l, eax, ebx) \ | |
478 | ON64(FOP2E(op##q, rax, rbx)) \ | |
479 | FOP_END | |
480 | ||
007a3b54 AK |
481 | /* 2 operand, src is CL */ |
482 | #define FASTOP2CL(op) \ | |
483 | FOP_START(op) \ | |
484 | FOP2E(op##b, al, cl) \ | |
485 | FOP2E(op##w, ax, cl) \ | |
486 | FOP2E(op##l, eax, cl) \ | |
487 | ON64(FOP2E(op##q, rax, cl)) \ | |
488 | FOP_END | |
489 | ||
0bdea068 AK |
490 | #define FOP3E(op, dst, src, src2) \ |
491 | FOP_ALIGN #op " %" #src2 ", %" #src ", %" #dst " \n\t" FOP_RET | |
492 | ||
493 | /* 3-operand, word-only, src2=cl */ | |
494 | #define FASTOP3WCL(op) \ | |
495 | FOP_START(op) \ | |
496 | FOPNOP() \ | |
497 | FOP3E(op##w, ax, bx, cl) \ | |
498 | FOP3E(op##l, eax, ebx, cl) \ | |
499 | ON64(FOP3E(op##q, rax, rbx, cl)) \ | |
500 | FOP_END | |
501 | ||
9ae9feba AK |
502 | /* Special case for SETcc - 1 instruction per cc */ |
503 | #define FOP_SETCC(op) ".align 4; " #op " %al; ret \n\t" | |
504 | ||
505 | FOP_START(setcc) | |
506 | FOP_SETCC(seto) | |
507 | FOP_SETCC(setno) | |
508 | FOP_SETCC(setc) | |
509 | FOP_SETCC(setnc) | |
510 | FOP_SETCC(setz) | |
511 | FOP_SETCC(setnz) | |
512 | FOP_SETCC(setbe) | |
513 | FOP_SETCC(setnbe) | |
514 | FOP_SETCC(sets) | |
515 | FOP_SETCC(setns) | |
516 | FOP_SETCC(setp) | |
517 | FOP_SETCC(setnp) | |
518 | FOP_SETCC(setl) | |
519 | FOP_SETCC(setnl) | |
520 | FOP_SETCC(setle) | |
521 | FOP_SETCC(setnle) | |
522 | FOP_END; | |
523 | ||
e8f2b1d6 | 524 | #define __emulate_1op_rax_rdx(ctxt, _op, _suffix, _ex) \ |
f6b3597b AK |
525 | do { \ |
526 | unsigned long _tmp; \ | |
dd856efa AK |
527 | ulong *rax = reg_rmw((ctxt), VCPU_REGS_RAX); \ |
528 | ulong *rdx = reg_rmw((ctxt), VCPU_REGS_RDX); \ | |
f6b3597b AK |
529 | \ |
530 | __asm__ __volatile__ ( \ | |
531 | _PRE_EFLAGS("0", "5", "1") \ | |
532 | "1: \n\t" \ | |
533 | _op _suffix " %6; " \ | |
534 | "2: \n\t" \ | |
535 | _POST_EFLAGS("0", "5", "1") \ | |
536 | ".pushsection .fixup,\"ax\" \n\t" \ | |
537 | "3: movb $1, %4 \n\t" \ | |
538 | "jmp 2b \n\t" \ | |
539 | ".popsection \n\t" \ | |
540 | _ASM_EXTABLE(1b, 3b) \ | |
e8f2b1d6 AK |
541 | : "=m" ((ctxt)->eflags), "=&r" (_tmp), \ |
542 | "+a" (*rax), "+d" (*rdx), "+qm"(_ex) \ | |
cb7cb286 | 543 | : "i" (EFLAGS_MASK), "m" ((ctxt)->src.val)); \ |
f6b3597b AK |
544 | } while (0) |
545 | ||
3f9f53b0 | 546 | /* instruction has only one source operand, destination is implicit (e.g. mul, div, imul, idiv) */ |
e8f2b1d6 | 547 | #define emulate_1op_rax_rdx(ctxt, _op, _ex) \ |
7295261c | 548 | do { \ |
e8f2b1d6 | 549 | switch((ctxt)->src.bytes) { \ |
7295261c | 550 | case 1: \ |
e8f2b1d6 | 551 | __emulate_1op_rax_rdx(ctxt, _op, "b", _ex); \ |
7295261c AK |
552 | break; \ |
553 | case 2: \ | |
e8f2b1d6 | 554 | __emulate_1op_rax_rdx(ctxt, _op, "w", _ex); \ |
7295261c AK |
555 | break; \ |
556 | case 4: \ | |
e8f2b1d6 | 557 | __emulate_1op_rax_rdx(ctxt, _op, "l", _ex); \ |
f6b3597b AK |
558 | break; \ |
559 | case 8: ON64( \ | |
e8f2b1d6 | 560 | __emulate_1op_rax_rdx(ctxt, _op, "q", _ex)); \ |
f6b3597b AK |
561 | break; \ |
562 | } \ | |
563 | } while (0) | |
564 | ||
8a76d7f2 JR |
565 | static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt, |
566 | enum x86_intercept intercept, | |
567 | enum x86_intercept_stage stage) | |
568 | { | |
569 | struct x86_instruction_info info = { | |
570 | .intercept = intercept, | |
9dac77fa AK |
571 | .rep_prefix = ctxt->rep_prefix, |
572 | .modrm_mod = ctxt->modrm_mod, | |
573 | .modrm_reg = ctxt->modrm_reg, | |
574 | .modrm_rm = ctxt->modrm_rm, | |
575 | .src_val = ctxt->src.val64, | |
576 | .src_bytes = ctxt->src.bytes, | |
577 | .dst_bytes = ctxt->dst.bytes, | |
578 | .ad_bytes = ctxt->ad_bytes, | |
8a76d7f2 JR |
579 | .next_rip = ctxt->eip, |
580 | }; | |
581 | ||
2953538e | 582 | return ctxt->ops->intercept(ctxt, &info, stage); |
8a76d7f2 JR |
583 | } |
584 | ||
f47cfa31 AK |
585 | static void assign_masked(ulong *dest, ulong src, ulong mask) |
586 | { | |
587 | *dest = (*dest & ~mask) | (src & mask); | |
588 | } | |
589 | ||
9dac77fa | 590 | static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt) |
ddcb2885 | 591 | { |
9dac77fa | 592 | return (1UL << (ctxt->ad_bytes << 3)) - 1; |
ddcb2885 HH |
593 | } |
594 | ||
f47cfa31 AK |
595 | static ulong stack_mask(struct x86_emulate_ctxt *ctxt) |
596 | { | |
597 | u16 sel; | |
598 | struct desc_struct ss; | |
599 | ||
600 | if (ctxt->mode == X86EMUL_MODE_PROT64) | |
601 | return ~0UL; | |
602 | ctxt->ops->get_segment(ctxt, &sel, &ss, NULL, VCPU_SREG_SS); | |
603 | return ~0U >> ((ss.d ^ 1) * 16); /* d=0: 0xffff; d=1: 0xffffffff */ | |
604 | } | |
605 | ||
612e89f0 AK |
606 | static int stack_size(struct x86_emulate_ctxt *ctxt) |
607 | { | |
608 | return (__fls(stack_mask(ctxt)) + 1) >> 3; | |
609 | } | |
610 | ||
6aa8b732 | 611 | /* Access/update address held in a register, based on addressing mode. */ |
e4706772 | 612 | static inline unsigned long |
9dac77fa | 613 | address_mask(struct x86_emulate_ctxt *ctxt, unsigned long reg) |
e4706772 | 614 | { |
9dac77fa | 615 | if (ctxt->ad_bytes == sizeof(unsigned long)) |
e4706772 HH |
616 | return reg; |
617 | else | |
9dac77fa | 618 | return reg & ad_mask(ctxt); |
e4706772 HH |
619 | } |
620 | ||
621 | static inline unsigned long | |
9dac77fa | 622 | register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg) |
e4706772 | 623 | { |
9dac77fa | 624 | return address_mask(ctxt, reg); |
e4706772 HH |
625 | } |
626 | ||
5ad105e5 AK |
627 | static void masked_increment(ulong *reg, ulong mask, int inc) |
628 | { | |
629 | assign_masked(reg, *reg + inc, mask); | |
630 | } | |
631 | ||
7a957275 | 632 | static inline void |
9dac77fa | 633 | register_address_increment(struct x86_emulate_ctxt *ctxt, unsigned long *reg, int inc) |
7a957275 | 634 | { |
5ad105e5 AK |
635 | ulong mask; |
636 | ||
9dac77fa | 637 | if (ctxt->ad_bytes == sizeof(unsigned long)) |
5ad105e5 | 638 | mask = ~0UL; |
7a957275 | 639 | else |
5ad105e5 AK |
640 | mask = ad_mask(ctxt); |
641 | masked_increment(reg, mask, inc); | |
642 | } | |
643 | ||
644 | static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc) | |
645 | { | |
dd856efa | 646 | masked_increment(reg_rmw(ctxt, VCPU_REGS_RSP), stack_mask(ctxt), inc); |
7a957275 | 647 | } |
6aa8b732 | 648 | |
9dac77fa | 649 | static inline void jmp_rel(struct x86_emulate_ctxt *ctxt, int rel) |
7a957275 | 650 | { |
9dac77fa | 651 | register_address_increment(ctxt, &ctxt->_eip, rel); |
7a957275 | 652 | } |
098c937b | 653 | |
56697687 AK |
654 | static u32 desc_limit_scaled(struct desc_struct *desc) |
655 | { | |
656 | u32 limit = get_desc_limit(desc); | |
657 | ||
658 | return desc->g ? (limit << 12) | 0xfff : limit; | |
659 | } | |
660 | ||
9dac77fa | 661 | static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg) |
7a5b56df | 662 | { |
9dac77fa AK |
663 | ctxt->has_seg_override = true; |
664 | ctxt->seg_override = seg; | |
7a5b56df AK |
665 | } |
666 | ||
7b105ca2 | 667 | static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg) |
7a5b56df AK |
668 | { |
669 | if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS) | |
670 | return 0; | |
671 | ||
7b105ca2 | 672 | return ctxt->ops->get_cached_segment_base(ctxt, seg); |
7a5b56df AK |
673 | } |
674 | ||
9dac77fa | 675 | static unsigned seg_override(struct x86_emulate_ctxt *ctxt) |
7a5b56df | 676 | { |
9dac77fa | 677 | if (!ctxt->has_seg_override) |
7a5b56df AK |
678 | return 0; |
679 | ||
9dac77fa | 680 | return ctxt->seg_override; |
7a5b56df AK |
681 | } |
682 | ||
35d3d4a1 AK |
683 | static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec, |
684 | u32 error, bool valid) | |
54b8486f | 685 | { |
da9cb575 AK |
686 | ctxt->exception.vector = vec; |
687 | ctxt->exception.error_code = error; | |
688 | ctxt->exception.error_code_valid = valid; | |
35d3d4a1 | 689 | return X86EMUL_PROPAGATE_FAULT; |
54b8486f GN |
690 | } |
691 | ||
3b88e41a JR |
692 | static int emulate_db(struct x86_emulate_ctxt *ctxt) |
693 | { | |
694 | return emulate_exception(ctxt, DB_VECTOR, 0, false); | |
695 | } | |
696 | ||
35d3d4a1 | 697 | static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err) |
54b8486f | 698 | { |
35d3d4a1 | 699 | return emulate_exception(ctxt, GP_VECTOR, err, true); |
54b8486f GN |
700 | } |
701 | ||
618ff15d AK |
702 | static int emulate_ss(struct x86_emulate_ctxt *ctxt, int err) |
703 | { | |
704 | return emulate_exception(ctxt, SS_VECTOR, err, true); | |
705 | } | |
706 | ||
35d3d4a1 | 707 | static int emulate_ud(struct x86_emulate_ctxt *ctxt) |
54b8486f | 708 | { |
35d3d4a1 | 709 | return emulate_exception(ctxt, UD_VECTOR, 0, false); |
54b8486f GN |
710 | } |
711 | ||
35d3d4a1 | 712 | static int emulate_ts(struct x86_emulate_ctxt *ctxt, int err) |
54b8486f | 713 | { |
35d3d4a1 | 714 | return emulate_exception(ctxt, TS_VECTOR, err, true); |
54b8486f GN |
715 | } |
716 | ||
34d1f490 AK |
717 | static int emulate_de(struct x86_emulate_ctxt *ctxt) |
718 | { | |
35d3d4a1 | 719 | return emulate_exception(ctxt, DE_VECTOR, 0, false); |
34d1f490 AK |
720 | } |
721 | ||
1253791d AK |
722 | static int emulate_nm(struct x86_emulate_ctxt *ctxt) |
723 | { | |
724 | return emulate_exception(ctxt, NM_VECTOR, 0, false); | |
725 | } | |
726 | ||
1aa36616 AK |
727 | static u16 get_segment_selector(struct x86_emulate_ctxt *ctxt, unsigned seg) |
728 | { | |
729 | u16 selector; | |
730 | struct desc_struct desc; | |
731 | ||
732 | ctxt->ops->get_segment(ctxt, &selector, &desc, NULL, seg); | |
733 | return selector; | |
734 | } | |
735 | ||
736 | static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector, | |
737 | unsigned seg) | |
738 | { | |
739 | u16 dummy; | |
740 | u32 base3; | |
741 | struct desc_struct desc; | |
742 | ||
743 | ctxt->ops->get_segment(ctxt, &dummy, &desc, &base3, seg); | |
744 | ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg); | |
745 | } | |
746 | ||
1c11b376 AK |
747 | /* |
748 | * x86 defines three classes of vector instructions: explicitly | |
749 | * aligned, explicitly unaligned, and the rest, which change behaviour | |
750 | * depending on whether they're AVX encoded or not. | |
751 | * | |
752 | * Also included is CMPXCHG16B which is not a vector instruction, yet it is | |
753 | * subject to the same check. | |
754 | */ | |
755 | static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size) | |
756 | { | |
757 | if (likely(size < 16)) | |
758 | return false; | |
759 | ||
760 | if (ctxt->d & Aligned) | |
761 | return true; | |
762 | else if (ctxt->d & Unaligned) | |
763 | return false; | |
764 | else if (ctxt->d & Avx) | |
765 | return false; | |
766 | else | |
767 | return true; | |
768 | } | |
769 | ||
3d9b938e | 770 | static int __linearize(struct x86_emulate_ctxt *ctxt, |
52fd8b44 | 771 | struct segmented_address addr, |
3d9b938e | 772 | unsigned size, bool write, bool fetch, |
52fd8b44 AK |
773 | ulong *linear) |
774 | { | |
618ff15d AK |
775 | struct desc_struct desc; |
776 | bool usable; | |
52fd8b44 | 777 | ulong la; |
618ff15d | 778 | u32 lim; |
1aa36616 | 779 | u16 sel; |
3a78a4f4 | 780 | unsigned cpl; |
52fd8b44 | 781 | |
7b105ca2 | 782 | la = seg_base(ctxt, addr.seg) + addr.ea; |
618ff15d | 783 | switch (ctxt->mode) { |
618ff15d AK |
784 | case X86EMUL_MODE_PROT64: |
785 | if (((signed long)la << 16) >> 16 != la) | |
786 | return emulate_gp(ctxt, 0); | |
787 | break; | |
788 | default: | |
1aa36616 AK |
789 | usable = ctxt->ops->get_segment(ctxt, &sel, &desc, NULL, |
790 | addr.seg); | |
618ff15d AK |
791 | if (!usable) |
792 | goto bad; | |
58b7825b GN |
793 | /* code segment in protected mode or read-only data segment */ |
794 | if ((((ctxt->mode != X86EMUL_MODE_REAL) && (desc.type & 8)) | |
795 | || !(desc.type & 2)) && write) | |
618ff15d AK |
796 | goto bad; |
797 | /* unreadable code segment */ | |
3d9b938e | 798 | if (!fetch && (desc.type & 8) && !(desc.type & 2)) |
618ff15d AK |
799 | goto bad; |
800 | lim = desc_limit_scaled(&desc); | |
801 | if ((desc.type & 8) || !(desc.type & 4)) { | |
802 | /* expand-up segment */ | |
803 | if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim) | |
804 | goto bad; | |
805 | } else { | |
fc058680 | 806 | /* expand-down segment */ |
618ff15d AK |
807 | if (addr.ea <= lim || (u32)(addr.ea + size - 1) <= lim) |
808 | goto bad; | |
809 | lim = desc.d ? 0xffffffff : 0xffff; | |
810 | if (addr.ea > lim || (u32)(addr.ea + size - 1) > lim) | |
811 | goto bad; | |
812 | } | |
717746e3 | 813 | cpl = ctxt->ops->cpl(ctxt); |
618ff15d AK |
814 | if (!(desc.type & 8)) { |
815 | /* data segment */ | |
816 | if (cpl > desc.dpl) | |
817 | goto bad; | |
818 | } else if ((desc.type & 8) && !(desc.type & 4)) { | |
819 | /* nonconforming code segment */ | |
820 | if (cpl != desc.dpl) | |
821 | goto bad; | |
822 | } else if ((desc.type & 8) && (desc.type & 4)) { | |
823 | /* conforming code segment */ | |
824 | if (cpl < desc.dpl) | |
825 | goto bad; | |
826 | } | |
827 | break; | |
828 | } | |
9dac77fa | 829 | if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8) |
52fd8b44 | 830 | la &= (u32)-1; |
1c11b376 AK |
831 | if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0)) |
832 | return emulate_gp(ctxt, 0); | |
52fd8b44 AK |
833 | *linear = la; |
834 | return X86EMUL_CONTINUE; | |
618ff15d AK |
835 | bad: |
836 | if (addr.seg == VCPU_SREG_SS) | |
0afbe2f8 | 837 | return emulate_ss(ctxt, sel); |
618ff15d | 838 | else |
0afbe2f8 | 839 | return emulate_gp(ctxt, sel); |
52fd8b44 AK |
840 | } |
841 | ||
3d9b938e NE |
842 | static int linearize(struct x86_emulate_ctxt *ctxt, |
843 | struct segmented_address addr, | |
844 | unsigned size, bool write, | |
845 | ulong *linear) | |
846 | { | |
847 | return __linearize(ctxt, addr, size, write, false, linear); | |
848 | } | |
849 | ||
850 | ||
3ca3ac4d AK |
851 | static int segmented_read_std(struct x86_emulate_ctxt *ctxt, |
852 | struct segmented_address addr, | |
853 | void *data, | |
854 | unsigned size) | |
855 | { | |
9fa088f4 AK |
856 | int rc; |
857 | ulong linear; | |
858 | ||
83b8795a | 859 | rc = linearize(ctxt, addr, size, false, &linear); |
9fa088f4 AK |
860 | if (rc != X86EMUL_CONTINUE) |
861 | return rc; | |
0f65dd70 | 862 | return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception); |
3ca3ac4d AK |
863 | } |
864 | ||
807941b1 TY |
865 | /* |
866 | * Fetch the next byte of the instruction being emulated which is pointed to | |
867 | * by ctxt->_eip, then increment ctxt->_eip. | |
868 | * | |
869 | * Also prefetch the remaining bytes of the instruction without crossing page | |
870 | * boundary if they are not in fetch_cache yet. | |
871 | */ | |
872 | static int do_insn_fetch_byte(struct x86_emulate_ctxt *ctxt, u8 *dest) | |
62266869 | 873 | { |
9dac77fa | 874 | struct fetch_cache *fc = &ctxt->fetch; |
62266869 | 875 | int rc; |
2fb53ad8 | 876 | int size, cur_size; |
62266869 | 877 | |
807941b1 | 878 | if (ctxt->_eip == fc->end) { |
3d9b938e | 879 | unsigned long linear; |
807941b1 TY |
880 | struct segmented_address addr = { .seg = VCPU_SREG_CS, |
881 | .ea = ctxt->_eip }; | |
2fb53ad8 | 882 | cur_size = fc->end - fc->start; |
807941b1 TY |
883 | size = min(15UL - cur_size, |
884 | PAGE_SIZE - offset_in_page(ctxt->_eip)); | |
3d9b938e | 885 | rc = __linearize(ctxt, addr, size, false, true, &linear); |
7d88bb48 | 886 | if (unlikely(rc != X86EMUL_CONTINUE)) |
3d9b938e | 887 | return rc; |
ef5d75cc TY |
888 | rc = ctxt->ops->fetch(ctxt, linear, fc->data + cur_size, |
889 | size, &ctxt->exception); | |
7d88bb48 | 890 | if (unlikely(rc != X86EMUL_CONTINUE)) |
62266869 | 891 | return rc; |
2fb53ad8 | 892 | fc->end += size; |
62266869 | 893 | } |
807941b1 TY |
894 | *dest = fc->data[ctxt->_eip - fc->start]; |
895 | ctxt->_eip++; | |
3e2815e9 | 896 | return X86EMUL_CONTINUE; |
62266869 AK |
897 | } |
898 | ||
899 | static int do_insn_fetch(struct x86_emulate_ctxt *ctxt, | |
807941b1 | 900 | void *dest, unsigned size) |
62266869 | 901 | { |
3e2815e9 | 902 | int rc; |
62266869 | 903 | |
eb3c79e6 | 904 | /* x86 instructions are limited to 15 bytes. */ |
7d88bb48 | 905 | if (unlikely(ctxt->_eip + size - ctxt->eip > 15)) |
eb3c79e6 | 906 | return X86EMUL_UNHANDLEABLE; |
62266869 | 907 | while (size--) { |
807941b1 | 908 | rc = do_insn_fetch_byte(ctxt, dest++); |
3e2815e9 | 909 | if (rc != X86EMUL_CONTINUE) |
62266869 AK |
910 | return rc; |
911 | } | |
3e2815e9 | 912 | return X86EMUL_CONTINUE; |
62266869 AK |
913 | } |
914 | ||
67cbc90d | 915 | /* Fetch next part of the instruction being emulated. */ |
e85a1085 | 916 | #define insn_fetch(_type, _ctxt) \ |
67cbc90d | 917 | ({ unsigned long _x; \ |
e85a1085 | 918 | rc = do_insn_fetch(_ctxt, &_x, sizeof(_type)); \ |
67cbc90d TY |
919 | if (rc != X86EMUL_CONTINUE) \ |
920 | goto done; \ | |
67cbc90d TY |
921 | (_type)_x; \ |
922 | }) | |
923 | ||
807941b1 TY |
924 | #define insn_fetch_arr(_arr, _size, _ctxt) \ |
925 | ({ rc = do_insn_fetch(_ctxt, _arr, (_size)); \ | |
67cbc90d TY |
926 | if (rc != X86EMUL_CONTINUE) \ |
927 | goto done; \ | |
67cbc90d TY |
928 | }) |
929 | ||
1e3c5cb0 RR |
930 | /* |
931 | * Given the 'reg' portion of a ModRM byte, and a register block, return a | |
932 | * pointer into the block that addresses the relevant register. | |
933 | * @highbyte_regs specifies whether to decode AH,CH,DH,BH. | |
934 | */ | |
dd856efa | 935 | static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg, |
1e3c5cb0 | 936 | int highbyte_regs) |
6aa8b732 AK |
937 | { |
938 | void *p; | |
939 | ||
6aa8b732 | 940 | if (highbyte_regs && modrm_reg >= 4 && modrm_reg < 8) |
dd856efa AK |
941 | p = (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1; |
942 | else | |
943 | p = reg_rmw(ctxt, modrm_reg); | |
6aa8b732 AK |
944 | return p; |
945 | } | |
946 | ||
947 | static int read_descriptor(struct x86_emulate_ctxt *ctxt, | |
90de84f5 | 948 | struct segmented_address addr, |
6aa8b732 AK |
949 | u16 *size, unsigned long *address, int op_bytes) |
950 | { | |
951 | int rc; | |
952 | ||
953 | if (op_bytes == 2) | |
954 | op_bytes = 3; | |
955 | *address = 0; | |
3ca3ac4d | 956 | rc = segmented_read_std(ctxt, addr, size, 2); |
1b30eaa8 | 957 | if (rc != X86EMUL_CONTINUE) |
6aa8b732 | 958 | return rc; |
30b31ab6 | 959 | addr.ea += 2; |
3ca3ac4d | 960 | rc = segmented_read_std(ctxt, addr, address, op_bytes); |
6aa8b732 AK |
961 | return rc; |
962 | } | |
963 | ||
9ae9feba | 964 | static u8 test_cc(unsigned int condition, unsigned long flags) |
bbe9abbd | 965 | { |
9ae9feba AK |
966 | u8 rc; |
967 | void (*fop)(void) = (void *)em_setcc + 4 * (condition & 0xf); | |
bbe9abbd | 968 | |
9ae9feba AK |
969 | flags = (flags & EFLAGS_MASK) | X86_EFLAGS_IF; |
970 | asm("pushq %[flags]; popf; call *%[fastop]" | |
971 | : "=a"(rc) : [fastop]"r"(fop), [flags]"r"(flags)); | |
972 | return rc; | |
bbe9abbd NK |
973 | } |
974 | ||
91ff3cb4 AK |
975 | static void fetch_register_operand(struct operand *op) |
976 | { | |
977 | switch (op->bytes) { | |
978 | case 1: | |
979 | op->val = *(u8 *)op->addr.reg; | |
980 | break; | |
981 | case 2: | |
982 | op->val = *(u16 *)op->addr.reg; | |
983 | break; | |
984 | case 4: | |
985 | op->val = *(u32 *)op->addr.reg; | |
986 | break; | |
987 | case 8: | |
988 | op->val = *(u64 *)op->addr.reg; | |
989 | break; | |
990 | } | |
991 | } | |
992 | ||
1253791d AK |
993 | static void read_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, int reg) |
994 | { | |
995 | ctxt->ops->get_fpu(ctxt); | |
996 | switch (reg) { | |
89a87c67 MK |
997 | case 0: asm("movdqa %%xmm0, %0" : "=m"(*data)); break; |
998 | case 1: asm("movdqa %%xmm1, %0" : "=m"(*data)); break; | |
999 | case 2: asm("movdqa %%xmm2, %0" : "=m"(*data)); break; | |
1000 | case 3: asm("movdqa %%xmm3, %0" : "=m"(*data)); break; | |
1001 | case 4: asm("movdqa %%xmm4, %0" : "=m"(*data)); break; | |
1002 | case 5: asm("movdqa %%xmm5, %0" : "=m"(*data)); break; | |
1003 | case 6: asm("movdqa %%xmm6, %0" : "=m"(*data)); break; | |
1004 | case 7: asm("movdqa %%xmm7, %0" : "=m"(*data)); break; | |
1253791d | 1005 | #ifdef CONFIG_X86_64 |
89a87c67 MK |
1006 | case 8: asm("movdqa %%xmm8, %0" : "=m"(*data)); break; |
1007 | case 9: asm("movdqa %%xmm9, %0" : "=m"(*data)); break; | |
1008 | case 10: asm("movdqa %%xmm10, %0" : "=m"(*data)); break; | |
1009 | case 11: asm("movdqa %%xmm11, %0" : "=m"(*data)); break; | |
1010 | case 12: asm("movdqa %%xmm12, %0" : "=m"(*data)); break; | |
1011 | case 13: asm("movdqa %%xmm13, %0" : "=m"(*data)); break; | |
1012 | case 14: asm("movdqa %%xmm14, %0" : "=m"(*data)); break; | |
1013 | case 15: asm("movdqa %%xmm15, %0" : "=m"(*data)); break; | |
1253791d AK |
1014 | #endif |
1015 | default: BUG(); | |
1016 | } | |
1017 | ctxt->ops->put_fpu(ctxt); | |
1018 | } | |
1019 | ||
1020 | static void write_sse_reg(struct x86_emulate_ctxt *ctxt, sse128_t *data, | |
1021 | int reg) | |
1022 | { | |
1023 | ctxt->ops->get_fpu(ctxt); | |
1024 | switch (reg) { | |
89a87c67 MK |
1025 | case 0: asm("movdqa %0, %%xmm0" : : "m"(*data)); break; |
1026 | case 1: asm("movdqa %0, %%xmm1" : : "m"(*data)); break; | |
1027 | case 2: asm("movdqa %0, %%xmm2" : : "m"(*data)); break; | |
1028 | case 3: asm("movdqa %0, %%xmm3" : : "m"(*data)); break; | |
1029 | case 4: asm("movdqa %0, %%xmm4" : : "m"(*data)); break; | |
1030 | case 5: asm("movdqa %0, %%xmm5" : : "m"(*data)); break; | |
1031 | case 6: asm("movdqa %0, %%xmm6" : : "m"(*data)); break; | |
1032 | case 7: asm("movdqa %0, %%xmm7" : : "m"(*data)); break; | |
1253791d | 1033 | #ifdef CONFIG_X86_64 |
89a87c67 MK |
1034 | case 8: asm("movdqa %0, %%xmm8" : : "m"(*data)); break; |
1035 | case 9: asm("movdqa %0, %%xmm9" : : "m"(*data)); break; | |
1036 | case 10: asm("movdqa %0, %%xmm10" : : "m"(*data)); break; | |
1037 | case 11: asm("movdqa %0, %%xmm11" : : "m"(*data)); break; | |
1038 | case 12: asm("movdqa %0, %%xmm12" : : "m"(*data)); break; | |
1039 | case 13: asm("movdqa %0, %%xmm13" : : "m"(*data)); break; | |
1040 | case 14: asm("movdqa %0, %%xmm14" : : "m"(*data)); break; | |
1041 | case 15: asm("movdqa %0, %%xmm15" : : "m"(*data)); break; | |
1253791d AK |
1042 | #endif |
1043 | default: BUG(); | |
1044 | } | |
1045 | ctxt->ops->put_fpu(ctxt); | |
1046 | } | |
1047 | ||
cbe2c9d3 AK |
1048 | static void read_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) |
1049 | { | |
1050 | ctxt->ops->get_fpu(ctxt); | |
1051 | switch (reg) { | |
1052 | case 0: asm("movq %%mm0, %0" : "=m"(*data)); break; | |
1053 | case 1: asm("movq %%mm1, %0" : "=m"(*data)); break; | |
1054 | case 2: asm("movq %%mm2, %0" : "=m"(*data)); break; | |
1055 | case 3: asm("movq %%mm3, %0" : "=m"(*data)); break; | |
1056 | case 4: asm("movq %%mm4, %0" : "=m"(*data)); break; | |
1057 | case 5: asm("movq %%mm5, %0" : "=m"(*data)); break; | |
1058 | case 6: asm("movq %%mm6, %0" : "=m"(*data)); break; | |
1059 | case 7: asm("movq %%mm7, %0" : "=m"(*data)); break; | |
1060 | default: BUG(); | |
1061 | } | |
1062 | ctxt->ops->put_fpu(ctxt); | |
1063 | } | |
1064 | ||
1065 | static void write_mmx_reg(struct x86_emulate_ctxt *ctxt, u64 *data, int reg) | |
1066 | { | |
1067 | ctxt->ops->get_fpu(ctxt); | |
1068 | switch (reg) { | |
1069 | case 0: asm("movq %0, %%mm0" : : "m"(*data)); break; | |
1070 | case 1: asm("movq %0, %%mm1" : : "m"(*data)); break; | |
1071 | case 2: asm("movq %0, %%mm2" : : "m"(*data)); break; | |
1072 | case 3: asm("movq %0, %%mm3" : : "m"(*data)); break; | |
1073 | case 4: asm("movq %0, %%mm4" : : "m"(*data)); break; | |
1074 | case 5: asm("movq %0, %%mm5" : : "m"(*data)); break; | |
1075 | case 6: asm("movq %0, %%mm6" : : "m"(*data)); break; | |
1076 | case 7: asm("movq %0, %%mm7" : : "m"(*data)); break; | |
1077 | default: BUG(); | |
1078 | } | |
1079 | ctxt->ops->put_fpu(ctxt); | |
1080 | } | |
1081 | ||
045a282c GN |
1082 | static int em_fninit(struct x86_emulate_ctxt *ctxt) |
1083 | { | |
1084 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) | |
1085 | return emulate_nm(ctxt); | |
1086 | ||
1087 | ctxt->ops->get_fpu(ctxt); | |
1088 | asm volatile("fninit"); | |
1089 | ctxt->ops->put_fpu(ctxt); | |
1090 | return X86EMUL_CONTINUE; | |
1091 | } | |
1092 | ||
1093 | static int em_fnstcw(struct x86_emulate_ctxt *ctxt) | |
1094 | { | |
1095 | u16 fcw; | |
1096 | ||
1097 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) | |
1098 | return emulate_nm(ctxt); | |
1099 | ||
1100 | ctxt->ops->get_fpu(ctxt); | |
1101 | asm volatile("fnstcw %0": "+m"(fcw)); | |
1102 | ctxt->ops->put_fpu(ctxt); | |
1103 | ||
1104 | /* force 2 byte destination */ | |
1105 | ctxt->dst.bytes = 2; | |
1106 | ctxt->dst.val = fcw; | |
1107 | ||
1108 | return X86EMUL_CONTINUE; | |
1109 | } | |
1110 | ||
1111 | static int em_fnstsw(struct x86_emulate_ctxt *ctxt) | |
1112 | { | |
1113 | u16 fsw; | |
1114 | ||
1115 | if (ctxt->ops->get_cr(ctxt, 0) & (X86_CR0_TS | X86_CR0_EM)) | |
1116 | return emulate_nm(ctxt); | |
1117 | ||
1118 | ctxt->ops->get_fpu(ctxt); | |
1119 | asm volatile("fnstsw %0": "+m"(fsw)); | |
1120 | ctxt->ops->put_fpu(ctxt); | |
1121 | ||
1122 | /* force 2 byte destination */ | |
1123 | ctxt->dst.bytes = 2; | |
1124 | ctxt->dst.val = fsw; | |
1125 | ||
1126 | return X86EMUL_CONTINUE; | |
1127 | } | |
1128 | ||
1253791d | 1129 | static void decode_register_operand(struct x86_emulate_ctxt *ctxt, |
2adb5ad9 | 1130 | struct operand *op) |
3c118e24 | 1131 | { |
9dac77fa AK |
1132 | unsigned reg = ctxt->modrm_reg; |
1133 | int highbyte_regs = ctxt->rex_prefix == 0; | |
33615aa9 | 1134 | |
9dac77fa AK |
1135 | if (!(ctxt->d & ModRM)) |
1136 | reg = (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3); | |
1253791d | 1137 | |
9dac77fa | 1138 | if (ctxt->d & Sse) { |
1253791d AK |
1139 | op->type = OP_XMM; |
1140 | op->bytes = 16; | |
1141 | op->addr.xmm = reg; | |
1142 | read_sse_reg(ctxt, &op->vec_val, reg); | |
1143 | return; | |
1144 | } | |
cbe2c9d3 AK |
1145 | if (ctxt->d & Mmx) { |
1146 | reg &= 7; | |
1147 | op->type = OP_MM; | |
1148 | op->bytes = 8; | |
1149 | op->addr.mm = reg; | |
1150 | return; | |
1151 | } | |
1253791d | 1152 | |
3c118e24 | 1153 | op->type = OP_REG; |
2adb5ad9 | 1154 | if (ctxt->d & ByteOp) { |
dd856efa | 1155 | op->addr.reg = decode_register(ctxt, reg, highbyte_regs); |
3c118e24 AK |
1156 | op->bytes = 1; |
1157 | } else { | |
dd856efa | 1158 | op->addr.reg = decode_register(ctxt, reg, 0); |
9dac77fa | 1159 | op->bytes = ctxt->op_bytes; |
3c118e24 | 1160 | } |
91ff3cb4 | 1161 | fetch_register_operand(op); |
3c118e24 AK |
1162 | op->orig_val = op->val; |
1163 | } | |
1164 | ||
a6e3407b AK |
1165 | static void adjust_modrm_seg(struct x86_emulate_ctxt *ctxt, int base_reg) |
1166 | { | |
1167 | if (base_reg == VCPU_REGS_RSP || base_reg == VCPU_REGS_RBP) | |
1168 | ctxt->modrm_seg = VCPU_SREG_SS; | |
1169 | } | |
1170 | ||
1c73ef66 | 1171 | static int decode_modrm(struct x86_emulate_ctxt *ctxt, |
2dbd0dd7 | 1172 | struct operand *op) |
1c73ef66 | 1173 | { |
1c73ef66 | 1174 | u8 sib; |
f5b4edcd | 1175 | int index_reg = 0, base_reg = 0, scale; |
3e2815e9 | 1176 | int rc = X86EMUL_CONTINUE; |
2dbd0dd7 | 1177 | ulong modrm_ea = 0; |
1c73ef66 | 1178 | |
9dac77fa AK |
1179 | if (ctxt->rex_prefix) { |
1180 | ctxt->modrm_reg = (ctxt->rex_prefix & 4) << 1; /* REX.R */ | |
1181 | index_reg = (ctxt->rex_prefix & 2) << 2; /* REX.X */ | |
1182 | ctxt->modrm_rm = base_reg = (ctxt->rex_prefix & 1) << 3; /* REG.B */ | |
1c73ef66 AK |
1183 | } |
1184 | ||
9dac77fa AK |
1185 | ctxt->modrm_mod |= (ctxt->modrm & 0xc0) >> 6; |
1186 | ctxt->modrm_reg |= (ctxt->modrm & 0x38) >> 3; | |
1187 | ctxt->modrm_rm |= (ctxt->modrm & 0x07); | |
1188 | ctxt->modrm_seg = VCPU_SREG_DS; | |
1c73ef66 | 1189 | |
9dac77fa | 1190 | if (ctxt->modrm_mod == 3) { |
2dbd0dd7 | 1191 | op->type = OP_REG; |
9dac77fa | 1192 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
dd856efa | 1193 | op->addr.reg = decode_register(ctxt, ctxt->modrm_rm, ctxt->d & ByteOp); |
9dac77fa | 1194 | if (ctxt->d & Sse) { |
1253791d AK |
1195 | op->type = OP_XMM; |
1196 | op->bytes = 16; | |
9dac77fa AK |
1197 | op->addr.xmm = ctxt->modrm_rm; |
1198 | read_sse_reg(ctxt, &op->vec_val, ctxt->modrm_rm); | |
1253791d AK |
1199 | return rc; |
1200 | } | |
cbe2c9d3 AK |
1201 | if (ctxt->d & Mmx) { |
1202 | op->type = OP_MM; | |
1203 | op->bytes = 8; | |
1204 | op->addr.xmm = ctxt->modrm_rm & 7; | |
1205 | return rc; | |
1206 | } | |
2dbd0dd7 | 1207 | fetch_register_operand(op); |
1c73ef66 AK |
1208 | return rc; |
1209 | } | |
1210 | ||
2dbd0dd7 AK |
1211 | op->type = OP_MEM; |
1212 | ||
9dac77fa | 1213 | if (ctxt->ad_bytes == 2) { |
dd856efa AK |
1214 | unsigned bx = reg_read(ctxt, VCPU_REGS_RBX); |
1215 | unsigned bp = reg_read(ctxt, VCPU_REGS_RBP); | |
1216 | unsigned si = reg_read(ctxt, VCPU_REGS_RSI); | |
1217 | unsigned di = reg_read(ctxt, VCPU_REGS_RDI); | |
1c73ef66 AK |
1218 | |
1219 | /* 16-bit ModR/M decode. */ | |
9dac77fa | 1220 | switch (ctxt->modrm_mod) { |
1c73ef66 | 1221 | case 0: |
9dac77fa | 1222 | if (ctxt->modrm_rm == 6) |
e85a1085 | 1223 | modrm_ea += insn_fetch(u16, ctxt); |
1c73ef66 AK |
1224 | break; |
1225 | case 1: | |
e85a1085 | 1226 | modrm_ea += insn_fetch(s8, ctxt); |
1c73ef66 AK |
1227 | break; |
1228 | case 2: | |
e85a1085 | 1229 | modrm_ea += insn_fetch(u16, ctxt); |
1c73ef66 AK |
1230 | break; |
1231 | } | |
9dac77fa | 1232 | switch (ctxt->modrm_rm) { |
1c73ef66 | 1233 | case 0: |
2dbd0dd7 | 1234 | modrm_ea += bx + si; |
1c73ef66 AK |
1235 | break; |
1236 | case 1: | |
2dbd0dd7 | 1237 | modrm_ea += bx + di; |
1c73ef66 AK |
1238 | break; |
1239 | case 2: | |
2dbd0dd7 | 1240 | modrm_ea += bp + si; |
1c73ef66 AK |
1241 | break; |
1242 | case 3: | |
2dbd0dd7 | 1243 | modrm_ea += bp + di; |
1c73ef66 AK |
1244 | break; |
1245 | case 4: | |
2dbd0dd7 | 1246 | modrm_ea += si; |
1c73ef66 AK |
1247 | break; |
1248 | case 5: | |
2dbd0dd7 | 1249 | modrm_ea += di; |
1c73ef66 AK |
1250 | break; |
1251 | case 6: | |
9dac77fa | 1252 | if (ctxt->modrm_mod != 0) |
2dbd0dd7 | 1253 | modrm_ea += bp; |
1c73ef66 AK |
1254 | break; |
1255 | case 7: | |
2dbd0dd7 | 1256 | modrm_ea += bx; |
1c73ef66 AK |
1257 | break; |
1258 | } | |
9dac77fa AK |
1259 | if (ctxt->modrm_rm == 2 || ctxt->modrm_rm == 3 || |
1260 | (ctxt->modrm_rm == 6 && ctxt->modrm_mod != 0)) | |
1261 | ctxt->modrm_seg = VCPU_SREG_SS; | |
2dbd0dd7 | 1262 | modrm_ea = (u16)modrm_ea; |
1c73ef66 AK |
1263 | } else { |
1264 | /* 32/64-bit ModR/M decode. */ | |
9dac77fa | 1265 | if ((ctxt->modrm_rm & 7) == 4) { |
e85a1085 | 1266 | sib = insn_fetch(u8, ctxt); |
1c73ef66 AK |
1267 | index_reg |= (sib >> 3) & 7; |
1268 | base_reg |= sib & 7; | |
1269 | scale = sib >> 6; | |
1270 | ||
9dac77fa | 1271 | if ((base_reg & 7) == 5 && ctxt->modrm_mod == 0) |
e85a1085 | 1272 | modrm_ea += insn_fetch(s32, ctxt); |
a6e3407b | 1273 | else { |
dd856efa | 1274 | modrm_ea += reg_read(ctxt, base_reg); |
a6e3407b AK |
1275 | adjust_modrm_seg(ctxt, base_reg); |
1276 | } | |
dc71d0f1 | 1277 | if (index_reg != 4) |
dd856efa | 1278 | modrm_ea += reg_read(ctxt, index_reg) << scale; |
9dac77fa | 1279 | } else if ((ctxt->modrm_rm & 7) == 5 && ctxt->modrm_mod == 0) { |
84411d85 | 1280 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
9dac77fa | 1281 | ctxt->rip_relative = 1; |
a6e3407b AK |
1282 | } else { |
1283 | base_reg = ctxt->modrm_rm; | |
dd856efa | 1284 | modrm_ea += reg_read(ctxt, base_reg); |
a6e3407b AK |
1285 | adjust_modrm_seg(ctxt, base_reg); |
1286 | } | |
9dac77fa | 1287 | switch (ctxt->modrm_mod) { |
1c73ef66 | 1288 | case 0: |
9dac77fa | 1289 | if (ctxt->modrm_rm == 5) |
e85a1085 | 1290 | modrm_ea += insn_fetch(s32, ctxt); |
1c73ef66 AK |
1291 | break; |
1292 | case 1: | |
e85a1085 | 1293 | modrm_ea += insn_fetch(s8, ctxt); |
1c73ef66 AK |
1294 | break; |
1295 | case 2: | |
e85a1085 | 1296 | modrm_ea += insn_fetch(s32, ctxt); |
1c73ef66 AK |
1297 | break; |
1298 | } | |
1299 | } | |
90de84f5 | 1300 | op->addr.mem.ea = modrm_ea; |
1c73ef66 AK |
1301 | done: |
1302 | return rc; | |
1303 | } | |
1304 | ||
1305 | static int decode_abs(struct x86_emulate_ctxt *ctxt, | |
2dbd0dd7 | 1306 | struct operand *op) |
1c73ef66 | 1307 | { |
3e2815e9 | 1308 | int rc = X86EMUL_CONTINUE; |
1c73ef66 | 1309 | |
2dbd0dd7 | 1310 | op->type = OP_MEM; |
9dac77fa | 1311 | switch (ctxt->ad_bytes) { |
1c73ef66 | 1312 | case 2: |
e85a1085 | 1313 | op->addr.mem.ea = insn_fetch(u16, ctxt); |
1c73ef66 AK |
1314 | break; |
1315 | case 4: | |
e85a1085 | 1316 | op->addr.mem.ea = insn_fetch(u32, ctxt); |
1c73ef66 AK |
1317 | break; |
1318 | case 8: | |
e85a1085 | 1319 | op->addr.mem.ea = insn_fetch(u64, ctxt); |
1c73ef66 AK |
1320 | break; |
1321 | } | |
1322 | done: | |
1323 | return rc; | |
1324 | } | |
1325 | ||
9dac77fa | 1326 | static void fetch_bit_operand(struct x86_emulate_ctxt *ctxt) |
35c843c4 | 1327 | { |
7129eeca | 1328 | long sv = 0, mask; |
35c843c4 | 1329 | |
9dac77fa AK |
1330 | if (ctxt->dst.type == OP_MEM && ctxt->src.type == OP_REG) { |
1331 | mask = ~(ctxt->dst.bytes * 8 - 1); | |
35c843c4 | 1332 | |
9dac77fa AK |
1333 | if (ctxt->src.bytes == 2) |
1334 | sv = (s16)ctxt->src.val & (s16)mask; | |
1335 | else if (ctxt->src.bytes == 4) | |
1336 | sv = (s32)ctxt->src.val & (s32)mask; | |
35c843c4 | 1337 | |
9dac77fa | 1338 | ctxt->dst.addr.mem.ea += (sv >> 3); |
35c843c4 | 1339 | } |
ba7ff2b7 WY |
1340 | |
1341 | /* only subword offset */ | |
9dac77fa | 1342 | ctxt->src.val &= (ctxt->dst.bytes << 3) - 1; |
35c843c4 WY |
1343 | } |
1344 | ||
dde7e6d1 | 1345 | static int read_emulated(struct x86_emulate_ctxt *ctxt, |
dde7e6d1 | 1346 | unsigned long addr, void *dest, unsigned size) |
6aa8b732 | 1347 | { |
dde7e6d1 | 1348 | int rc; |
9dac77fa | 1349 | struct read_cache *mc = &ctxt->mem_read; |
6aa8b732 | 1350 | |
f23b070e XG |
1351 | if (mc->pos < mc->end) |
1352 | goto read_cached; | |
6aa8b732 | 1353 | |
f23b070e XG |
1354 | WARN_ON((mc->end + size) >= sizeof(mc->data)); |
1355 | ||
1356 | rc = ctxt->ops->read_emulated(ctxt, addr, mc->data + mc->end, size, | |
1357 | &ctxt->exception); | |
1358 | if (rc != X86EMUL_CONTINUE) | |
1359 | return rc; | |
1360 | ||
1361 | mc->end += size; | |
1362 | ||
1363 | read_cached: | |
1364 | memcpy(dest, mc->data + mc->pos, size); | |
1365 | mc->pos += size; | |
dde7e6d1 AK |
1366 | return X86EMUL_CONTINUE; |
1367 | } | |
6aa8b732 | 1368 | |
3ca3ac4d AK |
1369 | static int segmented_read(struct x86_emulate_ctxt *ctxt, |
1370 | struct segmented_address addr, | |
1371 | void *data, | |
1372 | unsigned size) | |
1373 | { | |
9fa088f4 AK |
1374 | int rc; |
1375 | ulong linear; | |
1376 | ||
83b8795a | 1377 | rc = linearize(ctxt, addr, size, false, &linear); |
9fa088f4 AK |
1378 | if (rc != X86EMUL_CONTINUE) |
1379 | return rc; | |
7b105ca2 | 1380 | return read_emulated(ctxt, linear, data, size); |
3ca3ac4d AK |
1381 | } |
1382 | ||
1383 | static int segmented_write(struct x86_emulate_ctxt *ctxt, | |
1384 | struct segmented_address addr, | |
1385 | const void *data, | |
1386 | unsigned size) | |
1387 | { | |
9fa088f4 AK |
1388 | int rc; |
1389 | ulong linear; | |
1390 | ||
83b8795a | 1391 | rc = linearize(ctxt, addr, size, true, &linear); |
9fa088f4 AK |
1392 | if (rc != X86EMUL_CONTINUE) |
1393 | return rc; | |
0f65dd70 AK |
1394 | return ctxt->ops->write_emulated(ctxt, linear, data, size, |
1395 | &ctxt->exception); | |
3ca3ac4d AK |
1396 | } |
1397 | ||
1398 | static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt, | |
1399 | struct segmented_address addr, | |
1400 | const void *orig_data, const void *data, | |
1401 | unsigned size) | |
1402 | { | |
9fa088f4 AK |
1403 | int rc; |
1404 | ulong linear; | |
1405 | ||
83b8795a | 1406 | rc = linearize(ctxt, addr, size, true, &linear); |
9fa088f4 AK |
1407 | if (rc != X86EMUL_CONTINUE) |
1408 | return rc; | |
0f65dd70 AK |
1409 | return ctxt->ops->cmpxchg_emulated(ctxt, linear, orig_data, data, |
1410 | size, &ctxt->exception); | |
3ca3ac4d AK |
1411 | } |
1412 | ||
dde7e6d1 | 1413 | static int pio_in_emulated(struct x86_emulate_ctxt *ctxt, |
dde7e6d1 AK |
1414 | unsigned int size, unsigned short port, |
1415 | void *dest) | |
1416 | { | |
9dac77fa | 1417 | struct read_cache *rc = &ctxt->io_read; |
b4c6abfe | 1418 | |
dde7e6d1 | 1419 | if (rc->pos == rc->end) { /* refill pio read ahead */ |
dde7e6d1 | 1420 | unsigned int in_page, n; |
9dac77fa | 1421 | unsigned int count = ctxt->rep_prefix ? |
dd856efa | 1422 | address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) : 1; |
dde7e6d1 | 1423 | in_page = (ctxt->eflags & EFLG_DF) ? |
dd856efa AK |
1424 | offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)) : |
1425 | PAGE_SIZE - offset_in_page(reg_read(ctxt, VCPU_REGS_RDI)); | |
dde7e6d1 AK |
1426 | n = min(min(in_page, (unsigned int)sizeof(rc->data)) / size, |
1427 | count); | |
1428 | if (n == 0) | |
1429 | n = 1; | |
1430 | rc->pos = rc->end = 0; | |
7b105ca2 | 1431 | if (!ctxt->ops->pio_in_emulated(ctxt, size, port, rc->data, n)) |
dde7e6d1 AK |
1432 | return 0; |
1433 | rc->end = n * size; | |
6aa8b732 AK |
1434 | } |
1435 | ||
b3356bf0 GN |
1436 | if (ctxt->rep_prefix && !(ctxt->eflags & EFLG_DF)) { |
1437 | ctxt->dst.data = rc->data + rc->pos; | |
1438 | ctxt->dst.type = OP_MEM_STR; | |
1439 | ctxt->dst.count = (rc->end - rc->pos) / size; | |
1440 | rc->pos = rc->end; | |
1441 | } else { | |
1442 | memcpy(dest, rc->data + rc->pos, size); | |
1443 | rc->pos += size; | |
1444 | } | |
dde7e6d1 AK |
1445 | return 1; |
1446 | } | |
6aa8b732 | 1447 | |
7f3d35fd KW |
1448 | static int read_interrupt_descriptor(struct x86_emulate_ctxt *ctxt, |
1449 | u16 index, struct desc_struct *desc) | |
1450 | { | |
1451 | struct desc_ptr dt; | |
1452 | ulong addr; | |
1453 | ||
1454 | ctxt->ops->get_idt(ctxt, &dt); | |
1455 | ||
1456 | if (dt.size < index * 8 + 7) | |
1457 | return emulate_gp(ctxt, index << 3 | 0x2); | |
1458 | ||
1459 | addr = dt.address + index * 8; | |
1460 | return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc, | |
1461 | &ctxt->exception); | |
1462 | } | |
1463 | ||
dde7e6d1 | 1464 | static void get_descriptor_table_ptr(struct x86_emulate_ctxt *ctxt, |
dde7e6d1 AK |
1465 | u16 selector, struct desc_ptr *dt) |
1466 | { | |
0225fb50 | 1467 | const struct x86_emulate_ops *ops = ctxt->ops; |
7b105ca2 | 1468 | |
dde7e6d1 AK |
1469 | if (selector & 1 << 2) { |
1470 | struct desc_struct desc; | |
1aa36616 AK |
1471 | u16 sel; |
1472 | ||
dde7e6d1 | 1473 | memset (dt, 0, sizeof *dt); |
1aa36616 | 1474 | if (!ops->get_segment(ctxt, &sel, &desc, NULL, VCPU_SREG_LDTR)) |
dde7e6d1 | 1475 | return; |
e09d082c | 1476 | |
dde7e6d1 AK |
1477 | dt->size = desc_limit_scaled(&desc); /* what if limit > 65535? */ |
1478 | dt->address = get_desc_base(&desc); | |
1479 | } else | |
4bff1e86 | 1480 | ops->get_gdt(ctxt, dt); |
dde7e6d1 | 1481 | } |
120df890 | 1482 | |
dde7e6d1 AK |
1483 | /* allowed just for 8 bytes segments */ |
1484 | static int read_segment_descriptor(struct x86_emulate_ctxt *ctxt, | |
e919464b AK |
1485 | u16 selector, struct desc_struct *desc, |
1486 | ulong *desc_addr_p) | |
dde7e6d1 AK |
1487 | { |
1488 | struct desc_ptr dt; | |
1489 | u16 index = selector >> 3; | |
dde7e6d1 | 1490 | ulong addr; |
120df890 | 1491 | |
7b105ca2 | 1492 | get_descriptor_table_ptr(ctxt, selector, &dt); |
120df890 | 1493 | |
35d3d4a1 AK |
1494 | if (dt.size < index * 8 + 7) |
1495 | return emulate_gp(ctxt, selector & 0xfffc); | |
e09d082c | 1496 | |
e919464b | 1497 | *desc_addr_p = addr = dt.address + index * 8; |
7b105ca2 TY |
1498 | return ctxt->ops->read_std(ctxt, addr, desc, sizeof *desc, |
1499 | &ctxt->exception); | |
dde7e6d1 | 1500 | } |
ef65c889 | 1501 | |
dde7e6d1 AK |
1502 | /* allowed just for 8 bytes segments */ |
1503 | static int write_segment_descriptor(struct x86_emulate_ctxt *ctxt, | |
dde7e6d1 AK |
1504 | u16 selector, struct desc_struct *desc) |
1505 | { | |
1506 | struct desc_ptr dt; | |
1507 | u16 index = selector >> 3; | |
dde7e6d1 | 1508 | ulong addr; |
6aa8b732 | 1509 | |
7b105ca2 | 1510 | get_descriptor_table_ptr(ctxt, selector, &dt); |
6e3d5dfb | 1511 | |
35d3d4a1 AK |
1512 | if (dt.size < index * 8 + 7) |
1513 | return emulate_gp(ctxt, selector & 0xfffc); | |
6aa8b732 | 1514 | |
dde7e6d1 | 1515 | addr = dt.address + index * 8; |
7b105ca2 TY |
1516 | return ctxt->ops->write_std(ctxt, addr, desc, sizeof *desc, |
1517 | &ctxt->exception); | |
dde7e6d1 | 1518 | } |
c7e75a3d | 1519 | |
5601d05b | 1520 | /* Does not support long mode */ |
dde7e6d1 | 1521 | static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt, |
dde7e6d1 AK |
1522 | u16 selector, int seg) |
1523 | { | |
869be99c | 1524 | struct desc_struct seg_desc, old_desc; |
dde7e6d1 AK |
1525 | u8 dpl, rpl, cpl; |
1526 | unsigned err_vec = GP_VECTOR; | |
1527 | u32 err_code = 0; | |
1528 | bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */ | |
e919464b | 1529 | ulong desc_addr; |
dde7e6d1 | 1530 | int ret; |
03ebebeb | 1531 | u16 dummy; |
69f55cb1 | 1532 | |
dde7e6d1 | 1533 | memset(&seg_desc, 0, sizeof seg_desc); |
69f55cb1 | 1534 | |
dde7e6d1 AK |
1535 | if ((seg <= VCPU_SREG_GS && ctxt->mode == X86EMUL_MODE_VM86) |
1536 | || ctxt->mode == X86EMUL_MODE_REAL) { | |
1537 | /* set real mode segment descriptor */ | |
03ebebeb | 1538 | ctxt->ops->get_segment(ctxt, &dummy, &seg_desc, NULL, seg); |
dde7e6d1 | 1539 | set_desc_base(&seg_desc, selector << 4); |
dde7e6d1 AK |
1540 | goto load; |
1541 | } | |
1542 | ||
79d5b4c3 AK |
1543 | rpl = selector & 3; |
1544 | cpl = ctxt->ops->cpl(ctxt); | |
1545 | ||
1546 | /* NULL selector is not valid for TR, CS and SS (except for long mode) */ | |
1547 | if ((seg == VCPU_SREG_CS | |
1548 | || (seg == VCPU_SREG_SS | |
1549 | && (ctxt->mode != X86EMUL_MODE_PROT64 || rpl != cpl)) | |
1550 | || seg == VCPU_SREG_TR) | |
dde7e6d1 AK |
1551 | && null_selector) |
1552 | goto exception; | |
1553 | ||
1554 | /* TR should be in GDT only */ | |
1555 | if (seg == VCPU_SREG_TR && (selector & (1 << 2))) | |
1556 | goto exception; | |
1557 | ||
1558 | if (null_selector) /* for NULL selector skip all following checks */ | |
1559 | goto load; | |
1560 | ||
e919464b | 1561 | ret = read_segment_descriptor(ctxt, selector, &seg_desc, &desc_addr); |
dde7e6d1 AK |
1562 | if (ret != X86EMUL_CONTINUE) |
1563 | return ret; | |
1564 | ||
1565 | err_code = selector & 0xfffc; | |
1566 | err_vec = GP_VECTOR; | |
1567 | ||
fc058680 | 1568 | /* can't load system descriptor into segment selector */ |
dde7e6d1 AK |
1569 | if (seg <= VCPU_SREG_GS && !seg_desc.s) |
1570 | goto exception; | |
1571 | ||
1572 | if (!seg_desc.p) { | |
1573 | err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR; | |
1574 | goto exception; | |
1575 | } | |
1576 | ||
dde7e6d1 | 1577 | dpl = seg_desc.dpl; |
dde7e6d1 AK |
1578 | |
1579 | switch (seg) { | |
1580 | case VCPU_SREG_SS: | |
1581 | /* | |
1582 | * segment is not a writable data segment or segment | |
1583 | * selector's RPL != CPL or segment selector's RPL != CPL | |
1584 | */ | |
1585 | if (rpl != cpl || (seg_desc.type & 0xa) != 0x2 || dpl != cpl) | |
1586 | goto exception; | |
6aa8b732 | 1587 | break; |
dde7e6d1 AK |
1588 | case VCPU_SREG_CS: |
1589 | if (!(seg_desc.type & 8)) | |
1590 | goto exception; | |
1591 | ||
1592 | if (seg_desc.type & 4) { | |
1593 | /* conforming */ | |
1594 | if (dpl > cpl) | |
1595 | goto exception; | |
1596 | } else { | |
1597 | /* nonconforming */ | |
1598 | if (rpl > cpl || dpl != cpl) | |
1599 | goto exception; | |
1600 | } | |
1601 | /* CS(RPL) <- CPL */ | |
1602 | selector = (selector & 0xfffc) | cpl; | |
6aa8b732 | 1603 | break; |
dde7e6d1 AK |
1604 | case VCPU_SREG_TR: |
1605 | if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9)) | |
1606 | goto exception; | |
869be99c AK |
1607 | old_desc = seg_desc; |
1608 | seg_desc.type |= 2; /* busy */ | |
1609 | ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc, | |
1610 | sizeof(seg_desc), &ctxt->exception); | |
1611 | if (ret != X86EMUL_CONTINUE) | |
1612 | return ret; | |
dde7e6d1 AK |
1613 | break; |
1614 | case VCPU_SREG_LDTR: | |
1615 | if (seg_desc.s || seg_desc.type != 2) | |
1616 | goto exception; | |
1617 | break; | |
1618 | default: /* DS, ES, FS, or GS */ | |
4e62417b | 1619 | /* |
dde7e6d1 AK |
1620 | * segment is not a data or readable code segment or |
1621 | * ((segment is a data or nonconforming code segment) | |
1622 | * and (both RPL and CPL > DPL)) | |
4e62417b | 1623 | */ |
dde7e6d1 AK |
1624 | if ((seg_desc.type & 0xa) == 0x8 || |
1625 | (((seg_desc.type & 0xc) != 0xc) && | |
1626 | (rpl > dpl && cpl > dpl))) | |
1627 | goto exception; | |
6aa8b732 | 1628 | break; |
dde7e6d1 AK |
1629 | } |
1630 | ||
1631 | if (seg_desc.s) { | |
1632 | /* mark segment as accessed */ | |
1633 | seg_desc.type |= 1; | |
7b105ca2 | 1634 | ret = write_segment_descriptor(ctxt, selector, &seg_desc); |
dde7e6d1 AK |
1635 | if (ret != X86EMUL_CONTINUE) |
1636 | return ret; | |
1637 | } | |
1638 | load: | |
7b105ca2 | 1639 | ctxt->ops->set_segment(ctxt, selector, &seg_desc, 0, seg); |
dde7e6d1 AK |
1640 | return X86EMUL_CONTINUE; |
1641 | exception: | |
1642 | emulate_exception(ctxt, err_vec, err_code, true); | |
1643 | return X86EMUL_PROPAGATE_FAULT; | |
1644 | } | |
1645 | ||
31be40b3 WY |
1646 | static void write_register_operand(struct operand *op) |
1647 | { | |
1648 | /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */ | |
1649 | switch (op->bytes) { | |
1650 | case 1: | |
1651 | *(u8 *)op->addr.reg = (u8)op->val; | |
1652 | break; | |
1653 | case 2: | |
1654 | *(u16 *)op->addr.reg = (u16)op->val; | |
1655 | break; | |
1656 | case 4: | |
1657 | *op->addr.reg = (u32)op->val; | |
1658 | break; /* 64b: zero-extend */ | |
1659 | case 8: | |
1660 | *op->addr.reg = op->val; | |
1661 | break; | |
1662 | } | |
1663 | } | |
1664 | ||
adddcecf | 1665 | static int writeback(struct x86_emulate_ctxt *ctxt) |
dde7e6d1 AK |
1666 | { |
1667 | int rc; | |
dde7e6d1 | 1668 | |
b6744dc3 AK |
1669 | if (ctxt->d & NoWrite) |
1670 | return X86EMUL_CONTINUE; | |
1671 | ||
9dac77fa | 1672 | switch (ctxt->dst.type) { |
dde7e6d1 | 1673 | case OP_REG: |
9dac77fa | 1674 | write_register_operand(&ctxt->dst); |
6aa8b732 | 1675 | break; |
dde7e6d1 | 1676 | case OP_MEM: |
9dac77fa | 1677 | if (ctxt->lock_prefix) |
3ca3ac4d | 1678 | rc = segmented_cmpxchg(ctxt, |
9dac77fa AK |
1679 | ctxt->dst.addr.mem, |
1680 | &ctxt->dst.orig_val, | |
1681 | &ctxt->dst.val, | |
1682 | ctxt->dst.bytes); | |
341de7e3 | 1683 | else |
3ca3ac4d | 1684 | rc = segmented_write(ctxt, |
9dac77fa AK |
1685 | ctxt->dst.addr.mem, |
1686 | &ctxt->dst.val, | |
1687 | ctxt->dst.bytes); | |
dde7e6d1 AK |
1688 | if (rc != X86EMUL_CONTINUE) |
1689 | return rc; | |
a682e354 | 1690 | break; |
b3356bf0 GN |
1691 | case OP_MEM_STR: |
1692 | rc = segmented_write(ctxt, | |
1693 | ctxt->dst.addr.mem, | |
1694 | ctxt->dst.data, | |
1695 | ctxt->dst.bytes * ctxt->dst.count); | |
1696 | if (rc != X86EMUL_CONTINUE) | |
1697 | return rc; | |
1698 | break; | |
1253791d | 1699 | case OP_XMM: |
9dac77fa | 1700 | write_sse_reg(ctxt, &ctxt->dst.vec_val, ctxt->dst.addr.xmm); |
1253791d | 1701 | break; |
cbe2c9d3 AK |
1702 | case OP_MM: |
1703 | write_mmx_reg(ctxt, &ctxt->dst.mm_val, ctxt->dst.addr.mm); | |
1704 | break; | |
dde7e6d1 AK |
1705 | case OP_NONE: |
1706 | /* no writeback */ | |
414e6277 | 1707 | break; |
dde7e6d1 | 1708 | default: |
414e6277 | 1709 | break; |
6aa8b732 | 1710 | } |
dde7e6d1 AK |
1711 | return X86EMUL_CONTINUE; |
1712 | } | |
6aa8b732 | 1713 | |
51ddff50 | 1714 | static int push(struct x86_emulate_ctxt *ctxt, void *data, int bytes) |
dde7e6d1 | 1715 | { |
4179bb02 | 1716 | struct segmented_address addr; |
0dc8d10f | 1717 | |
5ad105e5 | 1718 | rsp_increment(ctxt, -bytes); |
dd856efa | 1719 | addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); |
4179bb02 TY |
1720 | addr.seg = VCPU_SREG_SS; |
1721 | ||
51ddff50 AK |
1722 | return segmented_write(ctxt, addr, data, bytes); |
1723 | } | |
1724 | ||
1725 | static int em_push(struct x86_emulate_ctxt *ctxt) | |
1726 | { | |
4179bb02 | 1727 | /* Disable writeback. */ |
9dac77fa | 1728 | ctxt->dst.type = OP_NONE; |
51ddff50 | 1729 | return push(ctxt, &ctxt->src.val, ctxt->op_bytes); |
dde7e6d1 | 1730 | } |
69f55cb1 | 1731 | |
dde7e6d1 | 1732 | static int emulate_pop(struct x86_emulate_ctxt *ctxt, |
dde7e6d1 AK |
1733 | void *dest, int len) |
1734 | { | |
dde7e6d1 | 1735 | int rc; |
90de84f5 | 1736 | struct segmented_address addr; |
8b4caf66 | 1737 | |
dd856efa | 1738 | addr.ea = reg_read(ctxt, VCPU_REGS_RSP) & stack_mask(ctxt); |
90de84f5 | 1739 | addr.seg = VCPU_SREG_SS; |
3ca3ac4d | 1740 | rc = segmented_read(ctxt, addr, dest, len); |
dde7e6d1 AK |
1741 | if (rc != X86EMUL_CONTINUE) |
1742 | return rc; | |
1743 | ||
5ad105e5 | 1744 | rsp_increment(ctxt, len); |
dde7e6d1 | 1745 | return rc; |
8b4caf66 LV |
1746 | } |
1747 | ||
c54fe504 TY |
1748 | static int em_pop(struct x86_emulate_ctxt *ctxt) |
1749 | { | |
9dac77fa | 1750 | return emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes); |
c54fe504 TY |
1751 | } |
1752 | ||
dde7e6d1 | 1753 | static int emulate_popf(struct x86_emulate_ctxt *ctxt, |
7b105ca2 | 1754 | void *dest, int len) |
9de41573 GN |
1755 | { |
1756 | int rc; | |
dde7e6d1 AK |
1757 | unsigned long val, change_mask; |
1758 | int iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; | |
7b105ca2 | 1759 | int cpl = ctxt->ops->cpl(ctxt); |
9de41573 | 1760 | |
3b9be3bf | 1761 | rc = emulate_pop(ctxt, &val, len); |
dde7e6d1 AK |
1762 | if (rc != X86EMUL_CONTINUE) |
1763 | return rc; | |
9de41573 | 1764 | |
dde7e6d1 AK |
1765 | change_mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_OF |
1766 | | EFLG_TF | EFLG_DF | EFLG_NT | EFLG_RF | EFLG_AC | EFLG_ID; | |
9de41573 | 1767 | |
dde7e6d1 AK |
1768 | switch(ctxt->mode) { |
1769 | case X86EMUL_MODE_PROT64: | |
1770 | case X86EMUL_MODE_PROT32: | |
1771 | case X86EMUL_MODE_PROT16: | |
1772 | if (cpl == 0) | |
1773 | change_mask |= EFLG_IOPL; | |
1774 | if (cpl <= iopl) | |
1775 | change_mask |= EFLG_IF; | |
1776 | break; | |
1777 | case X86EMUL_MODE_VM86: | |
35d3d4a1 AK |
1778 | if (iopl < 3) |
1779 | return emulate_gp(ctxt, 0); | |
dde7e6d1 AK |
1780 | change_mask |= EFLG_IF; |
1781 | break; | |
1782 | default: /* real mode */ | |
1783 | change_mask |= (EFLG_IOPL | EFLG_IF); | |
1784 | break; | |
9de41573 | 1785 | } |
dde7e6d1 AK |
1786 | |
1787 | *(unsigned long *)dest = | |
1788 | (ctxt->eflags & ~change_mask) | (val & change_mask); | |
1789 | ||
1790 | return rc; | |
9de41573 GN |
1791 | } |
1792 | ||
62aaa2f0 TY |
1793 | static int em_popf(struct x86_emulate_ctxt *ctxt) |
1794 | { | |
9dac77fa AK |
1795 | ctxt->dst.type = OP_REG; |
1796 | ctxt->dst.addr.reg = &ctxt->eflags; | |
1797 | ctxt->dst.bytes = ctxt->op_bytes; | |
1798 | return emulate_popf(ctxt, &ctxt->dst.val, ctxt->op_bytes); | |
62aaa2f0 TY |
1799 | } |
1800 | ||
612e89f0 AK |
1801 | static int em_enter(struct x86_emulate_ctxt *ctxt) |
1802 | { | |
1803 | int rc; | |
1804 | unsigned frame_size = ctxt->src.val; | |
1805 | unsigned nesting_level = ctxt->src2.val & 31; | |
dd856efa | 1806 | ulong rbp; |
612e89f0 AK |
1807 | |
1808 | if (nesting_level) | |
1809 | return X86EMUL_UNHANDLEABLE; | |
1810 | ||
dd856efa AK |
1811 | rbp = reg_read(ctxt, VCPU_REGS_RBP); |
1812 | rc = push(ctxt, &rbp, stack_size(ctxt)); | |
612e89f0 AK |
1813 | if (rc != X86EMUL_CONTINUE) |
1814 | return rc; | |
dd856efa | 1815 | assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP), |
612e89f0 | 1816 | stack_mask(ctxt)); |
dd856efa AK |
1817 | assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), |
1818 | reg_read(ctxt, VCPU_REGS_RSP) - frame_size, | |
612e89f0 AK |
1819 | stack_mask(ctxt)); |
1820 | return X86EMUL_CONTINUE; | |
1821 | } | |
1822 | ||
f47cfa31 AK |
1823 | static int em_leave(struct x86_emulate_ctxt *ctxt) |
1824 | { | |
dd856efa | 1825 | assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), reg_read(ctxt, VCPU_REGS_RBP), |
f47cfa31 | 1826 | stack_mask(ctxt)); |
dd856efa | 1827 | return emulate_pop(ctxt, reg_rmw(ctxt, VCPU_REGS_RBP), ctxt->op_bytes); |
f47cfa31 AK |
1828 | } |
1829 | ||
1cd196ea | 1830 | static int em_push_sreg(struct x86_emulate_ctxt *ctxt) |
7b262e90 | 1831 | { |
1cd196ea AK |
1832 | int seg = ctxt->src2.val; |
1833 | ||
9dac77fa | 1834 | ctxt->src.val = get_segment_selector(ctxt, seg); |
7b262e90 | 1835 | |
4487b3b4 | 1836 | return em_push(ctxt); |
7b262e90 GN |
1837 | } |
1838 | ||
1cd196ea | 1839 | static int em_pop_sreg(struct x86_emulate_ctxt *ctxt) |
38ba30ba | 1840 | { |
1cd196ea | 1841 | int seg = ctxt->src2.val; |
dde7e6d1 AK |
1842 | unsigned long selector; |
1843 | int rc; | |
38ba30ba | 1844 | |
9dac77fa | 1845 | rc = emulate_pop(ctxt, &selector, ctxt->op_bytes); |
dde7e6d1 AK |
1846 | if (rc != X86EMUL_CONTINUE) |
1847 | return rc; | |
1848 | ||
7b105ca2 | 1849 | rc = load_segment_descriptor(ctxt, (u16)selector, seg); |
dde7e6d1 | 1850 | return rc; |
38ba30ba GN |
1851 | } |
1852 | ||
b96a7fad | 1853 | static int em_pusha(struct x86_emulate_ctxt *ctxt) |
38ba30ba | 1854 | { |
dd856efa | 1855 | unsigned long old_esp = reg_read(ctxt, VCPU_REGS_RSP); |
dde7e6d1 AK |
1856 | int rc = X86EMUL_CONTINUE; |
1857 | int reg = VCPU_REGS_RAX; | |
38ba30ba | 1858 | |
dde7e6d1 AK |
1859 | while (reg <= VCPU_REGS_RDI) { |
1860 | (reg == VCPU_REGS_RSP) ? | |
dd856efa | 1861 | (ctxt->src.val = old_esp) : (ctxt->src.val = reg_read(ctxt, reg)); |
38ba30ba | 1862 | |
4487b3b4 | 1863 | rc = em_push(ctxt); |
dde7e6d1 AK |
1864 | if (rc != X86EMUL_CONTINUE) |
1865 | return rc; | |
38ba30ba | 1866 | |
dde7e6d1 | 1867 | ++reg; |
38ba30ba | 1868 | } |
38ba30ba | 1869 | |
dde7e6d1 | 1870 | return rc; |
38ba30ba GN |
1871 | } |
1872 | ||
62aaa2f0 TY |
1873 | static int em_pushf(struct x86_emulate_ctxt *ctxt) |
1874 | { | |
9dac77fa | 1875 | ctxt->src.val = (unsigned long)ctxt->eflags; |
62aaa2f0 TY |
1876 | return em_push(ctxt); |
1877 | } | |
1878 | ||
b96a7fad | 1879 | static int em_popa(struct x86_emulate_ctxt *ctxt) |
38ba30ba | 1880 | { |
dde7e6d1 AK |
1881 | int rc = X86EMUL_CONTINUE; |
1882 | int reg = VCPU_REGS_RDI; | |
38ba30ba | 1883 | |
dde7e6d1 AK |
1884 | while (reg >= VCPU_REGS_RAX) { |
1885 | if (reg == VCPU_REGS_RSP) { | |
5ad105e5 | 1886 | rsp_increment(ctxt, ctxt->op_bytes); |
dde7e6d1 AK |
1887 | --reg; |
1888 | } | |
38ba30ba | 1889 | |
dd856efa | 1890 | rc = emulate_pop(ctxt, reg_rmw(ctxt, reg), ctxt->op_bytes); |
dde7e6d1 AK |
1891 | if (rc != X86EMUL_CONTINUE) |
1892 | break; | |
1893 | --reg; | |
38ba30ba | 1894 | } |
dde7e6d1 | 1895 | return rc; |
38ba30ba GN |
1896 | } |
1897 | ||
dd856efa | 1898 | static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) |
6e154e56 | 1899 | { |
0225fb50 | 1900 | const struct x86_emulate_ops *ops = ctxt->ops; |
5c56e1cf | 1901 | int rc; |
6e154e56 MG |
1902 | struct desc_ptr dt; |
1903 | gva_t cs_addr; | |
1904 | gva_t eip_addr; | |
1905 | u16 cs, eip; | |
6e154e56 MG |
1906 | |
1907 | /* TODO: Add limit checks */ | |
9dac77fa | 1908 | ctxt->src.val = ctxt->eflags; |
4487b3b4 | 1909 | rc = em_push(ctxt); |
5c56e1cf AK |
1910 | if (rc != X86EMUL_CONTINUE) |
1911 | return rc; | |
6e154e56 MG |
1912 | |
1913 | ctxt->eflags &= ~(EFLG_IF | EFLG_TF | EFLG_AC); | |
1914 | ||
9dac77fa | 1915 | ctxt->src.val = get_segment_selector(ctxt, VCPU_SREG_CS); |
4487b3b4 | 1916 | rc = em_push(ctxt); |
5c56e1cf AK |
1917 | if (rc != X86EMUL_CONTINUE) |
1918 | return rc; | |
6e154e56 | 1919 | |
9dac77fa | 1920 | ctxt->src.val = ctxt->_eip; |
4487b3b4 | 1921 | rc = em_push(ctxt); |
5c56e1cf AK |
1922 | if (rc != X86EMUL_CONTINUE) |
1923 | return rc; | |
1924 | ||
4bff1e86 | 1925 | ops->get_idt(ctxt, &dt); |
6e154e56 MG |
1926 | |
1927 | eip_addr = dt.address + (irq << 2); | |
1928 | cs_addr = dt.address + (irq << 2) + 2; | |
1929 | ||
0f65dd70 | 1930 | rc = ops->read_std(ctxt, cs_addr, &cs, 2, &ctxt->exception); |
6e154e56 MG |
1931 | if (rc != X86EMUL_CONTINUE) |
1932 | return rc; | |
1933 | ||
0f65dd70 | 1934 | rc = ops->read_std(ctxt, eip_addr, &eip, 2, &ctxt->exception); |
6e154e56 MG |
1935 | if (rc != X86EMUL_CONTINUE) |
1936 | return rc; | |
1937 | ||
7b105ca2 | 1938 | rc = load_segment_descriptor(ctxt, cs, VCPU_SREG_CS); |
6e154e56 MG |
1939 | if (rc != X86EMUL_CONTINUE) |
1940 | return rc; | |
1941 | ||
9dac77fa | 1942 | ctxt->_eip = eip; |
6e154e56 MG |
1943 | |
1944 | return rc; | |
1945 | } | |
1946 | ||
dd856efa AK |
1947 | int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) |
1948 | { | |
1949 | int rc; | |
1950 | ||
1951 | invalidate_registers(ctxt); | |
1952 | rc = __emulate_int_real(ctxt, irq); | |
1953 | if (rc == X86EMUL_CONTINUE) | |
1954 | writeback_registers(ctxt); | |
1955 | return rc; | |
1956 | } | |
1957 | ||
7b105ca2 | 1958 | static int emulate_int(struct x86_emulate_ctxt *ctxt, int irq) |
6e154e56 MG |
1959 | { |
1960 | switch(ctxt->mode) { | |
1961 | case X86EMUL_MODE_REAL: | |
dd856efa | 1962 | return __emulate_int_real(ctxt, irq); |
6e154e56 MG |
1963 | case X86EMUL_MODE_VM86: |
1964 | case X86EMUL_MODE_PROT16: | |
1965 | case X86EMUL_MODE_PROT32: | |
1966 | case X86EMUL_MODE_PROT64: | |
1967 | default: | |
1968 | /* Protected mode interrupts unimplemented yet */ | |
1969 | return X86EMUL_UNHANDLEABLE; | |
1970 | } | |
1971 | } | |
1972 | ||
7b105ca2 | 1973 | static int emulate_iret_real(struct x86_emulate_ctxt *ctxt) |
38ba30ba | 1974 | { |
dde7e6d1 AK |
1975 | int rc = X86EMUL_CONTINUE; |
1976 | unsigned long temp_eip = 0; | |
1977 | unsigned long temp_eflags = 0; | |
1978 | unsigned long cs = 0; | |
1979 | unsigned long mask = EFLG_CF | EFLG_PF | EFLG_AF | EFLG_ZF | EFLG_SF | EFLG_TF | | |
1980 | EFLG_IF | EFLG_DF | EFLG_OF | EFLG_IOPL | EFLG_NT | EFLG_RF | | |
1981 | EFLG_AC | EFLG_ID | (1 << 1); /* Last one is the reserved bit */ | |
1982 | unsigned long vm86_mask = EFLG_VM | EFLG_VIF | EFLG_VIP; | |
38ba30ba | 1983 | |
dde7e6d1 | 1984 | /* TODO: Add stack limit check */ |
38ba30ba | 1985 | |
9dac77fa | 1986 | rc = emulate_pop(ctxt, &temp_eip, ctxt->op_bytes); |
38ba30ba | 1987 | |
dde7e6d1 AK |
1988 | if (rc != X86EMUL_CONTINUE) |
1989 | return rc; | |
38ba30ba | 1990 | |
35d3d4a1 AK |
1991 | if (temp_eip & ~0xffff) |
1992 | return emulate_gp(ctxt, 0); | |
38ba30ba | 1993 | |
9dac77fa | 1994 | rc = emulate_pop(ctxt, &cs, ctxt->op_bytes); |
38ba30ba | 1995 | |
dde7e6d1 AK |
1996 | if (rc != X86EMUL_CONTINUE) |
1997 | return rc; | |
38ba30ba | 1998 | |
9dac77fa | 1999 | rc = emulate_pop(ctxt, &temp_eflags, ctxt->op_bytes); |
38ba30ba | 2000 | |
dde7e6d1 AK |
2001 | if (rc != X86EMUL_CONTINUE) |
2002 | return rc; | |
38ba30ba | 2003 | |
7b105ca2 | 2004 | rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS); |
38ba30ba | 2005 | |
dde7e6d1 AK |
2006 | if (rc != X86EMUL_CONTINUE) |
2007 | return rc; | |
38ba30ba | 2008 | |
9dac77fa | 2009 | ctxt->_eip = temp_eip; |
38ba30ba | 2010 | |
38ba30ba | 2011 | |
9dac77fa | 2012 | if (ctxt->op_bytes == 4) |
dde7e6d1 | 2013 | ctxt->eflags = ((temp_eflags & mask) | (ctxt->eflags & vm86_mask)); |
9dac77fa | 2014 | else if (ctxt->op_bytes == 2) { |
dde7e6d1 AK |
2015 | ctxt->eflags &= ~0xffff; |
2016 | ctxt->eflags |= temp_eflags; | |
38ba30ba | 2017 | } |
dde7e6d1 AK |
2018 | |
2019 | ctxt->eflags &= ~EFLG_RESERVED_ZEROS_MASK; /* Clear reserved zeros */ | |
2020 | ctxt->eflags |= EFLG_RESERVED_ONE_MASK; | |
2021 | ||
2022 | return rc; | |
38ba30ba GN |
2023 | } |
2024 | ||
e01991e7 | 2025 | static int em_iret(struct x86_emulate_ctxt *ctxt) |
c37eda13 | 2026 | { |
dde7e6d1 AK |
2027 | switch(ctxt->mode) { |
2028 | case X86EMUL_MODE_REAL: | |
7b105ca2 | 2029 | return emulate_iret_real(ctxt); |
dde7e6d1 AK |
2030 | case X86EMUL_MODE_VM86: |
2031 | case X86EMUL_MODE_PROT16: | |
2032 | case X86EMUL_MODE_PROT32: | |
2033 | case X86EMUL_MODE_PROT64: | |
c37eda13 | 2034 | default: |
dde7e6d1 AK |
2035 | /* iret from protected mode unimplemented yet */ |
2036 | return X86EMUL_UNHANDLEABLE; | |
c37eda13 | 2037 | } |
c37eda13 WY |
2038 | } |
2039 | ||
d2f62766 TY |
2040 | static int em_jmp_far(struct x86_emulate_ctxt *ctxt) |
2041 | { | |
d2f62766 TY |
2042 | int rc; |
2043 | unsigned short sel; | |
2044 | ||
9dac77fa | 2045 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
d2f62766 | 2046 | |
7b105ca2 | 2047 | rc = load_segment_descriptor(ctxt, sel, VCPU_SREG_CS); |
d2f62766 TY |
2048 | if (rc != X86EMUL_CONTINUE) |
2049 | return rc; | |
2050 | ||
9dac77fa AK |
2051 | ctxt->_eip = 0; |
2052 | memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes); | |
d2f62766 TY |
2053 | return X86EMUL_CONTINUE; |
2054 | } | |
2055 | ||
45a1467d AK |
2056 | FASTOP1(not); |
2057 | FASTOP1(neg); | |
95413dc4 AK |
2058 | FASTOP1(inc); |
2059 | FASTOP1(dec); | |
3329ece1 | 2060 | |
007a3b54 AK |
2061 | FASTOP2CL(rol); |
2062 | FASTOP2CL(ror); | |
2063 | FASTOP2CL(rcl); | |
2064 | FASTOP2CL(rcr); | |
2065 | FASTOP2CL(shl); | |
2066 | FASTOP2CL(shr); | |
2067 | FASTOP2CL(sar); | |
2068 | ||
3329ece1 AK |
2069 | static int em_mul_ex(struct x86_emulate_ctxt *ctxt) |
2070 | { | |
2071 | u8 ex = 0; | |
2072 | ||
2073 | emulate_1op_rax_rdx(ctxt, "mul", ex); | |
2074 | return X86EMUL_CONTINUE; | |
2075 | } | |
2076 | ||
2077 | static int em_imul_ex(struct x86_emulate_ctxt *ctxt) | |
2078 | { | |
2079 | u8 ex = 0; | |
2080 | ||
2081 | emulate_1op_rax_rdx(ctxt, "imul", ex); | |
2082 | return X86EMUL_CONTINUE; | |
2083 | } | |
2084 | ||
2085 | static int em_div_ex(struct x86_emulate_ctxt *ctxt) | |
8cdbd2c9 | 2086 | { |
34d1f490 | 2087 | u8 de = 0; |
8cdbd2c9 | 2088 | |
3329ece1 AK |
2089 | emulate_1op_rax_rdx(ctxt, "div", de); |
2090 | if (de) | |
2091 | return emulate_de(ctxt); | |
2092 | return X86EMUL_CONTINUE; | |
2093 | } | |
2094 | ||
2095 | static int em_idiv_ex(struct x86_emulate_ctxt *ctxt) | |
2096 | { | |
2097 | u8 de = 0; | |
2098 | ||
2099 | emulate_1op_rax_rdx(ctxt, "idiv", de); | |
34d1f490 AK |
2100 | if (de) |
2101 | return emulate_de(ctxt); | |
8c5eee30 | 2102 | return X86EMUL_CONTINUE; |
8cdbd2c9 LV |
2103 | } |
2104 | ||
51187683 | 2105 | static int em_grp45(struct x86_emulate_ctxt *ctxt) |
8cdbd2c9 | 2106 | { |
4179bb02 | 2107 | int rc = X86EMUL_CONTINUE; |
8cdbd2c9 | 2108 | |
9dac77fa | 2109 | switch (ctxt->modrm_reg) { |
d19292e4 MG |
2110 | case 2: /* call near abs */ { |
2111 | long int old_eip; | |
9dac77fa AK |
2112 | old_eip = ctxt->_eip; |
2113 | ctxt->_eip = ctxt->src.val; | |
2114 | ctxt->src.val = old_eip; | |
4487b3b4 | 2115 | rc = em_push(ctxt); |
d19292e4 MG |
2116 | break; |
2117 | } | |
8cdbd2c9 | 2118 | case 4: /* jmp abs */ |
9dac77fa | 2119 | ctxt->_eip = ctxt->src.val; |
8cdbd2c9 | 2120 | break; |
d2f62766 TY |
2121 | case 5: /* jmp far */ |
2122 | rc = em_jmp_far(ctxt); | |
2123 | break; | |
8cdbd2c9 | 2124 | case 6: /* push */ |
4487b3b4 | 2125 | rc = em_push(ctxt); |
8cdbd2c9 | 2126 | break; |
8cdbd2c9 | 2127 | } |
4179bb02 | 2128 | return rc; |
8cdbd2c9 LV |
2129 | } |
2130 | ||
e0dac408 | 2131 | static int em_cmpxchg8b(struct x86_emulate_ctxt *ctxt) |
8cdbd2c9 | 2132 | { |
9dac77fa | 2133 | u64 old = ctxt->dst.orig_val64; |
8cdbd2c9 | 2134 | |
dd856efa AK |
2135 | if (((u32) (old >> 0) != (u32) reg_read(ctxt, VCPU_REGS_RAX)) || |
2136 | ((u32) (old >> 32) != (u32) reg_read(ctxt, VCPU_REGS_RDX))) { | |
2137 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32) (old >> 0); | |
2138 | *reg_write(ctxt, VCPU_REGS_RDX) = (u32) (old >> 32); | |
05f086f8 | 2139 | ctxt->eflags &= ~EFLG_ZF; |
8cdbd2c9 | 2140 | } else { |
dd856efa AK |
2141 | ctxt->dst.val64 = ((u64)reg_read(ctxt, VCPU_REGS_RCX) << 32) | |
2142 | (u32) reg_read(ctxt, VCPU_REGS_RBX); | |
8cdbd2c9 | 2143 | |
05f086f8 | 2144 | ctxt->eflags |= EFLG_ZF; |
8cdbd2c9 | 2145 | } |
1b30eaa8 | 2146 | return X86EMUL_CONTINUE; |
8cdbd2c9 LV |
2147 | } |
2148 | ||
ebda02c2 TY |
2149 | static int em_ret(struct x86_emulate_ctxt *ctxt) |
2150 | { | |
9dac77fa AK |
2151 | ctxt->dst.type = OP_REG; |
2152 | ctxt->dst.addr.reg = &ctxt->_eip; | |
2153 | ctxt->dst.bytes = ctxt->op_bytes; | |
ebda02c2 TY |
2154 | return em_pop(ctxt); |
2155 | } | |
2156 | ||
e01991e7 | 2157 | static int em_ret_far(struct x86_emulate_ctxt *ctxt) |
a77ab5ea | 2158 | { |
a77ab5ea AK |
2159 | int rc; |
2160 | unsigned long cs; | |
2161 | ||
9dac77fa | 2162 | rc = emulate_pop(ctxt, &ctxt->_eip, ctxt->op_bytes); |
1b30eaa8 | 2163 | if (rc != X86EMUL_CONTINUE) |
a77ab5ea | 2164 | return rc; |
9dac77fa AK |
2165 | if (ctxt->op_bytes == 4) |
2166 | ctxt->_eip = (u32)ctxt->_eip; | |
2167 | rc = emulate_pop(ctxt, &cs, ctxt->op_bytes); | |
1b30eaa8 | 2168 | if (rc != X86EMUL_CONTINUE) |
a77ab5ea | 2169 | return rc; |
7b105ca2 | 2170 | rc = load_segment_descriptor(ctxt, (u16)cs, VCPU_SREG_CS); |
a77ab5ea AK |
2171 | return rc; |
2172 | } | |
2173 | ||
e940b5c2 TY |
2174 | static int em_cmpxchg(struct x86_emulate_ctxt *ctxt) |
2175 | { | |
2176 | /* Save real source value, then compare EAX against destination. */ | |
2177 | ctxt->src.orig_val = ctxt->src.val; | |
dd856efa | 2178 | ctxt->src.val = reg_read(ctxt, VCPU_REGS_RAX); |
e940b5c2 TY |
2179 | emulate_2op_SrcV(ctxt, "cmp"); |
2180 | ||
2181 | if (ctxt->eflags & EFLG_ZF) { | |
2182 | /* Success: write back to memory. */ | |
2183 | ctxt->dst.val = ctxt->src.orig_val; | |
2184 | } else { | |
2185 | /* Failure: write the value we saw to EAX. */ | |
2186 | ctxt->dst.type = OP_REG; | |
dd856efa | 2187 | ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
e940b5c2 TY |
2188 | } |
2189 | return X86EMUL_CONTINUE; | |
2190 | } | |
2191 | ||
d4b4325f | 2192 | static int em_lseg(struct x86_emulate_ctxt *ctxt) |
09b5f4d3 | 2193 | { |
d4b4325f | 2194 | int seg = ctxt->src2.val; |
09b5f4d3 WY |
2195 | unsigned short sel; |
2196 | int rc; | |
2197 | ||
9dac77fa | 2198 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
09b5f4d3 | 2199 | |
7b105ca2 | 2200 | rc = load_segment_descriptor(ctxt, sel, seg); |
09b5f4d3 WY |
2201 | if (rc != X86EMUL_CONTINUE) |
2202 | return rc; | |
2203 | ||
9dac77fa | 2204 | ctxt->dst.val = ctxt->src.val; |
09b5f4d3 WY |
2205 | return rc; |
2206 | } | |
2207 | ||
7b105ca2 | 2208 | static void |
e66bb2cc | 2209 | setup_syscalls_segments(struct x86_emulate_ctxt *ctxt, |
7b105ca2 | 2210 | struct desc_struct *cs, struct desc_struct *ss) |
e66bb2cc | 2211 | { |
e66bb2cc | 2212 | cs->l = 0; /* will be adjusted later */ |
79168fd1 | 2213 | set_desc_base(cs, 0); /* flat segment */ |
e66bb2cc | 2214 | cs->g = 1; /* 4kb granularity */ |
79168fd1 | 2215 | set_desc_limit(cs, 0xfffff); /* 4GB limit */ |
e66bb2cc AP |
2216 | cs->type = 0x0b; /* Read, Execute, Accessed */ |
2217 | cs->s = 1; | |
2218 | cs->dpl = 0; /* will be adjusted later */ | |
79168fd1 GN |
2219 | cs->p = 1; |
2220 | cs->d = 1; | |
99245b50 | 2221 | cs->avl = 0; |
e66bb2cc | 2222 | |
79168fd1 GN |
2223 | set_desc_base(ss, 0); /* flat segment */ |
2224 | set_desc_limit(ss, 0xfffff); /* 4GB limit */ | |
e66bb2cc AP |
2225 | ss->g = 1; /* 4kb granularity */ |
2226 | ss->s = 1; | |
2227 | ss->type = 0x03; /* Read/Write, Accessed */ | |
79168fd1 | 2228 | ss->d = 1; /* 32bit stack segment */ |
e66bb2cc | 2229 | ss->dpl = 0; |
79168fd1 | 2230 | ss->p = 1; |
99245b50 GN |
2231 | ss->l = 0; |
2232 | ss->avl = 0; | |
e66bb2cc AP |
2233 | } |
2234 | ||
1a18a69b AK |
2235 | static bool vendor_intel(struct x86_emulate_ctxt *ctxt) |
2236 | { | |
2237 | u32 eax, ebx, ecx, edx; | |
2238 | ||
2239 | eax = ecx = 0; | |
0017f93a AK |
2240 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
2241 | return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx | |
1a18a69b AK |
2242 | && ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx |
2243 | && edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx; | |
2244 | } | |
2245 | ||
c2226fc9 SB |
2246 | static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt) |
2247 | { | |
0225fb50 | 2248 | const struct x86_emulate_ops *ops = ctxt->ops; |
c2226fc9 SB |
2249 | u32 eax, ebx, ecx, edx; |
2250 | ||
2251 | /* | |
2252 | * syscall should always be enabled in longmode - so only become | |
2253 | * vendor specific (cpuid) if other modes are active... | |
2254 | */ | |
2255 | if (ctxt->mode == X86EMUL_MODE_PROT64) | |
2256 | return true; | |
2257 | ||
2258 | eax = 0x00000000; | |
2259 | ecx = 0x00000000; | |
0017f93a AK |
2260 | ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
2261 | /* | |
2262 | * Intel ("GenuineIntel") | |
2263 | * remark: Intel CPUs only support "syscall" in 64bit | |
2264 | * longmode. Also an 64bit guest with a | |
2265 | * 32bit compat-app running will #UD !! While this | |
2266 | * behaviour can be fixed (by emulating) into AMD | |
2267 | * response - CPUs of AMD can't behave like Intel. | |
2268 | */ | |
2269 | if (ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx && | |
2270 | ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx && | |
2271 | edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx) | |
2272 | return false; | |
2273 | ||
2274 | /* AMD ("AuthenticAMD") */ | |
2275 | if (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx && | |
2276 | ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx && | |
2277 | edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx) | |
2278 | return true; | |
2279 | ||
2280 | /* AMD ("AMDisbetter!") */ | |
2281 | if (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx && | |
2282 | ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx && | |
2283 | edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx) | |
2284 | return true; | |
c2226fc9 SB |
2285 | |
2286 | /* default: (not Intel, not AMD), apply Intel's stricter rules... */ | |
2287 | return false; | |
2288 | } | |
2289 | ||
e01991e7 | 2290 | static int em_syscall(struct x86_emulate_ctxt *ctxt) |
e66bb2cc | 2291 | { |
0225fb50 | 2292 | const struct x86_emulate_ops *ops = ctxt->ops; |
79168fd1 | 2293 | struct desc_struct cs, ss; |
e66bb2cc | 2294 | u64 msr_data; |
79168fd1 | 2295 | u16 cs_sel, ss_sel; |
c2ad2bb3 | 2296 | u64 efer = 0; |
e66bb2cc AP |
2297 | |
2298 | /* syscall is not available in real mode */ | |
2e901c4c | 2299 | if (ctxt->mode == X86EMUL_MODE_REAL || |
35d3d4a1 AK |
2300 | ctxt->mode == X86EMUL_MODE_VM86) |
2301 | return emulate_ud(ctxt); | |
e66bb2cc | 2302 | |
c2226fc9 SB |
2303 | if (!(em_syscall_is_enabled(ctxt))) |
2304 | return emulate_ud(ctxt); | |
2305 | ||
c2ad2bb3 | 2306 | ops->get_msr(ctxt, MSR_EFER, &efer); |
7b105ca2 | 2307 | setup_syscalls_segments(ctxt, &cs, &ss); |
e66bb2cc | 2308 | |
c2226fc9 SB |
2309 | if (!(efer & EFER_SCE)) |
2310 | return emulate_ud(ctxt); | |
2311 | ||
717746e3 | 2312 | ops->get_msr(ctxt, MSR_STAR, &msr_data); |
e66bb2cc | 2313 | msr_data >>= 32; |
79168fd1 GN |
2314 | cs_sel = (u16)(msr_data & 0xfffc); |
2315 | ss_sel = (u16)(msr_data + 8); | |
e66bb2cc | 2316 | |
c2ad2bb3 | 2317 | if (efer & EFER_LMA) { |
79168fd1 | 2318 | cs.d = 0; |
e66bb2cc AP |
2319 | cs.l = 1; |
2320 | } | |
1aa36616 AK |
2321 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
2322 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); | |
e66bb2cc | 2323 | |
dd856efa | 2324 | *reg_write(ctxt, VCPU_REGS_RCX) = ctxt->_eip; |
c2ad2bb3 | 2325 | if (efer & EFER_LMA) { |
e66bb2cc | 2326 | #ifdef CONFIG_X86_64 |
dd856efa | 2327 | *reg_write(ctxt, VCPU_REGS_R11) = ctxt->eflags & ~EFLG_RF; |
e66bb2cc | 2328 | |
717746e3 | 2329 | ops->get_msr(ctxt, |
3fb1b5db GN |
2330 | ctxt->mode == X86EMUL_MODE_PROT64 ? |
2331 | MSR_LSTAR : MSR_CSTAR, &msr_data); | |
9dac77fa | 2332 | ctxt->_eip = msr_data; |
e66bb2cc | 2333 | |
717746e3 | 2334 | ops->get_msr(ctxt, MSR_SYSCALL_MASK, &msr_data); |
e66bb2cc AP |
2335 | ctxt->eflags &= ~(msr_data | EFLG_RF); |
2336 | #endif | |
2337 | } else { | |
2338 | /* legacy mode */ | |
717746e3 | 2339 | ops->get_msr(ctxt, MSR_STAR, &msr_data); |
9dac77fa | 2340 | ctxt->_eip = (u32)msr_data; |
e66bb2cc AP |
2341 | |
2342 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); | |
2343 | } | |
2344 | ||
e54cfa97 | 2345 | return X86EMUL_CONTINUE; |
e66bb2cc AP |
2346 | } |
2347 | ||
e01991e7 | 2348 | static int em_sysenter(struct x86_emulate_ctxt *ctxt) |
8c604352 | 2349 | { |
0225fb50 | 2350 | const struct x86_emulate_ops *ops = ctxt->ops; |
79168fd1 | 2351 | struct desc_struct cs, ss; |
8c604352 | 2352 | u64 msr_data; |
79168fd1 | 2353 | u16 cs_sel, ss_sel; |
c2ad2bb3 | 2354 | u64 efer = 0; |
8c604352 | 2355 | |
7b105ca2 | 2356 | ops->get_msr(ctxt, MSR_EFER, &efer); |
a0044755 | 2357 | /* inject #GP if in real mode */ |
35d3d4a1 AK |
2358 | if (ctxt->mode == X86EMUL_MODE_REAL) |
2359 | return emulate_gp(ctxt, 0); | |
8c604352 | 2360 | |
1a18a69b AK |
2361 | /* |
2362 | * Not recognized on AMD in compat mode (but is recognized in legacy | |
2363 | * mode). | |
2364 | */ | |
2365 | if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA) | |
2366 | && !vendor_intel(ctxt)) | |
2367 | return emulate_ud(ctxt); | |
2368 | ||
8c604352 AP |
2369 | /* XXX sysenter/sysexit have not been tested in 64bit mode. |
2370 | * Therefore, we inject an #UD. | |
2371 | */ | |
35d3d4a1 AK |
2372 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
2373 | return emulate_ud(ctxt); | |
8c604352 | 2374 | |
7b105ca2 | 2375 | setup_syscalls_segments(ctxt, &cs, &ss); |
8c604352 | 2376 | |
717746e3 | 2377 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data); |
8c604352 AP |
2378 | switch (ctxt->mode) { |
2379 | case X86EMUL_MODE_PROT32: | |
35d3d4a1 AK |
2380 | if ((msr_data & 0xfffc) == 0x0) |
2381 | return emulate_gp(ctxt, 0); | |
8c604352 AP |
2382 | break; |
2383 | case X86EMUL_MODE_PROT64: | |
35d3d4a1 AK |
2384 | if (msr_data == 0x0) |
2385 | return emulate_gp(ctxt, 0); | |
8c604352 | 2386 | break; |
9d1b39a9 GN |
2387 | default: |
2388 | break; | |
8c604352 AP |
2389 | } |
2390 | ||
2391 | ctxt->eflags &= ~(EFLG_VM | EFLG_IF | EFLG_RF); | |
79168fd1 GN |
2392 | cs_sel = (u16)msr_data; |
2393 | cs_sel &= ~SELECTOR_RPL_MASK; | |
2394 | ss_sel = cs_sel + 8; | |
2395 | ss_sel &= ~SELECTOR_RPL_MASK; | |
c2ad2bb3 | 2396 | if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) { |
79168fd1 | 2397 | cs.d = 0; |
8c604352 AP |
2398 | cs.l = 1; |
2399 | } | |
2400 | ||
1aa36616 AK |
2401 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
2402 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); | |
8c604352 | 2403 | |
717746e3 | 2404 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_EIP, &msr_data); |
9dac77fa | 2405 | ctxt->_eip = msr_data; |
8c604352 | 2406 | |
717746e3 | 2407 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_ESP, &msr_data); |
dd856efa | 2408 | *reg_write(ctxt, VCPU_REGS_RSP) = msr_data; |
8c604352 | 2409 | |
e54cfa97 | 2410 | return X86EMUL_CONTINUE; |
8c604352 AP |
2411 | } |
2412 | ||
e01991e7 | 2413 | static int em_sysexit(struct x86_emulate_ctxt *ctxt) |
4668f050 | 2414 | { |
0225fb50 | 2415 | const struct x86_emulate_ops *ops = ctxt->ops; |
79168fd1 | 2416 | struct desc_struct cs, ss; |
4668f050 AP |
2417 | u64 msr_data; |
2418 | int usermode; | |
1249b96e | 2419 | u16 cs_sel = 0, ss_sel = 0; |
4668f050 | 2420 | |
a0044755 GN |
2421 | /* inject #GP if in real mode or Virtual 8086 mode */ |
2422 | if (ctxt->mode == X86EMUL_MODE_REAL || | |
35d3d4a1 AK |
2423 | ctxt->mode == X86EMUL_MODE_VM86) |
2424 | return emulate_gp(ctxt, 0); | |
4668f050 | 2425 | |
7b105ca2 | 2426 | setup_syscalls_segments(ctxt, &cs, &ss); |
4668f050 | 2427 | |
9dac77fa | 2428 | if ((ctxt->rex_prefix & 0x8) != 0x0) |
4668f050 AP |
2429 | usermode = X86EMUL_MODE_PROT64; |
2430 | else | |
2431 | usermode = X86EMUL_MODE_PROT32; | |
2432 | ||
2433 | cs.dpl = 3; | |
2434 | ss.dpl = 3; | |
717746e3 | 2435 | ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data); |
4668f050 AP |
2436 | switch (usermode) { |
2437 | case X86EMUL_MODE_PROT32: | |
79168fd1 | 2438 | cs_sel = (u16)(msr_data + 16); |
35d3d4a1 AK |
2439 | if ((msr_data & 0xfffc) == 0x0) |
2440 | return emulate_gp(ctxt, 0); | |
79168fd1 | 2441 | ss_sel = (u16)(msr_data + 24); |
4668f050 AP |
2442 | break; |
2443 | case X86EMUL_MODE_PROT64: | |
79168fd1 | 2444 | cs_sel = (u16)(msr_data + 32); |
35d3d4a1 AK |
2445 | if (msr_data == 0x0) |
2446 | return emulate_gp(ctxt, 0); | |
79168fd1 GN |
2447 | ss_sel = cs_sel + 8; |
2448 | cs.d = 0; | |
4668f050 AP |
2449 | cs.l = 1; |
2450 | break; | |
2451 | } | |
79168fd1 GN |
2452 | cs_sel |= SELECTOR_RPL_MASK; |
2453 | ss_sel |= SELECTOR_RPL_MASK; | |
4668f050 | 2454 | |
1aa36616 AK |
2455 | ops->set_segment(ctxt, cs_sel, &cs, 0, VCPU_SREG_CS); |
2456 | ops->set_segment(ctxt, ss_sel, &ss, 0, VCPU_SREG_SS); | |
4668f050 | 2457 | |
dd856efa AK |
2458 | ctxt->_eip = reg_read(ctxt, VCPU_REGS_RDX); |
2459 | *reg_write(ctxt, VCPU_REGS_RSP) = reg_read(ctxt, VCPU_REGS_RCX); | |
4668f050 | 2460 | |
e54cfa97 | 2461 | return X86EMUL_CONTINUE; |
4668f050 AP |
2462 | } |
2463 | ||
7b105ca2 | 2464 | static bool emulator_bad_iopl(struct x86_emulate_ctxt *ctxt) |
f850e2e6 GN |
2465 | { |
2466 | int iopl; | |
2467 | if (ctxt->mode == X86EMUL_MODE_REAL) | |
2468 | return false; | |
2469 | if (ctxt->mode == X86EMUL_MODE_VM86) | |
2470 | return true; | |
2471 | iopl = (ctxt->eflags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; | |
7b105ca2 | 2472 | return ctxt->ops->cpl(ctxt) > iopl; |
f850e2e6 GN |
2473 | } |
2474 | ||
2475 | static bool emulator_io_port_access_allowed(struct x86_emulate_ctxt *ctxt, | |
f850e2e6 GN |
2476 | u16 port, u16 len) |
2477 | { | |
0225fb50 | 2478 | const struct x86_emulate_ops *ops = ctxt->ops; |
79168fd1 | 2479 | struct desc_struct tr_seg; |
5601d05b | 2480 | u32 base3; |
f850e2e6 | 2481 | int r; |
1aa36616 | 2482 | u16 tr, io_bitmap_ptr, perm, bit_idx = port & 0x7; |
f850e2e6 | 2483 | unsigned mask = (1 << len) - 1; |
5601d05b | 2484 | unsigned long base; |
f850e2e6 | 2485 | |
1aa36616 | 2486 | ops->get_segment(ctxt, &tr, &tr_seg, &base3, VCPU_SREG_TR); |
79168fd1 | 2487 | if (!tr_seg.p) |
f850e2e6 | 2488 | return false; |
79168fd1 | 2489 | if (desc_limit_scaled(&tr_seg) < 103) |
f850e2e6 | 2490 | return false; |
5601d05b GN |
2491 | base = get_desc_base(&tr_seg); |
2492 | #ifdef CONFIG_X86_64 | |
2493 | base |= ((u64)base3) << 32; | |
2494 | #endif | |
0f65dd70 | 2495 | r = ops->read_std(ctxt, base + 102, &io_bitmap_ptr, 2, NULL); |
f850e2e6 GN |
2496 | if (r != X86EMUL_CONTINUE) |
2497 | return false; | |
79168fd1 | 2498 | if (io_bitmap_ptr + port/8 > desc_limit_scaled(&tr_seg)) |
f850e2e6 | 2499 | return false; |
0f65dd70 | 2500 | r = ops->read_std(ctxt, base + io_bitmap_ptr + port/8, &perm, 2, NULL); |
f850e2e6 GN |
2501 | if (r != X86EMUL_CONTINUE) |
2502 | return false; | |
2503 | if ((perm >> bit_idx) & mask) | |
2504 | return false; | |
2505 | return true; | |
2506 | } | |
2507 | ||
2508 | static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt, | |
f850e2e6 GN |
2509 | u16 port, u16 len) |
2510 | { | |
4fc40f07 GN |
2511 | if (ctxt->perm_ok) |
2512 | return true; | |
2513 | ||
7b105ca2 TY |
2514 | if (emulator_bad_iopl(ctxt)) |
2515 | if (!emulator_io_port_access_allowed(ctxt, port, len)) | |
f850e2e6 | 2516 | return false; |
4fc40f07 GN |
2517 | |
2518 | ctxt->perm_ok = true; | |
2519 | ||
f850e2e6 GN |
2520 | return true; |
2521 | } | |
2522 | ||
38ba30ba | 2523 | static void save_state_to_tss16(struct x86_emulate_ctxt *ctxt, |
38ba30ba GN |
2524 | struct tss_segment_16 *tss) |
2525 | { | |
9dac77fa | 2526 | tss->ip = ctxt->_eip; |
38ba30ba | 2527 | tss->flag = ctxt->eflags; |
dd856efa AK |
2528 | tss->ax = reg_read(ctxt, VCPU_REGS_RAX); |
2529 | tss->cx = reg_read(ctxt, VCPU_REGS_RCX); | |
2530 | tss->dx = reg_read(ctxt, VCPU_REGS_RDX); | |
2531 | tss->bx = reg_read(ctxt, VCPU_REGS_RBX); | |
2532 | tss->sp = reg_read(ctxt, VCPU_REGS_RSP); | |
2533 | tss->bp = reg_read(ctxt, VCPU_REGS_RBP); | |
2534 | tss->si = reg_read(ctxt, VCPU_REGS_RSI); | |
2535 | tss->di = reg_read(ctxt, VCPU_REGS_RDI); | |
38ba30ba | 2536 | |
1aa36616 AK |
2537 | tss->es = get_segment_selector(ctxt, VCPU_SREG_ES); |
2538 | tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS); | |
2539 | tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS); | |
2540 | tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS); | |
2541 | tss->ldt = get_segment_selector(ctxt, VCPU_SREG_LDTR); | |
38ba30ba GN |
2542 | } |
2543 | ||
2544 | static int load_state_from_tss16(struct x86_emulate_ctxt *ctxt, | |
38ba30ba GN |
2545 | struct tss_segment_16 *tss) |
2546 | { | |
38ba30ba GN |
2547 | int ret; |
2548 | ||
9dac77fa | 2549 | ctxt->_eip = tss->ip; |
38ba30ba | 2550 | ctxt->eflags = tss->flag | 2; |
dd856efa AK |
2551 | *reg_write(ctxt, VCPU_REGS_RAX) = tss->ax; |
2552 | *reg_write(ctxt, VCPU_REGS_RCX) = tss->cx; | |
2553 | *reg_write(ctxt, VCPU_REGS_RDX) = tss->dx; | |
2554 | *reg_write(ctxt, VCPU_REGS_RBX) = tss->bx; | |
2555 | *reg_write(ctxt, VCPU_REGS_RSP) = tss->sp; | |
2556 | *reg_write(ctxt, VCPU_REGS_RBP) = tss->bp; | |
2557 | *reg_write(ctxt, VCPU_REGS_RSI) = tss->si; | |
2558 | *reg_write(ctxt, VCPU_REGS_RDI) = tss->di; | |
38ba30ba GN |
2559 | |
2560 | /* | |
2561 | * SDM says that segment selectors are loaded before segment | |
2562 | * descriptors | |
2563 | */ | |
1aa36616 AK |
2564 | set_segment_selector(ctxt, tss->ldt, VCPU_SREG_LDTR); |
2565 | set_segment_selector(ctxt, tss->es, VCPU_SREG_ES); | |
2566 | set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS); | |
2567 | set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS); | |
2568 | set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS); | |
38ba30ba GN |
2569 | |
2570 | /* | |
fc058680 | 2571 | * Now load segment descriptors. If fault happens at this stage |
38ba30ba GN |
2572 | * it is handled in a context of new task |
2573 | */ | |
7b105ca2 | 2574 | ret = load_segment_descriptor(ctxt, tss->ldt, VCPU_SREG_LDTR); |
38ba30ba GN |
2575 | if (ret != X86EMUL_CONTINUE) |
2576 | return ret; | |
7b105ca2 | 2577 | ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES); |
38ba30ba GN |
2578 | if (ret != X86EMUL_CONTINUE) |
2579 | return ret; | |
7b105ca2 | 2580 | ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS); |
38ba30ba GN |
2581 | if (ret != X86EMUL_CONTINUE) |
2582 | return ret; | |
7b105ca2 | 2583 | ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS); |
38ba30ba GN |
2584 | if (ret != X86EMUL_CONTINUE) |
2585 | return ret; | |
7b105ca2 | 2586 | ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS); |
38ba30ba GN |
2587 | if (ret != X86EMUL_CONTINUE) |
2588 | return ret; | |
2589 | ||
2590 | return X86EMUL_CONTINUE; | |
2591 | } | |
2592 | ||
2593 | static int task_switch_16(struct x86_emulate_ctxt *ctxt, | |
38ba30ba GN |
2594 | u16 tss_selector, u16 old_tss_sel, |
2595 | ulong old_tss_base, struct desc_struct *new_desc) | |
2596 | { | |
0225fb50 | 2597 | const struct x86_emulate_ops *ops = ctxt->ops; |
38ba30ba GN |
2598 | struct tss_segment_16 tss_seg; |
2599 | int ret; | |
bcc55cba | 2600 | u32 new_tss_base = get_desc_base(new_desc); |
38ba30ba | 2601 | |
0f65dd70 | 2602 | ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg, |
bcc55cba | 2603 | &ctxt->exception); |
db297e3d | 2604 | if (ret != X86EMUL_CONTINUE) |
38ba30ba | 2605 | /* FIXME: need to provide precise fault address */ |
38ba30ba | 2606 | return ret; |
38ba30ba | 2607 | |
7b105ca2 | 2608 | save_state_to_tss16(ctxt, &tss_seg); |
38ba30ba | 2609 | |
0f65dd70 | 2610 | ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg, |
bcc55cba | 2611 | &ctxt->exception); |
db297e3d | 2612 | if (ret != X86EMUL_CONTINUE) |
38ba30ba | 2613 | /* FIXME: need to provide precise fault address */ |
38ba30ba | 2614 | return ret; |
38ba30ba | 2615 | |
0f65dd70 | 2616 | ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg, |
bcc55cba | 2617 | &ctxt->exception); |
db297e3d | 2618 | if (ret != X86EMUL_CONTINUE) |
38ba30ba | 2619 | /* FIXME: need to provide precise fault address */ |
38ba30ba | 2620 | return ret; |
38ba30ba GN |
2621 | |
2622 | if (old_tss_sel != 0xffff) { | |
2623 | tss_seg.prev_task_link = old_tss_sel; | |
2624 | ||
0f65dd70 | 2625 | ret = ops->write_std(ctxt, new_tss_base, |
38ba30ba GN |
2626 | &tss_seg.prev_task_link, |
2627 | sizeof tss_seg.prev_task_link, | |
0f65dd70 | 2628 | &ctxt->exception); |
db297e3d | 2629 | if (ret != X86EMUL_CONTINUE) |
38ba30ba | 2630 | /* FIXME: need to provide precise fault address */ |
38ba30ba | 2631 | return ret; |
38ba30ba GN |
2632 | } |
2633 | ||
7b105ca2 | 2634 | return load_state_from_tss16(ctxt, &tss_seg); |
38ba30ba GN |
2635 | } |
2636 | ||
2637 | static void save_state_to_tss32(struct x86_emulate_ctxt *ctxt, | |
38ba30ba GN |
2638 | struct tss_segment_32 *tss) |
2639 | { | |
7b105ca2 | 2640 | tss->cr3 = ctxt->ops->get_cr(ctxt, 3); |
9dac77fa | 2641 | tss->eip = ctxt->_eip; |
38ba30ba | 2642 | tss->eflags = ctxt->eflags; |
dd856efa AK |
2643 | tss->eax = reg_read(ctxt, VCPU_REGS_RAX); |
2644 | tss->ecx = reg_read(ctxt, VCPU_REGS_RCX); | |
2645 | tss->edx = reg_read(ctxt, VCPU_REGS_RDX); | |
2646 | tss->ebx = reg_read(ctxt, VCPU_REGS_RBX); | |
2647 | tss->esp = reg_read(ctxt, VCPU_REGS_RSP); | |
2648 | tss->ebp = reg_read(ctxt, VCPU_REGS_RBP); | |
2649 | tss->esi = reg_read(ctxt, VCPU_REGS_RSI); | |
2650 | tss->edi = reg_read(ctxt, VCPU_REGS_RDI); | |
38ba30ba | 2651 | |
1aa36616 AK |
2652 | tss->es = get_segment_selector(ctxt, VCPU_SREG_ES); |
2653 | tss->cs = get_segment_selector(ctxt, VCPU_SREG_CS); | |
2654 | tss->ss = get_segment_selector(ctxt, VCPU_SREG_SS); | |
2655 | tss->ds = get_segment_selector(ctxt, VCPU_SREG_DS); | |
2656 | tss->fs = get_segment_selector(ctxt, VCPU_SREG_FS); | |
2657 | tss->gs = get_segment_selector(ctxt, VCPU_SREG_GS); | |
2658 | tss->ldt_selector = get_segment_selector(ctxt, VCPU_SREG_LDTR); | |
38ba30ba GN |
2659 | } |
2660 | ||
2661 | static int load_state_from_tss32(struct x86_emulate_ctxt *ctxt, | |
38ba30ba GN |
2662 | struct tss_segment_32 *tss) |
2663 | { | |
38ba30ba GN |
2664 | int ret; |
2665 | ||
7b105ca2 | 2666 | if (ctxt->ops->set_cr(ctxt, 3, tss->cr3)) |
35d3d4a1 | 2667 | return emulate_gp(ctxt, 0); |
9dac77fa | 2668 | ctxt->_eip = tss->eip; |
38ba30ba | 2669 | ctxt->eflags = tss->eflags | 2; |
4cee4798 KW |
2670 | |
2671 | /* General purpose registers */ | |
dd856efa AK |
2672 | *reg_write(ctxt, VCPU_REGS_RAX) = tss->eax; |
2673 | *reg_write(ctxt, VCPU_REGS_RCX) = tss->ecx; | |
2674 | *reg_write(ctxt, VCPU_REGS_RDX) = tss->edx; | |
2675 | *reg_write(ctxt, VCPU_REGS_RBX) = tss->ebx; | |
2676 | *reg_write(ctxt, VCPU_REGS_RSP) = tss->esp; | |
2677 | *reg_write(ctxt, VCPU_REGS_RBP) = tss->ebp; | |
2678 | *reg_write(ctxt, VCPU_REGS_RSI) = tss->esi; | |
2679 | *reg_write(ctxt, VCPU_REGS_RDI) = tss->edi; | |
38ba30ba GN |
2680 | |
2681 | /* | |
2682 | * SDM says that segment selectors are loaded before segment | |
2683 | * descriptors | |
2684 | */ | |
1aa36616 AK |
2685 | set_segment_selector(ctxt, tss->ldt_selector, VCPU_SREG_LDTR); |
2686 | set_segment_selector(ctxt, tss->es, VCPU_SREG_ES); | |
2687 | set_segment_selector(ctxt, tss->cs, VCPU_SREG_CS); | |
2688 | set_segment_selector(ctxt, tss->ss, VCPU_SREG_SS); | |
2689 | set_segment_selector(ctxt, tss->ds, VCPU_SREG_DS); | |
2690 | set_segment_selector(ctxt, tss->fs, VCPU_SREG_FS); | |
2691 | set_segment_selector(ctxt, tss->gs, VCPU_SREG_GS); | |
38ba30ba | 2692 | |
4cee4798 KW |
2693 | /* |
2694 | * If we're switching between Protected Mode and VM86, we need to make | |
2695 | * sure to update the mode before loading the segment descriptors so | |
2696 | * that the selectors are interpreted correctly. | |
2697 | * | |
2698 | * Need to get rflags to the vcpu struct immediately because it | |
2699 | * influences the CPL which is checked at least when loading the segment | |
2700 | * descriptors and when pushing an error code to the new kernel stack. | |
2701 | * | |
2702 | * TODO Introduce a separate ctxt->ops->set_cpl callback | |
2703 | */ | |
2704 | if (ctxt->eflags & X86_EFLAGS_VM) | |
2705 | ctxt->mode = X86EMUL_MODE_VM86; | |
2706 | else | |
2707 | ctxt->mode = X86EMUL_MODE_PROT32; | |
2708 | ||
2709 | ctxt->ops->set_rflags(ctxt, ctxt->eflags); | |
2710 | ||
38ba30ba GN |
2711 | /* |
2712 | * Now load segment descriptors. If fault happenes at this stage | |
2713 | * it is handled in a context of new task | |
2714 | */ | |
7b105ca2 | 2715 | ret = load_segment_descriptor(ctxt, tss->ldt_selector, VCPU_SREG_LDTR); |
38ba30ba GN |
2716 | if (ret != X86EMUL_CONTINUE) |
2717 | return ret; | |
7b105ca2 | 2718 | ret = load_segment_descriptor(ctxt, tss->es, VCPU_SREG_ES); |
38ba30ba GN |
2719 | if (ret != X86EMUL_CONTINUE) |
2720 | return ret; | |
7b105ca2 | 2721 | ret = load_segment_descriptor(ctxt, tss->cs, VCPU_SREG_CS); |
38ba30ba GN |
2722 | if (ret != X86EMUL_CONTINUE) |
2723 | return ret; | |
7b105ca2 | 2724 | ret = load_segment_descriptor(ctxt, tss->ss, VCPU_SREG_SS); |
38ba30ba GN |
2725 | if (ret != X86EMUL_CONTINUE) |
2726 | return ret; | |
7b105ca2 | 2727 | ret = load_segment_descriptor(ctxt, tss->ds, VCPU_SREG_DS); |
38ba30ba GN |
2728 | if (ret != X86EMUL_CONTINUE) |
2729 | return ret; | |
7b105ca2 | 2730 | ret = load_segment_descriptor(ctxt, tss->fs, VCPU_SREG_FS); |
38ba30ba GN |
2731 | if (ret != X86EMUL_CONTINUE) |
2732 | return ret; | |
7b105ca2 | 2733 | ret = load_segment_descriptor(ctxt, tss->gs, VCPU_SREG_GS); |
38ba30ba GN |
2734 | if (ret != X86EMUL_CONTINUE) |
2735 | return ret; | |
2736 | ||
2737 | return X86EMUL_CONTINUE; | |
2738 | } | |
2739 | ||
2740 | static int task_switch_32(struct x86_emulate_ctxt *ctxt, | |
38ba30ba GN |
2741 | u16 tss_selector, u16 old_tss_sel, |
2742 | ulong old_tss_base, struct desc_struct *new_desc) | |
2743 | { | |
0225fb50 | 2744 | const struct x86_emulate_ops *ops = ctxt->ops; |
38ba30ba GN |
2745 | struct tss_segment_32 tss_seg; |
2746 | int ret; | |
bcc55cba | 2747 | u32 new_tss_base = get_desc_base(new_desc); |
38ba30ba | 2748 | |
0f65dd70 | 2749 | ret = ops->read_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg, |
bcc55cba | 2750 | &ctxt->exception); |
db297e3d | 2751 | if (ret != X86EMUL_CONTINUE) |
38ba30ba | 2752 | /* FIXME: need to provide precise fault address */ |
38ba30ba | 2753 | return ret; |
38ba30ba | 2754 | |
7b105ca2 | 2755 | save_state_to_tss32(ctxt, &tss_seg); |
38ba30ba | 2756 | |
0f65dd70 | 2757 | ret = ops->write_std(ctxt, old_tss_base, &tss_seg, sizeof tss_seg, |
bcc55cba | 2758 | &ctxt->exception); |
db297e3d | 2759 | if (ret != X86EMUL_CONTINUE) |
38ba30ba | 2760 | /* FIXME: need to provide precise fault address */ |
38ba30ba | 2761 | return ret; |
38ba30ba | 2762 | |
0f65dd70 | 2763 | ret = ops->read_std(ctxt, new_tss_base, &tss_seg, sizeof tss_seg, |
bcc55cba | 2764 | &ctxt->exception); |
db297e3d | 2765 | if (ret != X86EMUL_CONTINUE) |
38ba30ba | 2766 | /* FIXME: need to provide precise fault address */ |
38ba30ba | 2767 | return ret; |
38ba30ba GN |
2768 | |
2769 | if (old_tss_sel != 0xffff) { | |
2770 | tss_seg.prev_task_link = old_tss_sel; | |
2771 | ||
0f65dd70 | 2772 | ret = ops->write_std(ctxt, new_tss_base, |
38ba30ba GN |
2773 | &tss_seg.prev_task_link, |
2774 | sizeof tss_seg.prev_task_link, | |
0f65dd70 | 2775 | &ctxt->exception); |
db297e3d | 2776 | if (ret != X86EMUL_CONTINUE) |
38ba30ba | 2777 | /* FIXME: need to provide precise fault address */ |
38ba30ba | 2778 | return ret; |
38ba30ba GN |
2779 | } |
2780 | ||
7b105ca2 | 2781 | return load_state_from_tss32(ctxt, &tss_seg); |
38ba30ba GN |
2782 | } |
2783 | ||
2784 | static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt, | |
7f3d35fd | 2785 | u16 tss_selector, int idt_index, int reason, |
e269fb21 | 2786 | bool has_error_code, u32 error_code) |
38ba30ba | 2787 | { |
0225fb50 | 2788 | const struct x86_emulate_ops *ops = ctxt->ops; |
38ba30ba GN |
2789 | struct desc_struct curr_tss_desc, next_tss_desc; |
2790 | int ret; | |
1aa36616 | 2791 | u16 old_tss_sel = get_segment_selector(ctxt, VCPU_SREG_TR); |
38ba30ba | 2792 | ulong old_tss_base = |
4bff1e86 | 2793 | ops->get_cached_segment_base(ctxt, VCPU_SREG_TR); |
ceffb459 | 2794 | u32 desc_limit; |
e919464b | 2795 | ulong desc_addr; |
38ba30ba GN |
2796 | |
2797 | /* FIXME: old_tss_base == ~0 ? */ | |
2798 | ||
e919464b | 2799 | ret = read_segment_descriptor(ctxt, tss_selector, &next_tss_desc, &desc_addr); |
38ba30ba GN |
2800 | if (ret != X86EMUL_CONTINUE) |
2801 | return ret; | |
e919464b | 2802 | ret = read_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc, &desc_addr); |
38ba30ba GN |
2803 | if (ret != X86EMUL_CONTINUE) |
2804 | return ret; | |
2805 | ||
2806 | /* FIXME: check that next_tss_desc is tss */ | |
2807 | ||
7f3d35fd KW |
2808 | /* |
2809 | * Check privileges. The three cases are task switch caused by... | |
2810 | * | |
2811 | * 1. jmp/call/int to task gate: Check against DPL of the task gate | |
2812 | * 2. Exception/IRQ/iret: No check is performed | |
fc058680 | 2813 | * 3. jmp/call to TSS: Check against DPL of the TSS |
7f3d35fd KW |
2814 | */ |
2815 | if (reason == TASK_SWITCH_GATE) { | |
2816 | if (idt_index != -1) { | |
2817 | /* Software interrupts */ | |
2818 | struct desc_struct task_gate_desc; | |
2819 | int dpl; | |
2820 | ||
2821 | ret = read_interrupt_descriptor(ctxt, idt_index, | |
2822 | &task_gate_desc); | |
2823 | if (ret != X86EMUL_CONTINUE) | |
2824 | return ret; | |
2825 | ||
2826 | dpl = task_gate_desc.dpl; | |
2827 | if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl) | |
2828 | return emulate_gp(ctxt, (idt_index << 3) | 0x2); | |
2829 | } | |
2830 | } else if (reason != TASK_SWITCH_IRET) { | |
2831 | int dpl = next_tss_desc.dpl; | |
2832 | if ((tss_selector & 3) > dpl || ops->cpl(ctxt) > dpl) | |
2833 | return emulate_gp(ctxt, tss_selector); | |
38ba30ba GN |
2834 | } |
2835 | ||
7f3d35fd | 2836 | |
ceffb459 GN |
2837 | desc_limit = desc_limit_scaled(&next_tss_desc); |
2838 | if (!next_tss_desc.p || | |
2839 | ((desc_limit < 0x67 && (next_tss_desc.type & 8)) || | |
2840 | desc_limit < 0x2b)) { | |
54b8486f | 2841 | emulate_ts(ctxt, tss_selector & 0xfffc); |
38ba30ba GN |
2842 | return X86EMUL_PROPAGATE_FAULT; |
2843 | } | |
2844 | ||
2845 | if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) { | |
2846 | curr_tss_desc.type &= ~(1 << 1); /* clear busy flag */ | |
7b105ca2 | 2847 | write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc); |
38ba30ba GN |
2848 | } |
2849 | ||
2850 | if (reason == TASK_SWITCH_IRET) | |
2851 | ctxt->eflags = ctxt->eflags & ~X86_EFLAGS_NT; | |
2852 | ||
2853 | /* set back link to prev task only if NT bit is set in eflags | |
fc058680 | 2854 | note that old_tss_sel is not used after this point */ |
38ba30ba GN |
2855 | if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE) |
2856 | old_tss_sel = 0xffff; | |
2857 | ||
2858 | if (next_tss_desc.type & 8) | |
7b105ca2 | 2859 | ret = task_switch_32(ctxt, tss_selector, old_tss_sel, |
38ba30ba GN |
2860 | old_tss_base, &next_tss_desc); |
2861 | else | |
7b105ca2 | 2862 | ret = task_switch_16(ctxt, tss_selector, old_tss_sel, |
38ba30ba | 2863 | old_tss_base, &next_tss_desc); |
0760d448 JK |
2864 | if (ret != X86EMUL_CONTINUE) |
2865 | return ret; | |
38ba30ba GN |
2866 | |
2867 | if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) | |
2868 | ctxt->eflags = ctxt->eflags | X86_EFLAGS_NT; | |
2869 | ||
2870 | if (reason != TASK_SWITCH_IRET) { | |
2871 | next_tss_desc.type |= (1 << 1); /* set busy flag */ | |
7b105ca2 | 2872 | write_segment_descriptor(ctxt, tss_selector, &next_tss_desc); |
38ba30ba GN |
2873 | } |
2874 | ||
717746e3 | 2875 | ops->set_cr(ctxt, 0, ops->get_cr(ctxt, 0) | X86_CR0_TS); |
1aa36616 | 2876 | ops->set_segment(ctxt, tss_selector, &next_tss_desc, 0, VCPU_SREG_TR); |
38ba30ba | 2877 | |
e269fb21 | 2878 | if (has_error_code) { |
9dac77fa AK |
2879 | ctxt->op_bytes = ctxt->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2; |
2880 | ctxt->lock_prefix = 0; | |
2881 | ctxt->src.val = (unsigned long) error_code; | |
4487b3b4 | 2882 | ret = em_push(ctxt); |
e269fb21 JK |
2883 | } |
2884 | ||
38ba30ba GN |
2885 | return ret; |
2886 | } | |
2887 | ||
2888 | int emulator_task_switch(struct x86_emulate_ctxt *ctxt, | |
7f3d35fd | 2889 | u16 tss_selector, int idt_index, int reason, |
e269fb21 | 2890 | bool has_error_code, u32 error_code) |
38ba30ba | 2891 | { |
38ba30ba GN |
2892 | int rc; |
2893 | ||
dd856efa | 2894 | invalidate_registers(ctxt); |
9dac77fa AK |
2895 | ctxt->_eip = ctxt->eip; |
2896 | ctxt->dst.type = OP_NONE; | |
38ba30ba | 2897 | |
7f3d35fd | 2898 | rc = emulator_do_task_switch(ctxt, tss_selector, idt_index, reason, |
e269fb21 | 2899 | has_error_code, error_code); |
38ba30ba | 2900 | |
dd856efa | 2901 | if (rc == X86EMUL_CONTINUE) { |
9dac77fa | 2902 | ctxt->eip = ctxt->_eip; |
dd856efa AK |
2903 | writeback_registers(ctxt); |
2904 | } | |
38ba30ba | 2905 | |
a0c0ab2f | 2906 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
38ba30ba GN |
2907 | } |
2908 | ||
f3bd64c6 GN |
2909 | static void string_addr_inc(struct x86_emulate_ctxt *ctxt, int reg, |
2910 | struct operand *op) | |
a682e354 | 2911 | { |
b3356bf0 | 2912 | int df = (ctxt->eflags & EFLG_DF) ? -op->count : op->count; |
a682e354 | 2913 | |
dd856efa AK |
2914 | register_address_increment(ctxt, reg_rmw(ctxt, reg), df * op->bytes); |
2915 | op->addr.mem.ea = register_address(ctxt, reg_read(ctxt, reg)); | |
a682e354 GN |
2916 | } |
2917 | ||
7af04fc0 AK |
2918 | static int em_das(struct x86_emulate_ctxt *ctxt) |
2919 | { | |
7af04fc0 AK |
2920 | u8 al, old_al; |
2921 | bool af, cf, old_cf; | |
2922 | ||
2923 | cf = ctxt->eflags & X86_EFLAGS_CF; | |
9dac77fa | 2924 | al = ctxt->dst.val; |
7af04fc0 AK |
2925 | |
2926 | old_al = al; | |
2927 | old_cf = cf; | |
2928 | cf = false; | |
2929 | af = ctxt->eflags & X86_EFLAGS_AF; | |
2930 | if ((al & 0x0f) > 9 || af) { | |
2931 | al -= 6; | |
2932 | cf = old_cf | (al >= 250); | |
2933 | af = true; | |
2934 | } else { | |
2935 | af = false; | |
2936 | } | |
2937 | if (old_al > 0x99 || old_cf) { | |
2938 | al -= 0x60; | |
2939 | cf = true; | |
2940 | } | |
2941 | ||
9dac77fa | 2942 | ctxt->dst.val = al; |
7af04fc0 | 2943 | /* Set PF, ZF, SF */ |
9dac77fa AK |
2944 | ctxt->src.type = OP_IMM; |
2945 | ctxt->src.val = 0; | |
2946 | ctxt->src.bytes = 1; | |
a31b9cea | 2947 | emulate_2op_SrcV(ctxt, "or"); |
7af04fc0 AK |
2948 | ctxt->eflags &= ~(X86_EFLAGS_AF | X86_EFLAGS_CF); |
2949 | if (cf) | |
2950 | ctxt->eflags |= X86_EFLAGS_CF; | |
2951 | if (af) | |
2952 | ctxt->eflags |= X86_EFLAGS_AF; | |
2953 | return X86EMUL_CONTINUE; | |
2954 | } | |
2955 | ||
7f662273 GN |
2956 | static int em_aad(struct x86_emulate_ctxt *ctxt) |
2957 | { | |
2958 | u8 al = ctxt->dst.val & 0xff; | |
2959 | u8 ah = (ctxt->dst.val >> 8) & 0xff; | |
2960 | ||
2961 | al = (al + (ah * ctxt->src.val)) & 0xff; | |
2962 | ||
2963 | ctxt->dst.val = (ctxt->dst.val & 0xffff0000) | al; | |
2964 | ||
2965 | ctxt->eflags &= ~(X86_EFLAGS_PF | X86_EFLAGS_SF | X86_EFLAGS_ZF); | |
2966 | ||
2967 | if (!al) | |
2968 | ctxt->eflags |= X86_EFLAGS_ZF; | |
2969 | if (!(al & 1)) | |
2970 | ctxt->eflags |= X86_EFLAGS_PF; | |
2971 | if (al & 0x80) | |
2972 | ctxt->eflags |= X86_EFLAGS_SF; | |
2973 | ||
2974 | return X86EMUL_CONTINUE; | |
2975 | } | |
2976 | ||
d4ddafcd TY |
2977 | static int em_call(struct x86_emulate_ctxt *ctxt) |
2978 | { | |
2979 | long rel = ctxt->src.val; | |
2980 | ||
2981 | ctxt->src.val = (unsigned long)ctxt->_eip; | |
2982 | jmp_rel(ctxt, rel); | |
2983 | return em_push(ctxt); | |
2984 | } | |
2985 | ||
0ef753b8 AK |
2986 | static int em_call_far(struct x86_emulate_ctxt *ctxt) |
2987 | { | |
0ef753b8 AK |
2988 | u16 sel, old_cs; |
2989 | ulong old_eip; | |
2990 | int rc; | |
2991 | ||
1aa36616 | 2992 | old_cs = get_segment_selector(ctxt, VCPU_SREG_CS); |
9dac77fa | 2993 | old_eip = ctxt->_eip; |
0ef753b8 | 2994 | |
9dac77fa | 2995 | memcpy(&sel, ctxt->src.valptr + ctxt->op_bytes, 2); |
7b105ca2 | 2996 | if (load_segment_descriptor(ctxt, sel, VCPU_SREG_CS)) |
0ef753b8 AK |
2997 | return X86EMUL_CONTINUE; |
2998 | ||
9dac77fa AK |
2999 | ctxt->_eip = 0; |
3000 | memcpy(&ctxt->_eip, ctxt->src.valptr, ctxt->op_bytes); | |
0ef753b8 | 3001 | |
9dac77fa | 3002 | ctxt->src.val = old_cs; |
4487b3b4 | 3003 | rc = em_push(ctxt); |
0ef753b8 AK |
3004 | if (rc != X86EMUL_CONTINUE) |
3005 | return rc; | |
3006 | ||
9dac77fa | 3007 | ctxt->src.val = old_eip; |
4487b3b4 | 3008 | return em_push(ctxt); |
0ef753b8 AK |
3009 | } |
3010 | ||
40ece7c7 AK |
3011 | static int em_ret_near_imm(struct x86_emulate_ctxt *ctxt) |
3012 | { | |
40ece7c7 AK |
3013 | int rc; |
3014 | ||
9dac77fa AK |
3015 | ctxt->dst.type = OP_REG; |
3016 | ctxt->dst.addr.reg = &ctxt->_eip; | |
3017 | ctxt->dst.bytes = ctxt->op_bytes; | |
3018 | rc = emulate_pop(ctxt, &ctxt->dst.val, ctxt->op_bytes); | |
40ece7c7 AK |
3019 | if (rc != X86EMUL_CONTINUE) |
3020 | return rc; | |
5ad105e5 | 3021 | rsp_increment(ctxt, ctxt->src.val); |
40ece7c7 AK |
3022 | return X86EMUL_CONTINUE; |
3023 | } | |
3024 | ||
fb864fbc AK |
3025 | FASTOP2(add); |
3026 | FASTOP2(or); | |
3027 | FASTOP2(adc); | |
3028 | FASTOP2(sbb); | |
3029 | FASTOP2(and); | |
3030 | FASTOP2(sub); | |
3031 | FASTOP2(xor); | |
3032 | FASTOP2(cmp); | |
3033 | FASTOP2(test); | |
9f21ca59 | 3034 | |
0bdea068 AK |
3035 | FASTOP3WCL(shld); |
3036 | FASTOP3WCL(shrd); | |
3037 | ||
e4f973ae TY |
3038 | static int em_xchg(struct x86_emulate_ctxt *ctxt) |
3039 | { | |
e4f973ae | 3040 | /* Write back the register source. */ |
9dac77fa AK |
3041 | ctxt->src.val = ctxt->dst.val; |
3042 | write_register_operand(&ctxt->src); | |
e4f973ae TY |
3043 | |
3044 | /* Write back the memory destination with implicit LOCK prefix. */ | |
9dac77fa AK |
3045 | ctxt->dst.val = ctxt->src.orig_val; |
3046 | ctxt->lock_prefix = 1; | |
e4f973ae TY |
3047 | return X86EMUL_CONTINUE; |
3048 | } | |
3049 | ||
5c82aa29 | 3050 | static int em_imul(struct x86_emulate_ctxt *ctxt) |
f3a1b9f4 | 3051 | { |
a31b9cea | 3052 | emulate_2op_SrcV_nobyte(ctxt, "imul"); |
f3a1b9f4 AK |
3053 | return X86EMUL_CONTINUE; |
3054 | } | |
3055 | ||
5c82aa29 AK |
3056 | static int em_imul_3op(struct x86_emulate_ctxt *ctxt) |
3057 | { | |
9dac77fa | 3058 | ctxt->dst.val = ctxt->src2.val; |
5c82aa29 AK |
3059 | return em_imul(ctxt); |
3060 | } | |
3061 | ||
61429142 AK |
3062 | static int em_cwd(struct x86_emulate_ctxt *ctxt) |
3063 | { | |
9dac77fa AK |
3064 | ctxt->dst.type = OP_REG; |
3065 | ctxt->dst.bytes = ctxt->src.bytes; | |
dd856efa | 3066 | ctxt->dst.addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
9dac77fa | 3067 | ctxt->dst.val = ~((ctxt->src.val >> (ctxt->src.bytes * 8 - 1)) - 1); |
61429142 AK |
3068 | |
3069 | return X86EMUL_CONTINUE; | |
3070 | } | |
3071 | ||
48bb5d3c AK |
3072 | static int em_rdtsc(struct x86_emulate_ctxt *ctxt) |
3073 | { | |
48bb5d3c AK |
3074 | u64 tsc = 0; |
3075 | ||
717746e3 | 3076 | ctxt->ops->get_msr(ctxt, MSR_IA32_TSC, &tsc); |
dd856efa AK |
3077 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)tsc; |
3078 | *reg_write(ctxt, VCPU_REGS_RDX) = tsc >> 32; | |
48bb5d3c AK |
3079 | return X86EMUL_CONTINUE; |
3080 | } | |
3081 | ||
222d21aa AK |
3082 | static int em_rdpmc(struct x86_emulate_ctxt *ctxt) |
3083 | { | |
3084 | u64 pmc; | |
3085 | ||
dd856efa | 3086 | if (ctxt->ops->read_pmc(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &pmc)) |
222d21aa | 3087 | return emulate_gp(ctxt, 0); |
dd856efa AK |
3088 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)pmc; |
3089 | *reg_write(ctxt, VCPU_REGS_RDX) = pmc >> 32; | |
222d21aa AK |
3090 | return X86EMUL_CONTINUE; |
3091 | } | |
3092 | ||
b9eac5f4 AK |
3093 | static int em_mov(struct x86_emulate_ctxt *ctxt) |
3094 | { | |
49597d81 | 3095 | memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes); |
b9eac5f4 AK |
3096 | return X86EMUL_CONTINUE; |
3097 | } | |
3098 | ||
bc00f8d2 TY |
3099 | static int em_cr_write(struct x86_emulate_ctxt *ctxt) |
3100 | { | |
3101 | if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val)) | |
3102 | return emulate_gp(ctxt, 0); | |
3103 | ||
3104 | /* Disable writeback. */ | |
3105 | ctxt->dst.type = OP_NONE; | |
3106 | return X86EMUL_CONTINUE; | |
3107 | } | |
3108 | ||
3109 | static int em_dr_write(struct x86_emulate_ctxt *ctxt) | |
3110 | { | |
3111 | unsigned long val; | |
3112 | ||
3113 | if (ctxt->mode == X86EMUL_MODE_PROT64) | |
3114 | val = ctxt->src.val & ~0ULL; | |
3115 | else | |
3116 | val = ctxt->src.val & ~0U; | |
3117 | ||
3118 | /* #UD condition is already handled. */ | |
3119 | if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val) < 0) | |
3120 | return emulate_gp(ctxt, 0); | |
3121 | ||
3122 | /* Disable writeback. */ | |
3123 | ctxt->dst.type = OP_NONE; | |
3124 | return X86EMUL_CONTINUE; | |
3125 | } | |
3126 | ||
e1e210b0 TY |
3127 | static int em_wrmsr(struct x86_emulate_ctxt *ctxt) |
3128 | { | |
3129 | u64 msr_data; | |
3130 | ||
dd856efa AK |
3131 | msr_data = (u32)reg_read(ctxt, VCPU_REGS_RAX) |
3132 | | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32); | |
3133 | if (ctxt->ops->set_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), msr_data)) | |
e1e210b0 TY |
3134 | return emulate_gp(ctxt, 0); |
3135 | ||
3136 | return X86EMUL_CONTINUE; | |
3137 | } | |
3138 | ||
3139 | static int em_rdmsr(struct x86_emulate_ctxt *ctxt) | |
3140 | { | |
3141 | u64 msr_data; | |
3142 | ||
dd856efa | 3143 | if (ctxt->ops->get_msr(ctxt, reg_read(ctxt, VCPU_REGS_RCX), &msr_data)) |
e1e210b0 TY |
3144 | return emulate_gp(ctxt, 0); |
3145 | ||
dd856efa AK |
3146 | *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data; |
3147 | *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32; | |
e1e210b0 TY |
3148 | return X86EMUL_CONTINUE; |
3149 | } | |
3150 | ||
1bd5f469 TY |
3151 | static int em_mov_rm_sreg(struct x86_emulate_ctxt *ctxt) |
3152 | { | |
9dac77fa | 3153 | if (ctxt->modrm_reg > VCPU_SREG_GS) |
1bd5f469 TY |
3154 | return emulate_ud(ctxt); |
3155 | ||
9dac77fa | 3156 | ctxt->dst.val = get_segment_selector(ctxt, ctxt->modrm_reg); |
1bd5f469 TY |
3157 | return X86EMUL_CONTINUE; |
3158 | } | |
3159 | ||
3160 | static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt) | |
3161 | { | |
9dac77fa | 3162 | u16 sel = ctxt->src.val; |
1bd5f469 | 3163 | |
9dac77fa | 3164 | if (ctxt->modrm_reg == VCPU_SREG_CS || ctxt->modrm_reg > VCPU_SREG_GS) |
1bd5f469 TY |
3165 | return emulate_ud(ctxt); |
3166 | ||
9dac77fa | 3167 | if (ctxt->modrm_reg == VCPU_SREG_SS) |
1bd5f469 TY |
3168 | ctxt->interruptibility = KVM_X86_SHADOW_INT_MOV_SS; |
3169 | ||
3170 | /* Disable writeback. */ | |
9dac77fa AK |
3171 | ctxt->dst.type = OP_NONE; |
3172 | return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg); | |
1bd5f469 TY |
3173 | } |
3174 | ||
a14e579f AK |
3175 | static int em_lldt(struct x86_emulate_ctxt *ctxt) |
3176 | { | |
3177 | u16 sel = ctxt->src.val; | |
3178 | ||
3179 | /* Disable writeback. */ | |
3180 | ctxt->dst.type = OP_NONE; | |
3181 | return load_segment_descriptor(ctxt, sel, VCPU_SREG_LDTR); | |
3182 | } | |
3183 | ||
80890006 AK |
3184 | static int em_ltr(struct x86_emulate_ctxt *ctxt) |
3185 | { | |
3186 | u16 sel = ctxt->src.val; | |
3187 | ||
3188 | /* Disable writeback. */ | |
3189 | ctxt->dst.type = OP_NONE; | |
3190 | return load_segment_descriptor(ctxt, sel, VCPU_SREG_TR); | |
3191 | } | |
3192 | ||
38503911 AK |
3193 | static int em_invlpg(struct x86_emulate_ctxt *ctxt) |
3194 | { | |
9fa088f4 AK |
3195 | int rc; |
3196 | ulong linear; | |
3197 | ||
9dac77fa | 3198 | rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear); |
9fa088f4 | 3199 | if (rc == X86EMUL_CONTINUE) |
3cb16fe7 | 3200 | ctxt->ops->invlpg(ctxt, linear); |
38503911 | 3201 | /* Disable writeback. */ |
9dac77fa | 3202 | ctxt->dst.type = OP_NONE; |
38503911 AK |
3203 | return X86EMUL_CONTINUE; |
3204 | } | |
3205 | ||
2d04a05b AK |
3206 | static int em_clts(struct x86_emulate_ctxt *ctxt) |
3207 | { | |
3208 | ulong cr0; | |
3209 | ||
3210 | cr0 = ctxt->ops->get_cr(ctxt, 0); | |
3211 | cr0 &= ~X86_CR0_TS; | |
3212 | ctxt->ops->set_cr(ctxt, 0, cr0); | |
3213 | return X86EMUL_CONTINUE; | |
3214 | } | |
3215 | ||
26d05cc7 AK |
3216 | static int em_vmcall(struct x86_emulate_ctxt *ctxt) |
3217 | { | |
26d05cc7 AK |
3218 | int rc; |
3219 | ||
9dac77fa | 3220 | if (ctxt->modrm_mod != 3 || ctxt->modrm_rm != 1) |
26d05cc7 AK |
3221 | return X86EMUL_UNHANDLEABLE; |
3222 | ||
3223 | rc = ctxt->ops->fix_hypercall(ctxt); | |
3224 | if (rc != X86EMUL_CONTINUE) | |
3225 | return rc; | |
3226 | ||
3227 | /* Let the processor re-execute the fixed hypercall */ | |
9dac77fa | 3228 | ctxt->_eip = ctxt->eip; |
26d05cc7 | 3229 | /* Disable writeback. */ |
9dac77fa | 3230 | ctxt->dst.type = OP_NONE; |
26d05cc7 AK |
3231 | return X86EMUL_CONTINUE; |
3232 | } | |
3233 | ||
96051572 AK |
3234 | static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt, |
3235 | void (*get)(struct x86_emulate_ctxt *ctxt, | |
3236 | struct desc_ptr *ptr)) | |
3237 | { | |
3238 | struct desc_ptr desc_ptr; | |
3239 | ||
3240 | if (ctxt->mode == X86EMUL_MODE_PROT64) | |
3241 | ctxt->op_bytes = 8; | |
3242 | get(ctxt, &desc_ptr); | |
3243 | if (ctxt->op_bytes == 2) { | |
3244 | ctxt->op_bytes = 4; | |
3245 | desc_ptr.address &= 0x00ffffff; | |
3246 | } | |
3247 | /* Disable writeback. */ | |
3248 | ctxt->dst.type = OP_NONE; | |
3249 | return segmented_write(ctxt, ctxt->dst.addr.mem, | |
3250 | &desc_ptr, 2 + ctxt->op_bytes); | |
3251 | } | |
3252 | ||
3253 | static int em_sgdt(struct x86_emulate_ctxt *ctxt) | |
3254 | { | |
3255 | return emulate_store_desc_ptr(ctxt, ctxt->ops->get_gdt); | |
3256 | } | |
3257 | ||
3258 | static int em_sidt(struct x86_emulate_ctxt *ctxt) | |
3259 | { | |
3260 | return emulate_store_desc_ptr(ctxt, ctxt->ops->get_idt); | |
3261 | } | |
3262 | ||
26d05cc7 AK |
3263 | static int em_lgdt(struct x86_emulate_ctxt *ctxt) |
3264 | { | |
26d05cc7 AK |
3265 | struct desc_ptr desc_ptr; |
3266 | int rc; | |
3267 | ||
510425ff AK |
3268 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
3269 | ctxt->op_bytes = 8; | |
9dac77fa | 3270 | rc = read_descriptor(ctxt, ctxt->src.addr.mem, |
26d05cc7 | 3271 | &desc_ptr.size, &desc_ptr.address, |
9dac77fa | 3272 | ctxt->op_bytes); |
26d05cc7 AK |
3273 | if (rc != X86EMUL_CONTINUE) |
3274 | return rc; | |
3275 | ctxt->ops->set_gdt(ctxt, &desc_ptr); | |
3276 | /* Disable writeback. */ | |
9dac77fa | 3277 | ctxt->dst.type = OP_NONE; |
26d05cc7 AK |
3278 | return X86EMUL_CONTINUE; |
3279 | } | |
3280 | ||
5ef39c71 | 3281 | static int em_vmmcall(struct x86_emulate_ctxt *ctxt) |
26d05cc7 | 3282 | { |
26d05cc7 AK |
3283 | int rc; |
3284 | ||
5ef39c71 AK |
3285 | rc = ctxt->ops->fix_hypercall(ctxt); |
3286 | ||
26d05cc7 | 3287 | /* Disable writeback. */ |
9dac77fa | 3288 | ctxt->dst.type = OP_NONE; |
26d05cc7 AK |
3289 | return rc; |
3290 | } | |
3291 | ||
3292 | static int em_lidt(struct x86_emulate_ctxt *ctxt) | |
3293 | { | |
26d05cc7 AK |
3294 | struct desc_ptr desc_ptr; |
3295 | int rc; | |
3296 | ||
510425ff AK |
3297 | if (ctxt->mode == X86EMUL_MODE_PROT64) |
3298 | ctxt->op_bytes = 8; | |
9dac77fa | 3299 | rc = read_descriptor(ctxt, ctxt->src.addr.mem, |
509cf9fe | 3300 | &desc_ptr.size, &desc_ptr.address, |
9dac77fa | 3301 | ctxt->op_bytes); |
26d05cc7 AK |
3302 | if (rc != X86EMUL_CONTINUE) |
3303 | return rc; | |
3304 | ctxt->ops->set_idt(ctxt, &desc_ptr); | |
3305 | /* Disable writeback. */ | |
9dac77fa | 3306 | ctxt->dst.type = OP_NONE; |
26d05cc7 AK |
3307 | return X86EMUL_CONTINUE; |
3308 | } | |
3309 | ||
3310 | static int em_smsw(struct x86_emulate_ctxt *ctxt) | |
3311 | { | |
9dac77fa AK |
3312 | ctxt->dst.bytes = 2; |
3313 | ctxt->dst.val = ctxt->ops->get_cr(ctxt, 0); | |
26d05cc7 AK |
3314 | return X86EMUL_CONTINUE; |
3315 | } | |
3316 | ||
3317 | static int em_lmsw(struct x86_emulate_ctxt *ctxt) | |
3318 | { | |
26d05cc7 | 3319 | ctxt->ops->set_cr(ctxt, 0, (ctxt->ops->get_cr(ctxt, 0) & ~0x0eul) |
9dac77fa AK |
3320 | | (ctxt->src.val & 0x0f)); |
3321 | ctxt->dst.type = OP_NONE; | |
26d05cc7 AK |
3322 | return X86EMUL_CONTINUE; |
3323 | } | |
3324 | ||
d06e03ad TY |
3325 | static int em_loop(struct x86_emulate_ctxt *ctxt) |
3326 | { | |
dd856efa AK |
3327 | register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), -1); |
3328 | if ((address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) != 0) && | |
9dac77fa AK |
3329 | (ctxt->b == 0xe2 || test_cc(ctxt->b ^ 0x5, ctxt->eflags))) |
3330 | jmp_rel(ctxt, ctxt->src.val); | |
d06e03ad TY |
3331 | |
3332 | return X86EMUL_CONTINUE; | |
3333 | } | |
3334 | ||
3335 | static int em_jcxz(struct x86_emulate_ctxt *ctxt) | |
3336 | { | |
dd856efa | 3337 | if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) |
9dac77fa | 3338 | jmp_rel(ctxt, ctxt->src.val); |
d06e03ad TY |
3339 | |
3340 | return X86EMUL_CONTINUE; | |
3341 | } | |
3342 | ||
d7841a4b TY |
3343 | static int em_in(struct x86_emulate_ctxt *ctxt) |
3344 | { | |
3345 | if (!pio_in_emulated(ctxt, ctxt->dst.bytes, ctxt->src.val, | |
3346 | &ctxt->dst.val)) | |
3347 | return X86EMUL_IO_NEEDED; | |
3348 | ||
3349 | return X86EMUL_CONTINUE; | |
3350 | } | |
3351 | ||
3352 | static int em_out(struct x86_emulate_ctxt *ctxt) | |
3353 | { | |
3354 | ctxt->ops->pio_out_emulated(ctxt, ctxt->src.bytes, ctxt->dst.val, | |
3355 | &ctxt->src.val, 1); | |
3356 | /* Disable writeback. */ | |
3357 | ctxt->dst.type = OP_NONE; | |
3358 | return X86EMUL_CONTINUE; | |
3359 | } | |
3360 | ||
f411e6cd TY |
3361 | static int em_cli(struct x86_emulate_ctxt *ctxt) |
3362 | { | |
3363 | if (emulator_bad_iopl(ctxt)) | |
3364 | return emulate_gp(ctxt, 0); | |
3365 | ||
3366 | ctxt->eflags &= ~X86_EFLAGS_IF; | |
3367 | return X86EMUL_CONTINUE; | |
3368 | } | |
3369 | ||
3370 | static int em_sti(struct x86_emulate_ctxt *ctxt) | |
3371 | { | |
3372 | if (emulator_bad_iopl(ctxt)) | |
3373 | return emulate_gp(ctxt, 0); | |
3374 | ||
3375 | ctxt->interruptibility = KVM_X86_SHADOW_INT_STI; | |
3376 | ctxt->eflags |= X86_EFLAGS_IF; | |
3377 | return X86EMUL_CONTINUE; | |
3378 | } | |
3379 | ||
ce7faab2 TY |
3380 | static int em_bt(struct x86_emulate_ctxt *ctxt) |
3381 | { | |
3382 | /* Disable writeback. */ | |
3383 | ctxt->dst.type = OP_NONE; | |
3384 | /* only subword offset */ | |
3385 | ctxt->src.val &= (ctxt->dst.bytes << 3) - 1; | |
3386 | ||
3387 | emulate_2op_SrcV_nobyte(ctxt, "bt"); | |
3388 | return X86EMUL_CONTINUE; | |
3389 | } | |
3390 | ||
3391 | static int em_bts(struct x86_emulate_ctxt *ctxt) | |
3392 | { | |
3393 | emulate_2op_SrcV_nobyte(ctxt, "bts"); | |
3394 | return X86EMUL_CONTINUE; | |
3395 | } | |
3396 | ||
3397 | static int em_btr(struct x86_emulate_ctxt *ctxt) | |
3398 | { | |
3399 | emulate_2op_SrcV_nobyte(ctxt, "btr"); | |
3400 | return X86EMUL_CONTINUE; | |
3401 | } | |
3402 | ||
3403 | static int em_btc(struct x86_emulate_ctxt *ctxt) | |
3404 | { | |
3405 | emulate_2op_SrcV_nobyte(ctxt, "btc"); | |
3406 | return X86EMUL_CONTINUE; | |
3407 | } | |
3408 | ||
ff227392 TY |
3409 | static int em_bsf(struct x86_emulate_ctxt *ctxt) |
3410 | { | |
d54e4237 | 3411 | emulate_2op_SrcV_nobyte(ctxt, "bsf"); |
ff227392 TY |
3412 | return X86EMUL_CONTINUE; |
3413 | } | |
3414 | ||
3415 | static int em_bsr(struct x86_emulate_ctxt *ctxt) | |
3416 | { | |
d54e4237 | 3417 | emulate_2op_SrcV_nobyte(ctxt, "bsr"); |
ff227392 TY |
3418 | return X86EMUL_CONTINUE; |
3419 | } | |
3420 | ||
6d6eede4 AK |
3421 | static int em_cpuid(struct x86_emulate_ctxt *ctxt) |
3422 | { | |
3423 | u32 eax, ebx, ecx, edx; | |
3424 | ||
dd856efa AK |
3425 | eax = reg_read(ctxt, VCPU_REGS_RAX); |
3426 | ecx = reg_read(ctxt, VCPU_REGS_RCX); | |
6d6eede4 | 3427 | ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); |
dd856efa AK |
3428 | *reg_write(ctxt, VCPU_REGS_RAX) = eax; |
3429 | *reg_write(ctxt, VCPU_REGS_RBX) = ebx; | |
3430 | *reg_write(ctxt, VCPU_REGS_RCX) = ecx; | |
3431 | *reg_write(ctxt, VCPU_REGS_RDX) = edx; | |
6d6eede4 AK |
3432 | return X86EMUL_CONTINUE; |
3433 | } | |
3434 | ||
2dd7caa0 AK |
3435 | static int em_lahf(struct x86_emulate_ctxt *ctxt) |
3436 | { | |
dd856efa AK |
3437 | *reg_rmw(ctxt, VCPU_REGS_RAX) &= ~0xff00UL; |
3438 | *reg_rmw(ctxt, VCPU_REGS_RAX) |= (ctxt->eflags & 0xff) << 8; | |
2dd7caa0 AK |
3439 | return X86EMUL_CONTINUE; |
3440 | } | |
3441 | ||
9299836e AK |
3442 | static int em_bswap(struct x86_emulate_ctxt *ctxt) |
3443 | { | |
3444 | switch (ctxt->op_bytes) { | |
3445 | #ifdef CONFIG_X86_64 | |
3446 | case 8: | |
3447 | asm("bswap %0" : "+r"(ctxt->dst.val)); | |
3448 | break; | |
3449 | #endif | |
3450 | default: | |
3451 | asm("bswap %0" : "+r"(*(u32 *)&ctxt->dst.val)); | |
3452 | break; | |
3453 | } | |
3454 | return X86EMUL_CONTINUE; | |
3455 | } | |
3456 | ||
cfec82cb JR |
3457 | static bool valid_cr(int nr) |
3458 | { | |
3459 | switch (nr) { | |
3460 | case 0: | |
3461 | case 2 ... 4: | |
3462 | case 8: | |
3463 | return true; | |
3464 | default: | |
3465 | return false; | |
3466 | } | |
3467 | } | |
3468 | ||
3469 | static int check_cr_read(struct x86_emulate_ctxt *ctxt) | |
3470 | { | |
9dac77fa | 3471 | if (!valid_cr(ctxt->modrm_reg)) |
cfec82cb JR |
3472 | return emulate_ud(ctxt); |
3473 | ||
3474 | return X86EMUL_CONTINUE; | |
3475 | } | |
3476 | ||
3477 | static int check_cr_write(struct x86_emulate_ctxt *ctxt) | |
3478 | { | |
9dac77fa AK |
3479 | u64 new_val = ctxt->src.val64; |
3480 | int cr = ctxt->modrm_reg; | |
c2ad2bb3 | 3481 | u64 efer = 0; |
cfec82cb JR |
3482 | |
3483 | static u64 cr_reserved_bits[] = { | |
3484 | 0xffffffff00000000ULL, | |
3485 | 0, 0, 0, /* CR3 checked later */ | |
3486 | CR4_RESERVED_BITS, | |
3487 | 0, 0, 0, | |
3488 | CR8_RESERVED_BITS, | |
3489 | }; | |
3490 | ||
3491 | if (!valid_cr(cr)) | |
3492 | return emulate_ud(ctxt); | |
3493 | ||
3494 | if (new_val & cr_reserved_bits[cr]) | |
3495 | return emulate_gp(ctxt, 0); | |
3496 | ||
3497 | switch (cr) { | |
3498 | case 0: { | |
c2ad2bb3 | 3499 | u64 cr4; |
cfec82cb JR |
3500 | if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) || |
3501 | ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD))) | |
3502 | return emulate_gp(ctxt, 0); | |
3503 | ||
717746e3 AK |
3504 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
3505 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); | |
cfec82cb JR |
3506 | |
3507 | if ((new_val & X86_CR0_PG) && (efer & EFER_LME) && | |
3508 | !(cr4 & X86_CR4_PAE)) | |
3509 | return emulate_gp(ctxt, 0); | |
3510 | ||
3511 | break; | |
3512 | } | |
3513 | case 3: { | |
3514 | u64 rsvd = 0; | |
3515 | ||
c2ad2bb3 AK |
3516 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
3517 | if (efer & EFER_LMA) | |
cfec82cb | 3518 | rsvd = CR3_L_MODE_RESERVED_BITS; |
fd72c419 | 3519 | else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE) |
cfec82cb | 3520 | rsvd = CR3_PAE_RESERVED_BITS; |
fd72c419 | 3521 | else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG) |
cfec82cb JR |
3522 | rsvd = CR3_NONPAE_RESERVED_BITS; |
3523 | ||
3524 | if (new_val & rsvd) | |
3525 | return emulate_gp(ctxt, 0); | |
3526 | ||
3527 | break; | |
3528 | } | |
3529 | case 4: { | |
717746e3 | 3530 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
cfec82cb JR |
3531 | |
3532 | if ((efer & EFER_LMA) && !(new_val & X86_CR4_PAE)) | |
3533 | return emulate_gp(ctxt, 0); | |
3534 | ||
3535 | break; | |
3536 | } | |
3537 | } | |
3538 | ||
3539 | return X86EMUL_CONTINUE; | |
3540 | } | |
3541 | ||
3b88e41a JR |
3542 | static int check_dr7_gd(struct x86_emulate_ctxt *ctxt) |
3543 | { | |
3544 | unsigned long dr7; | |
3545 | ||
717746e3 | 3546 | ctxt->ops->get_dr(ctxt, 7, &dr7); |
3b88e41a JR |
3547 | |
3548 | /* Check if DR7.Global_Enable is set */ | |
3549 | return dr7 & (1 << 13); | |
3550 | } | |
3551 | ||
3552 | static int check_dr_read(struct x86_emulate_ctxt *ctxt) | |
3553 | { | |
9dac77fa | 3554 | int dr = ctxt->modrm_reg; |
3b88e41a JR |
3555 | u64 cr4; |
3556 | ||
3557 | if (dr > 7) | |
3558 | return emulate_ud(ctxt); | |
3559 | ||
717746e3 | 3560 | cr4 = ctxt->ops->get_cr(ctxt, 4); |
3b88e41a JR |
3561 | if ((cr4 & X86_CR4_DE) && (dr == 4 || dr == 5)) |
3562 | return emulate_ud(ctxt); | |
3563 | ||
3564 | if (check_dr7_gd(ctxt)) | |
3565 | return emulate_db(ctxt); | |
3566 | ||
3567 | return X86EMUL_CONTINUE; | |
3568 | } | |
3569 | ||
3570 | static int check_dr_write(struct x86_emulate_ctxt *ctxt) | |
3571 | { | |
9dac77fa AK |
3572 | u64 new_val = ctxt->src.val64; |
3573 | int dr = ctxt->modrm_reg; | |
3b88e41a JR |
3574 | |
3575 | if ((dr == 6 || dr == 7) && (new_val & 0xffffffff00000000ULL)) | |
3576 | return emulate_gp(ctxt, 0); | |
3577 | ||
3578 | return check_dr_read(ctxt); | |
3579 | } | |
3580 | ||
01de8b09 JR |
3581 | static int check_svme(struct x86_emulate_ctxt *ctxt) |
3582 | { | |
3583 | u64 efer; | |
3584 | ||
717746e3 | 3585 | ctxt->ops->get_msr(ctxt, MSR_EFER, &efer); |
01de8b09 JR |
3586 | |
3587 | if (!(efer & EFER_SVME)) | |
3588 | return emulate_ud(ctxt); | |
3589 | ||
3590 | return X86EMUL_CONTINUE; | |
3591 | } | |
3592 | ||
3593 | static int check_svme_pa(struct x86_emulate_ctxt *ctxt) | |
3594 | { | |
dd856efa | 3595 | u64 rax = reg_read(ctxt, VCPU_REGS_RAX); |
01de8b09 JR |
3596 | |
3597 | /* Valid physical address? */ | |
d4224449 | 3598 | if (rax & 0xffff000000000000ULL) |
01de8b09 JR |
3599 | return emulate_gp(ctxt, 0); |
3600 | ||
3601 | return check_svme(ctxt); | |
3602 | } | |
3603 | ||
d7eb8203 JR |
3604 | static int check_rdtsc(struct x86_emulate_ctxt *ctxt) |
3605 | { | |
717746e3 | 3606 | u64 cr4 = ctxt->ops->get_cr(ctxt, 4); |
d7eb8203 | 3607 | |
717746e3 | 3608 | if (cr4 & X86_CR4_TSD && ctxt->ops->cpl(ctxt)) |
d7eb8203 JR |
3609 | return emulate_ud(ctxt); |
3610 | ||
3611 | return X86EMUL_CONTINUE; | |
3612 | } | |
3613 | ||
8061252e JR |
3614 | static int check_rdpmc(struct x86_emulate_ctxt *ctxt) |
3615 | { | |
717746e3 | 3616 | u64 cr4 = ctxt->ops->get_cr(ctxt, 4); |
dd856efa | 3617 | u64 rcx = reg_read(ctxt, VCPU_REGS_RCX); |
8061252e | 3618 | |
717746e3 | 3619 | if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) || |
8061252e JR |
3620 | (rcx > 3)) |
3621 | return emulate_gp(ctxt, 0); | |
3622 | ||
3623 | return X86EMUL_CONTINUE; | |
3624 | } | |
3625 | ||
f6511935 JR |
3626 | static int check_perm_in(struct x86_emulate_ctxt *ctxt) |
3627 | { | |
9dac77fa AK |
3628 | ctxt->dst.bytes = min(ctxt->dst.bytes, 4u); |
3629 | if (!emulator_io_permited(ctxt, ctxt->src.val, ctxt->dst.bytes)) | |
f6511935 JR |
3630 | return emulate_gp(ctxt, 0); |
3631 | ||
3632 | return X86EMUL_CONTINUE; | |
3633 | } | |
3634 | ||
3635 | static int check_perm_out(struct x86_emulate_ctxt *ctxt) | |
3636 | { | |
9dac77fa AK |
3637 | ctxt->src.bytes = min(ctxt->src.bytes, 4u); |
3638 | if (!emulator_io_permited(ctxt, ctxt->dst.val, ctxt->src.bytes)) | |
f6511935 JR |
3639 | return emulate_gp(ctxt, 0); |
3640 | ||
3641 | return X86EMUL_CONTINUE; | |
3642 | } | |
3643 | ||
73fba5f4 | 3644 | #define D(_y) { .flags = (_y) } |
c4f035c6 | 3645 | #define DI(_y, _i) { .flags = (_y), .intercept = x86_intercept_##_i } |
d09beabd JR |
3646 | #define DIP(_y, _i, _p) { .flags = (_y), .intercept = x86_intercept_##_i, \ |
3647 | .check_perm = (_p) } | |
73fba5f4 | 3648 | #define N D(0) |
01de8b09 | 3649 | #define EXT(_f, _e) { .flags = ((_f) | RMExt), .u.group = (_e) } |
1c2545be TY |
3650 | #define G(_f, _g) { .flags = ((_f) | Group | ModRM), .u.group = (_g) } |
3651 | #define GD(_f, _g) { .flags = ((_f) | GroupDual | ModRM), .u.gdual = (_g) } | |
045a282c | 3652 | #define E(_f, _e) { .flags = ((_f) | Escape | ModRM), .u.esc = (_e) } |
73fba5f4 | 3653 | #define I(_f, _e) { .flags = (_f), .u.execute = (_e) } |
e28bbd44 | 3654 | #define F(_f, _e) { .flags = (_f) | Fastop, .u.fastop = (_e) } |
c4f035c6 AK |
3655 | #define II(_f, _e, _i) \ |
3656 | { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i } | |
d09beabd JR |
3657 | #define IIP(_f, _e, _i, _p) \ |
3658 | { .flags = (_f), .u.execute = (_e), .intercept = x86_intercept_##_i, \ | |
3659 | .check_perm = (_p) } | |
aa97bb48 | 3660 | #define GP(_f, _g) { .flags = ((_f) | Prefix), .u.gprefix = (_g) } |
73fba5f4 | 3661 | |
8d8f4e9f | 3662 | #define D2bv(_f) D((_f) | ByteOp), D(_f) |
f6511935 | 3663 | #define D2bvIP(_f, _i, _p) DIP((_f) | ByteOp, _i, _p), DIP(_f, _i, _p) |
8d8f4e9f | 3664 | #define I2bv(_f, _e) I((_f) | ByteOp, _e), I(_f, _e) |
f7857f35 | 3665 | #define F2bv(_f, _e) F((_f) | ByteOp, _e), F(_f, _e) |
d7841a4b TY |
3666 | #define I2bvIP(_f, _e, _i, _p) \ |
3667 | IIP((_f) | ByteOp, _e, _i, _p), IIP(_f, _e, _i, _p) | |
8d8f4e9f | 3668 | |
fb864fbc AK |
3669 | #define F6ALU(_f, _e) F2bv((_f) | DstMem | SrcReg | ModRM, _e), \ |
3670 | F2bv(((_f) | DstReg | SrcMem | ModRM) & ~Lock, _e), \ | |
3671 | F2bv(((_f) & ~Lock) | DstAcc | SrcImm, _e) | |
6230f7fc | 3672 | |
fd0a0d82 | 3673 | static const struct opcode group7_rm1[] = { |
1c2545be TY |
3674 | DI(SrcNone | Priv, monitor), |
3675 | DI(SrcNone | Priv, mwait), | |
d7eb8203 JR |
3676 | N, N, N, N, N, N, |
3677 | }; | |
3678 | ||
fd0a0d82 | 3679 | static const struct opcode group7_rm3[] = { |
1c2545be TY |
3680 | DIP(SrcNone | Prot | Priv, vmrun, check_svme_pa), |
3681 | II(SrcNone | Prot | VendorSpecific, em_vmmcall, vmmcall), | |
3682 | DIP(SrcNone | Prot | Priv, vmload, check_svme_pa), | |
3683 | DIP(SrcNone | Prot | Priv, vmsave, check_svme_pa), | |
3684 | DIP(SrcNone | Prot | Priv, stgi, check_svme), | |
3685 | DIP(SrcNone | Prot | Priv, clgi, check_svme), | |
3686 | DIP(SrcNone | Prot | Priv, skinit, check_svme), | |
3687 | DIP(SrcNone | Prot | Priv, invlpga, check_svme), | |
01de8b09 | 3688 | }; |
6230f7fc | 3689 | |
fd0a0d82 | 3690 | static const struct opcode group7_rm7[] = { |
d7eb8203 | 3691 | N, |
1c2545be | 3692 | DIP(SrcNone, rdtscp, check_rdtsc), |
d7eb8203 JR |
3693 | N, N, N, N, N, N, |
3694 | }; | |
d67fc27a | 3695 | |
fd0a0d82 | 3696 | static const struct opcode group1[] = { |
fb864fbc AK |
3697 | F(Lock, em_add), |
3698 | F(Lock | PageTable, em_or), | |
3699 | F(Lock, em_adc), | |
3700 | F(Lock, em_sbb), | |
3701 | F(Lock | PageTable, em_and), | |
3702 | F(Lock, em_sub), | |
3703 | F(Lock, em_xor), | |
3704 | F(NoWrite, em_cmp), | |
73fba5f4 AK |
3705 | }; |
3706 | ||
fd0a0d82 | 3707 | static const struct opcode group1A[] = { |
1c2545be | 3708 | I(DstMem | SrcNone | Mov | Stack, em_pop), N, N, N, N, N, N, N, |
73fba5f4 AK |
3709 | }; |
3710 | ||
007a3b54 AK |
3711 | static const struct opcode group2[] = { |
3712 | F(DstMem | ModRM, em_rol), | |
3713 | F(DstMem | ModRM, em_ror), | |
3714 | F(DstMem | ModRM, em_rcl), | |
3715 | F(DstMem | ModRM, em_rcr), | |
3716 | F(DstMem | ModRM, em_shl), | |
3717 | F(DstMem | ModRM, em_shr), | |
3718 | F(DstMem | ModRM, em_shl), | |
3719 | F(DstMem | ModRM, em_sar), | |
3720 | }; | |
3721 | ||
fd0a0d82 | 3722 | static const struct opcode group3[] = { |
fb864fbc AK |
3723 | F(DstMem | SrcImm | NoWrite, em_test), |
3724 | F(DstMem | SrcImm | NoWrite, em_test), | |
45a1467d AK |
3725 | F(DstMem | SrcNone | Lock, em_not), |
3726 | F(DstMem | SrcNone | Lock, em_neg), | |
1c2545be TY |
3727 | I(SrcMem, em_mul_ex), |
3728 | I(SrcMem, em_imul_ex), | |
3729 | I(SrcMem, em_div_ex), | |
3730 | I(SrcMem, em_idiv_ex), | |
73fba5f4 AK |
3731 | }; |
3732 | ||
fd0a0d82 | 3733 | static const struct opcode group4[] = { |
95413dc4 AK |
3734 | F(ByteOp | DstMem | SrcNone | Lock, em_inc), |
3735 | F(ByteOp | DstMem | SrcNone | Lock, em_dec), | |
73fba5f4 AK |
3736 | N, N, N, N, N, N, |
3737 | }; | |
3738 | ||
fd0a0d82 | 3739 | static const struct opcode group5[] = { |
95413dc4 AK |
3740 | F(DstMem | SrcNone | Lock, em_inc), |
3741 | F(DstMem | SrcNone | Lock, em_dec), | |
1c2545be TY |
3742 | I(SrcMem | Stack, em_grp45), |
3743 | I(SrcMemFAddr | ImplicitOps | Stack, em_call_far), | |
3744 | I(SrcMem | Stack, em_grp45), | |
3745 | I(SrcMemFAddr | ImplicitOps, em_grp45), | |
3746 | I(SrcMem | Stack, em_grp45), N, | |
73fba5f4 AK |
3747 | }; |
3748 | ||
fd0a0d82 | 3749 | static const struct opcode group6[] = { |
1c2545be TY |
3750 | DI(Prot, sldt), |
3751 | DI(Prot, str), | |
a14e579f | 3752 | II(Prot | Priv | SrcMem16, em_lldt, lldt), |
80890006 | 3753 | II(Prot | Priv | SrcMem16, em_ltr, ltr), |
dee6bb70 JR |
3754 | N, N, N, N, |
3755 | }; | |
3756 | ||
fd0a0d82 | 3757 | static const struct group_dual group7 = { { |
96051572 AK |
3758 | II(Mov | DstMem | Priv, em_sgdt, sgdt), |
3759 | II(Mov | DstMem | Priv, em_sidt, sidt), | |
1c2545be TY |
3760 | II(SrcMem | Priv, em_lgdt, lgdt), |
3761 | II(SrcMem | Priv, em_lidt, lidt), | |
3762 | II(SrcNone | DstMem | Mov, em_smsw, smsw), N, | |
3763 | II(SrcMem16 | Mov | Priv, em_lmsw, lmsw), | |
3764 | II(SrcMem | ByteOp | Priv | NoAccess, em_invlpg, invlpg), | |
73fba5f4 | 3765 | }, { |
1c2545be | 3766 | I(SrcNone | Priv | VendorSpecific, em_vmcall), |
5ef39c71 | 3767 | EXT(0, group7_rm1), |
01de8b09 | 3768 | N, EXT(0, group7_rm3), |
1c2545be TY |
3769 | II(SrcNone | DstMem | Mov, em_smsw, smsw), N, |
3770 | II(SrcMem16 | Mov | Priv, em_lmsw, lmsw), | |
3771 | EXT(0, group7_rm7), | |
73fba5f4 AK |
3772 | } }; |
3773 | ||
fd0a0d82 | 3774 | static const struct opcode group8[] = { |
73fba5f4 | 3775 | N, N, N, N, |
1c2545be TY |
3776 | I(DstMem | SrcImmByte, em_bt), |
3777 | I(DstMem | SrcImmByte | Lock | PageTable, em_bts), | |
3778 | I(DstMem | SrcImmByte | Lock, em_btr), | |
3779 | I(DstMem | SrcImmByte | Lock | PageTable, em_btc), | |
73fba5f4 AK |
3780 | }; |
3781 | ||
fd0a0d82 | 3782 | static const struct group_dual group9 = { { |
1c2545be | 3783 | N, I(DstMem64 | Lock | PageTable, em_cmpxchg8b), N, N, N, N, N, N, |
73fba5f4 AK |
3784 | }, { |
3785 | N, N, N, N, N, N, N, N, | |
3786 | } }; | |
3787 | ||
fd0a0d82 | 3788 | static const struct opcode group11[] = { |
1c2545be | 3789 | I(DstMem | SrcImm | Mov | PageTable, em_mov), |
d5ae7ce8 | 3790 | X7(D(Undefined)), |
a4d4a7c1 AK |
3791 | }; |
3792 | ||
fd0a0d82 | 3793 | static const struct gprefix pfx_0f_6f_0f_7f = { |
e5971755 | 3794 | I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov), |
aa97bb48 AK |
3795 | }; |
3796 | ||
fd0a0d82 | 3797 | static const struct gprefix pfx_vmovntpx = { |
3e114eb4 AK |
3798 | I(0, em_mov), N, N, N, |
3799 | }; | |
3800 | ||
045a282c GN |
3801 | static const struct escape escape_d9 = { { |
3802 | N, N, N, N, N, N, N, I(DstMem, em_fnstcw), | |
3803 | }, { | |
3804 | /* 0xC0 - 0xC7 */ | |
3805 | N, N, N, N, N, N, N, N, | |
3806 | /* 0xC8 - 0xCF */ | |
3807 | N, N, N, N, N, N, N, N, | |
3808 | /* 0xD0 - 0xC7 */ | |
3809 | N, N, N, N, N, N, N, N, | |
3810 | /* 0xD8 - 0xDF */ | |
3811 | N, N, N, N, N, N, N, N, | |
3812 | /* 0xE0 - 0xE7 */ | |
3813 | N, N, N, N, N, N, N, N, | |
3814 | /* 0xE8 - 0xEF */ | |
3815 | N, N, N, N, N, N, N, N, | |
3816 | /* 0xF0 - 0xF7 */ | |
3817 | N, N, N, N, N, N, N, N, | |
3818 | /* 0xF8 - 0xFF */ | |
3819 | N, N, N, N, N, N, N, N, | |
3820 | } }; | |
3821 | ||
3822 | static const struct escape escape_db = { { | |
3823 | N, N, N, N, N, N, N, N, | |
3824 | }, { | |
3825 | /* 0xC0 - 0xC7 */ | |
3826 | N, N, N, N, N, N, N, N, | |
3827 | /* 0xC8 - 0xCF */ | |
3828 | N, N, N, N, N, N, N, N, | |
3829 | /* 0xD0 - 0xC7 */ | |
3830 | N, N, N, N, N, N, N, N, | |
3831 | /* 0xD8 - 0xDF */ | |
3832 | N, N, N, N, N, N, N, N, | |
3833 | /* 0xE0 - 0xE7 */ | |
3834 | N, N, N, I(ImplicitOps, em_fninit), N, N, N, N, | |
3835 | /* 0xE8 - 0xEF */ | |
3836 | N, N, N, N, N, N, N, N, | |
3837 | /* 0xF0 - 0xF7 */ | |
3838 | N, N, N, N, N, N, N, N, | |
3839 | /* 0xF8 - 0xFF */ | |
3840 | N, N, N, N, N, N, N, N, | |
3841 | } }; | |
3842 | ||
3843 | static const struct escape escape_dd = { { | |
3844 | N, N, N, N, N, N, N, I(DstMem, em_fnstsw), | |
3845 | }, { | |
3846 | /* 0xC0 - 0xC7 */ | |
3847 | N, N, N, N, N, N, N, N, | |
3848 | /* 0xC8 - 0xCF */ | |
3849 | N, N, N, N, N, N, N, N, | |
3850 | /* 0xD0 - 0xC7 */ | |
3851 | N, N, N, N, N, N, N, N, | |
3852 | /* 0xD8 - 0xDF */ | |
3853 | N, N, N, N, N, N, N, N, | |
3854 | /* 0xE0 - 0xE7 */ | |
3855 | N, N, N, N, N, N, N, N, | |
3856 | /* 0xE8 - 0xEF */ | |
3857 | N, N, N, N, N, N, N, N, | |
3858 | /* 0xF0 - 0xF7 */ | |
3859 | N, N, N, N, N, N, N, N, | |
3860 | /* 0xF8 - 0xFF */ | |
3861 | N, N, N, N, N, N, N, N, | |
3862 | } }; | |
3863 | ||
fd0a0d82 | 3864 | static const struct opcode opcode_table[256] = { |
73fba5f4 | 3865 | /* 0x00 - 0x07 */ |
fb864fbc | 3866 | F6ALU(Lock, em_add), |
1cd196ea AK |
3867 | I(ImplicitOps | Stack | No64 | Src2ES, em_push_sreg), |
3868 | I(ImplicitOps | Stack | No64 | Src2ES, em_pop_sreg), | |
73fba5f4 | 3869 | /* 0x08 - 0x0F */ |
fb864fbc | 3870 | F6ALU(Lock | PageTable, em_or), |
1cd196ea AK |
3871 | I(ImplicitOps | Stack | No64 | Src2CS, em_push_sreg), |
3872 | N, | |
73fba5f4 | 3873 | /* 0x10 - 0x17 */ |
fb864fbc | 3874 | F6ALU(Lock, em_adc), |
1cd196ea AK |
3875 | I(ImplicitOps | Stack | No64 | Src2SS, em_push_sreg), |
3876 | I(ImplicitOps | Stack | No64 | Src2SS, em_pop_sreg), | |
73fba5f4 | 3877 | /* 0x18 - 0x1F */ |
fb864fbc | 3878 | F6ALU(Lock, em_sbb), |
1cd196ea AK |
3879 | I(ImplicitOps | Stack | No64 | Src2DS, em_push_sreg), |
3880 | I(ImplicitOps | Stack | No64 | Src2DS, em_pop_sreg), | |
73fba5f4 | 3881 | /* 0x20 - 0x27 */ |
fb864fbc | 3882 | F6ALU(Lock | PageTable, em_and), N, N, |
73fba5f4 | 3883 | /* 0x28 - 0x2F */ |
fb864fbc | 3884 | F6ALU(Lock, em_sub), N, I(ByteOp | DstAcc | No64, em_das), |
73fba5f4 | 3885 | /* 0x30 - 0x37 */ |
fb864fbc | 3886 | F6ALU(Lock, em_xor), N, N, |
73fba5f4 | 3887 | /* 0x38 - 0x3F */ |
fb864fbc | 3888 | F6ALU(NoWrite, em_cmp), N, N, |
73fba5f4 | 3889 | /* 0x40 - 0x4F */ |
95413dc4 | 3890 | X8(F(DstReg, em_inc)), X8(F(DstReg, em_dec)), |
73fba5f4 | 3891 | /* 0x50 - 0x57 */ |
63540382 | 3892 | X8(I(SrcReg | Stack, em_push)), |
73fba5f4 | 3893 | /* 0x58 - 0x5F */ |
c54fe504 | 3894 | X8(I(DstReg | Stack, em_pop)), |
73fba5f4 | 3895 | /* 0x60 - 0x67 */ |
b96a7fad TY |
3896 | I(ImplicitOps | Stack | No64, em_pusha), |
3897 | I(ImplicitOps | Stack | No64, em_popa), | |
73fba5f4 AK |
3898 | N, D(DstReg | SrcMem32 | ModRM | Mov) /* movsxd (x86/64) */ , |
3899 | N, N, N, N, | |
3900 | /* 0x68 - 0x6F */ | |
d46164db AK |
3901 | I(SrcImm | Mov | Stack, em_push), |
3902 | I(DstReg | SrcMem | ModRM | Src2Imm, em_imul_3op), | |
f3a1b9f4 AK |
3903 | I(SrcImmByte | Mov | Stack, em_push), |
3904 | I(DstReg | SrcMem | ModRM | Src2ImmByte, em_imul_3op), | |
b3356bf0 | 3905 | I2bvIP(DstDI | SrcDX | Mov | String | Unaligned, em_in, ins, check_perm_in), /* insb, insw/insd */ |
2b5e97e1 | 3906 | I2bvIP(SrcSI | DstDX | String, em_out, outs, check_perm_out), /* outsb, outsw/outsd */ |
73fba5f4 AK |
3907 | /* 0x70 - 0x7F */ |
3908 | X16(D(SrcImmByte)), | |
3909 | /* 0x80 - 0x87 */ | |
1c2545be TY |
3910 | G(ByteOp | DstMem | SrcImm, group1), |
3911 | G(DstMem | SrcImm, group1), | |
3912 | G(ByteOp | DstMem | SrcImm | No64, group1), | |
3913 | G(DstMem | SrcImmByte, group1), | |
fb864fbc | 3914 | F2bv(DstMem | SrcReg | ModRM | NoWrite, em_test), |
d5ae7ce8 | 3915 | I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_xchg), |
73fba5f4 | 3916 | /* 0x88 - 0x8F */ |
d5ae7ce8 | 3917 | I2bv(DstMem | SrcReg | ModRM | Mov | PageTable, em_mov), |
b9eac5f4 | 3918 | I2bv(DstReg | SrcMem | ModRM | Mov, em_mov), |
d5ae7ce8 | 3919 | I(DstMem | SrcNone | ModRM | Mov | PageTable, em_mov_rm_sreg), |
1bd5f469 TY |
3920 | D(ModRM | SrcMem | NoAccess | DstReg), |
3921 | I(ImplicitOps | SrcMem16 | ModRM, em_mov_sreg_rm), | |
3922 | G(0, group1A), | |
73fba5f4 | 3923 | /* 0x90 - 0x97 */ |
bf608f88 | 3924 | DI(SrcAcc | DstReg, pause), X7(D(SrcAcc | DstReg)), |
73fba5f4 | 3925 | /* 0x98 - 0x9F */ |
61429142 | 3926 | D(DstAcc | SrcNone), I(ImplicitOps | SrcAcc, em_cwd), |
cc4feed5 | 3927 | I(SrcImmFAddr | No64, em_call_far), N, |
62aaa2f0 | 3928 | II(ImplicitOps | Stack, em_pushf, pushf), |
2dd7caa0 | 3929 | II(ImplicitOps | Stack, em_popf, popf), N, I(ImplicitOps, em_lahf), |
73fba5f4 | 3930 | /* 0xA0 - 0xA7 */ |
b9eac5f4 | 3931 | I2bv(DstAcc | SrcMem | Mov | MemAbs, em_mov), |
d5ae7ce8 | 3932 | I2bv(DstMem | SrcAcc | Mov | MemAbs | PageTable, em_mov), |
b9eac5f4 | 3933 | I2bv(SrcSI | DstDI | Mov | String, em_mov), |
fb864fbc | 3934 | F2bv(SrcSI | DstDI | String | NoWrite, em_cmp), |
73fba5f4 | 3935 | /* 0xA8 - 0xAF */ |
fb864fbc | 3936 | F2bv(DstAcc | SrcImm | NoWrite, em_test), |
b9eac5f4 AK |
3937 | I2bv(SrcAcc | DstDI | Mov | String, em_mov), |
3938 | I2bv(SrcSI | DstAcc | Mov | String, em_mov), | |
fb864fbc | 3939 | F2bv(SrcAcc | DstDI | String | NoWrite, em_cmp), |
73fba5f4 | 3940 | /* 0xB0 - 0xB7 */ |
b9eac5f4 | 3941 | X8(I(ByteOp | DstReg | SrcImm | Mov, em_mov)), |
73fba5f4 | 3942 | /* 0xB8 - 0xBF */ |
5e2c6883 | 3943 | X8(I(DstReg | SrcImm64 | Mov, em_mov)), |
73fba5f4 | 3944 | /* 0xC0 - 0xC7 */ |
007a3b54 | 3945 | G(ByteOp | Src2ImmByte, group2), G(Src2ImmByte, group2), |
40ece7c7 | 3946 | I(ImplicitOps | Stack | SrcImmU16, em_ret_near_imm), |
ebda02c2 | 3947 | I(ImplicitOps | Stack, em_ret), |
d4b4325f AK |
3948 | I(DstReg | SrcMemFAddr | ModRM | No64 | Src2ES, em_lseg), |
3949 | I(DstReg | SrcMemFAddr | ModRM | No64 | Src2DS, em_lseg), | |
a4d4a7c1 | 3950 | G(ByteOp, group11), G(0, group11), |
73fba5f4 | 3951 | /* 0xC8 - 0xCF */ |
612e89f0 AK |
3952 | I(Stack | SrcImmU16 | Src2ImmByte, em_enter), I(Stack, em_leave), |
3953 | N, I(ImplicitOps | Stack, em_ret_far), | |
3c6e276f | 3954 | D(ImplicitOps), DI(SrcImmByte, intn), |
db5b0762 | 3955 | D(ImplicitOps | No64), II(ImplicitOps, em_iret, iret), |
73fba5f4 | 3956 | /* 0xD0 - 0xD7 */ |
007a3b54 AK |
3957 | G(Src2One | ByteOp, group2), G(Src2One, group2), |
3958 | G(Src2CL | ByteOp, group2), G(Src2CL, group2), | |
7f662273 | 3959 | N, I(DstAcc | SrcImmByte | No64, em_aad), N, N, |
73fba5f4 | 3960 | /* 0xD8 - 0xDF */ |
045a282c | 3961 | N, E(0, &escape_d9), N, E(0, &escape_db), N, E(0, &escape_dd), N, N, |
73fba5f4 | 3962 | /* 0xE0 - 0xE7 */ |
d06e03ad TY |
3963 | X3(I(SrcImmByte, em_loop)), |
3964 | I(SrcImmByte, em_jcxz), | |
d7841a4b TY |
3965 | I2bvIP(SrcImmUByte | DstAcc, em_in, in, check_perm_in), |
3966 | I2bvIP(SrcAcc | DstImmUByte, em_out, out, check_perm_out), | |
73fba5f4 | 3967 | /* 0xE8 - 0xEF */ |
d4ddafcd | 3968 | I(SrcImm | Stack, em_call), D(SrcImm | ImplicitOps), |
db5b0762 | 3969 | I(SrcImmFAddr | No64, em_jmp_far), D(SrcImmByte | ImplicitOps), |
d7841a4b TY |
3970 | I2bvIP(SrcDX | DstAcc, em_in, in, check_perm_in), |
3971 | I2bvIP(SrcAcc | DstDX, em_out, out, check_perm_out), | |
73fba5f4 | 3972 | /* 0xF0 - 0xF7 */ |
bf608f88 | 3973 | N, DI(ImplicitOps, icebp), N, N, |
3c6e276f AK |
3974 | DI(ImplicitOps | Priv, hlt), D(ImplicitOps), |
3975 | G(ByteOp, group3), G(0, group3), | |
73fba5f4 | 3976 | /* 0xF8 - 0xFF */ |
f411e6cd TY |
3977 | D(ImplicitOps), D(ImplicitOps), |
3978 | I(ImplicitOps, em_cli), I(ImplicitOps, em_sti), | |
73fba5f4 AK |
3979 | D(ImplicitOps), D(ImplicitOps), G(0, group4), G(0, group5), |
3980 | }; | |
3981 | ||
fd0a0d82 | 3982 | static const struct opcode twobyte_table[256] = { |
73fba5f4 | 3983 | /* 0x00 - 0x0F */ |
dee6bb70 | 3984 | G(0, group6), GD(0, &group7), N, N, |
db5b0762 TY |
3985 | N, I(ImplicitOps | VendorSpecific, em_syscall), |
3986 | II(ImplicitOps | Priv, em_clts, clts), N, | |
3c6e276f | 3987 | DI(ImplicitOps | Priv, invd), DI(ImplicitOps | Priv, wbinvd), N, N, |
73fba5f4 AK |
3988 | N, D(ImplicitOps | ModRM), N, N, |
3989 | /* 0x10 - 0x1F */ | |
3990 | N, N, N, N, N, N, N, N, D(ImplicitOps | ModRM), N, N, N, N, N, N, N, | |
3991 | /* 0x20 - 0x2F */ | |
cfec82cb | 3992 | DIP(ModRM | DstMem | Priv | Op3264, cr_read, check_cr_read), |
3b88e41a | 3993 | DIP(ModRM | DstMem | Priv | Op3264, dr_read, check_dr_read), |
bc00f8d2 TY |
3994 | IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write), |
3995 | IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write), | |
73fba5f4 | 3996 | N, N, N, N, |
3e114eb4 AK |
3997 | N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx), |
3998 | N, N, N, N, | |
73fba5f4 | 3999 | /* 0x30 - 0x3F */ |
e1e210b0 | 4000 | II(ImplicitOps | Priv, em_wrmsr, wrmsr), |
8061252e | 4001 | IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc), |
e1e210b0 | 4002 | II(ImplicitOps | Priv, em_rdmsr, rdmsr), |
222d21aa | 4003 | IIP(ImplicitOps, em_rdpmc, rdpmc, check_rdpmc), |
db5b0762 TY |
4004 | I(ImplicitOps | VendorSpecific, em_sysenter), |
4005 | I(ImplicitOps | Priv | VendorSpecific, em_sysexit), | |
d867162c | 4006 | N, N, |
73fba5f4 AK |
4007 | N, N, N, N, N, N, N, N, |
4008 | /* 0x40 - 0x4F */ | |
4009 | X16(D(DstReg | SrcMem | ModRM | Mov)), | |
4010 | /* 0x50 - 0x5F */ | |
4011 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, | |
4012 | /* 0x60 - 0x6F */ | |
aa97bb48 AK |
4013 | N, N, N, N, |
4014 | N, N, N, N, | |
4015 | N, N, N, N, | |
4016 | N, N, N, GP(SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f), | |
73fba5f4 | 4017 | /* 0x70 - 0x7F */ |
aa97bb48 AK |
4018 | N, N, N, N, |
4019 | N, N, N, N, | |
4020 | N, N, N, N, | |
4021 | N, N, N, GP(SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f), | |
73fba5f4 AK |
4022 | /* 0x80 - 0x8F */ |
4023 | X16(D(SrcImm)), | |
4024 | /* 0x90 - 0x9F */ | |
ee45b58e | 4025 | X16(D(ByteOp | DstMem | SrcNone | ModRM| Mov)), |
73fba5f4 | 4026 | /* 0xA0 - 0xA7 */ |
1cd196ea | 4027 | I(Stack | Src2FS, em_push_sreg), I(Stack | Src2FS, em_pop_sreg), |
6d6eede4 | 4028 | II(ImplicitOps, em_cpuid, cpuid), I(DstMem | SrcReg | ModRM | BitOp, em_bt), |
0bdea068 AK |
4029 | F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shld), |
4030 | F(DstMem | SrcReg | Src2CL | ModRM, em_shld), N, N, | |
73fba5f4 | 4031 | /* 0xA8 - 0xAF */ |
1cd196ea | 4032 | I(Stack | Src2GS, em_push_sreg), I(Stack | Src2GS, em_pop_sreg), |
d5ae7ce8 | 4033 | DI(ImplicitOps, rsm), |
ce7faab2 | 4034 | I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_bts), |
0bdea068 AK |
4035 | F(DstMem | SrcReg | Src2ImmByte | ModRM, em_shrd), |
4036 | F(DstMem | SrcReg | Src2CL | ModRM, em_shrd), | |
5c82aa29 | 4037 | D(ModRM), I(DstReg | SrcMem | ModRM, em_imul), |
73fba5f4 | 4038 | /* 0xB0 - 0xB7 */ |
e940b5c2 | 4039 | I2bv(DstMem | SrcReg | ModRM | Lock | PageTable, em_cmpxchg), |
d4b4325f | 4040 | I(DstReg | SrcMemFAddr | ModRM | Src2SS, em_lseg), |
ce7faab2 | 4041 | I(DstMem | SrcReg | ModRM | BitOp | Lock, em_btr), |
d4b4325f AK |
4042 | I(DstReg | SrcMemFAddr | ModRM | Src2FS, em_lseg), |
4043 | I(DstReg | SrcMemFAddr | ModRM | Src2GS, em_lseg), | |
2adb5ad9 | 4044 | D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
73fba5f4 AK |
4045 | /* 0xB8 - 0xBF */ |
4046 | N, N, | |
ce7faab2 TY |
4047 | G(BitOp, group8), |
4048 | I(DstMem | SrcReg | ModRM | BitOp | Lock | PageTable, em_btc), | |
ff227392 | 4049 | I(DstReg | SrcMem | ModRM, em_bsf), I(DstReg | SrcMem | ModRM, em_bsr), |
2adb5ad9 | 4050 | D(DstReg | SrcMem8 | ModRM | Mov), D(DstReg | SrcMem16 | ModRM | Mov), |
9299836e | 4051 | /* 0xC0 - 0xC7 */ |
739ae406 | 4052 | D2bv(DstMem | SrcReg | ModRM | Lock), |
92f738a5 | 4053 | N, D(DstMem | SrcReg | ModRM | Mov), |
73fba5f4 | 4054 | N, N, N, GD(0, &group9), |
9299836e AK |
4055 | /* 0xC8 - 0xCF */ |
4056 | X8(I(DstReg, em_bswap)), | |
73fba5f4 AK |
4057 | /* 0xD0 - 0xDF */ |
4058 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, | |
4059 | /* 0xE0 - 0xEF */ | |
4060 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, | |
4061 | /* 0xF0 - 0xFF */ | |
4062 | N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N | |
4063 | }; | |
4064 | ||
4065 | #undef D | |
4066 | #undef N | |
4067 | #undef G | |
4068 | #undef GD | |
4069 | #undef I | |
aa97bb48 | 4070 | #undef GP |
01de8b09 | 4071 | #undef EXT |
73fba5f4 | 4072 | |
8d8f4e9f | 4073 | #undef D2bv |
f6511935 | 4074 | #undef D2bvIP |
8d8f4e9f | 4075 | #undef I2bv |
d7841a4b | 4076 | #undef I2bvIP |
d67fc27a | 4077 | #undef I6ALU |
8d8f4e9f | 4078 | |
9dac77fa | 4079 | static unsigned imm_size(struct x86_emulate_ctxt *ctxt) |
39f21ee5 AK |
4080 | { |
4081 | unsigned size; | |
4082 | ||
9dac77fa | 4083 | size = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
39f21ee5 AK |
4084 | if (size == 8) |
4085 | size = 4; | |
4086 | return size; | |
4087 | } | |
4088 | ||
4089 | static int decode_imm(struct x86_emulate_ctxt *ctxt, struct operand *op, | |
4090 | unsigned size, bool sign_extension) | |
4091 | { | |
39f21ee5 AK |
4092 | int rc = X86EMUL_CONTINUE; |
4093 | ||
4094 | op->type = OP_IMM; | |
4095 | op->bytes = size; | |
9dac77fa | 4096 | op->addr.mem.ea = ctxt->_eip; |
39f21ee5 AK |
4097 | /* NB. Immediates are sign-extended as necessary. */ |
4098 | switch (op->bytes) { | |
4099 | case 1: | |
e85a1085 | 4100 | op->val = insn_fetch(s8, ctxt); |
39f21ee5 AK |
4101 | break; |
4102 | case 2: | |
e85a1085 | 4103 | op->val = insn_fetch(s16, ctxt); |
39f21ee5 AK |
4104 | break; |
4105 | case 4: | |
e85a1085 | 4106 | op->val = insn_fetch(s32, ctxt); |
39f21ee5 | 4107 | break; |
5e2c6883 NA |
4108 | case 8: |
4109 | op->val = insn_fetch(s64, ctxt); | |
4110 | break; | |
39f21ee5 AK |
4111 | } |
4112 | if (!sign_extension) { | |
4113 | switch (op->bytes) { | |
4114 | case 1: | |
4115 | op->val &= 0xff; | |
4116 | break; | |
4117 | case 2: | |
4118 | op->val &= 0xffff; | |
4119 | break; | |
4120 | case 4: | |
4121 | op->val &= 0xffffffff; | |
4122 | break; | |
4123 | } | |
4124 | } | |
4125 | done: | |
4126 | return rc; | |
4127 | } | |
4128 | ||
a9945549 AK |
4129 | static int decode_operand(struct x86_emulate_ctxt *ctxt, struct operand *op, |
4130 | unsigned d) | |
4131 | { | |
4132 | int rc = X86EMUL_CONTINUE; | |
4133 | ||
4134 | switch (d) { | |
4135 | case OpReg: | |
2adb5ad9 | 4136 | decode_register_operand(ctxt, op); |
a9945549 AK |
4137 | break; |
4138 | case OpImmUByte: | |
608aabe3 | 4139 | rc = decode_imm(ctxt, op, 1, false); |
a9945549 AK |
4140 | break; |
4141 | case OpMem: | |
41ddf978 | 4142 | ctxt->memop.bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; |
0fe59128 AK |
4143 | mem_common: |
4144 | *op = ctxt->memop; | |
4145 | ctxt->memopp = op; | |
4146 | if ((ctxt->d & BitOp) && op == &ctxt->dst) | |
a9945549 AK |
4147 | fetch_bit_operand(ctxt); |
4148 | op->orig_val = op->val; | |
4149 | break; | |
41ddf978 AK |
4150 | case OpMem64: |
4151 | ctxt->memop.bytes = 8; | |
4152 | goto mem_common; | |
a9945549 AK |
4153 | case OpAcc: |
4154 | op->type = OP_REG; | |
4155 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; | |
dd856efa | 4156 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RAX); |
a9945549 AK |
4157 | fetch_register_operand(op); |
4158 | op->orig_val = op->val; | |
4159 | break; | |
4160 | case OpDI: | |
4161 | op->type = OP_MEM; | |
4162 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; | |
4163 | op->addr.mem.ea = | |
dd856efa | 4164 | register_address(ctxt, reg_read(ctxt, VCPU_REGS_RDI)); |
a9945549 AK |
4165 | op->addr.mem.seg = VCPU_SREG_ES; |
4166 | op->val = 0; | |
b3356bf0 | 4167 | op->count = 1; |
a9945549 AK |
4168 | break; |
4169 | case OpDX: | |
4170 | op->type = OP_REG; | |
4171 | op->bytes = 2; | |
dd856efa | 4172 | op->addr.reg = reg_rmw(ctxt, VCPU_REGS_RDX); |
a9945549 AK |
4173 | fetch_register_operand(op); |
4174 | break; | |
4dd6a57d AK |
4175 | case OpCL: |
4176 | op->bytes = 1; | |
dd856efa | 4177 | op->val = reg_read(ctxt, VCPU_REGS_RCX) & 0xff; |
4dd6a57d AK |
4178 | break; |
4179 | case OpImmByte: | |
4180 | rc = decode_imm(ctxt, op, 1, true); | |
4181 | break; | |
4182 | case OpOne: | |
4183 | op->bytes = 1; | |
4184 | op->val = 1; | |
4185 | break; | |
4186 | case OpImm: | |
4187 | rc = decode_imm(ctxt, op, imm_size(ctxt), true); | |
4188 | break; | |
5e2c6883 NA |
4189 | case OpImm64: |
4190 | rc = decode_imm(ctxt, op, ctxt->op_bytes, true); | |
4191 | break; | |
28867cee AK |
4192 | case OpMem8: |
4193 | ctxt->memop.bytes = 1; | |
4194 | goto mem_common; | |
0fe59128 AK |
4195 | case OpMem16: |
4196 | ctxt->memop.bytes = 2; | |
4197 | goto mem_common; | |
4198 | case OpMem32: | |
4199 | ctxt->memop.bytes = 4; | |
4200 | goto mem_common; | |
4201 | case OpImmU16: | |
4202 | rc = decode_imm(ctxt, op, 2, false); | |
4203 | break; | |
4204 | case OpImmU: | |
4205 | rc = decode_imm(ctxt, op, imm_size(ctxt), false); | |
4206 | break; | |
4207 | case OpSI: | |
4208 | op->type = OP_MEM; | |
4209 | op->bytes = (ctxt->d & ByteOp) ? 1 : ctxt->op_bytes; | |
4210 | op->addr.mem.ea = | |
dd856efa | 4211 | register_address(ctxt, reg_read(ctxt, VCPU_REGS_RSI)); |
0fe59128 AK |
4212 | op->addr.mem.seg = seg_override(ctxt); |
4213 | op->val = 0; | |
b3356bf0 | 4214 | op->count = 1; |
0fe59128 AK |
4215 | break; |
4216 | case OpImmFAddr: | |
4217 | op->type = OP_IMM; | |
4218 | op->addr.mem.ea = ctxt->_eip; | |
4219 | op->bytes = ctxt->op_bytes + 2; | |
4220 | insn_fetch_arr(op->valptr, op->bytes, ctxt); | |
4221 | break; | |
4222 | case OpMemFAddr: | |
4223 | ctxt->memop.bytes = ctxt->op_bytes + 2; | |
4224 | goto mem_common; | |
c191a7a0 AK |
4225 | case OpES: |
4226 | op->val = VCPU_SREG_ES; | |
4227 | break; | |
4228 | case OpCS: | |
4229 | op->val = VCPU_SREG_CS; | |
4230 | break; | |
4231 | case OpSS: | |
4232 | op->val = VCPU_SREG_SS; | |
4233 | break; | |
4234 | case OpDS: | |
4235 | op->val = VCPU_SREG_DS; | |
4236 | break; | |
4237 | case OpFS: | |
4238 | op->val = VCPU_SREG_FS; | |
4239 | break; | |
4240 | case OpGS: | |
4241 | op->val = VCPU_SREG_GS; | |
4242 | break; | |
a9945549 AK |
4243 | case OpImplicit: |
4244 | /* Special instructions do their own operand decoding. */ | |
4245 | default: | |
4246 | op->type = OP_NONE; /* Disable writeback. */ | |
4247 | break; | |
4248 | } | |
4249 | ||
4250 | done: | |
4251 | return rc; | |
4252 | } | |
4253 | ||
ef5d75cc | 4254 | int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) |
dde7e6d1 | 4255 | { |
dde7e6d1 AK |
4256 | int rc = X86EMUL_CONTINUE; |
4257 | int mode = ctxt->mode; | |
46561646 | 4258 | int def_op_bytes, def_ad_bytes, goffset, simd_prefix; |
0d7cdee8 | 4259 | bool op_prefix = false; |
46561646 | 4260 | struct opcode opcode; |
dde7e6d1 | 4261 | |
f09ed83e AK |
4262 | ctxt->memop.type = OP_NONE; |
4263 | ctxt->memopp = NULL; | |
9dac77fa AK |
4264 | ctxt->_eip = ctxt->eip; |
4265 | ctxt->fetch.start = ctxt->_eip; | |
4266 | ctxt->fetch.end = ctxt->fetch.start + insn_len; | |
dc25e89e | 4267 | if (insn_len > 0) |
9dac77fa | 4268 | memcpy(ctxt->fetch.data, insn, insn_len); |
dde7e6d1 AK |
4269 | |
4270 | switch (mode) { | |
4271 | case X86EMUL_MODE_REAL: | |
4272 | case X86EMUL_MODE_VM86: | |
4273 | case X86EMUL_MODE_PROT16: | |
4274 | def_op_bytes = def_ad_bytes = 2; | |
4275 | break; | |
4276 | case X86EMUL_MODE_PROT32: | |
4277 | def_op_bytes = def_ad_bytes = 4; | |
4278 | break; | |
4279 | #ifdef CONFIG_X86_64 | |
4280 | case X86EMUL_MODE_PROT64: | |
4281 | def_op_bytes = 4; | |
4282 | def_ad_bytes = 8; | |
4283 | break; | |
4284 | #endif | |
4285 | default: | |
1d2887e2 | 4286 | return EMULATION_FAILED; |
dde7e6d1 AK |
4287 | } |
4288 | ||
9dac77fa AK |
4289 | ctxt->op_bytes = def_op_bytes; |
4290 | ctxt->ad_bytes = def_ad_bytes; | |
dde7e6d1 AK |
4291 | |
4292 | /* Legacy prefixes. */ | |
4293 | for (;;) { | |
e85a1085 | 4294 | switch (ctxt->b = insn_fetch(u8, ctxt)) { |
dde7e6d1 | 4295 | case 0x66: /* operand-size override */ |
0d7cdee8 | 4296 | op_prefix = true; |
dde7e6d1 | 4297 | /* switch between 2/4 bytes */ |
9dac77fa | 4298 | ctxt->op_bytes = def_op_bytes ^ 6; |
dde7e6d1 AK |
4299 | break; |
4300 | case 0x67: /* address-size override */ | |
4301 | if (mode == X86EMUL_MODE_PROT64) | |
4302 | /* switch between 4/8 bytes */ | |
9dac77fa | 4303 | ctxt->ad_bytes = def_ad_bytes ^ 12; |
dde7e6d1 AK |
4304 | else |
4305 | /* switch between 2/4 bytes */ | |
9dac77fa | 4306 | ctxt->ad_bytes = def_ad_bytes ^ 6; |
dde7e6d1 AK |
4307 | break; |
4308 | case 0x26: /* ES override */ | |
4309 | case 0x2e: /* CS override */ | |
4310 | case 0x36: /* SS override */ | |
4311 | case 0x3e: /* DS override */ | |
9dac77fa | 4312 | set_seg_override(ctxt, (ctxt->b >> 3) & 3); |
dde7e6d1 AK |
4313 | break; |
4314 | case 0x64: /* FS override */ | |
4315 | case 0x65: /* GS override */ | |
9dac77fa | 4316 | set_seg_override(ctxt, ctxt->b & 7); |
dde7e6d1 AK |
4317 | break; |
4318 | case 0x40 ... 0x4f: /* REX */ | |
4319 | if (mode != X86EMUL_MODE_PROT64) | |
4320 | goto done_prefixes; | |
9dac77fa | 4321 | ctxt->rex_prefix = ctxt->b; |
dde7e6d1 AK |
4322 | continue; |
4323 | case 0xf0: /* LOCK */ | |
9dac77fa | 4324 | ctxt->lock_prefix = 1; |
dde7e6d1 AK |
4325 | break; |
4326 | case 0xf2: /* REPNE/REPNZ */ | |
dde7e6d1 | 4327 | case 0xf3: /* REP/REPE/REPZ */ |
9dac77fa | 4328 | ctxt->rep_prefix = ctxt->b; |
dde7e6d1 AK |
4329 | break; |
4330 | default: | |
4331 | goto done_prefixes; | |
4332 | } | |
4333 | ||
4334 | /* Any legacy prefix after a REX prefix nullifies its effect. */ | |
4335 | ||
9dac77fa | 4336 | ctxt->rex_prefix = 0; |
dde7e6d1 AK |
4337 | } |
4338 | ||
4339 | done_prefixes: | |
4340 | ||
4341 | /* REX prefix. */ | |
9dac77fa AK |
4342 | if (ctxt->rex_prefix & 8) |
4343 | ctxt->op_bytes = 8; /* REX.W */ | |
dde7e6d1 AK |
4344 | |
4345 | /* Opcode byte(s). */ | |
9dac77fa | 4346 | opcode = opcode_table[ctxt->b]; |
d3ad6243 | 4347 | /* Two-byte opcode? */ |
9dac77fa AK |
4348 | if (ctxt->b == 0x0f) { |
4349 | ctxt->twobyte = 1; | |
e85a1085 | 4350 | ctxt->b = insn_fetch(u8, ctxt); |
9dac77fa | 4351 | opcode = twobyte_table[ctxt->b]; |
dde7e6d1 | 4352 | } |
9dac77fa | 4353 | ctxt->d = opcode.flags; |
dde7e6d1 | 4354 | |
9f4260e7 TY |
4355 | if (ctxt->d & ModRM) |
4356 | ctxt->modrm = insn_fetch(u8, ctxt); | |
4357 | ||
9dac77fa AK |
4358 | while (ctxt->d & GroupMask) { |
4359 | switch (ctxt->d & GroupMask) { | |
46561646 | 4360 | case Group: |
9dac77fa | 4361 | goffset = (ctxt->modrm >> 3) & 7; |
46561646 AK |
4362 | opcode = opcode.u.group[goffset]; |
4363 | break; | |
4364 | case GroupDual: | |
9dac77fa AK |
4365 | goffset = (ctxt->modrm >> 3) & 7; |
4366 | if ((ctxt->modrm >> 6) == 3) | |
46561646 AK |
4367 | opcode = opcode.u.gdual->mod3[goffset]; |
4368 | else | |
4369 | opcode = opcode.u.gdual->mod012[goffset]; | |
4370 | break; | |
4371 | case RMExt: | |
9dac77fa | 4372 | goffset = ctxt->modrm & 7; |
01de8b09 | 4373 | opcode = opcode.u.group[goffset]; |
46561646 AK |
4374 | break; |
4375 | case Prefix: | |
9dac77fa | 4376 | if (ctxt->rep_prefix && op_prefix) |
1d2887e2 | 4377 | return EMULATION_FAILED; |
9dac77fa | 4378 | simd_prefix = op_prefix ? 0x66 : ctxt->rep_prefix; |
46561646 AK |
4379 | switch (simd_prefix) { |
4380 | case 0x00: opcode = opcode.u.gprefix->pfx_no; break; | |
4381 | case 0x66: opcode = opcode.u.gprefix->pfx_66; break; | |
4382 | case 0xf2: opcode = opcode.u.gprefix->pfx_f2; break; | |
4383 | case 0xf3: opcode = opcode.u.gprefix->pfx_f3; break; | |
4384 | } | |
4385 | break; | |
045a282c GN |
4386 | case Escape: |
4387 | if (ctxt->modrm > 0xbf) | |
4388 | opcode = opcode.u.esc->high[ctxt->modrm - 0xc0]; | |
4389 | else | |
4390 | opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7]; | |
4391 | break; | |
46561646 | 4392 | default: |
1d2887e2 | 4393 | return EMULATION_FAILED; |
0d7cdee8 | 4394 | } |
46561646 | 4395 | |
b1ea50b2 | 4396 | ctxt->d &= ~(u64)GroupMask; |
9dac77fa | 4397 | ctxt->d |= opcode.flags; |
0d7cdee8 AK |
4398 | } |
4399 | ||
9dac77fa AK |
4400 | ctxt->execute = opcode.u.execute; |
4401 | ctxt->check_perm = opcode.check_perm; | |
4402 | ctxt->intercept = opcode.intercept; | |
dde7e6d1 AK |
4403 | |
4404 | /* Unrecognised? */ | |
9dac77fa | 4405 | if (ctxt->d == 0 || (ctxt->d & Undefined)) |
1d2887e2 | 4406 | return EMULATION_FAILED; |
dde7e6d1 | 4407 | |
9dac77fa | 4408 | if (!(ctxt->d & VendorSpecific) && ctxt->only_vendor_specific_insn) |
1d2887e2 | 4409 | return EMULATION_FAILED; |
d867162c | 4410 | |
9dac77fa AK |
4411 | if (mode == X86EMUL_MODE_PROT64 && (ctxt->d & Stack)) |
4412 | ctxt->op_bytes = 8; | |
dde7e6d1 | 4413 | |
9dac77fa | 4414 | if (ctxt->d & Op3264) { |
7f9b4b75 | 4415 | if (mode == X86EMUL_MODE_PROT64) |
9dac77fa | 4416 | ctxt->op_bytes = 8; |
7f9b4b75 | 4417 | else |
9dac77fa | 4418 | ctxt->op_bytes = 4; |
7f9b4b75 AK |
4419 | } |
4420 | ||
9dac77fa AK |
4421 | if (ctxt->d & Sse) |
4422 | ctxt->op_bytes = 16; | |
cbe2c9d3 AK |
4423 | else if (ctxt->d & Mmx) |
4424 | ctxt->op_bytes = 8; | |
1253791d | 4425 | |
dde7e6d1 | 4426 | /* ModRM and SIB bytes. */ |
9dac77fa | 4427 | if (ctxt->d & ModRM) { |
f09ed83e | 4428 | rc = decode_modrm(ctxt, &ctxt->memop); |
9dac77fa AK |
4429 | if (!ctxt->has_seg_override) |
4430 | set_seg_override(ctxt, ctxt->modrm_seg); | |
4431 | } else if (ctxt->d & MemAbs) | |
f09ed83e | 4432 | rc = decode_abs(ctxt, &ctxt->memop); |
dde7e6d1 AK |
4433 | if (rc != X86EMUL_CONTINUE) |
4434 | goto done; | |
4435 | ||
9dac77fa AK |
4436 | if (!ctxt->has_seg_override) |
4437 | set_seg_override(ctxt, VCPU_SREG_DS); | |
dde7e6d1 | 4438 | |
f09ed83e | 4439 | ctxt->memop.addr.mem.seg = seg_override(ctxt); |
dde7e6d1 | 4440 | |
f09ed83e AK |
4441 | if (ctxt->memop.type == OP_MEM && ctxt->ad_bytes != 8) |
4442 | ctxt->memop.addr.mem.ea = (u32)ctxt->memop.addr.mem.ea; | |
dde7e6d1 | 4443 | |
dde7e6d1 AK |
4444 | /* |
4445 | * Decode and fetch the source operand: register, memory | |
4446 | * or immediate. | |
4447 | */ | |
0fe59128 | 4448 | rc = decode_operand(ctxt, &ctxt->src, (ctxt->d >> SrcShift) & OpMask); |
39f21ee5 AK |
4449 | if (rc != X86EMUL_CONTINUE) |
4450 | goto done; | |
4451 | ||
dde7e6d1 AK |
4452 | /* |
4453 | * Decode and fetch the second source operand: register, memory | |
4454 | * or immediate. | |
4455 | */ | |
4dd6a57d | 4456 | rc = decode_operand(ctxt, &ctxt->src2, (ctxt->d >> Src2Shift) & OpMask); |
39f21ee5 AK |
4457 | if (rc != X86EMUL_CONTINUE) |
4458 | goto done; | |
4459 | ||
dde7e6d1 | 4460 | /* Decode and fetch the destination operand: register or memory. */ |
a9945549 | 4461 | rc = decode_operand(ctxt, &ctxt->dst, (ctxt->d >> DstShift) & OpMask); |
dde7e6d1 AK |
4462 | |
4463 | done: | |
f09ed83e AK |
4464 | if (ctxt->memopp && ctxt->memopp->type == OP_MEM && ctxt->rip_relative) |
4465 | ctxt->memopp->addr.mem.ea += ctxt->_eip; | |
cb16c348 | 4466 | |
1d2887e2 | 4467 | return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK; |
dde7e6d1 AK |
4468 | } |
4469 | ||
1cb3f3ae XG |
4470 | bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt) |
4471 | { | |
4472 | return ctxt->d & PageTable; | |
4473 | } | |
4474 | ||
3e2f65d5 GN |
4475 | static bool string_insn_completed(struct x86_emulate_ctxt *ctxt) |
4476 | { | |
3e2f65d5 GN |
4477 | /* The second termination condition only applies for REPE |
4478 | * and REPNE. Test if the repeat string operation prefix is | |
4479 | * REPE/REPZ or REPNE/REPNZ and if it's the case it tests the | |
4480 | * corresponding termination condition according to: | |
4481 | * - if REPE/REPZ and ZF = 0 then done | |
4482 | * - if REPNE/REPNZ and ZF = 1 then done | |
4483 | */ | |
9dac77fa AK |
4484 | if (((ctxt->b == 0xa6) || (ctxt->b == 0xa7) || |
4485 | (ctxt->b == 0xae) || (ctxt->b == 0xaf)) | |
4486 | && (((ctxt->rep_prefix == REPE_PREFIX) && | |
3e2f65d5 | 4487 | ((ctxt->eflags & EFLG_ZF) == 0)) |
9dac77fa | 4488 | || ((ctxt->rep_prefix == REPNE_PREFIX) && |
3e2f65d5 GN |
4489 | ((ctxt->eflags & EFLG_ZF) == EFLG_ZF)))) |
4490 | return true; | |
4491 | ||
4492 | return false; | |
4493 | } | |
4494 | ||
cbe2c9d3 AK |
4495 | static int flush_pending_x87_faults(struct x86_emulate_ctxt *ctxt) |
4496 | { | |
4497 | bool fault = false; | |
4498 | ||
4499 | ctxt->ops->get_fpu(ctxt); | |
4500 | asm volatile("1: fwait \n\t" | |
4501 | "2: \n\t" | |
4502 | ".pushsection .fixup,\"ax\" \n\t" | |
4503 | "3: \n\t" | |
4504 | "movb $1, %[fault] \n\t" | |
4505 | "jmp 2b \n\t" | |
4506 | ".popsection \n\t" | |
4507 | _ASM_EXTABLE(1b, 3b) | |
38e8a2dd | 4508 | : [fault]"+qm"(fault)); |
cbe2c9d3 AK |
4509 | ctxt->ops->put_fpu(ctxt); |
4510 | ||
4511 | if (unlikely(fault)) | |
4512 | return emulate_exception(ctxt, MF_VECTOR, 0, false); | |
4513 | ||
4514 | return X86EMUL_CONTINUE; | |
4515 | } | |
4516 | ||
4517 | static void fetch_possible_mmx_operand(struct x86_emulate_ctxt *ctxt, | |
4518 | struct operand *op) | |
4519 | { | |
4520 | if (op->type == OP_MM) | |
4521 | read_mmx_reg(ctxt, &op->mm_val, op->addr.mm); | |
4522 | } | |
4523 | ||
e28bbd44 AK |
4524 | static int fastop(struct x86_emulate_ctxt *ctxt, void (*fop)(struct fastop *)) |
4525 | { | |
4526 | ulong flags = (ctxt->eflags & EFLAGS_MASK) | X86_EFLAGS_IF; | |
4527 | fop += __ffs(ctxt->dst.bytes) * FASTOP_SIZE; | |
4528 | asm("push %[flags]; popf; call *%[fastop]; pushf; pop %[flags]\n" | |
4529 | : "+a"(ctxt->dst.val), "+b"(ctxt->src.val), [flags]"+D"(flags) | |
4530 | : "c"(ctxt->src2.val), [fastop]"S"(fop)); | |
4531 | ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK); | |
4532 | return X86EMUL_CONTINUE; | |
4533 | } | |
dd856efa | 4534 | |
7b105ca2 | 4535 | int x86_emulate_insn(struct x86_emulate_ctxt *ctxt) |
8b4caf66 | 4536 | { |
0225fb50 | 4537 | const struct x86_emulate_ops *ops = ctxt->ops; |
1b30eaa8 | 4538 | int rc = X86EMUL_CONTINUE; |
9dac77fa | 4539 | int saved_dst_type = ctxt->dst.type; |
8b4caf66 | 4540 | |
9dac77fa | 4541 | ctxt->mem_read.pos = 0; |
310b5d30 | 4542 | |
9dac77fa | 4543 | if (ctxt->mode == X86EMUL_MODE_PROT64 && (ctxt->d & No64)) { |
35d3d4a1 | 4544 | rc = emulate_ud(ctxt); |
1161624f GN |
4545 | goto done; |
4546 | } | |
4547 | ||
d380a5e4 | 4548 | /* LOCK prefix is allowed only with some instructions */ |
9dac77fa | 4549 | if (ctxt->lock_prefix && (!(ctxt->d & Lock) || ctxt->dst.type != OP_MEM)) { |
35d3d4a1 | 4550 | rc = emulate_ud(ctxt); |
d380a5e4 GN |
4551 | goto done; |
4552 | } | |
4553 | ||
9dac77fa | 4554 | if ((ctxt->d & SrcMask) == SrcMemFAddr && ctxt->src.type != OP_MEM) { |
35d3d4a1 | 4555 | rc = emulate_ud(ctxt); |
081bca0e AK |
4556 | goto done; |
4557 | } | |
4558 | ||
cbe2c9d3 AK |
4559 | if (((ctxt->d & (Sse|Mmx)) && ((ops->get_cr(ctxt, 0) & X86_CR0_EM))) |
4560 | || ((ctxt->d & Sse) && !(ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))) { | |
1253791d AK |
4561 | rc = emulate_ud(ctxt); |
4562 | goto done; | |
4563 | } | |
4564 | ||
cbe2c9d3 | 4565 | if ((ctxt->d & (Sse|Mmx)) && (ops->get_cr(ctxt, 0) & X86_CR0_TS)) { |
1253791d AK |
4566 | rc = emulate_nm(ctxt); |
4567 | goto done; | |
4568 | } | |
4569 | ||
cbe2c9d3 AK |
4570 | if (ctxt->d & Mmx) { |
4571 | rc = flush_pending_x87_faults(ctxt); | |
4572 | if (rc != X86EMUL_CONTINUE) | |
4573 | goto done; | |
4574 | /* | |
4575 | * Now that we know the fpu is exception safe, we can fetch | |
4576 | * operands from it. | |
4577 | */ | |
4578 | fetch_possible_mmx_operand(ctxt, &ctxt->src); | |
4579 | fetch_possible_mmx_operand(ctxt, &ctxt->src2); | |
4580 | if (!(ctxt->d & Mov)) | |
4581 | fetch_possible_mmx_operand(ctxt, &ctxt->dst); | |
4582 | } | |
4583 | ||
9dac77fa AK |
4584 | if (unlikely(ctxt->guest_mode) && ctxt->intercept) { |
4585 | rc = emulator_check_intercept(ctxt, ctxt->intercept, | |
8a76d7f2 | 4586 | X86_ICPT_PRE_EXCEPT); |
c4f035c6 AK |
4587 | if (rc != X86EMUL_CONTINUE) |
4588 | goto done; | |
4589 | } | |
4590 | ||
e92805ac | 4591 | /* Privileged instruction can be executed only in CPL=0 */ |
9dac77fa | 4592 | if ((ctxt->d & Priv) && ops->cpl(ctxt)) { |
35d3d4a1 | 4593 | rc = emulate_gp(ctxt, 0); |
e92805ac GN |
4594 | goto done; |
4595 | } | |
4596 | ||
8ea7d6ae | 4597 | /* Instruction can only be executed in protected mode */ |
9d1b39a9 | 4598 | if ((ctxt->d & Prot) && ctxt->mode < X86EMUL_MODE_PROT16) { |
8ea7d6ae JR |
4599 | rc = emulate_ud(ctxt); |
4600 | goto done; | |
4601 | } | |
4602 | ||
d09beabd | 4603 | /* Do instruction specific permission checks */ |
9dac77fa AK |
4604 | if (ctxt->check_perm) { |
4605 | rc = ctxt->check_perm(ctxt); | |
d09beabd JR |
4606 | if (rc != X86EMUL_CONTINUE) |
4607 | goto done; | |
4608 | } | |
4609 | ||
9dac77fa AK |
4610 | if (unlikely(ctxt->guest_mode) && ctxt->intercept) { |
4611 | rc = emulator_check_intercept(ctxt, ctxt->intercept, | |
8a76d7f2 | 4612 | X86_ICPT_POST_EXCEPT); |
c4f035c6 AK |
4613 | if (rc != X86EMUL_CONTINUE) |
4614 | goto done; | |
4615 | } | |
4616 | ||
9dac77fa | 4617 | if (ctxt->rep_prefix && (ctxt->d & String)) { |
b9fa9d6b | 4618 | /* All REP prefixes have the same first termination condition */ |
dd856efa | 4619 | if (address_mask(ctxt, reg_read(ctxt, VCPU_REGS_RCX)) == 0) { |
9dac77fa | 4620 | ctxt->eip = ctxt->_eip; |
b9fa9d6b AK |
4621 | goto done; |
4622 | } | |
b9fa9d6b AK |
4623 | } |
4624 | ||
9dac77fa AK |
4625 | if ((ctxt->src.type == OP_MEM) && !(ctxt->d & NoAccess)) { |
4626 | rc = segmented_read(ctxt, ctxt->src.addr.mem, | |
4627 | ctxt->src.valptr, ctxt->src.bytes); | |
b60d513c | 4628 | if (rc != X86EMUL_CONTINUE) |
8b4caf66 | 4629 | goto done; |
9dac77fa | 4630 | ctxt->src.orig_val64 = ctxt->src.val64; |
8b4caf66 LV |
4631 | } |
4632 | ||
9dac77fa AK |
4633 | if (ctxt->src2.type == OP_MEM) { |
4634 | rc = segmented_read(ctxt, ctxt->src2.addr.mem, | |
4635 | &ctxt->src2.val, ctxt->src2.bytes); | |
e35b7b9c GN |
4636 | if (rc != X86EMUL_CONTINUE) |
4637 | goto done; | |
4638 | } | |
4639 | ||
9dac77fa | 4640 | if ((ctxt->d & DstMask) == ImplicitOps) |
8b4caf66 LV |
4641 | goto special_insn; |
4642 | ||
4643 | ||
9dac77fa | 4644 | if ((ctxt->dst.type == OP_MEM) && !(ctxt->d & Mov)) { |
69f55cb1 | 4645 | /* optimisation - avoid slow emulated read if Mov */ |
9dac77fa AK |
4646 | rc = segmented_read(ctxt, ctxt->dst.addr.mem, |
4647 | &ctxt->dst.val, ctxt->dst.bytes); | |
69f55cb1 GN |
4648 | if (rc != X86EMUL_CONTINUE) |
4649 | goto done; | |
038e51de | 4650 | } |
9dac77fa | 4651 | ctxt->dst.orig_val = ctxt->dst.val; |
038e51de | 4652 | |
018a98db AK |
4653 | special_insn: |
4654 | ||
9dac77fa AK |
4655 | if (unlikely(ctxt->guest_mode) && ctxt->intercept) { |
4656 | rc = emulator_check_intercept(ctxt, ctxt->intercept, | |
8a76d7f2 | 4657 | X86_ICPT_POST_MEMACCESS); |
c4f035c6 AK |
4658 | if (rc != X86EMUL_CONTINUE) |
4659 | goto done; | |
4660 | } | |
4661 | ||
9dac77fa | 4662 | if (ctxt->execute) { |
e28bbd44 AK |
4663 | if (ctxt->d & Fastop) { |
4664 | void (*fop)(struct fastop *) = (void *)ctxt->execute; | |
4665 | rc = fastop(ctxt, fop); | |
4666 | if (rc != X86EMUL_CONTINUE) | |
4667 | goto done; | |
4668 | goto writeback; | |
4669 | } | |
9dac77fa | 4670 | rc = ctxt->execute(ctxt); |
ef65c889 AK |
4671 | if (rc != X86EMUL_CONTINUE) |
4672 | goto done; | |
4673 | goto writeback; | |
4674 | } | |
4675 | ||
9dac77fa | 4676 | if (ctxt->twobyte) |
6aa8b732 AK |
4677 | goto twobyte_insn; |
4678 | ||
9dac77fa | 4679 | switch (ctxt->b) { |
6aa8b732 | 4680 | case 0x63: /* movsxd */ |
8b4caf66 | 4681 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
6aa8b732 | 4682 | goto cannot_emulate; |
9dac77fa | 4683 | ctxt->dst.val = (s32) ctxt->src.val; |
6aa8b732 | 4684 | break; |
b2833e3c | 4685 | case 0x70 ... 0x7f: /* jcc (short) */ |
9dac77fa AK |
4686 | if (test_cc(ctxt->b, ctxt->eflags)) |
4687 | jmp_rel(ctxt, ctxt->src.val); | |
018a98db | 4688 | break; |
7e0b54b1 | 4689 | case 0x8d: /* lea r16/r32, m */ |
9dac77fa | 4690 | ctxt->dst.val = ctxt->src.addr.mem.ea; |
7e0b54b1 | 4691 | break; |
3d9e77df | 4692 | case 0x90 ... 0x97: /* nop / xchg reg, rax */ |
dd856efa | 4693 | if (ctxt->dst.addr.reg == reg_rmw(ctxt, VCPU_REGS_RAX)) |
34698d8c | 4694 | break; |
e4f973ae TY |
4695 | rc = em_xchg(ctxt); |
4696 | break; | |
e8b6fa70 | 4697 | case 0x98: /* cbw/cwde/cdqe */ |
9dac77fa AK |
4698 | switch (ctxt->op_bytes) { |
4699 | case 2: ctxt->dst.val = (s8)ctxt->dst.val; break; | |
4700 | case 4: ctxt->dst.val = (s16)ctxt->dst.val; break; | |
4701 | case 8: ctxt->dst.val = (s32)ctxt->dst.val; break; | |
e8b6fa70 WY |
4702 | } |
4703 | break; | |
6e154e56 | 4704 | case 0xcc: /* int3 */ |
5c5df76b TY |
4705 | rc = emulate_int(ctxt, 3); |
4706 | break; | |
6e154e56 | 4707 | case 0xcd: /* int n */ |
9dac77fa | 4708 | rc = emulate_int(ctxt, ctxt->src.val); |
6e154e56 MG |
4709 | break; |
4710 | case 0xce: /* into */ | |
5c5df76b TY |
4711 | if (ctxt->eflags & EFLG_OF) |
4712 | rc = emulate_int(ctxt, 4); | |
6e154e56 | 4713 | break; |
1a52e051 | 4714 | case 0xe9: /* jmp rel */ |
db5b0762 | 4715 | case 0xeb: /* jmp rel short */ |
9dac77fa AK |
4716 | jmp_rel(ctxt, ctxt->src.val); |
4717 | ctxt->dst.type = OP_NONE; /* Disable writeback. */ | |
1a52e051 | 4718 | break; |
111de5d6 | 4719 | case 0xf4: /* hlt */ |
6c3287f7 | 4720 | ctxt->ops->halt(ctxt); |
19fdfa0d | 4721 | break; |
111de5d6 AK |
4722 | case 0xf5: /* cmc */ |
4723 | /* complement carry flag from eflags reg */ | |
4724 | ctxt->eflags ^= EFLG_CF; | |
111de5d6 AK |
4725 | break; |
4726 | case 0xf8: /* clc */ | |
4727 | ctxt->eflags &= ~EFLG_CF; | |
111de5d6 | 4728 | break; |
8744aa9a MG |
4729 | case 0xf9: /* stc */ |
4730 | ctxt->eflags |= EFLG_CF; | |
4731 | break; | |
fb4616f4 MG |
4732 | case 0xfc: /* cld */ |
4733 | ctxt->eflags &= ~EFLG_DF; | |
fb4616f4 MG |
4734 | break; |
4735 | case 0xfd: /* std */ | |
4736 | ctxt->eflags |= EFLG_DF; | |
fb4616f4 | 4737 | break; |
91269b8f AK |
4738 | default: |
4739 | goto cannot_emulate; | |
6aa8b732 | 4740 | } |
018a98db | 4741 | |
7d9ddaed AK |
4742 | if (rc != X86EMUL_CONTINUE) |
4743 | goto done; | |
4744 | ||
018a98db | 4745 | writeback: |
adddcecf | 4746 | rc = writeback(ctxt); |
1b30eaa8 | 4747 | if (rc != X86EMUL_CONTINUE) |
018a98db AK |
4748 | goto done; |
4749 | ||
5cd21917 GN |
4750 | /* |
4751 | * restore dst type in case the decoding will be reused | |
4752 | * (happens for string instruction ) | |
4753 | */ | |
9dac77fa | 4754 | ctxt->dst.type = saved_dst_type; |
5cd21917 | 4755 | |
9dac77fa | 4756 | if ((ctxt->d & SrcMask) == SrcSI) |
f3bd64c6 | 4757 | string_addr_inc(ctxt, VCPU_REGS_RSI, &ctxt->src); |
a682e354 | 4758 | |
9dac77fa | 4759 | if ((ctxt->d & DstMask) == DstDI) |
f3bd64c6 | 4760 | string_addr_inc(ctxt, VCPU_REGS_RDI, &ctxt->dst); |
d9271123 | 4761 | |
9dac77fa | 4762 | if (ctxt->rep_prefix && (ctxt->d & String)) { |
b3356bf0 | 4763 | unsigned int count; |
9dac77fa | 4764 | struct read_cache *r = &ctxt->io_read; |
b3356bf0 GN |
4765 | if ((ctxt->d & SrcMask) == SrcSI) |
4766 | count = ctxt->src.count; | |
4767 | else | |
4768 | count = ctxt->dst.count; | |
4769 | register_address_increment(ctxt, reg_rmw(ctxt, VCPU_REGS_RCX), | |
4770 | -count); | |
3e2f65d5 | 4771 | |
d2ddd1c4 GN |
4772 | if (!string_insn_completed(ctxt)) { |
4773 | /* | |
4774 | * Re-enter guest when pio read ahead buffer is empty | |
4775 | * or, if it is not used, after each 1024 iteration. | |
4776 | */ | |
dd856efa | 4777 | if ((r->end != 0 || reg_read(ctxt, VCPU_REGS_RCX) & 0x3ff) && |
d2ddd1c4 GN |
4778 | (r->end == 0 || r->end != r->pos)) { |
4779 | /* | |
4780 | * Reset read cache. Usually happens before | |
4781 | * decode, but since instruction is restarted | |
4782 | * we have to do it here. | |
4783 | */ | |
9dac77fa | 4784 | ctxt->mem_read.end = 0; |
dd856efa | 4785 | writeback_registers(ctxt); |
d2ddd1c4 GN |
4786 | return EMULATION_RESTART; |
4787 | } | |
4788 | goto done; /* skip rip writeback */ | |
0fa6ccbd | 4789 | } |
5cd21917 | 4790 | } |
d2ddd1c4 | 4791 | |
9dac77fa | 4792 | ctxt->eip = ctxt->_eip; |
018a98db AK |
4793 | |
4794 | done: | |
da9cb575 AK |
4795 | if (rc == X86EMUL_PROPAGATE_FAULT) |
4796 | ctxt->have_exception = true; | |
775fde86 JR |
4797 | if (rc == X86EMUL_INTERCEPTED) |
4798 | return EMULATION_INTERCEPTED; | |
4799 | ||
dd856efa AK |
4800 | if (rc == X86EMUL_CONTINUE) |
4801 | writeback_registers(ctxt); | |
4802 | ||
d2ddd1c4 | 4803 | return (rc == X86EMUL_UNHANDLEABLE) ? EMULATION_FAILED : EMULATION_OK; |
6aa8b732 AK |
4804 | |
4805 | twobyte_insn: | |
9dac77fa | 4806 | switch (ctxt->b) { |
018a98db | 4807 | case 0x09: /* wbinvd */ |
cfb22375 | 4808 | (ctxt->ops->wbinvd)(ctxt); |
f5f48ee1 SY |
4809 | break; |
4810 | case 0x08: /* invd */ | |
018a98db AK |
4811 | case 0x0d: /* GrpP (prefetch) */ |
4812 | case 0x18: /* Grp16 (prefetch/nop) */ | |
018a98db AK |
4813 | break; |
4814 | case 0x20: /* mov cr, reg */ | |
9dac77fa | 4815 | ctxt->dst.val = ops->get_cr(ctxt, ctxt->modrm_reg); |
018a98db | 4816 | break; |
6aa8b732 | 4817 | case 0x21: /* mov from dr to reg */ |
9dac77fa | 4818 | ops->get_dr(ctxt, ctxt->modrm_reg, &ctxt->dst.val); |
6aa8b732 | 4819 | break; |
6aa8b732 | 4820 | case 0x40 ... 0x4f: /* cmov */ |
9dac77fa AK |
4821 | ctxt->dst.val = ctxt->dst.orig_val = ctxt->src.val; |
4822 | if (!test_cc(ctxt->b, ctxt->eflags)) | |
4823 | ctxt->dst.type = OP_NONE; /* no writeback */ | |
6aa8b732 | 4824 | break; |
b2833e3c | 4825 | case 0x80 ... 0x8f: /* jnz rel, etc*/ |
9dac77fa AK |
4826 | if (test_cc(ctxt->b, ctxt->eflags)) |
4827 | jmp_rel(ctxt, ctxt->src.val); | |
018a98db | 4828 | break; |
ee45b58e | 4829 | case 0x90 ... 0x9f: /* setcc r/m8 */ |
9dac77fa | 4830 | ctxt->dst.val = test_cc(ctxt->b, ctxt->eflags); |
ee45b58e | 4831 | break; |
2a7c5b8b GC |
4832 | case 0xae: /* clflush */ |
4833 | break; | |
6aa8b732 | 4834 | case 0xb6 ... 0xb7: /* movzx */ |
9dac77fa | 4835 | ctxt->dst.bytes = ctxt->op_bytes; |
361cad2b | 4836 | ctxt->dst.val = (ctxt->src.bytes == 1) ? (u8) ctxt->src.val |
9dac77fa | 4837 | : (u16) ctxt->src.val; |
6aa8b732 | 4838 | break; |
6aa8b732 | 4839 | case 0xbe ... 0xbf: /* movsx */ |
9dac77fa | 4840 | ctxt->dst.bytes = ctxt->op_bytes; |
361cad2b | 4841 | ctxt->dst.val = (ctxt->src.bytes == 1) ? (s8) ctxt->src.val : |
9dac77fa | 4842 | (s16) ctxt->src.val; |
6aa8b732 | 4843 | break; |
92f738a5 | 4844 | case 0xc0 ... 0xc1: /* xadd */ |
a31b9cea | 4845 | emulate_2op_SrcV(ctxt, "add"); |
92f738a5 | 4846 | /* Write back the register source. */ |
9dac77fa AK |
4847 | ctxt->src.val = ctxt->dst.orig_val; |
4848 | write_register_operand(&ctxt->src); | |
92f738a5 | 4849 | break; |
a012e65a | 4850 | case 0xc3: /* movnti */ |
9dac77fa AK |
4851 | ctxt->dst.bytes = ctxt->op_bytes; |
4852 | ctxt->dst.val = (ctxt->op_bytes == 4) ? (u32) ctxt->src.val : | |
4853 | (u64) ctxt->src.val; | |
a012e65a | 4854 | break; |
91269b8f AK |
4855 | default: |
4856 | goto cannot_emulate; | |
6aa8b732 | 4857 | } |
7d9ddaed AK |
4858 | |
4859 | if (rc != X86EMUL_CONTINUE) | |
4860 | goto done; | |
4861 | ||
6aa8b732 AK |
4862 | goto writeback; |
4863 | ||
4864 | cannot_emulate: | |
a0c0ab2f | 4865 | return EMULATION_FAILED; |
6aa8b732 | 4866 | } |
dd856efa AK |
4867 | |
4868 | void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt) | |
4869 | { | |
4870 | invalidate_registers(ctxt); | |
4871 | } | |
4872 | ||
4873 | void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt) | |
4874 | { | |
4875 | writeback_registers(ctxt); | |
4876 | } |