ptrace.c 11 KB

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