ptrace.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #include <asm/traps.h>
  22. void user_enable_single_step(struct task_struct *child)
  23. {
  24. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  25. }
  26. void user_disable_single_step(struct task_struct *child)
  27. {
  28. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  29. }
  30. /*
  31. * Called by kernel/ptrace.c when detaching..
  32. */
  33. void ptrace_disable(struct task_struct *child)
  34. {
  35. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  36. /*
  37. * These two are currently unused, but will be set by arch_ptrace()
  38. * and used in the syscall assembly when we do support them.
  39. */
  40. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  41. }
  42. long arch_ptrace(struct task_struct *child, long request,
  43. unsigned long addr, unsigned long data)
  44. {
  45. unsigned long __user *datap = (long __user __force *)data;
  46. unsigned long tmp;
  47. int i;
  48. long ret = -EIO;
  49. unsigned long *childregs;
  50. char *childreg;
  51. switch (request) {
  52. case PTRACE_PEEKUSR: /* Read register from pt_regs. */
  53. if (addr >= PTREGS_SIZE)
  54. break;
  55. childreg = (char *)task_pt_regs(child) + addr;
  56. #ifdef CONFIG_COMPAT
  57. if (is_compat_task()) {
  58. if (addr & (sizeof(compat_long_t)-1))
  59. break;
  60. ret = put_user(*(compat_long_t *)childreg,
  61. (compat_long_t __user *)datap);
  62. } else
  63. #endif
  64. {
  65. if (addr & (sizeof(long)-1))
  66. break;
  67. ret = put_user(*(long *)childreg, datap);
  68. }
  69. break;
  70. case PTRACE_POKEUSR: /* Write register in pt_regs. */
  71. if (addr >= PTREGS_SIZE)
  72. break;
  73. childreg = (char *)task_pt_regs(child) + addr;
  74. #ifdef CONFIG_COMPAT
  75. if (is_compat_task()) {
  76. if (addr & (sizeof(compat_long_t)-1))
  77. break;
  78. *(compat_long_t *)childreg = data;
  79. } else
  80. #endif
  81. {
  82. if (addr & (sizeof(long)-1))
  83. break;
  84. *(long *)childreg = data;
  85. }
  86. ret = 0;
  87. break;
  88. case PTRACE_GETREGS: /* Get all registers from the child. */
  89. if (!access_ok(VERIFY_WRITE, datap, PTREGS_SIZE))
  90. break;
  91. childregs = (long *)task_pt_regs(child);
  92. for (i = 0; i < sizeof(struct pt_regs)/sizeof(unsigned long);
  93. ++i) {
  94. ret = __put_user(childregs[i], &datap[i]);
  95. if (ret != 0)
  96. break;
  97. }
  98. break;
  99. case PTRACE_SETREGS: /* Set all registers in the child. */
  100. if (!access_ok(VERIFY_READ, datap, PTREGS_SIZE))
  101. break;
  102. childregs = (long *)task_pt_regs(child);
  103. for (i = 0; i < sizeof(struct pt_regs)/sizeof(unsigned long);
  104. ++i) {
  105. ret = __get_user(childregs[i], &datap[i]);
  106. if (ret != 0)
  107. break;
  108. }
  109. break;
  110. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  111. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  112. break;
  113. case PTRACE_SETOPTIONS:
  114. /* Support TILE-specific ptrace options. */
  115. child->ptrace &= ~PT_TRACE_MASK_TILE;
  116. tmp = data & PTRACE_O_MASK_TILE;
  117. data &= ~PTRACE_O_MASK_TILE;
  118. ret = ptrace_request(child, request, addr, data);
  119. if (tmp & PTRACE_O_TRACEMIGRATE)
  120. child->ptrace |= PT_TRACE_MIGRATE;
  121. break;
  122. default:
  123. #ifdef CONFIG_COMPAT
  124. if (task_thread_info(current)->status & TS_COMPAT) {
  125. ret = compat_ptrace_request(child, request,
  126. addr, data);
  127. break;
  128. }
  129. #endif
  130. ret = ptrace_request(child, request, addr, data);
  131. break;
  132. }
  133. return ret;
  134. }
  135. #ifdef CONFIG_COMPAT
  136. /* Not used; we handle compat issues in arch_ptrace() directly. */
  137. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  138. compat_ulong_t addr, compat_ulong_t data)
  139. {
  140. BUG();
  141. }
  142. #endif
  143. void do_syscall_trace(void)
  144. {
  145. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  146. return;
  147. if (!(current->ptrace & PT_PTRACED))
  148. return;
  149. /*
  150. * The 0x80 provides a way for the tracing parent to distinguish
  151. * between a syscall stop and SIGTRAP delivery
  152. */
  153. ptrace_notify(SIGTRAP|((current->ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
  154. /*
  155. * this isn't the same as continuing with a signal, but it will do
  156. * for normal use. strace only continues with a signal if the
  157. * stopping signal is not SIGTRAP. -brl
  158. */
  159. if (current->exit_code) {
  160. send_sig(current->exit_code, current, 1);
  161. current->exit_code = 0;
  162. }
  163. }
  164. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
  165. {
  166. struct siginfo info;
  167. memset(&info, 0, sizeof(info));
  168. info.si_signo = SIGTRAP;
  169. info.si_code = TRAP_BRKPT;
  170. info.si_addr = (void __user *) regs->pc;
  171. /* Send us the fakey SIGTRAP */
  172. force_sig_info(SIGTRAP, &info, tsk);
  173. }
  174. /* Handle synthetic interrupt delivered only by the simulator. */
  175. void __kprobes do_breakpoint(struct pt_regs* regs, int fault_num)
  176. {
  177. send_sigtrap(current, regs, fault_num);
  178. }