kprobes.c 16 KB

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