ptrace.c 7.5 KB

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