ptrace.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 <asm/uaccess.h>
  21. #include <asm/page.h>
  22. #include <asm/pgtable.h>
  23. #include <asm/system.h>
  24. #include <asm/processor.h>
  25. /*
  26. * does not yet catch signals sent when the child dies.
  27. * in exit.c or in signal.c.
  28. */
  29. /* determines which bits in the SR the user has access to. */
  30. /* 1 = access 0 = no access */
  31. #define SR_MASK 0x001f
  32. /* sets the trace bits. */
  33. #define TRACE_BITS 0x8000
  34. /* Find the stack offset for a register, relative to thread.esp0. */
  35. #define PT_REG(reg) ((long)&((struct pt_regs *)0)->reg)
  36. #define SW_REG(reg) ((long)&((struct switch_stack *)0)->reg \
  37. - sizeof(struct switch_stack))
  38. /* Mapping from PT_xxx to the stack offset at which the register is
  39. saved. Notice that usp has no stack-slot and needs to be treated
  40. specially (see get_reg/put_reg below). */
  41. static int regoff[] = {
  42. PT_REG(d1), PT_REG(d2), PT_REG(d3), PT_REG(d4),
  43. PT_REG(d5), SW_REG(d6), SW_REG(d7), PT_REG(a0),
  44. PT_REG(a1), PT_REG(a2), SW_REG(a3), SW_REG(a4),
  45. SW_REG(a5), SW_REG(a6), PT_REG(d0), -1,
  46. PT_REG(orig_d0), PT_REG(sr), PT_REG(pc),
  47. };
  48. /*
  49. * Get contents of register REGNO in task TASK.
  50. */
  51. static inline long get_reg(struct task_struct *task, int regno)
  52. {
  53. unsigned long *addr;
  54. if (regno == PT_USP)
  55. addr = &task->thread.usp;
  56. else if (regno < ARRAY_SIZE(regoff))
  57. addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
  58. else
  59. return 0;
  60. return *addr;
  61. }
  62. /*
  63. * Write contents of register REGNO in task TASK.
  64. */
  65. static inline int put_reg(struct task_struct *task, int regno,
  66. unsigned long data)
  67. {
  68. unsigned long *addr;
  69. if (regno == PT_USP)
  70. addr = &task->thread.usp;
  71. else if (regno < ARRAY_SIZE(regoff))
  72. addr = (unsigned long *) (task->thread.esp0 + regoff[regno]);
  73. else
  74. return -1;
  75. *addr = data;
  76. return 0;
  77. }
  78. void user_enable_single_step(struct task_struct *task)
  79. {
  80. unsigned long srflags;
  81. srflags = get_reg(task, PT_SR) | (TRACE_BITS << 16);
  82. put_reg(task, PT_SR, srflags);
  83. }
  84. void user_disable_single_step(struct task_struct *task)
  85. {
  86. unsigned long srflags;
  87. srflags = get_reg(task, PT_SR) & ~(TRACE_BITS << 16);
  88. put_reg(task, PT_SR, srflags);
  89. }
  90. /*
  91. * Called by kernel/ptrace.c when detaching..
  92. *
  93. * Make sure the single step bit is not set.
  94. */
  95. void ptrace_disable(struct task_struct *child)
  96. {
  97. /* make sure the single step bit is not set. */
  98. user_disable_single_step(child);
  99. }
  100. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  101. {
  102. int ret;
  103. switch (request) {
  104. /* read the word at location addr in the USER area. */
  105. case PTRACE_PEEKUSR: {
  106. unsigned long tmp;
  107. ret = -EIO;
  108. if ((addr & 3) || addr < 0 ||
  109. addr > sizeof(struct user) - 3)
  110. break;
  111. tmp = 0; /* Default return condition */
  112. addr = addr >> 2; /* temporary hack. */
  113. ret = -EIO;
  114. if (addr < 19) {
  115. tmp = get_reg(child, addr);
  116. if (addr == PT_SR)
  117. tmp >>= 16;
  118. } else if (addr >= 21 && addr < 49) {
  119. tmp = child->thread.fp[addr - 21];
  120. #ifdef CONFIG_M68KFPU_EMU
  121. /* Convert internal fpu reg representation
  122. * into long double format
  123. */
  124. if (FPU_IS_EMU && (addr < 45) && !(addr % 3))
  125. tmp = ((tmp & 0xffff0000) << 15) |
  126. ((tmp & 0x0000ffff) << 16);
  127. #endif
  128. } else if (addr == 49) {
  129. tmp = child->mm->start_code;
  130. } else if (addr == 50) {
  131. tmp = child->mm->start_data;
  132. } else if (addr == 51) {
  133. tmp = child->mm->end_code;
  134. } else
  135. break;
  136. ret = put_user(tmp,(unsigned long *) data);
  137. break;
  138. }
  139. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  140. ret = -EIO;
  141. if ((addr & 3) || addr < 0 ||
  142. addr > sizeof(struct user) - 3)
  143. break;
  144. addr = addr >> 2; /* temporary hack. */
  145. if (addr == PT_SR) {
  146. data &= SR_MASK;
  147. data <<= 16;
  148. data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
  149. }
  150. if (addr < 19) {
  151. if (put_reg(child, addr, data))
  152. break;
  153. ret = 0;
  154. break;
  155. }
  156. if (addr >= 21 && addr < 48)
  157. {
  158. #ifdef CONFIG_M68KFPU_EMU
  159. /* Convert long double format
  160. * into internal fpu reg representation
  161. */
  162. if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) {
  163. data = (unsigned long)data << 15;
  164. data = (data & 0xffff0000) |
  165. ((data & 0x0000ffff) >> 1);
  166. }
  167. #endif
  168. child->thread.fp[addr - 21] = data;
  169. ret = 0;
  170. }
  171. break;
  172. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  173. int i;
  174. unsigned long tmp;
  175. for (i = 0; i < 19; i++) {
  176. tmp = get_reg(child, i);
  177. if (i == PT_SR)
  178. tmp >>= 16;
  179. if (put_user(tmp, (unsigned long *) data)) {
  180. ret = -EFAULT;
  181. break;
  182. }
  183. data += sizeof(long);
  184. }
  185. ret = 0;
  186. break;
  187. }
  188. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  189. int i;
  190. unsigned long tmp;
  191. for (i = 0; i < 19; i++) {
  192. if (get_user(tmp, (unsigned long *) data)) {
  193. ret = -EFAULT;
  194. break;
  195. }
  196. if (i == PT_SR) {
  197. tmp &= SR_MASK;
  198. tmp <<= 16;
  199. tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
  200. }
  201. put_reg(child, i, tmp);
  202. data += sizeof(long);
  203. }
  204. ret = 0;
  205. break;
  206. }
  207. #ifdef PTRACE_GETFPREGS
  208. case PTRACE_GETFPREGS: { /* Get the child FPU state. */
  209. ret = 0;
  210. if (copy_to_user((void *)data, &child->thread.fp,
  211. sizeof(struct user_m68kfp_struct)))
  212. ret = -EFAULT;
  213. break;
  214. }
  215. #endif
  216. #ifdef PTRACE_SETFPREGS
  217. case PTRACE_SETFPREGS: { /* Set the child FPU state. */
  218. ret = 0;
  219. if (copy_from_user(&child->thread.fp, (void *)data,
  220. sizeof(struct user_m68kfp_struct)))
  221. ret = -EFAULT;
  222. break;
  223. }
  224. #endif
  225. case PTRACE_GET_THREAD_AREA:
  226. ret = put_user(task_thread_info(child)->tp_value,
  227. (unsigned long __user *)data);
  228. break;
  229. default:
  230. ret = ptrace_request(child, request, addr, data);
  231. break;
  232. }
  233. return ret;
  234. }
  235. asmlinkage void syscall_trace(void)
  236. {
  237. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  238. return;
  239. if (!(current->ptrace & PT_PTRACED))
  240. return;
  241. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  242. ? 0x80 : 0));
  243. /*
  244. * this isn't the same as continuing with a signal, but it will do
  245. * for normal use. strace only continues with a signal if the
  246. * stopping signal is not SIGTRAP. -brl
  247. */
  248. if (current->exit_code) {
  249. send_sig(current->exit_code, current, 1);
  250. current->exit_code = 0;
  251. }
  252. }