~ubuntu-branches/ubuntu/utopic/xen/utopic

« back to all changes in this revision

Viewing changes to xen/arch/x86/x86_emulate/x86_emulate.h

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2010-05-06 15:47:38 UTC
  • mto: (1.3.1) (15.1.1 sid) (4.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100506154738-agoz0rlafrh1fnq7
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 */
 
23
 
 
24
#ifndef __X86_EMULATE_H__
 
25
#define __X86_EMULATE_H__
 
26
 
 
27
struct x86_emulate_ctxt;
 
28
 
 
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
};
 
51
 
 
52
#define is_x86_user_segment(seg) ((unsigned)(seg) <= x86_seg_gs)
 
53
 
 
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
    uint16_t bytes;
 
60
    struct
 
61
    {
 
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 */
 
70
        uint16_t pad: 4;
 
71
    } fields;
 
72
} __attribute__ ((packed)) segment_attributes_t;
 
73
 
 
74
/*
 
75
 * Full state of a segment register (visible and hidden portions).
 
76
 * Again, this happens to match the format of an AMD SVM VMCB.
 
77
 */
 
78
struct segment_register {
 
79
    uint16_t   sel;
 
80
    segment_attributes_t attr;
 
81
    uint32_t   limit;
 
82
    uint64_t   base;
 
83
} __attribute__ ((packed));
 
84
 
 
85
/*
 
86
 * Return codes from state-accessor functions and from x86_emulate().
 
87
 */
 
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
 
98
 
 
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) */
 
103
};
 
104
 
 
105
/*
 
106
 * These operations represent the instruction emulator's interface to memory,
 
107
 * I/O ports, privileged state... pretty much everything other than GPRs.
 
108
 * 
 
109
 * NOTES:
 
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.
 
116
 */
 
117
struct x86_emulate_ops
 
