ptrace.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * arch/sh64/kernel/ptrace.c
  7. *
  8. * Copyright (C) 2000, 2001 Paolo Alberelli
  9. * Copyright (C) 2003 Paul Mundt
  10. *
  11. * Started from SH3/4 version:
  12. * SuperH version: Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
  13. *
  14. * Original x86 implementation:
  15. * By Ross Biro 1/23/92
  16. * edited by Linus Torvalds
  17. *
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/rwsem.h>
  21. #include <linux/sched.h>
  22. #include <linux/mm.h>
  23. #include <linux/smp.h>
  24. #include <linux/smp_lock.h>
  25. #include <linux/errno.h>
  26. #include <linux/ptrace.h>
  27. #include <linux/user.h>
  28. #include <linux/signal.h>
  29. #include <linux/syscalls.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. /* This mask defines the bits of the SR which the user is not allowed to
  37. change, which are everything except S, Q, M, PR, SZ, FR. */
  38. #define SR_MASK (0xffff8cfd)
  39. /*
  40. * does not yet catch signals sent when the child dies.
  41. * in exit.c or in signal.c.
  42. */
  43. /*
  44. * This routine will get a word from the user area in the process kernel stack.
  45. */
  46. static inline int get_stack_long(struct task_struct *task, int offset)
  47. {
  48. unsigned char *stack;
  49. stack = (unsigned char *)(task->thread.uregs);
  50. stack += offset;
  51. return (*((int *)stack));
  52. }
  53. static inline unsigned long
  54. get_fpu_long(struct task_struct *task, unsigned long addr)
  55. {
  56. unsigned long tmp;
  57. struct pt_regs *regs;
  58. regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
  59. if (!tsk_used_math(task)) {
  60. if (addr == offsetof(struct user_fpu_struct, fpscr)) {
  61. tmp = FPSCR_INIT;
  62. } else {
  63. tmp = 0xffffffffUL; /* matches initial value in fpu.c */
  64. }
  65. return tmp;
  66. }
  67. if (last_task_used_math == task) {
  68. grab_fpu();
  69. fpsave(&task->thread.fpu.hard);
  70. release_fpu();
  71. last_task_used_math = 0;
  72. regs->sr |= SR_FD;
  73. }
  74. tmp = ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)];
  75. return tmp;
  76. }
  77. /*
  78. * This routine will put a word into the user area in the process kernel stack.
  79. */
  80. static inline int put_stack_long(struct task_struct *task, int offset,
  81. unsigned long data)
  82. {
  83. unsigned char *stack;
  84. stack = (unsigned char *)(task->thread.uregs);
  85. stack += offset;
  86. *(unsigned long *) stack = data;
  87. return 0;
  88. }
  89. static inline int
  90. put_fpu_long(struct task_struct *task, unsigned long addr, unsigned long data)
  91. {
  92. struct pt_regs *regs;
  93. regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
  94. if (!tsk_used_math(task)) {
  95. fpinit(&task->thread.fpu.hard);
  96. set_stopped_child_used_math(task);
  97. } else if (last_task_used_math == task) {
  98. grab_fpu();
  99. fpsave(&task->thread.fpu.hard);
  100. release_fpu();
  101. last_task_used_math = 0;
  102. regs->sr |= SR_FD;
  103. }
  104. ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)] = data;
  105. return 0;
  106. }
  107. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  108. {
  109. int ret;
  110. switch (request) {
  111. /* when I and D space are separate, these will need to be fixed. */
  112. case PTRACE_PEEKTEXT: /* read word at location addr. */
  113. case PTRACE_PEEKDATA: {
  114. unsigned long tmp;
  115. int copied;
  116. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  117. ret = -EIO;
  118. if (copied != sizeof(tmp))
  119. break;
  120. ret = put_user(tmp,(unsigned long *) data);
  121. break;
  122. }
  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. /* when I and D space are separate, this will have to be fixed. */
  143. case PTRACE_POKETEXT: /* write the word at location addr. */
  144. case PTRACE_POKEDATA:
  145. ret = 0;
  146. if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
  147. break;
  148. ret = -EIO;
  149. break;
  150. case PTRACE_POKEUSR:
  151. /* write the word at location addr in the USER area. We must
  152. disallow any changes to certain SR bits or u_fpvalid, since
  153. this could crash the kernel or result in a security
  154. loophole. */
  155. ret = -EIO;
  156. if ((addr & 3) || addr < 0)
  157. break;
  158. if (addr < sizeof(struct pt_regs)) {
  159. /* Ignore change of top 32 bits of SR */
  160. if (addr == offsetof (struct pt_regs, sr)+4)
  161. {
  162. ret = 0;
  163. break;
  164. }
  165. /* If lower 32 bits of SR, ignore non-user bits */
  166. if (addr == offsetof (struct pt_regs, sr))
  167. {
  168. long cursr = get_stack_long(child, addr);
  169. data &= ~(SR_MASK);
  170. data |= (cursr & SR_MASK);
  171. }
  172. ret = put_stack_long(child, addr, data);
  173. }
  174. else if ((addr >= offsetof(struct user, fpu)) &&
  175. (addr < offsetof(struct user, u_fpvalid))) {
  176. ret = put_fpu_long(child, addr - offsetof(struct user, fpu), data);
  177. }
  178. break;
  179. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  180. case PTRACE_CONT: { /* restart after signal. */
  181. ret = -EIO;
  182. if (!valid_signal(data))
  183. break;
  184. if (request == PTRACE_SYSCALL)
  185. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  186. else
  187. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  188. child->exit_code = data;
  189. wake_up_process(child);
  190. ret = 0;
  191. break;
  192. }
  193. /*
  194. * make the child exit. Best I can do is send it a sigkill.
  195. * perhaps it should be put in the status that it wants to
  196. * exit.
  197. */
  198. case PTRACE_KILL: {
  199. ret = 0;
  200. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  201. break;
  202. child->exit_code = SIGKILL;
  203. wake_up_process(child);
  204. break;
  205. }
  206. case PTRACE_SINGLESTEP: { /* set the trap flag. */
  207. struct pt_regs *regs;
  208. ret = -EIO;
  209. if (!valid_signal(data))
  210. break;
  211. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  212. if ((child->ptrace & PT_DTRACE) == 0) {
  213. /* Spurious delayed TF traps may occur */
  214. child->ptrace |= PT_DTRACE;
  215. }
  216. regs = child->thread.uregs;
  217. regs->sr |= SR_SSTEP; /* auto-resetting upon exception */
  218. child->exit_code = data;
  219. /* give it a chance to run. */
  220. wake_up_process(child);
  221. ret = 0;
  222. break;
  223. }
  224. case PTRACE_DETACH: /* detach a process that was attached. */
  225. ret = ptrace_detach(child, data);
  226. break;
  227. default:
  228. ret = ptrace_request(child, request, addr, data);
  229. break;
  230. }
  231. return ret;
  232. }
  233. asmlinkage int sh64_ptrace(long request, long pid, long addr, long data)
  234. {
  235. extern void poke_real_address_q(unsigned long long addr, unsigned long long data);
  236. #define WPC_DBRMODE 0x0d104008
  237. static int first_call = 1;
  238. lock_kernel();
  239. if (first_call) {
  240. /* Set WPC.DBRMODE to 0. This makes all debug events get
  241. * delivered through RESVEC, i.e. into the handlers in entry.S.
  242. * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
  243. * would normally be left set to 1, which makes debug events get
  244. * delivered through DBRVEC, i.e. into the remote gdb's
  245. * handlers. This prevents ptrace getting them, and confuses
  246. * the remote gdb.) */
  247. printk("DBRMODE set to 0 to permit native debugging\n");
  248. poke_real_address_q(WPC_DBRMODE, 0);
  249. first_call = 0;
  250. }
  251. unlock_kernel();
  252. return sys_ptrace(request, pid, addr, data);
  253. }
  254. asmlinkage void syscall_trace(void)
  255. {
  256. struct task_struct *tsk = current;
  257. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  258. return;
  259. if (!(tsk->ptrace & PT_PTRACED))
  260. return;
  261. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  262. ? 0x80 : 0));
  263. /*
  264. * this isn't the same as continuing with a signal, but it will do
  265. * for normal use. strace only continues with a signal if the
  266. * stopping signal is not SIGTRAP. -brl
  267. */
  268. if (tsk->exit_code) {
  269. send_sig(tsk->exit_code, tsk, 1);
  270. tsk->exit_code = 0;
  271. }
  272. }
  273. /* Called with interrupts disabled */
  274. asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
  275. {
  276. /* This is called after a single step exception (DEBUGSS).
  277. There is no need to change the PC, as it is a post-execution
  278. exception, as entry.S does not do anything to the PC for DEBUGSS.
  279. We need to clear the Single Step setting in SR to avoid
  280. continually stepping. */
  281. local_irq_enable();
  282. regs->sr &= ~SR_SSTEP;
  283. force_sig(SIGTRAP, current);
  284. }
  285. /* Called with interrupts disabled */
  286. asmlinkage void do_software_break_point(unsigned long long vec,
  287. struct pt_regs *regs)
  288. {
  289. /* We need to forward step the PC, to counteract the backstep done
  290. in signal.c. */
  291. local_irq_enable();
  292. force_sig(SIGTRAP, current);
  293. regs->pc += 4;
  294. }
  295. /*
  296. * Called by kernel/ptrace.c when detaching..
  297. *
  298. * Make sure single step bits etc are not set.
  299. */
  300. void ptrace_disable(struct task_struct *child)
  301. {
  302. /* nothing to do.. */
  303. }