kprobes.c 10 KB

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