ptrace.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 <asm/traps.h>
  25. #include <arch/chip.h>
  26. #define CREATE_TRACE_POINTS
  27. #include <trace/events/syscalls.h>
  28. void user_enable_single_step(struct task_struct *child)
  29. {
  30. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  31. }
  32. void user_disable_single_step(struct task_struct *child)
  33. {
  34. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  35. }
  36. /*
  37. * Called by kernel/ptrace.c when detaching..
  38. */
  39. void ptrace_disable(struct task_struct *child)
  40. {
  41. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  42. /*
  43. * These two are currently unused, but will be set by arch_ptrace()
  44. * and used in the syscall assembly when we do support them.
  45. */
  46. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  47. }
  48. /*
  49. * Get registers from task and ready the result for userspace.
  50. * Note that we localize the API issues to getregs() and putregs() at
  51. * some cost in performance, e.g. we need a full pt_regs copy for
  52. * PEEKUSR, and two copies for POKEUSR. But in general we expect
  53. * GETREGS/PUTREGS to be the API of choice anyway.
  54. */
  55. static char *getregs(struct task_struct *child, struct pt_regs *uregs)
  56. {
  57. *uregs = *task_pt_regs(child);
  58. /* Set up flags ABI bits. */
  59. uregs->flags = 0;
  60. #ifdef CONFIG_COMPAT
  61. if (task_thread_info(child)->status & TS_COMPAT)
  62. uregs->flags |= PT_FLAGS_COMPAT;
  63. #endif
  64. return (char *)uregs;
  65. }
  66. /* Put registers back to task. */
  67. static void putregs(struct task_struct *child, struct pt_regs *uregs)
  68. {
  69. struct pt_regs *regs = task_pt_regs(child);
  70. /* Don't allow overwriting the kernel-internal flags word. */
  71. uregs->flags = regs->flags;
  72. /* Only allow setting the ICS bit in the ex1 word. */
  73. uregs->ex1 = PL_ICS_EX1(USER_PL, EX1_ICS(uregs->ex1));
  74. *regs = *uregs;
  75. }
  76. enum tile_regset {
  77. REGSET_GPR,
  78. };
  79. static int tile_gpr_get(struct task_struct *target,
  80. const struct user_regset *regset,
  81. unsigned int pos, unsigned int count,
  82. void *kbuf, void __user *ubuf)
  83. {
  84. struct pt_regs regs;
  85. getregs(target, &regs);
  86. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &regs, 0,
  87. sizeof(regs));
  88. }
  89. static int tile_gpr_set(struct task_struct *target,
  90. const struct user_regset *regset,
  91. unsigned int pos, unsigned int count,
  92. const void *kbuf, const void __user *ubuf)
  93. {
  94. int ret;
  95. struct pt_regs regs;
  96. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &regs, 0,
  97. sizeof(regs));
  98. if (ret)
  99. return ret;
  100. putregs(target, &regs);
  101. return 0;
  102. }
  103. static const struct user_regset tile_user_regset[] = {
  104. [REGSET_GPR] = {
  105. .core_note_type = NT_PRSTATUS,
  106. .n = ELF_NGREG,
  107. .size = sizeof(elf_greg_t),
  108. .align = sizeof(elf_greg_t),
  109. .get = tile_gpr_get,
  110. .set = tile_gpr_set,
  111. },
  112. };
  113. static const struct user_regset_view tile_user_regset_view = {
  114. .name = CHIP_ARCH_NAME,
  115. .e_machine = ELF_ARCH,
  116. .ei_osabi = ELF_OSABI,
  117. .regsets = tile_user_regset,
  118. .n = ARRAY_SIZE(tile_user_regset),
  119. };
  120. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  121. {
  122. return &tile_user_regset_view;
  123. }
  124. long arch_ptrace(struct task_struct *child, long request,
  125. unsigned long addr, unsigned long data)
  126. {
  127. unsigned long __user *datap = (long __user __force *)data;
  128. unsigned long tmp;
  129. long ret = -EIO;
  130. char *childreg;
  131. struct pt_regs copyregs;
  132. switch (request) {
  133. case PTRACE_PEEKUSR: /* Read register from pt_regs. */
  134. if (addr >= PTREGS_SIZE)
  135. break;
  136. childreg = getregs(child, &copyregs) + addr;
  137. #ifdef CONFIG_COMPAT
  138. if (is_compat_task()) {
  139. if (addr & (sizeof(compat_long_t)-1))
  140. break;
  141. ret = put_user(*(compat_long_t *)childreg,
  142. (compat_long_t __user *)datap);
  143. } else
  144. #endif
  145. {
  146. if (addr & (sizeof(long)-1))
  147. break;
  148. ret = put_user(*(long *)childreg, datap);
  149. }
  150. break;
  151. case PTRACE_POKEUSR: /* Write register in pt_regs. */
  152. if (addr >= PTREGS_SIZE)
  153. break;
  154. childreg = getregs(child, &copyregs) + addr;
  155. #ifdef CONFIG_COMPAT
  156. if (is_compat_task()) {
  157. if (addr & (sizeof(compat_long_t)-1))
  158. break;
  159. *(compat_long_t *)childreg = data;
  160. } else
  161. #endif
  162. {
  163. if (addr & (sizeof(long)-1))
  164. break;
  165. *(long *)childreg = data;
  166. }
  167. putregs(child, &copyregs);
  168. ret = 0;
  169. break;
  170. case PTRACE_GETREGS: /* Get all registers from the child. */
  171. ret = copy_regset_to_user(child, &tile_user_regset_view,
  172. REGSET_GPR, 0,
  173. sizeof(struct pt_regs), datap);
  174. break;
  175. case PTRACE_SETREGS: /* Set all registers in the child. */
  176. ret = copy_regset_from_user(child, &tile_user_regset_view,
  177. REGSET_GPR, 0,
  178. sizeof(struct pt_regs), datap);
  179. break;
  180. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  181. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  182. break;
  183. case PTRACE_SETOPTIONS:
  184. /* Support TILE-specific ptrace options. */
  185. BUILD_BUG_ON(PTRACE_O_MASK_TILE & PTRACE_O_MASK);
  186. tmp = data & PTRACE_O_MASK_TILE;
  187. data &= ~PTRACE_O_MASK_TILE;
  188. ret = ptrace_request(child, request, addr, data);
  189. if (ret == 0) {
  190. unsigned int flags = child->ptrace;
  191. flags &= ~(PTRACE_O_MASK_TILE << PT_OPT_FLAG_SHIFT);
  192. flags |= (tmp << PT_OPT_FLAG_SHIFT);
  193. child->ptrace = flags;
  194. }
  195. break;
  196. default:
  197. #ifdef CONFIG_COMPAT
  198. if (task_thread_info(current)->status & TS_COMPAT) {
  199. ret = compat_ptrace_request(child, request,
  200. addr, data);
  201. break;
  202. }
  203. #endif
  204. ret = ptrace_request(child, request, addr, data);
  205. break;
  206. }
  207. return ret;
  208. }
  209. #ifdef CONFIG_COMPAT
  210. /* Not used; we handle compat issues in arch_ptrace() directly. */
  211. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  212. compat_ulong_t addr, compat_ulong_t data)
  213. {
  214. BUG();
  215. }
  216. #endif
  217. int do_syscall_trace_enter(struct pt_regs *regs)
  218. {
  219. if (test_thread_flag(TIF_SYSCALL_TRACE)) {
  220. if (tracehook_report_syscall_entry(regs))
  221. regs->regs[TREG_SYSCALL_NR] = -1;
  222. }
  223. if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
  224. trace_sys_enter(regs, regs->regs[TREG_SYSCALL_NR]);
  225. return regs->regs[TREG_SYSCALL_NR];
  226. }
  227. void do_syscall_trace_exit(struct pt_regs *regs)
  228. {
  229. if (test_thread_flag(TIF_SYSCALL_TRACE))
  230. tracehook_report_syscall_exit(regs, 0);
  231. if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
  232. trace_sys_exit(regs, regs->regs[0]);
  233. }
  234. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
  235. {
  236. struct siginfo info;
  237. memset(&info, 0, sizeof(info));
  238. info.si_signo = SIGTRAP;
  239. info.si_code = TRAP_BRKPT;
  240. info.si_addr = (void __user *) regs->pc;
  241. /* Send us the fakey SIGTRAP */
  242. force_sig_info(SIGTRAP, &info, tsk);
  243. }
  244. /* Handle synthetic interrupt delivered only by the simulator. */
  245. void __kprobes do_breakpoint(struct pt_regs* regs, int fault_num)
  246. {
  247. send_sigtrap(current, regs, fault_num);
  248. }