ptrace.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. ret = generic_ptrace_peekdata(child, addr, data);
  115. break;
  116. /* read the word at location addr in the USER area. */
  117. case PTRACE_PEEKUSR: {
  118. unsigned long tmp;
  119. ret = -EIO;
  120. if ((addr & 3) || addr < 0)
  121. break;
  122. if (addr < sizeof(struct pt_regs))
  123. tmp = get_stack_long(child, addr);
  124. else if ((addr >= offsetof(struct user, fpu)) &&
  125. (addr < offsetof(struct user, u_fpvalid))) {
  126. tmp = get_fpu_long(child, addr - offsetof(struct user, fpu));
  127. } else if (addr == offsetof(struct user, u_fpvalid)) {
  128. tmp = !!tsk_used_math(child);
  129. } else {
  130. break;
  131. }
  132. ret = put_user(tmp, (unsigned long *)data);
  133. break;
  134. }
  135. /* when I and D space are separate, this will have to be fixed. */
  136. case PTRACE_POKETEXT: /* write the word at location addr. */
  137. case PTRACE_POKEDATA:
  138. ret = generic_ptrace_pokedata(child, addr, data);
  139. break;
  140. case PTRACE_POKEUSR:
  141. /* write the word at location addr in the USER area. We must
  142. disallow any changes to certain SR bits or u_fpvalid, since
  143. this could crash the kernel or result in a security
  144. loophole. */
  145. ret = -EIO;
  146. if ((addr & 3) || addr < 0)
  147. break;
  148. if (addr < sizeof(struct pt_regs)) {
  149. /* Ignore change of top 32 bits of SR */
  150. if (addr == offsetof (struct pt_regs, sr)+4)
  151. {
  152. ret = 0;
  153. break;
  154. }
  155. /* If lower 32 bits of SR, ignore non-user bits */
  156. if (addr == offsetof (struct pt_regs, sr))
  157. {
  158. long cursr = get_stack_long(child, addr);
  159. data &= ~(SR_MASK);
  160. data |= (cursr & SR_MASK);
  161. }
  162. ret = put_stack_long(child, addr, data);
  163. }
  164. else if ((addr >= offsetof(struct user, fpu)) &&
  165. (addr < offsetof(struct user, u_fpvalid))) {
  166. ret = put_fpu_long(child, addr - offsetof(struct user, fpu), data);
  167. }
  168. break;
  169. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  170. case PTRACE_CONT: { /* restart after signal. */
  171. ret = -EIO;
  172. if (!valid_signal(data))
  173. break;
  174. if (request == PTRACE_SYSCALL)
  175. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  176. else
  177. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  178. child->exit_code = data;
  179. wake_up_process(child);
  180. ret = 0;
  181. break;
  182. }
  183. /*
  184. * make the child exit. Best I can do is send it a sigkill.
  185. * perhaps it should be put in the status that it wants to
  186. * exit.
  187. */
  188. case PTRACE_KILL: {
  189. ret = 0;
  190. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  191. break;
  192. child->exit_code = SIGKILL;
  193. wake_up_process(child);
  194. break;
  195. }
  196. case PTRACE_SINGLESTEP: { /* set the trap flag. */
  197. struct pt_regs *regs;
  198. ret = -EIO;
  199. if (!valid_signal(data))
  200. break;
  201. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  202. if ((child->ptrace & PT_DTRACE) == 0) {
  203. /* Spurious delayed TF traps may occur */
  204. child->ptrace |= PT_DTRACE;
  205. }
  206. regs = child->thread.uregs;
  207. regs->sr |= SR_SSTEP; /* auto-resetting upon exception */
  208. child->exit_code = data;
  209. /* give it a chance to run. */
  210. wake_up_process(child);
  211. ret = 0;
  212. break;
  213. }
  214. default:
  215. ret = ptrace_request(child, request, addr, data);
  216. break;
  217. }
  218. return ret;
  219. }
  220. asmlinkage int sh64_ptrace(long request, long pid, long addr, long data)
  221. {
  222. extern void poke_real_address_q(unsigned long long addr, unsigned long long data);
  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(void)
  242. {
  243. struct task_struct *tsk = current;
  244. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  245. return;
  246. if (!(tsk->ptrace & PT_PTRACED))
  247. return;
  248. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  249. ? 0x80 : 0));
  250. /*
  251. * this isn't the same as continuing with a signal, but it will do
  252. * for normal use. strace only continues with a signal if the
  253. * stopping signal is not SIGTRAP. -brl
  254. */
  255. if (tsk->exit_code) {
  256. send_sig(tsk->exit_code, tsk, 1);
  257. tsk->exit_code = 0;
  258. }
  259. }
  260. /* Called with interrupts disabled */
  261. asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
  262. {
  263. /* This is called after a single step exception (DEBUGSS).
  264. There is no need to change the PC, as it is a post-execution
  265. exception, as entry.S does not do anything to the PC for DEBUGSS.
  266. We need to clear the Single Step setting in SR to avoid
  267. continually stepping. */
  268. local_irq_enable();
  269. regs->sr &= ~SR_SSTEP;
  270. force_sig(SIGTRAP, current);
  271. }
  272. /* Called with interrupts disabled */
  273. asmlinkage void do_software_break_point(unsigned long long vec,
  274. struct pt_regs *regs)
  275. {
  276. /* We need to forward step the PC, to counteract the backstep done
  277. in signal.c. */
  278. local_irq_enable();
  279. force_sig(SIGTRAP, current);
  280. regs->pc += 4;
  281. }
  282. /*
  283. * Called by kernel/ptrace.c when detaching..
  284. *
  285. * Make sure single step bits etc are not set.
  286. */
  287. void ptrace_disable(struct task_struct *child)
  288. {
  289. /* nothing to do.. */
  290. }