From: "Masami Hiramatsu (Google)" <mhiramat@kernel.org>
To: Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
x86@kernel.org, Masami Hiramatsu <mhiramat@kernel.org>,
Oleg Nesterov <oleg@redhat.com>,
Peter Zijlstra <peterz@infradead.org>,
Lukas Zobernig <lukas.zobernig@gmail.com>
Cc: "H . Peter Anvin" <hpa@zytor.com>,
linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
Pablo Fraile Alonso <pablo@pablofraile.net>,
Edgecombe Rick P <rick.p.edgecombe@intel.com>
Subject: [PATCH 4/5] x86/insn: Add insn_opcode_map() helper and fix insn_masking_exception()
Date: Mon, 23 Nov 2026 11:47:55 +0900 [thread overview]
Message-ID: <179540207562.233843.3618280650654568620.stgit@devnote2> (raw)
In-Reply-To: <179540203033.233843.12951034848925983867.stgit@devnote2>
From: Masami Hiramatsu <mhiramat@kernel.org>
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@vger.kernel.org
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
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);
next prev parent reply other threads:[~2026-11-23 2:48 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 2:47 [PATCH 0/5] x86/insn, uprobes: Fix opcode map handling for multi-byte, REX2, and VEX/EVEX/XOP instructions None
2026-11-23 2:47 ` [PATCH 1/5] x86/insn-eval: Do not mutate struct insn in insn_is_nop() Masami Hiramatsu (Google)
2026-11-23 2:47 ` [PATCH 2/5] x86/insn: Fix gen-insn-attr-x86.awk to emit INAT_REX2_VARIANT Masami Hiramatsu (Google)
2026-11-23 2:47 ` [PATCH 3/5] x86/insn: Reject invalid REX2 encodings in insn_get_opcode() Masami Hiramatsu (Google)
2026-11-23 2:47 ` Masami Hiramatsu (Google) [this message]
2026-11-23 2:48 ` [PATCH 5/5] uprobes/x86: Fix opcode validation and analysis for multi-byte, REX2, and VEX/EVEX/XOP instructions Masami Hiramatsu (Google)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=179540207562.233843.3618280650654568620.stgit@devnote2 \
--to=mhiramat@kernel.org \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=lukas.zobernig@gmail.com \
--cc=mingo@redhat.com \
--cc=oleg@redhat.com \
--cc=pablo@pablofraile.net \
--cc=peterz@infradead.org \
--cc=rick.p.edgecombe@intel.com \
--cc=tglx@kernel.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox