Commit | Line | Data |
---|---|---|
6aa8b732 AK |
1 | /****************************************************************************** |
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 | ||
1965aae3 PA |
11 | #ifndef _ASM_X86_KVM_X86_EMULATE_H |
12 | #define _ASM_X86_KVM_X86_EMULATE_H | |
6aa8b732 | 13 | |
38ba30ba GN |
14 | #include <asm/desc_defs.h> |
15 | ||
6aa8b732 | 16 | struct x86_emulate_ctxt; |
c4f035c6 AK |
17 | enum x86_intercept; |
18 | enum x86_intercept_stage; | |
6aa8b732 | 19 | |
da9cb575 AK |
20 | struct x86_exception { |
21 | u8 vector; | |
22 | bool error_code_valid; | |
23 | u16 error_code; | |
6389ee94 AK |
24 | bool nested_page_fault; |
25 | u64 address; /* cr2 or nested page fault gpa */ | |
da9cb575 AK |
26 | }; |
27 | ||
6aa8b732 AK |
28 | /* |
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 */ | |
e680080e GN |
64 | #define X86EMUL_RETRY_INSTR 3 /* retry the instruction for some reason */ |
65 | #define X86EMUL_CMPXCHG_FAILED 4 /* cmpxchg did not see expected value */ | |
c3cd7ffa | 66 | #define X86EMUL_IO_NEEDED 5 /* IO is needed to complete emulation */ |
c4f035c6 | 67 | #define X86EMUL_INTERCEPTED 6 /* Intercepted by nested VMCB/VMCS */ |
e680080e | 68 | |
6aa8b732 AK |
69 | struct x86_emulate_ops { |
70 | /* | |
71 | * read_std: Read bytes of standard (non-emulated/special) memory. | |
1871c602 | 72 | * Used for descriptor reading. |
6aa8b732 AK |
73 | * @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 | */ | |
4c690a1e | 77 | int (*read_std)(unsigned long addr, void *val, |
bcc55cba AK |
78 | unsigned int bytes, struct kvm_vcpu *vcpu, |
79 | struct x86_exception *fault); | |
1871c602 | 80 | |
2dafc6c2 GN |
81 | /* |
82 | * 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, | |
bcc55cba AK |
89 | unsigned int bytes, struct kvm_vcpu *vcpu, |
90 | struct x86_exception *fault); | |
1871c602 GN |
91 | /* |
92 | * 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, | |
bcc55cba AK |
99 | unsigned int bytes, struct kvm_vcpu *vcpu, |
100 | struct x86_exception *fault); | |
6aa8b732 AK |
101 | |
102 | /* | |
103 | * 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 | */ | |
0c7825e6 JP |
108 | int (*read_emulated)(unsigned long addr, |
109 | void *val, | |
110 | unsigned int bytes, | |
bcc55cba | 111 | struct x86_exception *fault, |
0c7825e6 | 112 | struct kvm_vcpu *vcpu); |
6aa8b732 AK |
113 | |
114 | /* | |
0d178975 | 115 | * write_emulated: Write bytes to emulated/special memory area. |
6aa8b732 AK |
116 | * @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 | */ | |
0c7825e6 JP |
121 | int (*write_emulated)(unsigned long addr, |
122 | const void *val, | |
123 | unsigned int bytes, | |
bcc55cba | 124 | struct x86_exception *fault, |
0c7825e6 | 125 | struct kvm_vcpu *vcpu); |
6aa8b732 AK |
126 | |
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 | */ | |
0c7825e6 JP |
135 | int (*cmpxchg_emulated)(unsigned long addr, |
136 | const void *old, | |
137 | const void *new, | |
138 | unsigned int bytes, | |
bcc55cba | 139 | struct x86_exception *fault, |
0c7825e6 | 140 | struct kvm_vcpu *vcpu); |
cf8f70bf GN |
141 | |
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 | ||
5601d05b | 148 | bool (*get_cached_descriptor)(struct desc_struct *desc, u32 *base3, |
2dafc6c2 | 149 | int seg, struct kvm_vcpu *vcpu); |
5601d05b | 150 | void (*set_cached_descriptor)(struct desc_struct *desc, u32 base3, |
2dafc6c2 GN |
151 | 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); | |
5951c442 | 154 | unsigned long (*get_cached_segment_base)(int seg, struct kvm_vcpu *vcpu); |
2dafc6c2 | 155 | void (*get_gdt)(struct desc_ptr *dt, struct kvm_vcpu *vcpu); |
160ce1f1 | 156 | void (*get_idt)(struct desc_ptr *dt, struct kvm_vcpu *vcpu); |
52a46617 | 157 | ulong (*get_cr)(int cr, struct kvm_vcpu *vcpu); |
0f12244f | 158 | int (*set_cr)(int cr, ulong val, struct kvm_vcpu *vcpu); |
9c537244 | 159 | int (*cpl)(struct kvm_vcpu *vcpu); |
35aa5375 GN |
160 | 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); | |
3fb1b5db GN |
162 | 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); | |
5037f6f3 AK |
164 | void (*get_fpu)(struct x86_emulate_ctxt *ctxt); /* disables preempt */ |
165 | void (*put_fpu)(struct x86_emulate_ctxt *ctxt); /* reenables preempt */ | |
c4f035c6 AK |
166 | int (*intercept)(struct x86_emulate_ctxt *ctxt, |
167 | enum x86_intercept intercept, | |
168 | enum x86_intercept_stage stage); | |
6aa8b732 AK |
169 | }; |
170 | ||
1253791d AK |
171 | typedef u32 __attribute__((vector_size(16))) sse128_t; |
172 | ||
e4e03ded LV |
173 | /* Type, address-of, and value of an instruction's operand. */ |
174 | struct operand { | |
1253791d | 175 | enum { OP_REG, OP_MEM, OP_IMM, OP_XMM, OP_NONE } type; |
e4e03ded | 176 | unsigned int bytes; |
16518d5a AK |
177 | union { |
178 | unsigned long orig_val; | |
179 | u64 orig_val64; | |
180 | }; | |
1a6440ae AK |
181 | union { |
182 | unsigned long *reg; | |
90de84f5 AK |
183 | struct segmented_address { |
184 | ulong ea; | |
185 | unsigned seg; | |
186 | } mem; | |
1253791d | 187 | unsigned xmm; |
1a6440ae | 188 | } addr; |
414e6277 GN |
189 | union { |
190 | unsigned long val; | |
16518d5a | 191 | u64 val64; |
414e6277 | 192 | char valptr[sizeof(unsigned long) + 2]; |
1253791d | 193 | sse128_t vec_val; |
414e6277 | 194 | }; |
e4e03ded LV |
195 | }; |
196 | ||
62266869 AK |
197 | struct fetch_cache { |
198 | u8 data[15]; | |
199 | unsigned long start; | |
200 | unsigned long end; | |
201 | }; | |
202 | ||
7b262e90 GN |
203 | struct read_cache { |
204 | u8 data[1024]; | |
205 | unsigned long pos; | |
206 | unsigned long end; | |
207 | }; | |
208 | ||
e4e03ded LV |
209 | struct decode_cache { |
210 | u8 twobyte; | |
211 | u8 b; | |
c4f035c6 | 212 | u8 intercept; |
e4e03ded LV |
213 | u8 lock_prefix; |
214 | u8 rep_prefix; | |
215 | u8 op_bytes; | |
216 | u8 ad_bytes; | |
33615aa9 | 217 | u8 rex_prefix; |
e4e03ded | 218 | struct operand src; |
0dc8d10f | 219 | struct operand src2; |
e4e03ded | 220 | struct operand dst; |
7a5b56df AK |
221 | bool has_seg_override; |
222 | u8 seg_override; | |
e4e03ded | 223 | unsigned int d; |
ef65c889 | 224 | int (*execute)(struct x86_emulate_ctxt *ctxt); |
d09beabd | 225 | int (*check_perm)(struct x86_emulate_ctxt *ctxt); |
e4e03ded | 226 | unsigned long regs[NR_VCPU_REGS]; |
063db061 | 227 | unsigned long eip; |
e4e03ded LV |
228 | /* modrm */ |
229 | u8 modrm; | |
230 | u8 modrm_mod; | |
231 | u8 modrm_reg; | |
232 | u8 modrm_rm; | |
09ee57cd | 233 | u8 modrm_seg; |
f5b4edcd | 234 | bool rip_relative; |
62266869 | 235 | struct fetch_cache fetch; |
7b262e90 | 236 | struct read_cache io_read; |
9de41573 | 237 | struct read_cache mem_read; |
e4e03ded LV |
238 | }; |
239 | ||
6aa8b732 | 240 | struct x86_emulate_ctxt { |
9aabc88f AK |
241 | struct x86_emulate_ops *ops; |
242 | ||
6aa8b732 AK |
243 | /* Register state before/after emulation. */ |
244 | struct kvm_vcpu *vcpu; | |
245 | ||
6aa8b732 | 246 | unsigned long eflags; |
063db061 | 247 | unsigned long eip; /* eip before instruction emulation */ |
6aa8b732 AK |
248 | /* Emulated execution mode, represented by an X86EMUL_MODE value. */ |
249 | int mode; | |
7a5b56df | 250 | u32 cs_base; |
e4e03ded | 251 | |
310b5d30 GC |
252 | /* interruptibility state, as a result of execution of STI or MOV SS */ |
253 | int interruptibility; | |
254 | ||
c4f035c6 | 255 | bool guest_mode; /* guest running a nested guest */ |
4fc40f07 | 256 | bool perm_ok; /* do not check permissions if true */ |
d867162c | 257 | bool only_vendor_specific_insn; |
54b8486f | 258 | |
da9cb575 AK |
259 | bool have_exception; |
260 | struct x86_exception exception; | |
54b8486f | 261 | |
e4e03ded | 262 | /* decode cache */ |
e4e03ded | 263 | struct decode_cache decode; |
6aa8b732 AK |
264 | }; |
265 | ||
90e0a28f | 266 | /* Repeat String Operation Prefix */ |
1d6b114f AK |
267 | #define REPE_PREFIX 0xf3 |
268 | #define REPNE_PREFIX 0xf2 | |
90e0a28f | 269 | |
6aa8b732 AK |
270 | /* Execution mode, passed to the emulator. */ |
271 | #define X86EMUL_MODE_REAL 0 /* Real mode. */ | |
a0044755 | 272 | #define X86EMUL_MODE_VM86 1 /* Virtual 8086 mode. */ |
6aa8b732 AK |
273 | #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 | ||
8ea7d6ae JR |
277 | /* any protected mode */ |
278 | #define X86EMUL_MODE_PROT (X86EMUL_MODE_PROT16|X86EMUL_MODE_PROT32| \ | |
279 | X86EMUL_MODE_PROT64) | |
280 | ||
c4f035c6 AK |
281 | enum x86_intercept_stage { |
282 | X86_ICPT_PRE_EXCEPT, | |
283 | X86_ICPT_POST_EXCEPT, | |
284 | X86_ICPT_POST_MEMACCESS, | |
285 | }; | |
286 | ||
287 | enum x86_intercept { | |
288 | x86_intercept_none, | |
3c6e276f AK |
289 | x86_intercept_lmsw, |
290 | x86_intercept_smsw, | |
291 | x86_intercept_lidt, | |
292 | x86_intercept_sidt, | |
293 | x86_intercept_lgdt, | |
294 | x86_intercept_sgdt, | |
295 | x86_intercept_lldt, | |
296 | x86_intercept_sldt, | |
297 | x86_intercept_ltr, | |
298 | x86_intercept_str, | |
299 | x86_intercept_rdtsc, | |
300 | x86_intercept_rdpmc, | |
301 | x86_intercept_pushf, | |
302 | x86_intercept_popf, | |
303 | x86_intercept_cpuid, | |
304 | x86_intercept_rsm, | |
305 | x86_intercept_iret, | |
306 | x86_intercept_intn, | |
307 | x86_intercept_invd, | |
308 | x86_intercept_pause, | |
309 | x86_intercept_hlt, | |
310 | x86_intercept_invlpg, | |
311 | x86_intercept_invlpga, | |
312 | x86_intercept_vmrun, | |
313 | x86_intercept_vmload, | |
314 | x86_intercept_vmsave, | |
315 | x86_intercept_vmmcall, | |
316 | x86_intercept_stgi, | |
317 | x86_intercept_clgi, | |
318 | x86_intercept_skinit, | |
319 | x86_intercept_rdtscp, | |
320 | x86_intercept_icebp, | |
321 | x86_intercept_wbinvd, | |
322 | x86_intercept_monitor, | |
323 | x86_intercept_mwait, | |
c4f035c6 AK |
324 | |
325 | nr_x86_intercepts | |
326 | }; | |
327 | ||
6aa8b732 | 328 | /* Host execution mode. */ |
d73fa29a | 329 | #if defined(CONFIG_X86_32) |
6aa8b732 | 330 | #define X86EMUL_MODE_HOST X86EMUL_MODE_PROT32 |
05b3e0c2 | 331 | #elif defined(CONFIG_X86_64) |
6aa8b732 AK |
332 | #define X86EMUL_MODE_HOST X86EMUL_MODE_PROT64 |
333 | #endif | |
334 | ||
dc25e89e | 335 | int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len); |
d2ddd1c4 GN |
336 | #define EMULATION_FAILED -1 |
337 | #define EMULATION_OK 0 | |
338 | #define EMULATION_RESTART 1 | |
775fde86 | 339 | #define EMULATION_INTERCEPTED 2 |
9aabc88f | 340 | int x86_emulate_insn(struct x86_emulate_ctxt *ctxt); |
38ba30ba | 341 | int emulator_task_switch(struct x86_emulate_ctxt *ctxt, |
e269fb21 JK |
342 | u16 tss_selector, int reason, |
343 | bool has_error_code, u32 error_code); | |
4ab8e024 MG |
344 | int emulate_int_real(struct x86_emulate_ctxt *ctxt, |
345 | struct x86_emulate_ops *ops, int irq); | |
1965aae3 | 346 | #endif /* _ASM_X86_KVM_X86_EMULATE_H */ |