ptrace.c 8.3 KB

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