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 1/3] target/i386/tcg: Set accessed bit on CS, SS and TSS segment loads
Date: Fri, 4 Dec 2026 23:26:38 +0200 [thread overview]
Message-ID: <20261204212641.2770-2-Nicksampanis@gmail.com> (raw)
In-Reply-To: <20261204212641.2770-1-Nicksampanis@gmail.com>
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
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 ` Nick Sampanis [this message]
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
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-2-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.