1
2
3
4
5
6#include <linux/kernel.h>
7#include <linux/kprobes.h>
8#include <linux/extable.h>
9#include <linux/kdebug.h>
10#include <linux/slab.h>
11#include <linux/context_tracking.h>
12#include <asm/signal.h>
13#include <asm/cacheflush.h>
14#include <linux/uaccess.h>
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
45DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
46
47struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}};
48
49int __kprobes arch_prepare_kprobe(struct kprobe *p)
50{
51 if ((unsigned long) p->addr & 0x3UL)
52 return -EILSEQ;
53
54 p->ainsn.insn[0] = *p->addr;
55 flushi(&p->ainsn.insn[0]);
56
57 p->ainsn.insn[1] = BREAKPOINT_INSTRUCTION_2;
58 flushi(&p->ainsn.insn[1]);
59
60 p->opcode = *p->addr;
61 return 0;
62}
63
64void __kprobes arch_arm_kprobe(struct kprobe *p)
65{
66 *p->addr = BREAKPOINT_INSTRUCTION;
67 flushi(p->addr);
68}
69
70void __kprobes arch_disarm_kprobe(struct kprobe *p)
71{
72 *p->addr = p->opcode;
73 flushi(p->addr);
74}
75
76static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
77{
78 kcb->prev_kprobe.kp = kprobe_running();
79 kcb->prev_kprobe.status = kcb->kprobe_status;
80 kcb->prev_kprobe.orig_tnpc = kcb->kprobe_orig_tnpc;
81 kcb->prev_kprobe.orig_tstate_pil = kcb->kprobe_orig_tstate_pil;
82}
83
84static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
85{
86 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
87 kcb->kprobe_status = kcb->prev_kprobe.status;
88 kcb->kprobe_orig_tnpc = kcb->prev_kprobe.orig_tnpc;
89 kcb->kprobe_orig_tstate_pil = kcb->prev_kprobe.orig_tstate_pil;
90}
91
92static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
93 struct kprobe_ctlblk *kcb)
94{
95 __this_cpu_write(current_kprobe, p);
96 kcb->kprobe_orig_tnpc = regs->tnpc;
97 kcb->kprobe_orig_tstate_pil = (regs->tstate & TSTATE_PIL);
98}
99
100static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs,
101 struct kprobe_ctlblk *kcb)
102{
103 regs->tstate |= TSTATE_PIL;
104
105
106 if (p->opcode == BREAKPOINT_INSTRUCTION) {
107 regs->tpc = (unsigned long) p->addr;
108 regs->tnpc = kcb->kprobe_orig_tnpc;
109 } else {
110 regs->tpc = (unsigned long) &p->ainsn.insn[0];
111 regs->tnpc = (unsigned long) &p->ainsn.insn[1];
112 }
113}
114
115static int __kprobes kprobe_handler(struct pt_regs *regs)
116{
117 struct kprobe *p;
118 void *addr = (void *) regs->tpc;
119 int ret = 0;
120 struct kprobe_ctlblk *kcb;
121
122
123
124
125
126 preempt_disable();
127 kcb = get_kprobe_ctlblk();
128
129 if (kprobe_running()) {
130 p = get_kprobe(addr);
131 if (p) {
132 if (kcb->kprobe_status == KPROBE_HIT_SS) {
133 regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
134 kcb->kprobe_orig_tstate_pil);
135 goto no_kprobe;
136 }
137
138
139
140
141
142
143 save_previous_kprobe(kcb);
144 set_current_kprobe(p, regs, kcb);
145 kprobes_inc_nmissed_count(p);
146 kcb->kprobe_status = KPROBE_REENTER;
147 prepare_singlestep(p, regs, kcb);
148 return 1;
149 } else {
150 if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) {
151
152
153
154
155 ret = 1;
156 goto no_kprobe;
157 }
158 p = __this_cpu_read(current_kprobe);
159 if (p->break_handler && p->break_handler(p, regs))
160 goto ss_probe;
161 }
162 goto no_kprobe;
163 }
164
165 p = get_kprobe(addr);
166 if (!p) {
167 if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) {
168
169
170
171
172
173
174
175 ret = 1;
176 }
177
178 goto no_kprobe;
179 }
180
181 set_current_kprobe(p, regs, kcb);
182 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
183 if (p->pre_handler && p->pre_handler(p, regs))
184 return 1;
185
186ss_probe:
187 prepare_singlestep(p, regs, kcb);
188 kcb->kprobe_status = KPROBE_HIT_SS;
189 return 1;
190
191no_kprobe:
192 preempt_enable_no_resched();
193 return ret;
194}
195
196
197
198
199
200
201
202
203
204static unsigned long __kprobes relbranch_fixup(u32 insn, struct kprobe *p,
205 struct pt_regs *regs)
206{
207 unsigned long real_pc = (unsigned long) p->addr;
208
209
210 if (regs->tnpc == regs->tpc + 0x4UL)
211 return real_pc + 0x8UL;
212
213
214
215
216 if ((insn & 0xc0000000) == 0x40000000 ||
217 (insn & 0xc1c00000) == 0x00400000 ||
218 (insn & 0xc1c00000) == 0x00800000) {
219 unsigned long ainsn_addr;
220
221 ainsn_addr = (unsigned long) &p->ainsn.insn[0];
222
223
224
225
226
227 return (real_pc + (regs->tnpc - ainsn_addr));
228 }
229
230
231
232
233 return regs->tnpc;
234}
235
236
237
238
239static void __kprobes retpc_fixup(struct pt_regs *regs, u32 insn,
240 unsigned long real_pc)
241{
242 unsigned long *slot = NULL;
243
244
245 if ((insn & 0xc0000000) == 0x40000000) {
246 slot = ®s->u_regs[UREG_I7];
247 }
248
249
250 if ((insn & 0xc1f80000) == 0x81c00000) {
251 unsigned long rd = ((insn >> 25) & 0x1f);
252
253 if (rd <= 15) {
254 slot = ®s->u_regs[rd];
255 } else {
256
257 flushw_all();
258
259 rd -= 16;
260 slot = (unsigned long *)
261 (regs->u_regs[UREG_FP] + STACK_BIAS);
262 slot += rd;
263 }
264 }
265 if (slot != NULL)
266 *slot = real_pc;
267}
268
269
270
271
272
273
274
275
276
277
278
279
280static void __kprobes resume_execution(struct kprobe *p,
281 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
282{
283 u32 insn = p->ainsn.insn[0];
284
285 regs->tnpc = relbranch_fixup(insn, p, regs);
286
287
288 regs->tpc = kcb->kprobe_orig_tnpc;
289
290 retpc_fixup(regs, insn, (unsigned long) p->addr);
291
292 regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
293 kcb->kprobe_orig_tstate_pil);
294}
295
296static int __kprobes post_kprobe_handler(struct pt_regs *regs)
297{
298 struct kprobe *cur = kprobe_running();
299 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
300
301 if (!cur)
302 return 0;
303
304 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
305 kcb->kprobe_status = KPROBE_HIT_SSDONE;
306 cur->post_handler(cur, regs, 0);
307 }
308
309 resume_execution(cur, regs, kcb);
310
311
312 if (kcb->kprobe_status == KPROBE_REENTER) {
313 restore_previous_kprobe(kcb);
314 goto out;
315 }
316 reset_current_kprobe();
317out:
318 preempt_enable_no_resched();
319
320 return 1;
321}
322
323int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
324{
325 struct kprobe *cur = kprobe_running();
326 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
327 const struct exception_table_entry *entry;
328
329 switch(kcb->kprobe_status) {
330 case KPROBE_HIT_SS:
331 case KPROBE_REENTER:
332
333
334
335
336
337
338
339 regs->tpc = (unsigned long)cur->addr;
340 regs->tnpc = kcb->kprobe_orig_tnpc;
341 regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
342 kcb->kprobe_orig_tstate_pil);
343 if (kcb->kprobe_status == KPROBE_REENTER)
344 restore_previous_kprobe(kcb);
345 else
346 reset_current_kprobe();
347 preempt_enable_no_resched();
348 break;
349 case KPROBE_HIT_ACTIVE:
350 case KPROBE_HIT_SSDONE:
351
352
353
354
355
356 kprobes_inc_nmissed_count(cur);
357
358
359
360
361
362
363
364
365 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
366 return 1;
367
368
369
370
371
372
373 entry = search_exception_tables(regs->tpc);
374 if (entry) {
375 regs->tpc = entry->fixup;
376 regs->tnpc = regs->tpc + 4;
377 return 1;
378 }
379
380
381
382
383
384 break;
385 default:
386 break;
387 }
388
389 return 0;
390}
391
392
393
394
395int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
396 unsigned long val, void *data)
397{
398 struct die_args *args = (struct die_args *)data;
399 int ret = NOTIFY_DONE;
400
401 if (args->regs && user_mode(args->regs))
402 return ret;
403
404 switch (val) {
405 case DIE_DEBUG:
406 if (kprobe_handler(args->regs))
407 ret = NOTIFY_STOP;
408 break;
409 case DIE_DEBUG_2:
410 if (post_kprobe_handler(args->regs))
411 ret = NOTIFY_STOP;
412 break;
413 default:
414 break;
415 }
416 return ret;
417}
418
419asmlinkage void __kprobes kprobe_trap(unsigned long trap_level,
420 struct pt_regs *regs)
421{
422 enum ctx_state prev_state = exception_enter();
423
424 BUG_ON(trap_level != 0x170 && trap_level != 0x171);
425
426 if (user_mode(regs)) {
427 local_irq_enable();
428 bad_trap(regs, trap_level);
429 goto out;
430 }
431
432
433
434
435 if (notify_die((trap_level == 0x170) ? DIE_DEBUG : DIE_DEBUG_2,
436 (trap_level == 0x170) ? "debug" : "debug_2",
437 regs, 0, trap_level, SIGTRAP) != NOTIFY_STOP)
438 bad_trap(regs, trap_level);
439out:
440 exception_exit(prev_state);
441}
442
443
444int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
445{
446 struct jprobe *jp = container_of(p, struct jprobe, kp);
447 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
448
449 memcpy(&(kcb->jprobe_saved_regs), regs, sizeof(*regs));
450
451 regs->tpc = (unsigned long) jp->entry;
452 regs->tnpc = ((unsigned long) jp->entry) + 0x4UL;
453 regs->tstate |= TSTATE_PIL;
454
455 return 1;
456}
457
458void __kprobes jprobe_return(void)
459{
460 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
461 register unsigned long orig_fp asm("g1");
462
463 orig_fp = kcb->jprobe_saved_regs.u_regs[UREG_FP];
464 __asm__ __volatile__("\n"
465"1: cmp %%sp, %0\n\t"
466 "blu,a,pt %%xcc, 1b\n\t"
467 " restore\n\t"
468 ".globl jprobe_return_trap_instruction\n"
469"jprobe_return_trap_instruction:\n\t"
470 "ta 0x70"
471 :
472 : "r" (orig_fp));
473}
474
475extern void jprobe_return_trap_instruction(void);
476
477int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
478{
479 u32 *addr = (u32 *) regs->tpc;
480 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
481
482 if (addr == (u32 *) jprobe_return_trap_instruction) {
483 memcpy(regs, &(kcb->jprobe_saved_regs), sizeof(*regs));
484 preempt_enable_no_resched();
485 return 1;
486 }
487 return 0;
488}
489
490
491
492
493
494
495
496
497
498
499
500
501
502void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
503 struct pt_regs *regs)
504{
505 ri->ret_addr = (kprobe_opcode_t *)(regs->u_regs[UREG_RETPC] + 8);
506
507
508 regs->u_regs[UREG_RETPC] =
509 ((unsigned long)kretprobe_trampoline) - 8;
510}
511
512
513
514
515static int __kprobes trampoline_probe_handler(struct kprobe *p,
516 struct pt_regs *regs)
517{
518 struct kretprobe_instance *ri = NULL;
519 struct hlist_head *head, empty_rp;
520 struct hlist_node *tmp;
521 unsigned long flags, orig_ret_address = 0;
522 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
523
524 INIT_HLIST_HEAD(&empty_rp);
525 kretprobe_hash_lock(current, &head, &flags);
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
541 if (ri->task != current)
542
543 continue;
544
545 if (ri->rp && ri->rp->handler)
546 ri->rp->handler(ri, regs);
547
548 orig_ret_address = (unsigned long)ri->ret_addr;
549 recycle_rp_inst(ri, &empty_rp);
550
551 if (orig_ret_address != trampoline_address)
552
553
554
555
556
557 break;
558 }
559
560 kretprobe_assert(ri, orig_ret_address, trampoline_address);
561 regs->tpc = orig_ret_address;
562 regs->tnpc = orig_ret_address + 4;
563
564 reset_current_kprobe();
565 kretprobe_hash_unlock(current, &flags);
566 preempt_enable_no_resched();
567
568 hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
569 hlist_del(&ri->hlist);
570 kfree(ri);
571 }
572
573
574
575
576
577 return 1;
578}
579
580static void __used kretprobe_trampoline_holder(void)
581{
582 asm volatile(".global kretprobe_trampoline\n"
583 "kretprobe_trampoline:\n"
584 "\tnop\n"
585 "\tnop\n");
586}
587static struct kprobe trampoline_p = {
588 .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
589 .pre_handler = trampoline_probe_handler
590};
591
592int __init arch_init_kprobes(void)
593{
594 return register_kprobe(&trampoline_p);
595}
596
597int __kprobes arch_trampoline_kprobe(struct kprobe *p)
598{
599 if (p->addr == (kprobe_opcode_t *)&kretprobe_trampoline)
600 return 1;
601
602 return 0;
603}
604