kprobes.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  4. */
  5. #include <linux/types.h>
  6. #include <linux/kprobes.h>
  7. #include <linux/slab.h>
  8. #include <linux/module.h>
  9. #include <linux/kdebug.h>
  10. #include <linux/sched.h>
  11. #include <linux/uaccess.h>
  12. #include <asm/cacheflush.h>
  13. #include <asm/current.h>
  14. #include <asm/disasm.h>
  15. #define MIN_STACK_SIZE(addr) min((unsigned long)MAX_STACK_SIZE, \
  16. (unsigned long)current_thread_info() + THREAD_SIZE - (addr))
  17. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  18. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  19. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  20. {
  21. /* Attempt to probe at unaligned address */
  22. if ((unsigned long)p->addr & 0x01)
  23. return -EINVAL;
  24. /* Address should not be in exception handling code */
  25. p->ainsn.is_short = is_short_instr((unsigned long)p->addr);
  26. p->opcode = *p->addr;
  27. return 0;
  28. }
  29. void __kprobes arch_arm_kprobe(struct kprobe *p)
  30. {
  31. *p->addr = UNIMP_S_INSTRUCTION;
  32. flush_icache_range((unsigned long)p->addr,
  33. (unsigned long)p->addr + sizeof(kprobe_opcode_t));
  34. }
  35. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  36. {
  37. *p->addr = p->opcode;
  38. flush_icache_range((unsigned long)p->addr,
  39. (unsigned long)p->addr + sizeof(kprobe_opcode_t));
  40. }
  41. void __kprobes arch_remove_kprobe(struct kprobe *p)
  42. {
  43. arch_disarm_kprobe(p);
  44. /* Can we remove the kprobe in the middle of kprobe handling? */
  45. if (p->ainsn.t1_addr) {
  46. *(p->ainsn.t1_addr) = p->ainsn.t1_opcode;
  47. flush_icache_range((unsigned long)p->ainsn.t1_addr,
  48. (unsigned long)p->ainsn.t1_addr +
  49. sizeof(kprobe_opcode_t));
  50. p->ainsn.t1_addr = NULL;
  51. }
  52. if (p->ainsn.t2_addr) {
  53. *(p->ainsn.t2_addr) = p->ainsn.t2_opcode;
  54. flush_icache_range((unsigned long)p->ainsn.t2_addr,
  55. (unsigned long)p->ainsn.t2_addr +
  56. sizeof(kprobe_opcode_t));
  57. p->ainsn.t2_addr = NULL;
  58. }
  59. }
  60. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  61. {
  62. kcb->prev_kprobe.kp = kprobe_running();
  63. kcb->prev_kprobe.status = kcb->kprobe_status;
  64. }
  65. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  66. {
  67. __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
  68. kcb->kprobe_status = kcb->prev_kprobe.status;
  69. }
  70. static inline void __kprobes set_current_kprobe(struct kprobe *p)
  71. {
  72. __this_cpu_write(current_kprobe, p);
  73. }
  74. static void __kprobes resume_execution(struct kprobe *p, unsigned long addr,
  75. struct pt_regs *regs)
  76. {
  77. /* Remove the trap instructions inserted for single step and
  78. * restore the original instructions
  79. */
  80. if (p->ainsn.t1_addr) {
  81. *(p->ainsn.t1_addr) = p->ainsn.t1_opcode;
  82. flush_icache_range((unsigned long)p->ainsn.t1_addr,
  83. (unsigned long)p->ainsn.t1_addr +
  84. sizeof(kprobe_opcode_t));
  85. p->ainsn.t1_addr = NULL;
  86. }
  87. if (p->ainsn.t2_addr) {
  88. *(p->ainsn.t2_addr) = p->ainsn.t2_opcode;
  89. flush_icache_range((unsigned long)p->ainsn.t2_addr,
  90. (unsigned long)p->ainsn.t2_addr +
  91. sizeof(kprobe_opcode_t));
  92. p->ainsn.t2_addr = NULL;
  93. }
  94. return;
  95. }
  96. static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs)
  97. {
  98. unsigned long next_pc;
  99. unsigned long tgt_if_br = 0;
  100. int is_branch;
  101. unsigned long bta;
  102. /* Copy the opcode back to the kprobe location and execute the
  103. * instruction. Because of this we will not be able to get into the
  104. * same kprobe until this kprobe is done
  105. */
  106. *(p->addr) = p->opcode;
  107. flush_icache_range((unsigned long)p->addr,
  108. (unsigned long)p->addr + sizeof(kprobe_opcode_t));
  109. /* Now we insert the trap at the next location after this instruction to
  110. * single step. If it is a branch we insert the trap at possible branch
  111. * targets
  112. */
  113. bta = regs->bta;
  114. if (regs->status32 & 0x40) {
  115. /* We are in a delay slot with the branch taken */
  116. next_pc = bta & ~0x01;
  117. if (!p->ainsn.is_short) {
  118. if (bta & 0x01)
  119. regs->blink += 2;
  120. else {
  121. /* Branch not taken */
  122. next_pc += 2;
  123. /* next pc is taken from bta after executing the
  124. * delay slot instruction
  125. */
  126. regs->bta += 2;
  127. }
  128. }
  129. is_branch = 0;
  130. } else
  131. is_branch =
  132. disasm_next_pc((unsigned long)p->addr, regs,
  133. (struct callee_regs *) current->thread.callee_reg,
  134. &next_pc, &tgt_if_br);
  135. p->ainsn.t1_addr = (kprobe_opcode_t *) next_pc;
  136. p->ainsn.t1_opcode = *(p->ainsn.t1_addr);
  137. *(p->ainsn.t1_addr) = TRAP_S_2_INSTRUCTION;
  138. flush_icache_range((unsigned long)p->ainsn.t1_addr,
  139. (unsigned long)p->ainsn.t1_addr +
  140. sizeof(kprobe_opcode_t));
  141. if (is_branch) {
  142. p->ainsn.t2_addr = (kprobe_opcode_t *) tgt_if_br;
  143. p->ainsn.t2_opcode = *(p->ainsn.t2_addr);
  144. *(p->ainsn.t2_addr) = TRAP_S_2_INSTRUCTION;
  145. flush_icache_range((unsigned long)p->ainsn.t2_addr,
  146. (unsigned long)p->ainsn.t2_addr +
  147. sizeof(kprobe_opcode_t));
  148. }
  149. }
  150. int __kprobes arc_kprobe_handler(unsigned long addr, struct pt_regs *regs)
  151. {
  152. struct kprobe *p;
  153. struct kprobe_ctlblk *kcb;
  154. preempt_disable();
  155. kcb = get_kprobe_ctlblk();
  156. p = get_kprobe((unsigned long *)addr);
  157. if (p) {
  158. /*
  159. * We have reentered the kprobe_handler, since another kprobe
  160. * was hit while within the handler, we save the original
  161. * kprobes and single step on the instruction of the new probe
  162. * without calling any user handlers to avoid recursive
  163. * kprobes.
  164. */
  165. if (kprobe_running()) {
  166. save_previous_kprobe(kcb);
  167. set_current_kprobe(p);
  168. kprobes_inc_nmissed_count(p);
  169. setup_singlestep(p, regs);
  170. kcb->kprobe_status = KPROBE_REENTER;
  171. return 1;
  172. }
  173. set_current_kprobe(p);
  174. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  175. /* If we have no pre-handler or it returned 0, we continue with
  176. * normal processing. If we have a pre-handler and it returned
  177. * non-zero - which means user handler setup registers to exit
  178. * to another instruction, we must skip the single stepping.
  179. */
  180. if (!p->pre_handler || !p->pre_handler(p, regs)) {
  181. setup_singlestep(p, regs);
  182. kcb->kprobe_status = KPROBE_HIT_SS;
  183. } else {
  184. reset_current_kprobe();
  185. preempt_enable_no_resched();
  186. }
  187. return 1;
  188. }
  189. /* no_kprobe: */
  190. preempt_enable_no_resched();
  191. return 0;
  192. }
  193. static int __kprobes arc_post_kprobe_handler(unsigned long addr,
  194. struct pt_regs *regs)
  195. {
  196. struct kprobe *cur = kprobe_running();
  197. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  198. if (!cur)
  199. return 0;
  200. resume_execution(cur, addr, regs);
  201. /* Rearm the kprobe */
  202. arch_arm_kprobe(cur);
  203. /*
  204. * When we return from trap instruction we go to the next instruction
  205. * We restored the actual instruction in resume_exectuiont and we to
  206. * return to the same address and execute it
  207. */
  208. regs->ret = addr;
  209. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  210. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  211. cur->post_handler(cur, regs, 0);
  212. }
  213. if (kcb->kprobe_status == KPROBE_REENTER) {
  214. restore_previous_kprobe(kcb);
  215. goto out;
  216. }
  217. reset_current_kprobe();
  218. out:
  219. preempt_enable_no_resched();
  220. return 1;
  221. }
  222. /*
  223. * Fault can be for the instruction being single stepped or for the
  224. * pre/post handlers in the module.
  225. * This is applicable for applications like user probes, where we have the
  226. * probe in user space and the handlers in the kernel
  227. */
  228. int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned long trapnr)
  229. {
  230. struct kprobe *cur = kprobe_running();
  231. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  232. switch (kcb->kprobe_status) {
  233. case KPROBE_HIT_SS:
  234. case KPROBE_REENTER:
  235. /*
  236. * We are here because the instruction being single stepped
  237. * caused the fault. We reset the current kprobe and allow the
  238. * exception handler as if it is regular exception. In our
  239. * case it doesn't matter because the system will be halted
  240. */
  241. resume_execution(cur, (unsigned long)cur->addr, regs);
  242. if (kcb->kprobe_status == KPROBE_REENTER)
  243. restore_previous_kprobe(kcb);
  244. else
  245. reset_current_kprobe();
  246. preempt_enable_no_resched();
  247. break;
  248. case KPROBE_HIT_ACTIVE:
  249. case KPROBE_HIT_SSDONE:
  250. /*
  251. * We are here because the instructions in the pre/post handler
  252. * caused the fault.
  253. */
  254. /* We increment the nmissed count for accounting,
  255. * we can also use npre/npostfault count for accounting
  256. * these specific fault cases.
  257. */
  258. kprobes_inc_nmissed_count(cur);
  259. /*
  260. * We come here because instructions in the pre/post
  261. * handler caused the page_fault, this could happen
  262. * if handler tries to access user space by
  263. * copy_from_user(), get_user() etc. Let the
  264. * user-specified handler try to fix it first.
  265. */
  266. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  267. return 1;
  268. /*
  269. * In case the user-specified fault handler returned zero,
  270. * try to fix up.
  271. */
  272. if (fixup_exception(regs))
  273. return 1;
  274. /*
  275. * fixup_exception() could not handle it,
  276. * Let do_page_fault() fix it.
  277. */
  278. break;
  279. default:
  280. break;
  281. }
  282. return 0;
  283. }
  284. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  285. unsigned long val, void *data)
  286. {
  287. struct die_args *args = data;
  288. unsigned long addr = args->err;
  289. int ret = NOTIFY_DONE;
  290. switch (val) {
  291. case DIE_IERR:
  292. if (arc_kprobe_handler(addr, args->regs))
  293. return NOTIFY_STOP;
  294. break;
  295. case DIE_TRAP:
  296. if (arc_post_kprobe_handler(addr, args->regs))
  297. return NOTIFY_STOP;
  298. break;
  299. default:
  300. break;
  301. }
  302. return ret;
  303. }
  304. static void __used kretprobe_trampoline_holder(void)
  305. {
  306. __asm__ __volatile__(".global kretprobe_trampoline\n"
  307. "kretprobe_trampoline:\n" "nop\n");
  308. }
  309. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  310. struct pt_regs *regs)
  311. {
  312. ri->ret_addr = (kprobe_opcode_t *) regs->blink;
  313. ri->fp = NULL;
  314. /* Replace the return addr with trampoline addr */
  315. regs->blink = (unsigned long)&kretprobe_trampoline;
  316. }
  317. static int __kprobes trampoline_probe_handler(struct kprobe *p,
  318. struct pt_regs *regs)
  319. {
  320. regs->ret = __kretprobe_trampoline_handler(regs, &kretprobe_trampoline, NULL);
  321. /* By returning a non zero value, we are telling the kprobe handler
  322. * that we don't want the post_handler to run
  323. */
  324. return 1;
  325. }
  326. static struct kprobe trampoline_p = {
  327. .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
  328. .pre_handler = trampoline_probe_handler
  329. };
  330. int __init arch_init_kprobes(void)
  331. {
  332. /* Registering the trampoline code for the kret probe */
  333. return register_kprobe(&trampoline_p);
  334. }
  335. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  336. {
  337. if (p->addr == (kprobe_opcode_t *) &kretprobe_trampoline)
  338. return 1;
  339. return 0;
  340. }
  341. void trap_is_kprobe(unsigned long address, struct pt_regs *regs)
  342. {
  343. notify_die(DIE_TRAP, "kprobe_trap", regs, address, 0, SIGTRAP);
  344. }