]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - arch/arm64/kernel/probes/kprobes.c
ASoC: sti: fix missing clk_disable_unprepare() on error in uni_player_start()
[mirror_ubuntu-zesty-kernel.git] / arch / arm64 / kernel / probes / kprobes.c
1 /*
2 * arch/arm64/kernel/probes/kprobes.c
3 *
4 * Kprobes support for ARM64
5 *
6 * Copyright (C) 2013 Linaro Limited.
7 * Author: Sandeepa Prabhu <sandeepa.prabhu@linaro.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 */
19 #include <linux/kasan.h>
20 #include <linux/kernel.h>
21 #include <linux/kprobes.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/stop_machine.h>
25 #include <linux/stringify.h>
26 #include <asm/traps.h>
27 #include <asm/ptrace.h>
28 #include <asm/cacheflush.h>
29 #include <asm/debug-monitors.h>
30 #include <asm/system_misc.h>
31 #include <asm/insn.h>
32 #include <asm/uaccess.h>
33 #include <asm/irq.h>
34 #include <asm-generic/sections.h>
35
36 #include "decode-insn.h"
37
38 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
39 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
40
41 static void __kprobes
42 post_kprobe_handler(struct kprobe_ctlblk *, struct pt_regs *);
43
44 static inline unsigned long min_stack_size(unsigned long addr)
45 {
46 unsigned long size;
47
48 if (on_irq_stack(addr, raw_smp_processor_id()))
49 size = IRQ_STACK_PTR(raw_smp_processor_id()) - addr;
50 else
51 size = (unsigned long)current_thread_info() + THREAD_START_SP - addr;
52
53 return min(size, FIELD_SIZEOF(struct kprobe_ctlblk, jprobes_stack));
54 }
55
56 static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
57 {
58 /* prepare insn slot */
59 p->ainsn.insn[0] = cpu_to_le32(p->opcode);
60
61 flush_icache_range((uintptr_t) (p->ainsn.insn),
62 (uintptr_t) (p->ainsn.insn) +
63 MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
64
65 /*
66 * Needs restoring of return address after stepping xol.
67 */
68 p->ainsn.restore = (unsigned long) p->addr +
69 sizeof(kprobe_opcode_t);
70 }
71
72 static void __kprobes arch_prepare_simulate(struct kprobe *p)
73 {
74 /* This instructions is not executed xol. No need to adjust the PC */
75 p->ainsn.restore = 0;
76 }
77
78 static void __kprobes arch_simulate_insn(struct kprobe *p, struct pt_regs *regs)
79 {
80 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
81
82 if (p->ainsn.handler)
83 p->ainsn.handler((u32)p->opcode, (long)p->addr, regs);
84
85 /* single step simulated, now go for post processing */
86 post_kprobe_handler(kcb, regs);
87 }
88
89 int __kprobes arch_prepare_kprobe(struct kprobe *p)
90 {
91 unsigned long probe_addr = (unsigned long)p->addr;
92 extern char __start_rodata[];
93 extern char __end_rodata[];
94
95 if (probe_addr & 0x3)
96 return -EINVAL;
97
98 /* copy instruction */
99 p->opcode = le32_to_cpu(*p->addr);
100
101 if (in_exception_text(probe_addr))
102 return -EINVAL;
103 if (probe_addr >= (unsigned long) __start_rodata &&
104 probe_addr <= (unsigned long) __end_rodata)
105 return -EINVAL;
106
107 /* decode instruction */
108 switch (arm_kprobe_decode_insn(p->addr, &p->ainsn)) {
109 case INSN_REJECTED: /* insn not supported */
110 return -EINVAL;
111
112 case INSN_GOOD_NO_SLOT: /* insn need simulation */
113 p->ainsn.insn = NULL;
114 break;
115
116 case INSN_GOOD: /* instruction uses slot */
117 p->ainsn.insn = get_insn_slot();
118 if (!p->ainsn.insn)
119 return -ENOMEM;
120 break;
121 };
122
123 /* prepare the instruction */
124 if (p->ainsn.insn)
125 arch_prepare_ss_slot(p);
126 else
127 arch_prepare_simulate(p);
128
129 return 0;
130 }
131
132 static int __kprobes patch_text(kprobe_opcode_t *addr, u32 opcode)
133 {
134 void *addrs[1];
135 u32 insns[1];
136
137 addrs[0] = (void *)addr;
138 insns[0] = (u32)opcode;
139
140 return aarch64_insn_patch_text(addrs, insns, 1);
141 }
142
143 /* arm kprobe: install breakpoint in text */
144 void __kprobes arch_arm_kprobe(struct kprobe *p)
145 {
146 patch_text(p->addr, BRK64_OPCODE_KPROBES);
147 }
148
149 /* disarm kprobe: remove breakpoint from text */
150 void __kprobes arch_disarm_kprobe(struct kprobe *p)
151 {
152 patch_text(p->addr, p->opcode);
153 }
154
155 void __kprobes arch_remove_kprobe(struct kprobe *p)
156 {
157 if (p->ainsn.insn) {
158 free_insn_slot(p->ainsn.insn, 0);
159 p->ainsn.insn = NULL;
160 }
161 }
162
163 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
164 {
165 kcb->prev_kprobe.kp = kprobe_running();
166 kcb->prev_kprobe.status = kcb->kprobe_status;
167 }
168
169 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
170 {
171 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
172 kcb->kprobe_status = kcb->prev_kprobe.status;
173 }
174
175 static void __kprobes set_current_kprobe(struct kprobe *p)
176 {
177 __this_cpu_write(current_kprobe, p);
178 }
179
180 /*
181 * The D-flag (Debug mask) is set (masked) upon debug exception entry.
182 * Kprobes needs to clear (unmask) D-flag -ONLY- in case of recursive
183 * probe i.e. when probe hit from kprobe handler context upon
184 * executing the pre/post handlers. In this case we return with
185 * D-flag clear so that single-stepping can be carried-out.
186 *
187 * Leave D-flag set in all other cases.
188 */
189 static void __kprobes
190 spsr_set_debug_flag(struct pt_regs *regs, int mask)
191 {
192 unsigned long spsr = regs->pstate;
193
194 if (mask)
195 spsr |= PSR_D_BIT;
196 else
197 spsr &= ~PSR_D_BIT;
198
199 regs->pstate = spsr;
200 }
201
202 /*
203 * Interrupts need to be disabled before single-step mode is set, and not
204 * reenabled until after single-step mode ends.
205 * Without disabling interrupt on local CPU, there is a chance of
206 * interrupt occurrence in the period of exception return and start of
207 * out-of-line single-step, that result in wrongly single stepping
208 * into the interrupt handler.
209 */
210 static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
211 struct pt_regs *regs)
212 {
213 kcb->saved_irqflag = regs->pstate;
214 regs->pstate |= PSR_I_BIT;
215 }
216
217 static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
218 struct pt_regs *regs)
219 {
220 if (kcb->saved_irqflag & PSR_I_BIT)
221 regs->pstate |= PSR_I_BIT;
222 else
223 regs->pstate &= ~PSR_I_BIT;
224 }
225
226 static void __kprobes
227 set_ss_context(struct kprobe_ctlblk *kcb, unsigned long addr)
228 {
229 kcb->ss_ctx.ss_pending = true;
230 kcb->ss_ctx.match_addr = addr + sizeof(kprobe_opcode_t);
231 }
232
233 static void __kprobes clear_ss_context(struct kprobe_ctlblk *kcb)
234 {
235 kcb->ss_ctx.ss_pending = false;
236 kcb->ss_ctx.match_addr = 0;
237 }
238
239 static void __kprobes setup_singlestep(struct kprobe *p,
240 struct pt_regs *regs,
241 struct kprobe_ctlblk *kcb, int reenter)
242 {
243 unsigned long slot;
244
245 if (reenter) {
246 save_previous_kprobe(kcb);
247 set_current_kprobe(p);
248 kcb->kprobe_status = KPROBE_REENTER;
249 } else {
250 kcb->kprobe_status = KPROBE_HIT_SS;
251 }
252
253
254 if (p->ainsn.insn) {
255 /* prepare for single stepping */
256 slot = (unsigned long)p->ainsn.insn;
257
258 set_ss_context(kcb, slot); /* mark pending ss */
259
260 if (kcb->kprobe_status == KPROBE_REENTER)
261 spsr_set_debug_flag(regs, 0);
262 else
263 WARN_ON(regs->pstate & PSR_D_BIT);
264
265 /* IRQs and single stepping do not mix well. */
266 kprobes_save_local_irqflag(kcb, regs);
267 kernel_enable_single_step(regs);
268 instruction_pointer_set(regs, slot);
269 } else {
270 /* insn simulation */
271 arch_simulate_insn(p, regs);
272 }
273 }
274
275 static int __kprobes reenter_kprobe(struct kprobe *p,
276 struct pt_regs *regs,
277 struct kprobe_ctlblk *kcb)
278 {
279 switch (kcb->kprobe_status) {
280 case KPROBE_HIT_SSDONE:
281 case KPROBE_HIT_ACTIVE:
282 kprobes_inc_nmissed_count(p);
283 setup_singlestep(p, regs, kcb, 1);
284 break;
285 case KPROBE_HIT_SS:
286 case KPROBE_REENTER:
287 pr_warn("Unrecoverable kprobe detected at %p.\n", p->addr);
288 dump_kprobe(p);
289 BUG();
290 break;
291 default:
292 WARN_ON(1);
293 return 0;
294 }
295
296 return 1;
297 }
298
299 static void __kprobes
300 post_kprobe_handler(struct kprobe_ctlblk *kcb, struct pt_regs *regs)
301 {
302 struct kprobe *cur = kprobe_running();
303
304 if (!cur)
305 return;
306
307 /* return addr restore if non-branching insn */
308 if (cur->ainsn.restore != 0)
309 instruction_pointer_set(regs, cur->ainsn.restore);
310
311 /* restore back original saved kprobe variables and continue */
312 if (kcb->kprobe_status == KPROBE_REENTER) {
313 restore_previous_kprobe(kcb);
314 return;
315 }
316 /* call post handler */
317 kcb->kprobe_status = KPROBE_HIT_SSDONE;
318 if (cur->post_handler) {
319 /* post_handler can hit breakpoint and single step
320 * again, so we enable D-flag for recursive exception.
321 */
322 cur->post_handler(cur, regs, 0);
323 }
324
325 reset_current_kprobe();
326 }
327
328 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
329 {
330 struct kprobe *cur = kprobe_running();
331 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
332
333 switch (kcb->kprobe_status) {
334 case KPROBE_HIT_SS:
335 case KPROBE_REENTER:
336 /*
337 * We are here because the instruction being single
338 * stepped caused a page fault. We reset the current
339 * kprobe and the ip points back to the probe address
340 * and allow the page fault handler to continue as a
341 * normal page fault.
342 */
343 instruction_pointer_set(regs, (unsigned long) cur->addr);
344 if (!instruction_pointer(regs))
345 BUG();
346
347 kernel_disable_single_step();
348 if (kcb->kprobe_status == KPROBE_REENTER)
349 spsr_set_debug_flag(regs, 1);
350
351 if (kcb->kprobe_status == KPROBE_REENTER)
352 restore_previous_kprobe(kcb);
353 else
354 reset_current_kprobe();
355
356 break;
357 case KPROBE_HIT_ACTIVE:
358 case KPROBE_HIT_SSDONE:
359 /*
360 * We increment the nmissed count for accounting,
361 * we can also use npre/npostfault count for accounting
362 * these specific fault cases.
363 */
364 kprobes_inc_nmissed_count(cur);
365
366 /*
367 * We come here because instructions in the pre/post
368 * handler caused the page_fault, this could happen
369 * if handler tries to access user space by
370 * copy_from_user(), get_user() etc. Let the
371 * user-specified handler try to fix it first.
372 */
373 if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
374 return 1;
375
376 /*
377 * In case the user-specified fault handler returned
378 * zero, try to fix up.
379 */
380 if (fixup_exception(regs))
381 return 1;
382 }
383 return 0;
384 }
385
386 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
387 unsigned long val, void *data)
388 {
389 return NOTIFY_DONE;
390 }
391
392 static void __kprobes kprobe_handler(struct pt_regs *regs)
393 {
394 struct kprobe *p, *cur_kprobe;
395 struct kprobe_ctlblk *kcb;
396 unsigned long addr = instruction_pointer(regs);
397
398 kcb = get_kprobe_ctlblk();
399 cur_kprobe = kprobe_running();
400
401 p = get_kprobe((kprobe_opcode_t *) addr);
402
403 if (p) {
404 if (cur_kprobe) {
405 if (reenter_kprobe(p, regs, kcb))
406 return;
407 } else {
408 /* Probe hit */
409 set_current_kprobe(p);
410 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
411
412 /*
413 * If we have no pre-handler or it returned 0, we
414 * continue with normal processing. If we have a
415 * pre-handler and it returned non-zero, it prepped
416 * for calling the break_handler below on re-entry,
417 * so get out doing nothing more here.
418 *
419 * pre_handler can hit a breakpoint and can step thru
420 * before return, keep PSTATE D-flag enabled until
421 * pre_handler return back.
422 */
423 if (!p->pre_handler || !p->pre_handler(p, regs)) {
424 setup_singlestep(p, regs, kcb, 0);
425 return;
426 }
427 }
428 } else if ((le32_to_cpu(*(kprobe_opcode_t *) addr) ==
429 BRK64_OPCODE_KPROBES) && cur_kprobe) {
430 /* We probably hit a jprobe. Call its break handler. */
431 if (cur_kprobe->break_handler &&
432 cur_kprobe->break_handler(cur_kprobe, regs)) {
433 setup_singlestep(cur_kprobe, regs, kcb, 0);
434 return;
435 }
436 }
437 /*
438 * The breakpoint instruction was removed right
439 * after we hit it. Another cpu has removed
440 * either a probepoint or a debugger breakpoint
441 * at this address. In either case, no further
442 * handling of this interrupt is appropriate.
443 * Return back to original instruction, and continue.
444 */
445 }
446
447 static int __kprobes
448 kprobe_ss_hit(struct kprobe_ctlblk *kcb, unsigned long addr)
449 {
450 if ((kcb->ss_ctx.ss_pending)
451 && (kcb->ss_ctx.match_addr == addr)) {
452 clear_ss_context(kcb); /* clear pending ss */
453 return DBG_HOOK_HANDLED;
454 }
455 /* not ours, kprobes should ignore it */
456 return DBG_HOOK_ERROR;
457 }
458
459 int __kprobes
460 kprobe_single_step_handler(struct pt_regs *regs, unsigned int esr)
461 {
462 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
463 int retval;
464
465 /* return error if this is not our step */
466 retval = kprobe_ss_hit(kcb, instruction_pointer(regs));
467
468 if (retval == DBG_HOOK_HANDLED) {
469 kprobes_restore_local_irqflag(kcb, regs);
470 kernel_disable_single_step();
471
472 if (kcb->kprobe_status == KPROBE_REENTER)
473 spsr_set_debug_flag(regs, 1);
474
475 post_kprobe_handler(kcb, regs);
476 }
477
478 return retval;
479 }
480
481 int __kprobes
482 kprobe_breakpoint_handler(struct pt_regs *regs, unsigned int esr)
483 {
484 kprobe_handler(regs);
485 return DBG_HOOK_HANDLED;
486 }
487
488 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
489 {
490 struct jprobe *jp = container_of(p, struct jprobe, kp);
491 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
492 long stack_ptr = kernel_stack_pointer(regs);
493
494 kcb->jprobe_saved_regs = *regs;
495 /*
496 * As Linus pointed out, gcc assumes that the callee
497 * owns the argument space and could overwrite it, e.g.
498 * tailcall optimization. So, to be absolutely safe
499 * we also save and restore enough stack bytes to cover
500 * the argument area.
501 */
502 kasan_disable_current();
503 memcpy(kcb->jprobes_stack, (void *)stack_ptr,
504 min_stack_size(stack_ptr));
505 kasan_enable_current();
506
507 instruction_pointer_set(regs, (unsigned long) jp->entry);
508 preempt_disable();
509 pause_graph_tracing();
510 return 1;
511 }
512
513 void __kprobes jprobe_return(void)
514 {
515 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
516
517 /*
518 * Jprobe handler return by entering break exception,
519 * encoded same as kprobe, but with following conditions
520 * -a special PC to identify it from the other kprobes.
521 * -restore stack addr to original saved pt_regs
522 */
523 asm volatile(" mov sp, %0 \n"
524 "jprobe_return_break: brk %1 \n"
525 :
526 : "r" (kcb->jprobe_saved_regs.sp),
527 "I" (BRK64_ESR_KPROBES)
528 : "memory");
529
530 unreachable();
531 }
532
533 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
534 {
535 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
536 long stack_addr = kcb->jprobe_saved_regs.sp;
537 long orig_sp = kernel_stack_pointer(regs);
538 struct jprobe *jp = container_of(p, struct jprobe, kp);
539 extern const char jprobe_return_break[];
540
541 if (instruction_pointer(regs) != (u64) jprobe_return_break)
542 return 0;
543
544 if (orig_sp != stack_addr) {
545 struct pt_regs *saved_regs =
546 (struct pt_regs *)kcb->jprobe_saved_regs.sp;
547 pr_err("current sp %lx does not match saved sp %lx\n",
548 orig_sp, stack_addr);
549 pr_err("Saved registers for jprobe %p\n", jp);
550 show_regs(saved_regs);
551 pr_err("Current registers\n");
552 show_regs(regs);
553 BUG();
554 }
555 unpause_graph_tracing();
556 *regs = kcb->jprobe_saved_regs;
557 kasan_disable_current();
558 memcpy((void *)stack_addr, kcb->jprobes_stack,
559 min_stack_size(stack_addr));
560 kasan_enable_current();
561 preempt_enable_no_resched();
562 return 1;
563 }
564
565 bool arch_within_kprobe_blacklist(unsigned long addr)
566 {
567 extern char __idmap_text_start[], __idmap_text_end[];
568 extern char __hyp_idmap_text_start[], __hyp_idmap_text_end[];
569
570 if ((addr >= (unsigned long)__kprobes_text_start &&
571 addr < (unsigned long)__kprobes_text_end) ||
572 (addr >= (unsigned long)__entry_text_start &&
573 addr < (unsigned long)__entry_text_end) ||
574 (addr >= (unsigned long)__idmap_text_start &&
575 addr < (unsigned long)__idmap_text_end) ||
576 !!search_exception_tables(addr))
577 return true;
578
579 if (!is_kernel_in_hyp_mode()) {
580 if ((addr >= (unsigned long)__hyp_text_start &&
581 addr < (unsigned long)__hyp_text_end) ||
582 (addr >= (unsigned long)__hyp_idmap_text_start &&
583 addr < (unsigned long)__hyp_idmap_text_end))
584 return true;
585 }
586
587 return false;
588 }
589
590 void __kprobes __used *trampoline_probe_handler(struct pt_regs *regs)
591 {
592 struct kretprobe_instance *ri = NULL;
593 struct hlist_head *head, empty_rp;
594 struct hlist_node *tmp;
595 unsigned long flags, orig_ret_address = 0;
596 unsigned long trampoline_address =
597 (unsigned long)&kretprobe_trampoline;
598 kprobe_opcode_t *correct_ret_addr = NULL;
599
600 INIT_HLIST_HEAD(&empty_rp);
601 kretprobe_hash_lock(current, &head, &flags);
602
603 /*
604 * It is possible to have multiple instances associated with a given
605 * task either because multiple functions in the call path have
606 * return probes installed on them, and/or more than one
607 * return probe was registered for a target function.
608 *
609 * We can handle this because:
610 * - instances are always pushed into the head of the list
611 * - when multiple return probes are registered for the same
612 * function, the (chronologically) first instance's ret_addr
613 * will be the real return address, and all the rest will
614 * point to kretprobe_trampoline.
615 */
616 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
617 if (ri->task != current)
618 /* another task is sharing our hash bucket */
619 continue;
620
621 orig_ret_address = (unsigned long)ri->ret_addr;
622
623 if (orig_ret_address != trampoline_address)
624 /*
625 * This is the real return address. Any other
626 * instances associated with this task are for
627 * other calls deeper on the call stack
628 */
629 break;
630 }
631
632 kretprobe_assert(ri, orig_ret_address, trampoline_address);
633
634 correct_ret_addr = ri->ret_addr;
635 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
636 if (ri->task != current)
637 /* another task is sharing our hash bucket */
638 continue;
639
640 orig_ret_address = (unsigned long)ri->ret_addr;
641 if (ri->rp && ri->rp->handler) {
642 __this_cpu_write(current_kprobe, &ri->rp->kp);
643 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
644 ri->ret_addr = correct_ret_addr;
645 ri->rp->handler(ri, regs);
646 __this_cpu_write(current_kprobe, NULL);
647 }
648
649 recycle_rp_inst(ri, &empty_rp);
650
651 if (orig_ret_address != trampoline_address)
652 /*
653 * This is the real return address. Any other
654 * instances associated with this task are for
655 * other calls deeper on the call stack
656 */
657 break;
658 }
659
660 kretprobe_hash_unlock(current, &flags);
661
662 hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
663 hlist_del(&ri->hlist);
664 kfree(ri);
665 }
666 return (void *)orig_ret_address;
667 }
668
669 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
670 struct pt_regs *regs)
671 {
672 ri->ret_addr = (kprobe_opcode_t *)regs->regs[30];
673
674 /* replace return addr (x30) with trampoline */
675 regs->regs[30] = (long)&kretprobe_trampoline;
676 }
677
678 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
679 {
680 return 0;
681 }
682
683 int __init arch_init_kprobes(void)
684 {
685 return 0;
686 }