uprobes.c 5.6 KB

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