VirtualBox

source: vbox/trunk/src/recompiler/target-i386/exec.h@ 11828

Last change on this file since 11828 was 6726, checked in by vboxsync, 17 years ago

2 fixes from qemu: fix cmpxchg8b detection and fix DR6 single step exception status bit

  • Property svn:eol-style set to native
File size: 14.4 KB
Line 
1/*
2 * i386 execution defines
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#include "config.h"
21#include "dyngen-exec.h"
22
23/* XXX: factorize this mess */
24#ifdef TARGET_X86_64
25#define TARGET_LONG_BITS 64
26#else
27#define TARGET_LONG_BITS 32
28#endif
29
30#include "cpu-defs.h"
31
32/* at least 4 register variables are defined */
33register struct CPUX86State *env asm(AREG0);
34
35#if TARGET_LONG_BITS > HOST_LONG_BITS
36
37/* no registers can be used */
38#define T0 (env->t0)
39#define T1 (env->t1)
40#define T2 (env->t2)
41
42#else
43
44/* XXX: use unsigned long instead of target_ulong - better code will
45 be generated for 64 bit CPUs */
46register target_ulong T0 asm(AREG1);
47register target_ulong T1 asm(AREG2);
48register target_ulong T2 asm(AREG3);
49
50/* if more registers are available, we define some registers too */
51#ifdef AREG4
52register target_ulong EAX asm(AREG4);
53#define reg_EAX
54#endif
55
56#ifdef AREG5
57register target_ulong ESP asm(AREG5);
58#define reg_ESP
59#endif
60
61#ifdef AREG6
62register target_ulong EBP asm(AREG6);
63#define reg_EBP
64#endif
65
66#ifdef AREG7
67register target_ulong ECX asm(AREG7);
68#define reg_ECX
69#endif
70
71#ifdef AREG8
72register target_ulong EDX asm(AREG8);
73#define reg_EDX
74#endif
75
76#ifdef AREG9
77register target_ulong EBX asm(AREG9);
78#define reg_EBX
79#endif
80
81#ifdef AREG10
82register target_ulong ESI asm(AREG10);
83#define reg_ESI
84#endif
85
86#ifdef AREG11
87register target_ulong EDI asm(AREG11);
88#define reg_EDI
89#endif
90
91#endif /* ! (TARGET_LONG_BITS > HOST_LONG_BITS) */
92
93#define A0 T2
94
95extern FILE *logfile;
96extern int loglevel;
97
98#ifndef reg_EAX
99#define EAX (env->regs[R_EAX])
100#endif
101#ifndef reg_ECX
102#define ECX (env->regs[R_ECX])
103#endif
104#ifndef reg_EDX
105#define EDX (env->regs[R_EDX])
106#endif
107#ifndef reg_EBX
108#define EBX (env->regs[R_EBX])
109#endif
110#ifndef reg_ESP
111#define ESP (env->regs[R_ESP])
112#endif
113#ifndef reg_EBP
114#define EBP (env->regs[R_EBP])
115#endif
116#ifndef reg_ESI
117#define ESI (env->regs[R_ESI])
118#endif
119#ifndef reg_EDI
120#define EDI (env->regs[R_EDI])
121#endif
122#define EIP (env->eip)
123#define DF (env->df)
124
125#define CC_SRC (env->cc_src)
126#define CC_DST (env->cc_dst)
127#define CC_OP (env->cc_op)
128
129/* float macros */
130#define FT0 (env->ft0)
131#define ST0 (env->fpregs[env->fpstt].d)
132#define ST(n) (env->fpregs[(env->fpstt + (n)) & 7].d)
133#define ST1 ST(1)
134
135#ifdef USE_FP_CONVERT
136#define FP_CONVERT (env->fp_convert)
137#endif
138
139#include "cpu.h"
140#include "exec-all.h"
141
142typedef struct CCTable {
143 int (*compute_all)(void); /* return all the flags */
144 int (*compute_c)(void); /* return the C flag */
145} CCTable;
146
147extern CCTable cc_table[];
148
149void load_seg(int seg_reg, int selector);
150void helper_ljmp_protected_T0_T1(int next_eip);
151void helper_lcall_real_T0_T1(int shift, int next_eip);
152void helper_lcall_protected_T0_T1(int shift, int next_eip);
153void helper_iret_real(int shift);
154void helper_iret_protected(int shift, int next_eip);
155void helper_lret_protected(int shift, int addend);
156void helper_lldt_T0(void);
157void helper_ltr_T0(void);
158void helper_movl_crN_T0(int reg);
159void helper_movl_drN_T0(int reg);
160void helper_invlpg(target_ulong addr);
161void cpu_x86_update_cr0(CPUX86State *env, uint32_t new_cr0);
162void cpu_x86_update_cr3(CPUX86State *env, target_ulong new_cr3);
163void cpu_x86_update_cr4(CPUX86State *env, uint32_t new_cr4);
164void cpu_x86_flush_tlb(CPUX86State *env, target_ulong addr);
165int cpu_x86_handle_mmu_fault(CPUX86State *env, target_ulong addr,
166 int is_write, int is_user, int is_softmmu);
167void tlb_fill(target_ulong addr, int is_write, int is_user,
168 void *retaddr);
169void __hidden cpu_lock(void);
170void __hidden cpu_unlock(void);
171void do_interrupt(int intno, int is_int, int error_code,
172 target_ulong next_eip, int is_hw);
173void do_interrupt_user(int intno, int is_int, int error_code,
174 target_ulong next_eip);
175void raise_interrupt(int intno, int is_int, int error_code,
176 int next_eip_addend);
177void raise_exception_err(int exception_index, int error_code);
178void raise_exception(int exception_index);
179void do_smm_enter(void);
180void __hidden cpu_loop_exit(void);
181
182void OPPROTO op_movl_eflags_T0(void);
183void OPPROTO op_movl_T0_eflags(void);
184#ifdef VBOX
185void OPPROTO op_movl_T0_eflags_vme(void);
186void OPPROTO op_movw_eflags_T0_vme(void);
187void OPPROTO op_cli_vme(void);
188void OPPROTO op_sti_vme(void);
189#endif
190void helper_divl_EAX_T0(void);
191void helper_idivl_EAX_T0(void);
192void helper_mulq_EAX_T0(void);
193void helper_imulq_EAX_T0(void);
194void helper_imulq_T0_T1(void);
195void helper_divq_EAX_T0(void);
196void helper_idivq_EAX_T0(void);
197void helper_bswapq_T0(void);
198void helper_cmpxchg8b(void);
199void helper_single_step(void);
200void helper_cpuid(void);
201void helper_enter_level(int level, int data32);
202void helper_enter64_level(int level, int data64);
203void helper_sysenter(void);
204void helper_sysexit(void);
205void helper_syscall(int next_eip_addend);
206void helper_sysret(int dflag);
207void helper_rdtsc(void);
208void helper_rdmsr(void);
209void helper_wrmsr(void);
210void helper_lsl(void);
211void helper_lar(void);
212void helper_verr(void);
213void helper_verw(void);
214void helper_rsm(void);
215
216#ifdef VBOX
217void helper_external_event(void);
218void helper_record_call(void);
219
220/* in helper.c */
221void sync_seg(CPUX86State *env1, int seg_reg, int selector);
222void sync_ldtr(CPUX86State *env1, int selector);
223int sync_tr(CPUX86State *env1, int selector);
224
225#endif
226
227void check_iob_T0(void);
228void check_iow_T0(void);
229void check_iol_T0(void);
230void check_iob_DX(void);
231void check_iow_DX(void);
232void check_iol_DX(void);
233
234#if !defined(CONFIG_USER_ONLY)
235
236#include "softmmu_exec.h"
237
238static inline double ldfq(target_ulong ptr)
239{
240 union {
241 double d;
242 uint64_t i;
243 } u;
244 u.i = ldq(ptr);
245 return u.d;
246}
247
248static inline void stfq(target_ulong ptr, double v)
249{
250 union {
251 double d;
252 uint64_t i;
253 } u;
254 u.d = v;
255 stq(ptr, u.i);
256}
257
258static inline float ldfl(target_ulong ptr)
259{
260 union {
261 float f;
262 uint32_t i;
263 } u;
264 u.i = ldl(ptr);
265 return u.f;
266}
267
268static inline void stfl(target_ulong ptr, float v)
269{
270 union {
271 float f;
272 uint32_t i;
273 } u;
274 u.f = v;
275 stl(ptr, u.i);
276}
277
278#endif /* !defined(CONFIG_USER_ONLY) */
279
280#ifdef USE_X86LDOUBLE
281/* use long double functions */
282#define floatx_to_int32 floatx80_to_int32
283#define floatx_to_int64 floatx80_to_int64
284#define floatx_to_int32_round_to_zero floatx80_to_int32_round_to_zero
285#define floatx_to_int64_round_to_zero floatx80_to_int64_round_to_zero
286#define floatx_abs floatx80_abs
287#define floatx_chs floatx80_chs
288#define floatx_round_to_int floatx80_round_to_int
289#define floatx_compare floatx80_compare
290#define floatx_compare_quiet floatx80_compare_quiet
291#ifdef VBOX
292#undef sin
293#undef cos
294#undef sqrt
295#undef pow
296#undef log
297#undef tan
298#undef atan2
299#undef floor
300#undef ceil
301#undef ldexp
302#endif /* !VBOX */
303#define sin sinl
304#define cos cosl
305#define sqrt sqrtl
306#define pow powl
307#define log logl
308#define tan tanl
309#define atan2 atan2l
310#define floor floorl
311#define ceil ceill
312#define ldexp ldexpl
313#else
314#define floatx_to_int32 float64_to_int32
315#define floatx_to_int64 float64_to_int64
316#define floatx_to_int32_round_to_zero float64_to_int32_round_to_zero
317#define floatx_to_int64_round_to_zero float64_to_int64_round_to_zero
318#define floatx_abs float64_abs
319#define floatx_chs float64_chs
320#define floatx_round_to_int float64_round_to_int
321#define floatx_compare float64_compare
322#define floatx_compare_quiet float64_compare_quiet
323#endif
324
325extern CPU86_LDouble sin(CPU86_LDouble x);
326extern CPU86_LDouble cos(CPU86_LDouble x);
327extern CPU86_LDouble sqrt(CPU86_LDouble x);
328extern CPU86_LDouble pow(CPU86_LDouble, CPU86_LDouble);
329extern CPU86_LDouble log(CPU86_LDouble x);
330extern CPU86_LDouble tan(CPU86_LDouble x);
331extern CPU86_LDouble atan2(CPU86_LDouble, CPU86_LDouble);
332extern CPU86_LDouble floor(CPU86_LDouble x);
333extern CPU86_LDouble ceil(CPU86_LDouble x);
334
335#define RC_MASK 0xc00
336#define RC_NEAR 0x000
337#define RC_DOWN 0x400
338#define RC_UP 0x800
339#define RC_CHOP 0xc00
340
341#define MAXTAN 9223372036854775808.0
342
343#ifdef USE_X86LDOUBLE
344
345/* only for x86 */
346typedef union {
347 long double d;
348 struct {
349 unsigned long long lower;
350 unsigned short upper;
351 } l;
352} CPU86_LDoubleU;
353
354/* the following deal with x86 long double-precision numbers */
355#define MAXEXPD 0x7fff
356#define EXPBIAS 16383
357#define EXPD(fp) (fp.l.upper & 0x7fff)
358#define SIGND(fp) ((fp.l.upper) & 0x8000)
359#define MANTD(fp) (fp.l.lower)
360#define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7fff)) | EXPBIAS
361
362#else
363
364/* NOTE: arm is horrible as double 32 bit words are stored in big endian ! */
365typedef union {
366 double d;
367#if !defined(WORDS_BIGENDIAN) && !defined(__arm__)
368 struct {
369 uint32_t lower;
370 int32_t upper;
371 } l;
372#else
373 struct {
374 int32_t upper;
375 uint32_t lower;
376 } l;
377#endif
378#ifndef __arm__
379 int64_t ll;
380#endif
381} CPU86_LDoubleU;
382
383/* the following deal with IEEE double-precision numbers */
384#define MAXEXPD 0x7ff
385#define EXPBIAS 1023
386#define EXPD(fp) (((fp.l.upper) >> 20) & 0x7FF)
387#define SIGND(fp) ((fp.l.upper) & 0x80000000)
388#ifdef __arm__
389#define MANTD(fp) (fp.l.lower | ((uint64_t)(fp.l.upper & ((1 << 20) - 1)) << 32))
390#else
391#define MANTD(fp) (fp.ll & ((1LL << 52) - 1))
392#endif
393#define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7ff << 20)) | (EXPBIAS << 20)
394#endif
395
396static inline void fpush(void)
397{
398 env->fpstt = (env->fpstt - 1) & 7;
399 env->fptags[env->fpstt] = 0; /* validate stack entry */
400}
401
402static inline void fpop(void)
403{
404 env->fptags[env->fpstt] = 1; /* invvalidate stack entry */
405 env->fpstt = (env->fpstt + 1) & 7;
406}
407
408#ifndef USE_X86LDOUBLE
409static inline CPU86_LDouble helper_fldt(target_ulong ptr)
410{
411 CPU86_LDoubleU temp;
412 int upper, e;
413 uint64_t ll;
414
415 /* mantissa */
416 upper = lduw(ptr + 8);
417 /* XXX: handle overflow ? */
418 e = (upper & 0x7fff) - 16383 + EXPBIAS; /* exponent */
419 e |= (upper >> 4) & 0x800; /* sign */
420 ll = (ldq(ptr) >> 11) & ((1LL << 52) - 1);
421#ifdef __arm__
422 temp.l.upper = (e << 20) | (ll >> 32);
423 temp.l.lower = ll;
424#else
425 temp.ll = ll | ((uint64_t)e << 52);
426#endif
427 return temp.d;
428}
429
430static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
431{
432 CPU86_LDoubleU temp;
433 int e;
434
435 temp.d = f;
436 /* mantissa */
437 stq(ptr, (MANTD(temp) << 11) | (1LL << 63));
438 /* exponent + sign */
439 e = EXPD(temp) - EXPBIAS + 16383;
440 e |= SIGND(temp) >> 16;
441 stw(ptr + 8, e);
442}
443#else
444
445/* XXX: same endianness assumed */
446
447#ifdef CONFIG_USER_ONLY
448
449static inline CPU86_LDouble helper_fldt(target_ulong ptr)
450{
451 return *(CPU86_LDouble *)ptr;
452}
453
454static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
455{
456 *(CPU86_LDouble *)ptr = f;
457}
458
459#else
460
461/* we use memory access macros */
462
463static inline CPU86_LDouble helper_fldt(target_ulong ptr)
464{
465 CPU86_LDoubleU temp;
466
467 temp.l.lower = ldq(ptr);
468 temp.l.upper = lduw(ptr + 8);
469 return temp.d;
470}
471
472static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
473{
474 CPU86_LDoubleU temp;
475
476 temp.d = f;
477 stq(ptr, temp.l.lower);
478 stw(ptr + 8, temp.l.upper);
479}
480
481#endif /* !CONFIG_USER_ONLY */
482
483#endif /* USE_X86LDOUBLE */
484
485#define FPUS_IE (1 << 0)
486#define FPUS_DE (1 << 1)
487#define FPUS_ZE (1 << 2)
488#define FPUS_OE (1 << 3)
489#define FPUS_UE (1 << 4)
490#define FPUS_PE (1 << 5)
491#define FPUS_SF (1 << 6)
492#define FPUS_SE (1 << 7)
493#define FPUS_B (1 << 15)
494
495#define FPUC_EM 0x3f
496
497extern const CPU86_LDouble f15rk[7];
498
499void helper_fldt_ST0_A0(void);
500void helper_fstt_ST0_A0(void);
501void fpu_raise_exception(void);
502CPU86_LDouble helper_fdiv(CPU86_LDouble a, CPU86_LDouble b);
503void helper_fbld_ST0_A0(void);
504void helper_fbst_ST0_A0(void);
505void helper_f2xm1(void);
506void helper_fyl2x(void);
507void helper_fptan(void);
508void helper_fpatan(void);
509void helper_fxtract(void);
510void helper_fprem1(void);
511void helper_fprem(void);
512void helper_fyl2xp1(void);
513void helper_fsqrt(void);
514void helper_fsincos(void);
515void helper_frndint(void);
516void helper_fscale(void);
517void helper_fsin(void);
518void helper_fcos(void);
519void helper_fxam_ST0(void);
520void helper_fstenv(target_ulong ptr, int data32);
521void helper_fldenv(target_ulong ptr, int data32);
522void helper_fsave(target_ulong ptr, int data32);
523void helper_frstor(target_ulong ptr, int data32);
524void helper_fxsave(target_ulong ptr, int data64);
525void helper_fxrstor(target_ulong ptr, int data64);
526void restore_native_fp_state(CPUState *env);
527void save_native_fp_state(CPUState *env);
528float approx_rsqrt(float a);
529float approx_rcp(float a);
530void update_fp_status(void);
531void helper_hlt(void);
532void helper_monitor(void);
533void helper_mwait(void);
534
535extern const uint8_t parity_table[256];
536extern const uint8_t rclw_table[32];
537extern const uint8_t rclb_table[32];
538
539static inline uint32_t compute_eflags(void)
540{
541 return env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
542}
543
544/* NOTE: CC_OP must be modified manually to CC_OP_EFLAGS */
545static inline void load_eflags(int eflags, int update_mask)
546{
547 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
548 DF = 1 - (2 * ((eflags >> 10) & 1));
549 env->eflags = (env->eflags & ~update_mask) |
550 (eflags & update_mask);
551}
552
553static inline void env_to_regs(void)
554{
555#ifdef reg_EAX
556 EAX = env->regs[R_EAX];
557#endif
558#ifdef reg_ECX
559 ECX = env->regs[R_ECX];
560#endif
561#ifdef reg_EDX
562 EDX = env->regs[R_EDX];
563#endif
564#ifdef reg_EBX
565 EBX = env->regs[R_EBX];
566#endif
567#ifdef reg_ESP
568 ESP = env->regs[R_ESP];
569#endif
570#ifdef reg_EBP
571 EBP = env->regs[R_EBP];
572#endif
573#ifdef reg_ESI
574 ESI = env->regs[R_ESI];
575#endif
576#ifdef reg_EDI
577 EDI = env->regs[R_EDI];
578#endif
579}
580
581static inline void regs_to_env(void)
582{
583#ifdef reg_EAX
584 env->regs[R_EAX] = EAX;
585#endif
586#ifdef reg_ECX
587 env->regs[R_ECX] = ECX;
588#endif
589#ifdef reg_EDX
590 env->regs[R_EDX] = EDX;
591#endif
592#ifdef reg_EBX
593 env->regs[R_EBX] = EBX;
594#endif
595#ifdef reg_ESP
596 env->regs[R_ESP] = ESP;
597#endif
598#ifdef reg_EBP
599 env->regs[R_EBP] = EBP;
600#endif
601#ifdef reg_ESI
602 env->regs[R_ESI] = ESI;
603#endif
604#ifdef reg_EDI
605 env->regs[R_EDI] = EDI;
606#endif
607}
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