kprobes.c 20 KB

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