1
/******************************************************************************
4
* Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
6
* Copyright (c) 2005-2007 Keir Fraser
7
* Copyright (c) 2005-2007 XenSource Inc.
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.
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.
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
24
#ifndef __X86_EMULATE_H__
25
#define __X86_EMULATE_H__
27
struct x86_emulate_ctxt;
29
/* Comprehensive enumeration of x86 segment registers. */
31
/* General purpose. */
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
52
#define is_x86_user_segment(seg) ((unsigned)(seg) <= x86_seg_gs)
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.
58
typedef union segment_attributes {
62
uint16_t type:4; /* 0; Bit 40-43 */
63
uint16_t s: 1; /* 4; Bit 44 */
64
uint16_t dpl: 2; /* 5; Bit 45-46 */
65
uint16_t p: 1; /* 7; Bit 47 */
66
uint16_t avl: 1; /* 8; Bit 52 */
67
uint16_t l: 1; /* 9; Bit 53 */
68
uint16_t db: 1; /* 10; Bit 54 */
69
uint16_t g: 1; /* 11; Bit 55 */
72
} __attribute__ ((packed)) segment_attributes_t;
75
* Full state of a segment register (visible and hidden portions).
76
* Again, this happens to match the format of an AMD SVM VMCB.
78
struct segment_register {
80
segment_attributes_t attr;
83
} __attribute__ ((packed));
86
* Return codes from state-accessor functions and from x86_emulate().
88
/* Completed successfully. State modified appropriately. */
89
#define X86EMUL_OKAY 0
90
/* Unhandleable access or emulation. No state modified. */
91
#define X86EMUL_UNHANDLEABLE 1
92
/* Exception raised and requires delivery. */
93
#define X86EMUL_EXCEPTION 2
94
/* Retry the emulation for some reason. No state modified. */
95
#define X86EMUL_RETRY 3
96
/* (cmpxchg accessor): CMPXCHG failed. Maps to X86EMUL_RETRY in caller. */
97
#define X86EMUL_CMPXCHG_FAILED 3
99
/* FPU sub-types which may be requested via ->get_fpu(). */
100
enum x86_emulate_fpu_type {
101
X86EMUL_FPU_fpu, /* Standard FPU coprocessor instruction set */
102
X86EMUL_FPU_mmx /* MMX instruction set (%mm0-%mm7) */
106
* These operations represent the instruction emulator's interface to memory,
107
* I/O ports, privileged state... pretty much everything other than GPRs.
110
* 1. If the access fails (cannot emulate, or a standard access faults) then
111
* it is up to the memop to propagate the fault to the guest VM via
112
* some out-of-band mechanism, unknown to the emulator. The memop signals
113
* failure by returning X86EMUL_EXCEPTION to the emulator, which will
114
* then immediately bail.
115
* 2. The emulator cannot handle 64-bit mode emulation on an x86/32 system.
117
struct x86_emulate_ops
121
* @ctxt: [IN ] Emulation context info as passed to the emulator.
122
* All memory-access functions:
123
* @seg: [IN ] Segment being dereferenced (specified as x86_seg_??).
124
* @offset:[IN ] Offset within segment.
125
* @p_data:[IN ] Pointer to i/o data buffer (length is @bytes)
127
* @val: [OUT] Value read, zero-extended to 'ulong'.
129
* @val: [IN ] Value to write (low-order bytes used as req'd).
130
* Variable-length access functions:
131
* @bytes: [IN ] Number of bytes to read or write. Valid access sizes are
132
* 1, 2, 4 and 8 (x86/64 only) bytes, unless otherwise
137
* read: Emulate a memory read.
138
* @bytes: Access length (0 < @bytes < 4096).
141
enum x86_segment seg,
142
unsigned long offset,
145
struct x86_emulate_ctxt *ctxt);
148
* insn_fetch: Emulate fetch from instruction byte stream.
149
* Parameters are same as for 'read'. @seg is always x86_seg_cs.
152
enum x86_segment seg,
153
unsigned long offset,
156
struct x86_emulate_ctxt *ctxt);
159
* write: Emulate a memory write.
160
* @bytes: Access length (0 < @bytes < 4096).
163
enum x86_segment seg,
164
unsigned long offset,
167
struct x86_emulate_ctxt *ctxt);
170
* cmpxchg: Emulate an atomic (LOCKed) CMPXCHG operation.
171
* @p_old: [IN ] Pointer to value expected to be current at @addr.
172
* @p_new: [IN ] Pointer to value to write to @addr.
173
* @bytes: [IN ] Operation size (up to 8 (x86/32) or 16 (x86/64) bytes).
176
enum x86_segment seg,
177
unsigned long offset,
181
struct x86_emulate_ctxt *ctxt);
184
* rep_ins: Emulate INS: <src_port> -> <dst_seg:dst_offset>.
185
* @bytes_per_rep: [IN ] Bytes transferred per repetition.
186
* @reps: [IN ] Maximum repetitions to be emulated.
187
* [OUT] Number of repetitions actually emulated.
191
enum x86_segment dst_seg,
192
unsigned long dst_offset,
193
unsigned int bytes_per_rep,
195
struct x86_emulate_ctxt *ctxt);
198
* rep_outs: Emulate OUTS: <src_seg:src_offset> -> <dst_port>.
199
* @bytes_per_rep: [IN ] Bytes transferred per repetition.
200
* @reps: [IN ] Maximum repetitions to be emulated.
201
* [OUT] Number of repetitions actually emulated.
204
enum x86_segment src_seg,
205
unsigned long src_offset,
207
unsigned int bytes_per_rep,
209
struct x86_emulate_ctxt *ctxt);
212
* rep_movs: Emulate MOVS: <src_seg:src_offset> -> <dst_seg:dst_offset>.
213
* @bytes_per_rep: [IN ] Bytes transferred per repetition.
214
* @reps: [IN ] Maximum repetitions to be emulated.
215
* [OUT] Number of repetitions actually emulated.
218
enum x86_segment src_seg,
219
unsigned long src_offset,
220
enum x86_segment dst_seg,
221
unsigned long dst_offset,
222
unsigned int bytes_per_rep,
224
struct x86_emulate_ctxt *ctxt);
227
* read_segment: Emulate a read of full context of a segment register.
228
* @reg: [OUT] Contents of segment register (visible and hidden state).
231
enum x86_segment seg,
232
struct segment_register *reg,
233
struct x86_emulate_ctxt *ctxt);
236
* write_segment: Emulate a read of full context of a segment register.
237
* @reg: [OUT] Contents of segment register (visible and hidden state).
239
int (*write_segment)(
240
enum x86_segment seg,
241
struct segment_register *reg,
242
struct x86_emulate_ctxt *ctxt);
245
* read_io: Read from I/O port(s).
246
* @port: [IN ] Base port for access.
252
struct x86_emulate_ctxt *ctxt);
255
* write_io: Write to I/O port(s).
256
* @port: [IN ] Base port for access.
262
struct x86_emulate_ctxt *ctxt);
265
* read_cr: Read from control register.
266
* @reg: [IN ] Register to read (0-15).
271
struct x86_emulate_ctxt *ctxt);
274
* write_cr: Write to control register.
275
* @reg: [IN ] Register to write (0-15).
280
struct x86_emulate_ctxt *ctxt);
283
* read_dr: Read from debug register.
284
* @reg: [IN ] Register to read (0-15).
289
struct x86_emulate_ctxt *ctxt);
292
* write_dr: Write to debug register.
293
* @reg: [IN ] Register to write (0-15).
298
struct x86_emulate_ctxt *ctxt);
301
* read_msr: Read from model-specific register.
302
* @reg: [IN ] Register to read.
307
struct x86_emulate_ctxt *ctxt);
310
* write_dr: Write to model-specific register.
311
* @reg: [IN ] Register to write.
316
struct x86_emulate_ctxt *ctxt);
318
/* wbinvd: Write-back and invalidate cache contents. */
320
struct x86_emulate_ctxt *ctxt);
322
/* cpuid: Emulate CPUID via given set of EAX-EDX inputs/outputs. */
328
struct x86_emulate_ctxt *ctxt);
330
/* inject_hw_exception */
331
int (*inject_hw_exception)(
334
struct x86_emulate_ctxt *ctxt);
336
/* inject_sw_interrupt */
337
int (*inject_sw_interrupt)(
340
struct x86_emulate_ctxt *ctxt);
343
* get_fpu: Load emulated environment's FPU state onto processor.
344
* @exn_callback: On any FPU or SIMD exception, pass control to
345
* (*exception_callback)(exception_callback_arg, regs).
348
void (*exception_callback)(void *, struct cpu_user_regs *),
349
void *exception_callback_arg,
350
enum x86_emulate_fpu_type type,
351
struct x86_emulate_ctxt *ctxt);
353
/* put_fpu: Relinquish the FPU. Unhook from FPU/SIMD exception handlers. */
355
struct x86_emulate_ctxt *ctxt);
357
/* invlpg: Invalidate paging structures which map addressed byte. */
359
enum x86_segment seg,
360
unsigned long offset,
361
struct x86_emulate_ctxt *ctxt);
364
struct cpu_user_regs;
366
struct x86_emulate_ctxt
368
/* Register state before/after emulation. */
369
struct cpu_user_regs *regs;
371
/* Default address size in current execution mode (16, 32, or 64). */
372
unsigned int addr_size;
374
/* Stack pointer width in bits (16, 32 or 64). */
375
unsigned int sp_size;
377
/* Set this if writes may have side effects. */
378
uint8_t force_writeback;
380
/* Retirement state, set by the emulator (valid only on X86EMUL_OKAY). */
383
uint8_t hlt:1; /* Instruction HLTed. */
384
uint8_t mov_ss:1; /* Instruction sets MOV-SS irq shadow. */
385
uint8_t sti:1; /* Instruction sets STI irq shadow. */
392
* x86_emulate: Emulate an instruction.
393
* Returns -1 on failure, 0 on success.
397
struct x86_emulate_ctxt *ctxt,
398
const struct x86_emulate_ops *ops);
401
* Given the 'reg' portion of a ModRM byte, and a register block, return a
402
* pointer into the block that addresses the relevant register.
403
* @highbyte_regs specifies whether to decode AH,CH,DH,BH.
407
uint8_t modrm_reg, struct cpu_user_regs *regs, int highbyte_regs);
409
#endif /* __X86_EMULATE_H__ */