kprobes.c 13 KB

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