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 X86CPU
*cpu
= x86_env_get_cpu(env
);
99 int type
, index
, shift
;
104 printf("TR: base=%p limit=%x\n", env
->tr
.base
, env
->tr
.limit
);
105 for (i
= 0; i
< env
->tr
.limit
; i
++) {
106 printf("%02x ", env
->tr
.base
[i
]);
115 if (!(env
->tr
.flags
& DESC_P_MASK
)) {
116 cpu_abort(CPU(cpu
), "invalid tss");
118 type
= (env
->tr
.flags
>> DESC_TYPE_SHIFT
) & 0xf;
119 if ((type
& 7) != 1) {
120 cpu_abort(CPU(cpu
), "invalid tss type");
123 index
= (dpl
* 4 + 2) << shift
;
124 if (index
+ (4 << shift
) - 1 > env
->tr
.limit
) {
125 raise_exception_err(env
, EXCP0A_TSS
, env
->tr
.selector
& 0xfffc);
128 *esp_ptr
= cpu_lduw_kernel(env
, env
->tr
.base
+ index
);
129 *ss_ptr
= cpu_lduw_kernel(env
, env
->tr
.base
+ index
+ 2);
131 *esp_ptr
= cpu_ldl_kernel(env
, env
->tr
.base
+ index
);
132 *ss_ptr
= cpu_lduw_kernel(env
, env
->tr
.base
+ index
+ 4);
136 /* XXX: merge with load_seg() */
137 static void tss_load_seg(CPUX86State
*env
, int seg_reg
, int selector
)
142 if ((selector
& 0xfffc) != 0) {
143 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
144 raise_exception_err(env
, EXCP0A_TSS
, selector
& 0xfffc);
146 if (!(e2
& DESC_S_MASK
)) {
147 raise_exception_err(env
, EXCP0A_TSS
, selector
& 0xfffc);
150 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
151 cpl
= env
->hflags
& HF_CPL_MASK
;
152 if (seg_reg
== R_CS
) {
153 if (!(e2
& DESC_CS_MASK
)) {
154 raise_exception_err(env
, EXCP0A_TSS
, selector
& 0xfffc);
156 /* XXX: is it correct? */
158 raise_exception_err(env
, EXCP0A_TSS
, selector
& 0xfffc);
160 if ((e2
& DESC_C_MASK
) && dpl
> rpl
) {
161 raise_exception_err(env
, EXCP0A_TSS
, selector
& 0xfffc);
163 } else if (seg_reg
== R_SS
) {
164 /* SS must be writable data */
165 if ((e2
& DESC_CS_MASK
) || !(e2
& DESC_W_MASK
)) {
166 raise_exception_err(env
, EXCP0A_TSS
, selector
& 0xfffc);
168 if (dpl
!= cpl
|| dpl
!= rpl
) {
169 raise_exception_err(env
, EXCP0A_TSS
, selector
& 0xfffc);
172 /* not readable code */
173 if ((e2
& DESC_CS_MASK
) && !(e2
& DESC_R_MASK
)) {
174 raise_exception_err(env
, EXCP0A_TSS
, selector
& 0xfffc);
176 /* if data or non conforming code, checks the rights */
177 if (((e2
>> DESC_TYPE_SHIFT
) & 0xf) < 12) {
178 if (dpl
< cpl
|| dpl
< rpl
) {
179 raise_exception_err(env
, EXCP0A_TSS
, selector
& 0xfffc);
183 if (!(e2
& DESC_P_MASK
)) {
184 raise_exception_err(env
, EXCP0B_NOSEG
, selector
& 0xfffc);
186 cpu_x86_load_seg_cache(env
, seg_reg
, selector
,
187 get_seg_base(e1
, e2
),
188 get_seg_limit(e1
, e2
),
191 if (seg_reg
== R_SS
|| seg_reg
== R_CS
) {
192 raise_exception_err(env
, EXCP0A_TSS
, selector
& 0xfffc);
197 #define SWITCH_TSS_JMP 0
198 #define SWITCH_TSS_IRET 1
199 #define SWITCH_TSS_CALL 2
201 /* XXX: restore CPU state in registers (PowerPC case) */
202 static void switch_tss(CPUX86State
*env
, int tss_selector
,
203 uint32_t e1
, uint32_t e2
, int source
,
206 int tss_limit
, tss_limit_max
, type
, old_tss_limit_max
, old_type
, v1
, v2
, i
;
207 target_ulong tss_base
;
208 uint32_t new_regs
[8], new_segs
[6];
209 uint32_t new_eflags
, new_eip
, new_cr3
, new_ldt
, new_trap
;
210 uint32_t old_eflags
, eflags_mask
;
215 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
216 LOG_PCALL("switch_tss: sel=0x%04x type=%d src=%d\n", tss_selector
, type
,
219 /* if task gate, we read the TSS segment and we load it */
221 if (!(e2
& DESC_P_MASK
)) {
222 raise_exception_err(env
, EXCP0B_NOSEG
, tss_selector
& 0xfffc);
224 tss_selector
= e1
>> 16;
225 if (tss_selector
& 4) {
226 raise_exception_err(env
, EXCP0A_TSS
, tss_selector
& 0xfffc);
228 if (load_segment(env
, &e1
, &e2
, tss_selector
) != 0) {
229 raise_exception_err(env
, EXCP0D_GPF
, tss_selector
& 0xfffc);
231 if (e2
& DESC_S_MASK
) {
232 raise_exception_err(env
, EXCP0D_GPF
, tss_selector
& 0xfffc);
234 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
235 if ((type
& 7) != 1) {
236 raise_exception_err(env
, EXCP0D_GPF
, tss_selector
& 0xfffc);
240 if (!(e2
& DESC_P_MASK
)) {
241 raise_exception_err(env
, EXCP0B_NOSEG
, tss_selector
& 0xfffc);
249 tss_limit
= get_seg_limit(e1
, e2
);
250 tss_base
= get_seg_base(e1
, e2
);
251 if ((tss_selector
& 4) != 0 ||
252 tss_limit
< tss_limit_max
) {
253 raise_exception_err(env
, EXCP0A_TSS
, tss_selector
& 0xfffc);
255 old_type
= (env
->tr
.flags
>> DESC_TYPE_SHIFT
) & 0xf;
257 old_tss_limit_max
= 103;
259 old_tss_limit_max
= 43;
262 /* read all the registers from the new TSS */
265 new_cr3
= cpu_ldl_kernel(env
, tss_base
+ 0x1c);
266 new_eip
= cpu_ldl_kernel(env
, tss_base
+ 0x20);
267 new_eflags
= cpu_ldl_kernel(env
, tss_base
+ 0x24);
268 for (i
= 0; i
< 8; i
++) {
269 new_regs
[i
] = cpu_ldl_kernel(env
, tss_base
+ (0x28 + i
* 4));
271 for (i
= 0; i
< 6; i
++) {
272 new_segs
[i
] = cpu_lduw_kernel(env
, tss_base
+ (0x48 + i
* 4));
274 new_ldt
= cpu_lduw_kernel(env
, tss_base
+ 0x60);
275 new_trap
= cpu_ldl_kernel(env
, tss_base
+ 0x64);
279 new_eip
= cpu_lduw_kernel(env
, tss_base
+ 0x0e);
280 new_eflags
= cpu_lduw_kernel(env
, tss_base
+ 0x10);
281 for (i
= 0; i
< 8; i
++) {
282 new_regs
[i
] = cpu_lduw_kernel(env
, tss_base
+ (0x12 + i
* 2)) |
285 for (i
= 0; i
< 4; i
++) {
286 new_segs
[i
] = cpu_lduw_kernel(env
, tss_base
+ (0x22 + i
* 4));
288 new_ldt
= cpu_lduw_kernel(env
, tss_base
+ 0x2a);
293 /* XXX: avoid a compiler warning, see
294 http://support.amd.com/us/Processor_TechDocs/24593.pdf
295 chapters 12.2.5 and 13.2.4 on how to implement TSS Trap bit */
298 /* NOTE: we must avoid memory exceptions during the task switch,
299 so we make dummy accesses before */
300 /* XXX: it can still fail in some cases, so a bigger hack is
301 necessary to valid the TLB after having done the accesses */
303 v1
= cpu_ldub_kernel(env
, env
->tr
.base
);
304 v2
= cpu_ldub_kernel(env
, env
->tr
.base
+ old_tss_limit_max
);
305 cpu_stb_kernel(env
, env
->tr
.base
, v1
);
306 cpu_stb_kernel(env
, env
->tr
.base
+ old_tss_limit_max
, v2
);
308 /* clear busy bit (it is restartable) */
309 if (source
== SWITCH_TSS_JMP
|| source
== SWITCH_TSS_IRET
) {
313 ptr
= env
->gdt
.base
+ (env
->tr
.selector
& ~7);
314 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
315 e2
&= ~DESC_TSS_BUSY_MASK
;
316 cpu_stl_kernel(env
, ptr
+ 4, e2
);
318 old_eflags
= cpu_compute_eflags(env
);
319 if (source
== SWITCH_TSS_IRET
) {
320 old_eflags
&= ~NT_MASK
;
323 /* save the current state in the old TSS */
326 cpu_stl_kernel(env
, env
->tr
.base
+ 0x20, next_eip
);
327 cpu_stl_kernel(env
, env
->tr
.base
+ 0x24, old_eflags
);
328 cpu_stl_kernel(env
, env
->tr
.base
+ (0x28 + 0 * 4), env
->regs
[R_EAX
]);
329 cpu_stl_kernel(env
, env
->tr
.base
+ (0x28 + 1 * 4), env
->regs
[R_ECX
]);
330 cpu_stl_kernel(env
, env
->tr
.base
+ (0x28 + 2 * 4), env
->regs
[R_EDX
]);
331 cpu_stl_kernel(env
, env
->tr
.base
+ (0x28 + 3 * 4), env
->regs
[R_EBX
]);
332 cpu_stl_kernel(env
, env
->tr
.base
+ (0x28 + 4 * 4), env
->regs
[R_ESP
]);
333 cpu_stl_kernel(env
, env
->tr
.base
+ (0x28 + 5 * 4), env
->regs
[R_EBP
]);
334 cpu_stl_kernel(env
, env
->tr
.base
+ (0x28 + 6 * 4), env
->regs
[R_ESI
]);
335 cpu_stl_kernel(env
, env
->tr
.base
+ (0x28 + 7 * 4), env
->regs
[R_EDI
]);
336 for (i
= 0; i
< 6; i
++) {
337 cpu_stw_kernel(env
, env
->tr
.base
+ (0x48 + i
* 4),
338 env
->segs
[i
].selector
);
342 cpu_stw_kernel(env
, env
->tr
.base
+ 0x0e, next_eip
);
343 cpu_stw_kernel(env
, env
->tr
.base
+ 0x10, old_eflags
);
344 cpu_stw_kernel(env
, env
->tr
.base
+ (0x12 + 0 * 2), env
->regs
[R_EAX
]);
345 cpu_stw_kernel(env
, env
->tr
.base
+ (0x12 + 1 * 2), env
->regs
[R_ECX
]);
346 cpu_stw_kernel(env
, env
->tr
.base
+ (0x12 + 2 * 2), env
->regs
[R_EDX
]);
347 cpu_stw_kernel(env
, env
->tr
.base
+ (0x12 + 3 * 2), env
->regs
[R_EBX
]);
348 cpu_stw_kernel(env
, env
->tr
.base
+ (0x12 + 4 * 2), env
->regs
[R_ESP
]);
349 cpu_stw_kernel(env
, env
->tr
.base
+ (0x12 + 5 * 2), env
->regs
[R_EBP
]);
350 cpu_stw_kernel(env
, env
->tr
.base
+ (0x12 + 6 * 2), env
->regs
[R_ESI
]);
351 cpu_stw_kernel(env
, env
->tr
.base
+ (0x12 + 7 * 2), env
->regs
[R_EDI
]);
352 for (i
= 0; i
< 4; i
++) {
353 cpu_stw_kernel(env
, env
->tr
.base
+ (0x22 + i
* 4),
354 env
->segs
[i
].selector
);
358 /* now if an exception occurs, it will occurs in the next task
361 if (source
== SWITCH_TSS_CALL
) {
362 cpu_stw_kernel(env
, tss_base
, env
->tr
.selector
);
363 new_eflags
|= NT_MASK
;
367 if (source
== SWITCH_TSS_JMP
|| source
== SWITCH_TSS_CALL
) {
371 ptr
= env
->gdt
.base
+ (tss_selector
& ~7);
372 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
373 e2
|= DESC_TSS_BUSY_MASK
;
374 cpu_stl_kernel(env
, ptr
+ 4, e2
);
377 /* set the new CPU state */
378 /* from this point, any exception which occurs can give problems */
379 env
->cr
[0] |= CR0_TS_MASK
;
380 env
->hflags
|= HF_TS_MASK
;
381 env
->tr
.selector
= tss_selector
;
382 env
->tr
.base
= tss_base
;
383 env
->tr
.limit
= tss_limit
;
384 env
->tr
.flags
= e2
& ~DESC_TSS_BUSY_MASK
;
386 if ((type
& 8) && (env
->cr
[0] & CR0_PG_MASK
)) {
387 cpu_x86_update_cr3(env
, new_cr3
);
390 /* load all registers without an exception, then reload them with
391 possible exception */
393 eflags_mask
= TF_MASK
| AC_MASK
| ID_MASK
|
394 IF_MASK
| IOPL_MASK
| VM_MASK
| RF_MASK
| NT_MASK
;
396 eflags_mask
&= 0xffff;
398 cpu_load_eflags(env
, new_eflags
, eflags_mask
);
399 /* XXX: what to do in 16 bit case? */
400 env
->regs
[R_EAX
] = new_regs
[0];
401 env
->regs
[R_ECX
] = new_regs
[1];
402 env
->regs
[R_EDX
] = new_regs
[2];
403 env
->regs
[R_EBX
] = new_regs
[3];
404 env
->regs
[R_ESP
] = new_regs
[4];
405 env
->regs
[R_EBP
] = new_regs
[5];
406 env
->regs
[R_ESI
] = new_regs
[6];
407 env
->regs
[R_EDI
] = new_regs
[7];
408 if (new_eflags
& VM_MASK
) {
409 for (i
= 0; i
< 6; i
++) {
410 load_seg_vm(env
, i
, new_segs
[i
]);
413 /* first just selectors as the rest may trigger exceptions */
414 for (i
= 0; i
< 6; i
++) {
415 cpu_x86_load_seg_cache(env
, i
, new_segs
[i
], 0, 0, 0);
419 env
->ldt
.selector
= new_ldt
& ~4;
426 raise_exception_err(env
, EXCP0A_TSS
, new_ldt
& 0xfffc);
429 if ((new_ldt
& 0xfffc) != 0) {
431 index
= new_ldt
& ~7;
432 if ((index
+ 7) > dt
->limit
) {
433 raise_exception_err(env
, EXCP0A_TSS
, new_ldt
& 0xfffc);
435 ptr
= dt
->base
+ index
;
436 e1
= cpu_ldl_kernel(env
, ptr
);
437 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
438 if ((e2
& DESC_S_MASK
) || ((e2
>> DESC_TYPE_SHIFT
) & 0xf) != 2) {
439 raise_exception_err(env
, EXCP0A_TSS
, new_ldt
& 0xfffc);
441 if (!(e2
& DESC_P_MASK
)) {
442 raise_exception_err(env
, EXCP0A_TSS
, new_ldt
& 0xfffc);
444 load_seg_cache_raw_dt(&env
->ldt
, e1
, e2
);
447 /* load the segments */
448 if (!(new_eflags
& VM_MASK
)) {
449 tss_load_seg(env
, R_CS
, new_segs
[R_CS
]);
450 tss_load_seg(env
, R_SS
, new_segs
[R_SS
]);
451 tss_load_seg(env
, R_ES
, new_segs
[R_ES
]);
452 tss_load_seg(env
, R_DS
, new_segs
[R_DS
]);
453 tss_load_seg(env
, R_FS
, new_segs
[R_FS
]);
454 tss_load_seg(env
, R_GS
, new_segs
[R_GS
]);
457 /* check that env->eip is in the CS segment limits */
458 if (new_eip
> env
->segs
[R_CS
].limit
) {
459 /* XXX: different exception if CALL? */
460 raise_exception_err(env
, EXCP0D_GPF
, 0);
463 #ifndef CONFIG_USER_ONLY
464 /* reset local breakpoints */
465 if (env
->dr
[7] & DR7_LOCAL_BP_MASK
) {
466 for (i
= 0; i
< DR7_MAX_BP
; i
++) {
467 if (hw_local_breakpoint_enabled(env
->dr
[7], i
) &&
468 !hw_global_breakpoint_enabled(env
->dr
[7], i
)) {
469 hw_breakpoint_remove(env
, i
);
472 env
->dr
[7] &= ~DR7_LOCAL_BP_MASK
;
477 static inline unsigned int get_sp_mask(unsigned int e2
)
479 if (e2
& DESC_B_MASK
) {
486 static int exception_has_error_code(int intno
)
502 #define SET_ESP(val, sp_mask) \
504 if ((sp_mask) == 0xffff) { \
505 env->regs[R_ESP] = (env->regs[R_ESP] & ~0xffff) | \
507 } else if ((sp_mask) == 0xffffffffLL) { \
508 env->regs[R_ESP] = (uint32_t)(val); \
510 env->regs[R_ESP] = (val); \
514 #define SET_ESP(val, sp_mask) \
516 env->regs[R_ESP] = (env->regs[R_ESP] & ~(sp_mask)) | \
517 ((val) & (sp_mask)); \
521 /* in 64-bit machines, this can overflow. So this segment addition macro
522 * can be used to trim the value to 32-bit whenever needed */
523 #define SEG_ADDL(ssp, sp, sp_mask) ((uint32_t)((ssp) + (sp & (sp_mask))))
525 /* XXX: add a is_user flag to have proper security support */
526 #define PUSHW(ssp, sp, sp_mask, val) \
529 cpu_stw_kernel(env, (ssp) + (sp & (sp_mask)), (val)); \
532 #define PUSHL(ssp, sp, sp_mask, val) \
535 cpu_stl_kernel(env, SEG_ADDL(ssp, sp, sp_mask), (uint32_t)(val)); \
538 #define POPW(ssp, sp, sp_mask, val) \
540 val = cpu_lduw_kernel(env, (ssp) + (sp & (sp_mask))); \
544 #define POPL(ssp, sp, sp_mask, val) \
546 val = (uint32_t)cpu_ldl_kernel(env, SEG_ADDL(ssp, sp, sp_mask)); \
550 /* protected mode interrupt */
551 static void do_interrupt_protected(CPUX86State
*env
, int intno
, int is_int
,
552 int error_code
, unsigned int next_eip
,
556 target_ulong ptr
, ssp
;
557 int type
, dpl
, selector
, ss_dpl
, cpl
;
558 int has_error_code
, new_stack
, shift
;
559 uint32_t e1
, e2
, offset
, ss
= 0, esp
, ss_e1
= 0, ss_e2
= 0;
560 uint32_t old_eip
, sp_mask
;
563 if (!is_int
&& !is_hw
) {
564 has_error_code
= exception_has_error_code(intno
);
573 if (intno
* 8 + 7 > dt
->limit
) {
574 raise_exception_err(env
, EXCP0D_GPF
, intno
* 8 + 2);
576 ptr
= dt
->base
+ intno
* 8;
577 e1
= cpu_ldl_kernel(env
, ptr
);
578 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
579 /* check gate type */
580 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x1f;
582 case 5: /* task gate */
583 /* must do that check here to return the correct error code */
584 if (!(e2
& DESC_P_MASK
)) {
585 raise_exception_err(env
, EXCP0B_NOSEG
, intno
* 8 + 2);
587 switch_tss(env
, intno
* 8, e1
, e2
, SWITCH_TSS_CALL
, old_eip
);
588 if (has_error_code
) {
592 /* push the error code */
593 type
= (env
->tr
.flags
>> DESC_TYPE_SHIFT
) & 0xf;
595 if (env
->segs
[R_SS
].flags
& DESC_B_MASK
) {
600 esp
= (env
->regs
[R_ESP
] - (2 << shift
)) & mask
;
601 ssp
= env
->segs
[R_SS
].base
+ esp
;
603 cpu_stl_kernel(env
, ssp
, error_code
);
605 cpu_stw_kernel(env
, ssp
, error_code
);
610 case 6: /* 286 interrupt gate */
611 case 7: /* 286 trap gate */
612 case 14: /* 386 interrupt gate */
613 case 15: /* 386 trap gate */
616 raise_exception_err(env
, EXCP0D_GPF
, intno
* 8 + 2);
619 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
620 cpl
= env
->hflags
& HF_CPL_MASK
;
621 /* check privilege if software int */
622 if (is_int
&& dpl
< cpl
) {
623 raise_exception_err(env
, EXCP0D_GPF
, intno
* 8 + 2);
625 /* check valid bit */
626 if (!(e2
& DESC_P_MASK
)) {
627 raise_exception_err(env
, EXCP0B_NOSEG
, intno
* 8 + 2);
630 offset
= (e2
& 0xffff0000) | (e1
& 0x0000ffff);
631 if ((selector
& 0xfffc) == 0) {
632 raise_exception_err(env
, EXCP0D_GPF
, 0);
634 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
635 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
637 if (!(e2
& DESC_S_MASK
) || !(e2
& (DESC_CS_MASK
))) {
638 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
640 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
642 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
644 if (!(e2
& DESC_P_MASK
)) {
645 raise_exception_err(env
, EXCP0B_NOSEG
, selector
& 0xfffc);
647 if (!(e2
& DESC_C_MASK
) && dpl
< cpl
) {
648 /* to inner privilege */
649 get_ss_esp_from_tss(env
, &ss
, &esp
, dpl
);
650 if ((ss
& 0xfffc) == 0) {
651 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
653 if ((ss
& 3) != dpl
) {
654 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
656 if (load_segment(env
, &ss_e1
, &ss_e2
, ss
) != 0) {
657 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
659 ss_dpl
= (ss_e2
>> DESC_DPL_SHIFT
) & 3;
661 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
663 if (!(ss_e2
& DESC_S_MASK
) ||
664 (ss_e2
& DESC_CS_MASK
) ||
665 !(ss_e2
& DESC_W_MASK
)) {
666 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
668 if (!(ss_e2
& DESC_P_MASK
)) {
669 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
672 sp_mask
= get_sp_mask(ss_e2
);
673 ssp
= get_seg_base(ss_e1
, ss_e2
);
674 } else if ((e2
& DESC_C_MASK
) || dpl
== cpl
) {
675 /* to same privilege */
676 if (env
->eflags
& VM_MASK
) {
677 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
680 sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
681 ssp
= env
->segs
[R_SS
].base
;
682 esp
= env
->regs
[R_ESP
];
685 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
686 new_stack
= 0; /* avoid warning */
687 sp_mask
= 0; /* avoid warning */
688 ssp
= 0; /* avoid warning */
689 esp
= 0; /* avoid warning */
695 /* XXX: check that enough room is available */
696 push_size
= 6 + (new_stack
<< 2) + (has_error_code
<< 1);
697 if (env
->eflags
& VM_MASK
) {
704 if (env
->eflags
& VM_MASK
) {
705 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_GS
].selector
);
706 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_FS
].selector
);
707 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_DS
].selector
);
708 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_ES
].selector
);
710 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_SS
].selector
);
711 PUSHL(ssp
, esp
, sp_mask
, env
->regs
[R_ESP
]);
713 PUSHL(ssp
, esp
, sp_mask
, cpu_compute_eflags(env
));
714 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_CS
].selector
);
715 PUSHL(ssp
, esp
, sp_mask
, old_eip
);
716 if (has_error_code
) {
717 PUSHL(ssp
, esp
, sp_mask
, error_code
);
721 if (env
->eflags
& VM_MASK
) {
722 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_GS
].selector
);
723 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_FS
].selector
);
724 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_DS
].selector
);
725 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_ES
].selector
);
727 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_SS
].selector
);
728 PUSHW(ssp
, esp
, sp_mask
, env
->regs
[R_ESP
]);
730 PUSHW(ssp
, esp
, sp_mask
, cpu_compute_eflags(env
));
731 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_CS
].selector
);
732 PUSHW(ssp
, esp
, sp_mask
, old_eip
);
733 if (has_error_code
) {
734 PUSHW(ssp
, esp
, sp_mask
, error_code
);
738 /* interrupt gate clear IF mask */
739 if ((type
& 1) == 0) {
740 env
->eflags
&= ~IF_MASK
;
742 env
->eflags
&= ~(TF_MASK
| VM_MASK
| RF_MASK
| NT_MASK
);
745 if (env
->eflags
& VM_MASK
) {
746 cpu_x86_load_seg_cache(env
, R_ES
, 0, 0, 0, 0);
747 cpu_x86_load_seg_cache(env
, R_DS
, 0, 0, 0, 0);
748 cpu_x86_load_seg_cache(env
, R_FS
, 0, 0, 0, 0);
749 cpu_x86_load_seg_cache(env
, R_GS
, 0, 0, 0, 0);
751 ss
= (ss
& ~3) | dpl
;
752 cpu_x86_load_seg_cache(env
, R_SS
, ss
,
753 ssp
, get_seg_limit(ss_e1
, ss_e2
), ss_e2
);
755 SET_ESP(esp
, sp_mask
);
757 selector
= (selector
& ~3) | dpl
;
758 cpu_x86_load_seg_cache(env
, R_CS
, selector
,
759 get_seg_base(e1
, e2
),
760 get_seg_limit(e1
, e2
),
767 #define PUSHQ(sp, val) \
770 cpu_stq_kernel(env, sp, (val)); \
773 #define POPQ(sp, val) \
775 val = cpu_ldq_kernel(env, sp); \
779 static inline target_ulong
get_rsp_from_tss(CPUX86State
*env
, int level
)
781 X86CPU
*cpu
= x86_env_get_cpu(env
);
785 printf("TR: base=" TARGET_FMT_lx
" limit=%x\n",
786 env
->tr
.base
, env
->tr
.limit
);
789 if (!(env
->tr
.flags
& DESC_P_MASK
)) {
790 cpu_abort(CPU(cpu
), "invalid tss");
792 index
= 8 * level
+ 4;
793 if ((index
+ 7) > env
->tr
.limit
) {
794 raise_exception_err(env
, EXCP0A_TSS
, env
->tr
.selector
& 0xfffc);
796 return cpu_ldq_kernel(env
, env
->tr
.base
+ index
);
799 /* 64 bit interrupt */
800 static void do_interrupt64(CPUX86State
*env
, int intno
, int is_int
,
801 int error_code
, target_ulong next_eip
, int is_hw
)
805 int type
, dpl
, selector
, cpl
, ist
;
806 int has_error_code
, new_stack
;
807 uint32_t e1
, e2
, e3
, ss
;
808 target_ulong old_eip
, esp
, offset
;
811 if (!is_int
&& !is_hw
) {
812 has_error_code
= exception_has_error_code(intno
);
821 if (intno
* 16 + 15 > dt
->limit
) {
822 raise_exception_err(env
, EXCP0D_GPF
, intno
* 16 + 2);
824 ptr
= dt
->base
+ intno
* 16;
825 e1
= cpu_ldl_kernel(env
, ptr
);
826 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
827 e3
= cpu_ldl_kernel(env
, ptr
+ 8);
828 /* check gate type */
829 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x1f;
831 case 14: /* 386 interrupt gate */
832 case 15: /* 386 trap gate */
835 raise_exception_err(env
, EXCP0D_GPF
, intno
* 16 + 2);
838 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
839 cpl
= env
->hflags
& HF_CPL_MASK
;
840 /* check privilege if software int */
841 if (is_int
&& dpl
< cpl
) {
842 raise_exception_err(env
, EXCP0D_GPF
, intno
* 16 + 2);
844 /* check valid bit */
845 if (!(e2
& DESC_P_MASK
)) {
846 raise_exception_err(env
, EXCP0B_NOSEG
, intno
* 16 + 2);
849 offset
= ((target_ulong
)e3
<< 32) | (e2
& 0xffff0000) | (e1
& 0x0000ffff);
851 if ((selector
& 0xfffc) == 0) {
852 raise_exception_err(env
, EXCP0D_GPF
, 0);
855 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
856 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
858 if (!(e2
& DESC_S_MASK
) || !(e2
& (DESC_CS_MASK
))) {
859 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
861 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
863 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
865 if (!(e2
& DESC_P_MASK
)) {
866 raise_exception_err(env
, EXCP0B_NOSEG
, selector
& 0xfffc);
868 if (!(e2
& DESC_L_MASK
) || (e2
& DESC_B_MASK
)) {
869 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
871 if ((!(e2
& DESC_C_MASK
) && dpl
< cpl
) || ist
!= 0) {
872 /* to inner privilege */
874 esp
= get_rsp_from_tss(env
, ist
+ 3);
876 esp
= get_rsp_from_tss(env
, dpl
);
878 esp
&= ~0xfLL
; /* align stack */
881 } else if ((e2
& DESC_C_MASK
) || dpl
== cpl
) {
882 /* to same privilege */
883 if (env
->eflags
& VM_MASK
) {
884 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
888 esp
= get_rsp_from_tss(env
, ist
+ 3);
890 esp
= env
->regs
[R_ESP
];
892 esp
&= ~0xfLL
; /* align stack */
895 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
896 new_stack
= 0; /* avoid warning */
897 esp
= 0; /* avoid warning */
900 PUSHQ(esp
, env
->segs
[R_SS
].selector
);
901 PUSHQ(esp
, env
->regs
[R_ESP
]);
902 PUSHQ(esp
, cpu_compute_eflags(env
));
903 PUSHQ(esp
, env
->segs
[R_CS
].selector
);
905 if (has_error_code
) {
906 PUSHQ(esp
, error_code
);
909 /* interrupt gate clear IF mask */
910 if ((type
& 1) == 0) {
911 env
->eflags
&= ~IF_MASK
;
913 env
->eflags
&= ~(TF_MASK
| VM_MASK
| RF_MASK
| NT_MASK
);
917 cpu_x86_load_seg_cache(env
, R_SS
, ss
, 0, 0, 0);
919 env
->regs
[R_ESP
] = esp
;
921 selector
= (selector
& ~3) | dpl
;
922 cpu_x86_load_seg_cache(env
, R_CS
, selector
,
923 get_seg_base(e1
, e2
),
924 get_seg_limit(e1
, e2
),
931 #if defined(CONFIG_USER_ONLY)
932 void helper_syscall(CPUX86State
*env
, int next_eip_addend
)
934 CPUState
*cs
= CPU(x86_env_get_cpu(env
));
936 cs
->exception_index
= EXCP_SYSCALL
;
937 env
->exception_next_eip
= env
->eip
+ next_eip_addend
;
941 void helper_syscall(CPUX86State
*env
, int next_eip_addend
)
945 if (!(env
->efer
& MSR_EFER_SCE
)) {
946 raise_exception_err(env
, EXCP06_ILLOP
, 0);
948 selector
= (env
->star
>> 32) & 0xffff;
949 if (env
->hflags
& HF_LMA_MASK
) {
952 env
->regs
[R_ECX
] = env
->eip
+ next_eip_addend
;
953 env
->regs
[11] = cpu_compute_eflags(env
);
955 code64
= env
->hflags
& HF_CS64_MASK
;
957 env
->eflags
&= ~env
->fmask
;
958 cpu_load_eflags(env
, env
->eflags
, 0);
959 cpu_x86_load_seg_cache(env
, R_CS
, selector
& 0xfffc,
961 DESC_G_MASK
| DESC_P_MASK
|
963 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
|
965 cpu_x86_load_seg_cache(env
, R_SS
, (selector
+ 8) & 0xfffc,
967 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
969 DESC_W_MASK
| DESC_A_MASK
);
971 env
->eip
= env
->lstar
;
973 env
->eip
= env
->cstar
;
976 env
->regs
[R_ECX
] = (uint32_t)(env
->eip
+ next_eip_addend
);
978 env
->eflags
&= ~(IF_MASK
| RF_MASK
| VM_MASK
);
979 cpu_x86_load_seg_cache(env
, R_CS
, selector
& 0xfffc,
981 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
983 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
);
984 cpu_x86_load_seg_cache(env
, R_SS
, (selector
+ 8) & 0xfffc,
986 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
988 DESC_W_MASK
| DESC_A_MASK
);
989 env
->eip
= (uint32_t)env
->star
;
996 void helper_sysret(CPUX86State
*env
, int dflag
)
1000 if (!(env
->efer
& MSR_EFER_SCE
)) {
1001 raise_exception_err(env
, EXCP06_ILLOP
, 0);
1003 cpl
= env
->hflags
& HF_CPL_MASK
;
1004 if (!(env
->cr
[0] & CR0_PE_MASK
) || cpl
!= 0) {
1005 raise_exception_err(env
, EXCP0D_GPF
, 0);
1007 selector
= (env
->star
>> 48) & 0xffff;
1008 if (env
->hflags
& HF_LMA_MASK
) {
1009 cpu_load_eflags(env
, (uint32_t)(env
->regs
[11]), TF_MASK
| AC_MASK
1010 | ID_MASK
| IF_MASK
| IOPL_MASK
| VM_MASK
| RF_MASK
|
1013 cpu_x86_load_seg_cache(env
, R_CS
, (selector
+ 16) | 3,
1015 DESC_G_MASK
| DESC_P_MASK
|
1016 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
1017 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
|
1019 env
->eip
= env
->regs
[R_ECX
];
1021 cpu_x86_load_seg_cache(env
, R_CS
, selector
| 3,
1023 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
1024 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
1025 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
);
1026 env
->eip
= (uint32_t)env
->regs
[R_ECX
];
1028 cpu_x86_load_seg_cache(env
, R_SS
, selector
+ 8,
1030 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
1031 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
1032 DESC_W_MASK
| DESC_A_MASK
);
1034 env
->eflags
|= IF_MASK
;
1035 cpu_x86_load_seg_cache(env
, R_CS
, selector
| 3,
1037 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
1038 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
1039 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
);
1040 env
->eip
= (uint32_t)env
->regs
[R_ECX
];
1041 cpu_x86_load_seg_cache(env
, R_SS
, selector
+ 8,
1043 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
1044 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
1045 DESC_W_MASK
| DESC_A_MASK
);
1050 /* real mode interrupt */
1051 static void do_interrupt_real(CPUX86State
*env
, int intno
, int is_int
,
1052 int error_code
, unsigned int next_eip
)
1055 target_ulong ptr
, ssp
;
1057 uint32_t offset
, esp
;
1058 uint32_t old_cs
, old_eip
;
1060 /* real mode (simpler!) */
1062 if (intno
* 4 + 3 > dt
->limit
) {
1063 raise_exception_err(env
, EXCP0D_GPF
, intno
* 8 + 2);
1065 ptr
= dt
->base
+ intno
* 4;
1066 offset
= cpu_lduw_kernel(env
, ptr
);
1067 selector
= cpu_lduw_kernel(env
, ptr
+ 2);
1068 esp
= env
->regs
[R_ESP
];
1069 ssp
= env
->segs
[R_SS
].base
;
1075 old_cs
= env
->segs
[R_CS
].selector
;
1076 /* XXX: use SS segment size? */
1077 PUSHW(ssp
, esp
, 0xffff, cpu_compute_eflags(env
));
1078 PUSHW(ssp
, esp
, 0xffff, old_cs
);
1079 PUSHW(ssp
, esp
, 0xffff, old_eip
);
1081 /* update processor state */
1082 env
->regs
[R_ESP
] = (env
->regs
[R_ESP
] & ~0xffff) | (esp
& 0xffff);
1084 env
->segs
[R_CS
].selector
= selector
;
1085 env
->segs
[R_CS
].base
= (selector
<< 4);
1086 env
->eflags
&= ~(IF_MASK
| TF_MASK
| AC_MASK
| RF_MASK
);
1089 #if defined(CONFIG_USER_ONLY)
1090 /* fake user mode interrupt */
1091 static void do_interrupt_user(CPUX86State
*env
, int intno
, int is_int
,
1092 int error_code
, target_ulong next_eip
)
1096 int dpl
, cpl
, shift
;
1100 if (env
->hflags
& HF_LMA_MASK
) {
1105 ptr
= dt
->base
+ (intno
<< shift
);
1106 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
1108 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1109 cpl
= env
->hflags
& HF_CPL_MASK
;
1110 /* check privilege if software int */
1111 if (is_int
&& dpl
< cpl
) {
1112 raise_exception_err(env
, EXCP0D_GPF
, (intno
<< shift
) + 2);
1115 /* Since we emulate only user space, we cannot do more than
1116 exiting the emulation with the suitable exception and error
1119 env
->eip
= next_eip
;
1125 static void handle_even_inj(CPUX86State
*env
, int intno
, int is_int
,
1126 int error_code
, int is_hw
, int rm
)
1128 CPUState
*cs
= CPU(x86_env_get_cpu(env
));
1129 uint32_t event_inj
= ldl_phys(cs
->as
, env
->vm_vmcb
+ offsetof(struct vmcb
,
1130 control
.event_inj
));
1132 if (!(event_inj
& SVM_EVTINJ_VALID
)) {
1136 type
= SVM_EVTINJ_TYPE_SOFT
;
1138 type
= SVM_EVTINJ_TYPE_EXEPT
;
1140 event_inj
= intno
| type
| SVM_EVTINJ_VALID
;
1141 if (!rm
&& exception_has_error_code(intno
)) {
1142 event_inj
|= SVM_EVTINJ_VALID_ERR
;
1143 stl_phys(cs
->as
, env
->vm_vmcb
+ offsetof(struct vmcb
,
1144 control
.event_inj_err
),
1148 env
->vm_vmcb
+ offsetof(struct vmcb
, control
.event_inj
),
1155 * Begin execution of an interruption. is_int is TRUE if coming from
1156 * the int instruction. next_eip is the env->eip value AFTER the interrupt
1157 * instruction. It is only relevant if is_int is TRUE.
1159 static void do_interrupt_all(X86CPU
*cpu
, int intno
, int is_int
,
1160 int error_code
, target_ulong next_eip
, int is_hw
)
1162 CPUX86State
*env
= &cpu
->env
;
1164 if (qemu_loglevel_mask(CPU_LOG_INT
)) {
1165 if ((env
->cr
[0] & CR0_PE_MASK
)) {
1168 qemu_log("%6d: v=%02x e=%04x i=%d cpl=%d IP=%04x:" TARGET_FMT_lx
1169 " pc=" TARGET_FMT_lx
" SP=%04x:" TARGET_FMT_lx
,
1170 count
, intno
, error_code
, is_int
,
1171 env
->hflags
& HF_CPL_MASK
,
1172 env
->segs
[R_CS
].selector
, env
->eip
,
1173 (int)env
->segs
[R_CS
].base
+ env
->eip
,
1174 env
->segs
[R_SS
].selector
, env
->regs
[R_ESP
]);
1175 if (intno
== 0x0e) {
1176 qemu_log(" CR2=" TARGET_FMT_lx
, env
->cr
[2]);
1178 qemu_log(" env->regs[R_EAX]=" TARGET_FMT_lx
, env
->regs
[R_EAX
]);
1181 log_cpu_state(CPU(cpu
), CPU_DUMP_CCOP
);
1188 ptr
= env
->segs
[R_CS
].base
+ env
->eip
;
1189 for (i
= 0; i
< 16; i
++) {
1190 qemu_log(" %02x", ldub(ptr
+ i
));
1198 if (env
->cr
[0] & CR0_PE_MASK
) {
1199 #if !defined(CONFIG_USER_ONLY)
1200 if (env
->hflags
& HF_SVMI_MASK
) {
1201 handle_even_inj(env
, intno
, is_int
, error_code
, is_hw
, 0);
1204 #ifdef TARGET_X86_64
1205 if (env
->hflags
& HF_LMA_MASK
) {
1206 do_interrupt64(env
, intno
, is_int
, error_code
, next_eip
, is_hw
);
1210 do_interrupt_protected(env
, intno
, is_int
, error_code
, next_eip
,
1214 #if !defined(CONFIG_USER_ONLY)
1215 if (env
->hflags
& HF_SVMI_MASK
) {
1216 handle_even_inj(env
, intno
, is_int
, error_code
, is_hw
, 1);
1219 do_interrupt_real(env
, intno
, is_int
, error_code
, next_eip
);
1222 #if !defined(CONFIG_USER_ONLY)
1223 if (env
->hflags
& HF_SVMI_MASK
) {
1224 CPUState
*cs
= CPU(cpu
);
1225 uint32_t event_inj
= ldl_phys(cs
->as
, env
->vm_vmcb
+
1226 offsetof(struct vmcb
,
1227 control
.event_inj
));
1230 env
->vm_vmcb
+ offsetof(struct vmcb
, control
.event_inj
),
1231 event_inj
& ~SVM_EVTINJ_VALID
);
1236 void x86_cpu_do_interrupt(CPUState
*cs
)
1238 X86CPU
*cpu
= X86_CPU(cs
);
1239 CPUX86State
*env
= &cpu
->env
;
1241 #if defined(CONFIG_USER_ONLY)
1242 /* if user mode only, we simulate a fake exception
1243 which will be handled outside the cpu execution
1245 do_interrupt_user(env
, cs
->exception_index
,
1246 env
->exception_is_int
,
1248 env
->exception_next_eip
);
1249 /* successfully delivered */
1250 env
->old_exception
= -1;
1252 /* simulate a real cpu exception. On i386, it can
1253 trigger new exceptions, but we do not handle
1254 double or triple faults yet. */
1255 do_interrupt_all(cpu
, cs
->exception_index
,
1256 env
->exception_is_int
,
1258 env
->exception_next_eip
, 0);
1259 /* successfully delivered */
1260 env
->old_exception
= -1;
1264 void do_interrupt_x86_hardirq(CPUX86State
*env
, int intno
, int is_hw
)
1266 do_interrupt_all(x86_env_get_cpu(env
), intno
, 0, 0, 0, is_hw
);
1269 void helper_enter_level(CPUX86State
*env
, int level
, int data32
,
1273 uint32_t esp_mask
, esp
, ebp
;
1275 esp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1276 ssp
= env
->segs
[R_SS
].base
;
1277 ebp
= env
->regs
[R_EBP
];
1278 esp
= env
->regs
[R_ESP
];
1285 cpu_stl_data(env
, ssp
+ (esp
& esp_mask
),
1286 cpu_ldl_data(env
, ssp
+ (ebp
& esp_mask
)));
1289 cpu_stl_data(env
, ssp
+ (esp
& esp_mask
), t1
);
1296 cpu_stw_data(env
, ssp
+ (esp
& esp_mask
),
1297 cpu_lduw_data(env
, ssp
+ (ebp
& esp_mask
)));
1300 cpu_stw_data(env
, ssp
+ (esp
& esp_mask
), t1
);
1304 #ifdef TARGET_X86_64
1305 void helper_enter64_level(CPUX86State
*env
, int level
, int data64
,
1308 target_ulong esp
, ebp
;
1310 ebp
= env
->regs
[R_EBP
];
1311 esp
= env
->regs
[R_ESP
];
1319 cpu_stq_data(env
, esp
, cpu_ldq_data(env
, ebp
));
1322 cpu_stq_data(env
, esp
, t1
);
1329 cpu_stw_data(env
, esp
, cpu_lduw_data(env
, ebp
));
1332 cpu_stw_data(env
, esp
, t1
);
1337 void helper_lldt(CPUX86State
*env
, int selector
)
1341 int index
, entry_limit
;
1345 if ((selector
& 0xfffc) == 0) {
1346 /* XXX: NULL selector case: invalid LDT */
1350 if (selector
& 0x4) {
1351 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1354 index
= selector
& ~7;
1355 #ifdef TARGET_X86_64
1356 if (env
->hflags
& HF_LMA_MASK
) {
1363 if ((index
+ entry_limit
) > dt
->limit
) {
1364 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1366 ptr
= dt
->base
+ index
;
1367 e1
= cpu_ldl_kernel(env
, ptr
);
1368 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
1369 if ((e2
& DESC_S_MASK
) || ((e2
>> DESC_TYPE_SHIFT
) & 0xf) != 2) {
1370 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1372 if (!(e2
& DESC_P_MASK
)) {
1373 raise_exception_err(env
, EXCP0B_NOSEG
, selector
& 0xfffc);
1375 #ifdef TARGET_X86_64
1376 if (env
->hflags
& HF_LMA_MASK
) {
1379 e3
= cpu_ldl_kernel(env
, ptr
+ 8);
1380 load_seg_cache_raw_dt(&env
->ldt
, e1
, e2
);
1381 env
->ldt
.base
|= (target_ulong
)e3
<< 32;
1385 load_seg_cache_raw_dt(&env
->ldt
, e1
, e2
);
1388 env
->ldt
.selector
= selector
;
1391 void helper_ltr(CPUX86State
*env
, int selector
)
1395 int index
, type
, entry_limit
;
1399 if ((selector
& 0xfffc) == 0) {
1400 /* NULL selector case: invalid TR */
1405 if (selector
& 0x4) {
1406 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1409 index
= selector
& ~7;
1410 #ifdef TARGET_X86_64
1411 if (env
->hflags
& HF_LMA_MASK
) {
1418 if ((index
+ entry_limit
) > dt
->limit
) {
1419 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1421 ptr
= dt
->base
+ index
;
1422 e1
= cpu_ldl_kernel(env
, ptr
);
1423 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
1424 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
1425 if ((e2
& DESC_S_MASK
) ||
1426 (type
!= 1 && type
!= 9)) {
1427 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1429 if (!(e2
& DESC_P_MASK
)) {
1430 raise_exception_err(env
, EXCP0B_NOSEG
, selector
& 0xfffc);
1432 #ifdef TARGET_X86_64
1433 if (env
->hflags
& HF_LMA_MASK
) {
1436 e3
= cpu_ldl_kernel(env
, ptr
+ 8);
1437 e4
= cpu_ldl_kernel(env
, ptr
+ 12);
1438 if ((e4
>> DESC_TYPE_SHIFT
) & 0xf) {
1439 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1441 load_seg_cache_raw_dt(&env
->tr
, e1
, e2
);
1442 env
->tr
.base
|= (target_ulong
)e3
<< 32;
1446 load_seg_cache_raw_dt(&env
->tr
, e1
, e2
);
1448 e2
|= DESC_TSS_BUSY_MASK
;
1449 cpu_stl_kernel(env
, ptr
+ 4, e2
);
1451 env
->tr
.selector
= selector
;
1454 /* only works if protected mode and not VM86. seg_reg must be != R_CS */
1455 void helper_load_seg(CPUX86State
*env
, int seg_reg
, int selector
)
1464 cpl
= env
->hflags
& HF_CPL_MASK
;
1465 if ((selector
& 0xfffc) == 0) {
1466 /* null selector case */
1468 #ifdef TARGET_X86_64
1469 && (!(env
->hflags
& HF_CS64_MASK
) || cpl
== 3)
1472 raise_exception_err(env
, EXCP0D_GPF
, 0);
1474 cpu_x86_load_seg_cache(env
, seg_reg
, selector
, 0, 0, 0);
1477 if (selector
& 0x4) {
1482 index
= selector
& ~7;
1483 if ((index
+ 7) > dt
->limit
) {
1484 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1486 ptr
= dt
->base
+ index
;
1487 e1
= cpu_ldl_kernel(env
, ptr
);
1488 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
1490 if (!(e2
& DESC_S_MASK
)) {
1491 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1494 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1495 if (seg_reg
== R_SS
) {
1496 /* must be writable segment */
1497 if ((e2
& DESC_CS_MASK
) || !(e2
& DESC_W_MASK
)) {
1498 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1500 if (rpl
!= cpl
|| dpl
!= cpl
) {
1501 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1504 /* must be readable segment */
1505 if ((e2
& (DESC_CS_MASK
| DESC_R_MASK
)) == DESC_CS_MASK
) {
1506 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1509 if (!(e2
& DESC_CS_MASK
) || !(e2
& DESC_C_MASK
)) {
1510 /* if not conforming code, test rights */
1511 if (dpl
< cpl
|| dpl
< rpl
) {
1512 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1517 if (!(e2
& DESC_P_MASK
)) {
1518 if (seg_reg
== R_SS
) {
1519 raise_exception_err(env
, EXCP0C_STACK
, selector
& 0xfffc);
1521 raise_exception_err(env
, EXCP0B_NOSEG
, selector
& 0xfffc);
1525 /* set the access bit if not already set */
1526 if (!(e2
& DESC_A_MASK
)) {
1528 cpu_stl_kernel(env
, ptr
+ 4, e2
);
1531 cpu_x86_load_seg_cache(env
, seg_reg
, selector
,
1532 get_seg_base(e1
, e2
),
1533 get_seg_limit(e1
, e2
),
1536 qemu_log("load_seg: sel=0x%04x base=0x%08lx limit=0x%08lx flags=%08x\n",
1537 selector
, (unsigned long)sc
->base
, sc
->limit
, sc
->flags
);
1542 /* protected mode jump */
1543 void helper_ljmp_protected(CPUX86State
*env
, int new_cs
, target_ulong new_eip
,
1544 int next_eip_addend
)
1547 uint32_t e1
, e2
, cpl
, dpl
, rpl
, limit
;
1548 target_ulong next_eip
;
1550 if ((new_cs
& 0xfffc) == 0) {
1551 raise_exception_err(env
, EXCP0D_GPF
, 0);
1553 if (load_segment(env
, &e1
, &e2
, new_cs
) != 0) {
1554 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1556 cpl
= env
->hflags
& HF_CPL_MASK
;
1557 if (e2
& DESC_S_MASK
) {
1558 if (!(e2
& DESC_CS_MASK
)) {
1559 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1561 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1562 if (e2
& DESC_C_MASK
) {
1563 /* conforming code segment */
1565 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1568 /* non conforming code segment */
1571 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1574 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1577 if (!(e2
& DESC_P_MASK
)) {
1578 raise_exception_err(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc);
1580 limit
= get_seg_limit(e1
, e2
);
1581 if (new_eip
> limit
&&
1582 !(env
->hflags
& HF_LMA_MASK
) && !(e2
& DESC_L_MASK
)) {
1583 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1585 cpu_x86_load_seg_cache(env
, R_CS
, (new_cs
& 0xfffc) | cpl
,
1586 get_seg_base(e1
, e2
), limit
, e2
);
1589 /* jump to call or task gate */
1590 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1592 cpl
= env
->hflags
& HF_CPL_MASK
;
1593 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
1595 case 1: /* 286 TSS */
1596 case 9: /* 386 TSS */
1597 case 5: /* task gate */
1598 if (dpl
< cpl
|| dpl
< rpl
) {
1599 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1601 next_eip
= env
->eip
+ next_eip_addend
;
1602 switch_tss(env
, new_cs
, e1
, e2
, SWITCH_TSS_JMP
, next_eip
);
1603 CC_OP
= CC_OP_EFLAGS
;
1605 case 4: /* 286 call gate */
1606 case 12: /* 386 call gate */
1607 if ((dpl
< cpl
) || (dpl
< rpl
)) {
1608 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1610 if (!(e2
& DESC_P_MASK
)) {
1611 raise_exception_err(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc);
1614 new_eip
= (e1
& 0xffff);
1616 new_eip
|= (e2
& 0xffff0000);
1618 if (load_segment(env
, &e1
, &e2
, gate_cs
) != 0) {
1619 raise_exception_err(env
, EXCP0D_GPF
, gate_cs
& 0xfffc);
1621 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1622 /* must be code segment */
1623 if (((e2
& (DESC_S_MASK
| DESC_CS_MASK
)) !=
1624 (DESC_S_MASK
| DESC_CS_MASK
))) {
1625 raise_exception_err(env
, EXCP0D_GPF
, gate_cs
& 0xfffc);
1627 if (((e2
& DESC_C_MASK
) && (dpl
> cpl
)) ||
1628 (!(e2
& DESC_C_MASK
) && (dpl
!= cpl
))) {
1629 raise_exception_err(env
, EXCP0D_GPF
, gate_cs
& 0xfffc);
1631 if (!(e2
& DESC_P_MASK
)) {
1632 raise_exception_err(env
, EXCP0D_GPF
, gate_cs
& 0xfffc);
1634 limit
= get_seg_limit(e1
, e2
);
1635 if (new_eip
> limit
) {
1636 raise_exception_err(env
, EXCP0D_GPF
, 0);
1638 cpu_x86_load_seg_cache(env
, R_CS
, (gate_cs
& 0xfffc) | cpl
,
1639 get_seg_base(e1
, e2
), limit
, e2
);
1643 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1649 /* real mode call */
1650 void helper_lcall_real(CPUX86State
*env
, int new_cs
, target_ulong new_eip1
,
1651 int shift
, int next_eip
)
1654 uint32_t esp
, esp_mask
;
1658 esp
= env
->regs
[R_ESP
];
1659 esp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1660 ssp
= env
->segs
[R_SS
].base
;
1662 PUSHL(ssp
, esp
, esp_mask
, env
->segs
[R_CS
].selector
);
1663 PUSHL(ssp
, esp
, esp_mask
, next_eip
);
1665 PUSHW(ssp
, esp
, esp_mask
, env
->segs
[R_CS
].selector
);
1666 PUSHW(ssp
, esp
, esp_mask
, next_eip
);
1669 SET_ESP(esp
, esp_mask
);
1671 env
->segs
[R_CS
].selector
= new_cs
;
1672 env
->segs
[R_CS
].base
= (new_cs
<< 4);
1675 /* protected mode call */
1676 void helper_lcall_protected(CPUX86State
*env
, int new_cs
, target_ulong new_eip
,
1677 int shift
, int next_eip_addend
)
1680 uint32_t e1
, e2
, cpl
, dpl
, rpl
, selector
, offset
, param_count
;
1681 uint32_t ss
= 0, ss_e1
= 0, ss_e2
= 0, sp
, type
, ss_dpl
, sp_mask
;
1682 uint32_t val
, limit
, old_sp_mask
;
1683 target_ulong ssp
, old_ssp
, next_eip
;
1685 next_eip
= env
->eip
+ next_eip_addend
;
1686 LOG_PCALL("lcall %04x:%08x s=%d\n", new_cs
, (uint32_t)new_eip
, shift
);
1687 LOG_PCALL_STATE(CPU(x86_env_get_cpu(env
)));
1688 if ((new_cs
& 0xfffc) == 0) {
1689 raise_exception_err(env
, EXCP0D_GPF
, 0);
1691 if (load_segment(env
, &e1
, &e2
, new_cs
) != 0) {
1692 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1694 cpl
= env
->hflags
& HF_CPL_MASK
;
1695 LOG_PCALL("desc=%08x:%08x\n", e1
, e2
);
1696 if (e2
& DESC_S_MASK
) {
1697 if (!(e2
& DESC_CS_MASK
)) {
1698 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1700 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1701 if (e2
& DESC_C_MASK
) {
1702 /* conforming code segment */
1704 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1707 /* non conforming code segment */
1710 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1713 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1716 if (!(e2
& DESC_P_MASK
)) {
1717 raise_exception_err(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc);
1720 #ifdef TARGET_X86_64
1721 /* XXX: check 16/32 bit cases in long mode */
1726 rsp
= env
->regs
[R_ESP
];
1727 PUSHQ(rsp
, env
->segs
[R_CS
].selector
);
1728 PUSHQ(rsp
, next_eip
);
1729 /* from this point, not restartable */
1730 env
->regs
[R_ESP
] = rsp
;
1731 cpu_x86_load_seg_cache(env
, R_CS
, (new_cs
& 0xfffc) | cpl
,
1732 get_seg_base(e1
, e2
),
1733 get_seg_limit(e1
, e2
), e2
);
1738 sp
= env
->regs
[R_ESP
];
1739 sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1740 ssp
= env
->segs
[R_SS
].base
;
1742 PUSHL(ssp
, sp
, sp_mask
, env
->segs
[R_CS
].selector
);
1743 PUSHL(ssp
, sp
, sp_mask
, next_eip
);
1745 PUSHW(ssp
, sp
, sp_mask
, env
->segs
[R_CS
].selector
);
1746 PUSHW(ssp
, sp
, sp_mask
, next_eip
);
1749 limit
= get_seg_limit(e1
, e2
);
1750 if (new_eip
> limit
) {
1751 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1753 /* from this point, not restartable */
1754 SET_ESP(sp
, sp_mask
);
1755 cpu_x86_load_seg_cache(env
, R_CS
, (new_cs
& 0xfffc) | cpl
,
1756 get_seg_base(e1
, e2
), limit
, e2
);
1760 /* check gate type */
1761 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x1f;
1762 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1765 case 1: /* available 286 TSS */
1766 case 9: /* available 386 TSS */
1767 case 5: /* task gate */
1768 if (dpl
< cpl
|| dpl
< rpl
) {
1769 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1771 switch_tss(env
, new_cs
, e1
, e2
, SWITCH_TSS_CALL
, next_eip
);
1772 CC_OP
= CC_OP_EFLAGS
;
1774 case 4: /* 286 call gate */
1775 case 12: /* 386 call gate */
1778 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1783 if (dpl
< cpl
|| dpl
< rpl
) {
1784 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1786 /* check valid bit */
1787 if (!(e2
& DESC_P_MASK
)) {
1788 raise_exception_err(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc);
1790 selector
= e1
>> 16;
1791 offset
= (e2
& 0xffff0000) | (e1
& 0x0000ffff);
1792 param_count
= e2
& 0x1f;
1793 if ((selector
& 0xfffc) == 0) {
1794 raise_exception_err(env
, EXCP0D_GPF
, 0);
1797 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
1798 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1800 if (!(e2
& DESC_S_MASK
) || !(e2
& (DESC_CS_MASK
))) {
1801 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1803 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1805 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1807 if (!(e2
& DESC_P_MASK
)) {
1808 raise_exception_err(env
, EXCP0B_NOSEG
, selector
& 0xfffc);
1811 if (!(e2
& DESC_C_MASK
) && dpl
< cpl
) {
1812 /* to inner privilege */
1813 get_ss_esp_from_tss(env
, &ss
, &sp
, dpl
);
1814 LOG_PCALL("new ss:esp=%04x:%08x param_count=%d env->regs[R_ESP]="
1815 TARGET_FMT_lx
"\n", ss
, sp
, param_count
,
1817 if ((ss
& 0xfffc) == 0) {
1818 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
1820 if ((ss
& 3) != dpl
) {
1821 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
1823 if (load_segment(env
, &ss_e1
, &ss_e2
, ss
) != 0) {
1824 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
1826 ss_dpl
= (ss_e2
>> DESC_DPL_SHIFT
) & 3;
1827 if (ss_dpl
!= dpl
) {
1828 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
1830 if (!(ss_e2
& DESC_S_MASK
) ||
1831 (ss_e2
& DESC_CS_MASK
) ||
1832 !(ss_e2
& DESC_W_MASK
)) {
1833 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
1835 if (!(ss_e2
& DESC_P_MASK
)) {
1836 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
1839 /* push_size = ((param_count * 2) + 8) << shift; */
1841 old_sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1842 old_ssp
= env
->segs
[R_SS
].base
;
1844 sp_mask
= get_sp_mask(ss_e2
);
1845 ssp
= get_seg_base(ss_e1
, ss_e2
);
1847 PUSHL(ssp
, sp
, sp_mask
, env
->segs
[R_SS
].selector
);
1848 PUSHL(ssp
, sp
, sp_mask
, env
->regs
[R_ESP
]);
1849 for (i
= param_count
- 1; i
>= 0; i
--) {
1850 val
= cpu_ldl_kernel(env
, old_ssp
+
1851 ((env
->regs
[R_ESP
] + i
* 4) &
1853 PUSHL(ssp
, sp
, sp_mask
, val
);
1856 PUSHW(ssp
, sp
, sp_mask
, env
->segs
[R_SS
].selector
);
1857 PUSHW(ssp
, sp
, sp_mask
, env
->regs
[R_ESP
]);
1858 for (i
= param_count
- 1; i
>= 0; i
--) {
1859 val
= cpu_lduw_kernel(env
, old_ssp
+
1860 ((env
->regs
[R_ESP
] + i
* 2) &
1862 PUSHW(ssp
, sp
, sp_mask
, val
);
1867 /* to same privilege */
1868 sp
= env
->regs
[R_ESP
];
1869 sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1870 ssp
= env
->segs
[R_SS
].base
;
1871 /* push_size = (4 << shift); */
1876 PUSHL(ssp
, sp
, sp_mask
, env
->segs
[R_CS
].selector
);
1877 PUSHL(ssp
, sp
, sp_mask
, next_eip
);
1879 PUSHW(ssp
, sp
, sp_mask
, env
->segs
[R_CS
].selector
);
1880 PUSHW(ssp
, sp
, sp_mask
, next_eip
);
1883 /* from this point, not restartable */
1886 ss
= (ss
& ~3) | dpl
;
1887 cpu_x86_load_seg_cache(env
, R_SS
, ss
,
1889 get_seg_limit(ss_e1
, ss_e2
),
1893 selector
= (selector
& ~3) | dpl
;
1894 cpu_x86_load_seg_cache(env
, R_CS
, selector
,
1895 get_seg_base(e1
, e2
),
1896 get_seg_limit(e1
, e2
),
1898 SET_ESP(sp
, sp_mask
);
1903 /* real and vm86 mode iret */
1904 void helper_iret_real(CPUX86State
*env
, int shift
)
1906 uint32_t sp
, new_cs
, new_eip
, new_eflags
, sp_mask
;
1910 sp_mask
= 0xffff; /* XXXX: use SS segment size? */
1911 sp
= env
->regs
[R_ESP
];
1912 ssp
= env
->segs
[R_SS
].base
;
1915 POPL(ssp
, sp
, sp_mask
, new_eip
);
1916 POPL(ssp
, sp
, sp_mask
, new_cs
);
1918 POPL(ssp
, sp
, sp_mask
, new_eflags
);
1921 POPW(ssp
, sp
, sp_mask
, new_eip
);
1922 POPW(ssp
, sp
, sp_mask
, new_cs
);
1923 POPW(ssp
, sp
, sp_mask
, new_eflags
);
1925 env
->regs
[R_ESP
] = (env
->regs
[R_ESP
] & ~sp_mask
) | (sp
& sp_mask
);
1926 env
->segs
[R_CS
].selector
= new_cs
;
1927 env
->segs
[R_CS
].base
= (new_cs
<< 4);
1929 if (env
->eflags
& VM_MASK
) {
1930 eflags_mask
= TF_MASK
| AC_MASK
| ID_MASK
| IF_MASK
| RF_MASK
|
1933 eflags_mask
= TF_MASK
| AC_MASK
| ID_MASK
| IF_MASK
| IOPL_MASK
|
1937 eflags_mask
&= 0xffff;
1939 cpu_load_eflags(env
, new_eflags
, eflags_mask
);
1940 env
->hflags2
&= ~HF2_NMI_MASK
;
1943 static inline void validate_seg(CPUX86State
*env
, int seg_reg
, int cpl
)
1948 /* XXX: on x86_64, we do not want to nullify FS and GS because
1949 they may still contain a valid base. I would be interested to
1950 know how a real x86_64 CPU behaves */
1951 if ((seg_reg
== R_FS
|| seg_reg
== R_GS
) &&
1952 (env
->segs
[seg_reg
].selector
& 0xfffc) == 0) {
1956 e2
= env
->segs
[seg_reg
].flags
;
1957 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1958 if (!(e2
& DESC_CS_MASK
) || !(e2
& DESC_C_MASK
)) {
1959 /* data or non conforming code segment */
1961 cpu_x86_load_seg_cache(env
, seg_reg
, 0, 0, 0, 0);
1966 /* protected mode iret */
1967 static inline void helper_ret_protected(CPUX86State
*env
, int shift
,
1968 int is_iret
, int addend
)
1970 uint32_t new_cs
, new_eflags
, new_ss
;
1971 uint32_t new_es
, new_ds
, new_fs
, new_gs
;
1972 uint32_t e1
, e2
, ss_e1
, ss_e2
;
1973 int cpl
, dpl
, rpl
, eflags_mask
, iopl
;
1974 target_ulong ssp
, sp
, new_eip
, new_esp
, sp_mask
;
1976 #ifdef TARGET_X86_64
1982 sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1984 sp
= env
->regs
[R_ESP
];
1985 ssp
= env
->segs
[R_SS
].base
;
1986 new_eflags
= 0; /* avoid warning */
1987 #ifdef TARGET_X86_64
1993 POPQ(sp
, new_eflags
);
2000 POPL(ssp
, sp
, sp_mask
, new_eip
);
2001 POPL(ssp
, sp
, sp_mask
, new_cs
);
2004 POPL(ssp
, sp
, sp_mask
, new_eflags
);
2005 if (new_eflags
& VM_MASK
) {
2006 goto return_to_vm86
;
2011 POPW(ssp
, sp
, sp_mask
, new_eip
);
2012 POPW(ssp
, sp
, sp_mask
, new_cs
);
2014 POPW(ssp
, sp
, sp_mask
, new_eflags
);
2018 LOG_PCALL("lret new %04x:" TARGET_FMT_lx
" s=%d addend=0x%x\n",
2019 new_cs
, new_eip
, shift
, addend
);
2020 LOG_PCALL_STATE(CPU(x86_env_get_cpu(env
)));
2021 if ((new_cs
& 0xfffc) == 0) {
2022 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
2024 if (load_segment(env
, &e1
, &e2
, new_cs
) != 0) {
2025 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
2027 if (!(e2
& DESC_S_MASK
) ||
2028 !(e2
& DESC_CS_MASK
)) {
2029 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
2031 cpl
= env
->hflags
& HF_CPL_MASK
;
2034 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
2036 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
2037 if (e2
& DESC_C_MASK
) {
2039 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
2043 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
2046 if (!(e2
& DESC_P_MASK
)) {
2047 raise_exception_err(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc);
2051 if (rpl
== cpl
&& (!(env
->hflags
& HF_CS64_MASK
) ||
2052 ((env
->hflags
& HF_CS64_MASK
) && !is_iret
))) {
2053 /* return to same privilege level */
2054 cpu_x86_load_seg_cache(env
, R_CS
, new_cs
,
2055 get_seg_base(e1
, e2
),
2056 get_seg_limit(e1
, e2
),
2059 /* return to different privilege level */
2060 #ifdef TARGET_X86_64
2070 POPL(ssp
, sp
, sp_mask
, new_esp
);
2071 POPL(ssp
, sp
, sp_mask
, new_ss
);
2075 POPW(ssp
, sp
, sp_mask
, new_esp
);
2076 POPW(ssp
, sp
, sp_mask
, new_ss
);
2079 LOG_PCALL("new ss:esp=%04x:" TARGET_FMT_lx
"\n",
2081 if ((new_ss
& 0xfffc) == 0) {
2082 #ifdef TARGET_X86_64
2083 /* NULL ss is allowed in long mode if cpl != 3 */
2084 /* XXX: test CS64? */
2085 if ((env
->hflags
& HF_LMA_MASK
) && rpl
!= 3) {
2086 cpu_x86_load_seg_cache(env
, R_SS
, new_ss
,
2088 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2089 DESC_S_MASK
| (rpl
<< DESC_DPL_SHIFT
) |
2090 DESC_W_MASK
| DESC_A_MASK
);
2091 ss_e2
= DESC_B_MASK
; /* XXX: should not be needed? */
2095 raise_exception_err(env
, EXCP0D_GPF
, 0);
2098 if ((new_ss
& 3) != rpl
) {
2099 raise_exception_err(env
, EXCP0D_GPF
, new_ss
& 0xfffc);
2101 if (load_segment(env
, &ss_e1
, &ss_e2
, new_ss
) != 0) {
2102 raise_exception_err(env
, EXCP0D_GPF
, new_ss
& 0xfffc);
2104 if (!(ss_e2
& DESC_S_MASK
) ||
2105 (ss_e2
& DESC_CS_MASK
) ||
2106 !(ss_e2
& DESC_W_MASK
)) {
2107 raise_exception_err(env
, EXCP0D_GPF
, new_ss
& 0xfffc);
2109 dpl
= (ss_e2
>> DESC_DPL_SHIFT
) & 3;
2111 raise_exception_err(env
, EXCP0D_GPF
, new_ss
& 0xfffc);
2113 if (!(ss_e2
& DESC_P_MASK
)) {
2114 raise_exception_err(env
, EXCP0B_NOSEG
, new_ss
& 0xfffc);
2116 cpu_x86_load_seg_cache(env
, R_SS
, new_ss
,
2117 get_seg_base(ss_e1
, ss_e2
),
2118 get_seg_limit(ss_e1
, ss_e2
),
2122 cpu_x86_load_seg_cache(env
, R_CS
, new_cs
,
2123 get_seg_base(e1
, e2
),
2124 get_seg_limit(e1
, e2
),
2127 #ifdef TARGET_X86_64
2128 if (env
->hflags
& HF_CS64_MASK
) {
2133 sp_mask
= get_sp_mask(ss_e2
);
2136 /* validate data segments */
2137 validate_seg(env
, R_ES
, rpl
);
2138 validate_seg(env
, R_DS
, rpl
);
2139 validate_seg(env
, R_FS
, rpl
);
2140 validate_seg(env
, R_GS
, rpl
);
2144 SET_ESP(sp
, sp_mask
);
2147 /* NOTE: 'cpl' is the _old_ CPL */
2148 eflags_mask
= TF_MASK
| AC_MASK
| ID_MASK
| RF_MASK
| NT_MASK
;
2150 eflags_mask
|= IOPL_MASK
;
2152 iopl
= (env
->eflags
>> IOPL_SHIFT
) & 3;
2154 eflags_mask
|= IF_MASK
;
2157 eflags_mask
&= 0xffff;
2159 cpu_load_eflags(env
, new_eflags
, eflags_mask
);
2164 POPL(ssp
, sp
, sp_mask
, new_esp
);
2165 POPL(ssp
, sp
, sp_mask
, new_ss
);
2166 POPL(ssp
, sp
, sp_mask
, new_es
);
2167 POPL(ssp
, sp
, sp_mask
, new_ds
);
2168 POPL(ssp
, sp
, sp_mask
, new_fs
);
2169 POPL(ssp
, sp
, sp_mask
, new_gs
);
2171 /* modify processor state */
2172 cpu_load_eflags(env
, new_eflags
, TF_MASK
| AC_MASK
| ID_MASK
|
2173 IF_MASK
| IOPL_MASK
| VM_MASK
| NT_MASK
| VIF_MASK
|
2175 load_seg_vm(env
, R_CS
, new_cs
& 0xffff);
2176 load_seg_vm(env
, R_SS
, new_ss
& 0xffff);
2177 load_seg_vm(env
, R_ES
, new_es
& 0xffff);
2178 load_seg_vm(env
, R_DS
, new_ds
& 0xffff);
2179 load_seg_vm(env
, R_FS
, new_fs
& 0xffff);
2180 load_seg_vm(env
, R_GS
, new_gs
& 0xffff);
2182 env
->eip
= new_eip
& 0xffff;
2183 env
->regs
[R_ESP
] = new_esp
;
2186 void helper_iret_protected(CPUX86State
*env
, int shift
, int next_eip
)
2188 int tss_selector
, type
;
2191 /* specific case for TSS */
2192 if (env
->eflags
& NT_MASK
) {
2193 #ifdef TARGET_X86_64
2194 if (env
->hflags
& HF_LMA_MASK
) {
2195 raise_exception_err(env
, EXCP0D_GPF
, 0);
2198 tss_selector
= cpu_lduw_kernel(env
, env
->tr
.base
+ 0);
2199 if (tss_selector
& 4) {
2200 raise_exception_err(env
, EXCP0A_TSS
, tss_selector
& 0xfffc);
2202 if (load_segment(env
, &e1
, &e2
, tss_selector
) != 0) {
2203 raise_exception_err(env
, EXCP0A_TSS
, tss_selector
& 0xfffc);
2205 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x17;
2206 /* NOTE: we check both segment and busy TSS */
2208 raise_exception_err(env
, EXCP0A_TSS
, tss_selector
& 0xfffc);
2210 switch_tss(env
, tss_selector
, e1
, e2
, SWITCH_TSS_IRET
, next_eip
);
2212 helper_ret_protected(env
, shift
, 1, 0);
2214 env
->hflags2
&= ~HF2_NMI_MASK
;
2217 void helper_lret_protected(CPUX86State
*env
, int shift
, int addend
)
2219 helper_ret_protected(env
, shift
, 0, addend
);
2222 void helper_sysenter(CPUX86State
*env
)
2224 if (env
->sysenter_cs
== 0) {
2225 raise_exception_err(env
, EXCP0D_GPF
, 0);
2227 env
->eflags
&= ~(VM_MASK
| IF_MASK
| RF_MASK
);
2229 #ifdef TARGET_X86_64
2230 if (env
->hflags
& HF_LMA_MASK
) {
2231 cpu_x86_load_seg_cache(env
, R_CS
, env
->sysenter_cs
& 0xfffc,
2233 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2235 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
|
2240 cpu_x86_load_seg_cache(env
, R_CS
, env
->sysenter_cs
& 0xfffc,
2242 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2244 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
);
2246 cpu_x86_load_seg_cache(env
, R_SS
, (env
->sysenter_cs
+ 8) & 0xfffc,
2248 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2250 DESC_W_MASK
| DESC_A_MASK
);
2251 env
->regs
[R_ESP
] = env
->sysenter_esp
;
2252 env
->eip
= env
->sysenter_eip
;
2255 void helper_sysexit(CPUX86State
*env
, int dflag
)
2259 cpl
= env
->hflags
& HF_CPL_MASK
;
2260 if (env
->sysenter_cs
== 0 || cpl
!= 0) {
2261 raise_exception_err(env
, EXCP0D_GPF
, 0);
2263 #ifdef TARGET_X86_64
2265 cpu_x86_load_seg_cache(env
, R_CS
, ((env
->sysenter_cs
+ 32) & 0xfffc) |
2267 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2268 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
2269 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
|
2271 cpu_x86_load_seg_cache(env
, R_SS
, ((env
->sysenter_cs
+ 40) & 0xfffc) |
2273 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2274 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
2275 DESC_W_MASK
| DESC_A_MASK
);
2279 cpu_x86_load_seg_cache(env
, R_CS
, ((env
->sysenter_cs
+ 16) & 0xfffc) |
2281 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2282 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
2283 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
);
2284 cpu_x86_load_seg_cache(env
, R_SS
, ((env
->sysenter_cs
+ 24) & 0xfffc) |
2286 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2287 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
2288 DESC_W_MASK
| DESC_A_MASK
);
2290 env
->regs
[R_ESP
] = env
->regs
[R_ECX
];
2291 env
->eip
= env
->regs
[R_EDX
];
2294 target_ulong
helper_lsl(CPUX86State
*env
, target_ulong selector1
)
2297 uint32_t e1
, e2
, eflags
, selector
;
2298 int rpl
, dpl
, cpl
, type
;
2300 selector
= selector1
& 0xffff;
2301 eflags
= cpu_cc_compute_all(env
, CC_OP
);
2302 if ((selector
& 0xfffc) == 0) {
2305 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
2309 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
2310 cpl
= env
->hflags
& HF_CPL_MASK
;
2311 if (e2
& DESC_S_MASK
) {
2312 if ((e2
& DESC_CS_MASK
) && (e2
& DESC_C_MASK
)) {
2315 if (dpl
< cpl
|| dpl
< rpl
) {
2320 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
2331 if (dpl
< cpl
|| dpl
< rpl
) {
2333 CC_SRC
= eflags
& ~CC_Z
;
2337 limit
= get_seg_limit(e1
, e2
);
2338 CC_SRC
= eflags
| CC_Z
;
2342 target_ulong
helper_lar(CPUX86State
*env
, target_ulong selector1
)
2344 uint32_t e1
, e2
, eflags
, selector
;
2345 int rpl
, dpl
, cpl
, type
;
2347 selector
= selector1
& 0xffff;
2348 eflags
= cpu_cc_compute_all(env
, CC_OP
);
2349 if ((selector
& 0xfffc) == 0) {
2352 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
2356 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
2357 cpl
= env
->hflags
& HF_CPL_MASK
;
2358 if (e2
& DESC_S_MASK
) {
2359 if ((e2
& DESC_CS_MASK
) && (e2
& DESC_C_MASK
)) {
2362 if (dpl
< cpl
|| dpl
< rpl
) {
2367 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
2381 if (dpl
< cpl
|| dpl
< rpl
) {
2383 CC_SRC
= eflags
& ~CC_Z
;
2387 CC_SRC
= eflags
| CC_Z
;
2388 return e2
& 0x00f0ff00;
2391 void helper_verr(CPUX86State
*env
, target_ulong selector1
)
2393 uint32_t e1
, e2
, eflags
, selector
;
2396 selector
= selector1
& 0xffff;
2397 eflags
= cpu_cc_compute_all(env
, CC_OP
);
2398 if ((selector
& 0xfffc) == 0) {
2401 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
2404 if (!(e2
& DESC_S_MASK
)) {
2408 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
2409 cpl
= env
->hflags
& HF_CPL_MASK
;
2410 if (e2
& DESC_CS_MASK
) {
2411 if (!(e2
& DESC_R_MASK
)) {
2414 if (!(e2
& DESC_C_MASK
)) {
2415 if (dpl
< cpl
|| dpl
< rpl
) {
2420 if (dpl
< cpl
|| dpl
< rpl
) {
2422 CC_SRC
= eflags
& ~CC_Z
;
2426 CC_SRC
= eflags
| CC_Z
;
2429 void helper_verw(CPUX86State
*env
, target_ulong selector1
)
2431 uint32_t e1
, e2
, eflags
, selector
;
2434 selector
= selector1
& 0xffff;
2435 eflags
= cpu_cc_compute_all(env
, CC_OP
);
2436 if ((selector
& 0xfffc) == 0) {
2439 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
2442 if (!(e2
& DESC_S_MASK
)) {
2446 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
2447 cpl
= env
->hflags
& HF_CPL_MASK
;
2448 if (e2
& DESC_CS_MASK
) {
2451 if (dpl
< cpl
|| dpl
< rpl
) {
2454 if (!(e2
& DESC_W_MASK
)) {
2456 CC_SRC
= eflags
& ~CC_Z
;
2460 CC_SRC
= eflags
| CC_Z
;
2463 #if defined(CONFIG_USER_ONLY)
2464 void cpu_x86_load_seg(CPUX86State
*env
, int seg_reg
, int selector
)
2466 if (!(env
->cr
[0] & CR0_PE_MASK
) || (env
->eflags
& VM_MASK
)) {
2468 cpu_x86_load_seg_cache(env
, seg_reg
, selector
,
2469 (selector
<< 4), 0xffff, 0);
2471 helper_load_seg(env
, seg_reg
, selector
);