ptrace.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (C) 2000-2003, Axis Communications AB.
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/sched.h>
  6. #include <linux/mm.h>
  7. #include <linux/smp.h>
  8. #include <linux/smp_lock.h>
  9. #include <linux/errno.h>
  10. #include <linux/ptrace.h>
  11. #include <linux/user.h>
  12. #include <linux/signal.h>
  13. #include <linux/security.h>
  14. #include <asm/uaccess.h>
  15. #include <asm/page.h>
  16. #include <asm/pgtable.h>
  17. #include <asm/system.h>
  18. #include <asm/processor.h>
  19. /*
  20. * Determines which bits in DCCR the user has access to.
  21. * 1 = access, 0 = no access.
  22. */
  23. #define DCCR_MASK 0x0000001f /* XNZVC */
  24. /*
  25. * Get contents of register REGNO in task TASK.
  26. */
  27. inline long get_reg(struct task_struct *task, unsigned int regno)
  28. {
  29. /* USP is a special case, it's not in the pt_regs struct but
  30. * in the tasks thread struct
  31. */
  32. if (regno == PT_USP)
  33. return task->thread.usp;
  34. else if (regno < PT_MAX)
  35. return ((unsigned long *)task_pt_regs(task))[regno];
  36. else
  37. return 0;
  38. }
  39. /*
  40. * Write contents of register REGNO in task TASK.
  41. */
  42. inline int put_reg(struct task_struct *task, unsigned int regno,
  43. unsigned long data)
  44. {
  45. if (regno == PT_USP)
  46. task->thread.usp = data;
  47. else if (regno < PT_MAX)
  48. ((unsigned long *)task_pt_regs(task))[regno] = data;
  49. else
  50. return -1;
  51. return 0;
  52. }
  53. /*
  54. * Called by kernel/ptrace.c when detaching.
  55. *
  56. * Make sure the single step bit is not set.
  57. */
  58. void
  59. ptrace_disable(struct task_struct *child)
  60. {
  61. /* Todo - pending singlesteps? */
  62. }
  63. /*
  64. * Note that this implementation of ptrace behaves differently from vanilla
  65. * ptrace. Contrary to what the man page says, in the PTRACE_PEEKTEXT,
  66. * PTRACE_PEEKDATA, and PTRACE_PEEKUSER requests the data variable is not
  67. * ignored. Instead, the data variable is expected to point at a location
  68. * (in user space) where the result of the ptrace call is written (instead of
  69. * being returned).
  70. */
  71. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  72. {
  73. int ret;
  74. unsigned long __user *datap = (unsigned long __user *)data;
  75. switch (request) {
  76. /* Read word at location address. */
  77. case PTRACE_PEEKTEXT:
  78. case PTRACE_PEEKDATA: {
  79. unsigned long tmp;
  80. int copied;
  81. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  82. ret = -EIO;
  83. if (copied != sizeof(tmp))
  84. break;
  85. ret = put_user(tmp,datap);
  86. break;
  87. }
  88. /* Read the word at location address in the USER area. */
  89. case PTRACE_PEEKUSR: {
  90. unsigned long tmp;
  91. ret = -EIO;
  92. if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
  93. break;
  94. tmp = get_reg(child, addr >> 2);
  95. ret = put_user(tmp, datap);
  96. break;
  97. }
  98. /* Write the word at location address. */
  99. case PTRACE_POKETEXT:
  100. case PTRACE_POKEDATA:
  101. ret = 0;
  102. if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
  103. break;
  104. ret = -EIO;
  105. break;
  106. /* Write the word at location address in the USER area. */
  107. case PTRACE_POKEUSR:
  108. ret = -EIO;
  109. if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
  110. break;
  111. addr >>= 2;
  112. if (addr == PT_DCCR) {
  113. /* don't allow the tracing process to change stuff like
  114. * interrupt enable, kernel/user bit, dma enables etc.
  115. */
  116. data &= DCCR_MASK;
  117. data |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
  118. }
  119. if (put_reg(child, addr, data))
  120. break;
  121. ret = 0;
  122. break;
  123. case PTRACE_SYSCALL:
  124. case PTRACE_CONT:
  125. ret = -EIO;
  126. if (!valid_signal(data))
  127. break;
  128. if (request == PTRACE_SYSCALL) {
  129. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  130. }
  131. else {
  132. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  133. }
  134. child->exit_code = data;
  135. /* TODO: make sure any pending breakpoint is killed */
  136. wake_up_process(child);
  137. ret = 0;
  138. break;
  139. /* Make the child exit by sending it a sigkill. */
  140. case PTRACE_KILL:
  141. ret = 0;
  142. if (child->exit_state == EXIT_ZOMBIE)
  143. break;
  144. child->exit_code = SIGKILL;
  145. /* TODO: make sure any pending breakpoint is killed */
  146. wake_up_process(child);
  147. break;
  148. /* Set the trap flag. */
  149. case PTRACE_SINGLESTEP:
  150. ret = -EIO;
  151. if (!valid_signal(data))
  152. break;
  153. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  154. /* TODO: set some clever breakpoint mechanism... */
  155. child->exit_code = data;
  156. wake_up_process(child);
  157. ret = 0;
  158. break;
  159. case PTRACE_DETACH:
  160. ret = ptrace_detach(child, data);
  161. break;
  162. /* Get all GP registers from the child. */
  163. case PTRACE_GETREGS: {
  164. int i;
  165. unsigned long tmp;
  166. ret = 0;
  167. for (i = 0; i <= PT_MAX; i++) {
  168. tmp = get_reg(child, i);
  169. if (put_user(tmp, datap)) {
  170. ret = -EFAULT;
  171. break;
  172. }
  173. data += sizeof(long);
  174. }
  175. break;
  176. }
  177. /* Set all GP registers in the child. */
  178. case PTRACE_SETREGS: {
  179. int i;
  180. unsigned long tmp;
  181. ret = 0;
  182. for (i = 0; i <= PT_MAX; i++) {
  183. if (get_user(tmp, datap)) {
  184. ret = -EFAULT;
  185. break;
  186. }
  187. if (i == PT_DCCR) {
  188. tmp &= DCCR_MASK;
  189. tmp |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
  190. }
  191. put_reg(child, i, tmp);
  192. data += sizeof(long);
  193. }
  194. break;
  195. }
  196. default:
  197. ret = ptrace_request(child, request, addr, data);
  198. break;
  199. }
  200. return ret;
  201. }
  202. void do_syscall_trace(void)
  203. {
  204. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  205. return;
  206. if (!(current->ptrace & PT_PTRACED))
  207. return;
  208. /* the 0x80 provides a way for the tracing parent to distinguish
  209. between a syscall stop and SIGTRAP delivery */
  210. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  211. ? 0x80 : 0));
  212. /*
  213. * This isn't the same as continuing with a signal, but it will do for
  214. * normal use.
  215. */
  216. if (current->exit_code) {
  217. send_sig(current->exit_code, current, 1);
  218. current->exit_code = 0;
  219. }
  220. }