Skip to contentSkip to conversation

x86/insn: Add an insn_decode() API

PATCH Applied Borislav Petkov 6 participants 53 messages Mar 4, 2021 – Mar 24, 2021

Subsystems (5)
x86 perf events objtool uprobes +1 more subsystems
lore
Share & tools
lore

Borislav Petkov

author maintainer
#0 lore raw

v3 cover x86/insn: Add an insn_decode() API

From: Borislav Petkov <[email protected]>

Hi all,

here's v3 with the snafu Masami caught, fixed.

The rest is unchanged, it all is based on x86/seves which contains a small fixlet to sev-es.c. If there are no more issues, I'll queue it next week.

Thx.

Changelog:
==========

v2:
--

finally had some time during the merge window to finish reworking this. Most if not all review feedback should be incorporated.

v1:
---

here's v1 with the requested change to return -ENODATA on short input to the decoder. The rest is as in the previous submission.

Only lightly tested.

Thx.

v0:
---

https://lkml.kernel.org/r/[email protected]

here's what I had in mind, finally split into proper patches. The final goal is for all users of the decoder to simply call insn_decode() and look at its retval. Simple.

As to amluto's question about detecting partial insns, see the diff below.

Running that gives:

insn buffer:
0x48 0xcf 0x48 0x83 0x90 0x90 0x90 0x90 0x90 0x90 0x90 0x90 0x90 0x90 0x90 supplied buf size: 15, ret 0
supplied buf size: 2, ret 0
supplied buf size: 3, ret 0
supplied buf size: 4, ret 0

and the return value is always success.

Which means that that buf_len that gets supplied to the decoder functions doesn't really work and I need to look into it.

That is, provided this is how we want to control what the instruction decoder decodes - by supplying the length of the buffer it should look at.

We could also say that probably there should be a way to say "decode only the first insn in the buffer and ignore the rest". That is all up to the use cases so I'm looking for suggestions here.

In any case, at least the case where I give it

0x48 0xcf 0x48 0x83

and say that buf size is 4, should return an error because the second insn is incomplete. So I need to go look at that now.

Borislav Petkov (21):
x86/insn: Rename insn_decode() to insn_decode_from_regs() x86/insn: Add @buf_len param to insn_init() kernel-doc comment x86/insn: Add a __ignore_sync_check__ marker x86/insn: Add an insn_decode() API
x86/insn-eval: Handle return values from the decoder x86/boot/compressed/sev-es: Convert to insn_decode() perf/x86/intel/ds: Check insn_get_length() retval perf/x86/intel/ds: Check return values of insn decoder functions x86/alternative: Use insn_decode()
x86/mce: Convert to insn_decode()
x86/kprobes: Convert to insn_decode()
x86/sev-es: Split vc_decode_insn()
x86/sev-es: Convert to insn_decode()
x86/traps: Convert to insn_decode()
x86/uprobes: Convert to insn_decode() x86/tools/insn_decoder_test: Convert to insn_decode() tools/objtool: Convert to insn_decode() x86/tools/insn_sanity: Convert to insn_decode() tools/perf: Convert to insn_decode()
x86/insn: Remove kernel_insn_init()
x86/insn: Make insn_complete() static

 arch/x86/boot/compressed/sev-es.c             |  11 +-
 arch/x86/events/intel/ds.c                    |  11 +-
 arch/x86/events/intel/lbr.c                   |  10 +-
 arch/x86/include/asm/inat.h                   |   2 +-
 arch/x86/include/asm/insn-eval.h              |   4 +-
 arch/x86/include/asm/insn.h                   |  44 ++--
 arch/x86/kernel/alternative.c                 |   6 +-
 arch/x86/kernel/cpu/mce/severity.c            |  12 +-
 arch/x86/kernel/kprobes/core.c                |  17 +-
 arch/x86/kernel/kprobes/opt.c                 |   9 +-
 arch/x86/kernel/sev-es.c                      |  63 +++--
 arch/x86/kernel/traps.c                       |   7 +-
 arch/x86/kernel/umip.c                        |   2 +-
 arch/x86/kernel/uprobes.c                     |   8 +-
 arch/x86/lib/inat.c                           |   2 +-
 arch/x86/lib/insn-eval.c                      |  40 +--
 arch/x86/lib/insn.c                           | 230 ++++++++++++++----
 arch/x86/tools/insn_decoder_test.c            |  10 +-
 arch/x86/tools/insn_sanity.c                  |   8 +-
 tools/arch/x86/include/asm/inat.h             |   2 +-
 tools/arch/x86/include/asm/insn.h             |  44 ++--
 tools/arch/x86/lib/inat.c                     |   2 +-
 tools/arch/x86/lib/insn.c                     | 230 ++++++++++++++----
 tools/include/linux/kconfig.h                 |  73 ++++++
 tools/objtool/arch/x86/decode.c               |   9 +-
 tools/objtool/sync-check.sh                   |  17 +-
 tools/perf/arch/x86/tests/insn-x86.c          |   9 +-
 tools/perf/arch/x86/util/archinsn.c           |   9 +-
 tools/perf/check-headers.sh                   |  15 +-
 .../intel-pt-decoder/intel-pt-insn-decoder.c  |  17 +-
 30 files changed, 648 insertions(+), 275 deletions(-)
 create mode 100644 tools/include/linux/kconfig.h
Signature · 1 line
2.29.2
author maintainer
#1 lore raw

PATCH v3 1/21 x86/insn: Rename insn_decode() to insn_decode_from_regs()

From: Borislav Petkov <[email protected]>

Rename insn_decode() to insn_decode_from_regs() to denote that it receives regs as param and uses registers from there during decoding. Free the former name for a more generic version of the function.

No functional changes.

---
 arch/x86/include/asm/insn-eval.h | 4 ++--
 arch/x86/kernel/sev-es.c         | 2 +-
 arch/x86/kernel/umip.c           | 2 +-
 arch/x86/lib/insn-eval.c         | 6 +++---
 4 files changed, 7 insertions(+), 7 deletions(-)
arch/x86/include/asm/insn-eval.h +2 -2
index a0f839aa144d..c755f1a2618b 100644
@@ -23,7 +23,7 @@ unsigned long insn_get_seg_base(struct pt_regs *regs, int seg_reg_idx);
 int insn_get_code_seg_params(struct pt_regs *regs);
 int insn_fetch_from_user(struct pt_regs *regs,
 			 unsigned char buf[MAX_INSN_SIZE]);
-bool insn_decode(struct insn *insn, struct pt_regs *regs,
-		 unsigned char buf[MAX_INSN_SIZE], int buf_size);
+bool insn_decode_from_regs(struct insn *insn, struct pt_regs *regs,
+			   unsigned char buf[MAX_INSN_SIZE], int buf_size);
 
 #endif /* _ASM_X86_INSN_EVAL_H */
arch/x86/kernel/sev-es.c +1 -1
index 1e78f4bd7bf2..557386ce6e3b 100644
@@ -256,7 +256,7 @@ static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
 			return ES_EXCEPTION;
 		}
 
-		if (!insn_decode(&ctxt->insn, ctxt->regs, buffer, res))
+		if (!insn_decode_from_regs(&ctxt->insn, ctxt->regs, buffer, res))
 			return ES_DECODE_FAILED;
 	} else {
 		res = vc_fetch_insn_kernel(ctxt, buffer);
arch/x86/kernel/umip.c +1 -1
index f6225bf22c02..8032f5f7eef9 100644
@@ -356,7 +356,7 @@ bool fixup_umip_exception(struct pt_regs *regs)
 	if (!nr_copied)
 		return false;
 
-	if (!insn_decode(&insn, regs, buf, nr_copied))
+	if (!insn_decode_from_regs(&insn, regs, buf, nr_copied))
 		return false;
 
 	umip_inst = identify_insn(&insn);
arch/x86/lib/insn-eval.c +3 -3
index 4229950a5d78..d0ebf9e93e35 100644
@@ -1454,7 +1454,7 @@ int insn_fetch_from_user(struct pt_regs *regs, unsigned char buf[MAX_INSN_SIZE])
 }
 
 /**
- * insn_decode() - Decode an instruction
+ * insn_decode_from_regs() - Decode an instruction
  * @insn:	Structure to store decoded instruction
  * @regs:	Structure with register values as seen when entering kernel mode
  * @buf:	Buffer containing the instruction bytes
@@ -1467,8 +1467,8 @@ int insn_fetch_from_user(struct pt_regs *regs, unsigned char buf[MAX_INSN_SIZE])
  *
  * True if instruction was decoded, False otherwise.
  */
-bool insn_decode(struct insn *insn, struct pt_regs *regs,
-		 unsigned char buf[MAX_INSN_SIZE], int buf_size)
+bool insn_decode_from_regs(struct insn *insn, struct pt_regs *regs,
+			   unsigned char buf[MAX_INSN_SIZE], int buf_size)
 {
 	int seg_defs;
 
Signature · 1 line
2.29.2
#2 lore raw

[tip: x86/core] x86/insn: Rename insn_decode() to insn_decode_from_regs()

The following commit has been merged into the x86/core branch of tip:

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     9e761296c52dcdb1aaa151b65bd39accb05740d9
Gitweb:        https://git.kernel.org/tip/9e761296c52dcdb1aaa151b65bd39accb05740d9
Author:        Borislav Petkov <[email protected]>
AuthorDate:    Mon, 02 Nov 2020 18:47:34 +01:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Mon, 15 Mar 2021 10:53:10 +01:00

x86/insn: Rename insn_decode() to insn_decode_from_regs()

Rename insn_decode() to insn_decode_from_regs() to denote that it receives regs as param and uses registers from there during decoding. Free the former name for a more generic version of the function.

No functional changes.

---
 arch/x86/include/asm/insn-eval.h | 4 ++--
 arch/x86/kernel/sev-es.c         | 2 +-
 arch/x86/kernel/umip.c           | 2 +-
 arch/x86/lib/insn-eval.c         | 6 +++---
 4 files changed, 7 insertions(+), 7 deletions(-)
arch/x86/include/asm/insn-eval.h +2 -2
index 98b4dae..91d7182 100644
@@ -25,7 +25,7 @@ int insn_fetch_from_user(struct pt_regs *regs,
 			 unsigned char buf[MAX_INSN_SIZE]);
 int insn_fetch_from_user_inatomic(struct pt_regs *regs,
 				  unsigned char buf[MAX_INSN_SIZE]);
-bool insn_decode(struct insn *insn, struct pt_regs *regs,
-		 unsigned char buf[MAX_INSN_SIZE], int buf_size);
+bool insn_decode_from_regs(struct insn *insn, struct pt_regs *regs,
+			   unsigned char buf[MAX_INSN_SIZE], int buf_size);
 
 #endif /* _ASM_X86_INSN_EVAL_H */
arch/x86/kernel/sev-es.c +1 -1
index 225704e..a28922f 100644
@@ -266,7 +266,7 @@ static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
 			return ES_EXCEPTION;
 		}
 
