ptrace.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  108. }
  109. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  110. {
  111. unsigned long tmp;
  112. int i, ret = 0;
  113. switch (request) {
  114. /* when I and D space are separate, these will need to be fixed. */
  115. case PTRACE_PEEKTEXT: /* read word at location addr. */
  116. case PTRACE_PEEKDATA:
  117. ret = generic_ptrace_peekdata(child, addr, data);
  118. break;
  119. /* read the word at location addr in the USER area. */
  120. case PTRACE_PEEKUSR:
  121. if (addr & 3)
  122. goto out_eio;
  123. addr >>= 2; /* temporary hack. */
  124. if (addr >= 0 && addr < 19) {
  125. tmp = get_reg(child, addr);
  126. if (addr == PT_SR)
  127. tmp >>= 16;
  128. } else if (addr >= 21 && addr < 49) {
  129. tmp = child->thread.fp[addr - 21];
  130. /* Convert internal fpu reg representation
  131. * into long double format
  132. */
  133. if (FPU_IS_EMU && (addr < 45) && !(addr % 3))
  134. tmp = ((tmp & 0xffff0000) << 15) |
  135. ((tmp & 0x0000ffff) << 16);
  136. } else
  137. break;
  138. ret = put_user(tmp, (unsigned long *)data);
  139. break;
  140. /* when I and D space are separate, this will have to be fixed. */
  141. case PTRACE_POKETEXT: /* write the word at location addr. */
  142. case PTRACE_POKEDATA:
  143. ret = generic_ptrace_pokedata(child, addr, data);
  144. break;
  145. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  146. if (addr & 3)
  147. goto out_eio;
  148. addr >>= 2; /* temporary hack. */
  149. if (addr == PT_SR) {
  150. data &= SR_MASK;
  151. data <<= 16;
  152. data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
  153. } else if (addr >= 0 && addr < 19) {
  154. if (put_reg(child, addr, data))
  155. goto out_eio;
  156. } else if (addr >= 21 && addr < 48) {
  157. /* Convert long double format
  158. * into internal fpu reg representation
  159. */
  160. if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) {
  161. data = (unsigned long)data << 15;
  162. data = (data & 0xffff0000) |
  163. ((data & 0x0000ffff) >> 1);
  164. }
  165. child->thread.fp[addr - 21] = data;
  166. } else
  167. goto out_eio;
  168. break;
  169. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  170. case PTRACE_CONT: /* restart after signal. */
  171. if (!valid_signal(data))
  172. goto out_eio;
  173. if (request == PTRACE_SYSCALL)
  174. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  175. else
  176. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  177. child->exit_code = data;
  178. singlestep_disable(child);
  179. wake_up_process(child);
  180. break;
  181. /*
  182. * make the child exit. Best I can do is send it a sigkill.
  183. * perhaps it should be put in the status that it wants to
  184. * exit.
  185. */
  186. case PTRACE_KILL:
  187. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  188. break;
  189. child->exit_code = SIGKILL;
  190. singlestep_disable(child);
  191. wake_up_process(child);
  192. break;
  193. case PTRACE_SINGLESTEP: /* set the trap flag. */
  194. if (!valid_signal(data))
  195. goto out_eio;
  196. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  197. tmp = get_reg(child, PT_SR) | (TRACE_BITS << 16);
  198. put_reg(child, PT_SR, tmp);
  199. set_tsk_thread_flag(child, TIF_DELAYED_TRACE);
  200. child->exit_code = data;
  201. /* give it a chance to run. */
  202. wake_up_process(child);
  203. break;
  204. case PTRACE_DETACH: /* detach a process that was attached. */
  205. ret = ptrace_detach(child, data);
  206. break;
  207. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  208. for (i = 0; i < 19; i++) {
  209. tmp = get_reg(child, i);
  210. if (i == PT_SR)
  211. tmp >>= 16;
  212. ret = put_user(tmp, (unsigned long *)data);
  213. if (ret)
  214. break;
  215. data += sizeof(long);
  216. }
  217. break;
  218. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  219. for (i = 0; i < 19; i++) {
  220. ret = get_user(tmp, (unsigned long *)data);
  221. if (ret)
  222. break;
  223. if (i == PT_SR) {
  224. tmp &= SR_MASK;
  225. tmp <<= 16;
  226. tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
  227. }
  228. put_reg(child, i, tmp);
  229. data += sizeof(long);
  230. }
  231. break;
  232. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  233. if (copy_to_user((void *)data, &child->thread.fp,
  234. sizeof(struct user_m68kfp_struct)))
  235. ret = -EFAULT;
  236. break;
  237. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  238. if (copy_from_user(&child->thread.fp, (void *)data,
  239. sizeof(struct user_m68kfp_struct)))
  240. ret = -EFAULT;
  241. break;
  242. default:
  243. ret = ptrace_request(child, request, addr, data);
  244. break;
  245. }
  246. return ret;
  247. out_eio:
  248. return -EIO;
  249. }
  250. asmlinkage void syscall_trace(void)
  251. {
  252. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  253. ? 0x80 : 0));
  254. /*
  255. * this isn't the same as continuing with a signal, but it will do
  256. * for normal use. strace only continues with a signal if the
  257. * stopping signal is not SIGTRAP. -brl
  258. */
  259. if (current->exit_code) {
  260. send_sig(current->exit_code, current, 1);
  261. current->exit_code = 0;
  262. }
  263. }