ptrace_no.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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,
  102. unsigned long addr, unsigned long data)
  103. {
  104. int ret;
  105. int regno = addr >> 2;
  106. unsigned long __user *datap = (unsigned long __user *) data;
  107. switch (request) {
  108. /* read the word at location addr in the USER area. */
  109. case PTRACE_PEEKUSR: {
  110. unsigned long tmp;
  111. ret = -EIO;
  112. if ((addr & 3) || addr > sizeof(struct user) - 3)
  113. break;
  114. tmp = 0; /* Default return condition */
  115. ret = -EIO;
  116. if (regno < 19) {
  117. tmp = get_reg(child, regno);
  118. if (regno == PT_SR)
  119. tmp >>= 16;
  120. } else if (regno >= 21 && regno < 49) {
  121. tmp = child->thread.fp[regno - 21];
  122. } else if (regno == 49) {
  123. tmp = child->mm->start_code;
  124. } else if (regno == 50) {
  125. tmp = child->mm->start_data;
  126. } else if (regno == 51) {
  127. tmp = child->mm->end_code;
  128. } else
  129. break;
  130. ret = put_user(tmp, datap);
  131. break;
  132. }
  133. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  134. ret = -EIO;
  135. if ((addr & 3) || addr > sizeof(struct user) - 3)
  136. break;
  137. if (regno == PT_SR) {
  138. data &= SR_MASK;
  139. data <<= 16;
  140. data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
  141. }
  142. if (regno < 19) {
  143. if (put_reg(child, regno, data))
  144. break;
  145. ret = 0;
  146. break;
  147. }
  148. if (regno >= 21 && regno < 48)
  149. {
  150. child->thread.fp[regno - 21] = data;
  151. ret = 0;
  152. }
  153. break;
  154. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  155. int i;
  156. unsigned long tmp;
  157. for (i = 0; i < 19; i++) {
  158. tmp = get_reg(child, i);
  159. if (i == PT_SR)
  160. tmp >>= 16;
  161. if (put_user(tmp, datap)) {
  162. ret = -EFAULT;
  163. break;
  164. }
  165. datap++;
  166. }
  167. ret = 0;
  168. break;
  169. }
  170. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  171. int i;
  172. unsigned long tmp;
  173. for (i = 0; i < 19; i++) {
  174. if (get_user(tmp, datap)) {
  175. ret = -EFAULT;
  176. break;
  177. }
  178. if (i == PT_SR) {
  179. tmp &= SR_MASK;
  180. tmp <<= 16;
  181. tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
  182. }
  183. put_reg(child, i, tmp);
  184. datap++;
  185. }
  186. ret = 0;
  187. break;
  188. }
  189. #ifdef PTRACE_GETFPREGS
  190. case PTRACE_GETFPREGS: { /* Get the child FPU state. */
  191. ret = 0;
  192. if (copy_to_user(datap, &child->thread.fp,
  193. sizeof(struct user_m68kfp_struct)))
  194. ret = -EFAULT;
  195. break;
  196. }
  197. #endif
  198. #ifdef PTRACE_SETFPREGS
  199. case PTRACE_SETFPREGS: { /* Set the child FPU state. */
  200. ret = 0;
  201. if (copy_from_user(&child->thread.fp, datap,
  202. sizeof(struct user_m68kfp_struct)))
  203. ret = -EFAULT;
  204. break;
  205. }
  206. #endif
  207. case PTRACE_GET_THREAD_AREA:
  208. ret = put_user(task_thread_info(child)->tp_value, datap);
  209. break;
  210. default:
  211. ret = ptrace_request(child, request, addr, data);
  212. break;
  213. }
  214. return ret;
  215. }
  216. asmlinkage int syscall_trace_enter(void)
  217. {
  218. int ret = 0;
  219. if (test_thread_flag(TIF_SYSCALL_TRACE))
  220. ret = tracehook_report_syscall_entry(task_pt_regs(current));
  221. return ret;
  222. }
  223. asmlinkage void syscall_trace_leave(void)
  224. {
  225. if (test_thread_flag(TIF_SYSCALL_TRACE))
  226. tracehook_report_syscall_exit(task_pt_regs(current), 0);
  227. }