ptrace.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * linux/arch/m68k/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. [0] = PT_REG(d1),
  43. [1] = PT_REG(d2),
  44. [2] = PT_REG(d3),
  45. [3] = PT_REG(d4),
  46. [4] = PT_REG(d5),
  47. [5] = SW_REG(d6),
  48. [6] = SW_REG(d7),
  49. [7] = PT_REG(a0),
  50. [8] = PT_REG(a1),
  51. [9] = PT_REG(a2),
  52. [10] = SW_REG(a3),
  53. [11] = SW_REG(a4),
  54. [12] = SW_REG(a5),
  55. [13] = SW_REG(a6),
  56. [14] = PT_REG(d0),
  57. [15] = -1,
  58. [16] = PT_REG(orig_d0),
  59. [17] = PT_REG(sr),
  60. [18] = PT_REG(pc),
  61. };
  62. /*
  63. * Get contents of register REGNO in task TASK.
  64. */
  65. static inline long get_reg(struct task_struct *task, int regno)
  66. {
  67. unsigned long *addr;
  68. if (regno == PT_USP)
  69. addr = &task->thread.usp;
  70. else if (regno < ARRAY_SIZE(regoff))
  71. addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
  72. else
  73. return 0;
  74. return *addr;
  75. }
  76. /*
  77. * Write contents of register REGNO in task TASK.
  78. */
  79. static inline int put_reg(struct task_struct *task, int regno,
  80. unsigned long data)
  81. {
  82. unsigned long *addr;
  83. if (regno == PT_USP)
  84. addr = &task->thread.usp;
  85. else if (regno < ARRAY_SIZE(regoff))
  86. addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
  87. else
  88. return -1;
  89. *addr = data;
  90. return 0;
  91. }
  92. /*
  93. * Make sure the single step bit is not set.
  94. */
  95. static inline void singlestep_disable(struct task_struct *child)
  96. {
  97. unsigned long tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
  98. put_reg(child, PT_SR, tmp);
  99. clear_tsk_thread_flag(child, TIF_DELAYED_TRACE);
  100. }
  101. /*
  102. * Called by kernel/ptrace.c when detaching..
  103. */
  104. void ptrace_disable(struct task_struct *child)
  105. {
  106. singlestep_disable(child);
  107. }
  108. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  109. {
  110. unsigned long tmp;
  111. int i, ret = 0;
  112. switch (request) {
  113. /* when I and D space are separate, these will need to be fixed. */
  114. case PTRACE_PEEKTEXT: /* read word at location addr. */
  115. case PTRACE_PEEKDATA:
  116. ret = generic_ptrace_peekdata(child, addr, data);
  117. break;
  118. /* read the word at location addr in the USER area. */
  119. case PTRACE_PEEKUSR:
  120. if (addr & 3)
  121. goto out_eio;
  122. addr >>= 2; /* temporary hack. */
  123. if (addr >= 0 && addr < 19) {
  124. tmp = get_reg(child, addr);
  125. if (addr == PT_SR)
  126. tmp >>= 16;
  127. } else if (addr >= 21 && addr < 49) {
  128. tmp = child->thread.fp[addr - 21];
  129. /* Convert internal fpu reg representation
  130. * into long double format
  131. */
  132. if (FPU_IS_EMU && (addr < 45) && !(addr % 3))
  133. tmp = ((tmp & 0xffff0000) << 15) |
  134. ((tmp & 0x0000ffff) << 16);
  135. } else
  136. break;
  137. ret = put_user(tmp, (unsigned long *)data);
  138. break;
  139. /* when I and D space are separate, this will have to be fixed. */
  140. case PTRACE_POKETEXT: /* write the word at location addr. */
  141. case PTRACE_POKEDATA:
  142. ret = generic_ptrace_pokedata(child, addr, data);
  143. break;
  144. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  145. if (addr & 3)
  146. goto out_eio;
  147. addr >>= 2; /* temporary hack. */
  148. if (addr == PT_SR) {
  149. data &= SR_MASK;
  150. data <<= 16;
  151. data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
  152. } else if (addr >= 0 && addr < 19) {
  153. if (put_reg(child, addr, data))
  154. goto out_eio;
  155. } else if (addr >= 21 && addr < 48) {
  156. /* Convert long double format
  157. * into internal fpu reg representation
  158. */
  159. if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) {
  160. data = (unsigned long)data << 15;
  161. data = (data & 0xffff0000) |
  162. ((data & 0x0000ffff) >> 1);
  163. }
  164. child->thread.fp[addr - 21] = data;
  165. } else
  166. goto out_eio;
  167. break;
  168. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  169. case PTRACE_CONT: /* restart after signal. */
  170. if (!valid_signal(data))
  171. goto out_eio;
  172. if (request == PTRACE_SYSCALL)
  173. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  174. else
  175. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  176. child->exit_code = data;
  177. singlestep_disable(child);
  178. wake_up_process(child);
  179. break;
  180. /*
  181. * make the child exit. Best I can do is send it a sigkill.
  182. * perhaps it should be put in the status that it wants to
  183. * exit.
  184. */
  185. case PTRACE_KILL:
  186. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  187. break;
  188. child->exit_code = SIGKILL;
  189. singlestep_disable(child);
  190. wake_up_process(child);
  191. break;
  192. case PTRACE_SINGLESTEP: /* set the trap flag. */
  193. if (!valid_signal(data))
  194. goto out_eio;
  195. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  196. tmp = get_reg(child, PT_SR) | (TRACE_BITS << 16);
  197. put_reg(child, PT_SR, tmp);
  198. set_tsk_thread_flag(child, TIF_DELAYED_TRACE);
  199. child->exit_code = data;
  200. /* give it a chance to run. */
  201. wake_up_process(child);
  202. break;
  203. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  204. for (i = 0; i < 19; i++) {
  205. tmp = get_reg(child, i);
  206. if (i == PT_SR)
  207. tmp >>= 16;
  208. ret = put_user(tmp, (unsigned long *)data);
  209. if (ret)
  210. break;
  211. data += sizeof(long);
  212. }
  213. break;
  214. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  215. for (i = 0; i < 19; i++) {
  216. ret = get_user(tmp, (unsigned long *)data);
  217. if (ret)
  218. break;
  219. if (i == PT_SR) {
  220. tmp &= SR_MASK;
  221. tmp <<= 16;
  222. tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
  223. }
  224. put_reg(child, i, tmp);
  225. data += sizeof(long);
  226. }
  227. break;
  228. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  229. if (copy_to_user((void *)data, &child->thread.fp,
  230. sizeof(struct user_m68kfp_struct)))
  231. ret = -EFAULT;
  232. break;
  233. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  234. if (copy_from_user(&child->thread.fp, (void *)data,
  235. sizeof(struct user_m68kfp_struct)))
  236. ret = -EFAULT;
  237. break;
  238. default:
  239. ret = ptrace_request(child, request, addr, data);
  240. break;
  241. }
  242. return ret;
  243. out_eio:
  244. return -EIO;
  245. }
  246. asmlinkage void syscall_trace(void)
  247. {
  248. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  249. ? 0x80 : 0));
  250. /*
  251. * this isn't the same as continuing with a signal, but it will do
  252. * for normal use. strace only continues with a signal if the
  253. * stopping signal is not SIGTRAP. -brl
  254. */
  255. if (current->exit_code) {
  256. send_sig(current->exit_code, current, 1);
  257. current->exit_code = 0;
  258. }
  259. }