VirtualBox

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

Last change on this file since 14658 was 14411, checked in by vboxsync, 16 years ago

RDTSCP support added. Enabled only for AMD-V guests.

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