x86: Unify/extend insn_is_nop()
Peter Zijlstra
maintainercover x86: Unify/extend insn_is_nop()
Hi all, welcome to an x86 instruction encoding quest.
I realized I implemented insn_is_nop() twice, obviously they have different bugs. Additionally, I was made aware of another uprobe use-case.
As such, unify and extend insn_is_nop().
While doing this, I went down the rabbit hole of REX2/*VEX* encoded instructions (because REX2.b4 NOP). The Intel APX document is clear as mud on what exactly VEX/EVEX Map0 is, but the way I read it, it is the normal single byte opcode map, no exceptions. Similarly Map1 is the 0f second byte opcode map. So VEX prefixed NOPs should just work.
REX2 has a similar Map0/Map1 scheme, except it excludes a number of opcode ranges -- notable not including NOP/NOPL. So REX2 prefixed NOPs are fine too.
Because I had to figure it all out anyway, I've made the function deal with them, but perhaps the more prudent option is to simply reject all REX2/VEX prefixed instructions entirely.
(although EVEX NOPL can hit 11 bytes without additional prefixes, so maybe someone will start using them at some point).
Peter Zijlstra
maintainerRFC PATCH 1/2 x86/insn,uprobes,alternative: Unify insn_is_nop()
Both uprobes and alternatives have insn_is_nop() variants, unify them and make sure insn_is_nop() works for both x86_64 and i386.
Specifically, uprobe must not compare userspace instructions to kernel nops as that does not work right in the compat case.
For the uprobe case we therefore must recognise common 32bit and 64bit nops. Because uprobe will consume the instruction as a nop, it must not mistakenly claim a non-nop instruction to be a nop. Eg. 'REX.b3 NOP' is 'xchg %r8,%rax' - not a nop.
For the kernel case similar constraints apply, is it used to optimize NOPs by replacing strings of short(er) nops with longer nops. Must not claim an instruction is a nop if it really isn't. Not recognising a nop is non-fatal.
XXX: the simpler case is probably to reject everything REX2/*VEX* encoded rather than try and deal with it.
--- arch/x86/include/asm/insn-eval.h | 2 arch/x86/include/asm/insn.h | 4 arch/x86/kernel/alternative.c | 20 ---- arch/x86/kernel/uprobes.c | 32 ------ arch/x86/lib/insn-eval.c | 192 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 202 insertions(+), 48 deletions(-)
arch/x86/include/asm/insn-eval.h +2 -0
@@ -44,4 +44,6 @@ enum insn_mmio_type {enum insn_mmio_type insn_decode_mmio (struct insn *insn,int *bytes); +bool insn_is_nop (struct insn *insn); +#endif /* _ASM_X86_INSN_EVAL_H */
arch/x86/include/asm/insn.h +4 -0
@@ -138,6 +138,10 @@ struct insn {#define X86_VEX_V (vex) (((vex) & 0x78) >> 3)/* VEX3 Byte2, VEX2 Byte1 */ #define X86_VEX_P (vex) ((vex) & 0x03)/* VEX3 Byte2, VEX2 Byte1 */ #define X86_VEX_M_MAX 0x1f/* VEX3.M Maximum value */ +/* EVEX bit fields */ +#define X86_EVEX_R4 (vex) ((vex) & 0x10)/* EVEX Byte1 */ +#define X86_EVEX_B4 (vex) ((vex) & 0x08)/* EVEX Byte1 */ +#define X86_EVEX_X4 (vex) ((vex) & 0x04)/* EVEX Byte2 */ /* XOP bit fields */ #define X86_XOP_R (xop) ((xop) & 0x80)/* XOP Byte2 */ #define X86_XOP_X (xop) ((xop) & 0x40)/* XOP Byte2 */
arch/x86/kernel/alternative.c +1 -19
@@ -9,6 +9,7 @@#include <asm/text-patching.h> #include <asm/insn.h> +#include <asm/insn-eval.h> #include <asm/ibt.h> #include <asm/set_memory.h> #include <asm/nmi.h>
@@ -346,25 +347,6 @@ static void add_nop(u8 *buf, unsigned in }/* -* Matches NOP and NOPL, not any of the other possible NOPs. -*/ -static bool insn_is_nop (struct insn *insn) -{ -/* Anything NOP, but no REP NOP */ -if (insn->opcode.bytes[0 ] ==0x90 && - (!insn->prefixes.nbytes || insn->prefixes.bytes[0 ] !=0xF3 )) -return true; - -/* NOPL */ -if (insn->opcode.bytes[0 ] ==0x0F && insn->opcode.bytes[1 ] ==0x1F ) -return true; - -/* TODO: more nops */ - -return false; -} - -/* * Find the offset of the first non-NOP instruction starting at @offset * but no further than @len. */
arch/x86/kernel/uprobes.c +3 -29
@@ -17,6 +17,7 @@#include <linux/kdebug.h> #include <asm/processor.h> #include <asm/insn.h> +#include <asm/insn-eval.h> #include <asm/mmu_context.h> #include <asm/nops.h>
@@ -1158,35 +1159,12 @@ void arch_uprobe_optimize(struct arch_upmmap_write_unlock (mm); } -static bool insn_is_nop (struct insn *insn) -{ -return insn->opcode.nbytes ==1 && insn->opcode.bytes[0 ] ==0x90 ; -} - -static bool insn_is_nopl (struct insn *insn) -{ -if (insn->opcode.nbytes !=2 ) -return false; - -if (insn->opcode.bytes[0 ] !=0x0f || insn->opcode.bytes[1 ] !=0x1f ) -return false; - -if (!insn->modrm.nbytes) -return false; - -if (X86_MODRM_REG (insn->modrm.bytes[0 ]) !=0 ) -return false; - -/* 0f 1f /0 - NOPL */ -return true; -} -static bool can_optimize (struct insn *insn,unsigned long vaddr) {if (!insn->x86_64 || insn->length !=5 )return false; -if (!insn_is_nop (insn) && !insn_is_nopl (insn)) +if (!insn_is_nop (insn))return false;/* We can't do cross page atomic writes yet. */
@@ -1428,17 +1406,13 @@ static int branch_setup_xol_ops(struct ainsn_byte_t p;int i; -/* x86_nops[insn->length]; same as jmp with .offs = 0 */ -if (insn->length <=ASM_NOP_MAX && - !memcmp (insn->kaddr, x86_nops[insn->length], insn->length)) +if (insn_is_nop (insn)) goto setup;switch (opc1) {case 0xeb :/* jmp 8 */ case 0xe9 :/* jmp 32 */ break ; -case 0x90 :/* prefix* + nop; same as jmp with .offs = 0 */ - goto setup;case 0xe8 :/* call relative */ branch_clear_offset (auprobe, insn);
arch/x86/lib/insn-eval.c +192 -0
195 lines not on this page · raw message
Peter Zijlstra
maintainerRFC PATCH 2/2 x86/insn: Simplify for_each_insn_prefix()
In reply to Peter ZijlstraUse the new-found freedom of allowing variable declarions inside for() to simplify the for_each_insn_prefix() iterator to no longer need an external temporary.
--- arch/x86/boot/compressed/sev-handle-vc.c | 3 +-- arch/x86/include/asm/insn.h | 5 ++--- arch/x86/kernel/kprobes/core.c | 3 +-- arch/x86/kernel/uprobes.c | 6 ++---- arch/x86/lib/insn-eval.c | 12 +++++------- tools/arch/x86/include/asm/insn.h | 5 ++--- 6 files changed, 13 insertions(+), 21 deletions(-)
arch/x86/boot/compressed/sev-handle-vc.c +1 -2
@@ -29,11 +29,10 @@bool insn_has_rep_prefix (struct insn *insn) {insn_byte_t p; -int i;insn_get_prefixes (insn); -for_each_insn_prefix (insn, i, p) { +for_each_insn_prefix (insn, p) {if (p ==0xf2 || p ==0xf3 )return true; }
arch/x86/include/asm/insn.h +2 -3
@@ -316,7 +316,6 @@ static inline int insn_offset_immediate(/** * for_each_insn_prefix() -- Iterate prefixes in the instruction * @insn: Pointer to struct insn. -* @idx: Index storage. * @prefix: Prefix byte. * * Iterate prefix bytes of given @insn. Each prefix byte is stored in @prefix
@@ -325,8 +324,8 @@ static inline int insn_offset_immediate(* Since prefixes.nbytes can be bigger than 4 if some prefixes * are repeated, it cannot be used for looping over the prefixes. */ -#define for_each_insn_prefix (insn, idx, prefix) \ - for (idx = 0; idx < ARRAY_SIZE(insn->prefixes.bytes) && (prefix = insn->prefixes.bytes[idx]) != 0; idx++) +#define for_each_insn_prefix (insn, prefix) \ + for (int idx = 0; idx < ARRAY_SIZE(insn->prefixes.bytes) && (prefix = insn->prefixes.bytes[idx]) != 0; idx++)#define POP_SS_OPCODE 0x1f#define MOV_SREG_OPCODE 0x8e
arch/x86/kernel/kprobes/core.c +1 -2
@@ -141,7 +141,6 @@ bool can_boost(struct insn *insn, void * {kprobe_opcode_t opcode;insn_byte_t prefix; -int i;if (search_exception_tables ((unsigned long )addr))return false;/* Page fault may occur on this address. */
@@ -154,7 +153,7 @@ bool can_boost(struct insn *insn, void *if (insn->opcode.nbytes !=1 )return false; -for_each_insn_prefix (insn, i, prefix) { +for_each_insn_prefix (insn, prefix) {insn_attr_t attr; attr =inat_get_opcode_attribute (prefix);
arch/x86/kernel/uprobes.c +2 -4
@@ -259,9 +259,8 @@ static volatile u32 good_2byte_insns[256static bool is_prefix_bad (struct insn *insn) {insn_byte_t p; -int i; -for_each_insn_prefix (insn, i, p) { +for_each_insn_prefix (insn, p) {insn_attr_t attr; attr =inat_get_opcode_attribute (p);
@@ -1404,7 +1403,6 @@ static int branch_setup_xol_ops(struct a {u8 opc1 =OPCODE1 (insn);insn_byte_t p; -int i;if (insn_is_nop (insn)) goto setup;
@@ -1437,7 +1435,7 @@ static int branch_setup_xol_ops(struct a* Intel and AMD behavior differ in 64-bit mode: Intel ignores 66 prefix. * No one uses these insns, reject any branch insns with such prefix. */ -for_each_insn_prefix (insn, i, p) { +for_each_insn_prefix (insn, p) {if (p ==0x66 )return -ENOTSUPP ; }
arch/x86/lib/insn-eval.c +5 -7
@@ -63,11 +63,10 @@ static bool is_string_insn(struct insn *bool insn_has_rep_prefix (struct insn *insn) {insn_byte_t p; -int i;insn_get_prefixes (insn); -for_each_insn_prefix (insn, i, p) { +for_each_insn_prefix (insn, p) {if (p ==0xf2 || p ==0xf3 )return true; }
@@ -92,13 +91,13 @@ bool insn_has_rep_prefix(struct insn *instatic int get_seg_reg_override_idx (struct insn *insn) {int idx =INAT_SEG_REG_DEFAULT ; -int num_overrides =0 , i; +int num_overrides =0 ;insn_byte_t p;insn_get_prefixes (insn);/* Look for any segment override prefixes. */ -for_each_insn_prefix (insn, i, p) { +for_each_insn_prefix (insn, p) {insn_attr_t attr; attr =inat_get_opcode_attribute (p);
@@ -1701,7 +1700,6 @@ bool insn_is_nop(struct insn *insn)u8 sib =0 , sib_scale, sib_index, sib_base;u8 p =0 , rep =0 ;u8 nrex, rex; -int i;if ((nrex = insn->rex_prefix.nbytes)) { rex = insn->rex_prefix.bytes[nrex-1 ];
@@ -1773,7 +1771,7 @@ bool insn_is_nop(struct insn *insn)if (p ==INAT_PFX_REPE ) rep =1 ; -for_each_insn_prefix (insn, i, p) { +for_each_insn_prefix (insn, p) {if (p ==0xf3 )/* REPE */ rep =1 ; }
@@ -1836,7 +1834,7 @@ bool insn_is_nop(struct insn *insn)if (sib && (sib_scale !=0 || sib_index !=4 ))/* (%reg, %eiz, 1) */ return false; -for_each_insn_prefix (insn, i, p) { +for_each_insn_prefix (insn, p) {if (p !=0x3e )/* DS */ return false; }
tools/arch/x86/include/asm/insn.h +2 -3
@@ -312,7 +312,6 @@ static inline int insn_offset_immediate(/** * for_each_insn_prefix() -- Iterate prefixes in the instruction * @insn: Pointer to struct insn. -* @idx: Index storage. * @prefix: Prefix byte. * * Iterate prefix bytes of given @insn. Each prefix byte is stored in @prefix
@@ -321,8 +320,8 @@ static inline int insn_offset_immediate(* Since prefixes.nbytes can be bigger than 4 if some prefixes * are repeated, it cannot be used for looping over the prefixes. */ -#define for_each_insn_prefix (insn, idx, prefix) \ - for (idx = 0; idx < ARRAY_SIZE(insn->prefixes.bytes) && (prefix = insn->prefixes.bytes[idx]) != 0; idx++) +#define for_each_insn_prefix (insn, prefix) \ + for (int idx = 0; idx < ARRAY_SIZE(insn->prefixes.bytes) && (prefix = insn->prefixes.bytes[idx]) != 0; idx++)#define POP_SS_OPCODE 0x1f#define MOV_SREG_OPCODE 0x8e