blob: 0ab1a5ff1049606eee55d3eba3a07f9cf214bcd8 [file] [log] [blame]
David Daneyc1bf2072010-08-03 11:22:20 -07001/*
2 * Kernel Probes (KProbes)
3 * arch/mips/kernel/kprobes.c
4 *
5 * Copyright 2006 Sony Corp.
6 * Copyright 2010 Cavium Networks
7 *
8 * Some portions copied from the powerpc version.
9 *
10 * Copyright (C) IBM Corporation, 2002, 2004
11 *
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.
15 *
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.
20 *
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
24 */
25
26#include <linux/kprobes.h>
27#include <linux/preempt.h>
Maneesh Soni41dde782011-11-08 17:04:54 +053028#include <linux/uaccess.h>
David Daneyc1bf2072010-08-03 11:22:20 -070029#include <linux/kdebug.h>
30#include <linux/slab.h>
31
32#include <asm/ptrace.h>
33#include <asm/break.h>
34#include <asm/inst.h>
35
36static const union mips_instruction breakpoint_insn = {
37 .b_format = {
38 .opcode = spec_op,
39 .code = BRK_KPROBE_BP,
40 .func = break_op
41 }
42};
43
44static const union mips_instruction breakpoint2_insn = {
45 .b_format = {
46 .opcode = spec_op,
47 .code = BRK_KPROBE_SSTEPBP,
48 .func = break_op
49 }
50};
51
52DEFINE_PER_CPU(struct kprobe *, current_kprobe);
53DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
54
55static int __kprobes insn_has_delayslot(union mips_instruction insn)
56{
57 switch (insn.i_format.opcode) {
58
59 /*
60 * This group contains:
61 * jr and jalr are in r_format format.
62 */
63 case spec_op:
64 switch (insn.r_format.func) {
65 case jr_op:
66 case jalr_op:
67 break;
68 default:
69 goto insn_ok;
70 }
71
72 /*
73 * This group contains:
74 * bltz_op, bgez_op, bltzl_op, bgezl_op,
75 * bltzal_op, bgezal_op, bltzall_op, bgezall_op.
76 */
77 case bcond_op:
78
79 /*
80 * These are unconditional and in j_format.
81 */
82 case jal_op:
83 case j_op:
84
85 /*
86 * These are conditional and in i_format.
87 */
88 case beq_op:
89 case beql_op:
90 case bne_op:
91 case bnel_op:
92 case blez_op:
93 case blezl_op:
94 case bgtz_op:
95 case bgtzl_op:
96
97 /*
98 * These are the FPA/cp1 branch instructions.
99 */
100 case cop1_op:
101
102#ifdef CONFIG_CPU_CAVIUM_OCTEON
103 case lwc2_op: /* This is bbit0 on Octeon */
104 case ldc2_op: /* This is bbit032 on Octeon */
105 case swc2_op: /* This is bbit1 on Octeon */
106 case sdc2_op: /* This is bbit132 on Octeon */
107#endif
108 return 1;
109 default:
110 break;
111 }
112insn_ok:
113 return 0;
114}
115
Maneesh Soni9233c1e2011-11-08 17:05:35 +0530116/*
117 * insn_has_ll_or_sc function checks whether instruction is ll or sc
118 * one; putting breakpoint on top of atomic ll/sc pair is bad idea;
119 * so we need to prevent it and refuse kprobes insertion for such
120 * instructions; cannot do much about breakpoint in the middle of
121 * ll/sc pair; it is upto user to avoid those places
122 */
123static int __kprobes insn_has_ll_or_sc(union mips_instruction insn)
124{
125 int ret = 0;
126
127 switch (insn.i_format.opcode) {
128 case ll_op:
129 case lld_op:
130 case sc_op:
131 case scd_op:
132 ret = 1;
133 break;
134 default:
135 break;
136 }
137 return ret;
138}
139
David Daneyc1bf2072010-08-03 11:22:20 -0700140int __kprobes arch_prepare_kprobe(struct kprobe *p)
141{
142 union mips_instruction insn;
143 union mips_instruction prev_insn;
144 int ret = 0;
145
David Daneyc1bf2072010-08-03 11:22:20 -0700146 insn = p->addr[0];
147
Maneesh Soni9233c1e2011-11-08 17:05:35 +0530148 if (insn_has_ll_or_sc(insn)) {
149 pr_notice("Kprobes for ll and sc instructions are not"
150 "supported\n");
151 ret = -EINVAL;
152 goto out;
153 }
154
Maneesh Soni41dde782011-11-08 17:04:54 +0530155 if (insn_has_delayslot(insn)) {
156 pr_notice("Kprobes for branch and jump instructions are not"
157 "supported\n");
158 ret = -EINVAL;
159 goto out;
160 }
161
162 if ((probe_kernel_read(&prev_insn, p->addr - 1,
163 sizeof(mips_instruction)) == 0) &&
164 insn_has_delayslot(prev_insn)) {
165 pr_notice("Kprobes for branch delayslot are not supported\n");
David Daneyc1bf2072010-08-03 11:22:20 -0700166 ret = -EINVAL;
167 goto out;
168 }
169
170 /* insn: must be on special executable page on mips. */
171 p->ainsn.insn = get_insn_slot();
172 if (!p->ainsn.insn) {
173 ret = -ENOMEM;
174 goto out;
175 }
176
177 /*
178 * In the kprobe->ainsn.insn[] array we store the original
179 * instruction at index zero and a break trap instruction at
180 * index one.
181 */
182
183 memcpy(&p->ainsn.insn[0], p->addr, sizeof(kprobe_opcode_t));
184 p->ainsn.insn[1] = breakpoint2_insn;
185 p->opcode = *p->addr;
186
187out:
188 return ret;
189}
190
191void __kprobes arch_arm_kprobe(struct kprobe *p)
192{
193 *p->addr = breakpoint_insn;
194 flush_insn_slot(p);
195}
196
197void __kprobes arch_disarm_kprobe(struct kprobe *p)
198{
199 *p->addr = p->opcode;
200 flush_insn_slot(p);
201}
202
203void __kprobes arch_remove_kprobe(struct kprobe *p)
204{
205 free_insn_slot(p->ainsn.insn, 0);
206}
207
208static void save_previous_kprobe(struct kprobe_ctlblk *kcb)
209{
210 kcb->prev_kprobe.kp = kprobe_running();
211 kcb->prev_kprobe.status = kcb->kprobe_status;
212 kcb->prev_kprobe.old_SR = kcb->kprobe_old_SR;
213 kcb->prev_kprobe.saved_SR = kcb->kprobe_saved_SR;
214 kcb->prev_kprobe.saved_epc = kcb->kprobe_saved_epc;
215}
216
217static void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
218{
219 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
220 kcb->kprobe_status = kcb->prev_kprobe.status;
221 kcb->kprobe_old_SR = kcb->prev_kprobe.old_SR;
222 kcb->kprobe_saved_SR = kcb->prev_kprobe.saved_SR;
223 kcb->kprobe_saved_epc = kcb->prev_kprobe.saved_epc;
224}
225
226static void set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
227 struct kprobe_ctlblk *kcb)
228{
229 __get_cpu_var(current_kprobe) = p;
230 kcb->kprobe_saved_SR = kcb->kprobe_old_SR = (regs->cp0_status & ST0_IE);
231 kcb->kprobe_saved_epc = regs->cp0_epc;
232}
233
234static void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
235{
236 regs->cp0_status &= ~ST0_IE;
237
238 /* single step inline if the instruction is a break */
239 if (p->opcode.word == breakpoint_insn.word ||
240 p->opcode.word == breakpoint2_insn.word)
241 regs->cp0_epc = (unsigned long)p->addr;
242 else
243 regs->cp0_epc = (unsigned long)&p->ainsn.insn[0];
244}
245
246static int __kprobes kprobe_handler(struct pt_regs *regs)
247{
248 struct kprobe *p;
249 int ret = 0;
250 kprobe_opcode_t *addr;
251 struct kprobe_ctlblk *kcb;
252
253 addr = (kprobe_opcode_t *) regs->cp0_epc;
254
255 /*
256 * We don't want to be preempted for the entire
257 * duration of kprobe processing
258 */
259 preempt_disable();
260 kcb = get_kprobe_ctlblk();
261
262 /* Check we're not actually recursing */
263 if (kprobe_running()) {
264 p = get_kprobe(addr);
265 if (p) {
266 if (kcb->kprobe_status == KPROBE_HIT_SS &&
267 p->ainsn.insn->word == breakpoint_insn.word) {
268 regs->cp0_status &= ~ST0_IE;
269 regs->cp0_status |= kcb->kprobe_saved_SR;
270 goto no_kprobe;
271 }
272 /*
273 * We have reentered the kprobe_handler(), since
274 * another probe was hit while within the handler.
275 * We here save the original kprobes variables and
276 * just single step on the instruction of the new probe
277 * without calling any user handlers.
278 */
279 save_previous_kprobe(kcb);
280 set_current_kprobe(p, regs, kcb);
281 kprobes_inc_nmissed_count(p);
282 prepare_singlestep(p, regs);
283 kcb->kprobe_status = KPROBE_REENTER;
284 return 1;
285 } else {
286 if (addr->word != breakpoint_insn.word) {
287 /*
288 * The breakpoint instruction was removed by
289 * another cpu right after we hit, no further
290 * handling of this interrupt is appropriate
291 */
292 ret = 1;
293 goto no_kprobe;
294 }
295 p = __get_cpu_var(current_kprobe);
296 if (p->break_handler && p->break_handler(p, regs))
297 goto ss_probe;
298 }
299 goto no_kprobe;
300 }
301
302 p = get_kprobe(addr);
303 if (!p) {
304 if (addr->word != breakpoint_insn.word) {
305 /*
306 * The breakpoint instruction was removed right
307 * after we hit it. Another cpu has removed
308 * either a probepoint or a debugger breakpoint
309 * at this address. In either case, no further
310 * handling of this interrupt is appropriate.
311 */
312 ret = 1;
313 }
314 /* Not one of ours: let kernel handle it */
315 goto no_kprobe;
316 }
317
318 set_current_kprobe(p, regs, kcb);
319 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
320
321 if (p->pre_handler && p->pre_handler(p, regs)) {
322 /* handler has already set things up, so skip ss setup */
323 return 1;
324 }
325
326ss_probe:
327 prepare_singlestep(p, regs);
328 kcb->kprobe_status = KPROBE_HIT_SS;
329 return 1;
330
331no_kprobe:
332 preempt_enable_no_resched();
333 return ret;
334
335}
336
337/*
338 * Called after single-stepping. p->addr is the address of the
339 * instruction whose first byte has been replaced by the "break 0"
340 * instruction. To avoid the SMP problems that can occur when we
341 * temporarily put back the original opcode to single-step, we
342 * single-stepped a copy of the instruction. The address of this
343 * copy is p->ainsn.insn.
344 *
345 * This function prepares to return from the post-single-step
346 * breakpoint trap.
347 */
348static void __kprobes resume_execution(struct kprobe *p,
349 struct pt_regs *regs,
350 struct kprobe_ctlblk *kcb)
351{
352 unsigned long orig_epc = kcb->kprobe_saved_epc;
353 regs->cp0_epc = orig_epc + 4;
354}
355
356static inline int post_kprobe_handler(struct pt_regs *regs)
357{
358 struct kprobe *cur = kprobe_running();
359 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
360
361 if (!cur)
362 return 0;
363
364 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
365 kcb->kprobe_status = KPROBE_HIT_SSDONE;
366 cur->post_handler(cur, regs, 0);
367 }
368
369 resume_execution(cur, regs, kcb);
370
371 regs->cp0_status |= kcb->kprobe_saved_SR;
372
373 /* Restore back the original saved kprobes variables and continue. */
374 if (kcb->kprobe_status == KPROBE_REENTER) {
375 restore_previous_kprobe(kcb);
376 goto out;
377 }
378 reset_current_kprobe();
379out:
380 preempt_enable_no_resched();
381
382 return 1;
383}
384
385static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
386{
387 struct kprobe *cur = kprobe_running();
388 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
389
390 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
391 return 1;
392
393 if (kcb->kprobe_status & KPROBE_HIT_SS) {
394 resume_execution(cur, regs, kcb);
395 regs->cp0_status |= kcb->kprobe_old_SR;
396
397 reset_current_kprobe();
398 preempt_enable_no_resched();
399 }
400 return 0;
401}
402
403/*
404 * Wrapper routine for handling exceptions.
405 */
406int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
407 unsigned long val, void *data)
408{
409
410 struct die_args *args = (struct die_args *)data;
411 int ret = NOTIFY_DONE;
412
413 switch (val) {
414 case DIE_BREAK:
415 if (kprobe_handler(args->regs))
416 ret = NOTIFY_STOP;
417 break;
418 case DIE_SSTEPBP:
419 if (post_kprobe_handler(args->regs))
420 ret = NOTIFY_STOP;
421 break;
422
423 case DIE_PAGE_FAULT:
424 /* kprobe_running() needs smp_processor_id() */
425 preempt_disable();
426
427 if (kprobe_running()
428 && kprobe_fault_handler(args->regs, args->trapnr))
429 ret = NOTIFY_STOP;
430 preempt_enable();
431 break;
432 default:
433 break;
434 }
435 return ret;
436}
437
438int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
439{
440 struct jprobe *jp = container_of(p, struct jprobe, kp);
441 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
442
443 kcb->jprobe_saved_regs = *regs;
444 kcb->jprobe_saved_sp = regs->regs[29];
445
446 memcpy(kcb->jprobes_stack, (void *)kcb->jprobe_saved_sp,
447 MIN_JPROBES_STACK_SIZE(kcb->jprobe_saved_sp));
448
449 regs->cp0_epc = (unsigned long)(jp->entry);
450
451 return 1;
452}
453
454/* Defined in the inline asm below. */
455void jprobe_return_end(void);
456
457void __kprobes jprobe_return(void)
458{
459 /* Assembler quirk necessitates this '0,code' business. */
460 asm volatile(
461 "break 0,%0\n\t"
462 ".globl jprobe_return_end\n"
463 "jprobe_return_end:\n"
464 : : "n" (BRK_KPROBE_BP) : "memory");
465}
466
467int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
468{
469 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
470
471 if (regs->cp0_epc >= (unsigned long)jprobe_return &&
472 regs->cp0_epc <= (unsigned long)jprobe_return_end) {
473 *regs = kcb->jprobe_saved_regs;
474 memcpy((void *)kcb->jprobe_saved_sp, kcb->jprobes_stack,
475 MIN_JPROBES_STACK_SIZE(kcb->jprobe_saved_sp));
476 preempt_enable_no_resched();
477
478 return 1;
479 }
480 return 0;
481}
482
483/*
484 * Function return probe trampoline:
485 * - init_kprobes() establishes a probepoint here
486 * - When the probed function returns, this probe causes the
487 * handlers to fire
488 */
489static void __used kretprobe_trampoline_holder(void)
490{
491 asm volatile(
492 ".set push\n\t"
493 /* Keep the assembler from reordering and placing JR here. */
494 ".set noreorder\n\t"
495 "nop\n\t"
496 ".global kretprobe_trampoline\n"
497 "kretprobe_trampoline:\n\t"
498 "nop\n\t"
499 ".set pop"
500 : : : "memory");
501}
502
503void kretprobe_trampoline(void);
504
505void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
506 struct pt_regs *regs)
507{
508 ri->ret_addr = (kprobe_opcode_t *) regs->regs[31];
509
510 /* Replace the return addr with trampoline addr */
511 regs->regs[31] = (unsigned long)kretprobe_trampoline;
512}
513
514/*
515 * Called when the probe at kretprobe trampoline is hit
516 */
517static int __kprobes trampoline_probe_handler(struct kprobe *p,
518 struct pt_regs *regs)
519{
520 struct kretprobe_instance *ri = NULL;
521 struct hlist_head *head, empty_rp;
522 struct hlist_node *node, *tmp;
523 unsigned long flags, orig_ret_address = 0;
524 unsigned long trampoline_address = (unsigned long)kretprobe_trampoline;
525
526 INIT_HLIST_HEAD(&empty_rp);
527 kretprobe_hash_lock(current, &head, &flags);
528
529 /*
530 * It is possible to have multiple instances associated with a given
531 * task either because an multiple functions in the call path
532 * have a return probe installed on them, and/or more than one return
533 * return probe was registered for a target function.
534 *
535 * We can handle this because:
536 * - instances are always inserted at the head of the list
537 * - when multiple return probes are registered for the same
538 * function, the first instance's ret_addr will point to the
539 * real return address, and all the rest will point to
540 * kretprobe_trampoline
541 */
542 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
543 if (ri->task != current)
544 /* another task is sharing our hash bucket */
545 continue;
546
547 if (ri->rp && ri->rp->handler)
548 ri->rp->handler(ri, regs);
549
550 orig_ret_address = (unsigned long)ri->ret_addr;
551 recycle_rp_inst(ri, &empty_rp);
552
553 if (orig_ret_address != trampoline_address)
554 /*
555 * This is the real return address. Any other
556 * instances associated with this task are for
557 * other calls deeper on the call stack
558 */
559 break;
560 }
561
562 kretprobe_assert(ri, orig_ret_address, trampoline_address);
563 instruction_pointer(regs) = orig_ret_address;
564
565 reset_current_kprobe();
566 kretprobe_hash_unlock(current, &flags);
567 preempt_enable_no_resched();
568
569 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
570 hlist_del(&ri->hlist);
571 kfree(ri);
572 }
573 /*
574 * By returning a non-zero value, we are telling
575 * kprobe_handler() that we don't want the post_handler
576 * to run (and have re-enabled preemption)
577 */
578 return 1;
579}
580
581int __kprobes arch_trampoline_kprobe(struct kprobe *p)
582{
583 if (p->addr == (kprobe_opcode_t *)kretprobe_trampoline)
584 return 1;
585
586 return 0;
587}
588
589static struct kprobe trampoline_p = {
590 .addr = (kprobe_opcode_t *)kretprobe_trampoline,
591 .pre_handler = trampoline_probe_handler
592};
593
594int __init arch_init_kprobes(void)
595{
596 return register_kprobe(&trampoline_p);
597}