/arch/mips/kernel/kprobes.c

http://github.com/mirrors/linux · C · 566 lines · 360 code · 81 blank · 125 comment · 60 complexity · 0abcdd693d1e1ef18e44a398a1241621 MD5 · raw file

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