ptrace.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * linux/arch/ppc64/kernel/ptrace.c
  3. *
  4. * PowerPC version
  5. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  6. *
  7. * Derived from "arch/m68k/kernel/ptrace.c"
  8. * Copyright (C) 1994 by Hamish Macdonald
  9. * Taken from linux/kernel/ptrace.c and modified for M680x0.
  10. * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
  11. *
  12. * Modified by Cort Dougan (cort@hq.fsmlabs.com)
  13. * and Paul Mackerras (paulus@linuxcare.com.au).
  14. *
  15. * This file is subject to the terms and conditions of the GNU General
  16. * Public License. See the file README.legal in the main directory of
  17. * this archive for more details.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/sched.h>
  21. #include <linux/mm.h>
  22. #include <linux/smp.h>
  23. #include <linux/smp_lock.h>
  24. #include <linux/errno.h>
  25. #include <linux/ptrace.h>
  26. #include <linux/user.h>
  27. #include <linux/security.h>
  28. #include <linux/audit.h>
  29. #include <linux/seccomp.h>
  30. #include <linux/signal.h>
  31. #include <asm/uaccess.h>
  32. #include <asm/page.h>
  33. #include <asm/pgtable.h>
  34. #include <asm/system.h>
  35. #include <asm/ptrace-common.h>
  36. /*
  37. * does not yet catch signals sent when the child dies.
  38. * in exit.c or in signal.c.
  39. */
  40. /*
  41. * Called by kernel/ptrace.c when detaching..
  42. *
  43. * Make sure single step bits etc are not set.
  44. */
  45. void ptrace_disable(struct task_struct *child)
  46. {
  47. /* make sure the single step bit is not set. */
  48. clear_single_step(child);
  49. }
  50. int sys_ptrace(long request, long pid, long addr, long data)
  51. {
  52. struct task_struct *child;
  53. int ret = -EPERM;
  54. lock_kernel();
  55. if (request == PTRACE_TRACEME) {
  56. /* are we already being traced? */
  57. if (current->ptrace & PT_PTRACED)
  58. goto out;
  59. ret = security_ptrace(current->parent, current);
  60. if (ret)
  61. goto out;
  62. /* set the ptrace bit in the process flags. */
  63. current->ptrace |= PT_PTRACED;
  64. ret = 0;
  65. goto out;
  66. }
  67. ret = -ESRCH;
  68. read_lock(&tasklist_lock);
  69. child = find_task_by_pid(pid);
  70. if (child)
  71. get_task_struct(child);
  72. read_unlock(&tasklist_lock);
  73. if (!child)
  74. goto out;
  75. ret = -EPERM;
  76. if (pid == 1) /* you may not mess with init */
  77. goto out_tsk;
  78. if (request == PTRACE_ATTACH) {
  79. ret = ptrace_attach(child);
  80. goto out_tsk;
  81. }
  82. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  83. if (ret < 0)
  84. goto out_tsk;
  85. switch (request) {
  86. /* when I and D space are separate, these will need to be fixed. */
  87. case PTRACE_PEEKTEXT: /* read word at location addr. */
  88. case PTRACE_PEEKDATA: {
  89. unsigned long tmp;
  90. int copied;
  91. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  92. ret = -EIO;
  93. if (copied != sizeof(tmp))
  94. break;
  95. ret = put_user(tmp,(unsigned long __user *) data);
  96. break;
  97. }
  98. /* read the word at location addr in the USER area. */
  99. case PTRACE_PEEKUSR: {
  100. unsigned long index;
  101. unsigned long tmp;
  102. ret = -EIO;
  103. /* convert to index and check */
  104. index = (unsigned long) addr >> 3;
  105. if ((addr & 7) || (index > PT_FPSCR))
  106. break;
  107. if (index < PT_FPR0) {
  108. tmp = get_reg(child, (int)index);
  109. } else {
  110. flush_fp_to_thread(child);
  111. tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
  112. }
  113. ret = put_user(tmp,(unsigned long __user *) data);
  114. break;
  115. }
  116. /* If I and D space are separate, this will have to be fixed. */
  117. case PTRACE_POKETEXT: /* write the word at location addr. */
  118. case PTRACE_POKEDATA:
  119. ret = 0;
  120. if (access_process_vm(child, addr, &data, sizeof(data), 1)
  121. == sizeof(data))
  122. break;
  123. ret = -EIO;
  124. break;
  125. /* write the word at location addr in the USER area */
  126. case PTRACE_POKEUSR: {
  127. unsigned long index;
  128. ret = -EIO;
  129. /* convert to index and check */
  130. index = (unsigned long) addr >> 3;
  131. if ((addr & 7) || (index > PT_FPSCR))
  132. break;
  133. if (index == PT_ORIG_R3)
  134. break;
  135. if (index < PT_FPR0) {
  136. ret = put_reg(child, index, data);
  137. } else {
  138. flush_fp_to_thread(child);
  139. ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
  140. ret = 0;
  141. }
  142. break;
  143. }
  144. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  145. case PTRACE_CONT: { /* restart after signal. */
  146. ret = -EIO;
  147. if (!valid_signal(data))
  148. break;
  149. if (request == PTRACE_SYSCALL)
  150. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  151. else
  152. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  153. child->exit_code = data;
  154. /* make sure the single step bit is not set. */
  155. clear_single_step(child);
  156. wake_up_process(child);
  157. ret = 0;
  158. break;
  159. }
  160. /*
  161. * make the child exit. Best I can do is send it a sigkill.
  162. * perhaps it should be put in the status that it wants to
  163. * exit.
  164. */
  165. case PTRACE_KILL: {
  166. ret = 0;
  167. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  168. break;
  169. child->exit_code = SIGKILL;
  170. /* make sure the single step bit is not set. */
  171. clear_single_step(child);
  172. wake_up_process(child);
  173. break;
  174. }
  175. case PTRACE_SINGLESTEP: { /* set the trap flag. */
  176. ret = -EIO;
  177. if (!valid_signal(data))
  178. break;
  179. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  180. set_single_step(child);
  181. child->exit_code = data;
  182. /* give it a chance to run. */
  183. wake_up_process(child);
  184. ret = 0;
  185. break;
  186. }
  187. case PTRACE_DETACH:
  188. ret = ptrace_detach(child, data);
  189. break;
  190. case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
  191. int i;
  192. unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
  193. unsigned long __user *tmp = (unsigned long __user *)addr;
  194. for (i = 0; i < 32; i++) {
  195. ret = put_user(*reg, tmp);
  196. if (ret)
  197. break;
  198. reg++;
  199. tmp++;
  200. }
  201. break;
  202. }
  203. case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
  204. int i;
  205. unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
  206. unsigned long __user *tmp = (unsigned long __user *)addr;
  207. for (i = 0; i < 32; i++) {
  208. ret = get_user(*reg, tmp);
  209. if (ret)
  210. break;
  211. reg++;
  212. tmp++;
  213. }
  214. break;
  215. }
  216. case PPC_PTRACE_GETFPREGS: { /* Get FPRs 0 - 31. */
  217. int i;
  218. unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
  219. unsigned long __user *tmp = (unsigned long __user *)addr;
  220. flush_fp_to_thread(child);
  221. for (i = 0; i < 32; i++) {
  222. ret = put_user(*reg, tmp);
  223. if (ret)
  224. break;
  225. reg++;
  226. tmp++;
  227. }
  228. break;
  229. }
  230. case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */
  231. int i;
  232. unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
  233. unsigned long __user *tmp = (unsigned long __user *)addr;
  234. flush_fp_to_thread(child);
  235. for (i = 0; i < 32; i++) {
  236. ret = get_user(*reg, tmp);
  237. if (ret)
  238. break;
  239. reg++;
  240. tmp++;
  241. }
  242. break;
  243. }
  244. default:
  245. ret = ptrace_request(child, request, addr, data);
  246. break;
  247. }
  248. out_tsk:
  249. put_task_struct(child);
  250. out:
  251. unlock_kernel();
  252. return ret;
  253. }
  254. static void do_syscall_trace(void)
  255. {
  256. /* the 0x80 provides a way for the tracing parent to distinguish
  257. between a syscall stop and SIGTRAP delivery */
  258. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  259. ? 0x80 : 0));
  260. /*
  261. * this isn't the same as continuing with a signal, but it will do
  262. * for normal use. strace only continues with a signal if the
  263. * stopping signal is not SIGTRAP. -brl
  264. */
  265. if (current->exit_code) {
  266. send_sig(current->exit_code, current, 1);
  267. current->exit_code = 0;
  268. }
  269. }
  270. void do_syscall_trace_enter(struct pt_regs *regs)
  271. {
  272. secure_computing(regs->gpr[0]);
  273. if (test_thread_flag(TIF_SYSCALL_TRACE)
  274. && (current->ptrace & PT_PTRACED))
  275. do_syscall_trace();
  276. if (unlikely(current->audit_context))
  277. audit_syscall_entry(current,
  278. test_thread_flag(TIF_32BIT)?AUDIT_ARCH_PPC:AUDIT_ARCH_PPC64,
  279. regs->gpr[0],
  280. regs->gpr[3], regs->gpr[4],
  281. regs->gpr[5], regs->gpr[6]);
  282. }
  283. void do_syscall_trace_leave(struct pt_regs *regs)
  284. {
  285. if (unlikely(current->audit_context))
  286. audit_syscall_exit(current,
  287. (regs->ccr&0x1000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
  288. regs->result);
  289. if ((test_thread_flag(TIF_SYSCALL_TRACE)
  290. || test_thread_flag(TIF_SINGLESTEP))
  291. && (current->ptrace & PT_PTRACED))
  292. do_syscall_trace();
  293. }