ptrace.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. set_tsk_thread_flag(task, TIF_SINGLESTEP);
  188. }
  189. static inline void
  190. clear_single_step(struct task_struct *task)
  191. {
  192. struct pt_regs *regs = task->thread.regs;
  193. if (regs != NULL) {
  194. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  195. task->thread.dbcr0 = 0;
  196. regs->msr &= ~MSR_DE;
  197. #else
  198. regs->msr &= ~MSR_SE;
  199. #endif
  200. }
  201. clear_tsk_thread_flag(task, TIF_SINGLESTEP);
  202. }
  203. #endif /* CONFIG_PPC32 */
  204. /*
  205. * Called by kernel/ptrace.c when detaching..
  206. *
  207. * Make sure single step bits etc are not set.
  208. */
  209. void ptrace_disable(struct task_struct *child)
  210. {
  211. /* make sure the single step bit is not set. */
  212. clear_single_step(child);
  213. }
  214. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  215. {
  216. int ret = -EPERM;
  217. switch (request) {
  218. /* when I and D space are separate, these will need to be fixed. */
  219. case PTRACE_PEEKTEXT: /* read word at location addr. */
  220. case PTRACE_PEEKDATA: {
  221. unsigned long tmp;
  222. int copied;
  223. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  224. ret = -EIO;
  225. if (copied != sizeof(tmp))
  226. break;
  227. ret = put_user(tmp,(unsigned long __user *) data);
  228. break;
  229. }
  230. /* read the word at location addr in the USER area. */
  231. case PTRACE_PEEKUSR: {
  232. unsigned long index, tmp;
  233. ret = -EIO;
  234. /* convert to index and check */
  235. #ifdef CONFIG_PPC32
  236. index = (unsigned long) addr >> 2;
  237. if ((addr & 3) || (index > PT_FPSCR)
  238. || (child->thread.regs == NULL))
  239. #else
  240. index = (unsigned long) addr >> 3;
  241. if ((addr & 7) || (index > PT_FPSCR))
  242. #endif
  243. break;
  244. #ifdef CONFIG_PPC32
  245. CHECK_FULL_REGS(child->thread.regs);
  246. #endif
  247. if (index < PT_FPR0) {
  248. tmp = get_reg(child, (int) index);
  249. } else {
  250. flush_fp_to_thread(child);
  251. tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
  252. }
  253. ret = put_user(tmp,(unsigned long __user *) data);
  254. break;
  255. }
  256. /* If I and D space are separate, this will have to be fixed. */
  257. case PTRACE_POKETEXT: /* write the word at location addr. */
  258. case PTRACE_POKEDATA:
  259. ret = 0;
  260. if (access_process_vm(child, addr, &data, sizeof(data), 1)
  261. == sizeof(data))
  262. break;
  263. ret = -EIO;
  264. break;
  265. /* write the word at location addr in the USER area */
  266. case PTRACE_POKEUSR: {
  267. unsigned long index;
  268. ret = -EIO;
  269. /* convert to index and check */
  270. #ifdef CONFIG_PPC32
  271. index = (unsigned long) addr >> 2;
  272. if ((addr & 3) || (index > PT_FPSCR)
  273. || (child->thread.regs == NULL))
  274. #else
  275. index = (unsigned long) addr >> 3;
  276. if ((addr & 7) || (index > PT_FPSCR))
  277. #endif
  278. break;
  279. #ifdef CONFIG_PPC32
  280. CHECK_FULL_REGS(child->thread.regs);
  281. #endif
  282. if (index == PT_ORIG_R3)
  283. break;
  284. if (index < PT_FPR0) {
  285. ret = put_reg(child, index, data);
  286. } else {
  287. flush_fp_to_thread(child);
  288. ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
  289. ret = 0;
  290. }
  291. break;
  292. }
  293. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  294. case PTRACE_CONT: { /* restart after signal. */
  295. ret = -EIO;
  296. if (!valid_signal(data))
  297. break;
  298. if (request == PTRACE_SYSCALL)
  299. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  300. else
  301. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  302. child->exit_code = data;
  303. /* make sure the single step bit is not set. */
  304. clear_single_step(child);
  305. wake_up_process(child);
  306. ret = 0;
  307. break;
  308. }
  309. /*
  310. * make the child exit. Best I can do is send it a sigkill.
  311. * perhaps it should be put in the status that it wants to
  312. * exit.
  313. */
  314. case PTRACE_KILL: {
  315. ret = 0;
  316. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  317. break;
  318. child->exit_code = SIGKILL;
  319. /* make sure the single step bit is not set. */
  320. clear_single_step(child);
  321. wake_up_process(child);
  322. break;
  323. }
  324. case PTRACE_SINGLESTEP: { /* set the trap flag. */
  325. ret = -EIO;
  326. if (!valid_signal(data))
  327. break;
  328. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  329. set_single_step(child);
  330. child->exit_code = data;
  331. /* give it a chance to run. */
  332. wake_up_process(child);
  333. ret = 0;
  334. break;
  335. }
  336. #ifdef CONFIG_PPC64
  337. case PTRACE_GET_DEBUGREG: {
  338. ret = -EINVAL;
  339. /* We only support one DABR and no IABRS at the moment */
  340. if (addr > 0)
  341. break;
  342. ret = put_user(child->thread.dabr,
  343. (unsigned long __user *)data);
  344. break;
  345. }
  346. case PTRACE_SET_DEBUGREG:
  347. ret = ptrace_set_debugreg(child, addr, data);
  348. break;
  349. #endif
  350. case PTRACE_DETACH:
  351. ret = ptrace_detach(child, data);
  352. break;
  353. case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
  354. int i;
  355. unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
  356. unsigned long __user *tmp = (unsigned long __user *)addr;
  357. for (i = 0; i < 32; i++) {
  358. ret = put_user(*reg, tmp);
  359. if (ret)
  360. break;
  361. reg++;
  362. tmp++;
  363. }
  364. break;
  365. }
  366. case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
  367. int i;
  368. unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
  369. unsigned long __user *tmp = (unsigned long __user *)addr;
  370. for (i = 0; i < 32; i++) {
  371. ret = get_user(*reg, tmp);
  372. if (ret)
  373. break;
  374. reg++;
  375. tmp++;
  376. }
  377. break;
  378. }
  379. case PPC_PTRACE_GETFPREGS: { /* Get FPRs 0 - 31. */
  380. int i;
  381. unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
  382. unsigned long __user *tmp = (unsigned long __user *)addr;
  383. flush_fp_to_thread(child);
  384. for (i = 0; i < 32; i++) {
  385. ret = put_user(*reg, tmp);
  386. if (ret)
  387. break;
  388. reg++;
  389. tmp++;
  390. }
  391. break;
  392. }
  393. case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */
  394. int i;
  395. unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
  396. unsigned long __user *tmp = (unsigned long __user *)addr;
  397. flush_fp_to_thread(child);
  398. for (i = 0; i < 32; i++) {
  399. ret = get_user(*reg, tmp);
  400. if (ret)
  401. break;
  402. reg++;
  403. tmp++;
  404. }
  405. break;
  406. }
  407. #ifdef CONFIG_ALTIVEC
  408. case PTRACE_GETVRREGS:
  409. /* Get the child altivec register state. */
  410. flush_altivec_to_thread(child);
  411. ret = get_vrregs((unsigned long __user *)data, child);
  412. break;
  413. case PTRACE_SETVRREGS:
  414. /* Set the child altivec register state. */
  415. flush_altivec_to_thread(child);
  416. ret = set_vrregs(child, (unsigned long __user *)data);
  417. break;
  418. #endif
  419. #ifdef CONFIG_SPE
  420. case PTRACE_GETEVRREGS:
  421. /* Get the child spe register state. */
  422. if (child->thread.regs->msr & MSR_SPE)
  423. giveup_spe(child);
  424. ret = get_evrregs((unsigned long __user *)data, child);
  425. break;
  426. case PTRACE_SETEVRREGS:
  427. /* Set the child spe register state. */
  428. /* this is to clear the MSR_SPE bit to force a reload
  429. * of register state from memory */
  430. if (child->thread.regs->msr & MSR_SPE)
  431. giveup_spe(child);
  432. ret = set_evrregs(child, (unsigned long __user *)data);
  433. break;
  434. #endif
  435. default:
  436. ret = ptrace_request(child, request, addr, data);
  437. break;
  438. }
  439. return ret;
  440. }
  441. static void do_syscall_trace(void)
  442. {
  443. /* the 0x80 provides a way for the tracing parent to distinguish
  444. between a syscall stop and SIGTRAP delivery */
  445. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  446. ? 0x80 : 0));
  447. /*
  448. * this isn't the same as continuing with a signal, but it will do
  449. * for normal use. strace only continues with a signal if the
  450. * stopping signal is not SIGTRAP. -brl
  451. */
  452. if (current->exit_code) {
  453. send_sig(current->exit_code, current, 1);
  454. current->exit_code = 0;
  455. }
  456. }
  457. void do_syscall_trace_enter(struct pt_regs *regs)
  458. {
  459. secure_computing(regs->gpr[0]);
  460. if (test_thread_flag(TIF_SYSCALL_TRACE)
  461. && (current->ptrace & PT_PTRACED))
  462. do_syscall_trace();
  463. if (unlikely(current->audit_context)) {
  464. #ifdef CONFIG_PPC64
  465. if (!test_thread_flag(TIF_32BIT))
  466. audit_syscall_entry(AUDIT_ARCH_PPC64,
  467. regs->gpr[0],
  468. regs->gpr[3], regs->gpr[4],
  469. regs->gpr[5], regs->gpr[6]);
  470. else
  471. #endif
  472. audit_syscall_entry(AUDIT_ARCH_PPC,
  473. regs->gpr[0],
  474. regs->gpr[3] & 0xffffffff,
  475. regs->gpr[4] & 0xffffffff,
  476. regs->gpr[5] & 0xffffffff,
  477. regs->gpr[6] & 0xffffffff);
  478. }
  479. }
  480. void do_syscall_trace_leave(struct pt_regs *regs)
  481. {
  482. if (unlikely(current->audit_context))
  483. audit_syscall_exit((regs->ccr&0x10000000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
  484. regs->result);
  485. if ((test_thread_flag(TIF_SYSCALL_TRACE)
  486. || test_thread_flag(TIF_SINGLESTEP))
  487. && (current->ptrace & PT_PTRACED))
  488. do_syscall_trace();
  489. }