From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 985222FD66D for ; Thu, 4 Jun 2026 14:47:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780584435; cv=none; b=Y2eC/tgKMN9mxM2cd885uSQtsxgJeMYWDKX/2gHi1qpE5KSe+LhMSQ+io3sFk4fMwgxS/TzoNbGEtjgN7yjjFwjTXB2aN+N1q96IrjRugKBFiLabH9h1kRgBUsAAFWuvkX0PzNv2wUWJCeWfpB7KKl6HY7OaEp1NcuGQzinkr10= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780584435; c=relaxed/simple; bh=jgrP38vN0ksP/oa1kiyCmivDmkkc+7nIEFf7CigrHKs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cQzCykUqIEhlppFqh/DWddaaXmf6M6ik138MrvvRw4wxssDfsVLkjhswGzINttyjVU05tYfrKoJbFqefDNK3btAFifLUAzeiCqZtLIfsa/RX2mRoK+bOxORbFwJiKy0qIPiAmPgWuGYNzeq0kjBOy6GIvb9vE9C8RpCfStFWiZo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=KBFXnSsw; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="KBFXnSsw" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0DC8E1F00898; Thu, 4 Jun 2026 14:47:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1780584433; bh=Gap47TFOhQ6ls41YxeGXU4G423uVPHsg939VsDxPFk8=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=KBFXnSswvYVCg9eysZQNuFFJE4G9x4cOdwkvX8b9ue9jiemQBPzwvlfkMWFIOKDQ3 l57vw4ntncETnQcYHWHxpkFBUtE7GXuyWc1PmbvWGTfHaqFefoBP4B/x28aGWfRV4R uk+l5uX+LUF2TvCqJb6xTCtlS1TnNGKfVQiNQfiL+Odx4shL2ZfH329iTlvlJYqqPg 78Qs1LKUBk+ih4mhJg2RkQMJ7/aCdz1Ufu+TeFSaZyYXH5cmPWZ4vExijG1v6AMTW7 liMOugEg+kc2qjj/Tf6Jnwf6WmPj7SVmmesaAo9mknefU/K8TaJD9Z4M+zomGrpj8T ndQRjF279ryvA== Received: from phl-compute-02.internal (phl-compute-02.internal [10.202.2.42]) by mailfauth.phl.internal (Postfix) with ESMTP id 60A49F4006A; Thu, 4 Jun 2026 10:47:12 -0400 (EDT) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-02.internal (MEProxy); Thu, 04 Jun 2026 10:47:12 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: dmFkZTFj8ZnrVtOWx8yn50yce9nPuOihKudbBgfFxqft5oPy1zZX5JIfLuFpGdCcWrnhfl Pxz325N4XazAyAzTdNz9aWTIHmCJwIofCI+99fVGtXOCjxSo8k+EHuf2f7CbPoHEMSWrfL Eu/vyp4eHayFnrBZ+cvyjC170T8MtAfXrlwruHYlST6W6tnHi2O6FaxgAEwRiKVEZclZgB 1OCxiWZWO0+bd+wOVrTUbIWcIOKxo9Jhw8E1nFS7ABPYRi3tBDwc6A3isnRgid/yx8777k UbZgmYnR8zVjavXZPBuNijxgMcY5IV2jOz3OuyI0TuHvOl6pbuCj+E3+Wwala5e5leikEG C+j80mjhalvt/GDp7Kt08a3Sdvna0iBUHd63KUeNIYVAizY212bFNayQnsE6YGNQ9bDdU5 yjCD8mQ79RkHa7mHneLUSPxcUSoY0RAdvlDZ5GJqOHTFkuJbe8NJ+DTrjpsQ8JZwil1fjE FfgkAelctBIXeXXC5I+Yx6WUPRuegb8q4KGzM7FXjnNkLK/Pt7IXHIrbbwv48OiuILve2a PmYdA+kzbDzP4iRYWejwTR4+mMCT3DbFJdoc2erIO81qAUAZgENV7QIH2rWDiuZxyCpRwk /UI0kmcW0aLlkdxKzTlkpNVQPrnQFWSMmBt/gZKeufI97vEsqbUSlgUkl0bw X-ME-Proxy: Feedback-ID: i10464835:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 4 Jun 2026 10:47:11 -0400 (EDT) From: "Kiryl Shutsemau (Meta)" To: tglx@kernel.org, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com Cc: seanjc@google.com, pbonzini@redhat.com, sathyanarayanan.kuppuswamy@linux.intel.com, kai.huang@intel.com, xiaoyao.li@intel.com, binbin.wu@linux.intel.com, rick.p.edgecombe@intel.com, david.laight.linux@gmail.com, ak@linux.intel.com, djbw@kernel.org, tsyrulnikov.borys@gmail.com, x86@kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, "Kiryl Shutsemau (Meta)" Subject: [PATCH v4 2/3] x86/insn-eval: Add insn_assign_reg() helper Date: Thu, 4 Jun 2026 15:47:00 +0100 Message-ID: X-Mailer: git-send-email 2.54.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit KVM's instruction emulator has a small helper, assign_register(), that writes a value into a sub-register with x86 partial-register-write semantics: 1- and 2-byte writes leave the upper bits of the destination untouched, 4-byte writes zero-extend to 64 bits, 8-byte writes overwrite the full register. The TDX guest #VE handler needs the same logic for port I/O emulation to get 32-bit zero-extension right. Rather than copy-pasting the helper, lift it to as insn_assign_reg() so both can use it. Rewrite the body using arithmetic instead of pointer punning so the helper does not depend on -fno-strict-aliasing or little-endian byte order, and add to the header's includes so it builds standalone in callers that have not pulled it in transitively. No functional change. Signed-off-by: Kiryl Shutsemau --- arch/x86/include/asm/insn-eval.h | 25 +++++++++++++++++++++++++ arch/x86/kvm/emulate.c | 26 ++++---------------------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/arch/x86/include/asm/insn-eval.h b/arch/x86/include/asm/insn-eval.h index 4733e9064ee5..85251e718a77 100644 --- a/arch/x86/include/asm/insn-eval.h +++ b/arch/x86/include/asm/insn-eval.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #define INSN_CODE_SEG_ADDR_SZ(params) ((params >> 4) & 0xf) @@ -46,4 +47,28 @@ enum insn_mmio_type insn_decode_mmio(struct insn *insn, int *bytes); bool insn_is_nop(struct insn *insn); +/* + * Write @val into *@reg with x86 partial-register-write semantics: a 1- + * or 2-byte write leaves the upper bits of the destination untouched; a + * 4-byte write zero-extends to 64 bits (matching IN[BWL], MOV[BWL] + * etc.); an 8-byte write overwrites the full register. + */ +static inline void insn_assign_reg(unsigned long *reg, u64 val, int bytes) +{ + switch (bytes) { + case 1: + *reg = (*reg & ~0xfful) | (val & 0xff); + break; + case 2: + *reg = (*reg & ~0xfffful) | (val & 0xffff); + break; + case 4: + *reg = (u32)val; + break; + case 8: + *reg = val; + break; + } +} + #endif /* _ASM_X86_INSN_EVAL_H */ diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index 8013dccb3110..74972c17edb8 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -24,6 +24,7 @@ #include "kvm_emulate.h" #include #include +#include #include #include #include @@ -439,25 +440,6 @@ static void assign_masked(ulong *dest, ulong src, ulong mask) *dest = (*dest & ~mask) | (src & mask); } -static void assign_register(unsigned long *reg, u64 val, int bytes) -{ - /* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */ - switch (bytes) { - case 1: - *(u8 *)reg = (u8)val; - break; - case 2: - *(u16 *)reg = (u16)val; - break; - case 4: - *reg = (u32)val; - break; /* 64b: zero-extend */ - case 8: - *reg = val; - break; - } -} - static inline unsigned long ad_mask(struct x86_emulate_ctxt *ctxt) { return (1UL << (ctxt->ad_bytes << 3)) - 1; @@ -505,7 +487,7 @@ register_address_increment(struct x86_emulate_ctxt *ctxt, int reg, int inc) { ulong *preg = reg_rmw(ctxt, reg); - assign_register(preg, *preg + inc, ctxt->ad_bytes); + insn_assign_reg(preg, *preg + inc, ctxt->ad_bytes); } static void rsp_increment(struct x86_emulate_ctxt *ctxt, int inc) @@ -1766,7 +1748,7 @@ static int load_segment_descriptor(struct x86_emulate_ctxt *ctxt, static void write_register_operand(struct operand *op) { - return assign_register(op->addr.reg, op->val, op->bytes); + return insn_assign_reg(op->addr.reg, op->val, op->bytes); } static int writeback(struct x86_emulate_ctxt *ctxt, struct operand *op) @@ -2007,7 +1989,7 @@ static int em_popa(struct x86_emulate_ctxt *ctxt) rc = emulate_pop(ctxt, &val, ctxt->op_bytes); if (rc != X86EMUL_CONTINUE) break; - assign_register(reg_rmw(ctxt, reg), val, ctxt->op_bytes); + insn_assign_reg(reg_rmw(ctxt, reg), val, ctxt->op_bytes); --reg; } return rc; -- 2.54.0