ptrace.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * linux/arch/m68knommu/kernel/ptrace.c
  3. *
  4. * Copyright (C) 1994 by Hamish Macdonald
  5. * Taken from linux/kernel/ptrace.c and modified for M680x0.
  6. * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
  7. *
  8. * This file is subject to the terms and conditions of the GNU General
  9. * Public License. See the file COPYING in the main directory of
  10. * this archive for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/mm.h>
  15. #include <linux/smp.h>
  16. #include <linux/errno.h>
  17. #include <linux/ptrace.h>
  18. #include <linux/user.h>
  19. #include <linux/signal.h>
  20. #include <linux/tracehook.h>
  21. #include <asm/uaccess.h>
  22. #include <asm/page.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/system.h>
  25. #include <asm/processor.h>
  26. /*
  27. * does not yet catch signals sent when the child dies.
  28. * in exit.c or in signal.c.
  29. */
  30. /* determines which bits in the SR the user has access to. */
  31. /* 1 = access 0 = no access */
  32. #define SR_MASK 0x001f
  33. /* sets the trace bits. */
  34. #define TRACE_BITS 0x8000
  35. /* Find the stack offset for a register, relative to thread.esp0. */
  36. #define PT_REG(reg) ((long)&((struct pt_regs *)0)->reg)
  37. #define SW_REG(reg) ((long)&((struct switch_stack *)0)->reg \
  38. - sizeof(struct switch_stack))
  39. /* Mapping from PT_xxx to the stack offset at which the register is
  40. saved. Notice that usp has no stack-slot and needs to be treated
  41. specially (see get_reg/put_reg below). */
  42. static int regoff[] = {
  43. PT_REG(d1), PT_REG(d2), PT_REG(d3), PT_REG(d4),
  44. PT_REG(d5), SW_REG(d6), SW_REG(d7), PT_REG(a0),
  45. PT_REG(a1), PT_REG(a2), SW_REG(a3), SW_REG(a4),
  46. SW_REG(a5), SW_REG(a6), PT_REG(d0), -1,
  47. PT_REG(orig_d0), PT_REG(sr), PT_REG(pc),
  48. };
  49. /*
  50. * Get contents of register REGNO in task TASK.
  51. */
  52. static inline long get_reg(struct task_struct *task, int regno)
  53. {
  54. unsigned long *addr;
  55. if (regno == PT_USP)
  56. addr = &task->thread.usp;
  57. else if (regno < ARRAY_SIZE(regoff))
  58. addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
  59. else
  60. return 0;
  61. return *addr;
  62. }
  63. /*
  64. * Write contents of register REGNO in task TASK.
  65. */
  66. static inline int put_reg(struct task_struct *task, int regno,
  67. unsigned long data)
  68. {
  69. unsigned long *addr;
  70. if (regno == PT_USP)
  71. addr = &task->thread.usp;
  72. else if (regno < ARRAY_SIZE(regoff))
  73. addr = (unsigned long *) (task->thread.esp0 + regoff[regno]);
  74. else
  75. return -1;
  76. *addr = data;
  77. return 0;
  78. }
  79. void user_enable_single_step(struct task_struct *task)
  80. {
  81. unsigned long srflags;
  82. srflags = get_reg(task, PT_SR) | (TRACE_BITS << 16);
  83. put_reg(task, PT_SR, srflags);
  84. }
  85. void user_disable_single_step(struct task_struct *task)
  86. {
  87. unsigned long srflags;
  88. srflags = get_reg(task, PT_SR) & ~(TRACE_BITS << 16);
  89. put_reg(task, PT_SR, srflags);
  90. }
  91. /*
  92. * Called by kernel/ptrace.c when detaching..
  93. *
  94. * Make sure the single step bit is not set.
  95. */
  96. void ptrace_disable(struct task_struct *child)
  97. {
  98. /* make sure the single step bit is not set. */
  99. user_disable_single_step(child);
  100. }
  101. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  102. {
  103. int ret;
  104. switch (request) {
  105. /* read the word at location addr in the USER area. */
  106. case PTRACE_PEEKUSR: {
  107. unsigned long tmp;
  108. ret = -EIO;
  109. if ((addr & 3) || addr < 0 ||
  110. addr > sizeof(struct user) - 3)
  111. break;
  112. tmp = 0; /* Default return condition */
  113. addr = addr >> 2; /* temporary hack. */
  114. ret = -EIO;
  115. if (addr < 19) {
  116. tmp = get_reg(child, addr);
  117. if (addr == PT_SR)
  118. tmp >>= 16;
  119. } else if (addr >= 21 && addr < 49) {
  120. tmp = child->thread.fp[addr - 21];
  121. } else if (addr == 49) {
  122. tmp = child->mm->start_code;
  123. } else if (addr == 50) {
  124. tmp = child->mm->start_data;
  125. } else if (addr == 51) {
  126. tmp = child->mm->end_code;
  127. } else
  128. break;
  129. ret = put_user(tmp,(unsigned long *) data);
  130. break;
  131. }
  132. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  133. ret = -EIO;
  134. if ((addr & 3) || addr < 0 ||
  135. addr > sizeof(struct user) - 3)
  136. break;
  137. addr = addr >> 2; /* temporary hack. */
  138. if (addr == PT_SR) {
  139. data &= SR_MASK;
  140. data <<= 16;
  141. data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
  142. }
  143. if (addr < 19) {
  144. if (put_reg(child, addr, data))
  145. break;
  146. ret = 0;
  147. break;
  148. }
  149. if (addr >= 21 && addr < 48)
  150. {
  151. child->thread.fp[addr - 21] = data;
  152. ret = 0;
  153. }
  154. break;
  155. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  156. int i;
  157. unsigned long tmp;
  158. for (i = 0; i < 19; i++) {
  159. tmp = get_reg(child, i);
  160. if (i == PT_SR)
  161. tmp >>= 16;
  162. if (put_user(tmp, (unsigned long *) data)) {
  163. ret = -EFAULT;
  164. break;
  165. }
  166. data += sizeof(long);
  167. }
  168. ret = 0;
  169. break;
  170. }
  171. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  172. int i;
  173. unsigned long tmp;
  174. for (i = 0; i < 19; i++) {
  175. if (get_user(tmp, (unsigned long *) data)) {
  176. ret = -EFAULT;
  177. break;
  178. }
  179. if (i == PT_SR) {
  180. tmp &= SR_MASK;
  181. tmp <<= 16;
  182. tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
  183. }
  184. put_reg(child, i, tmp);
  185. data += sizeof(long);
  186. }
  187. ret = 0;
  188. break;
  189. }
  190. #ifdef PTRACE_GETFPREGS
  191. case PTRACE_GETFPREGS: { /* Get the child FPU state. */
  192. ret = 0;
  193. if (copy_to_user((void *)data, &child->thread.fp,
  194. sizeof(struct user_m68kfp_struct)))
  195. ret = -EFAULT;
  196. break;
  197. }
  198. #endif
  199. #ifdef PTRACE_SETFPREGS
  200. case PTRACE_SETFPREGS: { /* Set the child FPU state. */
  201. ret = 0;
  202. if (copy_from_user(&child->thread.fp, (void *)data,
  203. sizeof(struct user_m68kfp_struct)))
  204. ret = -EFAULT;
  205. break;
  206. }
  207. #endif
  208. case PTRACE_GET_THREAD_AREA:
  209. ret = put_user(task_thread_info(child)->tp_value,
  210. (unsigned long __user *)data);
  211. break;
  212. default:
  213. ret = ptrace_request(child, request, addr, data);
  214. break;
  215. }
  216. return ret;
  217. }
  218. asmlinkage int syscall_trace_enter(void)
  219. {
  220. int ret = 0;
  221. if (test_thread_flag(TIF_SYSCALL_TRACE))
  222. ret = tracehook_report_syscall_entry(task_pt_regs(current));
  223. return ret;
  224. }
  225. asmlinkage void syscall_trace_leave(void)
  226. {
  227. if (test_thread_flag(TIF_SYSCALL_TRACE))
  228. tracehook_report_syscall_exit(task_pt_regs(current), 0);
  229. }