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 "exec-i386.h"
23 /* full interrupt support (only useful for real CPU emulation, not
24 finished) - I won't do it any time soon, finish it if you want ! */
25 void raise_interrupt(int intno, int is_int, int error_code,
26 unsigned int next_eip)
28 SegmentDescriptorTable *dt;
34 if (intno * 8 + 7 > dt->limit)
35 raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
36 ptr = dt->base + intno * 8;
40 type = (e2 >> DESC_TYPE_SHIFT) & 0x1f;
42 case 5: /* task gate */
43 case 6: /* 286 interrupt gate */
44 case 7: /* 286 trap gate */
45 case 14: /* 386 interrupt gate */
46 case 15: /* 386 trap gate */
49 raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
52 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
53 cpl = env->segs[R_CS] & 3;
54 /* check privledge if software int */
55 if (is_int && dpl < cpl)
56 raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
58 if (!(e2 & DESC_P_MASK))
59 raise_exception_err(EXCP0B_NOSEG, intno * 8 + 2);
65 * is_int is TRUE if coming from the int instruction. next_eip is the
66 * EIP value AFTER the interrupt instruction. It is only relevant if
69 void raise_interrupt(int intno, int is_int, int error_code,
70 unsigned int next_eip)
72 SegmentDescriptorTable *dt;
78 ptr = dt->base + (intno * 8);
81 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
83 /* check privledge if software int */
84 if (is_int && dpl < cpl)
85 raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
87 /* Since we emulate only user space, we cannot do more than
88 exiting the emulation with the suitable exception and error
92 env->exception_index = intno;
93 env->error_code = error_code;
100 /* shortcuts to generate exceptions */
101 void raise_exception_err(int exception_index, int error_code)
103 raise_interrupt(exception_index, 0, error_code, 0);
106 void raise_exception(int exception_index)
108 raise_interrupt(exception_index, 0, 0, 0);
111 /* We simulate a pre-MMX pentium as in valgrind */
112 #define CPUID_FP87 (1 << 0)
113 #define CPUID_VME (1 << 1)
114 #define CPUID_DE (1 << 2)
115 #define CPUID_PSE (1 << 3)
116 #define CPUID_TSC (1 << 4)
117 #define CPUID_MSR (1 << 5)
118 #define CPUID_PAE (1 << 6)
119 #define CPUID_MCE (1 << 7)
120 #define CPUID_CX8 (1 << 8)
121 #define CPUID_APIC (1 << 9)
122 #define CPUID_SEP (1 << 11) /* sysenter/sysexit */
123 #define CPUID_MTRR (1 << 12)
124 #define CPUID_PGE (1 << 13)
125 #define CPUID_MCA (1 << 14)
126 #define CPUID_CMOV (1 << 15)
128 #define CPUID_MMX (1 << 23)
129 #define CPUID_FXSR (1 << 24)
130 #define CPUID_SSE (1 << 25)
131 #define CPUID_SSE2 (1 << 26)
133 void helper_cpuid(void)
136 EAX = 1; /* max EAX index supported */
140 } else if (EAX == 1) {
145 EDX = CPUID_FP87 | CPUID_DE | CPUID_PSE |
146 CPUID_TSC | CPUID_MSR | CPUID_MCE |
151 /* only works if protected mode and not VM86 */
152 void load_seg(int seg_reg, int selector, unsigned cur_eip)
155 SegmentDescriptorTable *dt;
160 sc = &env->seg_cache[seg_reg];
161 if ((selector & 0xfffc) == 0) {
162 /* null selector case */
163 if (seg_reg == R_SS) {
165 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
167 /* XXX: each access should trigger an exception */
177 index = selector & ~7;
178 if ((index + 7) > dt->limit) {
180 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
182 ptr = dt->base + index;
185 if (!(e2 & DESC_S_MASK) ||
186 (e2 & (DESC_CS_MASK | DESC_R_MASK)) == DESC_CS_MASK) {
188 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
191 if (seg_reg == R_SS) {
192 if ((e2 & (DESC_CS_MASK | DESC_W_MASK)) == 0) {
194 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
197 if ((e2 & (DESC_CS_MASK | DESC_R_MASK)) == DESC_CS_MASK) {
199 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
203 if (!(e2 & DESC_P_MASK)) {
206 raise_exception_err(EXCP0C_STACK, selector & 0xfffc);
208 raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
211 sc->base = (void *)((e1 >> 16) | ((e2 & 0xff) << 16) | (e2 & 0xff000000));
212 sc->limit = (e1 & 0xffff) | (e2 & 0x000f0000);
214 sc->limit = (sc->limit << 12) | 0xfff;
215 sc->seg_32bit = (e2 >> 22) & 1;
217 fprintf(logfile, "load_seg: sel=0x%04x base=0x%08lx limit=0x%08lx seg_32bit=%d\n",
218 selector, (unsigned long)sc->base, sc->limit, sc->seg_32bit);
221 env->segs[seg_reg] = selector;
224 void helper_lsl(void)
226 unsigned int selector, limit;
227 SegmentDescriptorTable *dt;
232 CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
233 selector = T0 & 0xffff;
238 index = selector & ~7;
239 if ((index + 7) > dt->limit)
241 ptr = dt->base + index;
244 limit = (e1 & 0xffff) | (e2 & 0x000f0000);
246 limit = (limit << 12) | 0xfff;
251 void helper_lar(void)
253 unsigned int selector;
254 SegmentDescriptorTable *dt;
259 CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
260 selector = T0 & 0xffff;
265 index = selector & ~7;
266 if ((index + 7) > dt->limit)
268 ptr = dt->base + index;
270 T1 = e2 & 0x00f0ff00;
276 #ifndef USE_X86LDOUBLE
277 void helper_fldt_ST0_A0(void)
279 ST0 = helper_fldt((uint8_t *)A0);
282 void helper_fstt_ST0_A0(void)
284 helper_fstt(ST0, (uint8_t *)A0);
290 #define MUL10(iv) ( iv + iv + (iv << 3) )
292 void helper_fbld_ST0_A0(void)
295 CPU86_LDouble fpsrcop;
299 /* in this code, seg/m32i will be used as temporary ptr/int */
300 seg = (uint8_t *)A0 + 8;
302 /* XXX: raise exception */
306 /* XXX: raise exception */
309 m32i = v; /* <-- d14 */
311 m32i = MUL10(m32i) + (v >> 4); /* <-- val * 10 + d13 */
312 m32i = MUL10(m32i) + (v & 0xf); /* <-- val * 10 + d12 */
314 m32i = MUL10(m32i) + (v >> 4); /* <-- val * 10 + d11 */
315 m32i = MUL10(m32i) + (v & 0xf); /* <-- val * 10 + d10 */
317 m32i = MUL10(m32i) + (v >> 4); /* <-- val * 10 + d9 */
318 m32i = MUL10(m32i) + (v & 0xf); /* <-- val * 10 + d8 */
319 fpsrcop = ((CPU86_LDouble)m32i) * 100000000.0;
322 m32i = (v >> 4); /* <-- d7 */
323 m32i = MUL10(m32i) + (v & 0xf); /* <-- val * 10 + d6 */
325 m32i = MUL10(m32i) + (v >> 4); /* <-- val * 10 + d5 */
326 m32i = MUL10(m32i) + (v & 0xf); /* <-- val * 10 + d4 */
328 m32i = MUL10(m32i) + (v >> 4); /* <-- val * 10 + d3 */
329 m32i = MUL10(m32i) + (v & 0xf); /* <-- val * 10 + d2 */
331 m32i = MUL10(m32i) + (v >> 4); /* <-- val * 10 + d1 */
332 m32i = MUL10(m32i) + (v & 0xf); /* <-- val * 10 + d0 */
333 fpsrcop += ((CPU86_LDouble)m32i);
334 if ( ldub(seg+9) & 0x80 )
339 void helper_fbst_ST0_A0(void)
341 CPU86_LDouble fptemp;
342 CPU86_LDouble fpsrcop;
344 uint8_t *mem_ref, *mem_end;
347 mem_ref = (uint8_t *)A0;
348 mem_end = mem_ref + 8;
349 if ( fpsrcop < 0.0 ) {
350 stw(mem_end, 0x8000);
353 stw(mem_end, 0x0000);
355 while (mem_ref < mem_end) {
358 fptemp = floor(fpsrcop/10.0);
359 v = ((int)(fpsrcop - fptemp*10.0));
365 fptemp = floor(fpsrcop/10.0);
366 v |= (((int)(fpsrcop - fptemp*10.0)) << 4);
370 while (mem_ref < mem_end) {
375 void helper_f2xm1(void)
377 ST0 = pow(2.0,ST0) - 1.0;
380 void helper_fyl2x(void)
382 CPU86_LDouble fptemp;
386 fptemp = log(fptemp)/log(2.0); /* log2(ST) */
390 env->fpus &= (~0x4700);
395 void helper_fptan(void)
397 CPU86_LDouble fptemp;
400 if((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
406 env->fpus &= (~0x400); /* C2 <-- 0 */
407 /* the above code is for |arg| < 2**52 only */
411 void helper_fpatan(void)
413 CPU86_LDouble fptemp, fpsrcop;
417 ST1 = atan2(fpsrcop,fptemp);
421 void helper_fxtract(void)
427 expdif = EXPD(temp) - EXPBIAS;
435 void helper_fprem1(void)
437 CPU86_LDouble dblq, fpsrcop, fptemp;
438 CPU86_LDoubleU fpsrcop1, fptemp1;
444 fpsrcop1.d = fpsrcop;
446 expdif = EXPD(fpsrcop1) - EXPD(fptemp1);
448 dblq = fpsrcop / fptemp;
449 dblq = (dblq < 0.0)? ceil(dblq): floor(dblq);
450 ST0 = fpsrcop - fptemp*dblq;
451 q = (int)dblq; /* cutting off top bits is assumed here */
452 env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
453 /* (C0,C1,C3) <-- (q2,q1,q0) */
454 env->fpus |= (q&0x4) << 6; /* (C0) <-- q2 */
455 env->fpus |= (q&0x2) << 8; /* (C1) <-- q1 */
456 env->fpus |= (q&0x1) << 14; /* (C3) <-- q0 */
458 env->fpus |= 0x400; /* C2 <-- 1 */
459 fptemp = pow(2.0, expdif-50);
460 fpsrcop = (ST0 / ST1) / fptemp;
461 /* fpsrcop = integer obtained by rounding to the nearest */
462 fpsrcop = (fpsrcop-floor(fpsrcop) < ceil(fpsrcop)-fpsrcop)?
463 floor(fpsrcop): ceil(fpsrcop);
464 ST0 -= (ST1 * fpsrcop * fptemp);
468 void helper_fprem(void)
470 CPU86_LDouble dblq, fpsrcop, fptemp;
471 CPU86_LDoubleU fpsrcop1, fptemp1;
477 fpsrcop1.d = fpsrcop;
479 expdif = EXPD(fpsrcop1) - EXPD(fptemp1);
481 dblq = fpsrcop / fptemp;
482 dblq = (dblq < 0.0)? ceil(dblq): floor(dblq);
483 ST0 = fpsrcop - fptemp*dblq;
484 q = (int)dblq; /* cutting off top bits is assumed here */
485 env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
486 /* (C0,C1,C3) <-- (q2,q1,q0) */
487 env->fpus |= (q&0x4) << 6; /* (C0) <-- q2 */
488 env->fpus |= (q&0x2) << 8; /* (C1) <-- q1 */
489 env->fpus |= (q&0x1) << 14; /* (C3) <-- q0 */
491 env->fpus |= 0x400; /* C2 <-- 1 */
492 fptemp = pow(2.0, expdif-50);
493 fpsrcop = (ST0 / ST1) / fptemp;
494 /* fpsrcop = integer obtained by chopping */
495 fpsrcop = (fpsrcop < 0.0)?
496 -(floor(fabs(fpsrcop))): floor(fpsrcop);
497 ST0 -= (ST1 * fpsrcop * fptemp);
501 void helper_fyl2xp1(void)
503 CPU86_LDouble fptemp;
506 if ((fptemp+1.0)>0.0) {
507 fptemp = log(fptemp+1.0) / log(2.0); /* log2(ST+1.0) */
511 env->fpus &= (~0x4700);
516 void helper_fsqrt(void)
518 CPU86_LDouble fptemp;
522 env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
528 void helper_fsincos(void)
530 CPU86_LDouble fptemp;
533 if ((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
539 env->fpus &= (~0x400); /* C2 <-- 0 */
540 /* the above code is for |arg| < 2**63 only */
544 void helper_frndint(void)
549 void helper_fscale(void)
551 CPU86_LDouble fpsrcop, fptemp;
554 fptemp = pow(fpsrcop,ST1);
558 void helper_fsin(void)
560 CPU86_LDouble fptemp;
563 if ((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
567 env->fpus &= (~0x400); /* C2 <-- 0 */
568 /* the above code is for |arg| < 2**53 only */
572 void helper_fcos(void)
574 CPU86_LDouble fptemp;
577 if((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
581 env->fpus &= (~0x400); /* C2 <-- 0 */
582 /* the above code is for |arg5 < 2**63 only */
586 void helper_fxam_ST0(void)
593 env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
595 env->fpus |= 0x200; /* C1 <-- 1 */
598 if (expdif == MAXEXPD) {
599 if (MANTD(temp) == 0)
600 env->fpus |= 0x500 /*Infinity*/;
602 env->fpus |= 0x100 /*NaN*/;
603 } else if (expdif == 0) {
604 if (MANTD(temp) == 0)
605 env->fpus |= 0x4000 /*Zero*/;
607 env->fpus |= 0x4400 /*Denormal*/;
613 void helper_fstenv(uint8_t *ptr, int data32)
615 int fpus, fptag, exp, i;
619 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
621 for (i=7; i>=0; i--) {
623 if (env->fptags[i]) {
626 tmp.d = env->fpregs[i];
629 if (exp == 0 && mant == 0) {
632 } else if (exp == 0 || exp == MAXEXPD
633 #ifdef USE_X86LDOUBLE
634 || (mant & (1LL << 63)) == 0
637 /* NaNs, infinity, denormal */
663 void helper_fldenv(uint8_t *ptr, int data32)
668 env->fpuc = lduw(ptr);
669 fpus = lduw(ptr + 4);
670 fptag = lduw(ptr + 8);
673 env->fpuc = lduw(ptr);
674 fpus = lduw(ptr + 2);
675 fptag = lduw(ptr + 4);
677 env->fpstt = (fpus >> 11) & 7;
678 env->fpus = fpus & ~0x3800;
679 for(i = 0;i < 7; i++) {
680 env->fptags[i] = ((fptag & 3) == 3);
685 void helper_fsave(uint8_t *ptr, int data32)
690 helper_fstenv(ptr, data32);
692 ptr += (14 << data32);
693 for(i = 0;i < 8; i++) {
695 #ifdef USE_X86LDOUBLE
696 *(long double *)ptr = tmp;
698 helper_fstt(tmp, ptr);
717 void helper_frstor(uint8_t *ptr, int data32)
722 helper_fldenv(ptr, data32);
723 ptr += (14 << data32);
725 for(i = 0;i < 8; i++) {
726 #ifdef USE_X86LDOUBLE
727 tmp = *(long double *)ptr;
729 tmp = helper_fldt(ptr);