kprobes.c 20 KB

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