kprobes.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. * Kernel Probes (KProbes)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright (C) IBM Corporation, 2002, 2004
  19. *
  20. * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
  21. * Probes initial implementation ( includes contributions from
  22. * Rusty Russell).
  23. * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
  24. * interface to access function arguments.
  25. * 2004-Nov Ananth N Mavinakayanahalli <ananth@in.ibm.com> kprobes port
  26. * for PPC64
  27. */
  28. #include <linux/kprobes.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/preempt.h>
  31. #include <linux/extable.h>
  32. #include <linux/kdebug.h>
  33. #include <linux/slab.h>
  34. #include <asm/code-patching.h>
  35. #include <asm/cacheflush.h>
  36. #include <asm/sstep.h>
  37. #include <asm/sections.h>
  38. #include <linux/uaccess.h>
  39. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  40. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  41. struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}};
  42. bool arch_within_kprobe_blacklist(unsigned long addr)
  43. {
  44. return (addr >= (unsigned long)__kprobes_text_start &&
  45. addr < (unsigned long)__kprobes_text_end) ||
  46. (addr >= (unsigned long)_stext &&
  47. addr < (unsigned long)__head_end);
  48. }
  49. kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset)
  50. {
  51. kprobe_opcode_t *addr = NULL;
  52. #ifdef PPC64_ELF_ABI_v2
  53. /* PPC64 ABIv2 needs local entry point */
  54. addr = (kprobe_opcode_t *)kallsyms_lookup_name(name);
  55. if (addr && !offset) {
  56. #ifdef CONFIG_KPROBES_ON_FTRACE
  57. unsigned long faddr;
  58. /*
  59. * Per livepatch.h, ftrace location is always within the first
  60. * 16 bytes of a function on powerpc with -mprofile-kernel.
  61. */
  62. faddr = ftrace_location_range((unsigned long)addr,
  63. (unsigned long)addr + 16);
  64. if (faddr)
  65. addr = (kprobe_opcode_t *)faddr;
  66. else
  67. #endif
  68. addr = (kprobe_opcode_t *)ppc_function_entry(addr);
  69. }
  70. #elif defined(PPC64_ELF_ABI_v1)
  71. /*
  72. * 64bit powerpc ABIv1 uses function descriptors:
  73. * - Check for the dot variant of the symbol first.
  74. * - If that fails, try looking up the symbol provided.
  75. *
  76. * This ensures we always get to the actual symbol and not
  77. * the descriptor.
  78. *
  79. * Also handle <module:symbol> format.
  80. */
  81. char dot_name[MODULE_NAME_LEN + 1 + KSYM_NAME_LEN];
  82. bool dot_appended = false;
  83. const char *c;
  84. ssize_t ret = 0;
  85. int len = 0;
  86. if ((c = strnchr(name, MODULE_NAME_LEN, ':')) != NULL) {
  87. c++;
  88. len = c - name;
  89. memcpy(dot_name, name, len);
  90. } else
  91. c = name;
  92. if (*c != '\0' && *c != '.') {
  93. dot_name[len++] = '.';
  94. dot_appended = true;
  95. }
  96. ret = strscpy(dot_name + len, c, KSYM_NAME_LEN);
  97. if (ret > 0)
  98. addr = (kprobe_opcode_t *)kallsyms_lookup_name(dot_name);
  99. /* Fallback to the original non-dot symbol lookup */
  100. if (!addr && dot_appended)
  101. addr = (kprobe_opcode_t *)kallsyms_lookup_name(name);
  102. #else
  103. addr = (kprobe_opcode_t *)kallsyms_lookup_name(name);
  104. #endif
  105. return addr;
  106. }
  107. int arch_prepare_kprobe(struct kprobe *p)
  108. {
  109. int ret = 0;
  110. kprobe_opcode_t insn = *p->addr;
  111. if ((unsigned long)p->addr & 0x03) {
  112. printk("Attempt to register kprobe at an unaligned address\n");
  113. ret = -EINVAL;
  114. } else if (IS_MTMSRD(insn) || IS_RFID(insn) || IS_RFI(insn)) {
  115. printk("Cannot register a kprobe on rfi/rfid or mtmsr[d]\n");
  116. ret = -EINVAL;
  117. }
  118. /* insn must be on a special executable page on ppc64. This is
  119. * not explicitly required on ppc32 (right now), but it doesn't hurt */
  120. if (!ret) {
  121. p->ainsn.insn = get_insn_slot();
  122. if (!p->ainsn.insn)
  123. ret = -ENOMEM;
  124. }
  125. if (!ret) {
  126. memcpy(p->ainsn.insn, p->addr,
  127. MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
  128. p->opcode = *p->addr;
  129. flush_icache_range((unsigned long)p->ainsn.insn,
  130. (unsigned long)p->ainsn.insn + sizeof(kprobe_opcode_t));
  131. }
  132. p->ainsn.boostable = 0;
  133. return ret;
  134. }
  135. NOKPROBE_SYMBOL(arch_prepare_kprobe);
  136. void arch_arm_kprobe(struct kprobe *p)
  137. {
  138. patch_instruction(p->addr, BREAKPOINT_INSTRUCTION);
  139. }
  140. NOKPROBE_SYMBOL(arch_arm_kprobe);
  141. void arch_disarm_kprobe(struct kprobe *p)
  142. {
  143. patch_instruction(p->addr, p->opcode);
  144. }
  145. NOKPROBE_SYMBOL(arch_disarm_kprobe);
  146. void arch_remove_kprobe(struct kprobe *p)
  147. {
  148. if (p->ainsn.insn) {
  149. free_insn_slot(p->ainsn.insn, 0);
  150. p->ainsn.insn = NULL;
  151. }
  152. }
  153. NOKPROBE_SYMBOL(arch_remove_kprobe);
  154. static nokprobe_inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  155. {
  156. enable_single_step(regs);
  157. /*
  158. * On powerpc we should single step on the original
  159. * instruction even if the probed insn is a trap
  160. * variant as values in regs could play a part in
  161. * if the trap is taken or not
  162. */
  163. regs->nip = (unsigned long)p->ainsn.insn;
  164. }
  165. static nokprobe_inline void save_previous_kprobe(struct kprobe_ctlblk *kcb)
  166. {
  167. kcb->prev_kprobe.kp = kprobe_running();
  168. kcb->prev_kprobe.status = kcb->kprobe_status;
  169. kcb->prev_kprobe.saved_msr = kcb->kprobe_saved_msr;
  170. }
  171. static nokprobe_inline void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  172. {
  173. __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
  174. kcb->kprobe_status = kcb->prev_kprobe.status;
  175. kcb->kprobe_saved_msr = kcb->prev_kprobe.saved_msr;
  176. }
  177. static nokprobe_inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  178. struct kprobe_ctlblk *kcb)
  179. {
  180. __this_cpu_write(current_kprobe, p);
  181. kcb->kprobe_saved_msr = regs->msr;
  182. }
  183. bool arch_kprobe_on_func_entry(unsigned long offset)
  184. {
  185. #ifdef PPC64_ELF_ABI_v2
  186. #ifdef CONFIG_KPROBES_ON_FTRACE
  187. return offset <= 16;
  188. #else
  189. return offset <= 8;
  190. #endif
  191. #else
  192. return !offset;
  193. #endif
  194. }
  195. void arch_prepare_kretprobe(struct kretprobe_instance *ri, struct pt_regs *regs)
  196. {
  197. ri->ret_addr = (kprobe_opcode_t *)regs->link;
  198. /* Replace the return addr with trampoline addr */
  199. regs->link = (unsigned long)kretprobe_trampoline;
  200. }
  201. NOKPROBE_SYMBOL(arch_prepare_kretprobe);
  202. static int try_to_emulate(struct kprobe *p, struct pt_regs *regs)
  203. {
  204. int ret;
  205. unsigned int insn = *p->ainsn.insn;
  206. /* regs->nip is also adjusted if emulate_step returns 1 */
  207. ret = emulate_step(regs, insn);
  208. if (ret > 0) {
  209. /*
  210. * Once this instruction has been boosted
  211. * successfully, set the boostable flag
  212. */
  213. if (unlikely(p->ainsn.boostable == 0))
  214. p->ainsn.boostable = 1;
  215. } else if (ret < 0) {
  216. /*
  217. * We don't allow kprobes on mtmsr(d)/rfi(d), etc.
  218. * So, we should never get here... but, its still
  219. * good to catch them, just in case...
  220. */
  221. printk("Can't step on instruction %x\n", insn);
  222. BUG();
  223. } else {
  224. /*
  225. * If we haven't previously emulated this instruction, then it
  226. * can't be boosted. Note it down so we don't try to do so again.
  227. *
  228. * If, however, we had emulated this instruction in the past,
  229. * then this is just an error with the current run (for
  230. * instance, exceptions due to a load/store). We return 0 so
  231. * that this is now single-stepped, but continue to try
  232. * emulating it in subsequent probe hits.
  233. */
  234. if (unlikely(p->ainsn.boostable != 1))
  235. p->ainsn.boostable = -1;
  236. }
  237. return ret;
  238. }
  239. NOKPROBE_SYMBOL(try_to_emulate);
  240. int kprobe_handler(struct pt_regs *regs)
  241. {
  242. struct kprobe *p;
  243. int ret = 0;
  244. unsigned int *addr = (unsigned int *)regs->nip;
  245. struct kprobe_ctlblk *kcb;
  246. if (user_mode(regs))
  247. return 0;
  248. if (!(regs->msr & MSR_IR) || !(regs->msr & MSR_DR))
  249. return 0;
  250. /*
  251. * We don't want to be preempted for the entire
  252. * duration of kprobe processing
  253. */
  254. preempt_disable();
  255. kcb = get_kprobe_ctlblk();
  256. /* Check we're not actually recursing */
  257. if (kprobe_running()) {
  258. p = get_kprobe(addr);
  259. if (p) {
  260. kprobe_opcode_t insn = *p->ainsn.insn;
  261. if (kcb->kprobe_status == KPROBE_HIT_SS &&
  262. is_trap(insn)) {
  263. /* Turn off 'trace' bits */
  264. regs->msr &= ~MSR_SINGLESTEP;
  265. regs->msr |= kcb->kprobe_saved_msr;
  266. goto no_kprobe;
  267. }
  268. /* We have reentered the kprobe_handler(), since
  269. * another probe was hit while within the handler.
  270. * We here save the original kprobes variables and
  271. * just single step on the instruction of the new probe
  272. * without calling any user handlers.
  273. */
  274. save_previous_kprobe(kcb);
  275. set_current_kprobe(p, regs, kcb);
  276. kprobes_inc_nmissed_count(p);
  277. kcb->kprobe_status = KPROBE_REENTER;
  278. if (p->ainsn.boostable >= 0) {
  279. ret = try_to_emulate(p, regs);
  280. if (ret > 0) {
  281. restore_previous_kprobe(kcb);
  282. preempt_enable_no_resched();
  283. return 1;
  284. }
  285. }
  286. prepare_singlestep(p, regs);
  287. return 1;
  288. } else if (*addr != BREAKPOINT_INSTRUCTION) {
  289. /* If trap variant, then it belongs not to us */
  290. kprobe_opcode_t cur_insn = *addr;
  291. if (is_trap(cur_insn))
  292. goto no_kprobe;
  293. /* The breakpoint instruction was removed by
  294. * another cpu right after we hit, no further
  295. * handling of this interrupt is appropriate
  296. */
  297. ret = 1;
  298. }
  299. goto no_kprobe;
  300. }
  301. p = get_kprobe(addr);
  302. if (!p) {
  303. if (*addr != BREAKPOINT_INSTRUCTION) {
  304. /*
  305. * PowerPC has multiple variants of the "trap"
  306. * instruction. If the current instruction is a
  307. * trap variant, it could belong to someone else
  308. */
  309. kprobe_opcode_t cur_insn = *addr;
  310. if (is_trap(cur_insn))
  311. goto no_kprobe;
  312. /*
  313. * The breakpoint instruction was removed right
  314. * after we hit it. Another cpu has removed
  315. * either a probepoint or a debugger breakpoint
  316. * at this address. In either case, no further
  317. * handling of this interrupt is appropriate.
  318. */
  319. ret = 1;
  320. }
  321. /* Not one of ours: let kernel handle it */
  322. goto no_kprobe;
  323. }
  324. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  325. set_current_kprobe(p, regs, kcb);
  326. if (p->pre_handler && p->pre_handler(p, regs)) {
  327. /* handler changed execution path, so skip ss setup */
  328. reset_current_kprobe();
  329. preempt_enable_no_resched();
  330. return 1;
  331. }
  332. if (p->ainsn.boostable >= 0) {
  333. ret = try_to_emulate(p, regs);
  334. if (ret > 0) {
  335. if (p->post_handler)
  336. p->post_handler(p, regs, 0);
  337. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  338. reset_current_kprobe();
  339. preempt_enable_no_resched();
  340. return 1;
  341. }
  342. }
  343. prepare_singlestep(p, regs);
  344. kcb->kprobe_status = KPROBE_HIT_SS;
  345. return 1;
  346. no_kprobe:
  347. preempt_enable_no_resched();
  348. return ret;
  349. }
  350. NOKPROBE_SYMBOL(kprobe_handler);
  351. /*
  352. * Function return probe trampoline:
  353. * - init_kprobes() establishes a probepoint here
  354. * - When the probed function returns, this probe
  355. * causes the handlers to fire
  356. */
  357. asm(".global kretprobe_trampoline\n"
  358. ".type kretprobe_trampoline, @function\n"
  359. "kretprobe_trampoline:\n"
  360. "nop\n"
  361. "blr\n"
  362. ".size kretprobe_trampoline, .-kretprobe_trampoline\n");
  363. /*
  364. * Called when the probe at kretprobe trampoline is hit
  365. */
  366. static int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
  367. {
  368. struct kretprobe_instance *ri = NULL;
  369. struct hlist_head *head, empty_rp;
  370. struct hlist_node *tmp;
  371. unsigned long flags, orig_ret_address = 0;
  372. unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
  373. INIT_HLIST_HEAD(&empty_rp);
  374. kretprobe_hash_lock(current, &head, &flags);
  375. /*
  376. * It is possible to have multiple instances associated with a given
  377. * task either because an multiple functions in the call path
  378. * have a return probe installed on them, and/or more than one return
  379. * return probe was registered for a target function.
  380. *
  381. * We can handle this because:
  382. * - instances are always inserted at the head of the list
  383. * - when multiple return probes are registered for the same
  384. * function, the first instance's ret_addr will point to the
  385. * real return address, and all the rest will point to
  386. * kretprobe_trampoline
  387. */
  388. hlist_for_each_entry_safe(ri, tmp, head, hlist) {
  389. if (ri->task != current)
  390. /* another task is sharing our hash bucket */
  391. continue;
  392. if (ri->rp && ri->rp->handler)
  393. ri->rp->handler(ri, regs);
  394. orig_ret_address = (unsigned long)ri->ret_addr;
  395. recycle_rp_inst(ri, &empty_rp);
  396. if (orig_ret_address != trampoline_address)
  397. /*
  398. * This is the real return address. Any other
  399. * instances associated with this task are for
  400. * other calls deeper on the call stack
  401. */
  402. break;
  403. }
  404. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  405. /*
  406. * We get here through one of two paths:
  407. * 1. by taking a trap -> kprobe_handler() -> here
  408. * 2. by optprobe branch -> optimized_callback() -> opt_pre_handler() -> here
  409. *
  410. * When going back through (1), we need regs->nip to be setup properly
  411. * as it is used to determine the return address from the trap.
  412. * For (2), since nip is not honoured with optprobes, we instead setup
  413. * the link register properly so that the subsequent 'blr' in
  414. * kretprobe_trampoline jumps back to the right instruction.
  415. *
  416. * For nip, we should set the address to the previous instruction since
  417. * we end up emulating it in kprobe_handler(), which increments the nip
  418. * again.
  419. */
  420. regs->nip = orig_ret_address - 4;
  421. regs->link = orig_ret_address;
  422. kretprobe_hash_unlock(current, &flags);
  423. hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
  424. hlist_del(&ri->hlist);
  425. kfree(ri);
  426. }
  427. return 0;
  428. }
  429. NOKPROBE_SYMBOL(trampoline_probe_handler);
  430. /*
  431. * Called after single-stepping. p->addr is the address of the
  432. * instruction whose first byte has been replaced by the "breakpoint"
  433. * instruction. To avoid the SMP problems that can occur when we
  434. * temporarily put back the original opcode to single-step, we
  435. * single-stepped a copy of the instruction. The address of this
  436. * copy is p->ainsn.insn.
  437. */
  438. int kprobe_post_handler(struct pt_regs *regs)
  439. {
  440. struct kprobe *cur = kprobe_running();
  441. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  442. if (!cur || user_mode(regs))
  443. return 0;
  444. /* make sure we got here for instruction we have a kprobe on */
  445. if (((unsigned long)cur->ainsn.insn + 4) != regs->nip)
  446. return 0;
  447. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  448. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  449. cur->post_handler(cur, regs, 0);
  450. }
  451. /* Adjust nip to after the single-stepped instruction */
  452. regs->nip = (unsigned long)cur->addr + 4;
  453. regs->msr |= kcb->kprobe_saved_msr;
  454. /*Restore back the original saved kprobes variables and continue. */
  455. if (kcb->kprobe_status == KPROBE_REENTER) {
  456. restore_previous_kprobe(kcb);
  457. goto out;
  458. }
  459. reset_current_kprobe();
  460. out:
  461. preempt_enable_no_resched();
  462. /*
  463. * if somebody else is singlestepping across a probe point, msr
  464. * will have DE/SE set, in which case, continue the remaining processing
  465. * of do_debug, as if this is not a probe hit.
  466. */
  467. if (regs->msr & MSR_SINGLESTEP)
  468. return 0;
  469. return 1;
  470. }
  471. NOKPROBE_SYMBOL(kprobe_post_handler);
  472. int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  473. {
  474. struct kprobe *cur = kprobe_running();
  475. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  476. const struct exception_table_entry *entry;
  477. switch(kcb->kprobe_status) {
  478. case KPROBE_HIT_SS:
  479. case KPROBE_REENTER:
  480. /*
  481. * We are here because the instruction being single
  482. * stepped caused a page fault. We reset the current
  483. * kprobe and the nip points back to the probe address
  484. * and allow the page fault handler to continue as a
  485. * normal page fault.
  486. */
  487. regs->nip = (unsigned long)cur->addr;
  488. regs->msr &= ~MSR_SINGLESTEP; /* Turn off 'trace' bits */
  489. regs->msr |= kcb->kprobe_saved_msr;
  490. if (kcb->kprobe_status == KPROBE_REENTER)
  491. restore_previous_kprobe(kcb);
  492. else
  493. reset_current_kprobe();
  494. preempt_enable_no_resched();
  495. break;
  496. case KPROBE_HIT_ACTIVE:
  497. case KPROBE_HIT_SSDONE:
  498. /*
  499. * We increment the nmissed count for accounting,
  500. * we can also use npre/npostfault count for accounting
  501. * these specific fault cases.
  502. */
  503. kprobes_inc_nmissed_count(cur);
  504. /*
  505. * We come here because instructions in the pre/post
  506. * handler caused the page_fault, this could happen
  507. * if handler tries to access user space by
  508. * copy_from_user(), get_user() etc. Let the
  509. * user-specified handler try to fix it first.
  510. */
  511. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  512. return 1;
  513. /*
  514. * In case the user-specified fault handler returned
  515. * zero, try to fix up.
  516. */
  517. if ((entry = search_exception_tables(regs->nip)) != NULL) {
  518. regs->nip = extable_fixup(entry);
  519. return 1;
  520. }
  521. /*
  522. * fixup_exception() could not handle it,
  523. * Let do_page_fault() fix it.
  524. */
  525. break;
  526. default:
  527. break;
  528. }
  529. return 0;
  530. }
  531. NOKPROBE_SYMBOL(kprobe_fault_handler);
  532. unsigned long arch_deref_entry_point(void *entry)
  533. {
  534. #ifdef PPC64_ELF_ABI_v1
  535. if (!kernel_text_address((unsigned long)entry))
  536. return ppc_global_function_entry(entry);
  537. else
  538. #endif
  539. return (unsigned long)entry;
  540. }
  541. NOKPROBE_SYMBOL(arch_deref_entry_point);
  542. static struct kprobe trampoline_p = {
  543. .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
  544. .pre_handler = trampoline_probe_handler
  545. };
  546. int __init arch_init_kprobes(void)
  547. {
  548. return register_kprobe(&trampoline_p);
  549. }
  550. int arch_trampoline_kprobe(struct kprobe *p)
  551. {
  552. if (p->addr == (kprobe_opcode_t *)&kretprobe_trampoline)
  553. return 1;
  554. return 0;
  555. }
  556. NOKPROBE_SYMBOL(arch_trampoline_kprobe);