ptrace.c 16 KB

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