ptrace_64.c 7.7 KB

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