ptrace_64.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 <asm/io.h>
  31. #include <asm/uaccess.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/system.h>
  34. #include <asm/processor.h>
  35. #include <asm/mmu_context.h>
  36. #include <asm/fpu.h>
  37. /* This mask defines the bits of the SR which the user is not allowed to
  38. change, which are everything except S, Q, M, PR, SZ, FR. */
  39. #define SR_MASK (0xffff8cfd)
  40. /*
  41. * does not yet catch signals sent when the child dies.
  42. * in exit.c or in signal.c.
  43. */
  44. /*
  45. * This routine will get a word from the user area in the process kernel stack.
  46. */
  47. static inline int get_stack_long(struct task_struct *task, int offset)
  48. {
  49. unsigned char *stack;
  50. stack = (unsigned char *)(task->thread.uregs);
  51. stack += offset;
  52. return (*((int *)stack));
  53. }
  54. static inline unsigned long
  55. get_fpu_long(struct task_struct *task, unsigned long addr)
  56. {
  57. unsigned long tmp;
  58. struct pt_regs *regs;
  59. regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
  60. if (!tsk_used_math(task)) {
  61. if (addr == offsetof(struct user_fpu_struct, fpscr)) {
  62. tmp = FPSCR_INIT;
  63. } else {
  64. tmp = 0xffffffffUL; /* matches initial value in fpu.c */
  65. }
  66. return tmp;
  67. }
  68. if (last_task_used_math == task) {
  69. enable_fpu();
  70. save_fpu(task, regs);
  71. disable_fpu();
  72. last_task_used_math = 0;
  73. regs->sr |= SR_FD;
  74. }
  75. tmp = ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)];
  76. return tmp;
  77. }
  78. /*
  79. * This routine will put a word into the user area in the process kernel stack.
  80. */
  81. static inline int put_stack_long(struct task_struct *task, int offset,
  82. unsigned long data)
  83. {
  84. unsigned char *stack;
  85. stack = (unsigned char *)(task->thread.uregs);
  86. stack += offset;
  87. *(unsigned long *) stack = data;
  88. return 0;
  89. }
  90. static inline int
  91. put_fpu_long(struct task_struct *task, unsigned long addr, unsigned long data)
  92. {
  93. struct pt_regs *regs;
  94. regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
  95. if (!tsk_used_math(task)) {
  96. fpinit(&task->thread.fpu.hard);
  97. set_stopped_child_used_math(task);
  98. } else if (last_task_used_math == task) {
  99. enable_fpu();
  100. save_fpu(task, regs);
  101. disable_fpu();
  102. last_task_used_math = 0;
  103. regs->sr |= SR_FD;
  104. }
  105. ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)] = data;
  106. return 0;
  107. }
  108. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  109. {
  110. int ret;
  111. switch (request) {
  112. /* when I and D space are separate, these will need to be fixed. */
  113. case PTRACE_PEEKTEXT: /* read word at location addr. */
  114. case PTRACE_PEEKDATA:
  115. ret = generic_ptrace_peekdata(child, addr, data);
  116. break;
  117. /* read the word at location addr in the USER area. */
  118. case PTRACE_PEEKUSR: {
  119. unsigned long tmp;
  120. ret = -EIO;
  121. if ((addr & 3) || addr < 0)
  122. break;
  123. if (addr < sizeof(struct pt_regs))
  124. tmp = get_stack_long(child, addr);
  125. else if ((addr >= offsetof(struct user, fpu)) &&
  126. (addr < offsetof(struct user, u_fpvalid))) {
  127. tmp = get_fpu_long(child, addr - offsetof(struct user, fpu));
  128. } else if (addr == offsetof(struct user, u_fpvalid)) {
  129. tmp = !!tsk_used_math(child);
  130. } else {
  131. break;
  132. }
  133. ret = put_user(tmp, (unsigned long *)data);
  134. break;
  135. }
  136. /* when I and D space are separate, this will have to be fixed. */
  137. case PTRACE_POKETEXT: /* write the word at location addr. */
  138. case PTRACE_POKEDATA:
  139. ret = generic_ptrace_pokedata(child, addr, data);
  140. break;
  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. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  171. case PTRACE_CONT: { /* restart after signal. */
  172. ret = -EIO;
  173. if (!valid_signal(data))
  174. break;
  175. if (request == PTRACE_SYSCALL)
  176. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  177. else
  178. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  179. child->exit_code = data;
  180. wake_up_process(child);
  181. ret = 0;
  182. break;
  183. }
  184. /*
  185. * make the child exit. Best I can do is send it a sigkill.
  186. * perhaps it should be put in the status that it wants to
  187. * exit.
  188. */
  189. case PTRACE_KILL: {
  190. ret = 0;
  191. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  192. break;
  193. child->exit_code = SIGKILL;
  194. wake_up_process(child);
  195. break;
  196. }
  197. case PTRACE_SINGLESTEP: { /* set the trap flag. */
  198. struct pt_regs *regs;
  199. ret = -EIO;
  200. if (!valid_signal(data))
  201. break;
  202. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  203. if ((child->ptrace & PT_DTRACE) == 0) {
  204. /* Spurious delayed TF traps may occur */
  205. child->ptrace |= PT_DTRACE;
  206. }
  207. regs = child->thread.uregs;
  208. regs->sr |= SR_SSTEP; /* auto-resetting upon exception */
  209. child->exit_code = data;
  210. /* give it a chance to run. */
  211. wake_up_process(child);
  212. ret = 0;
  213. break;
  214. }
  215. default:
  216. ret = ptrace_request(child, request, addr, data);
  217. break;
  218. }
  219. return ret;
  220. }
  221. asmlinkage int sh64_ptrace(long request, long pid, long addr, long data)
  222. {
  223. #define WPC_DBRMODE 0x0d104008
  224. static int first_call = 1;
  225. lock_kernel();
  226. if (first_call) {
  227. /* Set WPC.DBRMODE to 0. This makes all debug events get
  228. * delivered through RESVEC, i.e. into the handlers in entry.S.
  229. * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
  230. * would normally be left set to 1, which makes debug events get
  231. * delivered through DBRVEC, i.e. into the remote gdb's
  232. * handlers. This prevents ptrace getting them, and confuses
  233. * the remote gdb.) */
  234. printk("DBRMODE set to 0 to permit native debugging\n");
  235. poke_real_address_q(WPC_DBRMODE, 0);
  236. first_call = 0;
  237. }
  238. unlock_kernel();
  239. return sys_ptrace(request, pid, addr, data);
  240. }
  241. asmlinkage void syscall_trace(struct pt_regs *regs, int entryexit)
  242. {
  243. struct task_struct *tsk = current;
  244. if (unlikely(current->audit_context) && entryexit)
  245. audit_syscall_exit(AUDITSC_RESULT(regs->regs[9]),
  246. regs->regs[9]);
  247. if (!test_thread_flag(TIF_SYSCALL_TRACE) &&
  248. !test_thread_flag(TIF_SINGLESTEP))
  249. goto out;
  250. if (!(tsk->ptrace & PT_PTRACED))
  251. goto out;
  252. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) &&
  253. !test_thread_flag(TIF_SINGLESTEP) ? 0x80 : 0));
  254. /*
  255. * this isn't the same as continuing with a signal, but it will do
  256. * for normal use. strace only continues with a signal if the
  257. * stopping signal is not SIGTRAP. -brl
  258. */
  259. if (tsk->exit_code) {
  260. send_sig(tsk->exit_code, tsk, 1);
  261. tsk->exit_code = 0;
  262. }
  263. out:
  264. if (unlikely(current->audit_context) && !entryexit)
  265. audit_syscall_entry(AUDIT_ARCH_SH, regs->regs[1],
  266. regs->regs[2], regs->regs[3],
  267. regs->regs[4], regs->regs[5]);
  268. }
  269. /* Called with interrupts disabled */
  270. asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
  271. {
  272. /* This is called after a single step exception (DEBUGSS).
  273. There is no need to change the PC, as it is a post-execution
  274. exception, as entry.S does not do anything to the PC for DEBUGSS.
  275. We need to clear the Single Step setting in SR to avoid
  276. continually stepping. */
  277. local_irq_enable();
  278. regs->sr &= ~SR_SSTEP;
  279. force_sig(SIGTRAP, current);
  280. }
  281. /* Called with interrupts disabled */
  282. asmlinkage void do_software_break_point(unsigned long long vec,
  283. struct pt_regs *regs)
  284. {
  285. /* We need to forward step the PC, to counteract the backstep done
  286. in signal.c. */
  287. local_irq_enable();
  288. force_sig(SIGTRAP, current);
  289. regs->pc += 4;
  290. }
  291. /*
  292. * Called by kernel/ptrace.c when detaching..
  293. *
  294. * Make sure single step bits etc are not set.
  295. */
  296. void ptrace_disable(struct task_struct *child)
  297. {
  298. /* nothing to do.. */
  299. }