kprobes.c 22 KB

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