kprobes.c 18 KB

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