kprobes.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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. DEFINE_INSN_CACHE_OPS(dmainsn);
  37. static void *alloc_dmainsn_page(void)
  38. {
  39. return (void *)__get_free_page(GFP_KERNEL | GFP_DMA);
  40. }
  41. static void free_dmainsn_page(void *page)
  42. {
  43. free_page((unsigned long)page);
  44. }
  45. struct kprobe_insn_cache kprobe_dmainsn_slots = {
  46. .mutex = __MUTEX_INITIALIZER(kprobe_dmainsn_slots.mutex),
  47. .alloc = alloc_dmainsn_page,
  48. .free = free_dmainsn_page,
  49. .pages = LIST_HEAD_INIT(kprobe_dmainsn_slots.pages),
  50. .insn_size = MAX_INSN_SIZE,
  51. };
  52. static int __kprobes is_prohibited_opcode(kprobe_opcode_t *insn)
  53. {
  54. switch (insn[0] >> 8) {
  55. case 0x0c: /* bassm */
  56. case 0x0b: /* bsm */
  57. case 0x83: /* diag */
  58. case 0x44: /* ex */
  59. case 0xac: /* stnsm */
  60. case 0xad: /* stosm */
  61. return -EINVAL;
  62. }
  63. switch (insn[0]) {
  64. case 0x0101: /* pr */
  65. case 0xb25a: /* bsa */
  66. case 0xb240: /* bakr */
  67. case 0xb258: /* bsg */
  68. case 0xb218: /* pc */
  69. case 0xb228: /* pt */
  70. case 0xb98d: /* epsw */
  71. return -EINVAL;
  72. }
  73. return 0;
  74. }
  75. static int __kprobes get_fixup_type(kprobe_opcode_t *insn)
  76. {
  77. /* default fixup method */
  78. int fixup = FIXUP_PSW_NORMAL;
  79. switch (insn[0] >> 8) {
  80. case 0x05: /* balr */
  81. case 0x0d: /* basr */
  82. fixup = FIXUP_RETURN_REGISTER;
  83. /* if r2 = 0, no branch will be taken */
  84. if ((insn[0] & 0x0f) == 0)
  85. fixup |= FIXUP_BRANCH_NOT_TAKEN;
  86. break;
  87. case 0x06: /* bctr */
  88. case 0x07: /* bcr */
  89. fixup = FIXUP_BRANCH_NOT_TAKEN;
  90. break;
  91. case 0x45: /* bal */
  92. case 0x4d: /* bas */
  93. fixup = FIXUP_RETURN_REGISTER;
  94. break;
  95. case 0x47: /* bc */
  96. case 0x46: /* bct */
  97. case 0x86: /* bxh */
  98. case 0x87: /* bxle */
  99. fixup = FIXUP_BRANCH_NOT_TAKEN;
  100. break;
  101. case 0x82: /* lpsw */
  102. fixup = FIXUP_NOT_REQUIRED;
  103. break;
  104. case 0xb2: /* lpswe */
  105. if ((insn[0] & 0xff) == 0xb2)
  106. fixup = FIXUP_NOT_REQUIRED;
  107. break;
  108. case 0xa7: /* bras */
  109. if ((insn[0] & 0x0f) == 0x05)
  110. fixup |= FIXUP_RETURN_REGISTER;
  111. break;
  112. case 0xc0:
  113. if ((insn[0] & 0x0f) == 0x05) /* brasl */
  114. fixup |= FIXUP_RETURN_REGISTER;
  115. break;
  116. case 0xeb:
  117. switch (insn[2] & 0xff) {
  118. case 0x44: /* bxhg */
  119. case 0x45: /* bxleg */
  120. fixup = FIXUP_BRANCH_NOT_TAKEN;
  121. break;
  122. }
  123. break;
  124. case 0xe3: /* bctg */
  125. if ((insn[2] & 0xff) == 0x46)
  126. fixup = FIXUP_BRANCH_NOT_TAKEN;
  127. break;
  128. case 0xec:
  129. switch (insn[2] & 0xff) {
  130. case 0xe5: /* clgrb */
  131. case 0xe6: /* cgrb */
  132. case 0xf6: /* crb */
  133. case 0xf7: /* clrb */
  134. case 0xfc: /* cgib */
  135. case 0xfd: /* cglib */
  136. case 0xfe: /* cib */
  137. case 0xff: /* clib */
  138. fixup = FIXUP_BRANCH_NOT_TAKEN;
  139. break;
  140. }
  141. break;
  142. }
  143. return fixup;
  144. }
  145. static int __kprobes is_insn_relative_long(kprobe_opcode_t *insn)
  146. {
  147. /* Check if we have a RIL-b or RIL-c format instruction which
  148. * we need to modify in order to avoid instruction emulation. */
  149. switch (insn[0] >> 8) {
  150. case 0xc0:
  151. if ((insn[0] & 0x0f) == 0x00) /* larl */
  152. return true;
  153. break;
  154. case 0xc4:
  155. switch (insn[0] & 0x0f) {
  156. case 0x02: /* llhrl */
  157. case 0x04: /* lghrl */
  158. case 0x05: /* lhrl */
  159. case 0x06: /* llghrl */
  160. case 0x07: /* sthrl */
  161. case 0x08: /* lgrl */
  162. case 0x0b: /* stgrl */
  163. case 0x0c: /* lgfrl */
  164. case 0x0d: /* lrl */
  165. case 0x0e: /* llgfrl */
  166. case 0x0f: /* strl */
  167. return true;
  168. }
  169. break;
  170. case 0xc6:
  171. switch (insn[0] & 0x0f) {
  172. case 0x00: /* exrl */
  173. case 0x02: /* pfdrl */
  174. case 0x04: /* cghrl */
  175. case 0x05: /* chrl */
  176. case 0x06: /* clghrl */
  177. case 0x07: /* clhrl */
  178. case 0x08: /* cgrl */
  179. case 0x0a: /* clgrl */
  180. case 0x0c: /* cgfrl */
  181. case 0x0d: /* crl */
  182. case 0x0e: /* clgfrl */
  183. case 0x0f: /* clrl */
  184. return true;
  185. }
  186. break;
  187. }
  188. return false;
  189. }
  190. static void __kprobes copy_instruction(struct kprobe *p)
  191. {
  192. s64 disp, new_disp;
  193. u64 addr, new_addr;
  194. memcpy(p->ainsn.insn, p->addr, ((p->opcode >> 14) + 3) & -2);
  195. if (!is_insn_relative_long(p->ainsn.insn))
  196. return;
  197. /*
  198. * For pc-relative instructions in RIL-b or RIL-c format patch the
  199. * RI2 displacement field. We have already made sure that the insn
  200. * slot for the patched instruction is within the same 2GB area
  201. * as the original instruction (either kernel image or module area).
  202. * Therefore the new displacement will always fit.
  203. */
  204. disp = *(s32 *)&p->ainsn.insn[1];
  205. addr = (u64)(unsigned long)p->addr;
  206. new_addr = (u64)(unsigned long)p->ainsn.insn;
  207. new_disp = ((addr + (disp * 2)) - new_addr) / 2;
  208. *(s32 *)&p->ainsn.insn[1] = new_disp;
  209. }
  210. static inline int is_kernel_addr(void *addr)
  211. {
  212. return addr < (void *)_end;
  213. }
  214. static inline int is_module_addr(void *addr)
  215. {
  216. #ifdef CONFIG_64BIT
  217. BUILD_BUG_ON(MODULES_LEN > (1UL << 31));
  218. if (addr < (void *)MODULES_VADDR)
  219. return 0;
  220. if (addr > (void *)MODULES_END)
  221. return 0;
  222. #endif
  223. return 1;
  224. }
  225. static int __kprobes s390_get_insn_slot(struct kprobe *p)
  226. {
  227. /*
  228. * Get an insn slot that is within the same 2GB area like the original
  229. * instruction. That way instructions with a 32bit signed displacement
  230. * field can be patched and executed within the insn slot.
  231. */
  232. p->ainsn.insn = NULL;
  233. if (is_kernel_addr(p->addr))
  234. p->ainsn.insn = get_dmainsn_slot();
  235. if (is_module_addr(p->addr))
  236. p->ainsn.insn = get_insn_slot();
  237. return p->ainsn.insn ? 0 : -ENOMEM;
  238. }
  239. static void __kprobes s390_free_insn_slot(struct kprobe *p)
  240. {
  241. if (!p->ainsn.insn)
  242. return;
  243. if (is_kernel_addr(p->addr))
  244. free_dmainsn_slot(p->ainsn.insn, 0);
  245. else
  246. free_insn_slot(p->ainsn.insn, 0);
  247. p->ainsn.insn = NULL;
  248. }
  249. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  250. {
  251. if ((unsigned long) p->addr & 0x01)
  252. return -EINVAL;
  253. /* Make sure the probe isn't going on a difficult instruction */
  254. if (is_prohibited_opcode(p->addr))
  255. return -EINVAL;
  256. if (s390_get_insn_slot(p))
  257. return -ENOMEM;
  258. p->opcode = *p->addr;
  259. copy_instruction(p);
  260. return 0;
  261. }
  262. struct ins_replace_args {
  263. kprobe_opcode_t *ptr;
  264. kprobe_opcode_t opcode;
  265. };
  266. static int __kprobes swap_instruction(void *aref)
  267. {
  268. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  269. unsigned long status = kcb->kprobe_status;
  270. struct ins_replace_args *args = aref;
  271. kcb->kprobe_status = KPROBE_SWAP_INST;
  272. probe_kernel_write(args->ptr, &args->opcode, sizeof(args->opcode));
  273. kcb->kprobe_status = status;
  274. return 0;
  275. }
  276. void __kprobes arch_arm_kprobe(struct kprobe *p)
  277. {
  278. struct ins_replace_args args;
  279. args.ptr = p->addr;
  280. args.opcode = BREAKPOINT_INSTRUCTION;
  281. stop_machine(swap_instruction, &args, NULL);
  282. }
  283. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  284. {
  285. struct ins_replace_args args;
  286. args.ptr = p->addr;
  287. args.opcode = p->opcode;
  288. stop_machine(swap_instruction, &args, NULL);
  289. }
  290. void __kprobes arch_remove_kprobe(struct kprobe *p)
  291. {
  292. s390_free_insn_slot(p);
  293. }
  294. static void __kprobes enable_singlestep(struct kprobe_ctlblk *kcb,
  295. struct pt_regs *regs,
  296. unsigned long ip)
  297. {
  298. struct per_regs per_kprobe;
  299. /* Set up the PER control registers %cr9-%cr11 */
  300. per_kprobe.control = PER_EVENT_IFETCH;
  301. per_kprobe.start = ip;
  302. per_kprobe.end = ip;
  303. /* Save control regs and psw mask */
  304. __ctl_store(kcb->kprobe_saved_ctl, 9, 11);
  305. kcb->kprobe_saved_imask = regs->psw.mask &
  306. (PSW_MASK_PER | PSW_MASK_IO | PSW_MASK_EXT);
  307. /* Set PER control regs, turns on single step for the given address */
  308. __ctl_load(per_kprobe, 9, 11);
  309. regs->psw.mask |= PSW_MASK_PER;
  310. regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT);
  311. regs->psw.addr = ip | PSW_ADDR_AMODE;
  312. }
  313. static void __kprobes disable_singlestep(struct kprobe_ctlblk *kcb,
  314. struct pt_regs *regs,
  315. unsigned long ip)
  316. {
  317. /* Restore control regs and psw mask, set new psw address */
  318. __ctl_load(kcb->kprobe_saved_ctl, 9, 11);
  319. regs->psw.mask &= ~PSW_MASK_PER;
  320. regs->psw.mask |= kcb->kprobe_saved_imask;
  321. regs->psw.addr = ip | PSW_ADDR_AMODE;
  322. }
  323. /*
  324. * Activate a kprobe by storing its pointer to current_kprobe. The
  325. * previous kprobe is stored in kcb->prev_kprobe. A stack of up to
  326. * two kprobes can be active, see KPROBE_REENTER.
  327. */
  328. static void __kprobes push_kprobe(struct kprobe_ctlblk *kcb, struct kprobe *p)
  329. {
  330. kcb->prev_kprobe.kp = __get_cpu_var(current_kprobe);
  331. kcb->prev_kprobe.status = kcb->kprobe_status;
  332. __get_cpu_var(current_kprobe) = p;
  333. }
  334. /*
  335. * Deactivate a kprobe by backing up to the previous state. If the
  336. * current state is KPROBE_REENTER prev_kprobe.kp will be non-NULL,
  337. * for any other state prev_kprobe.kp will be NULL.
  338. */
  339. static void __kprobes pop_kprobe(struct kprobe_ctlblk *kcb)
  340. {
  341. __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
  342. kcb->kprobe_status = kcb->prev_kprobe.status;
  343. }
  344. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  345. struct pt_regs *regs)
  346. {
  347. ri->ret_addr = (kprobe_opcode_t *) regs->gprs[14];
  348. /* Replace the return addr with trampoline addr */
  349. regs->gprs[14] = (unsigned long) &kretprobe_trampoline;
  350. }
  351. static void __kprobes kprobe_reenter_check(struct kprobe_ctlblk *kcb,
  352. struct kprobe *p)
  353. {
  354. switch (kcb->kprobe_status) {
  355. case KPROBE_HIT_SSDONE:
  356. case KPROBE_HIT_ACTIVE:
  357. kprobes_inc_nmissed_count(p);
  358. break;
  359. case KPROBE_HIT_SS:
  360. case KPROBE_REENTER:
  361. default:
  362. /*
  363. * A kprobe on the code path to single step an instruction
  364. * is a BUG. The code path resides in the .kprobes.text
  365. * section and is executed with interrupts disabled.
  366. */
  367. printk(KERN_EMERG "Invalid kprobe detected at %p.\n", p->addr);
  368. dump_kprobe(p);
  369. BUG();
  370. }
  371. }
  372. static int __kprobes kprobe_handler(struct pt_regs *regs)
  373. {
  374. struct kprobe_ctlblk *kcb;
  375. struct kprobe *p;
  376. /*
  377. * We want to disable preemption for the entire duration of kprobe
  378. * processing. That includes the calls to the pre/post handlers
  379. * and single stepping the kprobe instruction.
  380. */
  381. preempt_disable();
  382. kcb = get_kprobe_ctlblk();
  383. p = get_kprobe((void *)((regs->psw.addr & PSW_ADDR_INSN) - 2));
  384. if (p) {
  385. if (kprobe_running()) {
  386. /*
  387. * We have hit a kprobe while another is still
  388. * active. This can happen in the pre and post
  389. * handler. Single step the instruction of the
  390. * new probe but do not call any handler function
  391. * of this secondary kprobe.
  392. * push_kprobe and pop_kprobe saves and restores
  393. * the currently active kprobe.
  394. */
  395. kprobe_reenter_check(kcb, p);
  396. push_kprobe(kcb, p);
  397. kcb->kprobe_status = KPROBE_REENTER;
  398. } else {
  399. /*
  400. * If we have no pre-handler or it returned 0, we
  401. * continue with single stepping. If we have a
  402. * pre-handler and it returned non-zero, it prepped
  403. * for calling the break_handler below on re-entry
  404. * for jprobe processing, so get out doing nothing
  405. * more here.
  406. */
  407. push_kprobe(kcb, p);
  408. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  409. if (p->pre_handler && p->pre_handler(p, regs))
  410. return 1;
  411. kcb->kprobe_status = KPROBE_HIT_SS;
  412. }
  413. enable_singlestep(kcb, regs, (unsigned long) p->ainsn.insn);
  414. return 1;
  415. } else if (kprobe_running()) {
  416. p = __get_cpu_var(current_kprobe);
  417. if (p->break_handler && p->break_handler(p, regs)) {
  418. /*
  419. * Continuation after the jprobe completed and
  420. * caused the jprobe_return trap. The jprobe
  421. * break_handler "returns" to the original
  422. * function that still has the kprobe breakpoint
  423. * installed. We continue with single stepping.
  424. */
  425. kcb->kprobe_status = KPROBE_HIT_SS;
  426. enable_singlestep(kcb, regs,
  427. (unsigned long) p->ainsn.insn);
  428. return 1;
  429. } /* else:
  430. * No kprobe at this address and the current kprobe
  431. * has no break handler (no jprobe!). The kernel just
  432. * exploded, let the standard trap handler pick up the
  433. * pieces.
  434. */
  435. } /* else:
  436. * No kprobe at this address and no active kprobe. The trap has
  437. * not been caused by a kprobe breakpoint. The race of breakpoint
  438. * vs. kprobe remove does not exist because on s390 as we use
  439. * stop_machine to arm/disarm the breakpoints.
  440. */
  441. preempt_enable_no_resched();
  442. return 0;
  443. }
  444. /*
  445. * Function return probe trampoline:
  446. * - init_kprobes() establishes a probepoint here
  447. * - When the probed function returns, this probe
  448. * causes the handlers to fire
  449. */
  450. static void __used kretprobe_trampoline_holder(void)
  451. {
  452. asm volatile(".global kretprobe_trampoline\n"
  453. "kretprobe_trampoline: bcr 0,0\n");
  454. }
  455. /*
  456. * Called when the probe at kretprobe trampoline is hit
  457. */
  458. static int __kprobes trampoline_probe_handler(struct kprobe *p,
  459. struct pt_regs *regs)
  460. {
  461. struct kretprobe_instance *ri;
  462. struct hlist_head *head, empty_rp;
  463. struct hlist_node *tmp;
  464. unsigned long flags, orig_ret_address;
  465. unsigned long trampoline_address;
  466. kprobe_opcode_t *correct_ret_addr;
  467. INIT_HLIST_HEAD(&empty_rp);
  468. kretprobe_hash_lock(current, &head, &flags);
  469. /*
  470. * It is possible to have multiple instances associated with a given
  471. * task either because an multiple functions in the call path
  472. * have a return probe installed on them, and/or more than one return
  473. * return probe was registered for a target function.
  474. *
  475. * We can handle this because:
  476. * - instances are always inserted at the head of the list
  477. * - when multiple return probes are registered for the same
  478. * function, the first instance's ret_addr will point to the
  479. * real return address, and all the rest will point to
  480. * kretprobe_trampoline
  481. */
  482. ri = NULL;
  483. orig_ret_address = 0;
  484. correct_ret_addr = NULL;
  485. trampoline_address = (unsigned long) &kretprobe_trampoline;
  486. hlist_for_each_entry_safe(ri, tmp, head, hlist) {
  487. if (ri->task != current)
  488. /* another task is sharing our hash bucket */
  489. continue;
  490. orig_ret_address = (unsigned long) ri->ret_addr;
  491. if (orig_ret_address != trampoline_address)
  492. /*
  493. * This is the real return address. Any other
  494. * instances associated with this task are for
  495. * other calls deeper on the call stack
  496. */
  497. break;
  498. }
  499. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  500. correct_ret_addr = ri->ret_addr;
  501. hlist_for_each_entry_safe(ri, tmp, head, hlist) {
  502. if (ri->task != current)
  503. /* another task is sharing our hash bucket */
  504. continue;
  505. orig_ret_address = (unsigned long) ri->ret_addr;
  506. if (ri->rp && ri->rp->handler) {
  507. ri->ret_addr = correct_ret_addr;
  508. ri->rp->handler(ri, regs);
  509. }
  510. recycle_rp_inst(ri, &empty_rp);
  511. if (orig_ret_address != trampoline_address)
  512. /*
  513. * This is the real return address. Any other
  514. * instances associated with this task are for
  515. * other calls deeper on the call stack
  516. */
  517. break;
  518. }
  519. regs->psw.addr = orig_ret_address | PSW_ADDR_AMODE;
  520. pop_kprobe(get_kprobe_ctlblk());
  521. kretprobe_hash_unlock(current, &flags);
  522. preempt_enable_no_resched();
  523. hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
  524. hlist_del(&ri->hlist);
  525. kfree(ri);
  526. }
  527. /*
  528. * By returning a non-zero value, we are telling
  529. * kprobe_handler() that we don't want the post_handler
  530. * to run (and have re-enabled preemption)
  531. */
  532. return 1;
  533. }
  534. /*
  535. * Called after single-stepping. p->addr is the address of the
  536. * instruction whose first byte has been replaced by the "breakpoint"
  537. * instruction. To avoid the SMP problems that can occur when we
  538. * temporarily put back the original opcode to single-step, we
  539. * single-stepped a copy of the instruction. The address of this
  540. * copy is p->ainsn.insn.
  541. */
  542. static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
  543. {
  544. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  545. unsigned long ip = regs->psw.addr & PSW_ADDR_INSN;
  546. int fixup = get_fixup_type(p->ainsn.insn);
  547. if (fixup & FIXUP_PSW_NORMAL)
  548. ip += (unsigned long) p->addr - (unsigned long) p->ainsn.insn;
  549. if (fixup & FIXUP_BRANCH_NOT_TAKEN) {
  550. int ilen = ((p->ainsn.insn[0] >> 14) + 3) & -2;
  551. if (ip - (unsigned long) p->ainsn.insn == ilen)
  552. ip = (unsigned long) p->addr + ilen;
  553. }
  554. if (fixup & FIXUP_RETURN_REGISTER) {
  555. int reg = (p->ainsn.insn[0] & 0xf0) >> 4;
  556. regs->gprs[reg] += (unsigned long) p->addr -
  557. (unsigned long) p->ainsn.insn;
  558. }
  559. disable_singlestep(kcb, regs, ip);
  560. }
  561. static int __kprobes post_kprobe_handler(struct pt_regs *regs)
  562. {
  563. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  564. struct kprobe *p = kprobe_running();
  565. if (!p)
  566. return 0;
  567. if (kcb->kprobe_status != KPROBE_REENTER && p->post_handler) {
  568. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  569. p->post_handler(p, regs, 0);
  570. }
  571. resume_execution(p, regs);
  572. pop_kprobe(kcb);
  573. preempt_enable_no_resched();
  574. /*
  575. * if somebody else is singlestepping across a probe point, psw mask
  576. * will have PER set, in which case, continue the remaining processing
  577. * of do_single_step, as if this is not a probe hit.
  578. */
  579. if (regs->psw.mask & PSW_MASK_PER)
  580. return 0;
  581. return 1;
  582. }
  583. static int __kprobes kprobe_trap_handler(struct pt_regs *regs, int trapnr)
  584. {
  585. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  586. struct kprobe *p = kprobe_running();
  587. const struct exception_table_entry *entry;
  588. switch(kcb->kprobe_status) {
  589. case KPROBE_SWAP_INST:
  590. /* We are here because the instruction replacement failed */
  591. return 0;
  592. case KPROBE_HIT_SS:
  593. case KPROBE_REENTER:
  594. /*
  595. * We are here because the instruction being single
  596. * stepped caused a page fault. We reset the current
  597. * kprobe and the nip points back to the probe address
  598. * and allow the page fault handler to continue as a
  599. * normal page fault.
  600. */
  601. disable_singlestep(kcb, regs, (unsigned long) p->addr);
  602. pop_kprobe(kcb);
  603. preempt_enable_no_resched();
  604. break;
  605. case KPROBE_HIT_ACTIVE:
  606. case KPROBE_HIT_SSDONE:
  607. /*
  608. * We increment the nmissed count for accounting,
  609. * we can also use npre/npostfault count for accouting
  610. * these specific fault cases.
  611. */
  612. kprobes_inc_nmissed_count(p);
  613. /*
  614. * We come here because instructions in the pre/post
  615. * handler caused the page_fault, this could happen
  616. * if handler tries to access user space by
  617. * copy_from_user(), get_user() etc. Let the
  618. * user-specified handler try to fix it first.
  619. */
  620. if (p->fault_handler && p->fault_handler(p, regs, trapnr))
  621. return 1;
  622. /*
  623. * In case the user-specified fault handler returned
  624. * zero, try to fix up.
  625. */
  626. entry = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN);
  627. if (entry) {
  628. regs->psw.addr = extable_fixup(entry) | PSW_ADDR_AMODE;
  629. return 1;
  630. }
  631. /*
  632. * fixup_exception() could not handle it,
  633. * Let do_page_fault() fix it.
  634. */
  635. break;
  636. default:
  637. break;
  638. }
  639. return 0;
  640. }
  641. int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  642. {
  643. int ret;
  644. if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
  645. local_irq_disable();
  646. ret = kprobe_trap_handler(regs, trapnr);
  647. if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
  648. local_irq_restore(regs->psw.mask & ~PSW_MASK_PER);
  649. return ret;
  650. }
  651. /*
  652. * Wrapper routine to for handling exceptions.
  653. */
  654. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  655. unsigned long val, void *data)
  656. {
  657. struct die_args *args = (struct die_args *) data;
  658. struct pt_regs *regs = args->regs;
  659. int ret = NOTIFY_DONE;
  660. if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
  661. local_irq_disable();
  662. switch (val) {
  663. case DIE_BPT:
  664. if (kprobe_handler(regs))
  665. ret = NOTIFY_STOP;
  666. break;
  667. case DIE_SSTEP:
  668. if (post_kprobe_handler(regs))
  669. ret = NOTIFY_STOP;
  670. break;
  671. case DIE_TRAP:
  672. if (!preemptible() && kprobe_running() &&
  673. kprobe_trap_handler(regs, args->trapnr))
  674. ret = NOTIFY_STOP;
  675. break;
  676. default:
  677. break;
  678. }
  679. if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
  680. local_irq_restore(regs->psw.mask & ~PSW_MASK_PER);
  681. return ret;
  682. }
  683. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  684. {
  685. struct jprobe *jp = container_of(p, struct jprobe, kp);
  686. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  687. unsigned long stack;
  688. memcpy(&kcb->jprobe_saved_regs, regs, sizeof(struct pt_regs));
  689. /* setup return addr to the jprobe handler routine */
  690. regs->psw.addr = (unsigned long) jp->entry | PSW_ADDR_AMODE;
  691. regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT);
  692. /* r15 is the stack pointer */
  693. stack = (unsigned long) regs->gprs[15];
  694. memcpy(kcb->jprobes_stack, (void *) stack, MIN_STACK_SIZE(stack));
  695. return 1;
  696. }
  697. void __kprobes jprobe_return(void)
  698. {
  699. asm volatile(".word 0x0002");
  700. }
  701. static void __used __kprobes jprobe_return_end(void)
  702. {
  703. asm volatile("bcr 0,0");
  704. }
  705. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  706. {
  707. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  708. unsigned long stack;
  709. stack = (unsigned long) kcb->jprobe_saved_regs.gprs[15];
  710. /* Put the regs back */
  711. memcpy(regs, &kcb->jprobe_saved_regs, sizeof(struct pt_regs));
  712. /* put the stack back */
  713. memcpy((void *) stack, kcb->jprobes_stack, MIN_STACK_SIZE(stack));
  714. preempt_enable_no_resched();
  715. return 1;
  716. }
  717. static struct kprobe trampoline = {
  718. .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
  719. .pre_handler = trampoline_probe_handler
  720. };
  721. int __init arch_init_kprobes(void)
  722. {
  723. return register_kprobe(&trampoline);
  724. }
  725. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  726. {
  727. return p->addr == (kprobe_opcode_t *) &kretprobe_trampoline;
  728. }