kprobes.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * arch/parisc/kernel/kprobes.c
  4. *
  5. * PA-RISC kprobes implementation
  6. *
  7. * Copyright (c) 2019 Sven Schnelle <[email protected]>
  8. * Copyright (c) 2022 Helge Deller <[email protected]>
  9. */
  10. #include <linux/types.h>
  11. #include <linux/kprobes.h>
  12. #include <linux/slab.h>
  13. #include <asm/cacheflush.h>
  14. #include <asm/patch.h>
  15. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  16. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  17. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  18. {
  19. if ((unsigned long)p->addr & 3UL)
  20. return -EINVAL;
  21. p->ainsn.insn = get_insn_slot();
  22. if (!p->ainsn.insn)
  23. return -ENOMEM;
  24. /*
  25. * Set up new instructions. Second break instruction will
  26. * trigger call of parisc_kprobe_ss_handler().
  27. */
  28. p->opcode = *p->addr;
  29. p->ainsn.insn[0] = p->opcode;
  30. p->ainsn.insn[1] = PARISC_KPROBES_BREAK_INSN2;
  31. flush_insn_slot(p);
  32. return 0;
  33. }
  34. void __kprobes arch_remove_kprobe(struct kprobe *p)
  35. {
  36. if (!p->ainsn.insn)
  37. return;
  38. free_insn_slot(p->ainsn.insn, 0);
  39. p->ainsn.insn = NULL;
  40. }
  41. void __kprobes arch_arm_kprobe(struct kprobe *p)
  42. {
  43. patch_text(p->addr, PARISC_KPROBES_BREAK_INSN);
  44. }
  45. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  46. {
  47. patch_text(p->addr, p->opcode);
  48. }
  49. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  50. {
  51. kcb->prev_kprobe.kp = kprobe_running();
  52. kcb->prev_kprobe.status = kcb->kprobe_status;
  53. }
  54. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  55. {
  56. __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
  57. kcb->kprobe_status = kcb->prev_kprobe.status;
  58. }
  59. static inline void __kprobes set_current_kprobe(struct kprobe *p)
  60. {
  61. __this_cpu_write(current_kprobe, p);
  62. }
  63. static void __kprobes setup_singlestep(struct kprobe *p,
  64. struct kprobe_ctlblk *kcb, struct pt_regs *regs)
  65. {
  66. kcb->iaoq[0] = regs->iaoq[0];
  67. kcb->iaoq[1] = regs->iaoq[1];
  68. instruction_pointer_set(regs, (unsigned long)p->ainsn.insn);
  69. }
  70. int __kprobes parisc_kprobe_break_handler(struct pt_regs *regs)
  71. {
  72. struct kprobe *p;
  73. struct kprobe_ctlblk *kcb;
  74. preempt_disable();
  75. kcb = get_kprobe_ctlblk();
  76. p = get_kprobe((unsigned long *)regs->iaoq[0]);
  77. if (!p) {
  78. preempt_enable_no_resched();
  79. return 0;
  80. }
  81. if (kprobe_running()) {
  82. /*
  83. * We have reentered the kprobe_handler, since another kprobe
  84. * was hit while within the handler, we save the original
  85. * kprobes and single step on the instruction of the new probe
  86. * without calling any user handlers to avoid recursive
  87. * kprobes.
  88. */
  89. save_previous_kprobe(kcb);
  90. set_current_kprobe(p);
  91. kprobes_inc_nmissed_count(p);
  92. setup_singlestep(p, kcb, regs);
  93. kcb->kprobe_status = KPROBE_REENTER;
  94. return 1;
  95. }
  96. set_current_kprobe(p);
  97. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  98. /* If we have no pre-handler or it returned 0, we continue with
  99. * normal processing. If we have a pre-handler and it returned
  100. * non-zero - which means user handler setup registers to exit
  101. * to another instruction, we must skip the single stepping.
  102. */
  103. if (!p->pre_handler || !p->pre_handler(p, regs)) {
  104. setup_singlestep(p, kcb, regs);
  105. kcb->kprobe_status = KPROBE_HIT_SS;
  106. } else {
  107. reset_current_kprobe();
  108. preempt_enable_no_resched();
  109. }
  110. return 1;
  111. }
  112. int __kprobes parisc_kprobe_ss_handler(struct pt_regs *regs)
  113. {
  114. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  115. struct kprobe *p = kprobe_running();
  116. if (!p)
  117. return 0;
  118. if (regs->iaoq[0] != (unsigned long)p->ainsn.insn+4)
  119. return 0;
  120. /* restore back original saved kprobe variables and continue */
  121. if (kcb->kprobe_status == KPROBE_REENTER) {
  122. restore_previous_kprobe(kcb);
  123. return 1;
  124. }
  125. /* for absolute branch instructions we can copy iaoq_b. for relative
  126. * branch instructions we need to calculate the new address based on the
  127. * difference between iaoq_f and iaoq_b. We cannot use iaoq_b without
  128. * modifications because it's based on our ainsn.insn address.
  129. */
  130. if (p->post_handler)
  131. p->post_handler(p, regs, 0);
  132. switch (regs->iir >> 26) {
  133. case 0x38: /* BE */
  134. case 0x39: /* BE,L */
  135. case 0x3a: /* BV */
  136. case 0x3b: /* BVE */
  137. /* for absolute branches, regs->iaoq[1] has already the right
  138. * address
  139. */
  140. regs->iaoq[0] = kcb->iaoq[1];
  141. break;
  142. default:
  143. regs->iaoq[0] = kcb->iaoq[1];
  144. regs->iaoq[1] = regs->iaoq[0] + 4;
  145. break;
  146. }
  147. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  148. reset_current_kprobe();
  149. return 1;
  150. }
  151. void __kretprobe_trampoline(void)
  152. {
  153. asm volatile("nop");
  154. asm volatile("nop");
  155. }
  156. static int __kprobes trampoline_probe_handler(struct kprobe *p,
  157. struct pt_regs *regs);
  158. static struct kprobe trampoline_p = {
  159. .pre_handler = trampoline_probe_handler
  160. };
  161. static int __kprobes trampoline_probe_handler(struct kprobe *p,
  162. struct pt_regs *regs)
  163. {
  164. __kretprobe_trampoline_handler(regs, NULL);
  165. return 1;
  166. }
  167. void arch_kretprobe_fixup_return(struct pt_regs *regs,
  168. kprobe_opcode_t *correct_ret_addr)
  169. {
  170. regs->gr[2] = (unsigned long)correct_ret_addr;
  171. }
  172. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  173. struct pt_regs *regs)
  174. {
  175. ri->ret_addr = (kprobe_opcode_t *)regs->gr[2];
  176. ri->fp = NULL;
  177. /* Replace the return addr with trampoline addr. */
  178. regs->gr[2] = (unsigned long)trampoline_p.addr;
  179. }
  180. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  181. {
  182. return p->addr == trampoline_p.addr;
  183. }
  184. int __init arch_init_kprobes(void)
  185. {
  186. trampoline_p.addr = (kprobe_opcode_t *)
  187. dereference_function_descriptor(__kretprobe_trampoline);
  188. return register_kprobe(&trampoline_p);
  189. }