ftrace.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. */
  20. #include <linux/kprobes.h>
  21. #include <linux/ptrace.h>
  22. #include <linux/hardirq.h>
  23. #include <linux/preempt.h>
  24. #include <linux/ftrace.h>
  25. #include "common.h"
  26. static nokprobe_inline
  27. void __skip_singlestep(struct kprobe *p, struct pt_regs *regs,
  28. struct kprobe_ctlblk *kcb, unsigned long orig_ip)
  29. {
  30. /*
  31. * Emulate singlestep (and also recover regs->ip)
  32. * as if there is a 5byte nop
  33. */
  34. regs->ip = (unsigned long)p->addr + MCOUNT_INSN_SIZE;
  35. if (unlikely(p->post_handler)) {
  36. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  37. p->post_handler(p, regs, 0);
  38. }
  39. __this_cpu_write(current_kprobe, NULL);
  40. if (orig_ip)
  41. regs->ip = orig_ip;
  42. }
  43. int skip_singlestep(struct kprobe *p, struct pt_regs *regs,
  44. struct kprobe_ctlblk *kcb)
  45. {
  46. if (kprobe_ftrace(p)) {
  47. __skip_singlestep(p, regs, kcb, 0);
  48. preempt_enable_no_resched();
  49. return 1;
  50. }
  51. return 0;
  52. }
  53. NOKPROBE_SYMBOL(skip_singlestep);
  54. /* Ftrace callback handler for kprobes -- called under preepmt disabed */
  55. void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
  56. struct ftrace_ops *ops, struct pt_regs *regs)
  57. {
  58. struct kprobe *p;
  59. struct kprobe_ctlblk *kcb;
  60. unsigned long flags;
  61. /* Disable irq for emulating a breakpoint and avoiding preempt */
  62. local_irq_save(flags);
  63. p = get_kprobe((kprobe_opcode_t *)ip);
  64. if (unlikely(!p) || kprobe_disabled(p))
  65. goto end;
  66. kcb = get_kprobe_ctlblk();
  67. if (kprobe_running()) {
  68. kprobes_inc_nmissed_count(p);
  69. } else {
  70. unsigned long orig_ip = regs->ip;
  71. /* Kprobe handler expects regs->ip = ip + 1 as breakpoint hit */
  72. regs->ip = ip + sizeof(kprobe_opcode_t);
  73. /* To emulate trap based kprobes, preempt_disable here */
  74. preempt_disable();
  75. __this_cpu_write(current_kprobe, p);
  76. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  77. if (!p->pre_handler || !p->pre_handler(p, regs)) {
  78. __skip_singlestep(p, regs, kcb, orig_ip);
  79. preempt_enable_no_resched();
  80. }
  81. /*
  82. * If pre_handler returns !0, it sets regs->ip and
  83. * resets current kprobe, and keep preempt count +1.
  84. */
  85. }
  86. end:
  87. local_irq_restore(flags);
  88. }
  89. NOKPROBE_SYMBOL(kprobe_ftrace_handler);
  90. int arch_prepare_kprobe_ftrace(struct kprobe *p)
  91. {
  92. p->ainsn.insn = NULL;
  93. p->ainsn.boostable = -1;
  94. return 0;
  95. }