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. /* 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. static struct kprobe *current_kprobe;
  61. static unsigned long current_kprobe_orig_tnpc;
  62. static unsigned long current_kprobe_orig_tstate_pil;
  63. static unsigned int kprobe_status;
  64. static struct kprobe *kprobe_prev;
  65. static unsigned long kprobe_orig_tnpc_prev;
  66. static unsigned long kprobe_orig_tstate_pil_prev;
  67. static unsigned int kprobe_status_prev;
  68. static inline void save_previous_kprobe(void)
  69. {
  70. kprobe_status_prev = kprobe_status;
  71. kprobe_orig_tnpc_prev = current_kprobe_orig_tnpc;
  72. kprobe_orig_tstate_pil_prev = current_kprobe_orig_tstate_pil;
  73. kprobe_prev = current_kprobe;
  74. }
  75. static inline void restore_previous_kprobe(void)
  76. {
  77. kprobe_status = kprobe_status_prev;
  78. current_kprobe_orig_tnpc = kprobe_orig_tnpc_prev;
  79. current_kprobe_orig_tstate_pil = kprobe_orig_tstate_pil_prev;
  80. current_kprobe = kprobe_prev;
  81. }
  82. static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs)
  83. {
  84. current_kprobe_orig_tnpc = regs->tnpc;
  85. current_kprobe_orig_tstate_pil = (regs->tstate & TSTATE_PIL);
  86. current_kprobe = p;
  87. }
  88. static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  89. {
  90. regs->tstate |= TSTATE_PIL;
  91. /*single step inline, if it a breakpoint instruction*/
  92. if (p->opcode == BREAKPOINT_INSTRUCTION) {
  93. regs->tpc = (unsigned long) p->addr;
  94. regs->tnpc = current_kprobe_orig_tnpc;
  95. } else {
  96. regs->tpc = (unsigned long) &p->ainsn.insn[0];
  97. regs->tnpc = (unsigned long) &p->ainsn.insn[1];
  98. }
  99. }
  100. static int kprobe_handler(struct pt_regs *regs)
  101. {
  102. struct kprobe *p;
  103. void *addr = (void *) regs->tpc;
  104. int ret = 0;
  105. preempt_disable();
  106. if (kprobe_running()) {
  107. /* We *are* holding lock here, so this is safe.
  108. * Disarm the probe we just hit, and ignore it.
  109. */
  110. p = get_kprobe(addr);
  111. if (p) {
  112. if (kprobe_status == KPROBE_HIT_SS) {
  113. regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
  114. current_kprobe_orig_tstate_pil);
  115. unlock_kprobes();
  116. goto no_kprobe;
  117. }
  118. /* We have reentered the kprobe_handler(), since
  119. * another probe was hit while within the handler.
  120. * We here save the original kprobes variables and
  121. * just single step on the instruction of the new probe
  122. * without calling any user handlers.
  123. */
  124. save_previous_kprobe();
  125. set_current_kprobe(p, regs);
  126. p->nmissed++;
  127. kprobe_status = KPROBE_REENTER;
  128. prepare_singlestep(p, regs);
  129. return 1;
  130. } else {
  131. p = current_kprobe;
  132. if (p->break_handler && p->break_handler(p, regs))
  133. goto ss_probe;
  134. }
  135. /* If it's not ours, can't be delete race, (we hold lock). */
  136. goto no_kprobe;
  137. }
  138. lock_kprobes();
  139. p = get_kprobe(addr);
  140. if (!p) {
  141. unlock_kprobes();
  142. if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) {
  143. /*
  144. * The breakpoint instruction was removed right
  145. * after we hit it. Another cpu has removed
  146. * either a probepoint or a debugger breakpoint
  147. * at this address. In either case, no further
  148. * handling of this interrupt is appropriate.
  149. */
  150. ret = 1;
  151. }
  152. /* Not one of ours: let kernel handle it */
  153. goto no_kprobe;
  154. }
  155. set_current_kprobe(p, regs);
  156. kprobe_status = KPROBE_HIT_ACTIVE;
  157. if (p->pre_handler && p->pre_handler(p, regs))
  158. return 1;
  159. ss_probe:
  160. prepare_singlestep(p, regs);
  161. kprobe_status = KPROBE_HIT_SS;
  162. return 1;
  163. no_kprobe:
  164. preempt_enable_no_resched();
  165. return ret;
  166. }
  167. /* If INSN is a relative control transfer instruction,
  168. * return the corrected branch destination value.
  169. *
  170. * The original INSN location was REAL_PC, it actually
  171. * executed at PC and produced destination address NPC.
  172. */
  173. static unsigned long relbranch_fixup(u32 insn, unsigned long real_pc,
  174. unsigned long pc, unsigned long npc)
  175. {
  176. /* Branch not taken, no mods necessary. */
  177. if (npc == pc + 0x4UL)
  178. return real_pc + 0x4UL;
  179. /* The three cases are call, branch w/prediction,
  180. * and traditional branch.
  181. */
  182. if ((insn & 0xc0000000) == 0x40000000 ||
  183. (insn & 0xc1c00000) == 0x00400000 ||
  184. (insn & 0xc1c00000) == 0x00800000) {
  185. /* The instruction did all the work for us
  186. * already, just apply the offset to the correct
  187. * instruction location.
  188. */
  189. return (real_pc + (npc - pc));
  190. }
  191. return real_pc + 0x4UL;
  192. }
  193. /* If INSN is an instruction which writes it's PC location
  194. * into a destination register, fix that up.
  195. */
  196. static void retpc_fixup(struct pt_regs *regs, u32 insn, 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 resume_execution(struct kprobe *p, struct pt_regs *regs)
  232. {
  233. u32 insn = p->ainsn.insn[0];
  234. regs->tpc = current_kprobe_orig_tnpc;
  235. regs->tnpc = relbranch_fixup(insn,
  236. (unsigned long) p->addr,
  237. (unsigned long) &p->ainsn.insn[0],
  238. regs->tnpc);
  239. retpc_fixup(regs, insn, (unsigned long) p->addr);
  240. regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
  241. current_kprobe_orig_tstate_pil);
  242. }
  243. static inline int post_kprobe_handler(struct pt_regs *regs)
  244. {
  245. if (!kprobe_running())
  246. return 0;
  247. if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) {
  248. kprobe_status = KPROBE_HIT_SSDONE;
  249. current_kprobe->post_handler(current_kprobe, regs, 0);
  250. }
  251. resume_execution(current_kprobe, regs);
  252. /*Restore back the original saved kprobes variables and continue. */
  253. if (kprobe_status == KPROBE_REENTER) {
  254. restore_previous_kprobe();
  255. goto out;
  256. }
  257. unlock_kprobes();
  258. out:
  259. preempt_enable_no_resched();
  260. return 1;
  261. }
  262. /* Interrupts disabled, kprobe_lock held. */
  263. static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  264. {
  265. if (current_kprobe->fault_handler
  266. && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
  267. return 1;
  268. if (kprobe_status & KPROBE_HIT_SS) {
  269. resume_execution(current_kprobe, regs);
  270. unlock_kprobes();
  271. preempt_enable_no_resched();
  272. }
  273. return 0;
  274. }
  275. /*
  276. * Wrapper routine to for handling exceptions.
  277. */
  278. int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
  279. void *data)
  280. {
  281. struct die_args *args = (struct die_args *)data;
  282. switch (val) {
  283. case DIE_DEBUG:
  284. if (kprobe_handler(args->regs))
  285. return NOTIFY_STOP;
  286. break;
  287. case DIE_DEBUG_2:
  288. if (post_kprobe_handler(args->regs))
  289. return NOTIFY_STOP;
  290. break;
  291. case DIE_GPF:
  292. if (kprobe_running() &&
  293. kprobe_fault_handler(args->regs, args->trapnr))
  294. return NOTIFY_STOP;
  295. break;
  296. case DIE_PAGE_FAULT:
  297. if (kprobe_running() &&
  298. kprobe_fault_handler(args->regs, args->trapnr))
  299. return NOTIFY_STOP;
  300. break;
  301. default:
  302. break;
  303. }
  304. return NOTIFY_DONE;
  305. }
  306. asmlinkage void kprobe_trap(unsigned long trap_level, struct pt_regs *regs)
  307. {
  308. BUG_ON(trap_level != 0x170 && trap_level != 0x171);
  309. if (user_mode(regs)) {
  310. local_irq_enable();
  311. bad_trap(regs, trap_level);
  312. return;
  313. }
  314. /* trap_level == 0x170 --> ta 0x70
  315. * trap_level == 0x171 --> ta 0x71
  316. */
  317. if (notify_die((trap_level == 0x170) ? DIE_DEBUG : DIE_DEBUG_2,
  318. (trap_level == 0x170) ? "debug" : "debug_2",
  319. regs, 0, trap_level, SIGTRAP) != NOTIFY_STOP)
  320. bad_trap(regs, trap_level);
  321. }
  322. /* Jprobes support. */
  323. static struct pt_regs jprobe_saved_regs;
  324. static struct pt_regs *jprobe_saved_regs_location;
  325. static struct sparc_stackf jprobe_saved_stack;
  326. int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  327. {
  328. struct jprobe *jp = container_of(p, struct jprobe, kp);
  329. jprobe_saved_regs_location = regs;
  330. memcpy(&jprobe_saved_regs, regs, sizeof(*regs));
  331. /* Save a whole stack frame, this gets arguments
  332. * pushed onto the stack after using up all the
  333. * arg registers.
  334. */
  335. memcpy(&jprobe_saved_stack,
  336. (char *) (regs->u_regs[UREG_FP] + STACK_BIAS),
  337. sizeof(jprobe_saved_stack));
  338. regs->tpc = (unsigned long) jp->entry;
  339. regs->tnpc = ((unsigned long) jp->entry) + 0x4UL;
  340. regs->tstate |= TSTATE_PIL;
  341. return 1;
  342. }
  343. void jprobe_return(void)
  344. {
  345. preempt_enable_no_resched();
  346. __asm__ __volatile__(
  347. ".globl jprobe_return_trap_instruction\n"
  348. "jprobe_return_trap_instruction:\n\t"
  349. "ta 0x70");
  350. }
  351. extern void jprobe_return_trap_instruction(void);
  352. extern void __show_regs(struct pt_regs * regs);
  353. int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  354. {
  355. u32 *addr = (u32 *) regs->tpc;
  356. if (addr == (u32 *) jprobe_return_trap_instruction) {
  357. if (jprobe_saved_regs_location != regs) {
  358. printk("JPROBE: Current regs (%p) does not match "
  359. "saved regs (%p).\n",
  360. regs, jprobe_saved_regs_location);
  361. printk("JPROBE: Saved registers\n");
  362. __show_regs(jprobe_saved_regs_location);
  363. printk("JPROBE: Current registers\n");
  364. __show_regs(regs);
  365. BUG();
  366. }
  367. /* Restore old register state. Do pt_regs
  368. * first so that UREG_FP is the original one for
  369. * the stack frame restore.
  370. */
  371. memcpy(regs, &jprobe_saved_regs, sizeof(*regs));
  372. memcpy((char *) (regs->u_regs[UREG_FP] + STACK_BIAS),
  373. &jprobe_saved_stack,
  374. sizeof(jprobe_saved_stack));
  375. return 1;
  376. }
  377. return 0;
  378. }