ptrace.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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/processor.h>
  17. /*
  18. * Determines which bits in DCCR the user has access to.
  19. * 1 = access, 0 = no access.
  20. */
  21. #define DCCR_MASK 0x0000001f /* XNZVC */
  22. /*
  23. * Get contents of register REGNO in task TASK.
  24. */
  25. inline long get_reg(struct task_struct *task, unsigned int regno)
  26. {
  27. /* USP is a special case, it's not in the pt_regs struct but
  28. * in the tasks thread struct
  29. */
  30. if (regno == PT_USP)
  31. return task->thread.usp;
  32. else if (regno < PT_MAX)
  33. return ((unsigned long *)task_pt_regs(task))[regno];
  34. else
  35. return 0;
  36. }
  37. /*
  38. * Write contents of register REGNO in task TASK.
  39. */
  40. inline int put_reg(struct task_struct *task, unsigned int regno,
  41. unsigned long data)
  42. {
  43. if (regno == PT_USP)
  44. task->thread.usp = data;
  45. else if (regno < PT_MAX)
  46. ((unsigned long *)task_pt_regs(task))[regno] = data;
  47. else
  48. return -1;
  49. return 0;
  50. }
  51. /*
  52. * Called by kernel/ptrace.c when detaching.
  53. *
  54. * Make sure the single step bit is not set.
  55. */
  56. void
  57. ptrace_disable(struct task_struct *child)
  58. {
  59. /* Todo - pending singlesteps? */
  60. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  61. }
  62. /*
  63. * Note that this implementation of ptrace behaves differently from vanilla
  64. * ptrace. Contrary to what the man page says, in the PTRACE_PEEKTEXT,
  65. * PTRACE_PEEKDATA, and PTRACE_PEEKUSER requests the data variable is not
  66. * ignored. Instead, the data variable is expected to point at a location
  67. * (in user space) where the result of the ptrace call is written (instead of
  68. * being returned).
  69. */
  70. long arch_ptrace(struct task_struct *child, long request,
  71. unsigned long addr, unsigned long data)
  72. {
  73. int ret;
  74. unsigned int regno = addr >> 2;
  75. unsigned long __user *datap = (unsigned long __user *)data;
  76. switch (request) {
  77. /* Read word at location address. */
  78. case PTRACE_PEEKTEXT:
  79. case PTRACE_PEEKDATA:
  80. ret = generic_ptrace_peekdata(child, addr, data);
  81. break;
  82. /* Read the word at location address in the USER area. */
  83. case PTRACE_PEEKUSR: {
  84. unsigned long tmp;
  85. ret = -EIO;
  86. if ((addr & 3) || regno > PT_MAX)
  87. break;
  88. tmp = get_reg(child, regno);
  89. ret = put_user(tmp, datap);
  90. break;
  91. }
  92. /* Write the word at location address. */
  93. case PTRACE_POKETEXT:
  94. case PTRACE_POKEDATA:
  95. ret = generic_ptrace_pokedata(child, addr, data);
  96. break;
  97. /* Write the word at location address in the USER area. */
  98. case PTRACE_POKEUSR:
  99. ret = -EIO;
  100. if ((addr & 3) || regno > PT_MAX)
  101. break;
  102. if (regno == 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, regno, data))
  110. break;
  111. ret = 0;
  112. break;
  113. /* Get all GP registers from the child. */
  114. case PTRACE_GETREGS: {
  115. int i;
  116. unsigned long tmp;
  117. ret = 0;
  118. for (i = 0; i <= PT_MAX; i++) {
  119. tmp = get_reg(child, i);
  120. if (put_user(tmp, datap)) {
  121. ret = -EFAULT;
  122. break;
  123. }
  124. datap++;
  125. }
  126. break;
  127. }
  128. /* Set all GP registers in the child. */
  129. case PTRACE_SETREGS: {
  130. int i;
  131. unsigned long tmp;
  132. ret = 0;
  133. for (i = 0; i <= PT_MAX; i++) {
  134. if (get_user(tmp, datap)) {
  135. ret = -EFAULT;
  136. break;
  137. }
  138. if (i == PT_DCCR) {
  139. tmp &= DCCR_MASK;
  140. tmp |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
  141. }
  142. put_reg(child, i, tmp);
  143. datap++;
  144. }
  145. break;
  146. }
  147. default:
  148. ret = ptrace_request(child, request, addr, data);
  149. break;
  150. }
  151. return ret;
  152. }
  153. void do_syscall_trace(void)
  154. {
  155. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  156. return;
  157. if (!(current->ptrace & PT_PTRACED))
  158. return;
  159. /* the 0x80 provides a way for the tracing parent to distinguish
  160. between a syscall stop and SIGTRAP delivery */
  161. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  162. ? 0x80 : 0));
  163. /*
  164. * This isn't the same as continuing with a signal, but it will do for
  165. * normal use.
  166. */
  167. if (current->exit_code) {
  168. send_sig(current->exit_code, current, 1);
  169. current->exit_code = 0;
  170. }
  171. }