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
21 #include "dyngen-exec.h"
23 /* XXX: factorize this mess */
24 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
25 #define HOST_LONG_BITS 64
27 #define HOST_LONG_BITS 32
31 #define TARGET_LONG_BITS 64
33 #define TARGET_LONG_BITS 32
36 /* at least 4 register variables are defined */
37 register struct CPUX86State *env asm(AREG0);
39 /* XXX: use 64 bit regs if HOST_LONG_BITS == 64 */
40 #if TARGET_LONG_BITS == 32
42 register uint32_t T0 asm(AREG1);
43 register uint32_t T1 asm(AREG2);
44 register uint32_t T2 asm(AREG3);
46 /* if more registers are available, we define some registers too */
48 register uint32_t EAX asm(AREG4);
53 register uint32_t ESP asm(AREG5);
58 register uint32_t EBP asm(AREG6);
63 register uint32_t ECX asm(AREG7);
68 register uint32_t EDX asm(AREG8);
73 register uint32_t EBX asm(AREG9);
78 register uint32_t ESI asm(AREG10);
83 register uint32_t EDI asm(AREG11);
89 /* no registers can be used */
102 #define EAX (env->regs[R_EAX])
105 #define ECX (env->regs[R_ECX])
108 #define EDX (env->regs[R_EDX])
111 #define EBX (env->regs[R_EBX])
114 #define ESP (env->regs[R_ESP])
117 #define EBP (env->regs[R_EBP])
120 #define ESI (env->regs[R_ESI])
123 #define EDI (env->regs[R_EDI])
125 #define EIP (env->eip)
128 #define CC_SRC (env->cc_src)
129 #define CC_DST (env->cc_dst)
130 #define CC_OP (env->cc_op)
133 #define FT0 (env->ft0)
134 #define ST0 (env->fpregs[env->fpstt])
135 #define ST(n) (env->fpregs[(env->fpstt + (n)) & 7])
138 #ifdef USE_FP_CONVERT
139 #define FP_CONVERT (env->fp_convert)
143 #include "exec-all.h"
145 typedef struct CCTable {
146 int (*compute_all)(void); /* return all the flags */
147 int (*compute_c)(void); /* return the C flag */
150 extern CCTable cc_table[];
152 void load_seg(int seg_reg, int selector);
153 void helper_ljmp_protected_T0_T1(int next_eip);
154 void helper_lcall_real_T0_T1(int shift, int next_eip);
155 void helper_lcall_protected_T0_T1(int shift, int next_eip);
156 void helper_iret_real(int shift);
157 void helper_iret_protected(int shift, int next_eip);
158 void helper_lret_protected(int shift, int addend);
159 void helper_lldt_T0(void);
160 void helper_ltr_T0(void);
161 void helper_movl_crN_T0(int reg);
162 void helper_movl_drN_T0(int reg);
163 void helper_invlpg(unsigned int addr);
164 void cpu_x86_update_cr0(CPUX86State *env, uint32_t new_cr0);
165 void cpu_x86_update_cr3(CPUX86State *env, target_ulong new_cr3);
166 void cpu_x86_update_cr4(CPUX86State *env, uint32_t new_cr4);
167 void cpu_x86_flush_tlb(CPUX86State *env, uint32_t addr);
168 int cpu_x86_handle_mmu_fault(CPUX86State *env, target_ulong addr,
169 int is_write, int is_user, int is_softmmu);
170 void tlb_fill(target_ulong addr, int is_write, int is_user,
172 void __hidden cpu_lock(void);
173 void __hidden cpu_unlock(void);
174 void do_interrupt(int intno, int is_int, int error_code,
175 target_ulong next_eip, int is_hw);
176 void do_interrupt_user(int intno, int is_int, int error_code,
177 target_ulong next_eip);
178 void raise_interrupt(int intno, int is_int, int error_code,
179 unsigned int next_eip);
180 void raise_exception_err(int exception_index, int error_code);
181 void raise_exception(int exception_index);
182 void __hidden cpu_loop_exit(void);
184 void OPPROTO op_movl_eflags_T0(void);
185 void OPPROTO op_movl_T0_eflags(void);
186 void raise_interrupt(int intno, int is_int, int error_code,
187 unsigned int next_eip);
188 void raise_exception_err(int exception_index, int error_code);
189 void raise_exception(int exception_index);
190 void helper_divl_EAX_T0(void);
191 void helper_idivl_EAX_T0(void);
192 void helper_mulq_EAX_T0(void);
193 void helper_imulq_EAX_T0(void);
194 void helper_imulq_T0_T1(void);
195 void helper_divq_EAX_T0(void);
196 void helper_idivq_EAX_T0(void);
197 void helper_cmpxchg8b(void);
198 void helper_cpuid(void);
199 void helper_enter_level(int level, int data32);
200 void helper_sysenter(void);
201 void helper_sysexit(void);
202 void helper_syscall(int next_eip_addend);
203 void helper_sysret(int dflag);
204 void helper_rdtsc(void);
205 void helper_rdmsr(void);
206 void helper_wrmsr(void);
207 void helper_lsl(void);
208 void helper_lar(void);
209 void helper_verr(void);
210 void helper_verw(void);
212 void check_iob_T0(void);
213 void check_iow_T0(void);
214 void check_iol_T0(void);
215 void check_iob_DX(void);
216 void check_iow_DX(void);
217 void check_iol_DX(void);
219 /* XXX: move that to a generic header */
220 #if !defined(CONFIG_USER_ONLY)
222 #define ldul_user ldl_user
223 #define ldul_kernel ldl_kernel
225 #define ACCESS_TYPE 0
226 #define MEMSUFFIX _kernel
228 #include "softmmu_header.h"
231 #include "softmmu_header.h"
234 #include "softmmu_header.h"
237 #include "softmmu_header.h"
241 #define ACCESS_TYPE 1
242 #define MEMSUFFIX _user
244 #include "softmmu_header.h"
247 #include "softmmu_header.h"
250 #include "softmmu_header.h"
253 #include "softmmu_header.h"
257 /* these access are slower, they must be as rare as possible */
258 #define ACCESS_TYPE 2
259 #define MEMSUFFIX _data
261 #include "softmmu_header.h"
264 #include "softmmu_header.h"
267 #include "softmmu_header.h"
270 #include "softmmu_header.h"
274 #define ldub(p) ldub_data(p)
275 #define ldsb(p) ldsb_data(p)
276 #define lduw(p) lduw_data(p)
277 #define ldsw(p) ldsw_data(p)
278 #define ldl(p) ldl_data(p)
279 #define ldq(p) ldq_data(p)
281 #define stb(p, v) stb_data(p, v)
282 #define stw(p, v) stw_data(p, v)
283 #define stl(p, v) stl_data(p, v)
284 #define stq(p, v) stq_data(p, v)
286 static inline double ldfq(target_ulong ptr)
296 static inline void stfq(target_ulong ptr, double v)
306 static inline float ldfl(target_ulong ptr)
316 static inline void stfl(target_ulong ptr, float v)
326 #endif /* !defined(CONFIG_USER_ONLY) */
328 #ifdef USE_X86LDOUBLE
329 /* use long double functions */
331 #define llrint llrintl
346 extern int lrint(CPU86_LDouble x);
347 extern int64_t llrint(CPU86_LDouble x);
349 #define lrint(d) ((int)rint(d))
350 #define llrint(d) ((int)rint(d))
352 extern CPU86_LDouble fabs(CPU86_LDouble x);
353 extern CPU86_LDouble sin(CPU86_LDouble x);
354 extern CPU86_LDouble cos(CPU86_LDouble x);
355 extern CPU86_LDouble sqrt(CPU86_LDouble x);
356 extern CPU86_LDouble pow(CPU86_LDouble, CPU86_LDouble);
357 extern CPU86_LDouble log(CPU86_LDouble x);
358 extern CPU86_LDouble tan(CPU86_LDouble x);
359 extern CPU86_LDouble atan2(CPU86_LDouble, CPU86_LDouble);
360 extern CPU86_LDouble floor(CPU86_LDouble x);
361 extern CPU86_LDouble ceil(CPU86_LDouble x);
362 extern CPU86_LDouble rint(CPU86_LDouble x);
364 #define RC_MASK 0xc00
365 #define RC_NEAR 0x000
366 #define RC_DOWN 0x400
368 #define RC_CHOP 0xc00
370 #define MAXTAN 9223372036854775808.0
373 /* we have no way to do correct rounding - a FPU emulator is needed */
374 #define FE_DOWNWARD FE_TONEAREST
375 #define FE_UPWARD FE_TONEAREST
376 #define FE_TOWARDZERO FE_TONEAREST
379 #ifdef USE_X86LDOUBLE
385 unsigned long long lower;
386 unsigned short upper;
390 /* the following deal with x86 long double-precision numbers */
391 #define MAXEXPD 0x7fff
392 #define EXPBIAS 16383
393 #define EXPD(fp) (fp.l.upper & 0x7fff)
394 #define SIGND(fp) ((fp.l.upper) & 0x8000)
395 #define MANTD(fp) (fp.l.lower)
396 #define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7fff)) | EXPBIAS
400 /* NOTE: arm is horrible as double 32 bit words are stored in big endian ! */
403 #if !defined(WORDS_BIGENDIAN) && !defined(__arm__)
419 /* the following deal with IEEE double-precision numbers */
420 #define MAXEXPD 0x7ff
422 #define EXPD(fp) (((fp.l.upper) >> 20) & 0x7FF)
423 #define SIGND(fp) ((fp.l.upper) & 0x80000000)
425 #define MANTD(fp) (fp.l.lower | ((uint64_t)(fp.l.upper & ((1 << 20) - 1)) << 32))
427 #define MANTD(fp) (fp.ll & ((1LL << 52) - 1))
429 #define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7ff << 20)) | (EXPBIAS << 20)
432 static inline void fpush(void)
434 env->fpstt = (env->fpstt - 1) & 7;
435 env->fptags[env->fpstt] = 0; /* validate stack entry */
438 static inline void fpop(void)
440 env->fptags[env->fpstt] = 1; /* invvalidate stack entry */
441 env->fpstt = (env->fpstt + 1) & 7;
444 #ifndef USE_X86LDOUBLE
445 static inline CPU86_LDouble helper_fldt(target_ulong ptr)
452 upper = lduw(ptr + 8);
453 /* XXX: handle overflow ? */
454 e = (upper & 0x7fff) - 16383 + EXPBIAS; /* exponent */
455 e |= (upper >> 4) & 0x800; /* sign */
456 ll = (ldq(ptr) >> 11) & ((1LL << 52) - 1);
458 temp.l.upper = (e << 20) | (ll >> 32);
461 temp.ll = ll | ((uint64_t)e << 52);
466 static inline void helper_fstt(CPU86_LDouble f, uint8_t *ptr)
473 stq(ptr, (MANTD(temp) << 11) | (1LL << 63));
474 /* exponent + sign */
475 e = EXPD(temp) - EXPBIAS + 16383;
476 e |= SIGND(temp) >> 16;
481 /* XXX: same endianness assumed */
483 #ifdef CONFIG_USER_ONLY
485 static inline CPU86_LDouble helper_fldt(target_ulong ptr)
487 return *(CPU86_LDouble *)ptr;
490 static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
492 *(CPU86_LDouble *)ptr = f;
497 /* we use memory access macros */
499 static inline CPU86_LDouble helper_fldt(target_ulong ptr)
503 temp.l.lower = ldq(ptr);
504 temp.l.upper = lduw(ptr + 8);
508 static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
513 stq(ptr, temp.l.lower);
514 stw(ptr + 8, temp.l.upper);
517 #endif /* !CONFIG_USER_ONLY */
519 #endif /* USE_X86LDOUBLE */
521 #define FPUS_IE (1 << 0)
522 #define FPUS_DE (1 << 1)
523 #define FPUS_ZE (1 << 2)
524 #define FPUS_OE (1 << 3)
525 #define FPUS_UE (1 << 4)
526 #define FPUS_PE (1 << 5)
527 #define FPUS_SF (1 << 6)
528 #define FPUS_SE (1 << 7)
529 #define FPUS_B (1 << 15)
533 extern const CPU86_LDouble f15rk[7];
535 void helper_fldt_ST0_A0(void);
536 void helper_fstt_ST0_A0(void);
537 void fpu_raise_exception(void);
538 CPU86_LDouble helper_fdiv(CPU86_LDouble a, CPU86_LDouble b);
539 void helper_fbld_ST0_A0(void);
540 void helper_fbst_ST0_A0(void);
541 void helper_f2xm1(void);
542 void helper_fyl2x(void);
543 void helper_fptan(void);
544 void helper_fpatan(void);
545 void helper_fxtract(void);
546 void helper_fprem1(void);
547 void helper_fprem(void);
548 void helper_fyl2xp1(void);
549 void helper_fsqrt(void);
550 void helper_fsincos(void);
551 void helper_frndint(void);
552 void helper_fscale(void);
553 void helper_fsin(void);
554 void helper_fcos(void);
555 void helper_fxam_ST0(void);
556 void helper_fstenv(target_ulong ptr, int data32);
557 void helper_fldenv(target_ulong ptr, int data32);
558 void helper_fsave(target_ulong ptr, int data32);
559 void helper_frstor(target_ulong ptr, int data32);
560 void helper_fxsave(target_ulong ptr, int data64);
561 void helper_fxrstor(target_ulong ptr, int data64);
562 void restore_native_fp_state(CPUState *env);
563 void save_native_fp_state(CPUState *env);
565 extern const uint8_t parity_table[256];
566 extern const uint8_t rclw_table[32];
567 extern const uint8_t rclb_table[32];
569 static inline uint32_t compute_eflags(void)
571 return env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
574 /* NOTE: CC_OP must be modified manually to CC_OP_EFLAGS */
575 static inline void load_eflags(int eflags, int update_mask)
577 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
578 DF = 1 - (2 * ((eflags >> 10) & 1));
579 env->eflags = (env->eflags & ~update_mask) |
580 (eflags & update_mask);
583 static inline void env_to_regs(void)
586 EAX = env->regs[R_EAX];
589 ECX = env->regs[R_ECX];
592 EDX = env->regs[R_EDX];
595 EBX = env->regs[R_EBX];
598 ESP = env->regs[R_ESP];
601 EBP = env->regs[R_EBP];
604 ESI = env->regs[R_ESI];
607 EDI = env->regs[R_EDI];
611 static inline void regs_to_env(void)
614 env->regs[R_EAX] = EAX;
617 env->regs[R_ECX] = ECX;
620 env->regs[R_EDX] = EDX;
623 env->regs[R_EBX] = EBX;
626 env->regs[R_ESP] = ESP;
629 env->regs[R_EBP] = EBP;
632 env->regs[R_ESI] = ESI;
635 env->regs[R_EDI] = EDI;