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 */
25 #define TARGET_LONG_BITS 64
27 #define TARGET_LONG_BITS 32
32 /* at least 4 register variables are defined */
33 register struct CPUX86State *env asm(AREG0);
35 #ifndef CPU_NO_GLOBAL_REGS
37 #if TARGET_LONG_BITS > HOST_LONG_BITS
39 /* no registers can be used */
46 /* XXX: use unsigned long instead of target_ulong - better code will
47 be generated for 64 bit CPUs */
48 register target_ulong T0 asm(AREG1);
49 register target_ulong T1 asm(AREG2);
50 register target_ulong T2 asm(AREG3);
52 #endif /* ! (TARGET_LONG_BITS > HOST_LONG_BITS) */
54 #endif /* ! CPU_NO_GLOBAL_REGS */
62 #define EAX (env->regs[R_EAX])
65 #define ECX (env->regs[R_ECX])
68 #define EDX (env->regs[R_EDX])
71 #define EBX (env->regs[R_EBX])
74 #define ESP (env->regs[R_ESP])
77 #define EBP (env->regs[R_EBP])
80 #define ESI (env->regs[R_ESI])
83 #define EDI (env->regs[R_EDI])
85 #define EIP (env->eip)
88 #define CC_SRC (env->cc_src)
89 #define CC_DST (env->cc_dst)
90 #define CC_OP (env->cc_op)
93 #define FT0 (env->ft0)
94 #define ST0 (env->fpregs[env->fpstt].d)
95 #define ST(n) (env->fpregs[(env->fpstt + (n)) & 7].d)
99 #define FP_CONVERT (env->fp_convert)
103 #include "exec-all.h"
105 typedef struct CCTable {
106 int (*compute_all)(void); /* return all the flags */
107 int (*compute_c)(void); /* return the C flag */
110 extern CCTable cc_table[];
112 void load_seg(int seg_reg, int selector);
113 void helper_ljmp_protected_T0_T1(int next_eip);
114 void helper_lcall_real_T0_T1(int shift, int next_eip);
115 void helper_lcall_protected_T0_T1(int shift, int next_eip);
116 void helper_iret_real(int shift);
117 void helper_iret_protected(int shift, int next_eip);
118 void helper_lret_protected(int shift, int addend);
119 void helper_lldt_T0(void);
120 void helper_ltr_T0(void);
121 void helper_movl_crN_T0(int reg);
122 void helper_movl_drN_T0(int reg);
123 void helper_invlpg(target_ulong addr);
124 void cpu_x86_update_cr0(CPUX86State *env, uint32_t new_cr0);
125 void cpu_x86_update_cr3(CPUX86State *env, target_ulong new_cr3);
126 void cpu_x86_update_cr4(CPUX86State *env, uint32_t new_cr4);
127 void cpu_x86_flush_tlb(CPUX86State *env, target_ulong addr);
128 int cpu_x86_handle_mmu_fault(CPUX86State *env, target_ulong addr,
129 int is_write, int mmu_idx, int is_softmmu);
130 void tlb_fill(target_ulong addr, int is_write, int mmu_idx,
132 void __hidden cpu_lock(void);
133 void __hidden cpu_unlock(void);
134 void do_interrupt(int intno, int is_int, int error_code,
135 target_ulong next_eip, int is_hw);
136 void do_interrupt_user(int intno, int is_int, int error_code,
137 target_ulong next_eip);
138 void raise_interrupt(int intno, int is_int, int error_code,
139 int next_eip_addend);
140 void raise_exception_err(int exception_index, int error_code);
141 void raise_exception(int exception_index);
142 void do_smm_enter(void);
143 void __hidden cpu_loop_exit(void);
145 void OPPROTO op_movl_eflags_T0(void);
146 void OPPROTO op_movl_T0_eflags(void);
150 void helper_mulq_EAX_T0(void);
151 void helper_imulq_EAX_T0(void);
152 void helper_imulq_T0_T1(void);
153 void helper_divq_EAX_T0(void);
154 void helper_idivq_EAX_T0(void);
155 void helper_bswapq_T0(void);
156 void helper_cmpxchg8b(void);
157 void helper_single_step(void);
158 void helper_cpuid(void);
159 void helper_enter_level(int level, int data32);
160 void helper_enter64_level(int level, int data64);
161 void helper_sysenter(void);
162 void helper_sysexit(void);
163 void helper_syscall(int next_eip_addend);
164 void helper_sysret(int dflag);
165 void helper_rdtsc(void);
166 void helper_rdpmc(void);
167 void helper_rdmsr(void);
168 void helper_wrmsr(void);
169 void helper_lsl(void);
170 void helper_lar(void);
171 void helper_verr(void);
172 void helper_verw(void);
173 void helper_rsm(void);
175 void check_iob_T0(void);
176 void check_iow_T0(void);
177 void check_iol_T0(void);
178 void check_iob_DX(void);
179 void check_iow_DX(void);
180 void check_iol_DX(void);
182 #if !defined(CONFIG_USER_ONLY)
184 #include "softmmu_exec.h"
186 static inline double ldfq(target_ulong ptr)
196 static inline void stfq(target_ulong ptr, double v)
206 static inline float ldfl(target_ulong ptr)
216 static inline void stfl(target_ulong ptr, float v)
226 #endif /* !defined(CONFIG_USER_ONLY) */
228 #ifdef USE_X86LDOUBLE
229 /* use long double functions */
230 #define floatx_to_int32 floatx80_to_int32
231 #define floatx_to_int64 floatx80_to_int64
232 #define floatx_to_int32_round_to_zero floatx80_to_int32_round_to_zero
233 #define floatx_to_int64_round_to_zero floatx80_to_int64_round_to_zero
234 #define floatx_abs floatx80_abs
235 #define floatx_chs floatx80_chs
236 #define floatx_round_to_int floatx80_round_to_int
237 #define floatx_compare floatx80_compare
238 #define floatx_compare_quiet floatx80_compare_quiet
250 #define floatx_to_int32 float64_to_int32
251 #define floatx_to_int64 float64_to_int64
252 #define floatx_to_int32_round_to_zero float64_to_int32_round_to_zero
253 #define floatx_to_int64_round_to_zero float64_to_int64_round_to_zero
254 #define floatx_abs float64_abs
255 #define floatx_chs float64_chs
256 #define floatx_round_to_int float64_round_to_int
257 #define floatx_compare float64_compare
258 #define floatx_compare_quiet float64_compare_quiet
261 extern CPU86_LDouble sin(CPU86_LDouble x);
262 extern CPU86_LDouble cos(CPU86_LDouble x);
263 extern CPU86_LDouble sqrt(CPU86_LDouble x);
264 extern CPU86_LDouble pow(CPU86_LDouble, CPU86_LDouble);
265 extern CPU86_LDouble log(CPU86_LDouble x);
266 extern CPU86_LDouble tan(CPU86_LDouble x);
267 extern CPU86_LDouble atan2(CPU86_LDouble, CPU86_LDouble);
268 extern CPU86_LDouble floor(CPU86_LDouble x);
269 extern CPU86_LDouble ceil(CPU86_LDouble x);
271 #define RC_MASK 0xc00
272 #define RC_NEAR 0x000
273 #define RC_DOWN 0x400
275 #define RC_CHOP 0xc00
277 #define MAXTAN 9223372036854775808.0
279 #ifdef USE_X86LDOUBLE
285 unsigned long long lower;
286 unsigned short upper;
290 /* the following deal with x86 long double-precision numbers */
291 #define MAXEXPD 0x7fff
292 #define EXPBIAS 16383
293 #define EXPD(fp) (fp.l.upper & 0x7fff)
294 #define SIGND(fp) ((fp.l.upper) & 0x8000)
295 #define MANTD(fp) (fp.l.lower)
296 #define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7fff)) | EXPBIAS
300 /* NOTE: arm is horrible as double 32 bit words are stored in big endian ! */
303 #if !defined(WORDS_BIGENDIAN) && !defined(__arm__)
319 /* the following deal with IEEE double-precision numbers */
320 #define MAXEXPD 0x7ff
322 #define EXPD(fp) (((fp.l.upper) >> 20) & 0x7FF)
323 #define SIGND(fp) ((fp.l.upper) & 0x80000000)
325 #define MANTD(fp) (fp.l.lower | ((uint64_t)(fp.l.upper & ((1 << 20) - 1)) << 32))
327 #define MANTD(fp) (fp.ll & ((1LL << 52) - 1))
329 #define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7ff << 20)) | (EXPBIAS << 20)
332 static inline void fpush(void)
334 env->fpstt = (env->fpstt - 1) & 7;
335 env->fptags[env->fpstt] = 0; /* validate stack entry */
338 static inline void fpop(void)
340 env->fptags[env->fpstt] = 1; /* invvalidate stack entry */
341 env->fpstt = (env->fpstt + 1) & 7;
344 #ifndef USE_X86LDOUBLE
345 static inline CPU86_LDouble helper_fldt(target_ulong ptr)
352 upper = lduw(ptr + 8);
353 /* XXX: handle overflow ? */
354 e = (upper & 0x7fff) - 16383 + EXPBIAS; /* exponent */
355 e |= (upper >> 4) & 0x800; /* sign */
356 ll = (ldq(ptr) >> 11) & ((1LL << 52) - 1);
358 temp.l.upper = (e << 20) | (ll >> 32);
361 temp.ll = ll | ((uint64_t)e << 52);
366 static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
373 stq(ptr, (MANTD(temp) << 11) | (1LL << 63));
374 /* exponent + sign */
375 e = EXPD(temp) - EXPBIAS + 16383;
376 e |= SIGND(temp) >> 16;
381 /* XXX: same endianness assumed */
383 #ifdef CONFIG_USER_ONLY
385 static inline CPU86_LDouble helper_fldt(target_ulong ptr)
387 return *(CPU86_LDouble *)(unsigned long)ptr;
390 static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
392 *(CPU86_LDouble *)(unsigned long)ptr = f;
397 /* we use memory access macros */
399 static inline CPU86_LDouble helper_fldt(target_ulong ptr)
403 temp.l.lower = ldq(ptr);
404 temp.l.upper = lduw(ptr + 8);
408 static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
413 stq(ptr, temp.l.lower);
414 stw(ptr + 8, temp.l.upper);
417 #endif /* !CONFIG_USER_ONLY */
419 #endif /* USE_X86LDOUBLE */
421 #define FPUS_IE (1 << 0)
422 #define FPUS_DE (1 << 1)
423 #define FPUS_ZE (1 << 2)
424 #define FPUS_OE (1 << 3)
425 #define FPUS_UE (1 << 4)
426 #define FPUS_PE (1 << 5)
427 #define FPUS_SF (1 << 6)
428 #define FPUS_SE (1 << 7)
429 #define FPUS_B (1 << 15)
433 extern const CPU86_LDouble f15rk[7];
435 void helper_fldt_ST0_A0(void);
436 void helper_fstt_ST0_A0(void);
437 void fpu_raise_exception(void);
438 CPU86_LDouble helper_fdiv(CPU86_LDouble a, CPU86_LDouble b);
439 void helper_fbld_ST0_A0(void);
440 void helper_fbst_ST0_A0(void);
441 void helper_f2xm1(void);
442 void helper_fyl2x(void);
443 void helper_fptan(void);
444 void helper_fpatan(void);
445 void helper_fxtract(void);
446 void helper_fprem1(void);
447 void helper_fprem(void);
448 void helper_fyl2xp1(void);
449 void helper_fsqrt(void);
450 void helper_fsincos(void);
451 void helper_frndint(void);
452 void helper_fscale(void);
453 void helper_fsin(void);
454 void helper_fcos(void);
455 void helper_fxam_ST0(void);
456 void helper_fstenv(target_ulong ptr, int data32);
457 void helper_fldenv(target_ulong ptr, int data32);
458 void helper_fsave(target_ulong ptr, int data32);
459 void helper_frstor(target_ulong ptr, int data32);
460 void helper_fxsave(target_ulong ptr, int data64);
461 void helper_fxrstor(target_ulong ptr, int data64);
462 void restore_native_fp_state(CPUState *env);
463 void save_native_fp_state(CPUState *env);
464 float approx_rsqrt(float a);
465 float approx_rcp(float a);
466 void update_fp_status(void);
467 void helper_hlt(void);
468 void helper_monitor(void);
469 void helper_mwait(void);
470 void helper_vmrun(target_ulong addr);
471 void helper_vmmcall(void);
472 void helper_vmload(target_ulong addr);
473 void helper_vmsave(target_ulong addr);
474 void helper_stgi(void);
475 void helper_clgi(void);
476 void helper_skinit(void);
477 void helper_invlpga(void);
478 void vmexit(uint64_t exit_code, uint64_t exit_info_1);
480 extern const uint8_t parity_table[256];
481 extern const uint8_t rclw_table[32];
482 extern const uint8_t rclb_table[32];
484 static inline uint32_t compute_eflags(void)
486 return env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
489 /* NOTE: CC_OP must be modified manually to CC_OP_EFLAGS */
490 static inline void load_eflags(int eflags, int update_mask)
492 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
493 DF = 1 - (2 * ((eflags >> 10) & 1));
494 env->eflags = (env->eflags & ~update_mask) |
495 (eflags & update_mask);
498 static inline void env_to_regs(void)
501 EAX = env->regs[R_EAX];
504 ECX = env->regs[R_ECX];
507 EDX = env->regs[R_EDX];
510 EBX = env->regs[R_EBX];
513 ESP = env->regs[R_ESP];
516 EBP = env->regs[R_EBP];
519 ESI = env->regs[R_ESI];
522 EDI = env->regs[R_EDI];
526 static inline void regs_to_env(void)
529 env->regs[R_EAX] = EAX;
532 env->regs[R_ECX] = ECX;
535 env->regs[R_EDX] = EDX;
538 env->regs[R_EBX] = EBX;
541 env->regs[R_ESP] = ESP;
544 env->regs[R_EBP] = EBP;
547 env->regs[R_ESI] = ESI;
550 env->regs[R_EDI] = EDI;
554 static inline int cpu_halted(CPUState *env) {
555 /* handle exit of HALTED state */
556 if (!(env->hflags & HF_HALTED_MASK))
558 /* disable halt condition */
559 if (((env->interrupt_request & CPU_INTERRUPT_HARD) &&
560 (env->eflags & IF_MASK)) ||
561 (env->interrupt_request & CPU_INTERRUPT_NMI)) {
562 env->hflags &= ~HF_HALTED_MASK;