ptrace.c 5.0 KB

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