ptrace.c 8.4 KB

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