kprobes.c 12 KB

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