ptrace.c 6.9 KB

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