ptrace.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * arch/v850/kernel/ptrace.c -- `ptrace' system call
  3. *
  4. * Copyright (C) 2002,03,04 NEC Electronics Corporation
  5. * Copyright (C) 2002,03,04 Miles Bader <miles@gnu.org>
  6. *
  7. * Derived from arch/mips/kernel/ptrace.c:
  8. *
  9. * Copyright (C) 1992 Ross Biro
  10. * Copyright (C) Linus Torvalds
  11. * Copyright (C) 1994, 95, 96, 97, 98, 2000 Ralf Baechle
  12. * Copyright (C) 1996 David S. Miller
  13. * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
  14. * Copyright (C) 1999 MIPS Technologies, Inc.
  15. *
  16. * This file is subject to the terms and conditions of the GNU General
  17. * Public License. See the file COPYING in the main directory of this
  18. * archive for more details.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/mm.h>
  22. #include <linux/sched.h>
  23. #include <linux/smp_lock.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/signal.h>
  26. #include <asm/errno.h>
  27. #include <asm/ptrace.h>
  28. #include <asm/processor.h>
  29. #include <asm/uaccess.h>
  30. /* Returns the address where the register at REG_OFFS in P is stashed away. */
  31. static v850_reg_t *reg_save_addr (unsigned reg_offs, struct task_struct *t)
  32. {
  33. struct pt_regs *regs;
  34. /* Three basic cases:
  35. (1) A register normally saved before calling the scheduler, is
  36. available in the kernel entry pt_regs structure at the top
  37. of the kernel stack. The kernel trap/irq exit path takes
  38. care to save/restore almost all registers for ptrace'd
  39. processes.
  40. (2) A call-clobbered register, where the process P entered the
  41. kernel via [syscall] trap, is not stored anywhere; that's
  42. OK, because such registers are not expected to be preserved
  43. when the trap returns anyway (so we don't actually bother to
  44. test for this case).
  45. (3) A few registers not used at all by the kernel, and so
  46. normally never saved except by context-switches, are in the
  47. context switch state. */
  48. if (reg_offs == PT_CTPC || reg_offs == PT_CTPSW || reg_offs == PT_CTBP)
  49. /* Register saved during context switch. */
  50. regs = thread_saved_regs (t);
  51. else
  52. /* Register saved during kernel entry (or not available). */
  53. regs = task_regs (t);
  54. return (v850_reg_t *)((char *)regs + reg_offs);
  55. }
  56. /* Set the bits SET and clear the bits CLEAR in the v850e DIR
  57. (`debug information register'). Returns the new value of DIR. */
  58. static inline v850_reg_t set_dir (v850_reg_t set, v850_reg_t clear)
  59. {
  60. register v850_reg_t rval asm ("r10");
  61. register v850_reg_t arg0 asm ("r6") = set;
  62. register v850_reg_t arg1 asm ("r7") = clear;
  63. /* The dbtrap handler has exactly this functionality when called
  64. from kernel mode. 0xf840 is a `dbtrap' insn. */
  65. asm (".short 0xf840" : "=r" (rval) : "r" (arg0), "r" (arg1));
  66. return rval;
  67. }
  68. /* Makes sure hardware single-stepping is (globally) enabled.
  69. Returns true if successful. */
  70. static inline int enable_single_stepping (void)
  71. {
  72. static int enabled = 0; /* Remember whether we already did it. */
  73. if (! enabled) {
  74. /* Turn on the SE (`single-step enable') bit, 0x100, in the
  75. DIR (`debug information register'). This may fail if a
  76. processor doesn't support it or something. We also try
  77. to clear bit 0x40 (`INI'), which is necessary to use the
  78. debug stuff on the v850e2; on the v850e, clearing 0x40
  79. shouldn't cause any problem. */
  80. v850_reg_t dir = set_dir (0x100, 0x40);
  81. /* Make sure it really got set. */
  82. if (dir & 0x100)
  83. enabled = 1;
  84. }
  85. return enabled;
  86. }
  87. /* Try to set CHILD's single-step flag to VAL. Returns true if successful. */
  88. static int set_single_step (struct task_struct *t, int val)
  89. {
  90. v850_reg_t *psw_addr = reg_save_addr(PT_PSW, t);
  91. if (val) {
  92. /* Make sure single-stepping is enabled. */
  93. if (! enable_single_stepping ())
  94. return 0;
  95. /* Set T's single-step flag. */
  96. *psw_addr |= 0x800;
  97. } else
  98. *psw_addr &= ~0x800;
  99. return 1;
  100. }
  101. int sys_ptrace(long request, long pid, long addr, long data)
  102. {
  103. struct task_struct *child;
  104. int rval;
  105. lock_kernel();
  106. if (request == PTRACE_TRACEME) {
  107. /* are we already being traced? */
  108. if (current->ptrace & PT_PTRACED) {
  109. rval = -EPERM;
  110. goto out;
  111. }
  112. /* set the ptrace bit in the process flags. */
  113. current->ptrace |= PT_PTRACED;
  114. rval = 0;
  115. goto out;
  116. }
  117. rval = -ESRCH;
  118. read_lock(&tasklist_lock);
  119. child = find_task_by_pid(pid);
  120. if (child)
  121. get_task_struct(child);
  122. read_unlock(&tasklist_lock);
  123. if (!child)
  124. goto out;
  125. rval = -EPERM;
  126. if (pid == 1) /* you may not mess with init */
  127. goto out_tsk;
  128. if (request == PTRACE_ATTACH) {
  129. rval = ptrace_attach(child);
  130. goto out_tsk;
  131. }
  132. rval = ptrace_check_attach(child, request == PTRACE_KILL);
  133. if (rval < 0)
  134. goto out_tsk;
  135. switch (request) {
  136. unsigned long val, copied;
  137. case PTRACE_PEEKTEXT: /* read word at location addr. */
  138. case PTRACE_PEEKDATA:
  139. copied = access_process_vm(child, addr, &val, sizeof(val), 0);
  140. rval = -EIO;
  141. if (copied != sizeof(val))
  142. break;
  143. rval = put_user(val, (unsigned long *)data);
  144. goto out;
  145. case PTRACE_POKETEXT: /* write the word at location addr. */
  146. case PTRACE_POKEDATA:
  147. rval = 0;
  148. if (access_process_vm(child, addr, &data, sizeof(data), 1)
  149. == sizeof(data))
  150. break;
  151. rval = -EIO;
  152. goto out;
  153. /* Read/write the word at location ADDR in the registers. */
  154. case PTRACE_PEEKUSR:
  155. case PTRACE_POKEUSR:
  156. rval = 0;
  157. if (addr >= PT_SIZE && request == PTRACE_PEEKUSR) {
  158. /* Special requests that don't actually correspond
  159. to offsets in struct pt_regs. */
  160. if (addr == PT_TEXT_ADDR)
  161. val = child->mm->start_code;
  162. else if (addr == PT_DATA_ADDR)
  163. val = child->mm->start_data;
  164. else if (addr == PT_TEXT_LEN)
  165. val = child->mm->end_code
  166. - child->mm->start_code;
  167. else
  168. rval = -EIO;
  169. } else if (addr >= 0 && addr < PT_SIZE && (addr & 0x3) == 0) {
  170. v850_reg_t *reg_addr = reg_save_addr(addr, child);
  171. if (request == PTRACE_PEEKUSR)
  172. val = *reg_addr;
  173. else
  174. *reg_addr = data;
  175. } else
  176. rval = -EIO;
  177. if (rval == 0 && request == PTRACE_PEEKUSR)
  178. rval = put_user (val, (unsigned long *)data);
  179. goto out;
  180. /* Continue and stop at next (return from) syscall */
  181. case PTRACE_SYSCALL:
  182. /* Restart after a signal. */
  183. case PTRACE_CONT:
  184. /* Execute a single instruction. */
  185. case PTRACE_SINGLESTEP:
  186. rval = -EIO;
  187. if (!valid_signal(data))
  188. break;
  189. /* Turn CHILD's single-step flag on or off. */
  190. if (! set_single_step (child, request == PTRACE_SINGLESTEP))
  191. break;
  192. if (request == PTRACE_SYSCALL)
  193. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  194. else
  195. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  196. child->exit_code = data;
  197. wake_up_process(child);
  198. rval = 0;
  199. break;
  200. /*
  201. * make the child exit. Best I can do is send it a sigkill.
  202. * perhaps it should be put in the status that it wants to
  203. * exit.
  204. */
  205. case PTRACE_KILL:
  206. rval = 0;
  207. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  208. break;
  209. child->exit_code = SIGKILL;
  210. wake_up_process(child);
  211. break;
  212. case PTRACE_DETACH: /* detach a process that was attached. */
  213. set_single_step (child, 0); /* Clear single-step flag */
  214. rval = ptrace_detach(child, data);
  215. break;
  216. default:
  217. rval = -EIO;
  218. goto out;
  219. }
  220. out_tsk:
  221. put_task_struct(child);
  222. out:
  223. unlock_kernel();
  224. return rval;
  225. }
  226. asmlinkage void syscall_trace(void)
  227. {
  228. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  229. return;
  230. if (!(current->ptrace & PT_PTRACED))
  231. return;
  232. /* The 0x80 provides a way for the tracing parent to distinguish
  233. between a syscall stop and SIGTRAP delivery */
  234. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  235. ? 0x80 : 0));
  236. /*
  237. * this isn't the same as continuing with a signal, but it will do
  238. * for normal use. strace only continues with a signal if the
  239. * stopping signal is not SIGTRAP. -brl
  240. */
  241. if (current->exit_code) {
  242. send_sig(current->exit_code, current, 1);
  243. current->exit_code = 0;
  244. }
  245. }
  246. void ptrace_disable (struct task_struct *child)
  247. {
  248. /* nothing to do */
  249. }