uprobes.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. /*
  52. * We currently don't support a uprobe on an already
  53. * existing breakpoint instruction underneath
  54. */
  55. if (is_trap(auprobe->ainsn))
  56. return -ENOTSUPP;
  57. return 0;
  58. }
  59. /*
  60. * arch_uprobe_pre_xol - prepare to execute out of line.
  61. * @auprobe: the probepoint information.
  62. * @regs: reflects the saved user state of current task.
  63. */
  64. int arch_uprobe_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  65. {
  66. struct arch_uprobe_task *autask = &current->utask->autask;
  67. autask->saved_trap_nr = current->thread.trap_nr;
  68. current->thread.trap_nr = UPROBE_TRAP_NR;
  69. regs->nip = current->utask->xol_vaddr;
  70. user_enable_single_step(current);
  71. return 0;
  72. }
  73. /**
  74. * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
  75. * @regs: Reflects the saved state of the task after it has hit a breakpoint
  76. * instruction.
  77. * Return the address of the breakpoint instruction.
  78. */
  79. unsigned long uprobe_get_swbp_addr(struct pt_regs *regs)
  80. {
  81. return instruction_pointer(regs);
  82. }
  83. /*
  84. * If xol insn itself traps and generates a signal (SIGILL/SIGSEGV/etc),
  85. * then detect the case where a singlestepped instruction jumps back to its
  86. * own address. It is assumed that anything like do_page_fault/do_trap/etc
  87. * sets thread.trap_nr != UINT_MAX.
  88. *
  89. * arch_uprobe_pre_xol/arch_uprobe_post_xol save/restore thread.trap_nr,
  90. * arch_uprobe_xol_was_trapped() simply checks that ->trap_nr is not equal to
  91. * UPROBE_TRAP_NR == UINT_MAX set by arch_uprobe_pre_xol().
  92. */
  93. bool arch_uprobe_xol_was_trapped(struct task_struct *t)
  94. {
  95. if (t->thread.trap_nr != UPROBE_TRAP_NR)
  96. return true;
  97. return false;
  98. }
  99. /*
  100. * Called after single-stepping. To avoid the SMP problems that can
  101. * occur when we temporarily put back the original opcode to
  102. * single-step, we single-stepped a copy of the instruction.
  103. *
  104. * This function prepares to resume execution after the single-step.
  105. */
  106. int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  107. {
  108. struct uprobe_task *utask = current->utask;
  109. WARN_ON_ONCE(current->thread.trap_nr != UPROBE_TRAP_NR);
  110. current->thread.trap_nr = utask->autask.saved_trap_nr;
  111. /*
  112. * On powerpc, except for loads and stores, most instructions
  113. * including ones that alter code flow (branches, calls, returns)
  114. * are emulated in the kernel. We get here only if the emulation
  115. * support doesn't exist and have to fix-up the next instruction
  116. * to be executed.
  117. */
  118. regs->nip = utask->vaddr + MAX_UINSN_BYTES;
  119. user_disable_single_step(current);
  120. return 0;
  121. }
  122. /* callback routine for handling exceptions. */
  123. int arch_uprobe_exception_notify(struct notifier_block *self,
  124. unsigned long val, void *data)
  125. {
  126. struct die_args *args = data;
  127. struct pt_regs *regs = args->regs;
  128. /* regs == NULL is a kernel bug */
  129. if (WARN_ON(!regs))
  130. return NOTIFY_DONE;
  131. /* We are only interested in userspace traps */
  132. if (!user_mode(regs))
  133. return NOTIFY_DONE;
  134. switch (val) {
  135. case DIE_BPT:
  136. if (uprobe_pre_sstep_notifier(regs))
  137. return NOTIFY_STOP;
  138. break;
  139. case DIE_SSTEP:
  140. if (uprobe_post_sstep_notifier(regs))
  141. return NOTIFY_STOP;
  142. default:
  143. break;
  144. }
  145. return NOTIFY_DONE;
  146. }
  147. /*
  148. * This function gets called when XOL instruction either gets trapped or
  149. * the thread has a fatal signal, so reset the instruction pointer to its
  150. * probed address.
  151. */
  152. void arch_uprobe_abort_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  153. {
  154. struct uprobe_task *utask = current->utask;
  155. current->thread.trap_nr = utask->autask.saved_trap_nr;
  156. instruction_pointer_set(regs, utask->vaddr);
  157. user_disable_single_step(current);
  158. }
  159. /*
  160. * See if the instruction can be emulated.
  161. * Returns true if instruction was emulated, false otherwise.
  162. */
  163. bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
  164. {
  165. int ret;
  166. /*
  167. * emulate_step() returns 1 if the insn was successfully emulated.
  168. * For all other cases, we need to single-step in hardware.
  169. */
  170. ret = emulate_step(regs, auprobe->ainsn);
  171. if (ret > 0)
  172. return true;
  173. return false;
  174. }