ptrace.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. /*
  56. * Here are the old "legacy" powerpc specific getregs/setregs ptrace calls,
  57. * we mark them as obsolete now, they will be removed in a future version
  58. */
  59. static long arch_ptrace_old(struct task_struct *child, long request, long addr,
  60. long data)
  61. {
  62. int ret = -EPERM;
  63. switch(request) {
  64. case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
  65. int i;
  66. unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
  67. unsigned long __user *tmp = (unsigned long __user *)addr;
  68. for (i = 0; i < 32; i++) {
  69. ret = put_user(*reg, tmp);
  70. if (ret)
  71. break;
  72. reg++;
  73. tmp++;
  74. }
  75. break;
  76. }
  77. case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
  78. int i;
  79. unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
  80. unsigned long __user *tmp = (unsigned long __user *)addr;
  81. for (i = 0; i < 32; i++) {
  82. ret = get_user(*reg, tmp);
  83. if (ret)
  84. break;
  85. reg++;
  86. tmp++;
  87. }
  88. break;
  89. }
  90. case PPC_PTRACE_GETFPREGS: { /* Get FPRs 0 - 31. */
  91. flush_fp_to_thread(child);
  92. ret = get_fpregs((void __user *)addr, child, 0);
  93. break;
  94. }
  95. case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */
  96. flush_fp_to_thread(child);
  97. ret = set_fpregs((void __user *)addr, child, 0);
  98. break;
  99. }
  100. }
  101. return ret;
  102. }
  103. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  104. {
  105. int ret = -EPERM;
  106. switch (request) {
  107. /* when I and D space are separate, these will need to be fixed. */
  108. case PTRACE_PEEKTEXT: /* read word at location addr. */
  109. case PTRACE_PEEKDATA: {
  110. unsigned long tmp;
  111. int copied;
  112. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  113. ret = -EIO;
  114. if (copied != sizeof(tmp))
  115. break;
  116. ret = put_user(tmp,(unsigned long __user *) data);
  117. break;
  118. }
  119. /* read the word at location addr in the USER area. */
  120. case PTRACE_PEEKUSR: {
  121. unsigned long index, tmp;
  122. ret = -EIO;
  123. /* convert to index and check */
  124. #ifdef CONFIG_PPC32
  125. index = (unsigned long) addr >> 2;
  126. if ((addr & 3) || (index > PT_FPSCR)
  127. || (child->thread.regs == NULL))
  128. #else
  129. index = (unsigned long) addr >> 3;
  130. if ((addr & 7) || (index > PT_FPSCR))
  131. #endif
  132. break;
  133. #ifdef CONFIG_PPC32
  134. CHECK_FULL_REGS(child->thread.regs);
  135. #endif
  136. if (index < PT_FPR0) {
  137. tmp = get_reg(child, (int) index);
  138. } else {
  139. flush_fp_to_thread(child);
  140. tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
  141. }
  142. ret = put_user(tmp,(unsigned long __user *) data);
  143. break;
  144. }
  145. /* If I and D space are separate, this will have to be fixed. */
  146. case PTRACE_POKETEXT: /* write the word at location addr. */
  147. case PTRACE_POKEDATA:
  148. ret = 0;
  149. if (access_process_vm(child, addr, &data, sizeof(data), 1)
  150. == sizeof(data))
  151. break;
  152. ret = -EIO;
  153. break;
  154. /* write the word at location addr in the USER area */
  155. case PTRACE_POKEUSR: {
  156. unsigned long index;
  157. ret = -EIO;
  158. /* convert to index and check */
  159. #ifdef CONFIG_PPC32
  160. index = (unsigned long) addr >> 2;
  161. if ((addr & 3) || (index > PT_FPSCR)
  162. || (child->thread.regs == NULL))
  163. #else
  164. index = (unsigned long) addr >> 3;
  165. if ((addr & 7) || (index > PT_FPSCR))
  166. #endif
  167. break;
  168. #ifdef CONFIG_PPC32
  169. CHECK_FULL_REGS(child->thread.regs);
  170. #endif
  171. if (index == PT_ORIG_R3)
  172. break;
  173. if (index < PT_FPR0) {
  174. ret = put_reg(child, index, data);
  175. } else {
  176. flush_fp_to_thread(child);
  177. ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
  178. ret = 0;
  179. }
  180. break;
  181. }
  182. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  183. case PTRACE_CONT: { /* restart after signal. */
  184. ret = -EIO;
  185. if (!valid_signal(data))
  186. break;
  187. if (request == PTRACE_SYSCALL)
  188. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  189. else
  190. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  191. child->exit_code = data;
  192. /* make sure the single step bit is not set. */
  193. clear_single_step(child);
  194. wake_up_process(child);
  195. ret = 0;
  196. break;
  197. }
  198. /*
  199. * make the child exit. Best I can do is send it a sigkill.
  200. * perhaps it should be put in the status that it wants to
  201. * exit.
  202. */
  203. case PTRACE_KILL: {
  204. ret = 0;
  205. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  206. break;
  207. child->exit_code = SIGKILL;
  208. /* make sure the single step bit is not set. */
  209. clear_single_step(child);
  210. wake_up_process(child);
  211. break;
  212. }
  213. case PTRACE_SINGLESTEP: { /* set the trap flag. */
  214. ret = -EIO;
  215. if (!valid_signal(data))
  216. break;
  217. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  218. set_single_step(child);
  219. child->exit_code = data;
  220. /* give it a chance to run. */
  221. wake_up_process(child);
  222. ret = 0;
  223. break;
  224. }
  225. #ifdef CONFIG_PPC64
  226. case PTRACE_GET_DEBUGREG: {
  227. ret = -EINVAL;
  228. /* We only support one DABR and no IABRS at the moment */
  229. if (addr > 0)
  230. break;
  231. ret = put_user(child->thread.dabr,
  232. (unsigned long __user *)data);
  233. break;
  234. }
  235. case PTRACE_SET_DEBUGREG:
  236. ret = ptrace_set_debugreg(child, addr, data);
  237. break;
  238. #endif
  239. case PTRACE_DETACH:
  240. ret = ptrace_detach(child, data);
  241. break;
  242. #ifdef CONFIG_PPC64
  243. case PTRACE_GETREGS64:
  244. #endif
  245. case PTRACE_GETREGS: { /* Get all pt_regs from the child. */
  246. int ui;
  247. if (!access_ok(VERIFY_WRITE, (void __user *)data,
  248. sizeof(struct pt_regs))) {
  249. ret = -EIO;
  250. break;
  251. }
  252. ret = 0;
  253. for (ui = 0; ui < PT_REGS_COUNT; ui ++) {
  254. ret |= __put_user(get_reg(child, ui),
  255. (unsigned long __user *) data);
  256. data += sizeof(long);
  257. }
  258. break;
  259. }
  260. #ifdef CONFIG_PPC64
  261. case PTRACE_SETREGS64:
  262. #endif
  263. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  264. unsigned long tmp;
  265. int ui;
  266. if (!access_ok(VERIFY_READ, (void __user *)data,
  267. sizeof(struct pt_regs))) {
  268. ret = -EIO;
  269. break;
  270. }
  271. ret = 0;
  272. for (ui = 0; ui < PT_REGS_COUNT; ui ++) {
  273. ret = __get_user(tmp, (unsigned long __user *) data);
  274. if (ret)
  275. break;
  276. put_reg(child, ui, tmp);
  277. data += sizeof(long);
  278. }
  279. break;
  280. }
  281. case PTRACE_GETFPREGS: { /* Get the child FPU state (FPR0...31 + FPSCR) */
  282. flush_fp_to_thread(child);
  283. ret = get_fpregs((void __user *)data, child, 1);
  284. break;
  285. }
  286. case PTRACE_SETFPREGS: { /* Set the child FPU state (FPR0...31 + FPSCR) */
  287. flush_fp_to_thread(child);
  288. ret = set_fpregs((void __user *)data, child, 1);
  289. break;
  290. }
  291. #ifdef CONFIG_ALTIVEC
  292. case PTRACE_GETVRREGS:
  293. /* Get the child altivec register state. */
  294. flush_altivec_to_thread(child);
  295. ret = get_vrregs((unsigned long __user *)data, child);
  296. break;
  297. case PTRACE_SETVRREGS:
  298. /* Set the child altivec register state. */
  299. flush_altivec_to_thread(child);
  300. ret = set_vrregs(child, (unsigned long __user *)data);
  301. break;
  302. #endif
  303. #ifdef CONFIG_SPE
  304. case PTRACE_GETEVRREGS:
  305. /* Get the child spe register state. */
  306. if (child->thread.regs->msr & MSR_SPE)
  307. giveup_spe(child);
  308. ret = get_evrregs((unsigned long __user *)data, child);
  309. break;
  310. case PTRACE_SETEVRREGS:
  311. /* Set the child spe register state. */
  312. /* this is to clear the MSR_SPE bit to force a reload
  313. * of register state from memory */
  314. if (child->thread.regs->msr & MSR_SPE)
  315. giveup_spe(child);
  316. ret = set_evrregs(child, (unsigned long __user *)data);
  317. break;
  318. #endif
  319. /* Old reverse args ptrace callss */
  320. case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
  321. case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
  322. case PPC_PTRACE_GETFPREGS: /* Get FPRs 0 - 31. */
  323. case PPC_PTRACE_SETFPREGS: /* Get FPRs 0 - 31. */
  324. ret = arch_ptrace_old(child, request, addr, data);
  325. break;
  326. default:
  327. ret = ptrace_request(child, request, addr, data);
  328. break;
  329. }
  330. return ret;
  331. }
  332. static void do_syscall_trace(void)
  333. {
  334. /* the 0x80 provides a way for the tracing parent to distinguish
  335. between a syscall stop and SIGTRAP delivery */
  336. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  337. ? 0x80 : 0));
  338. /*
  339. * this isn't the same as continuing with a signal, but it will do
  340. * for normal use. strace only continues with a signal if the
  341. * stopping signal is not SIGTRAP. -brl
  342. */
  343. if (current->exit_code) {
  344. send_sig(current->exit_code, current, 1);
  345. current->exit_code = 0;
  346. }
  347. }
  348. void do_syscall_trace_enter(struct pt_regs *regs)
  349. {
  350. secure_computing(regs->gpr[0]);
  351. if (test_thread_flag(TIF_SYSCALL_TRACE)
  352. && (current->ptrace & PT_PTRACED))
  353. do_syscall_trace();
  354. if (unlikely(current->audit_context)) {
  355. #ifdef CONFIG_PPC64
  356. if (!test_thread_flag(TIF_32BIT))
  357. audit_syscall_entry(AUDIT_ARCH_PPC64,
  358. regs->gpr[0],
  359. regs->gpr[3], regs->gpr[4],
  360. regs->gpr[5], regs->gpr[6]);
  361. else
  362. #endif
  363. audit_syscall_entry(AUDIT_ARCH_PPC,
  364. regs->gpr[0],
  365. regs->gpr[3] & 0xffffffff,
  366. regs->gpr[4] & 0xffffffff,
  367. regs->gpr[5] & 0xffffffff,
  368. regs->gpr[6] & 0xffffffff);
  369. }
  370. }
  371. void do_syscall_trace_leave(struct pt_regs *regs)
  372. {
  373. if (unlikely(current->audit_context))
  374. audit_syscall_exit((regs->ccr&0x10000000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
  375. regs->result);
  376. if ((test_thread_flag(TIF_SYSCALL_TRACE)
  377. || test_thread_flag(TIF_SINGLESTEP))
  378. && (current->ptrace & PT_PTRACED))
  379. do_syscall_trace();
  380. }