ptrace.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. child->thread.work.delayed_trace = 0;
  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. child->thread.work.syscall_trace = 0;
  110. }
  111. asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
  112. {
  113. struct task_struct *child;
  114. unsigned long tmp;
  115. int i, ret = 0;
  116. lock_kernel();
  117. if (request == PTRACE_TRACEME) {
  118. /* are we already being traced? */
  119. if (current->ptrace & PT_PTRACED) {
  120. ret = -EPERM;
  121. goto out;
  122. }
  123. /* set the ptrace bit in the process flags. */
  124. current->ptrace |= PT_PTRACED;
  125. goto out;
  126. }
  127. read_lock(&tasklist_lock);
  128. child = find_task_by_pid(pid);
  129. if (child)
  130. get_task_struct(child);
  131. read_unlock(&tasklist_lock);
  132. if (unlikely(!child)) {
  133. ret = -ESRCH;
  134. goto out;
  135. }
  136. /* you may not mess with init */
  137. if (unlikely(pid == 1)) {
  138. ret = -EPERM;
  139. goto out_tsk;
  140. }
  141. if (request == PTRACE_ATTACH) {
  142. ret = ptrace_attach(child);
  143. goto out_tsk;
  144. }
  145. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  146. if (ret)
  147. goto out_tsk;
  148. switch (request) {
  149. /* when I and D space are separate, these will need to be fixed. */
  150. case PTRACE_PEEKTEXT: /* read word at location addr. */
  151. case PTRACE_PEEKDATA:
  152. i = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  153. if (i != sizeof(tmp))
  154. goto out_eio;
  155. ret = put_user(tmp, (unsigned long *)data);
  156. break;
  157. /* read the word at location addr in the USER area. */
  158. case PTRACE_PEEKUSR:
  159. if (addr & 3)
  160. goto out_eio;
  161. addr >>= 2; /* temporary hack. */
  162. if (addr >= 0 && addr < 19) {
  163. tmp = get_reg(child, addr);
  164. if (addr == PT_SR)
  165. tmp >>= 16;
  166. } else if (addr >= 21 && addr < 49) {
  167. tmp = child->thread.fp[addr - 21];
  168. /* Convert internal fpu reg representation
  169. * into long double format
  170. */
  171. if (FPU_IS_EMU && (addr < 45) && !(addr % 3))
  172. tmp = ((tmp & 0xffff0000) << 15) |
  173. ((tmp & 0x0000ffff) << 16);
  174. } else
  175. break;
  176. ret = put_user(tmp, (unsigned long *)data);
  177. break;
  178. /* when I and D space are separate, this will have to be fixed. */
  179. case PTRACE_POKETEXT: /* write the word at location addr. */
  180. case PTRACE_POKEDATA:
  181. if (access_process_vm(child, addr, &data, sizeof(data), 1) != sizeof(data))
  182. goto out_eio;
  183. break;
  184. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  185. if (addr & 3)
  186. goto out_eio;
  187. addr >>= 2; /* temporary hack. */
  188. if (addr == PT_SR) {
  189. data &= SR_MASK;
  190. data <<= 16;
  191. data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
  192. } else if (addr >= 0 && addr < 19) {
  193. if (put_reg(child, addr, data))
  194. goto out_eio;
  195. } else if (addr >= 21 && addr < 48) {
  196. /* Convert long double format
  197. * into internal fpu reg representation
  198. */
  199. if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) {
  200. data = (unsigned long)data << 15;
  201. data = (data & 0xffff0000) |
  202. ((data & 0x0000ffff) >> 1);
  203. }
  204. child->thread.fp[addr - 21] = data;
  205. } else
  206. goto out_eio;
  207. break;
  208. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  209. case PTRACE_CONT: /* restart after signal. */
  210. if (!valid_signal(data))
  211. goto out_eio;
  212. if (request == PTRACE_SYSCALL)
  213. child->thread.work.syscall_trace = ~0;
  214. else
  215. child->thread.work.syscall_trace = 0;
  216. child->exit_code = data;
  217. singlestep_disable(child);
  218. wake_up_process(child);
  219. break;
  220. /*
  221. * make the child exit. Best I can do is send it a sigkill.
  222. * perhaps it should be put in the status that it wants to
  223. * exit.
  224. */
  225. case PTRACE_KILL:
  226. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  227. break;
  228. child->exit_code = SIGKILL;
  229. singlestep_disable(child);
  230. wake_up_process(child);
  231. break;
  232. case PTRACE_SINGLESTEP: /* set the trap flag. */
  233. if (!valid_signal(data))
  234. goto out_eio;
  235. child->thread.work.syscall_trace = 0;
  236. tmp = get_reg(child, PT_SR) | (TRACE_BITS << 16);
  237. put_reg(child, PT_SR, tmp);
  238. child->thread.work.delayed_trace = 1;
  239. child->exit_code = data;
  240. /* give it a chance to run. */
  241. wake_up_process(child);
  242. break;
  243. case PTRACE_DETACH: /* detach a process that was attached. */
  244. ret = ptrace_detach(child, data);
  245. break;
  246. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  247. for (i = 0; i < 19; i++) {
  248. tmp = get_reg(child, i);
  249. if (i == PT_SR)
  250. tmp >>= 16;
  251. ret = put_user(tmp, (unsigned long *)data);
  252. if (ret)
  253. break;
  254. data += sizeof(long);
  255. }
  256. break;
  257. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  258. for (i = 0; i < 19; i++) {
  259. ret = get_user(tmp, (unsigned long *)data);
  260. if (ret)
  261. break;
  262. if (i == PT_SR) {
  263. tmp &= SR_MASK;
  264. tmp <<= 16;
  265. tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
  266. }
  267. put_reg(child, i, tmp);
  268. data += sizeof(long);
  269. }
  270. break;
  271. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  272. if (copy_to_user((void *)data, &child->thread.fp,
  273. sizeof(struct user_m68kfp_struct)))
  274. ret = -EFAULT;
  275. break;
  276. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  277. if (copy_from_user(&child->thread.fp, (void *)data,
  278. sizeof(struct user_m68kfp_struct)))
  279. ret = -EFAULT;
  280. break;
  281. default:
  282. ret = ptrace_request(child, request, addr, data);
  283. break;
  284. }
  285. out_tsk:
  286. put_task_struct(child);
  287. out:
  288. unlock_kernel();
  289. return ret;
  290. out_eio:
  291. ret = -EIO;
  292. goto out_tsk;
  293. }
  294. asmlinkage void syscall_trace(void)
  295. {
  296. if (!current->thread.work.delayed_trace &&
  297. !current->thread.work.syscall_trace)
  298. return;
  299. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  300. ? 0x80 : 0));
  301. /*
  302. * this isn't the same as continuing with a signal, but it will do
  303. * for normal use. strace only continues with a signal if the
  304. * stopping signal is not SIGTRAP. -brl
  305. */
  306. if (current->exit_code) {
  307. send_sig(current->exit_code, current, 1);
  308. current->exit_code = 0;
  309. }
  310. }