kprobes.c 15 KB

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