ptrace.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. * This routine will put a word on the process's privileged stack.
  32. */
  33. static void putreg(struct task_struct *task,
  34. unsigned long addr, unsigned long value)
  35. {
  36. unsigned int regno = addr / sizeof(unsigned long);
  37. struct pt_regs *childregs = task_pt_regs(task);
  38. childregs->regs[regno] = value;
  39. childregs->flags |= PT_FLAGS_RESTORE_REGS;
  40. }
  41. static unsigned long getreg(struct task_struct *task, unsigned long addr)
  42. {
  43. unsigned int regno = addr / sizeof(unsigned long);
  44. struct pt_regs *childregs = task_pt_regs(task);
  45. return childregs->regs[regno];
  46. }
  47. /*
  48. * Called by kernel/ptrace.c when detaching..
  49. */
  50. void ptrace_disable(struct task_struct *child)
  51. {
  52. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  53. /*
  54. * These two are currently unused, but will be set by arch_ptrace()
  55. * and used in the syscall assembly when we do support them.
  56. */
  57. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  58. }
  59. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  60. {
  61. unsigned long __user *datap;
  62. unsigned long tmp;
  63. int i;
  64. long ret = -EIO;
  65. #ifdef CONFIG_COMPAT
  66. if (task_thread_info(current)->status & TS_COMPAT)
  67. data = (u32)data;
  68. if (task_thread_info(child)->status & TS_COMPAT)
  69. addr = (u32)addr;
  70. #endif
  71. datap = (unsigned long __user __force *)data;
  72. switch (request) {
  73. case PTRACE_PEEKUSR: /* Read register from pt_regs. */
  74. if (addr & (sizeof(data)-1))
  75. break;
  76. if (addr < 0 || addr >= PTREGS_SIZE)
  77. break;
  78. tmp = getreg(child, addr); /* Read register */
  79. ret = put_user(tmp, datap);
  80. break;
  81. case PTRACE_POKEUSR: /* Write register in pt_regs. */
  82. if (addr & (sizeof(data)-1))
  83. break;
  84. if (addr < 0 || addr >= PTREGS_SIZE)
  85. break;
  86. putreg(child, addr, data); /* Write register */
  87. ret = 0;
  88. break;
  89. case PTRACE_GETREGS: /* Get all registers from the child. */
  90. if (!access_ok(VERIFY_WRITE, datap, PTREGS_SIZE))
  91. break;
  92. for (i = 0; i < PTREGS_SIZE; i += sizeof(long)) {
  93. ret = __put_user(getreg(child, i), datap);
  94. if (ret != 0)
  95. break;
  96. datap++;
  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. for (i = 0; i < PTREGS_SIZE; i += sizeof(long)) {
  103. ret = __get_user(tmp, datap);
  104. if (ret != 0)
  105. break;
  106. putreg(child, i, tmp);
  107. datap++;
  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. }