VirtualBox

source: vbox/trunk/src/recompiler/target-i386/cpu.h@ 22173

最後變更 在這個檔案從22173是 18927,由 vboxsync 提交於 16 年 前

Big step to separate VMM data structures for guest SMP. (pgm, em)

  • 屬性 svn:eol-style 設為 native
檔案大小: 29.5 KB
 
1/*
2 * i386 virtual CPU header
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21/*
22 * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
23 * other than GPL or LGPL is available it will apply instead, Sun elects to use only
24 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
25 * a choice of LGPL license versions is made available with the language indicating
26 * that LGPLv2 or any later version may be used, or where a choice of which version
27 * of the LGPL is applied is otherwise unspecified.
28 */
29#ifndef CPU_I386_H
30#define CPU_I386_H
31
32#include "config.h"
33
34#ifdef TARGET_X86_64
35#define TARGET_LONG_BITS 64
36#else
37#define TARGET_LONG_BITS 32
38#endif
39
40/* target supports implicit self modifying code */
41#define TARGET_HAS_SMC
42/* support for self modifying code even if the modified instruction is
43 close to the modifying instruction */
44#define TARGET_HAS_PRECISE_SMC
45
46#define TARGET_HAS_ICE 1
47
48#ifdef TARGET_X86_64
49#define ELF_MACHINE EM_X86_64
50#else
51#define ELF_MACHINE EM_386
52#endif
53
54#include "cpu-defs.h"
55
56#include "softfloat.h"
57
58#if defined(VBOX)
59# include <iprt/critsect.h>
60# include <iprt/thread.h>
61# include <iprt/assert.h>
62# include <iprt/asm.h>
63# include <VBox/vmm.h>
64# include <VBox/stam.h>
65#endif /* VBOX */
66
67#define R_EAX 0
68#define R_ECX 1
69#define R_EDX 2
70#define R_EBX 3
71#define R_ESP 4
72#define R_EBP 5
73#define R_ESI 6
74#define R_EDI 7
75
76#define R_AL 0
77#define R_CL 1
78#define R_DL 2
79#define R_BL 3
80#define R_AH 4
81#define R_CH 5
82#define R_DH 6
83#define R_BH 7
84
85#define R_ES 0
86#define R_CS 1
87#define R_SS 2
88#define R_DS 3
89#define R_FS 4
90#define R_GS 5
91
92/* segment descriptor fields */
93#define DESC_G_MASK (1 << 23)
94#define DESC_B_SHIFT 22
95#define DESC_B_MASK (1 << DESC_B_SHIFT)
96#define DESC_L_SHIFT 21 /* x86_64 only : 64 bit code segment */
97#define DESC_L_MASK (1 << DESC_L_SHIFT)
98#define DESC_AVL_MASK (1 << 20)
99#define DESC_P_MASK (1 << 15)
100#define DESC_DPL_SHIFT 13
101#define DESC_S_MASK (1 << 12)
102#define DESC_TYPE_SHIFT 8
103#define DESC_A_MASK (1 << 8)
104
105#define DESC_CS_MASK (1 << 11) /* 1=code segment 0=data segment */
106#define DESC_C_MASK (1 << 10) /* code: conforming */
107#define DESC_R_MASK (1 << 9) /* code: readable */
108
109#define DESC_E_MASK (1 << 10) /* data: expansion direction */
110#define DESC_W_MASK (1 << 9) /* data: writable */
111
112#define DESC_TSS_BUSY_MASK (1 << 9)
113
114/* eflags masks */
115#define CC_C 0x0001
116#define CC_P 0x0004
117#define CC_A 0x0010
118#define CC_Z 0x0040
119#define CC_S 0x0080
120#define CC_O 0x0800
121
122#define TF_SHIFT 8
123#define IOPL_SHIFT 12
124#define VM_SHIFT 17
125
126#define TF_MASK 0x00000100
127#define IF_MASK 0x00000200
128#define DF_MASK 0x00000400
129#define IOPL_MASK 0x00003000
130#define NT_MASK 0x00004000
131#define RF_MASK 0x00010000
132#define VM_MASK 0x00020000
133#define AC_MASK 0x00040000
134#define VIF_MASK 0x00080000
135#define VIP_MASK 0x00100000
136#define ID_MASK 0x00200000
137
138/* hidden flags - used internally by qemu to represent additionnal cpu
139 states. Only the CPL, INHIBIT_IRQ, SMM and SVMI are not redundant. We avoid
140 using the IOPL_MASK, TF_MASK and VM_MASK bit position to ease oring
141 with eflags. */
142/* current cpl */
143#define HF_CPL_SHIFT 0
144/* true if soft mmu is being used */
145#define HF_SOFTMMU_SHIFT 2
146/* true if hardware interrupts must be disabled for next instruction */
147#define HF_INHIBIT_IRQ_SHIFT 3
148/* 16 or 32 segments */
149#define HF_CS32_SHIFT 4
150#define HF_SS32_SHIFT 5
151/* zero base for DS, ES and SS : can be '0' only in 32 bit CS segment */
152#define HF_ADDSEG_SHIFT 6
153/* copy of CR0.PE (protected mode) */
154#define HF_PE_SHIFT 7
155#define HF_TF_SHIFT 8 /* must be same as eflags */
156#define HF_MP_SHIFT 9 /* the order must be MP, EM, TS */
157#define HF_EM_SHIFT 10
158#define HF_TS_SHIFT 11
159#define HF_IOPL_SHIFT 12 /* must be same as eflags */
160#define HF_LMA_SHIFT 14 /* only used on x86_64: long mode active */
161#define HF_CS64_SHIFT 15 /* only used on x86_64: 64 bit code segment */
162#define HF_OSFXSR_SHIFT 16 /* CR4.OSFXSR */
163#define HF_VM_SHIFT 17 /* must be same as eflags */
164#define HF_HALTED_SHIFT 18 /* CPU halted */
165#define HF_SMM_SHIFT 19 /* CPU in SMM mode */
166#define HF_SVME_SHIFT 20 /* SVME enabled (copy of EFER.SVME) */
167#define HF_SVMI_SHIFT 21 /* SVM intercepts are active */
168
169#define HF_CPL_MASK (3 << HF_CPL_SHIFT)
170#define HF_SOFTMMU_MASK (1 << HF_SOFTMMU_SHIFT)
171#define HF_INHIBIT_IRQ_MASK (1 << HF_INHIBIT_IRQ_SHIFT)
172#define HF_CS32_MASK (1 << HF_CS32_SHIFT)
173#define HF_SS32_MASK (1 << HF_SS32_SHIFT)
174#define HF_ADDSEG_MASK (1 << HF_ADDSEG_SHIFT)
175#define HF_PE_MASK (1 << HF_PE_SHIFT)
176#define HF_TF_MASK (1 << HF_TF_SHIFT)
177#define HF_MP_MASK (1 << HF_MP_SHIFT)
178#define HF_EM_MASK (1 << HF_EM_SHIFT)
179#define HF_TS_MASK (1 << HF_TS_SHIFT)
180#define HF_LMA_MASK (1 << HF_LMA_SHIFT)
181#define HF_CS64_MASK (1 << HF_CS64_SHIFT)
182#define HF_OSFXSR_MASK (1 << HF_OSFXSR_SHIFT)
183#define HF_HALTED_MASK (1 << HF_HALTED_SHIFT)
184#define HF_SMM_MASK (1 << HF_SMM_SHIFT)
185#define HF_SVME_MASK (1 << HF_SVME_SHIFT)
186#define HF_SVMI_MASK (1 << HF_SVMI_SHIFT)
187
188/* hflags2 */
189
190#define HF2_GIF_SHIFT 0 /* if set CPU takes interrupts */
191#define HF2_HIF_SHIFT 1 /* value of IF_MASK when entering SVM */
192#define HF2_NMI_SHIFT 2 /* CPU serving NMI */
193#define HF2_VINTR_SHIFT 3 /* value of V_INTR_MASKING bit */
194
195#define HF2_GIF_MASK (1 << HF2_GIF_SHIFT)
196#define HF2_HIF_MASK (1 << HF2_HIF_SHIFT)
197#define HF2_NMI_MASK (1 << HF2_NMI_SHIFT)
198#define HF2_VINTR_MASK (1 << HF2_VINTR_SHIFT)
199
200#define CR0_PE_MASK (1 << 0)
201#define CR0_MP_MASK (1 << 1)
202#define CR0_EM_MASK (1 << 2)
203#define CR0_TS_MASK (1 << 3)
204#define CR0_ET_MASK (1 << 4)
205#define CR0_NE_MASK (1 << 5)
206#define CR0_WP_MASK (1 << 16)
207#define CR0_AM_MASK (1 << 18)
208#define CR0_PG_MASK (1 << 31)
209
210#define CR4_VME_MASK (1 << 0)
211#define CR4_PVI_MASK (1 << 1)
212#define CR4_TSD_MASK (1 << 2)
213#define CR4_DE_MASK (1 << 3)
214#define CR4_PSE_MASK (1 << 4)
215#define CR4_PAE_MASK (1 << 5)
216#define CR4_PGE_MASK (1 << 7)
217#define CR4_PCE_MASK (1 << 8)
218#define CR4_OSFXSR_MASK (1 << 9)
219#define CR4_OSXMMEXCPT_MASK (1 << 10)
220
221#define PG_PRESENT_BIT 0
222#define PG_RW_BIT 1
223#define PG_USER_BIT 2
224#define PG_PWT_BIT 3
225#define PG_PCD_BIT 4
226#define PG_ACCESSED_BIT 5
227#define PG_DIRTY_BIT 6
228#define PG_PSE_BIT 7
229#define PG_GLOBAL_BIT 8
230#define PG_NX_BIT 63
231
232#define PG_PRESENT_MASK (1 << PG_PRESENT_BIT)
233#define PG_RW_MASK (1 << PG_RW_BIT)
234#define PG_USER_MASK (1 << PG_USER_BIT)
235#define PG_PWT_MASK (1 << PG_PWT_BIT)
236#define PG_PCD_MASK (1 << PG_PCD_BIT)
237#define PG_ACCESSED_MASK (1 << PG_ACCESSED_BIT)
238#define PG_DIRTY_MASK (1 << PG_DIRTY_BIT)
239#define PG_PSE_MASK (1 << PG_PSE_BIT)
240#define PG_GLOBAL_MASK (1 << PG_GLOBAL_BIT)
241#define PG_NX_MASK (1LL << PG_NX_BIT)
242
243#define PG_ERROR_W_BIT 1
244
245#define PG_ERROR_P_MASK 0x01
246#define PG_ERROR_W_MASK (1 << PG_ERROR_W_BIT)
247#define PG_ERROR_U_MASK 0x04
248#define PG_ERROR_RSVD_MASK 0x08
249#define PG_ERROR_I_D_MASK 0x10
250
251#define MSR_IA32_APICBASE 0x1b
252#define MSR_IA32_APICBASE_BSP (1<<8)
253#define MSR_IA32_APICBASE_ENABLE (1<<11)
254#define MSR_IA32_APICBASE_BASE (0xfffff<<12)
255
256#ifndef MSR_IA32_SYSENTER_CS /* VBox x86.h klugde */
257#define MSR_IA32_SYSENTER_CS 0x174
258#define MSR_IA32_SYSENTER_ESP 0x175
259#define MSR_IA32_SYSENTER_EIP 0x176
260#endif
261
262#define MSR_IA32_SYSENTER_CS 0x174
263#define MSR_IA32_SYSENTER_ESP 0x175
264#define MSR_IA32_SYSENTER_EIP 0x176
265
266#define MSR_MCG_CAP 0x179
267#define MSR_MCG_STATUS 0x17a
268#define MSR_MCG_CTL 0x17b
269
270#define MSR_IA32_PERF_STATUS 0x198
271
272#define MSR_PAT 0x277
273
274#define MSR_EFER 0xc0000080
275
276#define MSR_EFER_SCE (1 << 0)
277#define MSR_EFER_LME (1 << 8)
278#define MSR_EFER_LMA (1 << 10)
279#define MSR_EFER_NXE (1 << 11)
280#define MSR_EFER_SVME (1 << 12)
281#define MSR_EFER_FFXSR (1 << 14)
282
283#ifdef VBOX
284#define MSR_APIC_RANGE_START 0x800
285#define MSR_APIC_RANGE_END 0x900
286#endif
287
288#define MSR_STAR 0xc0000081
289#define MSR_LSTAR 0xc0000082
290#define MSR_CSTAR 0xc0000083
291#define MSR_FMASK 0xc0000084
292#define MSR_FSBASE 0xc0000100
293#define MSR_GSBASE 0xc0000101
294#define MSR_KERNELGSBASE 0xc0000102
295
296#define MSR_VM_HSAVE_PA 0xc0010117
297
298/* cpuid_features bits */
299#define CPUID_FP87 (1 << 0)
300#define CPUID_VME (1 << 1)
301#define CPUID_DE (1 << 2)
302#define CPUID_PSE (1 << 3)
303#define CPUID_TSC (1 << 4)
304#define CPUID_MSR (1 << 5)
305#define CPUID_PAE (1 << 6)
306#define CPUID_MCE (1 << 7)
307#define CPUID_CX8 (1 << 8)
308#define CPUID_APIC (1 << 9)
309#define CPUID_SEP (1 << 11) /* sysenter/sysexit */
310#define CPUID_MTRR (1 << 12)
311#define CPUID_PGE (1 << 13)
312#define CPUID_MCA (1 << 14)
313#define CPUID_CMOV (1 << 15)
314#define CPUID_PAT (1 << 16)
315#define CPUID_PSE36 (1 << 17)
316#define CPUID_CLFLUSH (1 << 19)
317#define CPUID_DTS (1 << 21)
318#define CPUID_ACPI (1 << 22)
319#define CPUID_MMX (1 << 23)
320#define CPUID_FXSR (1 << 24)
321#define CPUID_SSE (1 << 25)
322#define CPUID_SSE2 (1 << 26)
323#define CPUID_SS (1 << 27)
324#define CPUID_HT (1 << 28)
325#define CPUID_TM (1 << 29)
326#define CPUID_IA64 (1 << 30)
327#define CPUID_PBE (1 << 31)
328
329#define CPUID_EXT_SSE3 (1 << 0)
330#define CPUID_EXT_DTES64 (1 << 2)
331#define CPUID_EXT_MONITOR (1 << 3)
332#define CPUID_EXT_DSCPL (1 << 4)
333#define CPUID_EXT_VMX (1 << 5)
334#define CPUID_EXT_SMX (1 << 6)
335#define CPUID_EXT_EST (1 << 7)
336#define CPUID_EXT_TM2 (1 << 8)
337#define CPUID_EXT_SSSE3 (1 << 9)
338#define CPUID_EXT_CID (1 << 10)
339#define CPUID_EXT_CX16 (1 << 13)
340#define CPUID_EXT_XTPR (1 << 14)
341#define CPUID_EXT_PDCM (1 << 15)
342#define CPUID_EXT_DCA (1 << 18)
343#define CPUID_EXT_SSE41 (1 << 19)
344#define CPUID_EXT_SSE42 (1 << 20)
345#define CPUID_EXT_X2APIC (1 << 21)
346#define CPUID_EXT_MOVBE (1 << 22)
347#define CPUID_EXT_POPCNT (1 << 23)
348#define CPUID_EXT_XSAVE (1 << 26)
349#define CPUID_EXT_OSXSAVE (1 << 27)
350
351#define CPUID_EXT2_SYSCALL (1 << 11)
352#define CPUID_EXT2_MP (1 << 19)
353#define CPUID_EXT2_NX (1 << 20)
354#define CPUID_EXT2_MMXEXT (1 << 22)
355#define CPUID_EXT2_FFXSR (1 << 25)
356#define CPUID_EXT2_PDPE1GB (1 << 26)
357#define CPUID_EXT2_RDTSCP (1 << 27)
358#define CPUID_EXT2_LM (1 << 29)
359#define CPUID_EXT2_3DNOWEXT (1 << 30)
360#define CPUID_EXT2_3DNOW (1 << 31)
361
362#define CPUID_EXT3_LAHF_LM (1 << 0)
363#define CPUID_EXT3_CMP_LEG (1 << 1)
364#define CPUID_EXT3_SVM (1 << 2)
365#define CPUID_EXT3_EXTAPIC (1 << 3)
366#define CPUID_EXT3_CR8LEG (1 << 4)
367#define CPUID_EXT3_ABM (1 << 5)
368#define CPUID_EXT3_SSE4A (1 << 6)
369#define CPUID_EXT3_MISALIGNSSE (1 << 7)
370#define CPUID_EXT3_3DNOWPREFETCH (1 << 8)
371#define CPUID_EXT3_OSVW (1 << 9)
372#define CPUID_EXT3_IBS (1 << 10)
373#define CPUID_EXT3_SKINIT (1 << 12)
374
375#define CPUID_VENDOR_INTEL_1 0x756e6547 /* "Genu" */
376#define CPUID_VENDOR_INTEL_2 0x49656e69 /* "ineI" */
377#define CPUID_VENDOR_INTEL_3 0x6c65746e /* "ntel" */
378
379#define CPUID_VENDOR_AMD_1 0x68747541 /* "Auth" */
380#define CPUID_VENDOR_AMD_2 0x69746e65 /* "enti" */
381#define CPUID_VENDOR_AMD_3 0x444d4163 /* "cAMD" */
382
383#define CPUID_MWAIT_IBE (1 << 1) /* Interrupts can exit capability */
384#define CPUID_MWAIT_EMX (1 << 0) /* enumeration supported */
385
386#define EXCP00_DIVZ 0
387#define EXCP01_SSTP 1
388#define EXCP02_NMI 2
389#define EXCP03_INT3 3
390#define EXCP04_INTO 4
391#define EXCP05_BOUND 5
392#define EXCP06_ILLOP 6
393#define EXCP07_PREX 7
394#define EXCP08_DBLE 8
395#define EXCP09_XERR 9
396#define EXCP0A_TSS 10
397#define EXCP0B_NOSEG 11
398#define EXCP0C_STACK 12
399#define EXCP0D_GPF 13
400#define EXCP0E_PAGE 14
401#define EXCP10_COPR 16
402#define EXCP11_ALGN 17
403#define EXCP12_MCHK 18
404
405#define EXCP_SYSCALL 0x100 /* only happens in user only emulation
406 for syscall instruction */
407
408enum {
409 CC_OP_DYNAMIC, /* must use dynamic code to get cc_op */
410 CC_OP_EFLAGS, /* all cc are explicitely computed, CC_SRC = flags */
411
412 CC_OP_MULB, /* modify all flags, C, O = (CC_SRC != 0) */
413 CC_OP_MULW,
414 CC_OP_MULL,
415 CC_OP_MULQ,
416
417 CC_OP_ADDB, /* modify all flags, CC_DST = res, CC_SRC = src1 */
418 CC_OP_ADDW,
419 CC_OP_ADDL,
420 CC_OP_ADDQ,
421
422 CC_OP_ADCB, /* modify all flags, CC_DST = res, CC_SRC = src1 */
423 CC_OP_ADCW,
424 CC_OP_ADCL,
425 CC_OP_ADCQ,
426
427 CC_OP_SUBB, /* modify all flags, CC_DST = res, CC_SRC = src1 */
428 CC_OP_SUBW,
429 CC_OP_SUBL,
430 CC_OP_SUBQ,
431
432 CC_OP_SBBB, /* modify all flags, CC_DST = res, CC_SRC = src1 */
433 CC_OP_SBBW,
434 CC_OP_SBBL,
435 CC_OP_SBBQ,
436
437 CC_OP_LOGICB, /* modify all flags, CC_DST = res */
438 CC_OP_LOGICW,
439 CC_OP_LOGICL,
440 CC_OP_LOGICQ,
441
442 CC_OP_INCB, /* modify all flags except, CC_DST = res, CC_SRC = C */
443 CC_OP_INCW,
444 CC_OP_INCL,
445 CC_OP_INCQ,
446
447 CC_OP_DECB, /* modify all flags except, CC_DST = res, CC_SRC = C */
448 CC_OP_DECW,
449 CC_OP_DECL,
450 CC_OP_DECQ,
451
452 CC_OP_SHLB, /* modify all flags, CC_DST = res, CC_SRC.msb = C */
453 CC_OP_SHLW,
454 CC_OP_SHLL,
455 CC_OP_SHLQ,
456
457 CC_OP_SARB, /* modify all flags, CC_DST = res, CC_SRC.lsb = C */
458 CC_OP_SARW,
459 CC_OP_SARL,
460 CC_OP_SARQ,
461
462 CC_OP_NB
463};
464
465#ifdef FLOATX80
466#define USE_X86LDOUBLE
467#endif
468
469#ifdef USE_X86LDOUBLE
470typedef floatx80 CPU86_LDouble;
471#else
472typedef float64 CPU86_LDouble;
473#endif
474
475typedef struct SegmentCache {
476 uint32_t selector;
477 target_ulong base;
478 uint32_t limit;
479 uint32_t flags;
480#ifdef VBOX
481 /** The new selector is saved here when we are unable to sync it before invoking the recompiled code. */
482 uint32_t newselector;
483#endif
484} SegmentCache;
485
486typedef union {
487 uint8_t _b[16];
488 uint16_t _w[8];
489 uint32_t _l[4];
490 uint64_t _q[2];
491 float32 _s[4];
492 float64 _d[2];
493} XMMReg;
494
495typedef union {
496 uint8_t _b[8];
497 uint16_t _w[2];
498 uint32_t _l[1];
499 float32 _s[2];
500 uint64_t q;
501} MMXReg;
502
503#ifdef WORDS_BIGENDIAN
504#define XMM_B(n) _b[15 - (n)]
505#define XMM_W(n) _w[7 - (n)]
506#define XMM_L(n) _l[3 - (n)]
507#define XMM_S(n) _s[3 - (n)]
508#define XMM_Q(n) _q[1 - (n)]
509#define XMM_D(n) _d[1 - (n)]
510
511#define MMX_B(n) _b[7 - (n)]
512#define MMX_W(n) _w[3 - (n)]
513#define MMX_L(n) _l[1 - (n)]
514#define MMX_S(n) _s[1 - (n)]
515#else
516#define XMM_B(n) _b[n]
517#define XMM_W(n) _w[n]
518#define XMM_L(n) _l[n]
519#define XMM_S(n) _s[n]
520#define XMM_Q(n) _q[n]
521#define XMM_D(n) _d[n]
522
523#define MMX_B(n) _b[n]
524#define MMX_W(n) _w[n]
525#define MMX_L(n) _l[n]
526#define MMX_S(n) _s[n]
527#endif
528#define MMX_Q(n) q
529
530#ifdef TARGET_X86_64
531#define CPU_NB_REGS 16
532#else
533#define CPU_NB_REGS 8
534#endif
535
536#define NB_MMU_MODES 2
537
538typedef struct CPUX86State {
539 /* standard registers */
540 target_ulong regs[CPU_NB_REGS];
541 target_ulong eip;
542 target_ulong eflags; /* eflags register. During CPU emulation, CC
543 flags and DF are set to zero because they are
544 stored elsewhere */
545
546 /* emulator internal eflags handling */
547 target_ulong cc_src;
548 target_ulong cc_dst;
549 uint32_t cc_op;
550 int32_t df; /* D flag : 1 if D = 0, -1 if D = 1 */
551 uint32_t hflags; /* TB flags, see HF_xxx constants. These flags
552 are known at translation time. */
553 uint32_t hflags2; /* various other flags, see HF2_xxx constants. */
554
555 /* segments */
556 SegmentCache segs[6]; /* selector values */
557 SegmentCache ldt;
558 SegmentCache tr;
559 SegmentCache gdt; /* only base and limit are used */
560 SegmentCache idt; /* only base and limit are used */
561
562 target_ulong cr[5]; /* NOTE: cr1 is unused */
563 uint64_t a20_mask;
564
565 /* FPU state */
566 unsigned int fpstt; /* top of stack index */
567 unsigned int fpus;
568 unsigned int fpuc;
569 uint8_t fptags[8]; /* 0 = valid, 1 = empty */
570 union {
571#ifdef USE_X86LDOUBLE
572#ifndef VBOX
573 CPU86_LDouble d __attribute__((aligned(16)));
574#else
575 ALIGNED_MEMBER(CPU86_LDouble, d, 16);
576#endif
577#else
578 CPU86_LDouble d;
579#endif
580 MMXReg mmx;
581 } fpregs[8];
582
583 /* emulator internal variables */
584 float_status fp_status;
585#ifdef VBOX
586 uint32_t alignment3[3]; /* force the long double to start a 16 byte line. */
587#endif
588 CPU86_LDouble ft0;
589#if defined(VBOX) && defined(RT_ARCH_X86) && !defined(RT_OS_DARWIN)
590 uint32_t alignment4; /* long double is 12 byte, pad it to 16. */
591#endif
592
593 float_status mmx_status; /* for 3DNow! float ops */
594 float_status sse_status;
595 uint32_t mxcsr;
596 XMMReg xmm_regs[CPU_NB_REGS];
597 XMMReg xmm_t0;
598 MMXReg mmx_t0;
599 target_ulong cc_tmp; /* temporary for rcr/rcl */
600
601 /* sysenter registers */
602 uint32_t sysenter_cs;
603 uint64_t sysenter_esp;
604 uint64_t sysenter_eip;
605#ifdef VBOX
606 uint32_t alignment0;
607#endif
608 uint64_t efer;
609 uint64_t star;
610
611 uint64_t vm_hsave;
612 uint64_t vm_vmcb;
613 uint64_t tsc_offset;
614 uint64_t intercept;
615 uint16_t intercept_cr_read;
616 uint16_t intercept_cr_write;
617 uint16_t intercept_dr_read;
618 uint16_t intercept_dr_write;
619 uint32_t intercept_exceptions;
620 uint8_t v_tpr;
621
622#ifdef TARGET_X86_64
623 target_ulong lstar;
624 target_ulong cstar;
625 target_ulong fmask;
626 target_ulong kernelgsbase;
627#endif
628
629 uint64_t pat;
630
631 /* exception/interrupt handling */
632 int error_code;
633 int exception_is_int;
634 target_ulong exception_next_eip;
635 target_ulong dr[8]; /* debug registers */
636 uint32_t smbase;
637 int old_exception; /* exception in flight */
638
639 CPU_COMMON
640
641#ifdef VBOX
642 /** cpu state flags. (see defines below) */
643 uint32_t state;
644 /** The VM handle. */
645 PVM pVM;
646 /** The VMCPU handle. */
647 PVMCPU pVCpu;
648 /** code buffer for instruction emulation */
649 void *pvCodeBuffer;
650 /** code buffer size */
651 uint32_t cbCodeBuffer;
652#endif /* VBOX */
653
654 /* processor features (e.g. for CPUID insn) */
655#ifndef VBOX /* remR3CpuId deals with these */
656 uint32_t cpuid_level;
657 uint32_t cpuid_vendor1;
658 uint32_t cpuid_vendor2;
659 uint32_t cpuid_vendor3;
660 uint32_t cpuid_version;
661#endif /* !VBOX */
662 uint32_t cpuid_features;
663 uint32_t cpuid_ext_features;
664#ifndef VBOX
665 uint32_t cpuid_xlevel;
666 uint32_t cpuid_model[12];
667#endif /* !VBOX */
668 uint32_t cpuid_ext2_features;
669 uint32_t cpuid_ext3_features;
670 uint32_t cpuid_apic_id;
671
672#ifndef VBOX
673#ifdef USE_KQEMU
674 int kqemu_enabled;
675 int last_io_time;
676#endif
677 /* in order to simplify APIC support, we leave this pointer to the
678 user */
679 struct APICState *apic_state;
680#else
681 uint32_t alignment2[3];
682 /** Profiling tb_flush. */
683 STAMPROFILE StatTbFlush;
684#endif
685} CPUX86State;
686
687#ifdef VBOX
688
689/* Version 1.6 structure; just for loading the old saved state */
690typedef struct SegmentCache_Ver16 {
691 uint32_t selector;
692 uint32_t base;
693 uint32_t limit;
694 uint32_t flags;
695 /** The new selector is saved here when we are unable to sync it before invoking the recompiled code. */
696 uint32_t newselector;
697} SegmentCache_Ver16;
698
699#define CPU_NB_REGS_VER16 8
700
701/* Version 1.6 structure; just for loading the old saved state */
702typedef struct CPUX86State_Ver16 {
703#if TARGET_LONG_BITS > HOST_LONG_BITS
704 /* temporaries if we cannot store them in host registers */
705 uint32_t t0, t1, t2;
706#endif
707
708 /* standard registers */
709 uint32_t regs[CPU_NB_REGS_VER16];
710 uint32_t eip;
711 uint32_t eflags; /* eflags register. During CPU emulation, CC
712 flags and DF are set to zero because they are
713 stored elsewhere */
714
715 /* emulator internal eflags handling */
716 uint32_t cc_src;
717 uint32_t cc_dst;
718 uint32_t cc_op;
719 int32_t df; /* D flag : 1 if D = 0, -1 if D = 1 */
720 uint32_t hflags; /* hidden flags, see HF_xxx constants */
721
722 /* segments */
723 SegmentCache_Ver16 segs[6]; /* selector values */
724 SegmentCache_Ver16 ldt;
725 SegmentCache_Ver16 tr;
726 SegmentCache_Ver16 gdt; /* only base and limit are used */
727 SegmentCache_Ver16 idt; /* only base and limit are used */
728
729 uint32_t cr[5]; /* NOTE: cr1 is unused */
730 uint32_t a20_mask;
731
732 /* FPU state */
733 unsigned int fpstt; /* top of stack index */
734 unsigned int fpus;
735 unsigned int fpuc;
736 uint8_t fptags[8]; /* 0 = valid, 1 = empty */
737 union {
738#ifdef USE_X86LDOUBLE
739#ifndef VBOX
740 CPU86_LDouble d __attribute__((aligned(16)));
741#else
742 ALIGNED_MEMBER(CPU86_LDouble, d, 16);
743#endif
744#else
745 CPU86_LDouble d;
746#endif
747 MMXReg mmx;
748 } fpregs[8];
749
750 /* emulator internal variables */
751 float_status fp_status;
752#ifdef VBOX
753 uint32_t alignment3[3]; /* force the long double to start a 16 byte line. */
754#endif
755 CPU86_LDouble ft0;
756#if defined(VBOX) && defined(RT_ARCH_X86) && !defined(RT_OS_DARWIN)
757 uint32_t alignment4; /* long double is 12 byte, pad it to 16. */
758#endif
759 union {
760 float f;
761 double d;
762 int i32;
763 int64_t i64;
764 } fp_convert;
765
766 float_status sse_status;
767 uint32_t mxcsr;
768 XMMReg xmm_regs[CPU_NB_REGS_VER16];
769 XMMReg xmm_t0;
770 MMXReg mmx_t0;
771
772 /* sysenter registers */
773 uint32_t sysenter_cs;
774 uint32_t sysenter_esp;
775 uint32_t sysenter_eip;
776#ifdef VBOX
777 uint32_t alignment0;
778#endif
779 uint64_t efer;
780 uint64_t star;
781
782 uint64_t pat;
783
784 /* temporary data for USE_CODE_COPY mode */
785#ifdef USE_CODE_COPY
786 uint32_t tmp0;
787 uint32_t saved_esp;
788 int native_fp_regs; /* if true, the FPU state is in the native CPU regs */
789#endif
790
791 /* exception/interrupt handling */
792 jmp_buf jmp_env;
793} CPUX86State_Ver16;
794
795/** CPUX86State state flags
796 * @{ */
797#define CPU_RAW_RING0 0x0002 /* Set after first time RawR0 is executed, never cleared. */
798#define CPU_EMULATE_SINGLE_INSTR 0x0040 /* Execute a single instruction in emulation mode */
799#define CPU_EMULATE_SINGLE_STEP 0x0080 /* go into single step mode */
800#define CPU_RAW_HWACC 0x0100 /* Set after first time HWACC is executed, never cleared. */
801/** @} */
802#endif /* !VBOX */
803
804#ifdef VBOX
805CPUX86State *cpu_x86_init(CPUX86State *env, const char *cpu_model);
806#else /* !VBOX */
807CPUX86State *cpu_x86_init(const char *cpu_model);
808#endif /* !VBOX */
809int cpu_x86_exec(CPUX86State *s);
810void cpu_x86_close(CPUX86State *s);
811void x86_cpu_list (FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt,
812 ...));
813int cpu_get_pic_interrupt(CPUX86State *s);
814/* MSDOS compatibility mode FPU exception support */
815void cpu_set_ferr(CPUX86State *s);
816
817/* this function must always be used to load data in the segment
818 cache: it synchronizes the hflags with the segment cache values */
819#ifndef VBOX
820static inline void cpu_x86_load_seg_cache(CPUX86State *env,
821 int seg_reg, unsigned int selector,
822 target_ulong base,
823 unsigned int limit,
824 unsigned int flags)
825#else
826DECLINLINE(void) cpu_x86_load_seg_cache(CPUX86State *env,
827 int seg_reg, unsigned int selector,
828 target_ulong base,
829 unsigned int limit,
830 unsigned int flags)
831
832#endif
833{
834 SegmentCache *sc;
835 unsigned int new_hflags;
836
837 sc = &env->segs[seg_reg];
838 sc->selector = selector;
839 sc->base = base;
840 sc->limit = limit;
841 sc->flags = flags;
842#ifdef VBOX
843 sc->newselector = 0;
844#endif
845
846 /* update the hidden flags */
847 {
848 if (seg_reg == R_CS) {
849#ifdef TARGET_X86_64
850 if ((env->hflags & HF_LMA_MASK) && (flags & DESC_L_MASK)) {
851 /* long mode */
852 env->hflags |= HF_CS32_MASK | HF_SS32_MASK | HF_CS64_MASK;
853 env->hflags &= ~(HF_ADDSEG_MASK);
854 } else
855#endif
856 {
857 /* legacy / compatibility case */
858 new_hflags = (env->segs[R_CS].flags & DESC_B_MASK)
859 >> (DESC_B_SHIFT - HF_CS32_SHIFT);
860 env->hflags = (env->hflags & ~(HF_CS32_MASK | HF_CS64_MASK)) |
861 new_hflags;
862 }
863 }
864 new_hflags = (env->segs[R_SS].flags & DESC_B_MASK)
865 >> (DESC_B_SHIFT - HF_SS32_SHIFT);
866 if (env->hflags & HF_CS64_MASK) {
867 /* zero base assumed for DS, ES and SS in long mode */
868 } else if (!(env->cr[0] & CR0_PE_MASK) ||
869 (env->eflags & VM_MASK) ||
870 !(env->hflags & HF_CS32_MASK)) {
871 /* XXX: try to avoid this test. The problem comes from the
872 fact that is real mode or vm86 mode we only modify the
873 'base' and 'selector' fields of the segment cache to go
874 faster. A solution may be to force addseg to one in
875 translate-i386.c. */
876 new_hflags |= HF_ADDSEG_MASK;
877 } else {
878 new_hflags |= ((env->segs[R_DS].base |
879 env->segs[R_ES].base |
880 env->segs[R_SS].base) != 0) <<
881 HF_ADDSEG_SHIFT;
882 }
883 env->hflags = (env->hflags &
884 ~(HF_SS32_MASK | HF_ADDSEG_MASK)) | new_hflags;
885 }
886}
887
888/* wrapper, just in case memory mappings must be changed */
889#ifndef VBOX
890static inline void cpu_x86_set_cpl(CPUX86State *s, int cpl)
891#else
892DECLINLINE(void) cpu_x86_set_cpl(CPUX86State *s, int cpl)
893#endif
894{
895#if HF_CPL_MASK == 3
896 s->hflags = (s->hflags & ~HF_CPL_MASK) | cpl;
897#else
898#error HF_CPL_MASK is hardcoded
899#endif
900}
901
902/* used for debug or cpu save/restore */
903void cpu_get_fp80(uint64_t *pmant, uint16_t *pexp, CPU86_LDouble f);
904CPU86_LDouble cpu_set_fp80(uint64_t mant, uint16_t upper);
905
906/* the following helpers are only usable in user mode simulation as
907 they can trigger unexpected exceptions */
908void cpu_x86_load_seg(CPUX86State *s, int seg_reg, int selector);
909void cpu_x86_fsave(CPUX86State *s, uint8_t *ptr, int data32);
910void cpu_x86_frstor(CPUX86State *s, uint8_t *ptr, int data32);
911
912/* you can call this signal handler from your SIGBUS and SIGSEGV
913 signal handlers to inform the virtual CPU of exceptions. non zero
914 is returned if the signal was handled by the virtual CPU. */
915int cpu_x86_signal_handler(int host_signum, void *pinfo,
916 void *puc);
917void cpu_x86_set_a20(CPUX86State *env, int a20_state);
918
919uint64_t cpu_get_tsc(CPUX86State *env);
920
921void cpu_set_apic_base(CPUX86State *env, uint64_t val);
922uint64_t cpu_get_apic_base(CPUX86State *env);
923void cpu_set_apic_tpr(CPUX86State *env, uint8_t val);
924#ifndef NO_CPU_IO_DEFS
925uint8_t cpu_get_apic_tpr(CPUX86State *env);
926#endif
927#ifdef VBOX
928uint64_t cpu_apic_rdmsr(CPUX86State *env, uint32_t reg);
929void cpu_apic_wrmsr(CPUX86State *env, uint32_t reg, uint64_t value);
930uint64_t cpu_rdmsr(CPUX86State *env, uint32_t msr);
931void cpu_wrmsr(CPUX86State *env, uint32_t msr, uint64_t val);
932#endif
933void cpu_smm_update(CPUX86State *env);
934
935/* will be suppressed */
936void cpu_x86_update_cr0(CPUX86State *env, uint32_t new_cr0);
937
938/* used to debug */
939#define X86_DUMP_FPU 0x0001 /* dump FPU state too */
940#define X86_DUMP_CCOP 0x0002 /* dump qemu flag cache */
941
942#ifdef USE_KQEMU
943static inline int cpu_get_time_fast(void)
944{
945 int low, high;
946 asm volatile("rdtsc" : "=a" (low), "=d" (high));
947 return low;
948}
949#endif
950
951#ifdef VBOX
952void cpu_trap_raw(CPUX86State *env1);
953
954/* in helper.c */
955uint8_t read_byte(CPUX86State *env1, target_ulong addr);
956uint16_t read_word(CPUX86State *env1, target_ulong addr);
957void write_byte(CPUX86State *env1, target_ulong addr, uint8_t val);
958uint32_t read_dword(CPUX86State *env1, target_ulong addr);
959void write_word(CPUX86State *env1, target_ulong addr, uint16_t val);
960void write_dword(CPUX86State *env1, target_ulong addr, uint32_t val);
961/* in helper.c */
962int emulate_single_instr(CPUX86State *env1);
963int get_ss_esp_from_tss_raw(CPUX86State *env1, uint32_t *ss_ptr, uint32_t *esp_ptr, int dpl);
964
965void restore_raw_fp_state(CPUX86State *env, uint8_t *ptr);
966void save_raw_fp_state(CPUX86State *env, uint8_t *ptr);
967
968#endif
969
970#define TARGET_PAGE_BITS 12
971
972#define CPUState CPUX86State
973#define cpu_init cpu_x86_init
974#define cpu_exec cpu_x86_exec
975#define cpu_gen_code cpu_x86_gen_code
976#define cpu_signal_handler cpu_x86_signal_handler
977#define cpu_list x86_cpu_list
978
979#define CPU_SAVE_VERSION 7
980
981/* MMU modes definitions */
982#define MMU_MODE0_SUFFIX _kernel
983#define MMU_MODE1_SUFFIX _user
984#define MMU_USER_IDX 1
985#ifndef VBOX
986static inline int cpu_mmu_index (CPUState *env)
987#else
988DECLINLINE(int) cpu_mmu_index (CPUState *env)
989#endif
990{
991 return (env->hflags & HF_CPL_MASK) == 3 ? 1 : 0;
992}
993
994void optimize_flags_init(void);
995
996typedef struct CCTable {
997 int (*compute_all)(void); /* return all the flags */
998 int (*compute_c)(void); /* return the C flag */
999} CCTable;
1000
1001extern CCTable cc_table[];
1002
1003#if defined(CONFIG_USER_ONLY)
1004static inline void cpu_clone_regs(CPUState *env, target_ulong newsp)
1005{
1006 if (newsp)
1007 env->regs[R_ESP] = newsp;
1008 env->regs[R_EAX] = 0;
1009}
1010#endif
1011
1012#define CPU_PC_FROM_TB(env, tb) env->eip = tb->pc - tb->cs_base
1013
1014#include "cpu-all.h"
1015
1016#include "svm.h"
1017
1018#endif /* CPU_I386_H */
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette