kprobes.c 9.8 KB

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