kprobes.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. p = __get_cpu_var(current_kprobe);
  125. if (p->break_handler && p->break_handler(p, regs))
  126. goto ss_probe;
  127. }
  128. goto no_kprobe;
  129. }
  130. p = get_kprobe(addr);
  131. if (!p) {
  132. if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) {
  133. /*
  134. * The breakpoint instruction was removed right
  135. * after we hit it. Another cpu has removed
  136. * either a probepoint or a debugger breakpoint
  137. * at this address. In either case, no further
  138. * handling of this interrupt is appropriate.
  139. */
  140. ret = 1;
  141. }
  142. /* Not one of ours: let kernel handle it */
  143. goto no_kprobe;
  144. }
  145. set_current_kprobe(p, regs, kcb);
  146. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  147. if (p->pre_handler && p->pre_handler(p, regs))
  148. return 1;
  149. ss_probe:
  150. prepare_singlestep(p, regs, kcb);
  151. kcb->kprobe_status = KPROBE_HIT_SS;
  152. return 1;
  153. no_kprobe:
  154. preempt_enable_no_resched();
  155. return ret;
  156. }
  157. /* If INSN is a relative control transfer instruction,
  158. * return the corrected branch destination value.
  159. *
  160. * The original INSN location was REAL_PC, it actually
  161. * executed at PC and produced destination address NPC.
  162. */
  163. static unsigned long __kprobes relbranch_fixup(u32 insn, unsigned long real_pc,
  164. unsigned long pc,
  165. unsigned long npc)
  166. {
  167. /* Branch not taken, no mods necessary. */
  168. if (npc == pc + 0x4UL)
  169. return real_pc + 0x4UL;
  170. /* The three cases are call, branch w/prediction,
  171. * and traditional branch.
  172. */
  173. if ((insn & 0xc0000000) == 0x40000000 ||
  174. (insn & 0xc1c00000) == 0x00400000 ||
  175. (insn & 0xc1c00000) == 0x00800000) {
  176. /* The instruction did all the work for us
  177. * already, just apply the offset to the correct
  178. * instruction location.
  179. */
  180. return (real_pc + (npc - pc));
  181. }
  182. return real_pc + 0x4UL;
  183. }
  184. /* If INSN is an instruction which writes it's PC location
  185. * into a destination register, fix that up.
  186. */
  187. static void __kprobes retpc_fixup(struct pt_regs *regs, u32 insn,
  188. unsigned long real_pc)
  189. {
  190. unsigned long *slot = NULL;
  191. /* Simplest cast is call, which always uses %o7 */
  192. if ((insn & 0xc0000000) == 0x40000000) {
  193. slot = &regs->u_regs[UREG_I7];
  194. }
  195. /* Jmpl encodes the register inside of the opcode */
  196. if ((insn & 0xc1f80000) == 0x81c00000) {
  197. unsigned long rd = ((insn >> 25) & 0x1f);
  198. if (rd <= 15) {
  199. slot = &regs->u_regs[rd];
  200. } else {
  201. /* Hard case, it goes onto the stack. */
  202. flushw_all();
  203. rd -= 16;
  204. slot = (unsigned long *)
  205. (regs->u_regs[UREG_FP] + STACK_BIAS);
  206. slot += rd;
  207. }
  208. }
  209. if (slot != NULL)
  210. *slot = real_pc;
  211. }
  212. /*
  213. * Called after single-stepping. p->addr is the address of the
  214. * instruction whose first byte has been replaced by the breakpoint
  215. * instruction. To avoid the SMP problems that can occur when we
  216. * temporarily put back the original opcode to single-step, we
  217. * single-stepped a copy of the instruction. The address of this
  218. * copy is p->ainsn.insn.
  219. *
  220. * This function prepares to return from the post-single-step
  221. * breakpoint trap.
  222. */
  223. static void __kprobes resume_execution(struct kprobe *p,
  224. struct pt_regs *regs, struct kprobe_ctlblk *kcb)
  225. {
  226. u32 insn = p->ainsn.insn[0];
  227. regs->tpc = kcb->kprobe_orig_tnpc;
  228. regs->tnpc = relbranch_fixup(insn,
  229. (unsigned long) p->addr,
  230. (unsigned long) &p->ainsn.insn[0],
  231. regs->tnpc);
  232. retpc_fixup(regs, insn, (unsigned long) p->addr);
  233. regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
  234. kcb->kprobe_orig_tstate_pil);
  235. }
  236. static inline int post_kprobe_handler(struct pt_regs *regs)
  237. {
  238. struct kprobe *cur = kprobe_running();
  239. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  240. if (!cur)
  241. return 0;
  242. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  243. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  244. cur->post_handler(cur, regs, 0);
  245. }
  246. resume_execution(cur, regs, kcb);
  247. /*Restore back the original saved kprobes variables and continue. */
  248. if (kcb->kprobe_status == KPROBE_REENTER) {
  249. restore_previous_kprobe(kcb);
  250. goto out;
  251. }
  252. reset_current_kprobe();
  253. out:
  254. preempt_enable_no_resched();
  255. return 1;
  256. }
  257. static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  258. {
  259. struct kprobe *cur = kprobe_running();
  260. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  261. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  262. return 1;
  263. if (kcb->kprobe_status & KPROBE_HIT_SS) {
  264. resume_execution(cur, regs, kcb);
  265. reset_current_kprobe();
  266. preempt_enable_no_resched();
  267. }
  268. return 0;
  269. }
  270. /*
  271. * Wrapper routine to for handling exceptions.
  272. */
  273. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  274. unsigned long val, void *data)
  275. {
  276. struct die_args *args = (struct die_args *)data;
  277. int ret = NOTIFY_DONE;
  278. switch (val) {
  279. case DIE_DEBUG:
  280. if (kprobe_handler(args->regs))
  281. ret = NOTIFY_STOP;
  282. break;
  283. case DIE_DEBUG_2:
  284. if (post_kprobe_handler(args->regs))
  285. ret = NOTIFY_STOP;
  286. break;
  287. case DIE_GPF:
  288. case DIE_PAGE_FAULT:
  289. /* kprobe_running() needs smp_processor_id() */
  290. preempt_disable();
  291. if (kprobe_running() &&
  292. kprobe_fault_handler(args->regs, args->trapnr))
  293. ret = NOTIFY_STOP;
  294. preempt_enable();
  295. break;
  296. default:
  297. break;
  298. }
  299. return ret;
  300. }
  301. asmlinkage void __kprobes kprobe_trap(unsigned long trap_level,
  302. struct pt_regs *regs)
  303. {
  304. BUG_ON(trap_level != 0x170 && trap_level != 0x171);
  305. if (user_mode(regs)) {
  306. local_irq_enable();
  307. bad_trap(regs, trap_level);
  308. return;
  309. }
  310. /* trap_level == 0x170 --> ta 0x70
  311. * trap_level == 0x171 --> ta 0x71
  312. */
  313. if (notify_die((trap_level == 0x170) ? DIE_DEBUG : DIE_DEBUG_2,
  314. (trap_level == 0x170) ? "debug" : "debug_2",
  315. regs, 0, trap_level, SIGTRAP) != NOTIFY_STOP)
  316. bad_trap(regs, trap_level);
  317. }
  318. /* Jprobes support. */
  319. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  320. {
  321. struct jprobe *jp = container_of(p, struct jprobe, kp);
  322. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  323. kcb->jprobe_saved_regs_location = regs;
  324. memcpy(&(kcb->jprobe_saved_regs), regs, sizeof(*regs));
  325. /* Save a whole stack frame, this gets arguments
  326. * pushed onto the stack after using up all the
  327. * arg registers.
  328. */
  329. memcpy(&(kcb->jprobe_saved_stack),
  330. (char *) (regs->u_regs[UREG_FP] + STACK_BIAS),
  331. sizeof(kcb->jprobe_saved_stack));
  332. regs->tpc = (unsigned long) jp->entry;
  333. regs->tnpc = ((unsigned long) jp->entry) + 0x4UL;
  334. regs->tstate |= TSTATE_PIL;
  335. return 1;
  336. }
  337. void __kprobes jprobe_return(void)
  338. {
  339. __asm__ __volatile__(
  340. ".globl jprobe_return_trap_instruction\n"
  341. "jprobe_return_trap_instruction:\n\t"
  342. "ta 0x70");
  343. }
  344. extern void jprobe_return_trap_instruction(void);
  345. extern void __show_regs(struct pt_regs * regs);
  346. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  347. {
  348. u32 *addr = (u32 *) regs->tpc;
  349. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  350. if (addr == (u32 *) jprobe_return_trap_instruction) {
  351. if (kcb->jprobe_saved_regs_location != regs) {
  352. printk("JPROBE: Current regs (%p) does not match "
  353. "saved regs (%p).\n",
  354. regs, kcb->jprobe_saved_regs_location);
  355. printk("JPROBE: Saved registers\n");
  356. __show_regs(kcb->jprobe_saved_regs_location);
  357. printk("JPROBE: Current registers\n");
  358. __show_regs(regs);
  359. BUG();
  360. }
  361. /* Restore old register state. Do pt_regs
  362. * first so that UREG_FP is the original one for
  363. * the stack frame restore.
  364. */
  365. memcpy(regs, &(kcb->jprobe_saved_regs), sizeof(*regs));
  366. memcpy((char *) (regs->u_regs[UREG_FP] + STACK_BIAS),
  367. &(kcb->jprobe_saved_stack),
  368. sizeof(kcb->jprobe_saved_stack));
  369. preempt_enable_no_resched();
  370. return 1;
  371. }
  372. return 0;
  373. }
  374. /* architecture specific initialization */
  375. int arch_init_kprobes(void)
  376. {
  377. return 0;
  378. }