kprobes.c 15 KB

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