ptrace.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. #ifdef CONFIG_PPC32
  314. CHECK_FULL_REGS(child->thread.regs);
  315. #endif
  316. if (index < PT_FPR0) {
  317. tmp = ptrace_get_reg(child, (int) index);
  318. } else {
  319. flush_fp_to_thread(child);
  320. tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
  321. }
  322. ret = put_user(tmp,(unsigned long __user *) data);
  323. break;
  324. }
  325. /* If I and D space are separate, this will have to be fixed. */
  326. case PTRACE_POKETEXT: /* write the word at location addr. */
  327. case PTRACE_POKEDATA:
  328. ret = 0;
  329. if (access_process_vm(child, addr, &data, sizeof(data), 1)
  330. == sizeof(data))
  331. break;
  332. ret = -EIO;
  333. break;
  334. /* write the word at location addr in the USER area */
  335. case PTRACE_POKEUSR: {
  336. unsigned long index;
  337. ret = -EIO;
  338. /* convert to index and check */
  339. #ifdef CONFIG_PPC32
  340. index = (unsigned long) addr >> 2;
  341. if ((addr & 3) || (index > PT_FPSCR)
  342. || (child->thread.regs == NULL))
  343. #else
  344. index = (unsigned long) addr >> 3;
  345. if ((addr & 7) || (index > PT_FPSCR))
  346. #endif
  347. break;
  348. #ifdef CONFIG_PPC32
  349. CHECK_FULL_REGS(child->thread.regs);
  350. #endif
  351. if (index == PT_ORIG_R3)
  352. break;
  353. if (index < PT_FPR0) {
  354. ret = ptrace_put_reg(child, index, data);
  355. } else {
  356. flush_fp_to_thread(child);
  357. ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
  358. ret = 0;
  359. }
  360. break;
  361. }
  362. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  363. case PTRACE_CONT: { /* restart after signal. */
  364. ret = -EIO;
  365. if (!valid_signal(data))
  366. break;
  367. if (request == PTRACE_SYSCALL)
  368. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  369. else
  370. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  371. child->exit_code = data;
  372. /* make sure the single step bit is not set. */
  373. clear_single_step(child);
  374. wake_up_process(child);
  375. ret = 0;
  376. break;
  377. }
  378. /*
  379. * make the child exit. Best I can do is send it a sigkill.
  380. * perhaps it should be put in the status that it wants to
  381. * exit.
  382. */
  383. case PTRACE_KILL: {
  384. ret = 0;
  385. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  386. break;
  387. child->exit_code = SIGKILL;
  388. /* make sure the single step bit is not set. */
  389. clear_single_step(child);
  390. wake_up_process(child);
  391. break;
  392. }
  393. case PTRACE_SINGLESTEP: { /* set the trap flag. */
  394. ret = -EIO;
  395. if (!valid_signal(data))
  396. break;
  397. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  398. set_single_step(child);
  399. child->exit_code = data;
  400. /* give it a chance to run. */
  401. wake_up_process(child);
  402. ret = 0;
  403. break;
  404. }
  405. #ifdef CONFIG_PPC64
  406. case PTRACE_GET_DEBUGREG: {
  407. ret = -EINVAL;
  408. /* We only support one DABR and no IABRS at the moment */
  409. if (addr > 0)
  410. break;
  411. ret = put_user(child->thread.dabr,
  412. (unsigned long __user *)data);
  413. break;
  414. }
  415. case PTRACE_SET_DEBUGREG:
  416. ret = ptrace_set_debugreg(child, addr, data);
  417. break;
  418. #endif
  419. case PTRACE_DETACH:
  420. ret = ptrace_detach(child, data);
  421. break;
  422. #ifdef CONFIG_PPC64
  423. case PTRACE_GETREGS64:
  424. #endif
  425. case PTRACE_GETREGS: { /* Get all pt_regs from the child. */
  426. int ui;
  427. if (!access_ok(VERIFY_WRITE, (void __user *)data,
  428. sizeof(struct pt_regs))) {
  429. ret = -EIO;
  430. break;
  431. }
  432. ret = 0;
  433. for (ui = 0; ui < PT_REGS_COUNT; ui ++) {
  434. ret |= __put_user(ptrace_get_reg(child, ui),
  435. (unsigned long __user *) data);
  436. data += sizeof(long);
  437. }
  438. break;
  439. }
  440. #ifdef CONFIG_PPC64
  441. case PTRACE_SETREGS64:
  442. #endif
  443. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  444. unsigned long tmp;
  445. int ui;
  446. if (!access_ok(VERIFY_READ, (void __user *)data,
  447. sizeof(struct pt_regs))) {
  448. ret = -EIO;
  449. break;
  450. }
  451. ret = 0;
  452. for (ui = 0; ui < PT_REGS_COUNT; ui ++) {
  453. ret = __get_user(tmp, (unsigned long __user *) data);
  454. if (ret)
  455. break;
  456. ptrace_put_reg(child, ui, tmp);
  457. data += sizeof(long);
  458. }
  459. break;
  460. }
  461. case PTRACE_GETFPREGS: { /* Get the child FPU state (FPR0...31 + FPSCR) */
  462. flush_fp_to_thread(child);
  463. ret = get_fpregs((void __user *)data, child, 1);
  464. break;
  465. }
  466. case PTRACE_SETFPREGS: { /* Set the child FPU state (FPR0...31 + FPSCR) */
  467. flush_fp_to_thread(child);
  468. ret = set_fpregs((void __user *)data, child, 1);
  469. break;
  470. }
  471. #ifdef CONFIG_ALTIVEC
  472. case PTRACE_GETVRREGS:
  473. /* Get the child altivec register state. */
  474. flush_altivec_to_thread(child);
  475. ret = get_vrregs((unsigned long __user *)data, child);
  476. break;
  477. case PTRACE_SETVRREGS:
  478. /* Set the child altivec register state. */
  479. flush_altivec_to_thread(child);
  480. ret = set_vrregs(child, (unsigned long __user *)data);
  481. break;
  482. #endif
  483. #ifdef CONFIG_SPE
  484. case PTRACE_GETEVRREGS:
  485. /* Get the child spe register state. */
  486. if (child->thread.regs->msr & MSR_SPE)
  487. giveup_spe(child);
  488. ret = get_evrregs((unsigned long __user *)data, child);
  489. break;
  490. case PTRACE_SETEVRREGS:
  491. /* Set the child spe register state. */
  492. /* this is to clear the MSR_SPE bit to force a reload
  493. * of register state from memory */
  494. if (child->thread.regs->msr & MSR_SPE)
  495. giveup_spe(child);
  496. ret = set_evrregs(child, (unsigned long __user *)data);
  497. break;
  498. #endif
  499. /* Old reverse args ptrace callss */
  500. case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
  501. case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
  502. case PPC_PTRACE_GETFPREGS: /* Get FPRs 0 - 31. */
  503. case PPC_PTRACE_SETFPREGS: /* Get FPRs 0 - 31. */
  504. ret = arch_ptrace_old(child, request, addr, data);
  505. break;
  506. default:
  507. ret = ptrace_request(child, request, addr, data);
  508. break;
  509. }
  510. return ret;
  511. }
  512. static void do_syscall_trace(void)
  513. {
  514. /* the 0x80 provides a way for the tracing parent to distinguish
  515. between a syscall stop and SIGTRAP delivery */
  516. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  517. ? 0x80 : 0));
  518. /*
  519. * this isn't the same as continuing with a signal, but it will do
  520. * for normal use. strace only continues with a signal if the
  521. * stopping signal is not SIGTRAP. -brl
  522. */
  523. if (current->exit_code) {
  524. send_sig(current->exit_code, current, 1);
  525. current->exit_code = 0;
  526. }
  527. }
  528. void do_syscall_trace_enter(struct pt_regs *regs)
  529. {
  530. secure_computing(regs->gpr[0]);
  531. if (test_thread_flag(TIF_SYSCALL_TRACE)
  532. && (current->ptrace & PT_PTRACED))
  533. do_syscall_trace();
  534. if (unlikely(current->audit_context)) {
  535. #ifdef CONFIG_PPC64
  536. if (!test_thread_flag(TIF_32BIT))
  537. audit_syscall_entry(AUDIT_ARCH_PPC64,
  538. regs->gpr[0],
  539. regs->gpr[3], regs->gpr[4],
  540. regs->gpr[5], regs->gpr[6]);
  541. else
  542. #endif
  543. audit_syscall_entry(AUDIT_ARCH_PPC,
  544. regs->gpr[0],
  545. regs->gpr[3] & 0xffffffff,
  546. regs->gpr[4] & 0xffffffff,
  547. regs->gpr[5] & 0xffffffff,
  548. regs->gpr[6] & 0xffffffff);
  549. }
  550. }
  551. void do_syscall_trace_leave(struct pt_regs *regs)
  552. {
  553. if (unlikely(current->audit_context))
  554. audit_syscall_exit((regs->ccr&0x10000000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
  555. regs->result);
  556. if ((test_thread_flag(TIF_SYSCALL_TRACE)
  557. || test_thread_flag(TIF_SINGLESTEP))
  558. && (current->ptrace & PT_PTRACED))
  559. do_syscall_trace();
  560. }