kprobes.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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, 2006
  19. *
  20. * s390 port, used ppc64 as template. Mike Grundy <grundym@us.ibm.com>
  21. */
  22. #include <linux/kprobes.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/preempt.h>
  25. #include <linux/stop_machine.h>
  26. #include <linux/kdebug.h>
  27. #include <linux/uaccess.h>
  28. #include <asm/cacheflush.h>
  29. #include <asm/sections.h>
  30. #include <linux/module.h>
  31. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  32. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  33. struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}};
  34. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  35. {
  36. /* Make sure the probe isn't going on a difficult instruction */
  37. if (is_prohibited_opcode((kprobe_opcode_t *) p->addr))
  38. return -EINVAL;
  39. if ((unsigned long)p->addr & 0x01)
  40. return -EINVAL;
  41. /* Use the get_insn_slot() facility for correctness */
  42. if (!(p->ainsn.insn = get_insn_slot()))
  43. return -ENOMEM;
  44. memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
  45. get_instruction_type(&p->ainsn);
  46. p->opcode = *p->addr;
  47. return 0;
  48. }
  49. int __kprobes is_prohibited_opcode(kprobe_opcode_t *instruction)
  50. {
  51. switch (*(__u8 *) instruction) {
  52. case 0x0c: /* bassm */
  53. case 0x0b: /* bsm */
  54. case 0x83: /* diag */
  55. case 0x44: /* ex */
  56. return -EINVAL;
  57. }
  58. switch (*(__u16 *) instruction) {
  59. case 0x0101: /* pr */
  60. case 0xb25a: /* bsa */
  61. case 0xb240: /* bakr */
  62. case 0xb258: /* bsg */
  63. case 0xb218: /* pc */
  64. case 0xb228: /* pt */
  65. return -EINVAL;
  66. }
  67. return 0;
  68. }
  69. void __kprobes get_instruction_type(struct arch_specific_insn *ainsn)
  70. {
  71. /* default fixup method */
  72. ainsn->fixup = FIXUP_PSW_NORMAL;
  73. /* save r1 operand */
  74. ainsn->reg = (*ainsn->insn & 0xf0) >> 4;
  75. /* save the instruction length (pop 5-5) in bytes */
  76. switch (*(__u8 *) (ainsn->insn) >> 6) {
  77. case 0:
  78. ainsn->ilen = 2;
  79. break;
  80. case 1:
  81. case 2:
  82. ainsn->ilen = 4;
  83. break;
  84. case 3:
  85. ainsn->ilen = 6;
  86. break;
  87. }
  88. switch (*(__u8 *) ainsn->insn) {
  89. case 0x05: /* balr */
  90. case 0x0d: /* basr */
  91. ainsn->fixup = FIXUP_RETURN_REGISTER;
  92. /* if r2 = 0, no branch will be taken */
  93. if ((*ainsn->insn & 0x0f) == 0)
  94. ainsn->fixup |= FIXUP_BRANCH_NOT_TAKEN;
  95. break;
  96. case 0x06: /* bctr */
  97. case 0x07: /* bcr */
  98. ainsn->fixup = FIXUP_BRANCH_NOT_TAKEN;
  99. break;
  100. case 0x45: /* bal */
  101. case 0x4d: /* bas */
  102. ainsn->fixup = FIXUP_RETURN_REGISTER;
  103. break;
  104. case 0x47: /* bc */
  105. case 0x46: /* bct */
  106. case 0x86: /* bxh */
  107. case 0x87: /* bxle */
  108. ainsn->fixup = FIXUP_BRANCH_NOT_TAKEN;
  109. break;
  110. case 0x82: /* lpsw */
  111. ainsn->fixup = FIXUP_NOT_REQUIRED;
  112. break;
  113. case 0xb2: /* lpswe */
  114. if (*(((__u8 *) ainsn->insn) + 1) == 0xb2) {
  115. ainsn->fixup = FIXUP_NOT_REQUIRED;
  116. }
  117. break;
  118. case 0xa7: /* bras */
  119. if ((*ainsn->insn & 0x0f) == 0x05) {
  120. ainsn->fixup |= FIXUP_RETURN_REGISTER;
  121. }
  122. break;
  123. case 0xc0:
  124. if ((*ainsn->insn & 0x0f) == 0x00 /* larl */
  125. || (*ainsn->insn & 0x0f) == 0x05) /* brasl */
  126. ainsn->fixup |= FIXUP_RETURN_REGISTER;
  127. break;
  128. case 0xeb:
  129. if (*(((__u8 *) ainsn->insn) + 5 ) == 0x44 || /* bxhg */
  130. *(((__u8 *) ainsn->insn) + 5) == 0x45) {/* bxleg */
  131. ainsn->fixup = FIXUP_BRANCH_NOT_TAKEN;
  132. }
  133. break;
  134. case 0xe3: /* bctg */
  135. if (*(((__u8 *) ainsn->insn) + 5) == 0x46) {
  136. ainsn->fixup = FIXUP_BRANCH_NOT_TAKEN;
  137. }
  138. break;
  139. }
  140. }
  141. static int __kprobes swap_instruction(void *aref)
  142. {
  143. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  144. unsigned long status = kcb->kprobe_status;
  145. struct ins_replace_args *args = aref;
  146. int rc;
  147. kcb->kprobe_status = KPROBE_SWAP_INST;
  148. rc = probe_kernel_write(args->ptr, &args->new, sizeof(args->new));
  149. kcb->kprobe_status = status;
  150. return rc;
  151. }
  152. void __kprobes arch_arm_kprobe(struct kprobe *p)
  153. {
  154. struct ins_replace_args args;
  155. args.ptr = p->addr;
  156. args.old = p->opcode;
  157. args.new = BREAKPOINT_INSTRUCTION;
  158. stop_machine(swap_instruction, &args, NULL);
  159. }
  160. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  161. {
  162. struct ins_replace_args args;
  163. args.ptr = p->addr;
  164. args.old = BREAKPOINT_INSTRUCTION;
  165. args.new = p->opcode;
  166. stop_machine(swap_instruction, &args, NULL);
  167. }
  168. void __kprobes arch_remove_kprobe(struct kprobe *p)
  169. {
  170. if (p->ainsn.insn) {
  171. free_insn_slot(p->ainsn.insn, 0);
  172. p->ainsn.insn = NULL;
  173. }
  174. }
  175. static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  176. {
  177. per_cr_bits kprobe_per_regs[1];
  178. memset(kprobe_per_regs, 0, sizeof(per_cr_bits));
  179. regs->psw.addr = (unsigned long)p->ainsn.insn | PSW_ADDR_AMODE;
  180. /* Set up the per control reg info, will pass to lctl */
  181. kprobe_per_regs[0].em_instruction_fetch = 1;
  182. kprobe_per_regs[0].starting_addr = (unsigned long)p->ainsn.insn;
  183. kprobe_per_regs[0].ending_addr = (unsigned long)p->ainsn.insn + 1;
  184. /* Set the PER control regs, turns on single step for this address */
  185. __ctl_load(kprobe_per_regs, 9, 11);
  186. regs->psw.mask |= PSW_MASK_PER;
  187. regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK);
  188. }
  189. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  190. {
  191. kcb->prev_kprobe.kp = kprobe_running();
  192. kcb->prev_kprobe.status = kcb->kprobe_status;
  193. kcb->prev_kprobe.kprobe_saved_imask = kcb->kprobe_saved_imask;
  194. memcpy(kcb->prev_kprobe.kprobe_saved_ctl, kcb->kprobe_saved_ctl,
  195. sizeof(kcb->kprobe_saved_ctl));
  196. }
  197. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  198. {
  199. __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
  200. kcb->kprobe_status = kcb->prev_kprobe.status;
  201. kcb->kprobe_saved_imask = kcb->prev_kprobe.kprobe_saved_imask;
  202. memcpy(kcb->kprobe_saved_ctl, kcb->prev_kprobe.kprobe_saved_ctl,
  203. sizeof(kcb->kprobe_saved_ctl));
  204. }
  205. static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  206. struct kprobe_ctlblk *kcb)
  207. {
  208. __get_cpu_var(current_kprobe) = p;
  209. /* Save the interrupt and per flags */
  210. kcb->kprobe_saved_imask = regs->psw.mask &
  211. (PSW_MASK_PER | PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK);
  212. /* Save the control regs that govern PER */
  213. __ctl_store(kcb->kprobe_saved_ctl, 9, 11);
  214. }
  215. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  216. struct pt_regs *regs)
  217. {
  218. ri->ret_addr = (kprobe_opcode_t *) regs->gprs[14];
  219. /* Replace the return addr with trampoline addr */
  220. regs->gprs[14] = (unsigned long)&kretprobe_trampoline;
  221. }
  222. static int __kprobes kprobe_handler(struct pt_regs *regs)
  223. {
  224. struct kprobe *p;
  225. int ret = 0;
  226. unsigned long *addr = (unsigned long *)
  227. ((regs->psw.addr & PSW_ADDR_INSN) - 2);
  228. struct kprobe_ctlblk *kcb;
  229. /*
  230. * We don't want to be preempted for the entire
  231. * duration of kprobe processing
  232. */
  233. preempt_disable();
  234. kcb = get_kprobe_ctlblk();
  235. /* Check we're not actually recursing */
  236. if (kprobe_running()) {
  237. p = get_kprobe(addr);
  238. if (p) {
  239. if (kcb->kprobe_status == KPROBE_HIT_SS &&
  240. *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
  241. regs->psw.mask &= ~PSW_MASK_PER;
  242. regs->psw.mask |= kcb->kprobe_saved_imask;
  243. goto no_kprobe;
  244. }
  245. /* We have reentered the kprobe_handler(), since
  246. * another probe was hit while within the handler.
  247. * We here save the original kprobes variables and
  248. * just single step on the instruction of the new probe
  249. * without calling any user handlers.
  250. */
  251. save_previous_kprobe(kcb);
  252. set_current_kprobe(p, regs, kcb);
  253. kprobes_inc_nmissed_count(p);
  254. prepare_singlestep(p, regs);
  255. kcb->kprobe_status = KPROBE_REENTER;
  256. return 1;
  257. } else {
  258. p = __get_cpu_var(current_kprobe);
  259. if (p->break_handler && p->break_handler(p, regs)) {
  260. goto ss_probe;
  261. }
  262. }
  263. goto no_kprobe;
  264. }
  265. p = get_kprobe(addr);
  266. if (!p)
  267. /*
  268. * No kprobe at this address. The fault has not been
  269. * caused by a kprobe breakpoint. The race of breakpoint
  270. * vs. kprobe remove does not exist because on s390 we
  271. * use stop_machine to arm/disarm the breakpoints.
  272. */
  273. goto no_kprobe;
  274. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  275. set_current_kprobe(p, regs, kcb);
  276. if (p->pre_handler && p->pre_handler(p, regs))
  277. /* handler has already set things up, so skip ss setup */
  278. return 1;
  279. ss_probe:
  280. prepare_singlestep(p, regs);
  281. kcb->kprobe_status = KPROBE_HIT_SS;
  282. return 1;
  283. no_kprobe:
  284. preempt_enable_no_resched();
  285. return ret;
  286. }
  287. /*
  288. * Function return probe trampoline:
  289. * - init_kprobes() establishes a probepoint here
  290. * - When the probed function returns, this probe
  291. * causes the handlers to fire
  292. */
  293. static void __used kretprobe_trampoline_holder(void)
  294. {
  295. asm volatile(".global kretprobe_trampoline\n"
  296. "kretprobe_trampoline: bcr 0,0\n");
  297. }
  298. /*
  299. * Called when the probe at kretprobe trampoline is hit
  300. */
  301. static int __kprobes trampoline_probe_handler(struct kprobe *p,
  302. struct pt_regs *regs)
  303. {
  304. struct kretprobe_instance *ri = NULL;
  305. struct hlist_head *head, empty_rp;
  306. struct hlist_node *node, *tmp;
  307. unsigned long flags, orig_ret_address = 0;
  308. unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
  309. INIT_HLIST_HEAD(&empty_rp);
  310. kretprobe_hash_lock(current, &head, &flags);
  311. /*
  312. * It is possible to have multiple instances associated with a given
  313. * task either because an multiple functions in the call path
  314. * have a return probe installed on them, and/or more than one return
  315. * return probe was registered for a target function.
  316. *
  317. * We can handle this because:
  318. * - instances are always inserted at the head of the list
  319. * - when multiple return probes are registered for the same
  320. * function, the first instance's ret_addr will point to the
  321. * real return address, and all the rest will point to
  322. * kretprobe_trampoline
  323. */
  324. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  325. if (ri->task != current)
  326. /* another task is sharing our hash bucket */
  327. continue;
  328. if (ri->rp && ri->rp->handler)
  329. ri->rp->handler(ri, regs);
  330. orig_ret_address = (unsigned long)ri->ret_addr;
  331. recycle_rp_inst(ri, &empty_rp);
  332. if (orig_ret_address != trampoline_address) {
  333. /*
  334. * This is the real return address. Any other
  335. * instances associated with this task are for
  336. * other calls deeper on the call stack
  337. */
  338. break;
  339. }
  340. }
  341. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  342. regs->psw.addr = orig_ret_address | PSW_ADDR_AMODE;
  343. reset_current_kprobe();
  344. kretprobe_hash_unlock(current, &flags);
  345. preempt_enable_no_resched();
  346. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  347. hlist_del(&ri->hlist);
  348. kfree(ri);
  349. }
  350. /*
  351. * By returning a non-zero value, we are telling
  352. * kprobe_handler() that we don't want the post_handler
  353. * to run (and have re-enabled preemption)
  354. */
  355. return 1;
  356. }
  357. /*
  358. * Called after single-stepping. p->addr is the address of the
  359. * instruction whose first byte has been replaced by the "breakpoint"
  360. * instruction. To avoid the SMP problems that can occur when we
  361. * temporarily put back the original opcode to single-step, we
  362. * single-stepped a copy of the instruction. The address of this
  363. * copy is p->ainsn.insn.
  364. */
  365. static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
  366. {
  367. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  368. regs->psw.addr &= PSW_ADDR_INSN;
  369. if (p->ainsn.fixup & FIXUP_PSW_NORMAL)
  370. regs->psw.addr = (unsigned long)p->addr +
  371. ((unsigned long)regs->psw.addr -
  372. (unsigned long)p->ainsn.insn);
  373. if (p->ainsn.fixup & FIXUP_BRANCH_NOT_TAKEN)
  374. if ((unsigned long)regs->psw.addr -
  375. (unsigned long)p->ainsn.insn == p->ainsn.ilen)
  376. regs->psw.addr = (unsigned long)p->addr + p->ainsn.ilen;
  377. if (p->ainsn.fixup & FIXUP_RETURN_REGISTER)
  378. regs->gprs[p->ainsn.reg] = ((unsigned long)p->addr +
  379. (regs->gprs[p->ainsn.reg] -
  380. (unsigned long)p->ainsn.insn))
  381. | PSW_ADDR_AMODE;
  382. regs->psw.addr |= PSW_ADDR_AMODE;
  383. /* turn off PER mode */
  384. regs->psw.mask &= ~PSW_MASK_PER;
  385. /* Restore the original per control regs */
  386. __ctl_load(kcb->kprobe_saved_ctl, 9, 11);
  387. regs->psw.mask |= kcb->kprobe_saved_imask;
  388. }
  389. static int __kprobes post_kprobe_handler(struct pt_regs *regs)
  390. {
  391. struct kprobe *cur = kprobe_running();
  392. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  393. if (!cur)
  394. return 0;
  395. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  396. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  397. cur->post_handler(cur, regs, 0);
  398. }
  399. resume_execution(cur, regs);
  400. /*Restore back the original saved kprobes variables and continue. */
  401. if (kcb->kprobe_status == KPROBE_REENTER) {
  402. restore_previous_kprobe(kcb);
  403. goto out;
  404. }
  405. reset_current_kprobe();
  406. out:
  407. preempt_enable_no_resched();
  408. /*
  409. * if somebody else is singlestepping across a probe point, psw mask
  410. * will have PER set, in which case, continue the remaining processing
  411. * of do_single_step, as if this is not a probe hit.
  412. */
  413. if (regs->psw.mask & PSW_MASK_PER) {
  414. return 0;
  415. }
  416. return 1;
  417. }
  418. int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  419. {
  420. struct kprobe *cur = kprobe_running();
  421. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  422. const struct exception_table_entry *entry;
  423. switch(kcb->kprobe_status) {
  424. case KPROBE_SWAP_INST:
  425. /* We are here because the instruction replacement failed */
  426. return 0;
  427. case KPROBE_HIT_SS:
  428. case KPROBE_REENTER:
  429. /*
  430. * We are here because the instruction being single
  431. * stepped caused a page fault. We reset the current
  432. * kprobe and the nip points back to the probe address
  433. * and allow the page fault handler to continue as a
  434. * normal page fault.
  435. */
  436. regs->psw.addr = (unsigned long)cur->addr | PSW_ADDR_AMODE;
  437. regs->psw.mask &= ~PSW_MASK_PER;
  438. regs->psw.mask |= kcb->kprobe_saved_imask;
  439. if (kcb->kprobe_status == KPROBE_REENTER)
  440. restore_previous_kprobe(kcb);
  441. else
  442. reset_current_kprobe();
  443. preempt_enable_no_resched();
  444. break;
  445. case KPROBE_HIT_ACTIVE:
  446. case KPROBE_HIT_SSDONE:
  447. /*
  448. * We increment the nmissed count for accounting,
  449. * we can also use npre/npostfault count for accouting
  450. * these specific fault cases.
  451. */
  452. kprobes_inc_nmissed_count(cur);
  453. /*
  454. * We come here because instructions in the pre/post
  455. * handler caused the page_fault, this could happen
  456. * if handler tries to access user space by
  457. * copy_from_user(), get_user() etc. Let the
  458. * user-specified handler try to fix it first.
  459. */
  460. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  461. return 1;
  462. /*
  463. * In case the user-specified fault handler returned
  464. * zero, try to fix up.
  465. */
  466. entry = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN);
  467. if (entry) {
  468. regs->psw.addr = entry->fixup | PSW_ADDR_AMODE;
  469. return 1;
  470. }
  471. /*
  472. * fixup_exception() could not handle it,
  473. * Let do_page_fault() fix it.
  474. */
  475. break;
  476. default:
  477. break;
  478. }
  479. return 0;
  480. }
  481. /*
  482. * Wrapper routine to for handling exceptions.
  483. */
  484. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  485. unsigned long val, void *data)
  486. {
  487. struct die_args *args = (struct die_args *)data;
  488. int ret = NOTIFY_DONE;
  489. switch (val) {
  490. case DIE_BPT:
  491. if (kprobe_handler(args->regs))
  492. ret = NOTIFY_STOP;
  493. break;
  494. case DIE_SSTEP:
  495. if (post_kprobe_handler(args->regs))
  496. ret = NOTIFY_STOP;
  497. break;
  498. case DIE_TRAP:
  499. /* kprobe_running() needs smp_processor_id() */
  500. preempt_disable();
  501. if (kprobe_running() &&
  502. kprobe_fault_handler(args->regs, args->trapnr))
  503. ret = NOTIFY_STOP;
  504. preempt_enable();
  505. break;
  506. default:
  507. break;
  508. }
  509. return ret;
  510. }
  511. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  512. {
  513. struct jprobe *jp = container_of(p, struct jprobe, kp);
  514. unsigned long addr;
  515. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  516. memcpy(&kcb->jprobe_saved_regs, regs, sizeof(struct pt_regs));
  517. /* setup return addr to the jprobe handler routine */
  518. regs->psw.addr = (unsigned long)(jp->entry) | PSW_ADDR_AMODE;
  519. /* r14 is the function return address */
  520. kcb->jprobe_saved_r14 = (unsigned long)regs->gprs[14];
  521. /* r15 is the stack pointer */
  522. kcb->jprobe_saved_r15 = (unsigned long)regs->gprs[15];
  523. addr = (unsigned long)kcb->jprobe_saved_r15;
  524. memcpy(kcb->jprobes_stack, (kprobe_opcode_t *) addr,
  525. MIN_STACK_SIZE(addr));
  526. return 1;
  527. }
  528. void __kprobes jprobe_return(void)
  529. {
  530. asm volatile(".word 0x0002");
  531. }
  532. void __kprobes jprobe_return_end(void)
  533. {
  534. asm volatile("bcr 0,0");
  535. }
  536. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  537. {
  538. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  539. unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_r15);
  540. /* Put the regs back */
  541. memcpy(regs, &kcb->jprobe_saved_regs, sizeof(struct pt_regs));
  542. /* put the stack back */
  543. memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
  544. MIN_STACK_SIZE(stack_addr));
  545. preempt_enable_no_resched();
  546. return 1;
  547. }
  548. static struct kprobe trampoline_p = {
  549. .addr = (kprobe_opcode_t *) & kretprobe_trampoline,
  550. .pre_handler = trampoline_probe_handler
  551. };
  552. int __init arch_init_kprobes(void)
  553. {
  554. return register_kprobe(&trampoline_p);
  555. }
  556. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  557. {
  558. if (p->addr == (kprobe_opcode_t *) & kretprobe_trampoline)
  559. return 1;
  560. return 0;
  561. }