uprobes.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * User-space Probes (UProbes) for powerpc
  4. *
  5. * Copyright IBM Corporation, 2007-2012
  6. *
  7. * Adapted from the x86 port by Ananth N Mavinakayanahalli <ananth@in.ibm.com>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/sched.h>
  11. #include <linux/ptrace.h>
  12. #include <linux/uprobes.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/kdebug.h>
  15. #include <asm/sstep.h>
  16. #include <asm/inst.h>
  17. #define UPROBE_TRAP_NR UINT_MAX
  18. /**
  19. * is_trap_insn - check if the instruction is a trap variant
  20. * @insn: instruction to be checked.
  21. * Returns true if @insn is a trap variant.
  22. */
  23. bool is_trap_insn(uprobe_opcode_t *insn)
  24. {
  25. return (is_trap(*insn));
  26. }
  27. /**
  28. * arch_uprobe_analyze_insn
  29. * @mm: the probed address space.
  30. * @arch_uprobe: the probepoint information.
  31. * @addr: vaddr to probe.
  32. * Return 0 on success or a -ve number on error.
  33. */
  34. int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe,
  35. struct mm_struct *mm, unsigned long addr)
  36. {
  37. if (addr & 0x03)
  38. return -EINVAL;
  39. return 0;
  40. }
  41. /*
  42. * arch_uprobe_pre_xol - prepare to execute out of line.
  43. * @auprobe: the probepoint information.
  44. * @regs: reflects the saved user state of current task.
  45. */
  46. int arch_uprobe_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  47. {
  48. struct arch_uprobe_task *autask = &current->utask->autask;
  49. autask->saved_trap_nr = current->thread.trap_nr;
  50. current->thread.trap_nr = UPROBE_TRAP_NR;
  51. regs->nip = current->utask->xol_vaddr;
  52. user_enable_single_step(current);
  53. return 0;
  54. }
  55. /**
  56. * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
  57. * @regs: Reflects the saved state of the task after it has hit a breakpoint
  58. * instruction.
  59. * Return the address of the breakpoint instruction.
  60. */
  61. unsigned long uprobe_get_swbp_addr(struct pt_regs *regs)
  62. {
  63. return instruction_pointer(regs);
  64. }
  65. /*
  66. * If xol insn itself traps and generates a signal (SIGILL/SIGSEGV/etc),
  67. * then detect the case where a singlestepped instruction jumps back to its
  68. * own address. It is assumed that anything like do_page_fault/do_trap/etc
  69. * sets thread.trap_nr != UINT_MAX.
  70. *
  71. * arch_uprobe_pre_xol/arch_uprobe_post_xol save/restore thread.trap_nr,
  72. * arch_uprobe_xol_was_trapped() simply checks that ->trap_nr is not equal to
  73. * UPROBE_TRAP_NR == UINT_MAX set by arch_uprobe_pre_xol().
  74. */
  75. bool arch_uprobe_xol_was_trapped(struct task_struct *t)
  76. {
  77. if (t->thread.trap_nr != UPROBE_TRAP_NR)
  78. return true;
  79. return false;
  80. }
  81. /*
  82. * Called after single-stepping. To avoid the SMP problems that can
  83. * occur when we temporarily put back the original opcode to
  84. * single-step, we single-stepped a copy of the instruction.
  85. *
  86. * This function prepares to resume execution after the single-step.
  87. */
  88. int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  89. {
  90. struct uprobe_task *utask = current->utask;
  91. WARN_ON_ONCE(current->thread.trap_nr != UPROBE_TRAP_NR);
  92. current->thread.trap_nr = utask->autask.saved_trap_nr;
  93. /*
  94. * On powerpc, except for loads and stores, most instructions
  95. * including ones that alter code flow (branches, calls, returns)
  96. * are emulated in the kernel. We get here only if the emulation
  97. * support doesn't exist and have to fix-up the next instruction
  98. * to be executed.
  99. */
  100. regs->nip = (unsigned long)ppc_inst_next((void *)utask->vaddr, &auprobe->insn);
  101. user_disable_single_step(current);
  102. return 0;
  103. }
  104. /* callback routine for handling exceptions. */
  105. int arch_uprobe_exception_notify(struct notifier_block *self,
  106. unsigned long val, void *data)
  107. {
  108. struct die_args *args = data;
  109. struct pt_regs *regs = args->regs;
  110. /* regs == NULL is a kernel bug */
  111. if (WARN_ON(!regs))
  112. return NOTIFY_DONE;
  113. /* We are only interested in userspace traps */
  114. if (!user_mode(regs))
  115. return NOTIFY_DONE;
  116. switch (val) {
  117. case DIE_BPT:
  118. if (uprobe_pre_sstep_notifier(regs))
  119. return NOTIFY_STOP;
  120. break;
  121. case DIE_SSTEP:
  122. if (uprobe_post_sstep_notifier(regs))
  123. return NOTIFY_STOP;
  124. default:
  125. break;
  126. }
  127. return NOTIFY_DONE;
  128. }
  129. /*
  130. * This function gets called when XOL instruction either gets trapped or
  131. * the thread has a fatal signal, so reset the instruction pointer to its
  132. * probed address.
  133. */
  134. void arch_uprobe_abort_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  135. {
  136. struct uprobe_task *utask = current->utask;
  137. current->thread.trap_nr = utask->autask.saved_trap_nr;
  138. instruction_pointer_set(regs, utask->vaddr);
  139. user_disable_single_step(current);
  140. }
  141. /*
  142. * See if the instruction can be emulated.
  143. * Returns true if instruction was emulated, false otherwise.
  144. */
  145. bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
  146. {
  147. int ret;
  148. /*
  149. * emulate_step() returns 1 if the insn was successfully emulated.
  150. * For all other cases, we need to single-step in hardware.
  151. */
  152. ret = emulate_step(regs, ppc_inst_read(&auprobe->insn));
  153. if (ret > 0)
  154. return true;
  155. return false;
  156. }
  157. unsigned long
  158. arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs)
  159. {
  160. unsigned long orig_ret_vaddr;
  161. orig_ret_vaddr = regs->link;
  162. /* Replace the return addr with trampoline addr */
  163. regs->link = trampoline_vaddr;
  164. return orig_ret_vaddr;
  165. }
  166. bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx,
  167. struct pt_regs *regs)
  168. {
  169. if (ctx == RP_CHECK_CHAIN_CALL)
  170. return regs->gpr[1] <= ret->stack;
  171. else
  172. return regs->gpr[1] < ret->stack;
  173. }