From: Nick Sampanis <nicksampanis@gmail.com>
To: qemu-devel@nongnu.org
Cc: Paolo Bonzini <pbonzini@redhat.com>,
Richard Henderson <richard.henderson@linaro.org>,
Nick Sampanis <Nicksampanis@gmail.com>
Subject: [PATCH 2/3] target/i386/tcg: Keep busy TSS type in TR hidden state
Date: Fri, 4 Dec 2026 23:26:39 +0200 [thread overview]
Message-ID: <20261204212641.2770-3-Nicksampanis@gmail.com> (raw)
In-Reply-To: <20261204212641.2770-1-Nicksampanis@gmail.com>
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
next prev parent reply other threads:[~2026-12-04 21:27 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2026-12-04 21:26 ` [PATCH 3/3] target/i386/tcg: Set EXT bit in error codes during event delivery Nick Sampanis
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261204212641.2770-3-Nicksampanis@gmail.com \
--to=nicksampanis@gmail.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.