* [PATCH 0/3] target/i386: seg_helper fixes for segment, TSS, and EXT behavior
@ 2026-12-04 21:26 Nick Sampanis
2026-12-04 21:26 ` [PATCH 1/3] target/i386/tcg: Set accessed bit on CS, SS and TSS segment loads Nick Sampanis
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Nick Sampanis @ 2026-12-04 21:26 UTC (permalink / raw)
To: qemu-devel; +Cc: Paolo Bonzini, Richard Henderson, Nick Sampanis
This series makes three fixes in target/i386/tcg/seg_helper.c where
TCG segment, TSS, and exception error-code behavior diverges from
real hardware. They came up while running hypervisors under TCG when
guest-visible state did not match what hardware produces.
- Patch 1: sets the accessed bit on CS/SS segment loads and
task-switch segment loads.
- Patch 2: keeps the busy bit set in the hidden TR cache, relaxes TR
type checks so state from older QEMU versions with type 9 is still
accepted, and updates check_io().
- Patch 3: sets the EXT error-code bit during event delivery with
Intel and AMD vendor differences handled.
The changes are logically independent, though patch 3 applies on top
of patches 1 and 2.
Nick Sampanis (3):
target/i386/tcg: Set accessed bit on CS, SS and TSS segment loads
target/i386/tcg: Keep busy TSS type in TR hidden state
target/i386/tcg: Set EXT bit in error codes during event delivery
target/i386/tcg/seg_helper.c | 214 +++++++++++++++++-----------
target/i386/tcg/system/seg_helper.c | 2 +-
2 files changed, 131 insertions(+), 85 deletions(-)
--
2.51.0.windows.2
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/3] target/i386/tcg: Set accessed bit on CS, SS and TSS segment loads
2026-12-04 21:26 [PATCH 0/3] target/i386: seg_helper fixes for segment, TSS, and EXT behavior Nick Sampanis
@ 2026-12-04 21:26 ` Nick Sampanis
2026-12-04 21:26 ` [PATCH 2/3] target/i386/tcg: Keep busy TSS type in TR hidden state Nick Sampanis
2026-12-04 21:26 ` [PATCH 3/3] target/i386/tcg: Set EXT bit in error codes during event delivery Nick Sampanis
2 siblings, 0 replies; 4+ messages in thread
From: Nick Sampanis @ 2026-12-04 21:26 UTC (permalink / raw)
To: qemu-devel; +Cc: Paolo Bonzini, Richard Henderson, Nick Sampanis
The accessed bit isn't set for the CS, SS, TSS segments in LDT/GDT entry
and in the segments cache (env) when loading a segment, like it's set
for data segments in helper_load_seg().
According to SDM (Vol. 3A 3.4.5.1), the accessed bit indicates whether
a segment has been accessed since the last time the OS cleared the bit.
I found this bug while I was trying to add VMX support in QEMU and
one of the checks of vmlaunch is to check guest segment registers
(SDM Vol. 3C 29.3.1.2), where in non virtual-8086 mode, the CS type
must have the accessed bit set. Otherwise VM entry fails.
Add a set_seg_accessed() helper that sets the accessed bit in both the
segment cache and the linear address of the GDT/LDT descriptor, and call
it across the remaining segment load paths: far JMP/CALL, call gates,
RETF/IRET, interrupt and exception delivery, and task-switch segment
loads. Set the bit after descriptor validation and before committing any
architectural register state, since writing to guest descriptor memory
can fault with #PF. helper_load_seg() also calls set_seg_accessed() now
to avoid duplicates.
Signed-off-by: Nick Sampanis <Nicksampanis@gmail.com>
---
target/i386/tcg/seg_helper.c | 32 +++++++++++++++++++++++++-------
1 file changed, 25 insertions(+), 7 deletions(-)
diff --git a/target/i386/tcg/seg_helper.c b/target/i386/tcg/seg_helper.c
index f11b55d136..e960ec0fa8 100644
--- a/target/i386/tcg/seg_helper.c
+++ b/target/i386/tcg/seg_helper.c
@@ -176,6 +176,18 @@ static inline int load_segment(CPUX86State *env, uint32_t *e1_ptr,
return load_segment_ra(env, e1_ptr, e2_ptr, selector, 0);
}
+/* Set the accessed bit of a descriptor loaded into a segment register. */
+static void set_seg_accessed(CPUX86State *env, int selector, uint32_t *e2,
+ uintptr_t retaddr)
+{
+ SegmentCache *dt = selector & 0x4 ? &env->ldt : &env->gdt;
+
+ if (!(*e2 & DESC_A_MASK)) {
+ *e2 |= DESC_A_MASK;
+ cpu_stl_kernel_ra(env, dt->base + (selector & ~7) + 4, *e2, retaddr);
+ }
+}
+
static inline unsigned int get_seg_limit(uint32_t e1, uint32_t e2)
{
unsigned int limit;
@@ -297,6 +309,7 @@ static void tss_load_seg(CPUX86State *env, X86Seg seg_reg, int selector,
if (!(e2 & DESC_P_MASK)) {
raise_exception_err_ra(env, EXCP0B_NOSEG, selector & 0xfffc, retaddr);
}
+ set_seg_accessed(env, selector, &e2, retaddr);
cpu_x86_load_seg_cache(env, seg_reg, selector,
get_seg_base(e1, e2),
get_seg_limit(e1, e2),
@@ -774,6 +787,7 @@ static void do_interrupt_protected(CPUX86State *env, int intno, int is_int,
if (!(e2 & DESC_P_MASK)) {
raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc);
}
+ set_seg_accessed(env, selector, &e2, 0);
if (e2 & DESC_C_MASK) {
dpl = cpl;
}
@@ -803,6 +817,7 @@ static void do_interrupt_protected(CPUX86State *env, int intno, int is_int,
if (!(ss_e2 & DESC_P_MASK)) {
raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
}
+ set_seg_accessed(env, ss, &ss_e2, 0);
new_stack = 1;
sa.sp = esp;
sa.sp_mask = get_sp_mask(ss_e2);
@@ -1023,6 +1038,7 @@ static void do_interrupt64(CPUX86State *env, int intno, int is_int,
if (!(e2 & DESC_L_MASK) || (e2 & DESC_B_MASK)) {
raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
}
+ set_seg_accessed(env, selector, &e2, 0);
if (e2 & DESC_C_MASK) {
dpl = cpl;
}
@@ -1475,13 +1491,7 @@ void HELPER(load_seg)(CPUX86State *env, int seg_reg, int selector)
raise_exception_err_ra(env, EXCP0B_NOSEG, selector & 0xfffc, GETPC());
}
}
-
- /* set the access bit if not already set */
- if (!(e2 & DESC_A_MASK)) {
- e2 |= DESC_A_MASK;
- cpu_stl_kernel_ra(env, ptr + 4, e2, GETPC());
- }
-
+ set_seg_accessed(env, selector, &e2, GETPC());
cpu_x86_load_seg_cache(env, seg_reg, selector,
get_seg_base(e1, e2),
get_seg_limit(e1, e2),
@@ -1535,6 +1545,7 @@ void HELPER(ljmp_protected)(CPUX86State *env, int new_cs, target_ulong new_eip,
(!(env->hflags & HF_LMA_MASK) || !(e2 & DESC_L_MASK))) {
raise_exception_err_ra(env, EXCP0D_GPF, 0, GETPC());
}
+ set_seg_accessed(env, new_cs, &e2, GETPC());
cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,
get_seg_base(e1, e2), limit, e2);
env->eip = new_eip;
@@ -1623,6 +1634,7 @@ void HELPER(ljmp_protected)(CPUX86State *env, int new_cs, target_ulong new_eip,
(!(env->hflags & HF_LMA_MASK) || !(e2 & DESC_L_MASK))) {
raise_exception_err_ra(env, EXCP0D_GPF, 0, GETPC());
}
+ set_seg_accessed(env, gate_cs, &e2, GETPC());
cpu_x86_load_seg_cache(env, R_CS, (gate_cs & 0xfffc) | cpl,
get_seg_base(e1, e2), limit, e2);
env->eip = new_eip;
@@ -1721,6 +1733,7 @@ void HELPER(lcall_protected)(CPUX86State *env, int new_cs, target_ulong new_eip,
sa.ss_base = 0;
pushq(&sa, env->segs[R_CS].selector);
pushq(&sa, next_eip);
+ set_seg_accessed(env, new_cs, &e2, GETPC());
/* from this point, not restartable */
env->regs[R_ESP] = sa.sp;
cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,
@@ -1749,6 +1762,7 @@ void HELPER(lcall_protected)(CPUX86State *env, int new_cs, target_ulong new_eip,
if (new_eip > limit) {
raise_exception_err_ra(env, EXCP0D_GPF, new_cs & 0xfffc, GETPC());
}
+ set_seg_accessed(env, new_cs, &e2, GETPC());
/* from this point, not restartable */
SET_ESP(sa.sp, sa.sp_mask);
cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,
@@ -1841,6 +1855,7 @@ void HELPER(lcall_protected)(CPUX86State *env, int new_cs, target_ulong new_eip,
if (!(e2 & DESC_P_MASK)) {
raise_exception_err_ra(env, EXCP0B_NOSEG, selector & 0xfffc, GETPC());
}
+ set_seg_accessed(env, selector, &e2, GETPC());
if (!(e2 & DESC_C_MASK) && dpl < cpl) {
/* to inner privilege */
@@ -1883,6 +1898,7 @@ void HELPER(lcall_protected)(CPUX86State *env, int new_cs, target_ulong new_eip,
if (!(ss_e2 & DESC_P_MASK)) {
raise_exception_err_ra(env, EXCP0A_TSS, ss & 0xfffc, GETPC());
}
+ set_seg_accessed(env, ss, &ss_e2, GETPC());
sa.sp = sp32;
sa.sp_mask = get_sp_mask(ss_e2);
@@ -2135,6 +2151,7 @@ static inline void helper_ret_protected(CPUX86State *env, int shift,
if (!(e2 & DESC_P_MASK)) {
raise_exception_err_ra(env, EXCP0B_NOSEG, new_cs & 0xfffc, retaddr);
}
+ set_seg_accessed(env, new_cs, &e2, retaddr);
sa.sp += addend;
if (rpl == cpl && (!(env->hflags & HF_CS64_MASK) ||
@@ -2200,6 +2217,7 @@ static inline void helper_ret_protected(CPUX86State *env, int shift,
if (!(ss_e2 & DESC_P_MASK)) {
raise_exception_err_ra(env, EXCP0B_NOSEG, new_ss & 0xfffc, retaddr);
}
+ set_seg_accessed(env, new_ss, &ss_e2, retaddr);
cpu_x86_load_seg_cache(env, R_SS, new_ss,
get_seg_base(ss_e1, ss_e2),
get_seg_limit(ss_e1, ss_e2),
--
2.51.0.windows.2
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 2/3] target/i386/tcg: Keep busy TSS type in TR hidden state
2026-12-04 21:26 [PATCH 0/3] target/i386: seg_helper fixes for segment, TSS, and EXT behavior Nick Sampanis
2026-12-04 21:26 ` [PATCH 1/3] target/i386/tcg: Set accessed bit on CS, SS and TSS segment loads Nick Sampanis
@ 2026-12-04 21:26 ` Nick Sampanis
2026-12-04 21:26 ` [PATCH 3/3] target/i386/tcg: Set EXT bit in error codes during event delivery Nick Sampanis
2 siblings, 0 replies; 4+ messages in thread
From: Nick Sampanis @ 2026-12-04 21:26 UTC (permalink / raw)
To: qemu-devel; +Cc: Paolo Bonzini, Richard Henderson, Nick Sampanis
helper_ltr() loads the TR cache from e2 before setting the busy bit in
the GDT descriptor, so the GDT descriptor gets updated with the busy bit
while the hidden cache keeps an available type. In addition,
switch_tss_ra() explicitly clears DESC_TSS_BUSY_MASK when updating
env->tr.flags, leaving the cache with an available type (1 or 9) across
task switches.
On real hardware, the current task's TSS is busy by definition, so the
hidden TR cache holds a busy TSS type (3 or 11). This mismatch is already
inconsistent within QEMU, because x86_cpu_reset() initializes the TR
type to 11.
I hit this bug trying to implement VMX in QEMU. VM-entry checks require
TR type 3 or 11 (SDM Vol. 3C 29.3.1.2). An invalid type causes VM entry
to fail with exit reason 33.
Update helper_ltr() and switch_tss_ra() to keep the busy bit set in the
hidden TR cache, and update TR type checks in get_ss_esp_from_tss() and
check_io() to accept busy types alongside available types (1, 3, 9, and
11) so state from older QEMU versions with type 9 in TR is still
accepted.
Signed-off-by: Nick Sampanis <Nicksampanis@gmail.com>
---
target/i386/tcg/seg_helper.c | 6 +++---
target/i386/tcg/system/seg_helper.c | 2 +-
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/target/i386/tcg/seg_helper.c b/target/i386/tcg/seg_helper.c
index e960ec0fa8..1a986ce7a9 100644
--- a/target/i386/tcg/seg_helper.c
+++ b/target/i386/tcg/seg_helper.c
@@ -247,7 +247,7 @@ static inline void get_ss_esp_from_tss(CPUX86State *env, uint32_t *ss_ptr,
cpu_abort(CPU(cpu), "invalid tss");
}
type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf;
- if ((type & 7) != 1) {
+ if ((type & 5) != 1) {
cpu_abort(CPU(cpu), "invalid tss type");
}
shift = type >> 3;
@@ -525,7 +525,7 @@ static void switch_tss_ra(CPUX86State *env, int tss_selector,
env->tr.selector = tss_selector;
env->tr.base = tss_base;
env->tr.limit = tss_limit;
- env->tr.flags = e2 & ~DESC_TSS_BUSY_MASK;
+ env->tr.flags = e2 | DESC_TSS_BUSY_MASK;
if ((type & 8) && (env->cr[0] & CR0_PG_MASK)) {
cpu_x86_update_cr3(env, new_cr3);
@@ -1399,6 +1399,7 @@ void HELPER(ltr)(CPUX86State *env, int selector)
if (!(e2 & DESC_P_MASK)) {
raise_exception_err_ra(env, EXCP0B_NOSEG, selector & 0xfffc, GETPC());
}
+ e2 |= DESC_TSS_BUSY_MASK;
#ifdef TARGET_X86_64
if (env->hflags & HF_LMA_MASK) {
uint32_t e3, e4;
@@ -1415,7 +1416,6 @@ void HELPER(ltr)(CPUX86State *env, int selector)
{
load_seg_cache_raw_dt(&env->tr, e1, e2);
}
- e2 |= DESC_TSS_BUSY_MASK;
cpu_stl_kernel_ra(env, ptr + 4, e2, GETPC());
}
env->tr.selector = selector;
diff --git a/target/i386/tcg/system/seg_helper.c b/target/i386/tcg/system/seg_helper.c
index f45906b489..97252a77d8 100644
--- a/target/i386/tcg/system/seg_helper.c
+++ b/target/i386/tcg/system/seg_helper.c
@@ -237,7 +237,7 @@ void HELPER(check_io)(CPUX86State *env, uint32_t addr, uint32_t size)
/* TSS must be a valid 32 bit one */
if (!(env->tr.flags & DESC_P_MASK) ||
- ((env->tr.flags >> DESC_TYPE_SHIFT) & 0xf) != 9 ||
+ (((env->tr.flags >> DESC_TYPE_SHIFT) & 0xf) | 2) != 11 ||
env->tr.limit < 103) {
goto fail;
}
--
2.51.0.windows.2
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 3/3] target/i386/tcg: Set EXT bit in error codes during event delivery
2026-12-04 21:26 [PATCH 0/3] target/i386: seg_helper fixes for segment, TSS, and EXT behavior Nick Sampanis
2026-12-04 21:26 ` [PATCH 1/3] target/i386/tcg: Set accessed bit on CS, SS and TSS segment loads Nick Sampanis
2026-12-04 21:26 ` [PATCH 2/3] target/i386/tcg: Keep busy TSS type in TR hidden state Nick Sampanis
@ 2026-12-04 21:26 ` Nick Sampanis
2 siblings, 0 replies; 4+ messages in thread
From: Nick Sampanis @ 2026-12-04 21:26 UTC (permalink / raw)
To: qemu-devel; +Cc: Paolo Bonzini, Richard Henderson, Nick Sampanis
The EXT flag (bit 0 of an exception error code) marks faults raised
while delivering an event, including hardware interrupts and prior
exceptions, while staying clear for software interrupts such as INT n,
INT3, and INTO (Intel SDM Vol. 3A 7.13 "Error Code").
Currently, QEMU never sets EXT for faults raised during interrupt or
exception delivery. As a result, error codes are inaccurate across IDT
gate checks, target code-segment checks, TSS stack switching (#TS/#SS),
and task-gate transitions (#TS/#NP/#GP).
Vendor rules differ on when EXT applies (AMD64 APM Vol. 2 8.4.1
"Selector-Error Code"). On AMD, EXT is set when the exception source
is external to the processor (is_hw), whereas Intel sets it for any
event except software interrupts, including exceptions (!is_int).
Add an ext argument for switch_tss_ra(), tss_load_seg(),
get_ss_esp_from_tss(), and get_rsp_from_tss().
Signed-off-by: Nick Sampanis <Nicksampanis@gmail.com>
---
target/i386/tcg/seg_helper.c | 176 ++++++++++++++++++++---------------
1 file changed, 102 insertions(+), 74 deletions(-)
diff --git a/target/i386/tcg/seg_helper.c b/target/i386/tcg/seg_helper.c
index 1a986ce7a9..ea1eb827e8 100644
--- a/target/i386/tcg/seg_helper.c
+++ b/target/i386/tcg/seg_helper.c
@@ -224,7 +224,7 @@ static inline void load_seg_vm(CPUX86State *env, int seg, int selector)
static inline void get_ss_esp_from_tss(CPUX86State *env, uint32_t *ss_ptr,
uint32_t *esp_ptr, int dpl,
- uintptr_t retaddr)
+ uintptr_t retaddr, uint8_t ext)
{
X86CPU *cpu = env_archcpu(env);
int type, index, shift;
@@ -253,7 +253,8 @@ static inline void get_ss_esp_from_tss(CPUX86State *env, uint32_t *ss_ptr,
shift = type >> 3;
index = (dpl * 4 + 2) << shift;
if (index + (4 << shift) - 1 > env->tr.limit) {
- raise_exception_err_ra(env, EXCP0A_TSS, env->tr.selector & 0xfffc, retaddr);
+ raise_exception_err_ra(env, EXCP0A_TSS,
+ (env->tr.selector & 0xfffc) | ext, retaddr);
}
if (shift == 0) {
*esp_ptr = cpu_lduw_kernel_ra(env, env->tr.base + index, retaddr);
@@ -265,49 +266,58 @@ static inline void get_ss_esp_from_tss(CPUX86State *env, uint32_t *ss_ptr,
}
static void tss_load_seg(CPUX86State *env, X86Seg seg_reg, int selector,
- int cpl, uintptr_t retaddr)
+ int cpl, uintptr_t retaddr, uint8_t ext)
{
uint32_t e1, e2;
int rpl, dpl;
if ((selector & 0xfffc) != 0) {
if (load_segment_ra(env, &e1, &e2, selector, retaddr) != 0) {
- raise_exception_err_ra(env, EXCP0A_TSS, selector & 0xfffc, retaddr);
+ raise_exception_err_ra(env, EXCP0A_TSS, (selector & 0xfffc) | ext,
+ retaddr);
}
if (!(e2 & DESC_S_MASK)) {
- raise_exception_err_ra(env, EXCP0A_TSS, selector & 0xfffc, retaddr);
+ raise_exception_err_ra(env, EXCP0A_TSS, (selector & 0xfffc) | ext,
+ retaddr);
}
rpl = selector & 3;
dpl = (e2 >> DESC_DPL_SHIFT) & 3;
if (seg_reg == R_CS) {
if (!(e2 & DESC_CS_MASK)) {
- raise_exception_err_ra(env, EXCP0A_TSS, selector & 0xfffc, retaddr);
+ raise_exception_err_ra(env, EXCP0A_TSS,
+ (selector & 0xfffc) | ext, retaddr);
}
if (dpl != rpl) {
- raise_exception_err_ra(env, EXCP0A_TSS, selector & 0xfffc, retaddr);
+ raise_exception_err_ra(env, EXCP0A_TSS,
+ (selector & 0xfffc) | ext, retaddr);
}
} else if (seg_reg == R_SS) {
/* SS must be writable data */
if ((e2 & DESC_CS_MASK) || !(e2 & DESC_W_MASK)) {
- raise_exception_err_ra(env, EXCP0A_TSS, selector & 0xfffc, retaddr);
+ raise_exception_err_ra(env, EXCP0A_TSS,
+ (selector & 0xfffc) | ext, retaddr);
}
if (dpl != cpl || dpl != rpl) {
- raise_exception_err_ra(env, EXCP0A_TSS, selector & 0xfffc, retaddr);
+ raise_exception_err_ra(env, EXCP0A_TSS,
+ (selector & 0xfffc) | ext, retaddr);
}
} else {
/* not readable code */
if ((e2 & DESC_CS_MASK) && !(e2 & DESC_R_MASK)) {
- raise_exception_err_ra(env, EXCP0A_TSS, selector & 0xfffc, retaddr);
+ raise_exception_err_ra(env, EXCP0A_TSS,
+ (selector & 0xfffc) | ext, retaddr);
}
/* if data or non conforming code, checks the rights */
if (((e2 >> DESC_TYPE_SHIFT) & 0xf) < 12) {
if (dpl < cpl || dpl < rpl) {
- raise_exception_err_ra(env, EXCP0A_TSS, selector & 0xfffc, retaddr);
+ raise_exception_err_ra(env, EXCP0A_TSS,
+ (selector & 0xfffc) | ext, retaddr);
}
}
}
if (!(e2 & DESC_P_MASK)) {
- raise_exception_err_ra(env, EXCP0B_NOSEG, selector & 0xfffc, retaddr);
+ raise_exception_err_ra(env, EXCP0B_NOSEG, (selector & 0xfffc) | ext,
+ retaddr);
}
set_seg_accessed(env, selector, &e2, retaddr);
cpu_x86_load_seg_cache(env, seg_reg, selector,
@@ -316,7 +326,8 @@ static void tss_load_seg(CPUX86State *env, X86Seg seg_reg, int selector,
e2);
} else {
if (seg_reg == R_SS || seg_reg == R_CS) {
- raise_exception_err_ra(env, EXCP0A_TSS, selector & 0xfffc, retaddr);
+ raise_exception_err_ra(env, EXCP0A_TSS, (selector & 0xfffc) | ext,
+ retaddr);
}
}
}
@@ -343,7 +354,7 @@ static void tss_set_busy(CPUX86State *env, int tss_selector, bool value,
static void switch_tss_ra(CPUX86State *env, int tss_selector,
uint32_t e1, uint32_t e2, int source,
uint32_t next_eip, bool has_error_code,
- uint32_t error_code, uintptr_t retaddr)
+ uint32_t error_code, uintptr_t retaddr, uint8_t ext)
{
int tss_limit, tss_limit_max, type, old_tss_limit_max, old_type, i;
target_ulong tss_base;
@@ -362,26 +373,32 @@ static void switch_tss_ra(CPUX86State *env, int tss_selector,
/* if task gate, we read the TSS segment and we load it */
if (type == 5) {
if (!(e2 & DESC_P_MASK)) {
- raise_exception_err_ra(env, EXCP0B_NOSEG, tss_selector & 0xfffc, retaddr);
+ raise_exception_err_ra(env, EXCP0B_NOSEG,
+ (tss_selector & 0xfffc) | ext, retaddr);
}
tss_selector = e1 >> 16;
if (tss_selector & 4) {
- raise_exception_err_ra(env, EXCP0A_TSS, tss_selector & 0xfffc, retaddr);
+ raise_exception_err_ra(env, EXCP0A_TSS,
+ (tss_selector & 0xfffc) | ext, retaddr);
}
if (load_segment_ra(env, &e1, &e2, tss_selector, retaddr) != 0) {
- raise_exception_err_ra(env, EXCP0D_GPF, tss_selector & 0xfffc, retaddr);
+ raise_exception_err_ra(env, EXCP0D_GPF,
+ (tss_selector & 0xfffc) | ext, retaddr);
}
if (e2 & DESC_S_MASK) {
- raise_exception_err_ra(env, EXCP0D_GPF, tss_selector & 0xfffc, retaddr);
+ raise_exception_err_ra(env, EXCP0D_GPF,
+ (tss_selector & 0xfffc) | ext, retaddr);
}
type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
if ((type & 7) != 1) {
- raise_exception_err_ra(env, EXCP0D_GPF, tss_selector & 0xfffc, retaddr);
+ raise_exception_err_ra(env, EXCP0D_GPF,
+ (tss_selector & 0xfffc) | ext, retaddr);
}
}
if (!(e2 & DESC_P_MASK)) {
- raise_exception_err_ra(env, EXCP0B_NOSEG, tss_selector & 0xfffc, retaddr);
+ raise_exception_err_ra(env, EXCP0B_NOSEG, (tss_selector & 0xfffc) | ext,
+ retaddr);
}
if (type & 8) {
@@ -393,7 +410,8 @@ static void switch_tss_ra(CPUX86State *env, int tss_selector,
tss_base = get_seg_base(e1, e2);
if ((tss_selector & 4) != 0 ||
tss_limit < tss_limit_max) {
- raise_exception_err_ra(env, EXCP0A_TSS, tss_selector & 0xfffc, retaddr);
+ raise_exception_err_ra(env, EXCP0A_TSS, (tss_selector & 0xfffc) | ext,
+ retaddr);
}
old_type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf;
if (old_type & 8) {
@@ -404,7 +422,8 @@ static void switch_tss_ra(CPUX86State *env, int tss_selector,
/* new TSS must be busy iff the source is an IRET instruction */
if (!!(e2 & DESC_TSS_BUSY_MASK) != (source == SWITCH_TSS_IRET)) {
- raise_exception_err_ra(env, EXCP0A_TSS, tss_selector & 0xfffc, retaddr);
+ raise_exception_err_ra(env, EXCP0A_TSS, (tss_selector & 0xfffc) | ext,
+ retaddr);
}
/* X86Access avoids memory exceptions during the task switch */
@@ -565,23 +584,27 @@ static void switch_tss_ra(CPUX86State *env, int tss_selector,
/* load the LDT */
if (new_ldt & 4) {
- raise_exception_err_ra(env, EXCP0A_TSS, new_ldt & 0xfffc, retaddr);
+ raise_exception_err_ra(env, EXCP0A_TSS, (new_ldt & 0xfffc) | ext,
+ retaddr);
}
if ((new_ldt & 0xfffc) != 0) {
dt = &env->gdt;
index = new_ldt & ~7;
if ((index + 7) > dt->limit) {
- raise_exception_err_ra(env, EXCP0A_TSS, new_ldt & 0xfffc, retaddr);
+ raise_exception_err_ra(env, EXCP0A_TSS, (new_ldt & 0xfffc) | ext,
+ retaddr);
}
ptr = dt->base + index;
e1 = cpu_ldl_kernel_ra(env, ptr, retaddr);
e2 = cpu_ldl_kernel_ra(env, ptr + 4, retaddr);
if ((e2 & DESC_S_MASK) || ((e2 >> DESC_TYPE_SHIFT) & 0xf) != 2) {
- raise_exception_err_ra(env, EXCP0A_TSS, new_ldt & 0xfffc, retaddr);
+ raise_exception_err_ra(env, EXCP0A_TSS, (new_ldt & 0xfffc) | ext,
+ retaddr);
}
if (!(e2 & DESC_P_MASK)) {
- raise_exception_err_ra(env, EXCP0A_TSS, new_ldt & 0xfffc, retaddr);
+ raise_exception_err_ra(env, EXCP0A_TSS, (new_ldt & 0xfffc) | ext,
+ retaddr);
}
load_seg_cache_raw_dt(&env->ldt, e1, e2);
}
@@ -589,18 +612,18 @@ static void switch_tss_ra(CPUX86State *env, int tss_selector,
/* load the segments */
if (!(new_eflags & VM_MASK)) {
int cpl = new_segs[R_CS] & 3;
- tss_load_seg(env, R_CS, new_segs[R_CS], cpl, retaddr);
- tss_load_seg(env, R_SS, new_segs[R_SS], cpl, retaddr);
- tss_load_seg(env, R_ES, new_segs[R_ES], cpl, retaddr);
- tss_load_seg(env, R_DS, new_segs[R_DS], cpl, retaddr);
- tss_load_seg(env, R_FS, new_segs[R_FS], cpl, retaddr);
- tss_load_seg(env, R_GS, new_segs[R_GS], cpl, retaddr);
+ tss_load_seg(env, R_CS, new_segs[R_CS], cpl, retaddr, ext);
+ tss_load_seg(env, R_SS, new_segs[R_SS], cpl, retaddr, ext);
+ tss_load_seg(env, R_ES, new_segs[R_ES], cpl, retaddr, ext);
+ tss_load_seg(env, R_DS, new_segs[R_DS], cpl, retaddr, ext);
+ tss_load_seg(env, R_FS, new_segs[R_FS], cpl, retaddr, ext);
+ tss_load_seg(env, R_GS, new_segs[R_GS], cpl, retaddr, ext);
}
/* check that env->eip is in the CS segment limits */
if (new_eip > env->segs[R_CS].limit) {
/* XXX: different exception if CALL? */
- raise_exception_err_ra(env, EXCP0D_GPF, 0, retaddr);
+ raise_exception_err_ra(env, EXCP0D_GPF, ext, retaddr);
}
#ifndef CONFIG_USER_ONLY
@@ -642,10 +665,10 @@ static void switch_tss_ra(CPUX86State *env, int tss_selector,
static void switch_tss(CPUX86State *env, int tss_selector,
uint32_t e1, uint32_t e2, int source,
uint32_t next_eip, bool has_error_code,
- int error_code)
+ int error_code, uint8_t ext)
{
switch_tss_ra(env, tss_selector, e1, e2, source, next_eip,
- has_error_code, error_code, 0);
+ has_error_code, error_code, 0, ext);
}
static inline unsigned int get_sp_mask(unsigned int e2)
@@ -709,7 +732,9 @@ static void do_interrupt_protected(CPUX86State *env, int intno, int is_int,
int vm86 = env->eflags & VM_MASK;
StackAccess sa;
bool set_rf;
+ uint8_t ext;
+ ext = IS_AMD_CPU(env) ? !!is_hw : !is_int;
has_error_code = 0;
if (!is_int && !is_hw) {
has_error_code = exception_has_error_code(intno);
@@ -724,7 +749,7 @@ static void do_interrupt_protected(CPUX86State *env, int intno, int is_int,
dt = &env->idt;
if (intno * 8 + 7 > dt->limit) {
- raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2);
+ raise_exception_err(env, EXCP0D_GPF, (intno * 8) | 2 | ext);
}
ptr = dt->base + intno * 8;
e1 = cpu_ldl_kernel(env, ptr);
@@ -739,14 +764,14 @@ static void do_interrupt_protected(CPUX86State *env, int intno, int is_int,
case 15: /* 386 trap gate */
break;
default:
- raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2);
+ raise_exception_err(env, EXCP0D_GPF, (intno * 8) | 2 | ext);
break;
}
dpl = (e2 >> DESC_DPL_SHIFT) & 3;
cpl = env->hflags & HF_CPL_MASK;
/* check privilege if software int */
if (is_int && dpl < cpl) {
- raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2);
+ raise_exception_err(env, EXCP0D_GPF, (intno * 8) | 2 | ext);
}
sa.env = env;
@@ -756,10 +781,10 @@ static void do_interrupt_protected(CPUX86State *env, int intno, int is_int,
/* task gate */
/* must do that check here to return the correct error code */
if (!(e2 & DESC_P_MASK)) {
- raise_exception_err(env, EXCP0B_NOSEG, intno * 8 + 2);
+ raise_exception_err(env, EXCP0B_NOSEG, (intno * 8) | 2 | ext);
}
switch_tss(env, intno * 8, e1, e2, SWITCH_TSS_CALL, old_eip,
- has_error_code, error_code);
+ has_error_code, error_code, ext);
return;
}
@@ -767,25 +792,25 @@ static void do_interrupt_protected(CPUX86State *env, int intno, int is_int,
/* check valid bit */
if (!(e2 & DESC_P_MASK)) {
- raise_exception_err(env, EXCP0B_NOSEG, intno * 8 + 2);
+ raise_exception_err(env, EXCP0B_NOSEG, (intno * 8) | 2 | ext);
}
selector = e1 >> 16;
offset = (e2 & 0xffff0000) | (e1 & 0x0000ffff);
if ((selector & 0xfffc) == 0) {
- raise_exception_err(env, EXCP0D_GPF, 0);
+ raise_exception_err(env, EXCP0D_GPF, ext);
}
if (load_segment(env, &e1, &e2, selector) != 0) {
- raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
+ raise_exception_err(env, EXCP0D_GPF, (selector & 0xfffc) | ext);
}
if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK))) {
- raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
+ raise_exception_err(env, EXCP0D_GPF, (selector & 0xfffc) | ext);
}
dpl = (e2 >> DESC_DPL_SHIFT) & 3;
if (dpl > cpl) {
- raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
+ raise_exception_err(env, EXCP0D_GPF, (selector & 0xfffc) | ext);
}
if (!(e2 & DESC_P_MASK)) {
- raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc);
+ raise_exception_err(env, EXCP0B_NOSEG, (selector & 0xfffc) | ext);
}
set_seg_accessed(env, selector, &e2, 0);
if (e2 & DESC_C_MASK) {
@@ -795,27 +820,27 @@ static void do_interrupt_protected(CPUX86State *env, int intno, int is_int,
if (dpl < cpl) {
/* to inner privilege */
uint32_t esp;
- get_ss_esp_from_tss(env, &ss, &esp, dpl, 0);
+ get_ss_esp_from_tss(env, &ss, &esp, dpl, 0, ext);
if ((ss & 0xfffc) == 0) {
- raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
+ raise_exception_err(env, EXCP0A_TSS, (ss & 0xfffc) | ext);
}
if ((ss & 3) != dpl) {
- raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
+ raise_exception_err(env, EXCP0A_TSS, (ss & 0xfffc) | ext);
}
if (load_segment(env, &ss_e1, &ss_e2, ss) != 0) {
- raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
+ raise_exception_err(env, EXCP0A_TSS, (ss & 0xfffc) | ext);
}
ss_dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
if (ss_dpl != dpl) {
- raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
+ raise_exception_err(env, EXCP0A_TSS, (ss & 0xfffc) | ext);
}
if (!(ss_e2 & DESC_S_MASK) ||
(ss_e2 & DESC_CS_MASK) ||
!(ss_e2 & DESC_W_MASK)) {
- raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
+ raise_exception_err(env, EXCP0A_TSS, (ss & 0xfffc) | ext);
}
if (!(ss_e2 & DESC_P_MASK)) {
- raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
+ raise_exception_err(env, EXCP0A_TSS, (ss & 0xfffc) | ext);
}
set_seg_accessed(env, ss, &ss_e2, 0);
new_stack = 1;
@@ -825,7 +850,7 @@ static void do_interrupt_protected(CPUX86State *env, int intno, int is_int,
} else {
/* to same privilege */
if (vm86) {
- raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
+ raise_exception_err(env, EXCP0D_GPF, (selector & 0xfffc) | ext);
}
new_stack = 0;
sa.sp = env->regs[R_ESP];
@@ -930,7 +955,8 @@ static uint64_t popq(StackAccess *sa)
return ret;
}
-static inline target_ulong get_rsp_from_tss(CPUX86State *env, int level)
+static inline target_ulong get_rsp_from_tss(CPUX86State *env, int level,
+ uint8_t ext)
{
X86CPU *cpu = env_archcpu(env);
int index, pg_mode;
@@ -947,7 +973,7 @@ static inline target_ulong get_rsp_from_tss(CPUX86State *env, int level)
}
index = 8 * level + 4;
if ((index + 7) > env->tr.limit) {
- raise_exception_err(env, EXCP0A_TSS, env->tr.selector & 0xfffc);
+ raise_exception_err(env, EXCP0A_TSS, (env->tr.selector & 0xfffc) | ext);
}
rsp = cpu_ldq_kernel(env, env->tr.base + index);
@@ -956,7 +982,7 @@ static inline target_ulong get_rsp_from_tss(CPUX86State *env, int level)
pg_mode = get_pg_mode(env);
sext = (int64_t)rsp >> (pg_mode & PG_MODE_LA57 ? 56 : 47);
if (sext != 0 && sext != -1) {
- raise_exception_err(env, EXCP0C_STACK, 0);
+ raise_exception_err(env, EXCP0C_STACK, ext);
}
return rsp;
@@ -974,7 +1000,9 @@ static void do_interrupt64(CPUX86State *env, int intno, int is_int,
target_ulong old_eip, offset;
bool set_rf;
StackAccess sa;
+ uint8_t ext;
+ ext = IS_AMD_CPU(env) ? !!is_hw : !is_int;
has_error_code = 0;
if (!is_int && !is_hw) {
has_error_code = exception_has_error_code(intno);
@@ -989,7 +1017,7 @@ static void do_interrupt64(CPUX86State *env, int intno, int is_int,
dt = &env->idt;
if (intno * 16 + 15 > dt->limit) {
- raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2);
+ raise_exception_err(env, EXCP0D_GPF, (intno * 8) | 2 | ext);
}
ptr = dt->base + intno * 16;
e1 = cpu_ldl_kernel(env, ptr);
@@ -1002,41 +1030,41 @@ static void do_interrupt64(CPUX86State *env, int intno, int is_int,
case 15: /* 386 trap gate */
break;
default:
- raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2);
+ raise_exception_err(env, EXCP0D_GPF, (intno * 8) | 2 | ext);
break;
}
dpl = (e2 >> DESC_DPL_SHIFT) & 3;
cpl = env->hflags & HF_CPL_MASK;
/* check privilege if software int */
if (is_int && dpl < cpl) {
- raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2);
+ raise_exception_err(env, EXCP0D_GPF, (intno * 8) | 2 | ext);
}
/* check valid bit */
if (!(e2 & DESC_P_MASK)) {
- raise_exception_err(env, EXCP0B_NOSEG, intno * 8 + 2);
+ raise_exception_err(env, EXCP0B_NOSEG, (intno * 8) | 2 | ext);
}
selector = e1 >> 16;
offset = ((target_ulong)e3 << 32) | (e2 & 0xffff0000) | (e1 & 0x0000ffff);
ist = e2 & 7;
if ((selector & 0xfffc) == 0) {
- raise_exception_err(env, EXCP0D_GPF, 0);
+ raise_exception_err(env, EXCP0D_GPF, ext);
}
if (load_segment(env, &e1, &e2, selector) != 0) {
- raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
+ raise_exception_err(env, EXCP0D_GPF, (selector & 0xfffc) | ext);
}
if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK))) {
- raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
+ raise_exception_err(env, EXCP0D_GPF, (selector & 0xfffc) | ext);
}
dpl = (e2 >> DESC_DPL_SHIFT) & 3;
if (dpl > cpl) {
- raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
+ raise_exception_err(env, EXCP0D_GPF, (selector & 0xfffc) | ext);
}
if (!(e2 & DESC_P_MASK)) {
- raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc);
+ raise_exception_err(env, EXCP0B_NOSEG, (selector & 0xfffc) | ext);
}
if (!(e2 & DESC_L_MASK) || (e2 & DESC_B_MASK)) {
- raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
+ raise_exception_err(env, EXCP0D_GPF, (selector & 0xfffc) | ext);
}
set_seg_accessed(env, selector, &e2, 0);
if (e2 & DESC_C_MASK) {
@@ -1051,11 +1079,11 @@ static void do_interrupt64(CPUX86State *env, int intno, int is_int,
if (dpl < cpl || ist != 0) {
/* to inner privilege */
new_stack = 1;
- sa.sp = get_rsp_from_tss(env, ist != 0 ? ist + 3 : dpl);
+ sa.sp = get_rsp_from_tss(env, ist != 0 ? ist + 3 : dpl, ext);
} else {
/* to same privilege */
if (env->eflags & VM_MASK) {
- raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
+ raise_exception_err(env, EXCP0D_GPF, (selector & 0xfffc) | ext);
}
new_stack = 0;
sa.sp = env->regs[R_ESP];
@@ -1571,7 +1599,7 @@ void HELPER(ljmp_protected)(CPUX86State *env, int new_cs, target_ulong new_eip,
raise_exception_err_ra(env, EXCP0D_GPF, new_cs & 0xfffc, GETPC());
}
switch_tss_ra(env, new_cs, e1, e2, SWITCH_TSS_JMP, next_eip,
- false, 0, GETPC());
+ false, 0, GETPC(), 0);
break;
case 4: /* 286 call gate */
case 12: /* 386 call gate */
@@ -1791,7 +1819,7 @@ void HELPER(lcall_protected)(CPUX86State *env, int new_cs, target_ulong new_eip,
raise_exception_err_ra(env, EXCP0D_GPF, new_cs & 0xfffc, GETPC());
}
switch_tss_ra(env, new_cs, e1, e2, SWITCH_TSS_CALL, next_eip,
- false, 0, GETPC());
+ false, 0, GETPC(), 0);
return;
case 4: /* 286 call gate */
case 12: /* 386 call gate */
@@ -1864,7 +1892,7 @@ void HELPER(lcall_protected)(CPUX86State *env, int new_cs, target_ulong new_eip,
if (shift == 2) {
ss = dpl; /* SS = NULL selector with RPL = new CPL */
new_stack = 1;
- sa.sp = get_rsp_from_tss(env, dpl);
+ sa.sp = get_rsp_from_tss(env, dpl, 0);
sa.sp_mask = -1;
sa.ss_base = 0; /* SS base is always zero in IA-32e mode */
LOG_PCALL("new ss:rsp=%04x:%016llx env->regs[R_ESP]="
@@ -1873,7 +1901,7 @@ void HELPER(lcall_protected)(CPUX86State *env, int new_cs, target_ulong new_eip,
#endif
{
uint32_t sp32;
- get_ss_esp_from_tss(env, &ss, &sp32, dpl, GETPC());
+ get_ss_esp_from_tss(env, &ss, &sp32, dpl, GETPC(), 0);
LOG_PCALL("new ss:esp=%04x:%08x param_count=%d env->regs[R_ESP]="
TARGET_FMT_lx "\n", ss, sp32, param_count,
env->regs[R_ESP]);
@@ -2313,7 +2341,7 @@ void HELPER(iret_protected)(CPUX86State *env, int shift, int next_eip)
raise_exception_err_ra(env, EXCP0A_TSS, tss_selector & 0xfffc, GETPC());
}
switch_tss_ra(env, tss_selector, e1, e2, SWITCH_TSS_IRET, next_eip,
- false, 0, GETPC());
+ false, 0, GETPC(), 0);
} else {
helper_ret_protected(env, shift, 1, 0, GETPC());
}
--
2.51.0.windows.2
^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-04 21:27 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 21:26 [PATCH 0/3] target/i386: seg_helper fixes for segment, TSS, and EXT behavior Nick Sampanis
2026-12-04 21:26 ` [PATCH 1/3] target/i386/tcg: Set accessed bit on CS, SS and TSS segment loads Nick Sampanis
2026-12-04 21:26 ` [PATCH 2/3] target/i386/tcg: Keep busy TSS type in TR hidden state Nick Sampanis
2026-12-04 21:26 ` [PATCH 3/3] target/i386/tcg: Set EXT bit in error codes during event delivery Nick Sampanis
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.