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. per_cr_bits kprobe_per_regs[1];
  160. /* Set up the per control reg info, will pass to lctl */
  161. memset(kprobe_per_regs, 0, sizeof(per_cr_bits));
  162. kprobe_per_regs[0].em_instruction_fetch = 1;
  163. kprobe_per_regs[0].starting_addr = ip;
  164. kprobe_per_regs[0].ending_addr = ip;
  165. /* Save control regs and psw mask */
  166. __ctl_store(kcb->kprobe_saved_ctl, 9, 11);
  167. kcb->kprobe_saved_imask = regs->psw.mask &
  168. (PSW_MASK_PER | PSW_MASK_IO | PSW_MASK_EXT);
  169. /* Set PER control regs, turns on single step for the given address */
  170. __ctl_load(kprobe_per_regs, 9, 11);
  171. regs->psw.mask |= PSW_MASK_PER;
  172. regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT);
  173. regs->psw.addr = ip | PSW_ADDR_AMODE;
  174. }
  175. static void __kprobes disable_singlestep(struct kprobe_ctlblk *kcb,
  176. struct pt_regs *regs,
  177. unsigned long ip)
  178. {
  179. /* Restore control regs and psw mask, set new psw address */
  180. __ctl_load(kcb->kprobe_saved_ctl, 9, 11);
  181. regs->psw.mask &= ~PSW_MASK_PER;
  182. regs->psw.mask |= kcb->kprobe_saved_imask;
  183. regs->psw.addr = ip | PSW_ADDR_AMODE;
  184. }
  185. /*
  186. * Activate a kprobe by storing its pointer to current_kprobe. The
  187. * previous kprobe is stored in kcb->prev_kprobe. A stack of up to
  188. * two kprobes can be active, see KPROBE_REENTER.
  189. */
  190. static void __kprobes push_kprobe(struct kprobe_ctlblk *kcb, struct kprobe *p)
  191. {
  192. kcb->prev_kprobe.kp = __get_cpu_var(current_kprobe);
  193. kcb->prev_kprobe.status = kcb->kprobe_status;
  194. __get_cpu_var(current_kprobe) = p;
  195. }
  196. /*
  197. * Deactivate a kprobe by backing up to the previous state. If the
  198. * current state is KPROBE_REENTER prev_kprobe.kp will be non-NULL,
  199. * for any other state prev_kprobe.kp will be NULL.
  200. */
  201. static void __kprobes pop_kprobe(struct kprobe_ctlblk *kcb)
  202. {
  203. __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
  204. kcb->kprobe_status = kcb->prev_kprobe.status;
  205. }
  206. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  207. struct pt_regs *regs)
  208. {
  209. ri->ret_addr = (kprobe_opcode_t *) regs->gprs[14];
  210. /* Replace the return addr with trampoline addr */
  211. regs->gprs[14] = (unsigned long) &kretprobe_trampoline;
  212. }
  213. static void __kprobes kprobe_reenter_check(struct kprobe_ctlblk *kcb,
  214. struct kprobe *p)
  215. {
  216. switch (kcb->kprobe_status) {
  217. case KPROBE_HIT_SSDONE:
  218. case KPROBE_HIT_ACTIVE:
  219. kprobes_inc_nmissed_count(p);
  220. break;
  221. case KPROBE_HIT_SS:
  222. case KPROBE_REENTER:
  223. default:
  224. /*
  225. * A kprobe on the code path to single step an instruction
  226. * is a BUG. The code path resides in the .kprobes.text
  227. * section and is executed with interrupts disabled.
  228. */
  229. printk(KERN_EMERG "Invalid kprobe detected at %p.\n", p->addr);
  230. dump_kprobe(p);
  231. BUG();
  232. }
  233. }
  234. static int __kprobes kprobe_handler(struct pt_regs *regs)
  235. {
  236. struct kprobe_ctlblk *kcb;
  237. struct kprobe *p;
  238. /*
  239. * We want to disable preemption for the entire duration of kprobe
  240. * processing. That includes the calls to the pre/post handlers
  241. * and single stepping the kprobe instruction.
  242. */
  243. preempt_disable();
  244. kcb = get_kprobe_ctlblk();
  245. p = get_kprobe((void *)((regs->psw.addr & PSW_ADDR_INSN) - 2));
  246. if (p) {
  247. if (kprobe_running()) {
  248. /*
  249. * We have hit a kprobe while another is still
  250. * active. This can happen in the pre and post
  251. * handler. Single step the instruction of the
  252. * new probe but do not call any handler function
  253. * of this secondary kprobe.
  254. * push_kprobe and pop_kprobe saves and restores
  255. * the currently active kprobe.
  256. */
  257. kprobe_reenter_check(kcb, p);
  258. push_kprobe(kcb, p);
  259. kcb->kprobe_status = KPROBE_REENTER;
  260. } else {
  261. /*
  262. * If we have no pre-handler or it returned 0, we
  263. * continue with single stepping. If we have a
  264. * pre-handler and it returned non-zero, it prepped
  265. * for calling the break_handler below on re-entry
  266. * for jprobe processing, so get out doing nothing
  267. * more here.
  268. */
  269. push_kprobe(kcb, p);
  270. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  271. if (p->pre_handler && p->pre_handler(p, regs))
  272. return 1;
  273. kcb->kprobe_status = KPROBE_HIT_SS;
  274. }
  275. enable_singlestep(kcb, regs, (unsigned long) p->ainsn.insn);
  276. return 1;
  277. } else if (kprobe_running()) {
  278. p = __get_cpu_var(current_kprobe);
  279. if (p->break_handler && p->break_handler(p, regs)) {
  280. /*
  281. * Continuation after the jprobe completed and
  282. * caused the jprobe_return trap. The jprobe
  283. * break_handler "returns" to the original
  284. * function that still has the kprobe breakpoint
  285. * installed. We continue with single stepping.
  286. */
  287. kcb->kprobe_status = KPROBE_HIT_SS;
  288. enable_singlestep(kcb, regs,
  289. (unsigned long) p->ainsn.insn);
  290. return 1;
  291. } /* else:
  292. * No kprobe at this address and the current kprobe
  293. * has no break handler (no jprobe!). The kernel just
  294. * exploded, let the standard trap handler pick up the
  295. * pieces.
  296. */
  297. } /* else:
  298. * No kprobe at this address and no active kprobe. The trap has
  299. * not been caused by a kprobe breakpoint. The race of breakpoint
  300. * vs. kprobe remove does not exist because on s390 as we use
  301. * stop_machine to arm/disarm the breakpoints.
  302. */
  303. preempt_enable_no_resched();
  304. return 0;
  305. }
  306. /*
  307. * Function return probe trampoline:
  308. * - init_kprobes() establishes a probepoint here
  309. * - When the probed function returns, this probe
  310. * causes the handlers to fire
  311. */
  312. static void __used kretprobe_trampoline_holder(void)
  313. {
  314. asm volatile(".global kretprobe_trampoline\n"
  315. "kretprobe_trampoline: bcr 0,0\n");
  316. }
  317. /*
  318. * Called when the probe at kretprobe trampoline is hit
  319. */
  320. static int __kprobes trampoline_probe_handler(struct kprobe *p,
  321. struct pt_regs *regs)
  322. {
  323. struct kretprobe_instance *ri;
  324. struct hlist_head *head, empty_rp;
  325. struct hlist_node *node, *tmp;
  326. unsigned long flags, orig_ret_address;
  327. unsigned long trampoline_address;
  328. kprobe_opcode_t *correct_ret_addr;
  329. INIT_HLIST_HEAD(&empty_rp);
  330. kretprobe_hash_lock(current, &head, &flags);
  331. /*
  332. * It is possible to have multiple instances associated with a given
  333. * task either because an multiple functions in the call path
  334. * have a return probe installed on them, and/or more than one return
  335. * return probe was registered for a target function.
  336. *
  337. * We can handle this because:
  338. * - instances are always inserted at the head of the list
  339. * - when multiple return probes are registered for the same
  340. * function, the first instance's ret_addr will point to the
  341. * real return address, and all the rest will point to
  342. * kretprobe_trampoline
  343. */
  344. ri = NULL;
  345. orig_ret_address = 0;
  346. correct_ret_addr = NULL;
  347. trampoline_address = (unsigned long) &kretprobe_trampoline;
  348. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  349. if (ri->task != current)
  350. /* another task is sharing our hash bucket */
  351. continue;
  352. orig_ret_address = (unsigned long) ri->ret_addr;
  353. if (orig_ret_address != trampoline_address)
  354. /*
  355. * This is the real return address. Any other
  356. * instances associated with this task are for
  357. * other calls deeper on the call stack
  358. */
  359. break;
  360. }
  361. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  362. correct_ret_addr = ri->ret_addr;
  363. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  364. if (ri->task != current)
  365. /* another task is sharing our hash bucket */
  366. continue;
  367. orig_ret_address = (unsigned long) ri->ret_addr;
  368. if (ri->rp && ri->rp->handler) {
  369. ri->ret_addr = correct_ret_addr;
  370. ri->rp->handler(ri, regs);
  371. }
  372. recycle_rp_inst(ri, &empty_rp);
  373. if (orig_ret_address != trampoline_address)
  374. /*
  375. * This is the real return address. Any other
  376. * instances associated with this task are for
  377. * other calls deeper on the call stack
  378. */
  379. break;
  380. }
  381. regs->psw.addr = orig_ret_address | PSW_ADDR_AMODE;
  382. pop_kprobe(get_kprobe_ctlblk());
  383. kretprobe_hash_unlock(current, &flags);
  384. preempt_enable_no_resched();
  385. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  386. hlist_del(&ri->hlist);
  387. kfree(ri);
  388. }
  389. /*
  390. * By returning a non-zero value, we are telling
  391. * kprobe_handler() that we don't want the post_handler
  392. * to run (and have re-enabled preemption)
  393. */
  394. return 1;
  395. }
  396. /*
  397. * Called after single-stepping. p->addr is the address of the
  398. * instruction whose first byte has been replaced by the "breakpoint"
  399. * instruction. To avoid the SMP problems that can occur when we
  400. * temporarily put back the original opcode to single-step, we
  401. * single-stepped a copy of the instruction. The address of this
  402. * copy is p->ainsn.insn.
  403. */
  404. static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
  405. {
  406. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  407. unsigned long ip = regs->psw.addr & PSW_ADDR_INSN;
  408. int fixup = get_fixup_type(p->ainsn.insn);
  409. if (fixup & FIXUP_PSW_NORMAL)
  410. ip += (unsigned long) p->addr - (unsigned long) p->ainsn.insn;
  411. if (fixup & FIXUP_BRANCH_NOT_TAKEN) {
  412. int ilen = ((p->ainsn.insn[0] >> 14) + 3) & -2;
  413. if (ip - (unsigned long) p->ainsn.insn == ilen)
  414. ip = (unsigned long) p->addr + ilen;
  415. }
  416. if (fixup & FIXUP_RETURN_REGISTER) {
  417. int reg = (p->ainsn.insn[0] & 0xf0) >> 4;
  418. regs->gprs[reg] += (unsigned long) p->addr -
  419. (unsigned long) p->ainsn.insn;
  420. }
  421. disable_singlestep(kcb, regs, ip);
  422. }
  423. static int __kprobes post_kprobe_handler(struct pt_regs *regs)
  424. {
  425. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  426. struct kprobe *p = kprobe_running();
  427. if (!p)
  428. return 0;
  429. if (kcb->kprobe_status != KPROBE_REENTER && p->post_handler) {
  430. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  431. p->post_handler(p, regs, 0);
  432. }
  433. resume_execution(p, regs);
  434. pop_kprobe(kcb);
  435. preempt_enable_no_resched();
  436. /*
  437. * if somebody else is singlestepping across a probe point, psw mask
  438. * will have PER set, in which case, continue the remaining processing
  439. * of do_single_step, as if this is not a probe hit.
  440. */
  441. if (regs->psw.mask & PSW_MASK_PER)
  442. return 0;
  443. return 1;
  444. }
  445. static int __kprobes kprobe_trap_handler(struct pt_regs *regs, int trapnr)
  446. {
  447. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  448. struct kprobe *p = kprobe_running();
  449. const struct exception_table_entry *entry;
  450. switch(kcb->kprobe_status) {
  451. case KPROBE_SWAP_INST:
  452. /* We are here because the instruction replacement failed */
  453. return 0;
  454. case KPROBE_HIT_SS:
  455. case KPROBE_REENTER:
  456. /*
  457. * We are here because the instruction being single
  458. * stepped caused a page fault. We reset the current
  459. * kprobe and the nip points back to the probe address
  460. * and allow the page fault handler to continue as a
  461. * normal page fault.
  462. */
  463. disable_singlestep(kcb, regs, (unsigned long) p->addr);
  464. pop_kprobe(kcb);
  465. preempt_enable_no_resched();
  466. break;
  467. case KPROBE_HIT_ACTIVE:
  468. case KPROBE_HIT_SSDONE:
  469. /*
  470. * We increment the nmissed count for accounting,
  471. * we can also use npre/npostfault count for accouting
  472. * these specific fault cases.
  473. */
  474. kprobes_inc_nmissed_count(p);
  475. /*
  476. * We come here because instructions in the pre/post
  477. * handler caused the page_fault, this could happen
  478. * if handler tries to access user space by
  479. * copy_from_user(), get_user() etc. Let the
  480. * user-specified handler try to fix it first.
  481. */
  482. if (p->fault_handler && p->fault_handler(p, regs, trapnr))
  483. return 1;
  484. /*
  485. * In case the user-specified fault handler returned
  486. * zero, try to fix up.
  487. */
  488. entry = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN);
  489. if (entry) {
  490. regs->psw.addr = entry->fixup | PSW_ADDR_AMODE;
  491. return 1;
  492. }
  493. /*
  494. * fixup_exception() could not handle it,
  495. * Let do_page_fault() fix it.
  496. */
  497. break;
  498. default:
  499. break;
  500. }
  501. return 0;
  502. }
  503. int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  504. {
  505. int ret;
  506. if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
  507. local_irq_disable();
  508. ret = kprobe_trap_handler(regs, trapnr);
  509. if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
  510. local_irq_restore(regs->psw.mask & ~PSW_MASK_PER);
  511. return ret;
  512. }
  513. /*
  514. * Wrapper routine to for handling exceptions.
  515. */
  516. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  517. unsigned long val, void *data)
  518. {
  519. struct die_args *args = (struct die_args *) data;
  520. struct pt_regs *regs = args->regs;
  521. int ret = NOTIFY_DONE;
  522. if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
  523. local_irq_disable();
  524. switch (val) {
  525. case DIE_BPT:
  526. if (kprobe_handler(regs))
  527. ret = NOTIFY_STOP;
  528. break;
  529. case DIE_SSTEP:
  530. if (post_kprobe_handler(regs))
  531. ret = NOTIFY_STOP;
  532. break;
  533. case DIE_TRAP:
  534. if (!preemptible() && kprobe_running() &&
  535. kprobe_trap_handler(regs, args->trapnr))
  536. ret = NOTIFY_STOP;
  537. break;
  538. default:
  539. break;
  540. }
  541. if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
  542. local_irq_restore(regs->psw.mask & ~PSW_MASK_PER);
  543. return ret;
  544. }
  545. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  546. {
  547. struct jprobe *jp = container_of(p, struct jprobe, kp);
  548. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  549. unsigned long stack;
  550. memcpy(&kcb->jprobe_saved_regs, regs, sizeof(struct pt_regs));
  551. /* setup return addr to the jprobe handler routine */
  552. regs->psw.addr = (unsigned long) jp->entry | PSW_ADDR_AMODE;
  553. regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT);
  554. /* r15 is the stack pointer */
  555. stack = (unsigned long) regs->gprs[15];
  556. memcpy(kcb->jprobes_stack, (void *) stack, MIN_STACK_SIZE(stack));
  557. return 1;
  558. }
  559. void __kprobes jprobe_return(void)
  560. {
  561. asm volatile(".word 0x0002");
  562. }
  563. void __kprobes jprobe_return_end(void)
  564. {
  565. asm volatile("bcr 0,0");
  566. }
  567. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  568. {
  569. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  570. unsigned long stack;
  571. stack = (unsigned long) kcb->jprobe_saved_regs.gprs[15];
  572. /* Put the regs back */
  573. memcpy(regs, &kcb->jprobe_saved_regs, sizeof(struct pt_regs));
  574. /* put the stack back */
  575. memcpy((void *) stack, kcb->jprobes_stack, MIN_STACK_SIZE(stack));
  576. preempt_enable_no_resched();
  577. return 1;
  578. }
  579. static struct kprobe trampoline = {
  580. .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
  581. .pre_handler = trampoline_probe_handler
  582. };
  583. int __init arch_init_kprobes(void)
  584. {
  585. return register_kprobe(&trampoline);
  586. }
  587. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  588. {
  589. return p->addr == (kprobe_opcode_t *) &kretprobe_trampoline;
  590. }