kprobes.c 18 KB

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