kprobes.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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 <asm/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. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  42. {
  43. p->ainsn.insn[0] = *p->addr;
  44. p->ainsn.insn[1] = BREAKPOINT_INSTRUCTION_2;
  45. p->opcode = *p->addr;
  46. return 0;
  47. }
  48. void __kprobes arch_arm_kprobe(struct kprobe *p)
  49. {
  50. *p->addr = BREAKPOINT_INSTRUCTION;
  51. flushi(p->addr);
  52. }
  53. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  54. {
  55. *p->addr = p->opcode;
  56. flushi(p->addr);
  57. }
  58. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  59. {
  60. kcb->prev_kprobe.kp = kprobe_running();
  61. kcb->prev_kprobe.status = kcb->kprobe_status;
  62. kcb->prev_kprobe.orig_tnpc = kcb->kprobe_orig_tnpc;
  63. kcb->prev_kprobe.orig_tstate_pil = kcb->kprobe_orig_tstate_pil;
  64. }
  65. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  66. {
  67. __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
  68. kcb->kprobe_status = kcb->prev_kprobe.status;
  69. kcb->kprobe_orig_tnpc = kcb->prev_kprobe.orig_tnpc;
  70. kcb->kprobe_orig_tstate_pil = kcb->prev_kprobe.orig_tstate_pil;
  71. }
  72. static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  73. struct kprobe_ctlblk *kcb)
  74. {
  75. __get_cpu_var(current_kprobe) = p;
  76. kcb->kprobe_orig_tnpc = regs->tnpc;
  77. kcb->kprobe_orig_tstate_pil = (regs->tstate & TSTATE_PIL);
  78. }
  79. static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs,
  80. struct kprobe_ctlblk *kcb)
  81. {
  82. regs->tstate |= TSTATE_PIL;
  83. /*single step inline, if it a breakpoint instruction*/
  84. if (p->opcode == BREAKPOINT_INSTRUCTION) {
  85. regs->tpc = (unsigned long) p->addr;
  86. regs->tnpc = kcb->kprobe_orig_tnpc;
  87. } else {
  88. regs->tpc = (unsigned long) &p->ainsn.insn[0];
  89. regs->tnpc = (unsigned long) &p->ainsn.insn[1];
  90. }
  91. }
  92. static int __kprobes kprobe_handler(struct pt_regs *regs)
  93. {
  94. struct kprobe *p;
  95. void *addr = (void *) regs->tpc;
  96. int ret = 0;
  97. struct kprobe_ctlblk *kcb;
  98. /*
  99. * We don't want to be preempted for the entire
  100. * duration of kprobe processing
  101. */
  102. preempt_disable();
  103. kcb = get_kprobe_ctlblk();
  104. if (kprobe_running()) {
  105. p = get_kprobe(addr);
  106. if (p) {
  107. if (kcb->kprobe_status == KPROBE_HIT_SS) {
  108. regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
  109. kcb->kprobe_orig_tstate_pil);
  110. goto no_kprobe;
  111. }
  112. /* We have reentered the kprobe_handler(), since
  113. * another probe was hit while within the handler.
  114. * We here save the original kprobes variables and
  115. * just single step on the instruction of the new probe
  116. * without calling any user handlers.
  117. */
  118. save_previous_kprobe(kcb);
  119. set_current_kprobe(p, regs, kcb);
  120. kprobes_inc_nmissed_count(p);
  121. kcb->kprobe_status = KPROBE_REENTER;
  122. prepare_singlestep(p, regs, kcb);
  123. return 1;
  124. } else {
  125. if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) {
  126. /* The breakpoint instruction was removed by
  127. * another cpu right after we hit, no further
  128. * handling of this interrupt is appropriate
  129. */
  130. ret = 1;
  131. goto no_kprobe;
  132. }
  133. p = __get_cpu_var(current_kprobe);
  134. if (p->break_handler && p->break_handler(p, regs))
  135. goto ss_probe;
  136. }
  137. goto no_kprobe;
  138. }
  139. p = get_kprobe(addr);
  140. if (!p) {
  141. if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) {
  142. /*
  143. * The breakpoint instruction was removed right
  144. * after we hit it. Another cpu has removed
  145. * either a probepoint or a debugger breakpoint
  146. * at this address. In either case, no further
  147. * handling of this interrupt is appropriate.
  148. */
  149. ret = 1;
  150. }
  151. /* Not one of ours: let kernel handle it */
  152. goto no_kprobe;
  153. }
  154. set_current_kprobe(p, regs, kcb);
  155. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  156. if (p->pre_handler && p->pre_handler(p, regs))
  157. return 1;
  158. ss_probe:
  159. prepare_singlestep(p, regs, kcb);
  160. kcb->kprobe_status = KPROBE_HIT_SS;
  161. return 1;
  162. no_kprobe:
  163. preempt_enable_no_resched();
  164. return ret;
  165. }
  166. /* If INSN is a relative control transfer instruction,
  167. * return the corrected branch destination value.
  168. *
  169. * The original INSN location was REAL_PC, it actually
  170. * executed at PC and produced destination address NPC.
  171. */
  172. static unsigned long __kprobes relbranch_fixup(u32 insn, unsigned long real_pc,
  173. unsigned long pc,
  174. unsigned long npc)
  175. {
  176. /* Branch not taken, no mods necessary. */
  177. if (npc == pc + 0x4UL)
  178. return real_pc + 0x4UL;
  179. /* The three cases are call, branch w/prediction,
  180. * and traditional branch.
  181. */
  182. if ((insn & 0xc0000000) == 0x40000000 ||
  183. (insn & 0xc1c00000) == 0x00400000 ||
  184. (insn & 0xc1c00000) == 0x00800000) {
  185. /* The instruction did all the work for us
  186. * already, just apply the offset to the correct
  187. * instruction location.
  188. */
  189. return (real_pc + (npc - pc));
  190. }
  191. return real_pc + 0x4UL;
  192. }
  193. /* If INSN is an instruction which writes it's PC location
  194. * into a destination register, fix that up.
  195. */
  196. static void __kprobes retpc_fixup(struct pt_regs *regs, u32 insn,
  197. unsigned long real_pc)
  198. {
  199. unsigned long *slot = NULL;
  200. /* Simplest cast is call, which always uses %o7 */
  201. if ((insn & 0xc0000000) == 0x40000000) {
  202. slot = &regs->u_regs[UREG_I7];
  203. }
  204. /* Jmpl encodes the register inside of the opcode */
  205. if ((insn & 0xc1f80000) == 0x81c00000) {
  206. unsigned long rd = ((insn >> 25) & 0x1f);
  207. if (rd <= 15) {
  208. slot = &regs->u_regs[rd];
  209. } else {
  210. /* Hard case, it goes onto the stack. */
  211. flushw_all();
  212. rd -= 16;
  213. slot = (unsigned long *)
  214. (regs->u_regs[UREG_FP] + STACK_BIAS);
  215. slot += rd;
  216. }
  217. }
  218. if (slot != NULL)
  219. *slot = real_pc;
  220. }
  221. /*
  222. * Called after single-stepping. p->addr is the address of the
  223. * instruction whose first byte has been replaced by the breakpoint
  224. * instruction. To avoid the SMP problems that can occur when we
  225. * temporarily put back the original opcode to single-step, we
  226. * single-stepped a copy of the instruction. The address of this
  227. * copy is p->ainsn.insn.
  228. *
  229. * This function prepares to return from the post-single-step
  230. * breakpoint trap.
  231. */
  232. static void __kprobes resume_execution(struct kprobe *p,
  233. struct pt_regs *regs, struct kprobe_ctlblk *kcb)
  234. {
  235. u32 insn = p->ainsn.insn[0];
  236. regs->tpc = kcb->kprobe_orig_tnpc;
  237. regs->tnpc = relbranch_fixup(insn,
  238. (unsigned long) p->addr,
  239. (unsigned long) &p->ainsn.insn[0],
  240. regs->tnpc);
  241. retpc_fixup(regs, insn, (unsigned long) p->addr);
  242. regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
  243. kcb->kprobe_orig_tstate_pil);
  244. }
  245. static int __kprobes post_kprobe_handler(struct pt_regs *regs)
  246. {
  247. struct kprobe *cur = kprobe_running();
  248. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  249. if (!cur)
  250. return 0;
  251. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  252. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  253. cur->post_handler(cur, regs, 0);
  254. }
  255. resume_execution(cur, regs, kcb);
  256. /*Restore back the original saved kprobes variables and continue. */
  257. if (kcb->kprobe_status == KPROBE_REENTER) {
  258. restore_previous_kprobe(kcb);
  259. goto out;
  260. }
  261. reset_current_kprobe();
  262. out:
  263. preempt_enable_no_resched();
  264. return 1;
  265. }
  266. static int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  267. {
  268. struct kprobe *cur = kprobe_running();
  269. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  270. const struct exception_table_entry *entry;
  271. switch(kcb->kprobe_status) {
  272. case KPROBE_HIT_SS:
  273. case KPROBE_REENTER:
  274. /*
  275. * We are here because the instruction being single
  276. * stepped caused a page fault. We reset the current
  277. * kprobe and the tpc points back to the probe address
  278. * and allow the page fault handler to continue as a
  279. * normal page fault.
  280. */
  281. regs->tpc = (unsigned long)cur->addr;
  282. regs->tnpc = kcb->kprobe_orig_tnpc;
  283. regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
  284. kcb->kprobe_orig_tstate_pil);
  285. if (kcb->kprobe_status == KPROBE_REENTER)
  286. restore_previous_kprobe(kcb);
  287. else
  288. reset_current_kprobe();
  289. preempt_enable_no_resched();
  290. break;
  291. case KPROBE_HIT_ACTIVE:
  292. case KPROBE_HIT_SSDONE:
  293. /*
  294. * We increment the nmissed count for accounting,
  295. * we can also use npre/npostfault count for accouting
  296. * these specific fault cases.
  297. */
  298. kprobes_inc_nmissed_count(cur);
  299. /*
  300. * We come here because instructions in the pre/post
  301. * handler caused the page_fault, this could happen
  302. * if handler tries to access user space by
  303. * copy_from_user(), get_user() etc. Let the
  304. * user-specified handler try to fix it first.
  305. */
  306. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  307. return 1;
  308. /*
  309. * In case the user-specified fault handler returned
  310. * zero, try to fix up.
  311. */
  312. entry = search_exception_tables(regs->tpc);
  313. if (entry) {
  314. regs->tpc = entry->fixup;
  315. regs->tnpc = regs->tpc + 4;
  316. return 1;
  317. }
  318. /*
  319. * fixup_exception() could not handle it,
  320. * Let do_page_fault() fix it.
  321. */
  322. break;
  323. default:
  324. break;
  325. }
  326. return 0;
  327. }
  328. /*
  329. * Wrapper routine to for handling exceptions.
  330. */
  331. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  332. unsigned long val, void *data)
  333. {
  334. struct die_args *args = (struct die_args *)data;
  335. int ret = NOTIFY_DONE;
  336. if (args->regs && user_mode(args->regs))
  337. return ret;
  338. switch (val) {
  339. case DIE_DEBUG:
  340. if (kprobe_handler(args->regs))
  341. ret = NOTIFY_STOP;
  342. break;
  343. case DIE_DEBUG_2:
  344. if (post_kprobe_handler(args->regs))
  345. ret = NOTIFY_STOP;
  346. break;
  347. case DIE_GPF:
  348. case DIE_PAGE_FAULT:
  349. /* kprobe_running() needs smp_processor_id() */
  350. preempt_disable();
  351. if (kprobe_running() &&
  352. kprobe_fault_handler(args->regs, args->trapnr))
  353. ret = NOTIFY_STOP;
  354. preempt_enable();
  355. break;
  356. default:
  357. break;
  358. }
  359. return ret;
  360. }
  361. asmlinkage void __kprobes kprobe_trap(unsigned long trap_level,
  362. struct pt_regs *regs)
  363. {
  364. BUG_ON(trap_level != 0x170 && trap_level != 0x171);
  365. if (user_mode(regs)) {
  366. local_irq_enable();
  367. bad_trap(regs, trap_level);
  368. return;
  369. }
  370. /* trap_level == 0x170 --> ta 0x70
  371. * trap_level == 0x171 --> ta 0x71
  372. */
  373. if (notify_die((trap_level == 0x170) ? DIE_DEBUG : DIE_DEBUG_2,
  374. (trap_level == 0x170) ? "debug" : "debug_2",
  375. regs, 0, trap_level, SIGTRAP) != NOTIFY_STOP)
  376. bad_trap(regs, trap_level);
  377. }
  378. /* Jprobes support. */
  379. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  380. {
  381. struct jprobe *jp = container_of(p, struct jprobe, kp);
  382. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  383. kcb->jprobe_saved_regs_location = regs;
  384. memcpy(&(kcb->jprobe_saved_regs), regs, sizeof(*regs));
  385. /* Save a whole stack frame, this gets arguments
  386. * pushed onto the stack after using up all the
  387. * arg registers.
  388. */
  389. memcpy(&(kcb->jprobe_saved_stack),
  390. (char *) (regs->u_regs[UREG_FP] + STACK_BIAS),
  391. sizeof(kcb->jprobe_saved_stack));
  392. regs->tpc = (unsigned long) jp->entry;
  393. regs->tnpc = ((unsigned long) jp->entry) + 0x4UL;
  394. regs->tstate |= TSTATE_PIL;
  395. return 1;
  396. }
  397. void __kprobes jprobe_return(void)
  398. {
  399. __asm__ __volatile__(
  400. ".globl jprobe_return_trap_instruction\n"
  401. "jprobe_return_trap_instruction:\n\t"
  402. "ta 0x70");
  403. }
  404. extern void jprobe_return_trap_instruction(void);
  405. extern void __show_regs(struct pt_regs * regs);
  406. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  407. {
  408. u32 *addr = (u32 *) regs->tpc;
  409. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  410. if (addr == (u32 *) jprobe_return_trap_instruction) {
  411. if (kcb->jprobe_saved_regs_location != regs) {
  412. printk("JPROBE: Current regs (%p) does not match "
  413. "saved regs (%p).\n",
  414. regs, kcb->jprobe_saved_regs_location);
  415. printk("JPROBE: Saved registers\n");
  416. __show_regs(kcb->jprobe_saved_regs_location);
  417. printk("JPROBE: Current registers\n");
  418. __show_regs(regs);
  419. BUG();
  420. }
  421. /* Restore old register state. Do pt_regs
  422. * first so that UREG_FP is the original one for
  423. * the stack frame restore.
  424. */
  425. memcpy(regs, &(kcb->jprobe_saved_regs), sizeof(*regs));
  426. memcpy((char *) (regs->u_regs[UREG_FP] + STACK_BIAS),
  427. &(kcb->jprobe_saved_stack),
  428. sizeof(kcb->jprobe_saved_stack));
  429. preempt_enable_no_resched();
  430. return 1;
  431. }
  432. return 0;
  433. }
  434. /* architecture specific initialization */
  435. int arch_init_kprobes(void)
  436. {
  437. return 0;
  438. }