kprobes.c 18 KB

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