kprobes.c 11 KB

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