ptrace.c 16 KB

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