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 "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(env) \
34 log_cpu_state_mask(CPU_LOG_PCALL, (env), X86_DUMP_CCOP)
36 # define LOG_PCALL(...) do { } while (0)
37 # define LOG_PCALL_STATE(env) 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), EAX
);
328 cpu_stl_kernel(env
, env
->tr
.base
+ (0x28 + 1 * 4), ECX
);
329 cpu_stl_kernel(env
, env
->tr
.base
+ (0x28 + 2 * 4), EDX
);
330 cpu_stl_kernel(env
, env
->tr
.base
+ (0x28 + 3 * 4), EBX
);
331 cpu_stl_kernel(env
, env
->tr
.base
+ (0x28 + 4 * 4), ESP
);
332 cpu_stl_kernel(env
, env
->tr
.base
+ (0x28 + 5 * 4), EBP
);
333 cpu_stl_kernel(env
, env
->tr
.base
+ (0x28 + 6 * 4), ESI
);
334 cpu_stl_kernel(env
, env
->tr
.base
+ (0x28 + 7 * 4), 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), EAX
);
344 cpu_stw_kernel(env
, env
->tr
.base
+ (0x12 + 1 * 2), ECX
);
345 cpu_stw_kernel(env
, env
->tr
.base
+ (0x12 + 2 * 2), EDX
);
346 cpu_stw_kernel(env
, env
->tr
.base
+ (0x12 + 3 * 2), EBX
);
347 cpu_stw_kernel(env
, env
->tr
.base
+ (0x12 + 4 * 2), ESP
);
348 cpu_stw_kernel(env
, env
->tr
.base
+ (0x12 + 5 * 2), EBP
);
349 cpu_stw_kernel(env
, env
->tr
.base
+ (0x12 + 6 * 2), ESI
);
350 cpu_stw_kernel(env
, env
->tr
.base
+ (0x12 + 7 * 2), 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? */
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 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] & 0x55) {
469 for (i
= 0; i
< 4; i
++) {
470 if (hw_breakpoint_enabled(env
->dr
[7], i
) == 0x1) {
471 hw_breakpoint_remove(env
, i
);
479 static inline unsigned int get_sp_mask(unsigned int e2
)
481 if (e2
& DESC_B_MASK
) {
488 static int exception_has_error_code(int intno
)
504 #define SET_ESP(val, sp_mask) \
506 if ((sp_mask) == 0xffff) { \
507 ESP = (ESP & ~0xffff) | ((val) & 0xffff); \
508 } else if ((sp_mask) == 0xffffffffLL) { \
509 ESP = (uint32_t)(val); \
515 #define SET_ESP(val, sp_mask) \
517 ESP = (ESP & ~(sp_mask)) | ((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
= (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
;
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
, 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
, 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
);
739 if (env
->eflags
& VM_MASK
) {
740 cpu_x86_load_seg_cache(env
, R_ES
, 0, 0, 0, 0);
741 cpu_x86_load_seg_cache(env
, R_DS
, 0, 0, 0, 0);
742 cpu_x86_load_seg_cache(env
, R_FS
, 0, 0, 0, 0);
743 cpu_x86_load_seg_cache(env
, R_GS
, 0, 0, 0, 0);
745 ss
= (ss
& ~3) | dpl
;
746 cpu_x86_load_seg_cache(env
, R_SS
, ss
,
747 ssp
, get_seg_limit(ss_e1
, ss_e2
), ss_e2
);
749 SET_ESP(esp
, sp_mask
);
751 selector
= (selector
& ~3) | dpl
;
752 cpu_x86_load_seg_cache(env
, R_CS
, selector
,
753 get_seg_base(e1
, e2
),
754 get_seg_limit(e1
, e2
),
756 cpu_x86_set_cpl(env
, dpl
);
759 /* interrupt gate clear IF mask */
760 if ((type
& 1) == 0) {
761 env
->eflags
&= ~IF_MASK
;
763 env
->eflags
&= ~(TF_MASK
| VM_MASK
| RF_MASK
| NT_MASK
);
768 #define PUSHQ(sp, val) \
771 cpu_stq_kernel(env, sp, (val)); \
774 #define POPQ(sp, val) \
776 val = cpu_ldq_kernel(env, sp); \
780 static inline target_ulong
get_rsp_from_tss(CPUX86State
*env
, int level
)
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(env
, "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);
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
);
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
);
911 cpu_x86_load_seg_cache(env
, R_SS
, ss
, 0, 0, 0);
915 selector
= (selector
& ~3) | dpl
;
916 cpu_x86_load_seg_cache(env
, R_CS
, selector
,
917 get_seg_base(e1
, e2
),
918 get_seg_limit(e1
, e2
),
920 cpu_x86_set_cpl(env
, dpl
);
923 /* interrupt gate clear IF mask */
924 if ((type
& 1) == 0) {
925 env
->eflags
&= ~IF_MASK
;
927 env
->eflags
&= ~(TF_MASK
| VM_MASK
| RF_MASK
| NT_MASK
);
932 #if defined(CONFIG_USER_ONLY)
933 void helper_syscall(CPUX86State
*env
, int next_eip_addend
)
935 env
->exception_index
= EXCP_SYSCALL
;
936 env
->exception_next_eip
= env
->eip
+ next_eip_addend
;
940 void helper_syscall(CPUX86State
*env
, int next_eip_addend
)
944 if (!(env
->efer
& MSR_EFER_SCE
)) {
945 raise_exception_err(env
, EXCP06_ILLOP
, 0);
947 selector
= (env
->star
>> 32) & 0xffff;
948 if (env
->hflags
& HF_LMA_MASK
) {
951 ECX
= env
->eip
+ next_eip_addend
;
952 env
->regs
[11] = cpu_compute_eflags(env
);
954 code64
= env
->hflags
& HF_CS64_MASK
;
956 cpu_x86_set_cpl(env
, 0);
957 cpu_x86_load_seg_cache(env
, R_CS
, selector
& 0xfffc,
959 DESC_G_MASK
| DESC_P_MASK
|
961 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
|
963 cpu_x86_load_seg_cache(env
, R_SS
, (selector
+ 8) & 0xfffc,
965 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
967 DESC_W_MASK
| DESC_A_MASK
);
968 env
->eflags
&= ~env
->fmask
;
969 cpu_load_eflags(env
, env
->eflags
, 0);
971 env
->eip
= env
->lstar
;
973 env
->eip
= env
->cstar
;
976 ECX
= (uint32_t)(env
->eip
+ next_eip_addend
);
978 cpu_x86_set_cpl(env
, 0);
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
->eflags
&= ~(IF_MASK
| RF_MASK
| VM_MASK
);
990 env
->eip
= (uint32_t)env
->star
;
997 void helper_sysret(CPUX86State
*env
, int dflag
)
1001 if (!(env
->efer
& MSR_EFER_SCE
)) {
1002 raise_exception_err(env
, EXCP06_ILLOP
, 0);
1004 cpl
= env
->hflags
& HF_CPL_MASK
;
1005 if (!(env
->cr
[0] & CR0_PE_MASK
) || cpl
!= 0) {
1006 raise_exception_err(env
, EXCP0D_GPF
, 0);
1008 selector
= (env
->star
>> 48) & 0xffff;
1009 if (env
->hflags
& HF_LMA_MASK
) {
1011 cpu_x86_load_seg_cache(env
, R_CS
, (selector
+ 16) | 3,
1013 DESC_G_MASK
| DESC_P_MASK
|
1014 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
1015 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
|
1019 cpu_x86_load_seg_cache(env
, R_CS
, selector
| 3,
1021 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
1022 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
1023 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
);
1024 env
->eip
= (uint32_t)ECX
;
1026 cpu_x86_load_seg_cache(env
, R_SS
, selector
+ 8,
1028 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
1029 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
1030 DESC_W_MASK
| DESC_A_MASK
);
1031 cpu_load_eflags(env
, (uint32_t)(env
->regs
[11]), TF_MASK
| AC_MASK
1032 | ID_MASK
| IF_MASK
| IOPL_MASK
| VM_MASK
| RF_MASK
|
1034 cpu_x86_set_cpl(env
, 3);
1036 cpu_x86_load_seg_cache(env
, R_CS
, selector
| 3,
1038 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
1039 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
1040 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
);
1041 env
->eip
= (uint32_t)ECX
;
1042 cpu_x86_load_seg_cache(env
, R_SS
, selector
+ 8,
1044 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
1045 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
1046 DESC_W_MASK
| DESC_A_MASK
);
1047 env
->eflags
|= IF_MASK
;
1048 cpu_x86_set_cpl(env
, 3);
1053 /* real mode interrupt */
1054 static void do_interrupt_real(CPUX86State
*env
, int intno
, int is_int
,
1055 int error_code
, unsigned int next_eip
)
1058 target_ulong ptr
, ssp
;
1060 uint32_t offset
, esp
;
1061 uint32_t old_cs
, old_eip
;
1063 /* real mode (simpler!) */
1065 if (intno
* 4 + 3 > dt
->limit
) {
1066 raise_exception_err(env
, EXCP0D_GPF
, intno
* 8 + 2);
1068 ptr
= dt
->base
+ intno
* 4;
1069 offset
= cpu_lduw_kernel(env
, ptr
);
1070 selector
= cpu_lduw_kernel(env
, ptr
+ 2);
1072 ssp
= env
->segs
[R_SS
].base
;
1078 old_cs
= env
->segs
[R_CS
].selector
;
1079 /* XXX: use SS segment size? */
1080 PUSHW(ssp
, esp
, 0xffff, cpu_compute_eflags(env
));
1081 PUSHW(ssp
, esp
, 0xffff, old_cs
);
1082 PUSHW(ssp
, esp
, 0xffff, old_eip
);
1084 /* update processor state */
1085 ESP
= (ESP
& ~0xffff) | (esp
& 0xffff);
1087 env
->segs
[R_CS
].selector
= selector
;
1088 env
->segs
[R_CS
].base
= (selector
<< 4);
1089 env
->eflags
&= ~(IF_MASK
| TF_MASK
| AC_MASK
| RF_MASK
);
1092 #if defined(CONFIG_USER_ONLY)
1093 /* fake user mode interrupt */
1094 static void do_interrupt_user(CPUX86State
*env
, int intno
, int is_int
,
1095 int error_code
, target_ulong next_eip
)
1099 int dpl
, cpl
, shift
;
1103 if (env
->hflags
& HF_LMA_MASK
) {
1108 ptr
= dt
->base
+ (intno
<< shift
);
1109 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
1111 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1112 cpl
= env
->hflags
& HF_CPL_MASK
;
1113 /* check privilege if software int */
1114 if (is_int
&& dpl
< cpl
) {
1115 raise_exception_err(env
, EXCP0D_GPF
, (intno
<< shift
) + 2);
1118 /* Since we emulate only user space, we cannot do more than
1119 exiting the emulation with the suitable exception and error
1128 static void handle_even_inj(CPUX86State
*env
, int intno
, int is_int
,
1129 int error_code
, int is_hw
, int rm
)
1131 uint32_t event_inj
= ldl_phys(env
->vm_vmcb
+ offsetof(struct vmcb
,
1132 control
.event_inj
));
1134 if (!(event_inj
& SVM_EVTINJ_VALID
)) {
1138 type
= SVM_EVTINJ_TYPE_SOFT
;
1140 type
= SVM_EVTINJ_TYPE_EXEPT
;
1142 event_inj
= intno
| type
| SVM_EVTINJ_VALID
;
1143 if (!rm
&& exception_has_error_code(intno
)) {
1144 event_inj
|= SVM_EVTINJ_VALID_ERR
;
1145 stl_phys(env
->vm_vmcb
+ offsetof(struct vmcb
,
1146 control
.event_inj_err
),
1149 stl_phys(env
->vm_vmcb
+ offsetof(struct vmcb
, control
.event_inj
),
1156 * Begin execution of an interruption. is_int is TRUE if coming from
1157 * the int instruction. next_eip is the EIP value AFTER the interrupt
1158 * instruction. It is only relevant if is_int is TRUE.
1160 static void do_interrupt_all(CPUX86State
*env
, int intno
, int is_int
,
1161 int error_code
, target_ulong next_eip
, int is_hw
)
1163 if (qemu_loglevel_mask(CPU_LOG_INT
)) {
1164 if ((env
->cr
[0] & CR0_PE_MASK
)) {
1167 qemu_log("%6d: v=%02x e=%04x i=%d cpl=%d IP=%04x:" TARGET_FMT_lx
1168 " pc=" TARGET_FMT_lx
" SP=%04x:" TARGET_FMT_lx
,
1169 count
, intno
, error_code
, is_int
,
1170 env
->hflags
& HF_CPL_MASK
,
1171 env
->segs
[R_CS
].selector
, EIP
,
1172 (int)env
->segs
[R_CS
].base
+ EIP
,
1173 env
->segs
[R_SS
].selector
, ESP
);
1174 if (intno
== 0x0e) {
1175 qemu_log(" CR2=" TARGET_FMT_lx
, env
->cr
[2]);
1177 qemu_log(" EAX=" TARGET_FMT_lx
, EAX
);
1180 log_cpu_state(env
, X86_DUMP_CCOP
);
1187 ptr
= env
->segs
[R_CS
].base
+ env
->eip
;
1188 for (i
= 0; i
< 16; i
++) {
1189 qemu_log(" %02x", ldub(ptr
+ i
));
1197 if (env
->cr
[0] & CR0_PE_MASK
) {
1198 #if !defined(CONFIG_USER_ONLY)
1199 if (env
->hflags
& HF_SVMI_MASK
) {
1200 handle_even_inj(env
, intno
, is_int
, error_code
, is_hw
, 0);
1203 #ifdef TARGET_X86_64
1204 if (env
->hflags
& HF_LMA_MASK
) {
1205 do_interrupt64(env
, intno
, is_int
, error_code
, next_eip
, is_hw
);
1209 do_interrupt_protected(env
, intno
, is_int
, error_code
, next_eip
,
1213 #if !defined(CONFIG_USER_ONLY)
1214 if (env
->hflags
& HF_SVMI_MASK
) {
1215 handle_even_inj(env
, intno
, is_int
, error_code
, is_hw
, 1);
1218 do_interrupt_real(env
, intno
, is_int
, error_code
, next_eip
);
1221 #if !defined(CONFIG_USER_ONLY)
1222 if (env
->hflags
& HF_SVMI_MASK
) {
1223 uint32_t event_inj
= ldl_phys(env
->vm_vmcb
+
1224 offsetof(struct vmcb
,
1225 control
.event_inj
));
1227 stl_phys(env
->vm_vmcb
+ offsetof(struct vmcb
, control
.event_inj
),
1228 event_inj
& ~SVM_EVTINJ_VALID
);
1233 void do_interrupt(CPUX86State
*env
)
1235 #if defined(CONFIG_USER_ONLY)
1236 /* if user mode only, we simulate a fake exception
1237 which will be handled outside the cpu execution
1239 do_interrupt_user(env
, env
->exception_index
,
1240 env
->exception_is_int
,
1242 env
->exception_next_eip
);
1243 /* successfully delivered */
1244 env
->old_exception
= -1;
1246 /* simulate a real cpu exception. On i386, it can
1247 trigger new exceptions, but we do not handle
1248 double or triple faults yet. */
1249 do_interrupt_all(env
, env
->exception_index
,
1250 env
->exception_is_int
,
1252 env
->exception_next_eip
, 0);
1253 /* successfully delivered */
1254 env
->old_exception
= -1;
1258 void do_interrupt_x86_hardirq(CPUX86State
*env
, int intno
, int is_hw
)
1260 do_interrupt_all(env
, intno
, 0, 0, 0, is_hw
);
1263 void helper_enter_level(CPUX86State
*env
, int level
, int data32
,
1267 uint32_t esp_mask
, esp
, ebp
;
1269 esp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1270 ssp
= env
->segs
[R_SS
].base
;
1279 cpu_stl_data(env
, ssp
+ (esp
& esp_mask
),
1280 cpu_ldl_data(env
, ssp
+ (ebp
& esp_mask
)));
1283 cpu_stl_data(env
, ssp
+ (esp
& esp_mask
), t1
);
1290 cpu_stw_data(env
, ssp
+ (esp
& esp_mask
),
1291 cpu_lduw_data(env
, ssp
+ (ebp
& esp_mask
)));
1294 cpu_stw_data(env
, ssp
+ (esp
& esp_mask
), t1
);
1298 #ifdef TARGET_X86_64
1299 void helper_enter64_level(CPUX86State
*env
, int level
, int data64
,
1302 target_ulong esp
, ebp
;
1313 cpu_stq_data(env
, esp
, cpu_ldq_data(env
, ebp
));
1316 cpu_stq_data(env
, esp
, t1
);
1323 cpu_stw_data(env
, esp
, cpu_lduw_data(env
, ebp
));
1326 cpu_stw_data(env
, esp
, t1
);
1331 void helper_lldt(CPUX86State
*env
, int selector
)
1335 int index
, entry_limit
;
1339 if ((selector
& 0xfffc) == 0) {
1340 /* XXX: NULL selector case: invalid LDT */
1344 if (selector
& 0x4) {
1345 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1348 index
= selector
& ~7;
1349 #ifdef TARGET_X86_64
1350 if (env
->hflags
& HF_LMA_MASK
) {
1357 if ((index
+ entry_limit
) > dt
->limit
) {
1358 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1360 ptr
= dt
->base
+ index
;
1361 e1
= cpu_ldl_kernel(env
, ptr
);
1362 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
1363 if ((e2
& DESC_S_MASK
) || ((e2
>> DESC_TYPE_SHIFT
) & 0xf) != 2) {
1364 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1366 if (!(e2
& DESC_P_MASK
)) {
1367 raise_exception_err(env
, EXCP0B_NOSEG
, selector
& 0xfffc);
1369 #ifdef TARGET_X86_64
1370 if (env
->hflags
& HF_LMA_MASK
) {
1373 e3
= cpu_ldl_kernel(env
, ptr
+ 8);
1374 load_seg_cache_raw_dt(&env
->ldt
, e1
, e2
);
1375 env
->ldt
.base
|= (target_ulong
)e3
<< 32;
1379 load_seg_cache_raw_dt(&env
->ldt
, e1
, e2
);
1382 env
->ldt
.selector
= selector
;
1385 void helper_ltr(CPUX86State
*env
, int selector
)
1389 int index
, type
, entry_limit
;
1393 if ((selector
& 0xfffc) == 0) {
1394 /* NULL selector case: invalid TR */
1399 if (selector
& 0x4) {
1400 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1403 index
= selector
& ~7;
1404 #ifdef TARGET_X86_64
1405 if (env
->hflags
& HF_LMA_MASK
) {
1412 if ((index
+ entry_limit
) > dt
->limit
) {
1413 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1415 ptr
= dt
->base
+ index
;
1416 e1
= cpu_ldl_kernel(env
, ptr
);
1417 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
1418 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
1419 if ((e2
& DESC_S_MASK
) ||
1420 (type
!= 1 && type
!= 9)) {
1421 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1423 if (!(e2
& DESC_P_MASK
)) {
1424 raise_exception_err(env
, EXCP0B_NOSEG
, selector
& 0xfffc);
1426 #ifdef TARGET_X86_64
1427 if (env
->hflags
& HF_LMA_MASK
) {
1430 e3
= cpu_ldl_kernel(env
, ptr
+ 8);
1431 e4
= cpu_ldl_kernel(env
, ptr
+ 12);
1432 if ((e4
>> DESC_TYPE_SHIFT
) & 0xf) {
1433 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1435 load_seg_cache_raw_dt(&env
->tr
, e1
, e2
);
1436 env
->tr
.base
|= (target_ulong
)e3
<< 32;
1440 load_seg_cache_raw_dt(&env
->tr
, e1
, e2
);
1442 e2
|= DESC_TSS_BUSY_MASK
;
1443 cpu_stl_kernel(env
, ptr
+ 4, e2
);
1445 env
->tr
.selector
= selector
;
1448 /* only works if protected mode and not VM86. seg_reg must be != R_CS */
1449 void helper_load_seg(CPUX86State
*env
, int seg_reg
, int selector
)
1458 cpl
= env
->hflags
& HF_CPL_MASK
;
1459 if ((selector
& 0xfffc) == 0) {
1460 /* null selector case */
1462 #ifdef TARGET_X86_64
1463 && (!(env
->hflags
& HF_CS64_MASK
) || cpl
== 3)
1466 raise_exception_err(env
, EXCP0D_GPF
, 0);
1468 cpu_x86_load_seg_cache(env
, seg_reg
, selector
, 0, 0, 0);
1471 if (selector
& 0x4) {
1476 index
= selector
& ~7;
1477 if ((index
+ 7) > dt
->limit
) {
1478 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1480 ptr
= dt
->base
+ index
;
1481 e1
= cpu_ldl_kernel(env
, ptr
);
1482 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
1484 if (!(e2
& DESC_S_MASK
)) {
1485 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1488 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1489 if (seg_reg
== R_SS
) {
1490 /* must be writable segment */
1491 if ((e2
& DESC_CS_MASK
) || !(e2
& DESC_W_MASK
)) {
1492 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1494 if (rpl
!= cpl
|| dpl
!= cpl
) {
1495 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1498 /* must be readable segment */
1499 if ((e2
& (DESC_CS_MASK
| DESC_R_MASK
)) == DESC_CS_MASK
) {
1500 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1503 if (!(e2
& DESC_CS_MASK
) || !(e2
& DESC_C_MASK
)) {
1504 /* if not conforming code, test rights */
1505 if (dpl
< cpl
|| dpl
< rpl
) {
1506 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1511 if (!(e2
& DESC_P_MASK
)) {
1512 if (seg_reg
== R_SS
) {
1513 raise_exception_err(env
, EXCP0C_STACK
, selector
& 0xfffc);
1515 raise_exception_err(env
, EXCP0B_NOSEG
, selector
& 0xfffc);
1519 /* set the access bit if not already set */
1520 if (!(e2
& DESC_A_MASK
)) {
1522 cpu_stl_kernel(env
, ptr
+ 4, e2
);
1525 cpu_x86_load_seg_cache(env
, seg_reg
, selector
,
1526 get_seg_base(e1
, e2
),
1527 get_seg_limit(e1
, e2
),
1530 qemu_log("load_seg: sel=0x%04x base=0x%08lx limit=0x%08lx flags=%08x\n",
1531 selector
, (unsigned long)sc
->base
, sc
->limit
, sc
->flags
);
1536 /* protected mode jump */
1537 void helper_ljmp_protected(CPUX86State
*env
, int new_cs
, target_ulong new_eip
,
1538 int next_eip_addend
)
1541 uint32_t e1
, e2
, cpl
, dpl
, rpl
, limit
;
1542 target_ulong next_eip
;
1544 if ((new_cs
& 0xfffc) == 0) {
1545 raise_exception_err(env
, EXCP0D_GPF
, 0);
1547 if (load_segment(env
, &e1
, &e2
, new_cs
) != 0) {
1548 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1550 cpl
= env
->hflags
& HF_CPL_MASK
;
1551 if (e2
& DESC_S_MASK
) {
1552 if (!(e2
& DESC_CS_MASK
)) {
1553 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1555 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1556 if (e2
& DESC_C_MASK
) {
1557 /* conforming code segment */
1559 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1562 /* non conforming code segment */
1565 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1568 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1571 if (!(e2
& DESC_P_MASK
)) {
1572 raise_exception_err(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc);
1574 limit
= get_seg_limit(e1
, e2
);
1575 if (new_eip
> limit
&&
1576 !(env
->hflags
& HF_LMA_MASK
) && !(e2
& DESC_L_MASK
)) {
1577 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1579 cpu_x86_load_seg_cache(env
, R_CS
, (new_cs
& 0xfffc) | cpl
,
1580 get_seg_base(e1
, e2
), limit
, e2
);
1583 /* jump to call or task gate */
1584 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1586 cpl
= env
->hflags
& HF_CPL_MASK
;
1587 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
1589 case 1: /* 286 TSS */
1590 case 9: /* 386 TSS */
1591 case 5: /* task gate */
1592 if (dpl
< cpl
|| dpl
< rpl
) {
1593 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1595 next_eip
= env
->eip
+ next_eip_addend
;
1596 switch_tss(env
, new_cs
, e1
, e2
, SWITCH_TSS_JMP
, next_eip
);
1597 CC_OP
= CC_OP_EFLAGS
;
1599 case 4: /* 286 call gate */
1600 case 12: /* 386 call gate */
1601 if ((dpl
< cpl
) || (dpl
< rpl
)) {
1602 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1604 if (!(e2
& DESC_P_MASK
)) {
1605 raise_exception_err(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc);
1608 new_eip
= (e1
& 0xffff);
1610 new_eip
|= (e2
& 0xffff0000);
1612 if (load_segment(env
, &e1
, &e2
, gate_cs
) != 0) {
1613 raise_exception_err(env
, EXCP0D_GPF
, gate_cs
& 0xfffc);
1615 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1616 /* must be code segment */
1617 if (((e2
& (DESC_S_MASK
| DESC_CS_MASK
)) !=
1618 (DESC_S_MASK
| DESC_CS_MASK
))) {
1619 raise_exception_err(env
, EXCP0D_GPF
, gate_cs
& 0xfffc);
1621 if (((e2
& DESC_C_MASK
) && (dpl
> cpl
)) ||
1622 (!(e2
& DESC_C_MASK
) && (dpl
!= cpl
))) {
1623 raise_exception_err(env
, EXCP0D_GPF
, gate_cs
& 0xfffc);
1625 if (!(e2
& DESC_P_MASK
)) {
1626 raise_exception_err(env
, EXCP0D_GPF
, gate_cs
& 0xfffc);
1628 limit
= get_seg_limit(e1
, e2
);
1629 if (new_eip
> limit
) {
1630 raise_exception_err(env
, EXCP0D_GPF
, 0);
1632 cpu_x86_load_seg_cache(env
, R_CS
, (gate_cs
& 0xfffc) | cpl
,
1633 get_seg_base(e1
, e2
), limit
, e2
);
1637 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1643 /* real mode call */
1644 void helper_lcall_real(CPUX86State
*env
, int new_cs
, target_ulong new_eip1
,
1645 int shift
, int next_eip
)
1648 uint32_t esp
, esp_mask
;
1653 esp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1654 ssp
= env
->segs
[R_SS
].base
;
1656 PUSHL(ssp
, esp
, esp_mask
, env
->segs
[R_CS
].selector
);
1657 PUSHL(ssp
, esp
, esp_mask
, next_eip
);
1659 PUSHW(ssp
, esp
, esp_mask
, env
->segs
[R_CS
].selector
);
1660 PUSHW(ssp
, esp
, esp_mask
, next_eip
);
1663 SET_ESP(esp
, esp_mask
);
1665 env
->segs
[R_CS
].selector
= new_cs
;
1666 env
->segs
[R_CS
].base
= (new_cs
<< 4);
1669 /* protected mode call */
1670 void helper_lcall_protected(CPUX86State
*env
, int new_cs
, target_ulong new_eip
,
1671 int shift
, int next_eip_addend
)
1674 uint32_t e1
, e2
, cpl
, dpl
, rpl
, selector
, offset
, param_count
;
1675 uint32_t ss
= 0, ss_e1
= 0, ss_e2
= 0, sp
, type
, ss_dpl
, sp_mask
;
1676 uint32_t val
, limit
, old_sp_mask
;
1677 target_ulong ssp
, old_ssp
, next_eip
;
1679 next_eip
= env
->eip
+ next_eip_addend
;
1680 LOG_PCALL("lcall %04x:%08x s=%d\n", new_cs
, (uint32_t)new_eip
, shift
);
1681 LOG_PCALL_STATE(env
);
1682 if ((new_cs
& 0xfffc) == 0) {
1683 raise_exception_err(env
, EXCP0D_GPF
, 0);
1685 if (load_segment(env
, &e1
, &e2
, new_cs
) != 0) {
1686 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1688 cpl
= env
->hflags
& HF_CPL_MASK
;
1689 LOG_PCALL("desc=%08x:%08x\n", e1
, e2
);
1690 if (e2
& DESC_S_MASK
) {
1691 if (!(e2
& DESC_CS_MASK
)) {
1692 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1694 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1695 if (e2
& DESC_C_MASK
) {
1696 /* conforming code segment */
1698 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1701 /* non conforming code segment */
1704 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1707 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1710 if (!(e2
& DESC_P_MASK
)) {
1711 raise_exception_err(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc);
1714 #ifdef TARGET_X86_64
1715 /* XXX: check 16/32 bit cases in long mode */
1721 PUSHQ(rsp
, env
->segs
[R_CS
].selector
);
1722 PUSHQ(rsp
, next_eip
);
1723 /* from this point, not restartable */
1725 cpu_x86_load_seg_cache(env
, R_CS
, (new_cs
& 0xfffc) | cpl
,
1726 get_seg_base(e1
, e2
),
1727 get_seg_limit(e1
, e2
), e2
);
1733 sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1734 ssp
= env
->segs
[R_SS
].base
;
1736 PUSHL(ssp
, sp
, sp_mask
, env
->segs
[R_CS
].selector
);
1737 PUSHL(ssp
, sp
, sp_mask
, next_eip
);
1739 PUSHW(ssp
, sp
, sp_mask
, env
->segs
[R_CS
].selector
);
1740 PUSHW(ssp
, sp
, sp_mask
, next_eip
);
1743 limit
= get_seg_limit(e1
, e2
);
1744 if (new_eip
> limit
) {
1745 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1747 /* from this point, not restartable */
1748 SET_ESP(sp
, sp_mask
);
1749 cpu_x86_load_seg_cache(env
, R_CS
, (new_cs
& 0xfffc) | cpl
,
1750 get_seg_base(e1
, e2
), limit
, e2
);
1754 /* check gate type */
1755 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x1f;
1756 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1759 case 1: /* available 286 TSS */
1760 case 9: /* available 386 TSS */
1761 case 5: /* task gate */
1762 if (dpl
< cpl
|| dpl
< rpl
) {
1763 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1765 switch_tss(env
, new_cs
, e1
, e2
, SWITCH_TSS_CALL
, next_eip
);
1766 CC_OP
= CC_OP_EFLAGS
;
1768 case 4: /* 286 call gate */
1769 case 12: /* 386 call gate */
1772 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1777 if (dpl
< cpl
|| dpl
< rpl
) {
1778 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
1780 /* check valid bit */
1781 if (!(e2
& DESC_P_MASK
)) {
1782 raise_exception_err(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc);
1784 selector
= e1
>> 16;
1785 offset
= (e2
& 0xffff0000) | (e1
& 0x0000ffff);
1786 param_count
= e2
& 0x1f;
1787 if ((selector
& 0xfffc) == 0) {
1788 raise_exception_err(env
, EXCP0D_GPF
, 0);
1791 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
1792 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1794 if (!(e2
& DESC_S_MASK
) || !(e2
& (DESC_CS_MASK
))) {
1795 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1797 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1799 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
1801 if (!(e2
& DESC_P_MASK
)) {
1802 raise_exception_err(env
, EXCP0B_NOSEG
, selector
& 0xfffc);
1805 if (!(e2
& DESC_C_MASK
) && dpl
< cpl
) {
1806 /* to inner privilege */
1807 get_ss_esp_from_tss(env
, &ss
, &sp
, dpl
);
1808 LOG_PCALL("new ss:esp=%04x:%08x param_count=%d ESP=" TARGET_FMT_lx
1810 ss
, sp
, param_count
, ESP
);
1811 if ((ss
& 0xfffc) == 0) {
1812 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
1814 if ((ss
& 3) != dpl
) {
1815 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
1817 if (load_segment(env
, &ss_e1
, &ss_e2
, ss
) != 0) {
1818 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
1820 ss_dpl
= (ss_e2
>> DESC_DPL_SHIFT
) & 3;
1821 if (ss_dpl
!= dpl
) {
1822 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
1824 if (!(ss_e2
& DESC_S_MASK
) ||
1825 (ss_e2
& DESC_CS_MASK
) ||
1826 !(ss_e2
& DESC_W_MASK
)) {
1827 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
1829 if (!(ss_e2
& DESC_P_MASK
)) {
1830 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
1833 /* push_size = ((param_count * 2) + 8) << shift; */
1835 old_sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1836 old_ssp
= env
->segs
[R_SS
].base
;
1838 sp_mask
= get_sp_mask(ss_e2
);
1839 ssp
= get_seg_base(ss_e1
, ss_e2
);
1841 PUSHL(ssp
, sp
, sp_mask
, env
->segs
[R_SS
].selector
);
1842 PUSHL(ssp
, sp
, sp_mask
, ESP
);
1843 for (i
= param_count
- 1; i
>= 0; i
--) {
1844 val
= cpu_ldl_kernel(env
, old_ssp
+ ((ESP
+ i
* 4) &
1846 PUSHL(ssp
, sp
, sp_mask
, val
);
1849 PUSHW(ssp
, sp
, sp_mask
, env
->segs
[R_SS
].selector
);
1850 PUSHW(ssp
, sp
, sp_mask
, ESP
);
1851 for (i
= param_count
- 1; i
>= 0; i
--) {
1852 val
= cpu_lduw_kernel(env
, old_ssp
+ ((ESP
+ i
* 2) &
1854 PUSHW(ssp
, sp
, sp_mask
, val
);
1859 /* to same privilege */
1861 sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1862 ssp
= env
->segs
[R_SS
].base
;
1863 /* push_size = (4 << shift); */
1868 PUSHL(ssp
, sp
, sp_mask
, env
->segs
[R_CS
].selector
);
1869 PUSHL(ssp
, sp
, sp_mask
, next_eip
);
1871 PUSHW(ssp
, sp
, sp_mask
, env
->segs
[R_CS
].selector
);
1872 PUSHW(ssp
, sp
, sp_mask
, next_eip
);
1875 /* from this point, not restartable */
1878 ss
= (ss
& ~3) | dpl
;
1879 cpu_x86_load_seg_cache(env
, R_SS
, ss
,
1881 get_seg_limit(ss_e1
, ss_e2
),
1885 selector
= (selector
& ~3) | dpl
;
1886 cpu_x86_load_seg_cache(env
, R_CS
, selector
,
1887 get_seg_base(e1
, e2
),
1888 get_seg_limit(e1
, e2
),
1890 cpu_x86_set_cpl(env
, dpl
);
1891 SET_ESP(sp
, sp_mask
);
1896 /* real and vm86 mode iret */
1897 void helper_iret_real(CPUX86State
*env
, int shift
)
1899 uint32_t sp
, new_cs
, new_eip
, new_eflags
, sp_mask
;
1903 sp_mask
= 0xffff; /* XXXX: use SS segment size? */
1905 ssp
= env
->segs
[R_SS
].base
;
1908 POPL(ssp
, sp
, sp_mask
, new_eip
);
1909 POPL(ssp
, sp
, sp_mask
, new_cs
);
1911 POPL(ssp
, sp
, sp_mask
, new_eflags
);
1914 POPW(ssp
, sp
, sp_mask
, new_eip
);
1915 POPW(ssp
, sp
, sp_mask
, new_cs
);
1916 POPW(ssp
, sp
, sp_mask
, new_eflags
);
1918 ESP
= (ESP
& ~sp_mask
) | (sp
& sp_mask
);
1919 env
->segs
[R_CS
].selector
= new_cs
;
1920 env
->segs
[R_CS
].base
= (new_cs
<< 4);
1922 if (env
->eflags
& VM_MASK
) {
1923 eflags_mask
= TF_MASK
| AC_MASK
| ID_MASK
| IF_MASK
| RF_MASK
|
1926 eflags_mask
= TF_MASK
| AC_MASK
| ID_MASK
| IF_MASK
| IOPL_MASK
|
1930 eflags_mask
&= 0xffff;
1932 cpu_load_eflags(env
, new_eflags
, eflags_mask
);
1933 env
->hflags2
&= ~HF2_NMI_MASK
;
1936 static inline void validate_seg(CPUX86State
*env
, int seg_reg
, int cpl
)
1941 /* XXX: on x86_64, we do not want to nullify FS and GS because
1942 they may still contain a valid base. I would be interested to
1943 know how a real x86_64 CPU behaves */
1944 if ((seg_reg
== R_FS
|| seg_reg
== R_GS
) &&
1945 (env
->segs
[seg_reg
].selector
& 0xfffc) == 0) {
1949 e2
= env
->segs
[seg_reg
].flags
;
1950 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1951 if (!(e2
& DESC_CS_MASK
) || !(e2
& DESC_C_MASK
)) {
1952 /* data or non conforming code segment */
1954 cpu_x86_load_seg_cache(env
, seg_reg
, 0, 0, 0, 0);
1959 /* protected mode iret */
1960 static inline void helper_ret_protected(CPUX86State
*env
, int shift
,
1961 int is_iret
, int addend
)
1963 uint32_t new_cs
, new_eflags
, new_ss
;
1964 uint32_t new_es
, new_ds
, new_fs
, new_gs
;
1965 uint32_t e1
, e2
, ss_e1
, ss_e2
;
1966 int cpl
, dpl
, rpl
, eflags_mask
, iopl
;
1967 target_ulong ssp
, sp
, new_eip
, new_esp
, sp_mask
;
1969 #ifdef TARGET_X86_64
1975 sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1978 ssp
= env
->segs
[R_SS
].base
;
1979 new_eflags
= 0; /* avoid warning */
1980 #ifdef TARGET_X86_64
1986 POPQ(sp
, new_eflags
);
1993 POPL(ssp
, sp
, sp_mask
, new_eip
);
1994 POPL(ssp
, sp
, sp_mask
, new_cs
);
1997 POPL(ssp
, sp
, sp_mask
, new_eflags
);
1998 if (new_eflags
& VM_MASK
) {
1999 goto return_to_vm86
;
2004 POPW(ssp
, sp
, sp_mask
, new_eip
);
2005 POPW(ssp
, sp
, sp_mask
, new_cs
);
2007 POPW(ssp
, sp
, sp_mask
, new_eflags
);
2011 LOG_PCALL("lret new %04x:" TARGET_FMT_lx
" s=%d addend=0x%x\n",
2012 new_cs
, new_eip
, shift
, addend
);
2013 LOG_PCALL_STATE(env
);
2014 if ((new_cs
& 0xfffc) == 0) {
2015 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
2017 if (load_segment(env
, &e1
, &e2
, new_cs
) != 0) {
2018 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
2020 if (!(e2
& DESC_S_MASK
) ||
2021 !(e2
& DESC_CS_MASK
)) {
2022 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
2024 cpl
= env
->hflags
& HF_CPL_MASK
;
2027 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
2029 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
2030 if (e2
& DESC_C_MASK
) {
2032 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
2036 raise_exception_err(env
, EXCP0D_GPF
, new_cs
& 0xfffc);
2039 if (!(e2
& DESC_P_MASK
)) {
2040 raise_exception_err(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc);
2044 if (rpl
== cpl
&& (!(env
->hflags
& HF_CS64_MASK
) ||
2045 ((env
->hflags
& HF_CS64_MASK
) && !is_iret
))) {
2046 /* return to same privilege level */
2047 cpu_x86_load_seg_cache(env
, R_CS
, new_cs
,
2048 get_seg_base(e1
, e2
),
2049 get_seg_limit(e1
, e2
),
2052 /* return to different privilege level */
2053 #ifdef TARGET_X86_64
2063 POPL(ssp
, sp
, sp_mask
, new_esp
);
2064 POPL(ssp
, sp
, sp_mask
, new_ss
);
2068 POPW(ssp
, sp
, sp_mask
, new_esp
);
2069 POPW(ssp
, sp
, sp_mask
, new_ss
);
2072 LOG_PCALL("new ss:esp=%04x:" TARGET_FMT_lx
"\n",
2074 if ((new_ss
& 0xfffc) == 0) {
2075 #ifdef TARGET_X86_64
2076 /* NULL ss is allowed in long mode if cpl != 3 */
2077 /* XXX: test CS64? */
2078 if ((env
->hflags
& HF_LMA_MASK
) && rpl
!= 3) {
2079 cpu_x86_load_seg_cache(env
, R_SS
, new_ss
,
2081 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2082 DESC_S_MASK
| (rpl
<< DESC_DPL_SHIFT
) |
2083 DESC_W_MASK
| DESC_A_MASK
);
2084 ss_e2
= DESC_B_MASK
; /* XXX: should not be needed? */
2088 raise_exception_err(env
, EXCP0D_GPF
, 0);
2091 if ((new_ss
& 3) != rpl
) {
2092 raise_exception_err(env
, EXCP0D_GPF
, new_ss
& 0xfffc);
2094 if (load_segment(env
, &ss_e1
, &ss_e2
, new_ss
) != 0) {
2095 raise_exception_err(env
, EXCP0D_GPF
, new_ss
& 0xfffc);
2097 if (!(ss_e2
& DESC_S_MASK
) ||
2098 (ss_e2
& DESC_CS_MASK
) ||
2099 !(ss_e2
& DESC_W_MASK
)) {
2100 raise_exception_err(env
, EXCP0D_GPF
, new_ss
& 0xfffc);
2102 dpl
= (ss_e2
>> DESC_DPL_SHIFT
) & 3;
2104 raise_exception_err(env
, EXCP0D_GPF
, new_ss
& 0xfffc);
2106 if (!(ss_e2
& DESC_P_MASK
)) {
2107 raise_exception_err(env
, EXCP0B_NOSEG
, new_ss
& 0xfffc);
2109 cpu_x86_load_seg_cache(env
, R_SS
, new_ss
,
2110 get_seg_base(ss_e1
, ss_e2
),
2111 get_seg_limit(ss_e1
, ss_e2
),
2115 cpu_x86_load_seg_cache(env
, R_CS
, new_cs
,
2116 get_seg_base(e1
, e2
),
2117 get_seg_limit(e1
, e2
),
2119 cpu_x86_set_cpl(env
, rpl
);
2121 #ifdef TARGET_X86_64
2122 if (env
->hflags
& HF_CS64_MASK
) {
2127 sp_mask
= get_sp_mask(ss_e2
);
2130 /* validate data segments */
2131 validate_seg(env
, R_ES
, rpl
);
2132 validate_seg(env
, R_DS
, rpl
);
2133 validate_seg(env
, R_FS
, rpl
);
2134 validate_seg(env
, R_GS
, rpl
);
2138 SET_ESP(sp
, sp_mask
);
2141 /* NOTE: 'cpl' is the _old_ CPL */
2142 eflags_mask
= TF_MASK
| AC_MASK
| ID_MASK
| RF_MASK
| NT_MASK
;
2144 eflags_mask
|= IOPL_MASK
;
2146 iopl
= (env
->eflags
>> IOPL_SHIFT
) & 3;
2148 eflags_mask
|= IF_MASK
;
2151 eflags_mask
&= 0xffff;
2153 cpu_load_eflags(env
, new_eflags
, eflags_mask
);
2158 POPL(ssp
, sp
, sp_mask
, new_esp
);
2159 POPL(ssp
, sp
, sp_mask
, new_ss
);
2160 POPL(ssp
, sp
, sp_mask
, new_es
);
2161 POPL(ssp
, sp
, sp_mask
, new_ds
);
2162 POPL(ssp
, sp
, sp_mask
, new_fs
);
2163 POPL(ssp
, sp
, sp_mask
, new_gs
);
2165 /* modify processor state */
2166 cpu_load_eflags(env
, new_eflags
, TF_MASK
| AC_MASK
| ID_MASK
|
2167 IF_MASK
| IOPL_MASK
| VM_MASK
| NT_MASK
| VIF_MASK
|
2169 load_seg_vm(env
, R_CS
, new_cs
& 0xffff);
2170 cpu_x86_set_cpl(env
, 3);
2171 load_seg_vm(env
, R_SS
, new_ss
& 0xffff);
2172 load_seg_vm(env
, R_ES
, new_es
& 0xffff);
2173 load_seg_vm(env
, R_DS
, new_ds
& 0xffff);
2174 load_seg_vm(env
, R_FS
, new_fs
& 0xffff);
2175 load_seg_vm(env
, R_GS
, new_gs
& 0xffff);
2177 env
->eip
= new_eip
& 0xffff;
2181 void helper_iret_protected(CPUX86State
*env
, int shift
, int next_eip
)
2183 int tss_selector
, type
;
2186 /* specific case for TSS */
2187 if (env
->eflags
& NT_MASK
) {
2188 #ifdef TARGET_X86_64
2189 if (env
->hflags
& HF_LMA_MASK
) {
2190 raise_exception_err(env
, EXCP0D_GPF
, 0);
2193 tss_selector
= cpu_lduw_kernel(env
, env
->tr
.base
+ 0);
2194 if (tss_selector
& 4) {
2195 raise_exception_err(env
, EXCP0A_TSS
, tss_selector
& 0xfffc);
2197 if (load_segment(env
, &e1
, &e2
, tss_selector
) != 0) {
2198 raise_exception_err(env
, EXCP0A_TSS
, tss_selector
& 0xfffc);
2200 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x17;
2201 /* NOTE: we check both segment and busy TSS */
2203 raise_exception_err(env
, EXCP0A_TSS
, tss_selector
& 0xfffc);
2205 switch_tss(env
, tss_selector
, e1
, e2
, SWITCH_TSS_IRET
, next_eip
);
2207 helper_ret_protected(env
, shift
, 1, 0);
2209 env
->hflags2
&= ~HF2_NMI_MASK
;
2212 void helper_lret_protected(CPUX86State
*env
, int shift
, int addend
)
2214 helper_ret_protected(env
, shift
, 0, addend
);
2217 void helper_sysenter(CPUX86State
*env
)
2219 if (env
->sysenter_cs
== 0) {
2220 raise_exception_err(env
, EXCP0D_GPF
, 0);
2222 env
->eflags
&= ~(VM_MASK
| IF_MASK
| RF_MASK
);
2223 cpu_x86_set_cpl(env
, 0);
2225 #ifdef TARGET_X86_64
2226 if (env
->hflags
& HF_LMA_MASK
) {
2227 cpu_x86_load_seg_cache(env
, R_CS
, env
->sysenter_cs
& 0xfffc,
2229 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2231 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
|
2236 cpu_x86_load_seg_cache(env
, R_CS
, env
->sysenter_cs
& 0xfffc,
2238 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2240 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
);
2242 cpu_x86_load_seg_cache(env
, R_SS
, (env
->sysenter_cs
+ 8) & 0xfffc,
2244 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2246 DESC_W_MASK
| DESC_A_MASK
);
2247 ESP
= env
->sysenter_esp
;
2248 EIP
= env
->sysenter_eip
;
2251 void helper_sysexit(CPUX86State
*env
, int dflag
)
2255 cpl
= env
->hflags
& HF_CPL_MASK
;
2256 if (env
->sysenter_cs
== 0 || cpl
!= 0) {
2257 raise_exception_err(env
, EXCP0D_GPF
, 0);
2259 cpu_x86_set_cpl(env
, 3);
2260 #ifdef TARGET_X86_64
2262 cpu_x86_load_seg_cache(env
, R_CS
, ((env
->sysenter_cs
+ 32) & 0xfffc) |
2264 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2265 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
2266 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
|
2268 cpu_x86_load_seg_cache(env
, R_SS
, ((env
->sysenter_cs
+ 40) & 0xfffc) |
2270 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2271 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
2272 DESC_W_MASK
| DESC_A_MASK
);
2276 cpu_x86_load_seg_cache(env
, R_CS
, ((env
->sysenter_cs
+ 16) & 0xfffc) |
2278 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2279 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
2280 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
);
2281 cpu_x86_load_seg_cache(env
, R_SS
, ((env
->sysenter_cs
+ 24) & 0xfffc) |
2283 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2284 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
2285 DESC_W_MASK
| DESC_A_MASK
);
2291 target_ulong
helper_lsl(CPUX86State
*env
, target_ulong selector1
)
2294 uint32_t e1
, e2
, eflags
, selector
;
2295 int rpl
, dpl
, cpl
, type
;
2297 selector
= selector1
& 0xffff;
2298 eflags
= cpu_cc_compute_all(env
, CC_OP
);
2299 if ((selector
& 0xfffc) == 0) {
2302 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
2306 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
2307 cpl
= env
->hflags
& HF_CPL_MASK
;
2308 if (e2
& DESC_S_MASK
) {
2309 if ((e2
& DESC_CS_MASK
) && (e2
& DESC_C_MASK
)) {
2312 if (dpl
< cpl
|| dpl
< rpl
) {
2317 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
2328 if (dpl
< cpl
|| dpl
< rpl
) {
2330 CC_SRC
= eflags
& ~CC_Z
;
2334 limit
= get_seg_limit(e1
, e2
);
2335 CC_SRC
= eflags
| CC_Z
;
2339 target_ulong
helper_lar(CPUX86State
*env
, target_ulong selector1
)
2341 uint32_t e1
, e2
, eflags
, selector
;
2342 int rpl
, dpl
, cpl
, type
;
2344 selector
= selector1
& 0xffff;
2345 eflags
= cpu_cc_compute_all(env
, CC_OP
);
2346 if ((selector
& 0xfffc) == 0) {
2349 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
2353 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
2354 cpl
= env
->hflags
& HF_CPL_MASK
;
2355 if (e2
& DESC_S_MASK
) {
2356 if ((e2
& DESC_CS_MASK
) && (e2
& DESC_C_MASK
)) {
2359 if (dpl
< cpl
|| dpl
< rpl
) {
2364 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
2378 if (dpl
< cpl
|| dpl
< rpl
) {
2380 CC_SRC
= eflags
& ~CC_Z
;
2384 CC_SRC
= eflags
| CC_Z
;
2385 return e2
& 0x00f0ff00;
2388 void helper_verr(CPUX86State
*env
, target_ulong selector1
)
2390 uint32_t e1
, e2
, eflags
, selector
;
2393 selector
= selector1
& 0xffff;
2394 eflags
= cpu_cc_compute_all(env
, CC_OP
);
2395 if ((selector
& 0xfffc) == 0) {
2398 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
2401 if (!(e2
& DESC_S_MASK
)) {
2405 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
2406 cpl
= env
->hflags
& HF_CPL_MASK
;
2407 if (e2
& DESC_CS_MASK
) {
2408 if (!(e2
& DESC_R_MASK
)) {
2411 if (!(e2
& DESC_C_MASK
)) {
2412 if (dpl
< cpl
|| dpl
< rpl
) {
2417 if (dpl
< cpl
|| dpl
< rpl
) {
2419 CC_SRC
= eflags
& ~CC_Z
;
2423 CC_SRC
= eflags
| CC_Z
;
2426 void helper_verw(CPUX86State
*env
, target_ulong selector1
)
2428 uint32_t e1
, e2
, eflags
, selector
;
2431 selector
= selector1
& 0xffff;
2432 eflags
= cpu_cc_compute_all(env
, CC_OP
);
2433 if ((selector
& 0xfffc) == 0) {
2436 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
2439 if (!(e2
& DESC_S_MASK
)) {
2443 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
2444 cpl
= env
->hflags
& HF_CPL_MASK
;
2445 if (e2
& DESC_CS_MASK
) {
2448 if (dpl
< cpl
|| dpl
< rpl
) {
2451 if (!(e2
& DESC_W_MASK
)) {
2453 CC_SRC
= eflags
& ~CC_Z
;
2457 CC_SRC
= eflags
| CC_Z
;
2460 #if defined(CONFIG_USER_ONLY)
2461 void cpu_x86_load_seg(CPUX86State
*env
, int seg_reg
, int selector
)
2463 if (!(env
->cr
[0] & CR0_PE_MASK
) || (env
->eflags
& VM_MASK
)) {
2465 cpu_x86_load_seg_cache(env
, seg_reg
, selector
,
2466 (selector
<< 4), 0xffff, 0);
2468 helper_load_seg(env
, seg_reg
, selector
);