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 static void tss_set_busy(CPUX86State
*env
, int tss_selector
, bool value
,
232 target_ulong ptr
= env
->gdt
.base
+ (tss_selector
& ~7);
233 uint32_t e2
= cpu_ldl_kernel_ra(env
, ptr
+ 4, retaddr
);
236 e2
|= DESC_TSS_BUSY_MASK
;
238 e2
&= ~DESC_TSS_BUSY_MASK
;
241 cpu_stl_kernel_ra(env
, ptr
+ 4, e2
, retaddr
);
244 #define SWITCH_TSS_JMP 0
245 #define SWITCH_TSS_IRET 1
246 #define SWITCH_TSS_CALL 2
248 /* return 0 if switching to a 16-bit selector */
249 static int switch_tss_ra(CPUX86State
*env
, int tss_selector
,
250 uint32_t e1
, uint32_t e2
, int source
,
251 uint32_t next_eip
, uintptr_t retaddr
)
253 int tss_limit
, tss_limit_max
, type
, old_tss_limit_max
, old_type
, v1
, v2
, i
;
254 target_ulong tss_base
;
255 uint32_t new_regs
[8], new_segs
[6];
256 uint32_t new_eflags
, new_eip
, new_cr3
, new_ldt
, new_trap
;
257 uint32_t old_eflags
, eflags_mask
;
262 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
263 LOG_PCALL("switch_tss: sel=0x%04x type=%d src=%d\n", tss_selector
, type
,
266 /* if task gate, we read the TSS segment and we load it */
268 if (!(e2
& DESC_P_MASK
)) {
269 raise_exception_err_ra(env
, EXCP0B_NOSEG
, tss_selector
& 0xfffc, retaddr
);
271 tss_selector
= e1
>> 16;
272 if (tss_selector
& 4) {
273 raise_exception_err_ra(env
, EXCP0A_TSS
, tss_selector
& 0xfffc, retaddr
);
275 if (load_segment_ra(env
, &e1
, &e2
, tss_selector
, retaddr
) != 0) {
276 raise_exception_err_ra(env
, EXCP0D_GPF
, tss_selector
& 0xfffc, retaddr
);
278 if (e2
& DESC_S_MASK
) {
279 raise_exception_err_ra(env
, EXCP0D_GPF
, tss_selector
& 0xfffc, retaddr
);
281 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
282 if ((type
& 7) != 1) {
283 raise_exception_err_ra(env
, EXCP0D_GPF
, tss_selector
& 0xfffc, retaddr
);
287 if (!(e2
& DESC_P_MASK
)) {
288 raise_exception_err_ra(env
, EXCP0B_NOSEG
, tss_selector
& 0xfffc, retaddr
);
296 tss_limit
= get_seg_limit(e1
, e2
);
297 tss_base
= get_seg_base(e1
, e2
);
298 if ((tss_selector
& 4) != 0 ||
299 tss_limit
< tss_limit_max
) {
300 raise_exception_err_ra(env
, EXCP0A_TSS
, tss_selector
& 0xfffc, retaddr
);
302 old_type
= (env
->tr
.flags
>> DESC_TYPE_SHIFT
) & 0xf;
304 old_tss_limit_max
= 103;
306 old_tss_limit_max
= 43;
309 /* read all the registers from the new TSS */
312 new_cr3
= cpu_ldl_kernel_ra(env
, tss_base
+ 0x1c, retaddr
);
313 new_eip
= cpu_ldl_kernel_ra(env
, tss_base
+ 0x20, retaddr
);
314 new_eflags
= cpu_ldl_kernel_ra(env
, tss_base
+ 0x24, retaddr
);
315 for (i
= 0; i
< 8; i
++) {
316 new_regs
[i
] = cpu_ldl_kernel_ra(env
, tss_base
+ (0x28 + i
* 4),
319 for (i
= 0; i
< 6; i
++) {
320 new_segs
[i
] = cpu_lduw_kernel_ra(env
, tss_base
+ (0x48 + i
* 4),
323 new_ldt
= cpu_lduw_kernel_ra(env
, tss_base
+ 0x60, retaddr
);
324 new_trap
= cpu_ldl_kernel_ra(env
, tss_base
+ 0x64, retaddr
);
328 new_eip
= cpu_lduw_kernel_ra(env
, tss_base
+ 0x0e, retaddr
);
329 new_eflags
= cpu_lduw_kernel_ra(env
, tss_base
+ 0x10, retaddr
);
330 for (i
= 0; i
< 8; i
++) {
331 new_regs
[i
] = cpu_lduw_kernel_ra(env
, tss_base
+ (0x12 + i
* 2), retaddr
);
333 for (i
= 0; i
< 4; i
++) {
334 new_segs
[i
] = cpu_lduw_kernel_ra(env
, tss_base
+ (0x22 + i
* 2),
337 new_ldt
= cpu_lduw_kernel_ra(env
, tss_base
+ 0x2a, retaddr
);
342 /* XXX: avoid a compiler warning, see
343 http://support.amd.com/us/Processor_TechDocs/24593.pdf
344 chapters 12.2.5 and 13.2.4 on how to implement TSS Trap bit */
347 /* NOTE: we must avoid memory exceptions during the task switch,
348 so we make dummy accesses before */
349 /* XXX: it can still fail in some cases, so a bigger hack is
350 necessary to valid the TLB after having done the accesses */
352 v1
= cpu_ldub_kernel_ra(env
, env
->tr
.base
, retaddr
);
353 v2
= cpu_ldub_kernel_ra(env
, env
->tr
.base
+ old_tss_limit_max
, retaddr
);
354 cpu_stb_kernel_ra(env
, env
->tr
.base
, v1
, retaddr
);
355 cpu_stb_kernel_ra(env
, env
->tr
.base
+ old_tss_limit_max
, v2
, retaddr
);
357 /* clear busy bit (it is restartable) */
358 if (source
== SWITCH_TSS_JMP
|| source
== SWITCH_TSS_IRET
) {
359 tss_set_busy(env
, env
->tr
.selector
, 0, retaddr
);
361 old_eflags
= cpu_compute_eflags(env
);
362 if (source
== SWITCH_TSS_IRET
) {
363 old_eflags
&= ~NT_MASK
;
366 /* save the current state in the old TSS */
369 cpu_stl_kernel_ra(env
, env
->tr
.base
+ 0x20, next_eip
, retaddr
);
370 cpu_stl_kernel_ra(env
, env
->tr
.base
+ 0x24, old_eflags
, retaddr
);
371 cpu_stl_kernel_ra(env
, env
->tr
.base
+ (0x28 + 0 * 4), env
->regs
[R_EAX
], retaddr
);
372 cpu_stl_kernel_ra(env
, env
->tr
.base
+ (0x28 + 1 * 4), env
->regs
[R_ECX
], retaddr
);
373 cpu_stl_kernel_ra(env
, env
->tr
.base
+ (0x28 + 2 * 4), env
->regs
[R_EDX
], retaddr
);
374 cpu_stl_kernel_ra(env
, env
->tr
.base
+ (0x28 + 3 * 4), env
->regs
[R_EBX
], retaddr
);
375 cpu_stl_kernel_ra(env
, env
->tr
.base
+ (0x28 + 4 * 4), env
->regs
[R_ESP
], retaddr
);
376 cpu_stl_kernel_ra(env
, env
->tr
.base
+ (0x28 + 5 * 4), env
->regs
[R_EBP
], retaddr
);
377 cpu_stl_kernel_ra(env
, env
->tr
.base
+ (0x28 + 6 * 4), env
->regs
[R_ESI
], retaddr
);
378 cpu_stl_kernel_ra(env
, env
->tr
.base
+ (0x28 + 7 * 4), env
->regs
[R_EDI
], retaddr
);
379 for (i
= 0; i
< 6; i
++) {
380 cpu_stw_kernel_ra(env
, env
->tr
.base
+ (0x48 + i
* 4),
381 env
->segs
[i
].selector
, retaddr
);
385 cpu_stw_kernel_ra(env
, env
->tr
.base
+ 0x0e, next_eip
, retaddr
);
386 cpu_stw_kernel_ra(env
, env
->tr
.base
+ 0x10, old_eflags
, retaddr
);
387 cpu_stw_kernel_ra(env
, env
->tr
.base
+ (0x12 + 0 * 2), env
->regs
[R_EAX
], retaddr
);
388 cpu_stw_kernel_ra(env
, env
->tr
.base
+ (0x12 + 1 * 2), env
->regs
[R_ECX
], retaddr
);
389 cpu_stw_kernel_ra(env
, env
->tr
.base
+ (0x12 + 2 * 2), env
->regs
[R_EDX
], retaddr
);
390 cpu_stw_kernel_ra(env
, env
->tr
.base
+ (0x12 + 3 * 2), env
->regs
[R_EBX
], retaddr
);
391 cpu_stw_kernel_ra(env
, env
->tr
.base
+ (0x12 + 4 * 2), env
->regs
[R_ESP
], retaddr
);
392 cpu_stw_kernel_ra(env
, env
->tr
.base
+ (0x12 + 5 * 2), env
->regs
[R_EBP
], retaddr
);
393 cpu_stw_kernel_ra(env
, env
->tr
.base
+ (0x12 + 6 * 2), env
->regs
[R_ESI
], retaddr
);
394 cpu_stw_kernel_ra(env
, env
->tr
.base
+ (0x12 + 7 * 2), env
->regs
[R_EDI
], retaddr
);
395 for (i
= 0; i
< 4; i
++) {
396 cpu_stw_kernel_ra(env
, env
->tr
.base
+ (0x22 + i
* 2),
397 env
->segs
[i
].selector
, retaddr
);
401 /* now if an exception occurs, it will occurs in the next task
404 if (source
== SWITCH_TSS_CALL
) {
405 cpu_stw_kernel_ra(env
, tss_base
, env
->tr
.selector
, retaddr
);
406 new_eflags
|= NT_MASK
;
410 if (source
== SWITCH_TSS_JMP
|| source
== SWITCH_TSS_CALL
) {
411 tss_set_busy(env
, tss_selector
, 1, retaddr
);
414 /* set the new CPU state */
415 /* from this point, any exception which occurs can give problems */
416 env
->cr
[0] |= CR0_TS_MASK
;
417 env
->hflags
|= HF_TS_MASK
;
418 env
->tr
.selector
= tss_selector
;
419 env
->tr
.base
= tss_base
;
420 env
->tr
.limit
= tss_limit
;
421 env
->tr
.flags
= e2
& ~DESC_TSS_BUSY_MASK
;
423 if ((type
& 8) && (env
->cr
[0] & CR0_PG_MASK
)) {
424 cpu_x86_update_cr3(env
, new_cr3
);
427 /* load all registers without an exception, then reload them with
428 possible exception */
430 eflags_mask
= TF_MASK
| AC_MASK
| ID_MASK
|
431 IF_MASK
| IOPL_MASK
| VM_MASK
| RF_MASK
| NT_MASK
;
433 cpu_load_eflags(env
, new_eflags
, eflags_mask
);
434 for (i
= 0; i
< 8; i
++) {
435 env
->regs
[i
] = new_regs
[i
];
438 cpu_load_eflags(env
, new_eflags
, eflags_mask
& 0xffff);
439 for (i
= 0; i
< 8; i
++) {
440 env
->regs
[i
] = (env
->regs
[i
] & 0xffff0000) | new_regs
[i
];
443 if (new_eflags
& VM_MASK
) {
444 for (i
= 0; i
< 6; i
++) {
445 load_seg_vm(env
, i
, new_segs
[i
]);
448 /* first just selectors as the rest may trigger exceptions */
449 for (i
= 0; i
< 6; i
++) {
450 cpu_x86_load_seg_cache(env
, i
, new_segs
[i
], 0, 0, 0);
454 env
->ldt
.selector
= new_ldt
& ~4;
461 raise_exception_err_ra(env
, EXCP0A_TSS
, new_ldt
& 0xfffc, retaddr
);
464 if ((new_ldt
& 0xfffc) != 0) {
466 index
= new_ldt
& ~7;
467 if ((index
+ 7) > dt
->limit
) {
468 raise_exception_err_ra(env
, EXCP0A_TSS
, new_ldt
& 0xfffc, retaddr
);
470 ptr
= dt
->base
+ index
;
471 e1
= cpu_ldl_kernel_ra(env
, ptr
, retaddr
);
472 e2
= cpu_ldl_kernel_ra(env
, ptr
+ 4, retaddr
);
473 if ((e2
& DESC_S_MASK
) || ((e2
>> DESC_TYPE_SHIFT
) & 0xf) != 2) {
474 raise_exception_err_ra(env
, EXCP0A_TSS
, new_ldt
& 0xfffc, retaddr
);
476 if (!(e2
& DESC_P_MASK
)) {
477 raise_exception_err_ra(env
, EXCP0A_TSS
, new_ldt
& 0xfffc, retaddr
);
479 load_seg_cache_raw_dt(&env
->ldt
, e1
, e2
);
482 /* load the segments */
483 if (!(new_eflags
& VM_MASK
)) {
484 int cpl
= new_segs
[R_CS
] & 3;
485 tss_load_seg(env
, R_CS
, new_segs
[R_CS
], cpl
, retaddr
);
486 tss_load_seg(env
, R_SS
, new_segs
[R_SS
], cpl
, retaddr
);
487 tss_load_seg(env
, R_ES
, new_segs
[R_ES
], cpl
, retaddr
);
488 tss_load_seg(env
, R_DS
, new_segs
[R_DS
], cpl
, retaddr
);
489 tss_load_seg(env
, R_FS
, new_segs
[R_FS
], cpl
, retaddr
);
490 tss_load_seg(env
, R_GS
, new_segs
[R_GS
], cpl
, retaddr
);
493 /* check that env->eip is in the CS segment limits */
494 if (new_eip
> env
->segs
[R_CS
].limit
) {
495 /* XXX: different exception if CALL? */
496 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, retaddr
);
499 #ifndef CONFIG_USER_ONLY
500 /* reset local breakpoints */
501 if (env
->dr
[7] & DR7_LOCAL_BP_MASK
) {
502 cpu_x86_update_dr7(env
, env
->dr
[7] & ~DR7_LOCAL_BP_MASK
);
508 static int switch_tss(CPUX86State
*env
, int tss_selector
,
509 uint32_t e1
, uint32_t e2
, int source
,
512 return switch_tss_ra(env
, tss_selector
, e1
, e2
, source
, next_eip
, 0);
515 static inline unsigned int get_sp_mask(unsigned int e2
)
518 if (e2
& DESC_L_MASK
) {
522 if (e2
& DESC_B_MASK
) {
529 int exception_has_error_code(int intno
)
545 #define SET_ESP(val, sp_mask) \
547 if ((sp_mask) == 0xffff) { \
548 env->regs[R_ESP] = (env->regs[R_ESP] & ~0xffff) | \
550 } else if ((sp_mask) == 0xffffffffLL) { \
551 env->regs[R_ESP] = (uint32_t)(val); \
553 env->regs[R_ESP] = (val); \
557 #define SET_ESP(val, sp_mask) \
559 env->regs[R_ESP] = (env->regs[R_ESP] & ~(sp_mask)) | \
560 ((val) & (sp_mask)); \
564 /* in 64-bit machines, this can overflow. So this segment addition macro
565 * can be used to trim the value to 32-bit whenever needed */
566 #define SEG_ADDL(ssp, sp, sp_mask) ((uint32_t)((ssp) + (sp & (sp_mask))))
568 /* XXX: add a is_user flag to have proper security support */
569 #define PUSHW_RA(ssp, sp, sp_mask, val, ra) \
572 cpu_stw_kernel_ra(env, (ssp) + (sp & (sp_mask)), (val), ra); \
575 #define PUSHL_RA(ssp, sp, sp_mask, val, ra) \
578 cpu_stl_kernel_ra(env, SEG_ADDL(ssp, sp, sp_mask), (uint32_t)(val), ra); \
581 #define POPW_RA(ssp, sp, sp_mask, val, ra) \
583 val = cpu_lduw_kernel_ra(env, (ssp) + (sp & (sp_mask)), ra); \
587 #define POPL_RA(ssp, sp, sp_mask, val, ra) \
589 val = (uint32_t)cpu_ldl_kernel_ra(env, SEG_ADDL(ssp, sp, sp_mask), ra); \
593 #define PUSHW(ssp, sp, sp_mask, val) PUSHW_RA(ssp, sp, sp_mask, val, 0)
594 #define PUSHL(ssp, sp, sp_mask, val) PUSHL_RA(ssp, sp, sp_mask, val, 0)
595 #define POPW(ssp, sp, sp_mask, val) POPW_RA(ssp, sp, sp_mask, val, 0)
596 #define POPL(ssp, sp, sp_mask, val) POPL_RA(ssp, sp, sp_mask, val, 0)
598 /* protected mode interrupt */
599 static void do_interrupt_protected(CPUX86State
*env
, int intno
, int is_int
,
600 int error_code
, unsigned int next_eip
,
604 target_ulong ptr
, ssp
;
605 int type
, dpl
, selector
, ss_dpl
, cpl
;
606 int has_error_code
, new_stack
, shift
;
607 uint32_t e1
, e2
, offset
, ss
= 0, esp
, ss_e1
= 0, ss_e2
= 0;
608 uint32_t old_eip
, sp_mask
;
609 int vm86
= env
->eflags
& VM_MASK
;
612 if (!is_int
&& !is_hw
) {
613 has_error_code
= exception_has_error_code(intno
);
622 if (intno
* 8 + 7 > dt
->limit
) {
623 raise_exception_err(env
, EXCP0D_GPF
, intno
* 8 + 2);
625 ptr
= dt
->base
+ intno
* 8;
626 e1
= cpu_ldl_kernel(env
, ptr
);
627 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
628 /* check gate type */
629 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x1f;
631 case 5: /* task gate */
632 case 6: /* 286 interrupt gate */
633 case 7: /* 286 trap gate */
634 case 14: /* 386 interrupt gate */
635 case 15: /* 386 trap gate */
638 raise_exception_err(env
, EXCP0D_GPF
, intno
* 8 + 2);
641 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
642 cpl
= env
->hflags
& HF_CPL_MASK
;
643 /* check privilege if software int */
644 if (is_int
&& dpl
< cpl
) {
645 raise_exception_err(env
, EXCP0D_GPF
, intno
* 8 + 2);
650 /* must do that check here to return the correct error code */
651 if (!(e2
& DESC_P_MASK
)) {
652 raise_exception_err(env
, EXCP0B_NOSEG
, intno
* 8 + 2);
654 shift
= switch_tss(env
, intno
* 8, e1
, e2
, SWITCH_TSS_CALL
, old_eip
);
655 if (has_error_code
) {
658 /* push the error code */
659 if (env
->segs
[R_SS
].flags
& DESC_B_MASK
) {
664 esp
= (env
->regs
[R_ESP
] - (2 << shift
)) & mask
;
665 ssp
= env
->segs
[R_SS
].base
+ esp
;
667 cpu_stl_kernel(env
, ssp
, error_code
);
669 cpu_stw_kernel(env
, ssp
, error_code
);
676 /* Otherwise, trap or interrupt gate */
678 /* check valid bit */
679 if (!(e2
& DESC_P_MASK
)) {
680 raise_exception_err(env
, EXCP0B_NOSEG
, intno
* 8 + 2);
683 offset
= (e2
& 0xffff0000) | (e1
& 0x0000ffff);
684 if ((selector
& 0xfffc) == 0) {
685 raise_exception_err(env
, EXCP0D_GPF
, 0);
687 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
688 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
690 if (!(e2
& DESC_S_MASK
) || !(e2
& (DESC_CS_MASK
))) {
691 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
693 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
695 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
697 if (!(e2
& DESC_P_MASK
)) {
698 raise_exception_err(env
, EXCP0B_NOSEG
, selector
& 0xfffc);
700 if (e2
& DESC_C_MASK
) {
704 /* to inner privilege */
705 get_ss_esp_from_tss(env
, &ss
, &esp
, dpl
, 0);
706 if ((ss
& 0xfffc) == 0) {
707 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
709 if ((ss
& 3) != dpl
) {
710 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
712 if (load_segment(env
, &ss_e1
, &ss_e2
, ss
) != 0) {
713 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
715 ss_dpl
= (ss_e2
>> DESC_DPL_SHIFT
) & 3;
717 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
719 if (!(ss_e2
& DESC_S_MASK
) ||
720 (ss_e2
& DESC_CS_MASK
) ||
721 !(ss_e2
& DESC_W_MASK
)) {
722 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
724 if (!(ss_e2
& DESC_P_MASK
)) {
725 raise_exception_err(env
, EXCP0A_TSS
, ss
& 0xfffc);
728 sp_mask
= get_sp_mask(ss_e2
);
729 ssp
= get_seg_base(ss_e1
, ss_e2
);
731 /* to same privilege */
733 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
736 sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
737 ssp
= env
->segs
[R_SS
].base
;
738 esp
= env
->regs
[R_ESP
];
744 /* XXX: check that enough room is available */
745 push_size
= 6 + (new_stack
<< 2) + (has_error_code
<< 1);
754 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_GS
].selector
);
755 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_FS
].selector
);
756 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_DS
].selector
);
757 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_ES
].selector
);
759 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_SS
].selector
);
760 PUSHL(ssp
, esp
, sp_mask
, env
->regs
[R_ESP
]);
762 PUSHL(ssp
, esp
, sp_mask
, cpu_compute_eflags(env
));
763 PUSHL(ssp
, esp
, sp_mask
, env
->segs
[R_CS
].selector
);
764 PUSHL(ssp
, esp
, sp_mask
, old_eip
);
765 if (has_error_code
) {
766 PUSHL(ssp
, esp
, sp_mask
, error_code
);
771 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_GS
].selector
);
772 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_FS
].selector
);
773 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_DS
].selector
);
774 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_ES
].selector
);
776 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_SS
].selector
);
777 PUSHW(ssp
, esp
, sp_mask
, env
->regs
[R_ESP
]);
779 PUSHW(ssp
, esp
, sp_mask
, cpu_compute_eflags(env
));
780 PUSHW(ssp
, esp
, sp_mask
, env
->segs
[R_CS
].selector
);
781 PUSHW(ssp
, esp
, sp_mask
, old_eip
);
782 if (has_error_code
) {
783 PUSHW(ssp
, esp
, sp_mask
, error_code
);
787 /* interrupt gate clear IF mask */
788 if ((type
& 1) == 0) {
789 env
->eflags
&= ~IF_MASK
;
791 env
->eflags
&= ~(TF_MASK
| VM_MASK
| RF_MASK
| NT_MASK
);
795 cpu_x86_load_seg_cache(env
, R_ES
, 0, 0, 0, 0);
796 cpu_x86_load_seg_cache(env
, R_DS
, 0, 0, 0, 0);
797 cpu_x86_load_seg_cache(env
, R_FS
, 0, 0, 0, 0);
798 cpu_x86_load_seg_cache(env
, R_GS
, 0, 0, 0, 0);
800 ss
= (ss
& ~3) | dpl
;
801 cpu_x86_load_seg_cache(env
, R_SS
, ss
,
802 ssp
, get_seg_limit(ss_e1
, ss_e2
), ss_e2
);
804 SET_ESP(esp
, sp_mask
);
806 selector
= (selector
& ~3) | dpl
;
807 cpu_x86_load_seg_cache(env
, R_CS
, selector
,
808 get_seg_base(e1
, e2
),
809 get_seg_limit(e1
, e2
),
816 #define PUSHQ_RA(sp, val, ra) \
819 cpu_stq_kernel_ra(env, sp, (val), ra); \
822 #define POPQ_RA(sp, val, ra) \
824 val = cpu_ldq_kernel_ra(env, sp, ra); \
828 #define PUSHQ(sp, val) PUSHQ_RA(sp, val, 0)
829 #define POPQ(sp, val) POPQ_RA(sp, val, 0)
831 static inline target_ulong
get_rsp_from_tss(CPUX86State
*env
, int level
)
833 X86CPU
*cpu
= env_archcpu(env
);
839 printf("TR: base=" TARGET_FMT_lx
" limit=%x\n",
840 env
->tr
.base
, env
->tr
.limit
);
843 if (!(env
->tr
.flags
& DESC_P_MASK
)) {
844 cpu_abort(CPU(cpu
), "invalid tss");
846 index
= 8 * level
+ 4;
847 if ((index
+ 7) > env
->tr
.limit
) {
848 raise_exception_err(env
, EXCP0A_TSS
, env
->tr
.selector
& 0xfffc);
851 rsp
= cpu_ldq_kernel(env
, env
->tr
.base
+ index
);
853 /* test virtual address sign extension */
854 pg_mode
= get_pg_mode(env
);
855 sext
= (int64_t)rsp
>> (pg_mode
& PG_MODE_LA57
? 56 : 47);
856 if (sext
!= 0 && sext
!= -1) {
857 raise_exception_err(env
, EXCP0C_STACK
, 0);
863 /* 64 bit interrupt */
864 static void do_interrupt64(CPUX86State
*env
, int intno
, int is_int
,
865 int error_code
, target_ulong next_eip
, int is_hw
)
869 int type
, dpl
, selector
, cpl
, ist
;
870 int has_error_code
, new_stack
;
871 uint32_t e1
, e2
, e3
, ss
;
872 target_ulong old_eip
, esp
, offset
;
875 if (!is_int
&& !is_hw
) {
876 has_error_code
= exception_has_error_code(intno
);
885 if (intno
* 16 + 15 > dt
->limit
) {
886 raise_exception_err(env
, EXCP0D_GPF
, intno
* 8 + 2);
888 ptr
= dt
->base
+ intno
* 16;
889 e1
= cpu_ldl_kernel(env
, ptr
);
890 e2
= cpu_ldl_kernel(env
, ptr
+ 4);
891 e3
= cpu_ldl_kernel(env
, ptr
+ 8);
892 /* check gate type */
893 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x1f;
895 case 14: /* 386 interrupt gate */
896 case 15: /* 386 trap gate */
899 raise_exception_err(env
, EXCP0D_GPF
, intno
* 8 + 2);
902 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
903 cpl
= env
->hflags
& HF_CPL_MASK
;
904 /* check privilege if software int */
905 if (is_int
&& dpl
< cpl
) {
906 raise_exception_err(env
, EXCP0D_GPF
, intno
* 8 + 2);
908 /* check valid bit */
909 if (!(e2
& DESC_P_MASK
)) {
910 raise_exception_err(env
, EXCP0B_NOSEG
, intno
* 8 + 2);
913 offset
= ((target_ulong
)e3
<< 32) | (e2
& 0xffff0000) | (e1
& 0x0000ffff);
915 if ((selector
& 0xfffc) == 0) {
916 raise_exception_err(env
, EXCP0D_GPF
, 0);
919 if (load_segment(env
, &e1
, &e2
, selector
) != 0) {
920 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
922 if (!(e2
& DESC_S_MASK
) || !(e2
& (DESC_CS_MASK
))) {
923 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
925 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
927 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
929 if (!(e2
& DESC_P_MASK
)) {
930 raise_exception_err(env
, EXCP0B_NOSEG
, selector
& 0xfffc);
932 if (!(e2
& DESC_L_MASK
) || (e2
& DESC_B_MASK
)) {
933 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
935 if (e2
& DESC_C_MASK
) {
938 if (dpl
< cpl
|| ist
!= 0) {
939 /* to inner privilege */
941 esp
= get_rsp_from_tss(env
, ist
!= 0 ? ist
+ 3 : dpl
);
944 /* to same privilege */
945 if (env
->eflags
& VM_MASK
) {
946 raise_exception_err(env
, EXCP0D_GPF
, selector
& 0xfffc);
949 esp
= env
->regs
[R_ESP
];
951 esp
&= ~0xfLL
; /* align stack */
953 PUSHQ(esp
, env
->segs
[R_SS
].selector
);
954 PUSHQ(esp
, env
->regs
[R_ESP
]);
955 PUSHQ(esp
, cpu_compute_eflags(env
));
956 PUSHQ(esp
, env
->segs
[R_CS
].selector
);
958 if (has_error_code
) {
959 PUSHQ(esp
, error_code
);
962 /* interrupt gate clear IF mask */
963 if ((type
& 1) == 0) {
964 env
->eflags
&= ~IF_MASK
;
966 env
->eflags
&= ~(TF_MASK
| VM_MASK
| RF_MASK
| NT_MASK
);
970 cpu_x86_load_seg_cache(env
, R_SS
, ss
, 0, 0, dpl
<< DESC_DPL_SHIFT
);
972 env
->regs
[R_ESP
] = esp
;
974 selector
= (selector
& ~3) | dpl
;
975 cpu_x86_load_seg_cache(env
, R_CS
, selector
,
976 get_seg_base(e1
, e2
),
977 get_seg_limit(e1
, e2
),
981 #endif /* TARGET_X86_64 */
983 void helper_sysret(CPUX86State
*env
, int dflag
)
987 if (!(env
->efer
& MSR_EFER_SCE
)) {
988 raise_exception_err_ra(env
, EXCP06_ILLOP
, 0, GETPC());
990 cpl
= env
->hflags
& HF_CPL_MASK
;
991 if (!(env
->cr
[0] & CR0_PE_MASK
) || cpl
!= 0) {
992 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, GETPC());
994 selector
= (env
->star
>> 48) & 0xffff;
996 if (env
->hflags
& HF_LMA_MASK
) {
997 cpu_load_eflags(env
, (uint32_t)(env
->regs
[11]), TF_MASK
| AC_MASK
998 | ID_MASK
| IF_MASK
| IOPL_MASK
| VM_MASK
| RF_MASK
|
1001 cpu_x86_load_seg_cache(env
, R_CS
, (selector
+ 16) | 3,
1003 DESC_G_MASK
| DESC_P_MASK
|
1004 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
1005 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
|
1007 env
->eip
= env
->regs
[R_ECX
];
1009 cpu_x86_load_seg_cache(env
, R_CS
, selector
| 3,
1011 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
1012 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
1013 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
);
1014 env
->eip
= (uint32_t)env
->regs
[R_ECX
];
1016 cpu_x86_load_seg_cache(env
, R_SS
, (selector
+ 8) | 3,
1018 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
1019 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
1020 DESC_W_MASK
| DESC_A_MASK
);
1024 env
->eflags
|= IF_MASK
;
1025 cpu_x86_load_seg_cache(env
, R_CS
, selector
| 3,
1027 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
1028 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
1029 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
);
1030 env
->eip
= (uint32_t)env
->regs
[R_ECX
];
1031 cpu_x86_load_seg_cache(env
, R_SS
, (selector
+ 8) | 3,
1033 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
1034 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
1035 DESC_W_MASK
| DESC_A_MASK
);
1039 /* real mode interrupt */
1040 static void do_interrupt_real(CPUX86State
*env
, int intno
, int is_int
,
1041 int error_code
, unsigned int next_eip
)
1044 target_ulong ptr
, ssp
;
1046 uint32_t offset
, esp
;
1047 uint32_t old_cs
, old_eip
;
1049 /* real mode (simpler!) */
1051 if (intno
* 4 + 3 > dt
->limit
) {
1052 raise_exception_err(env
, EXCP0D_GPF
, intno
* 8 + 2);
1054 ptr
= dt
->base
+ intno
* 4;
1055 offset
= cpu_lduw_kernel(env
, ptr
);
1056 selector
= cpu_lduw_kernel(env
, ptr
+ 2);
1057 esp
= env
->regs
[R_ESP
];
1058 ssp
= env
->segs
[R_SS
].base
;
1064 old_cs
= env
->segs
[R_CS
].selector
;
1065 /* XXX: use SS segment size? */
1066 PUSHW(ssp
, esp
, 0xffff, cpu_compute_eflags(env
));
1067 PUSHW(ssp
, esp
, 0xffff, old_cs
);
1068 PUSHW(ssp
, esp
, 0xffff, old_eip
);
1070 /* update processor state */
1071 env
->regs
[R_ESP
] = (env
->regs
[R_ESP
] & ~0xffff) | (esp
& 0xffff);
1073 env
->segs
[R_CS
].selector
= selector
;
1074 env
->segs
[R_CS
].base
= (selector
<< 4);
1075 env
->eflags
&= ~(IF_MASK
| TF_MASK
| AC_MASK
| RF_MASK
);
1079 * Begin execution of an interruption. is_int is TRUE if coming from
1080 * the int instruction. next_eip is the env->eip value AFTER the interrupt
1081 * instruction. It is only relevant if is_int is TRUE.
1083 void do_interrupt_all(X86CPU
*cpu
, int intno
, int is_int
,
1084 int error_code
, target_ulong next_eip
, int is_hw
)
1086 CPUX86State
*env
= &cpu
->env
;
1088 if (qemu_loglevel_mask(CPU_LOG_INT
)) {
1089 if ((env
->cr
[0] & CR0_PE_MASK
)) {
1092 qemu_log("%6d: v=%02x e=%04x i=%d cpl=%d IP=%04x:" TARGET_FMT_lx
1093 " pc=" TARGET_FMT_lx
" SP=%04x:" TARGET_FMT_lx
,
1094 count
, intno
, error_code
, is_int
,
1095 env
->hflags
& HF_CPL_MASK
,
1096 env
->segs
[R_CS
].selector
, env
->eip
,
1097 (int)env
->segs
[R_CS
].base
+ env
->eip
,
1098 env
->segs
[R_SS
].selector
, env
->regs
[R_ESP
]);
1099 if (intno
== 0x0e) {
1100 qemu_log(" CR2=" TARGET_FMT_lx
, env
->cr
[2]);
1102 qemu_log(" env->regs[R_EAX]=" TARGET_FMT_lx
, env
->regs
[R_EAX
]);
1105 log_cpu_state(CPU(cpu
), CPU_DUMP_CCOP
);
1112 ptr
= env
->segs
[R_CS
].base
+ env
->eip
;
1113 for (i
= 0; i
< 16; i
++) {
1114 qemu_log(" %02x", ldub(ptr
+ i
));
1122 if (env
->cr
[0] & CR0_PE_MASK
) {
1123 #if !defined(CONFIG_USER_ONLY)
1124 if (env
->hflags
& HF_GUEST_MASK
) {
1125 handle_even_inj(env
, intno
, is_int
, error_code
, is_hw
, 0);
1128 #ifdef TARGET_X86_64
1129 if (env
->hflags
& HF_LMA_MASK
) {
1130 do_interrupt64(env
, intno
, is_int
, error_code
, next_eip
, is_hw
);
1134 do_interrupt_protected(env
, intno
, is_int
, error_code
, next_eip
,
1138 #if !defined(CONFIG_USER_ONLY)
1139 if (env
->hflags
& HF_GUEST_MASK
) {
1140 handle_even_inj(env
, intno
, is_int
, error_code
, is_hw
, 1);
1143 do_interrupt_real(env
, intno
, is_int
, error_code
, next_eip
);
1146 #if !defined(CONFIG_USER_ONLY)
1147 if (env
->hflags
& HF_GUEST_MASK
) {
1148 CPUState
*cs
= CPU(cpu
);
1149 uint32_t event_inj
= x86_ldl_phys(cs
, env
->vm_vmcb
+
1150 offsetof(struct vmcb
,
1151 control
.event_inj
));
1154 env
->vm_vmcb
+ offsetof(struct vmcb
, control
.event_inj
),
1155 event_inj
& ~SVM_EVTINJ_VALID
);
1160 void do_interrupt_x86_hardirq(CPUX86State
*env
, int intno
, int is_hw
)
1162 do_interrupt_all(env_archcpu(env
), intno
, 0, 0, 0, is_hw
);
1165 void helper_lldt(CPUX86State
*env
, int selector
)
1169 int index
, entry_limit
;
1173 if ((selector
& 0xfffc) == 0) {
1174 /* XXX: NULL selector case: invalid LDT */
1178 if (selector
& 0x4) {
1179 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1182 index
= selector
& ~7;
1183 #ifdef TARGET_X86_64
1184 if (env
->hflags
& HF_LMA_MASK
) {
1191 if ((index
+ entry_limit
) > dt
->limit
) {
1192 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1194 ptr
= dt
->base
+ index
;
1195 e1
= cpu_ldl_kernel_ra(env
, ptr
, GETPC());
1196 e2
= cpu_ldl_kernel_ra(env
, ptr
+ 4, GETPC());
1197 if ((e2
& DESC_S_MASK
) || ((e2
>> DESC_TYPE_SHIFT
) & 0xf) != 2) {
1198 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1200 if (!(e2
& DESC_P_MASK
)) {
1201 raise_exception_err_ra(env
, EXCP0B_NOSEG
, selector
& 0xfffc, GETPC());
1203 #ifdef TARGET_X86_64
1204 if (env
->hflags
& HF_LMA_MASK
) {
1207 e3
= cpu_ldl_kernel_ra(env
, ptr
+ 8, GETPC());
1208 load_seg_cache_raw_dt(&env
->ldt
, e1
, e2
);
1209 env
->ldt
.base
|= (target_ulong
)e3
<< 32;
1213 load_seg_cache_raw_dt(&env
->ldt
, e1
, e2
);
1216 env
->ldt
.selector
= selector
;
1219 void helper_ltr(CPUX86State
*env
, int selector
)
1223 int index
, type
, entry_limit
;
1227 if ((selector
& 0xfffc) == 0) {
1228 /* NULL selector case: invalid TR */
1233 if (selector
& 0x4) {
1234 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1237 index
= selector
& ~7;
1238 #ifdef TARGET_X86_64
1239 if (env
->hflags
& HF_LMA_MASK
) {
1246 if ((index
+ entry_limit
) > dt
->limit
) {
1247 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1249 ptr
= dt
->base
+ index
;
1250 e1
= cpu_ldl_kernel_ra(env
, ptr
, GETPC());
1251 e2
= cpu_ldl_kernel_ra(env
, ptr
+ 4, GETPC());
1252 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
1253 if ((e2
& DESC_S_MASK
) ||
1254 (type
!= 1 && type
!= 9)) {
1255 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1257 if (!(e2
& DESC_P_MASK
)) {
1258 raise_exception_err_ra(env
, EXCP0B_NOSEG
, selector
& 0xfffc, GETPC());
1260 #ifdef TARGET_X86_64
1261 if (env
->hflags
& HF_LMA_MASK
) {
1264 e3
= cpu_ldl_kernel_ra(env
, ptr
+ 8, GETPC());
1265 e4
= cpu_ldl_kernel_ra(env
, ptr
+ 12, GETPC());
1266 if ((e4
>> DESC_TYPE_SHIFT
) & 0xf) {
1267 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1269 load_seg_cache_raw_dt(&env
->tr
, e1
, e2
);
1270 env
->tr
.base
|= (target_ulong
)e3
<< 32;
1274 load_seg_cache_raw_dt(&env
->tr
, e1
, e2
);
1276 e2
|= DESC_TSS_BUSY_MASK
;
1277 cpu_stl_kernel_ra(env
, ptr
+ 4, e2
, GETPC());
1279 env
->tr
.selector
= selector
;
1282 /* only works if protected mode and not VM86. seg_reg must be != R_CS */
1283 void helper_load_seg(CPUX86State
*env
, int seg_reg
, int selector
)
1292 cpl
= env
->hflags
& HF_CPL_MASK
;
1293 if ((selector
& 0xfffc) == 0) {
1294 /* null selector case */
1296 #ifdef TARGET_X86_64
1297 && (!(env
->hflags
& HF_CS64_MASK
) || cpl
== 3)
1300 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, GETPC());
1302 cpu_x86_load_seg_cache(env
, seg_reg
, selector
, 0, 0, 0);
1305 if (selector
& 0x4) {
1310 index
= selector
& ~7;
1311 if ((index
+ 7) > dt
->limit
) {
1312 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1314 ptr
= dt
->base
+ index
;
1315 e1
= cpu_ldl_kernel_ra(env
, ptr
, GETPC());
1316 e2
= cpu_ldl_kernel_ra(env
, ptr
+ 4, GETPC());
1318 if (!(e2
& DESC_S_MASK
)) {
1319 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1322 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1323 if (seg_reg
== R_SS
) {
1324 /* must be writable segment */
1325 if ((e2
& DESC_CS_MASK
) || !(e2
& DESC_W_MASK
)) {
1326 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1328 if (rpl
!= cpl
|| dpl
!= cpl
) {
1329 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1332 /* must be readable segment */
1333 if ((e2
& (DESC_CS_MASK
| DESC_R_MASK
)) == DESC_CS_MASK
) {
1334 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1337 if (!(e2
& DESC_CS_MASK
) || !(e2
& DESC_C_MASK
)) {
1338 /* if not conforming code, test rights */
1339 if (dpl
< cpl
|| dpl
< rpl
) {
1340 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1345 if (!(e2
& DESC_P_MASK
)) {
1346 if (seg_reg
== R_SS
) {
1347 raise_exception_err_ra(env
, EXCP0C_STACK
, selector
& 0xfffc, GETPC());
1349 raise_exception_err_ra(env
, EXCP0B_NOSEG
, selector
& 0xfffc, GETPC());
1353 /* set the access bit if not already set */
1354 if (!(e2
& DESC_A_MASK
)) {
1356 cpu_stl_kernel_ra(env
, ptr
+ 4, e2
, GETPC());
1359 cpu_x86_load_seg_cache(env
, seg_reg
, selector
,
1360 get_seg_base(e1
, e2
),
1361 get_seg_limit(e1
, e2
),
1364 qemu_log("load_seg: sel=0x%04x base=0x%08lx limit=0x%08lx flags=%08x\n",
1365 selector
, (unsigned long)sc
->base
, sc
->limit
, sc
->flags
);
1370 /* protected mode jump */
1371 void helper_ljmp_protected(CPUX86State
*env
, int new_cs
, target_ulong new_eip
,
1372 target_ulong next_eip
)
1375 uint32_t e1
, e2
, cpl
, dpl
, rpl
, limit
;
1377 if ((new_cs
& 0xfffc) == 0) {
1378 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, GETPC());
1380 if (load_segment_ra(env
, &e1
, &e2
, new_cs
, GETPC()) != 0) {
1381 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1383 cpl
= env
->hflags
& HF_CPL_MASK
;
1384 if (e2
& DESC_S_MASK
) {
1385 if (!(e2
& DESC_CS_MASK
)) {
1386 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1388 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1389 if (e2
& DESC_C_MASK
) {
1390 /* conforming code segment */
1392 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1395 /* non conforming code segment */
1398 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1401 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1404 if (!(e2
& DESC_P_MASK
)) {
1405 raise_exception_err_ra(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc, GETPC());
1407 limit
= get_seg_limit(e1
, e2
);
1408 if (new_eip
> limit
&&
1409 (!(env
->hflags
& HF_LMA_MASK
) || !(e2
& DESC_L_MASK
))) {
1410 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, GETPC());
1412 cpu_x86_load_seg_cache(env
, R_CS
, (new_cs
& 0xfffc) | cpl
,
1413 get_seg_base(e1
, e2
), limit
, e2
);
1416 /* jump to call or task gate */
1417 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1419 cpl
= env
->hflags
& HF_CPL_MASK
;
1420 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
1422 #ifdef TARGET_X86_64
1423 if (env
->efer
& MSR_EFER_LMA
) {
1425 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1430 case 1: /* 286 TSS */
1431 case 9: /* 386 TSS */
1432 case 5: /* task gate */
1433 if (dpl
< cpl
|| dpl
< rpl
) {
1434 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1436 switch_tss_ra(env
, new_cs
, e1
, e2
, SWITCH_TSS_JMP
, next_eip
, GETPC());
1438 case 4: /* 286 call gate */
1439 case 12: /* 386 call gate */
1440 if ((dpl
< cpl
) || (dpl
< rpl
)) {
1441 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1443 if (!(e2
& DESC_P_MASK
)) {
1444 raise_exception_err_ra(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc, GETPC());
1447 new_eip
= (e1
& 0xffff);
1449 new_eip
|= (e2
& 0xffff0000);
1452 #ifdef TARGET_X86_64
1453 if (env
->efer
& MSR_EFER_LMA
) {
1454 /* load the upper 8 bytes of the 64-bit call gate */
1455 if (load_segment_ra(env
, &e1
, &e2
, new_cs
+ 8, GETPC())) {
1456 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc,
1459 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x1f;
1461 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc,
1464 new_eip
|= ((target_ulong
)e1
) << 32;
1468 if (load_segment_ra(env
, &e1
, &e2
, gate_cs
, GETPC()) != 0) {
1469 raise_exception_err_ra(env
, EXCP0D_GPF
, gate_cs
& 0xfffc, GETPC());
1471 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1472 /* must be code segment */
1473 if (((e2
& (DESC_S_MASK
| DESC_CS_MASK
)) !=
1474 (DESC_S_MASK
| DESC_CS_MASK
))) {
1475 raise_exception_err_ra(env
, EXCP0D_GPF
, gate_cs
& 0xfffc, GETPC());
1477 if (((e2
& DESC_C_MASK
) && (dpl
> cpl
)) ||
1478 (!(e2
& DESC_C_MASK
) && (dpl
!= cpl
))) {
1479 raise_exception_err_ra(env
, EXCP0D_GPF
, gate_cs
& 0xfffc, GETPC());
1481 #ifdef TARGET_X86_64
1482 if (env
->efer
& MSR_EFER_LMA
) {
1483 if (!(e2
& DESC_L_MASK
)) {
1484 raise_exception_err_ra(env
, EXCP0D_GPF
, gate_cs
& 0xfffc, GETPC());
1486 if (e2
& DESC_B_MASK
) {
1487 raise_exception_err_ra(env
, EXCP0D_GPF
, gate_cs
& 0xfffc, GETPC());
1491 if (!(e2
& DESC_P_MASK
)) {
1492 raise_exception_err_ra(env
, EXCP0D_GPF
, gate_cs
& 0xfffc, GETPC());
1494 limit
= get_seg_limit(e1
, e2
);
1495 if (new_eip
> limit
&&
1496 (!(env
->hflags
& HF_LMA_MASK
) || !(e2
& DESC_L_MASK
))) {
1497 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, GETPC());
1499 cpu_x86_load_seg_cache(env
, R_CS
, (gate_cs
& 0xfffc) | cpl
,
1500 get_seg_base(e1
, e2
), limit
, e2
);
1504 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1510 /* real mode call */
1511 void helper_lcall_real(CPUX86State
*env
, uint32_t new_cs
, uint32_t new_eip
,
1512 int shift
, uint32_t next_eip
)
1514 uint32_t esp
, esp_mask
;
1517 esp
= env
->regs
[R_ESP
];
1518 esp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1519 ssp
= env
->segs
[R_SS
].base
;
1521 PUSHL_RA(ssp
, esp
, esp_mask
, env
->segs
[R_CS
].selector
, GETPC());
1522 PUSHL_RA(ssp
, esp
, esp_mask
, next_eip
, GETPC());
1524 PUSHW_RA(ssp
, esp
, esp_mask
, env
->segs
[R_CS
].selector
, GETPC());
1525 PUSHW_RA(ssp
, esp
, esp_mask
, next_eip
, GETPC());
1528 SET_ESP(esp
, esp_mask
);
1530 env
->segs
[R_CS
].selector
= new_cs
;
1531 env
->segs
[R_CS
].base
= (new_cs
<< 4);
1534 /* protected mode call */
1535 void helper_lcall_protected(CPUX86State
*env
, int new_cs
, target_ulong new_eip
,
1536 int shift
, target_ulong next_eip
)
1539 uint32_t e1
, e2
, cpl
, dpl
, rpl
, selector
, param_count
;
1540 uint32_t ss
= 0, ss_e1
= 0, ss_e2
= 0, type
, ss_dpl
, sp_mask
;
1541 uint32_t val
, limit
, old_sp_mask
;
1542 target_ulong ssp
, old_ssp
, offset
, sp
;
1544 LOG_PCALL("lcall %04x:" TARGET_FMT_lx
" s=%d\n", new_cs
, new_eip
, shift
);
1545 LOG_PCALL_STATE(env_cpu(env
));
1546 if ((new_cs
& 0xfffc) == 0) {
1547 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, GETPC());
1549 if (load_segment_ra(env
, &e1
, &e2
, new_cs
, GETPC()) != 0) {
1550 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1552 cpl
= env
->hflags
& HF_CPL_MASK
;
1553 LOG_PCALL("desc=%08x:%08x\n", e1
, e2
);
1554 if (e2
& DESC_S_MASK
) {
1555 if (!(e2
& DESC_CS_MASK
)) {
1556 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1558 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1559 if (e2
& DESC_C_MASK
) {
1560 /* conforming code segment */
1562 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1565 /* non conforming code segment */
1568 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1571 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1574 if (!(e2
& DESC_P_MASK
)) {
1575 raise_exception_err_ra(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc, GETPC());
1578 #ifdef TARGET_X86_64
1579 /* XXX: check 16/32 bit cases in long mode */
1584 rsp
= env
->regs
[R_ESP
];
1585 PUSHQ_RA(rsp
, env
->segs
[R_CS
].selector
, GETPC());
1586 PUSHQ_RA(rsp
, next_eip
, GETPC());
1587 /* from this point, not restartable */
1588 env
->regs
[R_ESP
] = rsp
;
1589 cpu_x86_load_seg_cache(env
, R_CS
, (new_cs
& 0xfffc) | cpl
,
1590 get_seg_base(e1
, e2
),
1591 get_seg_limit(e1
, e2
), e2
);
1596 sp
= env
->regs
[R_ESP
];
1597 sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1598 ssp
= env
->segs
[R_SS
].base
;
1600 PUSHL_RA(ssp
, sp
, sp_mask
, env
->segs
[R_CS
].selector
, GETPC());
1601 PUSHL_RA(ssp
, sp
, sp_mask
, next_eip
, GETPC());
1603 PUSHW_RA(ssp
, sp
, sp_mask
, env
->segs
[R_CS
].selector
, GETPC());
1604 PUSHW_RA(ssp
, sp
, sp_mask
, next_eip
, GETPC());
1607 limit
= get_seg_limit(e1
, e2
);
1608 if (new_eip
> limit
) {
1609 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1611 /* from this point, not restartable */
1612 SET_ESP(sp
, sp_mask
);
1613 cpu_x86_load_seg_cache(env
, R_CS
, (new_cs
& 0xfffc) | cpl
,
1614 get_seg_base(e1
, e2
), limit
, e2
);
1618 /* check gate type */
1619 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x1f;
1620 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1623 #ifdef TARGET_X86_64
1624 if (env
->efer
& MSR_EFER_LMA
) {
1626 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1632 case 1: /* available 286 TSS */
1633 case 9: /* available 386 TSS */
1634 case 5: /* task gate */
1635 if (dpl
< cpl
|| dpl
< rpl
) {
1636 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1638 switch_tss_ra(env
, new_cs
, e1
, e2
, SWITCH_TSS_CALL
, next_eip
, GETPC());
1640 case 4: /* 286 call gate */
1641 case 12: /* 386 call gate */
1644 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1649 if (dpl
< cpl
|| dpl
< rpl
) {
1650 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, GETPC());
1652 /* check valid bit */
1653 if (!(e2
& DESC_P_MASK
)) {
1654 raise_exception_err_ra(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc, GETPC());
1656 selector
= e1
>> 16;
1657 param_count
= e2
& 0x1f;
1658 offset
= (e2
& 0xffff0000) | (e1
& 0x0000ffff);
1659 #ifdef TARGET_X86_64
1660 if (env
->efer
& MSR_EFER_LMA
) {
1661 /* load the upper 8 bytes of the 64-bit call gate */
1662 if (load_segment_ra(env
, &e1
, &e2
, new_cs
+ 8, GETPC())) {
1663 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc,
1666 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x1f;
1668 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc,
1671 offset
|= ((target_ulong
)e1
) << 32;
1674 if ((selector
& 0xfffc) == 0) {
1675 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, GETPC());
1678 if (load_segment_ra(env
, &e1
, &e2
, selector
, GETPC()) != 0) {
1679 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1681 if (!(e2
& DESC_S_MASK
) || !(e2
& (DESC_CS_MASK
))) {
1682 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1684 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1686 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1688 #ifdef TARGET_X86_64
1689 if (env
->efer
& MSR_EFER_LMA
) {
1690 if (!(e2
& DESC_L_MASK
)) {
1691 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1693 if (e2
& DESC_B_MASK
) {
1694 raise_exception_err_ra(env
, EXCP0D_GPF
, selector
& 0xfffc, GETPC());
1699 if (!(e2
& DESC_P_MASK
)) {
1700 raise_exception_err_ra(env
, EXCP0B_NOSEG
, selector
& 0xfffc, GETPC());
1703 if (!(e2
& DESC_C_MASK
) && dpl
< cpl
) {
1704 /* to inner privilege */
1705 #ifdef TARGET_X86_64
1707 sp
= get_rsp_from_tss(env
, dpl
);
1708 ss
= dpl
; /* SS = NULL selector with RPL = new CPL */
1711 ssp
= 0; /* SS base is always zero in IA-32e mode */
1712 LOG_PCALL("new ss:rsp=%04x:%016llx env->regs[R_ESP]="
1713 TARGET_FMT_lx
"\n", ss
, sp
, env
->regs
[R_ESP
]);
1718 get_ss_esp_from_tss(env
, &ss
, &sp32
, dpl
, GETPC());
1719 LOG_PCALL("new ss:esp=%04x:%08x param_count=%d env->regs[R_ESP]="
1720 TARGET_FMT_lx
"\n", ss
, sp32
, param_count
,
1723 if ((ss
& 0xfffc) == 0) {
1724 raise_exception_err_ra(env
, EXCP0A_TSS
, ss
& 0xfffc, GETPC());
1726 if ((ss
& 3) != dpl
) {
1727 raise_exception_err_ra(env
, EXCP0A_TSS
, ss
& 0xfffc, GETPC());
1729 if (load_segment_ra(env
, &ss_e1
, &ss_e2
, ss
, GETPC()) != 0) {
1730 raise_exception_err_ra(env
, EXCP0A_TSS
, ss
& 0xfffc, GETPC());
1732 ss_dpl
= (ss_e2
>> DESC_DPL_SHIFT
) & 3;
1733 if (ss_dpl
!= dpl
) {
1734 raise_exception_err_ra(env
, EXCP0A_TSS
, ss
& 0xfffc, GETPC());
1736 if (!(ss_e2
& DESC_S_MASK
) ||
1737 (ss_e2
& DESC_CS_MASK
) ||
1738 !(ss_e2
& DESC_W_MASK
)) {
1739 raise_exception_err_ra(env
, EXCP0A_TSS
, ss
& 0xfffc, GETPC());
1741 if (!(ss_e2
& DESC_P_MASK
)) {
1742 raise_exception_err_ra(env
, EXCP0A_TSS
, ss
& 0xfffc, GETPC());
1745 sp_mask
= get_sp_mask(ss_e2
);
1746 ssp
= get_seg_base(ss_e1
, ss_e2
);
1749 /* push_size = ((param_count * 2) + 8) << shift; */
1751 old_sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1752 old_ssp
= env
->segs
[R_SS
].base
;
1753 #ifdef TARGET_X86_64
1755 /* XXX: verify if new stack address is canonical */
1756 PUSHQ_RA(sp
, env
->segs
[R_SS
].selector
, GETPC());
1757 PUSHQ_RA(sp
, env
->regs
[R_ESP
], GETPC());
1758 /* parameters aren't supported for 64-bit call gates */
1762 PUSHL_RA(ssp
, sp
, sp_mask
, env
->segs
[R_SS
].selector
, GETPC());
1763 PUSHL_RA(ssp
, sp
, sp_mask
, env
->regs
[R_ESP
], GETPC());
1764 for (i
= param_count
- 1; i
>= 0; i
--) {
1765 val
= cpu_ldl_kernel_ra(env
, old_ssp
+
1766 ((env
->regs
[R_ESP
] + i
* 4) &
1767 old_sp_mask
), GETPC());
1768 PUSHL_RA(ssp
, sp
, sp_mask
, val
, GETPC());
1771 PUSHW_RA(ssp
, sp
, sp_mask
, env
->segs
[R_SS
].selector
, GETPC());
1772 PUSHW_RA(ssp
, sp
, sp_mask
, env
->regs
[R_ESP
], GETPC());
1773 for (i
= param_count
- 1; i
>= 0; i
--) {
1774 val
= cpu_lduw_kernel_ra(env
, old_ssp
+
1775 ((env
->regs
[R_ESP
] + i
* 2) &
1776 old_sp_mask
), GETPC());
1777 PUSHW_RA(ssp
, sp
, sp_mask
, val
, GETPC());
1782 /* to same privilege */
1783 sp
= env
->regs
[R_ESP
];
1784 sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1785 ssp
= env
->segs
[R_SS
].base
;
1786 /* push_size = (4 << shift); */
1790 #ifdef TARGET_X86_64
1792 PUSHQ_RA(sp
, env
->segs
[R_CS
].selector
, GETPC());
1793 PUSHQ_RA(sp
, next_eip
, GETPC());
1797 PUSHL_RA(ssp
, sp
, sp_mask
, env
->segs
[R_CS
].selector
, GETPC());
1798 PUSHL_RA(ssp
, sp
, sp_mask
, next_eip
, GETPC());
1800 PUSHW_RA(ssp
, sp
, sp_mask
, env
->segs
[R_CS
].selector
, GETPC());
1801 PUSHW_RA(ssp
, sp
, sp_mask
, next_eip
, GETPC());
1804 /* from this point, not restartable */
1807 #ifdef TARGET_X86_64
1809 cpu_x86_load_seg_cache(env
, R_SS
, ss
, 0, 0, 0);
1813 ss
= (ss
& ~3) | dpl
;
1814 cpu_x86_load_seg_cache(env
, R_SS
, ss
,
1816 get_seg_limit(ss_e1
, ss_e2
),
1821 selector
= (selector
& ~3) | dpl
;
1822 cpu_x86_load_seg_cache(env
, R_CS
, selector
,
1823 get_seg_base(e1
, e2
),
1824 get_seg_limit(e1
, e2
),
1826 SET_ESP(sp
, sp_mask
);
1831 /* real and vm86 mode iret */
1832 void helper_iret_real(CPUX86State
*env
, int shift
)
1834 uint32_t sp
, new_cs
, new_eip
, new_eflags
, sp_mask
;
1838 sp_mask
= 0xffff; /* XXXX: use SS segment size? */
1839 sp
= env
->regs
[R_ESP
];
1840 ssp
= env
->segs
[R_SS
].base
;
1843 POPL_RA(ssp
, sp
, sp_mask
, new_eip
, GETPC());
1844 POPL_RA(ssp
, sp
, sp_mask
, new_cs
, GETPC());
1846 POPL_RA(ssp
, sp
, sp_mask
, new_eflags
, GETPC());
1849 POPW_RA(ssp
, sp
, sp_mask
, new_eip
, GETPC());
1850 POPW_RA(ssp
, sp
, sp_mask
, new_cs
, GETPC());
1851 POPW_RA(ssp
, sp
, sp_mask
, new_eflags
, GETPC());
1853 env
->regs
[R_ESP
] = (env
->regs
[R_ESP
] & ~sp_mask
) | (sp
& sp_mask
);
1854 env
->segs
[R_CS
].selector
= new_cs
;
1855 env
->segs
[R_CS
].base
= (new_cs
<< 4);
1857 if (env
->eflags
& VM_MASK
) {
1858 eflags_mask
= TF_MASK
| AC_MASK
| ID_MASK
| IF_MASK
| RF_MASK
|
1861 eflags_mask
= TF_MASK
| AC_MASK
| ID_MASK
| IF_MASK
| IOPL_MASK
|
1865 eflags_mask
&= 0xffff;
1867 cpu_load_eflags(env
, new_eflags
, eflags_mask
);
1868 env
->hflags2
&= ~HF2_NMI_MASK
;
1871 static inline void validate_seg(CPUX86State
*env
, X86Seg seg_reg
, int cpl
)
1876 /* XXX: on x86_64, we do not want to nullify FS and GS because
1877 they may still contain a valid base. I would be interested to
1878 know how a real x86_64 CPU behaves */
1879 if ((seg_reg
== R_FS
|| seg_reg
== R_GS
) &&
1880 (env
->segs
[seg_reg
].selector
& 0xfffc) == 0) {
1884 e2
= env
->segs
[seg_reg
].flags
;
1885 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1886 if (!(e2
& DESC_CS_MASK
) || !(e2
& DESC_C_MASK
)) {
1887 /* data or non conforming code segment */
1889 cpu_x86_load_seg_cache(env
, seg_reg
, 0,
1890 env
->segs
[seg_reg
].base
,
1891 env
->segs
[seg_reg
].limit
,
1892 env
->segs
[seg_reg
].flags
& ~DESC_P_MASK
);
1897 /* protected mode iret */
1898 static inline void helper_ret_protected(CPUX86State
*env
, int shift
,
1899 int is_iret
, int addend
,
1902 uint32_t new_cs
, new_eflags
, new_ss
;
1903 uint32_t new_es
, new_ds
, new_fs
, new_gs
;
1904 uint32_t e1
, e2
, ss_e1
, ss_e2
;
1905 int cpl
, dpl
, rpl
, eflags_mask
, iopl
;
1906 target_ulong ssp
, sp
, new_eip
, new_esp
, sp_mask
;
1908 #ifdef TARGET_X86_64
1914 sp_mask
= get_sp_mask(env
->segs
[R_SS
].flags
);
1916 sp
= env
->regs
[R_ESP
];
1917 ssp
= env
->segs
[R_SS
].base
;
1918 new_eflags
= 0; /* avoid warning */
1919 #ifdef TARGET_X86_64
1921 POPQ_RA(sp
, new_eip
, retaddr
);
1922 POPQ_RA(sp
, new_cs
, retaddr
);
1925 POPQ_RA(sp
, new_eflags
, retaddr
);
1932 POPL_RA(ssp
, sp
, sp_mask
, new_eip
, retaddr
);
1933 POPL_RA(ssp
, sp
, sp_mask
, new_cs
, retaddr
);
1936 POPL_RA(ssp
, sp
, sp_mask
, new_eflags
, retaddr
);
1937 if (new_eflags
& VM_MASK
) {
1938 goto return_to_vm86
;
1943 POPW_RA(ssp
, sp
, sp_mask
, new_eip
, retaddr
);
1944 POPW_RA(ssp
, sp
, sp_mask
, new_cs
, retaddr
);
1946 POPW_RA(ssp
, sp
, sp_mask
, new_eflags
, retaddr
);
1950 LOG_PCALL("lret new %04x:" TARGET_FMT_lx
" s=%d addend=0x%x\n",
1951 new_cs
, new_eip
, shift
, addend
);
1952 LOG_PCALL_STATE(env_cpu(env
));
1953 if ((new_cs
& 0xfffc) == 0) {
1954 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, retaddr
);
1956 if (load_segment_ra(env
, &e1
, &e2
, new_cs
, retaddr
) != 0) {
1957 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, retaddr
);
1959 if (!(e2
& DESC_S_MASK
) ||
1960 !(e2
& DESC_CS_MASK
)) {
1961 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, retaddr
);
1963 cpl
= env
->hflags
& HF_CPL_MASK
;
1966 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, retaddr
);
1968 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
1969 if (e2
& DESC_C_MASK
) {
1971 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, retaddr
);
1975 raise_exception_err_ra(env
, EXCP0D_GPF
, new_cs
& 0xfffc, retaddr
);
1978 if (!(e2
& DESC_P_MASK
)) {
1979 raise_exception_err_ra(env
, EXCP0B_NOSEG
, new_cs
& 0xfffc, retaddr
);
1983 if (rpl
== cpl
&& (!(env
->hflags
& HF_CS64_MASK
) ||
1984 ((env
->hflags
& HF_CS64_MASK
) && !is_iret
))) {
1985 /* return to same privilege level */
1986 cpu_x86_load_seg_cache(env
, R_CS
, new_cs
,
1987 get_seg_base(e1
, e2
),
1988 get_seg_limit(e1
, e2
),
1991 /* return to different privilege level */
1992 #ifdef TARGET_X86_64
1994 POPQ_RA(sp
, new_esp
, retaddr
);
1995 POPQ_RA(sp
, new_ss
, retaddr
);
2002 POPL_RA(ssp
, sp
, sp_mask
, new_esp
, retaddr
);
2003 POPL_RA(ssp
, sp
, sp_mask
, new_ss
, retaddr
);
2007 POPW_RA(ssp
, sp
, sp_mask
, new_esp
, retaddr
);
2008 POPW_RA(ssp
, sp
, sp_mask
, new_ss
, retaddr
);
2011 LOG_PCALL("new ss:esp=%04x:" TARGET_FMT_lx
"\n",
2013 if ((new_ss
& 0xfffc) == 0) {
2014 #ifdef TARGET_X86_64
2015 /* NULL ss is allowed in long mode if cpl != 3 */
2016 /* XXX: test CS64? */
2017 if ((env
->hflags
& HF_LMA_MASK
) && rpl
!= 3) {
2018 cpu_x86_load_seg_cache(env
, R_SS
, new_ss
,
2020 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2021 DESC_S_MASK
| (rpl
<< DESC_DPL_SHIFT
) |
2022 DESC_W_MASK
| DESC_A_MASK
);
2023 ss_e2
= DESC_B_MASK
; /* XXX: should not be needed? */
2027 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, retaddr
);
2030 if ((new_ss
& 3) != rpl
) {
2031 raise_exception_err_ra(env
, EXCP0D_GPF
, new_ss
& 0xfffc, retaddr
);
2033 if (load_segment_ra(env
, &ss_e1
, &ss_e2
, new_ss
, retaddr
) != 0) {
2034 raise_exception_err_ra(env
, EXCP0D_GPF
, new_ss
& 0xfffc, retaddr
);
2036 if (!(ss_e2
& DESC_S_MASK
) ||
2037 (ss_e2
& DESC_CS_MASK
) ||
2038 !(ss_e2
& DESC_W_MASK
)) {
2039 raise_exception_err_ra(env
, EXCP0D_GPF
, new_ss
& 0xfffc, retaddr
);
2041 dpl
= (ss_e2
>> DESC_DPL_SHIFT
) & 3;
2043 raise_exception_err_ra(env
, EXCP0D_GPF
, new_ss
& 0xfffc, retaddr
);
2045 if (!(ss_e2
& DESC_P_MASK
)) {
2046 raise_exception_err_ra(env
, EXCP0B_NOSEG
, new_ss
& 0xfffc, retaddr
);
2048 cpu_x86_load_seg_cache(env
, R_SS
, new_ss
,
2049 get_seg_base(ss_e1
, ss_e2
),
2050 get_seg_limit(ss_e1
, ss_e2
),
2054 cpu_x86_load_seg_cache(env
, R_CS
, new_cs
,
2055 get_seg_base(e1
, e2
),
2056 get_seg_limit(e1
, e2
),
2059 #ifdef TARGET_X86_64
2060 if (env
->hflags
& HF_CS64_MASK
) {
2065 sp_mask
= get_sp_mask(ss_e2
);
2068 /* validate data segments */
2069 validate_seg(env
, R_ES
, rpl
);
2070 validate_seg(env
, R_DS
, rpl
);
2071 validate_seg(env
, R_FS
, rpl
);
2072 validate_seg(env
, R_GS
, rpl
);
2076 SET_ESP(sp
, sp_mask
);
2079 /* NOTE: 'cpl' is the _old_ CPL */
2080 eflags_mask
= TF_MASK
| AC_MASK
| ID_MASK
| RF_MASK
| NT_MASK
;
2082 eflags_mask
|= IOPL_MASK
;
2084 iopl
= (env
->eflags
>> IOPL_SHIFT
) & 3;
2086 eflags_mask
|= IF_MASK
;
2089 eflags_mask
&= 0xffff;
2091 cpu_load_eflags(env
, new_eflags
, eflags_mask
);
2096 POPL_RA(ssp
, sp
, sp_mask
, new_esp
, retaddr
);
2097 POPL_RA(ssp
, sp
, sp_mask
, new_ss
, retaddr
);
2098 POPL_RA(ssp
, sp
, sp_mask
, new_es
, retaddr
);
2099 POPL_RA(ssp
, sp
, sp_mask
, new_ds
, retaddr
);
2100 POPL_RA(ssp
, sp
, sp_mask
, new_fs
, retaddr
);
2101 POPL_RA(ssp
, sp
, sp_mask
, new_gs
, retaddr
);
2103 /* modify processor state */
2104 cpu_load_eflags(env
, new_eflags
, TF_MASK
| AC_MASK
| ID_MASK
|
2105 IF_MASK
| IOPL_MASK
| VM_MASK
| NT_MASK
| VIF_MASK
|
2107 load_seg_vm(env
, R_CS
, new_cs
& 0xffff);
2108 load_seg_vm(env
, R_SS
, new_ss
& 0xffff);
2109 load_seg_vm(env
, R_ES
, new_es
& 0xffff);
2110 load_seg_vm(env
, R_DS
, new_ds
& 0xffff);
2111 load_seg_vm(env
, R_FS
, new_fs
& 0xffff);
2112 load_seg_vm(env
, R_GS
, new_gs
& 0xffff);
2114 env
->eip
= new_eip
& 0xffff;
2115 env
->regs
[R_ESP
] = new_esp
;
2118 void helper_iret_protected(CPUX86State
*env
, int shift
, int next_eip
)
2120 int tss_selector
, type
;
2123 /* specific case for TSS */
2124 if (env
->eflags
& NT_MASK
) {
2125 #ifdef TARGET_X86_64
2126 if (env
->hflags
& HF_LMA_MASK
) {
2127 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, GETPC());
2130 tss_selector
= cpu_lduw_kernel_ra(env
, env
->tr
.base
+ 0, GETPC());
2131 if (tss_selector
& 4) {
2132 raise_exception_err_ra(env
, EXCP0A_TSS
, tss_selector
& 0xfffc, GETPC());
2134 if (load_segment_ra(env
, &e1
, &e2
, tss_selector
, GETPC()) != 0) {
2135 raise_exception_err_ra(env
, EXCP0A_TSS
, tss_selector
& 0xfffc, GETPC());
2137 type
= (e2
>> DESC_TYPE_SHIFT
) & 0x17;
2138 /* NOTE: we check both segment and busy TSS */
2140 raise_exception_err_ra(env
, EXCP0A_TSS
, tss_selector
& 0xfffc, GETPC());
2142 switch_tss_ra(env
, tss_selector
, e1
, e2
, SWITCH_TSS_IRET
, next_eip
, GETPC());
2144 helper_ret_protected(env
, shift
, 1, 0, GETPC());
2146 env
->hflags2
&= ~HF2_NMI_MASK
;
2149 void helper_lret_protected(CPUX86State
*env
, int shift
, int addend
)
2151 helper_ret_protected(env
, shift
, 0, addend
, GETPC());
2154 void helper_sysenter(CPUX86State
*env
)
2156 if (env
->sysenter_cs
== 0) {
2157 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, GETPC());
2159 env
->eflags
&= ~(VM_MASK
| IF_MASK
| RF_MASK
);
2161 #ifdef TARGET_X86_64
2162 if (env
->hflags
& HF_LMA_MASK
) {
2163 cpu_x86_load_seg_cache(env
, R_CS
, env
->sysenter_cs
& 0xfffc,
2165 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2167 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
|
2172 cpu_x86_load_seg_cache(env
, R_CS
, env
->sysenter_cs
& 0xfffc,
2174 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2176 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
);
2178 cpu_x86_load_seg_cache(env
, R_SS
, (env
->sysenter_cs
+ 8) & 0xfffc,
2180 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2182 DESC_W_MASK
| DESC_A_MASK
);
2183 env
->regs
[R_ESP
] = env
->sysenter_esp
;
2184 env
->eip
= env
->sysenter_eip
;
2187 void helper_sysexit(CPUX86State
*env
, int dflag
)
2191 cpl
= env
->hflags
& HF_CPL_MASK
;
2192 if (env
->sysenter_cs
== 0 || cpl
!= 0) {
2193 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, GETPC());
2195 #ifdef TARGET_X86_64
2197 cpu_x86_load_seg_cache(env
, R_CS
, ((env
->sysenter_cs
+ 32) & 0xfffc) |
2199 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2200 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
2201 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
|
2203 cpu_x86_load_seg_cache(env
, R_SS
, ((env
->sysenter_cs
+ 40) & 0xfffc) |
2205 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2206 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
2207 DESC_W_MASK
| DESC_A_MASK
);
2211 cpu_x86_load_seg_cache(env
, R_CS
, ((env
->sysenter_cs
+ 16) & 0xfffc) |
2213 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2214 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
2215 DESC_CS_MASK
| DESC_R_MASK
| DESC_A_MASK
);
2216 cpu_x86_load_seg_cache(env
, R_SS
, ((env
->sysenter_cs
+ 24) & 0xfffc) |
2218 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
|
2219 DESC_S_MASK
| (3 << DESC_DPL_SHIFT
) |
2220 DESC_W_MASK
| DESC_A_MASK
);
2222 env
->regs
[R_ESP
] = env
->regs
[R_ECX
];
2223 env
->eip
= env
->regs
[R_EDX
];
2226 target_ulong
helper_lsl(CPUX86State
*env
, target_ulong selector1
)
2229 uint32_t e1
, e2
, eflags
, selector
;
2230 int rpl
, dpl
, cpl
, type
;
2232 selector
= selector1
& 0xffff;
2233 eflags
= cpu_cc_compute_all(env
, CC_OP
);
2234 if ((selector
& 0xfffc) == 0) {
2237 if (load_segment_ra(env
, &e1
, &e2
, selector
, GETPC()) != 0) {
2241 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
2242 cpl
= env
->hflags
& HF_CPL_MASK
;
2243 if (e2
& DESC_S_MASK
) {
2244 if ((e2
& DESC_CS_MASK
) && (e2
& DESC_C_MASK
)) {
2247 if (dpl
< cpl
|| dpl
< rpl
) {
2252 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
2263 if (dpl
< cpl
|| dpl
< rpl
) {
2265 CC_SRC
= eflags
& ~CC_Z
;
2269 limit
= get_seg_limit(e1
, e2
);
2270 CC_SRC
= eflags
| CC_Z
;
2274 target_ulong
helper_lar(CPUX86State
*env
, target_ulong selector1
)
2276 uint32_t e1
, e2
, eflags
, selector
;
2277 int rpl
, dpl
, cpl
, type
;
2279 selector
= selector1
& 0xffff;
2280 eflags
= cpu_cc_compute_all(env
, CC_OP
);
2281 if ((selector
& 0xfffc) == 0) {
2284 if (load_segment_ra(env
, &e1
, &e2
, selector
, GETPC()) != 0) {
2288 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
2289 cpl
= env
->hflags
& HF_CPL_MASK
;
2290 if (e2
& DESC_S_MASK
) {
2291 if ((e2
& DESC_CS_MASK
) && (e2
& DESC_C_MASK
)) {
2294 if (dpl
< cpl
|| dpl
< rpl
) {
2299 type
= (e2
>> DESC_TYPE_SHIFT
) & 0xf;
2313 if (dpl
< cpl
|| dpl
< rpl
) {
2315 CC_SRC
= eflags
& ~CC_Z
;
2319 CC_SRC
= eflags
| CC_Z
;
2320 return e2
& 0x00f0ff00;
2323 void helper_verr(CPUX86State
*env
, target_ulong selector1
)
2325 uint32_t e1
, e2
, eflags
, selector
;
2328 selector
= selector1
& 0xffff;
2329 eflags
= cpu_cc_compute_all(env
, CC_OP
);
2330 if ((selector
& 0xfffc) == 0) {
2333 if (load_segment_ra(env
, &e1
, &e2
, selector
, GETPC()) != 0) {
2336 if (!(e2
& DESC_S_MASK
)) {
2340 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
2341 cpl
= env
->hflags
& HF_CPL_MASK
;
2342 if (e2
& DESC_CS_MASK
) {
2343 if (!(e2
& DESC_R_MASK
)) {
2346 if (!(e2
& DESC_C_MASK
)) {
2347 if (dpl
< cpl
|| dpl
< rpl
) {
2352 if (dpl
< cpl
|| dpl
< rpl
) {
2354 CC_SRC
= eflags
& ~CC_Z
;
2358 CC_SRC
= eflags
| CC_Z
;
2361 void helper_verw(CPUX86State
*env
, target_ulong selector1
)
2363 uint32_t e1
, e2
, eflags
, selector
;
2366 selector
= selector1
& 0xffff;
2367 eflags
= cpu_cc_compute_all(env
, CC_OP
);
2368 if ((selector
& 0xfffc) == 0) {
2371 if (load_segment_ra(env
, &e1
, &e2
, selector
, GETPC()) != 0) {
2374 if (!(e2
& DESC_S_MASK
)) {
2378 dpl
= (e2
>> DESC_DPL_SHIFT
) & 3;
2379 cpl
= env
->hflags
& HF_CPL_MASK
;
2380 if (e2
& DESC_CS_MASK
) {
2383 if (dpl
< cpl
|| dpl
< rpl
) {
2386 if (!(e2
& DESC_W_MASK
)) {
2388 CC_SRC
= eflags
& ~CC_Z
;
2392 CC_SRC
= eflags
| CC_Z
;