ptrace.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/smp_lock.h>
  30. #include <linux/ptrace.h>
  31. #include <linux/signal.h>
  32. #include <linux/errno.h>
  33. #include <linux/ptrace.h>
  34. #include <asm/processor.h>
  35. #include <linux/uaccess.h>
  36. #include <asm/asm-offsets.h>
  37. /* Returns the address where the register at REG_OFFS in P is stashed away. */
  38. static microblaze_reg_t *reg_save_addr(unsigned reg_offs,
  39. struct task_struct *t)
  40. {
  41. struct pt_regs *regs;
  42. /*
  43. * Three basic cases:
  44. *
  45. * (1) A register normally saved before calling the scheduler, is
  46. * available in the kernel entry pt_regs structure at the top
  47. * of the kernel stack. The kernel trap/irq exit path takes
  48. * care to save/restore almost all registers for ptrace'd
  49. * processes.
  50. *
  51. * (2) A call-clobbered register, where the process P entered the
  52. * kernel via [syscall] trap, is not stored anywhere; that's
  53. * OK, because such registers are not expected to be preserved
  54. * when the trap returns anyway (so we don't actually bother to
  55. * test for this case).
  56. *
  57. * (3) A few registers not used at all by the kernel, and so
  58. * normally never saved except by context-switches, are in the
  59. * context switch state.
  60. */
  61. /* Register saved during kernel entry (or not available). */
  62. regs = task_pt_regs(t);
  63. return (microblaze_reg_t *)((char *)regs + reg_offs);
  64. }
  65. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  66. {
  67. int rval;
  68. unsigned long val = 0;
  69. unsigned long copied;
  70. switch (request) {
  71. case PTRACE_PEEKTEXT: /* read word at location addr. */
  72. case PTRACE_PEEKDATA:
  73. pr_debug("PEEKTEXT/PEEKDATA at %08lX\n", addr);
  74. copied = access_process_vm(child, addr, &val, sizeof(val), 0);
  75. rval = -EIO;
  76. if (copied != sizeof(val))
  77. break;
  78. rval = put_user(val, (unsigned long *)data);
  79. break;
  80. case PTRACE_POKETEXT: /* write the word at location addr. */
  81. case PTRACE_POKEDATA:
  82. pr_debug("POKETEXT/POKEDATA to %08lX\n", addr);
  83. rval = 0;
  84. if (access_process_vm(child, addr, &data, sizeof(data), 1)
  85. == sizeof(data))
  86. break;
  87. rval = -EIO;
  88. break;
  89. /* Read/write the word at location ADDR in the registers. */
  90. case PTRACE_PEEKUSR:
  91. case PTRACE_POKEUSR:
  92. pr_debug("PEEKUSR/POKEUSR : 0x%08lx\n", addr);
  93. rval = 0;
  94. if (addr >= PT_SIZE && request == PTRACE_PEEKUSR) {
  95. /*
  96. * Special requests that don't actually correspond
  97. * to offsets in struct pt_regs.
  98. */
  99. if (addr == PT_TEXT_ADDR) {
  100. val = child->mm->start_code;
  101. } else if (addr == PT_DATA_ADDR) {
  102. val = child->mm->start_data;
  103. } else if (addr == PT_TEXT_LEN) {
  104. val = child->mm->end_code
  105. - child->mm->start_code;
  106. } else {
  107. rval = -EIO;
  108. }
  109. } else if (addr >= 0 && addr < PT_SIZE && (addr & 0x3) == 0) {
  110. microblaze_reg_t *reg_addr = reg_save_addr(addr, child);
  111. if (request == PTRACE_PEEKUSR)
  112. val = *reg_addr;
  113. else
  114. *reg_addr = data;
  115. } else
  116. rval = -EIO;
  117. if (rval == 0 && request == PTRACE_PEEKUSR)
  118. rval = put_user(val, (unsigned long *)data);
  119. break;
  120. /* Continue and stop at next (return from) syscall */
  121. case PTRACE_SYSCALL:
  122. pr_debug("PTRACE_SYSCALL\n");
  123. case PTRACE_SINGLESTEP:
  124. pr_debug("PTRACE_SINGLESTEP\n");
  125. /* Restart after a signal. */
  126. case PTRACE_CONT:
  127. pr_debug("PTRACE_CONT\n");
  128. rval = -EIO;
  129. if (!valid_signal(data))
  130. break;
  131. if (request == PTRACE_SYSCALL)
  132. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  133. else
  134. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  135. child->exit_code = data;
  136. pr_debug("wakeup_process\n");
  137. wake_up_process(child);
  138. rval = 0;
  139. break;
  140. /*
  141. * make the child exit. Best I can do is send it a sigkill.
  142. * perhaps it should be put in the status that it wants to
  143. * exit.
  144. */
  145. case PTRACE_KILL:
  146. pr_debug("PTRACE_KILL\n");
  147. rval = 0;
  148. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  149. break;
  150. child->exit_code = SIGKILL;
  151. wake_up_process(child);
  152. break;
  153. case PTRACE_DETACH: /* detach a process that was attached. */
  154. pr_debug("PTRACE_DETACH\n");
  155. rval = ptrace_detach(child, data);
  156. break;
  157. default:
  158. /* rval = ptrace_request(child, request, addr, data); noMMU */
  159. rval = -EIO;
  160. }
  161. return rval;
  162. }
  163. void ptrace_disable(struct task_struct *child)
  164. {
  165. /* nothing to do */
  166. }