Skip to contentSkip to conversation

target/i386/tcg: reject VEX encodings of PBLENDVB, BLENDVPS and BLENDVPD

PATCH In review Paolo Bonzini 2 participants 2 messages Nov 9 – Nov 13 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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...

---
 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
target/i386/tcg/decode-new.h +3 -0
index b40c75fa222..7f7ed3b6cf4 100644
@@ -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,
 
tests/tcg/i386/test-i386-blendv.c new file +51 -0

51 lines not on this page · raw message

target/i386/tcg/decode-new.c.inc +12 -4
index 9bcc0f76864..b91e3718cb5 100644
@@ -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),
@@ -3109,6 +3111,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;
tests/tcg/i386/meson.build +1 -0
index 648b516582e..c912f37fab7 100644
@@ -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': {},
tests/tcg/x86_64/meson.build +1 -0
index fc7f48e98eb..9738d04220b 100644
@@ -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': {},
 }
Signature · 1 line
2.56.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 13 lines · Paolo Bonzini

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...

Quoted 8 lines
---
  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

r~