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
,
283 int tss_limit
, tss_limit_max
, type
, old_tss_limit_max
, old_type
, v1
, v2
, i
;
285 uint32_t new_regs
[8], new_segs
[6];
286 uint32_t new_eflags
, new_eip
, new_cr3
, new_ldt
, new_trap
;
287 uint32_t old_eflags
, eflags_mask
;
292 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
294 if (loglevel
& CPU_LOG_PCALL
)
295 fprintf(logfile
, "switch_tss: sel=0x%04x type=%d src=%d\n", tss_selector
, type
, source
);
298 /* if task gate, we read the TSS segment and we load it */
300 if (!(e2
& DESC_P_MASK
))
301 raise_exception_err(EXCP0B_NOSEG
, tss_selector
& 0xfffc);
302 tss_selector
= e1
>> 16;
303 if (tss_selector
& 4)
304 raise_exception_err(EXCP0A_TSS
, tss_selector
& 0xfffc);
305 if (load_segment(&e1
, &e2
, tss_selector
) != 0)
306 raise_exception_err(EXCP0D_GPF
, tss_selector
& 0xfffc);
307 if (e2
& DESC_S_MASK
)
308 raise_exception_err(EXCP0D_GPF
, tss_selector
& 0xfffc);
309 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
311 raise_exception_err(EXCP0D_GPF
, tss_selector
& 0xfffc);
314 if (!(e2
& DESC_P_MASK
))
315 raise_exception_err(EXCP0B_NOSEG
, tss_selector
& 0xfffc);
321 tss_limit
= get_seg_limit(e1
, e2
);
322 tss_base
= get_seg_base(e1
, e2
);
323 if ((tss_selector
& 4) != 0 ||
324 tss_limit
< tss_limit_max
)
325 raise_exception_err(EXCP0A_TSS
, tss_selector
& 0xfffc);
326 old_type
= (env
->tr
.flags
>> DESC_TYPE_SHIFT
) & 0xf;
328 old_tss_limit_max
= 103;
330 old_tss_limit_max
= 43;
332 /* read all the registers from the new TSS */
335 new_cr3
= ldl_kernel(tss_base
+ 0x1c);
336 new_eip
= ldl_kernel(tss_base
+ 0x20);
337 new_eflags
= ldl_kernel(tss_base
+ 0x24);
338 for(i
= 0; i
< 8; i
++)
339 new_regs
[i
] = ldl_kernel(tss_base
+ (0x28 + i
* 4));
340 for(i
= 0; i
< 6; i
++)
341 new_segs
[i
] = lduw_kernel(tss_base
+ (0x48 + i
* 4));
342 new_ldt
= lduw_kernel(tss_base
+ 0x60);
343 new_trap
= ldl_kernel(tss_base
+ 0x64);
347 new_eip
= lduw_kernel(tss_base
+ 0x0e);
348 new_eflags
= lduw_kernel(tss_base
+ 0x10);
349 for(i
= 0; i
< 8; i
++)
350 new_regs
[i
] = lduw_kernel(tss_base
+ (0x12 + i
* 2)) | 0xffff0000;
351 for(i
= 0; i
< 4; i
++)
352 new_segs
[i
] = lduw_kernel(tss_base
+ (0x22 + i
* 4));
353 new_ldt
= lduw_kernel(tss_base
+ 0x2a);
359 /* NOTE: we must avoid memory exceptions during the task switch,
360 so we make dummy accesses before */
361 /* XXX: it can still fail in some cases, so a bigger hack is
362 necessary to valid the TLB after having done the accesses */
364 v1
= ldub_kernel(env
->tr
.base
);
365 v2
= ldub(env
->tr
.base
+ old_tss_limit_max
);
366 stb_kernel(env
->tr
.base
, v1
);
367 stb_kernel(env
->tr
.base
+ old_tss_limit_max
, v2
);
369 /* clear busy bit (it is restartable) */
370 if (source
== SWITCH_TSS_JMP
|| source
== SWITCH_TSS_IRET
) {
373 ptr
= env
->gdt
.base
+ (env
->tr
.selector
& ~7);
374 e2
= ldl_kernel(ptr
+ 4);
375 e2
&= ~DESC_TSS_BUSY_MASK
;
376 stl_kernel(ptr
+ 4, e2
);
378 old_eflags
= compute_eflags();
379 if (source
== SWITCH_TSS_IRET
)
380 old_eflags
&= ~NT_MASK
;
382 /* save the current state in the old TSS */
385 stl_kernel(env
->tr
.base
+ 0x20, next_eip
);
386 stl_kernel(env
->tr
.base
+ 0x24, old_eflags
);
387 for(i
= 0; i
< 8; i
++)
388 stl_kernel(env
->tr
.base
+ (0x28 + i
* 4), env
->regs
[i
]);
389 for(i
= 0; i
< 6; i
++)
390 stw_kernel(env
->tr
.base
+ (0x48 + i
* 4), env
->segs
[i
].selector
);
393 stw_kernel(env
->tr
.base
+ 0x0e, next_eip
);
394 stw_kernel(env
->tr
.base
+ 0x10, old_eflags
);
395 for(i
= 0; i
< 8; i
++)
396 stw_kernel(env
->tr
.base
+ (0x12 + i
* 2), env
->regs
[i
]);
397 for(i
= 0; i
< 4; i
++)
398 stw_kernel(env
->tr
.base
+ (0x22 + i
* 4), env
->segs
[i
].selector
);
401 /* now if an exception occurs, it will occurs in the next task
404 if (source
== SWITCH_TSS_CALL
) {
405 stw_kernel(tss_base
, env
->tr
.selector
);
406 new_eflags
|= NT_MASK
;
410 if (source
== SWITCH_TSS_JMP
|| source
== SWITCH_TSS_CALL
) {
413 ptr
= env
->gdt
.base
+ (tss_selector
& ~7);
414 e2
= ldl_kernel(ptr
+ 4);
415 e2
|= DESC_TSS_BUSY_MASK
;
416 stl_kernel(ptr
+ 4, e2
);
419 /* set the new CPU state */
420 /* from this point, any exception which occurs can give problems */
421 env
->cr
[0] |= CR0_TS_MASK
;
422 env
->hflags
|= HF_TS_MASK
;
423 env
->tr
.selector
= tss_selector
;
424 env
->tr
.base
= tss_base
;
425 env
->tr
.limit
= tss_limit
;
426 env
->tr
.flags
= e2
& ~DESC_TSS_BUSY_MASK
;
428 if ((type
& 8) && (env
->cr
[0] & CR0_PG_MASK
)) {
429 cpu_x86_update_cr3(env
, new_cr3
);
432 /* load all registers without an exception, then reload them with
433 possible exception */
435 eflags_mask
= TF_MASK
| AC_MASK
| ID_MASK
|
436 IF_MASK
| IOPL_MASK
| VM_MASK
| RF_MASK
| NT_MASK
;
438 eflags_mask
&= 0xffff;
439 load_eflags(new_eflags
, eflags_mask
);
440 for(i
= 0; i
< 8; i
++)
441 env
->regs
[i
] = new_regs
[i
];
442 if (new_eflags
& VM_MASK
) {
443 for(i
= 0; i
< 6; i
++)
444 load_seg_vm(i
, new_segs
[i
]);
445 /* in vm86, CPL is always 3 */
446 cpu_x86_set_cpl(env
, 3);
448 /* CPL is set the RPL of CS */
449 cpu_x86_set_cpl(env
, new_segs
[R_CS
] & 3);
450 /* first just selectors as the rest may trigger exceptions */
451 for(i
= 0; i
< 6; i
++)
452 cpu_x86_load_seg_cache(env
, i
, new_segs
[i
], NULL
, 0, 0);
455 env
->ldt
.selector
= new_ldt
& ~4;
456 env
->ldt
.base
= NULL
;
462 raise_exception_err(EXCP0A_TSS
, new_ldt
& 0xfffc);
464 if ((new_ldt
& 0xfffc) != 0) {
466 index
= new_ldt
& ~7;
467 if ((index
+ 7) > dt
->limit
)
468 raise_exception_err(EXCP0A_TSS
, new_ldt
& 0xfffc);
469 ptr
= dt
->base
+ index
;
470 e1
= ldl_kernel(ptr
);
471 e2
= ldl_kernel(ptr
+ 4);
472 if ((e2
& DESC_S_MASK
) || ((e2
>> DESC_TYPE_SHIFT
) & 0xf) != 2)
473 raise_exception_err(EXCP0A_TSS
, new_ldt
& 0xfffc);
474 if (!(e2
& DESC_P_MASK
))
475 raise_exception_err(EXCP0A_TSS
, new_ldt
& 0xfffc);
476 load_seg_cache_raw_dt(&env
->ldt
, e1
, e2
);
479 /* load the segments */
480 if (!(new_eflags
& VM_MASK
)) {
481 tss_load_seg(R_CS
, new_segs
[R_CS
]);
482 tss_load_seg(R_SS
, new_segs
[R_SS
]);
483 tss_load_seg(R_ES
, new_segs
[R_ES
]);
484 tss_load_seg(R_DS
, new_segs
[R_DS
]);
485 tss_load_seg(R_FS
, new_segs
[R_FS
]);
486 tss_load_seg(R_GS
, new_segs
[R_GS
]);
489 /* check that EIP is in the CS segment limits */
490 if (new_eip
> env
->segs
[R_CS
].limit
) {
491 /* XXX: different exception if CALL ? */
492 raise_exception_err(EXCP0D_GPF
, 0);
496 /* check if Port I/O is allowed in TSS */
497 static inline void check_io(int addr
, int size
)
499 int io_offset
, val
, mask
;
501 /* TSS must be a valid 32 bit one */
502 if (!(env
->tr
.flags
& DESC_P_MASK
) ||
503 ((env
->tr
.flags
>> DESC_TYPE_SHIFT
) & 0xf) != 9 ||
506 io_offset
= lduw_kernel(env
->tr
.base
+ 0x66);
507 io_offset
+= (addr
>> 3);
508 /* Note: the check needs two bytes */
509 if ((io_offset
+ 1) > env
->tr
.limit
)
511 val
= lduw_kernel(env
->tr
.base
+ io_offset
);
513 mask
= (1 << size
) - 1;
514 /* all bits must be zero to allow the I/O */
515 if ((val
& mask
) != 0) {
517 raise_exception_err(EXCP0D_GPF
, 0);
521 void check_iob_T0(void)
526 void check_iow_T0(void)
531 void check_iol_T0(void)
536 void check_iob_DX(void)
538 check_io(EDX
& 0xffff, 1);
541 void check_iow_DX(void)
543 check_io(EDX
& 0xffff, 2);
546 void check_iol_DX(void)
548 check_io(EDX
& 0xffff, 4);
551 static inline unsigned int get_sp_mask(unsigned int e2
)
553 if (e2
& DESC_B_MASK
)
559 /* XXX: add a is_user flag to have proper security support */
560 #define PUSHW(ssp, sp, sp_mask, val)\
563 stw_kernel((ssp) + (sp & (sp_mask)), (val));\
566 #define PUSHL(ssp, sp, sp_mask, val)\
569 stl_kernel((ssp) + (sp & (sp_mask)), (val));\
572 #define POPW(ssp, sp, sp_mask, val)\
574 val = lduw_kernel((ssp) + (sp & (sp_mask)));\
578 #define POPL(ssp, sp, sp_mask, val)\
580 val = ldl_kernel((ssp) + (sp & (sp_mask)));\
584 /* protected mode interrupt */
585 static void do_interrupt_protected(int intno
, int is_int
, int error_code
,
586 unsigned int next_eip
, int is_hw
)
590 int type
, dpl
, selector
, ss_dpl
, cpl
, sp_mask
;
591 int has_error_code
, new_stack
, shift
;
592 uint32_t e1
, e2
, offset
, ss
, esp
, ss_e1
, ss_e2
;
596 if (!is_int
&& !is_hw
) {
615 if (intno
* 8 + 7 > dt
->limit
)
616 raise_exception_err(EXCP0D_GPF
, intno
* 8 + 2);
617 ptr
= dt
->base
+ intno
* 8;
618 e1
= ldl_kernel(ptr
);
619 e2
= ldl_kernel(ptr
+ 4);
620 /* check gate type */
621 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x1f;
623 case 5: /* task gate */
624 /* must do that check here to return the correct error code */
625 if (!(e2
& DESC_P_MASK
))
626 raise_exception_err(EXCP0B_NOSEG
, intno
* 8 + 2);
627 switch_tss(intno
* 8, e1
, e2
, SWITCH_TSS_CALL
, old_eip
);
628 if (has_error_code
) {
630 /* push the error code */
631 shift
= (env
->segs
[R_CS
].flags
>> DESC_B_SHIFT
) & 1;
632 if (env
->segs
[R_SS
].flags
& DESC_B_MASK
)
636 esp
= (env
->regs
[R_ESP
] - (2 << shift
)) & mask
;
637 ssp
= env
->segs
[R_SS
].base
+ esp
;
639 stl_kernel(ssp
, error_code
);
641 stw_kernel(ssp
, error_code
);
642 env
->regs
[R_ESP
] = (esp
& mask
) | (env
->regs
[R_ESP
] & ~mask
);
645 case 6: /* 286 interrupt gate */
646 case 7: /* 286 trap gate */
647 case 14: /* 386 interrupt gate */
648 case 15: /* 386 trap gate */
651 raise_exception_err(EXCP0D_GPF
, intno
* 8 + 2);
654 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
655 cpl
= env
->hflags
& HF_CPL_MASK
;
656 /* check privledge if software int */
657 if (is_int
&& dpl
< cpl
)
658 raise_exception_err(EXCP0D_GPF
, intno
* 8 + 2);
659 /* check valid bit */
660 if (!(e2
& DESC_P_MASK
))
661 raise_exception_err(EXCP0B_NOSEG
, intno
* 8 + 2);
663 offset
= (e2
& 0xffff0000) | (e1
& 0x0000ffff);
664 if ((selector
& 0xfffc) == 0)
665 raise_exception_err(EXCP0D_GPF
, 0);
667 if (load_segment(&e1
, &e2
, selector
) != 0)
668 raise_exception_err(EXCP0D_GPF
, selector
& 0xfffc);
669 if (!(e2
& DESC_S_MASK
) || !(e2
& (DESC_CS_MASK
)))
670 raise_exception_err(EXCP0D_GPF
, selector
& 0xfffc);
671 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
673 raise_exception_err(EXCP0D_GPF
, selector
& 0xfffc);
674 if (!(e2
& DESC_P_MASK
))
675 raise_exception_err(EXCP0B_NOSEG
, selector
& 0xfffc);
676 if (!(e2
& DESC_C_MASK
) && dpl
< cpl
) {
677 /* to inner priviledge */
678 get_ss_esp_from_tss(&ss
, &esp
, dpl
);
679 if ((ss
& 0xfffc) == 0)
680 raise_exception_err(EXCP0A_TSS
, ss
& 0xfffc);
682 raise_exception_err(EXCP0A_TSS
, ss
& 0xfffc);
683 if (load_segment(&ss_e1
, &ss_e2
, ss
) != 0)
684 raise_exception_err(EXCP0A_TSS
, ss
& 0xfffc);
685 ss_dpl
= (ss_e2
>> DESC_DPL_SHIFT
) & 3;
687 raise_exception_err(EXCP0A_TSS
, ss
& 0xfffc);
688 if (!(ss_e2
& DESC_S_MASK
) ||
689 (ss_e2
& DESC_CS_MASK
) ||
690 !(ss_e2
& DESC_W_MASK
))
691 raise_exception_err(EXCP0A_TSS
, ss
& 0xfffc);
692 if (!(ss_e2
& DESC_P_MASK
))
693 raise_exception_err(EXCP0A_TSS
, ss
& 0xfffc);
695 sp_mask
= get_sp_mask(ss_e2
);
696 ssp
= get_seg_base(ss_e1
, ss_e2
);
697 } else if ((e2
& DESC_C_MASK
) || dpl
== cpl
) {
698 /* to same priviledge */
699 if (env
->eflags
& VM_MASK
)
700 raise_exception_err(EXCP0D_GPF
, selector
& 0xfffc);
702 sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
703 ssp
= env
->segs
[R_SS
].base
;
707 raise_exception_err(EXCP0D_GPF
, selector
& 0xfffc);
708 new_stack
= 0; /* avoid warning */
709 sp_mask
= 0; /* avoid warning */
710 ssp
= NULL
; /* avoid warning */
711 esp
= 0; /* avoid warning */
717 /* XXX: check that enough room is available */
718 push_size
= 6 + (new_stack
<< 2) + (has_error_code
<< 1);
719 if (env
->eflags
& VM_MASK
)
725 if (env
->eflags
& VM_MASK
) {
726 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_GS
].selector
);
727 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_FS
].selector
);
728 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_DS
].selector
);
729 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_ES
].selector
);
731 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_SS
].selector
);
732 PUSHL(ssp
, esp
, sp_mask
, ESP
);
734 PUSHL(ssp
, esp
, sp_mask
, compute_eflags());
735 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_CS
].selector
);
736 PUSHL(ssp
, esp
, sp_mask
, old_eip
);
737 if (has_error_code
) {
738 PUSHL(ssp
, esp
, sp_mask
, error_code
);
742 if (env
->eflags
& VM_MASK
) {
743 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_GS
].selector
);
744 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_FS
].selector
);
745 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_DS
].selector
);
746 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_ES
].selector
);
748 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_SS
].selector
);
749 PUSHW(ssp
, esp
, sp_mask
, ESP
);
751 PUSHW(ssp
, esp
, sp_mask
, compute_eflags());
752 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_CS
].selector
);
753 PUSHW(ssp
, esp
, sp_mask
, old_eip
);
754 if (has_error_code
) {
755 PUSHW(ssp
, esp
, sp_mask
, error_code
);
760 if (env
->eflags
& VM_MASK
) {
761 /* XXX: explain me why W2K hangs if the whole segment cache is
764 env
->segs
[R_ES
].selector
= 0;
765 env
->segs
[R_ES
].flags
= 0;
766 env
->segs
[R_DS
].selector
= 0;
767 env
->segs
[R_DS
].flags
= 0;
768 env
->segs
[R_FS
].selector
= 0;
769 env
->segs
[R_FS
].flags
= 0;
770 env
->segs
[R_GS
].selector
= 0;
771 env
->segs
[R_GS
].flags
= 0;
773 cpu_x86_load_seg_cache(env
, R_ES
, 0, NULL
, 0, 0);
774 cpu_x86_load_seg_cache(env
, R_DS
, 0, NULL
, 0, 0);
775 cpu_x86_load_seg_cache(env
, R_FS
, 0, NULL
, 0, 0);
776 cpu_x86_load_seg_cache(env
, R_GS
, 0, NULL
, 0, 0);
779 ss
= (ss
& ~3) | dpl
;
780 cpu_x86_load_seg_cache(env
, R_SS
, ss
,
781 ssp
, get_seg_limit(ss_e1
, ss_e2
), ss_e2
);
783 ESP
= (ESP
& ~sp_mask
) | (esp
& sp_mask
);
785 selector
= (selector
& ~3) | dpl
;
786 cpu_x86_load_seg_cache(env
, R_CS
, selector
,
787 get_seg_base(e1
, e2
),
788 get_seg_limit(e1
, e2
),
790 cpu_x86_set_cpl(env
, dpl
);
793 /* interrupt gate clear IF mask */
794 if ((type
& 1) == 0) {
795 env
->eflags
&= ~IF_MASK
;
797 env
->eflags
&= ~(TF_MASK
| VM_MASK
| RF_MASK
| NT_MASK
);
800 /* real mode interrupt */
801 static void do_interrupt_real(int intno
, int is_int
, int error_code
,
802 unsigned int next_eip
)
807 uint32_t offset
, esp
;
808 uint32_t old_cs
, old_eip
;
810 /* real mode (simpler !) */
812 if (intno
* 4 + 3 > dt
->limit
)
813 raise_exception_err(EXCP0D_GPF
, intno
* 8 + 2);
814 ptr
= dt
->base
+ intno
* 4;
815 offset
= lduw_kernel(ptr
);
816 selector
= lduw_kernel(ptr
+ 2);
818 ssp
= env
->segs
[R_SS
].base
;
823 old_cs
= env
->segs
[R_CS
].selector
;
824 /* XXX: use SS segment size ? */
825 PUSHW(ssp
, esp
, 0xffff, compute_eflags());
826 PUSHW(ssp
, esp
, 0xffff, old_cs
);
827 PUSHW(ssp
, esp
, 0xffff, old_eip
);
829 /* update processor state */
830 ESP
= (ESP
& ~0xffff) | (esp
& 0xffff);
832 env
->segs
[R_CS
].selector
= selector
;
833 env
->segs
[R_CS
].base
= (uint8_t *)(selector
<< 4);
834 env
->eflags
&= ~(IF_MASK
| TF_MASK
| AC_MASK
| RF_MASK
);
837 /* fake user mode interrupt */
838 void do_interrupt_user(int intno
, int is_int
, int error_code
,
839 unsigned int next_eip
)
847 ptr
= dt
->base
+ (intno
* 8);
848 e2
= ldl_kernel(ptr
+ 4);
850 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
851 cpl
= env
->hflags
& HF_CPL_MASK
;
852 /* check privledge if software int */
853 if (is_int
&& dpl
< cpl
)
854 raise_exception_err(EXCP0D_GPF
, intno
* 8 + 2);
856 /* Since we emulate only user space, we cannot do more than
857 exiting the emulation with the suitable exception and error
864 * Begin execution of an interruption. is_int is TRUE if coming from
865 * the int instruction. next_eip is the EIP value AFTER the interrupt
866 * instruction. It is only relevant if is_int is TRUE.
868 void do_interrupt(int intno
, int is_int
, int error_code
,
869 unsigned int next_eip
, int is_hw
)
872 if (loglevel
& (CPU_LOG_PCALL
| CPU_LOG_INT
)) {
873 if ((env
->cr
[0] & CR0_PE_MASK
)) {
875 fprintf(logfile
, "%6d: v=%02x e=%04x i=%d cpl=%d IP=%04x:%08x SP=%04x:%08x",
876 count
, intno
, error_code
, is_int
,
877 env
->hflags
& HF_CPL_MASK
,
878 env
->segs
[R_CS
].selector
, EIP
,
879 env
->segs
[R_SS
].selector
, ESP
);
881 fprintf(logfile
, " CR2=%08x", env
->cr
[2]);
883 fprintf(logfile
, " EAX=%08x", env
->regs
[R_EAX
]);
885 fprintf(logfile
, "\n");
887 cpu_x86_dump_state(env
, logfile
, X86_DUMP_CCOP
);
891 fprintf(logfile
, " code=");
892 ptr
= env
->segs
[R_CS
].base
+ env
->eip
;
893 for(i
= 0; i
< 16; i
++) {
894 fprintf(logfile
, " %02x", ldub(ptr
+ i
));
896 fprintf(logfile
, "\n");
903 if (env
->cr
[0] & CR0_PE_MASK
) {
904 do_interrupt_protected(intno
, is_int
, error_code
, next_eip
, is_hw
);
906 do_interrupt_real(intno
, is_int
, error_code
, next_eip
);
911 * Signal an interruption. It is executed in the main CPU loop.
912 * is_int is TRUE if coming from the int instruction. next_eip is the
913 * EIP value AFTER the interrupt instruction. It is only relevant if
916 void raise_interrupt(int intno
, int is_int
, int error_code
,
917 unsigned int next_eip
)
919 env
->exception_index
= intno
;
920 env
->error_code
= error_code
;
921 env
->exception_is_int
= is_int
;
922 env
->exception_next_eip
= next_eip
;
926 /* shortcuts to generate exceptions */
928 void (raise_exception_err
)(int exception_index
, int error_code
)
930 raise_interrupt(exception_index
, 0, error_code
, 0);
933 void raise_exception(int exception_index
)
935 raise_interrupt(exception_index
, 0, 0, 0);
938 #ifdef BUGGY_GCC_DIV64
939 /* gcc 2.95.4 on PowerPC does not seem to like using __udivdi3, so we
940 call it from another function */
941 uint32_t div64(uint32_t *q_ptr
, uint64_t num
, uint32_t den
)
947 int32_t idiv64(int32_t *q_ptr
, int64_t num
, int32_t den
)
954 void helper_divl_EAX_T0(uint32_t eip
)
956 unsigned int den
, q
, r
;
959 num
= EAX
| ((uint64_t)EDX
<< 32);
963 raise_exception(EXCP00_DIVZ
);
965 #ifdef BUGGY_GCC_DIV64
966 r
= div64(&q
, num
, den
);
975 void helper_idivl_EAX_T0(uint32_t eip
)
980 num
= EAX
| ((uint64_t)EDX
<< 32);
984 raise_exception(EXCP00_DIVZ
);
986 #ifdef BUGGY_GCC_DIV64
987 r
= idiv64(&q
, num
, den
);
996 void helper_cmpxchg8b(void)
1001 eflags
= cc_table
[CC_OP
].compute_all();
1002 d
= ldq((uint8_t *)A0
);
1003 if (d
== (((uint64_t)EDX
<< 32) | EAX
)) {
1004 stq((uint8_t *)A0
, ((uint64_t)ECX
<< 32) | EBX
);
1014 #define CPUID_FP87 (1 << 0)
1015 #define CPUID_VME (1 << 1)
1016 #define CPUID_DE (1 << 2)
1017 #define CPUID_PSE (1 << 3)
1018 #define CPUID_TSC (1 << 4)
1019 #define CPUID_MSR (1 << 5)
1020 #define CPUID_PAE (1 << 6)
1021 #define CPUID_MCE (1 << 7)
1022 #define CPUID_CX8 (1 << 8)
1023 #define CPUID_APIC (1 << 9)
1024 #define CPUID_SEP (1 << 11) /* sysenter/sysexit */
1025 #define CPUID_MTRR (1 << 12)
1026 #define CPUID_PGE (1 << 13)
1027 #define CPUID_MCA (1 << 14)
1028 #define CPUID_CMOV (1 << 15)
1030 #define CPUID_MMX (1 << 23)
1031 #define CPUID_FXSR (1 << 24)
1032 #define CPUID_SSE (1 << 25)
1033 #define CPUID_SSE2 (1 << 26)
1035 void helper_cpuid(void)
1039 EAX
= 2; /* max EAX index supported */
1046 int family
, model
, stepping
;
1049 /* pentium 75-200 */
1059 EAX
= (family
<< 8) | (model
<< 4) | stepping
;
1062 EDX
= CPUID_FP87
| CPUID_DE
| CPUID_PSE
|
1063 CPUID_TSC
| CPUID_MSR
| CPUID_MCE
|
1064 CPUID_CX8
| CPUID_PGE
| CPUID_CMOV
;
1068 /* cache info: needed for Pentium Pro compatibility */
1077 void helper_lldt_T0(void)
1085 selector
= T0
& 0xffff;
1086 if ((selector
& 0xfffc) == 0) {
1087 /* XXX: NULL selector case: invalid LDT */
1088 env
->ldt
.base
= NULL
;
1092 raise_exception_err(EXCP0D_GPF
, selector
& 0xfffc);
1094 index
= selector
& ~7;
1095 if ((index
+ 7) > dt
->limit
)
1096 raise_exception_err(EXCP0D_GPF
, selector
& 0xfffc);
1097 ptr
= dt
->base
+ index
;
1098 e1
= ldl_kernel(ptr
);
1099 e2
= ldl_kernel(ptr
+ 4);
1100 if ((e2
& DESC_S_MASK
) || ((e2
>> DESC_TYPE_SHIFT
) & 0xf) != 2)
1101 raise_exception_err(EXCP0D_GPF
, selector
& 0xfffc);
1102 if (!(e2
& DESC_P_MASK
))
1103 raise_exception_err(EXCP0B_NOSEG
, selector
& 0xfffc);
1104 load_seg_cache_raw_dt(&env
->ldt
, e1
, e2
);
1106 env
->ldt
.selector
= selector
;
1109 void helper_ltr_T0(void)
1117 selector
= T0
& 0xffff;
1118 if ((selector
& 0xfffc) == 0) {
1119 /* NULL selector case: invalid LDT */
1120 env
->tr
.base
= NULL
;
1125 raise_exception_err(EXCP0D_GPF
, selector
& 0xfffc);
1127 index
= selector
& ~7;
1128 if ((index
+ 7) > dt
->limit
)
1129 raise_exception_err(EXCP0D_GPF
, selector
& 0xfffc);
1130 ptr
= dt
->base
+ index
;
1131 e1
= ldl_kernel(ptr
);
1132 e2
= ldl_kernel(ptr
+ 4);
1133 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
1134 if ((e2
& DESC_S_MASK
) ||
1135 (type
!= 1 && type
!= 9))
1136 raise_exception_err(EXCP0D_GPF
, selector
& 0xfffc);
1137 if (!(e2
& DESC_P_MASK
))
1138 raise_exception_err(EXCP0B_NOSEG
, selector
& 0xfffc);
1139 load_seg_cache_raw_dt(&env
->tr
, e1
, e2
);
1140 e2
|= DESC_TSS_BUSY_MASK
;
1141 stl_kernel(ptr
+ 4, e2
);
1143 env
->tr
.selector
= selector
;
1146 /* only works if protected mode and not VM86. seg_reg must be != R_CS */
1147 void load_seg(int seg_reg
, int selector
)
1156 if ((selector
& 0xfffc) == 0) {
1157 /* null selector case */
1158 if (seg_reg
== R_SS
)
1159 raise_exception_err(EXCP0D_GPF
, 0);
1160 cpu_x86_load_seg_cache(env
, seg_reg
, selector
, NULL
, 0, 0);
1167 index
= selector
& ~7;
1168 if ((index
+ 7) > dt
->limit
)
1169 raise_exception_err(EXCP0D_GPF
, selector
& 0xfffc);
1170 ptr
= dt
->base
+ index
;
1171 e1
= ldl_kernel(ptr
);
1172 e2
= ldl_kernel(ptr
+ 4);
1174 if (!(e2
& DESC_S_MASK
))
1175 raise_exception_err(EXCP0D_GPF
, selector
& 0xfffc);
1177 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1178 cpl
= env
->hflags
& HF_CPL_MASK
;
1179 if (seg_reg
== R_SS
) {
1180 /* must be writable segment */
1181 if ((e2
& DESC_CS_MASK
) || !(e2
& DESC_W_MASK
))
1182 raise_exception_err(EXCP0D_GPF
, selector
& 0xfffc);
1183 if (rpl
!= cpl
|| dpl
!= cpl
)
1184 raise_exception_err(EXCP0D_GPF
, selector
& 0xfffc);
1186 /* must be readable segment */
1187 if ((e2
& (DESC_CS_MASK
| DESC_R_MASK
)) == DESC_CS_MASK
)
1188 raise_exception_err(EXCP0D_GPF
, selector
& 0xfffc);
1190 if (!(e2
& DESC_CS_MASK
) || !(e2
& DESC_C_MASK
)) {
1191 /* if not conforming code, test rights */
1192 if (dpl
< cpl
|| dpl
< rpl
)
1193 raise_exception_err(EXCP0D_GPF
, selector
& 0xfffc);
1197 if (!(e2
& DESC_P_MASK
)) {
1198 if (seg_reg
== R_SS
)
1199 raise_exception_err(EXCP0C_STACK
, selector
& 0xfffc);
1201 raise_exception_err(EXCP0B_NOSEG
, selector
& 0xfffc);
1204 /* set the access bit if not already set */
1205 if (!(e2
& DESC_A_MASK
)) {
1207 stl_kernel(ptr
+ 4, e2
);
1210 cpu_x86_load_seg_cache(env
, seg_reg
, selector
,
1211 get_seg_base(e1
, e2
),
1212 get_seg_limit(e1
, e2
),
1215 fprintf(logfile
, "load_seg: sel=0x%04x base=0x%08lx limit=0x%08lx flags=%08x\n",
1216 selector
, (unsigned long)sc
->base
, sc
->limit
, sc
->flags
);
1221 /* protected mode jump */
1222 void helper_ljmp_protected_T0_T1(void)
1224 int new_cs
, new_eip
, gate_cs
, type
;
1225 uint32_t e1
, e2
, cpl
, dpl
, rpl
, limit
;
1229 if ((new_cs
& 0xfffc) == 0)
1230 raise_exception_err(EXCP0D_GPF
, 0);
1231 if (load_segment(&e1
, &e2
, new_cs
) != 0)
1232 raise_exception_err(EXCP0D_GPF
, new_cs
& 0xfffc);
1233 cpl
= env
->hflags
& HF_CPL_MASK
;
1234 if (e2
& DESC_S_MASK
) {
1235 if (!(e2
& DESC_CS_MASK
))
1236 raise_exception_err(EXCP0D_GPF
, new_cs
& 0xfffc);
1237 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1238 if (e2
& DESC_C_MASK
) {
1239 /* conforming code segment */
1241 raise_exception_err(EXCP0D_GPF
, new_cs
& 0xfffc);
1243 /* non conforming code segment */
1246 raise_exception_err(EXCP0D_GPF
, new_cs
& 0xfffc);
1248 raise_exception_err(EXCP0D_GPF
, new_cs
& 0xfffc);
1250 if (!(e2
& DESC_P_MASK
))
1251 raise_exception_err(EXCP0B_NOSEG
, new_cs
& 0xfffc);
1252 limit
= get_seg_limit(e1
, e2
);
1253 if (new_eip
> limit
)
1254 raise_exception_err(EXCP0D_GPF
, new_cs
& 0xfffc);
1255 cpu_x86_load_seg_cache(env
, R_CS
, (new_cs
& 0xfffc) | cpl
,
1256 get_seg_base(e1
, e2
), limit
, e2
);
1259 /* jump to call or task gate */
1260 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1262 cpl
= env
->hflags
& HF_CPL_MASK
;
1263 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
1265 case 1: /* 286 TSS */
1266 case 9: /* 386 TSS */
1267 case 5: /* task gate */
1268 if (dpl
< cpl
|| dpl
< rpl
)
1269 raise_exception_err(EXCP0D_GPF
, new_cs
& 0xfffc);
1270 /* XXX: check if it is really the current EIP */
1271 switch_tss(new_cs
, e1
, e2
, SWITCH_TSS_JMP
, env
->eip
);
1273 case 4: /* 286 call gate */
1274 case 12: /* 386 call gate */
1275 if ((dpl
< cpl
) || (dpl
< rpl
))
1276 raise_exception_err(EXCP0D_GPF
, new_cs
& 0xfffc);
1277 if (!(e2
& DESC_P_MASK
))
1278 raise_exception_err(EXCP0B_NOSEG
, new_cs
& 0xfffc);
1280 if (load_segment(&e1
, &e2
, gate_cs
) != 0)
1281 raise_exception_err(EXCP0D_GPF
, gate_cs
& 0xfffc);
1282 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1283 /* must be code segment */
1284 if (((e2
& (DESC_S_MASK
| DESC_CS_MASK
)) !=
1285 (DESC_S_MASK
| DESC_CS_MASK
)))
1286 raise_exception_err(EXCP0D_GPF
, gate_cs
& 0xfffc);
1287 if (((e2
& DESC_C_MASK
) && (dpl
> cpl
)) ||
1288 (!(e2
& DESC_C_MASK
) && (dpl
!= cpl
)))
1289 raise_exception_err(EXCP0D_GPF
, gate_cs
& 0xfffc);
1290 if (!(e2
& DESC_P_MASK
))
1291 raise_exception_err(EXCP0D_GPF
, gate_cs
& 0xfffc);
1292 new_eip
= (e1
& 0xffff);
1294 new_eip
|= (e2
& 0xffff0000);
1295 limit
= get_seg_limit(e1
, e2
);
1296 if (new_eip
> limit
)
1297 raise_exception_err(EXCP0D_GPF
, 0);
1298 cpu_x86_load_seg_cache(env
, R_CS
, (gate_cs
& 0xfffc) | cpl
,
1299 get_seg_base(e1
, e2
), limit
, e2
);
1303 raise_exception_err(EXCP0D_GPF
, new_cs
& 0xfffc);
1309 /* real mode call */
1310 void helper_lcall_real_T0_T1(int shift
, int next_eip
)
1312 int new_cs
, new_eip
;
1313 uint32_t esp
, esp_mask
;
1319 esp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1320 ssp
= env
->segs
[R_SS
].base
;
1322 PUSHL(ssp
, esp
, esp_mask
, env
->segs
[R_CS
].selector
);
1323 PUSHL(ssp
, esp
, esp_mask
, next_eip
);
1325 PUSHW(ssp
, esp
, esp_mask
, env
->segs
[R_CS
].selector
);
1326 PUSHW(ssp
, esp
, esp_mask
, next_eip
);
1329 ESP
= (ESP
& ~esp_mask
) | (esp
& esp_mask
);
1331 env
->segs
[R_CS
].selector
= new_cs
;
1332 env
->segs
[R_CS
].base
= (uint8_t *)(new_cs
<< 4);
1335 /* protected mode call */
1336 void helper_lcall_protected_T0_T1(int shift
, int next_eip
)
1338 int new_cs
, new_eip
, new_stack
, i
;
1339 uint32_t e1
, e2
, cpl
, dpl
, rpl
, selector
, offset
, param_count
;
1340 uint32_t ss
, ss_e1
, ss_e2
, sp
, type
, ss_dpl
, sp_mask
;
1341 uint32_t val
, limit
, old_sp_mask
;
1342 uint8_t *ssp
, *old_ssp
;
1347 if (loglevel
& CPU_LOG_PCALL
) {
1348 fprintf(logfile
, "lcall %04x:%08x s=%d\n",
1349 new_cs
, new_eip
, shift
);
1350 cpu_x86_dump_state(env
, logfile
, X86_DUMP_CCOP
);
1353 if ((new_cs
& 0xfffc) == 0)
1354 raise_exception_err(EXCP0D_GPF
, 0);
1355 if (load_segment(&e1
, &e2
, new_cs
) != 0)
1356 raise_exception_err(EXCP0D_GPF
, new_cs
& 0xfffc);
1357 cpl
= env
->hflags
& HF_CPL_MASK
;
1359 if (loglevel
& CPU_LOG_PCALL
) {
1360 fprintf(logfile
, "desc=%08x:%08x\n", e1
, e2
);
1363 if (e2
& DESC_S_MASK
) {
1364 if (!(e2
& DESC_CS_MASK
))
1365 raise_exception_err(EXCP0D_GPF
, new_cs
& 0xfffc);
1366 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1367 if (e2
& DESC_C_MASK
) {
1368 /* conforming code segment */
1370 raise_exception_err(EXCP0D_GPF
, new_cs
& 0xfffc);
1372 /* non conforming code segment */
1375 raise_exception_err(EXCP0D_GPF
, new_cs
& 0xfffc);
1377 raise_exception_err(EXCP0D_GPF
, new_cs
& 0xfffc);
1379 if (!(e2
& DESC_P_MASK
))
1380 raise_exception_err(EXCP0B_NOSEG
, new_cs
& 0xfffc);
1383 sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1384 ssp
= env
->segs
[R_SS
].base
;
1386 PUSHL(ssp
, sp
, sp_mask
, env
->segs
[R_CS
].selector
);
1387 PUSHL(ssp
, sp
, sp_mask
, next_eip
);
1389 PUSHW(ssp
, sp
, sp_mask
, env
->segs
[R_CS
].selector
);
1390 PUSHW(ssp
, sp
, sp_mask
, next_eip
);
1393 limit
= get_seg_limit(e1
, e2
);
1394 if (new_eip
> limit
)
1395 raise_exception_err(EXCP0D_GPF
, new_cs
& 0xfffc);
1396 /* from this point, not restartable */
1397 ESP
= (ESP
& ~sp_mask
) | (sp
& sp_mask
);
1398 cpu_x86_load_seg_cache(env
, R_CS
, (new_cs
& 0xfffc) | cpl
,
1399 get_seg_base(e1
, e2
), limit
, e2
);
1402 /* check gate type */
1403 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x1f;
1404 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1407 case 1: /* available 286 TSS */
1408 case 9: /* available 386 TSS */
1409 case 5: /* task gate */
1410 if (dpl
< cpl
|| dpl
< rpl
)
1411 raise_exception_err(EXCP0D_GPF
, new_cs
& 0xfffc);
1412 switch_tss(new_cs
, e1
, e2
, SWITCH_TSS_CALL
, next_eip
);
1414 case 4: /* 286 call gate */
1415 case 12: /* 386 call gate */
1418 raise_exception_err(EXCP0D_GPF
, new_cs
& 0xfffc);
1423 if (dpl
< cpl
|| dpl
< rpl
)
1424 raise_exception_err(EXCP0D_GPF
, new_cs
& 0xfffc);
1425 /* check valid bit */
1426 if (!(e2
& DESC_P_MASK
))
1427 raise_exception_err(EXCP0B_NOSEG
, new_cs
& 0xfffc);
1428 selector
= e1
>> 16;
1429 offset
= (e2
& 0xffff0000) | (e1
& 0x0000ffff);
1430 param_count
= e2
& 0x1f;
1431 if ((selector
& 0xfffc) == 0)
1432 raise_exception_err(EXCP0D_GPF
, 0);
1434 if (load_segment(&e1
, &e2
, selector
) != 0)
1435 raise_exception_err(EXCP0D_GPF
, selector
& 0xfffc);
1436 if (!(e2
& DESC_S_MASK
) || !(e2
& (DESC_CS_MASK
)))
1437 raise_exception_err(EXCP0D_GPF
, selector
& 0xfffc);
1438 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1440 raise_exception_err(EXCP0D_GPF
, selector
& 0xfffc);
1441 if (!(e2
& DESC_P_MASK
))
1442 raise_exception_err(EXCP0B_NOSEG
, selector
& 0xfffc);
1444 if (!(e2
& DESC_C_MASK
) && dpl
< cpl
) {
1445 /* to inner priviledge */
1446 get_ss_esp_from_tss(&ss
, &sp
, dpl
);
1448 if (loglevel
& CPU_LOG_PCALL
)
1449 fprintf(logfile
, "new ss:esp=%04x:%08x param_count=%d ESP=%x\n",
1450 ss
, sp
, param_count
, ESP
);
1452 if ((ss
& 0xfffc) == 0)
1453 raise_exception_err(EXCP0A_TSS
, ss
& 0xfffc);
1454 if ((ss
& 3) != dpl
)
1455 raise_exception_err(EXCP0A_TSS
, ss
& 0xfffc);
1456 if (load_segment(&ss_e1
, &ss_e2
, ss
) != 0)
1457 raise_exception_err(EXCP0A_TSS
, ss
& 0xfffc);
1458 ss_dpl
= (ss_e2
>> DESC_DPL_SHIFT
) & 3;
1460 raise_exception_err(EXCP0A_TSS
, ss
& 0xfffc);
1461 if (!(ss_e2
& DESC_S_MASK
) ||
1462 (ss_e2
& DESC_CS_MASK
) ||
1463 !(ss_e2
& DESC_W_MASK
))
1464 raise_exception_err(EXCP0A_TSS
, ss
& 0xfffc);
1465 if (!(ss_e2
& DESC_P_MASK
))
1466 raise_exception_err(EXCP0A_TSS
, ss
& 0xfffc);
1468 // push_size = ((param_count * 2) + 8) << shift;
1470 old_sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1471 old_ssp
= env
->segs
[R_SS
].base
;
1473 sp_mask
= get_sp_mask(ss_e2
);
1474 ssp
= get_seg_base(ss_e1
, ss_e2
);
1476 PUSHL(ssp
, sp
, sp_mask
, env
->segs
[R_SS
].selector
);
1477 PUSHL(ssp
, sp
, sp_mask
, ESP
);
1478 for(i
= param_count
- 1; i
>= 0; i
--) {
1479 val
= ldl_kernel(old_ssp
+ ((ESP
+ i
* 4) & old_sp_mask
));
1480 PUSHL(ssp
, sp
, sp_mask
, val
);
1483 PUSHW(ssp
, sp
, sp_mask
, env
->segs
[R_SS
].selector
);
1484 PUSHW(ssp
, sp
, sp_mask
, ESP
);
1485 for(i
= param_count
- 1; i
>= 0; i
--) {
1486 val
= lduw_kernel(old_ssp
+ ((ESP
+ i
* 2) & old_sp_mask
));
1487 PUSHW(ssp
, sp
, sp_mask
, val
);
1492 /* to same priviledge */
1494 sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1495 ssp
= env
->segs
[R_SS
].base
;
1496 // push_size = (4 << shift);
1501 PUSHL(ssp
, sp
, sp_mask
, env
->segs
[R_CS
].selector
);
1502 PUSHL(ssp
, sp
, sp_mask
, next_eip
);
1504 PUSHW(ssp
, sp
, sp_mask
, env
->segs
[R_CS
].selector
);
1505 PUSHW(ssp
, sp
, sp_mask
, next_eip
);
1508 /* from this point, not restartable */
1511 ss
= (ss
& ~3) | dpl
;
1512 cpu_x86_load_seg_cache(env
, R_SS
, ss
,
1514 get_seg_limit(ss_e1
, ss_e2
),
1518 selector
= (selector
& ~3) | dpl
;
1519 cpu_x86_load_seg_cache(env
, R_CS
, selector
,
1520 get_seg_base(e1
, e2
),
1521 get_seg_limit(e1
, e2
),
1523 cpu_x86_set_cpl(env
, dpl
);
1524 ESP
= (ESP
& ~sp_mask
) | (sp
& sp_mask
);
1529 /* real and vm86 mode iret */
1530 void helper_iret_real(int shift
)
1532 uint32_t sp
, new_cs
, new_eip
, new_eflags
, sp_mask
;
1536 sp_mask
= 0xffff; /* XXXX: use SS segment size ? */
1538 ssp
= env
->segs
[R_SS
].base
;
1541 POPL(ssp
, sp
, sp_mask
, new_eip
);
1542 POPL(ssp
, sp
, sp_mask
, new_cs
);
1544 POPL(ssp
, sp
, sp_mask
, new_eflags
);
1547 POPW(ssp
, sp
, sp_mask
, new_eip
);
1548 POPW(ssp
, sp
, sp_mask
, new_cs
);
1549 POPW(ssp
, sp
, sp_mask
, new_eflags
);
1551 ESP
= (ESP
& ~sp_mask
) | (sp
& sp_mask
);
1552 load_seg_vm(R_CS
, new_cs
);
1554 if (env
->eflags
& VM_MASK
)
1555 eflags_mask
= TF_MASK
| AC_MASK
| ID_MASK
| IF_MASK
| RF_MASK
| NT_MASK
;
1557 eflags_mask
= TF_MASK
| AC_MASK
| ID_MASK
| IF_MASK
| IOPL_MASK
| RF_MASK
| NT_MASK
;
1559 eflags_mask
&= 0xffff;
1560 load_eflags(new_eflags
, eflags_mask
);
1563 static inline void validate_seg(int seg_reg
, int cpl
)
1568 e2
= env
->segs
[seg_reg
].flags
;
1569 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1570 if (!(e2
& DESC_CS_MASK
) || !(e2
& DESC_C_MASK
)) {
1571 /* data or non conforming code segment */
1573 cpu_x86_load_seg_cache(env
, seg_reg
, 0, NULL
, 0, 0);
1578 /* protected mode iret */
1579 static inline void helper_ret_protected(int shift
, int is_iret
, int addend
)
1581 uint32_t sp
, new_cs
, new_eip
, new_eflags
, new_esp
, new_ss
, sp_mask
;
1582 uint32_t new_es
, new_ds
, new_fs
, new_gs
;
1583 uint32_t e1
, e2
, ss_e1
, ss_e2
;
1584 int cpl
, dpl
, rpl
, eflags_mask
, iopl
;
1587 sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1589 ssp
= env
->segs
[R_SS
].base
;
1592 POPL(ssp
, sp
, sp_mask
, new_eip
);
1593 POPL(ssp
, sp
, sp_mask
, new_cs
);
1596 POPL(ssp
, sp
, sp_mask
, new_eflags
);
1597 if (new_eflags
& VM_MASK
)
1598 goto return_to_vm86
;
1602 POPW(ssp
, sp
, sp_mask
, new_eip
);
1603 POPW(ssp
, sp
, sp_mask
, new_cs
);
1605 POPW(ssp
, sp
, sp_mask
, new_eflags
);
1608 if (loglevel
& CPU_LOG_PCALL
) {
1609 fprintf(logfile
, "lret new %04x:%08x s=%d addend=0x%x\n",
1610 new_cs
, new_eip
, shift
, addend
);
1611 cpu_x86_dump_state(env
, logfile
, X86_DUMP_CCOP
);
1614 if ((new_cs
& 0xfffc) == 0)
1615 raise_exception_err(EXCP0D_GPF
, new_cs
& 0xfffc);
1616 if (load_segment(&e1
, &e2
, new_cs
) != 0)
1617 raise_exception_err(EXCP0D_GPF
, new_cs
& 0xfffc);
1618 if (!(e2
& DESC_S_MASK
) ||
1619 !(e2
& DESC_CS_MASK
))
1620 raise_exception_err(EXCP0D_GPF
, new_cs
& 0xfffc);
1621 cpl
= env
->hflags
& HF_CPL_MASK
;
1624 raise_exception_err(EXCP0D_GPF
, new_cs
& 0xfffc);
1625 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1626 if (e2
& DESC_C_MASK
) {
1628 raise_exception_err(EXCP0D_GPF
, new_cs
& 0xfffc);
1631 raise_exception_err(EXCP0D_GPF
, new_cs
& 0xfffc);
1633 if (!(e2
& DESC_P_MASK
))
1634 raise_exception_err(EXCP0B_NOSEG
, new_cs
& 0xfffc);
1638 /* return to same priledge level */
1639 cpu_x86_load_seg_cache(env
, R_CS
, new_cs
,
1640 get_seg_base(e1
, e2
),
1641 get_seg_limit(e1
, e2
),
1644 /* return to different priviledge level */
1647 POPL(ssp
, sp
, sp_mask
, new_esp
);
1648 POPL(ssp
, sp
, sp_mask
, new_ss
);
1652 POPW(ssp
, sp
, sp_mask
, new_esp
);
1653 POPW(ssp
, sp
, sp_mask
, new_ss
);
1656 if (loglevel
& CPU_LOG_PCALL
) {
1657 fprintf(logfile
, "new ss:esp=%04x:%08x\n",
1662 if ((new_ss
& 3) != rpl
)
1663 raise_exception_err(EXCP0D_GPF
, new_ss
& 0xfffc);
1664 if (load_segment(&ss_e1
, &ss_e2
, new_ss
) != 0)
1665 raise_exception_err(EXCP0D_GPF
, new_ss
& 0xfffc);
1666 if (!(ss_e2
& DESC_S_MASK
) ||
1667 (ss_e2
& DESC_CS_MASK
) ||
1668 !(ss_e2
& DESC_W_MASK
))
1669 raise_exception_err(EXCP0D_GPF
, new_ss
& 0xfffc);
1670 dpl
= (ss_e2
>> DESC_DPL_SHIFT
) & 3;
1672 raise_exception_err(EXCP0D_GPF
, new_ss
& 0xfffc);
1673 if (!(ss_e2
& DESC_P_MASK
))
1674 raise_exception_err(EXCP0B_NOSEG
, new_ss
& 0xfffc);
1676 cpu_x86_load_seg_cache(env
, R_CS
, new_cs
,
1677 get_seg_base(e1
, e2
),
1678 get_seg_limit(e1
, e2
),
1680 cpu_x86_load_seg_cache(env
, R_SS
, new_ss
,
1681 get_seg_base(ss_e1
, ss_e2
),
1682 get_seg_limit(ss_e1
, ss_e2
),
1684 cpu_x86_set_cpl(env
, rpl
);
1686 sp_mask
= get_sp_mask(ss_e2
);
1688 /* validate data segments */
1689 validate_seg(R_ES
, cpl
);
1690 validate_seg(R_DS
, cpl
);
1691 validate_seg(R_FS
, cpl
);
1692 validate_seg(R_GS
, cpl
);
1696 ESP
= (ESP
& ~sp_mask
) | (sp
& sp_mask
);
1699 /* NOTE: 'cpl' is the _old_ CPL */
1700 eflags_mask
= TF_MASK
| AC_MASK
| ID_MASK
| RF_MASK
| NT_MASK
;
1702 eflags_mask
|= IOPL_MASK
;
1703 iopl
= (env
->eflags
>> IOPL_SHIFT
) & 3;
1705 eflags_mask
|= IF_MASK
;
1707 eflags_mask
&= 0xffff;
1708 load_eflags(new_eflags
, eflags_mask
);
1713 POPL(ssp
, sp
, sp_mask
, new_esp
);
1714 POPL(ssp
, sp
, sp_mask
, new_ss
);
1715 POPL(ssp
, sp
, sp_mask
, new_es
);
1716 POPL(ssp
, sp
, sp_mask
, new_ds
);
1717 POPL(ssp
, sp
, sp_mask
, new_fs
);
1718 POPL(ssp
, sp
, sp_mask
, new_gs
);
1720 /* modify processor state */
1721 load_eflags(new_eflags
, TF_MASK
| AC_MASK
| ID_MASK
|
1722 IF_MASK
| IOPL_MASK
| VM_MASK
| NT_MASK
| VIF_MASK
| VIP_MASK
);
1723 load_seg_vm(R_CS
, new_cs
& 0xffff);
1724 cpu_x86_set_cpl(env
, 3);
1725 load_seg_vm(R_SS
, new_ss
& 0xffff);
1726 load_seg_vm(R_ES
, new_es
& 0xffff);
1727 load_seg_vm(R_DS
, new_ds
& 0xffff);
1728 load_seg_vm(R_FS
, new_fs
& 0xffff);
1729 load_seg_vm(R_GS
, new_gs
& 0xffff);
1735 void helper_iret_protected(int shift
)
1737 int tss_selector
, type
;
1740 /* specific case for TSS */
1741 if (env
->eflags
& NT_MASK
) {
1742 tss_selector
= lduw_kernel(env
->tr
.base
+ 0);
1743 if (tss_selector
& 4)
1744 raise_exception_err(EXCP0A_TSS
, tss_selector
& 0xfffc);
1745 if (load_segment(&e1
, &e2
, tss_selector
) != 0)
1746 raise_exception_err(EXCP0A_TSS
, tss_selector
& 0xfffc);
1747 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x17;
1748 /* NOTE: we check both segment and busy TSS */
1750 raise_exception_err(EXCP0A_TSS
, tss_selector
& 0xfffc);
1751 /* XXX: check if it is really the current EIP */
1752 switch_tss(tss_selector
, e1
, e2
, SWITCH_TSS_IRET
, env
->eip
);
1754 helper_ret_protected(shift
, 1, 0);
1758 void helper_lret_protected(int shift
, int addend
)
1760 helper_ret_protected(shift
, 0, addend
);
1763 void helper_movl_crN_T0(int reg
)
1767 cpu_x86_update_cr0(env
, T0
);
1770 cpu_x86_update_cr3(env
, T0
);
1773 cpu_x86_update_cr4(env
, T0
);
1782 void helper_movl_drN_T0(int reg
)
1787 void helper_invlpg(unsigned int addr
)
1789 cpu_x86_flush_tlb(env
, addr
);
1793 #if !defined(__i386__) && !defined(__x86_64__)
1797 void helper_rdtsc(void)
1800 #if defined(__i386__) || defined(__x86_64__)
1801 asm("rdtsc" : "=A" (val
));
1803 /* better than nothing: the time increases */
1810 void helper_wrmsr(void)
1813 case MSR_IA32_SYSENTER_CS
:
1814 env
->sysenter_cs
= EAX
& 0xffff;
1816 case MSR_IA32_SYSENTER_ESP
:
1817 env
->sysenter_esp
= EAX
;
1819 case MSR_IA32_SYSENTER_EIP
:
1820 env
->sysenter_eip
= EAX
;
1823 /* XXX: exception ? */
1828 void helper_rdmsr(void)
1831 case MSR_IA32_SYSENTER_CS
:
1832 EAX
= env
->sysenter_cs
;
1835 case MSR_IA32_SYSENTER_ESP
:
1836 EAX
= env
->sysenter_esp
;
1839 case MSR_IA32_SYSENTER_EIP
:
1840 EAX
= env
->sysenter_eip
;
1844 /* XXX: exception ? */
1849 void helper_lsl(void)
1851 unsigned int selector
, limit
;
1853 int rpl
, dpl
, cpl
, type
;
1855 CC_SRC
= cc_table
[CC_OP
].compute_all() & ~CC_Z
;
1856 selector
= T0
& 0xffff;
1857 if (load_segment(&e1
, &e2
, selector
) != 0)
1860 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1861 cpl
= env
->hflags
& HF_CPL_MASK
;
1862 if (e2
& DESC_S_MASK
) {
1863 if ((e2
& DESC_CS_MASK
) && (e2
& DESC_C_MASK
)) {
1866 if (dpl
< cpl
|| dpl
< rpl
)
1870 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
1881 if (dpl
< cpl
|| dpl
< rpl
)
1884 limit
= get_seg_limit(e1
, e2
);
1889 void helper_lar(void)
1891 unsigned int selector
;
1893 int rpl
, dpl
, cpl
, type
;
1895 CC_SRC
= cc_table
[CC_OP
].compute_all() & ~CC_Z
;
1896 selector
= T0
& 0xffff;
1897 if ((selector
& 0xfffc) == 0)
1899 if (load_segment(&e1
, &e2
, selector
) != 0)
1902 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1903 cpl
= env
->hflags
& HF_CPL_MASK
;
1904 if (e2
& DESC_S_MASK
) {
1905 if ((e2
& DESC_CS_MASK
) && (e2
& DESC_C_MASK
)) {
1908 if (dpl
< cpl
|| dpl
< rpl
)
1912 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
1926 if (dpl
< cpl
|| dpl
< rpl
)
1929 T1
= e2
& 0x00f0ff00;
1933 void helper_verr(void)
1935 unsigned int selector
;
1939 CC_SRC
= cc_table
[CC_OP
].compute_all() & ~CC_Z
;
1940 selector
= T0
& 0xffff;
1941 if ((selector
& 0xfffc) == 0)
1943 if (load_segment(&e1
, &e2
, selector
) != 0)
1945 if (!(e2
& DESC_S_MASK
))
1948 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1949 cpl
= env
->hflags
& HF_CPL_MASK
;
1950 if (e2
& DESC_CS_MASK
) {
1951 if (!(e2
& DESC_R_MASK
))
1953 if (!(e2
& DESC_C_MASK
)) {
1954 if (dpl
< cpl
|| dpl
< rpl
)
1958 if (dpl
< cpl
|| dpl
< rpl
)
1964 void helper_verw(void)
1966 unsigned int selector
;
1970 CC_SRC
= cc_table
[CC_OP
].compute_all() & ~CC_Z
;
1971 selector
= T0
& 0xffff;
1972 if ((selector
& 0xfffc) == 0)
1974 if (load_segment(&e1
, &e2
, selector
) != 0)
1976 if (!(e2
& DESC_S_MASK
))
1979 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1980 cpl
= env
->hflags
& HF_CPL_MASK
;
1981 if (e2
& DESC_CS_MASK
) {
1984 if (dpl
< cpl
|| dpl
< rpl
)
1986 if (!(e2
& DESC_W_MASK
))
1994 void helper_fldt_ST0_A0(void)
1997 new_fpstt
= (env
->fpstt
- 1) & 7;
1998 env
->fpregs
[new_fpstt
] = helper_fldt((uint8_t *)A0
);
1999 env
->fpstt
= new_fpstt
;
2000 env
->fptags
[new_fpstt
] = 0; /* validate stack entry */
2003 void helper_fstt_ST0_A0(void)
2005 helper_fstt(ST0
, (uint8_t *)A0
);
2010 #define MUL10(iv) ( iv + iv + (iv << 3) )
2012 void helper_fbld_ST0_A0(void)
2020 for(i
= 8; i
>= 0; i
--) {
2021 v
= ldub((uint8_t *)A0
+ i
);
2022 val
= (val
* 100) + ((v
>> 4) * 10) + (v
& 0xf);
2025 if (ldub((uint8_t *)A0
+ 9) & 0x80)
2031 void helper_fbst_ST0_A0(void)
2035 uint8_t *mem_ref
, *mem_end
;
2040 mem_ref
= (uint8_t *)A0
;
2041 mem_end
= mem_ref
+ 9;
2048 while (mem_ref
< mem_end
) {
2053 v
= ((v
/ 10) << 4) | (v
% 10);
2056 while (mem_ref
< mem_end
) {
2061 void helper_f2xm1(void)
2063 ST0
= pow(2.0,ST0
) - 1.0;
2066 void helper_fyl2x(void)
2068 CPU86_LDouble fptemp
;
2072 fptemp
= log(fptemp
)/log(2.0); /* log2(ST) */
2076 env
->fpus
&= (~0x4700);
2081 void helper_fptan(void)
2083 CPU86_LDouble fptemp
;
2086 if((fptemp
> MAXTAN
)||(fptemp
< -MAXTAN
)) {
2092 env
->fpus
&= (~0x400); /* C2 <-- 0 */
2093 /* the above code is for |arg| < 2**52 only */
2097 void helper_fpatan(void)
2099 CPU86_LDouble fptemp
, fpsrcop
;
2103 ST1
= atan2(fpsrcop
,fptemp
);
2107 void helper_fxtract(void)
2109 CPU86_LDoubleU temp
;
2110 unsigned int expdif
;
2113 expdif
= EXPD(temp
) - EXPBIAS
;
2114 /*DP exponent bias*/
2121 void helper_fprem1(void)
2123 CPU86_LDouble dblq
, fpsrcop
, fptemp
;
2124 CPU86_LDoubleU fpsrcop1
, fptemp1
;
2130 fpsrcop1
.d
= fpsrcop
;
2132 expdif
= EXPD(fpsrcop1
) - EXPD(fptemp1
);
2134 dblq
= fpsrcop
/ fptemp
;
2135 dblq
= (dblq
< 0.0)? ceil(dblq
): floor(dblq
);
2136 ST0
= fpsrcop
- fptemp
*dblq
;
2137 q
= (int)dblq
; /* cutting off top bits is assumed here */
2138 env
->fpus
&= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2139 /* (C0,C1,C3) <-- (q2,q1,q0) */
2140 env
->fpus
|= (q
&0x4) << 6; /* (C0) <-- q2 */
2141 env
->fpus
|= (q
&0x2) << 8; /* (C1) <-- q1 */
2142 env
->fpus
|= (q
&0x1) << 14; /* (C3) <-- q0 */
2144 env
->fpus
|= 0x400; /* C2 <-- 1 */
2145 fptemp
= pow(2.0, expdif
-50);
2146 fpsrcop
= (ST0
/ ST1
) / fptemp
;
2147 /* fpsrcop = integer obtained by rounding to the nearest */
2148 fpsrcop
= (fpsrcop
-floor(fpsrcop
) < ceil(fpsrcop
)-fpsrcop
)?
2149 floor(fpsrcop
): ceil(fpsrcop
);
2150 ST0
-= (ST1
* fpsrcop
* fptemp
);
2154 void helper_fprem(void)
2156 CPU86_LDouble dblq
, fpsrcop
, fptemp
;
2157 CPU86_LDoubleU fpsrcop1
, fptemp1
;
2163 fpsrcop1
.d
= fpsrcop
;
2165 expdif
= EXPD(fpsrcop1
) - EXPD(fptemp1
);
2166 if ( expdif
< 53 ) {
2167 dblq
= fpsrcop
/ fptemp
;
2168 dblq
= (dblq
< 0.0)? ceil(dblq
): floor(dblq
);
2169 ST0
= fpsrcop
- fptemp
*dblq
;
2170 q
= (int)dblq
; /* cutting off top bits is assumed here */
2171 env
->fpus
&= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2172 /* (C0,C1,C3) <-- (q2,q1,q0) */
2173 env
->fpus
|= (q
&0x4) << 6; /* (C0) <-- q2 */
2174 env
->fpus
|= (q
&0x2) << 8; /* (C1) <-- q1 */
2175 env
->fpus
|= (q
&0x1) << 14; /* (C3) <-- q0 */
2177 env
->fpus
|= 0x400; /* C2 <-- 1 */
2178 fptemp
= pow(2.0, expdif
-50);
2179 fpsrcop
= (ST0
/ ST1
) / fptemp
;
2180 /* fpsrcop = integer obtained by chopping */
2181 fpsrcop
= (fpsrcop
< 0.0)?
2182 -(floor(fabs(fpsrcop
))): floor(fpsrcop
);
2183 ST0
-= (ST1
* fpsrcop
* fptemp
);
2187 void helper_fyl2xp1(void)
2189 CPU86_LDouble fptemp
;
2192 if ((fptemp
+1.0)>0.0) {
2193 fptemp
= log(fptemp
+1.0) / log(2.0); /* log2(ST+1.0) */
2197 env
->fpus
&= (~0x4700);
2202 void helper_fsqrt(void)
2204 CPU86_LDouble fptemp
;
2208 env
->fpus
&= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2214 void helper_fsincos(void)
2216 CPU86_LDouble fptemp
;
2219 if ((fptemp
> MAXTAN
)||(fptemp
< -MAXTAN
)) {
2225 env
->fpus
&= (~0x400); /* C2 <-- 0 */
2226 /* the above code is for |arg| < 2**63 only */
2230 void helper_frndint(void)
2236 switch(env
->fpuc
& RC_MASK
) {
2239 asm("rndd %0, %1" : "=f" (a
) : "f"(a
));
2242 asm("rnddm %0, %1" : "=f" (a
) : "f"(a
));
2245 asm("rnddp %0, %1" : "=f" (a
) : "f"(a
));
2248 asm("rnddz %0, %1" : "=f" (a
) : "f"(a
));
2257 void helper_fscale(void)
2259 CPU86_LDouble fpsrcop
, fptemp
;
2262 fptemp
= pow(fpsrcop
,ST1
);
2266 void helper_fsin(void)
2268 CPU86_LDouble fptemp
;
2271 if ((fptemp
> MAXTAN
)||(fptemp
< -MAXTAN
)) {
2275 env
->fpus
&= (~0x400); /* C2 <-- 0 */
2276 /* the above code is for |arg| < 2**53 only */
2280 void helper_fcos(void)
2282 CPU86_LDouble fptemp
;
2285 if((fptemp
> MAXTAN
)||(fptemp
< -MAXTAN
)) {
2289 env
->fpus
&= (~0x400); /* C2 <-- 0 */
2290 /* the above code is for |arg5 < 2**63 only */
2294 void helper_fxam_ST0(void)
2296 CPU86_LDoubleU temp
;
2301 env
->fpus
&= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2303 env
->fpus
|= 0x200; /* C1 <-- 1 */
2305 expdif
= EXPD(temp
);
2306 if (expdif
== MAXEXPD
) {
2307 if (MANTD(temp
) == 0)
2308 env
->fpus
|= 0x500 /*Infinity*/;
2310 env
->fpus
|= 0x100 /*NaN*/;
2311 } else if (expdif
== 0) {
2312 if (MANTD(temp
) == 0)
2313 env
->fpus
|= 0x4000 /*Zero*/;
2315 env
->fpus
|= 0x4400 /*Denormal*/;
2321 void helper_fstenv(uint8_t *ptr
, int data32
)
2323 int fpus
, fptag
, exp
, i
;
2327 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
2329 for (i
=7; i
>=0; i
--) {
2331 if (env
->fptags
[i
]) {
2334 tmp
.d
= env
->fpregs
[i
];
2337 if (exp
== 0 && mant
== 0) {
2340 } else if (exp
== 0 || exp
== MAXEXPD
2341 #ifdef USE_X86LDOUBLE
2342 || (mant
& (1LL << 63)) == 0
2345 /* NaNs, infinity, denormal */
2352 stl(ptr
, env
->fpuc
);
2354 stl(ptr
+ 8, fptag
);
2355 stl(ptr
+ 12, 0); /* fpip */
2356 stl(ptr
+ 16, 0); /* fpcs */
2357 stl(ptr
+ 20, 0); /* fpoo */
2358 stl(ptr
+ 24, 0); /* fpos */
2361 stw(ptr
, env
->fpuc
);
2363 stw(ptr
+ 4, fptag
);
2371 void helper_fldenv(uint8_t *ptr
, int data32
)
2376 env
->fpuc
= lduw(ptr
);
2377 fpus
= lduw(ptr
+ 4);
2378 fptag
= lduw(ptr
+ 8);
2381 env
->fpuc
= lduw(ptr
);
2382 fpus
= lduw(ptr
+ 2);
2383 fptag
= lduw(ptr
+ 4);
2385 env
->fpstt
= (fpus
>> 11) & 7;
2386 env
->fpus
= fpus
& ~0x3800;
2387 for(i
= 0;i
< 8; i
++) {
2388 env
->fptags
[i
] = ((fptag
& 3) == 3);
2393 void helper_fsave(uint8_t *ptr
, int data32
)
2398 helper_fstenv(ptr
, data32
);
2400 ptr
+= (14 << data32
);
2401 for(i
= 0;i
< 8; i
++) {
2403 helper_fstt(tmp
, ptr
);
2421 void helper_frstor(uint8_t *ptr
, int data32
)
2426 helper_fldenv(ptr
, data32
);
2427 ptr
+= (14 << data32
);
2429 for(i
= 0;i
< 8; i
++) {
2430 tmp
= helper_fldt(ptr
);
2436 #if !defined(CONFIG_USER_ONLY)
2438 #define MMUSUFFIX _mmu
2439 #define GETPC() (__builtin_return_address(0))
2442 #include "softmmu_template.h"
2445 #include "softmmu_template.h"
2448 #include "softmmu_template.h"
2451 #include "softmmu_template.h"
2455 /* try to fill the TLB and return an exception if error. If retaddr is
2456 NULL, it means that the function was called in C code (i.e. not
2457 from generated code or from helper.c) */
2458 /* XXX: fix it to restore all registers */
2459 void tlb_fill(unsigned long addr
, int is_write
, int is_user
, void *retaddr
)
2461 TranslationBlock
*tb
;
2464 CPUX86State
*saved_env
;
2466 /* XXX: hack to restore env in all cases, even if not called from
2469 env
= cpu_single_env
;
2471 ret
= cpu_x86_handle_mmu_fault(env
, addr
, is_write
, is_user
, 1);
2474 /* now we have a real cpu fault */
2475 pc
= (unsigned long)retaddr
;
2476 tb
= tb_find_pc(pc
);
2478 /* the PC is inside the translated code. It means that we have
2479 a virtual CPU fault */
2480 cpu_restore_state(tb
, env
, pc
, NULL
);
2483 raise_exception_err(EXCP0E_PAGE
, env
->error_code
);