kprobes.c 20 KB

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