kprobes.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /* arch/sparc64/kernel/kprobes.c
  2. *
  3. * Copyright (C) 2004 David S. Miller <davem@davemloft.net>
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/kprobes.h>
  7. #include <linux/module.h>
  8. #include <linux/kdebug.h>
  9. #include <asm/signal.h>
  10. #include <asm/cacheflush.h>
  11. #include <asm/uaccess.h>
  12. /* We do not have hardware single-stepping on sparc64.
  13. * So we implement software single-stepping with breakpoint
  14. * traps. The top-level scheme is similar to that used
  15. * in the x86 kprobes implementation.
  16. *
  17. * In the kprobe->ainsn.insn[] array we store the original
  18. * instruction at index zero and a break instruction at
  19. * index one.
  20. *
  21. * When we hit a kprobe we:
  22. * - Run the pre-handler
  23. * - Remember "regs->tnpc" and interrupt level stored in
  24. * "regs->tstate" so we can restore them later
  25. * - Disable PIL interrupts
  26. * - Set regs->tpc to point to kprobe->ainsn.insn[0]
  27. * - Set regs->tnpc to point to kprobe->ainsn.insn[1]
  28. * - Mark that we are actively in a kprobe
  29. *
  30. * At this point we wait for the second breakpoint at
  31. * kprobe->ainsn.insn[1] to hit. When it does we:
  32. * - Run the post-handler
  33. * - Set regs->tpc to "remembered" regs->tnpc stored above,
  34. * restore the PIL interrupt level in "regs->tstate" as well
  35. * - Make any adjustments necessary to regs->tnpc in order
  36. * to handle relative branches correctly. See below.
  37. * - Mark that we are no longer actively in a kprobe.
  38. */
  39. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  40. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  41. struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}};
  42. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  43. {
  44. p->ainsn.insn[0] = *p->addr;
  45. flushi(&p->ainsn.insn[0]);
  46. p->ainsn.insn[1] = BREAKPOINT_INSTRUCTION_2;
  47. flushi(&p->ainsn.insn[1]);
  48. p->opcode = *p->addr;
  49. return 0;
  50. }
  51. void __kprobes arch_arm_kprobe(struct kprobe *p)
  52. {
  53. *p->addr = BREAKPOINT_INSTRUCTION;
  54. flushi(p->addr);
  55. }
  56. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  57. {
  58. *p->addr = p->opcode;
  59. flushi(p->addr);
  60. }
  61. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  62. {
  63. kcb->prev_kprobe.kp = kprobe_running();
  64. kcb->prev_kprobe.status = kcb->kprobe_status;
  65. kcb->prev_kprobe.orig_tnpc = kcb->kprobe_orig_tnpc;
  66. kcb->prev_kprobe.orig_tstate_pil = kcb->kprobe_orig_tstate_pil;
  67. }
  68. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  69. {
  70. __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
  71. kcb->kprobe_status = kcb->prev_kprobe.status;
  72. kcb->kprobe_orig_tnpc = kcb->prev_kprobe.orig_tnpc;
  73. kcb->kprobe_orig_tstate_pil = kcb->prev_kprobe.orig_tstate_pil;
  74. }
  75. static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  76. struct kprobe_ctlblk *kcb)
  77. {
  78. __get_cpu_var(current_kprobe) = p;
  79. kcb->kprobe_orig_tnpc = regs->tnpc;
  80. kcb->kprobe_orig_tstate_pil = (regs->tstate & TSTATE_PIL);
  81. }
  82. static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs,
  83. struct kprobe_ctlblk *kcb)
  84. {
  85. regs->tstate |= TSTATE_PIL;
  86. /*single step inline, if it a breakpoint instruction*/
  87. if (p->opcode == BREAKPOINT_INSTRUCTION) {
  88. regs->tpc = (unsigned long) p->addr;
  89. regs->tnpc = kcb->kprobe_orig_tnpc;
  90. } else {
  91. regs->tpc = (unsigned long) &p->ainsn.insn[0];
  92. regs->tnpc = (unsigned long) &p->ainsn.insn[1];
  93. }
  94. }
  95. static int __kprobes kprobe_handler(struct pt_regs *regs)
  96. {
  97. struct kprobe *p;
  98. void *addr = (void *) regs->tpc;
  99. int ret = 0;
  100. struct kprobe_ctlblk *kcb;
  101. /*
  102. * We don't want to be preempted for the entire
  103. * duration of kprobe processing
  104. */
  105. preempt_disable();
  106. kcb = get_kprobe_ctlblk();
  107. if (kprobe_running()) {
  108. p = get_kprobe(addr);
  109. if (p) {
  110. if (kcb->kprobe_status == KPROBE_HIT_SS) {
  111. regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
  112. kcb->kprobe_orig_tstate_pil);
  113. goto no_kprobe;
  114. }
  115. /* We have reentered the kprobe_handler(), since
  116. * another probe was hit while within the handler.
  117. * We here save the original kprobes variables and
  118. * just single step on the instruction of the new probe
  119. * without calling any user handlers.
  120. */
  121. save_previous_kprobe(kcb);
  122. set_current_kprobe(p, regs, kcb);
  123. kprobes_inc_nmissed_count(p);
  124. kcb->kprobe_status = KPROBE_REENTER;
  125. prepare_singlestep(p, regs, kcb);
  126. return 1;
  127. } else {
  128. if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) {
  129. /* The breakpoint instruction was removed by
  130. * another cpu right after we hit, no further
  131. * handling of this interrupt is appropriate
  132. */
  133. ret = 1;
  134. goto no_kprobe;
  135. }
  136. p = __get_cpu_var(current_kprobe);
  137. if (p->break_handler && p->break_handler(p, regs))
  138. goto ss_probe;
  139. }
  140. goto no_kprobe;
  141. }
  142. p = get_kprobe(addr);
  143. if (!p) {
  144. if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) {
  145. /*
  146. * The breakpoint instruction was removed right
  147. * after we hit it. Another cpu has removed
  148. * either a probepoint or a debugger breakpoint
  149. * at this address. In either case, no further
  150. * handling of this interrupt is appropriate.
  151. */
  152. ret = 1;
  153. }
  154. /* Not one of ours: let kernel handle it */
  155. goto no_kprobe;
  156. }
  157. set_current_kprobe(p, regs, kcb);
  158. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  159. if (p->pre_handler && p->pre_handler(p, regs))
  160. return 1;
  161. ss_probe:
  162. prepare_singlestep(p, regs, kcb);
  163. kcb->kprobe_status = KPROBE_HIT_SS;
  164. return 1;
  165. no_kprobe:
  166. preempt_enable_no_resched();
  167. return ret;
  168. }
  169. /* If INSN is a relative control transfer instruction,
  170. * return the corrected branch destination value.
  171. *
  172. * regs->tpc and regs->tnpc still hold the values of the
  173. * program counters at the time of trap due to the execution
  174. * of the BREAKPOINT_INSTRUCTION_2 at p->ainsn.insn[1]
  175. *
  176. */
  177. static unsigned long __kprobes relbranch_fixup(u32 insn, struct kprobe *p,
  178. struct pt_regs *regs)
  179. {
  180. unsigned long real_pc = (unsigned long) p->addr;
  181. /* Branch not taken, no mods necessary. */
  182. if (regs->tnpc == regs->tpc + 0x4UL)
  183. return real_pc + 0x8UL;
  184. /* The three cases are call, branch w/prediction,
  185. * and traditional branch.
  186. */
  187. if ((insn & 0xc0000000) == 0x40000000 ||
  188. (insn & 0xc1c00000) == 0x00400000 ||
  189. (insn & 0xc1c00000) == 0x00800000) {
  190. unsigned long ainsn_addr;
  191. ainsn_addr = (unsigned long) &p->ainsn.insn[0];
  192. /* The instruction did all the work for us
  193. * already, just apply the offset to the correct
  194. * instruction location.
  195. */
  196. return (real_pc + (regs->tnpc - ainsn_addr));
  197. }
  198. /* It is jmpl or some other absolute PC modification instruction,
  199. * leave NPC as-is.
  200. */
  201. return regs->tnpc;
  202. }
  203. /* If INSN is an instruction which writes it's PC location
  204. * into a destination register, fix that up.
  205. */
  206. static void __kprobes retpc_fixup(struct pt_regs *regs, u32 insn,
  207. unsigned long real_pc)
  208. {
  209. unsigned long *slot = NULL;
  210. /* Simplest case is 'call', which always uses %o7 */
  211. if ((insn & 0xc0000000) == 0x40000000) {
  212. slot = &regs->u_regs[UREG_I7];
  213. }
  214. /* 'jmpl' encodes the register inside of the opcode */
  215. if ((insn & 0xc1f80000) == 0x81c00000) {
  216. unsigned long rd = ((insn >> 25) & 0x1f);
  217. if (rd <= 15) {
  218. slot = &regs->u_regs[rd];
  219. } else {
  220. /* Hard case, it goes onto the stack. */
  221. flushw_all();
  222. rd -= 16;
  223. slot = (unsigned long *)
  224. (regs->u_regs[UREG_FP] + STACK_BIAS);
  225. slot += rd;
  226. }
  227. }
  228. if (slot != NULL)
  229. *slot = real_pc;
  230. }
  231. /*
  232. * Called after single-stepping. p->addr is the address of the
  233. * instruction which has been replaced by the breakpoint
  234. * instruction. To avoid the SMP problems that can occur when we
  235. * temporarily put back the original opcode to single-step, we
  236. * single-stepped a copy of the instruction. The address of this
  237. * copy is &p->ainsn.insn[0].
  238. *
  239. * This function prepares to return from the post-single-step
  240. * breakpoint trap.
  241. */
  242. static void __kprobes resume_execution(struct kprobe *p,
  243. struct pt_regs *regs, struct kprobe_ctlblk *kcb)
  244. {
  245. u32 insn = p->ainsn.insn[0];
  246. regs->tnpc = relbranch_fixup(insn, p, regs);
  247. /* This assignment must occur after relbranch_fixup() */
  248. regs->tpc = kcb->kprobe_orig_tnpc;
  249. retpc_fixup(regs, insn, (unsigned long) p->addr);
  250. regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
  251. kcb->kprobe_orig_tstate_pil);
  252. }
  253. static int __kprobes post_kprobe_handler(struct pt_regs *regs)
  254. {
  255. struct kprobe *cur = kprobe_running();
  256. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  257. if (!cur)
  258. return 0;
  259. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  260. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  261. cur->post_handler(cur, regs, 0);
  262. }
  263. resume_execution(cur, regs, kcb);
  264. /*Restore back the original saved kprobes variables and continue. */
  265. if (kcb->kprobe_status == KPROBE_REENTER) {
  266. restore_previous_kprobe(kcb);
  267. goto out;
  268. }
  269. reset_current_kprobe();
  270. out:
  271. preempt_enable_no_resched();
  272. return 1;
  273. }
  274. int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  275. {
  276. struct kprobe *cur = kprobe_running();
  277. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  278. const struct exception_table_entry *entry;
  279. switch(kcb->kprobe_status) {
  280. case KPROBE_HIT_SS:
  281. case KPROBE_REENTER:
  282. /*
  283. * We are here because the instruction being single
  284. * stepped caused a page fault. We reset the current
  285. * kprobe and the tpc points back to the probe address
  286. * and allow the page fault handler to continue as a
  287. * normal page fault.
  288. */
  289. regs->tpc = (unsigned long)cur->addr;
  290. regs->tnpc = kcb->kprobe_orig_tnpc;
  291. regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
  292. kcb->kprobe_orig_tstate_pil);
  293. if (kcb->kprobe_status == KPROBE_REENTER)
  294. restore_previous_kprobe(kcb);
  295. else
  296. reset_current_kprobe();
  297. preempt_enable_no_resched();
  298. break;
  299. case KPROBE_HIT_ACTIVE:
  300. case KPROBE_HIT_SSDONE:
  301. /*
  302. * We increment the nmissed count for accounting,
  303. * we can also use npre/npostfault count for accouting
  304. * these specific fault cases.
  305. */
  306. kprobes_inc_nmissed_count(cur);
  307. /*
  308. * We come here because instructions in the pre/post
  309. * handler caused the page_fault, this could happen
  310. * if handler tries to access user space by
  311. * copy_from_user(), get_user() etc. Let the
  312. * user-specified handler try to fix it first.
  313. */
  314. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  315. return 1;
  316. /*
  317. * In case the user-specified fault handler returned
  318. * zero, try to fix up.
  319. */
  320. entry = search_exception_tables(regs->tpc);
  321. if (entry) {
  322. regs->tpc = entry->fixup;
  323. regs->tnpc = regs->tpc + 4;
  324. return 1;
  325. }
  326. /*
  327. * fixup_exception() could not handle it,
  328. * Let do_page_fault() fix it.
  329. */
  330. break;
  331. default:
  332. break;
  333. }
  334. return 0;
  335. }
  336. /*
  337. * Wrapper routine to for handling exceptions.
  338. */
  339. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  340. unsigned long val, void *data)
  341. {
  342. struct die_args *args = (struct die_args *)data;
  343. int ret = NOTIFY_DONE;
  344. if (args->regs && user_mode(args->regs))
  345. return ret;
  346. switch (val) {
  347. case DIE_DEBUG:
  348. if (kprobe_handler(args->regs))
  349. ret = NOTIFY_STOP;
  350. break;
  351. case DIE_DEBUG_2:
  352. if (post_kprobe_handler(args->regs))
  353. ret = NOTIFY_STOP;
  354. break;
  355. default:
  356. break;
  357. }
  358. return ret;
  359. }
  360. asmlinkage void __kprobes kprobe_trap(unsigned long trap_level,
  361. struct pt_regs *regs)
  362. {
  363. BUG_ON(trap_level != 0x170 && trap_level != 0x171);
  364. if (user_mode(regs)) {
  365. local_irq_enable();
  366. bad_trap(regs, trap_level);
  367. return;
  368. }
  369. /* trap_level == 0x170 --> ta 0x70
  370. * trap_level == 0x171 --> ta 0x71
  371. */
  372. if (notify_die((trap_level == 0x170) ? DIE_DEBUG : DIE_DEBUG_2,
  373. (trap_level == 0x170) ? "debug" : "debug_2",
  374. regs, 0, trap_level, SIGTRAP) != NOTIFY_STOP)
  375. bad_trap(regs, trap_level);
  376. }
  377. /* Jprobes support. */
  378. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  379. {
  380. struct jprobe *jp = container_of(p, struct jprobe, kp);
  381. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  382. memcpy(&(kcb->jprobe_saved_regs), regs, sizeof(*regs));
  383. regs->tpc = (unsigned long) jp->entry;
  384. regs->tnpc = ((unsigned long) jp->entry) + 0x4UL;
  385. regs->tstate |= TSTATE_PIL;
  386. return 1;
  387. }
  388. void __kprobes jprobe_return(void)
  389. {
  390. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  391. register unsigned long orig_fp asm("g1");
  392. orig_fp = kcb->jprobe_saved_regs.u_regs[UREG_FP];
  393. __asm__ __volatile__("\n"
  394. "1: cmp %%sp, %0\n\t"
  395. "blu,a,pt %%xcc, 1b\n\t"
  396. " restore\n\t"
  397. ".globl jprobe_return_trap_instruction\n"
  398. "jprobe_return_trap_instruction:\n\t"
  399. "ta 0x70"
  400. : /* no outputs */
  401. : "r" (orig_fp));
  402. }
  403. extern void jprobe_return_trap_instruction(void);
  404. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  405. {
  406. u32 *addr = (u32 *) regs->tpc;
  407. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  408. if (addr == (u32 *) jprobe_return_trap_instruction) {
  409. memcpy(regs, &(kcb->jprobe_saved_regs), sizeof(*regs));
  410. preempt_enable_no_resched();
  411. return 1;
  412. }
  413. return 0;
  414. }
  415. /* The value stored in the return address register is actually 2
  416. * instructions before where the callee will return to.
  417. * Sequences usually look something like this
  418. *
  419. * call some_function <--- return register points here
  420. * nop <--- call delay slot
  421. * whatever <--- where callee returns to
  422. *
  423. * To keep trampoline_probe_handler logic simpler, we normalize the
  424. * value kept in ri->ret_addr so we don't need to keep adjusting it
  425. * back and forth.
  426. */
  427. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  428. struct pt_regs *regs)
  429. {
  430. ri->ret_addr = (kprobe_opcode_t *)(regs->u_regs[UREG_RETPC] + 8);
  431. /* Replace the return addr with trampoline addr */
  432. regs->u_regs[UREG_RETPC] =
  433. ((unsigned long)kretprobe_trampoline) - 8;
  434. }
  435. /*
  436. * Called when the probe at kretprobe trampoline is hit
  437. */
  438. int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
  439. {
  440. struct kretprobe_instance *ri = NULL;
  441. struct hlist_head *head, empty_rp;
  442. struct hlist_node *node, *tmp;
  443. unsigned long flags, orig_ret_address = 0;
  444. unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
  445. INIT_HLIST_HEAD(&empty_rp);
  446. kretprobe_hash_lock(current, &head, &flags);
  447. /*
  448. * It is possible to have multiple instances associated with a given
  449. * task either because an multiple functions in the call path
  450. * have a return probe installed on them, and/or more than one return
  451. * return probe was registered for a target function.
  452. *
  453. * We can handle this because:
  454. * - instances are always inserted at the head of the list
  455. * - when multiple return probes are registered for the same
  456. * function, the first instance's ret_addr will point to the
  457. * real return address, and all the rest will point to
  458. * kretprobe_trampoline
  459. */
  460. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  461. if (ri->task != current)
  462. /* another task is sharing our hash bucket */
  463. continue;
  464. if (ri->rp && ri->rp->handler)
  465. ri->rp->handler(ri, regs);
  466. orig_ret_address = (unsigned long)ri->ret_addr;
  467. recycle_rp_inst(ri, &empty_rp);
  468. if (orig_ret_address != trampoline_address)
  469. /*
  470. * This is the real return address. Any other
  471. * instances associated with this task are for
  472. * other calls deeper on the call stack
  473. */
  474. break;
  475. }
  476. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  477. regs->tpc = orig_ret_address;
  478. regs->tnpc = orig_ret_address + 4;
  479. reset_current_kprobe();
  480. kretprobe_hash_unlock(current, &flags);
  481. preempt_enable_no_resched();
  482. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  483. hlist_del(&ri->hlist);
  484. kfree(ri);
  485. }
  486. /*
  487. * By returning a non-zero value, we are telling
  488. * kprobe_handler() that we don't want the post_handler
  489. * to run (and have re-enabled preemption)
  490. */
  491. return 1;
  492. }
  493. void kretprobe_trampoline_holder(void)
  494. {
  495. asm volatile(".global kretprobe_trampoline\n"
  496. "kretprobe_trampoline:\n"
  497. "\tnop\n"
  498. "\tnop\n");
  499. }
  500. static struct kprobe trampoline_p = {
  501. .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
  502. .pre_handler = trampoline_probe_handler
  503. };
  504. int __init arch_init_kprobes(void)
  505. {
  506. return register_kprobe(&trampoline_p);
  507. }
  508. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  509. {
  510. if (p->addr == (kprobe_opcode_t *)&kretprobe_trampoline)
  511. return 1;
  512. return 0;
  513. }