ptrace.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // TODO some minor issues
  2. /*
  3. * This file is subject to the terms and conditions of the GNU General Public
  4. * License. See the file "COPYING" in the main directory of this archive
  5. * for more details.
  6. *
  7. * Copyright (C) 2001 - 2007 Tensilica Inc.
  8. *
  9. * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
  10. * Chris Zankel <chris@zankel.net>
  11. * Scott Foehner<sfoehner@yahoo.com>,
  12. * Kevin Chea
  13. * Marc Gauthier<marc@tensilica.com> <marc@alumni.uwaterloo.ca>
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/sched.h>
  17. #include <linux/mm.h>
  18. #include <linux/errno.h>
  19. #include <linux/ptrace.h>
  20. #include <linux/smp.h>
  21. #include <linux/security.h>
  22. #include <linux/signal.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/page.h>
  25. #include <asm/system.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/ptrace.h>
  28. #include <asm/elf.h>
  29. #include <asm/coprocessor.h>
  30. void user_enable_single_step(struct task_struct *child)
  31. {
  32. child->ptrace |= PT_SINGLESTEP;
  33. }
  34. void user_disable_single_step(struct task_struct *child)
  35. {
  36. child->ptrace &= ~PT_SINGLESTEP;
  37. }
  38. /*
  39. * Called by kernel/ptrace.c when detaching to disable single stepping.
  40. */
  41. void ptrace_disable(struct task_struct *child)
  42. {
  43. /* Nothing to do.. */
  44. }
  45. int ptrace_getregs(struct task_struct *child, void __user *uregs)
  46. {
  47. struct pt_regs *regs = task_pt_regs(child);
  48. xtensa_gregset_t __user *gregset = uregs;
  49. unsigned long wm = regs->wmask;
  50. unsigned long wb = regs->windowbase;
  51. int live, i;
  52. if (!access_ok(VERIFY_WRITE, uregs, sizeof(xtensa_gregset_t)))
  53. return -EIO;
  54. __put_user(regs->pc, &gregset->pc);
  55. __put_user(regs->ps & ~(1 << PS_EXCM_BIT), &gregset->ps);
  56. __put_user(regs->lbeg, &gregset->lbeg);
  57. __put_user(regs->lend, &gregset->lend);
  58. __put_user(regs->lcount, &gregset->lcount);
  59. __put_user(regs->windowstart, &gregset->windowstart);
  60. __put_user(regs->windowbase, &gregset->windowbase);
  61. live = (wm & 2) ? 4 : (wm & 4) ? 8 : (wm & 8) ? 12 : 16;
  62. for (i = 0; i < live; i++)
  63. __put_user(regs->areg[i],gregset->a+((wb*4+i)%XCHAL_NUM_AREGS));
  64. for (i = XCHAL_NUM_AREGS - (wm >> 4) * 4; i < XCHAL_NUM_AREGS; i++)
  65. __put_user(regs->areg[i],gregset->a+((wb*4+i)%XCHAL_NUM_AREGS));
  66. return 0;
  67. }
  68. int ptrace_setregs(struct task_struct *child, void __user *uregs)
  69. {
  70. struct pt_regs *regs = task_pt_regs(child);
  71. xtensa_gregset_t *gregset = uregs;
  72. const unsigned long ps_mask = PS_CALLINC_MASK | PS_OWB_MASK;
  73. unsigned long ps;
  74. unsigned long wb;
  75. if (!access_ok(VERIFY_WRITE, uregs, sizeof(xtensa_gregset_t)))
  76. return -EIO;
  77. __get_user(regs->pc, &gregset->pc);
  78. __get_user(ps, &gregset->ps);
  79. __get_user(regs->lbeg, &gregset->lbeg);
  80. __get_user(regs->lend, &gregset->lend);
  81. __get_user(regs->lcount, &gregset->lcount);
  82. __get_user(regs->windowstart, &gregset->windowstart);
  83. __get_user(wb, &gregset->windowbase);
  84. regs->ps = (regs->ps & ~ps_mask) | (ps & ps_mask) | (1 << PS_EXCM_BIT);
  85. if (wb >= XCHAL_NUM_AREGS / 4)
  86. return -EFAULT;
  87. regs->windowbase = wb;
  88. if (wb != 0 && __copy_from_user(regs->areg + XCHAL_NUM_AREGS - wb * 4,
  89. gregset->a, wb * 16))
  90. return -EFAULT;
  91. if (__copy_from_user(regs->areg, gregset->a + wb*4, (WSBITS-wb) * 16))
  92. return -EFAULT;
  93. return 0;
  94. }
  95. int ptrace_getxregs(struct task_struct *child, void __user *uregs)
  96. {
  97. struct pt_regs *regs = task_pt_regs(child);
  98. struct thread_info *ti = task_thread_info(child);
  99. elf_xtregs_t __user *xtregs = uregs;
  100. int ret = 0;
  101. if (!access_ok(VERIFY_WRITE, uregs, sizeof(elf_xtregs_t)))
  102. return -EIO;
  103. #if XTENSA_HAVE_COPROCESSORS
  104. /* Flush all coprocessor registers to memory. */
  105. coprocessor_flush_all(ti);
  106. ret |= __copy_to_user(&xtregs->cp0, &ti->xtregs_cp,
  107. sizeof(xtregs_coprocessor_t));
  108. #endif
  109. ret |= __copy_to_user(&xtregs->opt, &regs->xtregs_opt,
  110. sizeof(xtregs->opt));
  111. ret |= __copy_to_user(&xtregs->user,&ti->xtregs_user,
  112. sizeof(xtregs->user));
  113. return ret ? -EFAULT : 0;
  114. }
  115. int ptrace_setxregs(struct task_struct *child, void __user *uregs)
  116. {
  117. struct thread_info *ti = task_thread_info(child);
  118. struct pt_regs *regs = task_pt_regs(child);
  119. elf_xtregs_t *xtregs = uregs;
  120. int ret = 0;
  121. if (!access_ok(VERIFY_READ, uregs, sizeof(elf_xtregs_t)))
  122. return -EFAULT;
  123. #if XTENSA_HAVE_COPROCESSORS
  124. /* Flush all coprocessors before we overwrite them. */
  125. coprocessor_flush_all(ti);
  126. coprocessor_release_all(ti);
  127. ret |= __copy_from_user(&ti->xtregs_cp, &xtregs->cp0,
  128. sizeof(xtregs_coprocessor_t));
  129. #endif
  130. ret |= __copy_from_user(&regs->xtregs_opt, &xtregs->opt,
  131. sizeof(xtregs->opt));
  132. ret |= __copy_from_user(&ti->xtregs_user, &xtregs->user,
  133. sizeof(xtregs->user));
  134. return ret ? -EFAULT : 0;
  135. }
  136. int ptrace_peekusr(struct task_struct *child, long regno, long __user *ret)
  137. {
  138. struct pt_regs *regs;
  139. unsigned long tmp;
  140. regs = task_pt_regs(child);
  141. tmp = 0; /* Default return value. */
  142. switch(regno) {
  143. case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
  144. tmp = regs->areg[regno - REG_AR_BASE];
  145. break;
  146. case REG_A_BASE ... REG_A_BASE + 15:
  147. tmp = regs->areg[regno - REG_A_BASE];
  148. break;
  149. case REG_PC:
  150. tmp = regs->pc;
  151. break;
  152. case REG_PS:
  153. /* Note: PS.EXCM is not set while user task is running;
  154. * its being set in regs is for exception handling
  155. * convenience. */
  156. tmp = (regs->ps & ~(1 << PS_EXCM_BIT));
  157. break;
  158. case REG_WB:
  159. break; /* tmp = 0 */
  160. case REG_WS:
  161. {
  162. unsigned long wb = regs->windowbase;
  163. unsigned long ws = regs->windowstart;
  164. tmp = ((ws>>wb) | (ws<<(WSBITS-wb))) & ((1<<WSBITS)-1);
  165. break;
  166. }
  167. case REG_LBEG:
  168. tmp = regs->lbeg;
  169. break;
  170. case REG_LEND:
  171. tmp = regs->lend;
  172. break;
  173. case REG_LCOUNT:
  174. tmp = regs->lcount;
  175. break;
  176. case REG_SAR:
  177. tmp = regs->sar;
  178. break;
  179. case SYSCALL_NR:
  180. tmp = regs->syscall;
  181. break;
  182. default:
  183. return -EIO;
  184. }
  185. return put_user(tmp, ret);
  186. }
  187. int ptrace_pokeusr(struct task_struct *child, long regno, long val)
  188. {
  189. struct pt_regs *regs;
  190. regs = task_pt_regs(child);
  191. switch (regno) {
  192. case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
  193. regs->areg[regno - REG_AR_BASE] = val;
  194. break;
  195. case REG_A_BASE ... REG_A_BASE + 15:
  196. regs->areg[regno - REG_A_BASE] = val;
  197. break;
  198. case REG_PC:
  199. regs->pc = val;
  200. break;
  201. case SYSCALL_NR:
  202. regs->syscall = val;
  203. break;
  204. default:
  205. return -EIO;
  206. }
  207. return 0;
  208. }
  209. long arch_ptrace(struct task_struct *child, long request,
  210. unsigned long addr, unsigned long data)
  211. {
  212. int ret = -EPERM;
  213. void __user *datap = (void __user *) data;
  214. switch (request) {
  215. case PTRACE_PEEKTEXT: /* read word at location addr. */
  216. case PTRACE_PEEKDATA:
  217. ret = generic_ptrace_peekdata(child, addr, data);
  218. break;
  219. case PTRACE_PEEKUSR: /* read register specified by addr. */
  220. ret = ptrace_peekusr(child, addr, datap);
  221. break;
  222. case PTRACE_POKETEXT: /* write the word at location addr. */
  223. case PTRACE_POKEDATA:
  224. ret = generic_ptrace_pokedata(child, addr, data);
  225. break;
  226. case PTRACE_POKEUSR: /* write register specified by addr. */
  227. ret = ptrace_pokeusr(child, addr, data);
  228. break;
  229. case PTRACE_GETREGS:
  230. ret = ptrace_getregs(child, datap);
  231. break;
  232. case PTRACE_SETREGS:
  233. ret = ptrace_setregs(child, datap);
  234. break;
  235. case PTRACE_GETXTREGS:
  236. ret = ptrace_getxregs(child, datap);
  237. break;
  238. case PTRACE_SETXTREGS:
  239. ret = ptrace_setxregs(child, datap);
  240. break;
  241. default:
  242. ret = ptrace_request(child, request, addr, data);
  243. break;
  244. }
  245. return ret;
  246. }
  247. void do_syscall_trace(void)
  248. {
  249. /*
  250. * The 0x80 provides a way for the tracing parent to distinguish
  251. * between a syscall stop and SIGTRAP delivery
  252. */
  253. ptrace_notify(SIGTRAP|((current->ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
  254. /*
  255. * this isn't the same as continuing with a signal, but it will do
  256. * for normal use. strace only continues with a signal if the
  257. * stopping signal is not SIGTRAP. -brl
  258. */
  259. if (current->exit_code) {
  260. send_sig(current->exit_code, current, 1);
  261. current->exit_code = 0;
  262. }
  263. }
  264. void do_syscall_trace_enter(struct pt_regs *regs)
  265. {
  266. if (test_thread_flag(TIF_SYSCALL_TRACE)
  267. && (current->ptrace & PT_PTRACED))
  268. do_syscall_trace();
  269. #if 0
  270. if (unlikely(current->audit_context))
  271. audit_syscall_entry(current, AUDIT_ARCH_XTENSA..);
  272. #endif
  273. }
  274. void do_syscall_trace_leave(struct pt_regs *regs)
  275. {
  276. if ((test_thread_flag(TIF_SYSCALL_TRACE))
  277. && (current->ptrace & PT_PTRACED))
  278. do_syscall_trace();
  279. }