ia64/xen-unstable

view xen/include/asm-x86/x86_emulate.h @ 16989:92734271810a

vmx realmode: Emulate protected-mode transition while CS and SS have
bad selector values (bottom two bits non-zero).

Allows opensuse 10.3 install CD to boot. Unfortunately SUSE Linux 10.1
install CD still fails to work...

Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Feb 05 15:45:10 2008 +0000 (2008-02-05)
parents 5f3a178a80fc
children f1a107ec62b6
line source
1 /******************************************************************************
2 * x86_emulate.h
3 *
4 * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
5 *
6 * Copyright (c) 2005-2007 Keir Fraser
7 * Copyright (c) 2005-2007 XenSource Inc.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
24 #ifndef __X86_EMULATE_H__
25 #define __X86_EMULATE_H__
27 struct x86_emulate_ctxt;
29 /* Comprehensive enumeration of x86 segment registers. */
30 enum x86_segment {
31 /* General purpose. */
32 x86_seg_cs,
33 x86_seg_ss,
34 x86_seg_ds,
35 x86_seg_es,
36 x86_seg_fs,
37 x86_seg_gs,
38 /* System. */
39 x86_seg_tr,
40 x86_seg_ldtr,
41 x86_seg_gdtr,
42 x86_seg_idtr,
43 /*
44 * Dummy: used to emulate direct processor accesses to management
45 * structures (TSS, GDT, LDT, IDT, etc.) which use linear addressing
46 * (no segment component) and bypass usual segment- and page-level
47 * protection checks.
48 */
49 x86_seg_none
50 };
52 #define is_x86_user_segment(seg) ((unsigned)(seg) <= x86_seg_gs)
54 /*
55 * Attribute for segment selector. This is a copy of bit 40:47 & 52:55 of the
56 * segment descriptor. It happens to match the format of an AMD SVM VMCB.
57 */
58 typedef union segment_attributes {
59 u16 bytes;
60 struct
61 {
62 u16 type:4; /* 0; Bit 40-43 */
63 u16 s: 1; /* 4; Bit 44 */
64 u16 dpl: 2; /* 5; Bit 45-46 */
65 u16 p: 1; /* 7; Bit 47 */
66 u16 avl: 1; /* 8; Bit 52 */
67 u16 l: 1; /* 9; Bit 53 */
68 u16 db: 1; /* 10; Bit 54 */
69 u16 g: 1; /* 11; Bit 55 */
70 } fields;
71 } __attribute__ ((packed)) segment_attributes_t;
73 /*
74 * Full state of a segment register (visible and hidden portions).
75 * Again, this happens to match the format of an AMD SVM VMCB.
76 */
77 struct segment_register {
78 u16 sel;
79 segment_attributes_t attr;
80 u32 limit;
81 u64 base;
82 } __attribute__ ((packed));
84 /*
85 * Return codes from state-accessor functions and from x86_emulate().
86 */
87 /* Completed successfully. State modified appropriately. */
88 #define X86EMUL_OKAY 0
89 /* Unhandleable access or emulation. No state modified. */
90 #define X86EMUL_UNHANDLEABLE 1
91 /* Exception raised and requires delivery. */
92 #define X86EMUL_EXCEPTION 2
93 /* Retry the emulation for some reason. No state modified. */
94 #define X86EMUL_RETRY 3
95 /* (cmpxchg accessor): CMPXCHG failed. Maps to X86EMUL_RETRY in caller. */
96 #define X86EMUL_CMPXCHG_FAILED 3
98 /*
99 * These operations represent the instruction emulator's interface to memory.
100 *
101 * NOTES:
102 * 1. If the access fails (cannot emulate, or a standard access faults) then
103 * it is up to the memop to propagate the fault to the guest VM via
104 * some out-of-band mechanism, unknown to the emulator. The memop signals
105 * failure by returning X86EMUL_EXCEPTION to the emulator, which will
106 * then immediately bail.
107 * 2. Valid access sizes are 1, 2, 4 and 8 bytes. On x86/32 systems only
108 * cmpxchg8b_emulated need support 8-byte accesses.
109 * 3. The emulator cannot handle 64-bit mode emulation on an x86/32 system.
110 */
111 struct x86_emulate_ops
112 {
113 /*
114 * All functions:
115 * @ctxt: [IN ] Emulation context info as passed to the emulator.
116 * All memory-access functions:
117 * @seg: [IN ] Segment being dereferenced (specified as x86_seg_??).
118 * @offset:[IN ] Offset within segment.
119 * Read functions:
120 * @val: [OUT] Value read, zero-extended to 'ulong'.
121 * Write functions:
122 * @val: [IN ] Value to write (low-order bytes used as req'd).
123 * Variable-length access functions:
124 * @bytes: [IN ] Number of bytes to read or write.
125 */
127 /* read: Emulate a memory read. */
128 int (*read)(
129 enum x86_segment seg,
130 unsigned long offset,
131 unsigned long *val,
132 unsigned int bytes,
133 struct x86_emulate_ctxt *ctxt);
135 /*
136 * insn_fetch: Emulate fetch from instruction byte stream.
137 * Parameters are same as for 'read'. @seg is always x86_seg_cs.
138 */
139 int (*insn_fetch)(
140 enum x86_segment seg,
141 unsigned long offset,
142 unsigned long *val,
143 unsigned int bytes,
144 struct x86_emulate_ctxt *ctxt);
146 /* write: Emulate a memory write. */
147 int (*write)(
148 enum x86_segment seg,
149 unsigned long offset,
150 unsigned long val,
151 unsigned int bytes,
152 struct x86_emulate_ctxt *ctxt);
154 /*
155 * cmpxchg: Emulate an atomic (LOCKed) CMPXCHG operation.
156 * @old: [IN ] Value expected to be current at @addr.
157 * @new: [IN ] Value to write to @addr.
158 */
159 int (*cmpxchg)(
160 enum x86_segment seg,
161 unsigned long offset,
162 unsigned long old,
163 unsigned long new,
164 unsigned int bytes,
165 struct x86_emulate_ctxt *ctxt);
167 /*
168 * cmpxchg8b: Emulate an atomic (LOCKed) CMPXCHG8B operation.
169 * @old: [IN ] Value expected to be current at @addr.
170 * @new: [IN ] Value to write to @addr.
171 * NOTES:
172 * 1. This function is only ever called when emulating a real CMPXCHG8B.
173 * 2. This function is *never* called on x86/64 systems.
174 * 2. Not defining this function (i.e., specifying NULL) is equivalent
175 * to defining a function that always returns X86EMUL_UNHANDLEABLE.
176 */
177 int (*cmpxchg8b)(
178 enum x86_segment seg,
179 unsigned long offset,
180 unsigned long old_lo,
181 unsigned long old_hi,
182 unsigned long new_lo,
183 unsigned long new_hi,
184 struct x86_emulate_ctxt *ctxt);
186 /*
187 * rep_ins: Emulate INS: <src_port> -> <dst_seg:dst_offset>.
188 * @bytes_per_rep: [IN ] Bytes transferred per repetition.
189 * @reps: [IN ] Maximum repetitions to be emulated.
190 * [OUT] Number of repetitions actually emulated.
191 */
192 int (*rep_ins)(
193 uint16_t src_port,
194 enum x86_segment dst_seg,
195 unsigned long dst_offset,
196 unsigned int bytes_per_rep,
197 unsigned long *reps,
198 struct x86_emulate_ctxt *ctxt);
200 /*
201 * rep_outs: Emulate OUTS: <src_seg:src_offset> -> <dst_port>.
202 * @bytes_per_rep: [IN ] Bytes transferred per repetition.
203 * @reps: [IN ] Maximum repetitions to be emulated.
204 * [OUT] Number of repetitions actually emulated.
205 */
206 int (*rep_outs)(
207 enum x86_segment src_seg,
208 unsigned long src_offset,
209 uint16_t dst_port,
210 unsigned int bytes_per_rep,
211 unsigned long *reps,
212 struct x86_emulate_ctxt *ctxt);
214 /*
215 * rep_movs: Emulate MOVS: <src_seg:src_offset> -> <dst_seg:dst_offset>.
216 * @bytes_per_rep: [IN ] Bytes transferred per repetition.
217 * @reps: [IN ] Maximum repetitions to be emulated.
218 * [OUT] Number of repetitions actually emulated.
219 */
220 int (*rep_movs)(
221 enum x86_segment src_seg,
222 unsigned long src_offset,
223 enum x86_segment dst_seg,
224 unsigned long dst_offset,
225 unsigned int bytes_per_rep,
226 unsigned long *reps,
227 struct x86_emulate_ctxt *ctxt);
229 /*
230 * read_segment: Emulate a read of full context of a segment register.
231 * @reg: [OUT] Contents of segment register (visible and hidden state).
232 */
233 int (*read_segment)(
234 enum x86_segment seg,
235 struct segment_register *reg,
236 struct x86_emulate_ctxt *ctxt);
238 /*
239 * write_segment: Emulate a read of full context of a segment register.
240 * @reg: [OUT] Contents of segment register (visible and hidden state).
241 */
242 int (*write_segment)(
243 enum x86_segment seg,
244 struct segment_register *reg,
245 struct x86_emulate_ctxt *ctxt);
247 /*
248 * read_io: Read from I/O port(s).
249 * @port: [IN ] Base port for access.
250 */
251 int (*read_io)(
252 unsigned int port,
253 unsigned int bytes,
254 unsigned long *val,
255 struct x86_emulate_ctxt *ctxt);
257 /*
258 * write_io: Write to I/O port(s).
259 * @port: [IN ] Base port for access.
260 */
261 int (*write_io)(
262 unsigned int port,
263 unsigned int bytes,
264 unsigned long val,
265 struct x86_emulate_ctxt *ctxt);
267 /*
268 * read_cr: Read from control register.
269 * @reg: [IN ] Register to read (0-15).
270 */
271 int (*read_cr)(
272 unsigned int reg,
273 unsigned long *val,
274 struct x86_emulate_ctxt *ctxt);
276 /*
277 * write_cr: Write to control register.
278 * @reg: [IN ] Register to write (0-15).
279 */
280 int (*write_cr)(
281 unsigned int reg,
282 unsigned long val,
283 struct x86_emulate_ctxt *ctxt);
285 /*
286 * read_dr: Read from debug register.
287 * @reg: [IN ] Register to read (0-15).
288 */
289 int (*read_dr)(
290 unsigned int reg,
291 unsigned long *val,
292 struct x86_emulate_ctxt *ctxt);
294 /*
295 * write_dr: Write to debug register.
296 * @reg: [IN ] Register to write (0-15).
297 */
298 int (*write_dr)(
299 unsigned int reg,
300 unsigned long val,
301 struct x86_emulate_ctxt *ctxt);
303 /*
304 * read_msr: Read from model-specific register.
305 * @reg: [IN ] Register to read.
306 */
307 int (*read_msr)(
308 unsigned long reg,
309 uint64_t *val,
310 struct x86_emulate_ctxt *ctxt);
312 /*
313 * write_dr: Write to model-specific register.
314 * @reg: [IN ] Register to write.
315 */
316 int (*write_msr)(
317 unsigned long reg,
318 uint64_t val,
319 struct x86_emulate_ctxt *ctxt);
321 /* write_rflags: Modify privileged bits in RFLAGS. */
322 int (*write_rflags)(
323 unsigned long val,
324 struct x86_emulate_ctxt *ctxt);
326 /* wbinvd: Write-back and invalidate cache contents. */
327 int (*wbinvd)(
328 struct x86_emulate_ctxt *ctxt);
330 /* cpuid: Emulate CPUID via given set of EAX-EDX inputs/outputs. */
331 int (*cpuid)(
332 unsigned int *eax,
333 unsigned int *ebx,
334 unsigned int *ecx,
335 unsigned int *edx,
336 struct x86_emulate_ctxt *ctxt);
338 /* hlt: Emulate HLT. */
339 int (*hlt)(
340 struct x86_emulate_ctxt *ctxt);
342 /* inject_hw_exception */
343 int (*inject_hw_exception)(
344 uint8_t vector,
345 uint16_t error_code,
346 struct x86_emulate_ctxt *ctxt);
348 /* inject_sw_interrupt */
349 int (*inject_sw_interrupt)(
350 uint8_t vector,
351 uint8_t insn_len,
352 struct x86_emulate_ctxt *ctxt);
354 /* load_fpu_ctxt: Load emulated environment's FPU state onto processor. */
355 void (*load_fpu_ctxt)(
356 struct x86_emulate_ctxt *ctxt);
357 };
359 struct cpu_user_regs;
361 struct x86_emulate_ctxt
362 {
363 /* Register state before/after emulation. */
364 struct cpu_user_regs *regs;
366 /* Default address size in current execution mode (16, 32, or 64). */
367 unsigned int addr_size;
369 /* Stack pointer width in bits (16, 32 or 64). */
370 unsigned int sp_size;
371 };
373 /*
374 * x86_emulate: Emulate an instruction.
375 * Returns -1 on failure, 0 on success.
376 */
377 int
378 x86_emulate(
379 struct x86_emulate_ctxt *ctxt,
380 struct x86_emulate_ops *ops);
382 /*
383 * Given the 'reg' portion of a ModRM byte, and a register block, return a
384 * pointer into the block that addresses the relevant register.
385 * @highbyte_regs specifies whether to decode AH,CH,DH,BH.
386 */
387 void *
388 decode_register(
389 uint8_t modrm_reg, struct cpu_user_regs *regs, int highbyte_regs);
391 #endif /* __X86_EMULATE_H__ */