ptrace.c 13 KB

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