ptrace.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. *
  14. * Copied from i386: Ross Biro 1/23/92
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/ptrace.h>
  18. #include <linux/kprobes.h>
  19. #include <linux/compat.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/regset.h>
  22. #include <linux/elf.h>
  23. #include <linux/tracehook.h>
  24. #include <linux/context_tracking.h>
  25. #include <asm/traps.h>
  26. #include <arch/chip.h>
  27. #define CREATE_TRACE_POINTS
  28. #include <trace/events/syscalls.h>
  29. void user_enable_single_step(struct task_struct *child)
  30. {
  31. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  32. }
  33. void user_disable_single_step(struct task_struct *child)
  34. {
  35. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  36. }
  37. /*
  38. * Called by kernel/ptrace.c when detaching..
  39. */
  40. void ptrace_disable(struct task_struct *child)
  41. {
  42. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  43. /*
  44. * These two are currently unused, but will be set by arch_ptrace()
  45. * and used in the syscall assembly when we do support them.
  46. */
  47. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  48. }
  49. /*
  50. * Get registers from task and ready the result for userspace.
  51. * Note that we localize the API issues to getregs() and putregs() at
  52. * some cost in performance, e.g. we need a full pt_regs copy for
  53. * PEEKUSR, and two copies for POKEUSR. But in general we expect
  54. * GETREGS/PUTREGS to be the API of choice anyway.
  55. */
  56. static char *getregs(struct task_struct *child, struct pt_regs *uregs)
  57. {
  58. *uregs = *task_pt_regs(child);
  59. /* Set up flags ABI bits. */
  60. uregs->flags = 0;
  61. #ifdef CONFIG_COMPAT
  62. if (task_thread_info(child)->status & TS_COMPAT)
  63. uregs->flags |= PT_FLAGS_COMPAT;
  64. #endif
  65. return (char *)uregs;
  66. }
  67. /* Put registers back to task. */
  68. static void putregs(struct task_struct *child, struct pt_regs *uregs)
  69. {
  70. struct pt_regs *regs = task_pt_regs(child);
  71. /* Don't allow overwriting the kernel-internal flags word. */
  72. uregs->flags = regs->flags;
  73. /* Only allow setting the ICS bit in the ex1 word. */
  74. uregs->ex1 = PL_ICS_EX1(USER_PL, EX1_ICS(uregs->ex1));
  75. *regs = *uregs;
  76. }
  77. enum tile_regset {
  78. REGSET_GPR,
  79. };
  80. static int tile_gpr_get(struct task_struct *target,
  81. const struct user_regset *regset,
  82. unsigned int pos, unsigned int count,
  83. void *kbuf, void __user *ubuf)
  84. {
  85. struct pt_regs regs;
  86. getregs(target, &regs);
  87. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &regs, 0,
  88. sizeof(regs));
  89. }
  90. static int tile_gpr_set(struct task_struct *target,
  91. const struct user_regset *regset,
  92. unsigned int pos, unsigned int count,
  93. const void *kbuf, const void __user *ubuf)
  94. {
  95. int ret;
  96. struct pt_regs regs = *task_pt_regs(target);
  97. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &regs, 0,
  98. sizeof(regs));
  99. if (ret)
  100. return ret;
  101. putregs(target, &regs);
  102. return 0;
  103. }
  104. static const struct user_regset tile_user_regset[] = {
  105. [REGSET_GPR] = {
  106. .core_note_type = NT_PRSTATUS,
  107. .n = ELF_NGREG,
  108. .size = sizeof(elf_greg_t),
  109. .align = sizeof(elf_greg_t),
  110. .get = tile_gpr_get,
  111. .set = tile_gpr_set,
  112. },
  113. };
  114. static const struct user_regset_view tile_user_regset_view = {
  115. .name = CHIP_ARCH_NAME,
  116. .e_machine = ELF_ARCH,
  117. .ei_osabi = ELF_OSABI,
  118. .regsets = tile_user_regset,
  119. .n = ARRAY_SIZE(tile_user_regset),
  120. };
  121. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  122. {
  123. return &tile_user_regset_view;
  124. }
  125. long arch_ptrace(struct task_struct *child, long request,
  126. unsigned long addr, unsigned long data)
  127. {
  128. unsigned long __user *datap = (long __user __force *)data;
  129. unsigned long tmp;
  130. long ret = -EIO;
  131. char *childreg;
  132. struct pt_regs copyregs;
  133. switch (request) {
  134. case PTRACE_PEEKUSR: /* Read register from pt_regs. */
  135. if (addr >= PTREGS_SIZE)
  136. break;
  137. childreg = getregs(child, &copyregs) + addr;
  138. #ifdef CONFIG_COMPAT
  139. if (is_compat_task()) {
  140. if (addr & (sizeof(compat_long_t)-1))
  141. break;
  142. ret = put_user(*(compat_long_t *)childreg,
  143. (compat_long_t __user *)datap);
  144. } else
  145. #endif
  146. {
  147. if (addr & (sizeof(long)-1))
  148. break;
  149. ret = put_user(*(long *)childreg, datap);
  150. }
  151. break;
  152. case PTRACE_POKEUSR: /* Write register in pt_regs. */
  153. if (addr >= PTREGS_SIZE)
  154. break;
  155. childreg = getregs(child, &copyregs) + addr;
  156. #ifdef CONFIG_COMPAT
  157. if (is_compat_task()) {
  158. if (addr & (sizeof(compat_long_t)-1))
  159. break;
  160. *(compat_long_t *)childreg = data;
  161. } else
  162. #endif
  163. {
  164. if (addr & (sizeof(long)-1))
  165. break;
  166. *(long *)childreg = data;
  167. }
  168. putregs(child, &copyregs);
  169. ret = 0;
  170. break;
  171. case PTRACE_GETREGS: /* Get all registers from the child. */
  172. ret = copy_regset_to_user(child, &tile_user_regset_view,
  173. REGSET_GPR, 0,
  174. sizeof(struct pt_regs), datap);
  175. break;
  176. case PTRACE_SETREGS: /* Set all registers in the child. */
  177. ret = copy_regset_from_user(child, &tile_user_regset_view,
  178. REGSET_GPR, 0,
  179. sizeof(struct pt_regs), datap);
  180. break;
  181. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  182. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  183. break;
  184. case PTRACE_SETOPTIONS:
  185. /* Support TILE-specific ptrace options. */
  186. BUILD_BUG_ON(PTRACE_O_MASK_TILE & PTRACE_O_MASK);
  187. tmp = data & PTRACE_O_MASK_TILE;
  188. data &= ~PTRACE_O_MASK_TILE;
  189. ret = ptrace_request(child, request, addr, data);
  190. if (ret == 0) {
  191. unsigned int flags = child->ptrace;
  192. flags &= ~(PTRACE_O_MASK_TILE << PT_OPT_FLAG_SHIFT);
  193. flags |= (tmp << PT_OPT_FLAG_SHIFT);
  194. child->ptrace = flags;
  195. }
  196. break;
  197. default:
  198. #ifdef CONFIG_COMPAT
  199. if (task_thread_info(current)->status & TS_COMPAT) {
  200. ret = compat_ptrace_request(child, request,
  201. addr, data);
  202. break;
  203. }
  204. #endif
  205. ret = ptrace_request(child, request, addr, data);
  206. break;
  207. }
  208. return ret;
  209. }
  210. #ifdef CONFIG_COMPAT
  211. /* Not used; we handle compat issues in arch_ptrace() directly. */
  212. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  213. compat_ulong_t addr, compat_ulong_t data)
  214. {
  215. BUG();
  216. }
  217. #endif
  218. int do_syscall_trace_enter(struct pt_regs *regs)
  219. {
  220. u32 work = ACCESS_ONCE(current_thread_info()->flags);
  221. if ((work & _TIF_SYSCALL_TRACE) &&
  222. tracehook_report_syscall_entry(regs)) {
  223. regs->regs[TREG_SYSCALL_NR] = -1;
  224. return -1;
  225. }
  226. if (secure_computing(NULL) == -1)
  227. return -1;
  228. if (work & _TIF_SYSCALL_TRACEPOINT)
  229. trace_sys_enter(regs, regs->regs[TREG_SYSCALL_NR]);
  230. return regs->regs[TREG_SYSCALL_NR];
  231. }
  232. void do_syscall_trace_exit(struct pt_regs *regs)
  233. {
  234. long errno;
  235. /*
  236. * The standard tile calling convention returns the value (or negative
  237. * errno) in r0, and zero (or positive errno) in r1.
  238. * It saves a couple of cycles on the hot path to do this work in
  239. * registers only as we return, rather than updating the in-memory
  240. * struct ptregs.
  241. */
  242. errno = (long) regs->regs[0];
  243. if (errno < 0 && errno > -4096)
  244. regs->regs[1] = -errno;
  245. else
  246. regs->regs[1] = 0;
  247. if (test_thread_flag(TIF_SYSCALL_TRACE))
  248. tracehook_report_syscall_exit(regs, 0);
  249. if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
  250. trace_sys_exit(regs, regs->regs[0]);
  251. }
  252. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs)
  253. {
  254. struct siginfo info;
  255. memset(&info, 0, sizeof(info));
  256. info.si_signo = SIGTRAP;
  257. info.si_code = TRAP_BRKPT;
  258. info.si_addr = (void __user *) regs->pc;
  259. /* Send us the fakey SIGTRAP */
  260. force_sig_info(SIGTRAP, &info, tsk);
  261. }
  262. /* Handle synthetic interrupt delivered only by the simulator. */
  263. void __kprobes do_breakpoint(struct pt_regs* regs, int fault_num)
  264. {
  265. send_sigtrap(current, regs);
  266. }