uprobes.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #include <linux/highmem.h>
  2. #include <linux/kdebug.h>
  3. #include <linux/types.h>
  4. #include <linux/notifier.h>
  5. #include <linux/sched.h>
  6. #include <linux/uprobes.h>
  7. #include <asm/branch.h>
  8. #include <asm/cpu-features.h>
  9. #include <asm/ptrace.h>
  10. #include "probes-common.h"
  11. static inline int insn_has_delay_slot(const union mips_instruction insn)
  12. {
  13. return __insn_has_delay_slot(insn);
  14. }
  15. /**
  16. * arch_uprobe_analyze_insn - instruction analysis including validity and fixups.
  17. * @mm: the probed address space.
  18. * @arch_uprobe: the probepoint information.
  19. * @addr: virtual address at which to install the probepoint
  20. * Return 0 on success or a -ve number on error.
  21. */
  22. int arch_uprobe_analyze_insn(struct arch_uprobe *aup,
  23. struct mm_struct *mm, unsigned long addr)
  24. {
  25. union mips_instruction inst;
  26. /*
  27. * For the time being this also blocks attempts to use uprobes with
  28. * MIPS16 and microMIPS.
  29. */
  30. if (addr & 0x03)
  31. return -EINVAL;
  32. inst.word = aup->insn[0];
  33. if (__insn_is_compact_branch(inst)) {
  34. pr_notice("Uprobes for compact branches are not supported\n");
  35. return -EINVAL;
  36. }
  37. aup->ixol[0] = aup->insn[insn_has_delay_slot(inst)];
  38. aup->ixol[1] = UPROBE_BRK_UPROBE_XOL; /* NOP */
  39. return 0;
  40. }
  41. /**
  42. * is_trap_insn - check if the instruction is a trap variant
  43. * @insn: instruction to be checked.
  44. * Returns true if @insn is a trap variant.
  45. *
  46. * This definition overrides the weak definition in kernel/events/uprobes.c.
  47. * and is needed for the case where an architecture has multiple trap
  48. * instructions (like PowerPC or MIPS). We treat BREAK just like the more
  49. * modern conditional trap instructions.
  50. */
  51. bool is_trap_insn(uprobe_opcode_t *insn)
  52. {
  53. union mips_instruction inst;
  54. inst.word = *insn;
  55. switch (inst.i_format.opcode) {
  56. case spec_op:
  57. switch (inst.r_format.func) {
  58. case break_op:
  59. case teq_op:
  60. case tge_op:
  61. case tgeu_op:
  62. case tlt_op:
  63. case tltu_op:
  64. case tne_op:
  65. return 1;
  66. }
  67. break;
  68. case bcond_op: /* Yes, really ... */
  69. switch (inst.u_format.rt) {
  70. case teqi_op:
  71. case tgei_op:
  72. case tgeiu_op:
  73. case tlti_op:
  74. case tltiu_op:
  75. case tnei_op:
  76. return 1;
  77. }
  78. break;
  79. }
  80. return 0;
  81. }
  82. #define UPROBE_TRAP_NR ULONG_MAX
  83. /*
  84. * arch_uprobe_pre_xol - prepare to execute out of line.
  85. * @auprobe: the probepoint information.
  86. * @regs: reflects the saved user state of current task.
  87. */
  88. int arch_uprobe_pre_xol(struct arch_uprobe *aup, struct pt_regs *regs)
  89. {
  90. struct uprobe_task *utask = current->utask;
  91. /*
  92. * Now find the EPC where to resume after the breakpoint has been
  93. * dealt with. This may require emulation of a branch.
  94. */
  95. aup->resume_epc = regs->cp0_epc + 4;
  96. if (insn_has_delay_slot((union mips_instruction) aup->insn[0])) {
  97. unsigned long epc;
  98. epc = regs->cp0_epc;
  99. __compute_return_epc_for_insn(regs,
  100. (union mips_instruction) aup->insn[0]);
  101. aup->resume_epc = regs->cp0_epc;
  102. }
  103. utask->autask.saved_trap_nr = current->thread.trap_nr;
  104. current->thread.trap_nr = UPROBE_TRAP_NR;
  105. regs->cp0_epc = current->utask->xol_vaddr;
  106. return 0;
  107. }
  108. int arch_uprobe_post_xol(struct arch_uprobe *aup, struct pt_regs *regs)
  109. {
  110. struct uprobe_task *utask = current->utask;
  111. current->thread.trap_nr = utask->autask.saved_trap_nr;
  112. regs->cp0_epc = aup->resume_epc;
  113. return 0;
  114. }
  115. /*
  116. * If xol insn itself traps and generates a signal(Say,
  117. * SIGILL/SIGSEGV/etc), then detect the case where a singlestepped
  118. * instruction jumps back to its own address. It is assumed that anything
  119. * like do_page_fault/do_trap/etc sets thread.trap_nr != -1.
  120. *
  121. * arch_uprobe_pre_xol/arch_uprobe_post_xol save/restore thread.trap_nr,
  122. * arch_uprobe_xol_was_trapped() simply checks that ->trap_nr is not equal to
  123. * UPROBE_TRAP_NR == -1 set by arch_uprobe_pre_xol().
  124. */
  125. bool arch_uprobe_xol_was_trapped(struct task_struct *tsk)
  126. {
  127. if (tsk->thread.trap_nr != UPROBE_TRAP_NR)
  128. return true;
  129. return false;
  130. }
  131. int arch_uprobe_exception_notify(struct notifier_block *self,
  132. unsigned long val, void *data)
  133. {
  134. struct die_args *args = data;
  135. struct pt_regs *regs = args->regs;
  136. /* regs == NULL is a kernel bug */
  137. if (WARN_ON(!regs))
  138. return NOTIFY_DONE;
  139. /* We are only interested in userspace traps */
  140. if (!user_mode(regs))
  141. return NOTIFY_DONE;
  142. switch (val) {
  143. case DIE_UPROBE:
  144. if (uprobe_pre_sstep_notifier(regs))
  145. return NOTIFY_STOP;
  146. break;
  147. case DIE_UPROBE_XOL:
  148. if (uprobe_post_sstep_notifier(regs))
  149. return NOTIFY_STOP;
  150. default:
  151. break;
  152. }
  153. return 0;
  154. }
  155. /*
  156. * This function gets called when XOL instruction either gets trapped or
  157. * the thread has a fatal signal. Reset the instruction pointer to its
  158. * probed address for the potential restart or for post mortem analysis.
  159. */
  160. void arch_uprobe_abort_xol(struct arch_uprobe *aup,
  161. struct pt_regs *regs)
  162. {
  163. struct uprobe_task *utask = current->utask;
  164. instruction_pointer_set(regs, utask->vaddr);
  165. }
  166. unsigned long arch_uretprobe_hijack_return_addr(
  167. unsigned long trampoline_vaddr, struct pt_regs *regs)
  168. {
  169. unsigned long ra;
  170. ra = regs->regs[31];
  171. /* Replace the return address with the trampoline address */
  172. regs->regs[31] = trampoline_vaddr;
  173. return ra;
  174. }
  175. /**
  176. * set_swbp - store breakpoint at a given address.
  177. * @auprobe: arch specific probepoint information.
  178. * @mm: the probed process address space.
  179. * @vaddr: the virtual address to insert the opcode.
  180. *
  181. * For mm @mm, store the breakpoint instruction at @vaddr.
  182. * Return 0 (success) or a negative errno.
  183. *
  184. * This version overrides the weak version in kernel/events/uprobes.c.
  185. * It is required to handle MIPS16 and microMIPS.
  186. */
  187. int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm,
  188. unsigned long vaddr)
  189. {
  190. return uprobe_write_opcode(mm, vaddr, UPROBE_SWBP_INSN);
  191. }
  192. void __weak arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
  193. void *src, unsigned long len)
  194. {
  195. unsigned long kaddr, kstart;
  196. /* Initialize the slot */
  197. kaddr = (unsigned long)kmap_atomic(page);
  198. kstart = kaddr + (vaddr & ~PAGE_MASK);
  199. memcpy((void *)kstart, src, len);
  200. flush_icache_range(kstart, kstart + len);
  201. kunmap_atomic((void *)kaddr);
  202. }
  203. /**
  204. * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
  205. * @regs: Reflects the saved state of the task after it has hit a breakpoint
  206. * instruction.
  207. * Return the address of the breakpoint instruction.
  208. *
  209. * This overrides the weak version in kernel/events/uprobes.c.
  210. */
  211. unsigned long uprobe_get_swbp_addr(struct pt_regs *regs)
  212. {
  213. return instruction_pointer(regs);
  214. }
  215. /*
  216. * See if the instruction can be emulated.
  217. * Returns true if instruction was emulated, false otherwise.
  218. *
  219. * For now we always emulate so this function just returns 0.
  220. */
  221. bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
  222. {
  223. return 0;
  224. }