ptrace.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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/config.h>
  20. #include <linux/kernel.h>
  21. #include <linux/rwsem.h>
  22. #include <linux/sched.h>
  23. #include <linux/mm.h>
  24. #include <linux/smp.h>
  25. #include <linux/smp_lock.h>
  26. #include <linux/errno.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/user.h>
  29. #include <linux/signal.h>
  30. #include <linux/syscalls.h>
  31. #include <asm/io.h>
  32. #include <asm/uaccess.h>
  33. #include <asm/pgtable.h>
  34. #include <asm/system.h>
  35. #include <asm/processor.h>
  36. #include <asm/mmu_context.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. grab_fpu();
  70. fpsave(&task->thread.fpu.hard);
  71. release_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. grab_fpu();
  100. fpsave(&task->thread.fpu.hard);
  101. release_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. unsigned long tmp;
  116. int copied;
  117. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  118. ret = -EIO;
  119. if (copied != sizeof(tmp))
  120. break;
  121. ret = put_user(tmp,(unsigned long *) data);
  122. break;
  123. }
  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. /* when I and D space are separate, this will have to be fixed. */
  144. case PTRACE_POKETEXT: /* write the word at location addr. */
  145. case PTRACE_POKEDATA:
  146. ret = 0;
  147. if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
  148. break;
  149. ret = -EIO;
  150. break;
  151. case PTRACE_POKEUSR:
  152. /* write the word at location addr in the USER area. We must
  153. disallow any changes to certain SR bits or u_fpvalid, since
  154. this could crash the kernel or result in a security
  155. loophole. */
  156. ret = -EIO;
  157. if ((addr & 3) || addr < 0)
  158. break;
  159. if (addr < sizeof(struct pt_regs)) {
  160. /* Ignore change of top 32 bits of SR */
  161. if (addr == offsetof (struct pt_regs, sr)+4)
  162. {
  163. ret = 0;
  164. break;
  165. }
  166. /* If lower 32 bits of SR, ignore non-user bits */
  167. if (addr == offsetof (struct pt_regs, sr))
  168. {
  169. long cursr = get_stack_long(child, addr);
  170. data &= ~(SR_MASK);
  171. data |= (cursr & SR_MASK);
  172. }
  173. ret = put_stack_long(child, addr, data);
  174. }
  175. else if ((addr >= offsetof(struct user, fpu)) &&
  176. (addr < offsetof(struct user, u_fpvalid))) {
  177. ret = put_fpu_long(child, addr - offsetof(struct user, fpu), data);
  178. }
  179. break;
  180. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  181. case PTRACE_CONT: { /* restart after signal. */
  182. ret = -EIO;
  183. if (!valid_signal(data))
  184. break;
  185. if (request == PTRACE_SYSCALL)
  186. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  187. else
  188. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  189. child->exit_code = data;
  190. wake_up_process(child);
  191. ret = 0;
  192. break;
  193. }
  194. /*
  195. * make the child exit. Best I can do is send it a sigkill.
  196. * perhaps it should be put in the status that it wants to
  197. * exit.
  198. */
  199. case PTRACE_KILL: {
  200. ret = 0;
  201. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  202. break;
  203. child->exit_code = SIGKILL;
  204. wake_up_process(child);
  205. break;
  206. }
  207. case PTRACE_SINGLESTEP: { /* set the trap flag. */
  208. struct pt_regs *regs;
  209. ret = -EIO;
  210. if (!valid_signal(data))
  211. break;
  212. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  213. if ((child->ptrace & PT_DTRACE) == 0) {
  214. /* Spurious delayed TF traps may occur */
  215. child->ptrace |= PT_DTRACE;
  216. }
  217. regs = child->thread.uregs;
  218. regs->sr |= SR_SSTEP; /* auto-resetting upon exception */
  219. child->exit_code = data;
  220. /* give it a chance to run. */
  221. wake_up_process(child);
  222. ret = 0;
  223. break;
  224. }
  225. case PTRACE_DETACH: /* detach a process that was attached. */
  226. ret = ptrace_detach(child, data);
  227. break;
  228. default:
  229. ret = ptrace_request(child, request, addr, data);
  230. break;
  231. }
  232. return ret;
  233. }
  234. asmlinkage int sh64_ptrace(long request, long pid, long addr, long data)
  235. {
  236. extern void poke_real_address_q(unsigned long long addr, unsigned long long data);
  237. #define WPC_DBRMODE 0x0d104008
  238. static int first_call = 1;
  239. lock_kernel();
  240. if (first_call) {
  241. /* Set WPC.DBRMODE to 0. This makes all debug events get
  242. * delivered through RESVEC, i.e. into the handlers in entry.S.
  243. * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
  244. * would normally be left set to 1, which makes debug events get
  245. * delivered through DBRVEC, i.e. into the remote gdb's
  246. * handlers. This prevents ptrace getting them, and confuses
  247. * the remote gdb.) */
  248. printk("DBRMODE set to 0 to permit native debugging\n");
  249. poke_real_address_q(WPC_DBRMODE, 0);
  250. first_call = 0;
  251. }
  252. unlock_kernel();
  253. return sys_ptrace(request, pid, addr, data);
  254. }
  255. asmlinkage void syscall_trace(void)
  256. {
  257. struct task_struct *tsk = current;
  258. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  259. return;
  260. if (!(tsk->ptrace & PT_PTRACED))
  261. return;
  262. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  263. ? 0x80 : 0));
  264. /*
  265. * this isn't the same as continuing with a signal, but it will do
  266. * for normal use. strace only continues with a signal if the
  267. * stopping signal is not SIGTRAP. -brl
  268. */
  269. if (tsk->exit_code) {
  270. send_sig(tsk->exit_code, tsk, 1);
  271. tsk->exit_code = 0;
  272. }
  273. }
  274. /* Called with interrupts disabled */
  275. asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
  276. {
  277. /* This is called after a single step exception (DEBUGSS).
  278. There is no need to change the PC, as it is a post-execution
  279. exception, as entry.S does not do anything to the PC for DEBUGSS.
  280. We need to clear the Single Step setting in SR to avoid
  281. continually stepping. */
  282. local_irq_enable();
  283. regs->sr &= ~SR_SSTEP;
  284. force_sig(SIGTRAP, current);
  285. }
  286. /* Called with interrupts disabled */
  287. asmlinkage void do_software_break_point(unsigned long long vec,
  288. struct pt_regs *regs)
  289. {
  290. /* We need to forward step the PC, to counteract the backstep done
  291. in signal.c. */
  292. local_irq_enable();
  293. force_sig(SIGTRAP, current);
  294. regs->pc += 4;
  295. }
  296. /*
  297. * Called by kernel/ptrace.c when detaching..
  298. *
  299. * Make sure single step bits etc are not set.
  300. */
  301. void ptrace_disable(struct task_struct *child)
  302. {
  303. /* nothing to do.. */
  304. }