ptrace_64.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 <linux/tracehook.h>
  32. #include <asm/io.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/pgtable.h>
  35. #include <asm/system.h>
  36. #include <asm/processor.h>
  37. #include <asm/mmu_context.h>
  38. #include <asm/syscalls.h>
  39. #include <asm/fpu.h>
  40. /* This mask defines the bits of the SR which the user is not allowed to
  41. change, which are everything except S, Q, M, PR, SZ, FR. */
  42. #define SR_MASK (0xffff8cfd)
  43. /*
  44. * does not yet catch signals sent when the child dies.
  45. * in exit.c or in signal.c.
  46. */
  47. /*
  48. * This routine will get a word from the user area in the process kernel stack.
  49. */
  50. static inline int get_stack_long(struct task_struct *task, int offset)
  51. {
  52. unsigned char *stack;
  53. stack = (unsigned char *)(task->thread.uregs);
  54. stack += offset;
  55. return (*((int *)stack));
  56. }
  57. static inline unsigned long
  58. get_fpu_long(struct task_struct *task, unsigned long addr)
  59. {
  60. unsigned long tmp;
  61. struct pt_regs *regs;
  62. regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
  63. if (!tsk_used_math(task)) {
  64. if (addr == offsetof(struct user_fpu_struct, fpscr)) {
  65. tmp = FPSCR_INIT;
  66. } else {
  67. tmp = 0xffffffffUL; /* matches initial value in fpu.c */
  68. }
  69. return tmp;
  70. }
  71. if (last_task_used_math == task) {
  72. enable_fpu();
  73. save_fpu(task, regs);
  74. disable_fpu();
  75. last_task_used_math = 0;
  76. regs->sr |= SR_FD;
  77. }
  78. tmp = ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)];
  79. return tmp;
  80. }
  81. /*
  82. * This routine will put a word into the user area in the process kernel stack.
  83. */
  84. static inline int put_stack_long(struct task_struct *task, int offset,
  85. unsigned long data)
  86. {
  87. unsigned char *stack;
  88. stack = (unsigned char *)(task->thread.uregs);
  89. stack += offset;
  90. *(unsigned long *) stack = data;
  91. return 0;
  92. }
  93. static inline int
  94. put_fpu_long(struct task_struct *task, unsigned long addr, unsigned long data)
  95. {
  96. struct pt_regs *regs;
  97. regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
  98. if (!tsk_used_math(task)) {
  99. fpinit(&task->thread.fpu.hard);
  100. set_stopped_child_used_math(task);
  101. } else if (last_task_used_math == task) {
  102. enable_fpu();
  103. save_fpu(task, regs);
  104. disable_fpu();
  105. last_task_used_math = 0;
  106. regs->sr |= SR_FD;
  107. }
  108. ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)] = data;
  109. return 0;
  110. }
  111. void user_enable_single_step(struct task_struct *child)
  112. {
  113. struct pt_regs *regs = child->thread.uregs;
  114. regs->sr |= SR_SSTEP; /* auto-resetting upon exception */
  115. }
  116. void user_disable_single_step(struct task_struct *child)
  117. {
  118. struct pt_regs *regs = child->thread.uregs;
  119. regs->sr &= ~SR_SSTEP;
  120. }
  121. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  122. {
  123. int ret;
  124. switch (request) {
  125. /* read the word at location addr in the USER area. */
  126. case PTRACE_PEEKUSR: {
  127. unsigned long tmp;
  128. ret = -EIO;
  129. if ((addr & 3) || addr < 0)
  130. break;
  131. if (addr < sizeof(struct pt_regs))
  132. tmp = get_stack_long(child, addr);
  133. else if ((addr >= offsetof(struct user, fpu)) &&
  134. (addr < offsetof(struct user, u_fpvalid))) {
  135. tmp = get_fpu_long(child, addr - offsetof(struct user, fpu));
  136. } else if (addr == offsetof(struct user, u_fpvalid)) {
  137. tmp = !!tsk_used_math(child);
  138. } else {
  139. break;
  140. }
  141. ret = put_user(tmp, (unsigned long *)data);
  142. break;
  143. }
  144. case PTRACE_POKEUSR:
  145. /* write the word at location addr in the USER area. We must
  146. disallow any changes to certain SR bits or u_fpvalid, since
  147. this could crash the kernel or result in a security
  148. loophole. */
  149. ret = -EIO;
  150. if ((addr & 3) || addr < 0)
  151. break;
  152. if (addr < sizeof(struct pt_regs)) {
  153. /* Ignore change of top 32 bits of SR */
  154. if (addr == offsetof (struct pt_regs, sr)+4)
  155. {
  156. ret = 0;
  157. break;
  158. }
  159. /* If lower 32 bits of SR, ignore non-user bits */
  160. if (addr == offsetof (struct pt_regs, sr))
  161. {
  162. long cursr = get_stack_long(child, addr);
  163. data &= ~(SR_MASK);
  164. data |= (cursr & SR_MASK);
  165. }
  166. ret = put_stack_long(child, addr, data);
  167. }
  168. else if ((addr >= offsetof(struct user, fpu)) &&
  169. (addr < offsetof(struct user, u_fpvalid))) {
  170. ret = put_fpu_long(child, addr - offsetof(struct user, fpu), data);
  171. }
  172. break;
  173. default:
  174. ret = ptrace_request(child, request, addr, data);
  175. break;
  176. }
  177. return ret;
  178. }
  179. asmlinkage int sh64_ptrace(long request, long pid, long addr, long data)
  180. {
  181. #define WPC_DBRMODE 0x0d104008
  182. static int first_call = 1;
  183. lock_kernel();
  184. if (first_call) {
  185. /* Set WPC.DBRMODE to 0. This makes all debug events get
  186. * delivered through RESVEC, i.e. into the handlers in entry.S.
  187. * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
  188. * would normally be left set to 1, which makes debug events get
  189. * delivered through DBRVEC, i.e. into the remote gdb's
  190. * handlers. This prevents ptrace getting them, and confuses
  191. * the remote gdb.) */
  192. printk("DBRMODE set to 0 to permit native debugging\n");
  193. poke_real_address_q(WPC_DBRMODE, 0);
  194. first_call = 0;
  195. }
  196. unlock_kernel();
  197. return sys_ptrace(request, pid, addr, data);
  198. }
  199. static inline int audit_arch(void)
  200. {
  201. int arch = EM_SH;
  202. #ifdef CONFIG_64BIT
  203. arch |= __AUDIT_ARCH_64BIT;
  204. #endif
  205. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  206. arch |= __AUDIT_ARCH_LE;
  207. #endif
  208. return arch;
  209. }
  210. asmlinkage long long do_syscall_trace_enter(struct pt_regs *regs)
  211. {
  212. long long ret = 0;
  213. secure_computing(regs->regs[9]);
  214. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  215. tracehook_report_syscall_entry(regs))
  216. /*
  217. * Tracing decided this syscall should not happen.
  218. * We'll return a bogus call number to get an ENOSYS
  219. * error, but leave the original number in regs->regs[0].
  220. */
  221. ret = -1LL;
  222. if (unlikely(current->audit_context))
  223. audit_syscall_entry(audit_arch(), regs->regs[1],
  224. regs->regs[2], regs->regs[3],
  225. regs->regs[4], regs->regs[5]);
  226. return ret ?: regs->regs[9];
  227. }
  228. asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
  229. {
  230. if (unlikely(current->audit_context))
  231. audit_syscall_exit(AUDITSC_RESULT(regs->regs[9]),
  232. regs->regs[9]);
  233. if (test_thread_flag(TIF_SYSCALL_TRACE))
  234. tracehook_report_syscall_exit(regs, 0);
  235. }
  236. /* Called with interrupts disabled */
  237. asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
  238. {
  239. /* This is called after a single step exception (DEBUGSS).
  240. There is no need to change the PC, as it is a post-execution
  241. exception, as entry.S does not do anything to the PC for DEBUGSS.
  242. We need to clear the Single Step setting in SR to avoid
  243. continually stepping. */
  244. local_irq_enable();
  245. regs->sr &= ~SR_SSTEP;
  246. force_sig(SIGTRAP, current);
  247. }
  248. /* Called with interrupts disabled */
  249. asmlinkage void do_software_break_point(unsigned long long vec,
  250. struct pt_regs *regs)
  251. {
  252. /* We need to forward step the PC, to counteract the backstep done
  253. in signal.c. */
  254. local_irq_enable();
  255. force_sig(SIGTRAP, current);
  256. regs->pc += 4;
  257. }
  258. /*
  259. * Called by kernel/ptrace.c when detaching..
  260. *
  261. * Make sure single step bits etc are not set.
  262. */
  263. void ptrace_disable(struct task_struct *child)
  264. {
  265. user_disable_single_step(child);
  266. }