118
{
 
119
    /*
 
120
     * All functions:
 
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)
 
126
     * Read functions:
 
127
     *  @val:   [OUT] Value read, zero-extended to 'ulong'.
 
128
     * Write functions:
 
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
 
133
     *                stated.
 
134
     */
 
135
 
 
136
    /*
 
137
     * read: Emulate a memory read.
 
138
     *  @bytes: Access length (0 < @bytes < 4096).
 
139
     */
 
140
    int (*read)(
 
141
        enum x86_segment seg,
 
142
        unsigned long offset,
 
143
        void *p_data,
 
144
        unsigned int bytes,
 
145
        struct x86_emulate_ctxt *ctxt);
 
146
 
 
147
    /*
 
148
     * insn_fetch: Emulate fetch from instruction byte stream.
 
149
     *  Parameters are same as for 'read'. @seg is always x86_seg_cs.
 
150
     */
 
151
    int (*insn_fetch)(
 
152
        enum x86_segment seg,
 
153
        unsigned long offset,
 
154
        void *p_data,
 
155
        unsigned int bytes,
 
156
        struct x86_emulate_ctxt *ctxt);
 
157
 
 
158
    /*
 
159
     * write: Emulate a memory write.
 
160
     *  @bytes: Access length (0 < @bytes < 4096).
 
161
     */
 
162
    int (*write)(
 
163
        enum x86_segment seg,
 
164
        unsigned long offset,
 
165
        void *p_data,
 
166
        unsigned int bytes,
 
167
        struct x86_emulate_ctxt *ctxt);
 
168
 
 
169
    /*
 
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).
 
174
     */
 
175
    int (*cmpxchg)(
 
176
        enum x86_segment seg,
 
177
        unsigned long offset,
 
178
        void *p_old,
 
179
        void *p_new,
 
180
        unsigned int bytes,
 
181
        struct x86_emulate_ctxt *ctxt);
 
182
 
 
183
    /*
 
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.
 
188
     */
 
189
    int (*rep_ins)(
 
190
        uint16_t src_port,
 
191
        enum x86_segment dst_seg,
 
192
        unsigned long dst_offset,
 
193
        unsigned int bytes_per_rep,
 
194
        unsigned long *reps,
 
195
        struct x86_emulate_ctxt *ctxt);
 
196
 
 
197
    /*
 
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.
 
202
     */
 
203
    int (*rep_outs)(
 
204
        enum x86_segment src_seg,
 
205
        unsigned long src_offset,
 
206
        uint16_t dst_port,
 
207
        unsigned int bytes_per_rep,
 
208
        unsigned long *reps,
 
209
        struct x86_emulate_ctxt *ctxt);
 
210
 
 
211
    /*
 
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.
 
216
     */
 
217
    int (*rep_movs)(
 
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,
 
223
        unsigned long *reps,
 
224
        struct x86_emulate_ctxt *ctxt);
 
225
 
 
226
    /*
 
227
     * read_segment: Emulate a read of full context of a segment register.
 
228
     *  @reg:   [OUT] Contents of segment register (visible and hidden state).
 
229
     */
 
230
    int (*read_segment)(
 
231
        enum x86_segment seg,
 
232
        struct segment_register *reg,
 
233
        struct x86_emulate_ctxt *ctxt);
 
234
 
 
235
    /*
 
236
     * write_segment: Emulate a read of full context of a segment register.
 
237
     *  @reg:   [OUT] Contents of segment register (visible and hidden state).
 
238
     */
 
239
    int (*write_segment)(
 
240
        enum x86_segment seg,
 
241
        struct segment_register *reg,
 
242
        struct x86_emulate_ctxt *ctxt);
 
243
 
 
244
    /*
 
245
     * read_io: Read from I/O port(s).
 
246
     *  @port:  [IN ] Base port for access.
 
247
     */
 
248
    int (*read_io)(
 
249
        unsigned int port,
 
250
        unsigned int bytes,
 
251
        unsigned long *val,
 
252
        struct x86_emulate_ctxt *ctxt);
 
253
 
 
254
    /*
 
255
     * write_io: Write to I/O port(s).
 
256
     *  @port:  [IN ] Base port for access.
 
257
     */
 
258
    int (*write_io)(
 
259
        unsigned int port,
 
260
        unsigned int bytes,
 
261
        unsigned long val,
 
262
        struct x86_emulate_ctxt *ctxt);
 
263
 
 
264
    /*
 
265
     * read_cr: Read from control register.
 
266
     *  @reg:   [IN ] Register to read (0-15).
 
267
     */
 
268
    int (*read_cr)(
 
269
        unsigned int reg,
 
270
        unsigned long *val,
 
271
        struct x86_emulate_ctxt *ctxt);
 
272
 
 
273
    /*
 
274
     * write_cr: Write to control register.
 
275
     *  @reg:   [IN ] Register to write (0-15).
 
276
     */
 
277
    int (*write_cr)(
 
278
        unsigned int reg,
 
279
        unsigned long val,
 
280
        struct x86_emulate_ctxt *ctxt);
 
281
 
 
282
    /*
 
283
     * read_dr: Read from debug register.
 
284
     *  @reg:   [IN ] Register to read (0-15).
 
285
     */
 
286
    int (*read_dr)(
 
287
        unsigned int reg,
 
288
        unsigned long *val,
 
289
        struct x86_emulate_ctxt *ctxt);
 
290
 
 
291
    /*
 
292
     * write_dr: Write to debug register.
 
293
     *  @reg:   [IN ] Register to write (0-15).
 
294
     */
 
295
    int (*write_dr)(
 
296
        unsigned int reg,
 
297
        unsigned long val,
 
298
        struct x86_emulate_ctxt *ctxt);
 
299
 
 
300
    /*
 
301
     * read_msr: Read from model-specific register.
 
302
     *  @reg:   [IN ] Register to read.
 
303
     */
 
304
    int (*read_msr)(
 
305
        unsigned long reg,
 
306
        uint64_t *val,
 
307
        struct x86_emulate_ctxt *ctxt);
 
308
 
 
309
    /*
 
310
     * write_dr: Write to model-specific register.
 
311
     *  @reg:   [IN ] Register to write.
 
312
     */
 
313
    int (*write_msr)(
 
314
        unsigned long reg,
 
315
        uint64_t val,
 
316
        struct x86_emulate_ctxt *ctxt);
 
317
 
 
318
    /* wbinvd: Write-back and invalidate cache contents. */
 
319
    int (*wbinvd)(
 
320
        struct x86_emulate_ctxt *ctxt);
 
321
 
 
322
    /* cpuid: Emulate CPUID via given set of EAX-EDX inputs/outputs. */
 
323
    int (*cpuid)(
 
324
        unsigned int *eax,
 
325
        unsigned int *ebx,
 
326
        unsigned int *ecx,
 
327
        unsigned int *edx,
 
328
        struct x86_emulate_ctxt *ctxt);
 
329
 
 
330
    /* inject_hw_exception */
 
331
    int (*inject_hw_exception)(
 
332
        uint8_t vector,
 
333
        int32_t error_code,
 
334
        struct x86_emulate_ctxt *ctxt);
 
335
 
 
336
    /* inject_sw_interrupt */
 
337
    int (*inject_sw_interrupt)(
 
338
        uint8_t vector,
 
339
        uint8_t insn_len,
 
340
        struct x86_emulate_ctxt *ctxt);
 
341
 
 
342
    /*
 
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).
 
346
     */
 
347
    int (*get_fpu)(
 
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);
 
352
 
 
353
    /* put_fpu: Relinquish the FPU. Unhook from FPU/SIMD exception handlers. */
 
354
    void (*put_fpu)(
 
355
        struct x86_emulate_ctxt *ctxt);
 
356
 
 
357
    /* invlpg: Invalidate paging structures which map addressed byte. */
 
358
    int (*invlpg)(
 
359
        enum x86_segment seg,
 
360
        unsigned long offset,
 
361
        struct x86_emulate_ctxt *ctxt);
 
362
};
 
363
 
 
364
struct cpu_user_regs;
 
365
 
 
366
struct x86_emulate_ctxt
 
367
{
 
368
    /* Register state before/after emulation. */
 
369
    struct cpu_user_regs *regs;
 
370
 
 
371
    /* Default address size in current execution mode (16, 32, or 64). */
 
372
    unsigned int addr_size;
 
373
 
 
374
    /* Stack pointer width in bits (16, 32 or 64). */
 
375
    unsigned int sp_size;
 
376
 
 
377
    /* Set this if writes may have side effects. */
 
378
    uint8_t force_writeback;
 
379
 
 
380
    /* Retirement state, set by the emulator (valid only on X86EMUL_OKAY). */
 
381
    union {
 
382
        struct {
 
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. */
 
386
        } flags;
 
387
        uint8_t byte;
 
388
    } retire;
 
389
};
 
390
 
 
391
/*
 
392
 * x86_emulate: Emulate an instruction.
 
393
 * Returns -1 on failure, 0 on success.
 
394
 */
 
395
int
 
396
x86_emulate(
 
397
    struct x86_emulate_ctxt *ctxt,
 
398
    const struct x86_emulate_ops *ops);
 
399
 
 
400
/*
 
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.
 
404
 */
 
405
void *
 
406
decode_register(
 
407
    uint8_t modrm_reg, struct cpu_user_regs *regs, int highbyte_regs);
 
408
 
 
409
#endif /* __X86_EMULATE_H__ */