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 DEFINE_PER_CPU(struct kprobe, saved_current_opcode);
  22. static DEFINE_PER_CPU(struct kprobe, saved_next_opcode);
  23. static DEFINE_PER_CPU(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. struct kprobe *saved = this_cpu_ptr(&saved_next_opcode);
  87. if (saved->addr) {
  88. arch_disarm_kprobe(p);
  89. arch_disarm_kprobe(saved);
  90. saved->addr = NULL;
  91. saved->opcode = 0;
  92. saved = this_cpu_ptr(&saved_next_opcode2);
  93. if (saved->addr) {
  94. arch_disarm_kprobe(saved);
  95. saved->addr = NULL;
  96. saved->opcode = 0;
  97. }
  98. }
  99. }
  100. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  101. {
  102. kcb->prev_kprobe.kp = kprobe_running();
  103. kcb->prev_kprobe.status = kcb->kprobe_status;
  104. }
  105. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  106. {
  107. __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
  108. kcb->kprobe_status = kcb->prev_kprobe.status;
  109. }
  110. static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  111. struct kprobe_ctlblk *kcb)
  112. {
  113. __this_cpu_write(current_kprobe, p);
  114. }
  115. /*
  116. * Singlestep is implemented by disabling the current kprobe and setting one
  117. * on the next instruction, following branches. Two probes are set if the
  118. * branch is conditional.
  119. */
  120. static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  121. {
  122. __this_cpu_write(saved_current_opcode.addr, (kprobe_opcode_t *)regs->pc);
  123. if (p != NULL) {
  124. struct kprobe *op1, *op2;
  125. arch_disarm_kprobe(p);
  126. op1 = this_cpu_ptr(&saved_next_opcode);
  127. op2 = this_cpu_ptr(&saved_next_opcode2);
  128. if (OPCODE_JSR(p->opcode) || OPCODE_JMP(p->opcode)) {
  129. unsigned int reg_nr = ((p->opcode >> 8) & 0x000F);
  130. op1->addr = (kprobe_opcode_t *) regs->regs[reg_nr];
  131. } else if (OPCODE_BRA(p->opcode) || OPCODE_BSR(p->opcode)) {
  132. unsigned long disp = (p->opcode & 0x0FFF);
  133. op1->addr =
  134. (kprobe_opcode_t *) (regs->pc + 4 + disp * 2);
  135. } else if (OPCODE_BRAF(p->opcode) || OPCODE_BSRF(p->opcode)) {
  136. unsigned int reg_nr = ((p->opcode >> 8) & 0x000F);
  137. op1->addr =
  138. (kprobe_opcode_t *) (regs->pc + 4 +
  139. regs->regs[reg_nr]);
  140. } else if (OPCODE_RTS(p->opcode)) {
  141. op1->addr = (kprobe_opcode_t *) regs->pr;
  142. } else if (OPCODE_BF(p->opcode) || OPCODE_BT(p->opcode)) {
  143. unsigned long disp = (p->opcode & 0x00FF);
  144. /* case 1 */
  145. op1->addr = p->addr + 1;
  146. /* case 2 */
  147. op2->addr =
  148. (kprobe_opcode_t *) (regs->pc + 4 + disp * 2);
  149. op2->opcode = *(op2->addr);
  150. arch_arm_kprobe(op2);
  151. } else if (OPCODE_BF_S(p->opcode) || OPCODE_BT_S(p->opcode)) {
  152. unsigned long disp = (p->opcode & 0x00FF);
  153. /* case 1 */
  154. op1->addr = p->addr + 2;
  155. /* case 2 */
  156. op2->addr =
  157. (kprobe_opcode_t *) (regs->pc + 4 + disp * 2);
  158. op2->opcode = *(op2->addr);
  159. arch_arm_kprobe(op2);
  160. } else {
  161. op1->addr = p->addr + 1;
  162. }
  163. op1->opcode = *(op1->addr);
  164. arch_arm_kprobe(op1);
  165. }
  166. }
  167. /* Called with kretprobe_lock held */
  168. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  169. struct pt_regs *regs)
  170. {
  171. ri->ret_addr = (kprobe_opcode_t *) regs->pr;
  172. /* Replace the return addr with trampoline addr */
  173. regs->pr = (unsigned long)kretprobe_trampoline;
  174. }
  175. static int __kprobes kprobe_handler(struct pt_regs *regs)
  176. {
  177. struct kprobe *p;
  178. int ret = 0;
  179. kprobe_opcode_t *addr = NULL;
  180. struct kprobe_ctlblk *kcb;
  181. /*
  182. * We don't want to be preempted for the entire
  183. * duration of kprobe processing
  184. */
  185. preempt_disable();
  186. kcb = get_kprobe_ctlblk();
  187. addr = (kprobe_opcode_t *) (regs->pc);
  188. /* Check we're not actually recursing */
  189. if (kprobe_running()) {
  190. p = get_kprobe(addr);
  191. if (p) {
  192. if (kcb->kprobe_status == KPROBE_HIT_SS &&
  193. *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
  194. goto no_kprobe;
  195. }
  196. /* We have reentered the kprobe_handler(), since
  197. * another probe was hit while within the handler.
  198. * We here save the original kprobes variables and
  199. * just single step on the instruction of the new probe
  200. * without calling any user handlers.
  201. */
  202. save_previous_kprobe(kcb);
  203. set_current_kprobe(p, regs, kcb);
  204. kprobes_inc_nmissed_count(p);
  205. prepare_singlestep(p, regs);
  206. kcb->kprobe_status = KPROBE_REENTER;
  207. return 1;
  208. } else {
  209. p = __this_cpu_read(current_kprobe);
  210. if (p->break_handler && p->break_handler(p, regs)) {
  211. goto ss_probe;
  212. }
  213. }
  214. goto no_kprobe;
  215. }
  216. p = get_kprobe(addr);
  217. if (!p) {
  218. /* Not one of ours: let kernel handle it */
  219. if (*(kprobe_opcode_t *)addr != BREAKPOINT_INSTRUCTION) {
  220. /*
  221. * The breakpoint instruction was removed right
  222. * after we hit it. Another cpu has removed
  223. * either a probepoint or a debugger breakpoint
  224. * at this address. In either case, no further
  225. * handling of this interrupt is appropriate.
  226. */
  227. ret = 1;
  228. }
  229. goto no_kprobe;
  230. }
  231. set_current_kprobe(p, regs, kcb);
  232. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  233. if (p->pre_handler && p->pre_handler(p, regs))
  234. /* handler has already set things up, so skip ss setup */
  235. return 1;
  236. ss_probe:
  237. prepare_singlestep(p, regs);
  238. kcb->kprobe_status = KPROBE_HIT_SS;
  239. return 1;
  240. no_kprobe:
  241. preempt_enable_no_resched();
  242. return ret;
  243. }
  244. /*
  245. * For function-return probes, init_kprobes() establishes a probepoint
  246. * here. When a retprobed function returns, this probe is hit and
  247. * trampoline_probe_handler() runs, calling the kretprobe's handler.
  248. */
  249. static void __used kretprobe_trampoline_holder(void)
  250. {
  251. asm volatile (".globl kretprobe_trampoline\n"
  252. "kretprobe_trampoline:\n\t"
  253. "nop\n");
  254. }
  255. /*
  256. * Called when we hit the probe point at kretprobe_trampoline
  257. */
  258. int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
  259. {
  260. struct kretprobe_instance *ri = NULL;
  261. struct hlist_head *head, empty_rp;
  262. struct hlist_node *tmp;
  263. unsigned long flags, orig_ret_address = 0;
  264. unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
  265. INIT_HLIST_HEAD(&empty_rp);
  266. kretprobe_hash_lock(current, &head, &flags);
  267. /*
  268. * It is possible to have multiple instances associated with a given
  269. * task either because an multiple functions in the call path
  270. * have a return probe installed on them, and/or more then one return
  271. * return probe was registered for a target function.
  272. *
  273. * We can handle this because:
  274. * - instances are always inserted at the head of the list
  275. * - when multiple return probes are registered for the same
  276. * function, the first instance's ret_addr will point to the
  277. * real return address, and all the rest will point to
  278. * kretprobe_trampoline
  279. */
  280. hlist_for_each_entry_safe(ri, tmp, head, hlist) {
  281. if (ri->task != current)
  282. /* another task is sharing our hash bucket */
  283. continue;
  284. if (ri->rp && ri->rp->handler) {
  285. __this_cpu_write(current_kprobe, &ri->rp->kp);
  286. ri->rp->handler(ri, regs);
  287. __this_cpu_write(current_kprobe, NULL);
  288. }
  289. orig_ret_address = (unsigned long)ri->ret_addr;
  290. recycle_rp_inst(ri, &empty_rp);
  291. if (orig_ret_address != trampoline_address)
  292. /*
  293. * This is the real return address. Any other
  294. * instances associated with this task are for
  295. * other calls deeper on the call stack
  296. */
  297. break;
  298. }
  299. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  300. regs->pc = orig_ret_address;
  301. kretprobe_hash_unlock(current, &flags);
  302. preempt_enable_no_resched();
  303. hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
  304. hlist_del(&ri->hlist);
  305. kfree(ri);
  306. }
  307. return orig_ret_address;
  308. }
  309. static int __kprobes post_kprobe_handler(struct pt_regs *regs)
  310. {
  311. struct kprobe *cur = kprobe_running();
  312. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  313. kprobe_opcode_t *addr = NULL;
  314. struct kprobe *p = NULL;
  315. if (!cur)
  316. return 0;
  317. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  318. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  319. cur->post_handler(cur, regs, 0);
  320. }
  321. p = this_cpu_ptr(&saved_next_opcode);
  322. if (p->addr) {
  323. arch_disarm_kprobe(p);
  324. p->addr = NULL;
  325. p->opcode = 0;
  326. addr = __this_cpu_read(saved_current_opcode.addr);
  327. __this_cpu_write(saved_current_opcode.addr, NULL);
  328. p = get_kprobe(addr);
  329. arch_arm_kprobe(p);
  330. p = this_cpu_ptr(&saved_next_opcode2);
  331. if (p->addr) {
  332. arch_disarm_kprobe(p);
  333. p->addr = NULL;
  334. p->opcode = 0;
  335. }
  336. }
  337. /* Restore back the original saved kprobes variables and continue. */
  338. if (kcb->kprobe_status == KPROBE_REENTER) {
  339. restore_previous_kprobe(kcb);
  340. goto out;
  341. }
  342. reset_current_kprobe();
  343. out:
  344. preempt_enable_no_resched();
  345. return 1;
  346. }
  347. int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  348. {
  349. struct kprobe *cur = kprobe_running();
  350. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  351. const struct exception_table_entry *entry;
  352. switch (kcb->kprobe_status) {
  353. case KPROBE_HIT_SS:
  354. case KPROBE_REENTER:
  355. /*
  356. * We are here because the instruction being single
  357. * stepped caused a page fault. We reset the current
  358. * kprobe, point the pc back to the probe address
  359. * and allow the page fault handler to continue as a
  360. * normal page fault.
  361. */
  362. regs->pc = (unsigned long)cur->addr;
  363. if (kcb->kprobe_status == KPROBE_REENTER)
  364. restore_previous_kprobe(kcb);
  365. else
  366. reset_current_kprobe();
  367. preempt_enable_no_resched();
  368. break;
  369. case KPROBE_HIT_ACTIVE:
  370. case KPROBE_HIT_SSDONE:
  371. /*
  372. * We increment the nmissed count for accounting,
  373. * we can also use npre/npostfault count for accounting
  374. * these specific fault cases.
  375. */
  376. kprobes_inc_nmissed_count(cur);
  377. /*
  378. * We come here because instructions in the pre/post
  379. * handler caused the page_fault, this could happen
  380. * if handler tries to access user space by
  381. * copy_from_user(), get_user() etc. Let the
  382. * user-specified handler try to fix it first.
  383. */
  384. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  385. return 1;
  386. /*
  387. * In case the user-specified fault handler returned
  388. * zero, try to fix up.
  389. */
  390. if ((entry = search_exception_tables(regs->pc)) != NULL) {
  391. regs->pc = entry->fixup;
  392. return 1;
  393. }
  394. /*
  395. * fixup_exception() could not handle it,
  396. * Let do_page_fault() fix it.
  397. */
  398. break;
  399. default:
  400. break;
  401. }
  402. return 0;
  403. }
  404. /*
  405. * Wrapper routine to for handling exceptions.
  406. */
  407. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  408. unsigned long val, void *data)
  409. {
  410. struct kprobe *p = NULL;
  411. struct die_args *args = (struct die_args *)data;
  412. int ret = NOTIFY_DONE;
  413. kprobe_opcode_t *addr = NULL;
  414. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  415. addr = (kprobe_opcode_t *) (args->regs->pc);
  416. if (val == DIE_TRAP) {
  417. if (!kprobe_running()) {
  418. if (kprobe_handler(args->regs)) {
  419. ret = NOTIFY_STOP;
  420. } else {
  421. /* Not a kprobe trap */
  422. ret = NOTIFY_DONE;
  423. }
  424. } else {
  425. p = get_kprobe(addr);
  426. if ((kcb->kprobe_status == KPROBE_HIT_SS) ||
  427. (kcb->kprobe_status == KPROBE_REENTER)) {
  428. if (post_kprobe_handler(args->regs))
  429. ret = NOTIFY_STOP;
  430. } else {
  431. if (kprobe_handler(args->regs)) {
  432. ret = NOTIFY_STOP;
  433. } else {
  434. p = __this_cpu_read(current_kprobe);
  435. if (p->break_handler &&
  436. p->break_handler(p, args->regs))
  437. ret = NOTIFY_STOP;
  438. }
  439. }
  440. }
  441. }
  442. return ret;
  443. }
  444. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  445. {
  446. struct jprobe *jp = container_of(p, struct jprobe, kp);
  447. unsigned long addr;
  448. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  449. kcb->jprobe_saved_regs = *regs;
  450. kcb->jprobe_saved_r15 = regs->regs[15];
  451. addr = kcb->jprobe_saved_r15;
  452. /*
  453. * TBD: As Linus pointed out, gcc assumes that the callee
  454. * owns the argument space and could overwrite it, e.g.
  455. * tailcall optimization. So, to be absolutely safe
  456. * we also save and restore enough stack bytes to cover
  457. * the argument area.
  458. */
  459. memcpy(kcb->jprobes_stack, (kprobe_opcode_t *) addr,
  460. MIN_STACK_SIZE(addr));
  461. regs->pc = (unsigned long)(jp->entry);
  462. return 1;
  463. }
  464. void __kprobes jprobe_return(void)
  465. {
  466. asm volatile ("trapa #0x3a\n\t" "jprobe_return_end:\n\t" "nop\n\t");
  467. }
  468. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  469. {
  470. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  471. unsigned long stack_addr = kcb->jprobe_saved_r15;
  472. u8 *addr = (u8 *)regs->pc;
  473. if ((addr >= (u8 *)jprobe_return) &&
  474. (addr <= (u8 *)jprobe_return_end)) {
  475. *regs = kcb->jprobe_saved_regs;
  476. memcpy((kprobe_opcode_t *)stack_addr, kcb->jprobes_stack,
  477. MIN_STACK_SIZE(stack_addr));
  478. kcb->kprobe_status = KPROBE_HIT_SS;
  479. preempt_enable_no_resched();
  480. return 1;
  481. }
  482. return 0;
  483. }
  484. static struct kprobe trampoline_p = {
  485. .addr = (kprobe_opcode_t *)&kretprobe_trampoline,
  486. .pre_handler = trampoline_probe_handler
  487. };
  488. int __init arch_init_kprobes(void)
  489. {
  490. return register_kprobe(&trampoline_p);
  491. }