[PATCH 4/5] x86/insn: Add insn_opcode_map() helper and fix insn_masking_exception()

From: Masami Hiramatsu (Google)

Date: Sun Nov 22 2026 - 21:48:07 EST


From: Masami Hiramatsu <mhiramat@xxxxxxxxxx>

The x86 instruction decoder stores the physical opcode bytes in
insn->opcode.bytes[] and their physical byte count in insn->opcode.nbytes,
which is required for field offset calculations such as
insn_offset_modrm(). As a result, REX2 M0=1 (map 1) and VEX/EVEX/XOP
instructions all have insn->opcode.nbytes == 1 even though their opcodes
do not belong to map 0.

Add insn_opcode_map() and insn_last_opcode() helpers so callers can
reliably determine the logical opcode map and final opcode byte across
legacy, REX2, VEX, EVEX, and XOP encodings.

Use insn_opcode_map() in insn_masking_exception() so that non-map-0
instructions (e.g. VEX/EVEX/XOP or REX2 M0=1 instructions with opcode
0x1f or 0x8e) are not misidentified as POP SS or MOV SS.

Fixes: ee6a7354a362 ("kprobes/x86: Prohibit probing on exception masking instructions")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>
---
arch/x86/include/asm/insn.h | 43 +++++++++++++++++++++++++++++++++++++
tools/arch/x86/include/asm/insn.h | 43 +++++++++++++++++++++++++++++++++++++
2 files changed, 86 insertions(+)

diff --git a/arch/x86/include/asm/insn.h b/arch/x86/include/asm/insn.h
index 8fb92da1a2c4..9861560efc36 100644
--- a/arch/x86/include/asm/insn.h
+++ b/arch/x86/include/asm/insn.h
@@ -326,6 +326,46 @@ static inline int insn_offset_immediate(struct insn *insn)
#define POP_SS_OPCODE 0x1f
#define MOV_SREG_OPCODE 0x8e

+/*
+ * Return the logical opcode map number for @insn:
+ * 0: 1-byte opcode map (legacy / REX / REX2 M0=0)
+ * 1: 2-byte opcode map (0x0f / REX2 M0=1 / VEX m=1 / EVEX m=1)
+ * 2: 3-byte opcode map (0x0f 0x38 / VEX m=2 / EVEX m=2)
+ * 3: 3-byte opcode map (0x0f 0x3a / VEX m=3 / EVEX m=3)
+ * 4..7: EVEX/VEX maps 4..7
+ * 8..31: XOP maps
+ */
+static inline int insn_opcode_map(struct insn *insn)
+{
+ if (insn_is_avx_or_xop(insn)) {
+ if (avx_insn_is_xop(insn))
+ return insn_xop_map_bits(insn);
+ return insn_vex_m_bits(insn);
+ }
+
+ if (insn_is_rex2(insn))
+ return insn_rex2_m_bit(insn) ? 1 : 0;
+
+ if (insn->opcode.nbytes == 1)
+ return 0;
+ if (insn->opcode.nbytes == 2)
+ return 1;
+ if (insn->opcode.nbytes == 3) {
+ if (insn->opcode.bytes[1] == 0x38)
+ return 2;
+ if (insn->opcode.bytes[1] == 0x3a)
+ return 3;
+ }
+ return -1;
+}
+
+static inline insn_byte_t insn_last_opcode(struct insn *insn)
+{
+ if (!insn->opcode.nbytes)
+ return 0;
+ return insn->opcode.bytes[insn->opcode.nbytes - 1];
+}
+
/*
* Intel SDM Vol.3A 6.8.3 states;
* "Any single-step trap that would be delivered following the MOV to SS
@@ -336,6 +376,9 @@ static inline int insn_offset_immediate(struct insn *insn)
*/
static inline int insn_masking_exception(struct insn *insn)
{
+ if (insn_opcode_map(insn) != 0)
+ return 0;
+
return insn->opcode.bytes[0] == POP_SS_OPCODE ||
(insn->opcode.bytes[0] == MOV_SREG_OPCODE &&
X86_MODRM_REG(insn->modrm.bytes[0]) == 2);
diff --git a/tools/arch/x86/include/asm/insn.h b/tools/arch/x86/include/asm/insn.h
index 1d2903508bbe..e7fba77f0b11 100644
--- a/tools/arch/x86/include/asm/insn.h
+++ b/tools/arch/x86/include/asm/insn.h
@@ -326,6 +326,46 @@ static inline int insn_offset_immediate(struct insn *insn)
#define POP_SS_OPCODE 0x1f
#define MOV_SREG_OPCODE 0x8e

+/*
+ * Return the logical opcode map number for @insn:
+ * 0: 1-byte opcode map (legacy / REX / REX2 M0=0)
+ * 1: 2-byte opcode map (0x0f / REX2 M0=1 / VEX m=1 / EVEX m=1)
+ * 2: 3-byte opcode map (0x0f 0x38 / VEX m=2 / EVEX m=2)
+ * 3: 3-byte opcode map (0x0f 0x3a / VEX m=3 / EVEX m=3)
+ * 4..7: EVEX/VEX maps 4..7
+ * 8..31: XOP maps
+ */
+static inline int insn_opcode_map(struct insn *insn)
+{
+ if (insn_is_avx_or_xop(insn)) {
+ if (avx_insn_is_xop(insn))
+ return insn_xop_map_bits(insn);
+ return insn_vex_m_bits(insn);
+ }
+
+ if (insn_is_rex2(insn))
+ return insn_rex2_m_bit(insn) ? 1 : 0;
+
+ if (insn->opcode.nbytes == 1)
+ return 0;
+ if (insn->opcode.nbytes == 2)
+ return 1;
+ if (insn->opcode.nbytes == 3) {
+ if (insn->opcode.bytes[1] == 0x38)
+ return 2;
+ if (insn->opcode.bytes[1] == 0x3a)
+ return 3;
+ }
+ return -1;
+}
+
+static inline insn_byte_t insn_last_opcode(struct insn *insn)
+{
+ if (!insn->opcode.nbytes)
+ return 0;
+ return insn->opcode.bytes[insn->opcode.nbytes - 1];
+}
+
/*
* Intel SDM Vol.3A 6.8.3 states;
* "Any single-step trap that would be delivered following the MOV to SS
@@ -336,6 +376,9 @@ static inline int insn_offset_immediate(struct insn *insn)
*/
static inline int insn_masking_exception(struct insn *insn)
{
+ if (insn_opcode_map(insn) != 0)
+ return 0;
+
return insn->opcode.bytes[0] == POP_SS_OPCODE ||
(insn->opcode.bytes[0] == MOV_SREG_OPCODE &&
X86_MODRM_REG(insn->modrm.bytes[0]) == 2);