From nobody Mon Nov 23 05:10:05 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1790467034; cv=none; d=zohomail.com; s=zohoarc; b=kxclp++ck880TG3RKTP4Y8I3xIjd1wcBDZayQ0vfkyiitP93Xh1DQGWhLN6bB8HaShSrza9K2sVzx/hqL/yxhWfEqJmvzSxHGF/KJ+mPPM6UyvhDZde2PWgGVhM+3tJWPZp+Dy0toxGWtiNkE+cezJNbNQnr0ilqBFE74A5TI+c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1790467034; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=bzXbNAmr51YidYXbmZg8Y0lX6KNq+4GQ5EKwzzDhIKw=; b=WmJKilLmS+dMQJB87vV8iBPi+3a8DVAjje45kVk+E7Kt/lxYCHWrjRGcrOsEI/98QHLtAHZzCCuRLVKT+VvZnrBx5eren2Q81LLvny5G3Wug4VbeyU8MfkmOe05AmPW9O/oMBlCECBIYayS2X8Pw7iJ49xefWevbeQdBxYYMR7E= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1790467034028996.8078983106865; Sat, 26 Sep 2026 16:57:14 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xAcFs-0006Vc-Gf; Sat, 26 Sep 2026 19:56:32 -0400 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 1xAcFq-0006Uv-GC for qemu-devel@nongnu.org; Sat, 26 Sep 2026 19:56:30 -0400 Received: from mail-pj2-x0d.google.com ([2607:f8b0:4864:39::d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xAcFm-0004O9-Pl for qemu-devel@nongnu.org; Sat, 26 Sep 2026 19:56:29 -0400 Received: by mail-pj2-x0d.google.com with SMTP id 98e67ed59e1d1-3a0abca97c0so1036586a91.1 for ; Sat, 26 Sep 2026 16:56:26 -0700 (PDT) Received: from stoup (174-21-68-150.tukw.qwest.net. [174.21.68.150]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3a09773e7c3sm18136031a91.17.2026.09.26.16.56.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 26 Sep 2026 16:56:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1790466985; x=1791071785; 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=bzXbNAmr51YidYXbmZg8Y0lX6KNq+4GQ5EKwzzDhIKw=; b=D+N+ZoyJarPmZqrvxZusthbIQhaXttRSk63DD25u2sstvl6btYZtj6oWLC15ggjpaR 2OtO4kGZZcPT8b8rNprIa4aku87UmAPljj1DkAeQhXD4Jl4bmyUkVtsc4emkRzKvodiT jeeI4Dmn4f0qYMiKzYyihWcRuQah0XCkoY49Udoj9X8Eyd3m/2K53GL4Y1fpfBpmRxv/ Q/yrvNADnU4GkS/oFBnLKke7WCQ7/iPbltOBzNF+oHMyYM8GKCl9PFLINNjv9vwZltEv QzMnCD1ggc4UdYtehGbTFmjpa85djfsylF6rcME0KJMyAXfkGiw/BAvLfHyzpX3edSeI utJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790466985; x=1791071785; 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=bzXbNAmr51YidYXbmZg8Y0lX6KNq+4GQ5EKwzzDhIKw=; b=eBpFupy86WykIHQlRXe4E8moTV7+Sm4ybpJZYpa18nl781/l2R0UMDcgGkyBxyjAag KwKsQi836zliHXqPAxrAkKCkpoGSYv/oe8wT1dSnrMUa/UX32xVo7fHd/+BbH/ugQCX4 Jxj02dr2AxY5f8lAj5hCmtiCVj2mbZBXR/Pb0RyvF+RSNm0KwbQUP/dwZGh0+PtPJGF/ lr76RR7sugXlgkd35MQcEfAdwFHAGAJLFM31/DuBhIZshS2EEO44mVJlTEPNAbz//K7d 1R8iw+AqK8nM4poTuqVsExAa5fHcGU0/xrcE7fhrD/NYCIlLAwuOGqE/eHxuDbb8oxOL 6NqQ== X-Gm-Message-State: AFq9FYLDunouggXtmJiX2bVC4VuUHee5HBT8m0HmGu9oW4d09g8GqU5B Tzx+1q5AHSQMX107iPhDNvml3wIOBjD2LlYFUGNzs+LxEShdQ8yHoSqGOpNB5CJF1jaDP6Re8HY x1LQY X-Gm-Gg: AYBFou0VYPPCXnH7cPsdQzhNY+QXzOUuTsThzIX8alXURTU1cK6hdZhKlgHFd/4s2y0 UN0EDQW7QvfcE2dNwcO5S/mdIEPnOC/bRxSf3xvDYB5kAIWEseqCFUoru3mm9xUVKm6FxexgZRU x4YgSvXe7IVtckncuNJDkbTQjrfuNGktYFo+xbwgvXgDUIKz2Gzirr0dVHkTrTvJwryJMtP7TiM zN7h19WO6vmTvsmBr0VIw36pXQV2swvFpUtqypRSFrB1kgj9duMXZuykvvUdl+kVIyNtXD0T/o4 IidV/QNqEkcIyGFYb2dGJ5s/wT8dO1aj7LSgbTYPPEguujHdgHEoLrMTLJphLMXc5GGZ7gb6jSY rXvFSpFz7q3pLMSsCn0ivrb0ajq8UZmwlt9lE5ZhFZKVyfgPfn5jD/eepUTGZP9E57j6eygfIl4 cCTeko9OWJmaJspTh/Pb7rimAltPBAF892tr6uI60XT/B2xs68b7z7R7hfA5jmlAUyoBK8LV9q3 fkPXkBCmAOZwtyz3tJP2isRMt5RAQ== X-Received: by 2002:a17:90b:57c6:b0:3a0:22bd:6ef6 with SMTP id 98e67ed59e1d1-3a09853a6cdmr6761865a91.2.1790466985168; Sat, 26 Sep 2026 16:56:25 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: paul@galbraiths.ca, pbonzini@redhat.com, qemu-stable@nongnu.org Subject: [PATCH v4 1/3] target/i386/tcg: Add DisasContext.pc_save_valid Date: Sat, 26 Sep 2026 16:56:18 -0700 Message-ID: <20260926235620.851099-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260926235620.851099-1-richard.henderson@linaro.org> References: <20260926235620.851099-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:39::d; envelope-from=richard.henderson@linaro.org; helo=mail-pj2-x0d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1790467035421158500 Content-Type: text/plain; charset="utf-8" Use a separate bool rather than overloading -1, which is a perfectly valid value for pc. Cc: qemu-stable@nongnu.org Fixes: e3a79e0e878 ("target/i386: Enable TARGET_TB_PCREL") Signed-off-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- target/i386/tcg/translate.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/target/i386/tcg/translate.c b/target/i386/tcg/translate.c index 192cf43215e..a38cc2c15d0 100644 --- a/target/i386/tcg/translate.c +++ b/target/i386/tcg/translate.c @@ -95,6 +95,7 @@ typedef struct DisasContext { int8_t override; /* -1 if no override, else R_CS, R_DS, etc */ uint8_t prefix; =20 + bool pc_save_valid; bool has_modrm; uint8_t modrm; =20 @@ -471,7 +472,7 @@ static void gen_add_A0_im(DisasContext *s, int val) static inline void gen_op_jmp_v(DisasContext *s, TCGv dest) { tcg_gen_mov_tl(cpu_eip, dest); - s->pc_save =3D -1; + s->pc_save_valid =3D false; } =20 static inline void gen_op_add_reg(DisasContext *s, MemOp size, int reg, TC= Gv val) @@ -506,7 +507,7 @@ static inline void gen_op_st_v(DisasContext *s, int idx= , TCGv t0, TCGv a0) =20 static void gen_update_eip_next(DisasContext *s) { - assert(s->pc_save !=3D -1); + assert(s->pc_save_valid); if (tb_cflags(s->base.tb) & CF_PCREL) { tcg_gen_addi_tl(cpu_eip, cpu_eip, s->pc - s->pc_save); } else if (CODE64(s)) { @@ -519,7 +520,7 @@ static void gen_update_eip_next(DisasContext *s) =20 static void gen_update_eip_cur(DisasContext *s) { - assert(s->pc_save !=3D -1); + assert(s->pc_save_valid); if (tb_cflags(s->base.tb) & CF_PCREL) { tcg_gen_addi_tl(cpu_eip, cpu_eip, s->base.pc_next - s->pc_save); } else if (CODE64(s)) { @@ -542,7 +543,7 @@ static TCGv_i32 cur_insn_len_i32(DisasContext *s) =20 static TCGv_i32 eip_next_i32(DisasContext *s) { - assert(s->pc_save !=3D -1); + assert(s->pc_save_valid); /* * This function has two users: lcall_real (always 16-bit mode), and * iret_protected (16, 32, or 64-bit mode). IRET only uses the value @@ -566,7 +567,7 @@ static TCGv_i32 eip_next_i32(DisasContext *s) =20 static TCGv eip_next_tl(DisasContext *s) { - assert(s->pc_save !=3D -1); + assert(s->pc_save_valid); if (tb_cflags(s->base.tb) & CF_PCREL) { TCGv ret =3D tcg_temp_new(); tcg_gen_addi_tl(ret, cpu_eip, s->pc - s->pc_save); @@ -580,7 +581,7 @@ static TCGv eip_next_tl(DisasContext *s) =20 static TCGv eip_cur_tl(DisasContext *s) { - assert(s->pc_save !=3D -1); + assert(s->pc_save_valid); if (tb_cflags(s->base.tb) & CF_PCREL) { TCGv ret =3D tcg_temp_new(); tcg_gen_addi_tl(ret, cpu_eip, s->base.pc_next - s->pc_save); @@ -3433,6 +3434,7 @@ static void i386_tr_init_disas_context(DisasContextBa= se *dcbase, CPUState *cpu) =20 dc->cs_base =3D dc->base.tb->cs_base; dc->pc_save =3D dc->base.pc_next; + dc->pc_save_valid =3D true; dc->flags =3D flags; #ifndef CONFIG_USER_ONLY dc->cpl =3D cpl; --=20 2.53.0 From nobody Mon Nov 23 05:10:05 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1790467030; cv=none; d=zohomail.com; s=zohoarc; b=RElvwP7n7BV6fXBxmVe5RwDLn1GvONwILcx6uDnqrLpGplBE4yqwLeI7HCyEJknVCFYHGxsZA7iGW/qhZieB7tqpBFS2T9at7BYEl2hq914Cy6/3Zz8tZRvFCCayr7mFjwyMIZI/pf5qbBuIMH/DaQZz6Vsr/hMCPrugxHC9BAM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1790467030; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=0gz45yMIIVliy3SWUWlZVaKuMixUM+jElZ8dLueySp0=; b=bAKK+yhECIO5BbGFSbiKDMslHRfkjE6VbDUdbakWdtddX0mel0VxvhbugOEd5P9wmRscjWiAk9wr3h70VeMoX1qP9EVxRaeRS8Q8UauuKHnBjYWWwi02o1pLM9uKMzx14qZyAKKc/3dtAEZwtriQQaMUcvXUvggGHLqUxRjSiTs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1790467030239761.843138691513; Sat, 26 Sep 2026 16:57:10 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xAcG0-0006Wm-GS; Sat, 26 Sep 2026 19:56:46 -0400 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 1xAcFs-0006Vd-FW for qemu-devel@nongnu.org; Sat, 26 Sep 2026 19:56:32 -0400 Received: from mail-pj2-x0f.google.com ([2607:f8b0:4864:39::f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xAcFp-0004OL-75 for qemu-devel@nongnu.org; Sat, 26 Sep 2026 19:56:30 -0400 Received: by mail-pj2-x0f.google.com with SMTP id 98e67ed59e1d1-396ccb1a98dso1179544a91.0 for ; Sat, 26 Sep 2026 16:56:28 -0700 (PDT) Received: from stoup (174-21-68-150.tukw.qwest.net. [174.21.68.150]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3a09773e7c3sm18136031a91.17.2026.09.26.16.56.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 26 Sep 2026 16:56:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1790466987; x=1791071787; 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=0gz45yMIIVliy3SWUWlZVaKuMixUM+jElZ8dLueySp0=; b=EYdm+biwFbhsxClvL5NauDoTJUPei8OnDnWtuu97hefjmTgaDEvzJJRPc7HRNtwCxF CfpO6xXtmUTaxhJasDz5iGOXyjUWUwuz7n76Q6VZQOKlW1UgTrtm20VyzlgR5scTid1C a9s2ejtTvGmz83AmnRjdz5t5Td+SPnYoVp+JtN37XBRecyRYxFi3iOEgiOWz+Tg/6VDl zP7m5NlbBxuNCl0PL2F+yb2lZDbwEYcD12aBSUHOvpLr1peIXcwCYdzRTxoM95ucc9gz 0U5OHkfp04Gl7JPYq9XMDfGm0+j7mmZet6JvZrntvrsbgBNB/gCYEcId0loo5fnjvP4X O6ew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790466987; x=1791071787; 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=0gz45yMIIVliy3SWUWlZVaKuMixUM+jElZ8dLueySp0=; b=l3EL6mAC0/veZCsR5r5F8lofpcr+cdvIoyR5bf6qLRBjZ2qC440f1zXhjJPipr3HEa u+e6XH0UiUdAVtovqcGYX3Jv5f/JeP8WnIySJAKYZpVJdCZJX9Gt8Dxyx5QGJzg1oQw0 vUk503hBIskOU+O8e43TIydA4bOt6c4/qU91tOMFOX1htdN5tTjcLxKpLDj7PZ84evor kWCLHINnIP7Hxb/zTbth4MT9vsrX9QWKTRQYhjerSLUHRWH6rNKy/uAWC5BAd61dxGJj ZUeM6blpIwbSOTkICY1emU1yqltQAmhTpfgz/cIIzuxzxMZCus8hSRcxHg+yYmaSgHIb maMA== X-Gm-Message-State: AFq9FYJycm4mdve7b0P9yteL5L/675fkYqYRINT4n8bthkv2tzhHy+yu gitL1k/DczRco00TJt0JW86fDOPXRCzA+odNZ0dXpiuHBbjFLigq91jJy8a5QYWKeyCOLNuIUWA +XgGO X-Gm-Gg: AYBFou3RfojEZLunCCfPDHBj9mMJ6V4fFM635PupCJUhrAMV6kFcrVBXBZEufSHbf52 2xK+vuL47BuXAAXE5EYwe6RgHtLyX3XJe9Zhe0K5fU8dAO4IDtdTnKQJpjep8uAyQMZW8CX/eK3 vAkYiMvwb27W+GiW6vqC1Gqs6qzl4wantb3HGnkYijSQnzXf8amQZLPBP0uTDk0m68SV1RyAtI2 IT0i3lPLJdHPp3E2tG81llYYVMM4n9FEMEHKLdnWlahXOjRmMOltholwY7ljnZvwl09ZbKMSiz4 h4rWeCKXgDfzU8CpzrZfpQyKHsC5jrIDztLqIOB0S2BE7g4WbyBt491G0ucMULovsObyHJhndwn Z0QMsSP21wQl60hYo0R9KPmDt2syhVZhgzkFMIfPjFwKzsJYZf/LtbOFfeHPsilJV6QUfbHYi9U cYSGcgEPsxWWW0SJrSC9FX5VnDuXXvet8beZLoSCpQXozMV6dS/vbeDr3Gs3f2Mn8CYkr+jlcne J86Qgrqil9nHv2MCSA3dbuA/1MrlQ== X-Received: by 2002:a17:90b:49:b0:3a0:9ed5:5420 with SMTP id 98e67ed59e1d1-3a0bb6139bdmr4296117a91.44.1790466987279; Sat, 26 Sep 2026 16:56:27 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: paul@galbraiths.ca, pbonzini@redhat.com, qemu-stable@nongnu.org Subject: [PATCH v4 2/3] target/i386/tcg: fix EIP truncation for wrapping near branches Date: Sat, 26 Sep 2026 16:56:19 -0700 Message-ID: <20260926235620.851099-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260926235620.851099-1-richard.henderson@linaro.org> References: <20260926235620.851099-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:39::f; envelope-from=richard.henderson@linaro.org; helo=mail-pj2-x0f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1790467031668158500 Content-Type: text/plain; charset="utf-8" A 16-bit near branch that wraps through 0xFFFF must truncate EIP. TCG skipped this when the target was on the same page, which can happen when the CS base is not page aligned. There was an existing test for this further up that attempted to test this, but was restricted to a 16-bit branch in 32-bit mode, which can be subsumed. The previous test for pc being on the same page, rather than eip, is only relevant to using goto_tb not to eip wrapping. There is another test for pc on the same page further down, via translator_use_goto_tb. Cc: qemu-stable@nongnu.org Fixes: e3a79e0e878 ("target/i386: Enable TARGET_TB_PCREL") Reported-by: Paul Galbraith Signed-off-by: Richard Henderson --- target/i386/tcg/translate.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/target/i386/tcg/translate.c b/target/i386/tcg/translate.c index a38cc2c15d0..18706ec8a74 100644 --- a/target/i386/tcg/translate.c +++ b/target/i386/tcg/translate.c @@ -2015,9 +2015,6 @@ static void gen_jmp_rel(DisasContext *s, MemOp ot, in= t diff, int tb_num) if (!CODE64(s)) { if (ot =3D=3D MO_16) { mask =3D 0xffff; - if (tb_cflags(s->base.tb) & CF_PCREL && CODE32(s)) { - use_goto_tb =3D false; - } } else { mask =3D 0xffffffff; } @@ -2025,15 +2022,20 @@ static void gen_jmp_rel(DisasContext *s, MemOp ot, = int diff, int tb_num) new_eip &=3D mask; =20 if (tb_cflags(s->base.tb) & CF_PCREL) { + assert(s->pc_save_valid); tcg_gen_addi_tl(cpu_eip, cpu_eip, new_pc - s->pc_save); - /* - * If we can prove the branch does not leave the page and we have - * no extra masking to apply (data16 branch in code32, see above), - * then we have also proven that the addition does not wrap. - */ - if (!use_goto_tb || !translator_is_same_page(&s->base, new_pc)) { - tcg_gen_andi_tl(cpu_eip, cpu_eip, mask); - use_goto_tb =3D false; + + if (mask !=3D -1) { + /* + * If eip "page" doesn't change, then the addition cannot + * leave [0, mask] on any rerun of this TB, which only + * ever shifts EIP by whole pages. + */ + target_ulong old_eip =3D s->pc_save - s->cs_base; + if ((old_eip ^ new_eip) & TARGET_PAGE_MASK) { + tcg_gen_andi_tl(cpu_eip, cpu_eip, mask); + use_goto_tb =3D false; + } } } else if (!CODE64(s)) { new_pc =3D (uint32_t)(new_eip + s->cs_base); --=20 2.53.0 From nobody Mon Nov 23 05:10:05 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1790467037; cv=none; d=zohomail.com; s=zohoarc; b=dONsP4YtLwO/JjFGDXKUsAt5WaWI92LS0hEJ0M5X9k1JHPBRImJ/4i3NhdiGD2K/HhsVTq41RrK4dI3W4axGhSQzdwib6tcYmoJ/44+Qlh5OyR8aXuQiUxIC6DDAN8sTPxdCkSsKx44ieBnfPiQUNdbJGb3QqxegStfijcaUvd4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1790467037; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=lG+J3suELYt96W3OkQY+H0iATWW74eO2wnH4cNj4aBM=; b=CgCntLEBlYxdO4fTejbspDhzkoWLkcibykYWjheUN1VAvRNupUFdLDGxuk4h0GJFHiJtwv3elOIQbDouhec5vMq0CbAqEK2zLMb6G6edrBoPB56Mpu1CsqOhOzap3iAYzr1eINRxx81eDuCMA9VCqeJLLau98xPWlA3qekDEwzo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1790467037137328.9989739355142; Sat, 26 Sep 2026 16:57:17 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xAcGE-0006YH-7K; Sat, 26 Sep 2026 19:56:54 -0400 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 1xAcFu-0006WE-Dl for qemu-devel@nongnu.org; Sat, 26 Sep 2026 19:56:34 -0400 Received: from mail-pz2-x10.google.com ([2607:f8b0:4864:3b::10]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xAcFr-0004Ok-1e for qemu-devel@nongnu.org; Sat, 26 Sep 2026 19:56:33 -0400 Received: by mail-pz2-x10.google.com with SMTP id 41be03b00d2f7-cc1cea34f01so856231a12.1 for ; Sat, 26 Sep 2026 16:56:30 -0700 (PDT) Received: from stoup (174-21-68-150.tukw.qwest.net. [174.21.68.150]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3a09773e7c3sm18136031a91.17.2026.09.26.16.56.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 26 Sep 2026 16:56:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1790466989; x=1791071789; 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=lG+J3suELYt96W3OkQY+H0iATWW74eO2wnH4cNj4aBM=; b=M8pZ5iEC10qeX2mb5zUwmOVC13KK3H81VOUhANxsJdcSzCxL6+EOYBbo9/6wIX+yXX v0+rCsPZhHbDPz8VJ7m+cmf7EKvPOWqGe+9mTUEGA/q6Z5NcvvhKsg4UdlV3AKM/F/VY SydneaTSSPCWjx5s+1dgBonM/lIpB37HJ3WJ+nWmieF5MEIn3L7LJuxCztJKZJZ7xEAt QhjqVzDpnBy1nDCUf9/1wpHb8yzuD0LFOm4sA/sindgFH8ik9t3yeJJjyKQmWYM5y2O0 X5eu94nHpxVh+3jL/kzQTTmmZpZZadBUxgYyv8BrLVq20ExBrDW/hx626JvyKP1/bww2 ROoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790466989; x=1791071789; 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=lG+J3suELYt96W3OkQY+H0iATWW74eO2wnH4cNj4aBM=; b=cypDt4tliSYQXlpMkhrs3/8iewGhAkgMPVX9wmDtbLKoFP7dmY+q8A11bOGzBzn1hL NIoIn5ccMkg6GRz/jwRFSbXVuUj1wkHoNfdZGqdwitbWEWCcH4XDPsZ4hLb8PqAg3JcN FbhvuBRKYI4UJdRY7SgROFS6oIwWSgfbWIX1MmCJh84MKzwdVm6PFifyDq6Y/I6TDtIv YmPCTB8gq+09PtNWIQPBVPxc3uOcmNgrSLH7EJKmTZOpuUoVYDVAh0VGISeu6Wle+Ur+ av0qipEo+KM4I6dYznLPy7qQqjtvMl1Ln8tkvxXPUhlSX0HxRX7jFfC2s5uTi/uJDttv e8ZQ== X-Gm-Message-State: AFq9FYLlqV9DWRNRt4a6Xs+OkAWbRWMgNUM/W542wChT4+Gj3fRb6n6E dhdnZ86jWJbi2ytoe+vOkgOlgQp+RRe+9eQE5j/+pfyMr4tPVJe/EjlW5G7w37dYL0WoqQ2ZeR/ ivzNE X-Gm-Gg: AYBFou3G6tmBcjXtKSySYRhHpu/49vFBm8rIfeFTeoETe3Bs9chIhtGj7H1ZPJKMn7z Iwf1+rwucw9BmlYVffhddjTxhmJcM1dbAbTHQvnx3j10+LSxtc0vQWcy8mrk8W3zuvS3EP+l86o UdRA+D0/AowZHC++fzZcrGOvsc5A5rM3ODOSs6sHoE1qNr4xxDkCjrlUTuvYRY0L9LewZ/ZxWRQ Y/7Vd9UIOkwMiOjdMdW9BJjetgyYDIjrK5ysGQFc+ZZOMZ42/POWUkgeP48G8Sk5rlxz98O65Pl Z1h6bgCnIYbY3gjF31v8sAz6d0mo2TA0LPXhLx3TXfNcv6KKQocph+a9xXJpnqzSHYGTR/2FRzn IEBbNdefXxVS5wmklx+0cAp+2i0RMxC6e4ZzAiIOpcO77zgkZ8F1TqMK7z01HP8jimIgxDJYTPd 2SGP/6/C4lxkTZTOV822UEmwnNvtmE9MQzBkO5hM0KY2EtisqcqUX43dd1lpq66RWipCIrEdV1a LKuHvhdHOvMfqN9hQ4nU9so7c5RkFc= X-Received: by 2002:a17:90a:bc9:b0:3a0:a515:c47d with SMTP id 98e67ed59e1d1-3a0a515c8f3mr4882131a91.34.1790466989090; Sat, 26 Sep 2026 16:56:29 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: paul@galbraiths.ca, pbonzini@redhat.com Subject: [PATCH v4 3/3] tests/tcg/i386: Add regression test for wrapping 16-bit near branches Date: Sat, 26 Sep 2026 16:56:20 -0700 Message-ID: <20260926235620.851099-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260926235620.851099-1-richard.henderson@linaro.org> References: <20260926235620.851099-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:3b::10; envelope-from=richard.henderson@linaro.org; helo=mail-pz2-x10.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.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, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1790467037466158500 Content-Type: text/plain; charset="utf-8" From: Paul Galbraith Jump from IP 0xFFF0 to IP 0x0020 with a CS base that is not page aligned, and check that EIP wraps. Signed-off-by: Paul Galbraith Message-ID: <20260926032336.933-3-paul@galbraiths.ca> [rth: Simplfy slightly with ljmpl to pass/fail. Put 16-bit code in its own section with labels for ease of disassembly via "objdump -d -M i8086 -j .text16"] Signed-off-by: Richard Henderson --- tests/tcg/i386/system/meson.build | 3 +- tests/tcg/i386/system/test-branch-wrap16.S | 101 +++++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 tests/tcg/i386/system/test-branch-wrap16.S diff --git a/tests/tcg/i386/system/meson.build b/tests/tcg/i386/system/meso= n.build index 91c6e887191..a6278da3955 100644 --- a/tests/tcg/i386/system/meson.build +++ b/tests/tcg/i386/system/meson.build @@ -35,7 +35,8 @@ foreach t: tcg_tests['multiarch-softmmu']['tests'] endforeach =20 tests +=3D { - 'wraparound.S': { 'cflags': cflags, 'qemu_args': ['-m', '4G'] + qemu_def= _args } + 'test-branch-wrap16.S': { 'cflags': cflags, 'qemu_args': qemu_def_args }, + 'wraparound.S': { 'cflags': cflags, 'qemu_args': ['-m', '4G'] + qemu_def= _args }, } =20 if 'qemu-system-i386' in emulators diff --git a/tests/tcg/i386/system/test-branch-wrap16.S b/tests/tcg/i386/sy= stem/test-branch-wrap16.S new file mode 100644 index 00000000000..9a64b4c6d04 --- /dev/null +++ b/tests/tcg/i386/system/test-branch-wrap16.S @@ -0,0 +1,101 @@ +/* + * SPDX-License-Identifier: GPL-2.0-or-later + * + * A 16-bit near branch must truncate EIP to 16 bits. + * + * The test jumps from IP 0xFFF0 to IP 0x0020 in 16-bit code. The jump + * crosses 0xFFFF. For correct 16-bit behaviour the CPU should wrap the + * target to 0x0020, rather than branching to 0x10020. + * + * TCG used to skip the wrap when the untruncated target (CS base + + * 0x10020) was on the same guest page as the jump. That only happens + * when the CS base is not page aligned, so the code segment starts at + * a page boundary plus 0x10. + */ + +#define SEL_CODE32 0x08 +#define SEL_CODE16 0x18 + + .data + +/* 0x08 and 0x10 must match boot.S, which loaded the data selectors */ + .balign 8 +gdt16: + .quad 0 + .word 0xFFFF, 0 + .byte 0, 0x9b, 0xCF, 0 + .word 0xFFFF, 0 + .byte 0, 0x93, 0xCF, 0 +gdt16_code16: + /* 16-bit code, 64 KiB limit; main fills in the base */ + .word 0xFFFF, 0 + .byte 0, 0x9b, 0x00, 0 +gdt16_end: + +gdtr16: + .word gdt16_end - gdt16 - 1 + .long gdt16 + +fail_msg: + .asciz "FAIL: branch left EIP untruncated\n" +pass_msg: + .asciz "PASS\n" + + .section .text16, "ax" + .code16 + + /* + * Page aligned plus 0x10. A larger alignment breaks the + * multiboot load in boot.S. + */ + .balign 0x1000 + .skip 0x10 +wrap16_cs_base: + + .skip 0x10 +wrap16_target: + /* jmp lands here when EIP wraps to 0x0020 */ + ljmpl $SEL_CODE32, $test_passed + + .org wrap16_cs_base + 0xfff0 +wrap16_entry: + /* + * The problematic branch, which should wrap EIP: + * 16-bit relative 'jmp target', hand-assembled because clang reje= cts a + * rel16 displacement outside [-0x8000, 0x7FFF]. But, in 16-bit co= de, any + * 16-bit displacement is valid. GAS correctly accepts any 16-bit = offset. + */ + .byte 0xe9 /* jmp rel16 opcode */ + .word wrap16_target - (. + 2) /* cw: 16-bit offset */ + + .org wrap16_cs_base + 0x10020 +wrap16_fail: + /* jmp lands here when EIP does not wrap (0x10020) */ + ljmpl $SEL_CODE32, $test_failed + + .text + .code32 + .globl main +main: + movl $wrap16_cs_base, %eax + movw %ax, gdt16_code16 + 2 + shrl $16, %eax + movb %al, gdt16_code16 + 4 + movb %ah, gdt16_code16 + 7 + lgdt gdtr16 + + ljmpw $SEL_CODE16, $0xFFF0 + +test_passed: + pushl $pass_msg + call ml_printf + addl $4, %esp + xorl %eax, %eax + ret + +test_failed: + pushl $fail_msg + call ml_printf + addl $8, %esp + movl $1, %eax + ret --=20 2.53.0