kprobes.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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. int err = -EFAULT;
  146. asm volatile(
  147. "0: mvc 0(2,%2),0(%3)\n"
  148. "1: la %0,0\n"
  149. "2:\n"
  150. EX_TABLE(0b,2b)
  151. : "+d" (err), "=m" (*args->ptr)
  152. : "a" (args->ptr), "a" (&args->new), "m" (args->new));
  153. return err;
  154. }
  155. void __kprobes arch_arm_kprobe(struct kprobe *p)
  156. {
  157. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  158. unsigned long status = kcb->kprobe_status;
  159. struct ins_replace_args args;
  160. args.ptr = p->addr;
  161. args.old = p->opcode;
  162. args.new = BREAKPOINT_INSTRUCTION;
  163. kcb->kprobe_status = KPROBE_SWAP_INST;
  164. stop_machine_run(swap_instruction, &args, NR_CPUS);
  165. kcb->kprobe_status = status;
  166. }
  167. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  168. {
  169. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  170. unsigned long status = kcb->kprobe_status;
  171. struct ins_replace_args args;
  172. args.ptr = p->addr;
  173. args.old = BREAKPOINT_INSTRUCTION;
  174. args.new = p->opcode;
  175. kcb->kprobe_status = KPROBE_SWAP_INST;
  176. stop_machine_run(swap_instruction, &args, NR_CPUS);
  177. kcb->kprobe_status = status;
  178. }
  179. void __kprobes arch_remove_kprobe(struct kprobe *p)
  180. {
  181. mutex_lock(&kprobe_mutex);
  182. free_insn_slot(p->ainsn.insn, 0);
  183. mutex_unlock(&kprobe_mutex);
  184. }
  185. static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  186. {
  187. per_cr_bits kprobe_per_regs[1];
  188. memset(kprobe_per_regs, 0, sizeof(per_cr_bits));
  189. regs->psw.addr = (unsigned long)p->ainsn.insn | PSW_ADDR_AMODE;
  190. /* Set up the per control reg info, will pass to lctl */
  191. kprobe_per_regs[0].em_instruction_fetch = 1;
  192. kprobe_per_regs[0].starting_addr = (unsigned long)p->ainsn.insn;
  193. kprobe_per_regs[0].ending_addr = (unsigned long)p->ainsn.insn + 1;
  194. /* Set the PER control regs, turns on single step for this address */
  195. __ctl_load(kprobe_per_regs, 9, 11);
  196. regs->psw.mask |= PSW_MASK_PER;
  197. regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK);
  198. }
  199. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  200. {
  201. kcb->prev_kprobe.kp = kprobe_running();
  202. kcb->prev_kprobe.status = kcb->kprobe_status;
  203. kcb->prev_kprobe.kprobe_saved_imask = kcb->kprobe_saved_imask;
  204. memcpy(kcb->prev_kprobe.kprobe_saved_ctl, kcb->kprobe_saved_ctl,
  205. sizeof(kcb->kprobe_saved_ctl));
  206. }
  207. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  208. {
  209. __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
  210. kcb->kprobe_status = kcb->prev_kprobe.status;
  211. kcb->kprobe_saved_imask = kcb->prev_kprobe.kprobe_saved_imask;
  212. memcpy(kcb->kprobe_saved_ctl, kcb->prev_kprobe.kprobe_saved_ctl,
  213. sizeof(kcb->kprobe_saved_ctl));
  214. }
  215. static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  216. struct kprobe_ctlblk *kcb)
  217. {
  218. __get_cpu_var(current_kprobe) = p;
  219. /* Save the interrupt and per flags */
  220. kcb->kprobe_saved_imask = regs->psw.mask &
  221. (PSW_MASK_PER | PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK);
  222. /* Save the control regs that govern PER */
  223. __ctl_store(kcb->kprobe_saved_ctl, 9, 11);
  224. }
  225. /* Called with kretprobe_lock held */
  226. void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
  227. struct pt_regs *regs)
  228. {
  229. struct kretprobe_instance *ri;
  230. if ((ri = get_free_rp_inst(rp)) != NULL) {
  231. ri->rp = rp;
  232. ri->task = current;
  233. ri->ret_addr = (kprobe_opcode_t *) regs->gprs[14];
  234. /* Replace the return addr with trampoline addr */
  235. regs->gprs[14] = (unsigned long)&kretprobe_trampoline;
  236. add_rp_inst(ri);
  237. } else {
  238. rp->nmissed++;
  239. }
  240. }
  241. static int __kprobes kprobe_handler(struct pt_regs *regs)
  242. {
  243. struct kprobe *p;
  244. int ret = 0;
  245. unsigned long *addr = (unsigned long *)
  246. ((regs->psw.addr & PSW_ADDR_INSN) - 2);
  247. struct kprobe_ctlblk *kcb;
  248. /*
  249. * We don't want to be preempted for the entire
  250. * duration of kprobe processing
  251. */
  252. preempt_disable();
  253. kcb = get_kprobe_ctlblk();
  254. /* Check we're not actually recursing */
  255. if (kprobe_running()) {
  256. p = get_kprobe(addr);
  257. if (p) {
  258. if (kcb->kprobe_status == KPROBE_HIT_SS &&
  259. *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
  260. regs->psw.mask &= ~PSW_MASK_PER;
  261. regs->psw.mask |= kcb->kprobe_saved_imask;
  262. goto no_kprobe;
  263. }
  264. /* We have reentered the kprobe_handler(), since
  265. * another probe was hit while within the handler.
  266. * We here save the original kprobes variables and
  267. * just single step on the instruction of the new probe
  268. * without calling any user handlers.
  269. */
  270. save_previous_kprobe(kcb);
  271. set_current_kprobe(p, regs, kcb);
  272. kprobes_inc_nmissed_count(p);
  273. prepare_singlestep(p, regs);
  274. kcb->kprobe_status = KPROBE_REENTER;
  275. return 1;
  276. } else {
  277. p = __get_cpu_var(current_kprobe);
  278. if (p->break_handler && p->break_handler(p, regs)) {
  279. goto ss_probe;
  280. }
  281. }
  282. goto no_kprobe;
  283. }
  284. p = get_kprobe(addr);
  285. if (!p) {
  286. if (*addr != BREAKPOINT_INSTRUCTION) {
  287. /*
  288. * The breakpoint instruction was removed right
  289. * after we hit it. Another cpu has removed
  290. * either a probepoint or a debugger breakpoint
  291. * at this address. In either case, no further
  292. * handling of this interrupt is appropriate.
  293. *
  294. */
  295. ret = 1;
  296. }
  297. /* Not one of ours: let kernel handle it */
  298. goto no_kprobe;
  299. }
  300. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  301. set_current_kprobe(p, regs, kcb);
  302. if (p->pre_handler && p->pre_handler(p, regs))
  303. /* handler has already set things up, so skip ss setup */
  304. return 1;
  305. ss_probe:
  306. prepare_singlestep(p, regs);
  307. kcb->kprobe_status = KPROBE_HIT_SS;
  308. return 1;
  309. no_kprobe:
  310. preempt_enable_no_resched();
  311. return ret;
  312. }
  313. /*
  314. * Function return probe trampoline:
  315. * - init_kprobes() establishes a probepoint here
  316. * - When the probed function returns, this probe
  317. * causes the handlers to fire
  318. */
  319. void __kprobes kretprobe_trampoline_holder(void)
  320. {
  321. asm volatile(".global kretprobe_trampoline\n"
  322. "kretprobe_trampoline: bcr 0,0\n");
  323. }
  324. /*
  325. * Called when the probe at kretprobe trampoline is hit
  326. */
  327. static int __kprobes trampoline_probe_handler(struct kprobe *p,
  328. struct pt_regs *regs)
  329. {
  330. struct kretprobe_instance *ri = NULL;
  331. struct hlist_head *head, empty_rp;
  332. struct hlist_node *node, *tmp;
  333. unsigned long flags, orig_ret_address = 0;
  334. unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
  335. INIT_HLIST_HEAD(&empty_rp);
  336. spin_lock_irqsave(&kretprobe_lock, flags);
  337. head = kretprobe_inst_table_head(current);
  338. /*
  339. * It is possible to have multiple instances associated with a given
  340. * task either because an multiple functions in the call path
  341. * have a return probe installed on them, and/or more then one return
  342. * return probe was registered for a target function.
  343. *
  344. * We can handle this because:
  345. * - instances are always inserted at the head of the list
  346. * - when multiple return probes are registered for the same
  347. * function, the first instance's ret_addr will point to the
  348. * real return address, and all the rest will point to
  349. * kretprobe_trampoline
  350. */
  351. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  352. if (ri->task != current)
  353. /* another task is sharing our hash bucket */
  354. continue;
  355. if (ri->rp && ri->rp->handler)
  356. ri->rp->handler(ri, regs);
  357. orig_ret_address = (unsigned long)ri->ret_addr;
  358. recycle_rp_inst(ri, &empty_rp);
  359. if (orig_ret_address != trampoline_address) {
  360. /*
  361. * This is the real return address. Any other
  362. * instances associated with this task are for
  363. * other calls deeper on the call stack
  364. */
  365. break;
  366. }
  367. }
  368. BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
  369. regs->psw.addr = orig_ret_address | PSW_ADDR_AMODE;
  370. reset_current_kprobe();
  371. spin_unlock_irqrestore(&kretprobe_lock, flags);
  372. preempt_enable_no_resched();
  373. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  374. hlist_del(&ri->hlist);
  375. kfree(ri);
  376. }
  377. /*
  378. * By returning a non-zero value, we are telling
  379. * kprobe_handler() that we don't want the post_handler
  380. * to run (and have re-enabled preemption)
  381. */
  382. return 1;
  383. }
  384. /*
  385. * Called after single-stepping. p->addr is the address of the
  386. * instruction whose first byte has been replaced by the "breakpoint"
  387. * instruction. To avoid the SMP problems that can occur when we
  388. * temporarily put back the original opcode to single-step, we
  389. * single-stepped a copy of the instruction. The address of this
  390. * copy is p->ainsn.insn.
  391. */
  392. static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
  393. {
  394. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  395. regs->psw.addr &= PSW_ADDR_INSN;
  396. if (p->ainsn.fixup & FIXUP_PSW_NORMAL)
  397. regs->psw.addr = (unsigned long)p->addr +
  398. ((unsigned long)regs->psw.addr -
  399. (unsigned long)p->ainsn.insn);
  400. if (p->ainsn.fixup & FIXUP_BRANCH_NOT_TAKEN)
  401. if ((unsigned long)regs->psw.addr -
  402. (unsigned long)p->ainsn.insn == p->ainsn.ilen)
  403. regs->psw.addr = (unsigned long)p->addr + p->ainsn.ilen;
  404. if (p->ainsn.fixup & FIXUP_RETURN_REGISTER)
  405. regs->gprs[p->ainsn.reg] = ((unsigned long)p->addr +
  406. (regs->gprs[p->ainsn.reg] -
  407. (unsigned long)p->ainsn.insn))
  408. | PSW_ADDR_AMODE;
  409. regs->psw.addr |= PSW_ADDR_AMODE;
  410. /* turn off PER mode */
  411. regs->psw.mask &= ~PSW_MASK_PER;
  412. /* Restore the original per control regs */
  413. __ctl_load(kcb->kprobe_saved_ctl, 9, 11);
  414. regs->psw.mask |= kcb->kprobe_saved_imask;
  415. }
  416. static int __kprobes post_kprobe_handler(struct pt_regs *regs)
  417. {
  418. struct kprobe *cur = kprobe_running();
  419. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  420. if (!cur)
  421. return 0;
  422. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  423. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  424. cur->post_handler(cur, regs, 0);
  425. }
  426. resume_execution(cur, regs);
  427. /*Restore back the original saved kprobes variables and continue. */
  428. if (kcb->kprobe_status == KPROBE_REENTER) {
  429. restore_previous_kprobe(kcb);
  430. goto out;
  431. }
  432. reset_current_kprobe();
  433. out:
  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. }
  443. return 1;
  444. }
  445. static int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  446. {
  447. struct kprobe *cur = kprobe_running();
  448. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  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. regs->psw.addr = (unsigned long)cur->addr | PSW_ADDR_AMODE;
  464. regs->psw.mask &= ~PSW_MASK_PER;
  465. regs->psw.mask |= kcb->kprobe_saved_imask;
  466. if (kcb->kprobe_status == KPROBE_REENTER)
  467. restore_previous_kprobe(kcb);
  468. else
  469. reset_current_kprobe();
  470. preempt_enable_no_resched();
  471. break;
  472. case KPROBE_HIT_ACTIVE:
  473. case KPROBE_HIT_SSDONE:
  474. /*
  475. * We increment the nmissed count for accounting,
  476. * we can also use npre/npostfault count for accouting
  477. * these specific fault cases.
  478. */
  479. kprobes_inc_nmissed_count(cur);
  480. /*
  481. * We come here because instructions in the pre/post
  482. * handler caused the page_fault, this could happen
  483. * if handler tries to access user space by
  484. * copy_from_user(), get_user() etc. Let the
  485. * user-specified handler try to fix it first.
  486. */
  487. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  488. return 1;
  489. /*
  490. * In case the user-specified fault handler returned
  491. * zero, try to fix up.
  492. */
  493. entry = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN);
  494. if (entry) {
  495. regs->psw.addr = entry->fixup | PSW_ADDR_AMODE;
  496. return 1;
  497. }
  498. /*
  499. * fixup_exception() could not handle it,
  500. * Let do_page_fault() fix it.
  501. */
  502. break;
  503. default:
  504. break;
  505. }
  506. return 0;
  507. }
  508. /*
  509. * Wrapper routine to for handling exceptions.
  510. */
  511. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  512. unsigned long val, void *data)
  513. {
  514. struct die_args *args = (struct die_args *)data;
  515. int ret = NOTIFY_DONE;
  516. switch (val) {
  517. case DIE_BPT:
  518. if (kprobe_handler(args->regs))
  519. ret = NOTIFY_STOP;
  520. break;
  521. case DIE_SSTEP:
  522. if (post_kprobe_handler(args->regs))
  523. ret = NOTIFY_STOP;
  524. break;
  525. case DIE_TRAP:
  526. case DIE_PAGE_FAULT:
  527. /* kprobe_running() needs smp_processor_id() */
  528. preempt_disable();
  529. if (kprobe_running() &&
  530. kprobe_fault_handler(args->regs, args->trapnr))
  531. ret = NOTIFY_STOP;
  532. preempt_enable();
  533. break;
  534. default:
  535. break;
  536. }
  537. return ret;
  538. }
  539. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  540. {
  541. struct jprobe *jp = container_of(p, struct jprobe, kp);
  542. unsigned long addr;
  543. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  544. memcpy(&kcb->jprobe_saved_regs, regs, sizeof(struct pt_regs));
  545. /* setup return addr to the jprobe handler routine */
  546. regs->psw.addr = (unsigned long)(jp->entry) | PSW_ADDR_AMODE;
  547. /* r14 is the function return address */
  548. kcb->jprobe_saved_r14 = (unsigned long)regs->gprs[14];
  549. /* r15 is the stack pointer */
  550. kcb->jprobe_saved_r15 = (unsigned long)regs->gprs[15];
  551. addr = (unsigned long)kcb->jprobe_saved_r15;
  552. memcpy(kcb->jprobes_stack, (kprobe_opcode_t *) addr,
  553. MIN_STACK_SIZE(addr));
  554. return 1;
  555. }
  556. void __kprobes jprobe_return(void)
  557. {
  558. asm volatile(".word 0x0002");
  559. }
  560. void __kprobes jprobe_return_end(void)
  561. {
  562. asm volatile("bcr 0,0");
  563. }
  564. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  565. {
  566. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  567. unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_r15);
  568. /* Put the regs back */
  569. memcpy(regs, &kcb->jprobe_saved_regs, sizeof(struct pt_regs));
  570. /* put the stack back */
  571. memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
  572. MIN_STACK_SIZE(stack_addr));
  573. preempt_enable_no_resched();
  574. return 1;
  575. }
  576. static struct kprobe trampoline_p = {
  577. .addr = (kprobe_opcode_t *) & kretprobe_trampoline,
  578. .pre_handler = trampoline_probe_handler
  579. };
  580. int __init arch_init_kprobes(void)
  581. {
  582. return register_kprobe(&trampoline_p);
  583. }