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/kdebug.h>
29 #include <linux/slab.h>
31 #include <asm/ptrace.h>
32 #include <asm/break.h>
35 static const union mips_instruction breakpoint_insn
= {
38 .code
= BRK_KPROBE_BP
,
43 static const union mips_instruction breakpoint2_insn
= {
46 .code
= BRK_KPROBE_SSTEPBP
,
51 DEFINE_PER_CPU(struct kprobe
*, current_kprobe
);
52 DEFINE_PER_CPU(struct kprobe_ctlblk
, kprobe_ctlblk
);
54 static int __kprobes
insn_has_delayslot(union mips_instruction insn
)
56 switch (insn
.i_format
.opcode
) {
59 * This group contains:
60 * jr and jalr are in r_format format.
63 switch (insn
.r_format
.func
) {
72 * This group contains:
73 * bltz_op, bgez_op, bltzl_op, bgezl_op,
74 * bltzal_op, bgezal_op, bltzall_op, bgezall_op.
79 * These are unconditional and in j_format.
85 * These are conditional and in i_format.
97 * These are the FPA/cp1 branch instructions.
101 #ifdef CONFIG_CPU_CAVIUM_OCTEON
102 case lwc2_op
: /* This is bbit0 on Octeon */
103 case ldc2_op
: /* This is bbit032 on Octeon */
104 case swc2_op
: /* This is bbit1 on Octeon */
105 case sdc2_op
: /* This is bbit132 on Octeon */
115 int __kprobes
arch_prepare_kprobe(struct kprobe
*p
)
117 union mips_instruction insn
;
118 union mips_instruction prev_insn
;
121 prev_insn
= p
->addr
[-1];
124 if (insn_has_delayslot(insn
) || insn_has_delayslot(prev_insn
)) {
125 pr_notice("Kprobes for branch and jump instructions are not supported\n");
130 /* insn: must be on special executable page on mips. */
131 p
->ainsn
.insn
= get_insn_slot();
132 if (!p
->ainsn
.insn
) {
138 * In the kprobe->ainsn.insn[] array we store the original
139 * instruction at index zero and a break trap instruction at
143 memcpy(&p
->ainsn
.insn
[0], p
->addr
, sizeof(kprobe_opcode_t
));
144 p
->ainsn
.insn
[1] = breakpoint2_insn
;
145 p
->opcode
= *p
->addr
;
151 void __kprobes
arch_arm_kprobe(struct kprobe
*p
)
153 *p
->addr
= breakpoint_insn
;
157 void __kprobes
arch_disarm_kprobe(struct kprobe
*p
)
159 *p
->addr
= p
->opcode
;
163 void __kprobes
arch_remove_kprobe(struct kprobe
*p
)
165 free_insn_slot(p
->ainsn
.insn
, 0);
168 static void save_previous_kprobe(struct kprobe_ctlblk
*kcb
)
170 kcb
->prev_kprobe
.kp
= kprobe_running();
171 kcb
->prev_kprobe
.status
= kcb
->kprobe_status
;
172 kcb
->prev_kprobe
.old_SR
= kcb
->kprobe_old_SR
;
173 kcb
->prev_kprobe
.saved_SR
= kcb
->kprobe_saved_SR
;
174 kcb
->prev_kprobe
.saved_epc
= kcb
->kprobe_saved_epc
;
177 static void restore_previous_kprobe(struct kprobe_ctlblk
*kcb
)
179 __get_cpu_var(current_kprobe
) = kcb
->prev_kprobe
.kp
;
180 kcb
->kprobe_status
= kcb
->prev_kprobe
.status
;
181 kcb
->kprobe_old_SR
= kcb
->prev_kprobe
.old_SR
;
182 kcb
->kprobe_saved_SR
= kcb
->prev_kprobe
.saved_SR
;
183 kcb
->kprobe_saved_epc
= kcb
->prev_kprobe
.saved_epc
;
186 static void set_current_kprobe(struct kprobe
*p
, struct pt_regs
*regs
,
187 struct kprobe_ctlblk
*kcb
)
189 __get_cpu_var(current_kprobe
) = p
;
190 kcb
->kprobe_saved_SR
= kcb
->kprobe_old_SR
= (regs
->cp0_status
& ST0_IE
);
191 kcb
->kprobe_saved_epc
= regs
->cp0_epc
;
194 static void prepare_singlestep(struct kprobe
*p
, struct pt_regs
*regs
)
196 regs
->cp0_status
&= ~ST0_IE
;
198 /* single step inline if the instruction is a break */
199 if (p
->opcode
.word
== breakpoint_insn
.word
||
200 p
->opcode
.word
== breakpoint2_insn
.word
)
201 regs
->cp0_epc
= (unsigned long)p
->addr
;
203 regs
->cp0_epc
= (unsigned long)&p
->ainsn
.insn
[0];
206 static int __kprobes
kprobe_handler(struct pt_regs
*regs
)
210 kprobe_opcode_t
*addr
;
211 struct kprobe_ctlblk
*kcb
;
213 addr
= (kprobe_opcode_t
*) regs
->cp0_epc
;
216 * We don't want to be preempted for the entire
217 * duration of kprobe processing
220 kcb
= get_kprobe_ctlblk();
222 /* Check we're not actually recursing */
223 if (kprobe_running()) {
224 p
= get_kprobe(addr
);
226 if (kcb
->kprobe_status
== KPROBE_HIT_SS
&&
227 p
->ainsn
.insn
->word
== breakpoint_insn
.word
) {
228 regs
->cp0_status
&= ~ST0_IE
;
229 regs
->cp0_status
|= kcb
->kprobe_saved_SR
;
233 * We have reentered the kprobe_handler(), since
234 * another probe was hit while within the handler.
235 * We here save the original kprobes variables and
236 * just single step on the instruction of the new probe
237 * without calling any user handlers.
239 save_previous_kprobe(kcb
);
240 set_current_kprobe(p
, regs
, kcb
);
241 kprobes_inc_nmissed_count(p
);
242 prepare_singlestep(p
, regs
);
243 kcb
->kprobe_status
= KPROBE_REENTER
;
246 if (addr
->word
!= breakpoint_insn
.word
) {
248 * The breakpoint instruction was removed by
249 * another cpu right after we hit, no further
250 * handling of this interrupt is appropriate
255 p
= __get_cpu_var(current_kprobe
);
256 if (p
->break_handler
&& p
->break_handler(p
, regs
))
262 p
= get_kprobe(addr
);
264 if (addr
->word
!= breakpoint_insn
.word
) {
266 * The breakpoint instruction was removed right
267 * after we hit it. Another cpu has removed
268 * either a probepoint or a debugger breakpoint
269 * at this address. In either case, no further
270 * handling of this interrupt is appropriate.
274 /* Not one of ours: let kernel handle it */
278 set_current_kprobe(p
, regs
, kcb
);
279 kcb
->kprobe_status
= KPROBE_HIT_ACTIVE
;
281 if (p
->pre_handler
&& p
->pre_handler(p
, regs
)) {
282 /* handler has already set things up, so skip ss setup */
287 prepare_singlestep(p
, regs
);
288 kcb
->kprobe_status
= KPROBE_HIT_SS
;
292 preempt_enable_no_resched();
298 * Called after single-stepping. p->addr is the address of the
299 * instruction whose first byte has been replaced by the "break 0"
300 * instruction. To avoid the SMP problems that can occur when we
301 * temporarily put back the original opcode to single-step, we
302 * single-stepped a copy of the instruction. The address of this
303 * copy is p->ainsn.insn.
305 * This function prepares to return from the post-single-step
308 static void __kprobes
resume_execution(struct kprobe
*p
,
309 struct pt_regs
*regs
,
310 struct kprobe_ctlblk
*kcb
)
312 unsigned long orig_epc
= kcb
->kprobe_saved_epc
;
313 regs
->cp0_epc
= orig_epc
+ 4;
316 static inline int post_kprobe_handler(struct pt_regs
*regs
)
318 struct kprobe
*cur
= kprobe_running();
319 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
324 if ((kcb
->kprobe_status
!= KPROBE_REENTER
) && cur
->post_handler
) {
325 kcb
->kprobe_status
= KPROBE_HIT_SSDONE
;
326 cur
->post_handler(cur
, regs
, 0);
329 resume_execution(cur
, regs
, kcb
);
331 regs
->cp0_status
|= kcb
->kprobe_saved_SR
;
333 /* Restore back the original saved kprobes variables and continue. */
334 if (kcb
->kprobe_status
== KPROBE_REENTER
) {
335 restore_previous_kprobe(kcb
);
338 reset_current_kprobe();
340 preempt_enable_no_resched();
345 static inline int kprobe_fault_handler(struct pt_regs
*regs
, int trapnr
)
347 struct kprobe
*cur
= kprobe_running();
348 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
350 if (cur
->fault_handler
&& cur
->fault_handler(cur
, regs
, trapnr
))
353 if (kcb
->kprobe_status
& KPROBE_HIT_SS
) {
354 resume_execution(cur
, regs
, kcb
);
355 regs
->cp0_status
|= kcb
->kprobe_old_SR
;
357 reset_current_kprobe();
358 preempt_enable_no_resched();
364 * Wrapper routine for handling exceptions.
366 int __kprobes
kprobe_exceptions_notify(struct notifier_block
*self
,
367 unsigned long val
, void *data
)
370 struct die_args
*args
= (struct die_args
*)data
;
371 int ret
= NOTIFY_DONE
;
375 if (kprobe_handler(args
->regs
))
379 if (post_kprobe_handler(args
->regs
))
384 /* kprobe_running() needs smp_processor_id() */
388 && kprobe_fault_handler(args
->regs
, args
->trapnr
))
398 int __kprobes
setjmp_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
400 struct jprobe
*jp
= container_of(p
, struct jprobe
, kp
);
401 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
403 kcb
->jprobe_saved_regs
= *regs
;
404 kcb
->jprobe_saved_sp
= regs
->regs
[29];
406 memcpy(kcb
->jprobes_stack
, (void *)kcb
->jprobe_saved_sp
,
407 MIN_JPROBES_STACK_SIZE(kcb
->jprobe_saved_sp
));
409 regs
->cp0_epc
= (unsigned long)(jp
->entry
);
414 /* Defined in the inline asm below. */
415 void jprobe_return_end(void);
417 void __kprobes
jprobe_return(void)
419 /* Assembler quirk necessitates this '0,code' business. */
422 ".globl jprobe_return_end\n"
423 "jprobe_return_end:\n"
424 : : "n" (BRK_KPROBE_BP
) : "memory");
427 int __kprobes
longjmp_break_handler(struct kprobe
*p
, struct pt_regs
*regs
)
429 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
431 if (regs
->cp0_epc
>= (unsigned long)jprobe_return
&&
432 regs
->cp0_epc
<= (unsigned long)jprobe_return_end
) {
433 *regs
= kcb
->jprobe_saved_regs
;
434 memcpy((void *)kcb
->jprobe_saved_sp
, kcb
->jprobes_stack
,
435 MIN_JPROBES_STACK_SIZE(kcb
->jprobe_saved_sp
));
436 preempt_enable_no_resched();
444 * Function return probe trampoline:
445 * - init_kprobes() establishes a probepoint here
446 * - When the probed function returns, this probe causes the
449 static void __used
kretprobe_trampoline_holder(void)
453 /* Keep the assembler from reordering and placing JR here. */
456 ".global kretprobe_trampoline\n"
457 "kretprobe_trampoline:\n\t"
463 void kretprobe_trampoline(void);
465 void __kprobes
arch_prepare_kretprobe(struct kretprobe_instance
*ri
,
466 struct pt_regs
*regs
)
468 ri
->ret_addr
= (kprobe_opcode_t
*) regs
->regs
[31];
470 /* Replace the return addr with trampoline addr */
471 regs
->regs
[31] = (unsigned long)kretprobe_trampoline
;
475 * Called when the probe at kretprobe trampoline is hit
477 static int __kprobes
trampoline_probe_handler(struct kprobe
*p
,
478 struct pt_regs
*regs
)
480 struct kretprobe_instance
*ri
= NULL
;
481 struct hlist_head
*head
, empty_rp
;
482 struct hlist_node
*node
, *tmp
;
483 unsigned long flags
, orig_ret_address
= 0;
484 unsigned long trampoline_address
= (unsigned long)kretprobe_trampoline
;
486 INIT_HLIST_HEAD(&empty_rp
);
487 kretprobe_hash_lock(current
, &head
, &flags
);
490 * It is possible to have multiple instances associated with a given
491 * task either because an multiple functions in the call path
492 * have a return probe installed on them, and/or more than one return
493 * return probe was registered for a target function.
495 * We can handle this because:
496 * - instances are always inserted at the head of the list
497 * - when multiple return probes are registered for the same
498 * function, the first instance's ret_addr will point to the
499 * real return address, and all the rest will point to
500 * kretprobe_trampoline
502 hlist_for_each_entry_safe(ri
, node
, tmp
, head
, hlist
) {
503 if (ri
->task
!= current
)
504 /* another task is sharing our hash bucket */
507 if (ri
->rp
&& ri
->rp
->handler
)
508 ri
->rp
->handler(ri
, regs
);
510 orig_ret_address
= (unsigned long)ri
->ret_addr
;
511 recycle_rp_inst(ri
, &empty_rp
);
513 if (orig_ret_address
!= trampoline_address
)
515 * This is the real return address. Any other
516 * instances associated with this task are for
517 * other calls deeper on the call stack
522 kretprobe_assert(ri
, orig_ret_address
, trampoline_address
);
523 instruction_pointer(regs
) = orig_ret_address
;
525 reset_current_kprobe();
526 kretprobe_hash_unlock(current
, &flags
);
527 preempt_enable_no_resched();
529 hlist_for_each_entry_safe(ri
, node
, tmp
, &empty_rp
, hlist
) {
530 hlist_del(&ri
->hlist
);
534 * By returning a non-zero value, we are telling
535 * kprobe_handler() that we don't want the post_handler
536 * to run (and have re-enabled preemption)
541 int __kprobes
arch_trampoline_kprobe(struct kprobe
*p
)
543 if (p
->addr
== (kprobe_opcode_t
*)kretprobe_trampoline
)
549 static struct kprobe trampoline_p
= {
550 .addr
= (kprobe_opcode_t
*)kretprobe_trampoline
,
551 .pre_handler
= trampoline_probe_handler
554 int __init
arch_init_kprobes(void)
556 return register_kprobe(&trampoline_p
);