kprobes.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * Kernel probes (kprobes) for SuperH
  3. *
  4. * Copyright (C) 2007 Chris Smith <chris.smith@st.com>
  5. * Copyright (C) 2006 Lineo Solutions, Inc.
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file "COPYING" in the main directory of this archive
  9. * for more details.
  10. */
  11. #include <linux/kprobes.h>
  12. #include <linux/module.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/preempt.h>
  15. #include <linux/kdebug.h>
  16. #include <linux/slab.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/uaccess.h>
  19. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  20. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  21. static struct kprobe saved_current_opcode;
  22. static struct kprobe saved_next_opcode;
  23. static struct kprobe saved_next_opcode2;
  24. #define OPCODE_JMP(x) (((x) & 0xF0FF) == 0x402b)
  25. #define OPCODE_JSR(x) (((x) & 0xF0FF) == 0x400b)
  26. #define OPCODE_BRA(x) (((x) & 0xF000) == 0xa000)
  27. #define OPCODE_BRAF(x) (((x) & 0xF0FF) == 0x0023)
  28. #define OPCODE_BSR(x) (((x) & 0xF000) == 0xb000)
  29. #define OPCODE_BSRF(x) (((x) & 0xF0FF) == 0x0003)
  30. #define OPCODE_BF_S(x) (((x) & 0xFF00) == 0x8f00)
  31. #define OPCODE_BT_S(x) (((x) & 0xFF00) == 0x8d00)
  32. #define OPCODE_BF(x) (((x) & 0xFF00) == 0x8b00)
  33. #define OPCODE_BT(x) (((x) & 0xFF00) == 0x8900)
  34. #define OPCODE_RTS(x) (((x) & 0x000F) == 0x000b)
  35. #define OPCODE_RTE(x) (((x) & 0xFFFF) == 0x002b)
  36. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  37. {
  38. kprobe_opcode_t opcode = *(kprobe_opcode_t *) (p->addr);
  39. if (OPCODE_RTE(opcode))
  40. return -EFAULT; /* Bad breakpoint */
  41. p->opcode = opcode;
  42. return 0;
  43. }
  44. void __kprobes arch_copy_kprobe(struct kprobe *p)
  45. {
  46. memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
  47. p->opcode = *p->addr;
  48. }
  49. void __kprobes arch_arm_kprobe(struct kprobe *p)
  50. {
  51. *p->addr = BREAKPOINT_INSTRUCTION;
  52. flush_icache_range((unsigned long)p->addr,
  53. (unsigned long)p->addr + sizeof(kprobe_opcode_t));
  54. }
  55. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  56. {
  57. *p->addr = p->opcode;
  58. flush_icache_range((unsigned long)p->addr,
  59. (unsigned long)p->addr + sizeof(kprobe_opcode_t));
  60. }
  61. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  62. {
  63. if (*p->addr == BREAKPOINT_INSTRUCTION)
  64. return 1;
  65. return 0;
  66. }
  67. /**
  68. * If an illegal slot instruction exception occurs for an address
  69. * containing a kprobe, remove the probe.
  70. *
  71. * Returns 0 if the exception was handled successfully, 1 otherwise.
  72. */
  73. int __kprobes kprobe_handle_illslot(unsigned long pc)
  74. {
  75. struct kprobe *p = get_kprobe((kprobe_opcode_t *) pc + 1);
  76. if (p != NULL) {
  77. printk("Warning: removing kprobe from delay slot: 0x%.8x\n",
  78. (unsigned int)pc + 2);
  79. unregister_kprobe(p);
  80. return 0;
  81. }
  82. return 1;
  83. }
  84. void __kprobes arch_remove_kprobe(struct kprobe *p)
  85. {
  86. if (saved_next_opcode.addr != 0x0) {
  87. arch_disarm_kprobe(p);
  88. arch_disarm_kprobe(&saved_next_opcode);
  89. saved_next_opcode.addr = 0x0;
  90. saved_next_opcode.opcode = 0x0;
  91. if (saved_next_opcode2.addr != 0x0) {
  92. arch_disarm_kprobe(&saved_next_opcode2);
  93. saved_next_opcode2.addr = 0x0;
  94. saved_next_opcode2.opcode = 0x0;
  95. }
  96. }
  97. }
  98. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  99. {
  100. kcb->prev_kprobe.kp = kprobe_running();
  101. kcb->prev_kprobe.status = kcb->kprobe_status;
  102. }
  103. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  104. {
  105. __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
  106. kcb->kprobe_status = kcb->prev_kprobe.status;
  107. }
  108. static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  109. struct kprobe_ctlblk *kcb)
  110. {
  111. __get_cpu_var(current_kprobe) = p;
  112. }
  113. /*
  114. * Singlestep is implemented by disabling the current kprobe and setting one
  115. * on the next instruction, following branches. Two probes are set if the
  116. * branch is conditional.
  117. */
  118. static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  119. {
  120. kprobe_opcode_t *addr = NULL;
  121. saved_current_opcode.addr = (kprobe_opcode_t *) (regs->pc);
  122. addr = saved_current_opcode.addr;
  123. if (p != NULL) {
  124. arch_disarm_kprobe(p);
  125. if (OPCODE_JSR(p->opcode) || OPCODE_JMP(p->opcode)) {
  126. unsigned int reg_nr = ((p->opcode >> 8) & 0x000F);
  127. saved_next_opcode.addr =
  128. (kprobe_opcode_t *) regs->regs[reg_nr];
  129. } else if (OPCODE_BRA(p->opcode) || OPCODE_BSR(p->opcode)) {
  130. unsigned long disp = (p->opcode & 0x0FFF);
  131. saved_next_opcode.addr =
  132. (kprobe_opcode_t *) (regs->pc + 4 + disp * 2);
  133. } else if (OPCODE_BRAF(p->opcode) || OPCODE_BSRF(p->opcode)) {
  134. unsigned int reg_nr = ((p->opcode >> 8) & 0x000F);
  135. saved_next_opcode.addr =
  136. (kprobe_opcode_t *) (regs->pc + 4 +
  137. regs->regs[reg_nr]);
  138. } else if (OPCODE_RTS(p->opcode)) {
  139. saved_next_opcode.addr = (kprobe_opcode_t *) regs->pr;
  140. } else if (OPCODE_BF(p->opcode) || OPCODE_BT(p->opcode)) {
  141. unsigned long disp = (p->opcode & 0x00FF);
  142. /* case 1 */
  143. saved_next_opcode.addr = p->addr + 1;
  144. /* case 2 */
  145. saved_next_opcode2.addr =
  146. (kprobe_opcode_t *) (regs->pc + 4 + disp * 2);
  147. saved_next_opcode2.opcode = *(saved_next_opcode2.addr);
  148. arch_arm_kprobe(&saved_next_opcode2);
  149. } else if (OPCODE_BF_S(p->opcode) || OPCODE_BT_S(p->opcode)) {
  150. unsigned long disp = (p->opcode & 0x00FF);
  151. /* case 1 */
  152. saved_next_opcode.addr = p->addr + 2;
  153. /* case 2 */
  154. saved_next_opcode2.addr =
  155. (kprobe_opcode_t *) (regs->pc + 4 + disp * 2);
  156. saved_next_opcode2.opcode = *(saved_next_opcode2.addr);
  157. arch_arm_kprobe(&saved_next_opcode2);
  158. } else {
  159. saved_next_opcode.addr = p->addr + 1;
  160. }
  161. saved_next_opcode.opcode = *(saved_next_opcode.addr);
  162. arch_arm_kprobe(&saved_next_opcode);
  163. }
  164. }
  165. /* Called with kretprobe_lock held */
  166. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  167. struct pt_regs *regs)
  168. {
  169. ri->ret_addr = (kprobe_opcode_t *) regs->pr;
  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. } else {
  207. p = __get_cpu_var(current_kprobe);
  208. if (p->break_handler && p->break_handler(p, regs)) {
  209. goto ss_probe;
  210. }
  211. }
  212. goto no_kprobe;
  213. }
  214. p = get_kprobe(addr);
  215. if (!p) {
  216. /* Not one of ours: let kernel handle it */
  217. if (*(kprobe_opcode_t *)addr != BREAKPOINT_INSTRUCTION) {
  218. /*
  219. * The breakpoint instruction was removed right
  220. * after we hit it. Another cpu has removed
  221. * either a probepoint or a debugger breakpoint
  222. * at this address. In either case, no further
  223. * handling of this interrupt is appropriate.
  224. */
  225. ret = 1;
  226. }
  227. goto no_kprobe;
  228. }
  229. set_current_kprobe(p, regs, kcb);
  230. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  231. if (p->pre_handler && p->pre_handler(p, regs))
  232. /* handler has already set things up, so skip ss setup */
  233. return 1;
  234. ss_probe:
  235. prepare_singlestep(p, regs);
  236. kcb->kprobe_status = KPROBE_HIT_SS;
  237. return 1;
  238. no_kprobe:
  239. preempt_enable_no_resched();
  240. return ret;
  241. }
  242. /*
  243. * For function-return probes, init_kprobes() establishes a probepoint
  244. * here. When a retprobed function returns, this probe is hit and
  245. * trampoline_probe_handler() runs, calling the kretprobe's handler.
  246. */
  247. static void __used kretprobe_trampoline_holder(void)
  248. {
  249. asm volatile (".globl kretprobe_trampoline\n"
  250. "kretprobe_trampoline:\n\t"
  251. "nop\n");
  252. }
  253. /*
  254. * Called when we hit the probe point at kretprobe_trampoline
  255. */
  256. int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
  257. {
  258. struct kretprobe_instance *ri = NULL;
  259. struct hlist_head *head, empty_rp;
  260. struct hlist_node *node, *tmp;
  261. unsigned long flags, orig_ret_address = 0;
  262. unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
  263. INIT_HLIST_HEAD(&empty_rp);
  264. kretprobe_hash_lock(current, &head, &flags);
  265. /*
  266. * It is possible to have multiple instances associated with a given
  267. * task either because an multiple functions in the call path
  268. * have a return probe installed on them, and/or more then one return
  269. * return probe was registered for a target function.
  270. *
  271. * We can handle this because:
  272. * - instances are always inserted at the head of the list
  273. * - when multiple return probes are registered for the same
  274. * function, the first instance's ret_addr will point to the
  275. * real return address, and all the rest will point to
  276. * kretprobe_trampoline
  277. */
  278. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  279. if (ri->task != current)
  280. /* another task is sharing our hash bucket */
  281. continue;
  282. if (ri->rp && ri->rp->handler) {
  283. __get_cpu_var(current_kprobe) = &ri->rp->kp;
  284. ri->rp->handler(ri, regs);
  285. __get_cpu_var(current_kprobe) = NULL;
  286. }
  287. orig_ret_address = (unsigned long)ri->ret_addr;
  288. recycle_rp_inst(ri, &empty_rp);
  289. if (orig_ret_address != trampoline_address)
  290. /*
  291. * This is the real return address. Any other
  292. * instances associated with this task are for
  293. * other calls deeper on the call stack
  294. */
  295. break;
  296. }
  297. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  298. regs->pc = orig_ret_address;
  299. kretprobe_hash_unlock(current, &flags);
  300. preempt_enable_no_resched();
  301. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  302. hlist_del(&ri->hlist);
  303. kfree(ri);
  304. }
  305. return orig_ret_address;
  306. }
  307. static int __kprobes post_kprobe_handler(struct pt_regs *regs)
  308. {
  309. struct kprobe *cur = kprobe_running();
  310. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  311. kprobe_opcode_t *addr = NULL;
  312. struct kprobe *p = NULL;
  313. if (!cur)
  314. return 0;
  315. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  316. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  317. cur->post_handler(cur, regs, 0);
  318. }
  319. if (saved_next_opcode.addr != 0x0) {
  320. arch_disarm_kprobe(&saved_next_opcode);
  321. saved_next_opcode.addr = 0x0;
  322. saved_next_opcode.opcode = 0x0;
  323. addr = saved_current_opcode.addr;
  324. saved_current_opcode.addr = 0x0;
  325. p = get_kprobe(addr);
  326. arch_arm_kprobe(p);
  327. if (saved_next_opcode2.addr != 0x0) {
  328. arch_disarm_kprobe(&saved_next_opcode2);
  329. saved_next_opcode2.addr = 0x0;
  330. saved_next_opcode2.opcode = 0x0;
  331. }
  332. }
  333. /* Restore back the original saved kprobes variables and continue. */
  334. if (kcb->kprobe_status == KPROBE_REENTER) {
  335. restore_previous_kprobe(kcb);
  336. goto out;
  337. }
  338. reset_current_kprobe();
  339. out:
  340. preempt_enable_no_resched();
  341. return 1;
  342. }
  343. int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  344. {
  345. struct kprobe *cur = kprobe_running();
  346. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  347. const struct exception_table_entry *entry;
  348. switch (kcb->kprobe_status) {
  349. case KPROBE_HIT_SS:
  350. case KPROBE_REENTER:
  351. /*
  352. * We are here because the instruction being single
  353. * stepped caused a page fault. We reset the current
  354. * kprobe, point the pc back to the probe address
  355. * and allow the page fault handler to continue as a
  356. * normal page fault.
  357. */
  358. regs->pc = (unsigned long)cur->addr;
  359. if (kcb->kprobe_status == KPROBE_REENTER)
  360. restore_previous_kprobe(kcb);
  361. else
  362. reset_current_kprobe();
  363. preempt_enable_no_resched();
  364. break;
  365. case KPROBE_HIT_ACTIVE:
  366. case KPROBE_HIT_SSDONE:
  367. /*
  368. * We increment the nmissed count for accounting,
  369. * we can also use npre/npostfault count for accounting
  370. * these specific fault cases.
  371. */
  372. kprobes_inc_nmissed_count(cur);
  373. /*
  374. * We come here because instructions in the pre/post
  375. * handler caused the page_fault, this could happen
  376. * if handler tries to access user space by
  377. * copy_from_user(), get_user() etc. Let the
  378. * user-specified handler try to fix it first.
  379. */
  380. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  381. return 1;
  382. /*
  383. * In case the user-specified fault handler returned
  384. * zero, try to fix up.
  385. */
  386. if ((entry = search_exception_tables(regs->pc)) != NULL) {
  387. regs->pc = entry->fixup;
  388. return 1;
  389. }
  390. /*
  391. * fixup_exception() could not handle it,
  392. * Let do_page_fault() fix it.
  393. */
  394. break;
  395. default:
  396. break;
  397. }
  398. return 0;
  399. }
  400. /*
  401. * Wrapper routine to for handling exceptions.
  402. */
  403. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  404. unsigned long val, void *data)
  405. {
  406. struct kprobe *p = NULL;
  407. struct die_args *args = (struct die_args *)data;
  408. int ret = NOTIFY_DONE;
  409. kprobe_opcode_t *addr = NULL;
  410. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  411. addr = (kprobe_opcode_t *) (args->regs->pc);
  412. if (val == DIE_TRAP) {
  413. if (!kprobe_running()) {
  414. if (kprobe_handler(args->regs)) {
  415. ret = NOTIFY_STOP;
  416. } else {
  417. /* Not a kprobe trap */
  418. ret = NOTIFY_DONE;
  419. }
  420. } else {
  421. p = get_kprobe(addr);
  422. if ((kcb->kprobe_status == KPROBE_HIT_SS) ||
  423. (kcb->kprobe_status == KPROBE_REENTER)) {
  424. if (post_kprobe_handler(args->regs))
  425. ret = NOTIFY_STOP;
  426. } else {
  427. if (kprobe_handler(args->regs)) {
  428. ret = NOTIFY_STOP;
  429. } else {
  430. p = __get_cpu_var(current_kprobe);
  431. if (p->break_handler &&
  432. p->break_handler(p, args->regs))
  433. ret = NOTIFY_STOP;
  434. }
  435. }
  436. }
  437. }
  438. return ret;
  439. }
  440. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  441. {
  442. struct jprobe *jp = container_of(p, struct jprobe, kp);
  443. unsigned long addr;
  444. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  445. kcb->jprobe_saved_regs = *regs;
  446. kcb->jprobe_saved_r15 = regs->regs[15];
  447. addr = kcb->jprobe_saved_r15;
  448. /*
  449. * TBD: As Linus pointed out, gcc assumes that the callee
  450. * owns the argument space and could overwrite it, e.g.
  451. * tailcall optimization. So, to be absolutely safe
  452. * we also save and restore enough stack bytes to cover
  453. * the argument area.
  454. */
  455. memcpy(kcb->jprobes_stack, (kprobe_opcode_t *) addr,
  456. MIN_STACK_SIZE(addr));
  457. regs->pc = (unsigned long)(jp->entry);
  458. return 1;
  459. }
  460. void __kprobes jprobe_return(void)
  461. {
  462. asm volatile ("trapa #0x3a\n\t" "jprobe_return_end:\n\t" "nop\n\t");
  463. }
  464. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  465. {
  466. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  467. unsigned long stack_addr = kcb->jprobe_saved_r15;
  468. u8 *addr = (u8 *)regs->pc;
  469. if ((addr >= (u8 *)jprobe_return) &&
  470. (addr <= (u8 *)jprobe_return_end)) {
  471. *regs = kcb->jprobe_saved_regs;
  472. memcpy((kprobe_opcode_t *)stack_addr, kcb->jprobes_stack,
  473. MIN_STACK_SIZE(stack_addr));
  474. kcb->kprobe_status = KPROBE_HIT_SS;
  475. preempt_enable_no_resched();
  476. return 1;
  477. }
  478. return 0;
  479. }
  480. static struct kprobe trampoline_p = {
  481. .addr = (kprobe_opcode_t *)&kretprobe_trampoline,
  482. .pre_handler = trampoline_probe_handler
  483. };
  484. int __init arch_init_kprobes(void)
  485. {
  486. saved_next_opcode.addr = 0x0;
  487. saved_next_opcode.opcode = 0x0;
  488. saved_current_opcode.addr = 0x0;
  489. saved_current_opcode.opcode = 0x0;
  490. saved_next_opcode2.addr = 0x0;
  491. saved_next_opcode2.opcode = 0x0;
  492. return register_kprobe(&trampoline_p);
  493. }