ptrace.c 7.5 KB

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