From nobody Mon Nov 23 04:34:30 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=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1794250672; cv=none; d=zohomail.com; s=zohoarc; b=b4sj66gVSDmF8imFn3wL2Nl6ZXAQtgOfMQjy4PI+G6niaZQhvSnGrBRz6iuBH3d/RIihxC8zQDxZzKXFaYSEyRqU/PHQqgA0LmwuhaeHmDGWgFejc+QqnBuWHSscqdo/CZLHmKpSajhvPP87QAnlY6KrmO/Q1KjhfphpZLDNhKI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1794250672; h=Content-Transfer-Encoding:Date:Date:From:From: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:Cc; bh=3bk/eVkd28vuCJgHYUXEVLJN2TVuhyrY6wlAP5JhMjo=; b=jSPLdAHlu3NFY8xrjfxVcXVjmJgHaU4zlpAUE3ilcSOI8ntq/mdXcXfBpT514GSjqHrDJCoFT+IlXzDT/JVO8gjI+PJuMkYl1gYmF+Ah5IdeGMqLGiGnAU91Po17wkzyFYfA9al5jNm/A673Y8AqDoLQ1B0+l2ztXVbemCEky60= 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=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1794250672155546.257477220787; Mon, 9 Nov 2026 10:57:52 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xQUYL-0007YL-Tg; Mon, 09 Nov 2026 13:57:13 -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 1xQUYE-0007Tr-3L for qemu-devel@nongnu.org; Mon, 09 Nov 2026 13:57:09 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xQUYA-00067A-RH for qemu-devel@nongnu.org; Mon, 09 Nov 2026 13:57:04 -0500 Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-16-R7o5zj_LMNm-YyuyZ3oWlg-1; Mon, 09 Nov 2026 13:57:00 -0500 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 57B051956077 for ; Mon, 9 Nov 2026 18:56:59 +0000 (UTC) Received: from pbonzini-thinkpadx1carbon6th.rmtit.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A58A43001DA5 for ; Mon, 9 Nov 2026 18:56:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1794250621; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=3bk/eVkd28vuCJgHYUXEVLJN2TVuhyrY6wlAP5JhMjo=; b=gsFaOZDtPl+FQWLB/h6chRSSrqU5Pa56A86GQAmDINdK9TLOR78gOK0bo9TpbMAnj97Tx1 9VAHRiX9JaDJncP/9IiVAL8kKbC4QN4+Dqoe3KKjLEXWBOgOAjCQDF5Hv5NWaXv2+gTPKU YZRCShh2fyobpFoDPZpOpEUZsN5abTc= X-MC-Unique: R7o5zj_LMNm-YyuyZ3oWlg-1 X-Mimecast-MFC-AGG-ID: R7o5zj_LMNm-YyuyZ3oWlg_1794250619 From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH] target/i386/tcg: reject VEX encodings of PBLENDVB, BLENDVPS and BLENDVPD Date: Mon, 9 Nov 2026 19:56:56 +0100 Message-ID: <20261109185656.1068651-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: 5 X-Spam_score: 0.5 X-Spam_bar: / X-Spam_report: (0.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_PASS=-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-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 @redhat.com) X-ZM-MESSAGEID: 1794250674596158501 Content-Type: text/plain; charset="utf-8" The opcodes 66 0F 38 10, 14 and 15 (PBLENDVB, BLENDVPS and BLENDVPD) have vex_class 4, which allows both the legacy and the VEX encoding, so VEX.66.0F38 10, 14 and 15 were decoded as the same instructions. The VEX forms of these instructions are VPBLENDVB, VBLENDVPS and VBLENDVPD, which have four operands and are in map 0F 3A, so the VEX opcodes in map 0F 38 should raise #UD. Keep vex_class 4, which provides the legacy SSE checks, and add a new vex_special that raises #UD if the VEX or EVEX prefix is present. This is borderline disgusting, but an effective solution to x86's opcode sprawl will have to wait for a new AI policy... Signed-off-by: Paolo Bonzini Reviewed-by: Richard Henderson --- target/i386/tcg/decode-new.h | 3 ++ tests/tcg/i386/test-i386-blendv.c | 51 +++++++++++++++++++++++++++++++ target/i386/tcg/decode-new.c.inc | 16 +++++++--- tests/tcg/i386/meson.build | 1 + tests/tcg/x86_64/meson.build | 1 + 5 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 tests/tcg/i386/test-i386-blendv.c diff --git a/target/i386/tcg/decode-new.h b/target/i386/tcg/decode-new.h index b40c75fa222..7f7ed3b6cf4 100644 --- a/target/i386/tcg/decode-new.h +++ b/target/i386/tcg/decode-new.h @@ -257,6 +257,9 @@ typedef enum X86InsnSpecial { typedef enum X86VEXSpecial { X86_VEX_None, =20 + /* SSE and AVX instructions use different encoding */ + X86_VEX_SSEOnly, + /* Legacy SSE instructions that allow unaligned operands */ X86_VEX_SSEUnaligned, =20 diff --git a/tests/tcg/i386/test-i386-blendv.c b/tests/tcg/i386/test-i386-b= lendv.c new file mode 100644 index 00000000000..4f111fa4745 --- /dev/null +++ b/tests/tcg/i386/test-i386-blendv.c @@ -0,0 +1,51 @@ +/* + * Check that the legacy BLENDVPS, BLENDVPD and PBLENDVB exist, while the + * VEX encodings in the same opcodes do not (VBLENDVPS and the others are = in + * map 0F 3A as they add *two* register operands, and the second is encoded + * in an immediate byte). + */ +#include +#include +#include +#include + +static sigjmp_buf jmp_env; + +static void sigill_handler(int sig) +{ + siglongjmp(jmp_env, 1); +} + +/* 66 0F 38 xx with modrm =3D 11 001 001 (xmm1, xmm1 operands). */ +#define SSE_INSN(op) asm volatile(".byte 0x66, 0x0f, 0x38, " #op ", 0xc9" \ + : : : "xmm0", "xmm1") + +/* Same but encoded with a VEX3 prefix: RXB=3D111b, mmmmm=3D2 (0F38), w=3D= 0, + * VVVV=3D1111b (forced because QEMU does not list an Hx operand), + * l=3D0 (128-bit), pp=3D1 (66h). */ +#define VEX_INSN(op) asm volatile(".byte 0xc4, 0xe2, 0x79, " #op ", 0xc9") + +#define CHECK_VEX_ILLEGAL(op) \ + do { \ + if (sigsetjmp(jmp_env, 1) =3D=3D 0) { \ + VEX_INSN(op); \ + printf("VEX " #op " did not fault\n"); \ + exit(1); \ + } \ + } while (0) + +int main(void) +{ + struct sigaction act =3D { .sa_handler =3D sigill_handler }; + + sigaction(SIGILL, &act, NULL); + + SSE_INSN(0x10); + SSE_INSN(0x14); + SSE_INSN(0x15); + + CHECK_VEX_ILLEGAL(0x10); + CHECK_VEX_ILLEGAL(0x14); + CHECK_VEX_ILLEGAL(0x15); + return 0; +} diff --git a/target/i386/tcg/decode-new.c.inc b/target/i386/tcg/decode-new.= c.inc index 9bcc0f76864..b91e3718cb5 100644 --- a/target/i386/tcg/decode-new.c.inc +++ b/target/i386/tcg/decode-new.c.inc @@ -120,7 +120,8 @@ * * (*) AMD lists VPBLENDVB as related to SSE4.1 PBLENDVB, which may * explain why it is considered exception class 4. However, - * Intel says that VEX-only instructions should be in class 6... + * the two have different encodings (in 0F38 for SSE, in 0F3A + * for AVX). * * (+) Not found in Intel's table 2-16 * @@ -250,6 +251,7 @@ #define vex2_rep3 .vex_class =3D 2, .vex_special =3D X86_VEX_REPScalar, #define vex3 .vex_class =3D 3, #define vex4 .vex_class =3D 4, +#define sse4 .vex_class =3D 4, .vex_special =3D X86_VEX_SSEOnly, #define vex4_unal .vex_class =3D 4, .vex_special =3D X86_VEX_SSEUnaligned, #define vex4_rep5 .vex_class =3D 4, .vex_special =3D X86_VEX_REPScalar, #define vex5 .vex_class =3D 5, @@ -732,10 +734,10 @@ static const X86OpEntry opcodes_0F38_00toEF[240] =3D { [0x06] =3D X86_OP_ENTRY3(PHSUBD, V,x, H,x, W,x, vex4 cpuid(SSSE= 3) mmx avx2_256 p_00_66), [0x07] =3D X86_OP_ENTRY3(PHSUBSW, V,x, H,x, W,x, vex4 cpuid(SSSE= 3) mmx avx2_256 p_00_66), =20 - [0x10] =3D X86_OP_ENTRY2(PBLENDVB, V,x, W,x, vex4 cpuid(SSE4= 1) avx2_256 p_66), + [0x10] =3D X86_OP_ENTRY2(PBLENDVB, V,x, W,x, sse4 cpuid(SSE4= 1) p_66), [0x13] =3D X86_OP_ENTRY2(VCVTPH2PS, V,x, W,xh, vex11 chk(W0) c= puid(F16C) p_66), - [0x14] =3D X86_OP_ENTRY2(BLENDVPS, V,x, W,x, vex4 cpuid(SSE4= 1) p_66), - [0x15] =3D X86_OP_ENTRY2(BLENDVPD, V,x, W,x, vex4 cpuid(SSE4= 1) p_66), + [0x14] =3D X86_OP_ENTRY2(BLENDVPS, V,x, W,x, sse4 cpuid(SSE4= 1) p_66), + [0x15] =3D X86_OP_ENTRY2(BLENDVPD, V,x, W,x, sse4 cpuid(SSE4= 1) p_66), /* Listed incorrectly as type 4 */ [0x16] =3D X86_OP_ENTRY3(VPERMD, V,qq, H,qq, W,qq, vex6 chk(W= 0) cpuid(AVX2) p_66), /* vpermps */ [0x17] =3D X86_OP_ENTRY3(VPTEST, None,None, V,x, W,x, vex4 cpuid= (SSE41) p_66), @@ -3109,6 +3111,12 @@ static bool validate_vex(DisasContext *s, X86Decoded= Insn *decode) } break; =20 + case X86_VEX_SSEOnly: + if (s->prefix & PREFIX_VEX) { + goto illegal; + } + break; + case X86_VEX_SSEUnaligned: /* handled in sse_needs_alignment. */ break; diff --git a/tests/tcg/i386/meson.build b/tests/tcg/i386/meson.build index 648b516582e..c912f37fab7 100644 --- a/tests/tcg/i386/meson.build +++ b/tests/tcg/i386/meson.build @@ -10,6 +10,7 @@ tests +=3D { }, 'test-aes.c': {'cflags': ['-O1', '-msse2', '-maes']}, 'test-i386-adcox.c': {'cflags': ['-O2']}, + 'test-i386-blendv.c': {}, 'test-i386-bmi2.c': {'cflags': ['-O2', '-fwrapv']}, 'test-i386-f2xm1.c': {}, 'test-i386-fbstp.c': {}, diff --git a/tests/tcg/x86_64/meson.build b/tests/tcg/x86_64/meson.build index fc7f48e98eb..9738d04220b 100644 --- a/tests/tcg/x86_64/meson.build +++ b/tests/tcg/x86_64/meson.build @@ -47,6 +47,7 @@ tests +=3D { 'exe_name': 'test-x86_64' }, '../i386/test-i386-adcox.c': {'cflags': ['-O2']}, + '../i386/test-i386-blendv.c': {}, '../i386/test-i386-bmi2.c': {'cflags': ['-O2', '-fwrapv']}, '../i386/test-idiv-overflow.c': {}, } --=20 2.56.0