/arch/h8300/kernel/ptrace.c

https://bitbucket.org/evzijst/gittest · C · 277 lines · 214 code · 27 blank · 36 comment · 41 complexity · eb89d63d7d512e69c8e06a9790d12c82 MD5 · raw file

  1. /*
  2. * linux/arch/h8300/kernel/ptrace.c
  3. *
  4. * Yoshinori Sato <ysato@users.sourceforge.jp>
  5. *
  6. * Based on:
  7. * linux/arch/m68k/kernel/ptrace.c
  8. *
  9. * Copyright (C) 1994 by Hamish Macdonald
  10. * Taken from linux/kernel/ptrace.c and modified for M680x0.
  11. * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
  12. *
  13. * This file is subject to the terms and conditions of the GNU General
  14. * Public License. See the file COPYING in the main directory of
  15. * this archive for more details.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/sched.h>
  19. #include <linux/mm.h>
  20. #include <linux/smp.h>
  21. #include <linux/smp_lock.h>
  22. #include <linux/errno.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/user.h>
  25. #include <linux/config.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/page.h>
  28. #include <asm/pgtable.h>
  29. #include <asm/system.h>
  30. #include <asm/processor.h>
  31. #include <asm/signal.h>
  32. /* cpu depend functions */
  33. extern long h8300_get_reg(struct task_struct *task, int regno);
  34. extern int h8300_put_reg(struct task_struct *task, int regno, unsigned long data);
  35. extern void h8300_disable_trace(struct task_struct *child);
  36. extern void h8300_enable_trace(struct task_struct *child);
  37. /*
  38. * does not yet catch signals sent when the child dies.
  39. * in exit.c or in signal.c.
  40. */
  41. inline
  42. static int read_long(struct task_struct * tsk, unsigned long addr,
  43. unsigned long * result)
  44. {
  45. *result = *(unsigned long *)addr;
  46. return 0;
  47. }
  48. void ptrace_disable(struct task_struct *child)
  49. {
  50. h8300_disable_trace(child);
  51. }
  52. asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
  53. {
  54. struct task_struct *child;
  55. int ret;
  56. lock_kernel();
  57. ret = -EPERM;
  58. if (request == PTRACE_TRACEME) {
  59. /* are we already being traced? */
  60. if (current->ptrace & PT_PTRACED)
  61. goto out;
  62. /* set the ptrace bit in the process flags. */
  63. current->ptrace |= PT_PTRACED;
  64. ret = 0;
  65. goto out;
  66. }
  67. ret = -ESRCH;
  68. read_lock(&tasklist_lock);
  69. child = find_task_by_pid(pid);
  70. if (child)
  71. get_task_struct(child);
  72. read_unlock(&tasklist_lock);
  73. if (!child)
  74. goto out;
  75. ret = -EPERM;
  76. if (pid == 1) /* you may not mess with init */
  77. goto out_tsk;
  78. if (request == PTRACE_ATTACH) {
  79. ret = ptrace_attach(child);
  80. goto out_tsk;
  81. }
  82. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  83. if (ret < 0)
  84. goto out_tsk;
  85. switch (request) {
  86. case PTRACE_PEEKTEXT: /* read word at location addr. */
  87. case PTRACE_PEEKDATA: {
  88. unsigned long tmp;
  89. ret = read_long(child, addr, &tmp);
  90. if (ret < 0)
  91. break ;
  92. ret = put_user(tmp, (unsigned long *) data);
  93. break ;
  94. }
  95. /* read the word at location addr in the USER area. */
  96. case PTRACE_PEEKUSR: {
  97. unsigned long tmp = 0;
  98. if ((addr & 3) || addr < 0 || addr >= sizeof(struct user)) {
  99. ret = -EIO;
  100. break ;
  101. }
  102. ret = 0; /* Default return condition */
  103. addr = addr >> 2; /* temporary hack. */
  104. if (addr < H8300_REGS_NO)
  105. tmp = h8300_get_reg(child, addr);
  106. else {
  107. switch(addr) {
  108. case 49:
  109. tmp = child->mm->start_code;
  110. break ;
  111. case 50:
  112. tmp = child->mm->start_data;
  113. break ;
  114. case 51:
  115. tmp = child->mm->end_code;
  116. break ;
  117. case 52:
  118. tmp = child->mm->end_data;
  119. break ;
  120. default:
  121. ret = -EIO;
  122. }
  123. }
  124. if (!ret)
  125. ret = put_user(tmp,(unsigned long *) data);
  126. break ;
  127. }
  128. /* when I and D space are separate, this will have to be fixed. */
  129. case PTRACE_POKETEXT: /* write the word at location addr. */
  130. case PTRACE_POKEDATA:
  131. ret = 0;
  132. if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
  133. break;
  134. ret = -EIO;
  135. break;
  136. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  137. if ((addr & 3) || addr < 0 || addr >= sizeof(struct user)) {
  138. ret = -EIO;
  139. break ;
  140. }
  141. addr = addr >> 2; /* temporary hack. */
  142. if (addr == PT_ORIG_ER0) {
  143. ret = -EIO;
  144. break ;
  145. }
  146. if (addr < H8300_REGS_NO) {
  147. ret = h8300_put_reg(child, addr, data);
  148. break ;
  149. }
  150. ret = -EIO;
  151. break ;
  152. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  153. case PTRACE_CONT: { /* restart after signal. */
  154. ret = -EIO;
  155. if ((unsigned long) data >= _NSIG)
  156. break ;
  157. if (request == PTRACE_SYSCALL)
  158. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  159. else
  160. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  161. child->exit_code = data;
  162. wake_up_process(child);
  163. /* make sure the single step bit is not set. */
  164. h8300_disable_trace(child);
  165. ret = 0;
  166. }
  167. /*
  168. * make the child exit. Best I can do is send it a sigkill.
  169. * perhaps it should be put in the status that it wants to
  170. * exit.
  171. */
  172. case PTRACE_KILL: {
  173. ret = 0;
  174. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  175. break;
  176. child->exit_code = SIGKILL;
  177. h8300_disable_trace(child);
  178. wake_up_process(child);
  179. break;
  180. }
  181. case PTRACE_SINGLESTEP: { /* set the trap flag. */
  182. ret = -EIO;
  183. if ((unsigned long) data > _NSIG)
  184. break;
  185. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  186. child->exit_code = data;
  187. h8300_enable_trace(child);
  188. wake_up_process(child);
  189. ret = 0;
  190. break;
  191. }
  192. case PTRACE_DETACH: /* detach a process that was attached. */
  193. ret = ptrace_detach(child, data);
  194. break;
  195. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  196. int i;
  197. unsigned long tmp;
  198. for (i = 0; i < H8300_REGS_NO; i++) {
  199. tmp = h8300_get_reg(child, i);
  200. if (put_user(tmp, (unsigned long *) data)) {
  201. ret = -EFAULT;
  202. break;
  203. }
  204. data += sizeof(long);
  205. }
  206. ret = 0;
  207. break;
  208. }
  209. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  210. int i;
  211. unsigned long tmp;
  212. for (i = 0; i < H8300_REGS_NO; i++) {
  213. if (get_user(tmp, (unsigned long *) data)) {
  214. ret = -EFAULT;
  215. break;
  216. }
  217. h8300_put_reg(child, i, tmp);
  218. data += sizeof(long);
  219. }
  220. ret = 0;
  221. break;
  222. }
  223. default:
  224. ret = -EIO;
  225. break;
  226. }
  227. out_tsk:
  228. put_task_struct(child);
  229. out:
  230. unlock_kernel();
  231. return ret;
  232. }
  233. asmlinkage void syscall_trace(void)
  234. {
  235. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  236. return;
  237. if (!(current->ptrace & PT_PTRACED))
  238. return;
  239. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  240. ? 0x80 : 0));
  241. /*
  242. * this isn't the same as continuing with a signal, but it will do
  243. * for normal use. strace only continues with a signal if the
  244. * stopping signal is not SIGTRAP. -brl
  245. */
  246. if (current->exit_code) {
  247. send_sig(current->exit_code, current, 1);
  248. current->exit_code = 0;
  249. }
  250. }