ptrace_64.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. regs->sr &= ~SR_SSTEP;
  118. }
  119. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  120. {
  121. int ret;
  122. switch (request) {
  123. /* read the word at location addr in the USER area. */
  124. case PTRACE_PEEKUSR: {
  125. unsigned long tmp;
  126. ret = -EIO;
  127. if ((addr & 3) || addr < 0)
  128. break;
  129. if (addr < sizeof(struct pt_regs))
  130. tmp = get_stack_long(child, addr);
  131. else if ((addr >= offsetof(struct user, fpu)) &&
  132. (addr < offsetof(struct user, u_fpvalid))) {
  133. tmp = get_fpu_long(child, addr - offsetof(struct user, fpu));
  134. } else if (addr == offsetof(struct user, u_fpvalid)) {
  135. tmp = !!tsk_used_math(child);
  136. } else {
  137. break;
  138. }
  139. ret = put_user(tmp, (unsigned long *)data);
  140. break;
  141. }
  142. case PTRACE_POKEUSR:
  143. /* write the word at location addr in the USER area. We must
  144. disallow any changes to certain SR bits or u_fpvalid, since
  145. this could crash the kernel or result in a security
  146. loophole. */
  147. ret = -EIO;
  148. if ((addr & 3) || addr < 0)
  149. break;
  150. if (addr < sizeof(struct pt_regs)) {
  151. /* Ignore change of top 32 bits of SR */
  152. if (addr == offsetof (struct pt_regs, sr)+4)
  153. {
  154. ret = 0;
  155. break;
  156. }
  157. /* If lower 32 bits of SR, ignore non-user bits */
  158. if (addr == offsetof (struct pt_regs, sr))
  159. {
  160. long cursr = get_stack_long(child, addr);
  161. data &= ~(SR_MASK);
  162. data |= (cursr & SR_MASK);
  163. }
  164. ret = put_stack_long(child, addr, data);
  165. }
  166. else if ((addr >= offsetof(struct user, fpu)) &&
  167. (addr < offsetof(struct user, u_fpvalid))) {
  168. ret = put_fpu_long(child, addr - offsetof(struct user, fpu), data);
  169. }
  170. break;
  171. default:
  172. ret = ptrace_request(child, request, addr, data);
  173. break;
  174. }
  175. return ret;
  176. }
  177. asmlinkage int sh64_ptrace(long request, long pid, long addr, long data)
  178. {
  179. #define WPC_DBRMODE 0x0d104008
  180. static int first_call = 1;
  181. lock_kernel();
  182. if (first_call) {
  183. /* Set WPC.DBRMODE to 0. This makes all debug events get
  184. * delivered through RESVEC, i.e. into the handlers in entry.S.
  185. * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
  186. * would normally be left set to 1, which makes debug events get
  187. * delivered through DBRVEC, i.e. into the remote gdb's
  188. * handlers. This prevents ptrace getting them, and confuses
  189. * the remote gdb.) */
  190. printk("DBRMODE set to 0 to permit native debugging\n");
  191. poke_real_address_q(WPC_DBRMODE, 0);
  192. first_call = 0;
  193. }
  194. unlock_kernel();
  195. return sys_ptrace(request, pid, addr, data);
  196. }
  197. static inline int audit_arch(void)
  198. {
  199. int arch = EM_SH;
  200. #ifdef CONFIG_64BIT
  201. arch |= __AUDIT_ARCH_64BIT;
  202. #endif
  203. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  204. arch |= __AUDIT_ARCH_LE;
  205. #endif
  206. return arch;
  207. }
  208. asmlinkage long long do_syscall_trace_enter(struct pt_regs *regs)
  209. {
  210. long long ret = 0;
  211. secure_computing(regs->regs[9]);
  212. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  213. tracehook_report_syscall_entry(regs))
  214. /*
  215. * Tracing decided this syscall should not happen.
  216. * We'll return a bogus call number to get an ENOSYS
  217. * error, but leave the original number in regs->regs[0].
  218. */
  219. ret = -1LL;
  220. if (unlikely(current->audit_context))
  221. audit_syscall_entry(audit_arch(), regs->regs[1],
  222. regs->regs[2], regs->regs[3],
  223. regs->regs[4], regs->regs[5]);
  224. return ret ?: regs->regs[9];
  225. }
  226. asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
  227. {
  228. if (unlikely(current->audit_context))
  229. audit_syscall_exit(AUDITSC_RESULT(regs->regs[9]),
  230. regs->regs[9]);
  231. if (test_thread_flag(TIF_SYSCALL_TRACE))
  232. tracehook_report_syscall_exit(regs, 0);
  233. }
  234. /* Called with interrupts disabled */
  235. asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
  236. {
  237. /* This is called after a single step exception (DEBUGSS).
  238. There is no need to change the PC, as it is a post-execution
  239. exception, as entry.S does not do anything to the PC for DEBUGSS.
  240. We need to clear the Single Step setting in SR to avoid
  241. continually stepping. */
  242. local_irq_enable();
  243. regs->sr &= ~SR_SSTEP;
  244. force_sig(SIGTRAP, current);
  245. }
  246. /* Called with interrupts disabled */
  247. asmlinkage void do_software_break_point(unsigned long long vec,
  248. struct pt_regs *regs)
  249. {
  250. /* We need to forward step the PC, to counteract the backstep done
  251. in signal.c. */
  252. local_irq_enable();
  253. force_sig(SIGTRAP, current);
  254. regs->pc += 4;
  255. }
  256. /*
  257. * Called by kernel/ptrace.c when detaching..
  258. *
  259. * Make sure single step bits etc are not set.
  260. */
  261. void ptrace_disable(struct task_struct *child)
  262. {
  263. user_disable_single_step(child);
  264. }