ptrace.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. *
  14. * Copied from i386: Ross Biro 1/23/92
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/ptrace.h>
  18. #include <linux/kprobes.h>
  19. #include <linux/compat.h>
  20. #include <linux/uaccess.h>
  21. void user_enable_single_step(struct task_struct *child)
  22. {
  23. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  24. }
  25. void user_disable_single_step(struct task_struct *child)
  26. {
  27. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  28. }
  29. /*
  30. * This routine will put a word on the process's privileged stack.
  31. */
  32. static void putreg(struct task_struct *task,
  33. unsigned long addr, unsigned long value)
  34. {
  35. unsigned int regno = addr / sizeof(unsigned long);
  36. struct pt_regs *childregs = task_pt_regs(task);
  37. childregs->regs[regno] = value;
  38. childregs->flags |= PT_FLAGS_RESTORE_REGS;
  39. }
  40. static unsigned long getreg(struct task_struct *task, unsigned long addr)
  41. {
  42. unsigned int regno = addr / sizeof(unsigned long);
  43. struct pt_regs *childregs = task_pt_regs(task);
  44. return childregs->regs[regno];
  45. }
  46. /*
  47. * Called by kernel/ptrace.c when detaching..
  48. */
  49. void ptrace_disable(struct task_struct *child)
  50. {
  51. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  52. /*
  53. * These two are currently unused, but will be set by arch_ptrace()
  54. * and used in the syscall assembly when we do support them.
  55. */
  56. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  57. }
  58. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  59. {
  60. unsigned long __user *datap;
  61. unsigned long tmp;
  62. int i;
  63. long ret = -EIO;
  64. #ifdef CONFIG_COMPAT
  65. if (task_thread_info(current)->status & TS_COMPAT)
  66. data = (u32)data;
  67. if (task_thread_info(child)->status & TS_COMPAT)
  68. addr = (u32)addr;
  69. #endif
  70. datap = (unsigned long __user *)data;
  71. switch (request) {
  72. case PTRACE_PEEKUSR: /* Read register from pt_regs. */
  73. if (addr & (sizeof(data)-1))
  74. break;
  75. if (addr < 0 || addr >= PTREGS_SIZE)
  76. break;
  77. tmp = getreg(child, addr); /* Read register */
  78. ret = put_user(tmp, datap);
  79. break;
  80. case PTRACE_POKEUSR: /* Write register in pt_regs. */
  81. if (addr & (sizeof(data)-1))
  82. break;
  83. if (addr < 0 || addr >= PTREGS_SIZE)
  84. break;
  85. putreg(child, addr, data); /* Write register */
  86. break;
  87. case PTRACE_GETREGS: /* Get all registers from the child. */
  88. if (!access_ok(VERIFY_WRITE, datap, PTREGS_SIZE))
  89. break;
  90. for (i = 0; i < PTREGS_SIZE; i += sizeof(long)) {
  91. ret = __put_user(getreg(child, i), datap);
  92. if (ret != 0)
  93. break;
  94. datap++;
  95. }
  96. break;
  97. case PTRACE_SETREGS: /* Set all registers in the child. */
  98. if (!access_ok(VERIFY_READ, datap, PTREGS_SIZE))
  99. break;
  100. for (i = 0; i < PTREGS_SIZE; i += sizeof(long)) {
  101. ret = __get_user(tmp, datap);
  102. if (ret != 0)
  103. break;
  104. putreg(child, i, tmp);
  105. datap++;
  106. }
  107. break;
  108. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  109. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  110. break;
  111. case PTRACE_SETOPTIONS:
  112. /* Support TILE-specific ptrace options. */
  113. child->ptrace &= ~PT_TRACE_MASK_TILE;
  114. tmp = data & PTRACE_O_MASK_TILE;
  115. data &= ~PTRACE_O_MASK_TILE;
  116. ret = ptrace_request(child, request, addr, data);
  117. if (tmp & PTRACE_O_TRACEMIGRATE)
  118. child->ptrace |= PT_TRACE_MIGRATE;
  119. break;
  120. default:
  121. #ifdef CONFIG_COMPAT
  122. if (task_thread_info(current)->status & TS_COMPAT) {
  123. ret = compat_ptrace_request(child, request,
  124. addr, data);
  125. break;
  126. }
  127. #endif
  128. ret = ptrace_request(child, request, addr, data);
  129. break;
  130. }
  131. return ret;
  132. }
  133. #ifdef CONFIG_COMPAT
  134. /* Not used; we handle compat issues in arch_ptrace() directly. */
  135. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  136. compat_ulong_t addr, compat_ulong_t data)
  137. {
  138. BUG();
  139. }
  140. #endif
  141. void do_syscall_trace(void)
  142. {
  143. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  144. return;
  145. if (!(current->ptrace & PT_PTRACED))
  146. return;
  147. /*
  148. * The 0x80 provides a way for the tracing parent to distinguish
  149. * between a syscall stop and SIGTRAP delivery
  150. */
  151. ptrace_notify(SIGTRAP|((current->ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
  152. /*
  153. * this isn't the same as continuing with a signal, but it will do
  154. * for normal use. strace only continues with a signal if the
  155. * stopping signal is not SIGTRAP. -brl
  156. */
  157. if (current->exit_code) {
  158. send_sig(current->exit_code, current, 1);
  159. current->exit_code = 0;
  160. }
  161. }
  162. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
  163. {
  164. struct siginfo info;
  165. memset(&info, 0, sizeof(info));
  166. info.si_signo = SIGTRAP;
  167. info.si_code = TRAP_BRKPT;
  168. info.si_addr = (void __user *) regs->pc;
  169. /* Send us the fakey SIGTRAP */
  170. force_sig_info(SIGTRAP, &info, tsk);
  171. }
  172. /* Handle synthetic interrupt delivered only by the simulator. */
  173. void __kprobes do_breakpoint(struct pt_regs* regs, int fault_num)
  174. {
  175. send_sigtrap(current, regs, fault_num);
  176. }