diff options
Diffstat (limited to 'arch/x86/include/asm/kvm_emulate.h')
-rw-r--r-- | arch/x86/include/asm/kvm_emulate.h | 241 |
1 files changed, 179 insertions, 62 deletions
diff --git a/arch/x86/include/asm/kvm_emulate.h b/arch/x86/include/asm/kvm_emulate.h index 1f99ecfc48e1..0049211959c0 100644 --- a/arch/x86/include/asm/kvm_emulate.h +++ b/arch/x86/include/asm/kvm_emulate.h | |||
@@ -14,6 +14,34 @@ | |||
14 | #include <asm/desc_defs.h> | 14 | #include <asm/desc_defs.h> |
15 | 15 | ||
16 | struct x86_emulate_ctxt; | 16 | struct x86_emulate_ctxt; |
17 | enum x86_intercept; | ||
18 | enum x86_intercept_stage; | ||
19 | |||
20 | struct x86_exception { | ||
21 | u8 vector; | ||
22 | bool error_code_valid; | ||
23 | u16 error_code; | ||
24 | bool nested_page_fault; | ||
25 | u64 address; /* cr2 or nested page fault gpa */ | ||
26 | }; | ||
27 | |||
28 | /* | ||
29 | * This struct is used to carry enough information from the instruction | ||
30 | * decoder to main KVM so that a decision can be made whether the | ||
31 | * instruction needs to be intercepted or not. | ||
32 | */ | ||
33 | struct x86_instruction_info { | ||
34 | u8 intercept; /* which intercept */ | ||
35 | u8 rep_prefix; /* rep prefix? */ | ||
36 | u8 modrm_mod; /* mod part of modrm */ | ||
37 | u8 modrm_reg; /* index of register used */ | ||
38 | u8 modrm_rm; /* rm part of modrm */ | ||
39 | u64 src_val; /* value of source operand */ | ||
40 | u8 src_bytes; /* size of source operand */ | ||
41 | u8 dst_bytes; /* size of destination operand */ | ||
42 | u8 ad_bytes; /* size of src/dst address */ | ||
43 | u64 next_rip; /* rip following the instruction */ | ||
44 | }; | ||
17 | 45 | ||
18 | /* | 46 | /* |
19 | * x86_emulate_ops: | 47 | * x86_emulate_ops: |
@@ -54,6 +82,7 @@ struct x86_emulate_ctxt; | |||
54 | #define X86EMUL_RETRY_INSTR 3 /* retry the instruction for some reason */ | 82 | #define X86EMUL_RETRY_INSTR 3 /* retry the instruction for some reason */ |
55 | #define X86EMUL_CMPXCHG_FAILED 4 /* cmpxchg did not see expected value */ | 83 | #define X86EMUL_CMPXCHG_FAILED 4 /* cmpxchg did not see expected value */ |
56 | #define X86EMUL_IO_NEEDED 5 /* IO is needed to complete emulation */ | 84 | #define X86EMUL_IO_NEEDED 5 /* IO is needed to complete emulation */ |
85 | #define X86EMUL_INTERCEPTED 6 /* Intercepted by nested VMCB/VMCS */ | ||
57 | 86 | ||
58 | struct x86_emulate_ops { | 87 | struct x86_emulate_ops { |
59 | /* | 88 | /* |
@@ -63,8 +92,10 @@ struct x86_emulate_ops { | |||
63 | * @val: [OUT] Value read from memory, zero-extended to 'u_long'. | 92 | * @val: [OUT] Value read from memory, zero-extended to 'u_long'. |
64 | * @bytes: [IN ] Number of bytes to read from memory. | 93 | * @bytes: [IN ] Number of bytes to read from memory. |
65 | */ | 94 | */ |
66 | int (*read_std)(unsigned long addr, void *val, | 95 | int (*read_std)(struct x86_emulate_ctxt *ctxt, |
67 | unsigned int bytes, struct kvm_vcpu *vcpu, u32 *error); | 96 | unsigned long addr, void *val, |
97 | unsigned int bytes, | ||
98 | struct x86_exception *fault); | ||
68 | 99 | ||
69 | /* | 100 | /* |
70 | * write_std: Write bytes of standard (non-emulated/special) memory. | 101 | * write_std: Write bytes of standard (non-emulated/special) memory. |
@@ -73,8 +104,9 @@ struct x86_emulate_ops { | |||
73 | * @val: [OUT] Value write to memory, zero-extended to 'u_long'. | 104 | * @val: [OUT] Value write to memory, zero-extended to 'u_long'. |
74 | * @bytes: [IN ] Number of bytes to write to memory. | 105 | * @bytes: [IN ] Number of bytes to write to memory. |
75 | */ | 106 | */ |
76 | int (*write_std)(unsigned long addr, void *val, | 107 | int (*write_std)(struct x86_emulate_ctxt *ctxt, |
77 | unsigned int bytes, struct kvm_vcpu *vcpu, u32 *error); | 108 | unsigned long addr, void *val, unsigned int bytes, |
109 | struct x86_exception *fault); | ||
78 | /* | 110 | /* |
79 | * fetch: Read bytes of standard (non-emulated/special) memory. | 111 | * fetch: Read bytes of standard (non-emulated/special) memory. |
80 | * Used for instruction fetch. | 112 | * Used for instruction fetch. |
@@ -82,8 +114,9 @@ struct x86_emulate_ops { | |||
82 | * @val: [OUT] Value read from memory, zero-extended to 'u_long'. | 114 | * @val: [OUT] Value read from memory, zero-extended to 'u_long'. |
83 | * @bytes: [IN ] Number of bytes to read from memory. | 115 | * @bytes: [IN ] Number of bytes to read from memory. |
84 | */ | 116 | */ |
85 | int (*fetch)(unsigned long addr, void *val, | 117 | int (*fetch)(struct x86_emulate_ctxt *ctxt, |
86 | unsigned int bytes, struct kvm_vcpu *vcpu, u32 *error); | 118 | unsigned long addr, void *val, unsigned int bytes, |
119 | struct x86_exception *fault); | ||
87 | 120 | ||
88 | /* | 121 | /* |
89 | * read_emulated: Read bytes from emulated/special memory area. | 122 | * read_emulated: Read bytes from emulated/special memory area. |
@@ -91,11 +124,9 @@ struct x86_emulate_ops { | |||
91 | * @val: [OUT] Value read from memory, zero-extended to 'u_long'. | 124 | * @val: [OUT] Value read from memory, zero-extended to 'u_long'. |
92 | * @bytes: [IN ] Number of bytes to read from memory. | 125 | * @bytes: [IN ] Number of bytes to read from memory. |
93 | */ | 126 | */ |
94 | int (*read_emulated)(unsigned long addr, | 127 | int (*read_emulated)(struct x86_emulate_ctxt *ctxt, |
95 | void *val, | 128 | unsigned long addr, void *val, unsigned int bytes, |
96 | unsigned int bytes, | 129 | struct x86_exception *fault); |
97 | unsigned int *error, | ||
98 | struct kvm_vcpu *vcpu); | ||
99 | 130 | ||
100 | /* | 131 | /* |
101 | * write_emulated: Write bytes to emulated/special memory area. | 132 | * write_emulated: Write bytes to emulated/special memory area. |
@@ -104,11 +135,10 @@ struct x86_emulate_ops { | |||
104 | * required). | 135 | * required). |
105 | * @bytes: [IN ] Number of bytes to write to memory. | 136 | * @bytes: [IN ] Number of bytes to write to memory. |
106 | */ | 137 | */ |
107 | int (*write_emulated)(unsigned long addr, | 138 | int (*write_emulated)(struct x86_emulate_ctxt *ctxt, |
108 | const void *val, | 139 | unsigned long addr, const void *val, |
109 | unsigned int bytes, | 140 | unsigned int bytes, |
110 | unsigned int *error, | 141 | struct x86_exception *fault); |
111 | struct kvm_vcpu *vcpu); | ||
112 | 142 | ||
113 | /* | 143 | /* |
114 | * cmpxchg_emulated: Emulate an atomic (LOCKed) CMPXCHG operation on an | 144 | * cmpxchg_emulated: Emulate an atomic (LOCKed) CMPXCHG operation on an |
@@ -118,49 +148,72 @@ struct x86_emulate_ops { | |||
118 | * @new: [IN ] Value to write to @addr. | 148 | * @new: [IN ] Value to write to @addr. |
119 | * @bytes: [IN ] Number of bytes to access using CMPXCHG. | 149 | * @bytes: [IN ] Number of bytes to access using CMPXCHG. |
120 | */ | 150 | */ |
121 | int (*cmpxchg_emulated)(unsigned long addr, | 151 | int (*cmpxchg_emulated)(struct x86_emulate_ctxt *ctxt, |
152 | unsigned long addr, | ||
122 | const void *old, | 153 | const void *old, |
123 | const void *new, | 154 | const void *new, |
124 | unsigned int bytes, | 155 | unsigned int bytes, |
125 | unsigned int *error, | 156 | struct x86_exception *fault); |
126 | struct kvm_vcpu *vcpu); | 157 | void (*invlpg)(struct x86_emulate_ctxt *ctxt, ulong addr); |
127 | 158 | ||
128 | int (*pio_in_emulated)(int size, unsigned short port, void *val, | 159 | int (*pio_in_emulated)(struct x86_emulate_ctxt *ctxt, |
129 | unsigned int count, struct kvm_vcpu *vcpu); | 160 | int size, unsigned short port, void *val, |
130 | 161 | unsigned int count); | |
131 | int (*pio_out_emulated)(int size, unsigned short port, const void *val, | 162 | |
132 | unsigned int count, struct kvm_vcpu *vcpu); | 163 | int (*pio_out_emulated)(struct x86_emulate_ctxt *ctxt, |
133 | 164 | int size, unsigned short port, const void *val, | |
134 | bool (*get_cached_descriptor)(struct desc_struct *desc, | 165 | unsigned int count); |
135 | int seg, struct kvm_vcpu *vcpu); | 166 | |
136 | void (*set_cached_descriptor)(struct desc_struct *desc, | 167 | bool (*get_segment)(struct x86_emulate_ctxt *ctxt, u16 *selector, |
137 | int seg, struct kvm_vcpu *vcpu); | 168 | struct desc_struct *desc, u32 *base3, int seg); |
138 | u16 (*get_segment_selector)(int seg, struct kvm_vcpu *vcpu); | 169 | void (*set_segment)(struct x86_emulate_ctxt *ctxt, u16 selector, |
139 | void (*set_segment_selector)(u16 sel, int seg, struct kvm_vcpu *vcpu); | 170 | struct desc_struct *desc, u32 base3, int seg); |
140 | unsigned long (*get_cached_segment_base)(int seg, struct kvm_vcpu *vcpu); | 171 | unsigned long (*get_cached_segment_base)(struct x86_emulate_ctxt *ctxt, |
141 | void (*get_gdt)(struct desc_ptr *dt, struct kvm_vcpu *vcpu); | 172 | int seg); |
142 | ulong (*get_cr)(int cr, struct kvm_vcpu *vcpu); | 173 | void (*get_gdt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt); |
143 | int (*set_cr)(int cr, ulong val, struct kvm_vcpu *vcpu); | 174 | void (*get_idt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt); |
144 | int (*cpl)(struct kvm_vcpu *vcpu); | 175 | void (*set_gdt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt); |
145 | int (*get_dr)(int dr, unsigned long *dest, struct kvm_vcpu *vcpu); | 176 | void (*set_idt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt); |
146 | int (*set_dr)(int dr, unsigned long value, struct kvm_vcpu *vcpu); | 177 | ulong (*get_cr)(struct x86_emulate_ctxt *ctxt, int cr); |
147 | int (*set_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 data); | 178 | int (*set_cr)(struct x86_emulate_ctxt *ctxt, int cr, ulong val); |
148 | int (*get_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata); | 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); /* disables preempt */ | ||
188 | void (*put_fpu)(struct x86_emulate_ctxt *ctxt); /* reenables preempt */ | ||
189 | int (*intercept)(struct x86_emulate_ctxt *ctxt, | ||
190 | struct x86_instruction_info *info, | ||
191 | enum x86_intercept_stage stage); | ||
149 | }; | 192 | }; |
150 | 193 | ||
194 | typedef u32 __attribute__((vector_size(16))) sse128_t; | ||
195 | |||
151 | /* Type, address-of, and value of an instruction's operand. */ | 196 | /* Type, address-of, and value of an instruction's operand. */ |
152 | struct operand { | 197 | struct operand { |
153 | enum { OP_REG, OP_MEM, OP_IMM, OP_NONE } type; | 198 | enum { OP_REG, OP_MEM, OP_IMM, OP_XMM, OP_NONE } type; |
154 | unsigned int bytes; | 199 | unsigned int bytes; |
155 | union { | 200 | union { |
156 | unsigned long orig_val; | 201 | unsigned long orig_val; |
157 | u64 orig_val64; | 202 | u64 orig_val64; |
158 | }; | 203 | }; |
159 | unsigned long *ptr; | 204 | union { |
205 | unsigned long *reg; | ||
206 | struct segmented_address { | ||
207 | ulong ea; | ||
208 | unsigned seg; | ||
209 | } mem; | ||
210 | unsigned xmm; | ||
211 | } addr; | ||
160 | union { | 212 | union { |
161 | unsigned long val; | 213 | unsigned long val; |
162 | u64 val64; | 214 | u64 val64; |
163 | char valptr[sizeof(unsigned long) + 2]; | 215 | char valptr[sizeof(unsigned long) + 2]; |
216 | sse128_t vec_val; | ||
164 | }; | 217 | }; |
165 | }; | 218 | }; |
166 | 219 | ||
@@ -179,6 +232,7 @@ struct read_cache { | |||
179 | struct decode_cache { | 232 | struct decode_cache { |
180 | u8 twobyte; | 233 | u8 twobyte; |
181 | u8 b; | 234 | u8 b; |
235 | u8 intercept; | ||
182 | u8 lock_prefix; | 236 | u8 lock_prefix; |
183 | u8 rep_prefix; | 237 | u8 rep_prefix; |
184 | u8 op_bytes; | 238 | u8 op_bytes; |
@@ -190,6 +244,8 @@ struct decode_cache { | |||
190 | bool has_seg_override; | 244 | bool has_seg_override; |
191 | u8 seg_override; | 245 | u8 seg_override; |
192 | unsigned int d; | 246 | unsigned int d; |
247 | int (*execute)(struct x86_emulate_ctxt *ctxt); | ||
248 | int (*check_perm)(struct x86_emulate_ctxt *ctxt); | ||
193 | unsigned long regs[NR_VCPU_REGS]; | 249 | unsigned long regs[NR_VCPU_REGS]; |
194 | unsigned long eip; | 250 | unsigned long eip; |
195 | /* modrm */ | 251 | /* modrm */ |
@@ -197,43 +253,39 @@ struct decode_cache { | |||
197 | u8 modrm_mod; | 253 | u8 modrm_mod; |
198 | u8 modrm_reg; | 254 | u8 modrm_reg; |
199 | u8 modrm_rm; | 255 | u8 modrm_rm; |
200 | u8 use_modrm_ea; | 256 | u8 modrm_seg; |
201 | bool rip_relative; | 257 | bool rip_relative; |
202 | unsigned long modrm_ea; | ||
203 | void *modrm_ptr; | ||
204 | unsigned long modrm_val; | ||
205 | struct fetch_cache fetch; | 258 | struct fetch_cache fetch; |
206 | struct read_cache io_read; | 259 | struct read_cache io_read; |
207 | struct read_cache mem_read; | 260 | struct read_cache mem_read; |
208 | }; | 261 | }; |
209 | 262 | ||
210 | struct x86_emulate_ctxt { | 263 | struct x86_emulate_ctxt { |
211 | /* Register state before/after emulation. */ | 264 | struct x86_emulate_ops *ops; |
212 | struct kvm_vcpu *vcpu; | ||
213 | 265 | ||
266 | /* Register state before/after emulation. */ | ||
214 | unsigned long eflags; | 267 | unsigned long eflags; |
215 | unsigned long eip; /* eip before instruction emulation */ | 268 | unsigned long eip; /* eip before instruction emulation */ |
216 | /* Emulated execution mode, represented by an X86EMUL_MODE value. */ | 269 | /* Emulated execution mode, represented by an X86EMUL_MODE value. */ |
217 | int mode; | 270 | int mode; |
218 | u32 cs_base; | ||
219 | 271 | ||
220 | /* interruptibility state, as a result of execution of STI or MOV SS */ | 272 | /* interruptibility state, as a result of execution of STI or MOV SS */ |
221 | int interruptibility; | 273 | int interruptibility; |
222 | 274 | ||
223 | bool restart; /* restart string instruction after writeback */ | 275 | bool guest_mode; /* guest running a nested guest */ |
276 | bool perm_ok; /* do not check permissions if true */ | ||
277 | bool only_vendor_specific_insn; | ||
224 | 278 | ||
225 | int exception; /* exception that happens during emulation or -1 */ | 279 | bool have_exception; |
226 | u32 error_code; /* error code for exception */ | 280 | struct x86_exception exception; |
227 | bool error_code_valid; | ||
228 | unsigned long cr2; /* faulted address in case of #PF */ | ||
229 | 281 | ||
230 | /* decode cache */ | 282 | /* decode cache */ |
231 | struct decode_cache decode; | 283 | struct decode_cache decode; |
232 | }; | 284 | }; |
233 | 285 | ||
234 | /* Repeat String Operation Prefix */ | 286 | /* Repeat String Operation Prefix */ |
235 | #define REPE_PREFIX 1 | 287 | #define REPE_PREFIX 0xf3 |
236 | #define REPNE_PREFIX 2 | 288 | #define REPNE_PREFIX 0xf2 |
237 | 289 | ||
238 | /* Execution mode, passed to the emulator. */ | 290 | /* Execution mode, passed to the emulator. */ |
239 | #define X86EMUL_MODE_REAL 0 /* Real mode. */ | 291 | #define X86EMUL_MODE_REAL 0 /* Real mode. */ |
@@ -242,6 +294,69 @@ struct x86_emulate_ctxt { | |||
242 | #define X86EMUL_MODE_PROT32 4 /* 32-bit protected mode. */ | 294 | #define X86EMUL_MODE_PROT32 4 /* 32-bit protected mode. */ |
243 | #define X86EMUL_MODE_PROT64 8 /* 64-bit (long) mode. */ | 295 | #define X86EMUL_MODE_PROT64 8 /* 64-bit (long) mode. */ |
244 | 296 | ||
297 | /* any protected mode */ | ||
298 | #define X86EMUL_MODE_PROT (X86EMUL_MODE_PROT16|X86EMUL_MODE_PROT32| \ | ||
299 | X86EMUL_MODE_PROT64) | ||
300 | |||
301 | enum x86_intercept_stage { | ||
302 | X86_ICTP_NONE = 0, /* Allow zero-init to not match anything */ | ||
303 | X86_ICPT_PRE_EXCEPT, | ||
304 | X86_ICPT_POST_EXCEPT, | ||
305 | X86_ICPT_POST_MEMACCESS, | ||
306 | }; | ||
307 | |||
308 | enum x86_intercept { | ||
309 | x86_intercept_none, | ||
310 | x86_intercept_cr_read, | ||
311 | x86_intercept_cr_write, | ||
312 | x86_intercept_clts, | ||
313 | x86_intercept_lmsw, | ||
314 | x86_intercept_smsw, | ||
315 | x86_intercept_dr_read, | ||
316 | x86_intercept_dr_write, | ||
317 | x86_intercept_lidt, | ||
318 | x86_intercept_sidt, | ||
319 | x86_intercept_lgdt, | ||
320 | x86_intercept_sgdt, | ||
321 | x86_intercept_lldt, | ||
322 | x86_intercept_sldt, | ||
323 | x86_intercept_ltr, | ||
324 | x86_intercept_str, | ||
325 | x86_intercept_rdtsc, | ||
326 | x86_intercept_rdpmc, | ||
327 | x86_intercept_pushf, | ||
328 | x86_intercept_popf, | ||
329 | x86_intercept_cpuid, | ||
330 | x86_intercept_rsm, | ||
331 | x86_intercept_iret, | ||
332 | x86_intercept_intn, | ||
333 | x86_intercept_invd, | ||
334 | x86_intercept_pause, | ||
335 | x86_intercept_hlt, | ||
336 | x86_intercept_invlpg, | ||
337 | x86_intercept_invlpga, | ||
338 | x86_intercept_vmrun, | ||
339 | x86_intercept_vmload, | ||
340 | x86_intercept_vmsave, | ||
341 | x86_intercept_vmmcall, | ||
342 | x86_intercept_stgi, | ||
343 | x86_intercept_clgi, | ||
344 | x86_intercept_skinit, | ||
345 | x86_intercept_rdtscp, | ||
346 | x86_intercept_icebp, | ||
347 | x86_intercept_wbinvd, | ||
348 | x86_intercept_monitor, | ||
349 | x86_intercept_mwait, | ||
350 | x86_intercept_rdmsr, | ||
351 | x86_intercept_wrmsr, | ||
352 | x86_intercept_in, | ||
353 | x86_intercept_ins, | ||
354 | x86_intercept_out, | ||
355 | x86_intercept_outs, | ||
356 | |||
357 | nr_x86_intercepts | ||
358 | }; | ||
359 | |||
245 | /* Host execution mode. */ | 360 | /* Host execution mode. */ |
246 | #if defined(CONFIG_X86_32) | 361 | #if defined(CONFIG_X86_32) |
247 | #define X86EMUL_MODE_HOST X86EMUL_MODE_PROT32 | 362 | #define X86EMUL_MODE_HOST X86EMUL_MODE_PROT32 |
@@ -249,13 +364,15 @@ struct x86_emulate_ctxt { | |||
249 | #define X86EMUL_MODE_HOST X86EMUL_MODE_PROT64 | 364 | #define X86EMUL_MODE_HOST X86EMUL_MODE_PROT64 |
250 | #endif | 365 | #endif |
251 | 366 | ||
252 | int x86_decode_insn(struct x86_emulate_ctxt *ctxt, | 367 | int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len); |
253 | struct x86_emulate_ops *ops); | 368 | #define EMULATION_FAILED -1 |
254 | int x86_emulate_insn(struct x86_emulate_ctxt *ctxt, | 369 | #define EMULATION_OK 0 |
255 | struct x86_emulate_ops *ops); | 370 | #define EMULATION_RESTART 1 |
371 | #define EMULATION_INTERCEPTED 2 | ||
372 | int x86_emulate_insn(struct x86_emulate_ctxt *ctxt); | ||
256 | int emulator_task_switch(struct x86_emulate_ctxt *ctxt, | 373 | int emulator_task_switch(struct x86_emulate_ctxt *ctxt, |
257 | struct x86_emulate_ops *ops, | ||
258 | u16 tss_selector, int reason, | 374 | u16 tss_selector, int reason, |
259 | bool has_error_code, u32 error_code); | 375 | bool has_error_code, u32 error_code); |
260 | 376 | int emulate_int_real(struct x86_emulate_ctxt *ctxt, | |
377 | struct x86_emulate_ops *ops, int irq); | ||
261 | #endif /* _ASM_X86_KVM_X86_EMULATE_H */ | 378 | #endif /* _ASM_X86_KVM_X86_EMULATE_H */ |