2 * i386 execution defines
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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
20 #include "dyngen-exec.h"
22 /* at least 4 register variables are defines */
23 register struct CPUX86State
*env
asm(AREG0
);
24 register uint32_t T0
asm(AREG1
);
25 register uint32_t T1
asm(AREG2
);
26 register uint32_t T2
asm(AREG3
);
30 /* if more registers are available, we define some registers too */
32 register uint32_t EAX
asm(AREG4
);
37 register uint32_t ESP
asm(AREG5
);
42 register uint32_t EBP
asm(AREG6
);
47 register uint32_t ECX
asm(AREG7
);
52 register uint32_t EDX
asm(AREG8
);
57 register uint32_t EBX
asm(AREG9
);
62 register uint32_t ESI
asm(AREG10
);
67 register uint32_t EDI
asm(AREG11
);
75 #define EAX (env->regs[R_EAX])
78 #define ECX (env->regs[R_ECX])
81 #define EDX (env->regs[R_EDX])
84 #define EBX (env->regs[R_EBX])
87 #define ESP (env->regs[R_ESP])
90 #define EBP (env->regs[R_EBP])
93 #define ESI (env->regs[R_ESI])
96 #define EDI (env->regs[R_EDI])
98 #define EIP (env->eip)
101 #define CC_SRC (env->cc_src)
102 #define CC_DST (env->cc_dst)
103 #define CC_OP (env->cc_op)
106 #define FT0 (env->ft0)
107 #define ST0 (env->fpregs[env->fpstt])
108 #define ST(n) (env->fpregs[(env->fpstt + (n)) & 7])
111 #ifdef USE_FP_CONVERT
112 #define FP_CONVERT (env->fp_convert)
116 #include "exec-all.h"
118 typedef struct CCTable
{
119 int (*compute_all
)(void); /* return all the flags */
120 int (*compute_c
)(void); /* return the C flag */
123 extern CCTable cc_table
[];
125 void load_seg(int seg_reg
, int selector
, unsigned cur_eip
);
126 void helper_ljmp_protected_T0_T1(void);
127 void helper_lcall_real_T0_T1(int shift
, int next_eip
);
128 void helper_lcall_protected_T0_T1(int shift
, int next_eip
);
129 void helper_iret_real(int shift
);
130 void helper_iret_protected(int shift
);
131 void helper_lret_protected(int shift
, int addend
);
132 void helper_lldt_T0(void);
133 void helper_ltr_T0(void);
134 void helper_movl_crN_T0(int reg
);
135 void helper_movl_drN_T0(int reg
);
136 void helper_invlpg(unsigned int addr
);
137 void cpu_x86_update_cr0(CPUX86State
*env
);
138 void cpu_x86_update_cr3(CPUX86State
*env
);
139 void cpu_x86_flush_tlb(CPUX86State
*env
, uint32_t addr
);
140 int cpu_x86_handle_mmu_fault(CPUX86State
*env
, uint32_t addr
,
141 int is_write
, int is_user
, int is_softmmu
);
142 void tlb_fill(unsigned long addr
, int is_write
, int is_user
,
144 void __hidden
cpu_lock(void);
145 void __hidden
cpu_unlock(void);
146 void do_interrupt(int intno
, int is_int
, int error_code
,
147 unsigned int next_eip
, int is_hw
);
148 void do_interrupt_user(int intno
, int is_int
, int error_code
,
149 unsigned int next_eip
);
150 void raise_interrupt(int intno
, int is_int
, int error_code
,
151 unsigned int next_eip
);
152 void raise_exception_err(int exception_index
, int error_code
);
153 void raise_exception(int exception_index
);
154 void __hidden
cpu_loop_exit(void);
155 void helper_fsave(uint8_t *ptr
, int data32
);
156 void helper_frstor(uint8_t *ptr
, int data32
);
158 void OPPROTO
op_movl_eflags_T0(void);
159 void OPPROTO
op_movl_T0_eflags(void);
160 void raise_interrupt(int intno
, int is_int
, int error_code
,
161 unsigned int next_eip
);
162 void raise_exception_err(int exception_index
, int error_code
);
163 void raise_exception(int exception_index
);
164 void helper_divl_EAX_T0(uint32_t eip
);
165 void helper_idivl_EAX_T0(uint32_t eip
);
166 void helper_cmpxchg8b(void);
167 void helper_cpuid(void);
168 void helper_rdtsc(void);
169 void helper_rdmsr(void);
170 void helper_wrmsr(void);
171 void helper_lsl(void);
172 void helper_lar(void);
173 void helper_verr(void);
174 void helper_verw(void);
176 void check_iob_T0(void);
177 void check_iow_T0(void);
178 void check_iol_T0(void);
179 void check_iob_DX(void);
180 void check_iow_DX(void);
181 void check_iol_DX(void);
183 /* XXX: move that to a generic header */
184 #if !defined(CONFIG_USER_ONLY)
186 #define ldul_user ldl_user
187 #define ldul_kernel ldl_kernel
189 #define ACCESS_TYPE 0
190 #define MEMSUFFIX _kernel
192 #include "softmmu_header.h"
195 #include "softmmu_header.h"
198 #include "softmmu_header.h"
201 #include "softmmu_header.h"
205 #define ACCESS_TYPE 1
206 #define MEMSUFFIX _user
208 #include "softmmu_header.h"
211 #include "softmmu_header.h"
214 #include "softmmu_header.h"
217 #include "softmmu_header.h"
221 /* these access are slower, they must be as rare as possible */
222 #define ACCESS_TYPE 2
223 #define MEMSUFFIX _data
225 #include "softmmu_header.h"
228 #include "softmmu_header.h"
231 #include "softmmu_header.h"
234 #include "softmmu_header.h"
238 #define ldub(p) ldub_data(p)
239 #define ldsb(p) ldsb_data(p)
240 #define lduw(p) lduw_data(p)
241 #define ldsw(p) ldsw_data(p)
242 #define ldl(p) ldl_data(p)
243 #define ldq(p) ldq_data(p)
245 #define stb(p, v) stb_data(p, v)
246 #define stw(p, v) stw_data(p, v)
247 #define stl(p, v) stl_data(p, v)
248 #define stq(p, v) stq_data(p, v)
250 static inline double ldfq(void *ptr
)
260 static inline void stfq(void *ptr
, double v
)
270 static inline float ldfl(void *ptr
)
280 static inline void stfl(void *ptr
, float v
)
290 #endif /* !defined(CONFIG_USER_ONLY) */
292 #ifdef USE_X86LDOUBLE
293 /* use long double functions */
295 #define llrint llrintl
309 extern int lrint(CPU86_LDouble x
);
310 extern int64_t llrint(CPU86_LDouble x
);
311 extern CPU86_LDouble
fabs(CPU86_LDouble x
);
312 extern CPU86_LDouble
sin(CPU86_LDouble x
);
313 extern CPU86_LDouble
cos(CPU86_LDouble x
);
314 extern CPU86_LDouble
sqrt(CPU86_LDouble x
);
315 extern CPU86_LDouble
pow(CPU86_LDouble
, CPU86_LDouble
);
316 extern CPU86_LDouble
log(CPU86_LDouble x
);
317 extern CPU86_LDouble
tan(CPU86_LDouble x
);
318 extern CPU86_LDouble
atan2(CPU86_LDouble
, CPU86_LDouble
);
319 extern CPU86_LDouble
floor(CPU86_LDouble x
);
320 extern CPU86_LDouble
ceil(CPU86_LDouble x
);
321 extern CPU86_LDouble
rint(CPU86_LDouble x
);
323 #define RC_MASK 0xc00
324 #define RC_NEAR 0x000
325 #define RC_DOWN 0x400
327 #define RC_CHOP 0xc00
329 #define MAXTAN 9223372036854775808.0
332 /* we have no way to do correct rounding - a FPU emulator is needed */
333 #define FE_DOWNWARD FE_TONEAREST
334 #define FE_UPWARD FE_TONEAREST
335 #define FE_TOWARDZERO FE_TONEAREST
338 #ifdef USE_X86LDOUBLE
344 unsigned long long lower
;
345 unsigned short upper
;
349 /* the following deal with x86 long double-precision numbers */
350 #define MAXEXPD 0x7fff
351 #define EXPBIAS 16383
352 #define EXPD(fp) (fp.l.upper & 0x7fff)
353 #define SIGND(fp) ((fp.l.upper) & 0x8000)
354 #define MANTD(fp) (fp.l.lower)
355 #define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7fff)) | EXPBIAS
359 /* NOTE: arm is horrible as double 32 bit words are stored in big endian ! */
362 #if !defined(WORDS_BIGENDIAN) && !defined(__arm__)
378 /* the following deal with IEEE double-precision numbers */
379 #define MAXEXPD 0x7ff
381 #define EXPD(fp) (((fp.l.upper) >> 20) & 0x7FF)
382 #define SIGND(fp) ((fp.l.upper) & 0x80000000)
384 #define MANTD(fp) (fp.l.lower | ((uint64_t)(fp.l.upper & ((1 << 20) - 1)) << 32))
386 #define MANTD(fp) (fp.ll & ((1LL << 52) - 1))
388 #define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7ff << 20)) | (EXPBIAS << 20)
391 static inline void fpush(void)
393 env
->fpstt
= (env
->fpstt
- 1) & 7;
394 env
->fptags
[env
->fpstt
] = 0; /* validate stack entry */
397 static inline void fpop(void)
399 env
->fptags
[env
->fpstt
] = 1; /* invvalidate stack entry */
400 env
->fpstt
= (env
->fpstt
+ 1) & 7;
403 #ifndef USE_X86LDOUBLE
404 static inline CPU86_LDouble
helper_fldt(uint8_t *ptr
)
411 upper
= lduw(ptr
+ 8);
412 /* XXX: handle overflow ? */
413 e
= (upper
& 0x7fff) - 16383 + EXPBIAS
; /* exponent */
414 e
|= (upper
>> 4) & 0x800; /* sign */
415 ll
= (ldq(ptr
) >> 11) & ((1LL << 52) - 1);
417 temp
.l
.upper
= (e
<< 20) | (ll
>> 32);
420 temp
.ll
= ll
| ((uint64_t)e
<< 52);
425 static inline void helper_fstt(CPU86_LDouble f
, uint8_t *ptr
)
432 stq(ptr
, (MANTD(temp
) << 11) | (1LL << 63));
433 /* exponent + sign */
434 e
= EXPD(temp
) - EXPBIAS
+ 16383;
435 e
|= SIGND(temp
) >> 16;
440 /* XXX: same endianness assumed */
442 #ifdef CONFIG_USER_ONLY
444 static inline CPU86_LDouble
helper_fldt(uint8_t *ptr
)
446 return *(CPU86_LDouble
*)ptr
;
449 static inline void helper_fstt(CPU86_LDouble f
, uint8_t *ptr
)
451 *(CPU86_LDouble
*)ptr
= f
;
456 /* we use memory access macros */
458 static inline CPU86_LDouble
helper_fldt(uint8_t *ptr
)
462 temp
.l
.lower
= ldq(ptr
);
463 temp
.l
.upper
= lduw(ptr
+ 8);
467 static inline void helper_fstt(CPU86_LDouble f
, uint8_t *ptr
)
472 stq(ptr
, temp
.l
.lower
);
473 stw(ptr
+ 8, temp
.l
.upper
);
476 #endif /* !CONFIG_USER_ONLY */
478 #endif /* USE_X86LDOUBLE */
480 const CPU86_LDouble f15rk
[7];
482 void helper_fldt_ST0_A0(void);
483 void helper_fstt_ST0_A0(void);
484 void helper_fbld_ST0_A0(void);
485 void helper_fbst_ST0_A0(void);
486 void helper_f2xm1(void);
487 void helper_fyl2x(void);
488 void helper_fptan(void);
489 void helper_fpatan(void);
490 void helper_fxtract(void);
491 void helper_fprem1(void);
492 void helper_fprem(void);
493 void helper_fyl2xp1(void);
494 void helper_fsqrt(void);
495 void helper_fsincos(void);
496 void helper_frndint(void);
497 void helper_fscale(void);
498 void helper_fsin(void);
499 void helper_fcos(void);
500 void helper_fxam_ST0(void);
501 void helper_fstenv(uint8_t *ptr
, int data32
);
502 void helper_fldenv(uint8_t *ptr
, int data32
);
503 void helper_fsave(uint8_t *ptr
, int data32
);
504 void helper_frstor(uint8_t *ptr
, int data32
);
506 const uint8_t parity_table
[256];
507 const uint8_t rclw_table
[32];
508 const uint8_t rclb_table
[32];
510 static inline uint32_t compute_eflags(void)
512 return env
->eflags
| cc_table
[CC_OP
].compute_all() | (DF
& DF_MASK
);
515 /* NOTE: CC_OP must be modified manually to CC_OP_EFLAGS */
516 static inline void load_eflags(int eflags
, int update_mask
)
518 CC_SRC
= eflags
& (CC_O
| CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
);
519 DF
= 1 - (2 * ((eflags
>> 10) & 1));
520 env
->eflags
= (env
->eflags
& ~update_mask
) |
521 (eflags
& update_mask
);