public inbox for systemtap@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] kprobe: kprobe-2byte-booster
@ 2006-04-12 10:10 Masami Hiramatsu
  2006-04-13  9:50 ` Ananth N Mavinakayanahalli
  0 siblings, 1 reply; 3+ messages in thread
From: Masami Hiramatsu @ 2006-04-12 10:10 UTC (permalink / raw)
  To: Ananth N Mavinakayanahalli, Prasanna S Panchamukhi,
	Keshavamurthy, Anil S, SystemTAP
  Cc: Yumiko Sugita, Satoshi Oshima, Hideo Aoki

Hi,

Here is a patch of kprobe-booster enhancement on i386
This patch can be applied to 2.6.17-rc1.

Previous kprobe-booster patch has not handled any 2byte opcodes
and prefixes. I checked whole IA32 opcode map and classified it.

This patch enables kprobe to boost those 2byte opcodes and prefixes.

Best regards,

-- 
Masami HIRAMATSU
2nd Research Dept.
Hitachi, Ltd., Systems Development Laboratory
E-mail: hiramatu@sdl.hitachi.co.jp


 kprobes.c |   63 +++++++++++++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 50 insertions(+), 13 deletions(-)
diff -Narup a/arch/i386/kernel/kprobes.c b/arch/i386/kernel/kprobes.c
--- a/arch/i386/kernel/kprobes.c	2006-04-03 18:30:59.000000000 +0900
+++ b/arch/i386/kernel/kprobes.c	2006-04-03 19:41:54.000000000 +0900
@@ -55,32 +55,69 @@ static inline void set_jmp_op(void *from
 }

 /*
+ * Undefined/reserved opcodes, conditional jump, Opcode Extension Groups,
+ * and some special opcodes can not be boost.
+ */
+static char boostable_2nd_opcode[256] = {
+0,0,1,1,0,0,1,0,1,1,0,0,0,0,0,0,
+0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,
+0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,
+0,0,0,0,1,1,1,1,0,0,0,0,0,0,1,1,
+0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+1,1,0,1,1,1,0,0,1,1,0,1,1,1,0,1,
+1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,
+1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,
+0,1,1,1,0,1,0,0,1,1,0,1,1,1,0,1,
+0,1,1,0,0,1,0,0,1,1,0,1,1,1,0,1,
+0,1,1,1,0,1,0,0,1,1,1,0,1,1,1,0
+};
+
+/*
  * returns non-zero if opcodes can be boosted.
  */
-static inline int can_boost(kprobe_opcode_t opcode)
+static inline int can_boost(kprobe_opcode_t *opcodes)
 {
-	switch (opcode & 0xf0 ) {
+	kprobe_opcode_t opcode;
+	kprobe_opcode_t *orig_opcodes = opcodes;
+retry:
+	if (opcodes - orig_opcodes > MAX_INSN_SIZE) return 0;
+	opcode = *(opcodes++);
+	/* 2nd-byte opcode */
+	if (opcode == 0x0f)
+		return boostable_2nd_opcode[*opcodes];
+	
+	switch (opcode & 0xf0) {
+	case 0x60:
+		if (0x63 < opcode && opcode < 0x67)
+			goto retry; /* prefixes */
+		/* can't boost Address-size override and bound */
+		return (opcode != 0x62 && opcode != 0x67);
 	case 0x70:
 		return 0; /* can't boost conditional jump */
-	case 0x90:
-		/* can't boost call and pushf */
-		return opcode != 0x9a && opcode != 0x9c;
 	case 0xc0:
-		/* can't boost undefined opcodes and soft-interruptions */
-		return (0xc1 < opcode && opcode < 0xc6) ||
-			(0xc7 < opcode && opcode < 0xcc) || opcode == 0xcf;
+		/* can't boost software-interruptions */
+		return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
 	case 0xd0:
 		/* can boost AA* and XLAT */
 		return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
 	case 0xe0:
-		/* can boost in/out and (may be) jmps */
-		return (0xe3 < opcode && opcode != 0xe8);
+		/* can boost in/out and absolute jmps */
+		return ((opcode & 0x04) || opcode == 0xea);
 	case 0xf0:
+		if ((opcode & 0x0c) == 0 && opcode != 0xf1)
+			goto retry; /* lock/rep(ne) prefix */
 		/* clear and set flags can be boost */
 		return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
 	default:
-		/* currently, can't boost 2 bytes opcodes */
-		return opcode != 0x0f;
+		if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
+			goto retry; /* prefixes */
+		/* can't boost CS override and call */
+		return (opcode != 0x2e && opcode != 0x9a);
 	}
 }

@@ -109,7 +146,7 @@ int __kprobes arch_prepare_kprobe(struct

 	memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
 	p->opcode = *p->addr;
-	if (can_boost(p->opcode)) {
+	if (can_boost(p->addr)) {
 		p->ainsn.boostable = 0;
 	} else {
 		p->ainsn.boostable = -1;


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] kprobe: kprobe-2byte-booster
  2006-04-12 10:10 [PATCH] kprobe: kprobe-2byte-booster Masami Hiramatsu
@ 2006-04-13  9:50 ` Ananth N Mavinakayanahalli
  2006-04-14  0:44   ` Masami Hiramatsu
  0 siblings, 1 reply; 3+ messages in thread
From: Ananth N Mavinakayanahalli @ 2006-04-13  9:50 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: Prasanna S Panchamukhi, Keshavamurthy, Anil S, SystemTAP,
	Yumiko Sugita, Satoshi Oshima, Hideo Aoki

On Wed, Apr 12, 2006 at 07:10:30PM +0900, Masami Hiramatsu wrote:
> Hi,
> 
> Here is a patch of kprobe-booster enhancement on i386
> This patch can be applied to 2.6.17-rc1.
> 
> Previous kprobe-booster patch has not handled any 2byte opcodes
> and prefixes. I checked whole IA32 opcode map and classified it.
> 
> This patch enables kprobe to boost those 2byte opcodes and prefixes.

Masami,

Few comments below (I am not too well versed with IA32 internals to
comment on the intricacies).

Ananth

> 
> Best regards,
> 
> -- 
> Masami HIRAMATSU
> 2nd Research Dept.
> Hitachi, Ltd., Systems Development Laboratory
> E-mail: hiramatu@sdl.hitachi.co.jp
> 
> 
>  kprobes.c |   63 +++++++++++++++++++++++++++++++++++++++++++++++++-------------
>  1 file changed, 50 insertions(+), 13 deletions(-)
> diff -Narup a/arch/i386/kernel/kprobes.c b/arch/i386/kernel/kprobes.c
> --- a/arch/i386/kernel/kprobes.c	2006-04-03 18:30:59.000000000 +0900
> +++ b/arch/i386/kernel/kprobes.c	2006-04-03 19:41:54.000000000 +0900
> @@ -55,32 +55,69 @@ static inline void set_jmp_op(void *from
>  }
> 
>  /*
> + * Undefined/reserved opcodes, conditional jump, Opcode Extension Groups,
> + * and some special opcodes can not be boost.
> + */
> +static char boostable_2nd_opcode[256] = {
> +0,0,1,1,0,0,1,0,1,1,0,0,0,0,0,0,
> +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
> +1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,
> +0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
> +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
> +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
> +1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,
> +0,0,0,0,1,1,1,1,0,0,0,0,0,0,1,1,
> +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
> +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
> +1,1,0,1,1,1,0,0,1,1,0,1,1,1,0,1,
> +1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,
> +1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,
> +0,1,1,1,0,1,0,0,1,1,0,1,1,1,0,1,
> +0,1,1,0,0,1,0,0,1,1,0,1,1,1,0,1,
> +0,1,1,1,0,1,0,0,1,1,1,0,1,1,1,0
> +};

Please fix codingstyle per what Roland's rip-relative instruction
support code in arch/x86_64/kernel/kprobes.c

> +
> +/*
>   * returns non-zero if opcodes can be boosted.
>   */
> -static inline int can_boost(kprobe_opcode_t opcode)
> +static inline int can_boost(kprobe_opcode_t *opcodes)
>  {
> -	switch (opcode & 0xf0 ) {
> +	kprobe_opcode_t opcode;
> +	kprobe_opcode_t *orig_opcodes = opcodes;
> +retry:
> +	if (opcodes - orig_opcodes > MAX_INSN_SIZE) return 0;

CodingStyle

> +	opcode = *(opcodes++);
> +	/* 2nd-byte opcode */
> +	if (opcode == 0x0f)
> +		return boostable_2nd_opcode[*opcodes];
> +	
> +	switch (opcode & 0xf0) {
> +	case 0x60:
> +		if (0x63 < opcode && opcode < 0x67)
> +			goto retry; /* prefixes */
> +		/* can't boost Address-size override and bound */
> +		return (opcode != 0x62 && opcode != 0x67);
>  	case 0x70:
>  		return 0; /* can't boost conditional jump */
> -	case 0x90:
> -		/* can't boost call and pushf */
> -		return opcode != 0x9a && opcode != 0x9c;
>  	case 0xc0:
> -		/* can't boost undefined opcodes and soft-interruptions */
> -		return (0xc1 < opcode && opcode < 0xc6) ||
> -			(0xc7 < opcode && opcode < 0xcc) || opcode == 0xcf;
> +		/* can't boost software-interruptions */
> +		return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
>  	case 0xd0:
>  		/* can boost AA* and XLAT */
>  		return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
>  	case 0xe0:
> -		/* can boost in/out and (may be) jmps */
> -		return (0xe3 < opcode && opcode != 0xe8);
> +		/* can boost in/out and absolute jmps */
> +		return ((opcode & 0x04) || opcode == 0xea);
>  	case 0xf0:
> +		if ((opcode & 0x0c) == 0 && opcode != 0xf1)
> +			goto retry; /* lock/rep(ne) prefix */
>  		/* clear and set flags can be boost */
>  		return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
>  	default:
> -		/* currently, can't boost 2 bytes opcodes */
> -		return opcode != 0x0f;
> +		if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
> +			goto retry; /* prefixes */
> +		/* can't boost CS override and call */
> +		return (opcode != 0x2e && opcode != 0x9a);
>  	}
>  }
> 
> @@ -109,7 +146,7 @@ int __kprobes arch_prepare_kprobe(struct
> 
>  	memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
>  	p->opcode = *p->addr;
> -	if (can_boost(p->opcode)) {
> +	if (can_boost(p->addr)) {
>  		p->ainsn.boostable = 0;
>  	} else {
>  		p->ainsn.boostable = -1;
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] kprobe: kprobe-2byte-booster
  2006-04-13  9:50 ` Ananth N Mavinakayanahalli
