kprobes.c 9.5 KB

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