kprobes-ftrace.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Dynamic Ftrace based Kprobes Optimization
  4. *
  5. * Copyright (C) Hitachi Ltd., 2012
  6. * Copyright 2016 Naveen N. Rao <[email protected]>
  7. * IBM Corporation
  8. */
  9. #include <linux/kprobes.h>
  10. #include <linux/ptrace.h>
  11. #include <linux/hardirq.h>
  12. #include <linux/preempt.h>
  13. #include <linux/ftrace.h>
  14. /* Ftrace callback handler for kprobes */
  15. void kprobe_ftrace_handler(unsigned long nip, unsigned long parent_nip,
  16. struct ftrace_ops *ops, struct ftrace_regs *fregs)
  17. {
  18. struct kprobe *p;
  19. struct kprobe_ctlblk *kcb;
  20. struct pt_regs *regs;
  21. int bit;
  22. bit = ftrace_test_recursion_trylock(nip, parent_nip);
  23. if (bit < 0)
  24. return;
  25. regs = ftrace_get_regs(fregs);
  26. p = get_kprobe((kprobe_opcode_t *)nip);
  27. if (unlikely(!p) || kprobe_disabled(p))
  28. goto out;
  29. kcb = get_kprobe_ctlblk();
  30. if (kprobe_running()) {
  31. kprobes_inc_nmissed_count(p);
  32. } else {
  33. /*
  34. * On powerpc, NIP is *before* this instruction for the
  35. * pre handler
  36. */
  37. regs_add_return_ip(regs, -MCOUNT_INSN_SIZE);
  38. __this_cpu_write(current_kprobe, p);
  39. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  40. if (!p->pre_handler || !p->pre_handler(p, regs)) {
  41. /*
  42. * Emulate singlestep (and also recover regs->nip)
  43. * as if there is a nop
  44. */
  45. regs_add_return_ip(regs, MCOUNT_INSN_SIZE);
  46. if (unlikely(p->post_handler)) {
  47. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  48. p->post_handler(p, regs, 0);
  49. }
  50. }
  51. /*
  52. * If pre_handler returns !0, it changes regs->nip. We have to
  53. * skip emulating post_handler.
  54. */
  55. __this_cpu_write(current_kprobe, NULL);
  56. }
  57. out:
  58. ftrace_test_recursion_unlock(bit);
  59. }
  60. NOKPROBE_SYMBOL(kprobe_ftrace_handler);
  61. int arch_prepare_kprobe_ftrace(struct kprobe *p)
  62. {
  63. p->ainsn.insn = NULL;
  64. p->ainsn.boostable = -1;
  65. return 0;
  66. }