ptrace.c 13 KB

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