VirtualBox

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

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

rem: synced up to svn://svn.savannah.nongnu.org/qemu/trunk@6686 (repo UUID c046a42c-6fe2-441c-8c8c-71466251a162).

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