ptrace.c 7.2 KB

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