kprobes.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Kernel probes (kprobes) for SuperH
  4. *
  5. * Copyright (C) 2007 Chris Smith <[email protected]>
  6. * Copyright (C) 2006 Lineo Solutions, Inc.
  7. */
  8. #include <linux/kprobes.h>
  9. #include <linux/extable.h>
  10. #include <linux/ptrace.h>
  11. #include <linux/preempt.h>
  12. #include <linux/kdebug.h>
  13. #include <linux/slab.h>
  14. #include <asm/cacheflush.h>
  15. #include <linux/uaccess.h>
  16. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  17. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  18. static DEFINE_PER_CPU(struct kprobe, saved_current_opcode);
  19. static DEFINE_PER_CPU(struct kprobe, saved_next_opcode);
  20. static DEFINE_PER_CPU(struct kprobe, saved_next_opcode2);
  21. #define OPCODE_JMP(x) (((x) & 0xF0FF) == 0x402b)
  22. #define OPCODE_JSR(x) (((x) & 0xF0FF) == 0x400b)
  23. #define OPCODE_BRA(x) (((x) & 0xF000) == 0xa000)
  24. #define OPCODE_BRAF(x) (((x) & 0xF0FF) == 0x0023)
  25. #define OPCODE_BSR(x) (((x) & 0xF000) == 0xb000)
  26. #define OPCODE_BSRF(x) (((x) & 0xF0FF) == 0x0003)
  27. #define OPCODE_BF_S(x) (((x) & 0xFF00) == 0x8f00)
  28. #define OPCODE_BT_S(x) (((x) & 0xFF00) == 0x8d00)
  29. #define OPCODE_BF(x) (((x) & 0xFF00) == 0x8b00)
  30. #define OPCODE_BT(x) (((x) & 0xFF00) == 0x8900)
  31. #define OPCODE_RTS(x) (((x) & 0x000F) == 0x000b)
  32. #define OPCODE_RTE(x) (((x) & 0xFFFF) == 0x002b)
  33. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  34. {
  35. kprobe_opcode_t opcode = *(kprobe_opcode_t *) (p->addr);
  36. if (OPCODE_RTE(opcode))
  37. return -EFAULT; /* Bad breakpoint */
  38. p->opcode = opcode;
  39. return 0;
  40. }
  41. void __kprobes arch_copy_kprobe(struct kprobe *p)
  42. {
  43. memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
  44. p->opcode = *p->addr;
  45. }
  46. void __kprobes arch_arm_kprobe(struct kprobe *p)
  47. {
  48. *p->addr = BREAKPOINT_INSTRUCTION;
  49. flush_icache_range((unsigned long)p->addr,
  50. (unsigned long)p->addr + sizeof(kprobe_opcode_t));
  51. }
  52. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  53. {
  54. *p->addr = p->opcode;
  55. flush_icache_range((unsigned long)p->addr,
  56. (unsigned long)p->addr + sizeof(kprobe_opcode_t));
  57. }
  58. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  59. {
  60. if (*p->addr == BREAKPOINT_INSTRUCTION)
  61. return 1;
  62. return 0;
  63. }
  64. /**
  65. * If an illegal slot instruction exception occurs for an address
  66. * containing a kprobe, remove the probe.
  67. *
  68. * Returns 0 if the exception was handled successfully, 1 otherwise.
  69. */
  70. int __kprobes kprobe_handle_illslot(unsigned long pc)
  71. {
  72. struct kprobe *p = get_kprobe((kprobe_opcode_t *) pc + 1);
  73. if (p != NULL) {
  74. printk("Warning: removing kprobe from delay slot: 0x%.8x\n",
  75. (unsigned int)pc + 2);
  76. unregister_kprobe(p);
  77. return 0;
  78. }
  79. return 1;
  80. }
  81. void __kprobes arch_remove_kprobe(struct kprobe *p)
  82. {
  83. struct kprobe *saved = this_cpu_ptr(&saved_next_opcode);
  84. if (saved->addr) {
  85. arch_disarm_kprobe(p);
  86. arch_disarm_kprobe(saved);
  87. saved->addr = NULL;
  88. saved->opcode = 0;
  89. saved = this_cpu_ptr(&saved_next_opcode2);
  90. if (saved->addr) {
  91. arch_disarm_kprobe(saved);
  92. saved->addr = NULL;
  93. saved->opcode = 0;
  94. }
  95. }
  96. }
  97. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  98. {
  99. kcb->prev_kprobe.kp = kprobe_running();
  100. kcb->prev_kprobe.status = kcb->kprobe_status;
  101. }
  102. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  103. {
  104. __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
  105. kcb->kprobe_status = kcb->prev_kprobe.status;
  106. }
  107. static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  108. struct kprobe_ctlblk *kcb)
  109. {
  110. __this_cpu_write(current_kprobe, p);
  111. }
  112. /*
  113. * Singlestep is implemented by disabling the current kprobe and setting one
  114. * on the next instruction, following branches. Two probes are set if the
  115. * branch is conditional.
  116. */
  117. static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  118. {
  119. __this_cpu_write(saved_current_opcode.addr, (kprobe_opcode_t *)regs->pc);
  120. if (p != NULL) {
  121. struct kprobe *op1, *op2;
  122. arch_disarm_kprobe(p);
  123. op1 = this_cpu_ptr(&saved_next_opcode);
  124. op2 = this_cpu_ptr(&saved_next_opcode2);
  125. if (OPCODE_JSR(p->opcode) || OPCODE_JMP(p->opcode)) {
  126. unsigned int reg_nr = ((p->opcode >> 8) & 0x000F);
  127. op1->addr = (kprobe_opcode_t *) regs->regs[reg_nr];
  128. } else if (OPCODE_BRA(p->opcode) || OPCODE_BSR(p->opcode)) {
  129. unsigned long disp = (p->opcode & 0x0FFF);
  130. op1->addr =
  131. (kprobe_opcode_t *) (regs->pc + 4 + disp * 2);
  132. } else if (OPCODE_BRAF(p->opcode) || OPCODE_BSRF(p->opcode)) {
  133. unsigned int reg_nr = ((p->opcode >> 8) & 0x000F);
  134. op1->addr =
  135. (kprobe_opcode_t *) (regs->pc + 4 +
  136. regs->regs[reg_nr]);
  137. } else if (OPCODE_RTS(p->opcode)) {
  138. op1->addr = (kprobe_opcode_t *) regs->pr;
  139. } else if (OPCODE_BF(p->opcode) || OPCODE_BT(p->opcode)) {
  140. unsigned long disp = (p->opcode & 0x00FF);
  141. /* case 1 */
  142. op1->addr = p->addr + 1;
  143. /* case 2 */
  144. op2->addr =
  145. (kprobe_opcode_t *) (regs->pc + 4 + disp * 2);
  146. op2->opcode = *(op2->addr);
  147. arch_arm_kprobe(op2);
  148. } else if (OPCODE_BF_S(p->opcode) || OPCODE_BT_S(p->opcode)) {
  149. unsigned long disp = (p->opcode & 0x00FF);
  150. /* case 1 */
  151. op1->addr = p->addr + 2;
  152. /* case 2 */
  153. op2->addr =
  154. (kprobe_opcode_t *) (regs->pc + 4 + disp * 2);
  155. op2->opcode = *(op2->addr);
  156. arch_arm_kprobe(op2);
  157. } else {
  158. op1->addr = p->addr + 1;
  159. }
  160. op1->opcode = *(op1->addr);
  161. arch_arm_kprobe(op1);
  162. }
  163. }
  164. /* Called with kretprobe_lock held */
  165. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  166. struct pt_regs *regs)
  167. {
  168. ri->ret_addr = (kprobe_opcode_t *) regs->pr;
  169. ri->fp = NULL;
  170. /* Replace the return addr with trampoline addr */
  171. regs->pr = (unsigned long)__kretprobe_trampoline;
  172. }
  173. static int __kprobes kprobe_handler(struct pt_regs *regs)
  174. {
  175. struct kprobe *p;
  176. int ret = 0;
  177. kprobe_opcode_t *addr = NULL;
  178. struct kprobe_ctlblk *kcb;
  179. /*
  180. * We don't want to be preempted for the entire
  181. * duration of kprobe processing
  182. */
  183. preempt_disable();
  184. kcb = get_kprobe_ctlblk();
  185. addr = (kprobe_opcode_t *) (regs->pc);
  186. /* Check we're not actually recursing */
  187. if (kprobe_running()) {
  188. p = get_kprobe(addr);
  189. if (p) {
  190. if (kcb->kprobe_status == KPROBE_HIT_SS &&
  191. *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
  192. goto no_kprobe;
  193. }
  194. /* We have reentered the kprobe_handler(), since
  195. * another probe was hit while within the handler.
  196. * We here save the original kprobes variables and
  197. * just single step on the instruction of the new probe
  198. * without calling any user handlers.
  199. */
  200. save_previous_kprobe(kcb);
  201. set_current_kprobe(p, regs, kcb);
  202. kprobes_inc_nmissed_count(p);
  203. prepare_singlestep(p, regs);
  204. kcb->kprobe_status = KPROBE_REENTER;
  205. return 1;
  206. }
  207. goto no_kprobe;
  208. }
  209. p = get_kprobe(addr);
  210. if (!p) {
  211. /* Not one of ours: let kernel handle it */
  212. if (*(kprobe_opcode_t *)addr != BREAKPOINT_INSTRUCTION) {
  213. /*
  214. * The breakpoint instruction was removed right
  215. * after we hit it. Another cpu has removed
  216. * either a probepoint or a debugger breakpoint
  217. * at this address. In either case, no further
  218. * handling of this interrupt is appropriate.
  219. */
  220. ret = 1;
  221. }
  222. goto no_kprobe;
  223. }
  224. set_current_kprobe(p, regs, kcb);
  225. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  226. if (p->pre_handler && p->pre_handler(p, regs)) {
  227. /* handler has already set things up, so skip ss setup */
  228. reset_current_kprobe();
  229. preempt_enable_no_resched();
  230. return 1;
  231. }
  232. prepare_singlestep(p, regs);
  233. kcb->kprobe_status = KPROBE_HIT_SS;
  234. return 1;
  235. no_kprobe:
  236. preempt_enable_no_resched();
  237. return ret;
  238. }
  239. /*
  240. * For function-return probes, init_kprobes() establishes a probepoint
  241. * here. When a retprobed function returns, this probe is hit and
  242. * trampoline_probe_handler() runs, calling the kretprobe's handler.
  243. */
  244. static void __used kretprobe_trampoline_holder(void)
  245. {
  246. asm volatile (".globl __kretprobe_trampoline\n"
  247. "__kretprobe_trampoline:\n\t"
  248. "nop\n");
  249. }
  250. /*
  251. * Called when we hit the probe point at __kretprobe_trampoline
  252. */
  253. int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
  254. {
  255. regs->pc = __kretprobe_trampoline_handler(regs, NULL);
  256. return 1;
  257. }
  258. static int __kprobes post_kprobe_handler(struct pt_regs *regs)
  259. {
  260. struct kprobe *cur = kprobe_running();
  261. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  262. kprobe_opcode_t *addr = NULL;
  263. struct kprobe *p = NULL;
  264. if (!cur)
  265. return 0;
  266. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  267. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  268. cur->post_handler(cur, regs, 0);
  269. }
  270. p = this_cpu_ptr(&saved_next_opcode);
  271. if (p->addr) {
  272. arch_disarm_kprobe(p);
  273. p->addr = NULL;
  274. p->opcode = 0;
  275. addr = __this_cpu_read(saved_current_opcode.addr);
  276. __this_cpu_write(saved_current_opcode.addr, NULL);
  277. p = get_kprobe(addr);
  278. arch_arm_kprobe(p);
  279. p = this_cpu_ptr(&saved_next_opcode2);
  280. if (p->addr) {
  281. arch_disarm_kprobe(p);
  282. p->addr = NULL;
  283. p->opcode = 0;
  284. }
  285. }
  286. /* Restore back the original saved kprobes variables and continue. */
  287. if (kcb->kprobe_status == KPROBE_REENTER) {
  288. restore_previous_kprobe(kcb);
  289. goto out;
  290. }
  291. reset_current_kprobe();
  292. out:
  293. preempt_enable_no_resched();
  294. return 1;
  295. }
  296. int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  297. {
  298. struct kprobe *cur = kprobe_running();
  299. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  300. const struct exception_table_entry *entry;
  301. switch (kcb->kprobe_status) {
  302. case KPROBE_HIT_SS:
  303. case KPROBE_REENTER:
  304. /*
  305. * We are here because the instruction being single
  306. * stepped caused a page fault. We reset the current
  307. * kprobe, point the pc back to the probe address
  308. * and allow the page fault handler to continue as a
  309. * normal page fault.
  310. */
  311. regs->pc = (unsigned long)cur->addr;
  312. if (kcb->kprobe_status == KPROBE_REENTER)
  313. restore_previous_kprobe(kcb);
  314. else
  315. reset_current_kprobe();
  316. preempt_enable_no_resched();
  317. break;
  318. case KPROBE_HIT_ACTIVE:
  319. case KPROBE_HIT_SSDONE:
  320. /*
  321. * In case the user-specified fault handler returned
  322. * zero, try to fix up.
  323. */
  324. if ((entry = search_exception_tables(regs->pc)) != NULL) {
  325. regs->pc = entry->fixup;
  326. return 1;
  327. }
  328. /*
  329. * fixup_exception() could not handle it,
  330. * Let do_page_fault() fix it.
  331. */
  332. break;
  333. default:
  334. break;
  335. }
  336. return 0;
  337. }
  338. /*
  339. * Wrapper routine to for handling exceptions.
  340. */
  341. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  342. unsigned long val, void *data)
  343. {
  344. struct kprobe *p = NULL;
  345. struct die_args *args = (struct die_args *)data;
  346. int ret = NOTIFY_DONE;
  347. kprobe_opcode_t *addr = NULL;
  348. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  349. addr = (kprobe_opcode_t *) (args->regs->pc);
  350. if (val == DIE_TRAP &&
  351. args->trapnr == (BREAKPOINT_INSTRUCTION & 0xff)) {
  352. if (!kprobe_running()) {
  353. if (kprobe_handler(args->regs)) {
  354. ret = NOTIFY_STOP;
  355. } else {
  356. /* Not a kprobe trap */
  357. ret = NOTIFY_DONE;
  358. }
  359. } else {
  360. p = get_kprobe(addr);
  361. if ((kcb->kprobe_status == KPROBE_HIT_SS) ||
  362. (kcb->kprobe_status == KPROBE_REENTER)) {
  363. if (post_kprobe_handler(args->regs))
  364. ret = NOTIFY_STOP;
  365. } else {
  366. if (kprobe_handler(args->regs))
  367. ret = NOTIFY_STOP;
  368. }
  369. }
  370. }
  371. return ret;
  372. }
  373. static struct kprobe trampoline_p = {
  374. .addr = (kprobe_opcode_t *)&__kretprobe_trampoline,
  375. .pre_handler = trampoline_probe_handler
  376. };
  377. int __init arch_init_kprobes(void)
  378. {
  379. return register_kprobe(&trampoline_p);
  380. }