kprobes.c 10 KB

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