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
25 #define raise_exception_err(a, b)\
27 printf("raise_exception line=%d\n", __LINE__);\
28 (raise_exception_err)(a, b);\
32 const uint8_t parity_table[256] = {
33 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
34 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
35 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
36 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
37 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
38 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
39 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
40 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
41 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
42 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
43 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
44 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
45 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
46 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
47 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
48 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
49 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
50 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
51 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
52 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
53 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
54 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
55 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
56 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
57 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
58 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
59 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
60 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
61 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
62 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
63 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
64 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
68 const uint8_t rclw_table[32] = {
69 0, 1, 2, 3, 4, 5, 6, 7,
70 8, 9,10,11,12,13,14,15,
71 16, 0, 1, 2, 3, 4, 5, 6,
72 7, 8, 9,10,11,12,13,14,
76 const uint8_t rclb_table[32] = {
77 0, 1, 2, 3, 4, 5, 6, 7,
78 8, 0, 1, 2, 3, 4, 5, 6,
79 7, 8, 0, 1, 2, 3, 4, 5,
80 6, 7, 8, 0, 1, 2, 3, 4,
83 const CPU86_LDouble f15rk[7] =
85 0.00000000000000000000L,
86 1.00000000000000000000L,
87 3.14159265358979323851L, /*pi*/
88 0.30102999566398119523L, /*lg2*/
89 0.69314718055994530943L, /*ln2*/
90 1.44269504088896340739L, /*l2e*/
91 3.32192809488736234781L, /*l2t*/
96 spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
100 spin_lock(&global_cpu_lock);
103 void cpu_unlock(void)
105 spin_unlock(&global_cpu_lock);
108 void cpu_loop_exit(void)
110 /* NOTE: the register at this point must be saved by hand because
111 longjmp restore them */
113 env->regs[R_EAX] = EAX;
116 env->regs[R_ECX] = ECX;
119 env->regs[R_EDX] = EDX;
122 env->regs[R_EBX] = EBX;
125 env->regs[R_ESP] = ESP;
128 env->regs[R_EBP] = EBP;
131 env->regs[R_ESI] = ESI;
134 env->regs[R_EDI] = EDI;
136 longjmp(env->jmp_env, 1);
139 /* return non zero if error */
140 static inline int load_segment(uint32_t *e1_ptr, uint32_t *e2_ptr,
151 index = selector & ~7;
152 if ((index + 7) > dt->limit)
154 ptr = dt->base + index;
155 *e1_ptr = ldl_kernel(ptr);
156 *e2_ptr = ldl_kernel(ptr + 4);
160 static inline unsigned int get_seg_limit(uint32_t e1, uint32_t e2)
163 limit = (e1 & 0xffff) | (e2 & 0x000f0000);
164 if (e2 & DESC_G_MASK)
165 limit = (limit << 12) | 0xfff;
169 static inline uint8_t *get_seg_base(uint32_t e1, uint32_t e2)
171 return (uint8_t *)((e1 >> 16) | ((e2 & 0xff) << 16) | (e2 & 0xff000000));
174 static inline void load_seg_cache_raw_dt(SegmentCache *sc, uint32_t e1, uint32_t e2)
176 sc->base = get_seg_base(e1, e2);
177 sc->limit = get_seg_limit(e1, e2);
181 /* init the segment cache in vm86 mode. */
182 static inline void load_seg_vm(int seg, int selector)
185 cpu_x86_load_seg_cache(env, seg, selector,
186 (uint8_t *)(selector << 4), 0xffff, 0);
189 static inline void get_ss_esp_from_tss(uint32_t *ss_ptr,
190 uint32_t *esp_ptr, int dpl)
192 int type, index, shift;
197 printf("TR: base=%p limit=%x\n", env->tr.base, env->tr.limit);
198 for(i=0;i<env->tr.limit;i++) {
199 printf("%02x ", env->tr.base[i]);
200 if ((i & 7) == 7) printf("\n");
206 if (!(env->tr.flags & DESC_P_MASK))
207 cpu_abort(env, "invalid tss");
208 type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf;
210 cpu_abort(env, "invalid tss type");
212 index = (dpl * 4 + 2) << shift;
213 if (index + (4 << shift) - 1 > env->tr.limit)
214 raise_exception_err(EXCP0A_TSS, env->tr.selector & 0xfffc);
216 *esp_ptr = lduw_kernel(env->tr.base + index);
217 *ss_ptr = lduw_kernel(env->tr.base + index + 2);
219 *esp_ptr = ldl_kernel(env->tr.base + index);
220 *ss_ptr = lduw_kernel(env->tr.base + index + 4);
224 /* XXX: merge with load_seg() */
225 static void tss_load_seg(int seg_reg, int selector)
230 if ((selector & 0xfffc) != 0) {
231 if (load_segment(&e1, &e2, selector) != 0)
232 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
233 if (!(e2 & DESC_S_MASK))
234 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
236 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
237 cpl = env->hflags & HF_CPL_MASK;
238 if (seg_reg == R_CS) {
239 if (!(e2 & DESC_CS_MASK))
240 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
242 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
243 if ((e2 & DESC_C_MASK) && dpl > rpl)
244 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
246 } else if (seg_reg == R_SS) {
247 /* SS must be writable data */
248 if ((e2 & DESC_CS_MASK) || !(e2 & DESC_W_MASK))
249 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
250 if (dpl != cpl || dpl != rpl)
251 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
253 /* not readable code */
254 if ((e2 & DESC_CS_MASK) && !(e2 & DESC_R_MASK))
255 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
256 /* if data or non conforming code, checks the rights */
257 if (((e2 >> DESC_TYPE_SHIFT) & 0xf) < 12) {
258 if (dpl < cpl || dpl < rpl)
259 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
262 if (!(e2 & DESC_P_MASK))
263 raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
264 cpu_x86_load_seg_cache(env, seg_reg, selector,
265 get_seg_base(e1, e2),
266 get_seg_limit(e1, e2),
269 if (seg_reg == R_SS || seg_reg == R_CS)
270 raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
274 #define SWITCH_TSS_JMP 0
275 #define SWITCH_TSS_IRET 1
276 #define SWITCH_TSS_CALL 2
278 /* XXX: restore CPU state in registers (PowerPC case) */
279 static void switch_tss(int tss_selector,
280 uint32_t e1, uint32_t e2, int source)
282 int tss_limit, tss_limit_max, type, old_tss_limit_max, old_type, v1, v2, i;
284 uint32_t new_regs[8], new_segs[6];
285 uint32_t new_eflags, new_eip, new_cr3, new_ldt, new_trap;
286 uint32_t old_eflags, eflags_mask;
291 type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
293 if (loglevel & CPU_LOG_PCALL)
294 fprintf(logfile, "switch_tss: sel=0x%04x type=%d src=%d\n", tss_selector, type, source);
297 /* if task gate, we read the TSS segment and we load it */
299 if (!(e2 & DESC_P_MASK))
300 raise_exception_err(EXCP0B_NOSEG, tss_selector & 0xfffc);
301 tss_selector = e1 >> 16;
302 if (tss_selector & 4)
303 raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
304 if (load_segment(&e1, &e2, tss_selector) != 0)
305 raise_exception_err(EXCP0D_GPF, tss_selector & 0xfffc);
306 if (e2 & DESC_S_MASK)
307 raise_exception_err(EXCP0D_GPF, tss_selector & 0xfffc);
308 type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
310 raise_exception_err(EXCP0D_GPF, tss_selector & 0xfffc);
313 if (!(e2 & DESC_P_MASK))
314 raise_exception_err(EXCP0B_NOSEG, tss_selector & 0xfffc);
320 tss_limit = get_seg_limit(e1, e2);
321 tss_base = get_seg_base(e1, e2);
322 if ((tss_selector & 4) != 0 ||
323 tss_limit < tss_limit_max)
324 raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
325 old_type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf;
327 old_tss_limit_max = 103;
329 old_tss_limit_max = 43;
331 /* read all the registers from the new TSS */
334 new_cr3 = ldl_kernel(tss_base + 0x1c);
335 new_eip = ldl_kernel(tss_base + 0x20);
336 new_eflags = ldl_kernel(tss_base + 0x24);
337 for(i = 0; i < 8; i++)
338 new_regs[i] = ldl_kernel(tss_base + (0x28 + i * 4));
339 for(i = 0; i < 6; i++)
340 new_segs[i] = lduw_kernel(tss_base + (0x48 + i * 4));
341 new_ldt = lduw_kernel(tss_base + 0x60);
342 new_trap = ldl_kernel(tss_base + 0x64);
346 new_eip = lduw_kernel(tss_base + 0x0e);
347 new_eflags = lduw_kernel(tss_base + 0x10);
348 for(i = 0; i < 8; i++)
349 new_regs[i] = lduw_kernel(tss_base + (0x12 + i * 2)) | 0xffff0000;
350 for(i = 0; i < 4; i++)
351 new_segs[i] = lduw_kernel(tss_base + (0x22 + i * 4));
352 new_ldt = lduw_kernel(tss_base + 0x2a);
358 /* NOTE: we must avoid memory exceptions during the task switch,
359 so we make dummy accesses before */
360 /* XXX: it can still fail in some cases, so a bigger hack is
361 necessary to valid the TLB after having done the accesses */
363 v1 = ldub_kernel(env->tr.base);
364 v2 = ldub(env->tr.base + old_tss_limit_max);
365 stb_kernel(env->tr.base, v1);
366 stb_kernel(env->tr.base + old_tss_limit_max, v2);
368 /* clear busy bit (it is restartable) */
369 if (source == SWITCH_TSS_JMP || source == SWITCH_TSS_IRET) {
372 ptr = env->gdt.base + (env->tr.selector << 3);
373 e2 = ldl_kernel(ptr + 4);
374 e2 &= ~DESC_TSS_BUSY_MASK;
375 stl_kernel(ptr + 4, e2);
377 old_eflags = compute_eflags();
378 if (source == SWITCH_TSS_IRET)
379 old_eflags &= ~NT_MASK;
381 /* save the current state in the old TSS */
384 stl_kernel(env->tr.base + 0x20, env->eip);
385 stl_kernel(env->tr.base + 0x24, old_eflags);
386 for(i = 0; i < 8; i++)
387 stl_kernel(env->tr.base + (0x28 + i * 4), env->regs[i]);
388 for(i = 0; i < 6; i++)
389 stw_kernel(env->tr.base + (0x48 + i * 4), env->segs[i].selector);
392 stw_kernel(env->tr.base + 0x0e, new_eip);
393 stw_kernel(env->tr.base + 0x10, old_eflags);
394 for(i = 0; i < 8; i++)
395 stw_kernel(env->tr.base + (0x12 + i * 2), env->regs[i]);
396 for(i = 0; i < 4; i++)
397 stw_kernel(env->tr.base + (0x22 + i * 4), env->segs[i].selector);
400 /* now if an exception occurs, it will occurs in the next task
403 if (source == SWITCH_TSS_CALL) {
404 stw_kernel(tss_base, env->tr.selector);
405 new_eflags |= NT_MASK;
409 if (source == SWITCH_TSS_JMP || source == SWITCH_TSS_CALL) {
412 ptr = env->gdt.base + (tss_selector << 3);
413 e2 = ldl_kernel(ptr + 4);
414 e2 |= DESC_TSS_BUSY_MASK;
415 stl_kernel(ptr + 4, e2);
418 /* set the new CPU state */
419 /* from this point, any exception which occurs can give problems */
420 env->cr[0] |= CR0_TS_MASK;
421 env->tr.selector = tss_selector;
422 env->tr.base = tss_base;
423 env->tr.limit = tss_limit;
424 env->tr.flags = e2 & ~DESC_TSS_BUSY_MASK;
426 if ((type & 8) && (env->cr[0] & CR0_PG_MASK)) {
427 cpu_x86_update_cr3(env, new_cr3);
430 /* load all registers without an exception, then reload them with
431 possible exception */
433 eflags_mask = TF_MASK | AC_MASK | ID_MASK |
434 IF_MASK | IOPL_MASK | VM_MASK | RF_MASK | NT_MASK;
436 eflags_mask &= 0xffff;
437 load_eflags(new_eflags, eflags_mask);
438 for(i = 0; i < 8; i++)
439 env->regs[i] = new_regs[i];
440 if (new_eflags & VM_MASK) {
441 for(i = 0; i < 6; i++)
442 load_seg_vm(i, new_segs[i]);
443 /* in vm86, CPL is always 3 */
444 cpu_x86_set_cpl(env, 3);
446 /* CPL is set the RPL of CS */
447 cpu_x86_set_cpl(env, new_segs[R_CS] & 3);
448 /* first just selectors as the rest may trigger exceptions */
449 for(i = 0; i < 6; i++)
450 cpu_x86_load_seg_cache(env, i, new_segs[i], NULL, 0, 0);
453 env->ldt.selector = new_ldt & ~4;
454 env->ldt.base = NULL;
460 raise_exception_err(EXCP0A_TSS, new_ldt & 0xfffc);
462 if ((new_ldt & 0xfffc) != 0) {
464 index = new_ldt & ~7;
465 if ((index + 7) > dt->limit)
466 raise_exception_err(EXCP0A_TSS, new_ldt & 0xfffc);
467 ptr = dt->base + index;
468 e1 = ldl_kernel(ptr);
469 e2 = ldl_kernel(ptr + 4);
470 if ((e2 & DESC_S_MASK) || ((e2 >> DESC_TYPE_SHIFT) & 0xf) != 2)
471 raise_exception_err(EXCP0A_TSS, new_ldt & 0xfffc);
472 if (!(e2 & DESC_P_MASK))
473 raise_exception_err(EXCP0A_TSS, new_ldt & 0xfffc);
474 load_seg_cache_raw_dt(&env->ldt, e1, e2);
477 /* load the segments */
478 if (!(new_eflags & VM_MASK)) {
479 tss_load_seg(R_CS, new_segs[R_CS]);
480 tss_load_seg(R_SS, new_segs[R_SS]);
481 tss_load_seg(R_ES, new_segs[R_ES]);
482 tss_load_seg(R_DS, new_segs[R_DS]);
483 tss_load_seg(R_FS, new_segs[R_FS]);
484 tss_load_seg(R_GS, new_segs[R_GS]);
487 /* check that EIP is in the CS segment limits */
488 if (new_eip > env->segs[R_CS].limit) {
489 raise_exception_err(EXCP0D_GPF, 0);
493 /* check if Port I/O is allowed in TSS */
494 static inline void check_io(int addr, int size)
496 int io_offset, val, mask;
498 /* TSS must be a valid 32 bit one */
499 if (!(env->tr.flags & DESC_P_MASK) ||
500 ((env->tr.flags >> DESC_TYPE_SHIFT) & 0xf) != 9 ||
503 io_offset = lduw_kernel(env->tr.base + 0x66);
504 io_offset += (addr >> 3);
505 /* Note: the check needs two bytes */
506 if ((io_offset + 1) > env->tr.limit)
508 val = lduw_kernel(env->tr.base + io_offset);
510 mask = (1 << size) - 1;
511 /* all bits must be zero to allow the I/O */
512 if ((val & mask) != 0) {
514 raise_exception_err(EXCP0D_GPF, 0);
518 void check_iob_T0(void)
523 void check_iow_T0(void)
528 void check_iol_T0(void)
533 void check_iob_DX(void)
535 check_io(EDX & 0xffff, 1);
538 void check_iow_DX(void)
540 check_io(EDX & 0xffff, 2);
543 void check_iol_DX(void)
545 check_io(EDX & 0xffff, 4);
548 static inline unsigned int get_sp_mask(unsigned int e2)
550 if (e2 & DESC_B_MASK)
556 /* XXX: add a is_user flag to have proper security support */
557 #define PUSHW(ssp, sp, sp_mask, val)\
560 stw_kernel((ssp) + (sp & (sp_mask)), (val));\
563 #define PUSHL(ssp, sp, sp_mask, val)\
566 stl_kernel((ssp) + (sp & (sp_mask)), (val));\
569 #define POPW(ssp, sp, sp_mask, val)\
571 val = lduw_kernel((ssp) + (sp & (sp_mask)));\
575 #define POPL(ssp, sp, sp_mask, val)\
577 val = ldl_kernel((ssp) + (sp & (sp_mask)));\
581 /* protected mode interrupt */
582 static void do_interrupt_protected(int intno, int is_int, int error_code,
583 unsigned int next_eip, int is_hw)
587 int type, dpl, selector, ss_dpl, cpl, sp_mask;
588 int has_error_code, new_stack, shift;
589 uint32_t e1, e2, offset, ss, esp, ss_e1, ss_e2;
593 if (!is_int && !is_hw) {
608 if (intno * 8 + 7 > dt->limit)
609 raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
610 ptr = dt->base + intno * 8;
611 e1 = ldl_kernel(ptr);
612 e2 = ldl_kernel(ptr + 4);
613 /* check gate type */
614 type = (e2 >> DESC_TYPE_SHIFT) & 0x1f;
616 case 5: /* task gate */
617 /* must do that check here to return the correct error code */
618 if (!(e2 & DESC_P_MASK))
619 raise_exception_err(EXCP0B_NOSEG, intno * 8 + 2);
620 switch_tss(intno * 8, e1, e2, SWITCH_TSS_CALL);
621 if (has_error_code) {
623 /* push the error code */
624 shift = (env->segs[R_CS].flags >> DESC_B_SHIFT) & 1;
625 if (env->segs[R_SS].flags & DESC_B_MASK)
629 esp = (env->regs[R_ESP] - (2 << shift)) & mask;
630 ssp = env->segs[R_SS].base + esp;
632 stl_kernel(ssp, error_code);
634 stw_kernel(ssp, error_code);
635 env->regs[R_ESP] = (esp & mask) | (env->regs[R_ESP] & ~mask);
638 case 6: /* 286 interrupt gate */
639 case 7: /* 286 trap gate */
640 case 14: /* 386 interrupt gate */
641 case 15: /* 386 trap gate */
644 raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
647 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
648 cpl = env->hflags & HF_CPL_MASK;
649 /* check privledge if software int */
650 if (is_int && dpl < cpl)
651 raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
652 /* check valid bit */
653 if (!(e2 & DESC_P_MASK))
654 raise_exception_err(EXCP0B_NOSEG, intno * 8 + 2);
656 offset = (e2 & 0xffff0000) | (e1 & 0x0000ffff);
657 if ((selector & 0xfffc) == 0)
658 raise_exception_err(EXCP0D_GPF, 0);
660 if (load_segment(&e1, &e2, selector) != 0)
661 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
662 if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK)))
663 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
664 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
666 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
667 if (!(e2 & DESC_P_MASK))
668 raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
669 if (!(e2 & DESC_C_MASK) && dpl < cpl) {
670 /* to inner priviledge */
671 get_ss_esp_from_tss(&ss, &esp, dpl);
672 if ((ss & 0xfffc) == 0)
673 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
675 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
676 if (load_segment(&ss_e1, &ss_e2, ss) != 0)
677 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
678 ss_dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
680 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
681 if (!(ss_e2 & DESC_S_MASK) ||
682 (ss_e2 & DESC_CS_MASK) ||
683 !(ss_e2 & DESC_W_MASK))
684 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
685 if (!(ss_e2 & DESC_P_MASK))
686 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
688 sp_mask = get_sp_mask(ss_e2);
689 ssp = get_seg_base(ss_e1, ss_e2);
690 } else if ((e2 & DESC_C_MASK) || dpl == cpl) {
691 /* to same priviledge */
692 if (env->eflags & VM_MASK)
693 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
695 sp_mask = get_sp_mask(env->segs[R_SS].flags);
696 ssp = env->segs[R_SS].base;
700 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
701 new_stack = 0; /* avoid warning */
702 sp_mask = 0; /* avoid warning */
703 ssp = NULL; /* avoid warning */
704 esp = 0; /* avoid warning */
710 /* XXX: check that enough room is available */
711 push_size = 6 + (new_stack << 2) + (has_error_code << 1);
712 if (env->eflags & VM_MASK)
722 if (env->eflags & VM_MASK) {
723 PUSHL(ssp, esp, sp_mask, env->segs[R_GS].selector);
724 PUSHL(ssp, esp, sp_mask, env->segs[R_FS].selector);
725 PUSHL(ssp, esp, sp_mask, env->segs[R_DS].selector);
726 PUSHL(ssp, esp, sp_mask, env->segs[R_ES].selector);
728 PUSHL(ssp, esp, sp_mask, env->segs[R_SS].selector);
729 PUSHL(ssp, esp, sp_mask, ESP);
731 PUSHL(ssp, esp, sp_mask, compute_eflags());
732 PUSHL(ssp, esp, sp_mask, env->segs[R_CS].selector);
733 PUSHL(ssp, esp, sp_mask, old_eip);
734 if (has_error_code) {
735 PUSHL(ssp, esp, sp_mask, error_code);
739 if (env->eflags & VM_MASK) {
740 PUSHW(ssp, esp, sp_mask, env->segs[R_GS].selector);
741 PUSHW(ssp, esp, sp_mask, env->segs[R_FS].selector);
742 PUSHW(ssp, esp, sp_mask, env->segs[R_DS].selector);
743 PUSHW(ssp, esp, sp_mask, env->segs[R_ES].selector);
745 PUSHW(ssp, esp, sp_mask, env->segs[R_SS].selector);
746 PUSHW(ssp, esp, sp_mask, ESP);
748 PUSHW(ssp, esp, sp_mask, compute_eflags());
749 PUSHW(ssp, esp, sp_mask, env->segs[R_CS].selector);
750 PUSHW(ssp, esp, sp_mask, old_eip);
751 if (has_error_code) {
752 PUSHW(ssp, esp, sp_mask, error_code);
757 if (env->eflags & VM_MASK) {
758 /* XXX: explain me why W2K hangs if the whole segment cache is
761 env->segs[R_ES].selector = 0;
762 env->segs[R_ES].flags = 0;
763 env->segs[R_DS].selector = 0;
764 env->segs[R_DS].flags = 0;
765 env->segs[R_FS].selector = 0;
766 env->segs[R_FS].flags = 0;
767 env->segs[R_GS].selector = 0;
768 env->segs[R_GS].flags = 0;
770 cpu_x86_load_seg_cache(env, R_ES, 0, NULL, 0, 0);
771 cpu_x86_load_seg_cache(env, R_DS, 0, NULL, 0, 0);
772 cpu_x86_load_seg_cache(env, R_FS, 0, NULL, 0, 0);
773 cpu_x86_load_seg_cache(env, R_GS, 0, NULL, 0, 0);
776 ss = (ss & ~3) | dpl;
777 cpu_x86_load_seg_cache(env, R_SS, ss,
778 ssp, get_seg_limit(ss_e1, ss_e2), ss_e2);
780 ESP = (ESP & ~sp_mask) | (esp & sp_mask);
782 selector = (selector & ~3) | dpl;
783 cpu_x86_load_seg_cache(env, R_CS, selector,
784 get_seg_base(e1, e2),
785 get_seg_limit(e1, e2),
787 cpu_x86_set_cpl(env, dpl);
790 /* interrupt gate clear IF mask */
791 if ((type & 1) == 0) {
792 env->eflags &= ~IF_MASK;
794 env->eflags &= ~(TF_MASK | VM_MASK | RF_MASK | NT_MASK);
797 /* real mode interrupt */
798 static void do_interrupt_real(int intno, int is_int, int error_code,
799 unsigned int next_eip)
804 uint32_t offset, esp;
805 uint32_t old_cs, old_eip;
807 /* real mode (simpler !) */
809 if (intno * 4 + 3 > dt->limit)
810 raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
811 ptr = dt->base + intno * 4;
812 offset = lduw_kernel(ptr);
813 selector = lduw_kernel(ptr + 2);
815 ssp = env->segs[R_SS].base;
820 old_cs = env->segs[R_CS].selector;
821 /* XXX: use SS segment size ? */
822 PUSHW(ssp, esp, 0xffff, compute_eflags());
823 PUSHW(ssp, esp, 0xffff, old_cs);
824 PUSHW(ssp, esp, 0xffff, old_eip);
826 /* update processor state */
827 ESP = (ESP & ~0xffff) | (esp & 0xffff);
829 env->segs[R_CS].selector = selector;
830 env->segs[R_CS].base = (uint8_t *)(selector << 4);
831 env->eflags &= ~(IF_MASK | TF_MASK | AC_MASK | RF_MASK);
834 /* fake user mode interrupt */
835 void do_interrupt_user(int intno, int is_int, int error_code,
836 unsigned int next_eip)
844 ptr = dt->base + (intno * 8);
845 e2 = ldl_kernel(ptr + 4);
847 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
848 cpl = env->hflags & HF_CPL_MASK;
849 /* check privledge if software int */
850 if (is_int && dpl < cpl)
851 raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
853 /* Since we emulate only user space, we cannot do more than
854 exiting the emulation with the suitable exception and error
861 * Begin execution of an interruption. is_int is TRUE if coming from
862 * the int instruction. next_eip is the EIP value AFTER the interrupt
863 * instruction. It is only relevant if is_int is TRUE.
865 void do_interrupt(int intno, int is_int, int error_code,
866 unsigned int next_eip, int is_hw)
869 if (loglevel & (CPU_LOG_PCALL | CPU_LOG_INT)) {
870 if ((env->cr[0] & CR0_PE_MASK)) {
872 fprintf(logfile, "%6d: v=%02x e=%04x i=%d cpl=%d IP=%04x:%08x SP=%04x:%08x",
873 count, intno, error_code, is_int,
874 env->hflags & HF_CPL_MASK,
875 env->segs[R_CS].selector, EIP,
876 env->segs[R_SS].selector, ESP);
878 fprintf(logfile, " CR2=%08x", env->cr[2]);
880 fprintf(logfile, " EAX=%08x", env->regs[R_EAX]);
882 fprintf(logfile, "\n");
884 cpu_x86_dump_state(env, logfile, X86_DUMP_CCOP);
888 fprintf(logfile, " code=");
889 ptr = env->segs[R_CS].base + env->eip;
890 for(i = 0; i < 16; i++) {
891 fprintf(logfile, " %02x", ldub(ptr + i));
893 fprintf(logfile, "\n");
900 if (env->cr[0] & CR0_PE_MASK) {
901 do_interrupt_protected(intno, is_int, error_code, next_eip, is_hw);
903 do_interrupt_real(intno, is_int, error_code, next_eip);
908 * Signal an interruption. It is executed in the main CPU loop.
909 * is_int is TRUE if coming from the int instruction. next_eip is the
910 * EIP value AFTER the interrupt instruction. It is only relevant if
913 void raise_interrupt(int intno, int is_int, int error_code,
914 unsigned int next_eip)
916 env->exception_index = intno;
917 env->error_code = error_code;
918 env->exception_is_int = is_int;
919 env->exception_next_eip = next_eip;
923 /* shortcuts to generate exceptions */
925 void (raise_exception_err)(int exception_index, int error_code)
927 raise_interrupt(exception_index, 0, error_code, 0);
930 void raise_exception(int exception_index)
932 raise_interrupt(exception_index, 0, 0, 0);
935 #ifdef BUGGY_GCC_DIV64
936 /* gcc 2.95.4 on PowerPC does not seem to like using __udivdi3, so we
937 call it from another function */
938 uint32_t div64(uint32_t *q_ptr, uint64_t num, uint32_t den)
944 int32_t idiv64(int32_t *q_ptr, int64_t num, int32_t den)
951 void helper_divl_EAX_T0(uint32_t eip)
953 unsigned int den, q, r;
956 num = EAX | ((uint64_t)EDX << 32);
960 raise_exception(EXCP00_DIVZ);
962 #ifdef BUGGY_GCC_DIV64
963 r = div64(&q, num, den);
972 void helper_idivl_EAX_T0(uint32_t eip)
977 num = EAX | ((uint64_t)EDX << 32);
981 raise_exception(EXCP00_DIVZ);
983 #ifdef BUGGY_GCC_DIV64
984 r = idiv64(&q, num, den);
993 void helper_cmpxchg8b(void)
998 eflags = cc_table[CC_OP].compute_all();
999 d = ldq((uint8_t *)A0);
1000 if (d == (((uint64_t)EDX << 32) | EAX)) {
1001 stq((uint8_t *)A0, ((uint64_t)ECX << 32) | EBX);
1011 #define CPUID_FP87 (1 << 0)
1012 #define CPUID_VME (1 << 1)
1013 #define CPUID_DE (1 << 2)
1014 #define CPUID_PSE (1 << 3)
1015 #define CPUID_TSC (1 << 4)
1016 #define CPUID_MSR (1 << 5)
1017 #define CPUID_PAE (1 << 6)
1018 #define CPUID_MCE (1 << 7)
1019 #define CPUID_CX8 (1 << 8)
1020 #define CPUID_APIC (1 << 9)
1021 #define CPUID_SEP (1 << 11) /* sysenter/sysexit */
1022 #define CPUID_MTRR (1 << 12)
1023 #define CPUID_PGE (1 << 13)
1024 #define CPUID_MCA (1 << 14)
1025 #define CPUID_CMOV (1 << 15)
1027 #define CPUID_MMX (1 << 23)
1028 #define CPUID_FXSR (1 << 24)
1029 #define CPUID_SSE (1 << 25)
1030 #define CPUID_SSE2 (1 << 26)
1032 void helper_cpuid(void)
1036 EAX = 2; /* max EAX index supported */
1043 int family, model, stepping;
1046 /* pentium 75-200 */
1056 EAX = (family << 8) | (model << 4) | stepping;
1059 EDX = CPUID_FP87 | CPUID_DE | CPUID_PSE |
1060 CPUID_TSC | CPUID_MSR | CPUID_MCE |
1061 CPUID_CX8 | CPUID_PGE | CPUID_CMOV;
1065 /* cache info: needed for Pentium Pro compatibility */
1074 void helper_lldt_T0(void)
1082 selector = T0 & 0xffff;
1083 if ((selector & 0xfffc) == 0) {
1084 /* XXX: NULL selector case: invalid LDT */
1085 env->ldt.base = NULL;
1089 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1091 index = selector & ~7;
1092 if ((index + 7) > dt->limit)
1093 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1094 ptr = dt->base + index;
1095 e1 = ldl_kernel(ptr);
1096 e2 = ldl_kernel(ptr + 4);
1097 if ((e2 & DESC_S_MASK) || ((e2 >> DESC_TYPE_SHIFT) & 0xf) != 2)
1098 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1099 if (!(e2 & DESC_P_MASK))
1100 raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
1101 load_seg_cache_raw_dt(&env->ldt, e1, e2);
1103 env->ldt.selector = selector;
1106 void helper_ltr_T0(void)
1114 selector = T0 & 0xffff;
1115 if ((selector & 0xfffc) == 0) {
1116 /* NULL selector case: invalid LDT */
1117 env->tr.base = NULL;
1122 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1124 index = selector & ~7;
1125 if ((index + 7) > dt->limit)
1126 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1127 ptr = dt->base + index;
1128 e1 = ldl_kernel(ptr);
1129 e2 = ldl_kernel(ptr + 4);
1130 type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
1131 if ((e2 & DESC_S_MASK) ||
1132 (type != 1 && type != 9))
1133 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1134 if (!(e2 & DESC_P_MASK))
1135 raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
1136 load_seg_cache_raw_dt(&env->tr, e1, e2);
1137 e2 |= DESC_TSS_BUSY_MASK;
1138 stl_kernel(ptr + 4, e2);
1140 env->tr.selector = selector;
1143 /* only works if protected mode and not VM86. seg_reg must be != R_CS */
1144 void load_seg(int seg_reg, int selector)
1153 if ((selector & 0xfffc) == 0) {
1154 /* null selector case */
1155 if (seg_reg == R_SS)
1156 raise_exception_err(EXCP0D_GPF, 0);
1157 cpu_x86_load_seg_cache(env, seg_reg, selector, NULL, 0, 0);
1164 index = selector & ~7;
1165 if ((index + 7) > dt->limit)
1166 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1167 ptr = dt->base + index;
1168 e1 = ldl_kernel(ptr);
1169 e2 = ldl_kernel(ptr + 4);
1171 if (!(e2 & DESC_S_MASK))
1172 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1174 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1175 cpl = env->hflags & HF_CPL_MASK;
1176 if (seg_reg == R_SS) {
1177 /* must be writable segment */
1178 if ((e2 & DESC_CS_MASK) || !(e2 & DESC_W_MASK))
1179 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1180 if (rpl != cpl || dpl != cpl)
1181 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1183 /* must be readable segment */
1184 if ((e2 & (DESC_CS_MASK | DESC_R_MASK)) == DESC_CS_MASK)
1185 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1187 if (!(e2 & DESC_CS_MASK) || !(e2 & DESC_C_MASK)) {
1188 /* if not conforming code, test rights */
1189 if (dpl < cpl || dpl < rpl)
1190 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1194 if (!(e2 & DESC_P_MASK)) {
1195 if (seg_reg == R_SS)
1196 raise_exception_err(EXCP0C_STACK, selector & 0xfffc);
1198 raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
1201 /* set the access bit if not already set */
1202 if (!(e2 & DESC_A_MASK)) {
1204 stl_kernel(ptr + 4, e2);
1207 cpu_x86_load_seg_cache(env, seg_reg, selector,
1208 get_seg_base(e1, e2),
1209 get_seg_limit(e1, e2),
1212 fprintf(logfile, "load_seg: sel=0x%04x base=0x%08lx limit=0x%08lx flags=%08x\n",
1213 selector, (unsigned long)sc->base, sc->limit, sc->flags);
1218 /* protected mode jump */
1219 void helper_ljmp_protected_T0_T1(void)
1221 int new_cs, new_eip, gate_cs, type;
1222 uint32_t e1, e2, cpl, dpl, rpl, limit;
1226 if ((new_cs & 0xfffc) == 0)
1227 raise_exception_err(EXCP0D_GPF, 0);
1228 if (load_segment(&e1, &e2, new_cs) != 0)
1229 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1230 cpl = env->hflags & HF_CPL_MASK;
1231 if (e2 & DESC_S_MASK) {
1232 if (!(e2 & DESC_CS_MASK))
1233 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1234 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1235 if (e2 & DESC_C_MASK) {
1236 /* conforming code segment */
1238 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1240 /* non conforming code segment */
1243 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1245 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1247 if (!(e2 & DESC_P_MASK))
1248 raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1249 limit = get_seg_limit(e1, e2);
1250 if (new_eip > limit)
1251 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1252 cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,
1253 get_seg_base(e1, e2), limit, e2);
1256 /* jump to call or task gate */
1257 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1259 cpl = env->hflags & HF_CPL_MASK;
1260 type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
1262 case 1: /* 286 TSS */
1263 case 9: /* 386 TSS */
1264 case 5: /* task gate */
1265 if (dpl < cpl || dpl < rpl)
1266 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1267 switch_tss(new_cs, e1, e2, SWITCH_TSS_JMP);
1269 case 4: /* 286 call gate */
1270 case 12: /* 386 call gate */
1271 if ((dpl < cpl) || (dpl < rpl))
1272 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1273 if (!(e2 & DESC_P_MASK))
1274 raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1276 if (load_segment(&e1, &e2, gate_cs) != 0)
1277 raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
1278 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1279 /* must be code segment */
1280 if (((e2 & (DESC_S_MASK | DESC_CS_MASK)) !=
1281 (DESC_S_MASK | DESC_CS_MASK)))
1282 raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
1283 if (((e2 & DESC_C_MASK) && (dpl > cpl)) ||
1284 (!(e2 & DESC_C_MASK) && (dpl != cpl)))
1285 raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
1286 if (!(e2 & DESC_P_MASK))
1287 raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
1288 new_eip = (e1 & 0xffff);
1290 new_eip |= (e2 & 0xffff0000);
1291 limit = get_seg_limit(e1, e2);
1292 if (new_eip > limit)
1293 raise_exception_err(EXCP0D_GPF, 0);
1294 cpu_x86_load_seg_cache(env, R_CS, (gate_cs & 0xfffc) | cpl,
1295 get_seg_base(e1, e2), limit, e2);
1299 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1305 /* real mode call */
1306 void helper_lcall_real_T0_T1(int shift, int next_eip)
1308 int new_cs, new_eip;
1309 uint32_t esp, esp_mask;
1315 esp_mask = get_sp_mask(env->segs[R_SS].flags);
1316 ssp = env->segs[R_SS].base;
1318 PUSHL(ssp, esp, esp_mask, env->segs[R_CS].selector);
1319 PUSHL(ssp, esp, esp_mask, next_eip);
1321 PUSHW(ssp, esp, esp_mask, env->segs[R_CS].selector);
1322 PUSHW(ssp, esp, esp_mask, next_eip);
1325 ESP = (ESP & ~esp_mask) | (esp & esp_mask);
1327 env->segs[R_CS].selector = new_cs;
1328 env->segs[R_CS].base = (uint8_t *)(new_cs << 4);
1331 /* protected mode call */
1332 void helper_lcall_protected_T0_T1(int shift, int next_eip)
1334 int new_cs, new_eip, new_stack, i;
1335 uint32_t e1, e2, cpl, dpl, rpl, selector, offset, param_count;
1336 uint32_t ss, ss_e1, ss_e2, sp, type, ss_dpl, sp_mask;
1337 uint32_t val, limit, old_sp_mask;
1338 uint8_t *ssp, *old_ssp;
1343 if (loglevel & CPU_LOG_PCALL) {
1344 fprintf(logfile, "lcall %04x:%08x s=%d\n",
1345 new_cs, new_eip, shift);
1346 cpu_x86_dump_state(env, logfile, X86_DUMP_CCOP);
1349 if ((new_cs & 0xfffc) == 0)
1350 raise_exception_err(EXCP0D_GPF, 0);
1351 if (load_segment(&e1, &e2, new_cs) != 0)
1352 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1353 cpl = env->hflags & HF_CPL_MASK;
1355 if (loglevel & CPU_LOG_PCALL) {
1356 fprintf(logfile, "desc=%08x:%08x\n", e1, e2);
1359 if (e2 & DESC_S_MASK) {
1360 if (!(e2 & DESC_CS_MASK))
1361 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1362 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1363 if (e2 & DESC_C_MASK) {
1364 /* conforming code segment */
1366 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1368 /* non conforming code segment */
1371 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1373 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1375 if (!(e2 & DESC_P_MASK))
1376 raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1379 sp_mask = get_sp_mask(env->segs[R_SS].flags);
1380 ssp = env->segs[R_SS].base;
1382 PUSHL(ssp, sp, sp_mask, env->segs[R_CS].selector);
1383 PUSHL(ssp, sp, sp_mask, next_eip);
1385 PUSHW(ssp, sp, sp_mask, env->segs[R_CS].selector);
1386 PUSHW(ssp, sp, sp_mask, next_eip);
1389 limit = get_seg_limit(e1, e2);
1390 if (new_eip > limit)
1391 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1392 /* from this point, not restartable */
1393 ESP = (ESP & ~sp_mask) | (sp & sp_mask);
1394 cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,
1395 get_seg_base(e1, e2), limit, e2);
1398 /* check gate type */
1399 type = (e2 >> DESC_TYPE_SHIFT) & 0x1f;
1400 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1403 case 1: /* available 286 TSS */
1404 case 9: /* available 386 TSS */
1405 case 5: /* task gate */
1406 if (dpl < cpl || dpl < rpl)
1407 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1408 switch_tss(new_cs, e1, e2, SWITCH_TSS_CALL);
1410 case 4: /* 286 call gate */
1411 case 12: /* 386 call gate */
1414 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1419 if (dpl < cpl || dpl < rpl)
1420 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1421 /* check valid bit */
1422 if (!(e2 & DESC_P_MASK))
1423 raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1424 selector = e1 >> 16;
1425 offset = (e2 & 0xffff0000) | (e1 & 0x0000ffff);
1426 param_count = e2 & 0x1f;
1427 if ((selector & 0xfffc) == 0)
1428 raise_exception_err(EXCP0D_GPF, 0);
1430 if (load_segment(&e1, &e2, selector) != 0)
1431 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1432 if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK)))
1433 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1434 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1436 raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1437 if (!(e2 & DESC_P_MASK))
1438 raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
1440 if (!(e2 & DESC_C_MASK) && dpl < cpl) {
1441 /* to inner priviledge */
1442 get_ss_esp_from_tss(&ss, &sp, dpl);
1444 if (loglevel & CPU_LOG_PCALL)
1445 fprintf(logfile, "new ss:esp=%04x:%08x param_count=%d ESP=%x\n",
1446 ss, sp, param_count, ESP);
1448 if ((ss & 0xfffc) == 0)
1449 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1450 if ((ss & 3) != dpl)
1451 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1452 if (load_segment(&ss_e1, &ss_e2, ss) != 0)
1453 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1454 ss_dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
1456 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1457 if (!(ss_e2 & DESC_S_MASK) ||
1458 (ss_e2 & DESC_CS_MASK) ||
1459 !(ss_e2 & DESC_W_MASK))
1460 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1461 if (!(ss_e2 & DESC_P_MASK))
1462 raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1464 // push_size = ((param_count * 2) + 8) << shift;
1466 old_sp_mask = get_sp_mask(env->segs[R_SS].flags);
1467 old_ssp = env->segs[R_SS].base;
1469 sp_mask = get_sp_mask(ss_e2);
1470 ssp = get_seg_base(ss_e1, ss_e2);
1472 PUSHL(ssp, sp, sp_mask, env->segs[R_SS].selector);
1473 PUSHL(ssp, sp, sp_mask, ESP);
1474 for(i = param_count - 1; i >= 0; i--) {
1475 val = ldl_kernel(old_ssp + ((ESP + i * 4) & old_sp_mask));
1476 PUSHL(ssp, sp, sp_mask, val);
1479 PUSHW(ssp, sp, sp_mask, env->segs[R_SS].selector);
1480 PUSHW(ssp, sp, sp_mask, ESP);
1481 for(i = param_count - 1; i >= 0; i--) {
1482 val = lduw_kernel(old_ssp + ((ESP + i * 2) & old_sp_mask));
1483 PUSHW(ssp, sp, sp_mask, val);
1488 /* to same priviledge */
1490 sp_mask = get_sp_mask(env->segs[R_SS].flags);
1491 ssp = env->segs[R_SS].base;
1492 // push_size = (4 << shift);
1497 PUSHL(ssp, sp, sp_mask, env->segs[R_CS].selector);
1498 PUSHL(ssp, sp, sp_mask, next_eip);
1500 PUSHW(ssp, sp, sp_mask, env->segs[R_CS].selector);
1501 PUSHW(ssp, sp, sp_mask, next_eip);
1504 /* from this point, not restartable */
1507 ss = (ss & ~3) | dpl;
1508 cpu_x86_load_seg_cache(env, R_SS, ss,
1510 get_seg_limit(ss_e1, ss_e2),
1514 selector = (selector & ~3) | dpl;
1515 cpu_x86_load_seg_cache(env, R_CS, selector,
1516 get_seg_base(e1, e2),
1517 get_seg_limit(e1, e2),
1519 cpu_x86_set_cpl(env, dpl);
1520 ESP = (ESP & ~sp_mask) | (sp & sp_mask);
1525 /* real and vm86 mode iret */
1526 void helper_iret_real(int shift)
1528 uint32_t sp, new_cs, new_eip, new_eflags, sp_mask;
1532 sp_mask = 0xffff; /* XXXX: use SS segment size ? */
1534 ssp = env->segs[R_SS].base;
1537 POPL(ssp, sp, sp_mask, new_eip);
1538 POPL(ssp, sp, sp_mask, new_cs);
1540 POPL(ssp, sp, sp_mask, new_eflags);
1543 POPW(ssp, sp, sp_mask, new_eip);
1544 POPW(ssp, sp, sp_mask, new_cs);
1545 POPW(ssp, sp, sp_mask, new_eflags);
1547 ESP = (ESP & ~sp_mask) | (sp & sp_mask);
1548 load_seg_vm(R_CS, new_cs);
1550 if (env->eflags & VM_MASK)
1551 eflags_mask = TF_MASK | AC_MASK | ID_MASK | IF_MASK | RF_MASK | NT_MASK;
1553 eflags_mask = TF_MASK | AC_MASK | ID_MASK | IF_MASK | IOPL_MASK | RF_MASK | NT_MASK;
1555 eflags_mask &= 0xffff;
1556 load_eflags(new_eflags, eflags_mask);
1559 static inline void validate_seg(int seg_reg, int cpl)
1564 e2 = env->segs[seg_reg].flags;
1565 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1566 if (!(e2 & DESC_CS_MASK) || !(e2 & DESC_C_MASK)) {
1567 /* data or non conforming code segment */
1569 cpu_x86_load_seg_cache(env, seg_reg, 0, NULL, 0, 0);
1574 /* protected mode iret */
1575 static inline void helper_ret_protected(int shift, int is_iret, int addend)
1577 uint32_t sp, new_cs, new_eip, new_eflags, new_esp, new_ss, sp_mask;
1578 uint32_t new_es, new_ds, new_fs, new_gs;
1579 uint32_t e1, e2, ss_e1, ss_e2;
1580 int cpl, dpl, rpl, eflags_mask, iopl;
1583 sp_mask = get_sp_mask(env->segs[R_SS].flags);
1585 ssp = env->segs[R_SS].base;
1588 POPL(ssp, sp, sp_mask, new_eip);
1589 POPL(ssp, sp, sp_mask, new_cs);
1592 POPL(ssp, sp, sp_mask, new_eflags);
1593 if (new_eflags & VM_MASK)
1594 goto return_to_vm86;
1598 POPW(ssp, sp, sp_mask, new_eip);
1599 POPW(ssp, sp, sp_mask, new_cs);
1601 POPW(ssp, sp, sp_mask, new_eflags);
1604 if (loglevel & CPU_LOG_PCALL) {
1605 fprintf(logfile, "lret new %04x:%08x s=%d addend=0x%x\n",
1606 new_cs, new_eip, shift, addend);
1607 cpu_x86_dump_state(env, logfile, X86_DUMP_CCOP);
1610 if ((new_cs & 0xfffc) == 0)
1611 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1612 if (load_segment(&e1, &e2, new_cs) != 0)
1613 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1614 if (!(e2 & DESC_S_MASK) ||
1615 !(e2 & DESC_CS_MASK))
1616 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1617 cpl = env->hflags & HF_CPL_MASK;
1620 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1621 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1622 if (e2 & DESC_C_MASK) {
1624 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1627 raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1629 if (!(e2 & DESC_P_MASK))
1630 raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1634 /* return to same priledge level */
1635 cpu_x86_load_seg_cache(env, R_CS, new_cs,
1636 get_seg_base(e1, e2),
1637 get_seg_limit(e1, e2),
1640 /* return to different priviledge level */
1643 POPL(ssp, sp, sp_mask, new_esp);
1644 POPL(ssp, sp, sp_mask, new_ss);
1648 POPW(ssp, sp, sp_mask, new_esp);
1649 POPW(ssp, sp, sp_mask, new_ss);
1652 if (loglevel & CPU_LOG_PCALL) {
1653 fprintf(logfile, "new ss:esp=%04x:%08x\n",
1658 if ((new_ss & 3) != rpl)
1659 raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
1660 if (load_segment(&ss_e1, &ss_e2, new_ss) != 0)
1661 raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
1662 if (!(ss_e2 & DESC_S_MASK) ||
1663 (ss_e2 & DESC_CS_MASK) ||
1664 !(ss_e2 & DESC_W_MASK))
1665 raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
1666 dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
1668 raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
1669 if (!(ss_e2 & DESC_P_MASK))
1670 raise_exception_err(EXCP0B_NOSEG, new_ss & 0xfffc);
1672 cpu_x86_load_seg_cache(env, R_CS, new_cs,
1673 get_seg_base(e1, e2),
1674 get_seg_limit(e1, e2),
1676 cpu_x86_load_seg_cache(env, R_SS, new_ss,
1677 get_seg_base(ss_e1, ss_e2),
1678 get_seg_limit(ss_e1, ss_e2),
1680 cpu_x86_set_cpl(env, rpl);
1682 /* XXX: change sp_mask according to old segment ? */
1684 /* validate data segments */
1685 validate_seg(R_ES, cpl);
1686 validate_seg(R_DS, cpl);
1687 validate_seg(R_FS, cpl);
1688 validate_seg(R_GS, cpl);
1692 ESP = (ESP & ~sp_mask) | (sp & sp_mask);
1695 /* NOTE: 'cpl' is the _old_ CPL */
1696 eflags_mask = TF_MASK | AC_MASK | ID_MASK | RF_MASK | NT_MASK;
1698 eflags_mask |= IOPL_MASK;
1699 iopl = (env->eflags >> IOPL_SHIFT) & 3;
1701 eflags_mask |= IF_MASK;
1703 eflags_mask &= 0xffff;
1704 load_eflags(new_eflags, eflags_mask);
1709 POPL(ssp, sp, sp_mask, new_esp);
1710 POPL(ssp, sp, sp_mask, new_ss);
1711 POPL(ssp, sp, sp_mask, new_es);
1712 POPL(ssp, sp, sp_mask, new_ds);
1713 POPL(ssp, sp, sp_mask, new_fs);
1714 POPL(ssp, sp, sp_mask, new_gs);
1716 /* modify processor state */
1717 load_eflags(new_eflags, TF_MASK | AC_MASK | ID_MASK |
1718 IF_MASK | IOPL_MASK | VM_MASK | NT_MASK | VIF_MASK | VIP_MASK);
1719 load_seg_vm(R_CS, new_cs & 0xffff);
1720 cpu_x86_set_cpl(env, 3);
1721 load_seg_vm(R_SS, new_ss & 0xffff);
1722 load_seg_vm(R_ES, new_es & 0xffff);
1723 load_seg_vm(R_DS, new_ds & 0xffff);
1724 load_seg_vm(R_FS, new_fs & 0xffff);
1725 load_seg_vm(R_GS, new_gs & 0xffff);
1731 void helper_iret_protected(int shift)
1733 int tss_selector, type;
1736 /* specific case for TSS */
1737 if (env->eflags & NT_MASK) {
1738 tss_selector = lduw_kernel(env->tr.base + 0);
1739 if (tss_selector & 4)
1740 raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
1741 if (load_segment(&e1, &e2, tss_selector) != 0)
1742 raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
1743 type = (e2 >> DESC_TYPE_SHIFT) & 0x17;
1744 /* NOTE: we check both segment and busy TSS */
1746 raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
1747 switch_tss(tss_selector, e1, e2, SWITCH_TSS_IRET);
1749 helper_ret_protected(shift, 1, 0);
1753 void helper_lret_protected(int shift, int addend)
1755 helper_ret_protected(shift, 0, addend);
1758 void helper_movl_crN_T0(int reg)
1762 cpu_x86_update_cr0(env, T0);
1765 cpu_x86_update_cr3(env, T0);
1768 cpu_x86_update_cr4(env, T0);
1777 void helper_movl_drN_T0(int reg)
1782 void helper_invlpg(unsigned int addr)
1784 cpu_x86_flush_tlb(env, addr);
1788 #if !defined(__i386__) && !defined(__x86_64__)
1792 void helper_rdtsc(void)
1795 #if defined(__i386__) || defined(__x86_64__)
1796 asm("rdtsc" : "=A" (val));
1798 /* better than nothing: the time increases */
1805 void helper_wrmsr(void)
1808 case MSR_IA32_SYSENTER_CS:
1809 env->sysenter_cs = EAX & 0xffff;
1811 case MSR_IA32_SYSENTER_ESP:
1812 env->sysenter_esp = EAX;
1814 case MSR_IA32_SYSENTER_EIP:
1815 env->sysenter_eip = EAX;
1818 /* XXX: exception ? */
1823 void helper_rdmsr(void)
1826 case MSR_IA32_SYSENTER_CS:
1827 EAX = env->sysenter_cs;
1830 case MSR_IA32_SYSENTER_ESP:
1831 EAX = env->sysenter_esp;
1834 case MSR_IA32_SYSENTER_EIP:
1835 EAX = env->sysenter_eip;
1839 /* XXX: exception ? */
1844 void helper_lsl(void)
1846 unsigned int selector, limit;
1848 int rpl, dpl, cpl, type;
1850 CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
1851 selector = T0 & 0xffff;
1852 if (load_segment(&e1, &e2, selector) != 0)
1855 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1856 cpl = env->hflags & HF_CPL_MASK;
1857 if (e2 & DESC_S_MASK) {
1858 if ((e2 & DESC_CS_MASK) && (e2 & DESC_C_MASK)) {
1861 if (dpl < cpl || dpl < rpl)
1865 type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
1876 if (dpl < cpl || dpl < rpl)
1879 limit = get_seg_limit(e1, e2);
1884 void helper_lar(void)
1886 unsigned int selector;
1888 int rpl, dpl, cpl, type;
1890 CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
1891 selector = T0 & 0xffff;
1892 if ((selector & 0xfffc) == 0)
1894 if (load_segment(&e1, &e2, selector) != 0)
1897 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1898 cpl = env->hflags & HF_CPL_MASK;
1899 if (e2 & DESC_S_MASK) {
1900 if ((e2 & DESC_CS_MASK) && (e2 & DESC_C_MASK)) {
1903 if (dpl < cpl || dpl < rpl)
1907 type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
1921 if (dpl < cpl || dpl < rpl)
1924 T1 = e2 & 0x00f0ff00;
1928 void helper_verr(void)
1930 unsigned int selector;
1934 CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
1935 selector = T0 & 0xffff;
1936 if ((selector & 0xfffc) == 0)
1938 if (load_segment(&e1, &e2, selector) != 0)
1940 if (!(e2 & DESC_S_MASK))
1943 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1944 cpl = env->hflags & HF_CPL_MASK;
1945 if (e2 & DESC_CS_MASK) {
1946 if (!(e2 & DESC_R_MASK))
1948 if (!(e2 & DESC_C_MASK)) {
1949 if (dpl < cpl || dpl < rpl)
1953 if (dpl < cpl || dpl < rpl)
1959 void helper_verw(void)
1961 unsigned int selector;
1965 CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
1966 selector = T0 & 0xffff;
1967 if ((selector & 0xfffc) == 0)
1969 if (load_segment(&e1, &e2, selector) != 0)
1971 if (!(e2 & DESC_S_MASK))
1974 dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1975 cpl = env->hflags & HF_CPL_MASK;
1976 if (e2 & DESC_CS_MASK) {
1979 if (dpl < cpl || dpl < rpl)
1981 if (!(e2 & DESC_W_MASK))
1989 void helper_fldt_ST0_A0(void)
1992 new_fpstt = (env->fpstt - 1) & 7;
1993 env->fpregs[new_fpstt] = helper_fldt((uint8_t *)A0);
1994 env->fpstt = new_fpstt;
1995 env->fptags[new_fpstt] = 0; /* validate stack entry */
1998 void helper_fstt_ST0_A0(void)
2000 helper_fstt(ST0, (uint8_t *)A0);
2005 #define MUL10(iv) ( iv + iv + (iv << 3) )
2007 void helper_fbld_ST0_A0(void)
2015 for(i = 8; i >= 0; i--) {
2016 v = ldub((uint8_t *)A0 + i);
2017 val = (val * 100) + ((v >> 4) * 10) + (v & 0xf);
2020 if (ldub((uint8_t *)A0 + 9) & 0x80)
2026 void helper_fbst_ST0_A0(void)
2030 uint8_t *mem_ref, *mem_end;
2035 mem_ref = (uint8_t *)A0;
2036 mem_end = mem_ref + 9;
2043 while (mem_ref < mem_end) {
2048 v = ((v / 10) << 4) | (v % 10);
2051 while (mem_ref < mem_end) {
2056 void helper_f2xm1(void)
2058 ST0 = pow(2.0,ST0) - 1.0;
2061 void helper_fyl2x(void)
2063 CPU86_LDouble fptemp;
2067 fptemp = log(fptemp)/log(2.0); /* log2(ST) */
2071 env->fpus &= (~0x4700);
2076 void helper_fptan(void)
2078 CPU86_LDouble fptemp;
2081 if((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2087 env->fpus &= (~0x400); /* C2 <-- 0 */
2088 /* the above code is for |arg| < 2**52 only */
2092 void helper_fpatan(void)
2094 CPU86_LDouble fptemp, fpsrcop;
2098 ST1 = atan2(fpsrcop,fptemp);
2102 void helper_fxtract(void)
2104 CPU86_LDoubleU temp;
2105 unsigned int expdif;
2108 expdif = EXPD(temp) - EXPBIAS;
2109 /*DP exponent bias*/
2116 void helper_fprem1(void)
2118 CPU86_LDouble dblq, fpsrcop, fptemp;
2119 CPU86_LDoubleU fpsrcop1, fptemp1;
2125 fpsrcop1.d = fpsrcop;
2127 expdif = EXPD(fpsrcop1) - EXPD(fptemp1);
2129 dblq = fpsrcop / fptemp;
2130 dblq = (dblq < 0.0)? ceil(dblq): floor(dblq);
2131 ST0 = fpsrcop - fptemp*dblq;
2132 q = (int)dblq; /* cutting off top bits is assumed here */
2133 env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2134 /* (C0,C1,C3) <-- (q2,q1,q0) */
2135 env->fpus |= (q&0x4) << 6; /* (C0) <-- q2 */
2136 env->fpus |= (q&0x2) << 8; /* (C1) <-- q1 */
2137 env->fpus |= (q&0x1) << 14; /* (C3) <-- q0 */
2139 env->fpus |= 0x400; /* C2 <-- 1 */
2140 fptemp = pow(2.0, expdif-50);
2141 fpsrcop = (ST0 / ST1) / fptemp;
2142 /* fpsrcop = integer obtained by rounding to the nearest */
2143 fpsrcop = (fpsrcop-floor(fpsrcop) < ceil(fpsrcop)-fpsrcop)?
2144 floor(fpsrcop): ceil(fpsrcop);
2145 ST0 -= (ST1 * fpsrcop * fptemp);
2149 void helper_fprem(void)
2151 CPU86_LDouble dblq, fpsrcop, fptemp;
2152 CPU86_LDoubleU fpsrcop1, fptemp1;
2158 fpsrcop1.d = fpsrcop;
2160 expdif = EXPD(fpsrcop1) - EXPD(fptemp1);
2161 if ( expdif < 53 ) {
2162 dblq = fpsrcop / fptemp;
2163 dblq = (dblq < 0.0)? ceil(dblq): floor(dblq);
2164 ST0 = fpsrcop - fptemp*dblq;
2165 q = (int)dblq; /* cutting off top bits is assumed here */
2166 env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2167 /* (C0,C1,C3) <-- (q2,q1,q0) */
2168 env->fpus |= (q&0x4) << 6; /* (C0) <-- q2 */
2169 env->fpus |= (q&0x2) << 8; /* (C1) <-- q1 */
2170 env->fpus |= (q&0x1) << 14; /* (C3) <-- q0 */
2172 env->fpus |= 0x400; /* C2 <-- 1 */
2173 fptemp = pow(2.0, expdif-50);
2174 fpsrcop = (ST0 / ST1) / fptemp;
2175 /* fpsrcop = integer obtained by chopping */
2176 fpsrcop = (fpsrcop < 0.0)?
2177 -(floor(fabs(fpsrcop))): floor(fpsrcop);
2178 ST0 -= (ST1 * fpsrcop * fptemp);
2182 void helper_fyl2xp1(void)
2184 CPU86_LDouble fptemp;
2187 if ((fptemp+1.0)>0.0) {
2188 fptemp = log(fptemp+1.0) / log(2.0); /* log2(ST+1.0) */
2192 env->fpus &= (~0x4700);
2197 void helper_fsqrt(void)
2199 CPU86_LDouble fptemp;
2203 env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2209 void helper_fsincos(void)
2211 CPU86_LDouble fptemp;
2214 if ((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2220 env->fpus &= (~0x400); /* C2 <-- 0 */
2221 /* the above code is for |arg| < 2**63 only */
2225 void helper_frndint(void)
2231 switch(env->fpuc & RC_MASK) {
2234 asm("rndd %0, %1" : "=f" (a) : "f"(a));
2237 asm("rnddm %0, %1" : "=f" (a) : "f"(a));
2240 asm("rnddp %0, %1" : "=f" (a) : "f"(a));
2243 asm("rnddz %0, %1" : "=f" (a) : "f"(a));
2252 void helper_fscale(void)
2254 CPU86_LDouble fpsrcop, fptemp;
2257 fptemp = pow(fpsrcop,ST1);
2261 void helper_fsin(void)
2263 CPU86_LDouble fptemp;
2266 if ((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2270 env->fpus &= (~0x400); /* C2 <-- 0 */
2271 /* the above code is for |arg| < 2**53 only */
2275 void helper_fcos(void)
2277 CPU86_LDouble fptemp;
2280 if((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2284 env->fpus &= (~0x400); /* C2 <-- 0 */
2285 /* the above code is for |arg5 < 2**63 only */
2289 void helper_fxam_ST0(void)
2291 CPU86_LDoubleU temp;
2296 env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2298 env->fpus |= 0x200; /* C1 <-- 1 */
2300 expdif = EXPD(temp);
2301 if (expdif == MAXEXPD) {
2302 if (MANTD(temp) == 0)
2303 env->fpus |= 0x500 /*Infinity*/;
2305 env->fpus |= 0x100 /*NaN*/;
2306 } else if (expdif == 0) {
2307 if (MANTD(temp) == 0)
2308 env->fpus |= 0x4000 /*Zero*/;
2310 env->fpus |= 0x4400 /*Denormal*/;
2316 void helper_fstenv(uint8_t *ptr, int data32)
2318 int fpus, fptag, exp, i;
2322 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
2324 for (i=7; i>=0; i--) {
2326 if (env->fptags[i]) {
2329 tmp.d = env->fpregs[i];
2332 if (exp == 0 && mant == 0) {
2335 } else if (exp == 0 || exp == MAXEXPD
2336 #ifdef USE_X86LDOUBLE
2337 || (mant & (1LL << 63)) == 0
2340 /* NaNs, infinity, denormal */
2347 stl(ptr, env->fpuc);
2349 stl(ptr + 8, fptag);
2350 stl(ptr + 12, 0); /* fpip */
2351 stl(ptr + 16, 0); /* fpcs */
2352 stl(ptr + 20, 0); /* fpoo */
2353 stl(ptr + 24, 0); /* fpos */
2356 stw(ptr, env->fpuc);
2358 stw(ptr + 4, fptag);
2366 void helper_fldenv(uint8_t *ptr, int data32)
2371 env->fpuc = lduw(ptr);
2372 fpus = lduw(ptr + 4);
2373 fptag = lduw(ptr + 8);
2376 env->fpuc = lduw(ptr);
2377 fpus = lduw(ptr + 2);
2378 fptag = lduw(ptr + 4);
2380 env->fpstt = (fpus >> 11) & 7;
2381 env->fpus = fpus & ~0x3800;
2382 for(i = 0;i < 8; i++) {
2383 env->fptags[i] = ((fptag & 3) == 3);
2388 void helper_fsave(uint8_t *ptr, int data32)
2393 helper_fstenv(ptr, data32);
2395 ptr += (14 << data32);
2396 for(i = 0;i < 8; i++) {
2398 helper_fstt(tmp, ptr);
2416 void helper_frstor(uint8_t *ptr, int data32)
2421 helper_fldenv(ptr, data32);
2422 ptr += (14 << data32);
2424 for(i = 0;i < 8; i++) {
2425 tmp = helper_fldt(ptr);
2431 #if !defined(CONFIG_USER_ONLY)
2433 #define MMUSUFFIX _mmu
2434 #define GETPC() (__builtin_return_address(0))
2437 #include "softmmu_template.h"
2440 #include "softmmu_template.h"
2443 #include "softmmu_template.h"
2446 #include "softmmu_template.h"
2450 /* try to fill the TLB and return an exception if error. If retaddr is
2451 NULL, it means that the function was called in C code (i.e. not
2452 from generated code or from helper.c) */
2453 /* XXX: fix it to restore all registers */
2454 void tlb_fill(unsigned long addr, int is_write, int is_user, void *retaddr)
2456 TranslationBlock *tb;
2459 CPUX86State *saved_env;
2461 /* XXX: hack to restore env in all cases, even if not called from
2464 env = cpu_single_env;
2466 ret = cpu_x86_handle_mmu_fault(env, addr, is_write, is_user, 1);
2469 /* now we have a real cpu fault */
2470 pc = (unsigned long)retaddr;
2471 tb = tb_find_pc(pc);
2473 /* the PC is inside the translated code. It means that we have
2474 a virtual CPU fault */
2475 cpu_restore_state(tb, env, pc, NULL);
2478 raise_exception_err(EXCP0E_PAGE, env->error_code);