public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Harvey Harrison <harvey.harrison@gmail.com>
To: Masami Hiramatsu <mhiramat@redhat.com>, Ingo Molnar <mingo@elte.hu>
Cc: Jim Keniston <jkenisto@us.ibm.com>,
	Srikar Dronamraju <srikar@linux.vnet.ibm.com>,
	LKML <linux-kernel@vger.kernel.org>,
	Maneesh Soni <maneesh@linux.vnet.ibm.com>,
	srinivasa@in.ibm.com,
	Ananth N Mavinakayanahalli <ananth@in.ibm.com>,
	Masami Hiramatsu <hiramatu@sdl.hitachi.co.jp>,
	Rusty Lynch <rusty.lynch@intel.com>,
	Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>,
	Keshavamurthy Anil S <anil.s.keshavamurthy@intel.com>
Subject: [PATCH] x86: kprobes use stack_addr() macro
Date: Mon, 17 Dec 2007 19:10:23 -0800	[thread overview]
Message-ID: <1197947423.7734.4.camel@brick> (raw)
In-Reply-To: <47671407.7050607@redhat.com>

Signed-off-by: Harvey Harrison <harvey.harrison@gmail.com>
---
Replacement for the last patch in the kprobes series I just sent.

 arch/x86/kernel/kprobes.c |   45 +++++++++++++++++++++------------------------
 1 files changed, 21 insertions(+), 24 deletions(-)

diff --git a/arch/x86/kernel/kprobes.c b/arch/x86/kernel/kprobes.c
index 6f52c5e..c9df6fb 100644
--- a/arch/x86/kernel/kprobes.c
+++ b/arch/x86/kernel/kprobes.c
@@ -44,6 +44,20 @@ struct kretprobe_blackpoint kretprobe_blacklist[] = {
 };
 const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
 
+/*
+ * "&regs->sp" looks wrong, but it's correct for x86_32.  x86_32 CPUs
+ * don't save the ss and esp registers if the CPU is already in kernel
+ * mode when it traps.  So for kprobes, regs->sp and regs->ss are not
+ * the [nonexistent] saved stack pointer and ss register, but rather
+ * the top 8 bytes of the pre-int3 stack.  So &regs->sp happens to
+ * point to the top of the pre-int3 stack.
+ */
+#ifdef CONFIG_X86_32
+# define stack_addr(regs) ((unsigned long *)&regs->sp)
+#else
+# define stack_addr(regs) ((unsigned long *)regs->sp)
+#endif
+
 #define W(r, 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) |   \
@@ -409,11 +423,8 @@ static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
 				      struct pt_regs *regs)
 {
-#ifdef CONFIG_X86_32
-	unsigned long *sara = (unsigned long *)&regs->sp;
-#else
-	unsigned long *sara = (unsigned long *)regs->sp;
-#endif
+	unsigned long *sara = stack_addr(regs);
+
 	ri->ret_addr = (kprobe_opcode_t *) *sara;
 	/* Replace the return addr with trampoline addr */
 	*sara = (unsigned long) &kretprobe_trampoline;
@@ -751,11 +762,8 @@ void *__kprobes trampoline_handler(struct pt_regs *regs)
 static void __kprobes resume_execution(struct kprobe *p,
 		struct pt_regs *regs, struct kprobe_ctlblk *kcb)
 {
-#ifdef CONFIG_X86_32
-	unsigned long *tos = (unsigned long *)&regs->sp;
-#else
-	unsigned long *tos = (unsigned long *)regs->sp;
-#endif
+
+	unsigned long *tos = stack_addr(regs);
 	unsigned long copy_ip = (unsigned long)p->ainsn.insn;
 	unsigned long orig_ip = (unsigned long)p->addr;
 	kprobe_opcode_t *insn = p->ainsn.insn;
@@ -984,11 +992,7 @@ int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
 
 	kcb->jprobe_saved_regs = *regs;
-#ifdef CONFIG_X86_32
-	kcb->jprobe_saved_sp = &regs->sp;
-#else
-	kcb->jprobe_saved_sp = (long *) regs->sp;
-#endif
+	kcb->jprobe_saved_sp = (long *)stack_addr(regs);
 	addr = (unsigned long)(kcb->jprobe_saved_sp);
 	/*
 	 * As Linus pointed out, gcc assumes that the callee
@@ -1033,17 +1037,10 @@ int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
 	struct jprobe *jp = container_of(p, struct jprobe, kp);
 
 	if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
-#ifdef CONFIG_X86_32
-		if (&regs->sp != kcb->jprobe_saved_sp) {
+		if (stack_addr(regs) != kcb->jprobe_saved_sp) {
 			struct pt_regs *saved_regs = &kcb->jprobe_saved_sp;
 			printk("current sp %p does not match saved sp %p\n",
-			       &regs->sp, kcb->jprobe_saved_sp);
-#else
-		if ((long *)regs->sp != kcb->jprobe_saved_sp) {
-			struct pt_regs *saved_regs = &kcb->jprobe_saved_sp;
-			printk("current sp %p does not match saved sp %p\n",
-			       (long *)regs->sp, kcb->jprobe_saved_sp);
-#endif
+			       stack_addr(regs), kcb->jprobe_saved_sp);
 			printk("Saved registers for jprobe %p\n", jp);
 			show_registers(saved_regs);
 			printk("Current registers\n");
-- 
1.5.4.rc0.1083.gf568




      parent reply	other threads:[~2007-12-18  3:10 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-12-15  8:45 FInal kprobes rollup patches Harvey Harrison
2007-12-15  8:50 ` Ingo Molnar
2007-12-15  9:04   ` Harvey Harrison
2007-12-15 13:12     ` Ingo Molnar
2007-12-17 14:12       ` Srikar Dronamraju
2007-12-17 14:13       ` Masami Hiramatsu
2007-12-17 14:30         ` Final " Ingo Molnar
2007-12-17 15:29           ` Masami Hiramatsu
2007-12-17 16:06             ` Ingo Molnar
2007-12-17 16:19               ` Masami Hiramatsu
2007-12-17 20:17               ` Harvey Harrison
2007-12-17 14:22       ` FInal " Srikar Dronamraju
2007-12-17 19:20         ` Harvey Harrison
2007-12-17 21:28           ` Masami Hiramatsu
2007-12-17 21:36             ` Harvey Harrison
2007-12-17 21:52               ` Masami Hiramatsu
2007-12-17 22:00                 ` Harvey Harrison
2007-12-17 23:14                   ` Masami Hiramatsu
2007-12-17 23:27                     ` Harvey Harrison
2007-12-17 23:56                       ` Masami Hiramatsu
2007-12-18  0:27                         ` Masami Hiramatsu
2007-12-18  2:15                           ` Harvey Harrison
2007-12-18  3:10                           ` Harvey Harrison [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1197947423.7734.4.camel@brick \
    --to=harvey.harrison@gmail.com \
    --cc=ananth@in.ibm.com \
    --cc=anil.s.keshavamurthy@intel.com \
    --cc=hiramatu@sdl.hitachi.co.jp \
    --cc=jkenisto@us.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maneesh@linux.vnet.ibm.com \
    --cc=masami.hiramatsu.pt@hitachi.com \
    --cc=mhiramat@redhat.com \
    --cc=mingo@elte.hu \
    --cc=rusty.lynch@intel.com \
    --cc=srikar@linux.vnet.ibm.com \
    --cc=srinivasa@in.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox