kprobes.c 18 KB

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