ptrace.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 <linux/tracehook.h>
  21. #include <asm/uaccess.h>
  22. #include <asm/page.h>
  23. #include <asm/pgtable.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 0xC000
  34. #define T1_BIT 0x8000
  35. #define T0_BIT 0x4000
  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 const 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 < ARRAY_SIZE(regoff))
  73. addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
  74. else
  75. return 0;
  76. /* Need to take stkadj into account. */
  77. if (regno == PT_SR || regno == PT_PC) {
  78. long stkadj = *(long *)(task->thread.esp0 + PT_REG(stkadj));
  79. addr = (unsigned long *) ((unsigned long)addr + stkadj);
  80. /* The sr is actually a 16 bit register. */
  81. if (regno == PT_SR)
  82. return *(unsigned short *)addr;
  83. }
  84. return *addr;
  85. }
  86. /*
  87. * Write contents of register REGNO in task TASK.
  88. */
  89. static inline int put_reg(struct task_struct *task, int regno,
  90. unsigned long data)
  91. {
  92. unsigned long *addr;
  93. if (regno == PT_USP)
  94. addr = &task->thread.usp;
  95. else if (regno < ARRAY_SIZE(regoff))
  96. addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
  97. else
  98. return -1;
  99. /* Need to take stkadj into account. */
  100. if (regno == PT_SR || regno == PT_PC) {
  101. long stkadj = *(long *)(task->thread.esp0 + PT_REG(stkadj));
  102. addr = (unsigned long *) ((unsigned long)addr + stkadj);
  103. /* The sr is actually a 16 bit register. */
  104. if (regno == PT_SR) {
  105. *(unsigned short *)addr = data;
  106. return 0;
  107. }
  108. }
  109. *addr = data;
  110. return 0;
  111. }
  112. /*
  113. * Make sure the single step bit is not set.
  114. */
  115. static inline void singlestep_disable(struct task_struct *child)
  116. {
  117. unsigned long tmp = get_reg(child, PT_SR) & ~TRACE_BITS;
  118. put_reg(child, PT_SR, tmp);
  119. clear_tsk_thread_flag(child, TIF_DELAYED_TRACE);
  120. }
  121. /*
  122. * Called by kernel/ptrace.c when detaching..
  123. */
  124. void ptrace_disable(struct task_struct *child)
  125. {
  126. singlestep_disable(child);
  127. }
  128. void user_enable_single_step(struct task_struct *child)
  129. {
  130. unsigned long tmp = get_reg(child, PT_SR) & ~TRACE_BITS;
  131. put_reg(child, PT_SR, tmp | T1_BIT);
  132. set_tsk_thread_flag(child, TIF_DELAYED_TRACE);
  133. }
  134. #ifdef CONFIG_MMU
  135. void user_enable_block_step(struct task_struct *child)
  136. {
  137. unsigned long tmp = get_reg(child, PT_SR) & ~TRACE_BITS;
  138. put_reg(child, PT_SR, tmp | T0_BIT);
  139. }
  140. #endif
  141. void user_disable_single_step(struct task_struct *child)
  142. {
  143. singlestep_disable(child);
  144. }
  145. long arch_ptrace(struct task_struct *child, long request,
  146. unsigned long addr, unsigned long data)
  147. {
  148. unsigned long tmp;
  149. int i, ret = 0;
  150. int regno = addr >> 2; /* temporary hack. */
  151. unsigned long __user *datap = (unsigned long __user *) data;
  152. switch (request) {
  153. /* read the word at location addr in the USER area. */
  154. case PTRACE_PEEKUSR:
  155. if (addr & 3)
  156. goto out_eio;
  157. if (regno >= 0 && regno < 19) {
  158. tmp = get_reg(child, regno);
  159. } else if (regno >= 21 && regno < 49) {
  160. tmp = child->thread.fp[regno - 21];
  161. /* Convert internal fpu reg representation
  162. * into long double format
  163. */
  164. if (FPU_IS_EMU && (regno < 45) && !(regno % 3))
  165. tmp = ((tmp & 0xffff0000) << 15) |
  166. ((tmp & 0x0000ffff) << 16);
  167. #ifndef CONFIG_MMU
  168. } else if (regno == 49) {
  169. tmp = child->mm->start_code;
  170. } else if (regno == 50) {
  171. tmp = child->mm->start_data;
  172. } else if (regno == 51) {
  173. tmp = child->mm->end_code;
  174. #endif
  175. } else
  176. goto out_eio;
  177. ret = put_user(tmp, datap);
  178. break;
  179. case PTRACE_POKEUSR:
  180. /* write the word at location addr in the USER area */
  181. if (addr & 3)
  182. goto out_eio;
  183. if (regno == PT_SR) {
  184. data &= SR_MASK;
  185. data |= get_reg(child, PT_SR) & ~SR_MASK;
  186. }
  187. if (regno >= 0 && regno < 19) {
  188. if (put_reg(child, regno, data))
  189. goto out_eio;
  190. } else if (regno >= 21 && regno < 48) {
  191. /* Convert long double format
  192. * into internal fpu reg representation
  193. */
  194. if (FPU_IS_EMU && (regno < 45) && !(regno % 3)) {
  195. data <<= 15;
  196. data = (data & 0xffff0000) |
  197. ((data & 0x0000ffff) >> 1);
  198. }
  199. child->thread.fp[regno - 21] = data;
  200. } else
  201. goto out_eio;
  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. ret = put_user(tmp, datap);
  207. if (ret)
  208. break;
  209. datap++;
  210. }
  211. break;
  212. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  213. for (i = 0; i < 19; i++) {
  214. ret = get_user(tmp, datap);
  215. if (ret)
  216. break;
  217. if (i == PT_SR) {
  218. tmp &= SR_MASK;
  219. tmp |= get_reg(child, PT_SR) & ~SR_MASK;
  220. }
  221. put_reg(child, i, tmp);
  222. datap++;
  223. }
  224. break;
  225. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  226. if (copy_to_user(datap, &child->thread.fp,
  227. sizeof(struct user_m68kfp_struct)))
  228. ret = -EFAULT;
  229. break;
  230. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  231. if (copy_from_user(&child->thread.fp, datap,
  232. sizeof(struct user_m68kfp_struct)))
  233. ret = -EFAULT;
  234. break;
  235. case PTRACE_GET_THREAD_AREA:
  236. ret = put_user(task_thread_info(child)->tp_value, datap);
  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. }
  260. #if defined(CONFIG_COLDFIRE) || !defined(CONFIG_MMU)
  261. asmlinkage int syscall_trace_enter(void)
  262. {
  263. int ret = 0;
  264. if (test_thread_flag(TIF_SYSCALL_TRACE))
  265. ret = tracehook_report_syscall_entry(task_pt_regs(current));
  266. return ret;
  267. }
  268. asmlinkage void syscall_trace_leave(void)
  269. {
  270. if (test_thread_flag(TIF_SYSCALL_TRACE))
  271. tracehook_report_syscall_exit(task_pt_regs(current), 0);
  272. }
  273. #endif /* CONFIG_COLDFIRE */