kprobes.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #define pr_fmt(fmt) "kprobes: " fmt
  3. #include <linux/kprobes.h>
  4. #include <linux/extable.h>
  5. #include <linux/slab.h>
  6. #include <linux/stop_machine.h>
  7. #include <asm/ptrace.h>
  8. #include <linux/uaccess.h>
  9. #include <asm/sections.h>
  10. #include <asm/cacheflush.h>
  11. #include <asm/bug.h>
  12. #include <asm/patch.h>
  13. #include "decode-insn.h"
  14. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  15. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  16. static void __kprobes
  17. post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
  18. static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
  19. {
  20. unsigned long offset = GET_INSN_LENGTH(p->opcode);
  21. p->ainsn.api.restore = (unsigned long)p->addr + offset;
  22. patch_text(p->ainsn.api.insn, p->opcode);
  23. patch_text((void *)((unsigned long)(p->ainsn.api.insn) + offset),
  24. __BUG_INSN_32);
  25. }
  26. static void __kprobes arch_prepare_simulate(struct kprobe *p)
  27. {
  28. p->ainsn.api.restore = 0;
  29. }
  30. static void __kprobes arch_simulate_insn(struct kprobe *p, struct pt_regs *regs)
  31. {
  32. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  33. if (p->ainsn.api.handler)
  34. p->ainsn.api.handler((u32)p->opcode,
  35. (unsigned long)p->addr, regs);
  36. post_kprobe_handler(p, kcb, regs);
  37. }
  38. static bool __kprobes arch_check_kprobe(struct kprobe *p)
  39. {
  40. unsigned long tmp = (unsigned long)p->addr - p->offset;
  41. unsigned long addr = (unsigned long)p->addr;
  42. while (tmp <= addr) {
  43. if (tmp == addr)
  44. return true;
  45. tmp += GET_INSN_LENGTH(*(u16 *)tmp);
  46. }
  47. return false;
  48. }
  49. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  50. {
  51. u16 *insn = (u16 *)p->addr;
  52. if ((unsigned long)insn & 0x1)
  53. return -EILSEQ;
  54. if (!arch_check_kprobe(p))
  55. return -EILSEQ;
  56. /* copy instruction */
  57. p->opcode = (kprobe_opcode_t)(*insn++);
  58. if (GET_INSN_LENGTH(p->opcode) == 4)
  59. p->opcode |= (kprobe_opcode_t)(*insn) << 16;
  60. /* decode instruction */
  61. switch (riscv_probe_decode_insn(p->addr, &p->ainsn.api)) {
  62. case INSN_REJECTED: /* insn not supported */
  63. return -EINVAL;
  64. case INSN_GOOD_NO_SLOT: /* insn need simulation */
  65. p->ainsn.api.insn = NULL;
  66. break;
  67. case INSN_GOOD: /* instruction uses slot */
  68. p->ainsn.api.insn = get_insn_slot();
  69. if (!p->ainsn.api.insn)
  70. return -ENOMEM;
  71. break;
  72. }
  73. /* prepare the instruction */
  74. if (p->ainsn.api.insn)
  75. arch_prepare_ss_slot(p);
  76. else
  77. arch_prepare_simulate(p);
  78. return 0;
  79. }
  80. #ifdef CONFIG_MMU
  81. void *alloc_insn_page(void)
  82. {
  83. return __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, VMALLOC_END,
  84. GFP_KERNEL, PAGE_KERNEL_READ_EXEC,
  85. VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
  86. __builtin_return_address(0));
  87. }
  88. #endif
  89. /* install breakpoint in text */
  90. void __kprobes arch_arm_kprobe(struct kprobe *p)
  91. {
  92. if ((p->opcode & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
  93. patch_text(p->addr, __BUG_INSN_32);
  94. else
  95. patch_text(p->addr, __BUG_INSN_16);
  96. }
  97. /* remove breakpoint from text */
  98. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  99. {
  100. patch_text(p->addr, p->opcode);
  101. }
  102. void __kprobes arch_remove_kprobe(struct kprobe *p)
  103. {
  104. }
  105. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  106. {
  107. kcb->prev_kprobe.kp = kprobe_running();
  108. kcb->prev_kprobe.status = kcb->kprobe_status;
  109. }
  110. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  111. {
  112. __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
  113. kcb->kprobe_status = kcb->prev_kprobe.status;
  114. }
  115. static void __kprobes set_current_kprobe(struct kprobe *p)
  116. {
  117. __this_cpu_write(current_kprobe, p);
  118. }
  119. /*
  120. * Interrupts need to be disabled before single-step mode is set, and not
  121. * reenabled until after single-step mode ends.
  122. * Without disabling interrupt on local CPU, there is a chance of
  123. * interrupt occurrence in the period of exception return and start of
  124. * out-of-line single-step, that result in wrongly single stepping
  125. * into the interrupt handler.
  126. */
  127. static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
  128. struct pt_regs *regs)
  129. {
  130. kcb->saved_status = regs->status;
  131. regs->status &= ~SR_SPIE;
  132. }
  133. static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
  134. struct pt_regs *regs)
  135. {
  136. regs->status = kcb->saved_status;
  137. }
  138. static void __kprobes setup_singlestep(struct kprobe *p,
  139. struct pt_regs *regs,
  140. struct kprobe_ctlblk *kcb, int reenter)
  141. {
  142. unsigned long slot;
  143. if (reenter) {
  144. save_previous_kprobe(kcb);
  145. set_current_kprobe(p);
  146. kcb->kprobe_status = KPROBE_REENTER;
  147. } else {
  148. kcb->kprobe_status = KPROBE_HIT_SS;
  149. }
  150. if (p->ainsn.api.insn) {
  151. /* prepare for single stepping */
  152. slot = (unsigned long)p->ainsn.api.insn;
  153. /* IRQs and single stepping do not mix well. */
  154. kprobes_save_local_irqflag(kcb, regs);
  155. instruction_pointer_set(regs, slot);
  156. } else {
  157. /* insn simulation */
  158. arch_simulate_insn(p, regs);
  159. }
  160. }
  161. static int __kprobes reenter_kprobe(struct kprobe *p,
  162. struct pt_regs *regs,
  163. struct kprobe_ctlblk *kcb)
  164. {
  165. switch (kcb->kprobe_status) {
  166. case KPROBE_HIT_SSDONE:
  167. case KPROBE_HIT_ACTIVE:
  168. kprobes_inc_nmissed_count(p);
  169. setup_singlestep(p, regs, kcb, 1);
  170. break;
  171. case KPROBE_HIT_SS:
  172. case KPROBE_REENTER:
  173. pr_warn("Failed to recover from reentered kprobes.\n");
  174. dump_kprobe(p);
  175. BUG();
  176. break;
  177. default:
  178. WARN_ON(1);
  179. return 0;
  180. }
  181. return 1;
  182. }
  183. static void __kprobes
  184. post_kprobe_handler(struct kprobe *cur, struct kprobe_ctlblk *kcb, struct pt_regs *regs)
  185. {
  186. /* return addr restore if non-branching insn */
  187. if (cur->ainsn.api.restore != 0)
  188. regs->epc = cur->ainsn.api.restore;
  189. /* restore back original saved kprobe variables and continue */
  190. if (kcb->kprobe_status == KPROBE_REENTER) {
  191. restore_previous_kprobe(kcb);
  192. return;
  193. }
  194. /* call post handler */
  195. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  196. if (cur->post_handler) {
  197. /* post_handler can hit breakpoint and single step
  198. * again, so we enable D-flag for recursive exception.
  199. */
  200. cur->post_handler(cur, regs, 0);
  201. }
  202. reset_current_kprobe();
  203. }
  204. int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int trapnr)
  205. {
  206. struct kprobe *cur = kprobe_running();
  207. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  208. switch (kcb->kprobe_status) {
  209. case KPROBE_HIT_SS:
  210. case KPROBE_REENTER:
  211. /*
  212. * We are here because the instruction being single
  213. * stepped caused a page fault. We reset the current
  214. * kprobe and the ip points back to the probe address
  215. * and allow the page fault handler to continue as a
  216. * normal page fault.
  217. */
  218. regs->epc = (unsigned long) cur->addr;
  219. BUG_ON(!instruction_pointer(regs));
  220. if (kcb->kprobe_status == KPROBE_REENTER)
  221. restore_previous_kprobe(kcb);
  222. else {
  223. kprobes_restore_local_irqflag(kcb, regs);
  224. reset_current_kprobe();
  225. }
  226. break;
  227. case KPROBE_HIT_ACTIVE:
  228. case KPROBE_HIT_SSDONE:
  229. /*
  230. * In case the user-specified fault handler returned
  231. * zero, try to fix up.
  232. */
  233. if (fixup_exception(regs))
  234. return 1;
  235. }
  236. return 0;
  237. }
  238. bool __kprobes
  239. kprobe_breakpoint_handler(struct pt_regs *regs)
  240. {
  241. struct kprobe *p, *cur_kprobe;
  242. struct kprobe_ctlblk *kcb;
  243. unsigned long addr = instruction_pointer(regs);
  244. kcb = get_kprobe_ctlblk();
  245. cur_kprobe = kprobe_running();
  246. p = get_kprobe((kprobe_opcode_t *) addr);
  247. if (p) {
  248. if (cur_kprobe) {
  249. if (reenter_kprobe(p, regs, kcb))
  250. return true;
  251. } else {
  252. /* Probe hit */
  253. set_current_kprobe(p);
  254. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  255. /*
  256. * If we have no pre-handler or it returned 0, we
  257. * continue with normal processing. If we have a
  258. * pre-handler and it returned non-zero, it will
  259. * modify the execution path and no need to single
  260. * stepping. Let's just reset current kprobe and exit.
  261. *
  262. * pre_handler can hit a breakpoint and can step thru
  263. * before return.
  264. */
  265. if (!p->pre_handler || !p->pre_handler(p, regs))
  266. setup_singlestep(p, regs, kcb, 0);
  267. else
  268. reset_current_kprobe();
  269. }
  270. return true;
  271. }
  272. /*
  273. * The breakpoint instruction was removed right
  274. * after we hit it. Another cpu has removed
  275. * either a probepoint or a debugger breakpoint
  276. * at this address. In either case, no further
  277. * handling of this interrupt is appropriate.
  278. * Return back to original instruction, and continue.
  279. */
  280. return false;
  281. }
  282. bool __kprobes
  283. kprobe_single_step_handler(struct pt_regs *regs)
  284. {
  285. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  286. unsigned long addr = instruction_pointer(regs);
  287. struct kprobe *cur = kprobe_running();
  288. if (cur && (kcb->kprobe_status & (KPROBE_HIT_SS | KPROBE_REENTER)) &&
  289. ((unsigned long)&cur->ainsn.api.insn[0] + GET_INSN_LENGTH(cur->opcode) == addr)) {
  290. kprobes_restore_local_irqflag(kcb, regs);
  291. post_kprobe_handler(cur, kcb, regs);
  292. return true;
  293. }
  294. /* not ours, kprobes should ignore it */
  295. return false;
  296. }
  297. /*
  298. * Provide a blacklist of symbols identifying ranges which cannot be kprobed.
  299. * This blacklist is exposed to userspace via debugfs (kprobes/blacklist).
  300. */
  301. int __init arch_populate_kprobe_blacklist(void)
  302. {
  303. int ret;
  304. ret = kprobe_add_area_blacklist((unsigned long)__irqentry_text_start,
  305. (unsigned long)__irqentry_text_end);
  306. return ret;
  307. }
  308. void __kprobes __used *trampoline_probe_handler(struct pt_regs *regs)
  309. {
  310. return (void *)kretprobe_trampoline_handler(regs, NULL);
  311. }
  312. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  313. struct pt_regs *regs)
  314. {
  315. ri->ret_addr = (kprobe_opcode_t *)regs->ra;
  316. ri->fp = NULL;
  317. regs->ra = (unsigned long) &__kretprobe_trampoline;
  318. }
  319. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  320. {
  321. return 0;
  322. }
  323. int __init arch_init_kprobes(void)
  324. {
  325. return 0;
  326. }