1
2
3
4
5
6
7
8
9
10
11#ifndef _ASM_X86_KVM_X86_EMULATE_H
12#define _ASM_X86_KVM_X86_EMULATE_H
13
14#include <asm/desc_defs.h>
15
16struct x86_emulate_ctxt;
17enum x86_intercept;
18enum x86_intercept_stage;
19
20struct x86_exception {
21 u8 vector;
22 bool error_code_valid;
23 u16 error_code;
24 bool nested_page_fault;
25 u64 address;
26};
27
28
29
30
31
32
33struct x86_instruction_info {
34 u8 intercept;
35 u8 rep_prefix;
36 u8 modrm_mod;
37 u8 modrm_reg;
38 u8 modrm_rm;
39 u64 src_val;
40 u8 src_bytes;
41 u8 dst_bytes;
42 u8 ad_bytes;
43 u64 next_rip;
44};
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77#define X86EMUL_CONTINUE 0
78
79#define X86EMUL_UNHANDLEABLE 1
80
81#define X86EMUL_PROPAGATE_FAULT 2
82#define X86EMUL_RETRY_INSTR 3
83#define X86EMUL_CMPXCHG_FAILED 4
84#define X86EMUL_IO_NEEDED 5
85#define X86EMUL_INTERCEPTED 6
86
87struct x86_emulate_ops {
88
89
90
91
92
93
94
95 int (*read_std)(struct x86_emulate_ctxt *ctxt,
96 unsigned long addr, void *val,
97 unsigned int bytes,
98 struct x86_exception *fault);
99
100
101
102
103
104
105
106
107 int (*write_std)(struct x86_emulate_ctxt *ctxt,
108 unsigned long addr, void *val, unsigned int bytes,
109 struct x86_exception *fault);
110
111
112
113
114
115
116
117 int (*fetch)(struct x86_emulate_ctxt *ctxt,
118 unsigned long addr, void *val, unsigned int bytes,
119 struct x86_exception *fault);
120
121
122
123
124
125
126
127 int (*read_emulated)(struct x86_emulate_ctxt *ctxt,
128 unsigned long addr, void *val, unsigned int bytes,
129 struct x86_exception *fault);
130
131
132
133
134
135
136
137
138 int (*write_emulated)(struct x86_emulate_ctxt *ctxt,
139 unsigned long addr, const void *val,
140 unsigned int bytes,
141 struct x86_exception *fault);
142
143
144
145
146
147
148
149
150
151 int (*cmpxchg_emulated)(struct x86_emulate_ctxt *ctxt,
152 unsigned long addr,
153 const void *old,
154 const void *new,
155 unsigned int bytes,
156 struct x86_exception *fault);
157 void (*invlpg)(struct x86_emulate_ctxt *ctxt, ulong addr);
158
159 int (*pio_in_emulated)(struct x86_emulate_ctxt *ctxt,
160 int size, unsigned short port, void *val,
161 unsigned int count);
162
163 int (*pio_out_emulated)(struct x86_emulate_ctxt *ctxt,
164 int size, unsigned short port, const void *val,
165 unsigned int count);
166
167 bool (*get_segment)(struct x86_emulate_ctxt *ctxt, u16 *selector,
168 struct desc_struct *desc, u32 *base3, int seg);
169 void (*set_segment)(struct x86_emulate_ctxt *ctxt, u16 selector,
170 struct desc_struct *desc, u32 base3, int seg);
171 unsigned long (*get_cached_segment_base)(struct x86_emulate_ctxt *ctxt,
172 int seg);
173 void (*get_gdt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
174 void (*get_idt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
175 void (*set_gdt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
176 void (*set_idt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
177 ulong (*get_cr)(struct x86_emulate_ctxt *ctxt, int cr);
178 int (*set_cr)(struct x86_emulate_ctxt *ctxt, int cr, ulong val);
179 int (*cpl)(struct x86_emulate_ctxt *ctxt);
180 int (*get_dr)(struct x86_emulate_ctxt *ctxt, int dr, ulong *dest);
181 int (*set_dr)(struct x86_emulate_ctxt *ctxt, int dr, ulong value);
182 int (*set_msr)(struct x86_emulate_ctxt *ctxt, u32 msr_index, u64 data);
183 int (*get_msr)(struct x86_emulate_ctxt *ctxt, u32 msr_index, u64 *pdata);
184 void (*halt)(struct x86_emulate_ctxt *ctxt);
185 void (*wbinvd)(struct x86_emulate_ctxt *ctxt);
186 int (*fix_hypercall)(struct x86_emulate_ctxt *ctxt);
187 void (*get_fpu)(struct x86_emulate_ctxt *ctxt);
188 void (*put_fpu)(struct x86_emulate_ctxt *ctxt);
189 int (*intercept)(struct x86_emulate_ctxt *ctxt,
190 struct x86_instruction_info *info,
191 enum x86_intercept_stage stage);
192};
193
194typedef u32 __attribute__((vector_size(16))) sse128_t;
195
196
197struct operand {
198 enum { OP_REG, OP_MEM, OP_IMM, OP_XMM, OP_NONE } type;
199 unsigned int bytes;
200 union {
201 unsigned long orig_val;
202 u64 orig_val64;
203 };
204 union {
205 unsigned long *reg;
206 struct segmented_address {
207 ulong ea;
208 unsigned seg;
209 } mem;
210 unsigned xmm;
211 } addr;
212 union {
213 unsigned long val;
214 u64 val64;
215 char valptr[sizeof(unsigned long) + 2];
216 sse128_t vec_val;
217 };
218};
219
220struct fetch_cache {
221 u8 data[15];
222 unsigned long start;
223 unsigned long end;
224};
225
226struct read_cache {
227 u8 data[1024];
228 unsigned long pos;
229 unsigned long end;
230};
231
232struct x86_emulate_ctxt {
233 struct x86_emulate_ops *ops;
234
235
236 unsigned long eflags;
237 unsigned long eip;
238
239 int mode;
240
241
242 int interruptibility;
243
244 bool guest_mode;
245 bool perm_ok;
246 bool only_vendor_specific_insn;
247
248 bool have_exception;
249 struct x86_exception exception;
250
251
252 u8 twobyte;
253 u8 b;
254 u8 intercept;
255 u8 lock_prefix;
256 u8 rep_prefix;
257 u8 op_bytes;
258 u8 ad_bytes;
259 u8 rex_prefix;
260 struct operand src;
261 struct operand src2;
262 struct operand dst;
263 bool has_seg_override;
264 u8 seg_override;
265 unsigned int d;
266 int (*execute)(struct x86_emulate_ctxt *ctxt);
267 int (*check_perm)(struct x86_emulate_ctxt *ctxt);
268
269 u8 modrm;
270 u8 modrm_mod;
271 u8 modrm_reg;
272 u8 modrm_rm;
273 u8 modrm_seg;
274 bool rip_relative;
275 unsigned long _eip;
276
277 unsigned long regs[NR_VCPU_REGS];
278 struct fetch_cache fetch;
279 struct read_cache io_read;
280 struct read_cache mem_read;
281};
282
283
284#define REPE_PREFIX 0xf3
285#define REPNE_PREFIX 0xf2
286
287
288#define X86EMUL_MODE_REAL 0
289#define X86EMUL_MODE_VM86 1
290#define X86EMUL_MODE_PROT16 2
291#define X86EMUL_MODE_PROT32 4
292#define X86EMUL_MODE_PROT64 8
293
294
295#define X86EMUL_MODE_PROT (X86EMUL_MODE_PROT16|X86EMUL_MODE_PROT32| \
296 X86EMUL_MODE_PROT64)
297
298enum x86_intercept_stage {
299 X86_ICTP_NONE = 0,
300 X86_ICPT_PRE_EXCEPT,
301 X86_ICPT_POST_EXCEPT,
302 X86_ICPT_POST_MEMACCESS,
303};
304
305enum x86_intercept {
306 x86_intercept_none,
307 x86_intercept_cr_read,
308 x86_intercept_cr_write,
309 x86_intercept_clts,
310 x86_intercept_lmsw,
311 x86_intercept_smsw,
312 x86_intercept_dr_read,
313 x86_intercept_dr_write,
314 x86_intercept_lidt,
315 x86_intercept_sidt,
316 x86_intercept_lgdt,
317 x86_intercept_sgdt,
318 x86_intercept_lldt,
319 x86_intercept_sldt,
320 x86_intercept_ltr,
321 x86_intercept_str,
322 x86_intercept_rdtsc,
323 x86_intercept_rdpmc,
324 x86_intercept_pushf,
325 x86_intercept_popf,
326 x86_intercept_cpuid,
327 x86_intercept_rsm,
328 x86_intercept_iret,
329 x86_intercept_intn,
330 x86_intercept_invd,
331 x86_intercept_pause,
332 x86_intercept_hlt,
333 x86_intercept_invlpg,
334 x86_intercept_invlpga,
335 x86_intercept_vmrun,
336 x86_intercept_vmload,
337 x86_intercept_vmsave,
338 x86_intercept_vmmcall,
339 x86_intercept_stgi,
340 x86_intercept_clgi,
341 x86_intercept_skinit,
342 x86_intercept_rdtscp,
343 x86_intercept_icebp,
344 x86_intercept_wbinvd,
345 x86_intercept_monitor,
346 x86_intercept_mwait,
347 x86_intercept_rdmsr,
348 x86_intercept_wrmsr,
349 x86_intercept_in,
350 x86_intercept_ins,
351 x86_intercept_out,
352 x86_intercept_outs,
353
354 nr_x86_intercepts
355};
356
357
358#if defined(CONFIG_X86_32)
359#define X86EMUL_MODE_HOST X86EMUL_MODE_PROT32
360#elif defined(CONFIG_X86_64)
361#define X86EMUL_MODE_HOST X86EMUL_MODE_PROT64
362#endif
363
364int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len);
365#define EMULATION_FAILED -1
366#define EMULATION_OK 0
367#define EMULATION_RESTART 1
368#define EMULATION_INTERCEPTED 2
369int x86_emulate_insn(struct x86_emulate_ctxt *ctxt);
370int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
371 u16 tss_selector, int reason,
372 bool has_error_code, u32 error_code);
373int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq);
374#endif
375