ptrace.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * `ptrace' system call
  3. *
  4. * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
  5. * Copyright (C) 2007-2009 PetaLogix
  6. * Copyright (C) 2004-2007 John Williams <john.williams@petalogix.com>
  7. *
  8. * derived from arch/v850/kernel/ptrace.c
  9. *
  10. * Copyright (C) 2002,03 NEC Electronics Corporation
  11. * Copyright (C) 2002,03 Miles Bader <miles@gnu.org>
  12. *
  13. * Derived from arch/mips/kernel/ptrace.c:
  14. *
  15. * Copyright (C) 1992 Ross Biro
  16. * Copyright (C) Linus Torvalds
  17. * Copyright (C) 1994, 95, 96, 97, 98, 2000 Ralf Baechle
  18. * Copyright (C) 1996 David S. Miller
  19. * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
  20. * Copyright (C) 1999 MIPS Technologies, Inc.
  21. *
  22. * This file is subject to the terms and conditions of the GNU General
  23. * Public License. See the file COPYING in the main directory of this
  24. * archive for more details.
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/mm.h>
  28. #include <linux/sched.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/signal.h>
  31. #include <linux/elf.h>
  32. #include <linux/audit.h>
  33. #include <linux/seccomp.h>
  34. #include <linux/tracehook.h>
  35. #include <linux/errno.h>
  36. #include <asm/processor.h>
  37. #include <linux/uaccess.h>
  38. #include <asm/asm-offsets.h>
  39. /* Returns the address where the register at REG_OFFS in P is stashed away. */
  40. static microblaze_reg_t *reg_save_addr(unsigned reg_offs,
  41. struct task_struct *t)
  42. {
  43. struct pt_regs *regs;
  44. /*
  45. * Three basic cases:
  46. *
  47. * (1) A register normally saved before calling the scheduler, is
  48. * available in the kernel entry pt_regs structure at the top
  49. * of the kernel stack. The kernel trap/irq exit path takes
  50. * care to save/restore almost all registers for ptrace'd
  51. * processes.
  52. *
  53. * (2) A call-clobbered register, where the process P entered the
  54. * kernel via [syscall] trap, is not stored anywhere; that's
  55. * OK, because such registers are not expected to be preserved
  56. * when the trap returns anyway (so we don't actually bother to
  57. * test for this case).
  58. *
  59. * (3) A few registers not used at all by the kernel, and so
  60. * normally never saved except by context-switches, are in the
  61. * context switch state.
  62. */
  63. /* Register saved during kernel entry (or not available). */
  64. regs = task_pt_regs(t);
  65. return (microblaze_reg_t *)((char *)regs + reg_offs);
  66. }
  67. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  68. {
  69. int rval;
  70. unsigned long val = 0;
  71. unsigned long copied;
  72. switch (request) {
  73. case PTRACE_PEEKTEXT: /* read word at location addr. */
  74. case PTRACE_PEEKDATA:
  75. pr_debug("PEEKTEXT/PEEKDATA at %08lX\n", addr);
  76. copied = access_process_vm(child, addr, &val, sizeof(val), 0);
  77. rval = -EIO;
  78. if (copied != sizeof(val))
  79. break;
  80. rval = put_user(val, (unsigned long *)data);
  81. break;
  82. case PTRACE_POKETEXT: /* write the word at location addr. */
  83. case PTRACE_POKEDATA:
  84. pr_debug("POKETEXT/POKEDATA to %08lX\n", addr);
  85. rval = 0;
  86. if (access_process_vm(child, addr, &data, sizeof(data), 1)
  87. == sizeof(data))
  88. break;
  89. rval = -EIO;
  90. break;
  91. /* Read/write the word at location ADDR in the registers. */
  92. case PTRACE_PEEKUSR:
  93. case PTRACE_POKEUSR:
  94. pr_debug("PEEKUSR/POKEUSR : 0x%08lx\n", addr);
  95. rval = 0;
  96. if (addr >= PT_SIZE && request == PTRACE_PEEKUSR) {
  97. /*
  98. * Special requests that don't actually correspond
  99. * to offsets in struct pt_regs.
  100. */
  101. if (addr == PT_TEXT_ADDR) {
  102. val = child->mm->start_code;
  103. } else if (addr == PT_DATA_ADDR) {
  104. val = child->mm->start_data;
  105. } else if (addr == PT_TEXT_LEN) {
  106. val = child->mm->end_code
  107. - child->mm->start_code;
  108. } else {
  109. rval = -EIO;
  110. }
  111. } else if (addr >= 0 && addr < PT_SIZE && (addr & 0x3) == 0) {
  112. microblaze_reg_t *reg_addr = reg_save_addr(addr, child);
  113. if (request == PTRACE_PEEKUSR)
  114. val = *reg_addr;
  115. else
  116. *reg_addr = data;
  117. } else
  118. rval = -EIO;
  119. if (rval == 0 && request == PTRACE_PEEKUSR)
  120. rval = put_user(val, (unsigned long *)data);
  121. break;
  122. /* Continue and stop at next (return from) syscall */
  123. case PTRACE_SYSCALL:
  124. pr_debug("PTRACE_SYSCALL\n");
  125. case PTRACE_SINGLESTEP:
  126. pr_debug("PTRACE_SINGLESTEP\n");
  127. /* Restart after a signal. */
  128. case PTRACE_CONT:
  129. pr_debug("PTRACE_CONT\n");
  130. rval = -EIO;
  131. if (!valid_signal(data))
  132. break;
  133. if (request == PTRACE_SYSCALL)
  134. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  135. else
  136. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  137. child->exit_code = data;
  138. pr_debug("wakeup_process\n");
  139. wake_up_process(child);
  140. rval = 0;
  141. break;
  142. /*
  143. * make the child exit. Best I can do is send it a sigkill.
  144. * perhaps it should be put in the status that it wants to
  145. * exit.
  146. */
  147. case PTRACE_KILL:
  148. pr_debug("PTRACE_KILL\n");
  149. rval = 0;
  150. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  151. break;
  152. child->exit_code = SIGKILL;
  153. wake_up_process(child);
  154. break;
  155. case PTRACE_DETACH: /* detach a process that was attached. */
  156. pr_debug("PTRACE_DETACH\n");
  157. rval = ptrace_detach(child, data);
  158. break;
  159. default:
  160. /* rval = ptrace_request(child, request, addr, data); noMMU */
  161. rval = -EIO;
  162. }
  163. return rval;
  164. }
  165. asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
  166. {
  167. long ret = 0;
  168. secure_computing(regs->r12);
  169. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  170. tracehook_report_syscall_entry(regs))
  171. /*
  172. * Tracing decided this syscall should not happen.
  173. * We'll return a bogus call number to get an ENOSYS
  174. * error, but leave the original number in regs->regs[0].
  175. */
  176. ret = -1L;
  177. if (unlikely(current->audit_context))
  178. audit_syscall_entry(EM_XILINX_MICROBLAZE, regs->r12,
  179. regs->r5, regs->r6,
  180. regs->r7, regs->r8);
  181. return ret ?: regs->r12;
  182. }
  183. asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
  184. {
  185. int step;
  186. if (unlikely(current->audit_context))
  187. audit_syscall_exit(AUDITSC_RESULT(regs->r3), regs->r3);
  188. step = test_thread_flag(TIF_SINGLESTEP);
  189. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  190. tracehook_report_syscall_exit(regs, step);
  191. }
  192. #if 0
  193. static asmlinkage void syscall_trace(void)
  194. {
  195. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  196. return;
  197. if (!(current->ptrace & PT_PTRACED))
  198. return;
  199. /* The 0x80 provides a way for the tracing parent to distinguish
  200. between a syscall stop and SIGTRAP delivery */
  201. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  202. ? 0x80 : 0));
  203. /*
  204. * this isn't the same as continuing with a signal, but it will do
  205. * for normal use. strace only continues with a signal if the
  206. * stopping signal is not SIGTRAP. -brl
  207. */
  208. if (current->exit_code) {
  209. send_sig(current->exit_code, current, 1);
  210. current->exit_code = 0;
  211. }
  212. }
  213. #endif
  214. void ptrace_disable(struct task_struct *child)
  215. {
  216. /* nothing to do */
  217. }