VirtualBox

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

Last change on this file since 35951 was 35346, checked in by vboxsync, 14 years ago

VMM reorg: Moving the public include files from include/VBox to include/VBox/vmm.

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

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