Commit 0f9a4145bf for qemu.org
commit 0f9a4145bf5515d48345a9f775e66c6ec2cf4be1
Author: Paolo Bonzini <pbonzini@redhat.com>
Date: Mon Nov 2 01:28:15 2026 +0100
target/i386/tcg: reject VEX encodings of PBLENDVB, BLENDVPS and BLENDVPD
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 <pbonzini@redhat.com>
diff --git a/target/i386/tcg/decode-new.c.inc b/target/i386/tcg/decode-new.c.inc
index b51b2f1ae9..5b3d772d99 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 = 2, .vex_special = X86_VEX_REPScalar,
#define vex3 .vex_class = 3,
#define vex4 .vex_class = 4,
+#define sse4 .vex_class = 4, .vex_special = X86_VEX_SSEOnly,
#define vex4_unal .vex_class = 4, .vex_special = X86_VEX_SSEUnaligned,
#define vex4_rep5 .vex_class = 4, .vex_special = X86_VEX_REPScalar,
#define vex5 .vex_class = 5,
@@ -732,10 +734,10 @@ static const X86OpEntry opcodes_0F38_00toEF[240] = {
[0x06] = X86_OP_ENTRY3(PHSUBD, V,x, H,x, W,x, vex4 cpuid(SSSE3) mmx avx2_256 p_00_66),
[0x07] = X86_OP_ENTRY3(PHSUBSW, V,x, H,x, W,x, vex4 cpuid(SSSE3) mmx avx2_256 p_00_66),
- [0x10] = X86_OP_ENTRY2(PBLENDVB, V,x, W,x, vex4 cpuid(SSE41) avx2_256 p_66),
+ [0x10] = X86_OP_ENTRY2(PBLENDVB, V,x, W,x, sse4 cpuid(SSE41) p_66),
[0x13] = X86_OP_ENTRY2(VCVTPH2PS, V,x, W,xh, vex11 chk(W0) cpuid(F16C) p_66),
- [0x14] = X86_OP_ENTRY2(BLENDVPS, V,x, W,x, vex4 cpuid(SSE41) p_66),
- [0x15] = X86_OP_ENTRY2(BLENDVPD, V,x, W,x, vex4 cpuid(SSE41) p_66),
+ [0x14] = X86_OP_ENTRY2(BLENDVPS, V,x, W,x, sse4 cpuid(SSE41) p_66),
+ [0x15] = X86_OP_ENTRY2(BLENDVPD, V,x, W,x, sse4 cpuid(SSE41) p_66),
/* Listed incorrectly as type 4 */
[0x16] = X86_OP_ENTRY3(VPERMD, V,qq, H,qq, W,qq, vex6 chk(W0) cpuid(AVX2) p_66), /* vpermps */
[0x17] = X86_OP_ENTRY3(VPTEST, None,None, V,x, W,x, vex4 cpuid(SSE41) p_66),
@@ -3114,6 +3116,12 @@ static bool validate_vex(DisasContext *s, X86DecodedInsn *decode)
}
break;
+ 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/target/i386/tcg/decode-new.h b/target/i386/tcg/decode-new.h
index b40c75fa22..7f7ed3b6cf 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,
+ /* SSE and AVX instructions use different encoding */
+ X86_VEX_SSEOnly,
+
/* Legacy SSE instructions that allow unaligned operands */
X86_VEX_SSEUnaligned,
diff --git a/tests/tcg/i386/meson.build b/tests/tcg/i386/meson.build
index 648b516582..c912f37fab 100644
--- a/tests/tcg/i386/meson.build
+++ b/tests/tcg/i386/meson.build
@@ -10,6 +10,7 @@ tests += {
},
'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/i386/test-i386-blendv.c b/tests/tcg/i386/test-i386-blendv.c
new file mode 100644
index 0000000000..4f111fa474
--- /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 <setjmp.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+static sigjmp_buf jmp_env;
+
+static void sigill_handler(int sig)
+{
+ siglongjmp(jmp_env, 1);
+}
+
+/* 66 0F 38 xx with modrm = 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=111b, mmmmm=2 (0F38), w=0,
+ * VVVV=1111b (forced because QEMU does not list an Hx operand),
+ * l=0 (128-bit), pp=1 (66h). */
+#define VEX_INSN(op) asm volatile(".byte 0xc4, 0xe2, 0x79, " #op ", 0xc9")
+
+#define CHECK_VEX_ILLEGAL(op) \
+ do { \
+ if (sigsetjmp(jmp_env, 1) == 0) { \
+ VEX_INSN(op); \
+ printf("VEX " #op " did not fault\n"); \
+ exit(1); \
+ } \
+ } while (0)
+
+int main(void)
+{
+ struct sigaction act = { .sa_handler = 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/tests/tcg/x86_64/meson.build b/tests/tcg/x86_64/meson.build
index fc7f48e98e..9738d04220 100644
--- a/tests/tcg/x86_64/meson.build
+++ b/tests/tcg/x86_64/meson.build
@@ -47,6 +47,7 @@ tests += {
'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': {},
}