@ 2006-04-14  0:44   ` Masami Hiramatsu
  0 siblings, 0 replies; 3+ messages in thread
From: Masami Hiramatsu @ 2006-04-14  0:44 UTC (permalink / raw)
  To: ananth
  Cc: Prasanna S Panchamukhi, Keshavamurthy, Anil S, SystemTAP,
	Yumiko Sugita, Satoshi Oshima, Hideo Aoki

Hi, Ananth

Ananth N Mavinakayanahalli wrote:
> On Wed, Apr 12, 2006 at 07:10:30PM +0900, Masami Hiramatsu wrote:
>> Hi,
>>
>> Here is a patch of kprobe-booster enhancement on i386
>> This patch can be applied to 2.6.17-rc1.
>>
>> Previous kprobe-booster patch has not handled any 2byte opcodes
>> and prefixes. I checked whole IA32 opcode map and classified it.
>>
>> This patch enables kprobe to boost those 2byte opcodes and prefixes.
> 
> Masami,
> 
> Few comments below (I am not too well versed with IA32 internals to
> comment on the intricacies).

Thank you for review.
I modified it with reference to the x86-64 implementation of kprobes,
and added a boundary-condition check.

-- 
Masami HIRAMATSU
2nd Research Dept.
Hitachi, Ltd., Systems Development Laboratory
E-mail: hiramatu@sdl.hitachi.co.jp

 kprobes.c |   78 +++++++++++++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 65 insertions(+), 13 deletions(-)
diff --exclude=CVS -Narup a/arch/i386/kernel/kprobes.c b/arch/i386/kernel/kprobes.c
--- a/arch/i386/kernel/kprobes.c	2006-04-03 18:30:59.000000000 +0900
+++ b/arch/i386/kernel/kprobes.c	2006-04-13 22:53:14.000000000 +0900
@@ -57,30 +57,82 @@ static inline void set_jmp_op(void *from
 /*
  * returns non-zero if opcodes can be boosted.
  */
-static inline int can_boost(kprobe_opcode_t opcode)
+static inline int can_boost(kprobe_opcode_t *opcodes)
 {
-	switch (opcode & 0xf0 ) {
+#define W(row,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bb,bc,bd,be,bf)		      \
+	(((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) |   \
+	  (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) |   \
+	  (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) |   \
+	  (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf))    \
+	 << (row % 32))
+	/*
+	 * Undefined/reserved opcodes, conditional jump, Opcode Extension
+	 * Groups, and some special opcodes can not be boost.
+	 */
+	static const unsigned long twobyte_is_boostable[256 / 32] = {
+		/*      0 1 2 3 4 5 6 7 8 9 a b c d e f         */
+		/*      -------------------------------         */
+		W(0x00, 0,0,1,1,0,0,1,0,1,1,0,0,0,0,0,0)| /* 00 */
+		W(0x10, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 10 */
+		W(0x20, 1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0)| /* 20 */
+		W(0x30, 0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 30 */
+		W(0x40, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 40 */
+		W(0x50, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 50 */
+		W(0x60, 1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1)| /* 60 */
+		W(0x70, 0,0,0,0,1,1,1,1,0,0,0,0,0,0,1,1), /* 70 */
+		W(0x80, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 80 */
+		W(0x90, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1), /* 90 */
+		W(0xa0, 1,1,0,1,1,1,0,0,1,1,0,1,1,1,0,1)| /* a0 */
+		W(0xb0, 1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1), /* b0 */
+		W(0xc0, 1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1)| /* c0 */
+		W(0xd0, 0,1,1,1,0,1,0,0,1,1,0,1,1,1,0,1), /* d0 */
+		W(0xe0, 0,1,1,0,0,1,0,0,1,1,0,1,1,1,0,1)| /* e0 */
+		W(0xf0, 0,1,1,1,0,1,0,0,1,1,1,0,1,1,1,0)  /* f0 */
+		/*      -------------------------------         */
+		/*      0 1 2 3 4 5 6 7 8 9 a b c d e f         */
+	};
+#undef W
+	kprobe_opcode_t opcode;
+	kprobe_opcode_t *orig_opcodes = opcodes;
+retry:
+	if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
+		return 0;
+	opcode = *(opcodes++);
+
+	/* 2nd-byte opcode */
+	if (opcode == 0x0f) {
+		if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
+			return 0;
+		return test_bit(*opcodes, twobyte_is_boostable);
+	}
+
+	switch (opcode & 0xf0) {
+	case 0x60:
+		if (0x63 < opcode && opcode < 0x67)
+			goto retry; /* prefixes */
+		/* can't boost Address-size override and bound */
+		return (opcode != 0x62 && opcode != 0x67);
 	case 0x70:
 		return 0; /* can't boost conditional jump */
-	case 0x90:
-		/* can't boost call and pushf */
-		return opcode != 0x9a && opcode != 0x9c;
 	case 0xc0:
-		/* can't boost undefined opcodes and soft-interruptions */
-		return (0xc1 < opcode && opcode < 0xc6) ||
-			(0xc7 < opcode && opcode < 0xcc) || opcode == 0xcf;
+		/* can't boost software-interruptions */
+		return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
 	case 0xd0:
 		/* can boost AA* and XLAT */
 		return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
 	case 0xe0:
-		/* can boost in/out and (may be) jmps */
-		return (0xe3 < opcode && opcode != 0xe8);
+		/* can boost in/out and absolute jmps */
+		return ((opcode & 0x04) || opcode == 0xea);
 	case 0xf0:
+		if ((opcode & 0x0c) == 0 && opcode != 0xf1)
+			goto retry; /* lock/rep(ne) prefix */
 		/* clear and set flags can be boost */
 		return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
 	default:
-		/* currently, can't boost 2 bytes opcodes */
-		return opcode != 0x0f;
+		if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
+			goto retry; /* prefixes */
+		/* can't boost CS override and call */
+		return (opcode != 0x2e && opcode != 0x9a);
 	}
 }

@@ -109,7 +161,7 @@ int __kprobes arch_prepare_kprobe(struct

 	memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
 	p->opcode = *p->addr;
-	if (can_boost(p->opcode)) {
+	if (can_boost(p->addr)) {
 		p->ainsn.boostable = 0;
 	} else {
 		p->ainsn.boostable = -1;


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2006-04-14  0:44 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-04-12 10:10 [PATCH] kprobe: kprobe-2byte-booster Masami Hiramatsu
2006-04-13  9:50 ` Ananth N Mavinakayanahalli
2006-04-14  0:44   ` Masami Hiramatsu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).