]> git.proxmox.com Git - mirror_qemu.git/blob - target-i386/exec.h
division by zero FPU exception support
[mirror_qemu.git] / target-i386 / exec.h
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 "dyngen-exec.h"
21
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);
27
28 #define A0 T2
29
30 /* if more registers are available, we define some registers too */
31 #ifdef AREG4
32 register uint32_t EAX asm(AREG4);
33 #define reg_EAX
34 #endif
35
36 #ifdef AREG5
37 register uint32_t ESP asm(AREG5);
38 #define reg_ESP
39 #endif
40
41 #ifdef AREG6
42 register uint32_t EBP asm(AREG6);
43 #define reg_EBP
44 #endif
45
46 #ifdef AREG7
47 register uint32_t ECX asm(AREG7);
48 #define reg_ECX
49 #endif
50
51 #ifdef AREG8
52 register uint32_t EDX asm(AREG8);
53 #define reg_EDX
54 #endif
55
56 #ifdef AREG9
57 register uint32_t EBX asm(AREG9);
58 #define reg_EBX
59 #endif
60
61 #ifdef AREG10
62 register uint32_t ESI asm(AREG10);
63 #define reg_ESI
64 #endif
65
66 #ifdef AREG11
67 register uint32_t EDI asm(AREG11);
68 #define reg_EDI
69 #endif
70
71 extern FILE *logfile;
72 extern int loglevel;
73
74 #ifndef reg_EAX
75 #define EAX (env->regs[R_EAX])
76 #endif
77 #ifndef reg_ECX
78 #define ECX (env->regs[R_ECX])
79 #endif
80 #ifndef reg_EDX
81 #define EDX (env->regs[R_EDX])
82 #endif
83 #ifndef reg_EBX
84 #define EBX (env->regs[R_EBX])
85 #endif
86 #ifndef reg_ESP
87 #define ESP (env->regs[R_ESP])
88 #endif
89 #ifndef reg_EBP
90 #define EBP (env->regs[R_EBP])
91 #endif
92 #ifndef reg_ESI
93 #define ESI (env->regs[R_ESI])
94 #endif
95 #ifndef reg_EDI
96 #define EDI (env->regs[R_EDI])
97 #endif
98 #define EIP (env->eip)
99 #define DF (env->df)
100
101 #define CC_SRC (env->cc_src)
102 #define CC_DST (env->cc_dst)
103 #define CC_OP (env->cc_op)
104
105 /* float macros */
106 #define FT0 (env->ft0)
107 #define ST0 (env->fpregs[env->fpstt])
108 #define ST(n) (env->fpregs[(env->fpstt + (n)) & 7])
109 #define ST1 ST(1)
110
111 #ifdef USE_FP_CONVERT
112 #define FP_CONVERT (env->fp_convert)
113 #endif
114
115 #include "cpu.h"
116 #include "exec-all.h"
117
118 typedef struct CCTable {
119 int (*compute_all)(void); /* return all the flags */
120 int (*compute_c)(void); /* return the C flag */
121 } CCTable;
122
123 extern CCTable cc_table[];
124
125 void load_seg(int seg_reg, int selector);
126 void helper_ljmp_protected_T0_T1(int next_eip);
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, int next_eip);
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, uint32_t new_cr0);
138 void cpu_x86_update_cr3(CPUX86State *env, uint32_t new_cr3);
139 void cpu_x86_update_cr4(CPUX86State *env, uint32_t new_cr4);
140 void cpu_x86_flush_tlb(CPUX86State *env, uint32_t addr);
141 int cpu_x86_handle_mmu_fault(CPUX86State *env, uint32_t addr,
142 int is_write, int is_user, int is_softmmu);
143 void tlb_fill(unsigned long addr, int is_write, int is_user,
144 void *retaddr);
145 void __hidden cpu_lock(void);
146 void __hidden cpu_unlock(void);
147 void do_interrupt(int intno, int is_int, int error_code,
148 unsigned int next_eip, int is_hw);
149 void do_interrupt_user(int intno, int is_int, int error_code,
150 unsigned int next_eip);
151 void raise_interrupt(int intno, int is_int, int error_code,
152 unsigned int next_eip);
153 void raise_exception_err(int exception_index, int error_code);
154 void raise_exception(int exception_index);
155 void __hidden cpu_loop_exit(void);
156 void helper_fsave(uint8_t *ptr, int data32);
157 void helper_frstor(uint8_t *ptr, int data32);
158
159 void OPPROTO op_movl_eflags_T0(void);
160 void OPPROTO op_movl_T0_eflags(void);
161 void raise_interrupt(int intno, int is_int, int error_code,
162 unsigned int next_eip);
163 void raise_exception_err(int exception_index, int error_code);
164 void raise_exception(int exception_index);
165 void helper_divl_EAX_T0(uint32_t eip);
166 void helper_idivl_EAX_T0(uint32_t eip);
167 void helper_cmpxchg8b(void);
168 void helper_cpuid(void);
169 void helper_rdtsc(void);
170 void helper_rdmsr(void);
171 void helper_wrmsr(void);
172 void helper_lsl(void);
173 void helper_lar(void);
174 void helper_verr(void);
175 void helper_verw(void);
176
177 void check_iob_T0(void);
178 void check_iow_T0(void);
179 void check_iol_T0(void);
180 void check_iob_DX(void);
181 void check_iow_DX(void);
182 void check_iol_DX(void);
183
184 /* XXX: move that to a generic header */
185 #if !defined(CONFIG_USER_ONLY)
186
187 #define ldul_user ldl_user
188 #define ldul_kernel ldl_kernel
189
190 #define ACCESS_TYPE 0
191 #define MEMSUFFIX _kernel
192 #define DATA_SIZE 1
193 #include "softmmu_header.h"
194
195 #define DATA_SIZE 2
196 #include "softmmu_header.h"
197
198 #define DATA_SIZE 4
199 #include "softmmu_header.h"
200
201 #define DATA_SIZE 8
202 #include "softmmu_header.h"
203 #undef ACCESS_TYPE
204 #undef MEMSUFFIX
205
206 #define ACCESS_TYPE 1
207 #define MEMSUFFIX _user
208 #define DATA_SIZE 1
209 #include "softmmu_header.h"
210
211 #define DATA_SIZE 2
212 #include "softmmu_header.h"
213
214 #define DATA_SIZE 4
215 #include "softmmu_header.h"
216
217 #define DATA_SIZE 8
218 #include "softmmu_header.h"
219 #undef ACCESS_TYPE
220 #undef MEMSUFFIX
221
222 /* these access are slower, they must be as rare as possible */
223 #define ACCESS_TYPE 2
224 #define MEMSUFFIX _data
225 #define DATA_SIZE 1
226 #include "softmmu_header.h"
227
228 #define DATA_SIZE 2
229 #include "softmmu_header.h"
230
231 #define DATA_SIZE 4
232 #include "softmmu_header.h"
233
234 #define DATA_SIZE 8
235 #include "softmmu_header.h"
236 #undef ACCESS_TYPE
237 #undef MEMSUFFIX
238
239 #define ldub(p) ldub_data(p)
240 #define ldsb(p) ldsb_data(p)
241 #define lduw(p) lduw_data(p)
242 #define ldsw(p) ldsw_data(p)
243 #define ldl(p) ldl_data(p)
244 #define ldq(p) ldq_data(p)
245
246 #define stb(p, v) stb_data(p, v)
247 #define stw(p, v) stw_data(p, v)
248 #define stl(p, v) stl_data(p, v)
249 #define stq(p, v) stq_data(p, v)
250
251 static inline double ldfq(void *ptr)
252 {
253 union {
254 double d;
255 uint64_t i;
256 } u;
257 u.i = ldq(ptr);
258 return u.d;
259 }
260
261 static inline void stfq(void *ptr, double v)
262 {
263 union {
264 double d;
265 uint64_t i;
266 } u;
267 u.d = v;
268 stq(ptr, u.i);
269 }
270
271 static inline float ldfl(void *ptr)
272 {
273 union {
274 float f;
275 uint32_t i;
276 } u;
277 u.i = ldl(ptr);
278 return u.f;
279 }
280
281 static inline void stfl(void *ptr, float v)
282 {
283 union {
284 float f;
285 uint32_t i;
286 } u;
287 u.f = v;
288 stl(ptr, u.i);
289 }
290
291 #endif /* !defined(CONFIG_USER_ONLY) */
292
293 #ifdef USE_X86LDOUBLE
294 /* use long double functions */
295 #define lrint lrintl
296 #define llrint llrintl
297 #define fabs fabsl
298 #define sin sinl
299 #define cos cosl
300 #define sqrt sqrtl
301 #define pow powl
302 #define log logl
303 #define tan tanl
304 #define atan2 atan2l
305 #define floor floorl
306 #define ceil ceill
307 #define rint rintl
308 #endif
309
310 extern int lrint(CPU86_LDouble x);
311 extern int64_t llrint(CPU86_LDouble x);
312 extern CPU86_LDouble fabs(CPU86_LDouble x);
313 extern CPU86_LDouble sin(CPU86_LDouble x);
314 extern CPU86_LDouble cos(CPU86_LDouble x);
315 extern CPU86_LDouble sqrt(CPU86_LDouble x);
316 extern CPU86_LDouble pow(CPU86_LDouble, CPU86_LDouble);
317 extern CPU86_LDouble log(CPU86_LDouble x);
318 extern CPU86_LDouble tan(CPU86_LDouble x);
319 extern CPU86_LDouble atan2(CPU86_LDouble, CPU86_LDouble);
320 extern CPU86_LDouble floor(CPU86_LDouble x);
321 extern CPU86_LDouble ceil(CPU86_LDouble x);
322 extern CPU86_LDouble rint(CPU86_LDouble x);
323
324 #define RC_MASK 0xc00
325 #define RC_NEAR 0x000
326 #define RC_DOWN 0x400
327 #define RC_UP 0x800
328 #define RC_CHOP 0xc00
329
330 #define MAXTAN 9223372036854775808.0
331
332 #ifdef __arm__
333 /* we have no way to do correct rounding - a FPU emulator is needed */
334 #define FE_DOWNWARD FE_TONEAREST
335 #define FE_UPWARD FE_TONEAREST
336 #define FE_TOWARDZERO FE_TONEAREST
337 #endif
338
339 #ifdef USE_X86LDOUBLE
340
341 /* only for x86 */
342 typedef union {
343 long double d;
344 struct {
345 unsigned long long lower;
346 unsigned short upper;
347 } l;
348 } CPU86_LDoubleU;
349
350 /* the following deal with x86 long double-precision numbers */
351 #define MAXEXPD 0x7fff
352 #define EXPBIAS 16383
353 #define EXPD(fp) (fp.l.upper & 0x7fff)
354 #define SIGND(fp) ((fp.l.upper) & 0x8000)
355 #define MANTD(fp) (fp.l.lower)
356 #define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7fff)) | EXPBIAS
357
358 #else
359
360 /* NOTE: arm is horrible as double 32 bit words are stored in big endian ! */
361 typedef union {
362 double d;
363 #if !defined(WORDS_BIGENDIAN) && !defined(__arm__)
364 struct {
365 uint32_t lower;
366 int32_t upper;
367 } l;
368 #else
369 struct {
370 int32_t upper;
371 uint32_t lower;
372 } l;
373 #endif
374 #ifndef __arm__
375 int64_t ll;
376 #endif
377 } CPU86_LDoubleU;
378
379 /* the following deal with IEEE double-precision numbers */
380 #define MAXEXPD 0x7ff
381 #define EXPBIAS 1023
382 #define EXPD(fp) (((fp.l.upper) >> 20) & 0x7FF)
383 #define SIGND(fp) ((fp.l.upper) & 0x80000000)
384 #ifdef __arm__
385 #define MANTD(fp) (fp.l.lower | ((uint64_t)(fp.l.upper & ((1 << 20) - 1)) << 32))
386 #else
387 #define MANTD(fp) (fp.ll & ((1LL << 52) - 1))
388 #endif
389 #define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7ff << 20)) | (EXPBIAS << 20)
390 #endif
391
392 static inline void fpush(void)
393 {
394 env->fpstt = (env->fpstt - 1) & 7;
395 env->fptags[env->fpstt] = 0; /* validate stack entry */
396 }
397
398 static inline void fpop(void)
399 {
400 env->fptags[env->fpstt] = 1; /* invvalidate stack entry */
401 env->fpstt = (env->fpstt + 1) & 7;
402 }
403
404 #ifndef USE_X86LDOUBLE
405 static inline CPU86_LDouble helper_fldt(uint8_t *ptr)
406 {
407 CPU86_LDoubleU temp;
408 int upper, e;
409 uint64_t ll;
410
411 /* mantissa */
412 upper = lduw(ptr + 8);
413 /* XXX: handle overflow ? */
414 e = (upper & 0x7fff) - 16383 + EXPBIAS; /* exponent */
415 e |= (upper >> 4) & 0x800; /* sign */
416 ll = (ldq(ptr) >> 11) & ((1LL << 52) - 1);
417 #ifdef __arm__
418 temp.l.upper = (e << 20) | (ll >> 32);
419 temp.l.lower = ll;
420 #else
421 temp.ll = ll | ((uint64_t)e << 52);
422 #endif
423 return temp.d;
424 }
425
426 static inline void helper_fstt(CPU86_LDouble f, uint8_t *ptr)
427 {
428 CPU86_LDoubleU temp;
429 int e;
430
431 temp.d = f;
432 /* mantissa */
433 stq(ptr, (MANTD(temp) << 11) | (1LL << 63));
434 /* exponent + sign */
435 e = EXPD(temp) - EXPBIAS + 16383;
436 e |= SIGND(temp) >> 16;
437 stw(ptr + 8, e);
438 }
439 #else
440
441 /* XXX: same endianness assumed */
442
443 #ifdef CONFIG_USER_ONLY
444
445 static inline CPU86_LDouble helper_fldt(uint8_t *ptr)
446 {
447 return *(CPU86_LDouble *)ptr;
448 }
449
450 static inline void helper_fstt(CPU86_LDouble f, uint8_t *ptr)
451 {
452 *(CPU86_LDouble *)ptr = f;
453 }
454
455 #else
456
457 /* we use memory access macros */
458
459 static inline CPU86_LDouble helper_fldt(uint8_t *ptr)
460 {
461 CPU86_LDoubleU temp;
462
463 temp.l.lower = ldq(ptr);
464 temp.l.upper = lduw(ptr + 8);
465 return temp.d;
466 }
467
468 static inline void helper_fstt(CPU86_LDouble f, uint8_t *ptr)
469 {
470 CPU86_LDoubleU temp;
471
472 temp.d = f;
473 stq(ptr, temp.l.lower);
474 stw(ptr + 8, temp.l.upper);
475 }
476
477 #endif /* !CONFIG_USER_ONLY */
478
479 #endif /* USE_X86LDOUBLE */
480
481 #define FPUS_IE (1 << 0)
482 #define FPUS_DE (1 << 1)
483 #define FPUS_ZE (1 << 2)
484 #define FPUS_OE (1 << 3)
485 #define FPUS_UE (1 << 4)
486 #define FPUS_PE (1 << 5)
487 #define FPUS_SF (1 << 6)
488 #define FPUS_SE (1 << 7)
489 #define FPUS_B (1 << 15)
490
491 #define FPUC_EM 0x3f
492
493 const CPU86_LDouble f15rk[7];
494
495 void helper_fldt_ST0_A0(void);
496 void helper_fstt_ST0_A0(void);
497 void fpu_raise_exception(void);
498 CPU86_LDouble helper_fdiv(CPU86_LDouble a, CPU86_LDouble b);
499 void helper_fbld_ST0_A0(void);
500 void helper_fbst_ST0_A0(void);
501 void helper_f2xm1(void);
502 void helper_fyl2x(void);
503 void helper_fptan(void);
504 void helper_fpatan(void);
505 void helper_fxtract(void);
506 void helper_fprem1(void);
507 void helper_fprem(void);
508 void helper_fyl2xp1(void);
509 void helper_fsqrt(void);
510 void helper_fsincos(void);
511 void helper_frndint(void);
512 void helper_fscale(void);
513 void helper_fsin(void);
514 void helper_fcos(void);
515 void helper_fxam_ST0(void);
516 void helper_fstenv(uint8_t *ptr, int data32);
517 void helper_fldenv(uint8_t *ptr, int data32);
518 void helper_fsave(uint8_t *ptr, int data32);
519 void helper_frstor(uint8_t *ptr, int data32);
520 void restore_native_fp_state(CPUState *env);
521 void save_native_fp_state(CPUState *env);
522
523 const uint8_t parity_table[256];
524 const uint8_t rclw_table[32];
525 const uint8_t rclb_table[32];
526
527 static inline uint32_t compute_eflags(void)
528 {
529 return env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
530 }
531
532 /* NOTE: CC_OP must be modified manually to CC_OP_EFLAGS */
533 static inline void load_eflags(int eflags, int update_mask)
534 {
535 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
536 DF = 1 - (2 * ((eflags >> 10) & 1));
537 env->eflags = (env->eflags & ~update_mask) |
538 (eflags & update_mask);
539 }
540