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.1 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/>.
21 #include "qemu/osdep.h"
24 #include "exec/helper-proto.h"
25 #include "exec/exec-all.h"
26 #include "exec/cpu_ldst.h"
28 #include "helper-tcg.h"
29 #include "seg_helper.h"
31 int get_pg_mode(CPUX86State
*env
)
34 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
37 if (env
->cr
[0] & CR0_WP_MASK
) {
38 pg_mode
|= PG_MODE_WP
;
40 if (env
->cr
[4] & CR4_PAE_MASK
) {
41 pg_mode
|= PG_MODE_PAE
;
42 if (env
->efer
& MSR_EFER_NXE
) {
43 pg_mode
|= PG_MODE_NXE
;
46 if (env
->cr
[4] & CR4_PSE_MASK
) {
47 pg_mode
|= PG_MODE_PSE
;
49 if (env
->cr
[4] & CR4_SMEP_MASK
) {
50 pg_mode
|= PG_MODE_SMEP
;
52 if (env
->hflags
& HF_LMA_MASK
) {
53 pg_mode
|= PG_MODE_LMA
;
54 if (env
->cr
[4] & CR4_PKE_MASK
) {
55 pg_mode
|= PG_MODE_PKE
;
57 if (env
->cr
[4] & CR4_PKS_MASK
) {
58 pg_mode
|= PG_MODE_PKS
;
60 if (env
->cr
[4] & CR4_LA57_MASK
) {
61 pg_mode
|= PG_MODE_LA57
;
67 /* return non zero if error */
68 static inline int load_segment_ra(CPUX86State
*env
, uint32_t *e1_ptr
,
69 uint32_t *e2_ptr
, int selector
,
81 index
= selector
& ~7;
82 if ((index
+ 7) > dt
->limit
) {
85 ptr
= dt
->base
+ index
;
86 *e1_ptr
= cpu_ldl_kernel_ra(env
, ptr
, retaddr
);
87 *e2_ptr
= cpu_ldl_kernel_ra(env
, ptr
+ 4, retaddr
);
91 static inline int load_segment(CPUX86State
*env
, uint32_t *e1_ptr
,
92 uint32_t *e2_ptr
, int selector
)
94 return load_segment_ra(env
, e1_ptr
, e2_ptr
, selector
, 0);
97 static inline unsigned int get_seg_limit(uint32_t e1
, uint32_t e2
)
101 limit
= (e1
& 0xffff) | (e2
& 0x000f0000);
102 if (e2
& DESC_G_MASK
) {
103 limit
= (limit
<< 12) | 0xfff;
108 static inline uint32_t get_seg_base(uint32_t e1
, uint32_t e2
)
110 return (e1
>> 16) | ((e2
& 0xff) << 16) | (e2
& 0xff000000);
113 static inline void load_seg_cache_raw_dt(SegmentCache
*sc
, uint32_t e1
,
116 sc
->base
= get_seg_base(e1
, e2
);
117 sc
->limit
= get_seg_limit(e1
, e2
);
121 /* init the segment cache in vm86 mode. */
122 static inline void load_seg_vm(CPUX86State
*env
, int seg
, int selector
)
126 cpu_x86_load_seg_cache(env
, seg
, selector
, (selector
<< 4), 0xffff,
127 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
128 DESC_A_MASK
| (3 << DESC_DPL_SHIFT
));
131 static inline void get_ss_esp_from_tss(CPUX86State
*env
, uint32_t *ss_ptr
,
132 uint32_t *esp_ptr
, int dpl
,
135 X86CPU
*cpu
= env_archcpu(env
);
136 int type
, index
, shift
;
141 printf("TR: base=%p limit=%x\n", env
->tr
.base
, env
->tr
.limit
);
142 for (i
= 0; i
< env
->tr
.limit
; i
++) {
143 printf("%02x ", env
->tr
.base
[i
]);
152 if (!(env
->tr
.flags
& DESC_P_MASK
)) {
153 cpu_abort(CPU(cpu
), "invalid tss");
155 type
= (env
->tr
.flags
>> DESC_TYPE_SHIFT
) & 0xf;
156 if ((type
& 7) != 1) {
157 cpu_abort(CPU(cpu
), "invalid tss type");
160 index
= (dpl
* 4 + 2) << shift
;
161 if (index
+ (4 << shift
) - 1 > env
->tr
.limit
) {
162 raise_exception_err_ra(env
, EXCP0A_TSS
, env
->tr
.selector
& 0xfffc, retaddr
);
165 *esp_ptr
= cpu_lduw_kernel_ra(env
, env
->tr
.base
+ index
, retaddr
);
166 *ss_ptr
= cpu_lduw_kernel_ra(env
, env
->tr
.base
+ index
+ 2, retaddr
);
168 *esp_ptr
= cpu_ldl_kernel_ra(env
, env
->tr
.base
+ index
, retaddr
);
169 *ss_ptr
= cpu_lduw_kernel_ra(env
, env
->tr
.base
+ index
+ 4, retaddr
);
173 static void tss_load_seg(CPUX86State
*env
, X86Seg seg_reg
, int selector
,
174 int cpl
, uintptr_t retaddr
)
179 if ((selector
& 0xfffc) != 0) {
180 if (load_segment_ra(env
, &e1
, &e2
, selector
, retaddr
) != 0) {
181 raise_exception_err_ra(env
, EXCP0A_TSS
, selector
& 0xfffc, retaddr
);
183 if (!(e2
& DESC_S_MASK
)) {
184 raise_exception_err_ra(env
, EXCP0A_TSS
, selector
& 0xfffc, retaddr
);
187 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
188 if (seg_reg
== R_CS
) {
189 if (!(e2
& DESC_CS_MASK
)) {
190 raise_exception_err_ra(env
, EXCP0A_TSS
, selector
& 0xfffc, retaddr
);
193 raise_exception_err_ra(env
, EXCP0A_TSS
, selector
& 0xfffc, retaddr
);
195 } else if (seg_reg
== R_SS
) {
196 /* SS must be writable data */
197 if ((e2
& DESC_CS_MASK
) || !(e2
& DESC_W_MASK
)) {
198 raise_exception_err_ra(env
, EXCP0A_TSS
, selector
& 0xfffc, retaddr
);
200 if (dpl
!= cpl
|| dpl
!= rpl
) {
201 raise_exception_err_ra(env
, EXCP0A_TSS
, selector
& 0xfffc, retaddr
);
204 /* not readable code */
205 if ((e2
& DESC_CS_MASK
) && !(e2
& DESC_R_MASK
)) {
206 raise_exception_err_ra(env
, EXCP0A_TSS
, selector
& 0xfffc, retaddr
);
208 /* if data or non conforming code, checks the rights */
209 if (((e2
>> DESC_TYPE_SHIFT
) & 0xf) < 12) {
210 if (dpl
< cpl
|| dpl
< rpl
) {
211 raise_exception_err_ra(env
, EXCP0A_TSS
, selector
& 0xfffc, retaddr
);
215 if (!(e2
& DESC_P_MASK
)) {
216 raise_exception_err_ra(env
, EXCP0B_NOSEG
, selector
& 0xfffc, retaddr
);
218 cpu_x86_load_seg_cache(env
, seg_reg
, selector
,
219 get_seg_base(e1
, e2
),
220 get_seg_limit(e1
, e2
),
223 if (seg_reg
== R_SS
|| seg_reg
== R_CS
) {
224 raise_exception_err_ra(env
, EXCP0A_TSS
, selector
& 0xfffc, retaddr
);
229 #define SWITCH_TSS_JMP 0
230 #define SWITCH_TSS_IRET 1
231 #define SWITCH_TSS_CALL 2
233 /* XXX: restore CPU state in registers (PowerPC case) */
234 static void switch_tss_ra(CPUX86State
*env
, int tss_selector
,
235 uint32_t e1
, uint32_t e2
, int source
,
236 uint32_t next_eip
, uintptr_t retaddr
)
238 int tss_limit
, tss_limit_max
, type
, old_tss_limit_max
, old_type
, v1
, v2
, i
;
239 target_ulong tss_base
;
240 uint32_t new_regs
[8], new_segs
[6];
241 uint32_t new_eflags
, new_eip
, new_cr3
, new_ldt
, new_trap
;
242 uint32_t old_eflags
, eflags_mask
;
247 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
248 LOG_PCALL("switch_tss: sel=0x%04x type=%d src=%d\n", tss_selector
, type
,
251 /* if task gate, we read the TSS segment and we load it */
253 if (!(e2
& DESC_P_MASK
)) {
254 raise_exception_err_ra(env
, EXCP0B_NOSEG
, tss_selector
& 0xfffc, retaddr
);
256 tss_selector
= e1
>> 16;
257 if (tss_selector
& 4) {
258 raise_exception_err_ra(env
, EXCP0A_TSS
, tss_selector
& 0xfffc, retaddr
);
260 if (load_segment_ra(env
, &e1
, &e2
, tss_selector
, retaddr
) != 0) {
261 raise_exception_err_ra(env
, EXCP0D_GPF
, tss_selector
& 0xfffc, retaddr
);
263 if (e2
& DESC_S_MASK
) {
264 raise_exception_err_ra(env
, EXCP0D_GPF
, tss_selector
& 0xfffc, retaddr
);
266 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
267 if ((type
& 7) != 1) {
268 raise_exception_err_ra(env
, EXCP0D_GPF
, tss_selector
& 0xfffc, retaddr
);
272 if (!(e2
& DESC_P_MASK
)) {
273 raise_exception_err_ra(env
, EXCP0B_NOSEG
, tss_selector
& 0xfffc, retaddr
);
281 tss_limit
= get_seg_limit(e1
, e2
);
282 tss_base
= get_seg_base(e1
, e2
);
283 if ((tss_selector
& 4) != 0 ||
284 tss_limit
< tss_limit_max
) {
285 raise_exception_err_ra(env
, EXCP0A_TSS
, tss_selector
& 0xfffc, retaddr
);
287 old_type
= (env
->tr
.flags
>> DESC_TYPE_SHIFT
) & 0xf;
289 old_tss_limit_max
= 103;
291 old_tss_limit_max
= 43;
294 /* read all the registers from the new TSS */
297 new_cr3
= cpu_ldl_kernel_ra(env
, tss_base
+ 0x1c, retaddr
);
298 new_eip
= cpu_ldl_kernel_ra(env
, tss_base
+ 0x20, retaddr
);
299 new_eflags
= cpu_ldl_kernel_ra(env
, tss_base
+ 0x24, retaddr
);
300 for (i
= 0; i
< 8; i
++) {
301 new_regs
[i
] = cpu_ldl_kernel_ra(env
, tss_base
+ (0x28 + i
* 4),
304 for (i
= 0; i
< 6; i
++) {
305 new_segs
[i
] = cpu_lduw_kernel_ra(env
, tss_base
+ (0x48 + i
* 4),
308 new_ldt
= cpu_lduw_kernel_ra(env
, tss_base
+ 0x60, retaddr
);
309 new_trap
= cpu_ldl_kernel_ra(env
, tss_base
+ 0x64, retaddr
);
313 new_eip
= cpu_lduw_kernel_ra(env
, tss_base
+ 0x0e, retaddr
);
314 new_eflags
= cpu_lduw_kernel_ra(env
, tss_base
+ 0x10, retaddr
);
315 for (i
= 0; i
< 8; i
++) {
316 new_regs
[i
] = cpu_lduw_kernel_ra(env
, tss_base
+ (0x12 + i
* 2), retaddr
);
318 for (i
= 0; i
< 4; i
++) {
319 new_segs
[i
] = cpu_lduw_kernel_ra(env
, tss_base
+ (0x22 + i
* 2),
322 new_ldt
= cpu_lduw_kernel_ra(env
, tss_base
+ 0x2a, retaddr
);
327 /* XXX: avoid a compiler warning, see
328 http://support.amd.com/us/Processor_TechDocs/24593.pdf
329 chapters 12.2.5 and 13.2.4 on how to implement TSS Trap bit */
332 /* NOTE: we must avoid memory exceptions during the task switch,
333 so we make dummy accesses before */
334 /* XXX: it can still fail in some cases, so a bigger hack is
335 necessary to valid the TLB after having done the accesses */
337 v1
= cpu_ldub_kernel_ra(env
, env
->tr
.base
, retaddr
);
338 v2
= cpu_ldub_kernel_ra(env
, env
->tr
.base
+ old_tss_limit_max
, retaddr
);
339 cpu_stb_kernel_ra(env
, env
->tr
.base
, v1
, retaddr
);
340 cpu_stb_kernel_ra(env
, env
->tr
.base
+ old_tss_limit_max
, v2
, retaddr
);
342 /* clear busy bit (it is restartable) */
343 if (source
== SWITCH_TSS_JMP
|| source
== SWITCH_TSS_IRET
) {
347 ptr
= env
->gdt
.base
+ (env
->tr
.selector
& ~7);
348 e2
= cpu_ldl_kernel_ra(env
, ptr
+ 4, retaddr
);
349 e2
&= ~DESC_TSS_BUSY_MASK
;
350 cpu_stl_kernel_ra(env
, ptr
+ 4, e2
, retaddr
);
352 old_eflags
= cpu_compute_eflags(env
);
353 if (source
== SWITCH_TSS_IRET
) {
354 old_eflags
&= ~NT_MASK
;
357 /* save the current state in the old TSS */
360 cpu_stl_kernel_ra(env
, env
->tr
.base
+ 0x20, next_eip
, retaddr
);
361 cpu_stl_kernel_ra(env
, env
->tr
.base
+ 0x24, old_eflags
, retaddr
);
362 cpu_stl_kernel_ra(env
, env
->tr
.base
+ (0x28 + 0 * 4), env
->regs
[R_EAX
], retaddr
);
363 cpu_stl_kernel_ra(env
, env
->tr
.base
+ (0x28 + 1 * 4), env
->regs
[R_ECX
], retaddr
);
364 cpu_stl_kernel_ra(env
, env
->tr
.base
+ (0x28 + 2 * 4), env
->regs
[R_EDX
], retaddr
);
365 cpu_stl_kernel_ra(env
, env
->tr
.base
+ (0x28 + 3 * 4), env
->regs
[R_EBX
], retaddr
);
366 cpu_stl_kernel_ra(env
, env
->tr
.base
+ (0x28 + 4 * 4), env
->regs
[R_ESP
], retaddr
);
367 cpu_stl_kernel_ra(env
, env
->tr
.base
+ (0x28 + 5 * 4), env
->regs
[R_EBP
], retaddr
);
368 cpu_stl_kernel_ra(env
, env
->tr
.base
+ (0x28 + 6 * 4), env
->regs
[R_ESI
], retaddr
);
369 cpu_stl_kernel_ra(env
, env
->tr
.base
+ (0x28 + 7 * 4), env
->regs
[R_EDI
], retaddr
);
370 for (i
= 0; i
< 6; i
++) {
371 cpu_stw_kernel_ra(env
, env
->tr
.base
+ (0x48 + i
* 4),
372 env
->segs
[i
].selector
, retaddr
);
376 cpu_stw_kernel_ra(env
, env
->tr
.base
+ 0x0e, next_eip
, retaddr
);
377 cpu_stw_kernel_ra(env
, env
->tr
.base
+ 0x10, old_eflags
, retaddr
);
378 cpu_stw_kernel_ra(env
, env
->tr
.base
+ (0x12 + 0 * 2), env
->regs
[R_EAX
], retaddr
);
379 cpu_stw_kernel_ra(env
, env
->tr
.base
+ (0x12 + 1 * 2), env
->regs
[R_ECX
], retaddr
);
380 cpu_stw_kernel_ra(env
, env
->tr
.base
+ (0x12 + 2 * 2), env
->regs
[R_EDX
], retaddr
);
381 cpu_stw_kernel_ra(env
, env
->tr
.base
+ (0x12 + 3 * 2), env
->regs
[R_EBX
], retaddr
);
382 cpu_stw_kernel_ra(env
, env
->tr
.base
+ (0x12 + 4 * 2), env
->regs
[R_ESP
], retaddr
);
383 cpu_stw_kernel_ra(env
, env
->tr
.base
+ (0x12 + 5 * 2), env
->regs
[R_EBP
], retaddr
);
384 cpu_stw_kernel_ra(env
, env
->tr
.base
+ (0x12 + 6 * 2), env
->regs
[R_ESI
], retaddr
);
385 cpu_stw_kernel_ra(env
, env
->tr
.base
+ (0x12 + 7 * 2), env
->regs
[R_EDI
], retaddr
);
386 for (i
= 0; i
< 4; i
++) {
387 cpu_stw_kernel_ra(env
, env
->tr
.base
+ (0x22 + i
* 2),
388 env
->segs
[i
].selector
, retaddr
);
392 /* now if an exception occurs, it will occurs in the next task
395 if (source
== SWITCH_TSS_CALL
) {
396 cpu_stw_kernel_ra(env
, tss_base
, env
->tr
.selector
, retaddr
);
397 new_eflags
|= NT_MASK
;
401 if (source
== SWITCH_TSS_JMP
|| source
== SWITCH_TSS_CALL
) {
405 ptr
= env
->gdt
.base
+ (tss_selector
& ~7);
406 e2
= cpu_ldl_kernel_ra(env
, ptr
+ 4, retaddr
);
407 e2
|= DESC_TSS_BUSY_MASK
;
408 cpu_stl_kernel_ra(env
, ptr
+ 4, e2
, retaddr
);
411 /* set the new CPU state */
412 /* from this point, any exception which occurs can give problems */
413 env
->cr
[0] |= CR0_TS_MASK
;
414 env
->hflags
|= HF_TS_MASK
;
415 env
->tr
.selector
= tss_selector
;
416 env
->tr
.base
= tss_base
;
417 env
->tr
.limit
= tss_limit
;
418 env
->tr
.flags
= e2
& ~DESC_TSS_BUSY_MASK
;
420 if ((type
& 8) && (env
->cr
[0] & CR0_PG_MASK
)) {
421 cpu_x86_update_cr3(env
, new_cr3
);
424 /* load all registers without an exception, then reload them with
425 possible exception */
427 eflags_mask
= TF_MASK
| AC_MASK
| ID_MASK
|
428 IF_MASK
| IOPL_MASK
| VM_MASK
| RF_MASK
| NT_MASK
;
430 cpu_load_eflags(env
, new_eflags
, eflags_mask
);
431 for (i
= 0; i
< 8; i
++) {
432 env
->regs
[i
] = new_regs
[i
];
435 cpu_load_eflags(env
, new_eflags
, eflags_mask
& 0xffff);
436 for (i
= 0; i
< 8; i
++) {
437 env
->regs
[i
] = (env
->regs
[i
] & 0xffff0000) | new_regs
[i
];
440 if (new_eflags
& VM_MASK
) {
441 for (i
= 0; i
< 6; i
++) {
442 load_seg_vm(env
, i
, new_segs
[i
]);
445 /* first just selectors as the rest may trigger exceptions */
446 for (i
= 0; i
< 6; i
++) {
447 cpu_x86_load_seg_cache(env
, i
, new_segs
[i
], 0, 0, 0);
451 env
->ldt
.selector
= new_ldt
& ~4;
458 raise_exception_err_ra(env
, EXCP0A_TSS
, new_ldt
& 0xfffc, retaddr
);
461 if ((new_ldt
& 0xfffc) != 0) {
463 index
= new_ldt
& ~7;
464 if ((index
+ 7) > dt
->limit
) {
465 raise_exception_err_ra(env
, EXCP0A_TSS
, new_ldt
& 0xfffc, retaddr
);
467 ptr
= dt
->base
+ index
;
468 e1
= cpu_ldl_kernel_ra(env
, ptr
, retaddr
);
469 e2
= cpu_ldl_kernel_ra(env
, ptr
+ 4, retaddr
);
470 if ((e2
& DESC_S_MASK
) || ((e2
>> DESC_TYPE_SHIFT
) & 0xf) != 2) {
471 raise_exception_err_ra(env
, EXCP0A_TSS
, new_ldt
& 0xfffc, retaddr
);
473 if (!(e2
& DESC_P_MASK
)) {
474 raise_exception_err_ra(env
, EXCP0A_TSS
, new_ldt
& 0xfffc, retaddr
);
476 load_seg_cache_raw_dt(&env
->ldt
, e1
, e2
);
479 /* load the segments */
480 if (!(new_eflags
& VM_MASK
)) {
481 int cpl
= new_segs
[R_CS
] & 3;
482 tss_load_seg(env
, R_CS
, new_segs
[R_CS
], cpl
, retaddr
);
483 tss_load_seg(env
, R_SS
, new_segs
[R_SS
], cpl
, retaddr
);
484 tss_load_seg(env
, R_ES
, new_segs
[R_ES
], cpl
, retaddr
);
485 tss_load_seg(env
, R_DS
, new_segs
[R_DS
], cpl
, retaddr
);
486 tss_load_seg(env
, R_FS
, new_segs
[R_FS
], cpl
, retaddr
);
487 tss_load_seg(env
, R_GS
, new_segs
[R_GS
], cpl
, retaddr
);
490 /* check that env->eip is in the CS segment limits */
491 if (new_eip
> env
->segs
[R_CS
].limit
) {
492 /* XXX: different exception if CALL? */
493 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, retaddr
);
496 #ifndef CONFIG_USER_ONLY
497 /* reset local breakpoints */
498 if (env
->dr
[7] & DR7_LOCAL_BP_MASK
) {
499 cpu_x86_update_dr7(env
, env
->dr
[7] & ~DR7_LOCAL_BP_MASK
);
504 static void switch_tss(CPUX86State
*env
, int tss_selector
,
505 uint32_t e1
, uint32_t e2
, int source
,
508 switch_tss_ra(env
, tss_selector
, e1
, e2
, source
, next_eip
, 0);
511 static inline unsigned int get_sp_mask(unsigned int e2
)
514 if (e2
& DESC_L_MASK
) {
518 if (e2
& DESC_B_MASK
) {
525 int exception_has_error_code(int intno
)
541 #define SET_ESP(val, sp_mask) \
543 if ((sp_mask) == 0xffff) { \
544 env->regs[R_ESP] = (env->regs[R_ESP] & ~0xffff) | \
546 } else if ((sp_mask) == 0xffffffffLL) { \
547 env->regs[R_ESP] = (uint32_t)(val); \
549 env->regs[R_ESP] = (val); \
553 #define SET_ESP(val, sp_mask) \
555 env->regs[R_ESP] = (env->regs[R_ESP] & ~(sp_mask)) | \
556 ((val) & (sp_mask)); \
560 /* in 64-bit machines, this can overflow. So this segment addition macro
561 * can be used to trim the value to 32-bit whenever needed */
562 #define SEG_ADDL(ssp, sp, sp_mask) ((uint32_t)((ssp) + (sp & (sp_mask))))
564 /* XXX: add a is_user flag to have proper security support */
565 #define PUSHW_RA(ssp, sp, sp_mask, val, ra) \
568 cpu_stw_kernel_ra(env, (ssp) + (sp & (sp_mask)), (val), ra); \
571 #define PUSHL_RA(ssp, sp, sp_mask, val, ra) \
574 cpu_stl_kernel_ra(env, SEG_ADDL(ssp, sp, sp_mask), (uint32_t)(val), ra); \
577 #define POPW_RA(ssp, sp, sp_mask, val, ra) \
579 val = cpu_lduw_kernel_ra(env, (ssp) + (sp & (sp_mask)), ra); \
583 #define POPL_RA(ssp, sp, sp_mask, val, ra) \
585 val = (uint32_t)cpu_ldl_kernel_ra(env, SEG_ADDL(ssp, sp, sp_mask), ra); \
589 #define PUSHW(ssp, sp, sp_mask, val) PUSHW_RA(ssp, sp, sp_mask, val, 0)
590 #define PUSHL(ssp, sp, sp_mask, val) PUSHL_RA(ssp, sp, sp_mask, val, 0)
591 #define POPW(ssp, sp, sp_mask, val) POPW_RA(ssp, sp, sp_mask, val, 0)
592 #define POPL(ssp, sp, sp_mask, val) POPL_RA(ssp, sp, sp_mask, val, 0)
594 /* protected mode interrupt */
595 static void do_interrupt_protected(CPUX86State
*env
, int intno
, int is_int
,
596 int error_code
, unsigned int next_eip
,
600 target_ulong ptr
, ssp
;
601 int type
, dpl
, selector
, ss_dpl
, cpl
;
602 int has_error_code
, new_stack
, shift
;
603 uint32_t e1
, e2
, offset
, ss
= 0, esp
, ss_e1
= 0, ss_e2
= 0;
604 uint32_t old_eip
, sp_mask
;
605 int vm86
= env
->eflags
& VM_MASK
;
608 if (!is_int
&& !is_hw
) {
609 has_error_code
= exception_has_error_code(intno
);
618 if (intno
* 8 + 7 > dt
->limit
) {
619 raise_exception_err(env
, EXCP0D_GPF
, intno
* 8 + 2);
621 ptr
= dt
->base
+ intno
* 8;
622 e1
= cpu_ldl_kernel(env
, ptr
);
623 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
624 /* check gate type */
625 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x1f;
627 case 5: /* task gate */
628 case 6: /* 286 interrupt gate */
629 case 7: /* 286 trap gate */
630 case 14: /* 386 interrupt gate */
631 case 15: /* 386 trap gate */
634 raise_exception_err(env
, EXCP0D_GPF
, intno
* 8 + 2);
637 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
638 cpl
= env
->hflags
& HF_CPL_MASK
;
639 /* check privilege if software int */
640 if (is_int
&& dpl
< cpl
) {
641 raise_exception_err(env
, EXCP0D_GPF
, intno
* 8 + 2);
646 /* must do that check here to return the correct error code */
647 if (!(e2
& DESC_P_MASK
)) {
648 raise_exception_err(env
, EXCP0B_NOSEG
, intno
* 8 + 2);
650 switch_tss(env
, intno
* 8, e1
, e2
, SWITCH_TSS_CALL
, old_eip
);
651 if (has_error_code
) {
655 /* push the error code */
656 type
= (env
->tr
.flags
>> DESC_TYPE_SHIFT
) & 0xf;
658 if (env
->segs
[R_SS
].flags
& DESC_B_MASK
) {
663 esp
= (env
->regs
[R_ESP
] - (2 << shift
)) & mask
;
664 ssp
= env
->segs
[R_SS
].base
+ esp
;
666 cpu_stl_kernel(env
, ssp
, error_code
);
668 cpu_stw_kernel(env
, ssp
, error_code
);
675 /* Otherwise, trap or interrupt gate */
677 /* check valid bit */
678 if (!(e2
& DESC_P_MASK
)) {
679 raise_exception_err(env
, EXCP0B_NOSEG
, intno
* 8 + 2);
682 offset
= (e2
& 0xffff0000) | (e1
& 0x0000ffff);
683 if ((selector
& 0xfffc) == 0) {
684 raise_exception_err(env
, EXCP0D_GPF
, 0);
686 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
687 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
689 if (!(e2
& DESC_S_MASK
) || !(e2
& (DESC_CS_MASK
))) {
690 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
692 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
694 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
696 if (!(e2
& DESC_P_MASK
)) {
697 raise_exception_err(env
, EXCP0B_NOSEG
, selector
& 0xfffc);
699 if (e2
& DESC_C_MASK
) {
703 /* to inner privilege */
704 get_ss_esp_from_tss(env
, &ss
, &esp
, dpl
, 0);
705 if ((ss
& 0xfffc) == 0) {
706 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
708 if ((ss
& 3) != dpl
) {
709 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
711 if (load_segment(env
, &ss_e1
, &ss_e2
, ss
) != 0) {
712 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
714 ss_dpl
= (ss_e2
>> DESC_DPL_SHIFT
) & 3;
716 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
718 if (!(ss_e2
& DESC_S_MASK
) ||
719 (ss_e2
& DESC_CS_MASK
) ||
720 !(ss_e2
& DESC_W_MASK
)) {
721 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
723 if (!(ss_e2
& DESC_P_MASK
)) {
724 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
727 sp_mask
= get_sp_mask(ss_e2
);
728 ssp
= get_seg_base(ss_e1
, ss_e2
);
730 /* to same privilege */
732 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
735 sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
736 ssp
= env
->segs
[R_SS
].base
;
737 esp
= env
->regs
[R_ESP
];
743 /* XXX: check that enough room is available */
744 push_size
= 6 + (new_stack
<< 2) + (has_error_code
<< 1);
753 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_GS
].selector
);
754 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_FS
].selector
);
755 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_DS
].selector
);
756 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_ES
].selector
);
758 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_SS
].selector
);
759 PUSHL(ssp
, esp
, sp_mask
, env
->regs
[R_ESP
]);
761 PUSHL(ssp
, esp
, sp_mask
, cpu_compute_eflags(env
));
762 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_CS
].selector
);
763 PUSHL(ssp
, esp
, sp_mask
, old_eip
);
764 if (has_error_code
) {
765 PUSHL(ssp
, esp
, sp_mask
, error_code
);
770 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_GS
].selector
);
771 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_FS
].selector
);
772 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_DS
].selector
);
773 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_ES
].selector
);
775 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_SS
].selector
);
776 PUSHW(ssp
, esp
, sp_mask
, env
->regs
[R_ESP
]);
778 PUSHW(ssp
, esp
, sp_mask
, cpu_compute_eflags(env
));
779 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_CS
].selector
);
780 PUSHW(ssp
, esp
, sp_mask
, old_eip
);
781 if (has_error_code
) {
782 PUSHW(ssp
, esp
, sp_mask
, error_code
);
786 /* interrupt gate clear IF mask */
787 if ((type
& 1) == 0) {
788 env
->eflags
&= ~IF_MASK
;
790 env
->eflags
&= ~(TF_MASK
| VM_MASK
| RF_MASK
| NT_MASK
);
794 cpu_x86_load_seg_cache(env
, R_ES
, 0, 0, 0, 0);
795 cpu_x86_load_seg_cache(env
, R_DS
, 0, 0, 0, 0);
796 cpu_x86_load_seg_cache(env
, R_FS
, 0, 0, 0, 0);
797 cpu_x86_load_seg_cache(env
, R_GS
, 0, 0, 0, 0);
799 ss
= (ss
& ~3) | dpl
;
800 cpu_x86_load_seg_cache(env
, R_SS
, ss
,
801 ssp
, get_seg_limit(ss_e1
, ss_e2
), ss_e2
);
803 SET_ESP(esp
, sp_mask
);
805 selector
= (selector
& ~3) | dpl
;
806 cpu_x86_load_seg_cache(env
, R_CS
, selector
,
807 get_seg_base(e1
, e2
),
808 get_seg_limit(e1
, e2
),
815 #define PUSHQ_RA(sp, val, ra) \
818 cpu_stq_kernel_ra(env, sp, (val), ra); \
821 #define POPQ_RA(sp, val, ra) \
823 val = cpu_ldq_kernel_ra(env, sp, ra); \
827 #define PUSHQ(sp, val) PUSHQ_RA(sp, val, 0)
828 #define POPQ(sp, val) POPQ_RA(sp, val, 0)
830 static inline target_ulong
get_rsp_from_tss(CPUX86State
*env
, int level
)
832 X86CPU
*cpu
= env_archcpu(env
);
838 printf("TR: base=" TARGET_FMT_lx
" limit=%x\n",
839 env
->tr
.base
, env
->tr
.limit
);
842 if (!(env
->tr
.flags
& DESC_P_MASK
)) {
843 cpu_abort(CPU(cpu
), "invalid tss");
845 index
= 8 * level
+ 4;
846 if ((index
+ 7) > env
->tr
.limit
) {
847 raise_exception_err(env
, EXCP0A_TSS
, env
->tr
.selector
& 0xfffc);
850 rsp
= cpu_ldq_kernel(env
, env
->tr
.base
+ index
);
852 /* test virtual address sign extension */
853 pg_mode
= get_pg_mode(env
);
854 sext
= (int64_t)rsp
>> (pg_mode
& PG_MODE_LA57
? 56 : 47);
855 if (sext
!= 0 && sext
!= -1) {
856 raise_exception_err(env
, EXCP0C_STACK
, 0);
862 /* 64 bit interrupt */
863 static void do_interrupt64(CPUX86State
*env
, int intno
, int is_int
,
864 int error_code
, target_ulong next_eip
, int is_hw
)
868 int type
, dpl
, selector
, cpl
, ist
;
869 int has_error_code
, new_stack
;
870 uint32_t e1
, e2
, e3
, ss
;
871 target_ulong old_eip
, esp
, offset
;
874 if (!is_int
&& !is_hw
) {
875 has_error_code
= exception_has_error_code(intno
);
884 if (intno
* 16 + 15 > dt
->limit
) {
885 raise_exception_err(env
, EXCP0D_GPF
, intno
* 8 + 2);
887 ptr
= dt
->base
+ intno
* 16;
888 e1
= cpu_ldl_kernel(env
, ptr
);
889 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
890 e3
= cpu_ldl_kernel(env
, ptr
+ 8);
891 /* check gate type */
892 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x1f;
894 case 14: /* 386 interrupt gate */
895 case 15: /* 386 trap gate */
898 raise_exception_err(env
, EXCP0D_GPF
, intno
* 8 + 2);
901 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
902 cpl
= env
->hflags
& HF_CPL_MASK
;
903 /* check privilege if software int */
904 if (is_int
&& dpl
< cpl
) {
905 raise_exception_err(env
, EXCP0D_GPF
, intno
* 8 + 2);
907 /* check valid bit */
908 if (!(e2
& DESC_P_MASK
)) {
909 raise_exception_err(env
, EXCP0B_NOSEG
, intno
* 8 + 2);
912 offset
= ((target_ulong
)e3
<< 32) | (e2
& 0xffff0000) | (e1
& 0x0000ffff);
914 if ((selector
& 0xfffc) == 0) {
915 raise_exception_err(env
, EXCP0D_GPF
, 0);
918 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
919 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
921 if (!(e2
& DESC_S_MASK
) || !(e2
& (DESC_CS_MASK
))) {
922 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
924 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
926 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
928 if (!(e2
& DESC_P_MASK
)) {
929 raise_exception_err(env
, EXCP0B_NOSEG
, selector
& 0xfffc);
931 if (!(e2
& DESC_L_MASK
) || (e2
& DESC_B_MASK
)) {
932 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
934 if (e2
& DESC_C_MASK
) {
937 if (dpl
< cpl
|| ist
!= 0) {
938 /* to inner privilege */
940 esp
= get_rsp_from_tss(env
, ist
!= 0 ? ist
+ 3 : dpl
);
943 /* to same privilege */
944 if (env
->eflags
& VM_MASK
) {
945 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
948 esp
= env
->regs
[R_ESP
];
950 esp
&= ~0xfLL
; /* align stack */
952 PUSHQ(esp
, env
->segs
[R_SS
].selector
);
953 PUSHQ(esp
, env
->regs
[R_ESP
]);
954 PUSHQ(esp
, cpu_compute_eflags(env
));
955 PUSHQ(esp
, env
->segs
[R_CS
].selector
);
957 if (has_error_code
) {
958 PUSHQ(esp
, error_code
);
961 /* interrupt gate clear IF mask */
962 if ((type
& 1) == 0) {
963 env
->eflags
&= ~IF_MASK
;
965 env
->eflags
&= ~(TF_MASK
| VM_MASK
| RF_MASK
| NT_MASK
);
969 cpu_x86_load_seg_cache(env
, R_SS
, ss
, 0, 0, dpl
<< DESC_DPL_SHIFT
);
971 env
->regs
[R_ESP
] = esp
;
973 selector
= (selector
& ~3) | dpl
;
974 cpu_x86_load_seg_cache(env
, R_CS
, selector
,
975 get_seg_base(e1
, e2
),
976 get_seg_limit(e1
, e2
),
981 void helper_sysret(CPUX86State
*env
, int dflag
)
985 if (!(env
->efer
& MSR_EFER_SCE
)) {
986 raise_exception_err_ra(env
, EXCP06_ILLOP
, 0, GETPC());
988 cpl
= env
->hflags
& HF_CPL_MASK
;
989 if (!(env
->cr
[0] & CR0_PE_MASK
) || cpl
!= 0) {
990 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, GETPC());
992 selector
= (env
->star
>> 48) & 0xffff;
993 if (env
->hflags
& HF_LMA_MASK
) {
994 cpu_load_eflags(env
, (uint32_t)(env
->regs
[11]), TF_MASK
| AC_MASK
995 | ID_MASK
| IF_MASK
| IOPL_MASK
| VM_MASK
| RF_MASK
|
998 cpu_x86_load_seg_cache(env
, R_CS
, (selector
+ 16) | 3,
1000 DESC_G_MASK
| DESC_P_MASK
|
1001 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
1002 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
|
1004 env
->eip
= env
->regs
[R_ECX
];
1006 cpu_x86_load_seg_cache(env
, R_CS
, selector
| 3,
1008 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
1009 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
1010 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
);
1011 env
->eip
= (uint32_t)env
->regs
[R_ECX
];
1013 cpu_x86_load_seg_cache(env
, R_SS
, (selector
+ 8) | 3,
1015 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
1016 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
1017 DESC_W_MASK
| DESC_A_MASK
);
1019 env
->eflags
|= IF_MASK
;
1020 cpu_x86_load_seg_cache(env
, R_CS
, selector
| 3,
1022 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
1023 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
1024 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
);
1025 env
->eip
= (uint32_t)env
->regs
[R_ECX
];
1026 cpu_x86_load_seg_cache(env
, R_SS
, (selector
+ 8) | 3,
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
);
1033 #endif /* TARGET_X86_64 */
1035 /* real mode interrupt */
1036 static void do_interrupt_real(CPUX86State
*env
, int intno
, int is_int
,
1037 int error_code
, unsigned int next_eip
)
1040 target_ulong ptr
, ssp
;
1042 uint32_t offset
, esp
;
1043 uint32_t old_cs
, old_eip
;
1045 /* real mode (simpler!) */
1047 if (intno
* 4 + 3 > dt
->limit
) {
1048 raise_exception_err(env
, EXCP0D_GPF
, intno
* 8 + 2);
1050 ptr
= dt
->base
+ intno
* 4;
1051 offset
= cpu_lduw_kernel(env
, ptr
);
1052 selector
= cpu_lduw_kernel(env
, ptr
+ 2);
1053 esp
= env
->regs
[R_ESP
];
1054 ssp
= env
->segs
[R_SS
].base
;
1060 old_cs
= env
->segs
[R_CS
].selector
;
1061 /* XXX: use SS segment size? */
1062 PUSHW(ssp
, esp
, 0xffff, cpu_compute_eflags(env
));
1063 PUSHW(ssp
, esp
, 0xffff, old_cs
);
1064 PUSHW(ssp
, esp
, 0xffff, old_eip
);
1066 /* update processor state */
1067 env
->regs
[R_ESP
] = (env
->regs
[R_ESP
] & ~0xffff) | (esp
& 0xffff);
1069 env
->segs
[R_CS
].selector
= selector
;
1070 env
->segs
[R_CS
].base
= (selector
<< 4);
1071 env
->eflags
&= ~(IF_MASK
| TF_MASK
| AC_MASK
| RF_MASK
);
1075 * Begin execution of an interruption. is_int is TRUE if coming from
1076 * the int instruction. next_eip is the env->eip value AFTER the interrupt
1077 * instruction. It is only relevant if is_int is TRUE.
1079 void do_interrupt_all(X86CPU
*cpu
, int intno
, int is_int
,
1080 int error_code
, target_ulong next_eip
, int is_hw
)
1082 CPUX86State
*env
= &cpu
->env
;
1084 if (qemu_loglevel_mask(CPU_LOG_INT
)) {
1085 if ((env
->cr
[0] & CR0_PE_MASK
)) {
1088 qemu_log("%6d: v=%02x e=%04x i=%d cpl=%d IP=%04x:" TARGET_FMT_lx
1089 " pc=" TARGET_FMT_lx
" SP=%04x:" TARGET_FMT_lx
,
1090 count
, intno
, error_code
, is_int
,
1091 env
->hflags
& HF_CPL_MASK
,
1092 env
->segs
[R_CS
].selector
, env
->eip
,
1093 (int)env
->segs
[R_CS
].base
+ env
->eip
,
1094 env
->segs
[R_SS
].selector
, env
->regs
[R_ESP
]);
1095 if (intno
== 0x0e) {
1096 qemu_log(" CR2=" TARGET_FMT_lx
, env
->cr
[2]);
1098 qemu_log(" env->regs[R_EAX]=" TARGET_FMT_lx
, env
->regs
[R_EAX
]);
1101 log_cpu_state(CPU(cpu
), CPU_DUMP_CCOP
);
1108 ptr
= env
->segs
[R_CS
].base
+ env
->eip
;
1109 for (i
= 0; i
< 16; i
++) {
1110 qemu_log(" %02x", ldub(ptr
+ i
));
1118 if (env
->cr
[0] & CR0_PE_MASK
) {
1119 #if !defined(CONFIG_USER_ONLY)
1120 if (env
->hflags
& HF_GUEST_MASK
) {
1121 handle_even_inj(env
, intno
, is_int
, error_code
, is_hw
, 0);
1124 #ifdef TARGET_X86_64
1125 if (env
->hflags
& HF_LMA_MASK
) {
1126 do_interrupt64(env
, intno
, is_int
, error_code
, next_eip
, is_hw
);
1130 do_interrupt_protected(env
, intno
, is_int
, error_code
, next_eip
,
1134 #if !defined(CONFIG_USER_ONLY)
1135 if (env
->hflags
& HF_GUEST_MASK
) {
1136 handle_even_inj(env
, intno
, is_int
, error_code
, is_hw
, 1);
1139 do_interrupt_real(env
, intno
, is_int
, error_code
, next_eip
);
1142 #if !defined(CONFIG_USER_ONLY)
1143 if (env
->hflags
& HF_GUEST_MASK
) {
1144 CPUState
*cs
= CPU(cpu
);
1145 uint32_t event_inj
= x86_ldl_phys(cs
, env
->vm_vmcb
+
1146 offsetof(struct vmcb
,
1147 control
.event_inj
));
1150 env
->vm_vmcb
+ offsetof(struct vmcb
, control
.event_inj
),
1151 event_inj
& ~SVM_EVTINJ_VALID
);
1156 void do_interrupt_x86_hardirq(CPUX86State
*env
, int intno
, int is_hw
)
1158 do_interrupt_all(env_archcpu(env
), intno
, 0, 0, 0, is_hw
);
1161 void helper_lldt(CPUX86State
*env
, int selector
)
1165 int index
, entry_limit
;
1169 if ((selector
& 0xfffc) == 0) {
1170 /* XXX: NULL selector case: invalid LDT */
1174 if (selector
& 0x4) {
1175 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1178 index
= selector
& ~7;
1179 #ifdef TARGET_X86_64
1180 if (env
->hflags
& HF_LMA_MASK
) {
1187 if ((index
+ entry_limit
) > dt
->limit
) {
1188 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1190 ptr
= dt
->base
+ index
;
1191 e1
= cpu_ldl_kernel_ra(env
, ptr
, GETPC());
1192 e2
= cpu_ldl_kernel_ra(env
, ptr
+ 4, GETPC());
1193 if ((e2
& DESC_S_MASK
) || ((e2
>> DESC_TYPE_SHIFT
) & 0xf) != 2) {
1194 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1196 if (!(e2
& DESC_P_MASK
)) {
1197 raise_exception_err_ra(env
, EXCP0B_NOSEG
, selector
& 0xfffc, GETPC());
1199 #ifdef TARGET_X86_64
1200 if (env
->hflags
& HF_LMA_MASK
) {
1203 e3
= cpu_ldl_kernel_ra(env
, ptr
+ 8, GETPC());
1204 load_seg_cache_raw_dt(&env
->ldt
, e1
, e2
);
1205 env
->ldt
.base
|= (target_ulong
)e3
<< 32;
1209 load_seg_cache_raw_dt(&env
->ldt
, e1
, e2
);
1212 env
->ldt
.selector
= selector
;
1215 void helper_ltr(CPUX86State
*env
, int selector
)
1219 int index
, type
, entry_limit
;
1223 if ((selector
& 0xfffc) == 0) {
1224 /* NULL selector case: invalid TR */
1229 if (selector
& 0x4) {
1230 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1233 index
= selector
& ~7;
1234 #ifdef TARGET_X86_64
1235 if (env
->hflags
& HF_LMA_MASK
) {
1242 if ((index
+ entry_limit
) > dt
->limit
) {
1243 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1245 ptr
= dt
->base
+ index
;
1246 e1
= cpu_ldl_kernel_ra(env
, ptr
, GETPC());
1247 e2
= cpu_ldl_kernel_ra(env
, ptr
+ 4, GETPC());
1248 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
1249 if ((e2
& DESC_S_MASK
) ||
1250 (type
!= 1 && type
!= 9)) {
1251 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1253 if (!(e2
& DESC_P_MASK
)) {
1254 raise_exception_err_ra(env
, EXCP0B_NOSEG
, selector
& 0xfffc, GETPC());
1256 #ifdef TARGET_X86_64
1257 if (env
->hflags
& HF_LMA_MASK
) {
1260 e3
= cpu_ldl_kernel_ra(env
, ptr
+ 8, GETPC());
1261 e4
= cpu_ldl_kernel_ra(env
, ptr
+ 12, GETPC());
1262 if ((e4
>> DESC_TYPE_SHIFT
) & 0xf) {
1263 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1265 load_seg_cache_raw_dt(&env
->tr
, e1
, e2
);
1266 env
->tr
.base
|= (target_ulong
)e3
<< 32;
1270 load_seg_cache_raw_dt(&env
->tr
, e1
, e2
);
1272 e2
|= DESC_TSS_BUSY_MASK
;
1273 cpu_stl_kernel_ra(env
, ptr
+ 4, e2
, GETPC());
1275 env
->tr
.selector
= selector
;
1278 /* only works if protected mode and not VM86. seg_reg must be != R_CS */
1279 void helper_load_seg(CPUX86State
*env
, int seg_reg
, int selector
)
1288 cpl
= env
->hflags
& HF_CPL_MASK
;
1289 if ((selector
& 0xfffc) == 0) {
1290 /* null selector case */
1292 #ifdef TARGET_X86_64
1293 && (!(env
->hflags
& HF_CS64_MASK
) || cpl
== 3)
1296 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, GETPC());
1298 cpu_x86_load_seg_cache(env
, seg_reg
, selector
, 0, 0, 0);
1301 if (selector
& 0x4) {
1306 index
= selector
& ~7;
1307 if ((index
+ 7) > dt
->limit
) {
1308 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1310 ptr
= dt
->base
+ index
;
1311 e1
= cpu_ldl_kernel_ra(env
, ptr
, GETPC());
1312 e2
= cpu_ldl_kernel_ra(env
, ptr
+ 4, GETPC());
1314 if (!(e2
& DESC_S_MASK
)) {
1315 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1318 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1319 if (seg_reg
== R_SS
) {
1320 /* must be writable segment */
1321 if ((e2
& DESC_CS_MASK
) || !(e2
& DESC_W_MASK
)) {
1322 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1324 if (rpl
!= cpl
|| dpl
!= cpl
) {
1325 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1328 /* must be readable segment */
1329 if ((e2
& (DESC_CS_MASK
| DESC_R_MASK
)) == DESC_CS_MASK
) {
1330 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1333 if (!(e2
& DESC_CS_MASK
) || !(e2
& DESC_C_MASK
)) {
1334 /* if not conforming code, test rights */
1335 if (dpl
< cpl
|| dpl
< rpl
) {
1336 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1341 if (!(e2
& DESC_P_MASK
)) {
1342 if (seg_reg
== R_SS
) {
1343 raise_exception_err_ra(env
, EXCP0C_STACK
, selector
& 0xfffc, GETPC());
1345 raise_exception_err_ra(env
, EXCP0B_NOSEG
, selector
& 0xfffc, GETPC());
1349 /* set the access bit if not already set */
1350 if (!(e2
& DESC_A_MASK
)) {
1352 cpu_stl_kernel_ra(env
, ptr
+ 4, e2
, GETPC());
1355 cpu_x86_load_seg_cache(env
, seg_reg
, selector
,
1356 get_seg_base(e1
, e2
),
1357 get_seg_limit(e1
, e2
),
1360 qemu_log("load_seg: sel=0x%04x base=0x%08lx limit=0x%08lx flags=%08x\n",
1361 selector
, (unsigned long)sc
->base
, sc
->limit
, sc
->flags
);
1366 /* protected mode jump */
1367 void helper_ljmp_protected(CPUX86State
*env
, int new_cs
, target_ulong new_eip
,
1368 target_ulong next_eip
)
1371 uint32_t e1
, e2
, cpl
, dpl
, rpl
, limit
;
1373 if ((new_cs
& 0xfffc) == 0) {
1374 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, GETPC());
1376 if (load_segment_ra(env
, &e1
, &e2
, new_cs
, GETPC()) != 0) {
1377 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1379 cpl
= env
->hflags
& HF_CPL_MASK
;
1380 if (e2
& DESC_S_MASK
) {
1381 if (!(e2
& DESC_CS_MASK
)) {
1382 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1384 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1385 if (e2
& DESC_C_MASK
) {
1386 /* conforming code segment */
1388 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1391 /* non conforming code segment */
1394 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1397 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1400 if (!(e2
& DESC_P_MASK
)) {
1401 raise_exception_err_ra(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc, GETPC());
1403 limit
= get_seg_limit(e1
, e2
);
1404 if (new_eip
> limit
&&
1405 (!(env
->hflags
& HF_LMA_MASK
) || !(e2
& DESC_L_MASK
))) {
1406 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, GETPC());
1408 cpu_x86_load_seg_cache(env
, R_CS
, (new_cs
& 0xfffc) | cpl
,
1409 get_seg_base(e1
, e2
), limit
, e2
);
1412 /* jump to call or task gate */
1413 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1415 cpl
= env
->hflags
& HF_CPL_MASK
;
1416 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
1418 #ifdef TARGET_X86_64
1419 if (env
->efer
& MSR_EFER_LMA
) {
1421 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1426 case 1: /* 286 TSS */
1427 case 9: /* 386 TSS */
1428 case 5: /* task gate */
1429 if (dpl
< cpl
|| dpl
< rpl
) {
1430 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1432 switch_tss_ra(env
, new_cs
, e1
, e2
, SWITCH_TSS_JMP
, next_eip
, GETPC());
1434 case 4: /* 286 call gate */
1435 case 12: /* 386 call gate */
1436 if ((dpl
< cpl
) || (dpl
< rpl
)) {
1437 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1439 if (!(e2
& DESC_P_MASK
)) {
1440 raise_exception_err_ra(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc, GETPC());
1443 new_eip
= (e1
& 0xffff);
1445 new_eip
|= (e2
& 0xffff0000);
1448 #ifdef TARGET_X86_64
1449 if (env
->efer
& MSR_EFER_LMA
) {
1450 /* load the upper 8 bytes of the 64-bit call gate */
1451 if (load_segment_ra(env
, &e1
, &e2
, new_cs
+ 8, GETPC())) {
1452 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc,
1455 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x1f;
1457 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc,
1460 new_eip
|= ((target_ulong
)e1
) << 32;
1464 if (load_segment_ra(env
, &e1
, &e2
, gate_cs
, GETPC()) != 0) {
1465 raise_exception_err_ra(env
, EXCP0D_GPF
, gate_cs
& 0xfffc, GETPC());
1467 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1468 /* must be code segment */
1469 if (((e2
& (DESC_S_MASK
| DESC_CS_MASK
)) !=
1470 (DESC_S_MASK
| DESC_CS_MASK
))) {
1471 raise_exception_err_ra(env
, EXCP0D_GPF
, gate_cs
& 0xfffc, GETPC());
1473 if (((e2
& DESC_C_MASK
) && (dpl
> cpl
)) ||
1474 (!(e2
& DESC_C_MASK
) && (dpl
!= cpl
))) {
1475 raise_exception_err_ra(env
, EXCP0D_GPF
, gate_cs
& 0xfffc, GETPC());
1477 #ifdef TARGET_X86_64
1478 if (env
->efer
& MSR_EFER_LMA
) {
1479 if (!(e2
& DESC_L_MASK
)) {
1480 raise_exception_err_ra(env
, EXCP0D_GPF
, gate_cs
& 0xfffc, GETPC());
1482 if (e2
& DESC_B_MASK
) {
1483 raise_exception_err_ra(env
, EXCP0D_GPF
, gate_cs
& 0xfffc, GETPC());
1487 if (!(e2
& DESC_P_MASK
)) {
1488 raise_exception_err_ra(env
, EXCP0D_GPF
, gate_cs
& 0xfffc, GETPC());
1490 limit
= get_seg_limit(e1
, e2
);
1491 if (new_eip
> limit
&&
1492 (!(env
->hflags
& HF_LMA_MASK
) || !(e2
& DESC_L_MASK
))) {
1493 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, GETPC());
1495 cpu_x86_load_seg_cache(env
, R_CS
, (gate_cs
& 0xfffc) | cpl
,
1496 get_seg_base(e1
, e2
), limit
, e2
);
1500 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1506 /* real mode call */
1507 void helper_lcall_real(CPUX86State
*env
, uint32_t new_cs
, uint32_t new_eip
,
1508 int shift
, uint32_t next_eip
)
1510 uint32_t esp
, esp_mask
;
1513 esp
= env
->regs
[R_ESP
];
1514 esp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1515 ssp
= env
->segs
[R_SS
].base
;
1517 PUSHL_RA(ssp
, esp
, esp_mask
, env
->segs
[R_CS
].selector
, GETPC());
1518 PUSHL_RA(ssp
, esp
, esp_mask
, next_eip
, GETPC());
1520 PUSHW_RA(ssp
, esp
, esp_mask
, env
->segs
[R_CS
].selector
, GETPC());
1521 PUSHW_RA(ssp
, esp
, esp_mask
, next_eip
, GETPC());
1524 SET_ESP(esp
, esp_mask
);
1526 env
->segs
[R_CS
].selector
= new_cs
;
1527 env
->segs
[R_CS
].base
= (new_cs
<< 4);
1530 /* protected mode call */
1531 void helper_lcall_protected(CPUX86State
*env
, int new_cs
, target_ulong new_eip
,
1532 int shift
, target_ulong next_eip
)
1535 uint32_t e1
, e2
, cpl
, dpl
, rpl
, selector
, param_count
;
1536 uint32_t ss
= 0, ss_e1
= 0, ss_e2
= 0, type
, ss_dpl
, sp_mask
;
1537 uint32_t val
, limit
, old_sp_mask
;
1538 target_ulong ssp
, old_ssp
, offset
, sp
;
1540 LOG_PCALL("lcall %04x:" TARGET_FMT_lx
" s=%d\n", new_cs
, new_eip
, shift
);
1541 LOG_PCALL_STATE(env_cpu(env
));
1542 if ((new_cs
& 0xfffc) == 0) {
1543 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, GETPC());
1545 if (load_segment_ra(env
, &e1
, &e2
, new_cs
, GETPC()) != 0) {
1546 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1548 cpl
= env
->hflags
& HF_CPL_MASK
;
1549 LOG_PCALL("desc=%08x:%08x\n", e1
, e2
);
1550 if (e2
& DESC_S_MASK
) {
1551 if (!(e2
& DESC_CS_MASK
)) {
1552 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1554 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1555 if (e2
& DESC_C_MASK
) {
1556 /* conforming code segment */
1558 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1561 /* non conforming code segment */
1564 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1567 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1570 if (!(e2
& DESC_P_MASK
)) {
1571 raise_exception_err_ra(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc, GETPC());
1574 #ifdef TARGET_X86_64
1575 /* XXX: check 16/32 bit cases in long mode */
1580 rsp
= env
->regs
[R_ESP
];
1581 PUSHQ_RA(rsp
, env
->segs
[R_CS
].selector
, GETPC());
1582 PUSHQ_RA(rsp
, next_eip
, GETPC());
1583 /* from this point, not restartable */
1584 env
->regs
[R_ESP
] = rsp
;
1585 cpu_x86_load_seg_cache(env
, R_CS
, (new_cs
& 0xfffc) | cpl
,
1586 get_seg_base(e1
, e2
),
1587 get_seg_limit(e1
, e2
), e2
);
1592 sp
= env
->regs
[R_ESP
];
1593 sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1594 ssp
= env
->segs
[R_SS
].base
;
1596 PUSHL_RA(ssp
, sp
, sp_mask
, env
->segs
[R_CS
].selector
, GETPC());
1597 PUSHL_RA(ssp
, sp
, sp_mask
, next_eip
, GETPC());
1599 PUSHW_RA(ssp
, sp
, sp_mask
, env
->segs
[R_CS
].selector
, GETPC());
1600 PUSHW_RA(ssp
, sp
, sp_mask
, next_eip
, GETPC());
1603 limit
= get_seg_limit(e1
, e2
);
1604 if (new_eip
> limit
) {
1605 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1607 /* from this point, not restartable */
1608 SET_ESP(sp
, sp_mask
);
1609 cpu_x86_load_seg_cache(env
, R_CS
, (new_cs
& 0xfffc) | cpl
,
1610 get_seg_base(e1
, e2
), limit
, e2
);
1614 /* check gate type */
1615 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x1f;
1616 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1619 #ifdef TARGET_X86_64
1620 if (env
->efer
& MSR_EFER_LMA
) {
1622 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1628 case 1: /* available 286 TSS */
1629 case 9: /* available 386 TSS */
1630 case 5: /* task gate */
1631 if (dpl
< cpl
|| dpl
< rpl
) {
1632 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1634 switch_tss_ra(env
, new_cs
, e1
, e2
, SWITCH_TSS_CALL
, next_eip
, GETPC());
1636 case 4: /* 286 call gate */
1637 case 12: /* 386 call gate */
1640 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1645 if (dpl
< cpl
|| dpl
< rpl
) {
1646 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1648 /* check valid bit */
1649 if (!(e2
& DESC_P_MASK
)) {
1650 raise_exception_err_ra(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc, GETPC());
1652 selector
= e1
>> 16;
1653 param_count
= e2
& 0x1f;
1654 offset
= (e2
& 0xffff0000) | (e1
& 0x0000ffff);
1655 #ifdef TARGET_X86_64
1656 if (env
->efer
& MSR_EFER_LMA
) {
1657 /* load the upper 8 bytes of the 64-bit call gate */
1658 if (load_segment_ra(env
, &e1
, &e2
, new_cs
+ 8, GETPC())) {
1659 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc,
1662 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x1f;
1664 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc,
1667 offset
|= ((target_ulong
)e1
) << 32;
1670 if ((selector
& 0xfffc) == 0) {
1671 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, GETPC());
1674 if (load_segment_ra(env
, &e1
, &e2
, selector
, GETPC()) != 0) {
1675 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1677 if (!(e2
& DESC_S_MASK
) || !(e2
& (DESC_CS_MASK
))) {
1678 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1680 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1682 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1684 #ifdef TARGET_X86_64
1685 if (env
->efer
& MSR_EFER_LMA
) {
1686 if (!(e2
& DESC_L_MASK
)) {
1687 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1689 if (e2
& DESC_B_MASK
) {
1690 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1695 if (!(e2
& DESC_P_MASK
)) {
1696 raise_exception_err_ra(env
, EXCP0B_NOSEG
, selector
& 0xfffc, GETPC());
1699 if (!(e2
& DESC_C_MASK
) && dpl
< cpl
) {
1700 /* to inner privilege */
1701 #ifdef TARGET_X86_64
1703 sp
= get_rsp_from_tss(env
, dpl
);
1704 ss
= dpl
; /* SS = NULL selector with RPL = new CPL */
1707 ssp
= 0; /* SS base is always zero in IA-32e mode */
1708 LOG_PCALL("new ss:rsp=%04x:%016llx env->regs[R_ESP]="
1709 TARGET_FMT_lx
"\n", ss
, sp
, env
->regs
[R_ESP
]);
1714 get_ss_esp_from_tss(env
, &ss
, &sp32
, dpl
, GETPC());
1715 LOG_PCALL("new ss:esp=%04x:%08x param_count=%d env->regs[R_ESP]="
1716 TARGET_FMT_lx
"\n", ss
, sp32
, param_count
,
1719 if ((ss
& 0xfffc) == 0) {
1720 raise_exception_err_ra(env
, EXCP0A_TSS
, ss
& 0xfffc, GETPC());
1722 if ((ss
& 3) != dpl
) {
1723 raise_exception_err_ra(env
, EXCP0A_TSS
, ss
& 0xfffc, GETPC());
1725 if (load_segment_ra(env
, &ss_e1
, &ss_e2
, ss
, GETPC()) != 0) {
1726 raise_exception_err_ra(env
, EXCP0A_TSS
, ss
& 0xfffc, GETPC());
1728 ss_dpl
= (ss_e2
>> DESC_DPL_SHIFT
) & 3;
1729 if (ss_dpl
!= dpl
) {
1730 raise_exception_err_ra(env
, EXCP0A_TSS
, ss
& 0xfffc, GETPC());
1732 if (!(ss_e2
& DESC_S_MASK
) ||
1733 (ss_e2
& DESC_CS_MASK
) ||
1734 !(ss_e2
& DESC_W_MASK
)) {
1735 raise_exception_err_ra(env
, EXCP0A_TSS
, ss
& 0xfffc, GETPC());
1737 if (!(ss_e2
& DESC_P_MASK
)) {
1738 raise_exception_err_ra(env
, EXCP0A_TSS
, ss
& 0xfffc, GETPC());
1741 sp_mask
= get_sp_mask(ss_e2
);
1742 ssp
= get_seg_base(ss_e1
, ss_e2
);
1745 /* push_size = ((param_count * 2) + 8) << shift; */
1747 old_sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1748 old_ssp
= env
->segs
[R_SS
].base
;
1749 #ifdef TARGET_X86_64
1751 /* XXX: verify if new stack address is canonical */
1752 PUSHQ_RA(sp
, env
->segs
[R_SS
].selector
, GETPC());
1753 PUSHQ_RA(sp
, env
->regs
[R_ESP
], GETPC());
1754 /* parameters aren't supported for 64-bit call gates */
1758 PUSHL_RA(ssp
, sp
, sp_mask
, env
->segs
[R_SS
].selector
, GETPC());
1759 PUSHL_RA(ssp
, sp
, sp_mask
, env
->regs
[R_ESP
], GETPC());
1760 for (i
= param_count
- 1; i
>= 0; i
--) {
1761 val
= cpu_ldl_kernel_ra(env
, old_ssp
+
1762 ((env
->regs
[R_ESP
] + i
* 4) &
1763 old_sp_mask
), GETPC());
1764 PUSHL_RA(ssp
, sp
, sp_mask
, val
, GETPC());
1767 PUSHW_RA(ssp
, sp
, sp_mask
, env
->segs
[R_SS
].selector
, GETPC());
1768 PUSHW_RA(ssp
, sp
, sp_mask
, env
->regs
[R_ESP
], GETPC());
1769 for (i
= param_count
- 1; i
>= 0; i
--) {
1770 val
= cpu_lduw_kernel_ra(env
, old_ssp
+
1771 ((env
->regs
[R_ESP
] + i
* 2) &
1772 old_sp_mask
), GETPC());
1773 PUSHW_RA(ssp
, sp
, sp_mask
, val
, GETPC());
1778 /* to same privilege */
1779 sp
= env
->regs
[R_ESP
];
1780 sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1781 ssp
= env
->segs
[R_SS
].base
;
1782 /* push_size = (4 << shift); */
1786 #ifdef TARGET_X86_64
1788 PUSHQ_RA(sp
, env
->segs
[R_CS
].selector
, GETPC());
1789 PUSHQ_RA(sp
, next_eip
, GETPC());
1793 PUSHL_RA(ssp
, sp
, sp_mask
, env
->segs
[R_CS
].selector
, GETPC());
1794 PUSHL_RA(ssp
, sp
, sp_mask
, next_eip
, GETPC());
1796 PUSHW_RA(ssp
, sp
, sp_mask
, env
->segs
[R_CS
].selector
, GETPC());
1797 PUSHW_RA(ssp
, sp
, sp_mask
, next_eip
, GETPC());
1800 /* from this point, not restartable */
1803 #ifdef TARGET_X86_64
1805 cpu_x86_load_seg_cache(env
, R_SS
, ss
, 0, 0, 0);
1809 ss
= (ss
& ~3) | dpl
;
1810 cpu_x86_load_seg_cache(env
, R_SS
, ss
,
1812 get_seg_limit(ss_e1
, ss_e2
),
1817 selector
= (selector
& ~3) | dpl
;
1818 cpu_x86_load_seg_cache(env
, R_CS
, selector
,
1819 get_seg_base(e1
, e2
),
1820 get_seg_limit(e1
, e2
),
1822 SET_ESP(sp
, sp_mask
);
1827 /* real and vm86 mode iret */
1828 void helper_iret_real(CPUX86State
*env
, int shift
)
1830 uint32_t sp
, new_cs
, new_eip
, new_eflags
, sp_mask
;
1834 sp_mask
= 0xffff; /* XXXX: use SS segment size? */
1835 sp
= env
->regs
[R_ESP
];
1836 ssp
= env
->segs
[R_SS
].base
;
1839 POPL_RA(ssp
, sp
, sp_mask
, new_eip
, GETPC());
1840 POPL_RA(ssp
, sp
, sp_mask
, new_cs
, GETPC());
1842 POPL_RA(ssp
, sp
, sp_mask
, new_eflags
, GETPC());
1845 POPW_RA(ssp
, sp
, sp_mask
, new_eip
, GETPC());
1846 POPW_RA(ssp
, sp
, sp_mask
, new_cs
, GETPC());
1847 POPW_RA(ssp
, sp
, sp_mask
, new_eflags
, GETPC());
1849 env
->regs
[R_ESP
] = (env
->regs
[R_ESP
] & ~sp_mask
) | (sp
& sp_mask
);
1850 env
->segs
[R_CS
].selector
= new_cs
;
1851 env
->segs
[R_CS
].base
= (new_cs
<< 4);
1853 if (env
->eflags
& VM_MASK
) {
1854 eflags_mask
= TF_MASK
| AC_MASK
| ID_MASK
| IF_MASK
| RF_MASK
|
1857 eflags_mask
= TF_MASK
| AC_MASK
| ID_MASK
| IF_MASK
| IOPL_MASK
|
1861 eflags_mask
&= 0xffff;
1863 cpu_load_eflags(env
, new_eflags
, eflags_mask
);
1864 env
->hflags2
&= ~HF2_NMI_MASK
;
1867 static inline void validate_seg(CPUX86State
*env
, X86Seg seg_reg
, int cpl
)
1872 /* XXX: on x86_64, we do not want to nullify FS and GS because
1873 they may still contain a valid base. I would be interested to
1874 know how a real x86_64 CPU behaves */
1875 if ((seg_reg
== R_FS
|| seg_reg
== R_GS
) &&
1876 (env
->segs
[seg_reg
].selector
& 0xfffc) == 0) {
1880 e2
= env
->segs
[seg_reg
].flags
;
1881 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1882 if (!(e2
& DESC_CS_MASK
) || !(e2
& DESC_C_MASK
)) {
1883 /* data or non conforming code segment */
1885 cpu_x86_load_seg_cache(env
, seg_reg
, 0,
1886 env
->segs
[seg_reg
].base
,
1887 env
->segs
[seg_reg
].limit
,
1888 env
->segs
[seg_reg
].flags
& ~DESC_P_MASK
);
1893 /* protected mode iret */
1894 static inline void helper_ret_protected(CPUX86State
*env
, int shift
,
1895 int is_iret
, int addend
,
1898 uint32_t new_cs
, new_eflags
, new_ss
;
1899 uint32_t new_es
, new_ds
, new_fs
, new_gs
;
1900 uint32_t e1
, e2
, ss_e1
, ss_e2
;
1901 int cpl
, dpl
, rpl
, eflags_mask
, iopl
;
1902 target_ulong ssp
, sp
, new_eip
, new_esp
, sp_mask
;
1904 #ifdef TARGET_X86_64
1910 sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1912 sp
= env
->regs
[R_ESP
];
1913 ssp
= env
->segs
[R_SS
].base
;
1914 new_eflags
= 0; /* avoid warning */
1915 #ifdef TARGET_X86_64
1917 POPQ_RA(sp
, new_eip
, retaddr
);
1918 POPQ_RA(sp
, new_cs
, retaddr
);
1921 POPQ_RA(sp
, new_eflags
, retaddr
);
1928 POPL_RA(ssp
, sp
, sp_mask
, new_eip
, retaddr
);
1929 POPL_RA(ssp
, sp
, sp_mask
, new_cs
, retaddr
);
1932 POPL_RA(ssp
, sp
, sp_mask
, new_eflags
, retaddr
);
1933 if (new_eflags
& VM_MASK
) {
1934 goto return_to_vm86
;
1939 POPW_RA(ssp
, sp
, sp_mask
, new_eip
, retaddr
);
1940 POPW_RA(ssp
, sp
, sp_mask
, new_cs
, retaddr
);
1942 POPW_RA(ssp
, sp
, sp_mask
, new_eflags
, retaddr
);
1946 LOG_PCALL("lret new %04x:" TARGET_FMT_lx
" s=%d addend=0x%x\n",
1947 new_cs
, new_eip
, shift
, addend
);
1948 LOG_PCALL_STATE(env_cpu(env
));
1949 if ((new_cs
& 0xfffc) == 0) {
1950 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, retaddr
);
1952 if (load_segment_ra(env
, &e1
, &e2
, new_cs
, retaddr
) != 0) {
1953 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, retaddr
);
1955 if (!(e2
& DESC_S_MASK
) ||
1956 !(e2
& DESC_CS_MASK
)) {
1957 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, retaddr
);
1959 cpl
= env
->hflags
& HF_CPL_MASK
;
1962 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, retaddr
);
1964 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1965 if (e2
& DESC_C_MASK
) {
1967 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, retaddr
);
1971 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, retaddr
);
1974 if (!(e2
& DESC_P_MASK
)) {
1975 raise_exception_err_ra(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc, retaddr
);
1979 if (rpl
== cpl
&& (!(env
->hflags
& HF_CS64_MASK
) ||
1980 ((env
->hflags
& HF_CS64_MASK
) && !is_iret
))) {
1981 /* return to same privilege level */
1982 cpu_x86_load_seg_cache(env
, R_CS
, new_cs
,
1983 get_seg_base(e1
, e2
),
1984 get_seg_limit(e1
, e2
),
1987 /* return to different privilege level */
1988 #ifdef TARGET_X86_64
1990 POPQ_RA(sp
, new_esp
, retaddr
);
1991 POPQ_RA(sp
, new_ss
, retaddr
);
1998 POPL_RA(ssp
, sp
, sp_mask
, new_esp
, retaddr
);
1999 POPL_RA(ssp
, sp
, sp_mask
, new_ss
, retaddr
);
2003 POPW_RA(ssp
, sp
, sp_mask
, new_esp
, retaddr
);
2004 POPW_RA(ssp
, sp
, sp_mask
, new_ss
, retaddr
);
2007 LOG_PCALL("new ss:esp=%04x:" TARGET_FMT_lx
"\n",
2009 if ((new_ss
& 0xfffc) == 0) {
2010 #ifdef TARGET_X86_64
2011 /* NULL ss is allowed in long mode if cpl != 3 */
2012 /* XXX: test CS64? */
2013 if ((env
->hflags
& HF_LMA_MASK
) && rpl
!= 3) {
2014 cpu_x86_load_seg_cache(env
, R_SS
, new_ss
,
2016 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2017 DESC_S_MASK
| (rpl
<< DESC_DPL_SHIFT
) |
2018 DESC_W_MASK
| DESC_A_MASK
);
2019 ss_e2
= DESC_B_MASK
; /* XXX: should not be needed? */
2023 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, retaddr
);
2026 if ((new_ss
& 3) != rpl
) {
2027 raise_exception_err_ra(env
, EXCP0D_GPF
, new_ss
& 0xfffc, retaddr
);
2029 if (load_segment_ra(env
, &ss_e1
, &ss_e2
, new_ss
, retaddr
) != 0) {
2030 raise_exception_err_ra(env
, EXCP0D_GPF
, new_ss
& 0xfffc, retaddr
);
2032 if (!(ss_e2
& DESC_S_MASK
) ||
2033 (ss_e2
& DESC_CS_MASK
) ||
2034 !(ss_e2
& DESC_W_MASK
)) {
2035 raise_exception_err_ra(env
, EXCP0D_GPF
, new_ss
& 0xfffc, retaddr
);
2037 dpl
= (ss_e2
>> DESC_DPL_SHIFT
) & 3;
2039 raise_exception_err_ra(env
, EXCP0D_GPF
, new_ss
& 0xfffc, retaddr
);
2041 if (!(ss_e2
& DESC_P_MASK
)) {
2042 raise_exception_err_ra(env
, EXCP0B_NOSEG
, new_ss
& 0xfffc, retaddr
);
2044 cpu_x86_load_seg_cache(env
, R_SS
, new_ss
,
2045 get_seg_base(ss_e1
, ss_e2
),
2046 get_seg_limit(ss_e1
, ss_e2
),
2050 cpu_x86_load_seg_cache(env
, R_CS
, new_cs
,
2051 get_seg_base(e1
, e2
),
2052 get_seg_limit(e1
, e2
),
2055 #ifdef TARGET_X86_64
2056 if (env
->hflags
& HF_CS64_MASK
) {
2061 sp_mask
= get_sp_mask(ss_e2
);
2064 /* validate data segments */
2065 validate_seg(env
, R_ES
, rpl
);
2066 validate_seg(env
, R_DS
, rpl
);
2067 validate_seg(env
, R_FS
, rpl
);
2068 validate_seg(env
, R_GS
, rpl
);
2072 SET_ESP(sp
, sp_mask
);
2075 /* NOTE: 'cpl' is the _old_ CPL */
2076 eflags_mask
= TF_MASK
| AC_MASK
| ID_MASK
| RF_MASK
| NT_MASK
;
2078 eflags_mask
|= IOPL_MASK
;
2080 iopl
= (env
->eflags
>> IOPL_SHIFT
) & 3;
2082 eflags_mask
|= IF_MASK
;
2085 eflags_mask
&= 0xffff;
2087 cpu_load_eflags(env
, new_eflags
, eflags_mask
);
2092 POPL_RA(ssp
, sp
, sp_mask
, new_esp
, retaddr
);
2093 POPL_RA(ssp
, sp
, sp_mask
, new_ss
, retaddr
);
2094 POPL_RA(ssp
, sp
, sp_mask
, new_es
, retaddr
);
2095 POPL_RA(ssp
, sp
, sp_mask
, new_ds
, retaddr
);
2096 POPL_RA(ssp
, sp
, sp_mask
, new_fs
, retaddr
);
2097 POPL_RA(ssp
, sp
, sp_mask
, new_gs
, retaddr
);
2099 /* modify processor state */
2100 cpu_load_eflags(env
, new_eflags
, TF_MASK
| AC_MASK
| ID_MASK
|
2101 IF_MASK
| IOPL_MASK
| VM_MASK
| NT_MASK
| VIF_MASK
|
2103 load_seg_vm(env
, R_CS
, new_cs
& 0xffff);
2104 load_seg_vm(env
, R_SS
, new_ss
& 0xffff);
2105 load_seg_vm(env
, R_ES
, new_es
& 0xffff);
2106 load_seg_vm(env
, R_DS
, new_ds
& 0xffff);
2107 load_seg_vm(env
, R_FS
, new_fs
& 0xffff);
2108 load_seg_vm(env
, R_GS
, new_gs
& 0xffff);
2110 env
->eip
= new_eip
& 0xffff;
2111 env
->regs
[R_ESP
] = new_esp
;
2114 void helper_iret_protected(CPUX86State
*env
, int shift
, int next_eip
)
2116 int tss_selector
, type
;
2119 /* specific case for TSS */
2120 if (env
->eflags
& NT_MASK
) {
2121 #ifdef TARGET_X86_64
2122 if (env
->hflags
& HF_LMA_MASK
) {
2123 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, GETPC());
2126 tss_selector
= cpu_lduw_kernel_ra(env
, env
->tr
.base
+ 0, GETPC());
2127 if (tss_selector
& 4) {
2128 raise_exception_err_ra(env
, EXCP0A_TSS
, tss_selector
& 0xfffc, GETPC());
2130 if (load_segment_ra(env
, &e1
, &e2
, tss_selector
, GETPC()) != 0) {
2131 raise_exception_err_ra(env
, EXCP0A_TSS
, tss_selector
& 0xfffc, GETPC());
2133 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x17;
2134 /* NOTE: we check both segment and busy TSS */
2136 raise_exception_err_ra(env
, EXCP0A_TSS
, tss_selector
& 0xfffc, GETPC());
2138 switch_tss_ra(env
, tss_selector
, e1
, e2
, SWITCH_TSS_IRET
, next_eip
, GETPC());
2140 helper_ret_protected(env
, shift
, 1, 0, GETPC());
2142 env
->hflags2
&= ~HF2_NMI_MASK
;
2145 void helper_lret_protected(CPUX86State
*env
, int shift
, int addend
)
2147 helper_ret_protected(env
, shift
, 0, addend
, GETPC());
2150 void helper_sysenter(CPUX86State
*env
)
2152 if (env
->sysenter_cs
== 0) {
2153 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, GETPC());
2155 env
->eflags
&= ~(VM_MASK
| IF_MASK
| RF_MASK
);
2157 #ifdef TARGET_X86_64
2158 if (env
->hflags
& HF_LMA_MASK
) {
2159 cpu_x86_load_seg_cache(env
, R_CS
, env
->sysenter_cs
& 0xfffc,
2161 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2163 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
|
2168 cpu_x86_load_seg_cache(env
, R_CS
, env
->sysenter_cs
& 0xfffc,
2170 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2172 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
);
2174 cpu_x86_load_seg_cache(env
, R_SS
, (env
->sysenter_cs
+ 8) & 0xfffc,
2176 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2178 DESC_W_MASK
| DESC_A_MASK
);
2179 env
->regs
[R_ESP
] = env
->sysenter_esp
;
2180 env
->eip
= env
->sysenter_eip
;
2183 void helper_sysexit(CPUX86State
*env
, int dflag
)
2187 cpl
= env
->hflags
& HF_CPL_MASK
;
2188 if (env
->sysenter_cs
== 0 || cpl
!= 0) {
2189 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, GETPC());
2191 #ifdef TARGET_X86_64
2193 cpu_x86_load_seg_cache(env
, R_CS
, ((env
->sysenter_cs
+ 32) & 0xfffc) |
2195 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2196 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
2197 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
|
2199 cpu_x86_load_seg_cache(env
, R_SS
, ((env
->sysenter_cs
+ 40) & 0xfffc) |
2201 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2202 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
2203 DESC_W_MASK
| DESC_A_MASK
);
2207 cpu_x86_load_seg_cache(env
, R_CS
, ((env
->sysenter_cs
+ 16) & 0xfffc) |
2209 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2210 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
2211 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
);
2212 cpu_x86_load_seg_cache(env
, R_SS
, ((env
->sysenter_cs
+ 24) & 0xfffc) |
2214 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2215 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
2216 DESC_W_MASK
| DESC_A_MASK
);
2218 env
->regs
[R_ESP
] = env
->regs
[R_ECX
];
2219 env
->eip
= env
->regs
[R_EDX
];
2222 target_ulong
helper_lsl(CPUX86State
*env
, target_ulong selector1
)
2225 uint32_t e1
, e2
, eflags
, selector
;
2226 int rpl
, dpl
, cpl
, type
;
2228 selector
= selector1
& 0xffff;
2229 eflags
= cpu_cc_compute_all(env
, CC_OP
);
2230 if ((selector
& 0xfffc) == 0) {
2233 if (load_segment_ra(env
, &e1
, &e2
, selector
, GETPC()) != 0) {
2237 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
2238 cpl
= env
->hflags
& HF_CPL_MASK
;
2239 if (e2
& DESC_S_MASK
) {
2240 if ((e2
& DESC_CS_MASK
) && (e2
& DESC_C_MASK
)) {
2243 if (dpl
< cpl
|| dpl
< rpl
) {
2248 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
2259 if (dpl
< cpl
|| dpl
< rpl
) {
2261 CC_SRC
= eflags
& ~CC_Z
;
2265 limit
= get_seg_limit(e1
, e2
);
2266 CC_SRC
= eflags
| CC_Z
;
2270 target_ulong
helper_lar(CPUX86State
*env
, target_ulong selector1
)
2272 uint32_t e1
, e2
, eflags
, selector
;
2273 int rpl
, dpl
, cpl
, type
;
2275 selector
= selector1
& 0xffff;
2276 eflags
= cpu_cc_compute_all(env
, CC_OP
);
2277 if ((selector
& 0xfffc) == 0) {
2280 if (load_segment_ra(env
, &e1
, &e2
, selector
, GETPC()) != 0) {
2284 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
2285 cpl
= env
->hflags
& HF_CPL_MASK
;
2286 if (e2
& DESC_S_MASK
) {
2287 if ((e2
& DESC_CS_MASK
) && (e2
& DESC_C_MASK
)) {
2290 if (dpl
< cpl
|| dpl
< rpl
) {
2295 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
2309 if (dpl
< cpl
|| dpl
< rpl
) {
2311 CC_SRC
= eflags
& ~CC_Z
;
2315 CC_SRC
= eflags
| CC_Z
;
2316 return e2
& 0x00f0ff00;
2319 void helper_verr(CPUX86State
*env
, target_ulong selector1
)
2321 uint32_t e1
, e2
, eflags
, selector
;
2324 selector
= selector1
& 0xffff;
2325 eflags
= cpu_cc_compute_all(env
, CC_OP
);
2326 if ((selector
& 0xfffc) == 0) {
2329 if (load_segment_ra(env
, &e1
, &e2
, selector
, GETPC()) != 0) {
2332 if (!(e2
& DESC_S_MASK
)) {
2336 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
2337 cpl
= env
->hflags
& HF_CPL_MASK
;
2338 if (e2
& DESC_CS_MASK
) {
2339 if (!(e2
& DESC_R_MASK
)) {
2342 if (!(e2
& DESC_C_MASK
)) {
2343 if (dpl
< cpl
|| dpl
< rpl
) {
2348 if (dpl
< cpl
|| dpl
< rpl
) {
2350 CC_SRC
= eflags
& ~CC_Z
;
2354 CC_SRC
= eflags
| CC_Z
;
2357 void helper_verw(CPUX86State
*env
, target_ulong selector1
)
2359 uint32_t e1
, e2
, eflags
, selector
;
2362 selector
= selector1
& 0xffff;
2363 eflags
= cpu_cc_compute_all(env
, CC_OP
);
2364 if ((selector
& 0xfffc) == 0) {
2367 if (load_segment_ra(env
, &e1
, &e2
, selector
, GETPC()) != 0) {
2370 if (!(e2
& DESC_S_MASK
)) {
2374 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
2375 cpl
= env
->hflags
& HF_CPL_MASK
;
2376 if (e2
& DESC_CS_MASK
) {
2379 if (dpl
< cpl
|| dpl
< rpl
) {
2382 if (!(e2
& DESC_W_MASK
)) {
2384 CC_SRC
= eflags
& ~CC_Z
;
2388 CC_SRC
= eflags
| CC_Z
;