2 * Kernel Probes (KProbes)
3 * arch/mips/kernel/kprobes.c
5 * Copyright 2006 Sony Corp.
6 * Copyright 2010 Cavium Networks
8 * Some portions copied from the powerpc version.
10 * Copyright (C) IBM Corporation, 2002, 2004
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; version 2 of the License.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/kprobes.h>
27 #include <linux/preempt.h>
28 #include <linux/uaccess.h>
29 #include <linux/kdebug.h>
30 #include <linux/slab.h>
32 #include <asm/ptrace.h>
33 #include <asm/branch.h>
34 #include <asm/break.h>
37 static const union mips_instruction breakpoint_insn
= {
40 .code
= BRK_KPROBE_BP
,
45 static const union mips_instruction breakpoint2_insn
= {
48 .code
= BRK_KPROBE_SSTEPBP
,
53 DEFINE_PER_CPU(struct kprobe
*, current_kprobe
);
54 DEFINE_PER_CPU(struct kprobe_ctlblk
, kprobe_ctlblk
);
56 static int __kprobes
insn_has_delayslot(union mips_instruction insn
)
58 switch (insn
.i_format
.opcode
) {
61 * This group contains:
62 * jr and jalr are in r_format format.
65 switch (insn
.r_format
.func
) {
74 * This group contains:
75 * bltz_op, bgez_op, bltzl_op, bgezl_op,
76 * bltzal_op, bgezal_op, bltzall_op, bgezall_op.
81 * These are unconditional and in j_format.
87 * These are conditional and in i_format.
99 * These are the FPA/cp1 branch instructions.
103 #ifdef CONFIG_CPU_CAVIUM_OCTEON
104 case lwc2_op
: /* This is bbit0 on Octeon */
105 case ldc2_op
: /* This is bbit032 on Octeon */
106 case swc2_op
: /* This is bbit1 on Octeon */
107 case sdc2_op
: /* This is bbit132 on Octeon */
118 * insn_has_ll_or_sc function checks whether instruction is ll or sc
119 * one; putting breakpoint on top of atomic ll/sc pair is bad idea;
120 * so we need to prevent it and refuse kprobes insertion for such
121 * instructions; cannot do much about breakpoint in the middle of
122 * ll/sc pair; it is upto user to avoid those places
124 static int __kprobes
insn_has_ll_or_sc(union mips_instruction insn
)
128 switch (insn
.i_format
.opcode
) {
141 int __kprobes
arch_prepare_kprobe(struct kprobe
*p
)
143 union mips_instruction insn
;
144 union mips_instruction prev_insn
;
149 if (insn_has_ll_or_sc(insn
)) {
150 pr_notice("Kprobes for ll and sc instructions are not"
156 if ((probe_kernel_read(&prev_insn
, p
->addr
- 1,
157 sizeof(mips_instruction
)) == 0) &&
158 insn_has_delayslot(prev_insn
)) {
159 pr_notice("Kprobes for branch delayslot are not supported\n");
164 /* insn: must be on special executable page on mips. */
165 p
->ainsn
.insn
= get_insn_slot();
166 if (!p
->ainsn
.insn
) {
172 * In the kprobe->ainsn.insn[] array we store the original
173 * instruction at index zero and a break trap instruction at
176 * On MIPS arch if the instruction at probed address is a
177 * branch instruction, we need to execute the instruction at
178 * Branch Delayslot (BD) at the time of probe hit. As MIPS also
179 * doesn't have single stepping support, the BD instruction can
180 * not be executed in-line and it would be executed on SSOL slot
181 * using a normal breakpoint instruction in the next slot.
182 * So, read the instruction and save it for later execution.
184 if (insn_has_delayslot(insn
))
185 memcpy(&p
->ainsn
.insn
[0], p
->addr
+ 1, sizeof(kprobe_opcode_t
));
187 memcpy(&p
->ainsn
.insn
[0], p
->addr
, sizeof(kprobe_opcode_t
));
189 p
->ainsn
.insn
[1] = breakpoint2_insn
;
190 p
->opcode
= *p
->addr
;
196 void __kprobes
arch_arm_kprobe(struct kprobe
*p
)
198 *p
->addr
= breakpoint_insn
;
202 void __kprobes
arch_disarm_kprobe(struct kprobe
*p
)
204 *p
->addr
= p
->opcode
;
208 void __kprobes
arch_remove_kprobe(struct kprobe
*p
)
211 free_insn_slot(p
->ainsn
.insn
, 0);
212 p
->ainsn
.insn
= NULL
;
216 static void save_previous_kprobe(struct kprobe_ctlblk
*kcb
)
218 kcb
->prev_kprobe
.kp
= kprobe_running();
219 kcb
->prev_kprobe
.status
= kcb
->kprobe_status
;
220 kcb
->prev_kprobe
.old_SR
= kcb
->kprobe_old_SR
;
221 kcb
->prev_kprobe
.saved_SR
= kcb
->kprobe_saved_SR
;
222 kcb
->prev_kprobe
.saved_epc
= kcb
->kprobe_saved_epc
;
225 static void restore_previous_kprobe(struct kprobe_ctlblk
*kcb
)
227 __this_cpu_write(current_kprobe
, kcb
->prev_kprobe
.kp
);
228 kcb
->kprobe_status
= kcb
->prev_kprobe
.status
;
229 kcb
->kprobe_old_SR
= kcb
->prev_kprobe
.old_SR
;
230 kcb
->kprobe_saved_SR
= kcb
->prev_kprobe
.saved_SR
;
231 kcb
->kprobe_saved_epc
= kcb
->prev_kprobe
.saved_epc
;
234 static void set_current_kprobe(struct kprobe
*p
, struct pt_regs
*regs
,
235 struct kprobe_ctlblk
*kcb
)
237 __this_cpu_write(current_kprobe
, p
);
238 kcb
->kprobe_saved_SR
= kcb
->kprobe_old_SR
= (regs
->cp0_status
& ST0_IE
);
239 kcb
->kprobe_saved_epc
= regs
->cp0_epc
;
243 * evaluate_branch_instrucion -
245 * Evaluate the branch instruction at probed address during probe hit. The
246 * result of evaluation would be the updated epc. The insturction in delayslot
247 * would actually be single stepped using a normal breakpoint) on SSOL slot.
249 * The result is also saved in the kprobe control block for later use,
250 * in case we need to execute the delayslot instruction. The latter will be
251 * false for NOP instruction in dealyslot and the branch-likely instructions
252 * when the branch is taken. And for those cases we set a flag as
253 * SKIP_DELAYSLOT in the kprobe control block
255 static int evaluate_branch_instruction(struct kprobe
*p
, struct pt_regs
*regs
,
256 struct kprobe_ctlblk
*kcb
)
258 union mips_instruction insn
= p
->opcode
;
266 if (p
->ainsn
.insn
->word
== 0)
267 kcb
->flags
|= SKIP_DELAYSLOT
;
269 kcb
->flags
&= ~SKIP_DELAYSLOT
;
271 ret
= __compute_return_epc_for_insn(regs
, insn
);
275 if (ret
== BRANCH_LIKELY_TAKEN
)
276 kcb
->flags
|= SKIP_DELAYSLOT
;
278 kcb
->target_epc
= regs
->cp0_epc
;
283 pr_notice("%s: unaligned epc - sending SIGBUS.\n", current
->comm
);
284 force_sig(SIGBUS
, current
);
289 static void prepare_singlestep(struct kprobe
*p
, struct pt_regs
*regs
,
290 struct kprobe_ctlblk
*kcb
)
294 regs
->cp0_status
&= ~ST0_IE
;
296 /* single step inline if the instruction is a break */
297 if (p
->opcode
.word
== breakpoint_insn
.word
||
298 p
->opcode
.word
== breakpoint2_insn
.word
)
299 regs
->cp0_epc
= (unsigned long)p
->addr
;
300 else if (insn_has_delayslot(p
->opcode
)) {
301 ret
= evaluate_branch_instruction(p
, regs
, kcb
);
303 pr_notice("Kprobes: Error in evaluating branch\n");
307 regs
->cp0_epc
= (unsigned long)&p
->ainsn
.insn
[0];
311 * Called after single-stepping. p->addr is the address of the
312 * instruction whose first byte has been replaced by the "break 0"
313 * instruction. To avoid the SMP problems that can occur when we
314 * temporarily put back the original opcode to single-step, we
315 * single-stepped a copy of the instruction. The address of this
316 * copy is p->ainsn.insn.
318 * This function prepares to return from the post-single-step
319 * breakpoint trap. In case of branch instructions, the target
320 * epc to be restored.
322 static void __kprobes
resume_execution(struct kprobe
*p
,
323 struct pt_regs
*regs
,
324 struct kprobe_ctlblk
*kcb
)
326 if (insn_has_delayslot(p
->opcode
))
327 regs
->cp0_epc
= kcb
->target_epc
;
329 unsigned long orig_epc
= kcb
->kprobe_saved_epc
;
330 regs
->cp0_epc
= orig_epc
+ 4;
334 static int __kprobes
kprobe_handler(struct pt_regs
*regs
)
338 kprobe_opcode_t
*addr
;
339 struct kprobe_ctlblk
*kcb
;
341 addr
= (kprobe_opcode_t
*) regs
->cp0_epc
;
344 * We don't want to be preempted for the entire
345 * duration of kprobe processing
348 kcb
= get_kprobe_ctlblk();
350 /* Check we're not actually recursing */
351 if (kprobe_running()) {
352 p
= get_kprobe(addr
);
354 if (kcb
->kprobe_status
== KPROBE_HIT_SS
&&
355 p
->ainsn
.insn
->word
== breakpoint_insn
.word
) {
356 regs
->cp0_status
&= ~ST0_IE
;
357 regs
->cp0_status
|= kcb
->kprobe_saved_SR
;
361 * We have reentered the kprobe_handler(), since
362 * another probe was hit while within the handler.
363 * We here save the original kprobes variables and
364 * just single step on the instruction of the new probe
365 * without calling any user handlers.
367 save_previous_kprobe(kcb
);
368 set_current_kprobe(p
, regs
, kcb
);
369 kprobes_inc_nmissed_count(p
);
370 prepare_singlestep(p
, regs
, kcb
);
371 kcb
->kprobe_status
= KPROBE_REENTER
;
372 if (kcb
->flags
& SKIP_DELAYSLOT
) {
373 resume_execution(p
, regs
, kcb
);
374 restore_previous_kprobe(kcb
);
375 preempt_enable_no_resched();
379 if (addr
->word
!= breakpoint_insn
.word
) {
381 * The breakpoint instruction was removed by
382 * another cpu right after we hit, no further
383 * handling of this interrupt is appropriate
388 p
= __this_cpu_read(current_kprobe
);
389 if (p
->break_handler
&& p
->break_handler(p
, regs
))
395 p
= get_kprobe(addr
);
397 if (addr
->word
!= breakpoint_insn
.word
) {
399 * The breakpoint instruction was removed right
400 * after we hit it. Another cpu has removed
401 * either a probepoint or a debugger breakpoint
402 * at this address. In either case, no further
403 * handling of this interrupt is appropriate.
407 /* Not one of ours: let kernel handle it */
411 set_current_kprobe(p
, regs
, kcb
);
412 kcb
->kprobe_status
= KPROBE_HIT_ACTIVE
;
414 if (p
->pre_handler
&& p
->pre_handler(p
, regs
)) {
415 /* handler has already set things up, so skip ss setup */
420 prepare_singlestep(p
, regs
, kcb
);
421 if (kcb
->flags
& SKIP_DELAYSLOT
) {
422 kcb
->kprobe_status
= KPROBE_HIT_SSDONE
;
424 p
->post_handler(p
, regs
, 0);
425 resume_execution(p
, regs
, kcb
);
426 preempt_enable_no_resched();
428 kcb
->kprobe_status
= KPROBE_HIT_SS
;
433 preempt_enable_no_resched();
438 static inline int post_kprobe_handler(struct pt_regs
*regs
)
440 struct kprobe
*cur
= kprobe_running();
441 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
446 if ((kcb
->kprobe_status
!= KPROBE_REENTER
) && cur
->post_handler
) {
447 kcb
->kprobe_status
= KPROBE_HIT_SSDONE
;
448 cur
->post_handler(cur
, regs
, 0);
451 resume_execution(cur
, regs
, kcb
);
453 regs
->cp0_status
|= kcb
->kprobe_saved_SR
;
455 /* Restore back the original saved kprobes variables and continue. */
456 if (kcb
->kprobe_status
== KPROBE_REENTER
) {
457 restore_previous_kprobe(kcb
);
460 reset_current_kprobe();
462 preempt_enable_no_resched();
467 static inline int kprobe_fault_handler(struct pt_regs
*regs
, int trapnr
)
469 struct kprobe
*cur
= kprobe_running();
470 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
472 if (cur
->fault_handler
&& cur
->fault_handler(cur
, regs
, trapnr
))
475 if (kcb
->kprobe_status
& KPROBE_HIT_SS
) {
476 resume_execution(cur
, regs
, kcb
);
477 regs
->cp0_status
|= kcb
->kprobe_old_SR
;
479 reset_current_kprobe();
480 preempt_enable_no_resched();
486 * Wrapper routine for handling exceptions.
488 int __kprobes
kprobe_exceptions_notify(struct notifier_block
*self
,
489 unsigned long val
, void *data
)
492 struct die_args
*args
= (struct die_args
*)data
;
493 int ret
= NOTIFY_DONE
;
497 if (kprobe_handler(args
->regs
))
501 if (post_kprobe_handler(args
->regs
))
506 /* kprobe_running() needs smp_processor_id() */
510 && kprobe_fault_handler(args
->regs
, args
->trapnr
))
520 int __kprobes
setjmp_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
522 struct jprobe
*jp
= container_of(p
, struct jprobe
, kp
);
523 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
525 kcb
->jprobe_saved_regs
= *regs
;
526 kcb
->jprobe_saved_sp
= regs
->regs
[29];
528 memcpy(kcb
->jprobes_stack
, (void *)kcb
->jprobe_saved_sp
,
529 MIN_JPROBES_STACK_SIZE(kcb
->jprobe_saved_sp
));
531 regs
->cp0_epc
= (unsigned long)(jp
->entry
);
536 /* Defined in the inline asm below. */
537 void jprobe_return_end(void);
539 void __kprobes
jprobe_return(void)
541 /* Assembler quirk necessitates this '0,code' business. */
544 ".globl jprobe_return_end\n"
545 "jprobe_return_end:\n"
546 : : "n" (BRK_KPROBE_BP
) : "memory");
549 int __kprobes
longjmp_break_handler(struct kprobe
*p
, struct pt_regs
*regs
)
551 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
553 if (regs
->cp0_epc
>= (unsigned long)jprobe_return
&&
554 regs
->cp0_epc
<= (unsigned long)jprobe_return_end
) {
555 *regs
= kcb
->jprobe_saved_regs
;
556 memcpy((void *)kcb
->jprobe_saved_sp
, kcb
->jprobes_stack
,
557 MIN_JPROBES_STACK_SIZE(kcb
->jprobe_saved_sp
));
558 preempt_enable_no_resched();
566 * Function return probe trampoline:
567 * - init_kprobes() establishes a probepoint here
568 * - When the probed function returns, this probe causes the
571 static void __used
kretprobe_trampoline_holder(void)
575 /* Keep the assembler from reordering and placing JR here. */
578 ".global kretprobe_trampoline\n"
579 "kretprobe_trampoline:\n\t"
585 void kretprobe_trampoline(void);
587 void __kprobes
arch_prepare_kretprobe(struct kretprobe_instance
*ri
,
588 struct pt_regs
*regs
)
590 ri
->ret_addr
= (kprobe_opcode_t
*) regs
->regs
[31];
592 /* Replace the return addr with trampoline addr */
593 regs
->regs
[31] = (unsigned long)kretprobe_trampoline
;
597 * Called when the probe at kretprobe trampoline is hit
599 static int __kprobes
trampoline_probe_handler(struct kprobe
*p
,
600 struct pt_regs
*regs
)
602 struct kretprobe_instance
*ri
= NULL
;
603 struct hlist_head
*head
, empty_rp
;
604 struct hlist_node
*tmp
;
605 unsigned long flags
, orig_ret_address
= 0;
606 unsigned long trampoline_address
= (unsigned long)kretprobe_trampoline
;
608 INIT_HLIST_HEAD(&empty_rp
);
609 kretprobe_hash_lock(current
, &head
, &flags
);
612 * It is possible to have multiple instances associated with a given
613 * task either because an multiple functions in the call path
614 * have a return probe installed on them, and/or more than one return
615 * return probe was registered for a target function.
617 * We can handle this because:
618 * - instances are always inserted at the head of the list
619 * - when multiple return probes are registered for the same
620 * function, the first instance's ret_addr will point to the
621 * real return address, and all the rest will point to
622 * kretprobe_trampoline
624 hlist_for_each_entry_safe(ri
, tmp
, head
, hlist
) {
625 if (ri
->task
!= current
)
626 /* another task is sharing our hash bucket */
629 if (ri
->rp
&& ri
->rp
->handler
)
630 ri
->rp
->handler(ri
, regs
);
632 orig_ret_address
= (unsigned long)ri
->ret_addr
;
633 recycle_rp_inst(ri
, &empty_rp
);
635 if (orig_ret_address
!= trampoline_address
)
637 * This is the real return address. Any other
638 * instances associated with this task are for
639 * other calls deeper on the call stack
644 kretprobe_assert(ri
, orig_ret_address
, trampoline_address
);
645 instruction_pointer(regs
) = orig_ret_address
;
647 reset_current_kprobe();
648 kretprobe_hash_unlock(current
, &flags
);
649 preempt_enable_no_resched();
651 hlist_for_each_entry_safe(ri
, tmp
, &empty_rp
, hlist
) {
652 hlist_del(&ri
->hlist
);
656 * By returning a non-zero value, we are telling
657 * kprobe_handler() that we don't want the post_handler
658 * to run (and have re-enabled preemption)
663 int __kprobes
arch_trampoline_kprobe(struct kprobe
*p
)
665 if (p
->addr
== (kprobe_opcode_t
*)kretprobe_trampoline
)
671 static struct kprobe trampoline_p
= {
672 .addr
= (kprobe_opcode_t
*)kretprobe_trampoline
,
673 .pre_handler
= trampoline_probe_handler
676 int __init
arch_init_kprobes(void)
678 return register_kprobe(&trampoline_p
);