kprobes.c 21 KB

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