MIPS Kprobes: Support branch instructions probing
PATCH 1/1 MIPS Kprobes: Support branch instructions probing
From: Maneesh Soni <[email protected]>
This patch provides support for kprobes on branch instructions. The branch instruction at the probed address is actually emulated and not executed out-of-line like other normal instructions. Instead the delay-slot instruction is copied and single stepped out of line.
At the time of probe hit, the original branch instruction is evaluated and the target cp0_epc is computed similar to compute_retrun_epc(). It is also checked if the delay slot instruction can be skipped, which is true if there is a NOP in delay slot or branch is taken in case of branch likely instructions. Once the delay slot instruction is single stepped the normal execution resume with the cp0_epc updated the earlier computed cp0_epc as per the branch instructions.
--- arch/mips/include/asm/kprobes.h | 7 + arch/mips/kernel/kprobes.c | 341 +++++++++++++++++++++++++++++++++++---- 2 files changed, 320 insertions(+), 28 deletions(-)
arch/mips/include/asm/kprobes.h +7 -0
@@ -74,6 +74,8 @@ struct prev_kprobe { : MAX_JPROBES_STACK_SIZE) +#define SKIP_DELAYSLOT 1 +/* per-cpu kprobe control block */ struct kprobe_ctlblk {unsigned long kprobe_status;
@@ -81,6 +83,11 @@ struct kprobe_ctlblk {unsigned long kprobe_saved_SR;unsigned long kprobe_saved_epc;unsigned long jprobe_saved_sp; + +/* Per-thread fields, used while emulating branches */ +unsigned long flags; +unsigned long target_epc; +struct pt_regs jprobe_saved_regs;u8 jprobes_stack[MAX_JPROBES_STACK_SIZE ];struct prev_kprobe prev_kprobe;
arch/mips/kernel/kprobes.c +313 -28
387 lines not on this page · raw message
Signature · 1 line
1.7.1
On Thu, Oct 13, 2011 at 02:37:49PM +0530, Maneesh Soni wrote:
...
I know nothing of MIPS internals, but...
Quoted 18 lines
static int
__kprobeskprobe_handler(struct pt_regs *regs)arch/mips/kernel/kprobes.c +6
-1@@ -239,8 +531,13 @@ static int __kprobes kprobe_handler(struct pt_regs *regs)save_previous_kprobe (kcb);set_current_kprobe (p, regs, kcb);kprobes_inc_nmissed_count (p); -prepare_singlestep (p, regs); +prepare_singlestep (p, regs, kcb); kcb->kprobe_status =KPROBE_REENTER ; +if (kcb->flags &SKIP_DELAYSLOT ) { +resume_execution (p, regs, kcb); +restore_previous_kprobe (kcb); +preempt_enable_no_resched (); + }return 1 ; }else {if (addr->word != breakpoint_insn.word) {@@ -284,8 +581,15 @@ static int __kprobes kprobe_handler(struct pt_regs *regs) }
arch/mips/kernel/kprobes.c static int __kprobes kprobe_handler(struct pt_regs *regs) · line 589
Quoted 10 lines
arch/mips/kernel/kprobes.c +6
-2ss_probe: -prepare_singlestep (p, regs); - kcb->kprobe_status =KPROBE_HIT_SS ; +prepare_singlestep (p, regs, kcb); +if (kcb->flags &SKIP_DELAYSLOT ) { + kcb->kprobe_status =KPROBE_HIT_SSDONE ; +if (p->post_handler) + p->post_handler (p, regs,0 ); +resume_execution (p, regs, kcb);
You are missing a preempt_disable_no_resched() here.
Ananth
Quoted 25 lines · Ananth N Mavinakayanahalli
On Thu, Oct 13, 2011 at 02:37:49PM +0530, Maneesh Soni wrote:
...
I know nothing of MIPS internals, but...
Quoted 19 lines
static int
__kprobeskprobe_handler(struct pt_regs *regs)arch/mips/kernel/kprobes.c +6
-1@@ -239,8 +531,13 @@ static int __kprobes kprobe_handler(struct pt_regs *regs)save_previous_kprobe (kcb);set_current_kprobe (p, regs, kcb);kprobes_inc_nmissed_count (p); -prepare_singlestep (p, regs); +prepare_singlestep (p, regs, kcb); kcb->kprobe_status =KPROBE_REENTER ; +if (kcb->flags &SKIP_DELAYSLOT ) { +resume_execution (p, regs, kcb); +restore_previous_kprobe (kcb); +preempt_enable_no_resched (); + }return 1 ; }else {if (addr->word != breakpoint_insn.word) {@@ -284,8 +581,15 @@ static int __kprobes kprobe_handler(struct pt_regs *regs) }
Quoted 11 lines
arch/mips/kernel/kprobes.c static int __kprobes kprobe_handler(struct pt_regs *regs) · line 589
Quoted 9 lines
arch/mips/kernel/kprobes.c +6
-2ss_probe: -prepare_singlestep (p, regs); - kcb->kprobe_status =KPROBE_HIT_SS ; +prepare_singlestep (p, regs, kcb); +if (kcb->flags &SKIP_DELAYSLOT ) { + kcb->kprobe_status =KPROBE_HIT_SSDONE ; +if (p->post_handler) + p->post_handler (p, regs,0 ); +resume_execution (p, regs, kcb);You are missing a
preempt_disable_no_resched()here.
Oops! I meant preempt_enable_no_resched().
Ananth
Quoted 25 lines · Ananth N Mavinakayanahalli
On Thu, Oct 13, 2011 at 02:37:49PM +0530, Maneesh Soni wrote:
...
I know nothing of MIPS internals, but...
Quoted 19 lines
static int
__kprobeskprobe_handler(struct pt_regs *regs)arch/mips/kernel/kprobes.c +6
-1@@ -239,8 +531,13 @@ static int __kprobes kprobe_handler(struct pt_regs *regs)save_previous_kprobe (kcb);set_current_kprobe (p, regs, kcb);kprobes_inc_nmissed_count (p); -prepare_singlestep (p, regs); +prepare_singlestep (p, regs, kcb); kcb->kprobe_status =KPROBE_REENTER ; +if (kcb->flags &SKIP_DELAYSLOT ) { +resume_execution (p, regs, kcb); +restore_previous_kprobe (kcb); +preempt_enable_no_resched (); + }return 1 ; }else {if (addr->word != breakpoint_insn.word) {@@ -284,8 +581,15 @@ static int __kprobes kprobe_handler(struct pt_regs *regs) }
Quoted 12 lines
arch/mips/kernel/kprobes.c static int __kprobes kprobe_handler(struct pt_regs *regs) · line 589
Quoted 9 lines
arch/mips/kernel/kprobes.c +6
-2ss_probe: -prepare_singlestep (p, regs); - kcb->kprobe_status =KPROBE_HIT_SS ; +prepare_singlestep (p, regs, kcb); +if (kcb->flags &SKIP_DELAYSLOT ) { + kcb->kprobe_status =KPROBE_HIT_SSDONE ; +if (p->post_handler) + p->post_handler (p, regs,0 ); +resume_execution (p, regs, kcb);You are missing a
preempt_disable_no_resched()here.
ok.. you meant preempt_enable_no_resched(). Will add it in the next version. Thanks for pointing it out.
Thanks
Maneesh
Quoted 8 lines · Maneesh Soni
From: Maneesh Soni<[email protected]>
This patch provides support for kprobes on branch instructions. The branch instruction at the probed address is actually emulated and not executed out-of-line like other normal instructions. Instead the delay-slot instruction is copied and single stepped out of line.
Quoted 8 lines
At the time of probe hit, the original branch instruction is evaluated and the target
cp0_epcis computed similar tocompute_retrun_epc(). It is also checked if the delay slot instruction can be skipped, which is true if there is a NOP in delay slot or branch is taken in case of branch likely instructions. Once the delay slot instruction is single stepped the normal execution resume with thecp0_epcupdated the earlier computedcp0_epcas per the branch instructions.
I haven't tested it but...
Quoted 6 lines
Signed-off-by Maneesh Soni<[email protected]>--- arch/mips/include/asm/kprobes.h | 7 + arch/mips/kernel/kprobes.c | 341 +++++++++++++++++++++++++++++++++++---- 2 files changed, 320 insertions(+), 28 deletions(-)
[...]
Quoted 21 lines
arch/mips/kernel/kprobes.c +20
-0+static int evaluate_branch_instruction (struct kprobe *p,struct pt_regs *regs, +struct kprobe_ctlblk *kcb) { +union mips_instruction insn = p->opcode; +unsigned int dspcontrol; +long epc; + + epc = regs->cp0_epc; +if (epc&3 ) + goto unaligned; + +if (p->ainsn.insn->word ==0 ) + kcb->flags |=SKIP_DELAYSLOT ; +else + kcb->flags&= ~SKIP_DELAYSLOT ; + +switch (insn.i_format.opcode) { +/* +* jr and jalr are in r_format format. +*/ +case spec_op:
[...]
arch/mips/kernel/kprobes.c static void set_current_kprobe(struct kprobe *p, struct pt_regs *regs, · line 433
Quoted 9 lines
arch/mips/kernel/kprobes.c +9
-0+case bgtzl_op: +/* rt field assumed to be zero */ +if ((long )regs->regs[insn.i_format.rs]>0 ) { + epc = epc +4 + (insn.i_format.simmediate<<2 ); + kcb->flags |=SKIP_DELAYSLOT ; + }else + epc +=8 ; + regs->cp0_epc = epc; +break ;
Where is the handling for:
case cop1_op:
#ifdef CONFIG_CPU_CAVIUM_OCTEON
case lwc2_op: /* This is bbit0 on Octeon */ case ldc2_op: /* This is bbit032 on Octeon */ case swc2_op: /* This is bbit1 on Octeon */ case sdc2_op: /* This is bbit132 on Octeon */
#endif
These are all defined in insn_has_delayslot() but not here.
Quoted 4 lines · David Daney
Where is the handling for:
case
cop1_op:
Quoted 8 lines
#ifdef
CONFIG_CPU_CAVIUM_OCTEON
caselwc2_op: /* This is bbit0 on Octeon */ caseldc2_op: /* This is bbit032 on Octeon */ caseswc2_op: /* This is bbit1 on Octeon */ casesdc2_op: /* This is bbit132 on Octeon */
#endifThese are all defined in
insn_has_delayslot()but not here.
Which is a wonderful demonstration for why duplicating such a large function from branch.c was a baaad thing to do.
Maneesh, can you refactor the code to share everything that was copied from __compute_return_epc() can be shared with kprobes? Idealy make everything a two part series, first one patch to refactor branch.c and the 2nd patch to deal with kprobes.
Thanks,
Ralf
Quoted 6 lines · Ralf Baechle
Quoted 4 lines · David Daney
Where is the handling for:
case
cop1_op:
Quoted 8 lines
Quoted 8 lines
#ifdef
CONFIG_CPU_CAVIUM_OCTEON
caselwc2_op: /* This is bbit0 on Octeon */ caseldc2_op: /* This is bbit032 on Octeon */ caseswc2_op: /* This is bbit1 on Octeon */ casesdc2_op: /* This is bbit132 on Octeon */
#endifThese are all defined in
insn_has_delayslot()but not here.
David, nice catch!
Quoted 6 lines
Which is a wonderful demonstration for why duplicating such a large function from
branch.cwas a baaad thing to do.Maneesh, can you refactor the code to share everything that was copied from
__compute_return_epc()can be shared with kprobes? Idealy make everything a two part series, first one patch to refactorbranch.c
Yes, it does make a lot of sense. Don't you think we need to do EXPORT_SYMBOL for __compute_return_epc as well? So it could be used by klms.
Actually we have yet another copy of it in mips uprobes code, which normally is built as klm, if we refactor and export __compute_return_epc all three places could use the same function.
Thanks,
Victor
Quoted 7 lines
and
the 2nd patch to deal with kprobes.Thanks,
Ralf
Quoted 7 lines · Victor Kamensky
Yes, it does make a lot of sense. Don't you think we need to do
EXPORT_SYMBOLfor__compute_return_epcas well? So it could be used by klms.Actually we have yet another copy of it in mips uprobes code, which normally is built as klm, if we refactor and export
__compute_return_epcall three places could use the same function.
Nothing wrong with exporting __compute_return_epc() as long as there are actually users of the exported symbol. So far it's only used from the kernel proper which is why it's not been exported. However I'd prefer it to be exported as EXPORT_SYMBOL_GPL().
Ralf
Quoted 15 lines · Ralf Baechle
Quoted 4 lines · David Daney
Where is the handling for:
case
cop1_op:Quoted 8 lines
#ifdef
CONFIG_CPU_CAVIUM_OCTEON
caselwc2_op: /* This is bbit0 on Octeon */ caseldc2_op: /* This is bbit032 on Octeon */ caseswc2_op: /* This is bbit1 on Octeon */ casesdc2_op: /* This is bbit132 on Octeon */
#endifThese are all defined in
insn_has_delayslot()but not here.
Quoted 8 lines
Which is a wonderful demonstration for why duplicating such a large function from
branch.cwas a baaad thing to do.Maneesh, can you refactor the code to share everything that was copied from
__compute_return_epc()can be shared with kprobes? Idealy make everything a two part series, first one patch to refactorbranch.cand the 2nd patch to deal with kprobes.
Sure.. the branch likely instructions are not make it look good but still do it in the next version.
Thanks for the comments.
Regards,
Maneesh
Quoted 66 lines · David Daney
Quoted 8 lines · Maneesh Soni
From: Maneesh Soni<[email protected]>
This patch provides support for kprobes on branch instructions. The branch instruction at the probed address is actually emulated and not executed out-of-line like other normal instructions. Instead the delay-slot instruction is copied and single stepped out of line.
Quoted 8 lines
At the time of probe hit, the original branch instruction is evaluated and the target
cp0_epcis computed similar tocompute_retrun_epc(). It is also checked if the delay slot instruction can be skipped, which is true if there is a NOP in delay slot or branch is taken in case of branch likely instructions. Once the delay slot instruction is single stepped the normal execution resume with thecp0_epcupdated the earlier computedcp0_epcas per the branch instructions.I haven't tested it but...
Quoted 6 lines
Signed-off-by Maneesh Soni<[email protected]>--- arch/mips/include/asm/kprobes.h | 7 + arch/mips/kernel/kprobes.c | 341 +++++++++++++++++++++++++++++++++++---- 2 files changed, 320 insertions(+), 28 deletions(-)[...]
Quoted 21 lines
arch/mips/kernel/kprobes.c +20
-0+static int evaluate_branch_instruction (struct kprobe *p,struct pt_regs *regs, +struct kprobe_ctlblk *kcb) { +union mips_instruction insn = p->opcode; +unsigned int dspcontrol; +long epc; + + epc = regs->cp0_epc; +if (epc&3 ) + goto unaligned; + +if (p->ainsn.insn->word ==0 ) + kcb->flags |=SKIP_DELAYSLOT ; +else + kcb->flags&= ~SKIP_DELAYSLOT ; + +switch (insn.i_format.opcode) { +/* +* jr and jalr are in r_format format. +*/ +case spec_op:[...]
arch/mips/kernel/kprobes.c static void set_current_kprobe(struct kprobe *p, struct pt_regs *regs, · line 433
Quoted 9 lines
arch/mips/kernel/kprobes.c +9
-0+case bgtzl_op: +/* rt field assumed to be zero */ +if ((long )regs->regs[insn.i_format.rs]>0 ) { + epc = epc +4 + (insn.i_format.simmediate<<2 ); + kcb->flags |=SKIP_DELAYSLOT ; + }else + epc +=8 ; + regs->cp0_epc = epc; +break ;Where is the handling for:
case
cop1_op:
Quoted 8 lines
#ifdef
CONFIG_CPU_CAVIUM_OCTEON
caselwc2_op: /* This is bbit0 on Octeon */ caseldc2_op: /* This is bbit032 on Octeon */ caseswc2_op: /* This is bbit1 on Octeon */ casesdc2_op: /* This is bbit132 on Octeon */
#endifThese are all defined in
insn_has_delayslot()but not here.
My bad.. will include them as well. Actually as Ralf suggested, will keep this as common code.
Thanks
Maneesh