kprobes.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /* arch/sparc64/kernel/kprobes.c
  2. *
  3. * Copyright (C) 2004 David S. Miller <davem@davemloft.net>
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/kprobes.h>
  7. #include <linux/module.h>
  8. #include <linux/kdebug.h>
  9. #include <asm/signal.h>
  10. #include <asm/cacheflush.h>
  11. #include <asm/uaccess.h>
  12. /* We do not have hardware single-stepping on sparc64.
  13. * So we implement software single-stepping with breakpoint
  14. * traps. The top-level scheme is similar to that used
  15. * in the x86 kprobes implementation.
  16. *
  17. * In the kprobe->ainsn.insn[] array we store the original
  18. * instruction at index zero and a break instruction at
  19. * index one.
  20. *
  21. * When we hit a kprobe we:
  22. * - Run the pre-handler
  23. * - Remember "regs->tnpc" and interrupt level stored in
  24. * "regs->tstate" so we can restore them later
  25. * - Disable PIL interrupts
  26. * - Set regs->tpc to point to kprobe->ainsn.insn[0]
  27. * - Set regs->tnpc to point to kprobe->ainsn.insn[1]
  28. * - Mark that we are actively in a kprobe
  29. *
  30. * At this point we wait for the second breakpoint at
  31. * kprobe->ainsn.insn[1] to hit. When it does we:
  32. * - Run the post-handler
  33. * - Set regs->tpc to "remembered" regs->tnpc stored above,
  34. * restore the PIL interrupt level in "regs->tstate" as well
  35. * - Make any adjustments necessary to regs->tnpc in order
  36. * to handle relative branches correctly. See below.
  37. * - Mark that we are no longer actively in a kprobe.
  38. */
  39. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  40. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  41. struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}};
  42. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  43. {
  44. if ((unsigned long) p->addr & 0x3UL)
  45. return -EILSEQ;
  46. p->ainsn.insn[0] = *p->addr;
  47. flushi(&p->ainsn.insn[0]);
  48. p->ainsn.insn[1] = BREAKPOINT_INSTRUCTION_2;
  49. flushi(&p->ainsn.insn[1]);
  50. p->opcode = *p->addr;
  51. return 0;
  52. }
  53. void __kprobes arch_arm_kprobe(struct kprobe *p)
  54. {
  55. *p->addr = BREAKPOINT_INSTRUCTION;
  56. flushi(p->addr);
  57. }
  58. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  59. {
  60. *p->addr = p->opcode;
  61. flushi(p->addr);
  62. }
  63. static void __kprobes 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 void __kprobes 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 void __kprobes 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 void __kprobes 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;
  103. /*
  104. * We don't want to be preempted for the entire
  105. * duration of kprobe processing
  106. */
  107. preempt_disable();
  108. kcb = get_kprobe_ctlblk();
  109. if (kprobe_running()) {
  110. p = get_kprobe(addr);
  111. if (p) {
  112. if (kcb->kprobe_status == KPROBE_HIT_SS) {
  113. regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
  114. kcb->kprobe_orig_tstate_pil);
  115. goto no_kprobe;
  116. }
  117. /* We have reentered the kprobe_handler(), since
  118. * another probe was hit while within the handler.
  119. * We here save the original kprobes variables and
  120. * just single step on the instruction of the new probe
  121. * without calling any user handlers.
  122. */
  123. save_previous_kprobe(kcb);
  124. set_current_kprobe(p, regs, kcb);
  125. kprobes_inc_nmissed_count(p);
  126. kcb->kprobe_status = KPROBE_REENTER;
  127. prepare_singlestep(p, regs, kcb);
  128. return 1;
  129. } else {
  130. if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) {
  131. /* The breakpoint instruction was removed by
  132. * another cpu right after we hit, no further
  133. * handling of this interrupt is appropriate
  134. */
  135. ret = 1;
  136. goto no_kprobe;
  137. }
  138. p = __get_cpu_var(current_kprobe);
  139. if (p->break_handler && p->break_handler(p, regs))
  140. goto ss_probe;
  141. }
  142. goto no_kprobe;
  143. }
  144. p = get_kprobe(addr);
  145. if (!p) {
  146. if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) {
  147. /*
  148. * The breakpoint instruction was removed right
  149. * after we hit it. Another cpu has removed
  150. * either a probepoint or a debugger breakpoint
  151. * at this address. In either case, no further
  152. * handling of this interrupt is appropriate.
  153. */
  154. ret = 1;
  155. }
  156. /* Not one of ours: let kernel handle it */
  157. goto no_kprobe;
  158. }
  159. set_current_kprobe(p, regs, kcb);
  160. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  161. if (p->pre_handler && p->pre_handler(p, regs))
  162. return 1;
  163. ss_probe:
  164. prepare_singlestep(p, regs, kcb);
  165. kcb->kprobe_status = KPROBE_HIT_SS;
  166. return 1;
  167. no_kprobe:
  168. preempt_enable_no_resched();
  169. return ret;
  170. }
  171. /* If INSN is a relative control transfer instruction,
  172. * return the corrected branch destination value.
  173. *
  174. * regs->tpc and regs->tnpc still hold the values of the
  175. * program counters at the time of trap due to the execution
  176. * of the BREAKPOINT_INSTRUCTION_2 at p->ainsn.insn[1]
  177. *
  178. */
  179. static unsigned long __kprobes relbranch_fixup(u32 insn, struct kprobe *p,
  180. struct pt_regs *regs)
  181. {
  182. unsigned long real_pc = (unsigned long) p->addr;
  183. /* Branch not taken, no mods necessary. */
  184. if (regs->tnpc == regs->tpc + 0x4UL)
  185. return real_pc + 0x8UL;
  186. /* The three cases are call, branch w/prediction,
  187. * and traditional branch.
  188. */
  189. if ((insn & 0xc0000000) == 0x40000000 ||
  190. (insn & 0xc1c00000) == 0x00400000 ||
  191. (insn & 0xc1c00000) == 0x00800000) {
  192. unsigned long ainsn_addr;
  193. ainsn_addr = (unsigned long) &p->ainsn.insn[0];
  194. /* The instruction did all the work for us
  195. * already, just apply the offset to the correct
  196. * instruction location.
  197. */
  198. return (real_pc + (regs->tnpc - ainsn_addr));
  199. }
  200. /* It is jmpl or some other absolute PC modification instruction,
  201. * leave NPC as-is.
  202. */
  203. return regs->tnpc;
  204. }
  205. /* If INSN is an instruction which writes it's PC location
  206. * into a destination register, fix that up.
  207. */
  208. static void __kprobes retpc_fixup(struct pt_regs *regs, u32 insn,
  209. unsigned long real_pc)
  210. {
  211. unsigned long *slot = NULL;
  212. /* Simplest case is 'call', which always uses %o7 */
  213. if ((insn & 0xc0000000) == 0x40000000) {
  214. slot = &regs->u_regs[UREG_I7];
  215. }
  216. /* 'jmpl' encodes the register inside of the opcode */
  217. if ((insn & 0xc1f80000) == 0x81c00000) {
  218. unsigned long rd = ((insn >> 25) & 0x1f);
  219. if (rd <= 15) {
  220. slot = &regs->u_regs[rd];
  221. } else {
  222. /* Hard case, it goes onto the stack. */
  223. flushw_all();
  224. rd -= 16;
  225. slot = (unsigned long *)
  226. (regs->u_regs[UREG_FP] + STACK_BIAS);
  227. slot += rd;
  228. }
  229. }
  230. if (slot != NULL)
  231. *slot = real_pc;
  232. }
  233. /*
  234. * Called after single-stepping. p->addr is the address of the
  235. * instruction which has been replaced by the breakpoint
  236. * instruction. To avoid the SMP problems that can occur when we
  237. * temporarily put back the original opcode to single-step, we
  238. * single-stepped a copy of the instruction. The address of this
  239. * copy is &p->ainsn.insn[0].
  240. *
  241. * This function prepares to return from the post-single-step
  242. * breakpoint trap.
  243. */
  244. static void __kprobes resume_execution(struct kprobe *p,
  245. struct pt_regs *regs, struct kprobe_ctlblk *kcb)
  246. {
  247. u32 insn = p->ainsn.insn[0];
  248. regs->tnpc = relbranch_fixup(insn, p, regs);
  249. /* This assignment must occur after relbranch_fixup() */
  250. regs->tpc = kcb->kprobe_orig_tnpc;
  251. retpc_fixup(regs, insn, (unsigned long) p->addr);
  252. regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
  253. kcb->kprobe_orig_tstate_pil);
  254. }
  255. static int __kprobes post_kprobe_handler(struct pt_regs *regs)
  256. {
  257. struct kprobe *cur = kprobe_running();
  258. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  259. if (!cur)
  260. return 0;
  261. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  262. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  263. cur->post_handler(cur, regs, 0);
  264. }
  265. resume_execution(cur, regs, kcb);
  266. /*Restore back the original saved kprobes variables and continue. */
  267. if (kcb->kprobe_status == KPROBE_REENTER) {
  268. restore_previous_kprobe(kcb);
  269. goto out;
  270. }
  271. reset_current_kprobe();
  272. out:
  273. preempt_enable_no_resched();
  274. return 1;
  275. }
  276. int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  277. {
  278. struct kprobe *cur = kprobe_running();
  279. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  280. const struct exception_table_entry *entry;
  281. switch(kcb->kprobe_status) {
  282. case KPROBE_HIT_SS:
  283. case KPROBE_REENTER:
  284. /*
  285. * We are here because the instruction being single
  286. * stepped caused a page fault. We reset the current
  287. * kprobe and the tpc points back to the probe address
  288. * and allow the page fault handler to continue as a
  289. * normal page fault.
  290. */
  291. regs->tpc = (unsigned long)cur->addr;
  292. regs->tnpc = kcb->kprobe_orig_tnpc;
  293. regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
  294. kcb->kprobe_orig_tstate_pil);
  295. if (kcb->kprobe_status == KPROBE_REENTER)
  296. restore_previous_kprobe(kcb);
  297. else
  298. reset_current_kprobe();
  299. preempt_enable_no_resched();
  300. break;
  301. case KPROBE_HIT_ACTIVE:
  302. case KPROBE_HIT_SSDONE:
  303. /*
  304. * We increment the nmissed count for accounting,
  305. * we can also use npre/npostfault count for accouting
  306. * these specific fault cases.
  307. */
  308. kprobes_inc_nmissed_count(cur);
  309. /*
  310. * We come here because instructions in the pre/post
  311. * handler caused the page_fault, this could happen
  312. * if handler tries to access user space by
  313. * copy_from_user(), get_user() etc. Let the
  314. * user-specified handler try to fix it first.
  315. */
  316. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  317. return 1;
  318. /*
  319. * In case the user-specified fault handler returned
  320. * zero, try to fix up.
  321. */
  322. entry = search_exception_tables(regs->tpc);
  323. if (entry) {
  324. regs->tpc = entry->fixup;
  325. regs->tnpc = regs->tpc + 4;
  326. return 1;
  327. }
  328. /*
  329. * fixup_exception() could not handle it,
  330. * Let do_page_fault() fix it.
  331. */
  332. break;
  333. default:
  334. break;
  335. }
  336. return 0;
  337. }
  338. /*
  339. * Wrapper routine to for handling exceptions.
  340. */
  341. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  342. unsigned long val, void *data)
  343. {
  344. struct die_args *args = (struct die_args *)data;
  345. int ret = NOTIFY_DONE;
  346. if (args->regs && user_mode(args->regs))
  347. return ret;
  348. switch (val) {
  349. case DIE_DEBUG:
  350. if (kprobe_handler(args->regs))
  351. ret = NOTIFY_STOP;
  352. break;
  353. case DIE_DEBUG_2:
  354. if (post_kprobe_handler(args->regs))
  355. ret = NOTIFY_STOP;
  356. break;
  357. default:
  358. break;
  359. }
  360. return ret;
  361. }
  362. asmlinkage void __kprobes kprobe_trap(unsigned long trap_level,
  363. struct pt_regs *regs)
  364. {
  365. BUG_ON(trap_level != 0x170 && trap_level != 0x171);
  366. if (user_mode(regs)) {
  367. local_irq_enable();
  368. bad_trap(regs, trap_level);
  369. return;
  370. }
  371. /* trap_level == 0x170 --> ta 0x70
  372. * trap_level == 0x171 --> ta 0x71
  373. */
  374. if (notify_die((trap_level == 0x170) ? DIE_DEBUG : DIE_DEBUG_2,
  375. (trap_level == 0x170) ? "debug" : "debug_2",
  376. regs, 0, trap_level, SIGTRAP) != NOTIFY_STOP)
  377. bad_trap(regs, trap_level);
  378. }
  379. /* Jprobes support. */
  380. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  381. {
  382. struct jprobe *jp = container_of(p, struct jprobe, kp);
  383. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  384. memcpy(&(kcb->jprobe_saved_regs), regs, sizeof(*regs));
  385. regs->tpc = (unsigned long) jp->entry;
  386. regs->tnpc = ((unsigned long) jp->entry) + 0x4UL;
  387. regs->tstate |= TSTATE_PIL;
  388. return 1;
  389. }
  390. void __kprobes jprobe_return(void)
  391. {
  392. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  393. register unsigned long orig_fp asm("g1");
  394. orig_fp = kcb->jprobe_saved_regs.u_regs[UREG_FP];
  395. __asm__ __volatile__("\n"
  396. "1: cmp %%sp, %0\n\t"
  397. "blu,a,pt %%xcc, 1b\n\t"
  398. " restore\n\t"
  399. ".globl jprobe_return_trap_instruction\n"
  400. "jprobe_return_trap_instruction:\n\t"
  401. "ta 0x70"
  402. : /* no outputs */
  403. : "r" (orig_fp));
  404. }
  405. extern void jprobe_return_trap_instruction(void);
  406. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  407. {
  408. u32 *addr = (u32 *) regs->tpc;
  409. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  410. if (addr == (u32 *) jprobe_return_trap_instruction) {
  411. memcpy(regs, &(kcb->jprobe_saved_regs), sizeof(*regs));
  412. preempt_enable_no_resched();
  413. return 1;
  414. }
  415. return 0;
  416. }
  417. /* The value stored in the return address register is actually 2
  418. * instructions before where the callee will return to.
  419. * Sequences usually look something like this
  420. *
  421. * call some_function <--- return register points here
  422. * nop <--- call delay slot
  423. * whatever <--- where callee returns to
  424. *
  425. * To keep trampoline_probe_handler logic simpler, we normalize the
  426. * value kept in ri->ret_addr so we don't need to keep adjusting it
  427. * back and forth.
  428. */
  429. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  430. struct pt_regs *regs)
  431. {
  432. ri->ret_addr = (kprobe_opcode_t *)(regs->u_regs[UREG_RETPC] + 8);
  433. /* Replace the return addr with trampoline addr */
  434. regs->u_regs[UREG_RETPC] =
  435. ((unsigned long)kretprobe_trampoline) - 8;
  436. }
  437. /*
  438. * Called when the probe at kretprobe trampoline is hit
  439. */
  440. int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
  441. {
  442. struct kretprobe_instance *ri = NULL;
  443. struct hlist_head *head, empty_rp;
  444. struct hlist_node *node, *tmp;
  445. unsigned long flags, orig_ret_address = 0;
  446. unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
  447. INIT_HLIST_HEAD(&empty_rp);
  448. kretprobe_hash_lock(current, &head, &flags);
  449. /*
  450. * It is possible to have multiple instances associated with a given
  451. * task either because an multiple functions in the call path
  452. * have a return probe installed on them, and/or more than one return
  453. * return probe was registered for a target function.
  454. *
  455. * We can handle this because:
  456. * - instances are always inserted at the head of the list
  457. * - when multiple return probes are registered for the same
  458. * function, the first instance's ret_addr will point to the
  459. * real return address, and all the rest will point to
  460. * kretprobe_trampoline
  461. */
  462. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  463. if (ri->task != current)
  464. /* another task is sharing our hash bucket */
  465. continue;
  466. if (ri->rp && ri->rp->handler)
  467. ri->rp->handler(ri, regs);
  468. orig_ret_address = (unsigned long)ri->ret_addr;
  469. recycle_rp_inst(ri, &empty_rp);
  470. if (orig_ret_address != trampoline_address)
  471. /*
  472. * This is the real return address. Any other
  473. * instances associated with this task are for
  474. * other calls deeper on the call stack
  475. */
  476. break;
  477. }
  478. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  479. regs->tpc = orig_ret_address;
  480. regs->tnpc = orig_ret_address + 4;
  481. reset_current_kprobe();
  482. kretprobe_hash_unlock(current, &flags);
  483. preempt_enable_no_resched();
  484. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  485. hlist_del(&ri->hlist);
  486. kfree(ri);
  487. }
  488. /*
  489. * By returning a non-zero value, we are telling
  490. * kprobe_handler() that we don't want the post_handler
  491. * to run (and have re-enabled preemption)
  492. */
  493. return 1;
  494. }
  495. void kretprobe_trampoline_holder(void)
  496. {
  497. asm volatile(".global kretprobe_trampoline\n"
  498. "kretprobe_trampoline:\n"
  499. "\tnop\n"
  500. "\tnop\n");
  501. }
  502. static struct kprobe trampoline_p = {
  503. .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
  504. .pre_handler = trampoline_probe_handler
  505. };
  506. int __init arch_init_kprobes(void)
  507. {
  508. return register_kprobe(&trampoline_p);
  509. }
  510. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  511. {
  512. if (p->addr == (kprobe_opcode_t *)&kretprobe_trampoline)
  513. return 1;
  514. return 0;
  515. }