ptrace.c 16 KB

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