kprobes.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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. /*
  249. * We don't want to be preempted for the entire
  250. * duration of kprobe processing
  251. */
  252. preempt_disable();
  253. kcb = get_kprobe_ctlblk();
  254. /* Check we're not actually recursing */
  255. if (kprobe_running()) {
  256. p = get_kprobe(addr);
  257. if (p) {
  258. kprobe_opcode_t insn = *p->ainsn.insn;
  259. if (kcb->kprobe_status == KPROBE_HIT_SS &&
  260. is_trap(insn)) {
  261. /* Turn off 'trace' bits */
  262. regs->msr &= ~MSR_SINGLESTEP;
  263. regs->msr |= kcb->kprobe_saved_msr;
  264. goto no_kprobe;
  265. }
  266. /* We have reentered the kprobe_handler(), since
  267. * another probe was hit while within the handler.
  268. * We here save the original kprobes variables and
  269. * just single step on the instruction of the new probe
  270. * without calling any user handlers.
  271. */
  272. save_previous_kprobe(kcb);
  273. set_current_kprobe(p, regs, kcb);
  274. kprobes_inc_nmissed_count(p);
  275. kcb->kprobe_status = KPROBE_REENTER;
  276. if (p->ainsn.boostable >= 0) {
  277. ret = try_to_emulate(p, regs);
  278. if (ret > 0) {
  279. restore_previous_kprobe(kcb);
  280. preempt_enable_no_resched();
  281. return 1;
  282. }
  283. }
  284. prepare_singlestep(p, regs);
  285. return 1;
  286. } else if (*addr != BREAKPOINT_INSTRUCTION) {
  287. /* If trap variant, then it belongs not to us */
  288. kprobe_opcode_t cur_insn = *addr;
  289. if (is_trap(cur_insn))
  290. goto no_kprobe;
  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. }
  297. goto no_kprobe;
  298. }
  299. p = get_kprobe(addr);
  300. if (!p) {
  301. if (*addr != BREAKPOINT_INSTRUCTION) {
  302. /*
  303. * PowerPC has multiple variants of the "trap"
  304. * instruction. If the current instruction is a
  305. * trap variant, it could belong to someone else
  306. */
  307. kprobe_opcode_t cur_insn = *addr;
  308. if (is_trap(cur_insn))
  309. goto no_kprobe;
  310. /*
  311. * The breakpoint instruction was removed right
  312. * after we hit it. Another cpu has removed
  313. * either a probepoint or a debugger breakpoint
  314. * at this address. In either case, no further
  315. * handling of this interrupt is appropriate.
  316. */
  317. ret = 1;
  318. }
  319. /* Not one of ours: let kernel handle it */
  320. goto no_kprobe;
  321. }
  322. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  323. set_current_kprobe(p, regs, kcb);
  324. if (p->pre_handler && p->pre_handler(p, regs)) {
  325. /* handler changed execution path, so skip ss setup */
  326. reset_current_kprobe();
  327. preempt_enable_no_resched();
  328. return 1;
  329. }
  330. if (p->ainsn.boostable >= 0) {
  331. ret = try_to_emulate(p, regs);
  332. if (ret > 0) {
  333. if (p->post_handler)
  334. p->post_handler(p, regs, 0);
  335. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  336. reset_current_kprobe();
  337. preempt_enable_no_resched();
  338. return 1;
  339. }
  340. }
  341. prepare_singlestep(p, regs);
  342. kcb->kprobe_status = KPROBE_HIT_SS;
  343. return 1;
  344. no_kprobe:
  345. preempt_enable_no_resched();
  346. return ret;
  347. }
  348. NOKPROBE_SYMBOL(kprobe_handler);
  349. /*
  350. * Function return probe trampoline:
  351. * - init_kprobes() establishes a probepoint here
  352. * - When the probed function returns, this probe
  353. * causes the handlers to fire
  354. */
  355. asm(".global kretprobe_trampoline\n"
  356. ".type kretprobe_trampoline, @function\n"
  357. "kretprobe_trampoline:\n"
  358. "nop\n"
  359. "blr\n"
  360. ".size kretprobe_trampoline, .-kretprobe_trampoline\n");
  361. /*
  362. * Called when the probe at kretprobe trampoline is hit
  363. */
  364. static int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
  365. {
  366. struct kretprobe_instance *ri = NULL;
  367. struct hlist_head *head, empty_rp;
  368. struct hlist_node *tmp;
  369. unsigned long flags, orig_ret_address = 0;
  370. unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
  371. INIT_HLIST_HEAD(&empty_rp);
  372. kretprobe_hash_lock(current, &head, &flags);
  373. /*
  374. * It is possible to have multiple instances associated with a given
  375. * task either because an multiple functions in the call path
  376. * have a return probe installed on them, and/or more than one return
  377. * return probe was registered for a target function.
  378. *
  379. * We can handle this because:
  380. * - instances are always inserted at the head of the list
  381. * - when multiple return probes are registered for the same
  382. * function, the first instance's ret_addr will point to the
  383. * real return address, and all the rest will point to
  384. * kretprobe_trampoline
  385. */
  386. hlist_for_each_entry_safe(ri, tmp, head, hlist) {
  387. if (ri->task != current)
  388. /* another task is sharing our hash bucket */
  389. continue;
  390. if (ri->rp && ri->rp->handler)
  391. ri->rp->handler(ri, regs);
  392. orig_ret_address = (unsigned long)ri->ret_addr;
  393. recycle_rp_inst(ri, &empty_rp);
  394. if (orig_ret_address != trampoline_address)
  395. /*
  396. * This is the real return address. Any other
  397. * instances associated with this task are for
  398. * other calls deeper on the call stack
  399. */
  400. break;
  401. }
  402. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  403. /*
  404. * We get here through one of two paths:
  405. * 1. by taking a trap -> kprobe_handler() -> here
  406. * 2. by optprobe branch -> optimized_callback() -> opt_pre_handler() -> here
  407. *
  408. * When going back through (1), we need regs->nip to be setup properly
  409. * as it is used to determine the return address from the trap.
  410. * For (2), since nip is not honoured with optprobes, we instead setup
  411. * the link register properly so that the subsequent 'blr' in
  412. * kretprobe_trampoline jumps back to the right instruction.
  413. *
  414. * For nip, we should set the address to the previous instruction since
  415. * we end up emulating it in kprobe_handler(), which increments the nip
  416. * again.
  417. */
  418. regs->nip = orig_ret_address - 4;
  419. regs->link = orig_ret_address;
  420. kretprobe_hash_unlock(current, &flags);
  421. hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
  422. hlist_del(&ri->hlist);
  423. kfree(ri);
  424. }
  425. return 0;
  426. }
  427. NOKPROBE_SYMBOL(trampoline_probe_handler);
  428. /*
  429. * Called after single-stepping. p->addr is the address of the
  430. * instruction whose first byte has been replaced by the "breakpoint"
  431. * instruction. To avoid the SMP problems that can occur when we
  432. * temporarily put back the original opcode to single-step, we
  433. * single-stepped a copy of the instruction. The address of this
  434. * copy is p->ainsn.insn.
  435. */
  436. int kprobe_post_handler(struct pt_regs *regs)
  437. {
  438. struct kprobe *cur = kprobe_running();
  439. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  440. if (!cur || user_mode(regs))
  441. return 0;
  442. /* make sure we got here for instruction we have a kprobe on */
  443. if (((unsigned long)cur->ainsn.insn + 4) != regs->nip)
  444. return 0;
  445. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  446. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  447. cur->post_handler(cur, regs, 0);
  448. }
  449. /* Adjust nip to after the single-stepped instruction */
  450. regs->nip = (unsigned long)cur->addr + 4;
  451. regs->msr |= kcb->kprobe_saved_msr;
  452. /*Restore back the original saved kprobes variables and continue. */
  453. if (kcb->kprobe_status == KPROBE_REENTER) {
  454. restore_previous_kprobe(kcb);
  455. goto out;
  456. }
  457. reset_current_kprobe();
  458. out:
  459. preempt_enable_no_resched();
  460. /*
  461. * if somebody else is singlestepping across a probe point, msr
  462. * will have DE/SE set, in which case, continue the remaining processing
  463. * of do_debug, as if this is not a probe hit.
  464. */
  465. if (regs->msr & MSR_SINGLESTEP)
  466. return 0;
  467. return 1;
  468. }
  469. NOKPROBE_SYMBOL(kprobe_post_handler);
  470. int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  471. {
  472. struct kprobe *cur = kprobe_running();
  473. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  474. const struct exception_table_entry *entry;
  475. switch(kcb->kprobe_status) {
  476. case KPROBE_HIT_SS:
  477. case KPROBE_REENTER:
  478. /*
  479. * We are here because the instruction being single
  480. * stepped caused a page fault. We reset the current
  481. * kprobe and the nip points back to the probe address
  482. * and allow the page fault handler to continue as a
  483. * normal page fault.
  484. */
  485. regs->nip = (unsigned long)cur->addr;
  486. regs->msr &= ~MSR_SINGLESTEP; /* Turn off 'trace' bits */
  487. regs->msr |= kcb->kprobe_saved_msr;
  488. if (kcb->kprobe_status == KPROBE_REENTER)
  489. restore_previous_kprobe(kcb);
  490. else
  491. reset_current_kprobe();
  492. preempt_enable_no_resched();
  493. break;
  494. case KPROBE_HIT_ACTIVE:
  495. case KPROBE_HIT_SSDONE:
  496. /*
  497. * We increment the nmissed count for accounting,
  498. * we can also use npre/npostfault count for accounting
  499. * these specific fault cases.
  500. */
  501. kprobes_inc_nmissed_count(cur);
  502. /*
  503. * We come here because instructions in the pre/post
  504. * handler caused the page_fault, this could happen
  505. * if handler tries to access user space by
  506. * copy_from_user(), get_user() etc. Let the
  507. * user-specified handler try to fix it first.
  508. */
  509. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  510. return 1;
  511. /*
  512. * In case the user-specified fault handler returned
  513. * zero, try to fix up.
  514. */
  515. if ((entry = search_exception_tables(regs->nip)) != NULL) {
  516. regs->nip = extable_fixup(entry);
  517. return 1;
  518. }
  519. /*
  520. * fixup_exception() could not handle it,
  521. * Let do_page_fault() fix it.
  522. */
  523. break;
  524. default:
  525. break;
  526. }
  527. return 0;
  528. }
  529. NOKPROBE_SYMBOL(kprobe_fault_handler);
  530. unsigned long arch_deref_entry_point(void *entry)
  531. {
  532. #ifdef PPC64_ELF_ABI_v1
  533. if (!kernel_text_address((unsigned long)entry))
  534. return ppc_global_function_entry(entry);
  535. else
  536. #endif
  537. return (unsigned long)entry;
  538. }
  539. NOKPROBE_SYMBOL(arch_deref_entry_point);
  540. static struct kprobe trampoline_p = {
  541. .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
  542. .pre_handler = trampoline_probe_handler
  543. };
  544. int __init arch_init_kprobes(void)
  545. {
  546. return register_kprobe(&trampoline_p);
  547. }
  548. int arch_trampoline_kprobe(struct kprobe *p)
  549. {
  550. if (p->addr == (kprobe_opcode_t *)&kretprobe_trampoline)
  551. return 1;
  552. return 0;
  553. }
  554. NOKPROBE_SYMBOL(arch_trampoline_kprobe);