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