kprobes.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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/kdebug.h>
  28. #include <linux/slab.h>
  29. #include <asm/ptrace.h>
  30. #include <asm/break.h>
  31. #include <asm/inst.h>
  32. static const union mips_instruction breakpoint_insn = {
  33. .b_format = {
  34. .opcode = spec_op,
  35. .code = BRK_KPROBE_BP,
  36. .func = break_op
  37. }
  38. };
  39. static const union mips_instruction breakpoint2_insn = {
  40. .b_format = {
  41. .opcode = spec_op,
  42. .code = BRK_KPROBE_SSTEPBP,
  43. .func = break_op
  44. }
  45. };
  46. DEFINE_PER_CPU(struct kprobe *, current_kprobe);
  47. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  48. static int __kprobes insn_has_delayslot(union mips_instruction insn)
  49. {
  50. switch (insn.i_format.opcode) {
  51. /*
  52. * This group contains:
  53. * jr and jalr are in r_format format.
  54. */
  55. case spec_op:
  56. switch (insn.r_format.func) {
  57. case jr_op:
  58. case jalr_op:
  59. break;
  60. default:
  61. goto insn_ok;
  62. }
  63. /*
  64. * This group contains:
  65. * bltz_op, bgez_op, bltzl_op, bgezl_op,
  66. * bltzal_op, bgezal_op, bltzall_op, bgezall_op.
  67. */
  68. case bcond_op:
  69. /*
  70. * These are unconditional and in j_format.
  71. */
  72. case jal_op:
  73. case j_op:
  74. /*
  75. * These are conditional and in i_format.
  76. */
  77. case beq_op:
  78. case beql_op:
  79. case bne_op:
  80. case bnel_op:
  81. case blez_op:
  82. case blezl_op:
  83. case bgtz_op:
  84. case bgtzl_op:
  85. /*
  86. * These are the FPA/cp1 branch instructions.
  87. */
  88. case cop1_op:
  89. #ifdef CONFIG_CPU_CAVIUM_OCTEON
  90. case lwc2_op: /* This is bbit0 on Octeon */
  91. case ldc2_op: /* This is bbit032 on Octeon */
  92. case swc2_op: /* This is bbit1 on Octeon */
  93. case sdc2_op: /* This is bbit132 on Octeon */
  94. #endif
  95. return 1;
  96. default:
  97. break;
  98. }
  99. insn_ok:
  100. return 0;
  101. }
  102. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  103. {
  104. union mips_instruction insn;
  105. union mips_instruction prev_insn;
  106. int ret = 0;
  107. prev_insn = p->addr[-1];
  108. insn = p->addr[0];
  109. if (insn_has_delayslot(insn) || insn_has_delayslot(prev_insn)) {
  110. pr_notice("Kprobes for branch and jump instructions are not supported\n");
  111. ret = -EINVAL;
  112. goto out;
  113. }
  114. /* insn: must be on special executable page on mips. */
  115. p->ainsn.insn = get_insn_slot();
  116. if (!p->ainsn.insn) {
  117. ret = -ENOMEM;
  118. goto out;
  119. }
  120. /*
  121. * In the kprobe->ainsn.insn[] array we store the original
  122. * instruction at index zero and a break trap instruction at
  123. * index one.
  124. */
  125. memcpy(&p->ainsn.insn[0], p->addr, sizeof(kprobe_opcode_t));
  126. p->ainsn.insn[1] = breakpoint2_insn;
  127. p->opcode = *p->addr;
  128. out:
  129. return ret;
  130. }
  131. void __kprobes arch_arm_kprobe(struct kprobe *p)
  132. {
  133. *p->addr = breakpoint_insn;
  134. flush_insn_slot(p);
  135. }
  136. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  137. {
  138. *p->addr = p->opcode;
  139. flush_insn_slot(p);
  140. }
  141. void __kprobes arch_remove_kprobe(struct kprobe *p)
  142. {
  143. free_insn_slot(p->ainsn.insn, 0);
  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. __get_cpu_var(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. __get_cpu_var(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. static void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  169. {
  170. regs->cp0_status &= ~ST0_IE;
  171. /* single step inline if the instruction is a break */
  172. if (p->opcode.word == breakpoint_insn.word ||
  173. p->opcode.word == breakpoint2_insn.word)
  174. regs->cp0_epc = (unsigned long)p->addr;
  175. else
  176. regs->cp0_epc = (unsigned long)&p->ainsn.insn[0];
  177. }
  178. static int __kprobes kprobe_handler(struct pt_regs *regs)
  179. {
  180. struct kprobe *p;
  181. int ret = 0;
  182. kprobe_opcode_t *addr;
  183. struct kprobe_ctlblk *kcb;
  184. addr = (kprobe_opcode_t *) regs->cp0_epc;
  185. /*
  186. * We don't want to be preempted for the entire
  187. * duration of kprobe processing
  188. */
  189. preempt_disable();
  190. kcb = get_kprobe_ctlblk();
  191. /* Check we're not actually recursing */
  192. if (kprobe_running()) {
  193. p = get_kprobe(addr);
  194. if (p) {
  195. if (kcb->kprobe_status == KPROBE_HIT_SS &&
  196. p->ainsn.insn->word == breakpoint_insn.word) {
  197. regs->cp0_status &= ~ST0_IE;
  198. regs->cp0_status |= kcb->kprobe_saved_SR;
  199. goto no_kprobe;
  200. }
  201. /*
  202. * We have reentered the kprobe_handler(), since
  203. * another probe was hit while within the handler.
  204. * We here save the original kprobes variables and
  205. * just single step on the instruction of the new probe
  206. * without calling any user handlers.
  207. */
  208. save_previous_kprobe(kcb);
  209. set_current_kprobe(p, regs, kcb);
  210. kprobes_inc_nmissed_count(p);
  211. prepare_singlestep(p, regs);
  212. kcb->kprobe_status = KPROBE_REENTER;
  213. return 1;
  214. } else {
  215. if (addr->word != breakpoint_insn.word) {
  216. /*
  217. * The breakpoint instruction was removed by
  218. * another cpu right after we hit, no further
  219. * handling of this interrupt is appropriate
  220. */
  221. ret = 1;
  222. goto no_kprobe;
  223. }
  224. p = __get_cpu_var(current_kprobe);
  225. if (p->break_handler && p->break_handler(p, regs))
  226. goto ss_probe;
  227. }
  228. goto no_kprobe;
  229. }
  230. p = get_kprobe(addr);
  231. if (!p) {
  232. if (addr->word != breakpoint_insn.word) {
  233. /*
  234. * The breakpoint instruction was removed right
  235. * after we hit it. Another cpu has removed
  236. * either a probepoint or a debugger breakpoint
  237. * at this address. In either case, no further
  238. * handling of this interrupt is appropriate.
  239. */
  240. ret = 1;
  241. }
  242. /* Not one of ours: let kernel handle it */
  243. goto no_kprobe;
  244. }
  245. set_current_kprobe(p, regs, kcb);
  246. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  247. if (p->pre_handler && p->pre_handler(p, regs)) {
  248. /* handler has already set things up, so skip ss setup */
  249. return 1;
  250. }
  251. ss_probe:
  252. prepare_singlestep(p, regs);
  253. kcb->kprobe_status = KPROBE_HIT_SS;
  254. return 1;
  255. no_kprobe:
  256. preempt_enable_no_resched();
  257. return ret;
  258. }
  259. /*
  260. * Called after single-stepping. p->addr is the address of the
  261. * instruction whose first byte has been replaced by the "break 0"
  262. * instruction. To avoid the SMP problems that can occur when we
  263. * temporarily put back the original opcode to single-step, we
  264. * single-stepped a copy of the instruction. The address of this
  265. * copy is p->ainsn.insn.
  266. *
  267. * This function prepares to return from the post-single-step
  268. * breakpoint trap.
  269. */
  270. static void __kprobes resume_execution(struct kprobe *p,
  271. struct pt_regs *regs,
  272. struct kprobe_ctlblk *kcb)
  273. {
  274. unsigned long orig_epc = kcb->kprobe_saved_epc;
  275. regs->cp0_epc = orig_epc + 4;
  276. }
  277. static inline int post_kprobe_handler(struct pt_regs *regs)
  278. {
  279. struct kprobe *cur = kprobe_running();
  280. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  281. if (!cur)
  282. return 0;
  283. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  284. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  285. cur->post_handler(cur, regs, 0);
  286. }
  287. resume_execution(cur, regs, kcb);
  288. regs->cp0_status |= kcb->kprobe_saved_SR;
  289. /* Restore back the original saved kprobes variables and continue. */
  290. if (kcb->kprobe_status == KPROBE_REENTER) {
  291. restore_previous_kprobe(kcb);
  292. goto out;
  293. }
  294. reset_current_kprobe();
  295. out:
  296. preempt_enable_no_resched();
  297. return 1;
  298. }
  299. static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  300. {
  301. struct kprobe *cur = kprobe_running();
  302. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  303. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  304. return 1;
  305. if (kcb->kprobe_status & KPROBE_HIT_SS) {
  306. resume_execution(cur, regs, kcb);
  307. regs->cp0_status |= kcb->kprobe_old_SR;
  308. reset_current_kprobe();
  309. preempt_enable_no_resched();
  310. }
  311. return 0;
  312. }
  313. /*
  314. * Wrapper routine for handling exceptions.
  315. */
  316. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  317. unsigned long val, void *data)
  318. {
  319. struct die_args *args = (struct die_args *)data;
  320. int ret = NOTIFY_DONE;
  321. switch (val) {
  322. case DIE_BREAK:
  323. if (kprobe_handler(args->regs))
  324. ret = NOTIFY_STOP;
  325. break;
  326. case DIE_SSTEPBP:
  327. if (post_kprobe_handler(args->regs))
  328. ret = NOTIFY_STOP;
  329. break;
  330. case DIE_PAGE_FAULT:
  331. /* kprobe_running() needs smp_processor_id() */
  332. preempt_disable();
  333. if (kprobe_running()
  334. && kprobe_fault_handler(args->regs, args->trapnr))
  335. ret = NOTIFY_STOP;
  336. preempt_enable();
  337. break;
  338. default:
  339. break;
  340. }
  341. return ret;
  342. }
  343. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  344. {
  345. struct jprobe *jp = container_of(p, struct jprobe, kp);
  346. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  347. kcb->jprobe_saved_regs = *regs;
  348. kcb->jprobe_saved_sp = regs->regs[29];
  349. memcpy(kcb->jprobes_stack, (void *)kcb->jprobe_saved_sp,
  350. MIN_JPROBES_STACK_SIZE(kcb->jprobe_saved_sp));
  351. regs->cp0_epc = (unsigned long)(jp->entry);
  352. return 1;
  353. }
  354. /* Defined in the inline asm below. */
  355. void jprobe_return_end(void);
  356. void __kprobes jprobe_return(void)
  357. {
  358. /* Assembler quirk necessitates this '0,code' business. */
  359. asm volatile(
  360. "break 0,%0\n\t"
  361. ".globl jprobe_return_end\n"
  362. "jprobe_return_end:\n"
  363. : : "n" (BRK_KPROBE_BP) : "memory");
  364. }
  365. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  366. {
  367. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  368. if (regs->cp0_epc >= (unsigned long)jprobe_return &&
  369. regs->cp0_epc <= (unsigned long)jprobe_return_end) {
  370. *regs = kcb->jprobe_saved_regs;
  371. memcpy((void *)kcb->jprobe_saved_sp, kcb->jprobes_stack,
  372. MIN_JPROBES_STACK_SIZE(kcb->jprobe_saved_sp));
  373. preempt_enable_no_resched();
  374. return 1;
  375. }
  376. return 0;
  377. }
  378. /*
  379. * Function return probe trampoline:
  380. * - init_kprobes() establishes a probepoint here
  381. * - When the probed function returns, this probe causes the
  382. * handlers to fire
  383. */
  384. static void __used kretprobe_trampoline_holder(void)
  385. {
  386. asm volatile(
  387. ".set push\n\t"
  388. /* Keep the assembler from reordering and placing JR here. */
  389. ".set noreorder\n\t"
  390. "nop\n\t"
  391. ".global kretprobe_trampoline\n"
  392. "kretprobe_trampoline:\n\t"
  393. "nop\n\t"
  394. ".set pop"
  395. : : : "memory");
  396. }
  397. void kretprobe_trampoline(void);
  398. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  399. struct pt_regs *regs)
  400. {
  401. ri->ret_addr = (kprobe_opcode_t *) regs->regs[31];
  402. /* Replace the return addr with trampoline addr */
  403. regs->regs[31] = (unsigned long)kretprobe_trampoline;
  404. }
  405. /*
  406. * Called when the probe at kretprobe trampoline is hit
  407. */
  408. static int __kprobes trampoline_probe_handler(struct kprobe *p,
  409. struct pt_regs *regs)
  410. {
  411. struct kretprobe_instance *ri = NULL;
  412. struct hlist_head *head, empty_rp;
  413. struct hlist_node *node, *tmp;
  414. unsigned long flags, orig_ret_address = 0;
  415. unsigned long trampoline_address = (unsigned long)kretprobe_trampoline;
  416. INIT_HLIST_HEAD(&empty_rp);
  417. kretprobe_hash_lock(current, &head, &flags);
  418. /*
  419. * It is possible to have multiple instances associated with a given
  420. * task either because an multiple functions in the call path
  421. * have a return probe installed on them, and/or more than one return
  422. * return probe was registered for a target function.
  423. *
  424. * We can handle this because:
  425. * - instances are always inserted at the head of the list
  426. * - when multiple return probes are registered for the same
  427. * function, the first instance's ret_addr will point to the
  428. * real return address, and all the rest will point to
  429. * kretprobe_trampoline
  430. */
  431. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  432. if (ri->task != current)
  433. /* another task is sharing our hash bucket */
  434. continue;
  435. if (ri->rp && ri->rp->handler)
  436. ri->rp->handler(ri, regs);
  437. orig_ret_address = (unsigned long)ri->ret_addr;
  438. recycle_rp_inst(ri, &empty_rp);
  439. if (orig_ret_address != trampoline_address)
  440. /*
  441. * This is the real return address. Any other
  442. * instances associated with this task are for
  443. * other calls deeper on the call stack
  444. */
  445. break;
  446. }
  447. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  448. instruction_pointer(regs) = orig_ret_address;
  449. reset_current_kprobe();
  450. kretprobe_hash_unlock(current, &flags);
  451. preempt_enable_no_resched();
  452. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  453. hlist_del(&ri->hlist);
  454. kfree(ri);
  455. }
  456. /*
  457. * By returning a non-zero value, we are telling
  458. * kprobe_handler() that we don't want the post_handler
  459. * to run (and have re-enabled preemption)
  460. */
  461. return 1;
  462. }
  463. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  464. {
  465. if (p->addr == (kprobe_opcode_t *)kretprobe_trampoline)
  466. return 1;
  467. return 0;
  468. }
  469. static struct kprobe trampoline_p = {
  470. .addr = (kprobe_opcode_t *)kretprobe_trampoline,
  471. .pre_handler = trampoline_probe_handler
  472. };
  473. int __init arch_init_kprobes(void)
  474. {
  475. return register_kprobe(&trampoline_p);
  476. }