ftrace.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kprobes.h>
  3. /* Ftrace callback handler for kprobes -- called under preepmt disabed */
  4. void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
  5. struct ftrace_ops *ops, struct pt_regs *regs)
  6. {
  7. struct kprobe *p;
  8. struct kprobe_ctlblk *kcb;
  9. p = get_kprobe((kprobe_opcode_t *)ip);
  10. if (unlikely(!p) || kprobe_disabled(p))
  11. return;
  12. kcb = get_kprobe_ctlblk();
  13. if (kprobe_running()) {
  14. kprobes_inc_nmissed_count(p);
  15. } else {
  16. unsigned long orig_ip = instruction_pointer(regs);
  17. instruction_pointer_set(regs, ip);
  18. __this_cpu_write(current_kprobe, p);
  19. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  20. if (!p->pre_handler || !p->pre_handler(p, regs)) {
  21. /*
  22. * Emulate singlestep (and also recover regs->pc)
  23. * as if there is a nop
  24. */
  25. instruction_pointer_set(regs,
  26. (unsigned long)p->addr + MCOUNT_INSN_SIZE);
  27. if (unlikely(p->post_handler)) {
  28. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  29. p->post_handler(p, regs, 0);
  30. }
  31. instruction_pointer_set(regs, orig_ip);
  32. }
  33. /*
  34. * If pre_handler returns !0, it changes regs->pc. We have to
  35. * skip emulating post_handler.
  36. */
  37. __this_cpu_write(current_kprobe, NULL);
  38. }
  39. }
  40. NOKPROBE_SYMBOL(kprobe_ftrace_handler);
  41. int arch_prepare_kprobe_ftrace(struct kprobe *p)
  42. {
  43. p->ainsn.api.insn = NULL;
  44. return 0;
  45. }