-		if (!insn_decode(&ctxt->insn, ctxt->regs, buffer, res))
+		if (!insn_decode_from_regs(&ctxt->insn, ctxt->regs, buffer, res))
 			return ES_DECODE_FAILED;
 	} else {
 		res = vc_fetch_insn_kernel(ctxt, buffer);
arch/x86/kernel/umip.c +1 -1
index f6225bf..8032f5f 100644
@@ -356,7 +356,7 @@ bool fixup_umip_exception(struct pt_regs *regs)
 	if (!nr_copied)
 		return false;
 
-	if (!insn_decode(&insn, regs, buf, nr_copied))
+	if (!insn_decode_from_regs(&insn, regs, buf, nr_copied))
 		return false;
 
 	umip_inst = identify_insn(&insn);
arch/x86/lib/insn-eval.c +3 -3
index 8cec461..422a592 100644
@@ -1488,7 +1488,7 @@ int insn_fetch_from_user_inatomic(struct pt_regs *regs, unsigned char buf[MAX_IN
 }
 
 /**
- * insn_decode() - Decode an instruction
+ * insn_decode_from_regs() - Decode an instruction
  * @insn:	Structure to store decoded instruction
  * @regs:	Structure with register values as seen when entering kernel mode
  * @buf:	Buffer containing the instruction bytes
@@ -1501,8 +1501,8 @@ int insn_fetch_from_user_inatomic(struct pt_regs *regs, unsigned char buf[MAX_IN
  *
  * True if instruction was decoded, False otherwise.
  */
-bool insn_decode(struct insn *insn, struct pt_regs *regs,
-		 unsigned char buf[MAX_INSN_SIZE], int buf_size)
+bool insn_decode_from_regs(struct insn *insn, struct pt_regs *regs,
+			   unsigned char buf[MAX_INSN_SIZE], int buf_size)
 {
 	int seg_defs;
 
author maintainer
#3 lore raw

PATCH v3 2/21 x86/insn: Add @buf_len param to insn_init() kernel-doc comment

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

It wasn't documented so add it. No functional changes.

---
 arch/x86/lib/insn.c       | 1 +
 tools/arch/x86/lib/insn.c | 1 +
 2 files changed, 2 insertions(+)
arch/x86/lib/insn.c +1 -0
index 435630a6ec97..4d1640dc7882 100644
@@ -51,6 +51,7 @@
  * insn_init() - initialize struct insn
  * @insn:	&struct insn to be initialized
  * @kaddr:	address (in kernel memory) of instruction (or copy thereof)
+ * @buf_len:	length of the insn buffer at @kaddr
  * @x86_64:	!0 for 64-bit kernel or 64-bit app
  */
 void insn_init(struct insn *insn, const void *kaddr, int buf_len, int x86_64)
tools/arch/x86/lib/insn.c +1 -0
index 3d9355ed1246..31afbf0a75f7 100644
@@ -51,6 +51,7 @@
  * insn_init() - initialize struct insn
  * @insn:	&struct insn to be initialized
  * @kaddr:	address (in kernel memory) of instruction (or copy thereof)
+ * @buf_len:	length of the insn buffer at @kaddr
  * @x86_64:	!0 for 64-bit kernel or 64-bit app
  */
 void insn_init(struct insn *insn, const void *kaddr, int buf_len, int x86_64)
Signature · 1 line
2.29.2
#4 lore raw

[tip: x86/core] x86/insn: Add @buf_len param to insn_init() kernel-doc comment

The following commit has been merged into the x86/core branch of tip:

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     508ef28674c1fe6ac388586cb31dc0f0bbc4172c
Gitweb:        https://git.kernel.org/tip/508ef28674c1fe6ac388586cb31dc0f0bbc4172c
Author:        Borislav Petkov <[email protected]>
AuthorDate:    Mon, 02 Nov 2020 19:12:16 +01:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Mon, 15 Mar 2021 11:00:19 +01:00

x86/insn: Add @buf_len param to insn_init() kernel-doc comment

It wasn't documented so add it. No functional changes.

---
 arch/x86/lib/insn.c       | 1 +
 tools/arch/x86/lib/insn.c | 1 +
 2 files changed, 2 insertions(+)
arch/x86/lib/insn.c +1 -0
index 435630a..4d1640d 100644
@@ -51,6 +51,7 @@
  * insn_init() - initialize struct insn
  * @insn:	&struct insn to be initialized
  * @kaddr:	address (in kernel memory) of instruction (or copy thereof)
+ * @buf_len:	length of the insn buffer at @kaddr
  * @x86_64:	!0 for 64-bit kernel or 64-bit app
  */
 void insn_init(struct insn *insn, const void *kaddr, int buf_len, int x86_64)
tools/arch/x86/lib/insn.c +1 -0
index 3d9355e..31afbf0 100644
@@ -51,6 +51,7 @@
  * insn_init() - initialize struct insn
  * @insn:	&struct insn to be initialized
  * @kaddr:	address (in kernel memory) of instruction (or copy thereof)
+ * @buf_len:	length of the insn buffer at @kaddr
  * @x86_64:	!0 for 64-bit kernel or 64-bit app
  */
 void insn_init(struct insn *insn, const void *kaddr, int buf_len, int x86_64)
author maintainer
#5 lore raw

PATCH v3 3/21 x86/insn: Add a __ignore_sync_check__ marker

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

Add an explicit __ignore_sync_check__ marker which will be used to mark lines which are supposed to be ignored by file synchronization check scripts, its advantage being that it explicitly denotes such lines in the code.

---
 arch/x86/include/asm/inat.h       |  2 +-
 arch/x86/include/asm/insn.h       |  2 +-
 arch/x86/lib/inat.c               |  2 +-
 arch/x86/lib/insn.c               |  6 +++---
 tools/arch/x86/include/asm/inat.h |  2 +-
 tools/arch/x86/include/asm/insn.h |  2 +-
 tools/arch/x86/lib/inat.c         |  2 +-
 tools/arch/x86/lib/insn.c         |  6 +++---
 tools/objtool/sync-check.sh       | 17 +++++++++++++----
 tools/perf/check-headers.sh       | 15 +++++++++++----
 10 files changed, 36 insertions(+), 20 deletions(-)
arch/x86/include/asm/inat.h +1 -1
index 4cf2ad521f65..b56c5741581a 100644
@@ -6,7 +6,7 @@
  *
  * Written by Masami Hiramatsu <[email protected]>
  */
-#include <asm/inat_types.h>
+#include <asm/inat_types.h> /* __ignore_sync_check__ */
 
 /*
  * Internal bits. Don't use bitmasks directly, because these bits are
arch/x86/include/asm/insn.h +1 -1
index 95a448fbb44c..93f84600ac2c 100644
@@ -9,7 +9,7 @@
 
 #include <asm/byteorder.h>
 /* insn_attr_t is defined in inat.h */
-#include <asm/inat.h>
+#include <asm/inat.h> /* __ignore_sync_check__ */
 
 #if defined(__BYTE_ORDER) ? __BYTE_ORDER == __LITTLE_ENDIAN : defined(__LITTLE_ENDIAN)
 
arch/x86/lib/inat.c +1 -1
index 12539fca75c4..b0f3b2a62ae2 100644
@@ -4,7 +4,7 @@
  *
  * Written by Masami Hiramatsu <[email protected]>
  */
-#include <asm/insn.h>
+#include <asm/insn.h> /* __ignore_sync_check__ */
 
 /* Attribute tables are generated from opcode map */
 #include "inat-tables.c"
arch/x86/lib/insn.c +3 -3
index 4d1640dc7882..ed1e0335aa14 100644
@@ -11,10 +11,10 @@
 #else
 #include <string.h>
 #endif
-#include <asm/inat.h>
-#include <asm/insn.h>
+#include <asm/inat.h> /*__ignore_sync_check__ */
+#include <asm/insn.h> /* __ignore_sync_check__ */
 
-#include <asm/emulate_prefix.h>
+#include <asm/emulate_prefix.h> /* __ignore_sync_check__ */
 
 #define leXX_to_cpu(t, r)						\
 ({									\
tools/arch/x86/include/asm/inat.h +1 -1
index 877827b7c2c3..a61051400311 100644
@@ -6,7 +6,7 @@
  *
  * Written by Masami Hiramatsu <[email protected]>
  */
-#include "inat_types.h"
+#include "inat_types.h" /* __ignore_sync_check__ */
 
 /*
  * Internal bits. Don't use bitmasks directly, because these bits are
tools/arch/x86/include/asm/insn.h +1 -1
index cc777c185212..7e1239aa7dd9 100644
@@ -9,7 +9,7 @@
 
 #include <asm/byteorder.h>
 /* insn_attr_t is defined in inat.h */
-#include "inat.h"
+#include "inat.h" /* __ignore_sync_check__ */
 
 #if defined(__BYTE_ORDER) ? __BYTE_ORDER == __LITTLE_ENDIAN : defined(__LITTLE_ENDIAN)
 
tools/arch/x86/lib/inat.c +1 -1
index 4f5ed49e1b4e..dfbcc6405941 100644
@@ -4,7 +4,7 @@
  *
  * Written by Masami Hiramatsu <[email protected]>
  */
-#include "../include/asm/insn.h"
+#include "../include/asm/insn.h" /* __ignore_sync_check__ */
 
 /* Attribute tables are generated from opcode map */
 #include "inat-tables.c"
tools/arch/x86/lib/insn.c +3 -3
index 31afbf0a75f7..1174c43b3944 100644
@@ -11,10 +11,10 @@
 #else
 #include <string.h>
 #endif
-#include "../include/asm/inat.h"
-#include "../include/asm/insn.h"
+#include "../include/asm/inat.h" /* __ignore_sync_check__ */
+#include "../include/asm/insn.h" /* __ignore_sync_check__ */
 
-#include "../include/asm/emulate_prefix.h"
+#include "../include/asm/emulate_prefix.h" /* __ignore_sync_check__ */
 
 #define leXX_to_cpu(t, r)						\
 ({									\
tools/objtool/sync-check.sh +13 -4
index 606a4b5e929f..4bbabaecab14 100755
@@ -16,11 +16,14 @@ arch/x86/include/asm/emulate_prefix.h
 arch/x86/lib/x86-opcode-map.txt
 arch/x86/tools/gen-insn-attr-x86.awk
 include/linux/static_call_types.h
-arch/x86/include/asm/inat.h     -I '^#include [\"<]\(asm/\)*inat_types.h[\">]'
-arch/x86/include/asm/insn.h     -I '^#include [\"<]\(asm/\)*inat.h[\">]'
-arch/x86/lib/inat.c             -I '^#include [\"<]\(../include/\)*asm/insn.h[\">]'
-arch/x86/lib/insn.c             -I '^#include [\"<]\(../include/\)*asm/in\(at\|sn\).h[\">]' -I '^#include [\"<]\(../include/\)*asm/emulate_prefix.h[\">]'
 "
+
+SYNC_CHECK_FILES='
+arch/x86/include/asm/inat.h
+arch/x86/include/asm/insn.h
+arch/x86/lib/inat.c
+arch/x86/lib/insn.c
+'
 fi
 
 check_2 () {
@@ -63,3 +66,9 @@ while read -r file_entry; do
 done <<EOF
 $FILES
 EOF
+
+if [ "$SRCARCH" = "x86" ]; then
+	for i in $SYNC_CHECK_FILES; do
+		check $i '-I "^.*\/\*.*__ignore_sync_check__.*\*\/.*$"'
+	done
+fi
tools/perf/check-headers.sh +11 -4
index dded93a2bc89..07857dfb4d91 100755
@@ -75,6 +75,13 @@ include/uapi/asm-generic/mman-common.h
 include/uapi/asm-generic/unistd.h
 '
 
+SYNC_CHECK_FILES='
+arch/x86/include/asm/inat.h
+arch/x86/include/asm/insn.h
+arch/x86/lib/inat.c
+arch/x86/lib/insn.c
+'
+
 # These copies are under tools/perf/trace/beauty/ as they are not used to in
 # building object files only by scripts in tools/perf/trace/beauty/ to generate
 # tables that then gets included in .c files for things like id->string syscall
@@ -129,6 +136,10 @@ for i in $FILES; do
   check $i -B
 done
 
+for i in $SYNC_CHECK_FILES; do
+  check $i '-I "^.*\/\*.*__ignore_sync_check__.*\*\/.*$"'
+done
+
 # diff with extra ignore lines
 check arch/x86/lib/memcpy_64.S        '-I "^EXPORT_SYMBOL" -I "^#include <asm/export.h>" -I"^SYM_FUNC_START\(_LOCAL\)*(memcpy_\(erms\|orig\))"'
 check arch/x86/lib/memset_64.S        '-I "^EXPORT_SYMBOL" -I "^#include <asm/export.h>" -I"^SYM_FUNC_START\(_LOCAL\)*(memset_\(erms\|orig\))"'
@@ -137,10 +148,6 @@ check include/uapi/linux/mman.h       '-I "^#include <\(uapi/\)*asm/mman.h>"'
 check include/linux/build_bug.h       '-I "^#\(ifndef\|endif\)\( \/\/\)* static_assert$"'
 check include/linux/ctype.h	      '-I "isdigit("'
 check lib/ctype.c		      '-I "^EXPORT_SYMBOL" -I "^#include <linux/export.h>" -B'
-check arch/x86/include/asm/inat.h     '-I "^#include [\"<]\(asm/\)*inat_types.h[\">]"'
-check arch/x86/include/asm/insn.h     '-I "^#include [\"<]\(asm/\)*inat.h[\">]"'
-check arch/x86/lib/inat.c	      '-I "^#include [\"<]\(../include/\)*asm/insn.h[\">]"'
-check arch/x86/lib/insn.c             '-I "^#include [\"<]\(../include/\)*asm/in\(at\|sn\).h[\">]" -I "^#include [\"<]\(../include/\)*asm/emulate_prefix.h[\">]"'
 
 # diff non-symmetric files
 check_2 tools/perf/arch/x86/entry/syscalls/syscall_64.tbl arch/x86/entry/syscalls/syscall_64.tbl
Signature · 1 line
2.29.2
maintainer Reviewed-by on v3 3/21
#6 lore raw

Re: [PATCH v3 03/21] x86/insn: Add a __ignore_sync_check__ marker

Quoted 7 lines · On Thu, 4 Mar 2021 18:42:19 +0100 Borislav Petkov <[email protected]> wrote:

From: Borislav Petkov <[email protected]>

Add an explicit __ignore_sync_check__ marker which will be used to mark lines which are supposed to be ignored by file synchronization check scripts, its advantage being that it explicitly denotes such lines in the code.

Looks good to me.

Thanks,

Quoted 204 lines
---
 arch/x86/include/asm/inat.h       |  2 +-
 arch/x86/include/asm/insn.h       |  2 +-
 arch/x86/lib/inat.c               |  2 +-
 arch/x86/lib/insn.c               |  6 +++---
 tools/arch/x86/include/asm/inat.h |  2 +-
 tools/arch/x86/include/asm/insn.h |  2 +-
 tools/arch/x86/lib/inat.c         |  2 +-
 tools/arch/x86/lib/insn.c         |  6 +++---
 tools/objtool/sync-check.sh       | 17 +++++++++++++----
 tools/perf/check-headers.sh       | 15 +++++++++++----
 10 files changed, 36 insertions(+), 20 deletions(-)
arch/x86/include/asm/inat.h +1 -1
index 4cf2ad521f65..b56c5741581a 100644
@@ -6,7 +6,7 @@
  *
  * Written by Masami Hiramatsu <[email protected]>
  */
-#include <asm/inat_types.h>
+#include <asm/inat_types.h> /* __ignore_sync_check__ */
 
 /*
  * Internal bits. Don't use bitmasks directly, because these bits are
arch/x86/include/asm/insn.h +1 -1
index 95a448fbb44c..93f84600ac2c 100644
@@ -9,7 +9,7 @@
 
 #include <asm/byteorder.h>
 /* insn_attr_t is defined in inat.h */
-#include <asm/inat.h>
+#include <asm/inat.h> /* __ignore_sync_check__ */
 
 #if defined(__BYTE_ORDER) ? __BYTE_ORDER == __LITTLE_ENDIAN : defined(__LITTLE_ENDIAN)
 
arch/x86/lib/inat.c +1 -1
index 12539fca75c4..b0f3b2a62ae2 100644
@@ -4,7 +4,7 @@
  *
  * Written by Masami Hiramatsu <[email protected]>
  */
-#include <asm/insn.h>
+#include <asm/insn.h> /* __ignore_sync_check__ */
 
 /* Attribute tables are generated from opcode map */
 #include "inat-tables.c"
arch/x86/lib/insn.c +3 -3
index 4d1640dc7882..ed1e0335aa14 100644
@@ -11,10 +11,10 @@
 #else
 #include <string.h>
 #endif
-#include <asm/inat.h>
-#include <asm/insn.h>
+#include <asm/inat.h> /*__ignore_sync_check__ */
+#include <asm/insn.h> /* __ignore_sync_check__ */
 
-#include <asm/emulate_prefix.h>
+#include <asm/emulate_prefix.h> /* __ignore_sync_check__ */
 
 #define leXX_to_cpu(t, r)						\
 ({									\
tools/arch/x86/include/asm/inat.h +1 -1
index 877827b7c2c3..a61051400311 100644
@@ -6,7 +6,7 @@
  *
  * Written by Masami Hiramatsu <[email protected]>
  */
-#include "inat_types.h"
+#include "inat_types.h" /* __ignore_sync_check__ */
 
 /*
  * Internal bits. Don't use bitmasks directly, because these bits are
tools/arch/x86/include/asm/insn.h +1 -1
index cc777c185212..7e1239aa7dd9 100644
@@ -9,7 +9,7 @@
 
 #include <asm/byteorder.h>
 /* insn_attr_t is defined in inat.h */
-#include "inat.h"
+#include "inat.h" /* __ignore_sync_check__ */
 
 #if defined(__BYTE_ORDER) ? __BYTE_ORDER == __LITTLE_ENDIAN : defined(__LITTLE_ENDIAN)
 
tools/arch/x86/lib/inat.c +1 -1
index 4f5ed49e1b4e..dfbcc6405941 100644
@@ -4,7 +4,7 @@
  *
  * Written by Masami Hiramatsu <[email protected]>
  */
-#include "../include/asm/insn.h"
+#include "../include/asm/insn.h" /* __ignore_sync_check__ */
 
 /* Attribute tables are generated from opcode map */
 #include "inat-tables.c"
tools/arch/x86/lib/insn.c +3 -3
index 31afbf0a75f7..1174c43b3944 100644
@@ -11,10 +11,10 @@
 #else
 #include <string.h>
 #endif
-#include "../include/asm/inat.h"
-#include "../include/asm/insn.h"
+#include "../include/asm/inat.h" /* __ignore_sync_check__ */
+#include "../include/asm/insn.h" /* __ignore_sync_check__ */
 
-#include "../include/asm/emulate_prefix.h"
+#include "../include/asm/emulate_prefix.h" /* __ignore_sync_check__ */
 
 #define leXX_to_cpu(t, r)						\
 ({									\
tools/objtool/sync-check.sh +13 -4
index 606a4b5e929f..4bbabaecab14 100755
@@ -16,11 +16,14 @@ arch/x86/include/asm/emulate_prefix.h
 arch/x86/lib/x86-opcode-map.txt
 arch/x86/tools/gen-insn-attr-x86.awk
 include/linux/static_call_types.h
-arch/x86/include/asm/inat.h     -I '^#include [\"<]\(asm/\)*inat_types.h[\">]'
-arch/x86/include/asm/insn.h     -I '^#include [\"<]\(asm/\)*inat.h[\">]'
-arch/x86/lib/inat.c             -I '^#include [\"<]\(../include/\)*asm/insn.h[\">]'
-arch/x86/lib/insn.c             -I '^#include [\"<]\(../include/\)*asm/in\(at\|sn\).h[\">]' -I '^#include [\"<]\(../include/\)*asm/emulate_prefix.h[\">]'
 "
+
+SYNC_CHECK_FILES='
+arch/x86/include/asm/inat.h
+arch/x86/include/asm/insn.h
+arch/x86/lib/inat.c
+arch/x86/lib/insn.c
+'
 fi
 
 check_2 () {
@@ -63,3 +66,9 @@ while read -r file_entry; do
 done <<EOF
 $FILES
 EOF
+
+if [ "$SRCARCH" = "x86" ]; then
+	for i in $SYNC_CHECK_FILES; do
+		check $i '-I "^.*\/\*.*__ignore_sync_check__.*\*\/.*$"'
+	done
+fi
tools/perf/check-headers.sh +11 -4
index dded93a2bc89..07857dfb4d91 100755
@@ -75,6 +75,13 @@ include/uapi/asm-generic/mman-common.h
 include/uapi/asm-generic/unistd.h
 '
 
+SYNC_CHECK_FILES='
+arch/x86/include/asm/inat.h
+arch/x86/include/asm/insn.h
+arch/x86/lib/inat.c
+arch/x86/lib/insn.c
+'
+
 # These copies are under tools/perf/trace/beauty/ as they are not used to in
 # building object files only by scripts in tools/perf/trace/beauty/ to generate
 # tables that then gets included in .c files for things like id->string syscall
@@ -129,6 +136,10 @@ for i in $FILES; do
   check $i -B
 done
 
+for i in $SYNC_CHECK_FILES; do
+  check $i '-I "^.*\/\*.*__ignore_sync_check__.*\*\/.*$"'
+done
+
 # diff with extra ignore lines
 check arch/x86/lib/memcpy_64.S        '-I "^EXPORT_SYMBOL" -I "^#include <asm/export.h>" -I"^SYM_FUNC_START\(_LOCAL\)*(memcpy_\(erms\|orig\))"'
 check arch/x86/lib/memset_64.S        '-I "^EXPORT_SYMBOL" -I "^#include <asm/export.h>" -I"^SYM_FUNC_START\(_LOCAL\)*(memset_\(erms\|orig\))"'
@@ -137,10 +148,6 @@ check include/uapi/linux/mman.h       '-I "^#include <\(uapi/\)*asm/mman.h>"'
 check include/linux/build_bug.h       '-I "^#\(ifndef\|endif\)\( \/\/\)* static_assert$"'
 check include/linux/ctype.h	      '-I "isdigit("'
 check lib/ctype.c		      '-I "^EXPORT_SYMBOL" -I "^#include <linux/export.h>" -B'
-check arch/x86/include/asm/inat.h     '-I "^#include [\"<]\(asm/\)*inat_types.h[\">]"'
-check arch/x86/include/asm/insn.h     '-I "^#include [\"<]\(asm/\)*inat.h[\">]"'
-check arch/x86/lib/inat.c	      '-I "^#include [\"<]\(../include/\)*asm/insn.h[\">]"'
-check arch/x86/lib/insn.c             '-I "^#include [\"<]\(../include/\)*asm/in\(at\|sn\).h[\">]" -I "^#include [\"<]\(../include/\)*asm/emulate_prefix.h[\">]"'
 
 # diff non-symmetric files
 check_2 tools/perf/arch/x86/entry/syscalls/syscall_64.tbl arch/x86/entry/syscalls/syscall_64.tbl
Signature · 1 line
2.29.2
Signature · 1 line
Masami Hiramatsu <[email protected]>
#7 lore raw

[tip: x86/core] x86/insn: Add a __ignore_sync_check__ marker

In reply to Borislav Petkov
The following commit has been merged into the x86/core branch of tip:

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     d30c7b820be5c4777fe6c3b0c21f9d0064251e51
Gitweb:        https://git.kernel.org/tip/d30c7b820be5c4777fe6c3b0c21f9d0064251e51
Author:        Borislav Petkov <[email protected]>
AuthorDate:    Mon, 22 Feb 2021 13:34:40 +01:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Mon, 15 Mar 2021 11:00:57 +01:00

x86/insn: Add a __ignore_sync_check__ marker

Add an explicit __ignore_sync_check__ marker which will be used to mark lines which are supposed to be ignored by file synchronization check scripts, its advantage being that it explicitly denotes such lines in the code.

---
 arch/x86/include/asm/inat.h       |  2 +-
 arch/x86/include/asm/insn.h       |  2 +-
 arch/x86/lib/inat.c               |  2 +-
 arch/x86/lib/insn.c               |  6 +++---
 tools/arch/x86/include/asm/inat.h |  2 +-
 tools/arch/x86/include/asm/insn.h |  2 +-
 tools/arch/x86/lib/inat.c         |  2 +-
 tools/arch/x86/lib/insn.c         |  6 +++---
 tools/objtool/sync-check.sh       | 17 +++++++++++++----
 tools/perf/check-headers.sh       | 15 +++++++++++----
 10 files changed, 36 insertions(+), 20 deletions(-)
arch/x86/include/asm/inat.h +1 -1
index 4cf2ad5..b56c574 100644
@@ -6,7 +6,7 @@
  *
  * Written by Masami Hiramatsu <[email protected]>
  */
-#include <asm/inat_types.h>
+#include <asm/inat_types.h> /* __ignore_sync_check__ */
 
 /*
  * Internal bits. Don't use bitmasks directly, because these bits are
arch/x86/include/asm/insn.h +1 -1
index 95a448f..93f8460 100644
@@ -9,7 +9,7 @@
 
 #include <asm/byteorder.h>
 /* insn_attr_t is defined in inat.h */
-#include <asm/inat.h>
+#include <asm/inat.h> /* __ignore_sync_check__ */
 
 #if defined(__BYTE_ORDER) ? __BYTE_ORDER == __LITTLE_ENDIAN : defined(__LITTLE_ENDIAN)
 
arch/x86/lib/inat.c +1 -1
index 12539fc..b0f3b2a 100644
@@ -4,7 +4,7 @@
  *
  * Written by Masami Hiramatsu <[email protected]>
  */
-#include <asm/insn.h>
+#include <asm/insn.h> /* __ignore_sync_check__ */
 
 /* Attribute tables are generated from opcode map */
 #include "inat-tables.c"
arch/x86/lib/insn.c +3 -3
index 4d1640d..ed1e033 100644
@@ -11,10 +11,10 @@
 #else
 #include <string.h>
 #endif
-#include <asm/inat.h>
-#include <asm/insn.h>
+#include <asm/inat.h> /*__ignore_sync_check__ */
+#include <asm/insn.h> /* __ignore_sync_check__ */
 
-#include <asm/emulate_prefix.h>
+#include <asm/emulate_prefix.h> /* __ignore_sync_check__ */
 
 #define leXX_to_cpu(t, r)						\
 ({									\
tools/arch/x86/include/asm/inat.h +1 -1
index 877827b..a610514 100644
@@ -6,7 +6,7 @@
  *
  * Written by Masami Hiramatsu <[email protected]>
  */
-#include "inat_types.h"
+#include "inat_types.h" /* __ignore_sync_check__ */
 
 /*
  * Internal bits. Don't use bitmasks directly, because these bits are
tools/arch/x86/include/asm/insn.h +1 -1
index cc777c1..7e1239a 100644
@@ -9,7 +9,7 @@
 
 #include <asm/byteorder.h>
 /* insn_attr_t is defined in inat.h */
-#include "inat.h"
+#include "inat.h" /* __ignore_sync_check__ */
 
 #if defined(__BYTE_ORDER) ? __BYTE_ORDER == __LITTLE_ENDIAN : defined(__LITTLE_ENDIAN)
 
tools/arch/x86/lib/inat.c +1 -1
index 4f5ed49..dfbcc64 100644
@@ -4,7 +4,7 @@
  *
  * Written by Masami Hiramatsu <[email protected]>
  */
-#include "../include/asm/insn.h"
+#include "../include/asm/insn.h" /* __ignore_sync_check__ */
 
 /* Attribute tables are generated from opcode map */
 #include "inat-tables.c"
tools/arch/x86/lib/insn.c +3 -3
index 31afbf0..1174c43 100644
@@ -11,10 +11,10 @@
 #else
 #include <string.h>
 #endif
-#include "../include/asm/inat.h"
-#include "../include/asm/insn.h"
+#include "../include/asm/inat.h" /* __ignore_sync_check__ */
+#include "../include/asm/insn.h" /* __ignore_sync_check__ */
 
-#include "../include/asm/emulate_prefix.h"
+#include "../include/asm/emulate_prefix.h" /* __ignore_sync_check__ */
 
 #define leXX_to_cpu(t, r)						\
 ({									\
tools/objtool/sync-check.sh +13 -4
index 606a4b5..4bbabae 100755
@@ -16,11 +16,14 @@ arch/x86/include/asm/emulate_prefix.h
 arch/x86/lib/x86-opcode-map.txt
 arch/x86/tools/gen-insn-attr-x86.awk
 include/linux/static_call_types.h
-arch/x86/include/asm/inat.h     -I '^#include [\"<]\(asm/\)*inat_types.h[\">]'
-arch/x86/include/asm/insn.h     -I '^#include [\"<]\(asm/\)*inat.h[\">]'
-arch/x86/lib/inat.c             -I '^#include [\"<]\(../include/\)*asm/insn.h[\">]'
-arch/x86/lib/insn.c             -I '^#include [\"<]\(../include/\)*asm/in\(at\|sn\).h[\">]' -I '^#include [\"<]\(../include/\)*asm/emulate_prefix.h[\">]'
 "
+
+SYNC_CHECK_FILES='
+arch/x86/include/asm/inat.h
+arch/x86/include/asm/insn.h
+arch/x86/lib/inat.c
+arch/x86/lib/insn.c
+'
 fi
 
 check_2 () {
@@ -63,3 +66,9 @@ while read -r file_entry; do
 done <<EOF
 $FILES
 EOF
+
+if [ "$SRCARCH" = "x86" ]; then
+	for i in $SYNC_CHECK_FILES; do
+		check $i '-I "^.*\/\*.*__ignore_sync_check__.*\*\/.*$"'
+	done
+fi
tools/perf/check-headers.sh +11 -4
index dded93a..07857df 100755
@@ -75,6 +75,13 @@ include/uapi/asm-generic/mman-common.h
 include/uapi/asm-generic/unistd.h
 '
 
+SYNC_CHECK_FILES='
+arch/x86/include/asm/inat.h
+arch/x86/include/asm/insn.h
+arch/x86/lib/inat.c
+arch/x86/lib/insn.c
+'
+
 # These copies are under tools/perf/trace/beauty/ as they are not used to in
 # building object files only by scripts in tools/perf/trace/beauty/ to generate
 # tables that then gets included in .c files for things like id->string syscall
@@ -129,6 +136,10 @@ for i in $FILES; do
   check $i -B
 done
 
+for i in $SYNC_CHECK_FILES; do
+  check $i '-I "^.*\/\*.*__ignore_sync_check__.*\*\/.*$"'
+done
+
 # diff with extra ignore lines
 check arch/x86/lib/memcpy_64.S        '-I "^EXPORT_SYMBOL" -I "^#include <asm/export.h>" -I"^SYM_FUNC_START\(_LOCAL\)*(memcpy_\(erms\|orig\))"'
 check arch/x86/lib/memset_64.S        '-I "^EXPORT_SYMBOL" -I "^#include <asm/export.h>" -I"^SYM_FUNC_START\(_LOCAL\)*(memset_\(erms\|orig\))"'
@@ -137,10 +148,6 @@ check include/uapi/linux/mman.h       '-I "^#include <\(uapi/\)*asm/mman.h>"'
 check include/linux/build_bug.h       '-I "^#\(ifndef\|endif\)\( \/\/\)* static_assert$"'
 check include/linux/ctype.h	      '-I "isdigit("'
 check lib/ctype.c		      '-I "^EXPORT_SYMBOL" -I "^#include <linux/export.h>" -B'
-check arch/x86/include/asm/inat.h     '-I "^#include [\"<]\(asm/\)*inat_types.h[\">]"'
-check arch/x86/include/asm/insn.h     '-I "^#include [\"<]\(asm/\)*inat.h[\">]"'
-check arch/x86/lib/inat.c	      '-I "^#include [\"<]\(../include/\)*asm/insn.h[\">]"'
-check arch/x86/lib/insn.c             '-I "^#include [\"<]\(../include/\)*asm/in\(at\|sn\).h[\">]" -I "^#include [\"<]\(../include/\)*asm/emulate_prefix.h[\">]"'
 
 # diff non-symmetric files
 check_2 tools/perf/arch/x86/entry/syscalls/syscall_64.tbl arch/x86/entry/syscalls/syscall_64.tbl
author maintainer
#8 lore raw

PATCH v3 4/21 x86/insn: Add an insn_decode() API

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

Users of the instruction decoder should use this to decode instruction bytes. For that, have insn*() helpers return an int value to denote success/failure. When there's an error fetching the next insn byte and the insn falls short, return -ENODATA to denote that.

While at it, make insn_get_opcode() more stricter as to whether what has seen so far is a valid insn and if not.

Copy linux/kconfig.h for the tools-version of the decoder so that it can use IS_ENABLED().

Also, cast the INSN_MODE_KERN dummy define value to (enum insn_mode) for tools use of the decoder because perf tool builds with -Werror and errors out with -Werror=sign-compare otherwise.

---
 arch/x86/include/asm/insn.h       |  24 +++-
 arch/x86/lib/insn.c               | 216 +++++++++++++++++++++++------
 tools/arch/x86/include/asm/insn.h |  24 +++-
 tools/arch/x86/lib/insn.c         | 222 +++++++++++++++++++++++-------
 tools/include/linux/kconfig.h     |  73 ++++++++++
 5 files changed, 452 insertions(+), 107 deletions(-)
 create mode 100644 tools/include/linux/kconfig.h
arch/x86/include/asm/insn.h +17 -7
index 93f84600ac2c..de9fe760c1e7 100644
@@ -132,13 +132,23 @@ struct insn {
 #define X86_VEX_M_MAX	0x1f			/* VEX3.M Maximum value */
 
 extern void insn_init(struct insn *insn, const void *kaddr, int buf_len, int x86_64);
-extern void insn_get_prefixes(struct insn *insn);
-extern void insn_get_opcode(struct insn *insn);
-extern void insn_get_modrm(struct insn *insn);
-extern void insn_get_sib(struct insn *insn);
-extern void insn_get_displacement(struct insn *insn);
-extern void insn_get_immediate(struct insn *insn);
-extern void insn_get_length(struct insn *insn);
+extern int insn_get_prefixes(struct insn *insn);
+extern int insn_get_opcode(struct insn *insn);
+extern int insn_get_modrm(struct insn *insn);
+extern int insn_get_sib(struct insn *insn);
+extern int insn_get_displacement(struct insn *insn);
+extern int insn_get_immediate(struct insn *insn);
+extern int insn_get_length(struct insn *insn);
+
+enum insn_mode {
+	INSN_MODE_32,
+	INSN_MODE_64,
+	/* Mode is determined by the current kernel build. */
+	INSN_MODE_KERN,
+	INSN_NUM_MODES,
+};
+
+extern int insn_decode(struct insn *insn, const void *kaddr, int buf_len, enum insn_mode m);
 
 /* Attribute will be determined after getting ModRM (for opcode groups) */
 static inline void insn_get_attribute(struct insn *insn)
arch/x86/lib/insn.c +171 -45

370 lines not on this page · raw message

tools/arch/x86/include/asm/insn.h +17 -7
index 7e1239aa7dd9..2fe19b1a8765 100644
@@ -132,13 +132,23 @@ struct insn {
 #define X86_VEX_M_MAX	0x1f			/* VEX3.M Maximum value */
 
 extern void insn_init(struct insn *insn, const void *kaddr, int buf_len, int x86_64);
-extern void insn_get_prefixes(struct insn *insn);
-extern void insn_get_opcode(struct insn *insn);
-extern void insn_get_modrm(struct insn *insn);
-extern void insn_get_sib(struct insn *insn);
-extern void insn_get_displacement(struct insn *insn);
-extern void insn_get_immediate(struct insn *insn);
-extern void insn_get_length(struct insn *insn);
+extern int insn_get_prefixes(struct insn *insn);
+extern int insn_get_opcode(struct insn *insn);
+extern int insn_get_modrm(struct insn *insn);
+extern int insn_get_sib(struct insn *insn);
+extern int insn_get_displacement(struct insn *insn);
+extern int insn_get_immediate(struct insn *insn);
+extern int insn_get_length(struct insn *insn);
+
+enum insn_mode {
+	INSN_MODE_32,
+	INSN_MODE_64,
+	/* Mode is determined by the current kernel build. */
+	INSN_MODE_KERN,
+	INSN_NUM_MODES,
+};
+
+extern int insn_decode(struct insn *insn, const void *kaddr, int buf_len, enum insn_mode m);
 
 /* Attribute will be determined after getting ModRM (for opcode groups) */
 static inline void insn_get_attribute(struct insn *insn)
tools/arch/x86/lib/insn.c +174 -48

377 lines not on this page · raw message

tools/include/linux/kconfig.h new file +73 -0

73 lines not on this page · raw message

Signature · 1 line
2.29.2
#9 lore raw
The following commit has been merged into the x86/core branch of tip:

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     93281c4a96572a34504244969b938e035204778d
Gitweb:        https://git.kernel.org/tip/93281c4a96572a34504244969b938e035204778d
Author:        Borislav Petkov <[email protected]>
AuthorDate:    Tue, 03 Nov 2020 17:28:30 +01:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Mon, 15 Mar 2021 11:05:47 +01:00

x86/insn: Add an insn_decode() API

Users of the instruction decoder should use this to decode instruction bytes. For that, have insn*() helpers return an int value to denote success/failure. When there's an error fetching the next insn byte and the insn falls short, return -ENODATA to denote that.

While at it, make insn_get_opcode() more stricter as to whether what has seen so far is a valid insn and if not.

Copy linux/kconfig.h for the tools-version of the decoder so that it can use IS_ENABLED().

Also, cast the INSN_MODE_KERN dummy define value to (enum insn_mode) for tools use of the decoder because perf tool builds with -Werror and errors out with -Werror=sign-compare otherwise.

---
 arch/x86/include/asm/insn.h       |  24 ++-
 arch/x86/lib/insn.c               | 216 ++++++++++++++++++++++------
 tools/arch/x86/include/asm/insn.h |  24 ++-
 tools/arch/x86/lib/insn.c         | 222 ++++++++++++++++++++++-------
 tools/include/linux/kconfig.h     |  73 ++++++++++-
 5 files changed, 452 insertions(+), 107 deletions(-)
 create mode 100644 tools/include/linux/kconfig.h
arch/x86/include/asm/insn.h +17 -7
index 93f8460..de9fe76 100644
@@ -132,13 +132,23 @@ struct insn {
 #define X86_VEX_M_MAX	0x1f			/* VEX3.M Maximum value */
 
 extern void insn_init(struct insn *insn, const void *kaddr, int buf_len, int x86_64);
-extern void insn_get_prefixes(struct insn *insn);
-extern void insn_get_opcode(struct insn *insn);
-extern void insn_get_modrm(struct insn *insn);
-extern void insn_get_sib(struct insn *insn);
-extern void insn_get_displacement(struct insn *insn);
-extern void insn_get_immediate(struct insn *insn);
-extern void insn_get_length(struct insn *insn);
+extern int insn_get_prefixes(struct insn *insn);
+extern int insn_get_opcode(struct insn *insn);
+extern int insn_get_modrm(struct insn *insn);
+extern int insn_get_sib(struct insn *insn);
+extern int insn_get_displacement(struct insn *insn);
+extern int insn_get_immediate(struct insn *insn);
+extern int insn_get_length(struct insn *insn);
+
+enum insn_mode {
+	INSN_MODE_32,
+	INSN_MODE_64,
+	/* Mode is determined by the current kernel build. */
+	INSN_MODE_KERN,
+	INSN_NUM_MODES,
+};
+
+extern int insn_decode(struct insn *insn, const void *kaddr, int buf_len, enum insn_mode m);
 
 /* Attribute will be determined after getting ModRM (for opcode groups) */
 static inline void insn_get_attribute(struct insn *insn)
arch/x86/lib/insn.c +171 -45

370 lines not on this page · raw message

tools/arch/x86/include/asm/insn.h +17 -7
index 7e1239a..2fe19b1 100644
@@ -132,13 +132,23 @@ struct insn {
 #define X86_VEX_M_MAX	0x1f			/* VEX3.M Maximum value */
 
 extern void insn_init(struct insn *insn, const void *kaddr, int buf_len, int x86_64);
-extern void insn_get_prefixes(struct insn *insn);
-extern void insn_get_opcode(struct insn *insn);
-extern void insn_get_modrm(struct insn *insn);
-extern void insn_get_sib(struct insn *insn);
-extern void insn_get_displacement(struct insn *insn);
-extern void insn_get_immediate(struct insn *insn);
-extern void insn_get_length(struct insn *insn);
+extern int insn_get_prefixes(struct insn *insn);
+extern int insn_get_opcode(struct insn *insn);
+extern int insn_get_modrm(struct insn *insn);
+extern int insn_get_sib(struct insn *insn);
+extern int insn_get_displacement(struct insn *insn);
+extern int insn_get_immediate(struct insn *insn);
+extern int insn_get_length(struct insn *insn);
+
+enum insn_mode {
+	INSN_MODE_32,
+	INSN_MODE_64,
+	/* Mode is determined by the current kernel build. */
+	INSN_MODE_KERN,
+	INSN_NUM_MODES,
+};
+
+extern int insn_decode(struct insn *insn, const void *kaddr, int buf_len, enum insn_mode m);
 
 /* Attribute will be determined after getting ModRM (for opcode groups) */
 static inline void insn_get_attribute(struct insn *insn)
tools/arch/x86/lib/insn.c +174 -48

377 lines not on this page · raw message

tools/include/linux/kconfig.h new file +73 -0

73 lines not on this page · raw message

maintainer
#10 lore raw
Quoted 1 line · On Mon, Mar 15, 2021 at 03:47:48PM -0000, tip-bot2 for Borislav Petkov wrote:

x86/insn: Add an insn_decode() API

Seeing as how I'm a lazy sod, does we want something like so?

arch/x86/include/asm/insn.h +2 -0
@@ -150,6 +150,8 @@ enum insn_mode {
 
 extern int insn_decode(struct insn *insn, const void *kaddr, int buf_len, enum insn_mode m);
 
+#define insn_decode_kernel(_insn, _ptr) insn_decode((_insn), (_ptr), MAX_INSN_SIZE, INSN_MODE_KERN)
+
 /* Attribute will be determined after getting ModRM (for opcode groups) */
 static inline void insn_get_attribute(struct insn *insn)
 {
arch/x86/kernel/alternative.c +1 -1
@@ -1333,7 +1333,7 @@ static void text_poke_loc_init(struct te
 	if (!emulate)
 		emulate = opcode;
 
-	ret = insn_decode(&insn, emulate, MAX_INSN_SIZE, INSN_MODE_KERN);
+	ret = insn_decode_kernel(&insn, emulate);
 
 	BUG_ON(ret < 0);
 	BUG_ON(len != insn.length);
arch/x86/kernel/cpu/mce/severity.c +1 -1
@@ -225,7 +225,7 @@ static bool is_copy_from_user(struct pt_
 	if (copy_from_kernel_nofault(insn_buf, (void *)regs->ip, MAX_INSN_SIZE))
 		return false;
 
-	ret = insn_decode(&insn, insn_buf, MAX_INSN_SIZE, INSN_MODE_KERN);
+	ret = insn_decode_kernel(&insn, insn_buf);
 	if (ret < 0)
 		return false;
 
arch/x86/kernel/kprobes/core.c +2 -2
@@ -279,7 +279,7 @@ static int can_probe(unsigned long paddr
 		if (!__addr)
 			return 0;
 
-		ret = insn_decode(&insn, (void *)__addr, MAX_INSN_SIZE, INSN_MODE_KERN);
+		ret = insn_decode_kernel(&insn, (void *)__addr);
 		if (ret < 0)
 			return 0;
 
@@ -316,7 +316,7 @@ int __copy_instruction(u8 *dest, u8 *src
 			MAX_INSN_SIZE))
 		return 0;
 
-	ret = insn_decode(insn, dest, MAX_INSN_SIZE, INSN_MODE_KERN);
+	ret = insn_decode_kernel(insn, dest);
 	if (ret < 0)
 		return 0;
 
arch/x86/kernel/kprobes/opt.c +1 -1
@@ -324,7 +324,7 @@ static int can_optimize(unsigned long pa
 		if (!recovered_insn)
 			return 0;
 
-		ret = insn_decode(&insn, (void *)recovered_insn, MAX_INSN_SIZE, INSN_MODE_KERN);
+		ret = insn_decode_kernel(&insn, (void *)recovered_insn);
 		if (ret < 0)
 			return 0;
 
arch/x86/kernel/traps.c +1 -1
@@ -504,7 +504,7 @@ static enum kernel_gp_hint get_kernel_gp
 			MAX_INSN_SIZE))
 		return GP_NO_HINT;
 
-	ret = insn_decode(&insn, insn_buf, MAX_INSN_SIZE, INSN_MODE_KERN);
+	ret = insn_decode_kernel(&insn, insn_buf);
 	if (ret < 0)
 		return GP_NO_HINT;
 
reviewer
#11 lore raw
In reply to Borislav Petkov
Quoted 495 lines · Borislav Petkov

From: Borislav Petkov <[email protected]>

Users of the instruction decoder should use this to decode instruction bytes. For that, have insn*() helpers return an int value to denote success/failure. When there's an error fetching the next insn byte and the insn falls short, return -ENODATA to denote that.

While at it, make insn_get_opcode() more stricter as to whether what has seen so far is a valid insn and if not.

Copy linux/kconfig.h for the tools-version of the decoder so that it can use IS_ENABLED().

Also, cast the INSN_MODE_KERN dummy define value to (enum insn_mode) for tools use of the decoder because perf tool builds with -Werror and errors out with -Werror=sign-compare otherwise.

---
 arch/x86/include/asm/insn.h       |  24 +++-
 arch/x86/lib/insn.c               | 216 +++++++++++++++++++++++------
 tools/arch/x86/include/asm/insn.h |  24 +++-
 tools/arch/x86/lib/insn.c         | 222 +++++++++++++++++++++++-------
 tools/include/linux/kconfig.h     |  73 ++++++++++
 5 files changed, 452 insertions(+), 107 deletions(-)
 create mode 100644 tools/include/linux/kconfig.h
arch/x86/include/asm/insn.h +17 -7
index 93f84600ac2c..de9fe760c1e7 100644
@@ -132,13 +132,23 @@ struct insn {
 #define X86_VEX_M_MAX  0x1f                    /* VEX3.M Maximum value */
 
 extern void insn_init(struct insn *insn, const void *kaddr, int buf_len, int x86_64);
-extern void insn_get_prefixes(struct insn *insn);
-extern void insn_get_opcode(struct insn *insn);
-extern void insn_get_modrm(struct insn *insn);
-extern void insn_get_sib(struct insn *insn);
-extern void insn_get_displacement(struct insn *insn);
-extern void insn_get_immediate(struct insn *insn);
-extern void insn_get_length(struct insn *insn);
+extern int insn_get_prefixes(struct insn *insn);
+extern int insn_get_opcode(struct insn *insn);
+extern int insn_get_modrm(struct insn *insn);
+extern int insn_get_sib(struct insn *insn);
+extern int insn_get_displacement(struct insn *insn);
+extern int insn_get_immediate(struct insn *insn);
+extern int insn_get_length(struct insn *insn);
+
+enum insn_mode {
+       INSN_MODE_32,
+       INSN_MODE_64,
+       /* Mode is determined by the current kernel build. */
+       INSN_MODE_KERN,
+       INSN_NUM_MODES,
+};
+
+extern int insn_decode(struct insn *insn, const void *kaddr, int buf_len, enum insn_mode m);
 
 /* Attribute will be determined after getting ModRM (for opcode groups) */
 static inline void insn_get_attribute(struct insn *insn)
arch/x86/lib/insn.c +171 -45
index ed1e0335aa14..bb58004497f8 100644
@@ -14,6 +14,9 @@
 #include <asm/inat.h> /*__ignore_sync_check__ */
 #include <asm/insn.h> /* __ignore_sync_check__ */
 
+#include <linux/errno.h>
+#include <linux/kconfig.h>
+
 #include <asm/emulate_prefix.h> /* __ignore_sync_check__ */
 
 #define leXX_to_cpu(t, r)                                              \
@@ -112,8 +115,12 @@ static void insn_get_emulate_prefix(struct insn *insn)
  * Populates the @insn->prefixes bitmap, and updates @insn->next_byte
  * to point to the (first) opcode.  No effect if @insn->prefixes.got
  * is already set.
+ *
+ * * Returns:
+ * 0:  on success
+ * < 0: on error
  */
-void insn_get_prefixes(struct insn *insn)
+int insn_get_prefixes(struct insn *insn)
 {
        struct insn_field *prefixes = &insn->prefixes;
        insn_attr_t attr;
@@ -121,7 +128,7 @@ void insn_get_prefixes(struct insn *insn)
        int i, nb;
 
        if (prefixes->got)
-               return;
+               return 0;
 
        insn_get_emulate_prefix(insn);
 
@@ -231,8 +238,10 @@ void insn_get_prefixes(struct insn *insn)
 
        prefixes->got = 1;
 
+       return 0;
+
 err_out:
-       return;
+       return -ENODATA;
 }
 
 /**
@@ -244,16 +253,25 @@ void insn_get_prefixes(struct insn *insn)
  * If necessary, first collects any preceding (prefix) bytes.
  * Sets @insn->opcode.value = opcode1.  No effect if @insn->opcode.got
  * is already 1.
+ *
+ * Returns:
+ * 0:  on success
+ * < 0: on error
  */
-void insn_get_opcode(struct insn *insn)
+int insn_get_opcode(struct insn *insn)
 {
        struct insn_field *opcode = &insn->opcode;
+       int pfx_id, ret;
        insn_byte_t op;
-       int pfx_id;
+
        if (opcode->got)
-               return;
-       if (!insn->prefixes.got)
-               insn_get_prefixes(insn);
+               return 0;
+
+       if (!insn->prefixes.got) {
+               ret = insn_get_prefixes(insn);
+               if (ret)
+                       return ret;
+       }
 
        /* Get first opcode */
        op = get_next(insn_byte_t, insn);
@@ -268,9 +286,13 @@ void insn_get_opcode(struct insn *insn)
                insn->attr = inat_get_avx_attribute(op, m, p);
                if ((inat_must_evex(insn->attr) && !insn_is_evex(insn)) ||
                    (!inat_accept_vex(insn->attr) &&
-                    !inat_is_group(insn->attr)))
-                       insn->attr = 0; /* This instruction is bad */
-               goto end;       /* VEX has only 1 byte for opcode */
+                    !inat_is_group(insn->attr))) {
+                       /* This instruction is bad */
+                       insn->attr = 0;
+                       return -EINVAL;
+               }
+               /* VEX has only 1 byte for opcode */
+               goto end;
        }
 
        insn->attr = inat_get_opcode_attribute(op);
@@ -281,13 +303,18 @@ void insn_get_opcode(struct insn *insn)
                pfx_id = insn_last_prefix_id(insn);
                insn->attr = inat_get_escape_attribute(op, pfx_id, insn->attr);
        }
-       if (inat_must_vex(insn->attr))
-               insn->attr = 0; /* This instruction is bad */
+
+       if (inat_must_vex(insn->attr)) {
+               /* This instruction is bad */
+               insn->attr = 0;
+               return -EINVAL;
+       }
 end:
        opcode->got = 1;
+       return 0;
 
 err_out:
-       return;
+       return -ENODATA;
 }
 
 /**
@@ -297,15 +324,25 @@ void insn_get_opcode(struct insn *insn)
  * Populates @insn->modrm and updates @insn->next_byte to point past the
  * ModRM byte, if any.  If necessary, first collects the preceding bytes
  * (prefixes and opcode(s)).  No effect if @insn->modrm.got is already 1.
+ *
+ * Returns:
+ * 0:  on success
+ * < 0: on error
  */
-void insn_get_modrm(struct insn *insn)
+int insn_get_modrm(struct insn *insn)
 {
        struct insn_field *modrm = &insn->modrm;
        insn_byte_t pfx_id, mod;
+       int ret;
+
        if (modrm->got)
-               return;
-       if (!insn->opcode.got)
-               insn_get_opcode(insn);
+               return 0;
+
+       if (!insn->opcode.got) {
+               ret = insn_get_opcode(insn);
+               if (ret)
+                       return ret;
+       }
 
        if (inat_has_modrm(insn->attr)) {
                mod = get_next(insn_byte_t, insn);
@@ -314,17 +351,22 @@ void insn_get_modrm(struct insn *insn)
                        pfx_id = insn_last_prefix_id(insn);
                        insn->attr = inat_get_group_attribute(mod, pfx_id,
                                                              insn->attr);
-                       if (insn_is_avx(insn) && !inat_accept_vex(insn->attr))
-                               insn->attr = 0; /* This is bad */
+                       if (insn_is_avx(insn) && !inat_accept_vex(insn->attr)) {
+                               /* Bad insn */
+                               insn->attr = 0;
+                               return -EINVAL;
+                       }
                }
        }
 
        if (insn->x86_64 && inat_is_force64(insn->attr))
                insn->opnd_bytes = 8;
+
        modrm->got = 1;
+       return 0;
 
 err_out:
-       return;
+       return -ENODATA;
 }
 
 
@@ -338,11 +380,16 @@ void insn_get_modrm(struct insn *insn)
 int insn_rip_relative(struct insn *insn)
 {
        struct insn_field *modrm = &insn->modrm;
+       int ret;
 
        if (!insn->x86_64)
                return 0;
-       if (!modrm->got)
-               insn_get_modrm(insn);
+
+       if (!modrm->got) {
+               ret = insn_get_modrm(insn);
+               if (ret)
+                       return 0;
+       }
        /*
         * For rip-relative instructions, the mod field (top 2 bits)
         * is zero and the r/m field (bottom 3 bits) is 0x5.
@@ -356,15 +403,25 @@ int insn_rip_relative(struct insn *insn)
  *
  * If necessary, first collects the instruction up to and including the
  * ModRM byte.
+ *
+ * Returns:
+ * 0: if decoding succeeded
+ * < 0: otherwise.
  */
-void insn_get_sib(struct insn *insn)
+int insn_get_sib(struct insn *insn)
 {
        insn_byte_t modrm;
+       int ret;
 
        if (insn->sib.got)
-               return;
-       if (!insn->modrm.got)
-               insn_get_modrm(insn);
+               return 0;
+
+       if (!insn->modrm.got) {
+               ret = insn_get_modrm(insn);
+               if (ret)
+                       return ret;
+       }
+
        if (insn->modrm.nbytes) {
                modrm = insn->modrm.bytes[0];
                if (insn->addr_bytes != 2 &&
@@ -375,8 +432,10 @@ void insn_get_sib(struct insn *insn)
        }
        insn->sib.got = 1;
 
+       return 0;
+
 err_out:
-       return;
+       return -ENODATA;
 }
 
 
@@ -387,15 +446,25 @@ void insn_get_sib(struct insn *insn)
  * If necessary, first collects the instruction up to and including the
  * SIB byte.
  * Displacement value is sign-expanded.
+ *
+ * * Returns:
+ * 0: if decoding succeeded
+ * < 0: otherwise.
  */
-void insn_get_displacement(struct insn *insn)
+int insn_get_displacement(struct insn *insn)
 {
        insn_byte_t mod, rm, base;
+       int ret;
 
        if (insn->displacement.got)
-               return;
-       if (!insn->sib.got)
-               insn_get_sib(insn);
+               return 0;
+
+       if (!insn->sib.got) {
+               ret = insn_get_sib(insn);
+               if (ret)
+                       return ret;
+       }
+
        if (insn->modrm.nbytes) {
                /*
                 * Interpreting the modrm byte:
@@ -437,9 +506,10 @@ void insn_get_displacement(struct insn *insn)
        }
 out:
        insn->displacement.got = 1;
+       return 0;
 
 err_out:
-       return;
+       return -ENODATA;
 }
 
 /* Decode moffset16/32/64. Return 0 if failed */
@@ -538,20 +608,30 @@ static int __get_immptr(struct insn *insn)
 }
 
 /**
- * insn_get_immediate() - Get the immediates of instruction
+ * insn_get_immediate() - Get the immediate in an instruction
  * @insn:      &struct insn containing instruction
  *
  * If necessary, first collects the instruction up to and including the
  * displacement bytes.
  * Basically, most of immediates are sign-expanded. Unsigned-value can be
- * get by bit masking with ((1 << (nbytes * 8)) - 1)
+ * computed by bit masking with ((1 << (nbytes * 8)) - 1)
+ *
+ * Returns:
+ * 0:  on success
+ * < 0: on error
  */
-void insn_get_immediate(struct insn *insn)
+int insn_get_immediate(struct insn *insn)
 {
+       int ret;
+
        if (insn->immediate.got)
-               return;
-       if (!insn->displacement.got)
-               insn_get_displacement(insn);
+               return 0;
+
+       if (!insn->displacement.got) {
+               ret = insn_get_displacement(insn);
+               if (ret)
+                       return ret;
+       }
 
        if (inat_has_moffset(insn->attr)) {
                if (!__get_moffset(insn))
@@ -598,9 +678,10 @@ void insn_get_immediate(struct insn *insn)
        }
 done:
        insn->immediate.got = 1;
+       return 0;
 
 err_out:
-       return;
+       return -ENODATA;
 }
 
 /**
@@ -609,13 +690,58 @@ void insn_get_immediate(struct insn *insn)
  *
  * If necessary, first collects the instruction up to and including the
  * immediates bytes.
- */
-void insn_get_length(struct insn *insn)
+ *
+ * Returns:
+ *  - 0 on success
+ *  - < 0 on error
+*/
+int insn_get_length(struct insn *insn)
 {
+       int ret;
+
        if (insn->length)
-               return;
-       if (!insn->immediate.got)
-               insn_get_immediate(insn);
+               return 0;
+
+       if (!insn->immediate.got) {
+               ret = insn_get_immediate(insn);
+               if (ret)
+                       return ret;
+       }
+
        insn->length = (unsigned char)((unsigned long)insn->next_byte
                                     - (unsigned long)insn->kaddr);
+
+       return 0;
+}
+
+/**
+ * insn_decode() - Decode an x86 instruction
+ * @insn:      &struct insn to be initialized
+ * @kaddr:     address (in kernel memory) of instruction (or copy thereof)
+ * @buf_len:   length of the insn buffer at @kaddr
+ * @m:         insn mode, see enum insn_mode
+ *
+ * Returns:
+ * 0: if decoding succeeded
+ * < 0: otherwise.
+ */
+int insn_decode(struct insn *insn, const void *kaddr, int buf_len, enum insn_mode m)
+{
+       int ret;
+
+/* #define INSN_MODE_KERN      -1 __ignore_sync_check__ mode is only valid in the kernel */
+
+       if (m == INSN_MODE_KERN)
+               insn_init(insn, kaddr, buf_len, IS_ENABLED(CONFIG_X86_64));
+       else
+               insn_init(insn, kaddr, buf_len, m == INSN_MODE_64);
+
+       ret = insn_get_length(insn);
+       if (ret)
+               return ret;
+
+       if (insn_complete(insn))
+               return 0;
+
+       return -EINVAL;
 }
tools/arch/x86/include/asm/insn.h +17 -7
index 7e1239aa7dd9..2fe19b1a8765 100644
@@ -132,13 +132,23 @@ struct insn {
 #define X86_VEX_M_MAX  0x1f                    /* VEX3.M Maximum value */
 
 extern void insn_init(struct insn *insn, const void *kaddr, int buf_len, int x86_64);
-extern void insn_get_prefixes(struct insn *insn);
-extern void insn_get_opcode(struct insn *insn);
-extern void insn_get_modrm(struct insn *insn);
-extern void insn_get_sib(struct insn *insn);
-extern void insn_get_displacement(struct insn *insn);
-extern void insn_get_immediate(struct insn *insn);
-extern void insn_get_length(struct insn *insn);
+extern int insn_get_prefixes(struct insn *insn);
+extern int insn_get_opcode(struct insn *insn);
+extern int insn_get_modrm(struct insn *insn);
+extern int insn_get_sib(struct insn *insn);
+extern int insn_get_displacement(struct insn *insn);
+extern int insn_get_immediate(struct insn *insn);
+extern int insn_get_length(struct insn *insn);
+
+enum insn_mode {
+       INSN_MODE_32,
+       INSN_MODE_64,
+       /* Mode is determined by the current kernel build. */
+       INSN_MODE_KERN,
+       INSN_NUM_MODES,
+};
+
+extern int insn_decode(struct insn *insn, const void *kaddr, int buf_len, enum insn_mode m);
 
 /* Attribute will be determined after getting ModRM (for opcode groups) */
 static inline void insn_get_attribute(struct insn *insn)
tools/arch/x86/lib/insn.c +0 -0
index 1174c43b3944..be2b057f91d4 100644

tools/arch/x86/lib/insn.c line 15

Quoted 7 lines
tools/arch/x86/lib/insn.c +2 -2
@@ -11,10 +11,13 @@
 #else
 #include <string.h>
 #endif
-#include "../include/asm/inat.h" /* __ignore_sync_check__ */
-#include "../include/asm/insn.h" /* __ignore_sync_check__ */
+#include <asm/inat.h> /*__ignore_sync_check__ */
+#include <asm/insn.h> /* __ignore_sync_check__ */

Hi, this change is breaking non-x86 builds of perf for me in tip.git/master. The reason being that non-x86 builds compile the intel-pt-decoder, which includes this file, but don't have their include paths set to find tools/arch/x86. I think we want to keep the relative paths.

Thanks,
Ian

Quoted 467 lines
tools/arch/x86/lib/insn.c +172 -46
-#include "../include/asm/emulate_prefix.h" /* __ignore_sync_check__ */
+#include <linux/errno.h>
+#include <linux/kconfig.h>
+
+#include <asm/emulate_prefix.h> /* __ignore_sync_check__ */
 
 #define leXX_to_cpu(t, r)                                              \
 ({                                                                     \
@@ -112,8 +115,12 @@ static void insn_get_emulate_prefix(struct insn *insn)
  * Populates the @insn->prefixes bitmap, and updates @insn->next_byte
  * to point to the (first) opcode.  No effect if @insn->prefixes.got
  * is already set.
+ *
+ * * Returns:
+ * 0:  on success
+ * < 0: on error
  */
-void insn_get_prefixes(struct insn *insn)
+int insn_get_prefixes(struct insn *insn)
 {
        struct insn_field *prefixes = &insn->prefixes;
        insn_attr_t attr;
@@ -121,7 +128,7 @@ void insn_get_prefixes(struct insn *insn)
        int i, nb;
 
        if (prefixes->got)
-               return;
+               return 0;
 
        insn_get_emulate_prefix(insn);
 
@@ -231,8 +238,10 @@ void insn_get_prefixes(struct insn *insn)
 
        prefixes->got = 1;
 
+       return 0;
+
 err_out:
-       return;
+       return -ENODATA;
 }
 
 /**
@@ -244,16 +253,25 @@ void insn_get_prefixes(struct insn *insn)
  * If necessary, first collects any preceding (prefix) bytes.
  * Sets @insn->opcode.value = opcode1.  No effect if @insn->opcode.got
  * is already 1.
+ *
+ * Returns:
+ * 0:  on success
+ * < 0: on error
  */
-void insn_get_opcode(struct insn *insn)
+int insn_get_opcode(struct insn *insn)
 {
        struct insn_field *opcode = &insn->opcode;
+       int pfx_id, ret;
        insn_byte_t op;
-       int pfx_id;
+
        if (opcode->got)
-               return;
-       if (!insn->prefixes.got)
-               insn_get_prefixes(insn);
+               return 0;
+
+       if (!insn->prefixes.got) {
+               ret = insn_get_prefixes(insn);
+               if (ret)
+                       return ret;
+       }
 
        /* Get first opcode */
        op = get_next(insn_byte_t, insn);
@@ -268,9 +286,13 @@ void insn_get_opcode(struct insn *insn)
                insn->attr = inat_get_avx_attribute(op, m, p);
                if ((inat_must_evex(insn->attr) && !insn_is_evex(insn)) ||
                    (!inat_accept_vex(insn->attr) &&
-                    !inat_is_group(insn->attr)))
-                       insn->attr = 0; /* This instruction is bad */
-               goto end;       /* VEX has only 1 byte for opcode */
+                    !inat_is_group(insn->attr))) {
+                       /* This instruction is bad */
+                       insn->attr = 0;
+                       return -EINVAL;
+               }
+               /* VEX has only 1 byte for opcode */
+               goto end;
        }
 
        insn->attr = inat_get_opcode_attribute(op);
@@ -281,13 +303,18 @@ void insn_get_opcode(struct insn *insn)
                pfx_id = insn_last_prefix_id(insn);
                insn->attr = inat_get_escape_attribute(op, pfx_id, insn->attr);
        }
-       if (inat_must_vex(insn->attr))
-               insn->attr = 0; /* This instruction is bad */
+
+       if (inat_must_vex(insn->attr)) {
+               /* This instruction is bad */
+               insn->attr = 0;
+               return -EINVAL;
+       }
 end:
        opcode->got = 1;
+       return 0;
 
 err_out:
-       return;
+       return -ENODATA;
 }
 
 /**
@@ -297,15 +324,25 @@ void insn_get_opcode(struct insn *insn)
  * Populates @insn->modrm and updates @insn->next_byte to point past the
  * ModRM byte, if any.  If necessary, first collects the preceding bytes
  * (prefixes and opcode(s)).  No effect if @insn->modrm.got is already 1.
+ *
+ * Returns:
+ * 0:  on success
+ * < 0: on error
  */
-void insn_get_modrm(struct insn *insn)
+int insn_get_modrm(struct insn *insn)
 {
        struct insn_field *modrm = &insn->modrm;
        insn_byte_t pfx_id, mod;
+       int ret;
+
        if (modrm->got)
-               return;
-       if (!insn->opcode.got)
-               insn_get_opcode(insn);
+               return 0;
+
+       if (!insn->opcode.got) {
+               ret = insn_get_opcode(insn);
+               if (ret)
+                       return ret;
+       }
 
        if (inat_has_modrm(insn->attr)) {
                mod = get_next(insn_byte_t, insn);
@@ -314,17 +351,22 @@ void insn_get_modrm(struct insn *insn)
                        pfx_id = insn_last_prefix_id(insn);
                        insn->attr = inat_get_group_attribute(mod, pfx_id,
                                                              insn->attr);
-                       if (insn_is_avx(insn) && !inat_accept_vex(insn->attr))
-                               insn->attr = 0; /* This is bad */
+                       if (insn_is_avx(insn) && !inat_accept_vex(insn->attr)) {
+                               /* Bad insn */
+                               insn->attr = 0;
+                               return -EINVAL;
+                       }
                }
        }
 
        if (insn->x86_64 && inat_is_force64(insn->attr))
                insn->opnd_bytes = 8;
+
        modrm->got = 1;
+       return 0;
 
 err_out:
-       return;
+       return -ENODATA;
 }
 
 
@@ -338,11 +380,16 @@ void insn_get_modrm(struct insn *insn)
 int insn_rip_relative(struct insn *insn)
 {
        struct insn_field *modrm = &insn->modrm;
+       int ret;
 
        if (!insn->x86_64)
                return 0;
-       if (!modrm->got)
-               insn_get_modrm(insn);
+
+       if (!modrm->got) {
+               ret = insn_get_modrm(insn);
+               if (ret)
+                       return 0;
+       }
        /*
         * For rip-relative instructions, the mod field (top 2 bits)
         * is zero and the r/m field (bottom 3 bits) is 0x5.
@@ -356,15 +403,25 @@ int insn_rip_relative(struct insn *insn)
  *
  * If necessary, first collects the instruction up to and including the
  * ModRM byte.
+ *
+ * Returns:
+ * 0: if decoding succeeded
+ * < 0: otherwise.
  */
-void insn_get_sib(struct insn *insn)
+int insn_get_sib(struct insn *insn)
 {
        insn_byte_t modrm;
+       int ret;
 
        if (insn->sib.got)
-               return;
-       if (!insn->modrm.got)
-               insn_get_modrm(insn);
+               return 0;
+
+       if (!insn->modrm.got) {
+               ret = insn_get_modrm(insn);
+               if (ret)
+                       return ret;
+       }
+
        if (insn->modrm.nbytes) {
                modrm = insn->modrm.bytes[0];
                if (insn->addr_bytes != 2 &&
@@ -375,8 +432,10 @@ void insn_get_sib(struct insn *insn)
        }
        insn->sib.got = 1;
 
+       return 0;
+
 err_out:
-       return;
+       return -ENODATA;
 }
 
 
@@ -387,15 +446,25 @@ void insn_get_sib(struct insn *insn)
  * If necessary, first collects the instruction up to and including the
  * SIB byte.
  * Displacement value is sign-expanded.
+ *
+ * * Returns:
+ * 0: if decoding succeeded
+ * < 0: otherwise.
  */
-void insn_get_displacement(struct insn *insn)
+int insn_get_displacement(struct insn *insn)
 {
        insn_byte_t mod, rm, base;
+       int ret;
 
        if (insn->displacement.got)
-               return;
-       if (!insn->sib.got)
-               insn_get_sib(insn);
+               return 0;
+
+       if (!insn->sib.got) {
+               ret = insn_get_sib(insn);
+               if (ret)
+                       return ret;
+       }
+
        if (insn->modrm.nbytes) {
                /*
                 * Interpreting the modrm byte:
@@ -437,9 +506,10 @@ void insn_get_displacement(struct insn *insn)
        }
 out:
        insn->displacement.got = 1;
+       return 0;
 
 err_out:
-       return;
+       return -ENODATA;
 }
 
 /* Decode moffset16/32/64. Return 0 if failed */
@@ -538,20 +608,30 @@ static int __get_immptr(struct insn *insn)
 }
 
 /**
- * insn_get_immediate() - Get the immediates of instruction
+ * insn_get_immediate() - Get the immediate in an instruction
  * @insn:      &struct insn containing instruction
  *
  * If necessary, first collects the instruction up to and including the
  * displacement bytes.
  * Basically, most of immediates are sign-expanded. Unsigned-value can be
- * get by bit masking with ((1 << (nbytes * 8)) - 1)
+ * computed by bit masking with ((1 << (nbytes * 8)) - 1)
+ *
+ * Returns:
+ * 0:  on success
+ * < 0: on error
  */
-void insn_get_immediate(struct insn *insn)
+int insn_get_immediate(struct insn *insn)
 {
+       int ret;
+
        if (insn->immediate.got)
-               return;
-       if (!insn->displacement.got)
-               insn_get_displacement(insn);
+               return 0;
+
+       if (!insn->displacement.got) {
+               ret = insn_get_displacement(insn);
+               if (ret)
+                       return ret;
+       }
 
        if (inat_has_moffset(insn->attr)) {
                if (!__get_moffset(insn))
@@ -598,9 +678,10 @@ void insn_get_immediate(struct insn *insn)
        }
 done:
        insn->immediate.got = 1;
+       return 0;
 
 err_out:
-       return;
+       return -ENODATA;
 }
 
 /**
@@ -609,13 +690,58 @@ void insn_get_immediate(struct insn *insn)
  *
  * If necessary, first collects the instruction up to and including the
  * immediates bytes.
- */
-void insn_get_length(struct insn *insn)
+ *
+ * Returns:
+ *  - 0 on success
+ *  - < 0 on error
+*/
+int insn_get_length(struct insn *insn)
 {
+       int ret;
+
        if (insn->length)
-               return;
-       if (!insn->immediate.got)
-               insn_get_immediate(insn);
+               return 0;
+
+       if (!insn->immediate.got) {
+               ret = insn_get_immediate(insn);
+               if (ret)
+                       return ret;
+       }
+
        insn->length = (unsigned char)((unsigned long)insn->next_byte
                                     - (unsigned long)insn->kaddr);
+
+       return 0;
+}
+
+/**
+ * insn_decode() - Decode an x86 instruction
+ * @insn:      &struct insn to be initialized
+ * @kaddr:     address (in kernel memory) of instruction (or copy thereof)
+ * @buf_len:   length of the insn buffer at @kaddr
+ * @m:         insn mode, see enum insn_mode
+ *
+ * Returns:
+ * 0: if decoding succeeded
+ * < 0: otherwise.
+ */
+int insn_decode(struct insn *insn, const void *kaddr, int buf_len, enum insn_mode m)
+{
+       int ret;
+
+#define INSN_MODE_KERN (enum insn_mode)-1 /* __ignore_sync_check__ mode is only valid in the kernel */
+
+       if (m == INSN_MODE_KERN)
+               insn_init(insn, kaddr, buf_len, IS_ENABLED(CONFIG_X86_64));
+       else
+               insn_init(insn, kaddr, buf_len, m == INSN_MODE_64);
+
+       ret = insn_get_length(insn);
+       if (ret)
+               return ret;
+
+       if (insn_complete(insn))
+               return 0;
+
+       return -EINVAL;
 }
tools/include/linux/kconfig.h new file +73 -1
new file mode 100644
index 000000000000..1555a0c4f345
@@ -0,0 +1,73 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _TOOLS_LINUX_KCONFIG_H
+#define _TOOLS_LINUX_KCONFIG_H
+
+/* CONFIG_CC_VERSION_TEXT (Do not delete this comment. See help in Kconfig) */
+
+#ifdef CONFIG_CPU_BIG_ENDIAN
+#define __BIG_ENDIAN 4321
+#else
+#define __LITTLE_ENDIAN 1234
+#endif
+
+#define __ARG_PLACEHOLDER_1 0,
+#define __take_second_arg(__ignored, val, ...) val
+
+/*
+ * The use of "&&" / "||" is limited in certain expressions.
+ * The following enable to calculate "and" / "or" with macro expansion only.
+ */
+#define __and(x, y)                    ___and(x, y)
+#define ___and(x, y)                   ____and(__ARG_PLACEHOLDER_##x, y)
+#define ____and(arg1_or_junk, y)       __take_second_arg(arg1_or_junk y, 0)
+
+#define __or(x, y)                     ___or(x, y)
+#define ___or(x, y)                    ____or(__ARG_PLACEHOLDER_##x, y)
+#define ____or(arg1_or_junk, y)                __take_second_arg(arg1_or_junk 1, y)
+
+/*
+ * Helper macros to use CONFIG_ options in C/CPP expressions. Note that
+ * these only work with boolean and tristate options.
+ */
+
+/*
+ * Getting something that works in C and CPP for an arg that may or may
+ * not be defined is tricky.  Here, if we have "#define CONFIG_BOOGER 1"
+ * we match on the placeholder define, insert the "0," for arg1 and generate
+ * the triplet (0, 1, 0).  Then the last step cherry picks the 2nd arg (a one).
+ * When CONFIG_BOOGER is not defined, we generate a (... 1, 0) pair, and when
+ * the last step cherry picks the 2nd arg, we get a zero.
+ */
+#define __is_defined(x)                        ___is_defined(x)
+#define ___is_defined(val)             ____is_defined(__ARG_PLACEHOLDER_##val)
+#define ____is_defined(arg1_or_junk)   __take_second_arg(arg1_or_junk 1, 0)
+
+/*
+ * IS_BUILTIN(CONFIG_FOO) evaluates to 1 if CONFIG_FOO is set to 'y', 0
+ * otherwise. For boolean options, this is equivalent to
+ * IS_ENABLED(CONFIG_FOO).
+ */
+#define IS_BUILTIN(option) __is_defined(option)
+
+/*
+ * IS_MODULE(CONFIG_FOO) evaluates to 1 if CONFIG_FOO is set to 'm', 0
+ * otherwise.
+ */
+#define IS_MODULE(option) __is_defined(option##_MODULE)
+
+/*
+ * IS_REACHABLE(CONFIG_FOO) evaluates to 1 if the currently compiled
+ * code can call a function defined in code compiled based on CONFIG_FOO.
+ * This is similar to IS_ENABLED(), but returns false when invoked from
+ * built-in code when CONFIG_FOO is set to 'm'.
+ */
+#define IS_REACHABLE(option) __or(IS_BUILTIN(option), \
+                               __and(IS_MODULE(option), __is_defined(MODULE)))
+
+/*
+ * IS_ENABLED(CONFIG_FOO) evaluates to 1 if CONFIG_FOO is set to 'y' or 'm',
+ * 0 otherwise.
+ */
+#define IS_ENABLED(option) __or(IS_BUILTIN(option), IS_MODULE(option))
+
+#endif /* _TOOLS_LINUX_KCONFIG_H */
--

2.29.2

#12 lore raw

Em Tue, Mar 16, 2021 at 06:14:54PM -0700, Ian Rogers escreveu:

Quoted 21 lines · Borislav Petkov
Quoted 19 lines

From: Borislav Petkov <[email protected]>

Users of the instruction decoder should use this to decode instruction bytes. For that, have insn*() helpers return an int value to denote success/failure. When there's an error fetching the next insn byte and the insn falls short, return -ENODATA to denote that.

While at it, make insn_get_opcode() more stricter as to whether what has seen so far is a valid insn and if not.

Copy linux/kconfig.h for the tools-version of the decoder so that it can use IS_ENABLED().

Also, cast the INSN_MODE_KERN dummy define value to (enum insn_mode) for tools use of the decoder because perf tool builds with -Werror and errors out with -Werror=sign-compare otherwise.

Quoted 15 lines

tools/arch/x86/lib/insn.c line 15

Quoted 9 lines

+++ b/tools/arch/x86/lib/insn.c

tools/arch/x86/lib/insn.c +2 -2
@@ -11,10 +11,13 @@
 #else
 #include <string.h>
 #endif
-#include "../include/asm/inat.h" /* __ignore_sync_check__ */
-#include "../include/asm/insn.h" /* __ignore_sync_check__ */
+#include <asm/inat.h> /*__ignore_sync_check__ */
+#include <asm/insn.h> /* __ignore_sync_check__ */

Hi, this change is breaking non-x86 builds of perf for me in tip.git/master. The reason being that non-x86 builds compile the intel-pt-decoder, which includes this file, but don't have their include paths set to find tools/arch/x86. I think we want to keep the relative paths.

Borislav, was this addressed? Ian?

- Arnaldo

author maintainer
#16 lore raw

PATCH v3 5/21 x86/insn-eval: Handle return values from the decoder

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

Now that the different instruction-inspecting functions return a value, test that and return early from callers if error has been encountered.

While at it, do not call insn_get_modrm() when calling insn_get_displacement() because latter will make sure to call insn_get_modrm() if ModRM hasn't been parsed yet.

---
 arch/x86/lib/insn-eval.c | 34 +++++++++++++++++++++-------------
 1 file changed, 21 insertions(+), 13 deletions(-)
arch/x86/lib/insn-eval.c +21 -13
index d0ebf9e93e35..1a6c191a38f8 100644
@@ -928,10 +928,11 @@ static int get_seg_base_limit(struct insn *insn, struct pt_regs *regs,
 static int get_eff_addr_reg(struct insn *insn, struct pt_regs *regs,
 			    int *regoff, long *eff_addr)
 {
-	insn_get_modrm(insn);
+	int ret;
 
-	if (!insn->modrm.nbytes)
-		return -EINVAL;
+	ret = insn_get_modrm(insn);
+	if (ret)
+		return ret;
 
 	if (X86_MODRM_MOD(insn->modrm.value) != 3)
 		return -EINVAL;
@@ -977,14 +978,14 @@ static int get_eff_addr_modrm(struct insn *insn, struct pt_regs *regs,
 			      int *regoff, long *eff_addr)
 {
 	long tmp;
+	int ret;
 
 	if (insn->addr_bytes != 8 && insn->addr_bytes != 4)
 		return -EINVAL;
 
-	insn_get_modrm(insn);
-
-	if (!insn->modrm.nbytes)
-		return -EINVAL;
+	ret = insn_get_modrm(insn);
+	if (ret)
+		return ret;
 
 	if (X86_MODRM_MOD(insn->modrm.value) > 2)
 		return -EINVAL;
@@ -1106,18 +1107,21 @@ static int get_eff_addr_modrm_16(struct insn *insn, struct pt_regs *regs,
  * @base_offset will have a register, as an offset from the base of pt_regs,
  * that can be used to resolve the associated segment.
  *
- * -EINVAL on error.
+ * Negative value on error.
  */
 static int get_eff_addr_sib(struct insn *insn, struct pt_regs *regs,
 			    int *base_offset, long *eff_addr)
 {
 	long base, indx;
 	int indx_offset;
+	int ret;
 
 	if (insn->addr_bytes != 8 && insn->addr_bytes != 4)
 		return -EINVAL;
 
-	insn_get_modrm(insn);
+	ret = insn_get_modrm(insn);
+	if (ret)
+		return ret;
 
 	if (!insn->modrm.nbytes)
 		return -EINVAL;
@@ -1125,7 +1129,9 @@ static int get_eff_addr_sib(struct insn *insn, struct pt_regs *regs,
 	if (X86_MODRM_MOD(insn->modrm.value) > 2)
 		return -EINVAL;
 
-	insn_get_sib(insn);
+	ret = insn_get_sib(insn);
+	if (ret)
+		return ret;
 
 	if (!insn->sib.nbytes)
 		return -EINVAL;
@@ -1194,8 +1200,8 @@ static void __user *get_addr_ref_16(struct insn *insn, struct pt_regs *regs)
 	short eff_addr;
 	long tmp;
 
-	insn_get_modrm(insn);
-	insn_get_displacement(insn);
+	if (insn_get_displacement(insn))
+		goto out;
 
 	if (insn->addr_bytes != 2)
 		goto out;
@@ -1491,7 +1497,9 @@ bool insn_decode_from_regs(struct insn *insn, struct pt_regs *regs,
 	insn->addr_bytes = INSN_CODE_SEG_ADDR_SZ(seg_defs);
 	insn->opnd_bytes = INSN_CODE_SEG_OPND_SZ(seg_defs);
 
-	insn_get_length(insn);
+	if (insn_get_length(insn))
+		return false;
+
 	if (buf_size < insn->length)
 		return false;
 
Signature · 1 line
2.29.2
#17 lore raw

[tip: x86/core] x86/insn-eval: Handle return values from the decoder

The following commit has been merged into the x86/core branch of tip:

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     6e8c83d2a3afbfd5ee019ec720b75a42df515caa
Gitweb:        https://git.kernel.org/tip/6e8c83d2a3afbfd5ee019ec720b75a42df515caa
Author:        Borislav Petkov <[email protected]>
AuthorDate:    Thu, 19 Nov 2020 19:20:18 +01:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Mon, 15 Mar 2021 11:12:15 +01:00

x86/insn-eval: Handle return values from the decoder

Now that the different instruction-inspecting functions return a value, test that and return early from callers if error has been encountered.

While at it, do not call insn_get_modrm() when calling insn_get_displacement() because latter will make sure to call insn_get_modrm() if ModRM hasn't been parsed yet.

---
 arch/x86/lib/insn-eval.c | 34 +++++++++++++++++++++-------------
 1 file changed, 21 insertions(+), 13 deletions(-)
arch/x86/lib/insn-eval.c +21 -13
index 422a592..321a157 100644
@@ -924,10 +924,11 @@ static int get_seg_base_limit(struct insn *insn, struct pt_regs *regs,
 static int get_eff_addr_reg(struct insn *insn, struct pt_regs *regs,
 			    int *regoff, long *eff_addr)
 {
-	insn_get_modrm(insn);
+	int ret;
 
-	if (!insn->modrm.nbytes)
-		return -EINVAL;
+	ret = insn_get_modrm(insn);
+	if (ret)
+		return ret;
 
 	if (X86_MODRM_MOD(insn->modrm.value) != 3)
 		return -EINVAL;
@@ -973,14 +974,14 @@ static int get_eff_addr_modrm(struct insn *insn, struct pt_regs *regs,
 			      int *regoff, long *eff_addr)
 {
 	long tmp;
+	int ret;
 
 	if (insn->addr_bytes != 8 && insn->addr_bytes != 4)
 		return -EINVAL;
 
-	insn_get_modrm(insn);
-
-	if (!insn->modrm.nbytes)
-		return -EINVAL;
+	ret = insn_get_modrm(insn);
+	if (ret)
+		return ret;
 
 	if (X86_MODRM_MOD(insn->modrm.value) > 2)
 		return -EINVAL;
@@ -1102,18 +1103,21 @@ static int get_eff_addr_modrm_16(struct insn *insn, struct pt_regs *regs,
  * @base_offset will have a register, as an offset from the base of pt_regs,
  * that can be used to resolve the associated segment.
  *
- * -EINVAL on error.
+ * Negative value on error.
  */
 static int get_eff_addr_sib(struct insn *insn, struct pt_regs *regs,
 			    int *base_offset, long *eff_addr)
 {
 	long base, indx;
 	int indx_offset;
+	int ret;
 
 	if (insn->addr_bytes != 8 && insn->addr_bytes != 4)
 		return -EINVAL;
 
-	insn_get_modrm(insn);
+	ret = insn_get_modrm(insn);
+	if (ret)
+		return ret;
 
 	if (!insn->modrm.nbytes)
 		return -EINVAL;
@@ -1121,7 +1125,9 @@ static int get_eff_addr_sib(struct insn *insn, struct pt_regs *regs,
 	if (X86_MODRM_MOD(insn->modrm.value) > 2)
 		return -EINVAL;
 
-	insn_get_sib(insn);
+	ret = insn_get_sib(insn);
+	if (ret)
+		return ret;
 
 	if (!insn->sib.nbytes)
 		return -EINVAL;
@@ -1190,8 +1196,8 @@ static void __user *get_addr_ref_16(struct insn *insn, struct pt_regs *regs)
 	short eff_addr;
 	long tmp;
 
-	insn_get_modrm(insn);
-	insn_get_displacement(insn);
+	if (insn_get_displacement(insn))
+		goto out;
 
 	if (insn->addr_bytes != 2)
 		goto out;
@@ -1525,7 +1531,9 @@ bool insn_decode_from_regs(struct insn *insn, struct pt_regs *regs,
 	insn->addr_bytes = INSN_CODE_SEG_ADDR_SZ(seg_defs);
 	insn->opnd_bytes = INSN_CODE_SEG_OPND_SZ(seg_defs);
 
-	insn_get_length(insn);
+	if (insn_get_length(insn))
+		return false;
+
 	if (buf_size < insn->length)
 		return false;
 
author maintainer
#18 lore raw

PATCH v3 6/21 x86/boot/compressed/sev-es: Convert to insn_decode()

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

Other than simplifying the code there should be no functional changes resulting from this.

---
 arch/x86/boot/compressed/sev-es.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
arch/x86/boot/compressed/sev-es.c +5 -6
index 27826c265aab..801c626fc3d4 100644
@@ -78,16 +78,15 @@ static inline void sev_es_wr_ghcb_msr(u64 val)
 static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
 {
 	char buffer[MAX_INSN_SIZE];
-	enum es_result ret;
+	int ret;
 
 	memcpy(buffer, (unsigned char *)ctxt->regs->ip, MAX_INSN_SIZE);
 
-	insn_init(&ctxt->insn, buffer, MAX_INSN_SIZE, 1);
-	insn_get_length(&ctxt->insn);
+	ret = insn_decode(&ctxt->insn, buffer, MAX_INSN_SIZE, INSN_MODE_64);
+	if (ret < 0)
+		return ES_DECODE_FAILED;
 
-	ret = ctxt->insn.immediate.got ? ES_OK : ES_DECODE_FAILED;
-
-	return ret;
+	return ES_OK;
 }
 
 static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
Signature · 1 line
2.29.2
#19 lore raw

[tip: x86/core] x86/boot/compressed/sev-es: Convert to insn_decode()

The following commit has been merged into the x86/core branch of tip:

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     514ef77607b9ff184c11b88e8f100bc27f07460d
Gitweb:        https://git.kernel.org/tip/514ef77607b9ff184c11b88e8f100bc27f07460d
Author:        Borislav Petkov <[email protected]>
AuthorDate:    Thu, 05 Nov 2020 17:53:20 +01:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Mon, 15 Mar 2021 11:18:35 +01:00

x86/boot/compressed/sev-es: Convert to insn_decode()

Other than simplifying the code there should be no functional changes resulting from this.

---
 arch/x86/boot/compressed/sev-es.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
arch/x86/boot/compressed/sev-es.c +5 -6
index 27826c2..801c626 100644
@@ -78,16 +78,15 @@ static inline void sev_es_wr_ghcb_msr(u64 val)
 static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
 {
 	char buffer[MAX_INSN_SIZE];
-	enum es_result ret;
+	int ret;
 
 	memcpy(buffer, (unsigned char *)ctxt->regs->ip, MAX_INSN_SIZE);
 
-	insn_init(&ctxt->insn, buffer, MAX_INSN_SIZE, 1);
-	insn_get_length(&ctxt->insn);
+	ret = insn_decode(&ctxt->insn, buffer, MAX_INSN_SIZE, INSN_MODE_64);
+	if (ret < 0)
+		return ES_DECODE_FAILED;
 
-	ret = ctxt->insn.immediate.got ? ES_OK : ES_DECODE_FAILED;
-
-	return ret;
+	return ES_OK;
 }
 
 static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
author maintainer
#20 lore raw

PATCH v3 7/21 perf/x86/intel/ds: Check insn_get_length() retval

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

intel_pmu_pebs_fixup_ip() needs only the insn length so use the appropriate helper instead of a full decode. A full decode differs only in running insn_complete() on the decoded insn but that is not needed here.

---
 arch/x86/events/intel/ds.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
arch/x86/events/intel/ds.c +5 -6
index 7ebae1826403..cdd195a3804e 100644
@@ -1353,14 +1353,13 @@ static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
 		is_64bit = kernel_ip(to) || any_64bit_mode(regs);
 #endif
 		insn_init(&insn, kaddr, size, is_64bit);
-		insn_get_length(&insn);
+
 		/*
-		 * Make sure there was not a problem decoding the
-		 * instruction and getting the length.  This is
-		 * doubly important because we have an infinite
-		 * loop if insn.length=0.
+		 * Make sure there was not a problem decoding the instruction.
+		 * This is doubly important because we have an infinite loop if
+		 * insn.length=0.
 		 */
-		if (!insn.length)
+		if (insn_get_length(&insn))
 			break;
 
 		to += insn.length;
Signature · 1 line
2.29.2
#21 lore raw

[tip: x86/core] perf/x86/intel/ds: Check insn_get_length() retval

The following commit has been merged into the x86/core branch of tip:

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     2ff49881d606d5e0d5b27cb6066c8a18689bd341
Gitweb:        https://git.kernel.org/tip/2ff49881d606d5e0d5b27cb6066c8a18689bd341
Author:        Borislav Petkov <[email protected]>
AuthorDate:    Fri, 06 Nov 2020 16:33:34 +01:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Mon, 15 Mar 2021 11:20:29 +01:00

perf/x86/intel/ds: Check insn_get_length() retval

intel_pmu_pebs_fixup_ip() needs only the insn length so use the appropriate helper instead of a full decode. A full decode differs only in running insn_complete() on the decoded insn but that is not needed here.

---
 arch/x86/events/intel/ds.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
arch/x86/events/intel/ds.c +5 -6
index 7ebae18..cdd195a 100644
@@ -1353,14 +1353,13 @@ static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
 		is_64bit = kernel_ip(to) || any_64bit_mode(regs);
 #endif
 		insn_init(&insn, kaddr, size, is_64bit);
-		insn_get_length(&insn);
+
 		/*
-		 * Make sure there was not a problem decoding the
-		 * instruction and getting the length.  This is
-		 * doubly important because we have an infinite
-		 * loop if insn.length=0.
+		 * Make sure there was not a problem decoding the instruction.
+		 * This is doubly important because we have an infinite loop if
+		 * insn.length=0.
 		 */
-		if (!insn.length)
+		if (insn_get_length(&insn))
 			break;
 
 		to += insn.length;
author maintainer
#22 lore raw

PATCH v3 8/21 perf/x86/intel/ds: Check return values of insn decoder functions

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

branch_type() doesn't need to call the full insn_decode() because it doesn't need it in all cases thus leave the calls separate.

---
 arch/x86/events/intel/lbr.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
arch/x86/events/intel/lbr.c +5 -5
index 21890dacfcfe..9ecf5028fb8f 100644
@@ -1224,8 +1224,7 @@ static int branch_type(unsigned long from, unsigned long to, int abort)
 	is64 = kernel_ip((unsigned long)addr) || any_64bit_mode(current_pt_regs());
 #endif
 	insn_init(&insn, addr, bytes_read, is64);
-	insn_get_opcode(&insn);
-	if (!insn.opcode.got)
+	if (insn_get_opcode(&insn))
 		return X86_BR_ABORT;
 
 	switch (insn.opcode.bytes[0]) {
@@ -1262,8 +1261,7 @@ static int branch_type(unsigned long from, unsigned long to, int abort)
 		ret = X86_BR_INT;
 		break;
 	case 0xe8: /* call near rel */
-		insn_get_immediate(&insn);
-		if (insn.immediate1.value == 0) {
+		if (insn_get_immediate(&insn) || insn.immediate1.value == 0) {
 			/* zero length call */
 			ret = X86_BR_ZERO_CALL;
 			break;
@@ -1279,7 +1277,9 @@ static int branch_type(unsigned long from, unsigned long to, int abort)
 		ret = X86_BR_JMP;
 		break;
 	case 0xff: /* call near absolute, call far absolute ind */
-		insn_get_modrm(&insn);
+		if (insn_get_modrm(&insn))
+			return X86_BR_ABORT;
+
 		ext = (insn.modrm.bytes[0] >> 3) & 0x7;
 		switch (ext) {
 		case 2: /* near ind call */
Signature · 1 line
2.29.2
#23 lore raw

[tip: x86/core] perf/x86/intel/ds: Check return values of insn decoder functions

The following commit has been merged into the x86/core branch of tip:

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     8c98a605544cfdec21d32fcf8fc855dc439f608f
Gitweb:        https://git.kernel.org/tip/8c98a605544cfdec21d32fcf8fc855dc439f608f
Author:        Borislav Petkov <[email protected]>
AuthorDate:    Fri, 06 Nov 2020 16:36:24 +01:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Mon, 15 Mar 2021 11:23:48 +01:00

perf/x86/intel/ds: Check return values of insn decoder functions

branch_type() doesn't need to call the full insn_decode() because it doesn't need it in all cases thus leave the calls separate.

---
 arch/x86/events/intel/lbr.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
arch/x86/events/intel/lbr.c +5 -5
index 21890da..9ecf502 100644
@@ -1224,8 +1224,7 @@ static int branch_type(unsigned long from, unsigned long to, int abort)
 	is64 = kernel_ip((unsigned long)addr) || any_64bit_mode(current_pt_regs());
 #endif
 	insn_init(&insn, addr, bytes_read, is64);
-	insn_get_opcode(&insn);
-	if (!insn.opcode.got)
+	if (insn_get_opcode(&insn))
 		return X86_BR_ABORT;
 
 	switch (insn.opcode.bytes[0]) {
@@ -1262,8 +1261,7 @@ static int branch_type(unsigned long from, unsigned long to, int abort)
 		ret = X86_BR_INT;
 		break;
 	case 0xe8: /* call near rel */
-		insn_get_immediate(&insn);
-		if (insn.immediate1.value == 0) {
+		if (insn_get_immediate(&insn) || insn.immediate1.value == 0) {
 			/* zero length call */
 			ret = X86_BR_ZERO_CALL;
 			break;
@@ -1279,7 +1277,9 @@ static int branch_type(unsigned long from, unsigned long to, int abort)
 		ret = X86_BR_JMP;
 		break;
 	case 0xff: /* call near absolute, call far absolute ind */
-		insn_get_modrm(&insn);
+		if (insn_get_modrm(&insn))
+			return X86_BR_ABORT;
+
 		ext = (insn.modrm.bytes[0] >> 3) & 0x7;
 		switch (ext) {
 		case 2: /* near ind call */
author maintainer
#24 lore raw

PATCH v3 9/21 x86/alternative: Use insn_decode()

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

No functional changes, just simplification.

---
 arch/x86/kernel/alternative.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/alternative.c +3 -3
index 8d778e46725d..ce28c5c1deba 100644
@@ -1274,15 +1274,15 @@ static void text_poke_loc_init(struct text_poke_loc *tp, void *addr,
 			       const void *opcode, size_t len, const void *emulate)
 {
 	struct insn insn;
+	int ret;
 
 	memcpy((void *)tp->text, opcode, len);
 	if (!emulate)
 		emulate = opcode;
 
-	kernel_insn_init(&insn, emulate, MAX_INSN_SIZE);
-	insn_get_length(&insn);
+	ret = insn_decode(&insn, emulate, MAX_INSN_SIZE, INSN_MODE_KERN);
 
-	BUG_ON(!insn_complete(&insn));
+	BUG_ON(ret < 0);
 	BUG_ON(len != insn.length);
 
 	tp->rel_addr = addr - (void *)_stext;
Signature · 1 line
2.29.2
#25 lore raw

[tip: x86/core] x86/alternative: Use insn_decode()

The following commit has been merged into the x86/core branch of tip:

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     63c66cde7bbcc79aac14b25861c5b2495eede57b
Gitweb:        https://git.kernel.org/tip/63c66cde7bbcc79aac14b25861c5b2495eede57b
Author:        Borislav Petkov <[email protected]>
AuthorDate:    Fri, 06 Nov 2020 19:37:25 +01:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Mon, 15 Mar 2021 11:25:38 +01:00

x86/alternative: Use insn_decode()

No functional changes, just simplification.

---
 arch/x86/kernel/alternative.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/alternative.c +3 -3
index 8d778e4..ce28c5c 100644
@@ -1274,15 +1274,15 @@ static void text_poke_loc_init(struct text_poke_loc *tp, void *addr,
 			       const void *opcode, size_t len, const void *emulate)
 {
 	struct insn insn;
+	int ret;
 
 	memcpy((void *)tp->text, opcode, len);
 	if (!emulate)
 		emulate = opcode;
 
-	kernel_insn_init(&insn, emulate, MAX_INSN_SIZE);
-	insn_get_length(&insn);
+	ret = insn_decode(&insn, emulate, MAX_INSN_SIZE, INSN_MODE_KERN);
 
-	BUG_ON(!insn_complete(&insn));
+	BUG_ON(ret < 0);
 	BUG_ON(len != insn.length);
 
 	tp->rel_addr = addr - (void *)_stext;
author maintainer
#26 lore raw

PATCH v3 10/21 x86/mce: Convert to insn_decode()

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

Simplify code, no functional changes.

---
 arch/x86/kernel/cpu/mce/severity.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
arch/x86/kernel/cpu/mce/severity.c +4 -8
index 83df991314c5..a2136ced9d73 100644
@@ -218,15 +218,15 @@ static struct severity {
 static bool is_copy_from_user(struct pt_regs *regs)
 {
 	u8 insn_buf[MAX_INSN_SIZE];
-	struct insn insn;
 	unsigned long addr;
+	struct insn insn;
+	int ret;
 
 	if (copy_from_kernel_nofault(insn_buf, (void *)regs->ip, MAX_INSN_SIZE))
 		return false;
 
-	kernel_insn_init(&insn, insn_buf, MAX_INSN_SIZE);
-	insn_get_opcode(&insn);
-	if (!insn.opcode.got)
+	ret = insn_decode(&insn, insn_buf, MAX_INSN_SIZE, INSN_MODE_KERN);
+	if (ret < 0)
 		return false;
 
 	switch (insn.opcode.value) {
@@ -234,10 +234,6 @@ static bool is_copy_from_user(struct pt_regs *regs)
 	case 0x8A: case 0x8B:
 	/* MOVZ mem,reg */
 	case 0xB60F: case 0xB70F:
-		insn_get_modrm(&insn);
-		insn_get_sib(&insn);
-		if (!insn.modrm.got || !insn.sib.got)
-			return false;
 		addr = (unsigned long)insn_get_addr_ref(&insn, regs);
 		break;
 	/* REP MOVS */
Signature · 1 line
2.29.2
#27 lore raw

[tip: x86/core] x86/mce: Convert to insn_decode()

The following commit has been merged into the x86/core branch of tip:

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     1580f488ea8c6a62d002be364248c34c2f2e430b
Gitweb:        https://git.kernel.org/tip/1580f488ea8c6a62d002be364248c34c2f2e430b
Author:        Borislav Petkov <[email protected]>
AuthorDate:    Fri, 06 Nov 2020 19:39:08 +01:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Mon, 15 Mar 2021 11:26:40 +01:00

x86/mce: Convert to insn_decode()

Simplify code, no functional changes.

---
 arch/x86/kernel/cpu/mce/severity.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
arch/x86/kernel/cpu/mce/severity.c +4 -8
index 83df991..a2136ce 100644
@@ -218,15 +218,15 @@ static struct severity {
 static bool is_copy_from_user(struct pt_regs *regs)
 {
 	u8 insn_buf[MAX_INSN_SIZE];
-	struct insn insn;
 	unsigned long addr;
+	struct insn insn;
+	int ret;
 
 	if (copy_from_kernel_nofault(insn_buf, (void *)regs->ip, MAX_INSN_SIZE))
 		return false;
 
-	kernel_insn_init(&insn, insn_buf, MAX_INSN_SIZE);
-	insn_get_opcode(&insn);
-	if (!insn.opcode.got)
+	ret = insn_decode(&insn, insn_buf, MAX_INSN_SIZE, INSN_MODE_KERN);
+	if (ret < 0)
 		return false;
 
 	switch (insn.opcode.value) {
@@ -234,10 +234,6 @@ static bool is_copy_from_user(struct pt_regs *regs)
 	case 0x8A: case 0x8B:
 	/* MOVZ mem,reg */
 	case 0xB60F: case 0xB70F:
-		insn_get_modrm(&insn);
-		insn_get_sib(&insn);
-		if (!insn.modrm.got || !insn.sib.got)
-			return false;
 		addr = (unsigned long)insn_get_addr_ref(&insn, regs);
 		break;
 	/* REP MOVS */
author maintainer
#28 lore raw

PATCH v3 11/21 x86/kprobes: Convert to insn_decode()

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

Simplify code, improve decoding error checking.

---
 arch/x86/kernel/kprobes/core.c | 17 +++++++++++------
 arch/x86/kernel/kprobes/opt.c  |  9 +++++++--
 2 files changed, 18 insertions(+), 8 deletions(-)
arch/x86/kernel/kprobes/core.c +11 -6
index df776cdca327..60a540fcbd56 100644
@@ -265,6 +265,8 @@ static int can_probe(unsigned long paddr)
 	/* Decode instructions */
 	addr = paddr - offset;
 	while (addr < paddr) {
+		int ret;
+
 		/*
 		 * Check if the instruction has been modified by another
 		 * kprobe, in which case we replace the breakpoint by the
@@ -276,8 +278,10 @@ static int can_probe(unsigned long paddr)
 		__addr = recover_probed_instruction(buf, addr);
 		if (!__addr)
 			return 0;
-		kernel_insn_init(&insn, (void *)__addr, MAX_INSN_SIZE);
-		insn_get_length(&insn);
+
+		ret = insn_decode(&insn, (void *)__addr, MAX_INSN_SIZE, INSN_MODE_KERN);
+		if (ret < 0)
+			return 0;
 
 		/*
 		 * Another debugging subsystem might insert this breakpoint.
@@ -301,8 +305,8 @@ static int can_probe(unsigned long paddr)
 int __copy_instruction(u8 *dest, u8 *src, u8 *real, struct insn *insn)
 {
 	kprobe_opcode_t buf[MAX_INSN_SIZE];
-	unsigned long recovered_insn =
-		recover_probed_instruction(buf, (unsigned long)src);
+	unsigned long recovered_insn = recover_probed_instruction(buf, (unsigned long)src);
+	int ret;
 
 	if (!recovered_insn || !insn)
 		return 0;
@@ -312,8 +316,9 @@ int __copy_instruction(u8 *dest, u8 *src, u8 *real, struct insn *insn)
 			MAX_INSN_SIZE))
 		return 0;
 
-	kernel_insn_init(insn, dest, MAX_INSN_SIZE);
-	insn_get_length(insn);
+	ret = insn_decode(insn, dest, MAX_INSN_SIZE, INSN_MODE_KERN);
+	if (ret < 0)
+		return 0;
 
 	/* We can not probe force emulate prefixed instruction */
 	if (insn_has_emulate_prefix(insn))
arch/x86/kernel/kprobes/opt.c +7 -2
index 08eb23074f92..4299fc865732 100644
@@ -312,6 +312,8 @@ static int can_optimize(unsigned long paddr)
 	addr = paddr - offset;
 	while (addr < paddr - offset + size) { /* Decode until function end */
 		unsigned long recovered_insn;
+		int ret;
+
 		if (search_exception_tables(addr))
 			/*
 			 * Since some fixup code will jumps into this function,
@@ -321,8 +323,11 @@ static int can_optimize(unsigned long paddr)
 		recovered_insn = recover_probed_instruction(buf, addr);
 		if (!recovered_insn)
 			return 0;
-		kernel_insn_init(&insn, (void *)recovered_insn, MAX_INSN_SIZE);
-		insn_get_length(&insn);
+
+		ret = insn_decode(&insn, (void *)recovered_insn, MAX_INSN_SIZE, INSN_MODE_KERN);
+		if (ret < 0)
+			return 0;
+
 		/*
 		 * In the case of detecting unknown breakpoint, this could be
 		 * a padding INT3 between functions. Let's check that all the
Signature · 1 line
2.29.2
#29 lore raw

[tip: x86/core] x86/kprobes: Convert to insn_decode()

The following commit has been merged into the x86/core branch of tip:

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     77e768ec1391dc0d6cd89822aa60b9a1c1bd8128
Gitweb:        https://git.kernel.org/tip/77e768ec1391dc0d6cd89822aa60b9a1c1bd8128
Author:        Borislav Petkov <[email protected]>
AuthorDate:    Mon, 16 Nov 2020 18:10:11 +01:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Mon, 15 Mar 2021 11:28:20 +01:00

x86/kprobes: Convert to insn_decode()

Simplify code, improve decoding error checking.

---
 arch/x86/kernel/kprobes/core.c | 17 +++++++++++------
 arch/x86/kernel/kprobes/opt.c  |  9 +++++++--
 2 files changed, 18 insertions(+), 8 deletions(-)
arch/x86/kernel/kprobes/core.c +11 -6
index df776cd..60a540f 100644
@@ -265,6 +265,8 @@ static int can_probe(unsigned long paddr)
 	/* Decode instructions */
 	addr = paddr - offset;
 	while (addr < paddr) {
+		int ret;
+
 		/*
 		 * Check if the instruction has been modified by another
 		 * kprobe, in which case we replace the breakpoint by the
@@ -276,8 +278,10 @@ static int can_probe(unsigned long paddr)
 		__addr = recover_probed_instruction(buf, addr);
 		if (!__addr)
 			return 0;
-		kernel_insn_init(&insn, (void *)__addr, MAX_INSN_SIZE);
-		insn_get_length(&insn);
+
+		ret = insn_decode(&insn, (void *)__addr, MAX_INSN_SIZE, INSN_MODE_KERN);
+		if (ret < 0)
+			return 0;
 
 		/*
 		 * Another debugging subsystem might insert this breakpoint.
@@ -301,8 +305,8 @@ static int can_probe(unsigned long paddr)
 int __copy_instruction(u8 *dest, u8 *src, u8 *real, struct insn *insn)
 {
 	kprobe_opcode_t buf[MAX_INSN_SIZE];
-	unsigned long recovered_insn =
-		recover_probed_instruction(buf, (unsigned long)src);
+	unsigned long recovered_insn = recover_probed_instruction(buf, (unsigned long)src);
+	int ret;
 
 	if (!recovered_insn || !insn)
 		return 0;
@@ -312,8 +316,9 @@ int __copy_instruction(u8 *dest, u8 *src, u8 *real, struct insn *insn)
 			MAX_INSN_SIZE))
 		return 0;
 
-	kernel_insn_init(insn, dest, MAX_INSN_SIZE);
-	insn_get_length(insn);
+	ret = insn_decode(insn, dest, MAX_INSN_SIZE, INSN_MODE_KERN);
+	if (ret < 0)
+		return 0;
 
 	/* We can not probe force emulate prefixed instruction */
 	if (insn_has_emulate_prefix(insn))
arch/x86/kernel/kprobes/opt.c +7 -2
index 08eb230..4299fc8 100644
@@ -312,6 +312,8 @@ static int can_optimize(unsigned long paddr)
 	addr = paddr - offset;
 	while (addr < paddr - offset + size) { /* Decode until function end */
 		unsigned long recovered_insn;
+		int ret;
+
 		if (search_exception_tables(addr))
 			/*
 			 * Since some fixup code will jumps into this function,
@@ -321,8 +323,11 @@ static int can_optimize(unsigned long paddr)
 		recovered_insn = recover_probed_instruction(buf, addr);
 		if (!recovered_insn)
 			return 0;
-		kernel_insn_init(&insn, (void *)recovered_insn, MAX_INSN_SIZE);
-		insn_get_length(&insn);
+
+		ret = insn_decode(&insn, (void *)recovered_insn, MAX_INSN_SIZE, INSN_MODE_KERN);
+		if (ret < 0)
+			return 0;
+
 		/*
 		 * In the case of detecting unknown breakpoint, this could be
 		 * a padding INT3 between functions. Let's check that all the
author maintainer
#30 lore raw

PATCH v3 12/21 x86/sev-es: Split vc_decode_insn()

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

Split it into two helpers - a user- and a kernel-mode one for readability. Yes, the original function body is not that convoluted but splitting it makes following through that code trivial than having to pay attention to each little difference when in user or in kernel mode.

No functional changes.

---
 arch/x86/kernel/sev-es.c | 59 ++++++++++++++++++++++++++--------------
 1 file changed, 38 insertions(+), 21 deletions(-)
arch/x86/kernel/sev-es.c +38 -21
index 557386ce6e3b..e60ab556f51c 100644
@@ -241,41 +241,58 @@ static int vc_fetch_insn_kernel(struct es_em_ctxt *ctxt,
 	return copy_from_kernel_nofault(buffer, (unsigned char *)ctxt->regs->ip, MAX_INSN_SIZE);
 }
 
-static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
+static enum es_result __vc_decode_user_insn(struct es_em_ctxt *ctxt)
 {
 	char buffer[MAX_INSN_SIZE];
 	enum es_result ret;
 	int res;
 
-	if (user_mode(ctxt->regs)) {
-		res = insn_fetch_from_user(ctxt->regs, buffer);
-		if (!res) {
-			ctxt->fi.vector     = X86_TRAP_PF;
-			ctxt->fi.error_code = X86_PF_INSTR | X86_PF_USER;
-			ctxt->fi.cr2        = ctxt->regs->ip;
-			return ES_EXCEPTION;
-		}
+	res = insn_fetch_from_user(ctxt->regs, buffer);
+	if (!res) {
+		ctxt->fi.vector     = X86_TRAP_PF;
+		ctxt->fi.error_code = X86_PF_INSTR | X86_PF_USER;
+		ctxt->fi.cr2        = ctxt->regs->ip;
+		return ES_EXCEPTION;
+	}
 
-		if (!insn_decode_from_regs(&ctxt->insn, ctxt->regs, buffer, res))
-			return ES_DECODE_FAILED;
-	} else {
-		res = vc_fetch_insn_kernel(ctxt, buffer);
-		if (res) {
-			ctxt->fi.vector     = X86_TRAP_PF;
-			ctxt->fi.error_code = X86_PF_INSTR;
-			ctxt->fi.cr2        = ctxt->regs->ip;
-			return ES_EXCEPTION;
-		}
+	if (!insn_decode_from_regs(&ctxt->insn, ctxt->regs, buffer, res))
+		return ES_DECODE_FAILED;
+
+	ret = ctxt->insn.immediate.got ? ES_OK : ES_DECODE_FAILED;
+
+	return ret;
+}
 
-		insn_init(&ctxt->insn, buffer, MAX_INSN_SIZE, 1);
-		insn_get_length(&ctxt->insn);
+static enum es_result __vc_decode_kern_insn(struct es_em_ctxt *ctxt)
+{
+	char buffer[MAX_INSN_SIZE];
+	enum es_result ret;
+	int res;
+
+	res = vc_fetch_insn_kernel(ctxt, buffer);
+	if (res) {
+		ctxt->fi.vector     = X86_TRAP_PF;
+		ctxt->fi.error_code = X86_PF_INSTR;
+		ctxt->fi.cr2        = ctxt->regs->ip;
+		return ES_EXCEPTION;
 	}
 
+	insn_init(&ctxt->insn, buffer, MAX_INSN_SIZE, 1);
+	insn_get_length(&ctxt->insn);
+
 	ret = ctxt->insn.immediate.got ? ES_OK : ES_DECODE_FAILED;
 
 	return ret;
 }
 
+static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
+{
+	if (user_mode(ctxt->regs))
+		return __vc_decode_user_insn(ctxt);
+	else
+		return __vc_decode_kern_insn(ctxt);
+}
+
 static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
 				   char *dst, char *buf, size_t size)
 {
Signature · 1 line
2.29.2
#31 lore raw

[tip: x86/core] x86/sev-es: Split vc_decode_insn()

The following commit has been merged into the x86/core branch of tip:

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     99e4b0de4d663e247f068bb5e014593b624a4ef0
Gitweb:        https://git.kernel.org/tip/99e4b0de4d663e247f068bb5e014593b624a4ef0
Author:        Borislav Petkov <[email protected]>
AuthorDate:    Tue, 23 Feb 2021 11:28:02 +01:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Mon, 15 Mar 2021 11:40:05 +01:00

x86/sev-es: Split vc_decode_insn()

Split it into two helpers - a user- and a kernel-mode one for readability. Yes, the original function body is not that convoluted but splitting it makes following through that code trivial than having to pay attention to each little difference when in user or in kernel mode.

No functional changes.

---
 arch/x86/kernel/sev-es.c | 59 +++++++++++++++++++++++++--------------
 1 file changed, 38 insertions(+), 21 deletions(-)
arch/x86/kernel/sev-es.c +38 -21
index a28922f..043dc2f 100644
@@ -251,41 +251,58 @@ static int vc_fetch_insn_kernel(struct es_em_ctxt *ctxt,
 	return copy_from_kernel_nofault(buffer, (unsigned char *)ctxt->regs->ip, MAX_INSN_SIZE);
 }
 
-static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
+static enum es_result __vc_decode_user_insn(struct es_em_ctxt *ctxt)
 {
 	char buffer[MAX_INSN_SIZE];
 	enum es_result ret;
 	int res;
 
-	if (user_mode(ctxt->regs)) {
-		res = insn_fetch_from_user_inatomic(ctxt->regs, buffer);
-		if (!res) {
-			ctxt->fi.vector     = X86_TRAP_PF;
-			ctxt->fi.error_code = X86_PF_INSTR | X86_PF_USER;
-			ctxt->fi.cr2        = ctxt->regs->ip;
-			return ES_EXCEPTION;
-		}
+	res = insn_fetch_from_user_inatomic(ctxt->regs, buffer);
+	if (!res) {
+		ctxt->fi.vector     = X86_TRAP_PF;
+		ctxt->fi.error_code = X86_PF_INSTR | X86_PF_USER;
+		ctxt->fi.cr2        = ctxt->regs->ip;
+		return ES_EXCEPTION;
+	}
 
-		if (!insn_decode_from_regs(&ctxt->insn, ctxt->regs, buffer, res))
-			return ES_DECODE_FAILED;
-	} else {
-		res = vc_fetch_insn_kernel(ctxt, buffer);
-		if (res) {
-			ctxt->fi.vector     = X86_TRAP_PF;
-			ctxt->fi.error_code = X86_PF_INSTR;
-			ctxt->fi.cr2        = ctxt->regs->ip;
-			return ES_EXCEPTION;
-		}
+	if (!insn_decode_from_regs(&ctxt->insn, ctxt->regs, buffer, res))
+		return ES_DECODE_FAILED;
+
+	ret = ctxt->insn.immediate.got ? ES_OK : ES_DECODE_FAILED;
+
+	return ret;
+}
 
-		insn_init(&ctxt->insn, buffer, MAX_INSN_SIZE, 1);
-		insn_get_length(&ctxt->insn);
+static enum es_result __vc_decode_kern_insn(struct es_em_ctxt *ctxt)
+{
+	char buffer[MAX_INSN_SIZE];
+	enum es_result ret;
+	int res;
+
+	res = vc_fetch_insn_kernel(ctxt, buffer);
+	if (res) {
+		ctxt->fi.vector     = X86_TRAP_PF;
+		ctxt->fi.error_code = X86_PF_INSTR;
+		ctxt->fi.cr2        = ctxt->regs->ip;
+		return ES_EXCEPTION;
 	}
 
+	insn_init(&ctxt->insn, buffer, MAX_INSN_SIZE, 1);
+	insn_get_length(&ctxt->insn);
+
 	ret = ctxt->insn.immediate.got ? ES_OK : ES_DECODE_FAILED;
 
 	return ret;
 }
 
+static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
+{
+	if (user_mode(ctxt->regs))
+		return __vc_decode_user_insn(ctxt);
+	else
+		return __vc_decode_kern_insn(ctxt);
+}
+
 static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
 				   char *dst, char *buf, size_t size)
 {
author maintainer
#32 lore raw

PATCH v3 13/21 x86/sev-es: Convert to insn_decode()

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

Simplify code, no functional changes.

---
 arch/x86/kernel/sev-es.c | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)
arch/x86/kernel/sev-es.c +10 -12
index e60ab556f51c..d2880a305772 100644
@@ -244,7 +244,6 @@ static int vc_fetch_insn_kernel(struct es_em_ctxt *ctxt,
 static enum es_result __vc_decode_user_insn(struct es_em_ctxt *ctxt)
 {
 	char buffer[MAX_INSN_SIZE];
-	enum es_result ret;
 	int res;
 
 	res = insn_fetch_from_user(ctxt->regs, buffer);
@@ -258,16 +257,16 @@ static enum es_result __vc_decode_user_insn(struct es_em_ctxt *ctxt)
 	if (!insn_decode_from_regs(&ctxt->insn, ctxt->regs, buffer, res))
 		return ES_DECODE_FAILED;
 
-	ret = ctxt->insn.immediate.got ? ES_OK : ES_DECODE_FAILED;
-
-	return ret;
+	if (ctxt->insn.immediate.got)
+		return ES_OK;
+	else
+		return ES_DECODE_FAILED;
 }
 
 static enum es_result __vc_decode_kern_insn(struct es_em_ctxt *ctxt)
 {
 	char buffer[MAX_INSN_SIZE];
-	enum es_result ret;
-	int res;
+	int res, ret;
 
 	res = vc_fetch_insn_kernel(ctxt, buffer);
 	if (res) {
@@ -277,12 +276,11 @@ static enum es_result __vc_decode_kern_insn(struct es_em_ctxt *ctxt)
 		return ES_EXCEPTION;
 	}
 
-	insn_init(&ctxt->insn, buffer, MAX_INSN_SIZE, 1);
-	insn_get_length(&ctxt->insn);
-
-	ret = ctxt->insn.immediate.got ? ES_OK : ES_DECODE_FAILED;
-
-	return ret;
+	ret = insn_decode(&ctxt->insn, buffer, MAX_INSN_SIZE, INSN_MODE_64);
+	if (ret < 0)
+		return ES_DECODE_FAILED;
+	else
+		return ES_OK;
 }
 
 static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
Signature · 1 line
2.29.2
#33 lore raw

[tip: x86/core] x86/sev-es: Convert to insn_decode()

The following commit has been merged into the x86/core branch of tip:

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     5e32c64bb6912bdddc05216655dd37e848b717af
Gitweb:        https://git.kernel.org/tip/5e32c64bb6912bdddc05216655dd37e848b717af
Author:        Borislav Petkov <[email protected]>
AuthorDate:    Mon, 16 Nov 2020 18:21:23 +01:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Mon, 15 Mar 2021 11:46:40 +01:00

x86/sev-es: Convert to insn_decode()

Simplify code, no functional changes.

---
 arch/x86/kernel/sev-es.c | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)
arch/x86/kernel/sev-es.c +10 -12
index 043dc2f..75c7df3 100644
@@ -254,7 +254,6 @@ static int vc_fetch_insn_kernel(struct es_em_ctxt *ctxt,
 static enum es_result __vc_decode_user_insn(struct es_em_ctxt *ctxt)
 {
 	char buffer[MAX_INSN_SIZE];
-	enum es_result ret;
 	int res;
 
 	res = insn_fetch_from_user_inatomic(ctxt->regs, buffer);
@@ -268,16 +267,16 @@ static enum es_result __vc_decode_user_insn(struct es_em_ctxt *ctxt)
 	if (!insn_decode_from_regs(&ctxt->insn, ctxt->regs, buffer, res))
 		return ES_DECODE_FAILED;
 
-	ret = ctxt->insn.immediate.got ? ES_OK : ES_DECODE_FAILED;
-
-	return ret;
+	if (ctxt->insn.immediate.got)
+		return ES_OK;
+	else
+		return ES_DECODE_FAILED;
 }
 
 static enum es_result __vc_decode_kern_insn(struct es_em_ctxt *ctxt)
 {
 	char buffer[MAX_INSN_SIZE];
-	enum es_result ret;
-	int res;
+	int res, ret;
 
 	res = vc_fetch_insn_kernel(ctxt, buffer);
 	if (res) {
@@ -287,12 +286,11 @@ static enum es_result __vc_decode_kern_insn(struct es_em_ctxt *ctxt)
 		return ES_EXCEPTION;
 	}
 
-	insn_init(&ctxt->insn, buffer, MAX_INSN_SIZE, 1);
-	insn_get_length(&ctxt->insn);
-
-	ret = ctxt->insn.immediate.got ? ES_OK : ES_DECODE_FAILED;
-
-	return ret;
+	ret = insn_decode(&ctxt->insn, buffer, MAX_INSN_SIZE, INSN_MODE_64);
+	if (ret < 0)
+		return ES_DECODE_FAILED;
+	else
+		return ES_OK;
 }
 
 static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
author maintainer
#34 lore raw

PATCH v3 14/21 x86/traps: Convert to insn_decode()

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

Simplify code, no functional changes.

---
 arch/x86/kernel/traps.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/x86/kernel/traps.c +4 -3
index 7f5aec758f0e..c2a08c4c1872 100644
@@ -498,14 +498,15 @@ static enum kernel_gp_hint get_kernel_gp_address(struct pt_regs *regs,
 {
 	u8 insn_buf[MAX_INSN_SIZE];
 	struct insn insn;
+	int ret;
 
 	if (copy_from_kernel_nofault(insn_buf, (void *)regs->ip,
 			MAX_INSN_SIZE))
 		return GP_NO_HINT;
 
-	kernel_insn_init(&insn, insn_buf, MAX_INSN_SIZE);
-	insn_get_modrm(&insn);
-	insn_get_sib(&insn);
+	ret = insn_decode(&insn, insn_buf, MAX_INSN_SIZE, INSN_MODE_KERN);
+	if (ret < 0)
+		return GP_NO_HINT;
 
 	*addr = (unsigned long)insn_get_addr_ref(&insn, regs);
 	if (*addr == -1UL)
Signature · 1 line
2.29.2
#35 lore raw

[tip: x86/core] x86/traps: Convert to insn_decode()

The following commit has been merged into the x86/core branch of tip:

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     0be7f42d6fcce111f487283d596594c6da6588b0
Gitweb:        https://git.kernel.org/tip/0be7f42d6fcce111f487283d596594c6da6588b0
Author:        Borislav Petkov <[email protected]>
AuthorDate:    Mon, 16 Nov 2020 18:38:45 +01:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Mon, 15 Mar 2021 12:00:18 +01:00

x86/traps: Convert to insn_decode()

Simplify code, no functional changes.

---
 arch/x86/kernel/traps.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/x86/kernel/traps.c +4 -3
index ac1874a..0da8d2a 100644
@@ -498,14 +498,15 @@ static enum kernel_gp_hint get_kernel_gp_address(struct pt_regs *regs,
 {
 	u8 insn_buf[MAX_INSN_SIZE];
 	struct insn insn;
+	int ret;
 
 	if (copy_from_kernel_nofault(insn_buf, (void *)regs->ip,
 			MAX_INSN_SIZE))
 		return GP_NO_HINT;
 
-	kernel_insn_init(&insn, insn_buf, MAX_INSN_SIZE);
-	insn_get_modrm(&insn);
-	insn_get_sib(&insn);
+	ret = insn_decode(&insn, insn_buf, MAX_INSN_SIZE, INSN_MODE_KERN);
+	if (ret < 0)
+		return GP_NO_HINT;
 
 	*addr = (unsigned long)insn_get_addr_ref(&insn, regs);
 	if (*addr == -1UL)
author maintainer
#36 lore raw

PATCH v3 15/21 x86/uprobes: Convert to insn_decode()

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

Simplify code, no functional changes.

---
 arch/x86/kernel/uprobes.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/kernel/uprobes.c +4 -4
index a2b413394917..b63cf8f7745e 100644
@@ -276,12 +276,12 @@ static bool is_prefix_bad(struct insn *insn)
 
 static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn, bool x86_64)
 {
+	enum insn_mode m = x86_64 ? INSN_MODE_64 : INSN_MODE_32;
 	u32 volatile *good_insns;
+	int ret;
 
-	insn_init(insn, auprobe->insn, sizeof(auprobe->insn), x86_64);
-	/* has the side-effect of processing the entire instruction */
-	insn_get_length(insn);
-	if (!insn_complete(insn))
+	ret = insn_decode(insn, auprobe->insn, sizeof(auprobe->insn), m);
+	if (ret < 0)
 		return -ENOEXEC;
 
 	if (is_prefix_bad(insn))
Signature · 1 line
2.29.2
#37 lore raw

[tip: x86/core] x86/uprobes: Convert to insn_decode()

The following commit has been merged into the x86/core branch of tip:

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     88afc23922137cd3efdb0f0b6722785c9f6a35eb
Gitweb:        https://git.kernel.org/tip/88afc23922137cd3efdb0f0b6722785c9f6a35eb
Author:        Borislav Petkov <[email protected]>
AuthorDate:    Tue, 17 Nov 2020 15:26:12 +01:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Mon, 15 Mar 2021 12:05:03 +01:00

x86/uprobes: Convert to insn_decode()

Simplify code, no functional changes.

---
 arch/x86/kernel/uprobes.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/kernel/uprobes.c +4 -4
index a2b4133..b63cf8f 100644
@@ -276,12 +276,12 @@ static bool is_prefix_bad(struct insn *insn)
 
 static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn, bool x86_64)
 {
+	enum insn_mode m = x86_64 ? INSN_MODE_64 : INSN_MODE_32;
 	u32 volatile *good_insns;
+	int ret;
 
-	insn_init(insn, auprobe->insn, sizeof(auprobe->insn), x86_64);
-	/* has the side-effect of processing the entire instruction */
-	insn_get_length(insn);
-	if (!insn_complete(insn))
+	ret = insn_decode(insn, auprobe->insn, sizeof(auprobe->insn), m);
+	if (ret < 0)
 		return -ENOEXEC;
 
 	if (is_prefix_bad(insn))
author maintainer
#38 lore raw

PATCH v3 16/21 x86/tools/insn_decoder_test: Convert to insn_decode()

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

Simplify code, no functional changes.

---
 arch/x86/tools/insn_decoder_test.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
arch/x86/tools/insn_decoder_test.c +6 -4
index 34eda63c124b..472540aeabc2 100644
@@ -120,7 +120,7 @@ int main(int argc, char **argv)
 
 	while (fgets(line, BUFSIZE, stdin)) {
 		char copy[BUFSIZE], *s, *tab1, *tab2;
-		int nb = 0;
+		int nb = 0, ret;
 		unsigned int b;
 
 		if (line[0] == '<') {
@@ -148,10 +148,12 @@ int main(int argc, char **argv)
 			} else
 				break;
 		}
+
 		/* Decode an instruction */
-		insn_init(&insn, insn_buff, sizeof(insn_buff), x86_64);
-		insn_get_length(&insn);
-		if (insn.length != nb) {
+		ret = insn_decode(&insn, insn_buff, sizeof(insn_buff),
+				  x86_64 ? INSN_MODE_64 : INSN_MODE_32);
+
+		if (ret < 0 || insn.length != nb) {
 			warnings++;
 			pr_warn("Found an x86 instruction decoder bug, "
 				"please report this.\n", sym);
Signature · 1 line
2.29.2
#39 lore raw

[tip: x86/core] x86/tools/insn_decoder_test: Convert to insn_decode()

The following commit has been merged into the x86/core branch of tip:

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     0c925c61dae18ee3cb93a61cc9dd9562a066034d
Gitweb:        https://git.kernel.org/tip/0c925c61dae18ee3cb93a61cc9dd9562a066034d
Author:        Borislav Petkov <[email protected]>
AuthorDate:    Fri, 20 Nov 2020 15:01:20 +01:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Mon, 15 Mar 2021 12:09:09 +01:00

x86/tools/insn_decoder_test: Convert to insn_decode()

Simplify code, no functional changes.

---
 arch/x86/tools/insn_decoder_test.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
arch/x86/tools/insn_decoder_test.c +6 -4
index 34eda63..472540a 100644
@@ -120,7 +120,7 @@ int main(int argc, char **argv)
 
 	while (fgets(line, BUFSIZE, stdin)) {
 		char copy[BUFSIZE], *s, *tab1, *tab2;
-		int nb = 0;
+		int nb = 0, ret;
 		unsigned int b;
 
 		if (line[0] == '<') {
@@ -148,10 +148,12 @@ int main(int argc, char **argv)
 			} else
 				break;
 		}
+
 		/* Decode an instruction */
-		insn_init(&insn, insn_buff, sizeof(insn_buff), x86_64);
-		insn_get_length(&insn);
-		if (insn.length != nb) {
+		ret = insn_decode(&insn, insn_buff, sizeof(insn_buff),
+				  x86_64 ? INSN_MODE_64 : INSN_MODE_32);
+
+		if (ret < 0 || insn.length != nb) {
 			warnings++;
 			pr_warn("Found an x86 instruction decoder bug, "
 				"please report this.\n", sym);
author maintainer
#40 lore raw

PATCH v3 17/21 tools/objtool: Convert to insn_decode()

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

Simplify code, no functional changes.

---
 tools/objtool/arch/x86/decode.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
tools/objtool/arch/x86/decode.c +4 -5
index 549813cff8ab..8380d0b1d933 100644
@@ -90,7 +90,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
 			    struct list_head *ops_list)
 {
 	struct insn insn;
-	int x86_64, sign;
+	int x86_64, sign, ret;
 	unsigned char op1, op2, rex = 0, rex_b = 0, rex_r = 0, rex_w = 0,
 		      rex_x = 0, modrm = 0, modrm_mod = 0, modrm_rm = 0,
 		      modrm_reg = 0, sib = 0;
@@ -101,10 +101,9 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
 	if (x86_64 == -1)
 		return -1;
 
-	insn_init(&insn, sec->data->d_buf + offset, maxlen, x86_64);
-	insn_get_length(&insn);
-
-	if (!insn_complete(&insn)) {
+	ret = insn_decode(&insn, sec->data->d_buf + offset, maxlen,
+			  x86_64 ? INSN_MODE_64 : INSN_MODE_32);
+	if (ret < 0) {
 		WARN("can't decode instruction at %s:0x%lx", sec->name, offset);
 		return -1;
 	}
Signature · 1 line
2.29.2
maintainer
#41 lore raw

Re: [PATCH v3 17/21] tools/objtool: Convert to insn_decode()

Quoted 14 lines · Borislav Petkov

From: Borislav Petkov <[email protected]>

Simplify code, no functional changes.

---
 tools/objtool/arch/x86/decode.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
tools/objtool/arch/x86/decode.c +0 -0
index 549813cff8ab..8380d0b1d933 100644

tools/objtool/arch/x86/decode.c int arch_decode_instruction(const struct elf *elf, const struct section *sec, · line 96

Quoted 8 lines
tools/objtool/arch/x86/decode.c +1 -1
@@ -90,7 +90,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
 			    struct list_head *ops_list)
 {
 	struct insn insn;
-	int x86_64, sign;
+	int x86_64, sign, ret;
 	unsigned char op1, op2, rex = 0, rex_b = 0, rex_r = 0, rex_w = 0,
 		      rex_x = 0, modrm = 0, modrm_mod = 0, modrm_rm = 0,
 		      modrm_reg = 0, sib = 0;

This is going to have trivial rejects/fuzz against tip/objtool/core.

author maintainer
#42 lore raw

Re: [PATCH v3 17/21] tools/objtool: Convert to insn_decode()

Quoted 1 line · Peter Zijlstra

This is going to have trivial rejects/fuzz against tip/objtool/core.

I was just wondering whether to you show you how I resolved :)

tools/objtool/arch/x86/decode.c +0 -0
diff --cc tools/objtool/arch/x86/decode.c
index 431bafb881d4,8380d0b1d933..22a53ee322ea
@@@ -104,14 -90,12 +104,14 @@@ int arch_decode_instruction(const struc
  			    struct list_head *ops_list)
  {
  	struct insn insn;
- 	int x86_64;
 -	int x86_64, sign, ret;
 -	unsigned char op1, op2, rex = 0, rex_b = 0, rex_r = 0, rex_w = 0,
 -		      rex_x = 0, modrm = 0, modrm_mod = 0, modrm_rm = 0,
 -		      modrm_reg = 0, sib = 0;
++	int x86_64, ret;
 +	unsigned char op1, op2,
 +		      rex = 0, rex_b = 0, rex_r = 0, rex_w = 0, rex_x = 0,
 +		      modrm = 0, modrm_mod = 0, modrm_rm = 0, modrm_reg = 0,
 +		      sib = 0, /* sib_scale = 0, */ sib_index = 0, sib_base = 0;
  	struct stack_op *op = NULL;
  	struct symbol *sym;
 +	u64 imm;
  
  	x86_64 = is_x86_64(elf);
  	if (x86_64 == -1)
Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
maintainer
#43 lore raw

Re: [PATCH v3 17/21] tools/objtool: Convert to insn_decode()

Quoted 4 lines · Borislav Petkov
Quoted 1 line · Peter Zijlstra

This is going to have trivial rejects/fuzz against tip/objtool/core.

I was just wondering whether to you show you how I resolved :)

Looks good, thanks!

#44 lore raw

[tip: x86/core] tools/objtool: Convert to insn_decode()

In reply to Borislav Petkov
The following commit has been merged into the x86/core branch of tip:

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     c7e41b099be40112d53daccef8553e99e455e0d6
Gitweb:        https://git.kernel.org/tip/c7e41b099be40112d53daccef8553e99e455e0d6
Author:        Borislav Petkov <[email protected]>
AuthorDate:    Fri, 20 Nov 2020 17:37:06 +01:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Mon, 15 Mar 2021 12:10:41 +01:00

tools/objtool: Convert to insn_decode()

Simplify code, no functional changes.

---
 tools/objtool/arch/x86/decode.c |  9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
tools/objtool/arch/x86/decode.c +4 -5
index 549813c..8380d0b 100644
@@ -90,7 +90,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
 			    struct list_head *ops_list)
 {
 	struct insn insn;
-	int x86_64, sign;
+	int x86_64, sign, ret;
 	unsigned char op1, op2, rex = 0, rex_b = 0, rex_r = 0, rex_w = 0,
 		      rex_x = 0, modrm = 0, modrm_mod = 0, modrm_rm = 0,
 		      modrm_reg = 0, sib = 0;
@@ -101,10 +101,9 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
 	if (x86_64 == -1)
 		return -1;
 
-	insn_init(&insn, sec->data->d_buf + offset, maxlen, x86_64);
-	insn_get_length(&insn);
-
-	if (!insn_complete(&insn)) {
+	ret = insn_decode(&insn, sec->data->d_buf + offset, maxlen,
+			  x86_64 ? INSN_MODE_64 : INSN_MODE_32);
+	if (ret < 0) {
 		WARN("can't decode instruction at %s:0x%lx", sec->name, offset);
 		return -1;
 	}
author maintainer
#45 lore raw

PATCH v3 18/21 x86/tools/insn_sanity: Convert to insn_decode()

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

Simplify code, no functional changes.

---
 arch/x86/tools/insn_sanity.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/tools/insn_sanity.c +4 -4
index c6a0000ae635..213f35f94feb 100644
@@ -218,8 +218,8 @@ static void parse_args(int argc, char **argv)
 
 int main(int argc, char **argv)
 {
+	int insns = 0, ret;
 	struct insn insn;
-	int insns = 0;
 	int errors = 0;
 	unsigned long i;
 	unsigned char insn_buff[MAX_INSN_SIZE * 2];
@@ -237,15 +237,15 @@ int main(int argc, char **argv)
 			continue;
 
 		/* Decode an instruction */
-		insn_init(&insn, insn_buff, sizeof(insn_buff), x86_64);
-		insn_get_length(&insn);
+		ret = insn_decode(&insn, insn_buff, sizeof(insn_buff),
+				  x86_64 ? INSN_MODE_64 : INSN_MODE_32);
 
 		if (insn.next_byte <= insn.kaddr ||
 		    insn.kaddr + MAX_INSN_SIZE < insn.next_byte) {
 			/* Access out-of-range memory */
 			dump_stream(stderr, "Error: Found an access violation", i, insn_buff, &insn);
 			errors++;
-		} else if (verbose && !insn_complete(&insn))
+		} else if (verbose && ret < 0)
 			dump_stream(stdout, "Info: Found an undecodable input", i, insn_buff, &insn);
 		else if (verbose >= 2)
 			dump_insn(stdout, &insn);
Signature · 1 line
2.29.2
#46 lore raw

[tip: x86/core] x86/tools/insn_sanity: Convert to insn_decode()

The following commit has been merged into the x86/core branch of tip:

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     a277ce601cd1c75412a82dfcff547b3173098ef0
Gitweb:        https://git.kernel.org/tip/a277ce601cd1c75412a82dfcff547b3173098ef0
Author:        Borislav Petkov <[email protected]>
AuthorDate:    Sun, 22 Nov 2020 18:11:10 +01:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Mon, 15 Mar 2021 12:21:11 +01:00

x86/tools/insn_sanity: Convert to insn_decode()

Simplify code, no functional changes.

---
 arch/x86/tools/insn_sanity.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/tools/insn_sanity.c +4 -4
index c6a0000..213f35f 100644
@@ -218,8 +218,8 @@ static void parse_args(int argc, char **argv)
 
 int main(int argc, char **argv)
 {
+	int insns = 0, ret;
 	struct insn insn;
-	int insns = 0;
 	int errors = 0;
 	unsigned long i;
 	unsigned char insn_buff[MAX_INSN_SIZE * 2];
@@ -237,15 +237,15 @@ int main(int argc, char **argv)
 			continue;
 
 		/* Decode an instruction */
-		insn_init(&insn, insn_buff, sizeof(insn_buff), x86_64);
-		insn_get_length(&insn);
+		ret = insn_decode(&insn, insn_buff, sizeof(insn_buff),
+				  x86_64 ? INSN_MODE_64 : INSN_MODE_32);
 
 		if (insn.next_byte <= insn.kaddr ||
 		    insn.kaddr + MAX_INSN_SIZE < insn.next_byte) {
 			/* Access out-of-range memory */
 			dump_stream(stderr, "Error: Found an access violation", i, insn_buff, &insn);
 			errors++;
-		} else if (verbose && !insn_complete(&insn))
+		} else if (verbose && ret < 0)
 			dump_stream(stdout, "Info: Found an undecodable input", i, insn_buff, &insn);
 		else if (verbose >= 2)
 			dump_insn(stdout, &insn);
author maintainer
#47 lore raw

PATCH v3 19/21 tools/perf: Convert to insn_decode()

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

Simplify code, no functional changes.

Cc Arnaldo Carvalho de Melo <[email protected]>
---
 tools/perf/arch/x86/tests/insn-x86.c            |  9 ++++-----
 tools/perf/arch/x86/util/archinsn.c             |  9 +++++----
 .../intel-pt-decoder/intel-pt-insn-decoder.c    | 17 ++++++++++-------
 3 files changed, 19 insertions(+), 16 deletions(-)
tools/perf/arch/x86/tests/insn-x86.c +4 -5
index f782ef8c5982..049bcbc69eea 100644
@@ -96,13 +96,12 @@ static int get_branch(const char *branch_str)
 static int test_data_item(struct test_data *dat, int x86_64)
 {
 	struct intel_pt_insn intel_pt_insn;
+	int op, branch, ret;
 	struct insn insn;
-	int op, branch;
 
-	insn_init(&insn, dat->data, MAX_INSN_SIZE, x86_64);
-	insn_get_length(&insn);
-
-	if (!insn_complete(&insn)) {
+	ret = insn_decode(&insn, dat->data, MAX_INSN_SIZE,
+			  x86_64 ? INSN_MODE_64 : INSN_MODE_32);
+	if (ret < 0) {
 		pr_debug("Failed to decode: %s\n", dat->asm_rep);
 		return -1;
 	}
tools/perf/arch/x86/util/archinsn.c +5 -4
index 3e6791531ca5..9fb12e8e67eb 100644
@@ -11,7 +11,7 @@ void arch_fetch_insn(struct perf_sample *sample,
 		     struct machine *machine)
 {
 	struct insn insn;
-	int len;
+	int len, ret;
 	bool is64bit = false;
 
 	if (!sample->ip)
@@ -19,8 +19,9 @@ void arch_fetch_insn(struct perf_sample *sample,
 	len = thread__memcpy(thread, machine, sample->insn, sample->ip, sizeof(sample->insn), &is64bit);
 	if (len <= 0)
 		return;
-	insn_init(&insn, sample->insn, len, is64bit);
-	insn_get_length(&insn);
-	if (insn_complete(&insn) && insn.length <= len)
+
+	ret = insn_decode(&insn, sample->insn, len,
+			  is64bit ? INSN_MODE_64 : INSN_MODE_32);
+	if (ret >= 0 && insn.length <= len)
 		sample->insn_len = insn.length;
 }
tools/perf/util/intel-pt-decoder/intel-pt-insn-decoder.c +10 -7
index 2f6cc7eea251..593f20e9774c 100644
@@ -169,11 +169,13 @@ int intel_pt_get_insn(const unsigned char *buf, size_t len, int x86_64,
 		      struct intel_pt_insn *intel_pt_insn)
 {
 	struct insn insn;
+	int ret;
 
-	insn_init(&insn, buf, len, x86_64);
-	insn_get_length(&insn);
-	if (!insn_complete(&insn) || insn.length > len)
+	ret = insn_decode(&insn, buf, len,
+			  x86_64 ? INSN_MODE_64 : INSN_MODE_32);
+	if (ret < 0 || insn.length > len)
 		return -1;
+
 	intel_pt_insn_decoder(&insn, intel_pt_insn);
 	if (insn.length < INTEL_PT_INSN_BUF_SZ)
 		memcpy(intel_pt_insn->buf, buf, insn.length);
@@ -194,12 +196,13 @@ const char *dump_insn(struct perf_insn *x, uint64_t ip __maybe_unused,
 		      u8 *inbuf, int inlen, int *lenp)
 {
 	struct insn insn;
-	int n, i;
+	int n, i, ret;
 	int left;
 
-	insn_init(&insn, inbuf, inlen, x->is64bit);
-	insn_get_length(&insn);
-	if (!insn_complete(&insn) || insn.length > inlen)
+	ret = insn_decode(&insn, inbuf, inlen,
+			  x->is64bit ? INSN_MODE_64 : INSN_MODE_32);
+
+	if (ret < 0 || insn.length > inlen)
 		return "<bad>";
 	if (lenp)
 		*lenp = insn.length;
Signature · 1 line
2.29.2
#48 lore raw

[tip: x86/core] tools/perf: Convert to insn_decode()

The following commit has been merged into the x86/core branch of tip:

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     62660b0fd238253aff951479a2adf1f06a231422
Gitweb:        https://git.kernel.org/tip/62660b0fd238253aff951479a2adf1f06a231422
Author:        Borislav Petkov <[email protected]>
AuthorDate:    Mon, 23 Nov 2020 23:00:16 +01:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Mon, 15 Mar 2021 12:41:26 +01:00

tools/perf: Convert to insn_decode()

Simplify code, no functional changes.

---
 tools/perf/arch/x86/tests/insn-x86.c                     |  9 +---
 tools/perf/arch/x86/util/archinsn.c                      |  9 ++--
 tools/perf/util/intel-pt-decoder/intel-pt-insn-decoder.c | 17 ++++---
 3 files changed, 19 insertions(+), 16 deletions(-)
tools/perf/arch/x86/tests/insn-x86.c +4 -5
index 4f75ae9..0262b0d 100644
@@ -96,13 +96,12 @@ static int get_branch(const char *branch_str)
 static int test_data_item(struct test_data *dat, int x86_64)
 {
 	struct intel_pt_insn intel_pt_insn;
+	int op, branch, ret;
 	struct insn insn;
-	int op, branch;
 
-	insn_init(&insn, dat->data, MAX_INSN_SIZE, x86_64);
-	insn_get_length(&insn);
-
-	if (!insn_complete(&insn)) {
+	ret = insn_decode(&insn, dat->data, MAX_INSN_SIZE,
+			  x86_64 ? INSN_MODE_64 : INSN_MODE_32);
+	if (ret < 0) {
 		pr_debug("Failed to decode: %s\n", dat->asm_rep);
 		return -1;
 	}
tools/perf/arch/x86/util/archinsn.c +5 -4
index 34d600c..546feda 100644
@@ -11,7 +11,7 @@ void arch_fetch_insn(struct perf_sample *sample,
 		     struct machine *machine)
 {
 	struct insn insn;
-	int len;
+	int len, ret;
 	bool is64bit = false;
 
 	if (!sample->ip)
@@ -19,8 +19,9 @@ void arch_fetch_insn(struct perf_sample *sample,
 	len = thread__memcpy(thread, machine, sample->insn, sample->ip, sizeof(sample->insn), &is64bit);
 	if (len <= 0)
 		return;
-	insn_init(&insn, sample->insn, len, is64bit);
-	insn_get_length(&insn);
-	if (insn_complete(&insn) && insn.length <= len)
+
+	ret = insn_decode(&insn, sample->insn, len,
+			  is64bit ? INSN_MODE_64 : INSN_MODE_32);
+	if (ret >= 0 && insn.length <= len)
 		sample->insn_len = insn.length;
 }
tools/perf/util/intel-pt-decoder/intel-pt-insn-decoder.c +10 -7
index 2f6cc7e..593f20e 100644
@@ -169,11 +169,13 @@ int intel_pt_get_insn(const unsigned char *buf, size_t len, int x86_64,
 		      struct intel_pt_insn *intel_pt_insn)
 {
 	struct insn insn;
+	int ret;
 
-	insn_init(&insn, buf, len, x86_64);
-	insn_get_length(&insn);
-	if (!insn_complete(&insn) || insn.length > len)
+	ret = insn_decode(&insn, buf, len,
+			  x86_64 ? INSN_MODE_64 : INSN_MODE_32);
+	if (ret < 0 || insn.length > len)
 		return -1;
+
 	intel_pt_insn_decoder(&insn, intel_pt_insn);
 	if (insn.length < INTEL_PT_INSN_BUF_SZ)
 		memcpy(intel_pt_insn->buf, buf, insn.length);
@@ -194,12 +196,13 @@ const char *dump_insn(struct perf_insn *x, uint64_t ip __maybe_unused,
 		      u8 *inbuf, int inlen, int *lenp)
 {
 	struct insn insn;
-	int n, i;
+	int n, i, ret;
 	int left;
 
-	insn_init(&insn, inbuf, inlen, x->is64bit);
-	insn_get_length(&insn);
-	if (!insn_complete(&insn) || insn.length > inlen)
+	ret = insn_decode(&insn, inbuf, inlen,
+			  x->is64bit ? INSN_MODE_64 : INSN_MODE_32);
+
+	if (ret < 0 || insn.length > inlen)
 		return "<bad>";
 	if (lenp)
 		*lenp = insn.length;
author maintainer
#49 lore raw

PATCH v3 20/21 x86/insn: Remove kernel_insn_init()

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

Now that it is not needed anymore, drop it.

---
 arch/x86/include/asm/insn.h       | 11 -----------
 tools/arch/x86/include/asm/insn.h | 11 -----------
 2 files changed, 22 deletions(-)
arch/x86/include/asm/insn.h +0 -11
index de9fe760c1e7..5eb3753dce33 100644
@@ -159,17 +159,6 @@ static inline void insn_get_attribute(struct insn *insn)
 /* Instruction uses RIP-relative addressing */
 extern int insn_rip_relative(struct insn *insn);
 
-/* Init insn for kernel text */
-static inline void kernel_insn_init(struct insn *insn,
-				    const void *kaddr, int buf_len)
-{
-#ifdef CONFIG_X86_64
-	insn_init(insn, kaddr, buf_len, 1);
-#else /* CONFIG_X86_32 */
-	insn_init(insn, kaddr, buf_len, 0);
-#endif
-}
-
 static inline int insn_is_avx(struct insn *insn)
 {
 	if (!insn->prefixes.got)
tools/arch/x86/include/asm/insn.h +0 -11
index 2fe19b1a8765..5aae785003dc 100644
@@ -159,17 +159,6 @@ static inline void insn_get_attribute(struct insn *insn)
 /* Instruction uses RIP-relative addressing */
 extern int insn_rip_relative(struct insn *insn);
 
-/* Init insn for kernel text */
-static inline void kernel_insn_init(struct insn *insn,
-				    const void *kaddr, int buf_len)
-{
-#ifdef CONFIG_X86_64
-	insn_init(insn, kaddr, buf_len, 1);
-#else /* CONFIG_X86_32 */
-	insn_init(insn, kaddr, buf_len, 0);
-#endif
-}
-
 static inline int insn_is_avx(struct insn *insn)
 {
 	if (!insn->prefixes.got)
Signature · 1 line
2.29.2
#50 lore raw

[tip: x86/core] x86/insn: Remove kernel_insn_init()

The following commit has been merged into the x86/core branch of tip:

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     404b639e510b36136ef15b08ca8a022845ed87db
Gitweb:        https://git.kernel.org/tip/404b639e510b36136ef15b08ca8a022845ed87db
Author:        Borislav Petkov <[email protected]>
AuthorDate:    Sun, 22 Nov 2020 18:12:37 +01:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Mon, 15 Mar 2021 12:58:36 +01:00

x86/insn: Remove kernel_insn_init()

Now that it is not needed anymore, drop it.

---
 arch/x86/include/asm/insn.h       | 11 -----------
 tools/arch/x86/include/asm/insn.h | 11 -----------
 2 files changed, 22 deletions(-)
arch/x86/include/asm/insn.h +0 -11
index de9fe76..5eb3753 100644
@@ -159,17 +159,6 @@ static inline void insn_get_attribute(struct insn *insn)
 /* Instruction uses RIP-relative addressing */
 extern int insn_rip_relative(struct insn *insn);
 
-/* Init insn for kernel text */
-static inline void kernel_insn_init(struct insn *insn,
-				    const void *kaddr, int buf_len)
-{
-#ifdef CONFIG_X86_64
-	insn_init(insn, kaddr, buf_len, 1);
-#else /* CONFIG_X86_32 */
-	insn_init(insn, kaddr, buf_len, 0);
-#endif
-}
-
 static inline int insn_is_avx(struct insn *insn)
 {
 	if (!insn->prefixes.got)
tools/arch/x86/include/asm/insn.h +0 -11
index 2fe19b1..5aae785 100644
@@ -159,17 +159,6 @@ static inline void insn_get_attribute(struct insn *insn)
 /* Instruction uses RIP-relative addressing */
 extern int insn_rip_relative(struct insn *insn);
 
-/* Init insn for kernel text */
-static inline void kernel_insn_init(struct insn *insn,
-				    const void *kaddr, int buf_len)
-{
-#ifdef CONFIG_X86_64
-	insn_init(insn, kaddr, buf_len, 1);
-#else /* CONFIG_X86_32 */
-	insn_init(insn, kaddr, buf_len, 0);
-#endif
-}
-
 static inline int insn_is_avx(struct insn *insn)
 {
 	if (!insn->prefixes.got)
author maintainer
#51 lore raw

PATCH v3 21/21 x86/insn: Make insn_complete() static

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

... and move it above the only place it is used.

---
 arch/x86/include/asm/insn.h       | 7 -------
 arch/x86/lib/insn.c               | 7 +++++++
 tools/arch/x86/include/asm/insn.h | 7 -------
 tools/arch/x86/lib/insn.c         | 7 +++++++
 4 files changed, 14 insertions(+), 14 deletions(-)
arch/x86/include/asm/insn.h +0 -7
index 5eb3753dce33..f03b6ca7dec6 100644
@@ -178,13 +178,6 @@ static inline int insn_has_emulate_prefix(struct insn *insn)
 	return !!insn->emulate_prefix_size;
 }
 
-/* Ensure this instruction is decoded completely */
-static inline int insn_complete(struct insn *insn)
-{
-	return insn->opcode.got && insn->modrm.got && insn->sib.got &&
-		insn->displacement.got && insn->immediate.got;
-}
-
 static inline insn_byte_t insn_vex_m_bits(struct insn *insn)
 {
 	if (insn->vex_prefix.nbytes == 2)	/* 2 bytes VEX */
arch/x86/lib/insn.c +7 -0
index bb58004497f8..058f19b20465 100644
@@ -714,6 +714,13 @@ int insn_get_length(struct insn *insn)
 	return 0;
 }
 
+/* Ensure this instruction is decoded completely */
+static inline int insn_complete(struct insn *insn)
+{
+	return insn->opcode.got && insn->modrm.got && insn->sib.got &&
+		insn->displacement.got && insn->immediate.got;
+}
+
 /**
  * insn_decode() - Decode an x86 instruction
  * @insn:	&struct insn to be initialized
tools/arch/x86/include/asm/insn.h +0 -7
index 5aae785003dc..c9f3eeebb53b 100644
@@ -178,13 +178,6 @@ static inline int insn_has_emulate_prefix(struct insn *insn)
 	return !!insn->emulate_prefix_size;
 }
 
-/* Ensure this instruction is decoded completely */
-static inline int insn_complete(struct insn *insn)
-{
-	return insn->opcode.got && insn->modrm.got && insn->sib.got &&
-		insn->displacement.got && insn->immediate.got;
-}
-
 static inline insn_byte_t insn_vex_m_bits(struct insn *insn)
 {
 	if (insn->vex_prefix.nbytes == 2)	/* 2 bytes VEX */
tools/arch/x86/lib/insn.c +7 -0
index be2b057f91d4..cd4dedde3265 100644
@@ -714,6 +714,13 @@ int insn_get_length(struct insn *insn)
 	return 0;
 }
 
+/* Ensure this instruction is decoded completely */
+static inline int insn_complete(struct insn *insn)
+{
+	return insn->opcode.got && insn->modrm.got && insn->sib.got &&
+		insn->displacement.got && insn->immediate.got;
+}
+
 /**
  * insn_decode() - Decode an x86 instruction
  * @insn:	&struct insn to be initialized
Signature · 1 line
2.29.2
#52 lore raw

[tip: x86/core] x86/insn: Make insn_complete() static

The following commit has been merged into the x86/core branch of tip:

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     f935178b5c1c32ff803b15892a8ba85a1280cb01
Gitweb:        https://git.kernel.org/tip/f935178b5c1c32ff803b15892a8ba85a1280cb01
Author:        Borislav Petkov <[email protected]>
AuthorDate:    Mon, 23 Nov 2020 23:19:03 +01:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Mon, 15 Mar 2021 13:03:46 +01:00

x86/insn: Make insn_complete() static

... and move it above the only place it is used.

---
 arch/x86/include/asm/insn.h       | 7 -------
 arch/x86/lib/insn.c               | 7 +++++++
 tools/arch/x86/include/asm/insn.h | 7 -------
 tools/arch/x86/lib/insn.c         | 7 +++++++
 4 files changed, 14 insertions(+), 14 deletions(-)
arch/x86/include/asm/insn.h +0 -7
index 5eb3753..f03b6ca 100644
@@ -178,13 +178,6 @@ static inline int insn_has_emulate_prefix(struct insn *insn)
 	return !!insn->emulate_prefix_size;
 }
 
-/* Ensure this instruction is decoded completely */
-static inline int insn_complete(struct insn *insn)
-{
-	return insn->opcode.got && insn->modrm.got && insn->sib.got &&
-		insn->displacement.got && insn->immediate.got;
-}
-
 static inline insn_byte_t insn_vex_m_bits(struct insn *insn)
 {
 	if (insn->vex_prefix.nbytes == 2)	/* 2 bytes VEX */
arch/x86/lib/insn.c +7 -0
index bb58004..058f19b 100644
@@ -714,6 +714,13 @@ int insn_get_length(struct insn *insn)
 	return 0;
 }
 
+/* Ensure this instruction is decoded completely */
+static inline int insn_complete(struct insn *insn)
+{
+	return insn->opcode.got && insn->modrm.got && insn->sib.got &&
+		insn->displacement.got && insn->immediate.got;
+}
+
 /**
  * insn_decode() - Decode an x86 instruction
  * @insn:	&struct insn to be initialized
tools/arch/x86/include/asm/insn.h +0 -7
index 5aae785..c9f3eee 100644
@@ -178,13 +178,6 @@ static inline int insn_has_emulate_prefix(struct insn *insn)
 	return !!insn->emulate_prefix_size;
 }
 
-/* Ensure this instruction is decoded completely */
-static inline int insn_complete(struct insn *insn)
-{
-	return insn->opcode.got && insn->modrm.got && insn->sib.got &&
-		insn->displacement.got && insn->immediate.got;
-}
-
 static inline insn_byte_t insn_vex_m_bits(struct insn *insn)
 {
 	if (insn->vex_prefix.nbytes == 2)	/* 2 bytes VEX */
tools/arch/x86/lib/insn.c +7 -0
index be2b057..cd4dedd 100644
@@ -714,6 +714,13 @@ int insn_get_length(struct insn *insn)
 	return 0;
 }
 
+/* Ensure this instruction is decoded completely */
+static inline int insn_complete(struct insn *insn)
+{
+	return insn->opcode.got && insn->modrm.got && insn->sib.got &&
+		insn->displacement.got && insn->immediate.got;
+}
+
 /**
  * insn_decode() - Decode an x86 instruction
  * @insn:	&struct insn to be initialized