Skip to contentSkip to conversation

kmemcheck: add additional selfchecks

PATCH In review Sasha Levin 4 participants 29 messages Apr 14, 2014 – Apr 18, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/4 kmemcheck: add additional selfchecks

kmemcheck has it's own tiny opcode decoder, and is not using the kernel's decoder for historic reasons.

While the decoder works for more cases, it fails on quite a few opcodes and returns incorrect values, which leads to either a failure to detect an issue, or a false positive.

This patch adds a few of those opcodes: setne, btsl and callq, thus causing selfcheck to fail during boot.

---
 arch/x86/mm/kmemcheck/selftest.c |    9 +++++++++
 1 file changed, 9 insertions(+)
arch/x86/mm/kmemcheck/selftest.c +9 -0
index aef7140..c898d33 100644
@@ -23,6 +23,12 @@ static const struct selftest_opcode selftest_opcodes[] = {
 	{1, "\x66\x0f\xbe\x51\xf8",	"movswq <mem8>, <reg16>"},
 	{1, "\x0f\xbe\x51\xf8",		"movswq <mem8>, <reg32>"},
 
+	/* SETNE */
+	{1, "\x0f\x95\xc0",		"setne <reg8>"},
+
+	/* BTSL */
+	{4, "\x0f\xba\x6b\x10\x00",	"btsl <imm8>, <mem32>"},
+
 #ifdef CONFIG_X86_64
 	/* MOVZX / MOVZXD */
 	{1, "\x49\x0f\xb6\x51\xf8",	"movzbq <mem8>, <reg64>"},
@@ -32,6 +38,9 @@ static const struct selftest_opcode selftest_opcodes[] = {
 	{1, "\x49\x0f\xbe\x51\xf8",	"movsbq <mem8>, <reg64>"},
 	{2, "\x49\x0f\xbf\x51\xf8",	"movsbq <mem16>, <reg64>"},
 	{4, "\x49\x63\x51\xf8",		"movslq <mem32>, <reg64>"},
+
+	/* CALLQ */
+	{8, "\xe8\x00\x00\x00",		"call <mem64>"},
 #endif
 };
 
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH 2/4 x86: Move instruction decoder data into header

Right now we generate data for the instruction decoder and place it as a code file which gets #included directly (yuck).

Instead, make it a header which will also be usable by other code that wants to use the data in there.

---
 arch/x86/Makefile       |    6 ++++++
 arch/x86/lib/Makefile   |    8 +++++---
 arch/x86/lib/inat.c     |    2 +-
 arch/x86/tools/Makefile |    8 ++++----
 4 files changed, 16 insertions(+), 8 deletions(-)
arch/x86/Makefile +6 -0
index 602f57e..26eee4e 100644
@@ -178,6 +178,12 @@ archscripts: scripts_basic
 	$(Q)$(MAKE) $(build)=arch/x86/tools relocs
 
 ###
+# inat instruction table generation
+
+archprepare:
+	$(Q)$(MAKE) $(build)=arch/x86/lib inat_tables
+
+###
 # Syscall table generation
 
 archheaders:
arch/x86/lib/Makefile +5 -3
index eabcb6e..3014da8 100644
@@ -7,12 +7,12 @@ inat_tables_maps = $(srctree)/arch/x86/lib/x86-opcode-map.txt
 quiet_cmd_inat_tables = GEN     $@
       cmd_inat_tables = $(AWK) -f $(inat_tables_script) $(inat_tables_maps) > $@ || rm -f $@
 
-$(obj)/inat-tables.c: $(inat_tables_script) $(inat_tables_maps)
+$(obj)/../include/generated/asm/inat-tables.h: $(inat_tables_script) $(inat_tables_maps)
 	$(call cmd,inat_tables)
 
-$(obj)/inat.o: $(obj)/inat-tables.c
+$(obj)/inat.o: $(obj)/../include/generated/asm/inat-tables.h
 
-clean-files := inat-tables.c
+clean-files := ../include/asm/inat-tables.h
 
 obj-$(CONFIG_SMP) += msr-smp.o cache-smp.o
 
@@ -44,3 +44,5 @@ else
         lib-y += copy_user_64.o copy_user_nocache_64.o
 	lib-y += cmpxchg16b_emu.o
 endif
+
+inat_tables: $(obj)/../include/generated/asm/inat-tables.h
arch/x86/lib/inat.c +1 -1
index c1f01a8..641a996 100644
@@ -21,7 +21,7 @@
 #include <asm/insn.h>
 
 /* Attribute tables are generated from opcode map */
-#include "inat-tables.c"
+#include <asm/inat-tables.h>
 
 /* Attribute search APIs */
 insn_attr_t inat_get_opcode_attribute(insn_byte_t opcode)
arch/x86/tools/Makefile +4 -4
index e812034..e34e7e3 100644
@@ -28,14 +28,14 @@ posttest: $(obj)/test_get_len vmlinux $(obj)/insn_sanity
 hostprogs-y	+= test_get_len insn_sanity
 
 # -I needed for generated C source and C source which in the kernel tree.
-HOSTCFLAGS_test_get_len.o := -Wall -I$(objtree)/arch/x86/lib/ -I$(srctree)/arch/x86/include/uapi/ -I$(srctree)/arch/x86/include/ -I$(srctree)/arch/x86/lib/ -I$(srctree)/include/uapi/
+HOSTCFLAGS_test_get_len.o := -Wall -I$(objtree)/arch/x86/lib/ -I$(srctree)/arch/x86/include/uapi/ -I$(srctree)/arch/x86/include/ -I$(srctree)/arch/x86/lib/ -I$(srctree)/include/uapi/ -I$(srctree)/arch/x86/include/generated/
 
-HOSTCFLAGS_insn_sanity.o := -Wall -I$(objtree)/arch/x86/lib/ -I$(srctree)/arch/x86/include/ -I$(srctree)/arch/x86/lib/ -I$(srctree)/include/
+HOSTCFLAGS_insn_sanity.o := -Wall -I$(objtree)/arch/x86/lib/ -I$(srctree)/arch/x86/include/ -I$(srctree)/arch/x86/lib/ -I$(srctree)/include/ -I$(srctree)/arch/x86/include/generated/
 
 # Dependencies are also needed.
-$(obj)/test_get_len.o: $(srctree)/arch/x86/lib/insn.c $(srctree)/arch/x86/lib/inat.c $(srctree)/arch/x86/include/asm/inat_types.h $(srctree)/arch/x86/include/asm/inat.h $(srctree)/arch/x86/include/asm/insn.h $(objtree)/arch/x86/lib/inat-tables.c
+$(obj)/test_get_len.o: $(srctree)/arch/x86/lib/insn.c $(srctree)/arch/x86/lib/inat.c $(srctree)/arch/x86/include/asm/inat_types.h $(srctree)/arch/x86/include/asm/inat.h $(srctree)/arch/x86/include/asm/insn.h $(objtree)/arch/x86/include/generated/asm/inat-tables.h
 
-$(obj)/insn_sanity.o: $(srctree)/arch/x86/lib/insn.c $(srctree)/arch/x86/lib/inat.c $(srctree)/arch/x86/include/asm/inat_types.h $(srctree)/arch/x86/include/asm/inat.h $(srctree)/arch/x86/include/asm/insn.h $(objtree)/arch/x86/lib/inat-tables.c
+$(obj)/insn_sanity.o: $(srctree)/arch/x86/lib/insn.c $(srctree)/arch/x86/lib/inat.c $(srctree)/arch/x86/include/asm/inat_types.h $(srctree)/arch/x86/include/asm/inat.h $(srctree)/arch/x86/include/asm/insn.h $(objtree)/arch/x86/include/generated/asm/inat-tables.h
 
 HOST_EXTRACFLAGS += -I$(srctree)/tools/include
 hostprogs-y	+= relocs
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [PATCH 2/4] x86: Move instruction decoder data into header

Quoted 5 lines · Sasha Levin

Right now we generate data for the instruction decoder and place it as a code file which gets #included directly (yuck).

Instead, make it a header which will also be usable by other code that wants to use the data in there.

Hmm, making the generated data into a header file may clone the data table instances for each object file. Since the inat table is not so small, I think we'd better just export the tables.

Thank you,

Quoted 89 lines
---
 arch/x86/Makefile       |    6 ++++++
 arch/x86/lib/Makefile   |    8 +++++---
 arch/x86/lib/inat.c     |    2 +-
 arch/x86/tools/Makefile |    8 ++++----
 4 files changed, 16 insertions(+), 8 deletions(-)
arch/x86/Makefile +6 -0
index 602f57e..26eee4e 100644
@@ -178,6 +178,12 @@ archscripts: scripts_basic
 	$(Q)$(MAKE) $(build)=arch/x86/tools relocs
 
 ###
+# inat instruction table generation
+
+archprepare:
+	$(Q)$(MAKE) $(build)=arch/x86/lib inat_tables
+
+###
 # Syscall table generation
 
 archheaders:
arch/x86/lib/Makefile +5 -3
index eabcb6e..3014da8 100644
@@ -7,12 +7,12 @@ inat_tables_maps = $(srctree)/arch/x86/lib/x86-opcode-map.txt
 quiet_cmd_inat_tables = GEN     $@
       cmd_inat_tables = $(AWK) -f $(inat_tables_script) $(inat_tables_maps) > $@ || rm -f $@
 
-$(obj)/inat-tables.c: $(inat_tables_script) $(inat_tables_maps)
+$(obj)/../include/generated/asm/inat-tables.h: $(inat_tables_script) $(inat_tables_maps)
 	$(call cmd,inat_tables)
 
-$(obj)/inat.o: $(obj)/inat-tables.c
+$(obj)/inat.o: $(obj)/../include/generated/asm/inat-tables.h
 
-clean-files := inat-tables.c
+clean-files := ../include/asm/inat-tables.h
 
 obj-$(CONFIG_SMP) += msr-smp.o cache-smp.o
 
@@ -44,3 +44,5 @@ else
         lib-y += copy_user_64.o copy_user_nocache_64.o
 	lib-y += cmpxchg16b_emu.o
 endif
+
+inat_tables: $(obj)/../include/generated/asm/inat-tables.h
arch/x86/lib/inat.c +1 -1
index c1f01a8..641a996 100644
@@ -21,7 +21,7 @@
 #include <asm/insn.h>
 
 /* Attribute tables are generated from opcode map */
-#include "inat-tables.c"
+#include <asm/inat-tables.h>
 
 /* Attribute search APIs */
 insn_attr_t inat_get_opcode_attribute(insn_byte_t opcode)
arch/x86/tools/Makefile +4 -4
index e812034..e34e7e3 100644
@@ -28,14 +28,14 @@ posttest: $(obj)/test_get_len vmlinux $(obj)/insn_sanity
 hostprogs-y	+= test_get_len insn_sanity
 
 # -I needed for generated C source and C source which in the kernel tree.
-HOSTCFLAGS_test_get_len.o := -Wall -I$(objtree)/arch/x86/lib/ -I$(srctree)/arch/x86/include/uapi/ -I$(srctree)/arch/x86/include/ -I$(srctree)/arch/x86/lib/ -I$(srctree)/include/uapi/
+HOSTCFLAGS_test_get_len.o := -Wall -I$(objtree)/arch/x86/lib/ -I$(srctree)/arch/x86/include/uapi/ -I$(srctree)/arch/x86/include/ -I$(srctree)/arch/x86/lib/ -I$(srctree)/include/uapi/ -I$(srctree)/arch/x86/include/generated/
 
-HOSTCFLAGS_insn_sanity.o := -Wall -I$(objtree)/arch/x86/lib/ -I$(srctree)/arch/x86/include/ -I$(srctree)/arch/x86/lib/ -I$(srctree)/include/
+HOSTCFLAGS_insn_sanity.o := -Wall -I$(objtree)/arch/x86/lib/ -I$(srctree)/arch/x86/include/ -I$(srctree)/arch/x86/lib/ -I$(srctree)/include/ -I$(srctree)/arch/x86/include/generated/
 
 # Dependencies are also needed.
-$(obj)/test_get_len.o: $(srctree)/arch/x86/lib/insn.c $(srctree)/arch/x86/lib/inat.c $(srctree)/arch/x86/include/asm/inat_types.h $(srctree)/arch/x86/include/asm/inat.h $(srctree)/arch/x86/include/asm/insn.h $(objtree)/arch/x86/lib/inat-tables.c
+$(obj)/test_get_len.o: $(srctree)/arch/x86/lib/insn.c $(srctree)/arch/x86/lib/inat.c $(srctree)/arch/x86/include/asm/inat_types.h $(srctree)/arch/x86/include/asm/inat.h $(srctree)/arch/x86/include/asm/insn.h $(objtree)/arch/x86/include/generated/asm/inat-tables.h
 
-$(obj)/insn_sanity.o: $(srctree)/arch/x86/lib/insn.c $(srctree)/arch/x86/lib/inat.c $(srctree)/arch/x86/include/asm/inat_types.h $(srctree)/arch/x86/include/asm/inat.h $(srctree)/arch/x86/include/asm/insn.h $(objtree)/arch/x86/lib/inat-tables.c
+$(obj)/insn_sanity.o: $(srctree)/arch/x86/lib/insn.c $(srctree)/arch/x86/lib/inat.c $(srctree)/arch/x86/include/asm/inat_types.h $(srctree)/arch/x86/include/asm/inat.h $(srctree)/arch/x86/include/asm/insn.h $(objtree)/arch/x86/include/generated/asm/inat-tables.h
 
 HOST_EXTRACFLAGS += -I$(srctree)/tools/include
 hostprogs-y	+= relocs
Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
#3 lore raw

Re: [PATCH 2/4] x86: Move instruction decoder data into header

Quoted 9 lines · Masami Hiramatsu
Quoted 5 lines · Sasha Levin
Quoted 5 lines

Right now we generate data for the instruction decoder and place it as a code file which gets #included directly (yuck).

Instead, make it a header which will also be usable by other code that wants to use the data in there.

Hmm, making the generated data into a header file may clone the data table instances for each object file. Since the inat table is not so small, I think we'd better just export the tables.

The tables are defined as static, so the compiler drops them once it detects they are not used.

I feel it would be easier to let the compiler do it's job rather than do optimizations we don't need to do and which will complicate the code quite a bit.

Thanks,
Sasha

#4 lore raw

Re: Re: [PATCH 2/4] x86: Move instruction decoder data into header

Quoted 5 lines · Sasha Levin
Quoted 4 lines · Masami Hiramatsu
Quoted 3 lines · Sasha Levin
Quoted 3 lines

Right now we generate data for the instruction decoder and place it as a code file which gets #included directly (yuck).

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

Instead, make it a header which will also be usable by other code that wants to use the data in there.

Hmm, making the generated data into a header file may clone the data table instances for each object file. Since the inat table is not so small, I think we'd better just export the tables.

The tables are defined as static, so the compiler drops them once it detects they are not used.

No, I meant that if the table is used in the different object files, will the copies of the tables be compiled in several different instances?

And I can't see the part which makes the tables static in this patch...

Quoted 3 lines

I feel it would be easier to let the compiler do it's job rather than do optimizations we don't need to do and which will complicate the code quite a bit.

I haven't tend to optimize it, but just encapsulate it, to hide from other parts.

Thank you,

Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
#5 lore raw

Re: [PATCH 2/4] x86: Move instruction decoder data into header

Quoted 12 lines · Masami Hiramatsu
Quoted 11 lines · Sasha Levin
Quoted 9 lines · Masami Hiramatsu
Quoted 5 lines · Sasha Levin
Quoted 5 lines

Right now we generate data for the instruction decoder and place it as a code file which gets #included directly (yuck).

Instead, make it a header which will also be usable by other code that wants to use the data in there.

Hmm, making the generated data into a header file may clone the data table instances for each object file. Since the inat table is not so small, I think we'd better just export the tables.

Quoted 6 lines
Quoted 2 lines

The tables are defined as static, so the compiler drops them once it detects they are not used.

No, I meant that if the table is used in the different object files, will the copies of the tables be compiled in several different instances?

That's true, but there was and after this patchset there will still be only one user that touches the table. I also doubt that more users will appear since users of the table should be going through the API and not touching it directly, so I don't think it should be a concern at this point.

Quoted 1 line

And I can't see the part which makes the tables static in this patch...

Right, it sneaked to the next patch in this patchset. I'll pull it into this one in the next version.

Quoted 5 lines
Quoted 3 lines

I feel it would be easier to let the compiler do it's job rather than do optimizations we don't need to do and which will complicate the code quite a bit.

I haven't tend to optimize it, but just encapsulate it, to hide from other parts.

We could hide it under #ifdef, but that wouldn't change anything for the user or for the generated code itself.

Splitting code generation into two different files would complicate everything IMO.

Thanks,
Sasha

#6 lore raw

Re: [PATCH 2/4] x86: Move instruction decoder data into header

Quoted 20 lines · Sasha Levin
Quoted 12 lines · Masami Hiramatsu
Quoted 11 lines · Sasha Levin
Quoted 9 lines · Masami Hiramatsu
Quoted 5 lines · Sasha Levin
Quoted 5 lines

Right now we generate data for the instruction decoder and place it as a code file which gets #included directly (yuck).

Instead, make it a header which will also be usable by other code that wants to use the data in there.

Hmm, making the generated data into a header file may clone the data table instances for each object file. Since the inat table is not so small, I think we'd better just export the tables.

Quoted 6 lines
Quoted 2 lines

The tables are defined as static, so the compiler drops them once it detects they are not used.

No, I meant that if the table is used in the different object files, will the copies of the tables be compiled in several different instances?

Quoted 5 lines

That's true, but there was and after this patchset there will still be only one user that touches the table. I also doubt that more users will appear since users of the table should be going through the API and not touching it directly, so I don't think it should be a concern at this point.

Agreed, so I don't like to expose it. If no one need them, then give it to the owner. That must be the safest.

Quoted 9 lines
Quoted 1 line

And I can't see the part which makes the tables static in this patch...

Right, it sneaked to the next patch in this patchset. I'll pull it into this one in the next version.

Quoted 4 lines
Quoted 3 lines

I feel it would be easier to let the compiler do it's job rather than do optimizations we don't need to do and which will complicate the code quite a bit.

Quoted 7 lines
Quoted 1 line

I haven't tend to optimize it, but just encapsulate it, to hide from other parts.

We could hide it under #ifdef, but that wouldn't change anything for the user or for the generated code itself.

Splitting code generation into two different files would complicate everything IMO.

No, mixing data (definitions) and declarations on one header should be avoided, especially that is generated in the build path. I'd like to ask you to split them into header and body. I know it will be harder to implement, but worth to maintain.

Thank you,

Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
#7 lore raw

PATCH 3/4 x86/insn: Extract more information about instructions

In reply to Sasha Levin

arch/x86/lib/x86-opcode-map.txt provides us quite a lot of information about instructions. So far we've discarded information we didn't need to use elsewhere.

This patch extracts two more bits of information about instructions:

- Mnemonic. We'd like to refer to instructions by their mnemonic, and not
by their opcode. This both makes code readable, and less confusing and prone to typos since a single mnemonic may have quite a few different opcodes representing it.

- Memory access size. We're currently decoding the size (in bytes) of an
address size, and operand size. kmemcheck would like to know in addition how many bytes were read/written from/to an address by a given instruction, so we also keep the size of the memory access.

To sum it up, this patch translates more bits from arch/x86/lib/x86-opcode-map.txt into C. There's no new additional information being added to instructions, only what was there before.

---
 arch/x86/include/asm/inat.h          |  106 +++++++++++++++++-----------------
 arch/x86/include/asm/inat_types.h    |    9 ++-
 arch/x86/include/asm/insn.h          |    2 +
 arch/x86/kernel/kprobes/core.c       |   10 ++--
 arch/x86/lib/inat.c                  |   65 ++++++++++++---------
 arch/x86/lib/insn.c                  |   91 ++++++++++++++++++-----------
 arch/x86/tools/gen-insn-attr-x86.awk |   99 ++++++++++++++++++++++++-------
 arch/x86/tools/insn_sanity.c         |    8 +--
 8 files changed, 248 insertions(+), 142 deletions(-)
arch/x86/include/asm/inat.h +54 -52
index 74a2e31..38de08a 100644
@@ -96,126 +96,128 @@
 #define INAT_MAKE_IMM(imm)	(imm << INAT_IMM_OFFS)
 
 /* Attribute search APIs */
-extern insn_attr_t inat_get_opcode_attribute(insn_byte_t opcode);
+extern const insn_attr_t *inat_get_opcode(insn_byte_t opcode);
 extern int inat_get_last_prefix_id(insn_byte_t last_pfx);
-extern insn_attr_t inat_get_escape_attribute(insn_byte_t opcode,
-					     int lpfx_id,
-					     insn_attr_t esc_attr);
-extern insn_attr_t inat_get_group_attribute(insn_byte_t modrm,
+extern const insn_attr_t *inat_get_escape(insn_byte_t opcode, int lpfx_id,
+						insn_flags_t esc_flags);
+extern insn_flags_t inat_get_group_flags(insn_byte_t modrm,
 					    int lpfx_id,
-					    insn_attr_t esc_attr);
-extern insn_attr_t inat_get_avx_attribute(insn_byte_t opcode,
+					    insn_flags_t esc_flags);
+extern const insn_attr_t *inat_get_group(insn_byte_t modrm,
+						int lpfx_id,
+						insn_flags_t esc_flags);
+extern const insn_attr_t *inat_get_avx(insn_byte_t opcode,
 					  insn_byte_t vex_m,
 					  insn_byte_t vex_pp);
 
 /* Attribute checking functions */
-static inline int inat_is_legacy_prefix(insn_attr_t attr)
+static inline int inat_is_legacy_prefix(insn_flags_t flags)
 {
-	attr &= INAT_PFX_MASK;
-	return attr && attr <= INAT_LGCPFX_MAX;
+	flags &= INAT_PFX_MASK;
+	return flags && flags <= INAT_LGCPFX_MAX;
 }
 
-static inline int inat_is_address_size_prefix(insn_attr_t attr)
+static inline int inat_is_address_size_prefix(insn_flags_t flags)
 {
-	return (attr & INAT_PFX_MASK) == INAT_PFX_ADDRSZ;
+	return (flags & INAT_PFX_MASK) == INAT_PFX_ADDRSZ;
 }
 
-static inline int inat_is_operand_size_prefix(insn_attr_t attr)
+static inline int inat_is_operand_size_prefix(insn_flags_t flags)
 {
-	return (attr & INAT_PFX_MASK) == INAT_PFX_OPNDSZ;
+	return (flags & INAT_PFX_MASK) == INAT_PFX_OPNDSZ;
 }
 
-static inline int inat_is_rex_prefix(insn_attr_t attr)
+static inline int inat_is_rex_prefix(insn_flags_t flags)
 {
-	return (attr & INAT_PFX_MASK) == INAT_PFX_REX;
+	return (flags & INAT_PFX_MASK) == INAT_PFX_REX;
 }
 
-static inline int inat_last_prefix_id(insn_attr_t attr)
+static inline int inat_last_prefix_id(insn_flags_t flags)
 {
-	if ((attr & INAT_PFX_MASK) > INAT_LSTPFX_MAX)
+	if ((flags & INAT_PFX_MASK) > INAT_LSTPFX_MAX)
 		return 0;
 	else
-		return attr & INAT_PFX_MASK;
+		return flags & INAT_PFX_MASK;
 }
 
-static inline int inat_is_vex_prefix(insn_attr_t attr)
+static inline int inat_is_vex_prefix(insn_flags_t flags)
 {
-	attr &= INAT_PFX_MASK;
-	return attr == INAT_PFX_VEX2 || attr == INAT_PFX_VEX3;
+	flags &= INAT_PFX_MASK;
+	return flags == INAT_PFX_VEX2 || flags == INAT_PFX_VEX3;
 }
 
-static inline int inat_is_vex3_prefix(insn_attr_t attr)
+static inline int inat_is_vex3_prefix(insn_flags_t flags)
 {
-	return (attr & INAT_PFX_MASK) == INAT_PFX_VEX3;
+	return (flags & INAT_PFX_MASK) == INAT_PFX_VEX3;
 }
 
-static inline int inat_is_escape(insn_attr_t attr)
+static inline int inat_is_escape(insn_flags_t flags)
 {
-	return attr & INAT_ESC_MASK;
+	return flags & INAT_ESC_MASK;
 }
 
-static inline int inat_escape_id(insn_attr_t attr)
+static inline int inat_escape_id(insn_flags_t flags)
 {
-	return (attr & INAT_ESC_MASK) >> INAT_ESC_OFFS;
+	return (flags & INAT_ESC_MASK) >> INAT_ESC_OFFS;
 }
 
-static inline int inat_is_group(insn_attr_t attr)
+static inline int inat_is_group(insn_flags_t flags)
 {
-	return attr & INAT_GRP_MASK;
+	return flags & INAT_GRP_MASK;
 }
 
-static inline int inat_group_id(insn_attr_t attr)
+static inline int inat_group_id(insn_flags_t flags)
 {
-	return (attr & INAT_GRP_MASK) >> INAT_GRP_OFFS;
+	return (flags & INAT_GRP_MASK) >> INAT_GRP_OFFS;
 }
 
-static inline int inat_group_common_attribute(insn_attr_t attr)
+static inline int inat_group_common_flags(insn_flags_t flags)
 {
-	return attr & ~INAT_GRP_MASK;
+	return flags & ~INAT_GRP_MASK;
 }
 
-static inline int inat_has_immediate(insn_attr_t attr)
+static inline int inat_has_immediate(insn_flags_t flags)
 {
-	return attr & INAT_IMM_MASK;
+	return flags & INAT_IMM_MASK;
 }
 
-static inline int inat_immediate_size(insn_attr_t attr)
+static inline int inat_immediate_size(insn_flags_t flags)
 {
-	return (attr & INAT_IMM_MASK) >> INAT_IMM_OFFS;
+	return (flags & INAT_IMM_MASK) >> INAT_IMM_OFFS;
 }
 
-static inline int inat_has_modrm(insn_attr_t attr)
+static inline int inat_has_modrm(insn_flags_t flags)
 {
-	return attr & INAT_MODRM;
+	return flags & INAT_MODRM;
 }
 
-static inline int inat_is_force64(insn_attr_t attr)
+static inline int inat_is_force64(insn_flags_t flags)
 {
-	return attr & INAT_FORCE64;
+	return flags & INAT_FORCE64;
 }
 
-static inline int inat_has_second_immediate(insn_attr_t attr)
+static inline int inat_has_second_immediate(insn_flags_t flags)
 {
-	return attr & INAT_SCNDIMM;
+	return flags & INAT_SCNDIMM;
 }
 
-static inline int inat_has_moffset(insn_attr_t attr)
+static inline int inat_has_moffset(insn_flags_t flags)
 {
-	return attr & INAT_MOFFSET;
+	return flags & INAT_MOFFSET;
 }
 
-static inline int inat_has_variant(insn_attr_t attr)
+static inline int inat_has_variant(insn_flags_t flags)
 {
-	return attr & INAT_VARIANT;
+	return flags & INAT_VARIANT;
 }
 
-static inline int inat_accept_vex(insn_attr_t attr)
+static inline int inat_accept_vex(insn_flags_t flags)
 {
-	return attr & INAT_VEXOK;
+	return flags & INAT_VEXOK;
 }
 
-static inline int inat_must_vex(insn_attr_t attr)
+static inline int inat_must_vex(insn_flags_t flags)
 {
-	return attr & INAT_VEXONLY;
+	return flags & INAT_VEXONLY;
 }
 #endif
arch/x86/include/asm/inat_types.h +8 -1
index cb3c20c..028275a 100644
@@ -22,7 +22,14 @@
  */
 
 /* Instruction attributes */
-typedef unsigned int insn_attr_t;
+typedef unsigned int insn_flags_t;
+
+typedef struct {
+	insn_flags_t	flags;
+	unsigned int	mnemonic;
+	char		mem_bytes;
+} insn_attr_t;
+
 typedef unsigned char insn_byte_t;
 typedef signed int insn_value_t;
 
arch/x86/include/asm/insn.h +2 -0
index 48eb30a..c4076f8 100644
@@ -59,8 +59,10 @@ struct insn {
 	};
 
 	insn_attr_t attr;
+	unsigned int mnemonic;
 	unsigned char opnd_bytes;
 	unsigned char addr_bytes;
+	char mem_bytes;
 	unsigned char length;
 	unsigned char x86_64;
 
arch/x86/kernel/kprobes/core.c +5 -5
index 79a3f96..c9102b6 100644
@@ -141,15 +141,15 @@ void __kprobes synthesize_relcall(void *from, void *to)
  */
 static kprobe_opcode_t *__kprobes skip_prefixes(kprobe_opcode_t *insn)
 {
-	insn_attr_t attr;
+	insn_flags_t flags;
 
-	attr = inat_get_opcode_attribute((insn_byte_t)*insn);
-	while (inat_is_legacy_prefix(attr)) {
+	flags = inat_get_opcode((insn_byte_t)*insn)->flags;
+	while (inat_is_legacy_prefix(flags)) {
 		insn++;
-		attr = inat_get_opcode_attribute((insn_byte_t)*insn);
+		flags = inat_get_opcode((insn_byte_t)*insn)->flags;
 	}
 #ifdef CONFIG_X86_64
-	if (inat_is_rex_prefix(attr))
+	if (inat_is_rex_prefix(flags))
 		insn++;
 #endif
 	return insn;
arch/x86/lib/inat.c +38 -27

115 lines not on this page · raw message

arch/x86/lib/insn.c +58 -33

212 lines not on this page · raw message

arch/x86/tools/gen-insn-attr-x86.awk +79 -20

191 lines not on this page · raw message

arch/x86/tools/insn_sanity.c +4 -4
index 872eb60..377d273 100644
@@ -89,10 +89,10 @@ static void dump_insn(FILE *fp, struct insn *insn)
 	dump_field(fp, "displacement", "\t",	&insn->displacement);
 	dump_field(fp, "immediate1", "\t",	&insn->immediate1);
 	dump_field(fp, "immediate2", "\t",	&insn->immediate2);
-	fprintf(fp, "\t.attr = %x, .opnd_bytes = %d, .addr_bytes = %d,\n",
-		insn->attr, insn->opnd_bytes, insn->addr_bytes);
-	fprintf(fp, "\t.length = %d, .x86_64 = %d, .kaddr = %p}\n",
-		insn->length, insn->x86_64, insn->kaddr);
+	fprintf(fp, "\t.attr.flags = %x, .opnd_bytes = %d, .addr_bytes = %d, .mem_bytes = %d,\n",
+		insn->attr.flags, insn->opnd_bytes, insn->addr_bytes, insn->mem_bytes);
+	fprintf(fp, "\t.length = %d, t.mnemonic = %d, .x86_64 = %d, .kaddr = %p}\n",
+		insn->length, insn->mnemonic, insn->x86_64, insn->kaddr);
 }
 
 static void dump_stream(FILE *fp, const char *msg, unsigned long nr_iter,
Signature · 1 line
1.7.10.4
#8 lore raw

Re: [PATCH 3/4] x86/insn: Extract more information about instructions

Quoted 5 lines · Sasha Levin

arch/x86/lib/x86-opcode-map.txt provides us quite a lot of information about instructions. So far we've discarded information we didn't need to use elsewhere.

This patch extracts two more bits of information about instructions:

These information looks obscure to me. What information (documents) does it based on? Could you give me how would you get it?

Quoted 4 lines

- Mnemonic. We'd like to refer to instructions by their mnemonic, and not
by their opcode. This both makes code readable, and less confusing and prone to typos since a single mnemonic may have quite a few different opcodes representing it.

I don't like to call it as "mnemonic", it is just "operation".

Quoted 4 lines

- Memory access size. We're currently decoding the size (in bytes) of an
address size, and operand size. kmemcheck would like to know in addition how many bytes were read/written from/to an address by a given instruction, so we also keep the size of the memory access.

And also, at least in this time, since the operation/mem_size are only used in kmemcheck, you should generate another table for that in kmemcheck from x86-opcode-map.txt.

Hm, could you check out my private project repository of in-kernel disasm?
https://github.com/mhiramat/linux/tree/inkernel-disasm-20130414

it's a bit outdated, but I think you can do similar thing. :)

arch/x86/include/asm/inat.h line 118

Quoted 9 lines

/* Attribute checking functions */

arch/x86/include/asm/inat.h +3 -3
-static inline int inat_is_legacy_prefix(insn_attr_t attr)
+static inline int inat_is_legacy_prefix(insn_flags_t flags)
 {
-	attr &= INAT_PFX_MASK;
-	return attr && attr <= INAT_LGCPFX_MAX;
+	flags &= INAT_PFX_MASK;
+	return flags && flags <= INAT_LGCPFX_MAX;
 }

Since "inat" stands for "instruction-attribute", it should have insn_attr_t. And,

arch/x86/kernel/kprobes/core.c void __kprobes synthesize_relcall(void *from, void *to) · line 146

Quoted 10 lines
arch/x86/kernel/kprobes/core.c +2 -3
@@ -141,15 +141,15 @@ void __kprobes synthesize_relcall(void *from, void *to)
  */
 static kprobe_opcode_t *__kprobes skip_prefixes(kprobe_opcode_t *insn)
 {
-	insn_attr_t attr;
+	insn_flags_t flags;
 
-	attr = inat_get_opcode_attribute((insn_byte_t)*insn);
-	while (inat_is_legacy_prefix(attr)) {
+	flags = inat_get_opcode((insn_byte_t)*insn)->flags;

Do not refer a member from the return value directly. If it returns NULL, the kernel just crashes!

Quoted 5 lines
arch/x86/tools/gen-insn-attr-x86.awk +1 -0
@@ -61,6 +63,17 @@ BEGIN {
 	imm_flag["Ov"] = "INAT_MOFFSET"
 	imm_flag["Lx"] = "INAT_MAKE_IMM(INAT_IMM_BYTE)"
 
+	mem_expr = "^[EQXY][a-z]"

arch/x86/tools/gen-insn-attr-x86.awk BEGIN { · line 76

Quoted 10 lines
arch/x86/tools/gen-insn-attr-x86.awk +10 -0
+	mem_flag["Ev"] = "-1"
+	mem_flag["Eb"] = "1"
+	mem_flag["Ew"] = "2"
+	mem_flag["Ed"] = "4"
+	mem_flag["Yb"] = "1"
+	mem_flag["Xb"] = "1"
+	mem_flag["Yv"] = "-1"
+	mem_flag["Xv"] = "-1"
+	mem_flag["Qd"] = "8"
+

mem_flag? mem_size?

arch/x86/tools/gen-insn-attr-x86.awk function add_flags(old,new) { · line 259

Quoted 6 lines
arch/x86/tools/gen-insn-attr-x86.awk +1 -1
@@ -232,7 +256,7 @@ function add_flags(old,new) {
 }
 
 # convert operands to flags.
-function convert_operands(count,opnd,       i,j,imm,mod)
+function convert_operands(count,opnd,i,j,imm,mod)

Don't remove this space, that separates input arguments and local variables.

arch/x86/tools/gen-insn-attr-x86.awk function convert_operands(count,opnd, i,j,imm,mod) · line 326

Quoted 10 lines
arch/x86/tools/gen-insn-attr-x86.awk +6 -0
@@ -281,15 +318,23 @@ function convert_operands(count,opnd,       i,j,imm,mod)
 	i = 2
 	while (i <= NF) {
 		opcode = $(i++)
+		if (!(opcode in opcode_list)) {
+			opcode_list[opcode] = opcode
+			gsub(/[^A-Za-z0-9 \t]/, "_", opcode_list[opcode])
+			print "#define INSN_OPC_" opcode_list[opcode] " " opcode_cnt
+			opcode_cnt++
+		}

No, don't do that. Defining some immediate macros in auto-generated header makes code maintenance hard.

BTW, could you make a cover mail for the series?

Thank you,

Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
#9 lore raw

Re: [PATCH 3/4] x86/insn: Extract more information about instructions

Quoted 10 lines · Masami Hiramatsu
Quoted 5 lines · Sasha Levin

arch/x86/lib/x86-opcode-map.txt provides us quite a lot of information about instructions. So far we've discarded information we didn't need to use elsewhere.

This patch extracts two more bits of information about instructions:

These information looks obscure to me. What information (documents) does it based on? Could you give me how would you get it?

Quoted 6 lines
Quoted 4 lines

- Mnemonic. We'd like to refer to instructions by their mnemonic, and not
by their opcode. This both makes code readable, and less confusing and prone to typos since a single mnemonic may have quite a few different opcodes representing it.

I don't like to call it as "mnemonic", it is just "operation".

Ah, I see what you are doing now. Hmm, you'd like to generate a mnemonic-ID and its macros for each opcode, wouldn't you?
Even though, it seems waste of memory that we have both opcode and mnemonic-ID, can you integrate that? for example, you can use insn->opcode.value instead of checking each opcode bytes.

Thank you,

Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
#10 lore raw

Re: [PATCH 3/4] x86/insn: Extract more information about instructions

In reply to Masami Hiramatsu

Thanks for the review!

Some comments below.

Quoted 9 lines · Masami Hiramatsu
Quoted 5 lines · Sasha Levin

arch/x86/lib/x86-opcode-map.txt provides us quite a lot of information about instructions. So far we've discarded information we didn't need to use elsewhere.

This patch extracts two more bits of information about instructions:

These information looks obscure to me. What information (documents) does it based on? Could you give me how would you get it?

It's the two bits described below. Both are extracted from arch/x86/lib/x86-opcode-map.txt based on the Intel instruction manual.

Quoted 5 lines
Quoted 4 lines

- Mnemonic. We'd like to refer to instructions by their mnemonic, and not
by their opcode. This both makes code readable, and less confusing and prone to typos since a single mnemonic may have quite a few different opcodes representing it.

Quoted 7 lines

I don't like to call it as "mnemonic", it is just "operation".

Ah, I see what you are doing now. Hmm, you'd like to generate a mnemonic-ID and its macros for each opcode, wouldn't you?
Even though, it seems waste of memory that we have both opcode and mnemonic-ID, can you integrate that? for example, you can use insn->opcode.value instead of checking each opcode bytes.

Mnemonics don't have 1:1 relationship with opcodes. So, for example, if kmemcheck needs to check (and it does) whether a given instruction is an "ADD", it would need to compare it to 9 different opcodes.

The translation table/code to do that would be bigger than just adding a mnemonic field in the instruction.

Quoted 8 lines
Quoted 4 lines

- Memory access size. We're currently decoding the size (in bytes) of an
address size, and operand size. kmemcheck would like to know in addition how many bytes were read/written from/to an address by a given instruction, so we also keep the size of the memory access.

And also, at least in this time, since the operation/mem_size are only used in kmemcheck, you should generate another table for that in kmemcheck from x86-opcode-map.txt.

I don't want to "teach" kmemcheck to parse x86-opcode-map.txt, that should be something that the instruction API does.

kmemcheck would also be the 3rd in-kernel user of that API, so it's not fair to push it as an exception :)

It's also just one more byte in 'struct insn'...

Quoted 4 lines

Hm, could you check out my private project repository of in-kernel disasm?
https://github.com/mhiramat/linux/tree/inkernel-disasm-20130414

it's a bit outdated, but I think you can do similar thing. :)

Checked it out, it seems to be close to what we need. Some of the differences between that and this patchset are due to the use of the data.

For example, you save mnemonics as strings because you need to print them nicely for the user, while I save them as an ordinal number because I need to match mnemonics to instructions.

Quoted 20 lines

arch/x86/include/asm/inat.h line 118

Quoted 9 lines

/* Attribute checking functions */

arch/x86/include/asm/inat.h +3 -3
-static inline int inat_is_legacy_prefix(insn_attr_t attr)
+static inline int inat_is_legacy_prefix(insn_flags_t flags)
 {
-	attr &= INAT_PFX_MASK;
-	return attr && attr <= INAT_LGCPFX_MAX;
+	flags &= INAT_PFX_MASK;
+	return flags && flags <= INAT_LGCPFX_MAX;
 }

Since "inat" stands for "instruction-attribute", it should have insn_attr_t. And,

Quoted 7 lines
arch/x86/kernel/kprobes/core.c +1 -1
@@ -141,15 +141,15 @@ void __kprobes synthesize_relcall(void *from, void *to)
  */
 static kprobe_opcode_t *__kprobes skip_prefixes(kprobe_opcode_t *insn)
 {
-	insn_attr_t attr;
+	insn_flags_t flags;
 
Quoted 6 lines

arch/x86/kernel/kprobes/core.c void __kprobes synthesize_relcall(void *from, void *to) · line 146

Quoted 3 lines
arch/x86/kernel/kprobes/core.c +1 -2
-	attr = inat_get_opcode_attribute((insn_byte_t)*insn);
-	while (inat_is_legacy_prefix(attr)) {
+	flags = inat_get_opcode((insn_byte_t)*insn)->flags;

Do not refer a member from the return value directly. If it returns NULL, the kernel just crashes!

Right, I'll fix that. Probably by adding a dummy "empty" instruction just so we won't have to deal with too many NULL checks.

Quoted 4 lines
Quoted 4 lines
arch/x86/tools/gen-insn-attr-x86.awk +0 -0
@@ -61,6 +63,17 @@ BEGIN {
 	imm_flag["Ov"] = "INAT_MOFFSET"
 	imm_flag["Lx"] = "INAT_MAKE_IMM(INAT_IMM_BYTE)"
 
Quoted 13 lines

arch/x86/tools/gen-insn-attr-x86.awk BEGIN { · line 76

Quoted 11 lines
arch/x86/tools/gen-insn-attr-x86.awk +11 -0
+	mem_expr = "^[EQXY][a-z]"
+	mem_flag["Ev"] = "-1"
+	mem_flag["Eb"] = "1"
+	mem_flag["Ew"] = "2"
+	mem_flag["Ed"] = "4"
+	mem_flag["Yb"] = "1"
+	mem_flag["Xb"] = "1"
+	mem_flag["Yv"] = "-1"
+	mem_flag["Xv"] = "-1"
+	mem_flag["Qd"] = "8"
+

mem_flag? mem_size?

Yup, that makes more sense. I'll change that.

Quoted 8 lines

arch/x86/tools/gen-insn-attr-x86.awk function add_flags(old,new) { · line 259

Quoted 6 lines
arch/x86/tools/gen-insn-attr-x86.awk +1 -1
@@ -232,7 +256,7 @@ function add_flags(old,new) {
 }
 
 # convert operands to flags.
-function convert_operands(count,opnd,       i,j,imm,mod)
+function convert_operands(count,opnd,i,j,imm,mod)

Don't remove this space, that separates input arguments and local variables.

Will fix.

Quoted 5 lines
Quoted 5 lines
arch/x86/tools/gen-insn-attr-x86.awk +1 -0
@@ -281,15 +318,23 @@ function convert_operands(count,opnd,       i,j,imm,mod)
 	i = 2
 	while (i <= NF) {
 		opcode = $(i++)
+		if (!(opcode in opcode_list)) {
Quoted 8 lines

arch/x86/tools/gen-insn-attr-x86.awk function convert_operands(count,opnd, i,j,imm,mod) · line 326

Quoted 5 lines
arch/x86/tools/gen-insn-attr-x86.awk +5 -0
+			opcode_list[opcode] = opcode
+			gsub(/[^A-Za-z0-9 \t]/, "_", opcode_list[opcode])
+			print "#define INSN_OPC_" opcode_list[opcode] " " opcode_cnt
+			opcode_cnt++
+		}

No, don't do that. Defining some immediate macros in auto-generated header makes code maintenance hard.

Do you have a better suggestion to do the above? The definitions are pretty simple and straightforward ("INSN_OPC_[mnemonic]") so I don't feel they will be difficult to maintain. I'm not generating complex macros here.

Quoted 1 line

BTW, could you make a cover mail for the series?

Sure.

Thanks,
Sasha

#11 lore raw

Re: [PATCH 3/4] x86/insn: Extract more information about instructions

Quoted 5 lines · Sasha Levin

Mnemonics don't have 1:1 relationship with opcodes. So, for example, if kmemcheck needs to check (and it does) whether a given instruction is an "ADD", it would need to compare it to 9 different opcodes.

Excuse me, but on what planet does, for example, it makes sense if a particular instruction is a "MOV", for example? The trend in x86 opcodes have varied over the years and at some points it seems to have been trendy to have very general mnemonics (consider MOV CR, MOV DR) and at some points quite the opposite (hence MOVD, MOVQ, MOVDQA, MOVDQU, MOVAPS, MOVUPS, MOVAPD, MOVUPD, VMOVxxx).

So it is not at all clear that this makes any kind of sense whatsoever, and is more likely just going to be abused.

-hpa

#12 lore raw

Re: [PATCH 3/4] x86/insn: Extract more information about instructions

Quoted 14 lines · H. Peter Anvin
Quoted 5 lines · Sasha Levin

Mnemonics don't have 1:1 relationship with opcodes. So, for example, if kmemcheck needs to check (and it does) whether a given instruction is an "ADD", it would need to compare it to 9 different opcodes.

Excuse me, but on what planet does, for example, it makes sense if a particular instruction is a "MOV", for example? The trend in x86 opcodes have varied over the years and at some points it seems to have been trendy to have very general mnemonics (consider MOV CR, MOV DR) and at some points quite the opposite (hence MOVD, MOVQ, MOVDQA, MOVDQU, MOVAPS, MOVUPS, MOVAPD, MOVUPD, VMOVxxx).

Quoted 2 lines

So it is not at all clear that this makes any kind of sense whatsoever, and is more likely just going to be abused.

Looking at kmemcheck, and "AND" vs "MOV" for example, we need to know if a given instruction is AND because AND may operate on only part of the memory it's accessing to. So some accesses to what kmemcheck sees as "uninitialized memory" are actually valid ones because we don't touch the uninitialized part.

So for kmemcheck, AND and MOV (for example) are different because ANDing a value and MOVing a value mean different things wrt to uninitialized memory.

Yes, if kmemcheck for some reason needs to figure out if an instruction is a MOV variant we'll need to list quite a few mnemonics, but that list will be much shorter and more readable than a corresponding list of opcodes.

Thanks,
Sasha

#13 lore raw

Re: [PATCH 3/4] x86/insn: Extract more information about instructions

Quoted 5 lines · Sasha Levin

Yes, if kmemcheck for some reason needs to figure out if an instruction is a MOV variant we'll need to list quite a few mnemonics, but that list will be much shorter and more readable than a corresponding list of opcodes.

You're completely missing my point. If you are looking at MOV, with 80%+ probability you're doing something very, very wrong, because you will be including instructions that do something completely different from what you thought.

This is true for a lot of the x86 instructions.

-hpa

#14 lore raw

Re: [PATCH 3/4] x86/insn: Extract more information about instructions

Quoted 6 lines · H. Peter Anvin
Quoted 5 lines · Sasha Levin
Quoted 5 lines

Yes, if kmemcheck for some reason needs to figure out if an instruction is a MOV variant we'll need to list quite a few mnemonics, but that list will be much shorter and more readable than a corresponding list of opcodes.

Quoted 6 lines

You're completely missing my point. If you are looking at MOV, with 80%+ probability you're doing something very, very wrong, because you will be including instructions that do something completely different from what you thought.

This is true for a lot of the x86 instructions.

Right, but assuming that the AND example I presented earlier makes sense, I can't create mnemonic entries only for instructions where doing so would "probably" be right.

If there are use cases where working with mnemonics is correct, we should be doing that in kmemcheck. If the way kmemcheck deals with mnemonics is incorrect we should go ahead and fix kmemcheck.

Thanks,
Sasha

#15 lore raw

Re: [PATCH 3/4] x86/insn: Extract more information about instructions

I really wonder if it makes sense...

Quoted 32 lines · Sasha Levin
Quoted 3 lines · H. Peter Anvin
Quoted 2 lines · Sasha Levin
Quoted 2 lines

Yes, if kmemcheck for some reason needs to figure out if an

instruction

Quoted 1 line
Quoted 1 line
Quoted 1 line

is a MOV variant we'll need to list quite a few mnemonics, but

that list

Quoted 1 line
Quoted 1 line
Quoted 1 line

will be much shorter and more readable than a corresponding list

of opcodes.

Quoted 7 lines
Quoted 1 line
Quoted 1 line

You're completely missing my point. If you are looking at MOV, with 80%+ probability you're doing something very, very wrong, because you will be including instructions that do something completely different from what you thought.

This is true for a lot of the x86 instructions.

Right, but assuming that the AND example I presented earlier makes sense, I
can't create mnemonic entries only for instructions where doing so would
"probably" be right.

If there are use cases where working with mnemonics is correct, we should
be doing that in kmemcheck. If the way kmemcheck deals with mnemonics is
incorrect we should go ahead and fix kmemcheck.

Thanks,
Sasha

Signature · 1 line
Sent from my mobile phone.  Please pardon brevity and lack of formatting.
#16 lore raw

Re: Re: [PATCH 3/4] x86/insn: Extract more information about instructions

In reply to Sasha Levin
Quoted 14 lines · Sasha Levin
Quoted 6 lines · H. Peter Anvin
Quoted 5 lines · Sasha Levin
Quoted 5 lines

Yes, if kmemcheck for some reason needs to figure out if an instruction is a MOV variant we'll need to list quite a few mnemonics, but that list will be much shorter and more readable than a corresponding list of opcodes.

Quoted 6 lines

You're completely missing my point. If you are looking at MOV, with 80%+ probability you're doing something very, very wrong, because you will be including instructions that do something completely different from what you thought.

This is true for a lot of the x86 instructions.

Quoted 7 lines

Right, but assuming that the AND example I presented earlier makes sense, I can't create mnemonic entries only for instructions where doing so would "probably" be right.

If there are use cases where working with mnemonics is correct, we should be doing that in kmemcheck. If the way kmemcheck deals with mnemonics is incorrect we should go ahead and fix kmemcheck.

In that case, as I said, the mnemonics classifier should be build in kmemcheck at this point, since we cannot provide any general mnemonic classifier for that purpose. If it becomes enough generic, and accurate, it would be better consolidate both, I think.

Thank you,

Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
#17 lore raw

Re: [PATCH 3/4] x86/insn: Extract more information about instructions

Quoted 19 lines · Masami Hiramatsu
Quoted 18 lines · Sasha Levin
Quoted 18 lines · H. Peter Anvin
Quoted 6 lines
Quoted 6 lines · Sasha Levin
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Yes, if kmemcheck for some reason needs to figure out if an instruction is a MOV variant we'll need to list quite a few mnemonics, but that list will be much shorter and more readable than a corresponding list of opcodes.

Quoted 6 lines
Quoted 6 lines

You're completely missing my point. If you are looking at MOV, with 80%+ probability you're doing something very, very wrong, because you will be including instructions that do something completely different from what you thought.

This is true for a lot of the x86 instructions.

Right, but assuming that the AND example I presented earlier makes sense, I can't create mnemonic entries only for instructions where doing so would "probably" be right.

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

If there are use cases where working with mnemonics is correct, we should be doing that in kmemcheck. If the way kmemcheck deals with mnemonics is incorrect we should go ahead and fix kmemcheck.

In that case, as I said, the mnemonics classifier should be build in kmemcheck at this point, since we cannot provide any general mnemonic classifier for that purpose. If it becomes enough generic, and accurate, it would be better consolidate both, I think.

kmemcheck isn't an instruction decoder, it should not be parsing x86-opcode-map.txt, that's why there's an API to access that in inat.c.

Basically you're saying that you don't want to extend the API to extract the extra mnemonic field which already exists in the data we're extracting from anyways, and that kmemcheck should go around the API and do it itself.

It's not like the instruction decoder is a generic piece of code right now anyways, it only serves mostly [k,u]probes and was built around the their requirements, and now everybody are surprised that kmemcheck has different requirements than kprobes.

Thanks,
Sasha

#18 lore raw

Re: [PATCH 3/4] x86/insn: Extract more information about instructions

Quoted 5 lines · Sasha Levin

It's not like the instruction decoder is a generic piece of code right now anyways, it only serves mostly [k,u]probes and was built around the their requirements, and now everybody are surprised that kmemcheck has different requirements than kprobes.

What *ARE* kmemcheck's requirements? That's the real issue, I believe.

I also have seen several attempts at using the generic instruction decoder which has resulted in more complexity, not less, because of excess generality, so it is not an obvious thing.

-hpa

#19 lore raw

Re: [PATCH 3/4] x86/insn: Extract more information about instructions

Quoted 8 lines · H. Peter Anvin
Quoted 5 lines · Sasha Levin

It's not like the instruction decoder is a generic piece of code right now anyways, it only serves mostly [k,u]probes and was built around the their requirements, and now everybody are surprised that kmemcheck has different requirements than kprobes.

What *ARE* kmemcheck's requirements? That's the real issue, I believe.

As far as kmemcheck is concerned, it needs to decode instructions so that it could learn whether an instruction will access memory, and if it does - the size of memory an instruction attempts to access.

Note that right now kmemcheck implements a dumb instruction decoder (see arch/x86/mm/kmemcheck/opcode.c) which works for most cases, but breaks on more than few. The first patch in this series adds a few cases where that decoder will fail.

This is the main purpose of the patch series. We want to switch kmemcheck to use the generic instruction decoder to avoid bugs found in the dumbed down instruction decoder used by kmemcheck.

On top of that, there are several features which need to know more about instructions:

1. kmemcheck wants to know if a given instruction may act on only part of the memory it addresses, or all of it. This solves the problem of partially initialized bitfields where uninitialized bits are never accessed.

For the above, kmemcheck needs to recognize whether an instruction is AND, OR or XOR.

2. There are two sets of instructions, MOVS and CMPS which work a bit differently than the rest. They take two memory addresses but will generate only one page fault on access, which will make kmemcheck miss a memory access.

kmemcheck needs to know if an instruction is a MOVS or CMPS to deal with that corner case properly.

Quoted 3 lines

I also have seen several attempts at using the generic instruction decoder which has resulted in more complexity, not less, because of excess generality, so it is not an obvious thing.

Let's split this patchset into two:

We have one part which moves kmemcheck to the generic instruction decoder and adds memory access size to the instruction decoder. There seems to be no objection to that part beyond technical issues regarding how we store the new size value.

The other part is adding mnemonics to the instruction decoder. If my explanation above makes sense, and kmemcheck does need to know about AND, OR, XOR, MOVS and CMPS then let me know how to proceed about changing the instruction decoder to add that functionality.

If I'm wrong about needing to know about those mnemonics, I'd be happy to fix that issue in kmemcheck, but for that I need to know why I'm wrong.

Thanks,
Sasha

#20 lore raw

Re: [PATCH 3/4] x86/insn: Extract more information about instructions

Quoted 10 lines · Sasha Levin
Quoted 3 lines

I also have seen several attempts at using the generic instruction decoder which has resulted in more complexity, not less, because of excess generality, so it is not an obvious thing.

Let's split this patchset into two:

We have one part which moves kmemcheck to the generic instruction decoder and adds memory access size to the instruction decoder. There seems to be no objection to that part beyond technical issues regarding how we store the new size value.

This looks OK to me.

Quoted 4 lines

The other part is adding mnemonics to the instruction decoder. If my explanation above makes sense, and kmemcheck does need to know about AND, OR, XOR, MOVS and CMPS then let me know how to proceed about changing the instruction decoder to add that functionality.

I don't think we need to add such things to instruction decoder. You'd better start from clarifying the bit pattern of those instructions and making macros or inlines which evaluate insn->opcode.value.

Using automatic generated macros for immediate in the source code always leads misunderstanding and abuse, and is hard to fix if a bug is there. I strongly recommend you to define instruction classification macros for their use by hand. That's easy to review too.
Actually x86 has a long history and its mnemonics are not so simple...

Thank you,

Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
#21 lore raw

Re: [PATCH 3/4] x86/insn: Extract more information about instructions

Quoted 23 lines · Masami Hiramatsu
Quoted 10 lines · Sasha Levin
Quoted 3 lines

I also have seen several attempts at using the generic instruction decoder which has resulted in more complexity, not less, because of excess generality, so it is not an obvious thing.

Let's split this patchset into two:

We have one part which moves kmemcheck to the generic instruction decoder and adds memory access size to the instruction decoder. There seems to be no objection to that part beyond technical issues regarding how we store the new size value.

This looks OK to me.

Quoted 4 lines

The other part is adding mnemonics to the instruction decoder. If my explanation above makes sense, and kmemcheck does need to know about AND, OR, XOR, MOVS and CMPS then let me know how to proceed about changing the instruction decoder to add that functionality.

I don't think we need to add such things to instruction decoder. You'd better start from clarifying the bit pattern of those instructions and making macros or inlines which evaluate insn->opcode.value.

Quoted 6 lines

Using automatic generated macros for immediate in the source code always leads misunderstanding and abuse, and is hard to fix if a bug is there. I strongly recommend you to define instruction classification macros for their use by hand. That's easy to review too.
Actually x86 has a long history and its mnemonics are not so simple...

What it sounds like it really wants is a "bitwise" flag on the instruction.

-hpa

#22 lore raw

Re: [PATCH 3/4] x86/insn: Extract more information about instructions

Quoted 8 lines · H. Peter Anvin
Quoted 8 lines
Quoted 8 lines

The other part is adding mnemonics to the instruction decoder. If my

Quoted 3 lines
Quoted 3 lines

explanation above makes sense, and kmemcheck does need to know about AND, OR, XOR, MOVS and CMPS then let me know how to proceed about changing the instruction decoder to add that functionality.

I don't think we need to add such things to instruction decoder. You'd better start from clarifying the bit pattern of those instructions and making macros or inlines which evaluate insn->opcode.value.

There are very specific mnemonics that kmemchecks wants to detect and treat as a corner case.

What you're saying here is that while the instruction decoder already has the knowledge of mnemonics, kmemcheck shouldn't use it and instead write it's own opcode -> mnemonic parser and use that instead.

Note that it won't be enough to decode just the opcodes, as variants of the instructions we need to detect are hidden inside groups, so we'd need to parse mod/rm in addition to the opcode.

This means we're adding a tiny instruction parser to kmemcheck, which is exactly the thing we're trying to remove with the previous part of this patchset.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Using automatic generated macros for immediate in the source code always leads misunderstanding and abuse, and is hard to fix if a bug is there. I strongly recommend you to define instruction classification macros for their use by hand. That's easy to review too.
Actually x86 has a long history and its mnemonics are not so simple...

If the issue is that they get dynamically generated I'm fine with making that a static list and updating it by hand whenever new instructions or mnemonics are introduced.

Quoted 1 line

What it sounds like it really wants is a "bitwise" flag on the instruction.

A flag like that would solve part of the problem (we'd still need to work with CMPS and MOVS), and sounds very kmemcheck specific.

Thanks,
Sasha

#23 lore raw

Re: [PATCH 3/4] x86/insn: Extract more information about instructions

Quoted 8 lines · Sasha Levin

There are very specific mnemonics that kmemchecks wants to detect and treat as a corner case.

What you're saying here is that while the instruction decoder already has the knowledge of mnemonics, kmemcheck shouldn't use it and instead write it's own opcode -> mnemonic parser and use that instead.

I think that involving mnemonics is "bonghits" level of crazy. It's a solution in search of the problem, but it is a hack, and a pretty horrific one.

Quoted 6 lines
Quoted 2 lines

What it sounds like it really wants is a "bitwise" flag on the instruction.

A flag like that would solve part of the problem (we'd still need to work with CMPS and MOVS), and sounds very kmemcheck specific.

Well, guess what, the whole point is to export information to the users that need it.

-hpa

#24 lore raw

Re: Re: [PATCH 3/4] x86/insn: Extract more information about instructions

In reply to Sasha Levin
Quoted 9 lines · Sasha Levin
Quoted 8 lines
Quoted 4 lines

- Memory access size. We're currently decoding the size (in bytes) of an
address size, and operand size. kmemcheck would like to know in addition how many bytes were read/written from/to an address by a given instruction, so we also keep the size of the memory access.

And also, at least in this time, since the operation/mem_size are only used in kmemcheck, you should generate another table for that in kmemcheck from x86-opcode-map.txt.

Quoted 5 lines

I don't want to "teach" kmemcheck to parse x86-opcode-map.txt, that should be something that the instruction API does.

kmemcheck would also be the 3rd in-kernel user of that API, so it's not fair to push it as an exception :)

OK, I think we can push the size information bits into current insn_attr_t. I don't think we should have another byte for that.

For example, here I pulled the operand size detector from my disasm code,

----
static int get_operand_size(struct insn *insn, const char *opnd)

{
        int size = insn->opnd_bytes;

        switch (opnd[1]) {
        case 'b':
        case 'B':
                size = 1;
                break;
        case 'w':
                size = 2;
                break;
        case 'd':
                if (opnd[2] == 'q')
                        size = 16;
                else
                        size = 4;
                break;
        case 'p':
                if (opnd[2] == 's' || opnd[2] == 'd')
                        size = insn_vex_l_bit(insn) ? 32 : 16;
                break;
        case 'q':
                if (opnd[2] == 'q')
                        size = 32;
                else
                        size = 8;
                break;
        case 's':
                if (opnd[2] == 's' || opnd[2] == 'd')
                        size = 16;
                break;
        case 'x':
                size = insn_vex_l_bit(insn) ? 32 : 16;
                break;
        case 'z':
                if (size == 8)
                        size = 4;
                break;
        }
        return size;
}

----

Same thing can be done in awk part and insn.c, and we can encode it by

#define INAT_MAKE_MEMSZ(size) (size << INAT_MEMSZ_OFFS)

And decode it by

insn->memsz_bytes = 1 << ((attr & INAT_MEMSZ_MASK) >> INAT_MEMSZ_OFFS)

Thus, we only need 3 bits to represent 1, 2, 4, 8, 16 and 32. :)

Quoted 1 line

It's also just one more byte in 'struct insn'...

I actually don't like to expand struct insn_attr_t, I'd like to keep it in an immediate value.

[...]

Quoted 14 lines
Quoted 7 lines
Quoted 7 lines
arch/x86/kernel/kprobes/core.c +1 -1
@@ -141,15 +141,15 @@ void __kprobes synthesize_relcall(void *from, void *to)
  */
 static kprobe_opcode_t *__kprobes skip_prefixes(kprobe_opcode_t *insn)
 {
-	insn_attr_t attr;
+	insn_flags_t flags;
 
Quoted 6 lines

arch/x86/kernel/kprobes/core.c void __kprobes synthesize_relcall(void *from, void *to) · line 146

Quoted 3 lines
arch/x86/kernel/kprobes/core.c +1 -2
-	attr = inat_get_opcode_attribute((insn_byte_t)*insn);
-	while (inat_is_legacy_prefix(attr)) {
+	flags = inat_get_opcode((insn_byte_t)*insn)->flags;

Do not refer a member from the return value directly. If it returns NULL, the kernel just crashes!

Quoted 2 lines

Right, I'll fix that. Probably by adding a dummy "empty" instruction just so we won't have to deal with too many NULL checks.

Note that if we can put them all in one value, we can avoid such ugly NULL checks.

Thank you,

Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
#25 lore raw

Re: [PATCH 3/4] x86/insn: Extract more information about instructions

Quoted 9 lines · Masami Hiramatsu

Same thing can be done in awk part and insn.c, and we can encode it by

#define INAT_MAKE_MEMSZ(size) (size << INAT_MEMSZ_OFFS)

And decode it by

insn->memsz_bytes = 1 << ((attr & INAT_MEMSZ_MASK) >> INAT_MEMSZ_OFFS)

Thus, we only need 3 bits to represent 1, 2, 4, 8, 16 and 32. :)

We'll need 4 so that we could do 64 too :)

btw, why aren't we using regular bitfields? this manual encoding thingie seems to be a bit confusing (try figuring out how many bits are left...).

Thanks,
Sasha

#26 lore raw

Re: [PATCH 3/4] x86/insn: Extract more information about instructions

Quoted 5 lines · Sasha Levin
Quoted 4 lines · Masami Hiramatsu

Same thing can be done in awk part and insn.c, and we can encode it by

#define INAT_MAKE_MEMSZ(size) (size << INAT_MEMSZ_OFFS)

Quoted 7 lines
Quoted 5 lines

And decode it by

insn->memsz_bytes = 1 << ((attr & INAT_MEMSZ_MASK) >> INAT_MEMSZ_OFFS)

Thus, we only need 3 bits to represent 1, 2, 4, 8, 16 and 32. :)

We'll need 4 so that we could do 64 too :)

Would you mean AVX512? Actually it's not supported currently :( anyway, that's ok for me, and also, we need another 2 bits for the operands which depends on address-size prefix and operand-size prefix.

Quoted 3 lines

btw, why aren't we using regular bitfields? this manual encoding thingie seems to be a bit confusing (try figuring out how many bits are left...).

Ah, right. OK, I'll try to do that :)

Thank you!

Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
#27 lore raw

PATCH 4/4 kmemcheck: Switch to using kernel disassembler

In reply to Sasha Levin

kmemcheck used to do it's own basic instruction decoding, which is just a duplication of the work done in arch/x86/lib/insn.c.

Instead, switch it to using the already existing dissasembler, and switch the magic opcode numbers into something meaningful.

---
 arch/x86/mm/kmemcheck/Makefile    |    2 +-
 arch/x86/mm/kmemcheck/kmemcheck.c |  105 ++++++++++++++++++------------------
 arch/x86/mm/kmemcheck/opcode.c    |  106 -------------------------------------
 arch/x86/mm/kmemcheck/opcode.h    |    9 ----
 arch/x86/mm/kmemcheck/selftest.c  |   12 +++--
 arch/x86/mm/kmemcheck/selftest.h  |    1 +
 6 files changed, 61 insertions(+), 174 deletions(-)
 delete mode 100644 arch/x86/mm/kmemcheck/opcode.c
 delete mode 100644 arch/x86/mm/kmemcheck/opcode.h
arch/x86/mm/kmemcheck/Makefile +1 -1
index 520b3bc..f1749ad 100644
@@ -1 +1 @@
-obj-y := error.o kmemcheck.o opcode.o pte.o selftest.o shadow.o
+obj-y := error.o kmemcheck.o pte.o selftest.o shadow.o
arch/x86/mm/kmemcheck/kmemcheck.c +51 -54

146 lines not on this page · raw message

arch/x86/mm/kmemcheck/opcode.c deleted +0 -106

106 lines not on this page · raw message

arch/x86/mm/kmemcheck/opcode.h deleted +0 -9
deleted file mode 100644
index 6956aad..0000000
@@ -1,9 +0,0 @@
-#ifndef ARCH__X86__MM__KMEMCHECK__OPCODE_H
-#define ARCH__X86__MM__KMEMCHECK__OPCODE_H
-
-#include <linux/types.h>
-
-void kmemcheck_opcode_decode(const uint8_t *op, unsigned int *size);
-const uint8_t *kmemcheck_opcode_get_primary(const uint8_t *op);
-
-#endif
arch/x86/mm/kmemcheck/selftest.c +8 -4
index c898d33..8976938 100644
@@ -1,7 +1,9 @@
 #include <linux/bug.h>
 #include <linux/kernel.h>
 
-#include "opcode.h"
+#include <asm/insn.h>
+#include <asm/inat-tables.h>
+
 #include "selftest.h"
 
 struct selftest_opcode {
@@ -46,10 +48,12 @@ static const struct selftest_opcode selftest_opcodes[] = {
 
 static bool selftest_opcode_one(const struct selftest_opcode *op)
 {
-	unsigned size;
-
-	kmemcheck_opcode_decode(op->insn, &size);
+	struct insn insn;
+	char size;
 
+	kernel_insn_init(&insn, op->insn);
+	insn_get_length(&insn);
+	size = kmemcheck_get_addr_size(&insn);
 	if (size == op->expected_size)
 		return true;
 
arch/x86/mm/kmemcheck/selftest.h +1 -0
index 8fed4fe..4cd1c5e 100644
@@ -2,5 +2,6 @@
 #define ARCH_X86_MM_KMEMCHECK_SELFTEST_H
 
 bool kmemcheck_selftest(void);
+extern char kmemcheck_get_addr_size(struct insn *insn);
 
 #endif
Signature · 1 line
1.7.10.4
Acked-by on v1 4/4
#28 lore raw

Re: [PATCH 4/4] kmemcheck: Switch to using kernel disassembler

Quoted 7 lines · Sasha Levin

kmemcheck used to do it's own basic instruction decoding, which is just a duplication of the work done in arch/x86/lib/insn.c.

Instead, switch it to using the already existing dissasembler, and switch the magic opcode numbers into something meaningful.

Vegard probably should take a closer look at this but: