ptrace_64.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * arch/sh/kernel/ptrace_64.c
  3. *
  4. * Copyright (C) 2000, 2001 Paolo Alberelli
  5. * Copyright (C) 2003 - 2007 Paul Mundt
  6. *
  7. * Started from SH3/4 version:
  8. * SuperH version: Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
  9. *
  10. * Original x86 implementation:
  11. * By Ross Biro 1/23/92
  12. * edited by Linus Torvalds
  13. *
  14. * This file is subject to the terms and conditions of the GNU General Public
  15. * License. See the file "COPYING" in the main directory of this archive
  16. * for more details.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/rwsem.h>
  20. #include <linux/sched.h>
  21. #include <linux/mm.h>
  22. #include <linux/smp.h>
  23. #include <linux/smp_lock.h>
  24. #include <linux/errno.h>
  25. #include <linux/ptrace.h>
  26. #include <linux/user.h>
  27. #include <linux/signal.h>
  28. #include <linux/syscalls.h>
  29. #include <linux/audit.h>
  30. #include <linux/seccomp.h>
  31. #include <asm/io.h>
  32. #include <asm/uaccess.h>
  33. #include <asm/pgtable.h>
  34. #include <asm/system.h>
  35. #include <asm/processor.h>
  36. #include <asm/mmu_context.h>
  37. #include <asm/fpu.h>
  38. /* This mask defines the bits of the SR which the user is not allowed to
  39. change, which are everything except S, Q, M, PR, SZ, FR. */
  40. #define SR_MASK (0xffff8cfd)
  41. /*
  42. * does not yet catch signals sent when the child dies.
  43. * in exit.c or in signal.c.
  44. */
  45. /*
  46. * This routine will get a word from the user area in the process kernel stack.
  47. */
  48. static inline int get_stack_long(struct task_struct *task, int offset)
  49. {
  50. unsigned char *stack;
  51. stack = (unsigned char *)(task->thread.uregs);
  52. stack += offset;
  53. return (*((int *)stack));
  54. }
  55. static inline unsigned long
  56. get_fpu_long(struct task_struct *task, unsigned long addr)
  57. {
  58. unsigned long tmp;
  59. struct pt_regs *regs;
  60. regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
  61. if (!tsk_used_math(task)) {
  62. if (addr == offsetof(struct user_fpu_struct, fpscr)) {
  63. tmp = FPSCR_INIT;
  64. } else {
  65. tmp = 0xffffffffUL; /* matches initial value in fpu.c */
  66. }
  67. return tmp;
  68. }
  69. if (last_task_used_math == task) {
  70. enable_fpu();
  71. save_fpu(task, regs);
  72. disable_fpu();
  73. last_task_used_math = 0;
  74. regs->sr |= SR_FD;
  75. }
  76. tmp = ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)];
  77. return tmp;
  78. }
  79. /*
  80. * This routine will put a word into the user area in the process kernel stack.
  81. */
  82. static inline int put_stack_long(struct task_struct *task, int offset,
  83. unsigned long data)
  84. {
  85. unsigned char *stack;
  86. stack = (unsigned char *)(task->thread.uregs);
  87. stack += offset;
  88. *(unsigned long *) stack = data;
  89. return 0;
  90. }
  91. static inline int
  92. put_fpu_long(struct task_struct *task, unsigned long addr, unsigned long data)
  93. {
  94. struct pt_regs *regs;
  95. regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
  96. if (!tsk_used_math(task)) {
  97. fpinit(&task->thread.fpu.hard);
  98. set_stopped_child_used_math(task);
  99. } else if (last_task_used_math == task) {
  100. enable_fpu();
  101. save_fpu(task, regs);
  102. disable_fpu();
  103. last_task_used_math = 0;
  104. regs->sr |= SR_FD;
  105. }
  106. ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)] = data;
  107. return 0;
  108. }
  109. void user_enable_single_step(struct task_struct *child)
  110. {
  111. struct pt_regs *regs = child->thread.uregs;
  112. regs->sr |= SR_SSTEP; /* auto-resetting upon exception */
  113. }
  114. void user_disable_single_step(struct task_struct *child)
  115. {
  116. regs->sr &= ~SR_SSTEP;
  117. }
  118. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  119. {
  120. int ret;
  121. switch (request) {
  122. /* read the word at location addr in the USER area. */
  123. case PTRACE_PEEKUSR: {
  124. unsigned long tmp;
  125. ret = -EIO;
  126. if ((addr & 3) || addr < 0)
  127. break;
  128. if (addr < sizeof(struct pt_regs))
  129. tmp = get_stack_long(child, addr);
  130. else if ((addr >= offsetof(struct user, fpu)) &&
  131. (addr < offsetof(struct user, u_fpvalid))) {
  132. tmp = get_fpu_long(child, addr - offsetof(struct user, fpu));
  133. } else if (addr == offsetof(struct user, u_fpvalid)) {
  134. tmp = !!tsk_used_math(child);
  135. } else {
  136. break;
  137. }
  138. ret = put_user(tmp, (unsigned long *)data);
  139. break;
  140. }
  141. case PTRACE_POKEUSR:
  142. /* write the word at location addr in the USER area. We must
  143. disallow any changes to certain SR bits or u_fpvalid, since
  144. this could crash the kernel or result in a security
  145. loophole. */
  146. ret = -EIO;
  147. if ((addr & 3) || addr < 0)
  148. break;
  149. if (addr < sizeof(struct pt_regs)) {
  150. /* Ignore change of top 32 bits of SR */
  151. if (addr == offsetof (struct pt_regs, sr)+4)
  152. {
  153. ret = 0;
  154. break;
  155. }
  156. /* If lower 32 bits of SR, ignore non-user bits */
  157. if (addr == offsetof (struct pt_regs, sr))
  158. {
  159. long cursr = get_stack_long(child, addr);
  160. data &= ~(SR_MASK);
  161. data |= (cursr & SR_MASK);
  162. }
  163. ret = put_stack_long(child, addr, data);
  164. }
  165. else if ((addr >= offsetof(struct user, fpu)) &&
  166. (addr < offsetof(struct user, u_fpvalid))) {
  167. ret = put_fpu_long(child, addr - offsetof(struct user, fpu), data);
  168. }
  169. break;
  170. default:
  171. ret = ptrace_request(child, request, addr, data);
  172. break;
  173. }
  174. return ret;
  175. }
  176. asmlinkage int sh64_ptrace(long request, long pid, long addr, long data)
  177. {
  178. #define WPC_DBRMODE 0x0d104008
  179. static int first_call = 1;
  180. lock_kernel();
  181. if (first_call) {
  182. /* Set WPC.DBRMODE to 0. This makes all debug events get
  183. * delivered through RESVEC, i.e. into the handlers in entry.S.
  184. * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
  185. * would normally be left set to 1, which makes debug events get
  186. * delivered through DBRVEC, i.e. into the remote gdb's
  187. * handlers. This prevents ptrace getting them, and confuses
  188. * the remote gdb.) */
  189. printk("DBRMODE set to 0 to permit native debugging\n");
  190. poke_real_address_q(WPC_DBRMODE, 0);
  191. first_call = 0;
  192. }
  193. unlock_kernel();
  194. return sys_ptrace(request, pid, addr, data);
  195. }
  196. asmlinkage void syscall_trace(struct pt_regs *regs, int entryexit)
  197. {
  198. struct task_struct *tsk = current;
  199. secure_computing(regs->regs[9]);
  200. if (unlikely(current->audit_context) && entryexit)
  201. audit_syscall_exit(AUDITSC_RESULT(regs->regs[9]),
  202. regs->regs[9]);
  203. if (!test_thread_flag(TIF_SYSCALL_TRACE) &&
  204. !test_thread_flag(TIF_SINGLESTEP))
  205. goto out;
  206. if (!(tsk->ptrace & PT_PTRACED))
  207. goto out;
  208. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) &&
  209. !test_thread_flag(TIF_SINGLESTEP) ? 0x80 : 0));
  210. /*
  211. * this isn't the same as continuing with a signal, but it will do
  212. * for normal use. strace only continues with a signal if the
  213. * stopping signal is not SIGTRAP. -brl
  214. */
  215. if (tsk->exit_code) {
  216. send_sig(tsk->exit_code, tsk, 1);
  217. tsk->exit_code = 0;
  218. }
  219. out:
  220. if (unlikely(current->audit_context) && !entryexit)
  221. audit_syscall_entry(AUDIT_ARCH_SH, regs->regs[1],
  222. regs->regs[2], regs->regs[3],
  223. regs->regs[4], regs->regs[5]);
  224. }
  225. /* Called with interrupts disabled */
  226. asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
  227. {
  228. /* This is called after a single step exception (DEBUGSS).
  229. There is no need to change the PC, as it is a post-execution
  230. exception, as entry.S does not do anything to the PC for DEBUGSS.
  231. We need to clear the Single Step setting in SR to avoid
  232. continually stepping. */
  233. local_irq_enable();
  234. regs->sr &= ~SR_SSTEP;
  235. force_sig(SIGTRAP, current);
  236. }
  237. /* Called with interrupts disabled */
  238. asmlinkage void do_software_break_point(unsigned long long vec,
  239. struct pt_regs *regs)
  240. {
  241. /* We need to forward step the PC, to counteract the backstep done
  242. in signal.c. */
  243. local_irq_enable();
  244. force_sig(SIGTRAP, current);
  245. regs->pc += 4;
  246. }
  247. /*
  248. * Called by kernel/ptrace.c when detaching..
  249. *
  250. * Make sure single step bits etc are not set.
  251. */
  252. void ptrace_disable(struct task_struct *child)
  253. {
  254. user_disable_single_step(child);
  255. }