kprobes.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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. int is_current_kprobe_addr(unsigned long addr)
  43. {
  44. struct kprobe *p = kprobe_running();
  45. return (p && (unsigned long)p->addr == addr) ? 1 : 0;
  46. }
  47. bool arch_within_kprobe_blacklist(unsigned long addr)
  48. {
  49. return (addr >= (unsigned long)__kprobes_text_start &&
  50. addr < (unsigned long)__kprobes_text_end) ||
  51. (addr >= (unsigned long)_stext &&
  52. addr < (unsigned long)__head_end);
  53. }
  54. kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset)
  55. {
  56. kprobe_opcode_t *addr;
  57. #ifdef PPC64_ELF_ABI_v2
  58. /* PPC64 ABIv2 needs local entry point */
  59. addr = (kprobe_opcode_t *)kallsyms_lookup_name(name);
  60. if (addr && !offset) {
  61. #ifdef CONFIG_KPROBES_ON_FTRACE
  62. unsigned long faddr;
  63. /*
  64. * Per livepatch.h, ftrace location is always within the first
  65. * 16 bytes of a function on powerpc with -mprofile-kernel.
  66. */
  67. faddr = ftrace_location_range((unsigned long)addr,
  68. (unsigned long)addr + 16);
  69. if (faddr)
  70. addr = (kprobe_opcode_t *)faddr;
  71. else
  72. #endif
  73. addr = (kprobe_opcode_t *)ppc_function_entry(addr);
  74. }
  75. #elif defined(PPC64_ELF_ABI_v1)
  76. /*
  77. * 64bit powerpc ABIv1 uses function descriptors:
  78. * - Check for the dot variant of the symbol first.
  79. * - If that fails, try looking up the symbol provided.
  80. *
  81. * This ensures we always get to the actual symbol and not
  82. * the descriptor.
  83. *
  84. * Also handle <module:symbol> format.
  85. */
  86. char dot_name[MODULE_NAME_LEN + 1 + KSYM_NAME_LEN];
  87. const char *modsym;
  88. bool dot_appended = false;
  89. if ((modsym = strchr(name, ':')) != NULL) {
  90. modsym++;
  91. if (*modsym != '\0' && *modsym != '.') {
  92. /* Convert to <module:.symbol> */
  93. strncpy(dot_name, name, modsym - name);
  94. dot_name[modsym - name] = '.';
  95. dot_name[modsym - name + 1] = '\0';
  96. strncat(dot_name, modsym,
  97. sizeof(dot_name) - (modsym - name) - 2);
  98. dot_appended = true;
  99. } else {
  100. dot_name[0] = '\0';
  101. strncat(dot_name, name, sizeof(dot_name) - 1);
  102. }
  103. } else if (name[0] != '.') {
  104. dot_name[0] = '.';
  105. dot_name[1] = '\0';
  106. strncat(dot_name, name, KSYM_NAME_LEN - 2);
  107. dot_appended = true;
  108. } else {
  109. dot_name[0] = '\0';
  110. strncat(dot_name, name, KSYM_NAME_LEN - 1);
  111. }
  112. addr = (kprobe_opcode_t *)kallsyms_lookup_name(dot_name);
  113. if (!addr && dot_appended) {
  114. /* Let's try the original non-dot symbol lookup */
  115. addr = (kprobe_opcode_t *)kallsyms_lookup_name(name);
  116. }
  117. #else
  118. addr = (kprobe_opcode_t *)kallsyms_lookup_name(name);
  119. #endif
  120. return addr;
  121. }
  122. int arch_prepare_kprobe(struct kprobe *p)
  123. {
  124. int ret = 0;
  125. kprobe_opcode_t insn = *p->addr;
  126. if ((unsigned long)p->addr & 0x03) {
  127. printk("Attempt to register kprobe at an unaligned address\n");
  128. ret = -EINVAL;
  129. } else if (IS_MTMSRD(insn) || IS_RFID(insn) || IS_RFI(insn)) {
  130. printk("Cannot register a kprobe on rfi/rfid or mtmsr[d]\n");
  131. ret = -EINVAL;
  132. }
  133. /* insn must be on a special executable page on ppc64. This is
  134. * not explicitly required on ppc32 (right now), but it doesn't hurt */
  135. if (!ret) {
  136. p->ainsn.insn = get_insn_slot();
  137. if (!p->ainsn.insn)
  138. ret = -ENOMEM;
  139. }
  140. if (!ret) {
  141. memcpy(p->ainsn.insn, p->addr,
  142. MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
  143. p->opcode = *p->addr;
  144. flush_icache_range((unsigned long)p->ainsn.insn,
  145. (unsigned long)p->ainsn.insn + sizeof(kprobe_opcode_t));
  146. }
  147. p->ainsn.boostable = 0;
  148. return ret;
  149. }
  150. NOKPROBE_SYMBOL(arch_prepare_kprobe);
  151. void arch_arm_kprobe(struct kprobe *p)
  152. {
  153. *p->addr = BREAKPOINT_INSTRUCTION;
  154. flush_icache_range((unsigned long) p->addr,
  155. (unsigned long) p->addr + sizeof(kprobe_opcode_t));
  156. }
  157. NOKPROBE_SYMBOL(arch_arm_kprobe);
  158. void arch_disarm_kprobe(struct kprobe *p)
  159. {
  160. *p->addr = p->opcode;
  161. flush_icache_range((unsigned long) p->addr,
  162. (unsigned long) p->addr + sizeof(kprobe_opcode_t));
  163. }
  164. NOKPROBE_SYMBOL(arch_disarm_kprobe);
  165. void arch_remove_kprobe(struct kprobe *p)
  166. {
  167. if (p->ainsn.insn) {
  168. free_insn_slot(p->ainsn.insn, 0);
  169. p->ainsn.insn = NULL;
  170. }
  171. }
  172. NOKPROBE_SYMBOL(arch_remove_kprobe);
  173. static nokprobe_inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  174. {
  175. enable_single_step(regs);
  176. /*
  177. * On powerpc we should single step on the original
  178. * instruction even if the probed insn is a trap
  179. * variant as values in regs could play a part in
  180. * if the trap is taken or not
  181. */
  182. regs->nip = (unsigned long)p->ainsn.insn;
  183. }
  184. static nokprobe_inline void save_previous_kprobe(struct kprobe_ctlblk *kcb)
  185. {
  186. kcb->prev_kprobe.kp = kprobe_running();
  187. kcb->prev_kprobe.status = kcb->kprobe_status;
  188. kcb->prev_kprobe.saved_msr = kcb->kprobe_saved_msr;
  189. }
  190. static nokprobe_inline void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  191. {
  192. __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
  193. kcb->kprobe_status = kcb->prev_kprobe.status;
  194. kcb->kprobe_saved_msr = kcb->prev_kprobe.saved_msr;
  195. }
  196. static nokprobe_inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  197. struct kprobe_ctlblk *kcb)
  198. {
  199. __this_cpu_write(current_kprobe, p);
  200. kcb->kprobe_saved_msr = regs->msr;
  201. }
  202. bool arch_function_offset_within_entry(unsigned long offset)
  203. {
  204. #ifdef PPC64_ELF_ABI_v2
  205. #ifdef CONFIG_KPROBES_ON_FTRACE
  206. return offset <= 16;
  207. #else
  208. return offset <= 8;
  209. #endif
  210. #else
  211. return !offset;
  212. #endif
  213. }
  214. void arch_prepare_kretprobe(struct kretprobe_instance *ri, struct pt_regs *regs)
  215. {
  216. ri->ret_addr = (kprobe_opcode_t *)regs->link;
  217. /* Replace the return addr with trampoline addr */
  218. regs->link = (unsigned long)kretprobe_trampoline;
  219. }
  220. NOKPROBE_SYMBOL(arch_prepare_kretprobe);
  221. int try_to_emulate(struct kprobe *p, struct pt_regs *regs)
  222. {
  223. int ret;
  224. unsigned int insn = *p->ainsn.insn;
  225. /* regs->nip is also adjusted if emulate_step returns 1 */
  226. ret = emulate_step(regs, insn);
  227. if (ret > 0) {
  228. /*
  229. * Once this instruction has been boosted
  230. * successfully, set the boostable flag
  231. */
  232. if (unlikely(p->ainsn.boostable == 0))
  233. p->ainsn.boostable = 1;
  234. } else if (ret < 0) {
  235. /*
  236. * We don't allow kprobes on mtmsr(d)/rfi(d), etc.
  237. * So, we should never get here... but, its still
  238. * good to catch them, just in case...
  239. */
  240. printk("Can't step on instruction %x\n", insn);
  241. BUG();
  242. } else if (ret == 0)
  243. /* This instruction can't be boosted */
  244. p->ainsn.boostable = -1;
  245. return ret;
  246. }
  247. NOKPROBE_SYMBOL(try_to_emulate);
  248. int kprobe_handler(struct pt_regs *regs)
  249. {
  250. struct kprobe *p;
  251. int ret = 0;
  252. unsigned int *addr = (unsigned int *)regs->nip;
  253. struct kprobe_ctlblk *kcb;
  254. if (user_mode(regs))
  255. return 0;
  256. /*
  257. * We don't want to be preempted for the entire
  258. * duration of kprobe processing
  259. */
  260. preempt_disable();
  261. kcb = get_kprobe_ctlblk();
  262. /* Check we're not actually recursing */
  263. if (kprobe_running()) {
  264. p = get_kprobe(addr);
  265. if (p) {
  266. kprobe_opcode_t insn = *p->ainsn.insn;
  267. if (kcb->kprobe_status == KPROBE_HIT_SS &&
  268. is_trap(insn)) {
  269. /* Turn off 'trace' bits */
  270. regs->msr &= ~MSR_SINGLESTEP;
  271. regs->msr |= kcb->kprobe_saved_msr;
  272. goto no_kprobe;
  273. }
  274. /* We have reentered the kprobe_handler(), since
  275. * another probe was hit while within the handler.
  276. * We here save the original kprobes variables and
  277. * just single step on the instruction of the new probe
  278. * without calling any user handlers.
  279. */
  280. save_previous_kprobe(kcb);
  281. set_current_kprobe(p, regs, kcb);
  282. kprobes_inc_nmissed_count(p);
  283. kcb->kprobe_status = KPROBE_REENTER;
  284. if (p->ainsn.boostable >= 0) {
  285. ret = try_to_emulate(p, regs);
  286. if (ret > 0) {
  287. restore_previous_kprobe(kcb);
  288. preempt_enable_no_resched();
  289. return 1;
  290. }
  291. }
  292. prepare_singlestep(p, regs);
  293. return 1;
  294. } else {
  295. if (*addr != BREAKPOINT_INSTRUCTION) {
  296. /* If trap variant, then it belongs not to us */
  297. kprobe_opcode_t cur_insn = *addr;
  298. if (is_trap(cur_insn))
  299. goto no_kprobe;
  300. /* The breakpoint instruction was removed by
  301. * another cpu right after we hit, no further
  302. * handling of this interrupt is appropriate
  303. */
  304. ret = 1;
  305. goto no_kprobe;
  306. }
  307. p = __this_cpu_read(current_kprobe);
  308. if (p->break_handler && p->break_handler(p, regs)) {
  309. if (!skip_singlestep(p, regs, kcb))
  310. goto ss_probe;
  311. ret = 1;
  312. }
  313. }
  314. goto no_kprobe;
  315. }
  316. p = get_kprobe(addr);
  317. if (!p) {
  318. if (*addr != BREAKPOINT_INSTRUCTION) {
  319. /*
  320. * PowerPC has multiple variants of the "trap"
  321. * instruction. If the current instruction is a
  322. * trap variant, it could belong to someone else
  323. */
  324. kprobe_opcode_t cur_insn = *addr;
  325. if (is_trap(cur_insn))
  326. goto no_kprobe;
  327. /*
  328. * The breakpoint instruction was removed right
  329. * after we hit it. Another cpu has removed
  330. * either a probepoint or a debugger breakpoint
  331. * at this address. In either case, no further
  332. * handling of this interrupt is appropriate.
  333. */
  334. ret = 1;
  335. }
  336. /* Not one of ours: let kernel handle it */
  337. goto no_kprobe;
  338. }
  339. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  340. set_current_kprobe(p, regs, kcb);
  341. if (p->pre_handler && p->pre_handler(p, regs))
  342. /* handler has already set things up, so skip ss setup */
  343. return 1;
  344. ss_probe:
  345. if (p->ainsn.boostable >= 0) {
  346. ret = try_to_emulate(p, regs);
  347. if (ret > 0) {
  348. if (p->post_handler)
  349. p->post_handler(p, regs, 0);
  350. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  351. reset_current_kprobe();
  352. preempt_enable_no_resched();
  353. return 1;
  354. }
  355. }
  356. prepare_singlestep(p, regs);
  357. kcb->kprobe_status = KPROBE_HIT_SS;
  358. return 1;
  359. no_kprobe:
  360. preempt_enable_no_resched();
  361. return ret;
  362. }
  363. NOKPROBE_SYMBOL(kprobe_handler);
  364. /*
  365. * Function return probe trampoline:
  366. * - init_kprobes() establishes a probepoint here
  367. * - When the probed function returns, this probe
  368. * causes the handlers to fire
  369. */
  370. asm(".global kretprobe_trampoline\n"
  371. ".type kretprobe_trampoline, @function\n"
  372. "kretprobe_trampoline:\n"
  373. "nop\n"
  374. "blr\n"
  375. ".size kretprobe_trampoline, .-kretprobe_trampoline\n");
  376. /*
  377. * Called when the probe at kretprobe trampoline is hit
  378. */
  379. static int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
  380. {
  381. struct kretprobe_instance *ri = NULL;
  382. struct hlist_head *head, empty_rp;
  383. struct hlist_node *tmp;
  384. unsigned long flags, orig_ret_address = 0;
  385. unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
  386. INIT_HLIST_HEAD(&empty_rp);
  387. kretprobe_hash_lock(current, &head, &flags);
  388. /*
  389. * It is possible to have multiple instances associated with a given
  390. * task either because an multiple functions in the call path
  391. * have a return probe installed on them, and/or more than one return
  392. * return probe was registered for a target function.
  393. *
  394. * We can handle this because:
  395. * - instances are always inserted at the head of the list
  396. * - when multiple return probes are registered for the same
  397. * function, the first instance's ret_addr will point to the
  398. * real return address, and all the rest will point to
  399. * kretprobe_trampoline
  400. */
  401. hlist_for_each_entry_safe(ri, tmp, head, hlist) {
  402. if (ri->task != current)
  403. /* another task is sharing our hash bucket */
  404. continue;
  405. if (ri->rp && ri->rp->handler)
  406. ri->rp->handler(ri, regs);
  407. orig_ret_address = (unsigned long)ri->ret_addr;
  408. recycle_rp_inst(ri, &empty_rp);
  409. if (orig_ret_address != trampoline_address)
  410. /*
  411. * This is the real return address. Any other
  412. * instances associated with this task are for
  413. * other calls deeper on the call stack
  414. */
  415. break;
  416. }
  417. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  418. regs->nip = orig_ret_address;
  419. /*
  420. * Make LR point to the orig_ret_address.
  421. * When the 'nop' inside the kretprobe_trampoline
  422. * is optimized, we can do a 'blr' after executing the
  423. * detour buffer code.
  424. */
  425. regs->link = orig_ret_address;
  426. reset_current_kprobe();
  427. kretprobe_hash_unlock(current, &flags);
  428. preempt_enable_no_resched();
  429. hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
  430. hlist_del(&ri->hlist);
  431. kfree(ri);
  432. }
  433. /*
  434. * By returning a non-zero value, we are telling
  435. * kprobe_handler() that we don't want the post_handler
  436. * to run (and have re-enabled preemption)
  437. */
  438. return 1;
  439. }
  440. NOKPROBE_SYMBOL(trampoline_probe_handler);
  441. /*
  442. * Called after single-stepping. p->addr is the address of the
  443. * instruction whose first byte has been replaced by the "breakpoint"
  444. * instruction. To avoid the SMP problems that can occur when we
  445. * temporarily put back the original opcode to single-step, we
  446. * single-stepped a copy of the instruction. The address of this
  447. * copy is p->ainsn.insn.
  448. */
  449. int kprobe_post_handler(struct pt_regs *regs)
  450. {
  451. struct kprobe *cur = kprobe_running();
  452. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  453. if (!cur || user_mode(regs))
  454. return 0;
  455. /* make sure we got here for instruction we have a kprobe on */
  456. if (((unsigned long)cur->ainsn.insn + 4) != regs->nip)
  457. return 0;
  458. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  459. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  460. cur->post_handler(cur, regs, 0);
  461. }
  462. /* Adjust nip to after the single-stepped instruction */
  463. regs->nip = (unsigned long)cur->addr + 4;
  464. regs->msr |= kcb->kprobe_saved_msr;
  465. /*Restore back the original saved kprobes variables and continue. */
  466. if (kcb->kprobe_status == KPROBE_REENTER) {
  467. restore_previous_kprobe(kcb);
  468. goto out;
  469. }
  470. reset_current_kprobe();
  471. out:
  472. preempt_enable_no_resched();
  473. /*
  474. * if somebody else is singlestepping across a probe point, msr
  475. * will have DE/SE set, in which case, continue the remaining processing
  476. * of do_debug, as if this is not a probe hit.
  477. */
  478. if (regs->msr & MSR_SINGLESTEP)
  479. return 0;
  480. return 1;
  481. }
  482. NOKPROBE_SYMBOL(kprobe_post_handler);
  483. int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  484. {
  485. struct kprobe *cur = kprobe_running();
  486. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  487. const struct exception_table_entry *entry;
  488. switch(kcb->kprobe_status) {
  489. case KPROBE_HIT_SS:
  490. case KPROBE_REENTER:
  491. /*
  492. * We are here because the instruction being single
  493. * stepped caused a page fault. We reset the current
  494. * kprobe and the nip points back to the probe address
  495. * and allow the page fault handler to continue as a
  496. * normal page fault.
  497. */
  498. regs->nip = (unsigned long)cur->addr;
  499. regs->msr &= ~MSR_SINGLESTEP; /* Turn off 'trace' bits */
  500. regs->msr |= kcb->kprobe_saved_msr;
  501. if (kcb->kprobe_status == KPROBE_REENTER)
  502. restore_previous_kprobe(kcb);
  503. else
  504. reset_current_kprobe();
  505. preempt_enable_no_resched();
  506. break;
  507. case KPROBE_HIT_ACTIVE:
  508. case KPROBE_HIT_SSDONE:
  509. /*
  510. * We increment the nmissed count for accounting,
  511. * we can also use npre/npostfault count for accounting
  512. * these specific fault cases.
  513. */
  514. kprobes_inc_nmissed_count(cur);
  515. /*
  516. * We come here because instructions in the pre/post
  517. * handler caused the page_fault, this could happen
  518. * if handler tries to access user space by
  519. * copy_from_user(), get_user() etc. Let the
  520. * user-specified handler try to fix it first.
  521. */
  522. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  523. return 1;
  524. /*
  525. * In case the user-specified fault handler returned
  526. * zero, try to fix up.
  527. */
  528. if ((entry = search_exception_tables(regs->nip)) != NULL) {
  529. regs->nip = extable_fixup(entry);
  530. return 1;
  531. }
  532. /*
  533. * fixup_exception() could not handle it,
  534. * Let do_page_fault() fix it.
  535. */
  536. break;
  537. default:
  538. break;
  539. }
  540. return 0;
  541. }
  542. NOKPROBE_SYMBOL(kprobe_fault_handler);
  543. unsigned long arch_deref_entry_point(void *entry)
  544. {
  545. return ppc_global_function_entry(entry);
  546. }
  547. NOKPROBE_SYMBOL(arch_deref_entry_point);
  548. int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  549. {
  550. struct jprobe *jp = container_of(p, struct jprobe, kp);
  551. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  552. memcpy(&kcb->jprobe_saved_regs, regs, sizeof(struct pt_regs));
  553. /* setup return addr to the jprobe handler routine */
  554. regs->nip = arch_deref_entry_point(jp->entry);
  555. #ifdef PPC64_ELF_ABI_v2
  556. regs->gpr[12] = (unsigned long)jp->entry;
  557. #elif defined(PPC64_ELF_ABI_v1)
  558. regs->gpr[2] = (unsigned long)(((func_descr_t *)jp->entry)->toc);
  559. #endif
  560. /*
  561. * jprobes use jprobe_return() which skips the normal return
  562. * path of the function, and this messes up the accounting of the
  563. * function graph tracer.
  564. *
  565. * Pause function graph tracing while performing the jprobe function.
  566. */
  567. pause_graph_tracing();
  568. return 1;
  569. }
  570. NOKPROBE_SYMBOL(setjmp_pre_handler);
  571. void __used jprobe_return(void)
  572. {
  573. asm volatile("trap" ::: "memory");
  574. }
  575. NOKPROBE_SYMBOL(jprobe_return);
  576. static void __used jprobe_return_end(void)
  577. {
  578. }
  579. NOKPROBE_SYMBOL(jprobe_return_end);
  580. int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  581. {
  582. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  583. /*
  584. * FIXME - we should ideally be validating that we got here 'cos
  585. * of the "trap" in jprobe_return() above, before restoring the
  586. * saved regs...
  587. */
  588. memcpy(regs, &kcb->jprobe_saved_regs, sizeof(struct pt_regs));
  589. /* It's OK to start function graph tracing again */
  590. unpause_graph_tracing();
  591. preempt_enable_no_resched();
  592. return 1;
  593. }
  594. NOKPROBE_SYMBOL(longjmp_break_handler);
  595. static struct kprobe trampoline_p = {
  596. .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
  597. .pre_handler = trampoline_probe_handler
  598. };
  599. int __init arch_init_kprobes(void)
  600. {
  601. return register_kprobe(&trampoline_p);
  602. }
  603. int arch_trampoline_kprobe(struct kprobe *p)
  604. {
  605. if (p->addr == (kprobe_opcode_t *)&kretprobe_trampoline)
  606. return 1;
  607. return 0;
  608. }
  609. NOKPROBE_SYMBOL(arch_trampoline_kprobe);