kprobes.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. static int
  151. __kprobes arc_kprobe_handler(unsigned long addr, struct pt_regs *regs)
  152. {
  153. struct kprobe *p;
  154. struct kprobe_ctlblk *kcb;
  155. preempt_disable();
  156. kcb = get_kprobe_ctlblk();
  157. p = get_kprobe((unsigned long *)addr);
  158. if (p) {
  159. /*
  160. * We have reentered the kprobe_handler, since another kprobe
  161. * was hit while within the handler, we save the original
  162. * kprobes and single step on the instruction of the new probe
  163. * without calling any user handlers to avoid recursive
  164. * kprobes.
  165. */
  166. if (kprobe_running()) {
  167. save_previous_kprobe(kcb);
  168. set_current_kprobe(p);
  169. kprobes_inc_nmissed_count(p);
  170. setup_singlestep(p, regs);
  171. kcb->kprobe_status = KPROBE_REENTER;
  172. return 1;
  173. }
  174. set_current_kprobe(p);
  175. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  176. /* If we have no pre-handler or it returned 0, we continue with
  177. * normal processing. If we have a pre-handler and it returned
  178. * non-zero - which means user handler setup registers to exit
  179. * to another instruction, we must skip the single stepping.
  180. */
  181. if (!p->pre_handler || !p->pre_handler(p, regs)) {
  182. setup_singlestep(p, regs);
  183. kcb->kprobe_status = KPROBE_HIT_SS;
  184. } else {
  185. reset_current_kprobe();
  186. preempt_enable_no_resched();
  187. }
  188. return 1;
  189. }
  190. /* no_kprobe: */
  191. preempt_enable_no_resched();
  192. return 0;
  193. }
  194. static int
  195. __kprobes arc_post_kprobe_handler(unsigned long addr, struct pt_regs *regs)
  196. {
  197. struct kprobe *cur = kprobe_running();
  198. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  199. if (!cur)
  200. return 0;
  201. resume_execution(cur, addr, regs);
  202. /* Rearm the kprobe */
  203. arch_arm_kprobe(cur);
  204. /*
  205. * When we return from trap instruction we go to the next instruction
  206. * We restored the actual instruction in resume_exectuiont and we to
  207. * return to the same address and execute it
  208. */
  209. regs->ret = addr;
  210. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  211. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  212. cur->post_handler(cur, regs, 0);
  213. }
  214. if (kcb->kprobe_status == KPROBE_REENTER) {
  215. restore_previous_kprobe(kcb);
  216. goto out;
  217. }
  218. reset_current_kprobe();
  219. out:
  220. preempt_enable_no_resched();
  221. return 1;
  222. }
  223. /*
  224. * Fault can be for the instruction being single stepped or for the
  225. * pre/post handlers in the module.
  226. * This is applicable for applications like user probes, where we have the
  227. * probe in user space and the handlers in the kernel
  228. */
  229. int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned long trapnr)
  230. {
  231. struct kprobe *cur = kprobe_running();
  232. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  233. switch (kcb->kprobe_status) {
  234. case KPROBE_HIT_SS:
  235. case KPROBE_REENTER:
  236. /*
  237. * We are here because the instruction being single stepped
  238. * caused the fault. We reset the current kprobe and allow the
  239. * exception handler as if it is regular exception. In our
  240. * case it doesn't matter because the system will be halted
  241. */
  242. resume_execution(cur, (unsigned long)cur->addr, regs);
  243. if (kcb->kprobe_status == KPROBE_REENTER)
  244. restore_previous_kprobe(kcb);
  245. else
  246. reset_current_kprobe();
  247. preempt_enable_no_resched();
  248. break;
  249. case KPROBE_HIT_ACTIVE:
  250. case KPROBE_HIT_SSDONE:
  251. /*
  252. * We are here because the instructions in the pre/post handler
  253. * caused the fault.
  254. */
  255. /*
  256. * In case the user-specified fault handler returned zero,
  257. * try to fix up.
  258. */
  259. if (fixup_exception(regs))
  260. return 1;
  261. /*
  262. * fixup_exception() could not handle it,
  263. * Let do_page_fault() fix it.
  264. */
  265. break;
  266. default:
  267. break;
  268. }
  269. return 0;
  270. }
  271. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  272. unsigned long val, void *data)
  273. {
  274. struct die_args *args = data;
  275. unsigned long addr = args->err;
  276. int ret = NOTIFY_DONE;
  277. switch (val) {
  278. case DIE_IERR:
  279. if (arc_kprobe_handler(addr, args->regs))
  280. return NOTIFY_STOP;
  281. break;
  282. case DIE_TRAP:
  283. if (arc_post_kprobe_handler(addr, args->regs))
  284. return NOTIFY_STOP;
  285. break;
  286. default:
  287. break;
  288. }
  289. return ret;
  290. }
  291. static void __used kretprobe_trampoline_holder(void)
  292. {
  293. __asm__ __volatile__(".global __kretprobe_trampoline\n"
  294. "__kretprobe_trampoline:\n"
  295. "nop\n");
  296. }
  297. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  298. struct pt_regs *regs)
  299. {
  300. ri->ret_addr = (kprobe_opcode_t *) regs->blink;
  301. ri->fp = NULL;
  302. /* Replace the return addr with trampoline addr */
  303. regs->blink = (unsigned long)&__kretprobe_trampoline;
  304. }
  305. static int __kprobes trampoline_probe_handler(struct kprobe *p,
  306. struct pt_regs *regs)
  307. {
  308. regs->ret = __kretprobe_trampoline_handler(regs, NULL);
  309. /* By returning a non zero value, we are telling the kprobe handler
  310. * that we don't want the post_handler to run
  311. */
  312. return 1;
  313. }
  314. static struct kprobe trampoline_p = {
  315. .addr = (kprobe_opcode_t *) &__kretprobe_trampoline,
  316. .pre_handler = trampoline_probe_handler
  317. };
  318. int __init arch_init_kprobes(void)
  319. {
  320. /* Registering the trampoline code for the kret probe */
  321. return register_kprobe(&trampoline_p);
  322. }
  323. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  324. {
  325. if (p->addr == (kprobe_opcode_t *) &__kretprobe_trampoline)
  326. return 1;
  327. return 0;
  328. }
  329. void trap_is_kprobe(unsigned long address, struct pt_regs *regs)
  330. {
  331. notify_die(DIE_TRAP, "kprobe_trap", regs, address, 0, SIGTRAP);
  332. }