ptrace.c 7.5 KB

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