kprobes.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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 {
  289. if (addr->word != breakpoint_insn.word) {
  290. /*
  291. * The breakpoint instruction was removed by
  292. * another cpu right after we hit, no further
  293. * handling of this interrupt is appropriate
  294. */
  295. ret = 1;
  296. goto no_kprobe;
  297. }
  298. p = __this_cpu_read(current_kprobe);
  299. if (p->break_handler && p->break_handler(p, regs))
  300. goto ss_probe;
  301. }
  302. goto no_kprobe;
  303. }
  304. p = get_kprobe(addr);
  305. if (!p) {
  306. if (addr->word != breakpoint_insn.word) {
  307. /*
  308. * The breakpoint instruction was removed right
  309. * after we hit it. Another cpu has removed
  310. * either a probepoint or a debugger breakpoint
  311. * at this address. In either case, no further
  312. * handling of this interrupt is appropriate.
  313. */
  314. ret = 1;
  315. }
  316. /* Not one of ours: let kernel handle it */
  317. goto no_kprobe;
  318. }
  319. set_current_kprobe(p, regs, kcb);
  320. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  321. if (p->pre_handler && p->pre_handler(p, regs)) {
  322. /* handler has already set things up, so skip ss setup */
  323. return 1;
  324. }
  325. ss_probe:
  326. prepare_singlestep(p, regs, kcb);
  327. if (kcb->flags & SKIP_DELAYSLOT) {
  328. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  329. if (p->post_handler)
  330. p->post_handler(p, regs, 0);
  331. resume_execution(p, regs, kcb);
  332. preempt_enable_no_resched();
  333. } else
  334. kcb->kprobe_status = KPROBE_HIT_SS;
  335. return 1;
  336. no_kprobe:
  337. preempt_enable_no_resched();
  338. return ret;
  339. }
  340. static inline int post_kprobe_handler(struct pt_regs *regs)
  341. {
  342. struct kprobe *cur = kprobe_running();
  343. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  344. if (!cur)
  345. return 0;
  346. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  347. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  348. cur->post_handler(cur, regs, 0);
  349. }
  350. resume_execution(cur, regs, kcb);
  351. regs->cp0_status |= kcb->kprobe_saved_SR;
  352. /* Restore back the original saved kprobes variables and continue. */
  353. if (kcb->kprobe_status == KPROBE_REENTER) {
  354. restore_previous_kprobe(kcb);
  355. goto out;
  356. }
  357. reset_current_kprobe();
  358. out:
  359. preempt_enable_no_resched();
  360. return 1;
  361. }
  362. static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  363. {
  364. struct kprobe *cur = kprobe_running();
  365. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  366. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  367. return 1;
  368. if (kcb->kprobe_status & KPROBE_HIT_SS) {
  369. resume_execution(cur, regs, kcb);
  370. regs->cp0_status |= kcb->kprobe_old_SR;
  371. reset_current_kprobe();
  372. preempt_enable_no_resched();
  373. }
  374. return 0;
  375. }
  376. /*
  377. * Wrapper routine for handling exceptions.
  378. */
  379. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  380. unsigned long val, void *data)
  381. {
  382. struct die_args *args = (struct die_args *)data;
  383. int ret = NOTIFY_DONE;
  384. switch (val) {
  385. case DIE_BREAK:
  386. if (kprobe_handler(args->regs))
  387. ret = NOTIFY_STOP;
  388. break;
  389. case DIE_SSTEPBP:
  390. if (post_kprobe_handler(args->regs))
  391. ret = NOTIFY_STOP;
  392. break;
  393. case DIE_PAGE_FAULT:
  394. /* kprobe_running() needs smp_processor_id() */
  395. preempt_disable();
  396. if (kprobe_running()
  397. && kprobe_fault_handler(args->regs, args->trapnr))
  398. ret = NOTIFY_STOP;
  399. preempt_enable();
  400. break;
  401. default:
  402. break;
  403. }
  404. return ret;
  405. }
  406. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  407. {
  408. struct jprobe *jp = container_of(p, struct jprobe, kp);
  409. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  410. kcb->jprobe_saved_regs = *regs;
  411. kcb->jprobe_saved_sp = regs->regs[29];
  412. memcpy(kcb->jprobes_stack, (void *)kcb->jprobe_saved_sp,
  413. MIN_JPROBES_STACK_SIZE(kcb->jprobe_saved_sp));
  414. regs->cp0_epc = (unsigned long)(jp->entry);
  415. return 1;
  416. }
  417. /* Defined in the inline asm below. */
  418. void jprobe_return_end(void);
  419. void __kprobes jprobe_return(void)
  420. {
  421. /* Assembler quirk necessitates this '0,code' business. */
  422. asm volatile(
  423. "break 0,%0\n\t"
  424. ".globl jprobe_return_end\n"
  425. "jprobe_return_end:\n"
  426. : : "n" (BRK_KPROBE_BP) : "memory");
  427. }
  428. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  429. {
  430. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  431. if (regs->cp0_epc >= (unsigned long)jprobe_return &&
  432. regs->cp0_epc <= (unsigned long)jprobe_return_end) {
  433. *regs = kcb->jprobe_saved_regs;
  434. memcpy((void *)kcb->jprobe_saved_sp, kcb->jprobes_stack,
  435. MIN_JPROBES_STACK_SIZE(kcb->jprobe_saved_sp));
  436. preempt_enable_no_resched();
  437. return 1;
  438. }
  439. return 0;
  440. }
  441. /*
  442. * Function return probe trampoline:
  443. * - init_kprobes() establishes a probepoint here
  444. * - When the probed function returns, this probe causes the
  445. * handlers to fire
  446. */
  447. static void __used kretprobe_trampoline_holder(void)
  448. {
  449. asm volatile(
  450. ".set push\n\t"
  451. /* Keep the assembler from reordering and placing JR here. */
  452. ".set noreorder\n\t"
  453. "nop\n\t"
  454. ".global kretprobe_trampoline\n"
  455. "kretprobe_trampoline:\n\t"
  456. "nop\n\t"
  457. ".set pop"
  458. : : : "memory");
  459. }
  460. void kretprobe_trampoline(void);
  461. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  462. struct pt_regs *regs)
  463. {
  464. ri->ret_addr = (kprobe_opcode_t *) regs->regs[31];
  465. /* Replace the return addr with trampoline addr */
  466. regs->regs[31] = (unsigned long)kretprobe_trampoline;
  467. }
  468. /*
  469. * Called when the probe at kretprobe trampoline is hit
  470. */
  471. static int __kprobes trampoline_probe_handler(struct kprobe *p,
  472. struct pt_regs *regs)
  473. {
  474. struct kretprobe_instance *ri = NULL;
  475. struct hlist_head *head, empty_rp;
  476. struct hlist_node *tmp;
  477. unsigned long flags, orig_ret_address = 0;
  478. unsigned long trampoline_address = (unsigned long)kretprobe_trampoline;
  479. INIT_HLIST_HEAD(&empty_rp);
  480. kretprobe_hash_lock(current, &head, &flags);
  481. /*
  482. * It is possible to have multiple instances associated with a given
  483. * task either because an multiple functions in the call path
  484. * have a return probe installed on them, and/or more than one return
  485. * return probe was registered for a target function.
  486. *
  487. * We can handle this because:
  488. * - instances are always inserted at the head of the list
  489. * - when multiple return probes are registered for the same
  490. * function, the first instance's ret_addr will point to the
  491. * real return address, and all the rest will point to
  492. * kretprobe_trampoline
  493. */
  494. hlist_for_each_entry_safe(ri, tmp, head, hlist) {
  495. if (ri->task != current)
  496. /* another task is sharing our hash bucket */
  497. continue;
  498. if (ri->rp && ri->rp->handler)
  499. ri->rp->handler(ri, regs);
  500. orig_ret_address = (unsigned long)ri->ret_addr;
  501. recycle_rp_inst(ri, &empty_rp);
  502. if (orig_ret_address != trampoline_address)
  503. /*
  504. * This is the real return address. Any other
  505. * instances associated with this task are for
  506. * other calls deeper on the call stack
  507. */
  508. break;
  509. }
  510. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  511. instruction_pointer(regs) = orig_ret_address;
  512. reset_current_kprobe();
  513. kretprobe_hash_unlock(current, &flags);
  514. preempt_enable_no_resched();
  515. hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
  516. hlist_del(&ri->hlist);
  517. kfree(ri);
  518. }
  519. /*
  520. * By returning a non-zero value, we are telling
  521. * kprobe_handler() that we don't want the post_handler
  522. * to run (and have re-enabled preemption)
  523. */
  524. return 1;
  525. }
  526. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  527. {
  528. if (p->addr == (kprobe_opcode_t *)kretprobe_trampoline)
  529. return 1;
  530. return 0;
  531. }
  532. static struct kprobe trampoline_p = {
  533. .addr = (kprobe_opcode_t *)kretprobe_trampoline,
  534. .pre_handler = trampoline_probe_handler
  535. };
  536. int __init arch_init_kprobes(void)
  537. {
  538. return register_kprobe(&trampoline_p);
  539. }