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 AK |
16 | struct x86_emulate_ctxt; |
17 | ||
da9cb575 AK |
18 | struct x86_exception { |
19 | u8 vector; | |
20 | bool error_code_valid; | |
21 | u16 error_code; | |
22 | }; | |
23 | ||
6aa8b732 AK |
24 | /* |
25 | * x86_emulate_ops: | |
26 | * | |
27 | * These operations represent the instruction emulator's interface to memory. | |
28 | * There are two categories of operation: those that act on ordinary memory | |
29 | * regions (*_std), and those that act on memory regions known to require | |
30 | * special treatment or emulation (*_emulated). | |
31 | * | |
32 | * The emulator assumes that an instruction accesses only one 'emulated memory' | |
33 | * location, that this location is the given linear faulting address (cr2), and | |
34 | * that this is one of the instruction's data operands. Instruction fetches and | |
35 | * stack operations are assumed never to access emulated memory. The emulator | |
36 | * automatically deduces which operand of a string-move operation is accessing | |
37 | * emulated memory, and assumes that the other operand accesses normal memory. | |
38 | * | |
39 | * NOTES: | |
40 | * 1. The emulator isn't very smart about emulated vs. standard memory. | |
41 | * 'Emulated memory' access addresses should be checked for sanity. | |
42 | * 'Normal memory' accesses may fault, and the caller must arrange to | |
43 | * detect and handle reentrancy into the emulator via recursive faults. | |
44 | * Accesses may be unaligned and may cross page boundaries. | |
45 | * 2. If the access fails (cannot emulate, or a standard access faults) then | |
46 | * it is up to the memop to propagate the fault to the guest VM via | |
47 | * some out-of-band mechanism, unknown to the emulator. The memop signals | |
48 | * failure by returning X86EMUL_PROPAGATE_FAULT to the emulator, which will | |
49 | * then immediately bail. | |
50 | * 3. Valid access sizes are 1, 2, 4 and 8 bytes. On x86/32 systems only | |
51 | * cmpxchg8b_emulated need support 8-byte accesses. | |
52 | * 4. The emulator cannot handle 64-bit mode emulation on an x86/32 system. | |
53 | */ | |
54 | /* Access completed successfully: continue emulation as normal. */ | |
55 | #define X86EMUL_CONTINUE 0 | |
56 | /* Access is unhandleable: bail from emulation and return error to caller. */ | |
57 | #define X86EMUL_UNHANDLEABLE 1 | |
58 | /* Terminate emulation but return success to the caller. */ | |
59 | #define X86EMUL_PROPAGATE_FAULT 2 /* propagate a generated fault to guest */ | |
e680080e GN |
60 | #define X86EMUL_RETRY_INSTR 3 /* retry the instruction for some reason */ |
61 | #define X86EMUL_CMPXCHG_FAILED 4 /* cmpxchg did not see expected value */ | |
c3cd7ffa | 62 | #define X86EMUL_IO_NEEDED 5 /* IO is needed to complete emulation */ |
e680080e | 63 | |
6aa8b732 AK |
64 | struct x86_emulate_ops { |
65 | /* | |
66 | * read_std: Read bytes of standard (non-emulated/special) memory. | |
1871c602 | 67 | * Used for descriptor reading. |
6aa8b732 AK |
68 | * @addr: [IN ] Linear address from which to read. |
69 | * @val: [OUT] Value read from memory, zero-extended to 'u_long'. | |
70 | * @bytes: [IN ] Number of bytes to read from memory. | |
71 | */ | |
4c690a1e | 72 | int (*read_std)(unsigned long addr, void *val, |
1871c602 GN |
73 | unsigned int bytes, struct kvm_vcpu *vcpu, u32 *error); |
74 | ||
2dafc6c2 GN |
75 | /* |
76 | * write_std: Write bytes of standard (non-emulated/special) memory. | |
77 | * Used for descriptor writing. | |
78 | * @addr: [IN ] Linear address to which to write. | |
79 | * @val: [OUT] Value write to memory, zero-extended to 'u_long'. | |
80 | * @bytes: [IN ] Number of bytes to write to memory. | |
81 | */ | |
82 | int (*write_std)(unsigned long addr, void *val, | |
83 | unsigned int bytes, struct kvm_vcpu *vcpu, u32 *error); | |
1871c602 GN |
84 | /* |
85 | * fetch: Read bytes of standard (non-emulated/special) memory. | |
86 | * Used for instruction fetch. | |
87 | * @addr: [IN ] Linear address from which to read. | |
88 | * @val: [OUT] Value read from memory, zero-extended to 'u_long'. | |
89 | * @bytes: [IN ] Number of bytes to read from memory. | |
90 | */ | |
91 | int (*fetch)(unsigned long addr, void *val, | |
92 | unsigned int bytes, struct kvm_vcpu *vcpu, u32 *error); | |
6aa8b732 AK |
93 | |
94 | /* | |
95 | * read_emulated: Read bytes from emulated/special memory area. | |
96 | * @addr: [IN ] Linear address from which to read. | |
97 | * @val: [OUT] Value read from memory, zero-extended to 'u_long'. | |
98 | * @bytes: [IN ] Number of bytes to read from memory. | |
99 | */ | |
0c7825e6 JP |
100 | int (*read_emulated)(unsigned long addr, |
101 | void *val, | |
102 | unsigned int bytes, | |
8fe681e9 | 103 | unsigned int *error, |
0c7825e6 | 104 | struct kvm_vcpu *vcpu); |
6aa8b732 AK |
105 | |
106 | /* | |
0d178975 | 107 | * write_emulated: Write bytes to emulated/special memory area. |
6aa8b732 AK |
108 | * @addr: [IN ] Linear address to which to write. |
109 | * @val: [IN ] Value to write to memory (low-order bytes used as | |
110 | * required). | |
111 | * @bytes: [IN ] Number of bytes to write to memory. | |
112 | */ | |
0c7825e6 JP |
113 | int (*write_emulated)(unsigned long addr, |
114 | const void *val, | |
115 | unsigned int bytes, | |
8fe681e9 | 116 | unsigned int *error, |
0c7825e6 | 117 | struct kvm_vcpu *vcpu); |
6aa8b732 AK |
118 | |
119 | /* | |
120 | * cmpxchg_emulated: Emulate an atomic (LOCKed) CMPXCHG operation on an | |
121 | * emulated/special memory area. | |
122 | * @addr: [IN ] Linear address to access. | |
123 | * @old: [IN ] Value expected to be current at @addr. | |
124 | * @new: [IN ] Value to write to @addr. | |
125 | * @bytes: [IN ] Number of bytes to access using CMPXCHG. | |
126 | */ | |
0c7825e6 JP |
127 | int (*cmpxchg_emulated)(unsigned long addr, |
128 | const void *old, | |
129 | const void *new, | |
130 | unsigned int bytes, | |
8fe681e9 | 131 | unsigned int *error, |
0c7825e6 | 132 | struct kvm_vcpu *vcpu); |
cf8f70bf GN |
133 | |
134 | int (*pio_in_emulated)(int size, unsigned short port, void *val, | |
135 | unsigned int count, struct kvm_vcpu *vcpu); | |
136 | ||
137 | int (*pio_out_emulated)(int size, unsigned short port, const void *val, | |
138 | unsigned int count, struct kvm_vcpu *vcpu); | |
139 | ||
2dafc6c2 GN |
140 | bool (*get_cached_descriptor)(struct desc_struct *desc, |
141 | int seg, struct kvm_vcpu *vcpu); | |
142 | void (*set_cached_descriptor)(struct desc_struct *desc, | |
143 | int seg, struct kvm_vcpu *vcpu); | |
144 | u16 (*get_segment_selector)(int seg, struct kvm_vcpu *vcpu); | |
145 | void (*set_segment_selector)(u16 sel, int seg, struct kvm_vcpu *vcpu); | |
5951c442 | 146 | unsigned long (*get_cached_segment_base)(int seg, struct kvm_vcpu *vcpu); |
2dafc6c2 | 147 | void (*get_gdt)(struct desc_ptr *dt, struct kvm_vcpu *vcpu); |
160ce1f1 | 148 | void (*get_idt)(struct desc_ptr *dt, struct kvm_vcpu *vcpu); |
52a46617 | 149 | ulong (*get_cr)(int cr, struct kvm_vcpu *vcpu); |
0f12244f | 150 | int (*set_cr)(int cr, ulong val, struct kvm_vcpu *vcpu); |
9c537244 | 151 | int (*cpl)(struct kvm_vcpu *vcpu); |
35aa5375 GN |
152 | int (*get_dr)(int dr, unsigned long *dest, struct kvm_vcpu *vcpu); |
153 | int (*set_dr)(int dr, unsigned long value, struct kvm_vcpu *vcpu); | |
3fb1b5db GN |
154 | int (*set_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 data); |
155 | int (*get_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata); | |
6aa8b732 AK |
156 | }; |
157 | ||
e4e03ded LV |
158 | /* Type, address-of, and value of an instruction's operand. */ |
159 | struct operand { | |
a01af5ec | 160 | enum { OP_REG, OP_MEM, OP_IMM, OP_NONE } type; |
e4e03ded | 161 | unsigned int bytes; |
16518d5a AK |
162 | union { |
163 | unsigned long orig_val; | |
164 | u64 orig_val64; | |
165 | }; | |
1a6440ae AK |
166 | union { |
167 | unsigned long *reg; | |
90de84f5 AK |
168 | struct segmented_address { |
169 | ulong ea; | |
170 | unsigned seg; | |
171 | } mem; | |
1a6440ae | 172 | } addr; |
414e6277 GN |
173 | union { |
174 | unsigned long val; | |
16518d5a | 175 | u64 val64; |
414e6277 GN |
176 | char valptr[sizeof(unsigned long) + 2]; |
177 | }; | |
e4e03ded LV |
178 | }; |
179 | ||
62266869 AK |
180 | struct fetch_cache { |
181 | u8 data[15]; | |
182 | unsigned long start; | |
183 | unsigned long end; | |
184 | }; | |
185 | ||
7b262e90 GN |
186 | struct read_cache { |
187 | u8 data[1024]; | |
188 | unsigned long pos; | |
189 | unsigned long end; | |
190 | }; | |
191 | ||
e4e03ded LV |
192 | struct decode_cache { |
193 | u8 twobyte; | |
194 | u8 b; | |
195 | u8 lock_prefix; | |
196 | u8 rep_prefix; | |
197 | u8 op_bytes; | |
198 | u8 ad_bytes; | |
33615aa9 | 199 | u8 rex_prefix; |
e4e03ded | 200 | struct operand src; |
0dc8d10f | 201 | struct operand src2; |
e4e03ded | 202 | struct operand dst; |
7a5b56df AK |
203 | bool has_seg_override; |
204 | u8 seg_override; | |
e4e03ded | 205 | unsigned int d; |
ef65c889 | 206 | int (*execute)(struct x86_emulate_ctxt *ctxt); |
e4e03ded | 207 | unsigned long regs[NR_VCPU_REGS]; |
063db061 | 208 | unsigned long eip; |
e4e03ded LV |
209 | /* modrm */ |
210 | u8 modrm; | |
211 | u8 modrm_mod; | |
212 | u8 modrm_reg; | |
213 | u8 modrm_rm; | |
09ee57cd | 214 | u8 modrm_seg; |
f5b4edcd | 215 | bool rip_relative; |
62266869 | 216 | struct fetch_cache fetch; |
7b262e90 | 217 | struct read_cache io_read; |
9de41573 | 218 | struct read_cache mem_read; |
e4e03ded LV |
219 | }; |
220 | ||
6aa8b732 | 221 | struct x86_emulate_ctxt { |
9aabc88f AK |
222 | struct x86_emulate_ops *ops; |
223 | ||
6aa8b732 AK |
224 | /* Register state before/after emulation. */ |
225 | struct kvm_vcpu *vcpu; | |
226 | ||
6aa8b732 | 227 | unsigned long eflags; |
063db061 | 228 | unsigned long eip; /* eip before instruction emulation */ |
6aa8b732 AK |
229 | /* Emulated execution mode, represented by an X86EMUL_MODE value. */ |
230 | int mode; | |
7a5b56df | 231 | u32 cs_base; |
e4e03ded | 232 | |
310b5d30 GC |
233 | /* interruptibility state, as a result of execution of STI or MOV SS */ |
234 | int interruptibility; | |
235 | ||
4fc40f07 | 236 | bool perm_ok; /* do not check permissions if true */ |
54b8486f | 237 | |
da9cb575 AK |
238 | bool have_exception; |
239 | struct x86_exception exception; | |
54b8486f | 240 | |
e4e03ded | 241 | /* decode cache */ |
e4e03ded | 242 | struct decode_cache decode; |
6aa8b732 AK |
243 | }; |
244 | ||
90e0a28f | 245 | /* Repeat String Operation Prefix */ |
d73fa29a SY |
246 | #define REPE_PREFIX 1 |
247 | #define REPNE_PREFIX 2 | |
90e0a28f | 248 | |
6aa8b732 AK |
249 | /* Execution mode, passed to the emulator. */ |
250 | #define X86EMUL_MODE_REAL 0 /* Real mode. */ | |
a0044755 | 251 | #define X86EMUL_MODE_VM86 1 /* Virtual 8086 mode. */ |
6aa8b732 AK |
252 | #define X86EMUL_MODE_PROT16 2 /* 16-bit protected mode. */ |
253 | #define X86EMUL_MODE_PROT32 4 /* 32-bit protected mode. */ | |
254 | #define X86EMUL_MODE_PROT64 8 /* 64-bit (long) mode. */ | |
255 | ||
256 | /* Host execution mode. */ | |
d73fa29a | 257 | #if defined(CONFIG_X86_32) |
6aa8b732 | 258 | #define X86EMUL_MODE_HOST X86EMUL_MODE_PROT32 |
05b3e0c2 | 259 | #elif defined(CONFIG_X86_64) |
6aa8b732 AK |
260 | #define X86EMUL_MODE_HOST X86EMUL_MODE_PROT64 |
261 | #endif | |
262 | ||
9aabc88f | 263 | int x86_decode_insn(struct x86_emulate_ctxt *ctxt); |
d2ddd1c4 GN |
264 | #define EMULATION_FAILED -1 |
265 | #define EMULATION_OK 0 | |
266 | #define EMULATION_RESTART 1 | |
9aabc88f | 267 | int x86_emulate_insn(struct x86_emulate_ctxt *ctxt); |
38ba30ba | 268 | int emulator_task_switch(struct x86_emulate_ctxt *ctxt, |
e269fb21 JK |
269 | u16 tss_selector, int reason, |
270 | bool has_error_code, u32 error_code); | |
4ab8e024 MG |
271 | int emulate_int_real(struct x86_emulate_ctxt *ctxt, |
272 | struct x86_emulate_ops *ops, int irq); | |
1965aae3 | 273 | #endif /* _ASM_X86_KVM_X86_EMULATE_H */ |