rethook.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PowerPC implementation of rethook. This depends on kprobes.
  4. */
  5. #include <linux/kprobes.h>
  6. #include <linux/rethook.h>
  7. /*
  8. * Function return trampoline:
  9. * - init_kprobes() establishes a probepoint here
  10. * - When the probed function returns, this probe
  11. * causes the handlers to fire
  12. */
  13. asm(".global arch_rethook_trampoline\n"
  14. ".type arch_rethook_trampoline, @function\n"
  15. "arch_rethook_trampoline:\n"
  16. "nop\n"
  17. "blr\n"
  18. ".size arch_rethook_trampoline, .-arch_rethook_trampoline\n");
  19. /*
  20. * Called when the probe at kretprobe trampoline is hit
  21. */
  22. static int trampoline_rethook_handler(struct kprobe *p, struct pt_regs *regs)
  23. {
  24. return !rethook_trampoline_handler(regs, regs->gpr[1]);
  25. }
  26. NOKPROBE_SYMBOL(trampoline_rethook_handler);
  27. void arch_rethook_prepare(struct rethook_node *rh, struct pt_regs *regs, bool mcount)
  28. {
  29. rh->ret_addr = regs->link;
  30. rh->frame = regs->gpr[1];
  31. /* Replace the return addr with trampoline addr */
  32. regs->link = (unsigned long)arch_rethook_trampoline;
  33. }
  34. NOKPROBE_SYMBOL(arch_rethook_prepare);
  35. /* This is called from rethook_trampoline_handler(). */
  36. void arch_rethook_fixup_return(struct pt_regs *regs, unsigned long orig_ret_address)
  37. {
  38. /*
  39. * We get here through one of two paths:
  40. * 1. by taking a trap -> kprobe_handler() -> here
  41. * 2. by optprobe branch -> optimized_callback() -> opt_pre_handler() -> here
  42. *
  43. * When going back through (1), we need regs->nip to be setup properly
  44. * as it is used to determine the return address from the trap.
  45. * For (2), since nip is not honoured with optprobes, we instead setup
  46. * the link register properly so that the subsequent 'blr' in
  47. * arch_rethook_trampoline jumps back to the right instruction.
  48. *
  49. * For nip, we should set the address to the previous instruction since
  50. * we end up emulating it in kprobe_handler(), which increments the nip
  51. * again.
  52. */
  53. regs_set_return_ip(regs, orig_ret_address - 4);
  54. regs->link = orig_ret_address;
  55. }
  56. NOKPROBE_SYMBOL(arch_rethook_fixup_return);
  57. static struct kprobe trampoline_p = {
  58. .addr = (kprobe_opcode_t *) &arch_rethook_trampoline,
  59. .pre_handler = trampoline_rethook_handler
  60. };
  61. /* rethook initializer */
  62. int __init arch_init_kprobes(void)
  63. {
  64. return register_kprobe(&trampoline_p);
  65. }