ptrace.c 15 KB

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