2 * x86 segmentation related helpers:
3 * TSS, interrupts, system calls, jumps and call/task gates, descriptors
5 * Copyright (c) 2003 Fabrice Bellard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
27 #if !defined(CONFIG_USER_ONLY)
28 #include "exec/softmmu_exec.h"
29 #endif /* !defined(CONFIG_USER_ONLY) */
32 # define LOG_PCALL(...) qemu_log_mask(CPU_LOG_PCALL, ## __VA_ARGS__)
33 # define LOG_PCALL_STATE(cpu) \
34 log_cpu_state_mask(CPU_LOG_PCALL, (cpu), CPU_DUMP_CCOP)
36 # define LOG_PCALL(...) do { } while (0)
37 # define LOG_PCALL_STATE(cpu) do { } while (0)
40 /* return non zero if error */
41 static inline int load_segment(CPUX86State
*env
, uint32_t *e1_ptr
,
42 uint32_t *e2_ptr
, int selector
)
53 index
= selector
& ~7;
54 if ((index
+ 7) > dt
->limit
) {
57 ptr
= dt
->base
+ index
;
58 *e1_ptr
= cpu_ldl_kernel(env
, ptr
);
59 *e2_ptr
= cpu_ldl_kernel(env
, ptr
+ 4);
63 static inline unsigned int get_seg_limit(uint32_t e1
, uint32_t e2
)
67 limit
= (e1
& 0xffff) | (e2
& 0x000f0000);
68 if (e2
& DESC_G_MASK
) {
69 limit
= (limit
<< 12) | 0xfff;
74 static inline uint32_t get_seg_base(uint32_t e1
, uint32_t e2
)
76 return (e1
>> 16) | ((e2
& 0xff) << 16) | (e2
& 0xff000000);
79 static inline void load_seg_cache_raw_dt(SegmentCache
*sc
, uint32_t e1
,
82 sc
->base
= get_seg_base(e1
, e2
);
83 sc
->limit
= get_seg_limit(e1
, e2
);
87 /* init the segment cache in vm86 mode. */
88 static inline void load_seg_vm(CPUX86State
*env
, int seg
, int selector
)
91 cpu_x86_load_seg_cache(env
, seg
, selector
,
92 (selector
<< 4), 0xffff, 0);
95 static inline void get_ss_esp_from_tss(CPUX86State
*env
, uint32_t *ss_ptr
,
96 uint32_t *esp_ptr
, int dpl
)
98 int type
, index
, shift
;
103 printf("TR: base=%p limit=%x\n", env
->tr
.base
, env
->tr
.limit
);
104 for (i
= 0; i
< env
->tr
.limit
; i
++) {
105 printf("%02x ", env
->tr
.base
[i
]);
114 if (!(env
->tr
.flags
& DESC_P_MASK
)) {
115 cpu_abort(env
, "invalid tss");
117 type
= (env
->tr
.flags
>> DESC_TYPE_SHIFT
) & 0xf;
118 if ((type
& 7) != 1) {
119 cpu_abort(env
, "invalid tss type");
122 index
= (dpl
* 4 + 2) << shift
;
123 if (index
+ (4 << shift
) - 1 > env
->tr
.limit
) {
124 raise_exception_err(env
, EXCP0A_TSS
, env
->tr
.selector
& 0xfffc);
127 *esp_ptr
= cpu_lduw_kernel(env
, env
->tr
.base
+ index
);
128 *ss_ptr
= cpu_lduw_kernel(env
, env
->tr
.base
+ index
+ 2);
130 *esp_ptr
= cpu_ldl_kernel(env
, env
->tr
.base
+ index
);
131 *ss_ptr
= cpu_lduw_kernel(env
, env
->tr
.base
+ index
+ 4);
135 /* XXX: merge with load_seg() */
136 static void tss_load_seg(CPUX86State
*env
, int seg_reg
, int selector
)
141 if ((selector
& 0xfffc) != 0) {
142 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
143 raise_exception_err(env
, EXCP0A_TSS
, selector
& 0xfffc);
145 if (!(e2
& DESC_S_MASK
)) {
146 raise_exception_err(env
, EXCP0A_TSS
, selector
& 0xfffc);
149 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
150 cpl
= env
->hflags
& HF_CPL_MASK
;
151 if (seg_reg
== R_CS
) {
152 if (!(e2
& DESC_CS_MASK
)) {
153 raise_exception_err(env
, EXCP0A_TSS
, selector
& 0xfffc);
155 /* XXX: is it correct? */
157 raise_exception_err(env
, EXCP0A_TSS
, selector
& 0xfffc);
159 if ((e2
& DESC_C_MASK
) && dpl
> rpl
) {
160 raise_exception_err(env
, EXCP0A_TSS
, selector
& 0xfffc);
162 } else if (seg_reg
== R_SS
) {
163 /* SS must be writable data */
164 if ((e2
& DESC_CS_MASK
) || !(e2
& DESC_W_MASK
)) {
165 raise_exception_err(env
, EXCP0A_TSS
, selector
& 0xfffc);
167 if (dpl
!= cpl
|| dpl
!= rpl
) {
168 raise_exception_err(env
, EXCP0A_TSS
, selector
& 0xfffc);
171 /* not readable code */
172 if ((e2
& DESC_CS_MASK
) && !(e2
& DESC_R_MASK
)) {
173 raise_exception_err(env
, EXCP0A_TSS
, selector
& 0xfffc);
175 /* if data or non conforming code, checks the rights */
176 if (((e2
>> DESC_TYPE_SHIFT
) & 0xf) < 12) {
177 if (dpl
< cpl
|| dpl
< rpl
) {
178 raise_exception_err(env
, EXCP0A_TSS
, selector
& 0xfffc);
182 if (!(e2
& DESC_P_MASK
)) {
183 raise_exception_err(env
, EXCP0B_NOSEG
, selector
& 0xfffc);
185 cpu_x86_load_seg_cache(env
, seg_reg
, selector
,
186 get_seg_base(e1
, e2
),
187 get_seg_limit(e1
, e2
),
190 if (seg_reg
== R_SS
|| seg_reg
== R_CS
) {
191 raise_exception_err(env
, EXCP0A_TSS
, selector
& 0xfffc);
196 #define SWITCH_TSS_JMP 0
197 #define SWITCH_TSS_IRET 1
198 #define SWITCH_TSS_CALL 2
200 /* XXX: restore CPU state in registers (PowerPC case) */
201 static void switch_tss(CPUX86State
*env
, int tss_selector
,
202 uint32_t e1
, uint32_t e2
, int source
,
205 int tss_limit
, tss_limit_max
, type
, old_tss_limit_max
, old_type
, v1
, v2
, i
;
206 target_ulong tss_base
;
207 uint32_t new_regs
[8], new_segs
[6];
208 uint32_t new_eflags
, new_eip
, new_cr3
, new_ldt
, new_trap
;
209 uint32_t old_eflags
, eflags_mask
;
214 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
215 LOG_PCALL("switch_tss: sel=0x%04x type=%d src=%d\n", tss_selector
, type
,
218 /* if task gate, we read the TSS segment and we load it */
220 if (!(e2
& DESC_P_MASK
)) {
221 raise_exception_err(env
, EXCP0B_NOSEG
, tss_selector
& 0xfffc);
223 tss_selector
= e1
>> 16;
224 if (tss_selector
& 4) {
225 raise_exception_err(env
, EXCP0A_TSS
, tss_selector
& 0xfffc);
227 if (load_segment(env
, &e1
, &e2
, tss_selector
) != 0) {
228 raise_exception_err(env
, EXCP0D_GPF
, tss_selector
& 0xfffc);
230 if (e2
& DESC_S_MASK
) {
231 raise_exception_err(env
, EXCP0D_GPF
, tss_selector
& 0xfffc);
233 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
234 if ((type
& 7) != 1) {
235 raise_exception_err(env
, EXCP0D_GPF
, tss_selector
& 0xfffc);
239 if (!(e2
& DESC_P_MASK
)) {
240 raise_exception_err(env
, EXCP0B_NOSEG
, tss_selector
& 0xfffc);
248 tss_limit
= get_seg_limit(e1
, e2
);
249 tss_base
= get_seg_base(e1
, e2
);
250 if ((tss_selector
& 4) != 0 ||
251 tss_limit
< tss_limit_max
) {
252 raise_exception_err(env
, EXCP0A_TSS
, tss_selector
& 0xfffc);
254 old_type
= (env
->tr
.flags
>> DESC_TYPE_SHIFT
) & 0xf;
256 old_tss_limit_max
= 103;
258 old_tss_limit_max
= 43;
261 /* read all the registers from the new TSS */
264 new_cr3
= cpu_ldl_kernel(env
, tss_base
+ 0x1c);
265 new_eip
= cpu_ldl_kernel(env
, tss_base
+ 0x20);
266 new_eflags
= cpu_ldl_kernel(env
, tss_base
+ 0x24);
267 for (i
= 0; i
< 8; i
++) {
268 new_regs
[i
] = cpu_ldl_kernel(env
, tss_base
+ (0x28 + i
* 4));
270 for (i
= 0; i
< 6; i
++) {
271 new_segs
[i
] = cpu_lduw_kernel(env
, tss_base
+ (0x48 + i
* 4));
273 new_ldt
= cpu_lduw_kernel(env
, tss_base
+ 0x60);
274 new_trap
= cpu_ldl_kernel(env
, tss_base
+ 0x64);
278 new_eip
= cpu_lduw_kernel(env
, tss_base
+ 0x0e);
279 new_eflags
= cpu_lduw_kernel(env
, tss_base
+ 0x10);
280 for (i
= 0; i
< 8; i
++) {
281 new_regs
[i
] = cpu_lduw_kernel(env
, tss_base
+ (0x12 + i
* 2)) |
284 for (i
= 0; i
< 4; i
++) {
285 new_segs
[i
] = cpu_lduw_kernel(env
, tss_base
+ (0x22 + i
* 4));
287 new_ldt
= cpu_lduw_kernel(env
, tss_base
+ 0x2a);
292 /* XXX: avoid a compiler warning, see
293 http://support.amd.com/us/Processor_TechDocs/24593.pdf
294 chapters 12.2.5 and 13.2.4 on how to implement TSS Trap bit */
297 /* NOTE: we must avoid memory exceptions during the task switch,
298 so we make dummy accesses before */
299 /* XXX: it can still fail in some cases, so a bigger hack is
300 necessary to valid the TLB after having done the accesses */
302 v1
= cpu_ldub_kernel(env
, env
->tr
.base
);
303 v2
= cpu_ldub_kernel(env
, env
->tr
.base
+ old_tss_limit_max
);
304 cpu_stb_kernel(env
, env
->tr
.base
, v1
);
305 cpu_stb_kernel(env
, env
->tr
.base
+ old_tss_limit_max
, v2
);
307 /* clear busy bit (it is restartable) */
308 if (source
== SWITCH_TSS_JMP
|| source
== SWITCH_TSS_IRET
) {
312 ptr
= env
->gdt
.base
+ (env
->tr
.selector
& ~7);
313 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
314 e2
&= ~DESC_TSS_BUSY_MASK
;
315 cpu_stl_kernel(env
, ptr
+ 4, e2
);
317 old_eflags
= cpu_compute_eflags(env
);
318 if (source
== SWITCH_TSS_IRET
) {
319 old_eflags
&= ~NT_MASK
;
322 /* save the current state in the old TSS */
325 cpu_stl_kernel(env
, env
->tr
.base
+ 0x20, next_eip
);
326 cpu_stl_kernel(env
, env
->tr
.base
+ 0x24, old_eflags
);
327 cpu_stl_kernel(env
, env
->tr
.base
+ (0x28 + 0 * 4), env
->regs
[R_EAX
]);
328 cpu_stl_kernel(env
, env
->tr
.base
+ (0x28 + 1 * 4), env
->regs
[R_ECX
]);
329 cpu_stl_kernel(env
, env
->tr
.base
+ (0x28 + 2 * 4), env
->regs
[R_EDX
]);
330 cpu_stl_kernel(env
, env
->tr
.base
+ (0x28 + 3 * 4), env
->regs
[R_EBX
]);
331 cpu_stl_kernel(env
, env
->tr
.base
+ (0x28 + 4 * 4), env
->regs
[R_ESP
]);
332 cpu_stl_kernel(env
, env
->tr
.base
+ (0x28 + 5 * 4), env
->regs
[R_EBP
]);
333 cpu_stl_kernel(env
, env
->tr
.base
+ (0x28 + 6 * 4), env
->regs
[R_ESI
]);
334 cpu_stl_kernel(env
, env
->tr
.base
+ (0x28 + 7 * 4), env
->regs
[R_EDI
]);
335 for (i
= 0; i
< 6; i
++) {
336 cpu_stw_kernel(env
, env
->tr
.base
+ (0x48 + i
* 4),
337 env
->segs
[i
].selector
);
341 cpu_stw_kernel(env
, env
->tr
.base
+ 0x0e, next_eip
);
342 cpu_stw_kernel(env
, env
->tr
.base
+ 0x10, old_eflags
);
343 cpu_stw_kernel(env
, env
->tr
.base
+ (0x12 + 0 * 2), env
->regs
[R_EAX
]);
344 cpu_stw_kernel(env
, env
->tr
.base
+ (0x12 + 1 * 2), env
->regs
[R_ECX
]);
345 cpu_stw_kernel(env
, env
->tr
.base
+ (0x12 + 2 * 2), env
->regs
[R_EDX
]);
346 cpu_stw_kernel(env
, env
->tr
.base
+ (0x12 + 3 * 2), env
->regs
[R_EBX
]);
347 cpu_stw_kernel(env
, env
->tr
.base
+ (0x12 + 4 * 2), env
->regs
[R_ESP
]);
348 cpu_stw_kernel(env
, env
->tr
.base
+ (0x12 + 5 * 2), env
->regs
[R_EBP
]);
349 cpu_stw_kernel(env
, env
->tr
.base
+ (0x12 + 6 * 2), env
->regs
[R_ESI
]);
350 cpu_stw_kernel(env
, env
->tr
.base
+ (0x12 + 7 * 2), env
->regs
[R_EDI
]);
351 for (i
= 0; i
< 4; i
++) {
352 cpu_stw_kernel(env
, env
->tr
.base
+ (0x22 + i
* 4),
353 env
->segs
[i
].selector
);
357 /* now if an exception occurs, it will occurs in the next task
360 if (source
== SWITCH_TSS_CALL
) {
361 cpu_stw_kernel(env
, tss_base
, env
->tr
.selector
);
362 new_eflags
|= NT_MASK
;
366 if (source
== SWITCH_TSS_JMP
|| source
== SWITCH_TSS_CALL
) {
370 ptr
= env
->gdt
.base
+ (tss_selector
& ~7);
371 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
372 e2
|= DESC_TSS_BUSY_MASK
;
373 cpu_stl_kernel(env
, ptr
+ 4, e2
);
376 /* set the new CPU state */
377 /* from this point, any exception which occurs can give problems */
378 env
->cr
[0] |= CR0_TS_MASK
;
379 env
->hflags
|= HF_TS_MASK
;
380 env
->tr
.selector
= tss_selector
;
381 env
->tr
.base
= tss_base
;
382 env
->tr
.limit
= tss_limit
;
383 env
->tr
.flags
= e2
& ~DESC_TSS_BUSY_MASK
;
385 if ((type
& 8) && (env
->cr
[0] & CR0_PG_MASK
)) {
386 cpu_x86_update_cr3(env
, new_cr3
);
389 /* load all registers without an exception, then reload them with
390 possible exception */
392 eflags_mask
= TF_MASK
| AC_MASK
| ID_MASK
|
393 IF_MASK
| IOPL_MASK
| VM_MASK
| RF_MASK
| NT_MASK
;
395 eflags_mask
&= 0xffff;
397 cpu_load_eflags(env
, new_eflags
, eflags_mask
);
398 /* XXX: what to do in 16 bit case? */
399 env
->regs
[R_EAX
] = new_regs
[0];
400 env
->regs
[R_ECX
] = new_regs
[1];
401 env
->regs
[R_EDX
] = new_regs
[2];
402 env
->regs
[R_EBX
] = new_regs
[3];
403 env
->regs
[R_ESP
] = new_regs
[4];
404 env
->regs
[R_EBP
] = new_regs
[5];
405 env
->regs
[R_ESI
] = new_regs
[6];
406 env
->regs
[R_EDI
] = new_regs
[7];
407 if (new_eflags
& VM_MASK
) {
408 for (i
= 0; i
< 6; i
++) {
409 load_seg_vm(env
, i
, new_segs
[i
]);
411 /* in vm86, CPL is always 3 */
412 cpu_x86_set_cpl(env
, 3);
414 /* CPL is set the RPL of CS */
415 cpu_x86_set_cpl(env
, new_segs
[R_CS
] & 3);
416 /* first just selectors as the rest may trigger exceptions */
417 for (i
= 0; i
< 6; i
++) {
418 cpu_x86_load_seg_cache(env
, i
, new_segs
[i
], 0, 0, 0);
422 env
->ldt
.selector
= new_ldt
& ~4;
429 raise_exception_err(env
, EXCP0A_TSS
, new_ldt
& 0xfffc);
432 if ((new_ldt
& 0xfffc) != 0) {
434 index
= new_ldt
& ~7;
435 if ((index
+ 7) > dt
->limit
) {
436 raise_exception_err(env
, EXCP0A_TSS
, new_ldt
& 0xfffc);
438 ptr
= dt
->base
+ index
;
439 e1
= cpu_ldl_kernel(env
, ptr
);
440 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
441 if ((e2
& DESC_S_MASK
) || ((e2
>> DESC_TYPE_SHIFT
) & 0xf) != 2) {
442 raise_exception_err(env
, EXCP0A_TSS
, new_ldt
& 0xfffc);
444 if (!(e2
& DESC_P_MASK
)) {
445 raise_exception_err(env
, EXCP0A_TSS
, new_ldt
& 0xfffc);
447 load_seg_cache_raw_dt(&env
->ldt
, e1
, e2
);
450 /* load the segments */
451 if (!(new_eflags
& VM_MASK
)) {
452 tss_load_seg(env
, R_CS
, new_segs
[R_CS
]);
453 tss_load_seg(env
, R_SS
, new_segs
[R_SS
]);
454 tss_load_seg(env
, R_ES
, new_segs
[R_ES
]);
455 tss_load_seg(env
, R_DS
, new_segs
[R_DS
]);
456 tss_load_seg(env
, R_FS
, new_segs
[R_FS
]);
457 tss_load_seg(env
, R_GS
, new_segs
[R_GS
]);
460 /* check that env->eip is in the CS segment limits */
461 if (new_eip
> env
->segs
[R_CS
].limit
) {
462 /* XXX: different exception if CALL? */
463 raise_exception_err(env
, EXCP0D_GPF
, 0);
466 #ifndef CONFIG_USER_ONLY
467 /* reset local breakpoints */
468 if (env
->dr
[7] & DR7_LOCAL_BP_MASK
) {
469 for (i
= 0; i
< DR7_MAX_BP
; i
++) {
470 if (hw_local_breakpoint_enabled(env
->dr
[7], i
) &&
471 !hw_global_breakpoint_enabled(env
->dr
[7], i
)) {
472 hw_breakpoint_remove(env
, i
);
475 env
->dr
[7] &= ~DR7_LOCAL_BP_MASK
;
480 static inline unsigned int get_sp_mask(unsigned int e2
)
482 if (e2
& DESC_B_MASK
) {
489 static int exception_has_error_code(int intno
)
505 #define SET_ESP(val, sp_mask) \
507 if ((sp_mask) == 0xffff) { \
508 env->regs[R_ESP] = (env->regs[R_ESP] & ~0xffff) | \
510 } else if ((sp_mask) == 0xffffffffLL) { \
511 env->regs[R_ESP] = (uint32_t)(val); \
513 env->regs[R_ESP] = (val); \
517 #define SET_ESP(val, sp_mask) \
519 env->regs[R_ESP] = (env->regs[R_ESP] & ~(sp_mask)) | \
520 ((val) & (sp_mask)); \
524 /* in 64-bit machines, this can overflow. So this segment addition macro
525 * can be used to trim the value to 32-bit whenever needed */
526 #define SEG_ADDL(ssp, sp, sp_mask) ((uint32_t)((ssp) + (sp & (sp_mask))))
528 /* XXX: add a is_user flag to have proper security support */
529 #define PUSHW(ssp, sp, sp_mask, val) \
532 cpu_stw_kernel(env, (ssp) + (sp & (sp_mask)), (val)); \
535 #define PUSHL(ssp, sp, sp_mask, val) \
538 cpu_stl_kernel(env, SEG_ADDL(ssp, sp, sp_mask), (uint32_t)(val)); \
541 #define POPW(ssp, sp, sp_mask, val) \
543 val = cpu_lduw_kernel(env, (ssp) + (sp & (sp_mask))); \
547 #define POPL(ssp, sp, sp_mask, val) \
549 val = (uint32_t)cpu_ldl_kernel(env, SEG_ADDL(ssp, sp, sp_mask)); \
553 /* protected mode interrupt */
554 static void do_interrupt_protected(CPUX86State
*env
, int intno
, int is_int
,
555 int error_code
, unsigned int next_eip
,
559 target_ulong ptr
, ssp
;
560 int type
, dpl
, selector
, ss_dpl
, cpl
;
561 int has_error_code
, new_stack
, shift
;
562 uint32_t e1
, e2
, offset
, ss
= 0, esp
, ss_e1
= 0, ss_e2
= 0;
563 uint32_t old_eip
, sp_mask
;
566 if (!is_int
&& !is_hw
) {
567 has_error_code
= exception_has_error_code(intno
);
576 if (intno
* 8 + 7 > dt
->limit
) {
577 raise_exception_err(env
, EXCP0D_GPF
, intno
* 8 + 2);
579 ptr
= dt
->base
+ intno
* 8;
580 e1
= cpu_ldl_kernel(env
, ptr
);
581 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
582 /* check gate type */
583 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x1f;
585 case 5: /* task gate */
586 /* must do that check here to return the correct error code */
587 if (!(e2
& DESC_P_MASK
)) {
588 raise_exception_err(env
, EXCP0B_NOSEG
, intno
* 8 + 2);
590 switch_tss(env
, intno
* 8, e1
, e2
, SWITCH_TSS_CALL
, old_eip
);
591 if (has_error_code
) {
595 /* push the error code */
596 type
= (env
->tr
.flags
>> DESC_TYPE_SHIFT
) & 0xf;
598 if (env
->segs
[R_SS
].flags
& DESC_B_MASK
) {
603 esp
= (env
->regs
[R_ESP
] - (2 << shift
)) & mask
;
604 ssp
= env
->segs
[R_SS
].base
+ esp
;
606 cpu_stl_kernel(env
, ssp
, error_code
);
608 cpu_stw_kernel(env
, ssp
, error_code
);
613 case 6: /* 286 interrupt gate */
614 case 7: /* 286 trap gate */
615 case 14: /* 386 interrupt gate */
616 case 15: /* 386 trap gate */
619 raise_exception_err(env
, EXCP0D_GPF
, intno
* 8 + 2);
622 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
623 cpl
= env
->hflags
& HF_CPL_MASK
;
624 /* check privilege if software int */
625 if (is_int
&& dpl
< cpl
) {
626 raise_exception_err(env
, EXCP0D_GPF
, intno
* 8 + 2);
628 /* check valid bit */
629 if (!(e2
& DESC_P_MASK
)) {
630 raise_exception_err(env
, EXCP0B_NOSEG
, intno
* 8 + 2);
633 offset
= (e2
& 0xffff0000) | (e1
& 0x0000ffff);
634 if ((selector
& 0xfffc) == 0) {
635 raise_exception_err(env
, EXCP0D_GPF
, 0);
637 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
638 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
640 if (!(e2
& DESC_S_MASK
) || !(e2
& (DESC_CS_MASK
))) {
641 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
643 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
645 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
647 if (!(e2
& DESC_P_MASK
)) {
648 raise_exception_err(env
, EXCP0B_NOSEG
, selector
& 0xfffc);
650 if (!(e2
& DESC_C_MASK
) && dpl
< cpl
) {
651 /* to inner privilege */
652 get_ss_esp_from_tss(env
, &ss
, &esp
, dpl
);
653 if ((ss
& 0xfffc) == 0) {
654 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
656 if ((ss
& 3) != dpl
) {
657 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
659 if (load_segment(env
, &ss_e1
, &ss_e2
, ss
) != 0) {
660 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
662 ss_dpl
= (ss_e2
>> DESC_DPL_SHIFT
) & 3;
664 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
666 if (!(ss_e2
& DESC_S_MASK
) ||
667 (ss_e2
& DESC_CS_MASK
) ||
668 !(ss_e2
& DESC_W_MASK
)) {
669 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
671 if (!(ss_e2
& DESC_P_MASK
)) {
672 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
675 sp_mask
= get_sp_mask(ss_e2
);
676 ssp
= get_seg_base(ss_e1
, ss_e2
);
677 } else if ((e2
& DESC_C_MASK
) || dpl
== cpl
) {
678 /* to same privilege */
679 if (env
->eflags
& VM_MASK
) {
680 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
683 sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
684 ssp
= env
->segs
[R_SS
].base
;
685 esp
= env
->regs
[R_ESP
];
688 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
689 new_stack
= 0; /* avoid warning */
690 sp_mask
= 0; /* avoid warning */
691 ssp
= 0; /* avoid warning */
692 esp
= 0; /* avoid warning */
698 /* XXX: check that enough room is available */
699 push_size
= 6 + (new_stack
<< 2) + (has_error_code
<< 1);
700 if (env
->eflags
& VM_MASK
) {
707 if (env
->eflags
& VM_MASK
) {
708 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_GS
].selector
);
709 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_FS
].selector
);
710 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_DS
].selector
);
711 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_ES
].selector
);
713 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_SS
].selector
);
714 PUSHL(ssp
, esp
, sp_mask
, env
->regs
[R_ESP
]);
716 PUSHL(ssp
, esp
, sp_mask
, cpu_compute_eflags(env
));
717 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_CS
].selector
);
718 PUSHL(ssp
, esp
, sp_mask
, old_eip
);
719 if (has_error_code
) {
720 PUSHL(ssp
, esp
, sp_mask
, error_code
);
724 if (env
->eflags
& VM_MASK
) {
725 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_GS
].selector
);
726 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_FS
].selector
);
727 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_DS
].selector
);
728 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_ES
].selector
);
730 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_SS
].selector
);
731 PUSHW(ssp
, esp
, sp_mask
, env
->regs
[R_ESP
]);
733 PUSHW(ssp
, esp
, sp_mask
, cpu_compute_eflags(env
));
734 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_CS
].selector
);
735 PUSHW(ssp
, esp
, sp_mask
, old_eip
);
736 if (has_error_code
) {
737 PUSHW(ssp
, esp
, sp_mask
, error_code
);
742 if (env
->eflags
& VM_MASK
) {
743 cpu_x86_load_seg_cache(env
, R_ES
, 0, 0, 0, 0);
744 cpu_x86_load_seg_cache(env
, R_DS
, 0, 0, 0, 0);
745 cpu_x86_load_seg_cache(env
, R_FS
, 0, 0, 0, 0);
746 cpu_x86_load_seg_cache(env
, R_GS
, 0, 0, 0, 0);
748 ss
= (ss
& ~3) | dpl
;
749 cpu_x86_load_seg_cache(env
, R_SS
, ss
,
750 ssp
, get_seg_limit(ss_e1
, ss_e2
), ss_e2
);
752 SET_ESP(esp
, sp_mask
);
754 selector
= (selector
& ~3) | dpl
;
755 cpu_x86_load_seg_cache(env
, R_CS
, selector
,
756 get_seg_base(e1
, e2
),
757 get_seg_limit(e1
, e2
),
759 cpu_x86_set_cpl(env
, dpl
);
762 /* interrupt gate clear IF mask */
763 if ((type
& 1) == 0) {
764 env
->eflags
&= ~IF_MASK
;
766 env
->eflags
&= ~(TF_MASK
| VM_MASK
| RF_MASK
| NT_MASK
);
771 #define PUSHQ(sp, val) \
774 cpu_stq_kernel(env, sp, (val)); \
777 #define POPQ(sp, val) \
779 val = cpu_ldq_kernel(env, sp); \
783 static inline target_ulong
get_rsp_from_tss(CPUX86State
*env
, int level
)
788 printf("TR: base=" TARGET_FMT_lx
" limit=%x\n",
789 env
->tr
.base
, env
->tr
.limit
);
792 if (!(env
->tr
.flags
& DESC_P_MASK
)) {
793 cpu_abort(env
, "invalid tss");
795 index
= 8 * level
+ 4;
796 if ((index
+ 7) > env
->tr
.limit
) {
797 raise_exception_err(env
, EXCP0A_TSS
, env
->tr
.selector
& 0xfffc);
799 return cpu_ldq_kernel(env
, env
->tr
.base
+ index
);
802 /* 64 bit interrupt */
803 static void do_interrupt64(CPUX86State
*env
, int intno
, int is_int
,
804 int error_code
, target_ulong next_eip
, int is_hw
)
808 int type
, dpl
, selector
, cpl
, ist
;
809 int has_error_code
, new_stack
;
810 uint32_t e1
, e2
, e3
, ss
;
811 target_ulong old_eip
, esp
, offset
;
814 if (!is_int
&& !is_hw
) {
815 has_error_code
= exception_has_error_code(intno
);
824 if (intno
* 16 + 15 > dt
->limit
) {
825 raise_exception_err(env
, EXCP0D_GPF
, intno
* 16 + 2);
827 ptr
= dt
->base
+ intno
* 16;
828 e1
= cpu_ldl_kernel(env
, ptr
);
829 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
830 e3
= cpu_ldl_kernel(env
, ptr
+ 8);
831 /* check gate type */
832 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x1f;
834 case 14: /* 386 interrupt gate */
835 case 15: /* 386 trap gate */
838 raise_exception_err(env
, EXCP0D_GPF
, intno
* 16 + 2);
841 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
842 cpl
= env
->hflags
& HF_CPL_MASK
;
843 /* check privilege if software int */
844 if (is_int
&& dpl
< cpl
) {
845 raise_exception_err(env
, EXCP0D_GPF
, intno
* 16 + 2);
847 /* check valid bit */
848 if (!(e2
& DESC_P_MASK
)) {
849 raise_exception_err(env
, EXCP0B_NOSEG
, intno
* 16 + 2);
852 offset
= ((target_ulong
)e3
<< 32) | (e2
& 0xffff0000) | (e1
& 0x0000ffff);
854 if ((selector
& 0xfffc) == 0) {
855 raise_exception_err(env
, EXCP0D_GPF
, 0);
858 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
859 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
861 if (!(e2
& DESC_S_MASK
) || !(e2
& (DESC_CS_MASK
))) {
862 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
864 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
866 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
868 if (!(e2
& DESC_P_MASK
)) {
869 raise_exception_err(env
, EXCP0B_NOSEG
, selector
& 0xfffc);
871 if (!(e2
& DESC_L_MASK
) || (e2
& DESC_B_MASK
)) {
872 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
874 if ((!(e2
& DESC_C_MASK
) && dpl
< cpl
) || ist
!= 0) {
875 /* to inner privilege */
877 esp
= get_rsp_from_tss(env
, ist
+ 3);
879 esp
= get_rsp_from_tss(env
, dpl
);
881 esp
&= ~0xfLL
; /* align stack */
884 } else if ((e2
& DESC_C_MASK
) || dpl
== cpl
) {
885 /* to same privilege */
886 if (env
->eflags
& VM_MASK
) {
887 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
891 esp
= get_rsp_from_tss(env
, ist
+ 3);
893 esp
= env
->regs
[R_ESP
];
895 esp
&= ~0xfLL
; /* align stack */
898 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
899 new_stack
= 0; /* avoid warning */
900 esp
= 0; /* avoid warning */
903 PUSHQ(esp
, env
->segs
[R_SS
].selector
);
904 PUSHQ(esp
, env
->regs
[R_ESP
]);
905 PUSHQ(esp
, cpu_compute_eflags(env
));
906 PUSHQ(esp
, env
->segs
[R_CS
].selector
);
908 if (has_error_code
) {
909 PUSHQ(esp
, error_code
);
914 cpu_x86_load_seg_cache(env
, R_SS
, ss
, 0, 0, 0);
916 env
->regs
[R_ESP
] = esp
;
918 selector
= (selector
& ~3) | dpl
;
919 cpu_x86_load_seg_cache(env
, R_CS
, selector
,
920 get_seg_base(e1
, e2
),
921 get_seg_limit(e1
, e2
),
923 cpu_x86_set_cpl(env
, dpl
);
926 /* interrupt gate clear IF mask */
927 if ((type
& 1) == 0) {
928 env
->eflags
&= ~IF_MASK
;
930 env
->eflags
&= ~(TF_MASK
| VM_MASK
| RF_MASK
| NT_MASK
);
935 #if defined(CONFIG_USER_ONLY)
936 void helper_syscall(CPUX86State
*env
, int next_eip_addend
)
938 env
->exception_index
= EXCP_SYSCALL
;
939 env
->exception_next_eip
= env
->eip
+ next_eip_addend
;
943 void helper_syscall(CPUX86State
*env
, int next_eip_addend
)
947 if (!(env
->efer
& MSR_EFER_SCE
)) {
948 raise_exception_err(env
, EXCP06_ILLOP
, 0);
950 selector
= (env
->star
>> 32) & 0xffff;
951 if (env
->hflags
& HF_LMA_MASK
) {
954 env
->regs
[R_ECX
] = env
->eip
+ next_eip_addend
;
955 env
->regs
[11] = cpu_compute_eflags(env
);
957 code64
= env
->hflags
& HF_CS64_MASK
;
959 cpu_x86_set_cpl(env
, 0);
960 cpu_x86_load_seg_cache(env
, R_CS
, selector
& 0xfffc,
962 DESC_G_MASK
| DESC_P_MASK
|
964 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
|
966 cpu_x86_load_seg_cache(env
, R_SS
, (selector
+ 8) & 0xfffc,
968 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
970 DESC_W_MASK
| DESC_A_MASK
);
971 env
->eflags
&= ~env
->fmask
;
972 cpu_load_eflags(env
, env
->eflags
, 0);
974 env
->eip
= env
->lstar
;
976 env
->eip
= env
->cstar
;
979 env
->regs
[R_ECX
] = (uint32_t)(env
->eip
+ next_eip_addend
);
981 cpu_x86_set_cpl(env
, 0);
982 cpu_x86_load_seg_cache(env
, R_CS
, selector
& 0xfffc,
984 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
986 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
);
987 cpu_x86_load_seg_cache(env
, R_SS
, (selector
+ 8) & 0xfffc,
989 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
991 DESC_W_MASK
| DESC_A_MASK
);
992 env
->eflags
&= ~(IF_MASK
| RF_MASK
| VM_MASK
);
993 env
->eip
= (uint32_t)env
->star
;
1000 void helper_sysret(CPUX86State
*env
, int dflag
)
1004 if (!(env
->efer
& MSR_EFER_SCE
)) {
1005 raise_exception_err(env
, EXCP06_ILLOP
, 0);
1007 cpl
= env
->hflags
& HF_CPL_MASK
;
1008 if (!(env
->cr
[0] & CR0_PE_MASK
) || cpl
!= 0) {
1009 raise_exception_err(env
, EXCP0D_GPF
, 0);
1011 selector
= (env
->star
>> 48) & 0xffff;
1012 if (env
->hflags
& HF_LMA_MASK
) {
1014 cpu_x86_load_seg_cache(env
, R_CS
, (selector
+ 16) | 3,
1016 DESC_G_MASK
| DESC_P_MASK
|
1017 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
1018 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
|
1020 env
->eip
= env
->regs
[R_ECX
];
1022 cpu_x86_load_seg_cache(env
, R_CS
, selector
| 3,
1024 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
1025 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
1026 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
);
1027 env
->eip
= (uint32_t)env
->regs
[R_ECX
];
1029 cpu_x86_load_seg_cache(env
, R_SS
, selector
+ 8,
1031 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
1032 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
1033 DESC_W_MASK
| DESC_A_MASK
);
1034 cpu_load_eflags(env
, (uint32_t)(env
->regs
[11]), TF_MASK
| AC_MASK
1035 | ID_MASK
| IF_MASK
| IOPL_MASK
| VM_MASK
| RF_MASK
|
1037 cpu_x86_set_cpl(env
, 3);
1039 cpu_x86_load_seg_cache(env
, R_CS
, selector
| 3,
1041 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
1042 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
1043 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
);
1044 env
->eip
= (uint32_t)env
->regs
[R_ECX
];
1045 cpu_x86_load_seg_cache(env
, R_SS
, selector
+ 8,
1047 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
1048 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
1049 DESC_W_MASK
| DESC_A_MASK
);
1050 env
->eflags
|= IF_MASK
;
1051 cpu_x86_set_cpl(env
, 3);
1056 /* real mode interrupt */
1057 static void do_interrupt_real(CPUX86State
*env
, int intno
, int is_int
,
1058 int error_code
, unsigned int next_eip
)
1061 target_ulong ptr
, ssp
;
1063 uint32_t offset
, esp
;
1064 uint32_t old_cs
, old_eip
;
1066 /* real mode (simpler!) */
1068 if (intno
* 4 + 3 > dt
->limit
) {
1069 raise_exception_err(env
, EXCP0D_GPF
, intno
* 8 + 2);
1071 ptr
= dt
->base
+ intno
* 4;
1072 offset
= cpu_lduw_kernel(env
, ptr
);
1073 selector
= cpu_lduw_kernel(env
, ptr
+ 2);
1074 esp
= env
->regs
[R_ESP
];
1075 ssp
= env
->segs
[R_SS
].base
;
1081 old_cs
= env
->segs
[R_CS
].selector
;
1082 /* XXX: use SS segment size? */
1083 PUSHW(ssp
, esp
, 0xffff, cpu_compute_eflags(env
));
1084 PUSHW(ssp
, esp
, 0xffff, old_cs
);
1085 PUSHW(ssp
, esp
, 0xffff, old_eip
);
1087 /* update processor state */
1088 env
->regs
[R_ESP
] = (env
->regs
[R_ESP
] & ~0xffff) | (esp
& 0xffff);
1090 env
->segs
[R_CS
].selector
= selector
;
1091 env
->segs
[R_CS
].base
= (selector
<< 4);
1092 env
->eflags
&= ~(IF_MASK
| TF_MASK
| AC_MASK
| RF_MASK
);
1095 #if defined(CONFIG_USER_ONLY)
1096 /* fake user mode interrupt */
1097 static void do_interrupt_user(CPUX86State
*env
, int intno
, int is_int
,
1098 int error_code
, target_ulong next_eip
)
1102 int dpl
, cpl
, shift
;
1106 if (env
->hflags
& HF_LMA_MASK
) {
1111 ptr
= dt
->base
+ (intno
<< shift
);
1112 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
1114 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1115 cpl
= env
->hflags
& HF_CPL_MASK
;
1116 /* check privilege if software int */
1117 if (is_int
&& dpl
< cpl
) {
1118 raise_exception_err(env
, EXCP0D_GPF
, (intno
<< shift
) + 2);
1121 /* Since we emulate only user space, we cannot do more than
1122 exiting the emulation with the suitable exception and error
1125 env
->eip
= next_eip
;
1131 static void handle_even_inj(CPUX86State
*env
, int intno
, int is_int
,
1132 int error_code
, int is_hw
, int rm
)
1134 uint32_t event_inj
= ldl_phys(env
->vm_vmcb
+ offsetof(struct vmcb
,
1135 control
.event_inj
));
1137 if (!(event_inj
& SVM_EVTINJ_VALID
)) {
1141 type
= SVM_EVTINJ_TYPE_SOFT
;
1143 type
= SVM_EVTINJ_TYPE_EXEPT
;
1145 event_inj
= intno
| type
| SVM_EVTINJ_VALID
;
1146 if (!rm
&& exception_has_error_code(intno
)) {
1147 event_inj
|= SVM_EVTINJ_VALID_ERR
;
1148 stl_phys(env
->vm_vmcb
+ offsetof(struct vmcb
,
1149 control
.event_inj_err
),
1152 stl_phys(env
->vm_vmcb
+ offsetof(struct vmcb
, control
.event_inj
),
1159 * Begin execution of an interruption. is_int is TRUE if coming from
1160 * the int instruction. next_eip is the env->eip value AFTER the interrupt
1161 * instruction. It is only relevant if is_int is TRUE.
1163 static void do_interrupt_all(X86CPU
*cpu
, int intno
, int is_int
,
1164 int error_code
, target_ulong next_eip
, int is_hw
)
1166 CPUX86State
*env
= &cpu
->env
;
1168 if (qemu_loglevel_mask(CPU_LOG_INT
)) {
1169 if ((env
->cr
[0] & CR0_PE_MASK
)) {
1172 qemu_log("%6d: v=%02x e=%04x i=%d cpl=%d IP=%04x:" TARGET_FMT_lx
1173 " pc=" TARGET_FMT_lx
" SP=%04x:" TARGET_FMT_lx
,
1174 count
, intno
, error_code
, is_int
,
1175 env
->hflags
& HF_CPL_MASK
,
1176 env
->segs
[R_CS
].selector
, env
->eip
,
1177 (int)env
->segs
[R_CS
].base
+ env
->eip
,
1178 env
->segs
[R_SS
].selector
, env
->regs
[R_ESP
]);
1179 if (intno
== 0x0e) {
1180 qemu_log(" CR2=" TARGET_FMT_lx
, env
->cr
[2]);
1182 qemu_log(" env->regs[R_EAX]=" TARGET_FMT_lx
, env
->regs
[R_EAX
]);
1185 log_cpu_state(CPU(cpu
), CPU_DUMP_CCOP
);
1192 ptr
= env
->segs
[R_CS
].base
+ env
->eip
;
1193 for (i
= 0; i
< 16; i
++) {
1194 qemu_log(" %02x", ldub(ptr
+ i
));
1202 if (env
->cr
[0] & CR0_PE_MASK
) {
1203 #if !defined(CONFIG_USER_ONLY)
1204 if (env
->hflags
& HF_SVMI_MASK
) {
1205 handle_even_inj(env
, intno
, is_int
, error_code
, is_hw
, 0);
1208 #ifdef TARGET_X86_64
1209 if (env
->hflags
& HF_LMA_MASK
) {
1210 do_interrupt64(env
, intno
, is_int
, error_code
, next_eip
, is_hw
);
1214 do_interrupt_protected(env
, intno
, is_int
, error_code
, next_eip
,
1218 #if !defined(CONFIG_USER_ONLY)
1219 if (env
->hflags
& HF_SVMI_MASK
) {
1220 handle_even_inj(env
, intno
, is_int
, error_code
, is_hw
, 1);
1223 do_interrupt_real(env
, intno
, is_int
, error_code
, next_eip
);
1226 #if !defined(CONFIG_USER_ONLY)
1227 if (env
->hflags
& HF_SVMI_MASK
) {
1228 uint32_t event_inj
= ldl_phys(env
->vm_vmcb
+
1229 offsetof(struct vmcb
,
1230 control
.event_inj
));
1232 stl_phys(env
->vm_vmcb
+ offsetof(struct vmcb
, control
.event_inj
),
1233 event_inj
& ~SVM_EVTINJ_VALID
);
1238 void x86_cpu_do_interrupt(CPUState
*cs
)
1240 X86CPU
*cpu
= X86_CPU(cs
);
1241 CPUX86State
*env
= &cpu
->env
;
1243 #if defined(CONFIG_USER_ONLY)
1244 /* if user mode only, we simulate a fake exception
1245 which will be handled outside the cpu execution
1247 do_interrupt_user(env
, env
->exception_index
,
1248 env
->exception_is_int
,
1250 env
->exception_next_eip
);
1251 /* successfully delivered */
1252 env
->old_exception
= -1;
1254 /* simulate a real cpu exception. On i386, it can
1255 trigger new exceptions, but we do not handle
1256 double or triple faults yet. */
1257 do_interrupt_all(cpu
, env
->exception_index
,
1258 env
->exception_is_int
,
1260 env
->exception_next_eip
, 0);
1261 /* successfully delivered */
1262 env
->old_exception
= -1;
1266 void do_interrupt_x86_hardirq(CPUX86State
*env
, int intno
, int is_hw
)
1268 do_interrupt_all(x86_env_get_cpu(env
), intno
, 0, 0, 0, is_hw
);
1271 void helper_enter_level(CPUX86State
*env
, int level
, int data32
,
1275 uint32_t esp_mask
, esp
, ebp
;
1277 esp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1278 ssp
= env
->segs
[R_SS
].base
;
1279 ebp
= env
->regs
[R_EBP
];
1280 esp
= env
->regs
[R_ESP
];
1287 cpu_stl_data(env
, ssp
+ (esp
& esp_mask
),
1288 cpu_ldl_data(env
, ssp
+ (ebp
& esp_mask
)));
1291 cpu_stl_data(env
, ssp
+ (esp
& esp_mask
), t1
);
1298 cpu_stw_data(env
, ssp
+ (esp
& esp_mask
),
1299 cpu_lduw_data(env
, ssp
+ (ebp
& esp_mask
)));
1302 cpu_stw_data(env
, ssp
+ (esp
& esp_mask
), t1
);
1306 #ifdef TARGET_X86_64
1307 void helper_enter64_level(CPUX86State
*env
, int level
, int data64
,
1310 target_ulong esp
, ebp
;
1312 ebp
= env
->regs
[R_EBP
];
1313 esp
= env
->regs
[R_ESP
];
1321 cpu_stq_data(env
, esp
, cpu_ldq_data(env
, ebp
));
1324 cpu_stq_data(env
, esp
, t1
);
1331 cpu_stw_data(env
, esp
, cpu_lduw_data(env
, ebp
));
1334 cpu_stw_data(env
, esp
, t1
);
1339 void helper_lldt(CPUX86State
*env
, int selector
)
1343 int index
, entry_limit
;
1347 if ((selector
& 0xfffc) == 0) {
1348 /* XXX: NULL selector case: invalid LDT */
1352 if (selector
& 0x4) {
1353 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1356 index
= selector
& ~7;
1357 #ifdef TARGET_X86_64
1358 if (env
->hflags
& HF_LMA_MASK
) {
1365 if ((index
+ entry_limit
) > dt
->limit
) {
1366 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1368 ptr
= dt
->base
+ index
;
1369 e1
= cpu_ldl_kernel(env
, ptr
);
1370 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
1371 if ((e2
& DESC_S_MASK
) || ((e2
>> DESC_TYPE_SHIFT
) & 0xf) != 2) {
1372 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1374 if (!(e2
& DESC_P_MASK
)) {
1375 raise_exception_err(env
, EXCP0B_NOSEG
, selector
& 0xfffc);
1377 #ifdef TARGET_X86_64
1378 if (env
->hflags
& HF_LMA_MASK
) {
1381 e3
= cpu_ldl_kernel(env
, ptr
+ 8);
1382 load_seg_cache_raw_dt(&env
->ldt
, e1
, e2
);
1383 env
->ldt
.base
|= (target_ulong
)e3
<< 32;
1387 load_seg_cache_raw_dt(&env
->ldt
, e1
, e2
);
1390 env
->ldt
.selector
= selector
;
1393 void helper_ltr(CPUX86State
*env
, int selector
)
1397 int index
, type
, entry_limit
;
1401 if ((selector
& 0xfffc) == 0) {
1402 /* NULL selector case: invalid TR */
1407 if (selector
& 0x4) {
1408 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1411 index
= selector
& ~7;
1412 #ifdef TARGET_X86_64
1413 if (env
->hflags
& HF_LMA_MASK
) {
1420 if ((index
+ entry_limit
) > dt
->limit
) {
1421 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1423 ptr
= dt
->base
+ index
;
1424 e1
= cpu_ldl_kernel(env
, ptr
);
1425 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
1426 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
1427 if ((e2
& DESC_S_MASK
) ||
1428 (type
!= 1 && type
!= 9)) {
1429 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1431 if (!(e2
& DESC_P_MASK
)) {
1432 raise_exception_err(env
, EXCP0B_NOSEG
, selector
& 0xfffc);
1434 #ifdef TARGET_X86_64
1435 if (env
->hflags
& HF_LMA_MASK
) {
1438 e3
= cpu_ldl_kernel(env
, ptr
+ 8);
1439 e4
= cpu_ldl_kernel(env
, ptr
+ 12);
1440 if ((e4
>> DESC_TYPE_SHIFT
) & 0xf) {
1441 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1443 load_seg_cache_raw_dt(&env
->tr
, e1
, e2
);
1444 env
->tr
.base
|= (target_ulong
)e3
<< 32;
1448 load_seg_cache_raw_dt(&env
->tr
, e1
, e2
);
1450 e2
|= DESC_TSS_BUSY_MASK
;
1451 cpu_stl_kernel(env
, ptr
+ 4, e2
);
1453 env
->tr
.selector
= selector
;
1456 /* only works if protected mode and not VM86. seg_reg must be != R_CS */
1457 void helper_load_seg(CPUX86State
*env
, int seg_reg
, int selector
)
1466 cpl
= env
->hflags
& HF_CPL_MASK
;
1467 if ((selector
& 0xfffc) == 0) {
1468 /* null selector case */
1470 #ifdef TARGET_X86_64
1471 && (!(env
->hflags
& HF_CS64_MASK
) || cpl
== 3)
1474 raise_exception_err(env
, EXCP0D_GPF
, 0);
1476 cpu_x86_load_seg_cache(env
, seg_reg
, selector
, 0, 0, 0);
1479 if (selector
& 0x4) {
1484 index
= selector
& ~7;
1485 if ((index
+ 7) > dt
->limit
) {
1486 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1488 ptr
= dt
->base
+ index
;
1489 e1
= cpu_ldl_kernel(env
, ptr
);
1490 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
1492 if (!(e2
& DESC_S_MASK
)) {
1493 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1496 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1497 if (seg_reg
== R_SS
) {
1498 /* must be writable segment */
1499 if ((e2
& DESC_CS_MASK
) || !(e2
& DESC_W_MASK
)) {
1500 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1502 if (rpl
!= cpl
|| dpl
!= cpl
) {
1503 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1506 /* must be readable segment */
1507 if ((e2
& (DESC_CS_MASK
| DESC_R_MASK
)) == DESC_CS_MASK
) {
1508 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1511 if (!(e2
& DESC_CS_MASK
) || !(e2
& DESC_C_MASK
)) {
1512 /* if not conforming code, test rights */
1513 if (dpl
< cpl
|| dpl
< rpl
) {
1514 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1519 if (!(e2
& DESC_P_MASK
)) {
1520 if (seg_reg
== R_SS
) {
1521 raise_exception_err(env
, EXCP0C_STACK
, selector
& 0xfffc);
1523 raise_exception_err(env
, EXCP0B_NOSEG
, selector
& 0xfffc);
1527 /* set the access bit if not already set */
1528 if (!(e2
& DESC_A_MASK
)) {
1530 cpu_stl_kernel(env
, ptr
+ 4, e2
);
1533 cpu_x86_load_seg_cache(env
, seg_reg
, selector
,
1534 get_seg_base(e1
, e2
),
1535 get_seg_limit(e1
, e2
),
1538 qemu_log("load_seg: sel=0x%04x base=0x%08lx limit=0x%08lx flags=%08x\n",
1539 selector
, (unsigned long)sc
->base
, sc
->limit
, sc
->flags
);
1544 /* protected mode jump */
1545 void helper_ljmp_protected(CPUX86State
*env
, int new_cs
, target_ulong new_eip
,
1546 int next_eip_addend
)
1549 uint32_t e1
, e2
, cpl
, dpl
, rpl
, limit
;
1550 target_ulong next_eip
;
1552 if ((new_cs
& 0xfffc) == 0) {
1553 raise_exception_err(env
, EXCP0D_GPF
, 0);
1555 if (load_segment(env
, &e1
, &e2
, new_cs
) != 0) {
1556 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1558 cpl
= env
->hflags
& HF_CPL_MASK
;
1559 if (e2
& DESC_S_MASK
) {
1560 if (!(e2
& DESC_CS_MASK
)) {
1561 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1563 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1564 if (e2
& DESC_C_MASK
) {
1565 /* conforming code segment */
1567 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1570 /* non conforming code segment */
1573 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1576 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1579 if (!(e2
& DESC_P_MASK
)) {
1580 raise_exception_err(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc);
1582 limit
= get_seg_limit(e1
, e2
);
1583 if (new_eip
> limit
&&
1584 !(env
->hflags
& HF_LMA_MASK
) && !(e2
& DESC_L_MASK
)) {
1585 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1587 cpu_x86_load_seg_cache(env
, R_CS
, (new_cs
& 0xfffc) | cpl
,
1588 get_seg_base(e1
, e2
), limit
, e2
);
1591 /* jump to call or task gate */
1592 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1594 cpl
= env
->hflags
& HF_CPL_MASK
;
1595 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
1597 case 1: /* 286 TSS */
1598 case 9: /* 386 TSS */
1599 case 5: /* task gate */
1600 if (dpl
< cpl
|| dpl
< rpl
) {
1601 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1603 next_eip
= env
->eip
+ next_eip_addend
;
1604 switch_tss(env
, new_cs
, e1
, e2
, SWITCH_TSS_JMP
, next_eip
);
1605 CC_OP
= CC_OP_EFLAGS
;
1607 case 4: /* 286 call gate */
1608 case 12: /* 386 call gate */
1609 if ((dpl
< cpl
) || (dpl
< rpl
)) {
1610 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1612 if (!(e2
& DESC_P_MASK
)) {
1613 raise_exception_err(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc);
1616 new_eip
= (e1
& 0xffff);
1618 new_eip
|= (e2
& 0xffff0000);
1620 if (load_segment(env
, &e1
, &e2
, gate_cs
) != 0) {
1621 raise_exception_err(env
, EXCP0D_GPF
, gate_cs
& 0xfffc);
1623 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1624 /* must be code segment */
1625 if (((e2
& (DESC_S_MASK
| DESC_CS_MASK
)) !=
1626 (DESC_S_MASK
| DESC_CS_MASK
))) {
1627 raise_exception_err(env
, EXCP0D_GPF
, gate_cs
& 0xfffc);
1629 if (((e2
& DESC_C_MASK
) && (dpl
> cpl
)) ||
1630 (!(e2
& DESC_C_MASK
) && (dpl
!= cpl
))) {
1631 raise_exception_err(env
, EXCP0D_GPF
, gate_cs
& 0xfffc);
1633 if (!(e2
& DESC_P_MASK
)) {
1634 raise_exception_err(env
, EXCP0D_GPF
, gate_cs
& 0xfffc);
1636 limit
= get_seg_limit(e1
, e2
);
1637 if (new_eip
> limit
) {
1638 raise_exception_err(env
, EXCP0D_GPF
, 0);
1640 cpu_x86_load_seg_cache(env
, R_CS
, (gate_cs
& 0xfffc) | cpl
,
1641 get_seg_base(e1
, e2
), limit
, e2
);
1645 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1651 /* real mode call */
1652 void helper_lcall_real(CPUX86State
*env
, int new_cs
, target_ulong new_eip1
,
1653 int shift
, int next_eip
)
1656 uint32_t esp
, esp_mask
;
1660 esp
= env
->regs
[R_ESP
];
1661 esp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1662 ssp
= env
->segs
[R_SS
].base
;
1664 PUSHL(ssp
, esp
, esp_mask
, env
->segs
[R_CS
].selector
);
1665 PUSHL(ssp
, esp
, esp_mask
, next_eip
);
1667 PUSHW(ssp
, esp
, esp_mask
, env
->segs
[R_CS
].selector
);
1668 PUSHW(ssp
, esp
, esp_mask
, next_eip
);
1671 SET_ESP(esp
, esp_mask
);
1673 env
->segs
[R_CS
].selector
= new_cs
;
1674 env
->segs
[R_CS
].base
= (new_cs
<< 4);
1677 /* protected mode call */
1678 void helper_lcall_protected(CPUX86State
*env
, int new_cs
, target_ulong new_eip
,
1679 int shift
, int next_eip_addend
)
1682 uint32_t e1
, e2
, cpl
, dpl
, rpl
, selector
, offset
, param_count
;
1683 uint32_t ss
= 0, ss_e1
= 0, ss_e2
= 0, sp
, type
, ss_dpl
, sp_mask
;
1684 uint32_t val
, limit
, old_sp_mask
;
1685 target_ulong ssp
, old_ssp
, next_eip
;
1687 next_eip
= env
->eip
+ next_eip_addend
;
1688 LOG_PCALL("lcall %04x:%08x s=%d\n", new_cs
, (uint32_t)new_eip
, shift
);
1689 LOG_PCALL_STATE(CPU(x86_env_get_cpu(env
)));
1690 if ((new_cs
& 0xfffc) == 0) {
1691 raise_exception_err(env
, EXCP0D_GPF
, 0);
1693 if (load_segment(env
, &e1
, &e2
, new_cs
) != 0) {
1694 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1696 cpl
= env
->hflags
& HF_CPL_MASK
;
1697 LOG_PCALL("desc=%08x:%08x\n", e1
, e2
);
1698 if (e2
& DESC_S_MASK
) {
1699 if (!(e2
& DESC_CS_MASK
)) {
1700 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1702 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1703 if (e2
& DESC_C_MASK
) {
1704 /* conforming code segment */
1706 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1709 /* non conforming code segment */
1712 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1715 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1718 if (!(e2
& DESC_P_MASK
)) {
1719 raise_exception_err(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc);
1722 #ifdef TARGET_X86_64
1723 /* XXX: check 16/32 bit cases in long mode */
1728 rsp
= env
->regs
[R_ESP
];
1729 PUSHQ(rsp
, env
->segs
[R_CS
].selector
);
1730 PUSHQ(rsp
, next_eip
);
1731 /* from this point, not restartable */
1732 env
->regs
[R_ESP
] = rsp
;
1733 cpu_x86_load_seg_cache(env
, R_CS
, (new_cs
& 0xfffc) | cpl
,
1734 get_seg_base(e1
, e2
),
1735 get_seg_limit(e1
, e2
), e2
);
1740 sp
= env
->regs
[R_ESP
];
1741 sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1742 ssp
= env
->segs
[R_SS
].base
;
1744 PUSHL(ssp
, sp
, sp_mask
, env
->segs
[R_CS
].selector
);
1745 PUSHL(ssp
, sp
, sp_mask
, next_eip
);
1747 PUSHW(ssp
, sp
, sp_mask
, env
->segs
[R_CS
].selector
);
1748 PUSHW(ssp
, sp
, sp_mask
, next_eip
);
1751 limit
= get_seg_limit(e1
, e2
);
1752 if (new_eip
> limit
) {
1753 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1755 /* from this point, not restartable */
1756 SET_ESP(sp
, sp_mask
);
1757 cpu_x86_load_seg_cache(env
, R_CS
, (new_cs
& 0xfffc) | cpl
,
1758 get_seg_base(e1
, e2
), limit
, e2
);
1762 /* check gate type */
1763 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x1f;
1764 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1767 case 1: /* available 286 TSS */
1768 case 9: /* available 386 TSS */
1769 case 5: /* task gate */
1770 if (dpl
< cpl
|| dpl
< rpl
) {
1771 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1773 switch_tss(env
, new_cs
, e1
, e2
, SWITCH_TSS_CALL
, next_eip
);
1774 CC_OP
= CC_OP_EFLAGS
;
1776 case 4: /* 286 call gate */
1777 case 12: /* 386 call gate */
1780 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1785 if (dpl
< cpl
|| dpl
< rpl
) {
1786 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1788 /* check valid bit */
1789 if (!(e2
& DESC_P_MASK
)) {
1790 raise_exception_err(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc);
1792 selector
= e1
>> 16;
1793 offset
= (e2
& 0xffff0000) | (e1
& 0x0000ffff);
1794 param_count
= e2
& 0x1f;
1795 if ((selector
& 0xfffc) == 0) {
1796 raise_exception_err(env
, EXCP0D_GPF
, 0);
1799 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
1800 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1802 if (!(e2
& DESC_S_MASK
) || !(e2
& (DESC_CS_MASK
))) {
1803 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1805 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1807 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1809 if (!(e2
& DESC_P_MASK
)) {
1810 raise_exception_err(env
, EXCP0B_NOSEG
, selector
& 0xfffc);
1813 if (!(e2
& DESC_C_MASK
) && dpl
< cpl
) {
1814 /* to inner privilege */
1815 get_ss_esp_from_tss(env
, &ss
, &sp
, dpl
);
1816 LOG_PCALL("new ss:esp=%04x:%08x param_count=%d env->regs[R_ESP]="
1817 TARGET_FMT_lx
"\n", ss
, sp
, param_count
,
1819 if ((ss
& 0xfffc) == 0) {
1820 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
1822 if ((ss
& 3) != dpl
) {
1823 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
1825 if (load_segment(env
, &ss_e1
, &ss_e2
, ss
) != 0) {
1826 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
1828 ss_dpl
= (ss_e2
>> DESC_DPL_SHIFT
) & 3;
1829 if (ss_dpl
!= dpl
) {
1830 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
1832 if (!(ss_e2
& DESC_S_MASK
) ||
1833 (ss_e2
& DESC_CS_MASK
) ||
1834 !(ss_e2
& DESC_W_MASK
)) {
1835 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
1837 if (!(ss_e2
& DESC_P_MASK
)) {
1838 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
1841 /* push_size = ((param_count * 2) + 8) << shift; */
1843 old_sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1844 old_ssp
= env
->segs
[R_SS
].base
;
1846 sp_mask
= get_sp_mask(ss_e2
);
1847 ssp
= get_seg_base(ss_e1
, ss_e2
);
1849 PUSHL(ssp
, sp
, sp_mask
, env
->segs
[R_SS
].selector
);
1850 PUSHL(ssp
, sp
, sp_mask
, env
->regs
[R_ESP
]);
1851 for (i
= param_count
- 1; i
>= 0; i
--) {
1852 val
= cpu_ldl_kernel(env
, old_ssp
+
1853 ((env
->regs
[R_ESP
] + i
* 4) &
1855 PUSHL(ssp
, sp
, sp_mask
, val
);
1858 PUSHW(ssp
, sp
, sp_mask
, env
->segs
[R_SS
].selector
);
1859 PUSHW(ssp
, sp
, sp_mask
, env
->regs
[R_ESP
]);
1860 for (i
= param_count
- 1; i
>= 0; i
--) {
1861 val
= cpu_lduw_kernel(env
, old_ssp
+
1862 ((env
->regs
[R_ESP
] + i
* 2) &
1864 PUSHW(ssp
, sp
, sp_mask
, val
);
1869 /* to same privilege */
1870 sp
= env
->regs
[R_ESP
];
1871 sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1872 ssp
= env
->segs
[R_SS
].base
;
1873 /* push_size = (4 << shift); */
1878 PUSHL(ssp
, sp
, sp_mask
, env
->segs
[R_CS
].selector
);
1879 PUSHL(ssp
, sp
, sp_mask
, next_eip
);
1881 PUSHW(ssp
, sp
, sp_mask
, env
->segs
[R_CS
].selector
);
1882 PUSHW(ssp
, sp
, sp_mask
, next_eip
);
1885 /* from this point, not restartable */
1888 ss
= (ss
& ~3) | dpl
;
1889 cpu_x86_load_seg_cache(env
, R_SS
, ss
,
1891 get_seg_limit(ss_e1
, ss_e2
),
1895 selector
= (selector
& ~3) | dpl
;
1896 cpu_x86_load_seg_cache(env
, R_CS
, selector
,
1897 get_seg_base(e1
, e2
),
1898 get_seg_limit(e1
, e2
),
1900 cpu_x86_set_cpl(env
, dpl
);
1901 SET_ESP(sp
, sp_mask
);
1906 /* real and vm86 mode iret */
1907 void helper_iret_real(CPUX86State
*env
, int shift
)
1909 uint32_t sp
, new_cs
, new_eip
, new_eflags
, sp_mask
;
1913 sp_mask
= 0xffff; /* XXXX: use SS segment size? */
1914 sp
= env
->regs
[R_ESP
];
1915 ssp
= env
->segs
[R_SS
].base
;
1918 POPL(ssp
, sp
, sp_mask
, new_eip
);
1919 POPL(ssp
, sp
, sp_mask
, new_cs
);
1921 POPL(ssp
, sp
, sp_mask
, new_eflags
);
1924 POPW(ssp
, sp
, sp_mask
, new_eip
);
1925 POPW(ssp
, sp
, sp_mask
, new_cs
);
1926 POPW(ssp
, sp
, sp_mask
, new_eflags
);
1928 env
->regs
[R_ESP
] = (env
->regs
[R_ESP
] & ~sp_mask
) | (sp
& sp_mask
);
1929 env
->segs
[R_CS
].selector
= new_cs
;
1930 env
->segs
[R_CS
].base
= (new_cs
<< 4);
1932 if (env
->eflags
& VM_MASK
) {
1933 eflags_mask
= TF_MASK
| AC_MASK
| ID_MASK
| IF_MASK
| RF_MASK
|
1936 eflags_mask
= TF_MASK
| AC_MASK
| ID_MASK
| IF_MASK
| IOPL_MASK
|
1940 eflags_mask
&= 0xffff;
1942 cpu_load_eflags(env
, new_eflags
, eflags_mask
);
1943 env
->hflags2
&= ~HF2_NMI_MASK
;
1946 static inline void validate_seg(CPUX86State
*env
, int seg_reg
, int cpl
)
1951 /* XXX: on x86_64, we do not want to nullify FS and GS because
1952 they may still contain a valid base. I would be interested to
1953 know how a real x86_64 CPU behaves */
1954 if ((seg_reg
== R_FS
|| seg_reg
== R_GS
) &&
1955 (env
->segs
[seg_reg
].selector
& 0xfffc) == 0) {
1959 e2
= env
->segs
[seg_reg
].flags
;
1960 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1961 if (!(e2
& DESC_CS_MASK
) || !(e2
& DESC_C_MASK
)) {
1962 /* data or non conforming code segment */
1964 cpu_x86_load_seg_cache(env
, seg_reg
, 0, 0, 0, 0);
1969 /* protected mode iret */
1970 static inline void helper_ret_protected(CPUX86State
*env
, int shift
,
1971 int is_iret
, int addend
)
1973 uint32_t new_cs
, new_eflags
, new_ss
;
1974 uint32_t new_es
, new_ds
, new_fs
, new_gs
;
1975 uint32_t e1
, e2
, ss_e1
, ss_e2
;
1976 int cpl
, dpl
, rpl
, eflags_mask
, iopl
;
1977 target_ulong ssp
, sp
, new_eip
, new_esp
, sp_mask
;
1979 #ifdef TARGET_X86_64
1985 sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1987 sp
= env
->regs
[R_ESP
];
1988 ssp
= env
->segs
[R_SS
].base
;
1989 new_eflags
= 0; /* avoid warning */
1990 #ifdef TARGET_X86_64
1996 POPQ(sp
, new_eflags
);
2003 POPL(ssp
, sp
, sp_mask
, new_eip
);
2004 POPL(ssp
, sp
, sp_mask
, new_cs
);
2007 POPL(ssp
, sp
, sp_mask
, new_eflags
);
2008 if (new_eflags
& VM_MASK
) {
2009 goto return_to_vm86
;
2014 POPW(ssp
, sp
, sp_mask
, new_eip
);
2015 POPW(ssp
, sp
, sp_mask
, new_cs
);
2017 POPW(ssp
, sp
, sp_mask
, new_eflags
);
2021 LOG_PCALL("lret new %04x:" TARGET_FMT_lx
" s=%d addend=0x%x\n",
2022 new_cs
, new_eip
, shift
, addend
);
2023 LOG_PCALL_STATE(CPU(x86_env_get_cpu(env
)));
2024 if ((new_cs
& 0xfffc) == 0) {
2025 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
2027 if (load_segment(env
, &e1
, &e2
, new_cs
) != 0) {
2028 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
2030 if (!(e2
& DESC_S_MASK
) ||
2031 !(e2
& DESC_CS_MASK
)) {
2032 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
2034 cpl
= env
->hflags
& HF_CPL_MASK
;
2037 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
2039 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
2040 if (e2
& DESC_C_MASK
) {
2042 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
2046 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
2049 if (!(e2
& DESC_P_MASK
)) {
2050 raise_exception_err(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc);
2054 if (rpl
== cpl
&& (!(env
->hflags
& HF_CS64_MASK
) ||
2055 ((env
->hflags
& HF_CS64_MASK
) && !is_iret
))) {
2056 /* return to same privilege level */
2057 cpu_x86_load_seg_cache(env
, R_CS
, new_cs
,
2058 get_seg_base(e1
, e2
),
2059 get_seg_limit(e1
, e2
),
2062 /* return to different privilege level */
2063 #ifdef TARGET_X86_64
2073 POPL(ssp
, sp
, sp_mask
, new_esp
);
2074 POPL(ssp
, sp
, sp_mask
, new_ss
);
2078 POPW(ssp
, sp
, sp_mask
, new_esp
);
2079 POPW(ssp
, sp
, sp_mask
, new_ss
);
2082 LOG_PCALL("new ss:esp=%04x:" TARGET_FMT_lx
"\n",
2084 if ((new_ss
& 0xfffc) == 0) {
2085 #ifdef TARGET_X86_64
2086 /* NULL ss is allowed in long mode if cpl != 3 */
2087 /* XXX: test CS64? */
2088 if ((env
->hflags
& HF_LMA_MASK
) && rpl
!= 3) {
2089 cpu_x86_load_seg_cache(env
, R_SS
, new_ss
,
2091 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2092 DESC_S_MASK
| (rpl
<< DESC_DPL_SHIFT
) |
2093 DESC_W_MASK
| DESC_A_MASK
);
2094 ss_e2
= DESC_B_MASK
; /* XXX: should not be needed? */
2098 raise_exception_err(env
, EXCP0D_GPF
, 0);
2101 if ((new_ss
& 3) != rpl
) {
2102 raise_exception_err(env
, EXCP0D_GPF
, new_ss
& 0xfffc);
2104 if (load_segment(env
, &ss_e1
, &ss_e2
, new_ss
) != 0) {
2105 raise_exception_err(env
, EXCP0D_GPF
, new_ss
& 0xfffc);
2107 if (!(ss_e2
& DESC_S_MASK
) ||
2108 (ss_e2
& DESC_CS_MASK
) ||
2109 !(ss_e2
& DESC_W_MASK
)) {
2110 raise_exception_err(env
, EXCP0D_GPF
, new_ss
& 0xfffc);
2112 dpl
= (ss_e2
>> DESC_DPL_SHIFT
) & 3;
2114 raise_exception_err(env
, EXCP0D_GPF
, new_ss
& 0xfffc);
2116 if (!(ss_e2
& DESC_P_MASK
)) {
2117 raise_exception_err(env
, EXCP0B_NOSEG
, new_ss
& 0xfffc);
2119 cpu_x86_load_seg_cache(env
, R_SS
, new_ss
,
2120 get_seg_base(ss_e1
, ss_e2
),
2121 get_seg_limit(ss_e1
, ss_e2
),
2125 cpu_x86_load_seg_cache(env
, R_CS
, new_cs
,
2126 get_seg_base(e1
, e2
),
2127 get_seg_limit(e1
, e2
),
2129 cpu_x86_set_cpl(env
, rpl
);
2131 #ifdef TARGET_X86_64
2132 if (env
->hflags
& HF_CS64_MASK
) {
2137 sp_mask
= get_sp_mask(ss_e2
);
2140 /* validate data segments */
2141 validate_seg(env
, R_ES
, rpl
);
2142 validate_seg(env
, R_DS
, rpl
);
2143 validate_seg(env
, R_FS
, rpl
);
2144 validate_seg(env
, R_GS
, rpl
);
2148 SET_ESP(sp
, sp_mask
);
2151 /* NOTE: 'cpl' is the _old_ CPL */
2152 eflags_mask
= TF_MASK
| AC_MASK
| ID_MASK
| RF_MASK
| NT_MASK
;
2154 eflags_mask
|= IOPL_MASK
;
2156 iopl
= (env
->eflags
>> IOPL_SHIFT
) & 3;
2158 eflags_mask
|= IF_MASK
;
2161 eflags_mask
&= 0xffff;
2163 cpu_load_eflags(env
, new_eflags
, eflags_mask
);
2168 POPL(ssp
, sp
, sp_mask
, new_esp
);
2169 POPL(ssp
, sp
, sp_mask
, new_ss
);
2170 POPL(ssp
, sp
, sp_mask
, new_es
);
2171 POPL(ssp
, sp
, sp_mask
, new_ds
);
2172 POPL(ssp
, sp
, sp_mask
, new_fs
);
2173 POPL(ssp
, sp
, sp_mask
, new_gs
);
2175 /* modify processor state */
2176 cpu_load_eflags(env
, new_eflags
, TF_MASK
| AC_MASK
| ID_MASK
|
2177 IF_MASK
| IOPL_MASK
| VM_MASK
| NT_MASK
| VIF_MASK
|
2179 load_seg_vm(env
, R_CS
, new_cs
& 0xffff);
2180 cpu_x86_set_cpl(env
, 3);
2181 load_seg_vm(env
, R_SS
, new_ss
& 0xffff);
2182 load_seg_vm(env
, R_ES
, new_es
& 0xffff);
2183 load_seg_vm(env
, R_DS
, new_ds
& 0xffff);
2184 load_seg_vm(env
, R_FS
, new_fs
& 0xffff);
2185 load_seg_vm(env
, R_GS
, new_gs
& 0xffff);
2187 env
->eip
= new_eip
& 0xffff;
2188 env
->regs
[R_ESP
] = new_esp
;
2191 void helper_iret_protected(CPUX86State
*env
, int shift
, int next_eip
)
2193 int tss_selector
, type
;
2196 /* specific case for TSS */
2197 if (env
->eflags
& NT_MASK
) {
2198 #ifdef TARGET_X86_64
2199 if (env
->hflags
& HF_LMA_MASK
) {
2200 raise_exception_err(env
, EXCP0D_GPF
, 0);
2203 tss_selector
= cpu_lduw_kernel(env
, env
->tr
.base
+ 0);
2204 if (tss_selector
& 4) {
2205 raise_exception_err(env
, EXCP0A_TSS
, tss_selector
& 0xfffc);
2207 if (load_segment(env
, &e1
, &e2
, tss_selector
) != 0) {
2208 raise_exception_err(env
, EXCP0A_TSS
, tss_selector
& 0xfffc);
2210 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x17;
2211 /* NOTE: we check both segment and busy TSS */
2213 raise_exception_err(env
, EXCP0A_TSS
, tss_selector
& 0xfffc);
2215 switch_tss(env
, tss_selector
, e1
, e2
, SWITCH_TSS_IRET
, next_eip
);
2217 helper_ret_protected(env
, shift
, 1, 0);
2219 env
->hflags2
&= ~HF2_NMI_MASK
;
2222 void helper_lret_protected(CPUX86State
*env
, int shift
, int addend
)
2224 helper_ret_protected(env
, shift
, 0, addend
);
2227 void helper_sysenter(CPUX86State
*env
)
2229 if (env
->sysenter_cs
== 0) {
2230 raise_exception_err(env
, EXCP0D_GPF
, 0);
2232 env
->eflags
&= ~(VM_MASK
| IF_MASK
| RF_MASK
);
2233 cpu_x86_set_cpl(env
, 0);
2235 #ifdef TARGET_X86_64
2236 if (env
->hflags
& HF_LMA_MASK
) {
2237 cpu_x86_load_seg_cache(env
, R_CS
, env
->sysenter_cs
& 0xfffc,
2239 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2241 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
|
2246 cpu_x86_load_seg_cache(env
, R_CS
, env
->sysenter_cs
& 0xfffc,
2248 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2250 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
);
2252 cpu_x86_load_seg_cache(env
, R_SS
, (env
->sysenter_cs
+ 8) & 0xfffc,
2254 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2256 DESC_W_MASK
| DESC_A_MASK
);
2257 env
->regs
[R_ESP
] = env
->sysenter_esp
;
2258 env
->eip
= env
->sysenter_eip
;
2261 void helper_sysexit(CPUX86State
*env
, int dflag
)
2265 cpl
= env
->hflags
& HF_CPL_MASK
;
2266 if (env
->sysenter_cs
== 0 || cpl
!= 0) {
2267 raise_exception_err(env
, EXCP0D_GPF
, 0);
2269 cpu_x86_set_cpl(env
, 3);
2270 #ifdef TARGET_X86_64
2272 cpu_x86_load_seg_cache(env
, R_CS
, ((env
->sysenter_cs
+ 32) & 0xfffc) |
2274 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2275 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
2276 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
|
2278 cpu_x86_load_seg_cache(env
, R_SS
, ((env
->sysenter_cs
+ 40) & 0xfffc) |
2280 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2281 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
2282 DESC_W_MASK
| DESC_A_MASK
);
2286 cpu_x86_load_seg_cache(env
, R_CS
, ((env
->sysenter_cs
+ 16) & 0xfffc) |
2288 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2289 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
2290 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
);
2291 cpu_x86_load_seg_cache(env
, R_SS
, ((env
->sysenter_cs
+ 24) & 0xfffc) |
2293 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2294 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
2295 DESC_W_MASK
| DESC_A_MASK
);
2297 env
->regs
[R_ESP
] = env
->regs
[R_ECX
];
2298 env
->eip
= env
->regs
[R_EDX
];
2301 target_ulong
helper_lsl(CPUX86State
*env
, target_ulong selector1
)
2304 uint32_t e1
, e2
, eflags
, selector
;
2305 int rpl
, dpl
, cpl
, type
;
2307 selector
= selector1
& 0xffff;
2308 eflags
= cpu_cc_compute_all(env
, CC_OP
);
2309 if ((selector
& 0xfffc) == 0) {
2312 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
2316 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
2317 cpl
= env
->hflags
& HF_CPL_MASK
;
2318 if (e2
& DESC_S_MASK
) {
2319 if ((e2
& DESC_CS_MASK
) && (e2
& DESC_C_MASK
)) {
2322 if (dpl
< cpl
|| dpl
< rpl
) {
2327 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
2338 if (dpl
< cpl
|| dpl
< rpl
) {
2340 CC_SRC
= eflags
& ~CC_Z
;
2344 limit
= get_seg_limit(e1
, e2
);
2345 CC_SRC
= eflags
| CC_Z
;
2349 target_ulong
helper_lar(CPUX86State
*env
, target_ulong selector1
)
2351 uint32_t e1
, e2
, eflags
, selector
;
2352 int rpl
, dpl
, cpl
, type
;
2354 selector
= selector1
& 0xffff;
2355 eflags
= cpu_cc_compute_all(env
, CC_OP
);
2356 if ((selector
& 0xfffc) == 0) {
2359 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
2363 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
2364 cpl
= env
->hflags
& HF_CPL_MASK
;
2365 if (e2
& DESC_S_MASK
) {
2366 if ((e2
& DESC_CS_MASK
) && (e2
& DESC_C_MASK
)) {
2369 if (dpl
< cpl
|| dpl
< rpl
) {
2374 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
2388 if (dpl
< cpl
|| dpl
< rpl
) {
2390 CC_SRC
= eflags
& ~CC_Z
;
2394 CC_SRC
= eflags
| CC_Z
;
2395 return e2
& 0x00f0ff00;
2398 void helper_verr(CPUX86State
*env
, target_ulong selector1
)
2400 uint32_t e1
, e2
, eflags
, selector
;
2403 selector
= selector1
& 0xffff;
2404 eflags
= cpu_cc_compute_all(env
, CC_OP
);
2405 if ((selector
& 0xfffc) == 0) {
2408 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
2411 if (!(e2
& DESC_S_MASK
)) {
2415 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
2416 cpl
= env
->hflags
& HF_CPL_MASK
;
2417 if (e2
& DESC_CS_MASK
) {
2418 if (!(e2
& DESC_R_MASK
)) {
2421 if (!(e2
& DESC_C_MASK
)) {
2422 if (dpl
< cpl
|| dpl
< rpl
) {
2427 if (dpl
< cpl
|| dpl
< rpl
) {
2429 CC_SRC
= eflags
& ~CC_Z
;
2433 CC_SRC
= eflags
| CC_Z
;
2436 void helper_verw(CPUX86State
*env
, target_ulong selector1
)
2438 uint32_t e1
, e2
, eflags
, selector
;
2441 selector
= selector1
& 0xffff;
2442 eflags
= cpu_cc_compute_all(env
, CC_OP
);
2443 if ((selector
& 0xfffc) == 0) {
2446 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
2449 if (!(e2
& DESC_S_MASK
)) {
2453 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
2454 cpl
= env
->hflags
& HF_CPL_MASK
;
2455 if (e2
& DESC_CS_MASK
) {
2458 if (dpl
< cpl
|| dpl
< rpl
) {
2461 if (!(e2
& DESC_W_MASK
)) {
2463 CC_SRC
= eflags
& ~CC_Z
;
2467 CC_SRC
= eflags
| CC_Z
;
2470 #if defined(CONFIG_USER_ONLY)
2471 void cpu_x86_load_seg(CPUX86State
*env
, int seg_reg
, int selector
)
2473 if (!(env
->cr
[0] & CR0_PE_MASK
) || (env
->eflags
& VM_MASK
)) {
2475 cpu_x86_load_seg_cache(env
, seg_reg
, selector
,
2476 (selector
<< 4), 0xffff, 0);
2478 helper_load_seg(env
, seg_reg
, selector
);