kprobes.c 11 KB

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