ptrace.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * arch/ppc/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/signal.h>
  29. #include <linux/seccomp.h>
  30. #include <linux/audit.h>
  31. #include <linux/module.h>
  32. #include <asm/uaccess.h>
  33. #include <asm/page.h>
  34. #include <asm/pgtable.h>
  35. #include <asm/system.h>
  36. /*
  37. * Set of msr bits that gdb can change on behalf of a process.
  38. */
  39. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  40. #define MSR_DEBUGCHANGE 0
  41. #else
  42. #define MSR_DEBUGCHANGE (MSR_SE | MSR_BE)
  43. #endif
  44. /*
  45. * does not yet catch signals sent when the child dies.
  46. * in exit.c or in signal.c.
  47. */
  48. /*
  49. * Get contents of register REGNO in task TASK.
  50. */
  51. static inline unsigned long get_reg(struct task_struct *task, int regno)
  52. {
  53. if (regno < sizeof(struct pt_regs) / sizeof(unsigned long)
  54. && task->thread.regs != NULL)
  55. return ((unsigned long *)task->thread.regs)[regno];
  56. return (0);
  57. }
  58. /*
  59. * Write contents of register REGNO in task TASK.
  60. */
  61. static inline int put_reg(struct task_struct *task, int regno,
  62. unsigned long data)
  63. {
  64. if (regno <= PT_MQ && task->thread.regs != NULL) {
  65. if (regno == PT_MSR)
  66. data = (data & MSR_DEBUGCHANGE)
  67. | (task->thread.regs->msr & ~MSR_DEBUGCHANGE);
  68. ((unsigned long *)task->thread.regs)[regno] = data;
  69. return 0;
  70. }
  71. return -EIO;
  72. }
  73. #ifdef CONFIG_ALTIVEC
  74. /*
  75. * Get contents of AltiVec register state in task TASK
  76. */
  77. static inline int get_vrregs(unsigned long __user *data, struct task_struct *task)
  78. {
  79. int i, j;
  80. if (!access_ok(VERIFY_WRITE, data, 133 * sizeof(unsigned long)))
  81. return -EFAULT;
  82. /* copy AltiVec registers VR[0] .. VR[31] */
  83. for (i = 0; i < 32; i++)
  84. for (j = 0; j < 4; j++, data++)
  85. if (__put_user(task->thread.vr[i].u[j], data))
  86. return -EFAULT;
  87. /* copy VSCR */
  88. for (i = 0; i < 4; i++, data++)
  89. if (__put_user(task->thread.vscr.u[i], data))
  90. return -EFAULT;
  91. /* copy VRSAVE */
  92. if (__put_user(task->thread.vrsave, data))
  93. return -EFAULT;
  94. return 0;
  95. }
  96. /*
  97. * Write contents of AltiVec register state into task TASK.
  98. */
  99. static inline int set_vrregs(struct task_struct *task, unsigned long __user *data)
  100. {
  101. int i, j;
  102. if (!access_ok(VERIFY_READ, data, 133 * sizeof(unsigned long)))
  103. return -EFAULT;
  104. /* copy AltiVec registers VR[0] .. VR[31] */
  105. for (i = 0; i < 32; i++)
  106. for (j = 0; j < 4; j++, data++)
  107. if (__get_user(task->thread.vr[i].u[j], data))
  108. return -EFAULT;
  109. /* copy VSCR */
  110. for (i = 0; i < 4; i++, data++)
  111. if (__get_user(task->thread.vscr.u[i], data))
  112. return -EFAULT;
  113. /* copy VRSAVE */
  114. if (__get_user(task->thread.vrsave, data))
  115. return -EFAULT;
  116. return 0;
  117. }
  118. #endif
  119. #ifdef CONFIG_SPE
  120. /*
  121. * For get_evrregs/set_evrregs functions 'data' has the following layout:
  122. *
  123. * struct {
  124. * u32 evr[32];
  125. * u64 acc;
  126. * u32 spefscr;
  127. * }
  128. */
  129. /*
  130. * Get contents of SPE register state in task TASK.
  131. */
  132. static inline int get_evrregs(unsigned long *data, struct task_struct *task)
  133. {
  134. int i;
  135. if (!access_ok(VERIFY_WRITE, data, 35 * sizeof(unsigned long)))
  136. return -EFAULT;
  137. /* copy SPEFSCR */
  138. if (__put_user(task->thread.spefscr, &data[34]))
  139. return -EFAULT;
  140. /* copy SPE registers EVR[0] .. EVR[31] */
  141. for (i = 0; i < 32; i++, data++)
  142. if (__put_user(task->thread.evr[i], data))
  143. return -EFAULT;
  144. /* copy ACC */
  145. if (__put_user64(task->thread.acc, (unsigned long long *)data))
  146. return -EFAULT;
  147. return 0;
  148. }
  149. /*
  150. * Write contents of SPE register state into task TASK.
  151. */
  152. static inline int set_evrregs(struct task_struct *task, unsigned long *data)
  153. {
  154. int i;
  155. if (!access_ok(VERIFY_READ, data, 35 * sizeof(unsigned long)))
  156. return -EFAULT;
  157. /* copy SPEFSCR */
  158. if (__get_user(task->thread.spefscr, &data[34]))
  159. return -EFAULT;
  160. /* copy SPE registers EVR[0] .. EVR[31] */
  161. for (i = 0; i < 32; i++, data++)
  162. if (__get_user(task->thread.evr[i], data))
  163. return -EFAULT;
  164. /* copy ACC */
  165. if (__get_user64(task->thread.acc, (unsigned long long*)data))
  166. return -EFAULT;
  167. return 0;
  168. }
  169. #endif /* CONFIG_SPE */
  170. static inline void
  171. set_single_step(struct task_struct *task)
  172. {
  173. struct pt_regs *regs = task->thread.regs;
  174. if (regs != NULL) {
  175. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  176. task->thread.dbcr0 = DBCR0_IDM | DBCR0_IC;
  177. regs->msr |= MSR_DE;
  178. #else
  179. regs->msr |= MSR_SE;
  180. #endif
  181. }
  182. }
  183. static inline void
  184. clear_single_step(struct task_struct *task)
  185. {
  186. struct pt_regs *regs = task->thread.regs;
  187. if (regs != NULL) {
  188. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  189. task->thread.dbcr0 = 0;
  190. regs->msr &= ~MSR_DE;
  191. #else
  192. regs->msr &= ~MSR_SE;
  193. #endif
  194. }
  195. }
  196. /*
  197. * Called by kernel/ptrace.c when detaching..
  198. *
  199. * Make sure single step bits etc are not set.
  200. */
  201. void ptrace_disable(struct task_struct *child)
  202. {
  203. /* make sure the single step bit is not set. */
  204. clear_single_step(child);
  205. }
  206. int sys_ptrace(long request, long pid, long addr, long data)
  207. {
  208. struct task_struct *child;
  209. int ret = -EPERM;
  210. lock_kernel();
  211. if (request == PTRACE_TRACEME) {
  212. /* are we already being traced? */
  213. if (current->ptrace & PT_PTRACED)
  214. goto out;
  215. ret = security_ptrace(current->parent, current);
  216. if (ret)
  217. goto out;
  218. /* set the ptrace bit in the process flags. */
  219. current->ptrace |= PT_PTRACED;
  220. ret = 0;
  221. goto out;
  222. }
  223. ret = -ESRCH;
  224. read_lock(&tasklist_lock);
  225. child = find_task_by_pid(pid);
  226. if (child)
  227. get_task_struct(child);
  228. read_unlock(&tasklist_lock);
  229. if (!child)
  230. goto out;
  231. ret = -EPERM;
  232. if (pid == 1) /* you may not mess with init */
  233. goto out_tsk;
  234. if (request == PTRACE_ATTACH) {
  235. ret = ptrace_attach(child);
  236. goto out_tsk;
  237. }
  238. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  239. if (ret < 0)
  240. goto out_tsk;
  241. switch (request) {
  242. /* when I and D space are separate, these will need to be fixed. */
  243. case PTRACE_PEEKTEXT: /* read word at location addr. */
  244. case PTRACE_PEEKDATA: {
  245. unsigned long tmp;
  246. int copied;
  247. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  248. ret = -EIO;
  249. if (copied != sizeof(tmp))
  250. break;
  251. ret = put_user(tmp,(unsigned long __user *) data);
  252. break;
  253. }
  254. /* read the word at location addr in the USER area. */
  255. /* XXX this will need fixing for 64-bit */
  256. case PTRACE_PEEKUSR: {
  257. unsigned long index, tmp;
  258. ret = -EIO;
  259. /* convert to index and check */
  260. index = (unsigned long) addr >> 2;
  261. if ((addr & 3) || index > PT_FPSCR
  262. || child->thread.regs == NULL)
  263. break;
  264. CHECK_FULL_REGS(child->thread.regs);
  265. if (index < PT_FPR0) {
  266. tmp = get_reg(child, (int) index);
  267. } else {
  268. preempt_disable();
  269. if (child->thread.regs->msr & MSR_FP)
  270. giveup_fpu(child);
  271. preempt_enable();
  272. tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
  273. }
  274. ret = put_user(tmp,(unsigned long __user *) data);
  275. break;
  276. }
  277. /* If I and D space are separate, this will have to be fixed. */
  278. case PTRACE_POKETEXT: /* write the word at location addr. */
  279. case PTRACE_POKEDATA:
  280. ret = 0;
  281. if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
  282. break;
  283. ret = -EIO;
  284. break;
  285. /* write the word at location addr in the USER area */
  286. case PTRACE_POKEUSR: {
  287. unsigned long index;
  288. ret = -EIO;
  289. /* convert to index and check */
  290. index = (unsigned long) addr >> 2;
  291. if ((addr & 3) || index > PT_FPSCR
  292. || child->thread.regs == NULL)
  293. break;
  294. CHECK_FULL_REGS(child->thread.regs);
  295. if (index == PT_ORIG_R3)
  296. break;
  297. if (index < PT_FPR0) {
  298. ret = put_reg(child, index, data);
  299. } else {
  300. preempt_disable();
  301. if (child->thread.regs->msr & MSR_FP)
  302. giveup_fpu(child);
  303. preempt_enable();
  304. ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
  305. ret = 0;
  306. }
  307. break;
  308. }
  309. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  310. case PTRACE_CONT: { /* restart after signal. */
  311. ret = -EIO;
  312. if (!valid_signal(data))
  313. break;
  314. if (request == PTRACE_SYSCALL) {
  315. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  316. } else {
  317. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  318. }
  319. child->exit_code = data;
  320. /* make sure the single step bit is not set. */
  321. clear_single_step(child);
  322. wake_up_process(child);
  323. ret = 0;
  324. break;
  325. }
  326. /*
  327. * make the child exit. Best I can do is send it a sigkill.
  328. * perhaps it should be put in the status that it wants to
  329. * exit.
  330. */
  331. case PTRACE_KILL: {
  332. ret = 0;
  333. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  334. break;
  335. child->exit_code = SIGKILL;
  336. /* make sure the single step bit is not set. */
  337. clear_single_step(child);
  338. wake_up_process(child);
  339. break;
  340. }
  341. case PTRACE_SINGLESTEP: { /* set the trap flag. */
  342. ret = -EIO;
  343. if (!valid_signal(data))
  344. break;
  345. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  346. set_single_step(child);
  347. child->exit_code = data;
  348. /* give it a chance to run. */
  349. wake_up_process(child);
  350. ret = 0;
  351. break;
  352. }
  353. case PTRACE_DETACH:
  354. ret = ptrace_detach(child, data);
  355. break;
  356. #ifdef CONFIG_ALTIVEC
  357. case PTRACE_GETVRREGS:
  358. /* Get the child altivec register state. */
  359. preempt_disable();
  360. if (child->thread.regs->msr & MSR_VEC)
  361. giveup_altivec(child);
  362. preempt_enable();
  363. ret = get_vrregs((unsigned long __user *)data, child);
  364. break;
  365. case PTRACE_SETVRREGS:
  366. /* Set the child altivec register state. */
  367. /* this is to clear the MSR_VEC bit to force a reload
  368. * of register state from memory */
  369. preempt_disable();
  370. if (child->thread.regs->msr & MSR_VEC)
  371. giveup_altivec(child);
  372. preempt_enable();
  373. ret = set_vrregs(child, (unsigned long __user *)data);
  374. break;
  375. #endif
  376. #ifdef CONFIG_SPE
  377. case PTRACE_GETEVRREGS:
  378. /* Get the child spe register state. */
  379. if (child->thread.regs->msr & MSR_SPE)
  380. giveup_spe(child);
  381. ret = get_evrregs((unsigned long __user *)data, child);
  382. break;
  383. case PTRACE_SETEVRREGS:
  384. /* Set the child spe register state. */
  385. /* this is to clear the MSR_SPE bit to force a reload
  386. * of register state from memory */
  387. if (child->thread.regs->msr & MSR_SPE)
  388. giveup_spe(child);
  389. ret = set_evrregs(child, (unsigned long __user *)data);
  390. break;
  391. #endif
  392. default:
  393. ret = ptrace_request(child, request, addr, data);
  394. break;
  395. }
  396. out_tsk:
  397. put_task_struct(child);
  398. out:
  399. unlock_kernel();
  400. return ret;
  401. }
  402. static void do_syscall_trace(void)
  403. {
  404. /* the 0x80 provides a way for the tracing parent to distinguish
  405. between a syscall stop and SIGTRAP delivery */
  406. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  407. ? 0x80 : 0));
  408. /*
  409. * this isn't the same as continuing with a signal, but it will do
  410. * for normal use. strace only continues with a signal if the
  411. * stopping signal is not SIGTRAP. -brl
  412. */
  413. if (current->exit_code) {
  414. send_sig(current->exit_code, current, 1);
  415. current->exit_code = 0;
  416. }
  417. }
  418. void do_syscall_trace_enter(struct pt_regs *regs)
  419. {
  420. if (test_thread_flag(TIF_SYSCALL_TRACE)
  421. && (current->ptrace & PT_PTRACED))
  422. do_syscall_trace();
  423. if (unlikely(current->audit_context))
  424. audit_syscall_entry(current, AUDIT_ARCH_PPC,
  425. regs->gpr[0],
  426. regs->gpr[3], regs->gpr[4],
  427. regs->gpr[5], regs->gpr[6]);
  428. }
  429. void do_syscall_trace_leave(struct pt_regs *regs)
  430. {
  431. secure_computing(regs->gpr[0]);
  432. if (unlikely(current->audit_context))
  433. audit_syscall_exit(current,
  434. (regs->ccr&0x1000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
  435. regs->result);
  436. if ((test_thread_flag(TIF_SYSCALL_TRACE))
  437. && (current->ptrace & PT_PTRACED))
  438. do_syscall_trace();
  439. }
  440. EXPORT_SYMBOL(do_syscall_trace_enter);
  441. EXPORT_SYMBOL(do_syscall_trace_leave);