blob: 460c2d8964b7db48994fd814cc9f47145a06579a [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/******************************************************************************
2 * x86_emulate.h
3 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005 Keir Fraser
7 *
8 * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
9 */
10
H. Peter Anvin1965aae2008-10-22 22:26:29 -070011#ifndef _ASM_X86_KVM_X86_EMULATE_H
12#define _ASM_X86_KVM_X86_EMULATE_H
Avi Kivity6aa8b732006-12-10 02:21:36 -080013
Gleb Natapov38ba30b2010-03-18 15:20:17 +020014#include <asm/desc_defs.h>
15
Avi Kivity6aa8b732006-12-10 02:21:36 -080016struct x86_emulate_ctxt;
Avi Kivityc4f035c2011-04-04 12:39:22 +020017enum x86_intercept;
18enum x86_intercept_stage;
Avi Kivity6aa8b732006-12-10 02:21:36 -080019
Avi Kivityda9cb572010-11-22 17:53:21 +020020struct x86_exception {
21 u8 vector;
22 bool error_code_valid;
23 u16 error_code;
Avi Kivity6389ee92010-11-29 16:12:30 +020024 bool nested_page_fault;
25 u64 address; /* cr2 or nested page fault gpa */
Avi Kivityda9cb572010-11-22 17:53:21 +020026};
27
Avi Kivity6aa8b732006-12-10 02:21:36 -080028/*
29 * x86_emulate_ops:
30 *
31 * These operations represent the instruction emulator's interface to memory.
32 * There are two categories of operation: those that act on ordinary memory
33 * regions (*_std), and those that act on memory regions known to require
34 * special treatment or emulation (*_emulated).
35 *
36 * The emulator assumes that an instruction accesses only one 'emulated memory'
37 * location, that this location is the given linear faulting address (cr2), and
38 * that this is one of the instruction's data operands. Instruction fetches and
39 * stack operations are assumed never to access emulated memory. The emulator
40 * automatically deduces which operand of a string-move operation is accessing
41 * emulated memory, and assumes that the other operand accesses normal memory.
42 *
43 * NOTES:
44 * 1. The emulator isn't very smart about emulated vs. standard memory.
45 * 'Emulated memory' access addresses should be checked for sanity.
46 * 'Normal memory' accesses may fault, and the caller must arrange to
47 * detect and handle reentrancy into the emulator via recursive faults.
48 * Accesses may be unaligned and may cross page boundaries.
49 * 2. If the access fails (cannot emulate, or a standard access faults) then
50 * it is up to the memop to propagate the fault to the guest VM via
51 * some out-of-band mechanism, unknown to the emulator. The memop signals
52 * failure by returning X86EMUL_PROPAGATE_FAULT to the emulator, which will
53 * then immediately bail.
54 * 3. Valid access sizes are 1, 2, 4 and 8 bytes. On x86/32 systems only
55 * cmpxchg8b_emulated need support 8-byte accesses.
56 * 4. The emulator cannot handle 64-bit mode emulation on an x86/32 system.
57 */
58/* Access completed successfully: continue emulation as normal. */
59#define X86EMUL_CONTINUE 0
60/* Access is unhandleable: bail from emulation and return error to caller. */
61#define X86EMUL_UNHANDLEABLE 1
62/* Terminate emulation but return success to the caller. */
63#define X86EMUL_PROPAGATE_FAULT 2 /* propagate a generated fault to guest */
Gleb Natapove6800802010-04-28 19:15:33 +030064#define X86EMUL_RETRY_INSTR 3 /* retry the instruction for some reason */
65#define X86EMUL_CMPXCHG_FAILED 4 /* cmpxchg did not see expected value */
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +030066#define X86EMUL_IO_NEEDED 5 /* IO is needed to complete emulation */
Avi Kivityc4f035c2011-04-04 12:39:22 +020067#define X86EMUL_INTERCEPTED 6 /* Intercepted by nested VMCB/VMCS */
Gleb Natapove6800802010-04-28 19:15:33 +030068
Avi Kivity6aa8b732006-12-10 02:21:36 -080069struct x86_emulate_ops {
70 /*
71 * read_std: Read bytes of standard (non-emulated/special) memory.
Gleb Natapov1871c602010-02-10 14:21:32 +020072 * Used for descriptor reading.
Avi Kivity6aa8b732006-12-10 02:21:36 -080073 * @addr: [IN ] Linear address from which to read.
74 * @val: [OUT] Value read from memory, zero-extended to 'u_long'.
75 * @bytes: [IN ] Number of bytes to read from memory.
76 */
Avi Kivity4c690a12007-04-22 15:28:19 +030077 int (*read_std)(unsigned long addr, void *val,
Avi Kivitybcc55cb2010-11-22 17:53:22 +020078 unsigned int bytes, struct kvm_vcpu *vcpu,
79 struct x86_exception *fault);
Gleb Natapov1871c602010-02-10 14:21:32 +020080
81 /*
Gleb Natapov2dafc6c2010-03-18 15:20:16 +020082 * write_std: Write bytes of standard (non-emulated/special) memory.
83 * Used for descriptor writing.
84 * @addr: [IN ] Linear address to which to write.
85 * @val: [OUT] Value write to memory, zero-extended to 'u_long'.
86 * @bytes: [IN ] Number of bytes to write to memory.
87 */
88 int (*write_std)(unsigned long addr, void *val,
Avi Kivitybcc55cb2010-11-22 17:53:22 +020089 unsigned int bytes, struct kvm_vcpu *vcpu,
90 struct x86_exception *fault);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +020091 /*
Gleb Natapov1871c602010-02-10 14:21:32 +020092 * fetch: Read bytes of standard (non-emulated/special) memory.
93 * Used for instruction fetch.
94 * @addr: [IN ] Linear address from which to read.
95 * @val: [OUT] Value read from memory, zero-extended to 'u_long'.
96 * @bytes: [IN ] Number of bytes to read from memory.
97 */
98 int (*fetch)(unsigned long addr, void *val,
Avi Kivitybcc55cb2010-11-22 17:53:22 +020099 unsigned int bytes, struct kvm_vcpu *vcpu,
100 struct x86_exception *fault);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800101
102 /*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800103 * read_emulated: Read bytes from emulated/special memory area.
104 * @addr: [IN ] Linear address from which to read.
105 * @val: [OUT] Value read from memory, zero-extended to 'u_long'.
106 * @bytes: [IN ] Number of bytes to read from memory.
107 */
Joe Perches0c7825e2008-03-23 01:02:35 -0700108 int (*read_emulated)(unsigned long addr,
109 void *val,
110 unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200111 struct x86_exception *fault,
Joe Perches0c7825e2008-03-23 01:02:35 -0700112 struct kvm_vcpu *vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113
114 /*
Takuya Yoshikawa0d178972010-01-06 17:55:23 +0900115 * write_emulated: Write bytes to emulated/special memory area.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800116 * @addr: [IN ] Linear address to which to write.
117 * @val: [IN ] Value to write to memory (low-order bytes used as
118 * required).
119 * @bytes: [IN ] Number of bytes to write to memory.
120 */
Joe Perches0c7825e2008-03-23 01:02:35 -0700121 int (*write_emulated)(unsigned long addr,
122 const void *val,
123 unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200124 struct x86_exception *fault,
Joe Perches0c7825e2008-03-23 01:02:35 -0700125 struct kvm_vcpu *vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800126
127 /*
128 * cmpxchg_emulated: Emulate an atomic (LOCKed) CMPXCHG operation on an
129 * emulated/special memory area.
130 * @addr: [IN ] Linear address to access.
131 * @old: [IN ] Value expected to be current at @addr.
132 * @new: [IN ] Value to write to @addr.
133 * @bytes: [IN ] Number of bytes to access using CMPXCHG.
134 */
Joe Perches0c7825e2008-03-23 01:02:35 -0700135 int (*cmpxchg_emulated)(unsigned long addr,
136 const void *old,
137 const void *new,
138 unsigned int bytes,
Avi Kivitybcc55cb2010-11-22 17:53:22 +0200139 struct x86_exception *fault,
Joe Perches0c7825e2008-03-23 01:02:35 -0700140 struct kvm_vcpu *vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +0200141
142 int (*pio_in_emulated)(int size, unsigned short port, void *val,
143 unsigned int count, struct kvm_vcpu *vcpu);
144
145 int (*pio_out_emulated)(int size, unsigned short port, const void *val,
146 unsigned int count, struct kvm_vcpu *vcpu);
147
Gleb Natapov5601d052011-03-07 14:55:06 +0200148 bool (*get_cached_descriptor)(struct desc_struct *desc, u32 *base3,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +0200149 int seg, struct kvm_vcpu *vcpu);
Gleb Natapov5601d052011-03-07 14:55:06 +0200150 void (*set_cached_descriptor)(struct desc_struct *desc, u32 base3,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +0200151 int seg, struct kvm_vcpu *vcpu);
152 u16 (*get_segment_selector)(int seg, struct kvm_vcpu *vcpu);
153 void (*set_segment_selector)(u16 sel, int seg, struct kvm_vcpu *vcpu);
Gleb Natapov5951c442010-04-28 19:15:29 +0300154 unsigned long (*get_cached_segment_base)(int seg, struct kvm_vcpu *vcpu);
Gleb Natapov2dafc6c2010-03-18 15:20:16 +0200155 void (*get_gdt)(struct desc_ptr *dt, struct kvm_vcpu *vcpu);
Mohammed Gamal160ce1f2010-08-04 05:44:24 +0300156 void (*get_idt)(struct desc_ptr *dt, struct kvm_vcpu *vcpu);
Gleb Natapov52a46612010-03-18 15:20:03 +0200157 ulong (*get_cr)(int cr, struct kvm_vcpu *vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300158 int (*set_cr)(int cr, ulong val, struct kvm_vcpu *vcpu);
Gleb Natapov9c537242010-03-18 15:20:05 +0200159 int (*cpl)(struct kvm_vcpu *vcpu);
Gleb Natapov35aa5372010-04-28 19:15:27 +0300160 int (*get_dr)(int dr, unsigned long *dest, struct kvm_vcpu *vcpu);
161 int (*set_dr)(int dr, unsigned long value, struct kvm_vcpu *vcpu);
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +0300162 int (*set_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
163 int (*get_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata);
Avi Kivity5037f6f2011-03-28 16:53:59 +0200164 void (*get_fpu)(struct x86_emulate_ctxt *ctxt); /* disables preempt */
165 void (*put_fpu)(struct x86_emulate_ctxt *ctxt); /* reenables preempt */
Avi Kivityc4f035c2011-04-04 12:39:22 +0200166 int (*intercept)(struct x86_emulate_ctxt *ctxt,
167 enum x86_intercept intercept,
168 enum x86_intercept_stage stage);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800169};
170
Avi Kivity12537912011-03-29 11:41:27 +0200171typedef u32 __attribute__((vector_size(16))) sse128_t;
172
Laurent Viviere4e03de2007-09-18 11:52:50 +0200173/* Type, address-of, and value of an instruction's operand. */
174struct operand {
Avi Kivity12537912011-03-29 11:41:27 +0200175 enum { OP_REG, OP_MEM, OP_IMM, OP_XMM, OP_NONE } type;
Laurent Viviere4e03de2007-09-18 11:52:50 +0200176 unsigned int bytes;
Avi Kivity16518d52010-08-26 14:31:30 +0300177 union {
178 unsigned long orig_val;
179 u64 orig_val64;
180 };
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300181 union {
182 unsigned long *reg;
Avi Kivity90de84f2010-11-17 15:28:21 +0200183 struct segmented_address {
184 ulong ea;
185 unsigned seg;
186 } mem;
Avi Kivity12537912011-03-29 11:41:27 +0200187 unsigned xmm;
Avi Kivity1a6440aef2010-08-01 12:35:10 +0300188 } addr;
Gleb Natapov414e6272010-04-28 19:15:26 +0300189 union {
190 unsigned long val;
Avi Kivity16518d52010-08-26 14:31:30 +0300191 u64 val64;
Gleb Natapov414e6272010-04-28 19:15:26 +0300192 char valptr[sizeof(unsigned long) + 2];
Avi Kivity12537912011-03-29 11:41:27 +0200193 sse128_t vec_val;
Gleb Natapov414e6272010-04-28 19:15:26 +0300194 };
Laurent Viviere4e03de2007-09-18 11:52:50 +0200195};
196
Avi Kivity62266862007-11-20 13:15:52 +0200197struct fetch_cache {
198 u8 data[15];
199 unsigned long start;
200 unsigned long end;
201};
202
Gleb Natapov7b262e92010-03-18 15:20:27 +0200203struct read_cache {
204 u8 data[1024];
205 unsigned long pos;
206 unsigned long end;
207};
208
Laurent Viviere4e03de2007-09-18 11:52:50 +0200209struct decode_cache {
210 u8 twobyte;
211 u8 b;
Avi Kivityc4f035c2011-04-04 12:39:22 +0200212 u8 intercept;
Laurent Viviere4e03de2007-09-18 11:52:50 +0200213 u8 lock_prefix;
214 u8 rep_prefix;
215 u8 op_bytes;
216 u8 ad_bytes;
Avi Kivity33615aa2007-10-31 11:15:56 +0200217 u8 rex_prefix;
Laurent Viviere4e03de2007-09-18 11:52:50 +0200218 struct operand src;
Guillaume Thouvenin0dc8d102008-12-04 14:26:42 +0100219 struct operand src2;
Laurent Viviere4e03de2007-09-18 11:52:50 +0200220 struct operand dst;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300221 bool has_seg_override;
222 u8 seg_override;
Laurent Viviere4e03de2007-09-18 11:52:50 +0200223 unsigned int d;
Avi Kivityef65c882010-07-29 15:11:51 +0300224 int (*execute)(struct x86_emulate_ctxt *ctxt);
Joerg Roedeld09beab2011-04-04 12:39:25 +0200225 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
Laurent Viviere4e03de2007-09-18 11:52:50 +0200226 unsigned long regs[NR_VCPU_REGS];
Gleb Natapov063db062010-03-18 15:20:06 +0200227 unsigned long eip;
Laurent Viviere4e03de2007-09-18 11:52:50 +0200228 /* modrm */
229 u8 modrm;
230 u8 modrm_mod;
231 u8 modrm_reg;
232 u8 modrm_rm;
Avi Kivity09ee57c2010-08-01 12:07:29 +0300233 u8 modrm_seg;
Avi Kivityf5b4edc2008-06-15 22:09:11 -0700234 bool rip_relative;
Avi Kivity62266862007-11-20 13:15:52 +0200235 struct fetch_cache fetch;
Gleb Natapov7b262e92010-03-18 15:20:27 +0200236 struct read_cache io_read;
Gleb Natapov9de41572010-04-28 19:15:22 +0300237 struct read_cache mem_read;
Laurent Viviere4e03de2007-09-18 11:52:50 +0200238};
239
Avi Kivity6aa8b732006-12-10 02:21:36 -0800240struct x86_emulate_ctxt {
Avi Kivity9aabc88f2010-07-29 15:11:50 +0300241 struct x86_emulate_ops *ops;
242
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243 /* Register state before/after emulation. */
244 struct kvm_vcpu *vcpu;
245
Avi Kivity6aa8b732006-12-10 02:21:36 -0800246 unsigned long eflags;
Gleb Natapov063db062010-03-18 15:20:06 +0200247 unsigned long eip; /* eip before instruction emulation */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800248 /* Emulated execution mode, represented by an X86EMUL_MODE value. */
249 int mode;
Avi Kivity7a5b56d2008-06-22 16:22:51 +0300250 u32 cs_base;
Laurent Viviere4e03de2007-09-18 11:52:50 +0200251
Glauber Costa310b5d32009-05-12 16:21:06 -0400252 /* interruptibility state, as a result of execution of STI or MOV SS */
253 int interruptibility;
254
Avi Kivityc4f035c2011-04-04 12:39:22 +0200255 bool guest_mode; /* guest running a nested guest */
Gleb Natapov4fc40f02010-08-02 12:47:51 +0300256 bool perm_ok; /* do not check permissions if true */
Avi Kivityd8671622011-02-01 16:32:03 +0200257 bool only_vendor_specific_insn;
Gleb Natapov54b84862010-04-28 19:15:44 +0300258
Avi Kivityda9cb572010-11-22 17:53:21 +0200259 bool have_exception;
260 struct x86_exception exception;
Gleb Natapov54b84862010-04-28 19:15:44 +0300261
Laurent Viviere4e03de2007-09-18 11:52:50 +0200262 /* decode cache */
Laurent Viviere4e03de2007-09-18 11:52:50 +0200263 struct decode_cache decode;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800264};
265
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100266/* Repeat String Operation Prefix */
Avi Kivity1d6b1142010-01-20 16:00:35 +0200267#define REPE_PREFIX 0xf3
268#define REPNE_PREFIX 0xf2
Guillaume Thouvenin90e0a282007-11-22 11:32:09 +0100269
Avi Kivity6aa8b732006-12-10 02:21:36 -0800270/* Execution mode, passed to the emulator. */
271#define X86EMUL_MODE_REAL 0 /* Real mode. */
Gleb Natapova0044752010-02-10 14:21:31 +0200272#define X86EMUL_MODE_VM86 1 /* Virtual 8086 mode. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800273#define X86EMUL_MODE_PROT16 2 /* 16-bit protected mode. */
274#define X86EMUL_MODE_PROT32 4 /* 32-bit protected mode. */
275#define X86EMUL_MODE_PROT64 8 /* 64-bit (long) mode. */
276
Avi Kivityc4f035c2011-04-04 12:39:22 +0200277enum x86_intercept_stage {
278 X86_ICPT_PRE_EXCEPT,
279 X86_ICPT_POST_EXCEPT,
280 X86_ICPT_POST_MEMACCESS,
281};
282
283enum x86_intercept {
284 x86_intercept_none,
Avi Kivity3c6e2762011-04-04 12:39:23 +0200285 x86_intercept_lmsw,
286 x86_intercept_smsw,
287 x86_intercept_lidt,
288 x86_intercept_sidt,
289 x86_intercept_lgdt,
290 x86_intercept_sgdt,
291 x86_intercept_lldt,
292 x86_intercept_sldt,
293 x86_intercept_ltr,
294 x86_intercept_str,
295 x86_intercept_rdtsc,
296 x86_intercept_rdpmc,
297 x86_intercept_pushf,
298 x86_intercept_popf,
299 x86_intercept_cpuid,
300 x86_intercept_rsm,
301 x86_intercept_iret,
302 x86_intercept_intn,
303 x86_intercept_invd,
304 x86_intercept_pause,
305 x86_intercept_hlt,
306 x86_intercept_invlpg,
307 x86_intercept_invlpga,
308 x86_intercept_vmrun,
309 x86_intercept_vmload,
310 x86_intercept_vmsave,
311 x86_intercept_vmmcall,
312 x86_intercept_stgi,
313 x86_intercept_clgi,
314 x86_intercept_skinit,
315 x86_intercept_rdtscp,
316 x86_intercept_icebp,
317 x86_intercept_wbinvd,
318 x86_intercept_monitor,
319 x86_intercept_mwait,
Avi Kivityc4f035c2011-04-04 12:39:22 +0200320
321 nr_x86_intercepts
322};
323
Avi Kivity6aa8b732006-12-10 02:21:36 -0800324/* Host execution mode. */
Sheng Yangd73fa292008-10-14 15:59:10 +0800325#if defined(CONFIG_X86_32)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800326#define X86EMUL_MODE_HOST X86EMUL_MODE_PROT32
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800327#elif defined(CONFIG_X86_64)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800328#define X86EMUL_MODE_HOST X86EMUL_MODE_PROT64
329#endif
330
Andre Przywaradc25e892010-12-21 11:12:07 +0100331int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len);
Gleb Natapovd2ddd1c2010-08-25 12:47:43 +0300332#define EMULATION_FAILED -1
333#define EMULATION_OK 0
334#define EMULATION_RESTART 1
Joerg Roedel775fde82011-04-04 12:39:24 +0200335#define EMULATION_INTERCEPTED 2
Avi Kivity9aabc88f2010-07-29 15:11:50 +0300336int x86_emulate_insn(struct x86_emulate_ctxt *ctxt);
Gleb Natapov38ba30b2010-03-18 15:20:17 +0200337int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
Jan Kiszkae269fb22010-04-14 15:51:09 +0200338 u16 tss_selector, int reason,
339 bool has_error_code, u32 error_code);
Mohammed Gamal4ab8e022010-09-19 14:34:05 +0200340int emulate_int_real(struct x86_emulate_ctxt *ctxt,
341 struct x86_emulate_ops *ops, int irq);
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700342#endif /* _ASM_X86_KVM_X86_EMULATE_H */