uprobes.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * User-space Probes (UProbes) for sparc
  3. *
  4. * Copyright (C) 2013 Oracle Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * Authors:
  20. * Jose E. Marchesi <jose.marchesi@oracle.com>
  21. * Eric Saint Etienne <eric.saint.etienne@oracle.com>
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/highmem.h>
  25. #include <linux/uprobes.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/sched.h> /* For struct task_struct */
  28. #include <linux/kdebug.h>
  29. #include <asm/cacheflush.h>
  30. #include <linux/uaccess.h>
  31. /* Compute the address of the breakpoint instruction and return it.
  32. *
  33. * Note that uprobe_get_swbp_addr is defined as a weak symbol in
  34. * kernel/events/uprobe.c.
  35. */
  36. unsigned long uprobe_get_swbp_addr(struct pt_regs *regs)
  37. {
  38. return instruction_pointer(regs);
  39. }
  40. static void copy_to_page(struct page *page, unsigned long vaddr,
  41. const void *src, int len)
  42. {
  43. void *kaddr = kmap_atomic(page);
  44. memcpy(kaddr + (vaddr & ~PAGE_MASK), src, len);
  45. kunmap_atomic(kaddr);
  46. }
  47. /* Fill in the xol area with the probed instruction followed by the
  48. * single-step trap. Some fixups in the copied instruction are
  49. * performed at this point.
  50. *
  51. * Note that uprobe_xol_copy is defined as a weak symbol in
  52. * kernel/events/uprobe.c.
  53. */
  54. void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
  55. void *src, unsigned long len)
  56. {
  57. const u32 stp_insn = UPROBE_STP_INSN;
  58. u32 insn = *(u32 *) src;
  59. /* Branches annulling their delay slot must be fixed to not do
  60. * so. Clearing the annul bit on these instructions we can be
  61. * sure the single-step breakpoint in the XOL slot will be
  62. * executed.
  63. */
  64. u32 op = (insn >> 30) & 0x3;
  65. u32 op2 = (insn >> 22) & 0x7;
  66. if (op == 0 &&
  67. (op2 == 1 || op2 == 2 || op2 == 3 || op2 == 5 || op2 == 6) &&
  68. (insn & ANNUL_BIT) == ANNUL_BIT)
  69. insn &= ~ANNUL_BIT;
  70. copy_to_page(page, vaddr, &insn, len);
  71. copy_to_page(page, vaddr+len, &stp_insn, 4);
  72. }
  73. /* Instruction analysis/validity.
  74. *
  75. * This function returns 0 on success or a -ve number on error.
  76. */
  77. int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe,
  78. struct mm_struct *mm, unsigned long addr)
  79. {
  80. /* Any unsupported instruction? Then return -EINVAL */
  81. return 0;
  82. }
  83. /* If INSN is a relative control transfer instruction, return the
  84. * corrected branch destination value.
  85. *
  86. * Note that regs->tpc and regs->tnpc still hold the values of the
  87. * program counters at the time of the single-step trap due to the
  88. * execution of the UPROBE_STP_INSN at utask->xol_vaddr + 4.
  89. *
  90. */
  91. static unsigned long relbranch_fixup(u32 insn, struct uprobe_task *utask,
  92. struct pt_regs *regs)
  93. {
  94. /* Branch not taken, no mods necessary. */
  95. if (regs->tnpc == regs->tpc + 0x4UL)
  96. return utask->autask.saved_tnpc + 0x4UL;
  97. /* The three cases are call, branch w/prediction,
  98. * and traditional branch.
  99. */
  100. if ((insn & 0xc0000000) == 0x40000000 ||
  101. (insn & 0xc1c00000) == 0x00400000 ||
  102. (insn & 0xc1c00000) == 0x00800000) {
  103. unsigned long real_pc = (unsigned long) utask->vaddr;
  104. unsigned long ixol_addr = utask->xol_vaddr;
  105. /* The instruction did all the work for us
  106. * already, just apply the offset to the correct
  107. * instruction location.
  108. */
  109. return (real_pc + (regs->tnpc - ixol_addr));
  110. }
  111. /* It is jmpl or some other absolute PC modification instruction,
  112. * leave NPC as-is.
  113. */
  114. return regs->tnpc;
  115. }
  116. /* If INSN is an instruction which writes its PC location
  117. * into a destination register, fix that up.
  118. */
  119. static int retpc_fixup(struct pt_regs *regs, u32 insn,
  120. unsigned long real_pc)
  121. {
  122. unsigned long *slot = NULL;
  123. int rc = 0;
  124. /* Simplest case is 'call', which always uses %o7 */
  125. if ((insn & 0xc0000000) == 0x40000000)
  126. slot = &regs->u_regs[UREG_I7];
  127. /* 'jmpl' encodes the register inside of the opcode */
  128. if ((insn & 0xc1f80000) == 0x81c00000) {
  129. unsigned long rd = ((insn >> 25) & 0x1f);
  130. if (rd <= 15) {
  131. slot = &regs->u_regs[rd];
  132. } else {
  133. unsigned long fp = regs->u_regs[UREG_FP];
  134. /* Hard case, it goes onto the stack. */
  135. flushw_all();
  136. rd -= 16;
  137. if (test_thread_64bit_stack(fp)) {
  138. unsigned long __user *uslot =
  139. (unsigned long __user *) (fp + STACK_BIAS) + rd;
  140. rc = __put_user(real_pc, uslot);
  141. } else {
  142. unsigned int __user *uslot = (unsigned int
  143. __user *) fp + rd;
  144. rc = __put_user((u32) real_pc, uslot);
  145. }
  146. }
  147. }
  148. if (slot != NULL)
  149. *slot = real_pc;
  150. return rc;
  151. }
  152. /* Single-stepping can be avoided for certain instructions: NOPs and
  153. * instructions that can be emulated. This function determines
  154. * whether the instruction where the uprobe is installed falls in one
  155. * of these cases and emulates it.
  156. *
  157. * This function returns true if the single-stepping can be skipped,
  158. * false otherwise.
  159. */
  160. bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
  161. {
  162. /* We currently only emulate NOP instructions.
  163. */
  164. if (auprobe->ixol == (1 << 24)) {
  165. regs->tnpc += 4;
  166. regs->tpc += 4;
  167. return true;
  168. }
  169. return false;
  170. }
  171. /* Prepare to execute out of line. At this point
  172. * current->utask->xol_vaddr points to an allocated XOL slot properly
  173. * initialized with the original instruction and the single-stepping
  174. * trap instruction.
  175. *
  176. * This function returns 0 on success, any other number on error.
  177. */
  178. int arch_uprobe_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  179. {
  180. struct uprobe_task *utask = current->utask;
  181. struct arch_uprobe_task *autask = &current->utask->autask;
  182. /* Save the current program counters so they can be restored
  183. * later.
  184. */
  185. autask->saved_tpc = regs->tpc;
  186. autask->saved_tnpc = regs->tnpc;
  187. /* Adjust PC and NPC so the first instruction in the XOL slot
  188. * will be executed by the user task.
  189. */
  190. instruction_pointer_set(regs, utask->xol_vaddr);
  191. return 0;
  192. }
  193. /* Prepare to resume execution after the single-step. Called after
  194. * single-stepping. To avoid the SMP problems that can occur when we
  195. * temporarily put back the original opcode to single-step, we
  196. * single-stepped a copy of the instruction.
  197. *
  198. * This function returns 0 on success, any other number on error.
  199. */
  200. int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  201. {
  202. struct uprobe_task *utask = current->utask;
  203. struct arch_uprobe_task *autask = &utask->autask;
  204. u32 insn = auprobe->ixol;
  205. int rc = 0;
  206. if (utask->state == UTASK_SSTEP_ACK) {
  207. regs->tnpc = relbranch_fixup(insn, utask, regs);
  208. regs->tpc = autask->saved_tnpc;
  209. rc = retpc_fixup(regs, insn, (unsigned long) utask->vaddr);
  210. } else {
  211. regs->tnpc = utask->vaddr+4;
  212. regs->tpc = autask->saved_tnpc+4;
  213. }
  214. return rc;
  215. }
  216. /* Handler for uprobe traps. This is called from the traps table and
  217. * triggers the proper die notification.
  218. */
  219. asmlinkage void uprobe_trap(struct pt_regs *regs,
  220. unsigned long trap_level)
  221. {
  222. BUG_ON(trap_level != 0x173 && trap_level != 0x174);
  223. /* We are only interested in user-mode code. Uprobe traps
  224. * shall not be present in kernel code.
  225. */
  226. if (!user_mode(regs)) {
  227. local_irq_enable();
  228. bad_trap(regs, trap_level);
  229. return;
  230. }
  231. /* trap_level == 0x173 --> ta 0x73
  232. * trap_level == 0x174 --> ta 0x74
  233. */
  234. if (notify_die((trap_level == 0x173) ? DIE_BPT : DIE_SSTEP,
  235. (trap_level == 0x173) ? "bpt" : "sstep",
  236. regs, 0, trap_level, SIGTRAP) != NOTIFY_STOP)
  237. bad_trap(regs, trap_level);
  238. }
  239. /* Callback routine for handling die notifications.
  240. */
  241. int arch_uprobe_exception_notify(struct notifier_block *self,
  242. unsigned long val, void *data)
  243. {
  244. int ret = NOTIFY_DONE;
  245. struct die_args *args = (struct die_args *)data;
  246. /* We are only interested in userspace traps */
  247. if (args->regs && !user_mode(args->regs))
  248. return NOTIFY_DONE;
  249. switch (val) {
  250. case DIE_BPT:
  251. if (uprobe_pre_sstep_notifier(args->regs))
  252. ret = NOTIFY_STOP;
  253. break;
  254. case DIE_SSTEP:
  255. if (uprobe_post_sstep_notifier(args->regs))
  256. ret = NOTIFY_STOP;
  257. default:
  258. break;
  259. }
  260. return ret;
  261. }
  262. /* This function gets called when a XOL instruction either gets
  263. * trapped or the thread has a fatal signal, so reset the instruction
  264. * pointer to its probed address.
  265. */
  266. void arch_uprobe_abort_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  267. {
  268. struct uprobe_task *utask = current->utask;
  269. instruction_pointer_set(regs, utask->vaddr);
  270. }
  271. /* If xol insn itself traps and generates a signal(Say,
  272. * SIGILL/SIGSEGV/etc), then detect the case where a singlestepped
  273. * instruction jumps back to its own address.
  274. */
  275. bool arch_uprobe_xol_was_trapped(struct task_struct *t)
  276. {
  277. return false;
  278. }
  279. unsigned long
  280. arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr,
  281. struct pt_regs *regs)
  282. {
  283. unsigned long orig_ret_vaddr = regs->u_regs[UREG_I7];
  284. regs->u_regs[UREG_I7] = trampoline_vaddr-8;
  285. return orig_ret_vaddr + 8;
  286. }