kprobes.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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 <linux/module.h>
  29. #include <linux/slab.h>
  30. #include <linux/hardirq.h>
  31. #include <asm/cacheflush.h>
  32. #include <asm/sections.h>
  33. #include <asm/dis.h>
  34. DEFINE_PER_CPU(struct kprobe *, current_kprobe);
  35. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  36. struct kretprobe_blackpoint kretprobe_blacklist[] = { };
  37. DEFINE_INSN_CACHE_OPS(dmainsn);
  38. static void *alloc_dmainsn_page(void)
  39. {
  40. return (void *)__get_free_page(GFP_KERNEL | GFP_DMA);
  41. }
  42. static void free_dmainsn_page(void *page)
  43. {
  44. free_page((unsigned long)page);
  45. }
  46. struct kprobe_insn_cache kprobe_dmainsn_slots = {
  47. .mutex = __MUTEX_INITIALIZER(kprobe_dmainsn_slots.mutex),
  48. .alloc = alloc_dmainsn_page,
  49. .free = free_dmainsn_page,
  50. .pages = LIST_HEAD_INIT(kprobe_dmainsn_slots.pages),
  51. .insn_size = MAX_INSN_SIZE,
  52. };
  53. static void __kprobes copy_instruction(struct kprobe *p)
  54. {
  55. s64 disp, new_disp;
  56. u64 addr, new_addr;
  57. memcpy(p->ainsn.insn, p->addr, insn_length(p->opcode >> 8));
  58. if (!probe_is_insn_relative_long(p->ainsn.insn))
  59. return;
  60. /*
  61. * For pc-relative instructions in RIL-b or RIL-c format patch the
  62. * RI2 displacement field. We have already made sure that the insn
  63. * slot for the patched instruction is within the same 2GB area
  64. * as the original instruction (either kernel image or module area).
  65. * Therefore the new displacement will always fit.
  66. */
  67. disp = *(s32 *)&p->ainsn.insn[1];
  68. addr = (u64)(unsigned long)p->addr;
  69. new_addr = (u64)(unsigned long)p->ainsn.insn;
  70. new_disp = ((addr + (disp * 2)) - new_addr) / 2;
  71. *(s32 *)&p->ainsn.insn[1] = new_disp;
  72. }
  73. static inline int is_kernel_addr(void *addr)
  74. {
  75. return addr < (void *)_end;
  76. }
  77. static inline int is_module_addr(void *addr)
  78. {
  79. #ifdef CONFIG_64BIT
  80. BUILD_BUG_ON(MODULES_LEN > (1UL << 31));
  81. if (addr < (void *)MODULES_VADDR)
  82. return 0;
  83. if (addr > (void *)MODULES_END)
  84. return 0;
  85. #endif
  86. return 1;
  87. }
  88. static int __kprobes s390_get_insn_slot(struct kprobe *p)
  89. {
  90. /*
  91. * Get an insn slot that is within the same 2GB area like the original
  92. * instruction. That way instructions with a 32bit signed displacement
  93. * field can be patched and executed within the insn slot.
  94. */
  95. p->ainsn.insn = NULL;
  96. if (is_kernel_addr(p->addr))
  97. p->ainsn.insn = get_dmainsn_slot();
  98. else if (is_module_addr(p->addr))
  99. p->ainsn.insn = get_insn_slot();
  100. return p->ainsn.insn ? 0 : -ENOMEM;
  101. }
  102. static void __kprobes s390_free_insn_slot(struct kprobe *p)
  103. {
  104. if (!p->ainsn.insn)
  105. return;
  106. if (is_kernel_addr(p->addr))
  107. free_dmainsn_slot(p->ainsn.insn, 0);
  108. else
  109. free_insn_slot(p->ainsn.insn, 0);
  110. p->ainsn.insn = NULL;
  111. }
  112. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  113. {
  114. if ((unsigned long) p->addr & 0x01)
  115. return -EINVAL;
  116. /* Make sure the probe isn't going on a difficult instruction */
  117. if (probe_is_prohibited_opcode(p->addr))
  118. return -EINVAL;
  119. if (s390_get_insn_slot(p))
  120. return -ENOMEM;
  121. p->opcode = *p->addr;
  122. copy_instruction(p);
  123. return 0;
  124. }
  125. struct ins_replace_args {
  126. kprobe_opcode_t *ptr;
  127. kprobe_opcode_t opcode;
  128. };
  129. static int __kprobes swap_instruction(void *aref)
  130. {
  131. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  132. unsigned long status = kcb->kprobe_status;
  133. struct ins_replace_args *args = aref;
  134. kcb->kprobe_status = KPROBE_SWAP_INST;
  135. probe_kernel_write(args->ptr, &args->opcode, sizeof(args->opcode));
  136. kcb->kprobe_status = status;
  137. return 0;
  138. }
  139. void __kprobes arch_arm_kprobe(struct kprobe *p)
  140. {
  141. struct ins_replace_args args;
  142. args.ptr = p->addr;
  143. args.opcode = BREAKPOINT_INSTRUCTION;
  144. stop_machine(swap_instruction, &args, NULL);
  145. }
  146. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  147. {
  148. struct ins_replace_args args;
  149. args.ptr = p->addr;
  150. args.opcode = p->opcode;
  151. stop_machine(swap_instruction, &args, NULL);
  152. }
  153. void __kprobes arch_remove_kprobe(struct kprobe *p)
  154. {
  155. s390_free_insn_slot(p);
  156. }
  157. static void __kprobes enable_singlestep(struct kprobe_ctlblk *kcb,
  158. struct pt_regs *regs,
  159. unsigned long ip)
  160. {
  161. struct per_regs per_kprobe;
  162. /* Set up the PER control registers %cr9-%cr11 */
  163. per_kprobe.control = PER_EVENT_IFETCH;
  164. per_kprobe.start = ip;
  165. per_kprobe.end = ip;
  166. /* Save control regs and psw mask */
  167. __ctl_store(kcb->kprobe_saved_ctl, 9, 11);
  168. kcb->kprobe_saved_imask = regs->psw.mask &
  169. (PSW_MASK_PER | PSW_MASK_IO | PSW_MASK_EXT);
  170. /* Set PER control regs, turns on single step for the given address */
  171. __ctl_load(per_kprobe, 9, 11);
  172. regs->psw.mask |= PSW_MASK_PER;
  173. regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT);
  174. regs->psw.addr = ip | PSW_ADDR_AMODE;
  175. }
  176. static void __kprobes disable_singlestep(struct kprobe_ctlblk *kcb,
  177. struct pt_regs *regs,
  178. unsigned long ip)
  179. {
  180. /* Restore control regs and psw mask, set new psw address */
  181. __ctl_load(kcb->kprobe_saved_ctl, 9, 11);
  182. regs->psw.mask &= ~PSW_MASK_PER;
  183. regs->psw.mask |= kcb->kprobe_saved_imask;
  184. regs->psw.addr = ip | PSW_ADDR_AMODE;
  185. }
  186. /*
  187. * Activate a kprobe by storing its pointer to current_kprobe. The
  188. * previous kprobe is stored in kcb->prev_kprobe. A stack of up to
  189. * two kprobes can be active, see KPROBE_REENTER.
  190. */
  191. static void __kprobes push_kprobe(struct kprobe_ctlblk *kcb, struct kprobe *p)
  192. {
  193. kcb->prev_kprobe.kp = __this_cpu_read(current_kprobe);
  194. kcb->prev_kprobe.status = kcb->kprobe_status;
  195. __this_cpu_write(current_kprobe, p);
  196. }
  197. /*
  198. * Deactivate a kprobe by backing up to the previous state. If the
  199. * current state is KPROBE_REENTER prev_kprobe.kp will be non-NULL,
  200. * for any other state prev_kprobe.kp will be NULL.
  201. */
  202. static void __kprobes pop_kprobe(struct kprobe_ctlblk *kcb)
  203. {
  204. __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
  205. kcb->kprobe_status = kcb->prev_kprobe.status;
  206. }
  207. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  208. struct pt_regs *regs)
  209. {
  210. ri->ret_addr = (kprobe_opcode_t *) regs->gprs[14];
  211. /* Replace the return addr with trampoline addr */
  212. regs->gprs[14] = (unsigned long) &kretprobe_trampoline;
  213. }
  214. static void __kprobes kprobe_reenter_check(struct kprobe_ctlblk *kcb,
  215. struct kprobe *p)
  216. {
  217. switch (kcb->kprobe_status) {
  218. case KPROBE_HIT_SSDONE:
  219. case KPROBE_HIT_ACTIVE:
  220. kprobes_inc_nmissed_count(p);
  221. break;
  222. case KPROBE_HIT_SS:
  223. case KPROBE_REENTER:
  224. default:
  225. /*
  226. * A kprobe on the code path to single step an instruction
  227. * is a BUG. The code path resides in the .kprobes.text
  228. * section and is executed with interrupts disabled.
  229. */
  230. printk(KERN_EMERG "Invalid kprobe detected at %p.\n", p->addr);
  231. dump_kprobe(p);
  232. BUG();
  233. }
  234. }
  235. static int __kprobes kprobe_handler(struct pt_regs *regs)
  236. {
  237. struct kprobe_ctlblk *kcb;
  238. struct kprobe *p;
  239. /*
  240. * We want to disable preemption for the entire duration of kprobe
  241. * processing. That includes the calls to the pre/post handlers
  242. * and single stepping the kprobe instruction.
  243. */
  244. preempt_disable();
  245. kcb = get_kprobe_ctlblk();
  246. p = get_kprobe((void *)((regs->psw.addr & PSW_ADDR_INSN) - 2));
  247. if (p) {
  248. if (kprobe_running()) {
  249. /*
  250. * We have hit a kprobe while another is still
  251. * active. This can happen in the pre and post
  252. * handler. Single step the instruction of the
  253. * new probe but do not call any handler function
  254. * of this secondary kprobe.
  255. * push_kprobe and pop_kprobe saves and restores
  256. * the currently active kprobe.
  257. */
  258. kprobe_reenter_check(kcb, p);
  259. push_kprobe(kcb, p);
  260. kcb->kprobe_status = KPROBE_REENTER;
  261. } else {
  262. /*
  263. * If we have no pre-handler or it returned 0, we
  264. * continue with single stepping. If we have a
  265. * pre-handler and it returned non-zero, it prepped
  266. * for calling the break_handler below on re-entry
  267. * for jprobe processing, so get out doing nothing
  268. * more here.
  269. */
  270. push_kprobe(kcb, p);
  271. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  272. if (p->pre_handler && p->pre_handler(p, regs))
  273. return 1;
  274. kcb->kprobe_status = KPROBE_HIT_SS;
  275. }
  276. enable_singlestep(kcb, regs, (unsigned long) p->ainsn.insn);
  277. return 1;
  278. } else if (kprobe_running()) {
  279. p = __this_cpu_read(current_kprobe);
  280. if (p->break_handler && p->break_handler(p, regs)) {
  281. /*
  282. * Continuation after the jprobe completed and
  283. * caused the jprobe_return trap. The jprobe
  284. * break_handler "returns" to the original
  285. * function that still has the kprobe breakpoint
  286. * installed. We continue with single stepping.
  287. */
  288. kcb->kprobe_status = KPROBE_HIT_SS;
  289. enable_singlestep(kcb, regs,
  290. (unsigned long) p->ainsn.insn);
  291. return 1;
  292. } /* else:
  293. * No kprobe at this address and the current kprobe
  294. * has no break handler (no jprobe!). The kernel just
  295. * exploded, let the standard trap handler pick up the
  296. * pieces.
  297. */
  298. } /* else:
  299. * No kprobe at this address and no active kprobe. The trap has
  300. * not been caused by a kprobe breakpoint. The race of breakpoint
  301. * vs. kprobe remove does not exist because on s390 as we use
  302. * stop_machine to arm/disarm the breakpoints.
  303. */
  304. preempt_enable_no_resched();
  305. return 0;
  306. }
  307. /*
  308. * Function return probe trampoline:
  309. * - init_kprobes() establishes a probepoint here
  310. * - When the probed function returns, this probe
  311. * causes the handlers to fire
  312. */
  313. static void __used kretprobe_trampoline_holder(void)
  314. {
  315. asm volatile(".global kretprobe_trampoline\n"
  316. "kretprobe_trampoline: bcr 0,0\n");
  317. }
  318. /*
  319. * Called when the probe at kretprobe trampoline is hit
  320. */
  321. static int __kprobes trampoline_probe_handler(struct kprobe *p,
  322. struct pt_regs *regs)
  323. {
  324. struct kretprobe_instance *ri;
  325. struct hlist_head *head, empty_rp;
  326. struct hlist_node *tmp;
  327. unsigned long flags, orig_ret_address;
  328. unsigned long trampoline_address;
  329. kprobe_opcode_t *correct_ret_addr;
  330. INIT_HLIST_HEAD(&empty_rp);
  331. kretprobe_hash_lock(current, &head, &flags);
  332. /*
  333. * It is possible to have multiple instances associated with a given
  334. * task either because an multiple functions in the call path
  335. * have a return probe installed on them, and/or more than one return
  336. * return probe was registered for a target function.
  337. *
  338. * We can handle this because:
  339. * - instances are always inserted at the head of the list
  340. * - when multiple return probes are registered for the same
  341. * function, the first instance's ret_addr will point to the
  342. * real return address, and all the rest will point to
  343. * kretprobe_trampoline
  344. */
  345. ri = NULL;
  346. orig_ret_address = 0;
  347. correct_ret_addr = NULL;
  348. trampoline_address = (unsigned long) &kretprobe_trampoline;
  349. hlist_for_each_entry_safe(ri, tmp, head, hlist) {
  350. if (ri->task != current)
  351. /* another task is sharing our hash bucket */
  352. continue;
  353. orig_ret_address = (unsigned long) ri->ret_addr;
  354. if (orig_ret_address != trampoline_address)
  355. /*
  356. * This is the real return address. Any other
  357. * instances associated with this task are for
  358. * other calls deeper on the call stack
  359. */
  360. break;
  361. }
  362. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  363. correct_ret_addr = ri->ret_addr;
  364. hlist_for_each_entry_safe(ri, tmp, head, hlist) {
  365. if (ri->task != current)
  366. /* another task is sharing our hash bucket */
  367. continue;
  368. orig_ret_address = (unsigned long) ri->ret_addr;
  369. if (ri->rp && ri->rp->handler) {
  370. ri->ret_addr = correct_ret_addr;
  371. ri->rp->handler(ri, regs);
  372. }
  373. recycle_rp_inst(ri, &empty_rp);
  374. if (orig_ret_address != trampoline_address)
  375. /*
  376. * This is the real return address. Any other
  377. * instances associated with this task are for
  378. * other calls deeper on the call stack
  379. */
  380. break;
  381. }
  382. regs->psw.addr = orig_ret_address | PSW_ADDR_AMODE;
  383. pop_kprobe(get_kprobe_ctlblk());
  384. kretprobe_hash_unlock(current, &flags);
  385. preempt_enable_no_resched();
  386. hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
  387. hlist_del(&ri->hlist);
  388. kfree(ri);
  389. }
  390. /*
  391. * By returning a non-zero value, we are telling
  392. * kprobe_handler() that we don't want the post_handler
  393. * to run (and have re-enabled preemption)
  394. */
  395. return 1;
  396. }
  397. /*
  398. * Called after single-stepping. p->addr is the address of the
  399. * instruction whose first byte has been replaced by the "breakpoint"
  400. * instruction. To avoid the SMP problems that can occur when we
  401. * temporarily put back the original opcode to single-step, we
  402. * single-stepped a copy of the instruction. The address of this
  403. * copy is p->ainsn.insn.
  404. */
  405. static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
  406. {
  407. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  408. unsigned long ip = regs->psw.addr & PSW_ADDR_INSN;
  409. int fixup = probe_get_fixup_type(p->ainsn.insn);
  410. if (fixup & FIXUP_PSW_NORMAL)
  411. ip += (unsigned long) p->addr - (unsigned long) p->ainsn.insn;
  412. if (fixup & FIXUP_BRANCH_NOT_TAKEN) {
  413. int ilen = insn_length(p->ainsn.insn[0] >> 8);
  414. if (ip - (unsigned long) p->ainsn.insn == ilen)
  415. ip = (unsigned long) p->addr + ilen;
  416. }
  417. if (fixup & FIXUP_RETURN_REGISTER) {
  418. int reg = (p->ainsn.insn[0] & 0xf0) >> 4;
  419. regs->gprs[reg] += (unsigned long) p->addr -
  420. (unsigned long) p->ainsn.insn;
  421. }
  422. disable_singlestep(kcb, regs, ip);
  423. }
  424. static int __kprobes post_kprobe_handler(struct pt_regs *regs)
  425. {
  426. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  427. struct kprobe *p = kprobe_running();
  428. if (!p)
  429. return 0;
  430. if (kcb->kprobe_status != KPROBE_REENTER && p->post_handler) {
  431. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  432. p->post_handler(p, regs, 0);
  433. }
  434. resume_execution(p, regs);
  435. pop_kprobe(kcb);
  436. preempt_enable_no_resched();
  437. /*
  438. * if somebody else is singlestepping across a probe point, psw mask
  439. * will have PER set, in which case, continue the remaining processing
  440. * of do_single_step, as if this is not a probe hit.
  441. */
  442. if (regs->psw.mask & PSW_MASK_PER)
  443. return 0;
  444. return 1;
  445. }
  446. static int __kprobes kprobe_trap_handler(struct pt_regs *regs, int trapnr)
  447. {
  448. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  449. struct kprobe *p = kprobe_running();
  450. const struct exception_table_entry *entry;
  451. switch(kcb->kprobe_status) {
  452. case KPROBE_SWAP_INST:
  453. /* We are here because the instruction replacement failed */
  454. return 0;
  455. case KPROBE_HIT_SS:
  456. case KPROBE_REENTER:
  457. /*
  458. * We are here because the instruction being single
  459. * stepped caused a page fault. We reset the current
  460. * kprobe and the nip points back to the probe address
  461. * and allow the page fault handler to continue as a
  462. * normal page fault.
  463. */
  464. disable_singlestep(kcb, regs, (unsigned long) p->addr);
  465. pop_kprobe(kcb);
  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 accounting
  473. * these specific fault cases.
  474. */
  475. kprobes_inc_nmissed_count(p);
  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 (p->fault_handler && p->fault_handler(p, 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 = extable_fixup(entry) | 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(regs))
  528. ret = NOTIFY_STOP;
  529. break;
  530. case DIE_SSTEP:
  531. if (post_kprobe_handler(regs))
  532. ret = NOTIFY_STOP;
  533. break;
  534. case DIE_TRAP:
  535. if (!preemptible() && kprobe_running() &&
  536. kprobe_trap_handler(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. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  550. unsigned long stack;
  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. /* r15 is the stack pointer */
  556. stack = (unsigned long) regs->gprs[15];
  557. memcpy(kcb->jprobes_stack, (void *) stack, MIN_STACK_SIZE(stack));
  558. return 1;
  559. }
  560. void __kprobes jprobe_return(void)
  561. {
  562. asm volatile(".word 0x0002");
  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;
  568. stack = (unsigned long) kcb->jprobe_saved_regs.gprs[15];
  569. /* Put the regs back */
  570. memcpy(regs, &kcb->jprobe_saved_regs, sizeof(struct pt_regs));
  571. /* put the stack back */
  572. memcpy((void *) stack, kcb->jprobes_stack, MIN_STACK_SIZE(stack));
  573. preempt_enable_no_resched();
  574. return 1;
  575. }
  576. static struct kprobe trampoline = {
  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);
  583. }
  584. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  585. {
  586. return p->addr == (kprobe_opcode_t *) &kretprobe_trampoline;
  587. }