ptrace.c 8.8 KB

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