--- v4
+++ v3
@@ -1,58 +1,73 @@
-Kprobe placed on the kretprobe_trampoline during boot time can be
-optimized, since the instruction at probe point is a 'nop'.
+From: "Naveen N. Rao" <naveen.n.rao@linux.vnet.ibm.com>
+To permit the use of relative branch instruction in powerpc, the target
+address has to be relatively nearby, since the address is specified in an
+immediate field (24 bit filed) in the instruction opcode itself. Here
+nearby refers to 32MB on either side of the current instruction.
+
+This patch verifies whether the target address is within +/- 32MB
+range or not.
+
+Signed-off-by: Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
Signed-off-by: Anju T Sudhakar <anju@linux.vnet.ibm.com>
-Acked-by: Masami Hiramatsu <mhiramat@kernel.org>
---
- arch/powerpc/kernel/kprobes.c | 8 ++++++++
- arch/powerpc/kernel/optprobes.c | 7 +++----
- 2 files changed, 11 insertions(+), 4 deletions(-)
+ arch/powerpc/include/asm/code-patching.h | 1 +
+ arch/powerpc/lib/code-patching.c | 24 +++++++++++++++++++++++-
+ 2 files changed, 24 insertions(+), 1 deletion(-)
-diff --git a/arch/powerpc/kernel/kprobes.c b/arch/powerpc/kernel/kprobes.c
-index e785cc9..5b0fd07 100644
---- a/arch/powerpc/kernel/kprobes.c
-+++ b/arch/powerpc/kernel/kprobes.c
-@@ -282,6 +282,7 @@ asm(".global kretprobe_trampoline\n"
- ".type kretprobe_trampoline, @function\n"
- "kretprobe_trampoline:\n"
- "nop\n"
-+ "blr\n"
- ".size kretprobe_trampoline, .-kretprobe_trampoline\n");
+diff --git a/arch/powerpc/include/asm/code-patching.h b/arch/powerpc/include/asm/code-patching.h
+index 2015b07..75ee4f4 100644
+--- a/arch/powerpc/include/asm/code-patching.h
++++ b/arch/powerpc/include/asm/code-patching.h
+@@ -22,6 +22,7 @@
+ #define BRANCH_SET_LINK 0x1
+ #define BRANCH_ABSOLUTE 0x2
- /*
-@@ -334,6 +335,13 @@ static int __kprobes trampoline_probe_handler(struct kprobe *p,
++bool is_offset_in_branch_range(long offset);
+ unsigned int create_branch(const unsigned int *addr,
+ unsigned long target, int flags);
+ unsigned int create_cond_branch(const unsigned int *addr,
+diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c
+index d5edbeb..f643451 100644
+--- a/arch/powerpc/lib/code-patching.c
++++ b/arch/powerpc/lib/code-patching.c
+@@ -32,6 +32,28 @@ int patch_branch(unsigned int *addr, unsigned long target, int flags)
+ return patch_instruction(addr, create_branch(addr, target, flags));
+ }
- kretprobe_assert(ri, orig_ret_address, trampoline_address);
- regs->nip = orig_ret_address;
++bool is_offset_in_branch_range(long offset)
++{
+ /*
-+ * Make LR point to the orig_ret_address.
-+ * When the 'nop' inside the kretprobe_trampoline
-+ * is optimized, we can do a 'blr' after executing the
-+ * detour buffer code.
++ * Powerpc branch instruction is :
++ *
++ * 0 6 30 31
++ * +---------+----------------+---+---+
++ * | opcode | LI |AA |LK |
++ * +---------+----------------+---+---+
++ * Where AA = 0 and LK = 0
++ *
++ * LI is a signed 24 bits integer. The real branch offset is computed
++ * by: imm32 = SignExtend(LI:'0b00', 32);
++ *
++ * So the maximum forward branch should be:
++ * (0x007fffff << 2) = 0x01fffffc = 0x1fffffc
++ * The maximum backward branch should be:
++ * (0xff800000 << 2) = 0xfe000000 = -0x2000000
+ */
-+ regs->link = orig_ret_address;
++ return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
++}
++
+ unsigned int create_branch(const unsigned int *addr,
+ unsigned long target, int flags)
+ {
+@@ -43,7 +65,7 @@ unsigned int create_branch(const unsigned int *addr,
+ offset = offset - (unsigned long)addr;
- reset_current_kprobe();
- kretprobe_hash_unlock(current, &flags);
-diff --git a/arch/powerpc/kernel/optprobes.c b/arch/powerpc/kernel/optprobes.c
-index ecba221..5e4c254 100644
---- a/arch/powerpc/kernel/optprobes.c
-+++ b/arch/powerpc/kernel/optprobes.c
-@@ -72,12 +72,11 @@ static unsigned long can_optimize(struct kprobe *p)
+ /* Check we can represent the target in the instruction format */
+- if (offset < -0x2000000 || offset > 0x1fffffc || offset & 0x3)
++ if (!is_offset_in_branch_range(offset))
+ return 0;
- /*
- * kprobe placed for kretprobe during boot time
-- * is not optimizing now.
-- *
-- * TODO: Optimize kprobe in kretprobe_trampoline
-+ * has a 'nop' instruction, which can be emulated.
-+ * So further checks can be skipped.
- */
- if (p->addr == (kprobe_opcode_t *)&kretprobe_trampoline)
-- return 0;
-+ return (unsigned long)p->addr + sizeof(kprobe_opcode_t);
-
- /*
- * We only support optimizing kernel addresses, but not
+ /* Mask out the flags and target, so they don't step on each other. */
--
2.7.4