kprobes.c 21 KB

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