kprobes.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Kernel Probes (KProbes)
  4. * arch/mips/kernel/kprobes.c
  5. *
  6. * Copyright 2006 Sony Corp.
  7. * Copyright 2010 Cavium Networks
  8. *
  9. * Some portions copied from the powerpc version.
  10. *
  11. * Copyright (C) IBM Corporation, 2002, 2004
  12. */
  13. #define pr_fmt(fmt) "kprobes: " fmt
  14. #include <linux/kprobes.h>
  15. #include <linux/preempt.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/kdebug.h>
  18. #include <linux/slab.h>
  19. #include <asm/ptrace.h>
  20. #include <asm/branch.h>
  21. #include <asm/break.h>
  22. #include "probes-common.h"
  23. static const union mips_instruction breakpoint_insn = {
  24. .b_format = {
  25. .opcode = spec_op,
  26. .code = BRK_KPROBE_BP,
  27. .func = break_op
  28. }
  29. };
  30. static const union mips_instruction breakpoint2_insn = {
  31. .b_format = {
  32. .opcode = spec_op,
  33. .code = BRK_KPROBE_SSTEPBP,
  34. .func = break_op
  35. }
  36. };
  37. DEFINE_PER_CPU(struct kprobe *, current_kprobe);
  38. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  39. static int insn_has_delayslot(union mips_instruction insn)
  40. {
  41. return __insn_has_delay_slot(insn);
  42. }
  43. NOKPROBE_SYMBOL(insn_has_delayslot);
  44. /*
  45. * insn_has_ll_or_sc function checks whether instruction is ll or sc
  46. * one; putting breakpoint on top of atomic ll/sc pair is bad idea;
  47. * so we need to prevent it and refuse kprobes insertion for such
  48. * instructions; cannot do much about breakpoint in the middle of
  49. * ll/sc pair; it is upto user to avoid those places
  50. */
  51. static int insn_has_ll_or_sc(union mips_instruction insn)
  52. {
  53. int ret = 0;
  54. switch (insn.i_format.opcode) {
  55. case ll_op:
  56. case lld_op:
  57. case sc_op:
  58. case scd_op:
  59. ret = 1;
  60. break;
  61. default:
  62. break;
  63. }
  64. return ret;
  65. }
  66. NOKPROBE_SYMBOL(insn_has_ll_or_sc);
  67. int arch_prepare_kprobe(struct kprobe *p)
  68. {
  69. union mips_instruction insn;
  70. union mips_instruction prev_insn;
  71. int ret = 0;
  72. insn = p->addr[0];
  73. if (insn_has_ll_or_sc(insn)) {
  74. pr_notice("Kprobes for ll and sc instructions are not supported\n");
  75. ret = -EINVAL;
  76. goto out;
  77. }
  78. if (copy_from_kernel_nofault(&prev_insn, p->addr - 1,
  79. sizeof(mips_instruction)) == 0 &&
  80. insn_has_delayslot(prev_insn)) {
  81. pr_notice("Kprobes for branch delayslot are not supported\n");
  82. ret = -EINVAL;
  83. goto out;
  84. }
  85. if (__insn_is_compact_branch(insn)) {
  86. pr_notice("Kprobes for compact branches are not supported\n");
  87. ret = -EINVAL;
  88. goto out;
  89. }
  90. /* insn: must be on special executable page on mips. */
  91. p->ainsn.insn = get_insn_slot();
  92. if (!p->ainsn.insn) {
  93. ret = -ENOMEM;
  94. goto out;
  95. }
  96. /*
  97. * In the kprobe->ainsn.insn[] array we store the original
  98. * instruction at index zero and a break trap instruction at
  99. * index one.
  100. *
  101. * On MIPS arch if the instruction at probed address is a
  102. * branch instruction, we need to execute the instruction at
  103. * Branch Delayslot (BD) at the time of probe hit. As MIPS also
  104. * doesn't have single stepping support, the BD instruction can
  105. * not be executed in-line and it would be executed on SSOL slot
  106. * using a normal breakpoint instruction in the next slot.
  107. * So, read the instruction and save it for later execution.
  108. */
  109. if (insn_has_delayslot(insn))
  110. memcpy(&p->ainsn.insn[0], p->addr + 1, sizeof(kprobe_opcode_t));
  111. else
  112. memcpy(&p->ainsn.insn[0], p->addr, sizeof(kprobe_opcode_t));
  113. p->ainsn.insn[1] = breakpoint2_insn;
  114. p->opcode = *p->addr;
  115. out:
  116. return ret;
  117. }
  118. NOKPROBE_SYMBOL(arch_prepare_kprobe);
  119. void arch_arm_kprobe(struct kprobe *p)
  120. {
  121. *p->addr = breakpoint_insn;
  122. flush_insn_slot(p);
  123. }
  124. NOKPROBE_SYMBOL(arch_arm_kprobe);
  125. void arch_disarm_kprobe(struct kprobe *p)
  126. {
  127. *p->addr = p->opcode;
  128. flush_insn_slot(p);
  129. }
  130. NOKPROBE_SYMBOL(arch_disarm_kprobe);
  131. void arch_remove_kprobe(struct kprobe *p)
  132. {
  133. if (p->ainsn.insn) {
  134. free_insn_slot(p->ainsn.insn, 0);
  135. p->ainsn.insn = NULL;
  136. }
  137. }
  138. NOKPROBE_SYMBOL(arch_remove_kprobe);
  139. static void save_previous_kprobe(struct kprobe_ctlblk *kcb)
  140. {
  141. kcb->prev_kprobe.kp = kprobe_running();
  142. kcb->prev_kprobe.status = kcb->kprobe_status;
  143. kcb->prev_kprobe.old_SR = kcb->kprobe_old_SR;
  144. kcb->prev_kprobe.saved_SR = kcb->kprobe_saved_SR;
  145. kcb->prev_kprobe.saved_epc = kcb->kprobe_saved_epc;
  146. }
  147. static void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  148. {
  149. __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
  150. kcb->kprobe_status = kcb->prev_kprobe.status;
  151. kcb->kprobe_old_SR = kcb->prev_kprobe.old_SR;
  152. kcb->kprobe_saved_SR = kcb->prev_kprobe.saved_SR;
  153. kcb->kprobe_saved_epc = kcb->prev_kprobe.saved_epc;
  154. }
  155. static void set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  156. struct kprobe_ctlblk *kcb)
  157. {
  158. __this_cpu_write(current_kprobe, p);
  159. kcb->kprobe_saved_SR = kcb->kprobe_old_SR = (regs->cp0_status & ST0_IE);
  160. kcb->kprobe_saved_epc = regs->cp0_epc;
  161. }
  162. /**
  163. * evaluate_branch_instrucion -
  164. *
  165. * Evaluate the branch instruction at probed address during probe hit. The
  166. * result of evaluation would be the updated epc. The insturction in delayslot
  167. * would actually be single stepped using a normal breakpoint) on SSOL slot.
  168. *
  169. * The result is also saved in the kprobe control block for later use,
  170. * in case we need to execute the delayslot instruction. The latter will be
  171. * false for NOP instruction in dealyslot and the branch-likely instructions
  172. * when the branch is taken. And for those cases we set a flag as
  173. * SKIP_DELAYSLOT in the kprobe control block
  174. */
  175. static int evaluate_branch_instruction(struct kprobe *p, struct pt_regs *regs,
  176. struct kprobe_ctlblk *kcb)
  177. {
  178. union mips_instruction insn = p->opcode;
  179. long epc;
  180. int ret = 0;
  181. epc = regs->cp0_epc;
  182. if (epc & 3)
  183. goto unaligned;
  184. if (p->ainsn.insn->word == 0)
  185. kcb->flags |= SKIP_DELAYSLOT;
  186. else
  187. kcb->flags &= ~SKIP_DELAYSLOT;
  188. ret = __compute_return_epc_for_insn(regs, insn);
  189. if (ret < 0)
  190. return ret;
  191. if (ret == BRANCH_LIKELY_TAKEN)
  192. kcb->flags |= SKIP_DELAYSLOT;
  193. kcb->target_epc = regs->cp0_epc;
  194. return 0;
  195. unaligned:
  196. pr_notice("Failed to emulate branch instruction because of unaligned epc - sending SIGBUS to %s.\n", current->comm);
  197. force_sig(SIGBUS);
  198. return -EFAULT;
  199. }
  200. static void prepare_singlestep(struct kprobe *p, struct pt_regs *regs,
  201. struct kprobe_ctlblk *kcb)
  202. {
  203. int ret = 0;
  204. regs->cp0_status &= ~ST0_IE;
  205. /* single step inline if the instruction is a break */
  206. if (p->opcode.word == breakpoint_insn.word ||
  207. p->opcode.word == breakpoint2_insn.word)
  208. regs->cp0_epc = (unsigned long)p->addr;
  209. else if (insn_has_delayslot(p->opcode)) {
  210. ret = evaluate_branch_instruction(p, regs, kcb);
  211. if (ret < 0)
  212. return;
  213. }
  214. regs->cp0_epc = (unsigned long)&p->ainsn.insn[0];
  215. }
  216. /*
  217. * Called after single-stepping. p->addr is the address of the
  218. * instruction whose first byte has been replaced by the "break 0"
  219. * instruction. To avoid the SMP problems that can occur when we
  220. * temporarily put back the original opcode to single-step, we
  221. * single-stepped a copy of the instruction. The address of this
  222. * copy is p->ainsn.insn.
  223. *
  224. * This function prepares to return from the post-single-step
  225. * breakpoint trap. In case of branch instructions, the target
  226. * epc to be restored.
  227. */
  228. static void resume_execution(struct kprobe *p,
  229. struct pt_regs *regs,
  230. struct kprobe_ctlblk *kcb)
  231. {
  232. if (insn_has_delayslot(p->opcode))
  233. regs->cp0_epc = kcb->target_epc;
  234. else {
  235. unsigned long orig_epc = kcb->kprobe_saved_epc;
  236. regs->cp0_epc = orig_epc + 4;
  237. }
  238. }
  239. NOKPROBE_SYMBOL(resume_execution);
  240. static int kprobe_handler(struct pt_regs *regs)
  241. {
  242. struct kprobe *p;
  243. int ret = 0;
  244. kprobe_opcode_t *addr;
  245. struct kprobe_ctlblk *kcb;
  246. addr = (kprobe_opcode_t *) regs->cp0_epc;
  247. /*
  248. * We don't want to be preempted for the entire
  249. * duration of kprobe processing
  250. */
  251. preempt_disable();
  252. kcb = get_kprobe_ctlblk();
  253. /* Check we're not actually recursing */
  254. if (kprobe_running()) {
  255. p = get_kprobe(addr);
  256. if (p) {
  257. if (kcb->kprobe_status == KPROBE_HIT_SS &&
  258. p->ainsn.insn->word == breakpoint_insn.word) {
  259. regs->cp0_status &= ~ST0_IE;
  260. regs->cp0_status |= kcb->kprobe_saved_SR;
  261. goto no_kprobe;
  262. }
  263. /*
  264. * We have reentered the kprobe_handler(), since
  265. * another probe was hit while within the handler.
  266. * We here save the original kprobes variables and
  267. * just single step on the instruction of the new probe
  268. * without calling any user handlers.
  269. */
  270. save_previous_kprobe(kcb);
  271. set_current_kprobe(p, regs, kcb);
  272. kprobes_inc_nmissed_count(p);
  273. prepare_singlestep(p, regs, kcb);
  274. kcb->kprobe_status = KPROBE_REENTER;
  275. if (kcb->flags & SKIP_DELAYSLOT) {
  276. resume_execution(p, regs, kcb);
  277. restore_previous_kprobe(kcb);
  278. preempt_enable_no_resched();
  279. }
  280. return 1;
  281. } else if (addr->word != breakpoint_insn.word) {
  282. /*
  283. * The breakpoint instruction was removed by
  284. * another cpu right after we hit, no further
  285. * handling of this interrupt is appropriate
  286. */
  287. ret = 1;
  288. }
  289. goto no_kprobe;
  290. }
  291. p = get_kprobe(addr);
  292. if (!p) {
  293. if (addr->word != breakpoint_insn.word) {
  294. /*
  295. * The breakpoint instruction was removed right
  296. * after we hit it. Another cpu has removed
  297. * either a probepoint or a debugger breakpoint
  298. * at this address. In either case, no further
  299. * handling of this interrupt is appropriate.
  300. */
  301. ret = 1;
  302. }
  303. /* Not one of ours: let kernel handle it */
  304. goto no_kprobe;
  305. }
  306. set_current_kprobe(p, regs, kcb);
  307. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  308. if (p->pre_handler && p->pre_handler(p, regs)) {
  309. /* handler has already set things up, so skip ss setup */
  310. reset_current_kprobe();
  311. preempt_enable_no_resched();
  312. return 1;
  313. }
  314. prepare_singlestep(p, regs, kcb);
  315. if (kcb->flags & SKIP_DELAYSLOT) {
  316. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  317. if (p->post_handler)
  318. p->post_handler(p, regs, 0);
  319. resume_execution(p, regs, kcb);
  320. preempt_enable_no_resched();
  321. } else
  322. kcb->kprobe_status = KPROBE_HIT_SS;
  323. return 1;
  324. no_kprobe:
  325. preempt_enable_no_resched();
  326. return ret;
  327. }
  328. NOKPROBE_SYMBOL(kprobe_handler);
  329. static inline int post_kprobe_handler(struct pt_regs *regs)
  330. {
  331. struct kprobe *cur = kprobe_running();
  332. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  333. if (!cur)
  334. return 0;
  335. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  336. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  337. cur->post_handler(cur, regs, 0);
  338. }
  339. resume_execution(cur, regs, kcb);
  340. regs->cp0_status |= kcb->kprobe_saved_SR;
  341. /* Restore back the original saved kprobes variables and continue. */
  342. if (kcb->kprobe_status == KPROBE_REENTER) {
  343. restore_previous_kprobe(kcb);
  344. goto out;
  345. }
  346. reset_current_kprobe();
  347. out:
  348. preempt_enable_no_resched();
  349. return 1;
  350. }
  351. int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  352. {
  353. struct kprobe *cur = kprobe_running();
  354. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  355. if (kcb->kprobe_status & KPROBE_HIT_SS) {
  356. resume_execution(cur, regs, kcb);
  357. regs->cp0_status |= kcb->kprobe_old_SR;
  358. reset_current_kprobe();
  359. preempt_enable_no_resched();
  360. }
  361. return 0;
  362. }
  363. /*
  364. * Wrapper routine for handling exceptions.
  365. */
  366. int kprobe_exceptions_notify(struct notifier_block *self,
  367. unsigned long val, void *data)
  368. {
  369. struct die_args *args = (struct die_args *)data;
  370. int ret = NOTIFY_DONE;
  371. switch (val) {
  372. case DIE_BREAK:
  373. if (kprobe_handler(args->regs))
  374. ret = NOTIFY_STOP;
  375. break;
  376. case DIE_SSTEPBP:
  377. if (post_kprobe_handler(args->regs))
  378. ret = NOTIFY_STOP;
  379. break;
  380. case DIE_PAGE_FAULT:
  381. /* kprobe_running() needs smp_processor_id() */
  382. preempt_disable();
  383. if (kprobe_running()
  384. && kprobe_fault_handler(args->regs, args->trapnr))
  385. ret = NOTIFY_STOP;
  386. preempt_enable();
  387. break;
  388. default:
  389. break;
  390. }
  391. return ret;
  392. }
  393. NOKPROBE_SYMBOL(kprobe_exceptions_notify);
  394. /*
  395. * Function return probe trampoline:
  396. * - init_kprobes() establishes a probepoint here
  397. * - When the probed function returns, this probe causes the
  398. * handlers to fire
  399. */
  400. static void __used kretprobe_trampoline_holder(void)
  401. {
  402. asm volatile(
  403. ".set push\n\t"
  404. /* Keep the assembler from reordering and placing JR here. */
  405. ".set noreorder\n\t"
  406. "nop\n\t"
  407. ".global __kretprobe_trampoline\n"
  408. "__kretprobe_trampoline:\n\t"
  409. "nop\n\t"
  410. ".set pop"
  411. : : : "memory");
  412. }
  413. void __kretprobe_trampoline(void);
  414. void arch_prepare_kretprobe(struct kretprobe_instance *ri,
  415. struct pt_regs *regs)
  416. {
  417. ri->ret_addr = (kprobe_opcode_t *) regs->regs[31];
  418. ri->fp = NULL;
  419. /* Replace the return addr with trampoline addr */
  420. regs->regs[31] = (unsigned long)__kretprobe_trampoline;
  421. }
  422. NOKPROBE_SYMBOL(arch_prepare_kretprobe);
  423. /*
  424. * Called when the probe at kretprobe trampoline is hit
  425. */
  426. static int trampoline_probe_handler(struct kprobe *p,
  427. struct pt_regs *regs)
  428. {
  429. instruction_pointer(regs) = __kretprobe_trampoline_handler(regs, NULL);
  430. /*
  431. * By returning a non-zero value, we are telling
  432. * kprobe_handler() that we don't want the post_handler
  433. * to run (and have re-enabled preemption)
  434. */
  435. return 1;
  436. }
  437. NOKPROBE_SYMBOL(trampoline_probe_handler);
  438. int arch_trampoline_kprobe(struct kprobe *p)
  439. {
  440. if (p->addr == (kprobe_opcode_t *)__kretprobe_trampoline)
  441. return 1;
  442. return 0;
  443. }
  444. NOKPROBE_SYMBOL(arch_trampoline_kprobe);
  445. static struct kprobe trampoline_p = {
  446. .addr = (kprobe_opcode_t *)__kretprobe_trampoline,
  447. .pre_handler = trampoline_probe_handler
  448. };
  449. int __init arch_init_kprobes(void)
  450. {
  451. return register_kprobe(&trampoline_p);
  452. }