ptrace.c 15 KB

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