ptrace.c 9.3 KB

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