From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 597BF606A322 for ; Fri, 4 Dec 2026 21:27:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=tN9VcyHs Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xZaoG-00020s-DX; Fri, 04 Dec 2026 16:27:16 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xZant-0001yY-RX for qemu-devel@nongnu.org; Fri, 04 Dec 2026 16:27:00 -0500 Received: from mail-wr1-x429.google.com ([2a00:1450:4864:20::429]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xZanr-0006Qa-6w for qemu-devel@nongnu.org; Fri, 04 Dec 2026 16:26:52 -0500 Received: by mail-wr1-x429.google.com with SMTP id ffacd0b85a97d-4a358881793so746246f8f.0 for ; Fri, 04 Dec 2026 13:26:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796419609; x=1797024409; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=E3HaSAVXllIZvkgNqUfDCLygNbkDoFkfIOI9cjClxhM=; b=tN9VcyHs4u4WSUUWuroF/BG2yz4QS3BQ1RJ6LGdW1IepKxJHwRkinT5eGjdOrRqJv9 hq55PBLapYCKRA9U7UsO6Rd5PF87JRlwCOKA7zSOJV1gFGlRbmRJnSu/khQvn0CPRXxT sz8jCLtPD63klyqTKiGRf+2PftxagXxQVzBeVUzokq9PrNn51CqeBxGmlSACNPfcsbZZ Dk20edCuMKJY7BAVKzDbhYJ+9jV46f+HzAvyE7eHfcR1ttwITbp1PruSuHX67mam8nNv Sgfk4y++feoZGur56hnOzBEuSXo3x4BrvFaOn1znqFgysB8KQ+f7hyM14UVjdFVGQtS7 Oz2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796419609; x=1797024409; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=E3HaSAVXllIZvkgNqUfDCLygNbkDoFkfIOI9cjClxhM=; b=v29Vq3xgMlRzdPb8CXdg9qQV8qIMZk8JEuoa2xAmlKTiwX+6enTx8QTcxZFT3QnhIh LnH26FyLxPLNI2PiSoUZjqG4uzomZayposuTuTqFzfIWv5bPE9AB/+ARRmeMGCtz7zeV db2XBQ/KkLpT5jPaKrzR9T4VgOOVbCiuHf8fwqZC4NONCjxUDek7SN0zdr+30zSeVrZo 9xBkxVbJ55cxKVNfiJS1g+4WjrAUPtcoVdC17Xgr+vs7VCtuc2HBSJ36FrDAgD7HYSzp xBxuvBWlKbAfhSiReMb4JbI5tWdlURg0xqzdaJA778DQaKkvNMp5PSw8iCGvSZJcLjiX Eh2w== X-Gm-Message-State: AB4q+uEP3oBsN7Y1HeOExIgWw0ISywH606IUEAoR5TVYTaexgQQQrs1p MMVKU7pl8O8dy6Wz6/j66yDPs/mDr45zk+prp09e9OSQWqwS7zmi7Veb1/OWFHhLKbw= X-Gm-Gg: AZFdTZeNIzI910SF1j3nZBSnB6WG+rIeR+0oDsOxJ+B0Jv1gYLP3TIm4FUqdPly1fME ShomK1ATa9wg5ysc+UMqbqOi5TRZyvBlvlOgETVRCwVTTksAQFL3dv+PAOsJ0hm3hDnbHAmoj3I 1fBJqL9ik8/Hsq6kxSBldLFOHqVNpGGIeXE2i5DHECKnvvxnfhsSFiuDJUcU4dArVO0SDcwsO/f QB2Bo7RlZ61fWUZSQYmM5ZP+NN1uxHF1yQOQdGf+IxafDjBXS3ZdMTXU/25bhl3uAH0OKxerl+/ ZUut3X4CWAsunrpR/4UIAMaI6luG1zZwdv2iKdQ3dRgkQtuICLviZUSoCshAnTc+L1cqTE+Eb2X +y5xusrOOM0/kq8QM/aHps0nUHwIl0eEqJuxbHIYx8nHziCbKxzDwYt5wrWeozr2Tw6+6ExfIMT sexxbwt+k7MtS9fTA1wy80OzrICbYFhar0HlhjQZnBoV+7s2fdHRC5BBtcH/TH4ooAnqSt6Sb5/ HJ4HVW/eDgDw9Nqxx837gqWJQOundDdD7Xybhs5LrJcw8O9xRX4hHH87ic9QrcUHneVw3F4Hl4K WS+oLYlhonN/EI77aLVD4j6DMDU7wi1F/gT7d4VPObxJ3j7RzrvcRW/ucsuWSShQfhLd3ghR+rs a8Cre/YInrgpR/HhEG1q5rUtoXg== X-Received: by 2002:a5d:5888:0:b0:4cf:4a06:5d26 with SMTP id ffacd0b85a97d-4d1ec91ac6fmr6320564f8f.51.1796419608525; Fri, 04 Dec 2026 13:26:48 -0800 (PST) Received: from DESKTOP-ECOGLG7 (ppp079166142084.access.hol.gr. [79.166.142.84]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4d1723b8ab8sm7469726f8f.20.2026.12.04.13.26.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 13:26:48 -0800 (PST) From: Nick Sampanis X-Google-Original-From: Nick Sampanis To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Richard Henderson , Nick Sampanis 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 Message-ID: <20261204212641.2770-2-Nicksampanis@gmail.com> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20261204212641.2770-1-Nicksampanis@gmail.com> References: <20261204212641.2770-1-Nicksampanis@gmail.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2a00:1450:4864:20::429; envelope-from=nicksampanis@gmail.com; helo=mail-wr1-x429.google.com X-Spam_score_int: -10 X-Spam_score: -1.1 X-Spam_bar: - X-Spam_report: (-1.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FORGED_GMAIL_RCVD=1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org X-Spam-Status: No, score=-15.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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 --- 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