ptrace.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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/config.h>
  18. #include <linux/kernel.h>
  19. #include <linux/sched.h>
  20. #include <linux/mm.h>
  21. #include <linux/smp.h>
  22. #include <linux/smp_lock.h>
  23. #include <linux/errno.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/user.h>
  26. #include <linux/security.h>
  27. #include <linux/signal.h>
  28. #include <linux/seccomp.h>
  29. #include <linux/audit.h>
  30. #ifdef CONFIG_PPC32
  31. #include <linux/module.h>
  32. #endif
  33. #include <asm/uaccess.h>
  34. #include <asm/page.h>
  35. #include <asm/pgtable.h>
  36. #include <asm/system.h>
  37. #ifdef CONFIG_PPC64
  38. #include "ptrace-common.h"
  39. #endif
  40. #ifdef CONFIG_PPC32
  41. /*
  42. * Set of msr bits that gdb can change on behalf of a process.
  43. */
  44. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  45. #define MSR_DEBUGCHANGE 0
  46. #else
  47. #define MSR_DEBUGCHANGE (MSR_SE | MSR_BE)
  48. #endif
  49. #endif /* CONFIG_PPC32 */
  50. /*
  51. * does not yet catch signals sent when the child dies.
  52. * in exit.c or in signal.c.
  53. */
  54. #ifdef CONFIG_PPC32
  55. /*
  56. * Get contents of register REGNO in task TASK.
  57. */
  58. static inline unsigned long get_reg(struct task_struct *task, int regno)
  59. {
  60. if (regno < sizeof(struct pt_regs) / sizeof(unsigned long)
  61. && task->thread.regs != NULL)
  62. return ((unsigned long *)task->thread.regs)[regno];
  63. return (0);
  64. }
  65. /*
  66. * Write contents of register REGNO in task TASK.
  67. */
  68. static inline int put_reg(struct task_struct *task, int regno,
  69. unsigned long data)
  70. {
  71. if (regno <= PT_MQ && task->thread.regs != NULL) {
  72. if (regno == PT_MSR)
  73. data = (data & MSR_DEBUGCHANGE)
  74. | (task->thread.regs->msr & ~MSR_DEBUGCHANGE);
  75. ((unsigned long *)task->thread.regs)[regno] = data;
  76. return 0;
  77. }
  78. return -EIO;
  79. }
  80. #ifdef CONFIG_ALTIVEC
  81. /*
  82. * Get contents of AltiVec register state in task TASK
  83. */
  84. static inline int get_vrregs(unsigned long __user *data, struct task_struct *task)
  85. {
  86. int i, j;
  87. if (!access_ok(VERIFY_WRITE, data, 133 * sizeof(unsigned long)))
  88. return -EFAULT;
  89. /* copy AltiVec registers VR[0] .. VR[31] */
  90. for (i = 0; i < 32; i++)
  91. for (j = 0; j < 4; j++, data++)
  92. if (__put_user(task->thread.vr[i].u[j], data))
  93. return -EFAULT;
  94. /* copy VSCR */
  95. for (i = 0; i < 4; i++, data++)
  96. if (__put_user(task->thread.vscr.u[i], data))
  97. return -EFAULT;
  98. /* copy VRSAVE */
  99. if (__put_user(task->thread.vrsave, data))
  100. return -EFAULT;
  101. return 0;
  102. }
  103. /*
  104. * Write contents of AltiVec register state into task TASK.
  105. */
  106. static inline int set_vrregs(struct task_struct *task, unsigned long __user *data)
  107. {
  108. int i, j;
  109. if (!access_ok(VERIFY_READ, data, 133 * sizeof(unsigned long)))
  110. return -EFAULT;
  111. /* copy AltiVec registers VR[0] .. VR[31] */
  112. for (i = 0; i < 32; i++)
  113. for (j = 0; j < 4; j++, data++)
  114. if (__get_user(task->thread.vr[i].u[j], data))
  115. return -EFAULT;
  116. /* copy VSCR */
  117. for (i = 0; i < 4; i++, data++)
  118. if (__get_user(task->thread.vscr.u[i], data))
  119. return -EFAULT;
  120. /* copy VRSAVE */
  121. if (__get_user(task->thread.vrsave, data))
  122. return -EFAULT;
  123. return 0;
  124. }
  125. #endif
  126. #ifdef CONFIG_SPE
  127. /*
  128. * For get_evrregs/set_evrregs functions 'data' has the following layout:
  129. *
  130. * struct {
  131. * u32 evr[32];
  132. * u64 acc;
  133. * u32 spefscr;
  134. * }
  135. */
  136. /*
  137. * Get contents of SPE register state in task TASK.
  138. */
  139. static inline int get_evrregs(unsigned long *data, struct task_struct *task)
  140. {
  141. int i;
  142. if (!access_ok(VERIFY_WRITE, data, 35 * sizeof(unsigned long)))
  143. return -EFAULT;
  144. /* copy SPEFSCR */
  145. if (__put_user(task->thread.spefscr, &data[34]))
  146. return -EFAULT;
  147. /* copy SPE registers EVR[0] .. EVR[31] */
  148. for (i = 0; i < 32; i++, data++)
  149. if (__put_user(task->thread.evr[i], data))
  150. return -EFAULT;
  151. /* copy ACC */
  152. if (__put_user64(task->thread.acc, (unsigned long long *)data))
  153. return -EFAULT;
  154. return 0;
  155. }
  156. /*
  157. * Write contents of SPE register state into task TASK.
  158. */
  159. static inline int set_evrregs(struct task_struct *task, unsigned long *data)
  160. {
  161. int i;
  162. if (!access_ok(VERIFY_READ, data, 35 * sizeof(unsigned long)))
  163. return -EFAULT;
  164. /* copy SPEFSCR */
  165. if (__get_user(task->thread.spefscr, &data[34]))
  166. return -EFAULT;
  167. /* copy SPE registers EVR[0] .. EVR[31] */
  168. for (i = 0; i < 32; i++, data++)
  169. if (__get_user(task->thread.evr[i], data))
  170. return -EFAULT;
  171. /* copy ACC */
  172. if (__get_user64(task->thread.acc, (unsigned long long*)data))
  173. return -EFAULT;
  174. return 0;
  175. }
  176. #endif /* CONFIG_SPE */
  177. static inline void
  178. set_single_step(struct task_struct *task)
  179. {
  180. struct pt_regs *regs = task->thread.regs;
  181. if (regs != NULL) {
  182. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  183. task->thread.dbcr0 = DBCR0_IDM | DBCR0_IC;
  184. regs->msr |= MSR_DE;
  185. #else
  186. regs->msr |= MSR_SE;
  187. #endif
  188. }
  189. }
  190. static inline void
  191. clear_single_step(struct task_struct *task)
  192. {
  193. struct pt_regs *regs = task->thread.regs;
  194. if (regs != NULL) {
  195. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  196. task->thread.dbcr0 = 0;
  197. regs->msr &= ~MSR_DE;
  198. #else
  199. regs->msr &= ~MSR_SE;
  200. #endif
  201. }
  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. #ifdef CONFIG_PPC64
  354. case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
  355. int i;
  356. unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
  357. unsigned long __user *tmp = (unsigned long __user *)addr;
  358. for (i = 0; i < 32; i++) {
  359. ret = put_user(*reg, tmp);
  360. if (ret)
  361. break;
  362. reg++;
  363. tmp++;
  364. }
  365. break;
  366. }
  367. case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
  368. int i;
  369. unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
  370. unsigned long __user *tmp = (unsigned long __user *)addr;
  371. for (i = 0; i < 32; i++) {
  372. ret = get_user(*reg, tmp);
  373. if (ret)
  374. break;
  375. reg++;
  376. tmp++;
  377. }
  378. break;
  379. }
  380. case PPC_PTRACE_GETFPREGS: { /* Get FPRs 0 - 31. */
  381. int i;
  382. unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
  383. unsigned long __user *tmp = (unsigned long __user *)addr;
  384. flush_fp_to_thread(child);
  385. for (i = 0; i < 32; i++) {
  386. ret = put_user(*reg, tmp);
  387. if (ret)
  388. break;
  389. reg++;
  390. tmp++;
  391. }
  392. break;
  393. }
  394. case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */
  395. int i;
  396. unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
  397. unsigned long __user *tmp = (unsigned long __user *)addr;
  398. flush_fp_to_thread(child);
  399. for (i = 0; i < 32; i++) {
  400. ret = get_user(*reg, tmp);
  401. if (ret)
  402. break;
  403. reg++;
  404. tmp++;
  405. }
  406. break;
  407. }
  408. #endif /* CONFIG_PPC64 */
  409. #ifdef CONFIG_ALTIVEC
  410. case PTRACE_GETVRREGS:
  411. /* Get the child altivec register state. */
  412. flush_altivec_to_thread(child);
  413. ret = get_vrregs((unsigned long __user *)data, child);
  414. break;
  415. case PTRACE_SETVRREGS:
  416. /* Set the child altivec register state. */
  417. flush_altivec_to_thread(child);
  418. ret = set_vrregs(child, (unsigned long __user *)data);
  419. break;
  420. #endif
  421. #ifdef CONFIG_SPE
  422. case PTRACE_GETEVRREGS:
  423. /* Get the child spe register state. */
  424. if (child->thread.regs->msr & MSR_SPE)
  425. giveup_spe(child);
  426. ret = get_evrregs((unsigned long __user *)data, child);
  427. break;
  428. case PTRACE_SETEVRREGS:
  429. /* Set the child spe register state. */
  430. /* this is to clear the MSR_SPE bit to force a reload
  431. * of register state from memory */
  432. if (child->thread.regs->msr & MSR_SPE)
  433. giveup_spe(child);
  434. ret = set_evrregs(child, (unsigned long __user *)data);
  435. break;
  436. #endif
  437. default:
  438. ret = ptrace_request(child, request, addr, data);
  439. break;
  440. }
  441. return ret;
  442. }
  443. static void do_syscall_trace(void)
  444. {
  445. /* the 0x80 provides a way for the tracing parent to distinguish
  446. between a syscall stop and SIGTRAP delivery */
  447. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  448. ? 0x80 : 0));
  449. /*
  450. * this isn't the same as continuing with a signal, but it will do
  451. * for normal use. strace only continues with a signal if the
  452. * stopping signal is not SIGTRAP. -brl
  453. */
  454. if (current->exit_code) {
  455. send_sig(current->exit_code, current, 1);
  456. current->exit_code = 0;
  457. }
  458. }
  459. void do_syscall_trace_enter(struct pt_regs *regs)
  460. {
  461. #ifdef CONFIG_PPC64
  462. secure_computing(regs->gpr[0]);
  463. #endif
  464. if (test_thread_flag(TIF_SYSCALL_TRACE)
  465. && (current->ptrace & PT_PTRACED))
  466. do_syscall_trace();
  467. if (unlikely(current->audit_context))
  468. audit_syscall_entry(current,
  469. #ifdef CONFIG_PPC32
  470. AUDIT_ARCH_PPC,
  471. #else
  472. test_thread_flag(TIF_32BIT)?AUDIT_ARCH_PPC:AUDIT_ARCH_PPC64,
  473. #endif
  474. regs->gpr[0],
  475. regs->gpr[3], regs->gpr[4],
  476. regs->gpr[5], regs->gpr[6]);
  477. }
  478. void do_syscall_trace_leave(struct pt_regs *regs)
  479. {
  480. #ifdef CONFIG_PPC32
  481. secure_computing(regs->gpr[0]);
  482. #endif
  483. if (unlikely(current->audit_context))
  484. audit_syscall_exit(current,
  485. (regs->ccr&0x1000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
  486. regs->result);
  487. if ((test_thread_flag(TIF_SYSCALL_TRACE)
  488. || test_thread_flag(TIF_SINGLESTEP))
  489. && (current->ptrace & PT_PTRACED))
  490. do_syscall_trace();
  491. }
  492. #ifdef CONFIG_PPC32
  493. EXPORT_SYMBOL(do_syscall_trace_enter);
  494. EXPORT_SYMBOL(do_syscall_trace_leave);
  495. #endif