ptrace.c 5.4 KB

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