ptrace.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* MN10300 Process tracing
  2. *
  3. * Copyright (C) 2007 Matsushita Electric Industrial Co., Ltd.
  4. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  5. * Modified by David Howells (dhowells@redhat.com)
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public Licence
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the Licence, or (at your option) any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/mm.h>
  15. #include <linux/smp.h>
  16. #include <linux/smp_lock.h>
  17. #include <linux/errno.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/user.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/pgtable.h>
  22. #include <asm/system.h>
  23. #include <asm/processor.h>
  24. #include <asm/cacheflush.h>
  25. #include <asm/fpu.h>
  26. #include <asm/asm-offsets.h>
  27. /*
  28. * translate ptrace register IDs into struct pt_regs offsets
  29. */
  30. static const u8 ptrace_regid_to_frame[] = {
  31. [PT_A3 << 2] = REG_A3,
  32. [PT_A2 << 2] = REG_A2,
  33. [PT_D3 << 2] = REG_D3,
  34. [PT_D2 << 2] = REG_D2,
  35. [PT_MCVF << 2] = REG_MCVF,
  36. [PT_MCRL << 2] = REG_MCRL,
  37. [PT_MCRH << 2] = REG_MCRH,
  38. [PT_MDRQ << 2] = REG_MDRQ,
  39. [PT_E1 << 2] = REG_E1,
  40. [PT_E0 << 2] = REG_E0,
  41. [PT_E7 << 2] = REG_E7,
  42. [PT_E6 << 2] = REG_E6,
  43. [PT_E5 << 2] = REG_E5,
  44. [PT_E4 << 2] = REG_E4,
  45. [PT_E3 << 2] = REG_E3,
  46. [PT_E2 << 2] = REG_E2,
  47. [PT_SP << 2] = REG_SP,
  48. [PT_LAR << 2] = REG_LAR,
  49. [PT_LIR << 2] = REG_LIR,
  50. [PT_MDR << 2] = REG_MDR,
  51. [PT_A1 << 2] = REG_A1,
  52. [PT_A0 << 2] = REG_A0,
  53. [PT_D1 << 2] = REG_D1,
  54. [PT_D0 << 2] = REG_D0,
  55. [PT_ORIG_D0 << 2] = REG_ORIG_D0,
  56. [PT_EPSW << 2] = REG_EPSW,
  57. [PT_PC << 2] = REG_PC,
  58. };
  59. static inline int get_stack_long(struct task_struct *task, int offset)
  60. {
  61. return *(unsigned long *)
  62. ((unsigned long) task->thread.uregs + offset);
  63. }
  64. /*
  65. * this routine will put a word on the processes privileged stack.
  66. * the offset is how far from the base addr as stored in the TSS.
  67. * this routine assumes that all the privileged stacks are in our
  68. * data space.
  69. */
  70. static inline
  71. int put_stack_long(struct task_struct *task, int offset, unsigned long data)
  72. {
  73. unsigned long stack;
  74. stack = (unsigned long) task->thread.uregs + offset;
  75. *(unsigned long *) stack = data;
  76. return 0;
  77. }
  78. static inline unsigned long get_fpregs(struct fpu_state_struct *buf,
  79. struct task_struct *tsk)
  80. {
  81. return __copy_to_user(buf, &tsk->thread.fpu_state,
  82. sizeof(struct fpu_state_struct));
  83. }
  84. static inline unsigned long set_fpregs(struct task_struct *tsk,
  85. struct fpu_state_struct *buf)
  86. {
  87. return __copy_from_user(&tsk->thread.fpu_state, buf,
  88. sizeof(struct fpu_state_struct));
  89. }
  90. static inline void fpsave_init(struct task_struct *task)
  91. {
  92. memset(&task->thread.fpu_state, 0, sizeof(struct fpu_state_struct));
  93. }
  94. /*
  95. * make sure the single step bit is not set
  96. */
  97. void ptrace_disable(struct task_struct *child)
  98. {
  99. #ifndef CONFIG_MN10300_USING_JTAG
  100. struct user *dummy = NULL;
  101. long tmp;
  102. tmp = get_stack_long(child, (unsigned long) &dummy->regs.epsw);
  103. tmp &= ~EPSW_T;
  104. put_stack_long(child, (unsigned long) &dummy->regs.epsw, tmp);
  105. #endif
  106. }
  107. /*
  108. * set the single step bit
  109. */
  110. void ptrace_enable(struct task_struct *child)
  111. {
  112. #ifndef CONFIG_MN10300_USING_JTAG
  113. struct user *dummy = NULL;
  114. long tmp;
  115. tmp = get_stack_long(child, (unsigned long) &dummy->regs.epsw);
  116. tmp |= EPSW_T;
  117. put_stack_long(child, (unsigned long) &dummy->regs.epsw, tmp);
  118. #endif
  119. }
  120. /*
  121. * handle the arch-specific side of process tracing
  122. */
  123. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  124. {
  125. struct fpu_state_struct fpu_state;
  126. int i, ret;
  127. switch (request) {
  128. /* read the word at location addr. */
  129. case PTRACE_PEEKTEXT: {
  130. unsigned long tmp;
  131. int copied;
  132. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  133. ret = -EIO;
  134. if (copied != sizeof(tmp))
  135. break;
  136. ret = put_user(tmp, (unsigned long *) data);
  137. break;
  138. }
  139. /* read the word at location addr. */
  140. case PTRACE_PEEKDATA: {
  141. unsigned long tmp;
  142. int copied;
  143. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  144. ret = -EIO;
  145. if (copied != sizeof(tmp))
  146. break;
  147. ret = put_user(tmp, (unsigned long *) data);
  148. break;
  149. }
  150. /* read the word at location addr in the USER area. */
  151. case PTRACE_PEEKUSR: {
  152. unsigned long tmp;
  153. ret = -EIO;
  154. if ((addr & 3) || addr < 0 ||
  155. addr > sizeof(struct user) - 3)
  156. break;
  157. tmp = 0; /* Default return condition */
  158. if (addr < NR_PTREGS << 2)
  159. tmp = get_stack_long(child,
  160. ptrace_regid_to_frame[addr]);
  161. ret = put_user(tmp, (unsigned long *) data);
  162. break;
  163. }
  164. /* write the word at location addr. */
  165. case PTRACE_POKETEXT:
  166. case PTRACE_POKEDATA:
  167. if (access_process_vm(child, addr, &data, sizeof(data), 1) ==
  168. sizeof(data))
  169. ret = 0;
  170. else
  171. ret = -EIO;
  172. break;
  173. /* write the word at location addr in the USER area */
  174. case PTRACE_POKEUSR:
  175. ret = -EIO;
  176. if ((addr & 3) || addr < 0 ||
  177. addr > sizeof(struct user) - 3)
  178. break;
  179. ret = 0;
  180. if (addr < NR_PTREGS << 2)
  181. ret = put_stack_long(child, ptrace_regid_to_frame[addr],
  182. data);
  183. break;
  184. /* continue and stop at next (return from) syscall */
  185. case PTRACE_SYSCALL:
  186. /* restart after signal. */
  187. case PTRACE_CONT:
  188. ret = -EIO;
  189. if ((unsigned long) data > _NSIG)
  190. break;
  191. if (request == PTRACE_SYSCALL)
  192. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  193. else
  194. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  195. child->exit_code = data;
  196. ptrace_disable(child);
  197. wake_up_process(child);
  198. ret = 0;
  199. break;
  200. /*
  201. * make the child exit
  202. * - the best I can do is send it a sigkill
  203. * - perhaps it should be put in the status that it wants to
  204. * exit
  205. */
  206. case PTRACE_KILL:
  207. ret = 0;
  208. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  209. break;
  210. child->exit_code = SIGKILL;
  211. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  212. ptrace_disable(child);
  213. wake_up_process(child);
  214. break;
  215. case PTRACE_SINGLESTEP: /* set the trap flag. */
  216. #ifndef CONFIG_MN10300_USING_JTAG
  217. ret = -EIO;
  218. if ((unsigned long) data > _NSIG)
  219. break;
  220. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  221. ptrace_enable(child);
  222. child->exit_code = data;
  223. wake_up_process(child);
  224. ret = 0;
  225. #else
  226. ret = -EINVAL;
  227. #endif
  228. break;
  229. case PTRACE_DETACH: /* detach a process that was attached. */
  230. ret = ptrace_detach(child, data);
  231. break;
  232. /* Get all gp regs from the child. */
  233. case PTRACE_GETREGS: {
  234. unsigned long tmp;
  235. if (!access_ok(VERIFY_WRITE, (unsigned *) data, NR_PTREGS << 2)) {
  236. ret = -EIO;
  237. break;
  238. }
  239. for (i = 0; i < NR_PTREGS << 2; i += 4) {
  240. tmp = get_stack_long(child, ptrace_regid_to_frame[i]);
  241. __put_user(tmp, (unsigned long *) data);
  242. data += sizeof(tmp);
  243. }
  244. ret = 0;
  245. break;
  246. }
  247. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  248. unsigned long tmp;
  249. if (!access_ok(VERIFY_READ, (unsigned long *)data,
  250. sizeof(struct pt_regs))) {
  251. ret = -EIO;
  252. break;
  253. }
  254. for (i = 0; i < NR_PTREGS << 2; i += 4) {
  255. __get_user(tmp, (unsigned long *) data);
  256. put_stack_long(child, ptrace_regid_to_frame[i], tmp);
  257. data += sizeof(tmp);
  258. }
  259. ret = 0;
  260. break;
  261. }
  262. case PTRACE_GETFPREGS: { /* Get the child FPU state. */
  263. if (is_using_fpu(child)) {
  264. unlazy_fpu(child);
  265. fpu_state = child->thread.fpu_state;
  266. } else {
  267. memset(&fpu_state, 0, sizeof(fpu_state));
  268. }
  269. ret = -EIO;
  270. if (copy_to_user((void *) data, &fpu_state,
  271. sizeof(fpu_state)) == 0)
  272. ret = 0;
  273. break;
  274. }
  275. case PTRACE_SETFPREGS: { /* Set the child FPU state. */
  276. ret = -EFAULT;
  277. if (copy_from_user(&fpu_state, (const void *) data,
  278. sizeof(fpu_state)) == 0) {
  279. fpu_kill_state(child);
  280. child->thread.fpu_state = fpu_state;
  281. set_using_fpu(child);
  282. ret = 0;
  283. }
  284. break;
  285. }
  286. case PTRACE_SETOPTIONS: {
  287. if (data & PTRACE_O_TRACESYSGOOD)
  288. child->ptrace |= PT_TRACESYSGOOD;
  289. else
  290. child->ptrace &= ~PT_TRACESYSGOOD;
  291. ret = 0;
  292. break;
  293. }
  294. default:
  295. ret = -EIO;
  296. break;
  297. }
  298. return ret;
  299. }
  300. /*
  301. * notification of system call entry/exit
  302. * - triggered by current->work.syscall_trace
  303. */
  304. asmlinkage void do_syscall_trace(struct pt_regs *regs, int entryexit)
  305. {
  306. #if 0
  307. /* just in case... */
  308. printk(KERN_DEBUG "[%d] syscall_%lu(%lx,%lx,%lx,%lx) = %lx\n",
  309. current->pid,
  310. regs->orig_d0,
  311. regs->a0,
  312. regs->d1,
  313. regs->a3,
  314. regs->a2,
  315. regs->d0);
  316. return;
  317. #endif
  318. if (!test_thread_flag(TIF_SYSCALL_TRACE) &&
  319. !test_thread_flag(TIF_SINGLESTEP))
  320. return;
  321. if (!(current->ptrace & PT_PTRACED))
  322. return;
  323. /* the 0x80 provides a way for the tracing parent to distinguish
  324. between a syscall stop and SIGTRAP delivery */
  325. ptrace_notify(SIGTRAP |
  326. ((current->ptrace & PT_TRACESYSGOOD) &&
  327. !test_thread_flag(TIF_SINGLESTEP) ? 0x80 : 0));
  328. /*
  329. * this isn't the same as continuing with a signal, but it will do
  330. * for normal use. strace only continues with a signal if the
  331. * stopping signal is not SIGTRAP. -brl
  332. */
  333. if (current->exit_code) {
  334. send_sig(current->exit_code, current, 1);
  335. current->exit_code = 0;
  336. }
  337. }