kprobes-ftrace.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 <naveen.n.rao@linux.vnet.ibm.com>
  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. if (unlikely(kprobe_ftrace_disabled))
  23. return;
  24. bit = ftrace_test_recursion_trylock(nip, parent_nip);
  25. if (bit < 0)
  26. return;
  27. regs = ftrace_get_regs(fregs);
  28. p = get_kprobe((kprobe_opcode_t *)nip);
  29. if (unlikely(!p) || kprobe_disabled(p))
  30. goto out;
  31. kcb = get_kprobe_ctlblk();
  32. if (kprobe_running()) {
  33. kprobes_inc_nmissed_count(p);
  34. } else {
  35. /*
  36. * On powerpc, NIP is *before* this instruction for the
  37. * pre handler
  38. */
  39. regs_add_return_ip(regs, -MCOUNT_INSN_SIZE);
  40. __this_cpu_write(current_kprobe, p);
  41. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  42. if (!p->pre_handler || !p->pre_handler(p, regs)) {
  43. /*
  44. * Emulate singlestep (and also recover regs->nip)
  45. * as if there is a nop
  46. */
  47. regs_add_return_ip(regs, MCOUNT_INSN_SIZE);
  48. if (unlikely(p->post_handler)) {
  49. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  50. p->post_handler(p, regs, 0);
  51. }
  52. }
  53. /*
  54. * If pre_handler returns !0, it changes regs->nip. We have to
  55. * skip emulating post_handler.
  56. */
  57. __this_cpu_write(current_kprobe, NULL);
  58. }
  59. out:
  60. ftrace_test_recursion_unlock(bit);
  61. }
  62. NOKPROBE_SYMBOL(kprobe_ftrace_handler);
  63. int arch_prepare_kprobe_ftrace(struct kprobe *p)
  64. {
  65. p->ainsn.insn = NULL;
  66. p->ainsn.boostable = -1;
  67. return 0;
  68. }