kprobes.c 16 KB

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