kprobes-ftrace.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Dynamic Ftrace based Kprobes Optimization
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright (C) Hitachi Ltd., 2012
  19. * Copyright 2016 Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
  20. * IBM Corporation
  21. */
  22. #include <linux/kprobes.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/hardirq.h>
  25. #include <linux/preempt.h>
  26. #include <linux/ftrace.h>
  27. /* Ftrace callback handler for kprobes */
  28. void kprobe_ftrace_handler(unsigned long nip, unsigned long parent_nip,
  29. struct ftrace_ops *ops, struct pt_regs *regs)
  30. {
  31. struct kprobe *p;
  32. struct kprobe_ctlblk *kcb;
  33. p = get_kprobe((kprobe_opcode_t *)nip);
  34. if (unlikely(!p) || kprobe_disabled(p))
  35. return;
  36. kcb = get_kprobe_ctlblk();
  37. if (kprobe_running()) {
  38. kprobes_inc_nmissed_count(p);
  39. } else {
  40. /*
  41. * On powerpc, NIP is *before* this instruction for the
  42. * pre handler
  43. */
  44. regs->nip -= MCOUNT_INSN_SIZE;
  45. __this_cpu_write(current_kprobe, p);
  46. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  47. if (!p->pre_handler || !p->pre_handler(p, regs)) {
  48. /*
  49. * Emulate singlestep (and also recover regs->nip)
  50. * as if there is a nop
  51. */
  52. regs->nip += MCOUNT_INSN_SIZE;
  53. if (unlikely(p->post_handler)) {
  54. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  55. p->post_handler(p, regs, 0);
  56. }
  57. }
  58. /*
  59. * If pre_handler returns !0, it changes regs->nip. We have to
  60. * skip emulating post_handler.
  61. */
  62. __this_cpu_write(current_kprobe, NULL);
  63. }
  64. }
  65. NOKPROBE_SYMBOL(kprobe_ftrace_handler);
  66. int arch_prepare_kprobe_ftrace(struct kprobe *p)
  67. {
  68. p->ainsn.insn = NULL;
  69. p->ainsn.boostable = -1;
  70. return 0;
  71. }