ptrace.c 8.2 KB

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