ptrace.c 8.6 KB

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