ptrace_32.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /* ptrace.c: Sparc process tracing support.
  2. *
  3. * Copyright (C) 1996, 2008 David S. Miller (davem@davemloft.net)
  4. *
  5. * Based upon code written by Ross Biro, Linus Torvalds, Bob Manson,
  6. * and David Mosberger.
  7. *
  8. * Added Linux support -miguel (weird, eh?, the original code was meant
  9. * to emulate SunOS).
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/mm.h>
  14. #include <linux/errno.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/user.h>
  17. #include <linux/smp.h>
  18. #include <linux/security.h>
  19. #include <linux/signal.h>
  20. #include <linux/regset.h>
  21. #include <linux/elf.h>
  22. #include <linux/tracehook.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/system.h>
  25. #include <asm/uaccess.h>
  26. /* #define ALLOW_INIT_TRACING */
  27. /*
  28. * Called by kernel/ptrace.c when detaching..
  29. *
  30. * Make sure single step bits etc are not set.
  31. */
  32. void ptrace_disable(struct task_struct *child)
  33. {
  34. /* nothing to do */
  35. }
  36. enum sparc_regset {
  37. REGSET_GENERAL,
  38. REGSET_FP,
  39. };
  40. static int genregs32_get(struct task_struct *target,
  41. const struct user_regset *regset,
  42. unsigned int pos, unsigned int count,
  43. void *kbuf, void __user *ubuf)
  44. {
  45. const struct pt_regs *regs = target->thread.kregs;
  46. unsigned long __user *reg_window;
  47. unsigned long *k = kbuf;
  48. unsigned long __user *u = ubuf;
  49. unsigned long reg;
  50. if (target == current)
  51. flush_user_windows();
  52. pos /= sizeof(reg);
  53. count /= sizeof(reg);
  54. if (kbuf) {
  55. for (; count > 0 && pos < 16; count--)
  56. *k++ = regs->u_regs[pos++];
  57. reg_window = (unsigned long __user *) regs->u_regs[UREG_I6];
  58. reg_window -= 16;
  59. for (; count > 0 && pos < 32; count--) {
  60. if (get_user(*k++, &reg_window[pos++]))
  61. return -EFAULT;
  62. }
  63. } else {
  64. for (; count > 0 && pos < 16; count--) {
  65. if (put_user(regs->u_regs[pos++], u++))
  66. return -EFAULT;
  67. }
  68. reg_window = (unsigned long __user *) regs->u_regs[UREG_I6];
  69. reg_window -= 16;
  70. for (; count > 0 && pos < 32; count--) {
  71. if (get_user(reg, &reg_window[pos++]) ||
  72. put_user(reg, u++))
  73. return -EFAULT;
  74. }
  75. }
  76. while (count > 0) {
  77. switch (pos) {
  78. case 32: /* PSR */
  79. reg = regs->psr;
  80. break;
  81. case 33: /* PC */
  82. reg = regs->pc;
  83. break;
  84. case 34: /* NPC */
  85. reg = regs->npc;
  86. break;
  87. case 35: /* Y */
  88. reg = regs->y;
  89. break;
  90. case 36: /* WIM */
  91. case 37: /* TBR */
  92. reg = 0;
  93. break;
  94. default:
  95. goto finish;
  96. }
  97. if (kbuf)
  98. *k++ = reg;
  99. else if (put_user(reg, u++))
  100. return -EFAULT;
  101. pos++;
  102. count--;
  103. }
  104. finish:
  105. pos *= sizeof(reg);
  106. count *= sizeof(reg);
  107. return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  108. 38 * sizeof(reg), -1);
  109. }
  110. static int genregs32_set(struct task_struct *target,
  111. const struct user_regset *regset,
  112. unsigned int pos, unsigned int count,
  113. const void *kbuf, const void __user *ubuf)
  114. {
  115. struct pt_regs *regs = target->thread.kregs;
  116. unsigned long __user *reg_window;
  117. const unsigned long *k = kbuf;
  118. const unsigned long __user *u = ubuf;
  119. unsigned long reg;
  120. if (target == current)
  121. flush_user_windows();
  122. pos /= sizeof(reg);
  123. count /= sizeof(reg);
  124. if (kbuf) {
  125. for (; count > 0 && pos < 16; count--)
  126. regs->u_regs[pos++] = *k++;
  127. reg_window = (unsigned long __user *) regs->u_regs[UREG_I6];
  128. reg_window -= 16;
  129. for (; count > 0 && pos < 32; count--) {
  130. if (put_user(*k++, &reg_window[pos++]))
  131. return -EFAULT;
  132. }
  133. } else {
  134. for (; count > 0 && pos < 16; count--) {
  135. if (get_user(reg, u++))
  136. return -EFAULT;
  137. regs->u_regs[pos++] = reg;
  138. }
  139. reg_window = (unsigned long __user *) regs->u_regs[UREG_I6];
  140. reg_window -= 16;
  141. for (; count > 0 && pos < 32; count--) {
  142. if (get_user(reg, u++) ||
  143. put_user(reg, &reg_window[pos++]))
  144. return -EFAULT;
  145. }
  146. }
  147. while (count > 0) {
  148. unsigned long psr;
  149. if (kbuf)
  150. reg = *k++;
  151. else if (get_user(reg, u++))
  152. return -EFAULT;
  153. switch (pos) {
  154. case 32: /* PSR */
  155. psr = regs->psr;
  156. psr &= ~(PSR_ICC | PSR_SYSCALL);
  157. psr |= (reg & (PSR_ICC | PSR_SYSCALL));
  158. regs->psr = psr;
  159. break;
  160. case 33: /* PC */
  161. regs->pc = reg;
  162. break;
  163. case 34: /* NPC */
  164. regs->npc = reg;
  165. break;
  166. case 35: /* Y */
  167. regs->y = reg;
  168. break;
  169. case 36: /* WIM */
  170. case 37: /* TBR */
  171. break;
  172. default:
  173. goto finish;
  174. }
  175. pos++;
  176. count--;
  177. }
  178. finish:
  179. pos *= sizeof(reg);
  180. count *= sizeof(reg);
  181. return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  182. 38 * sizeof(reg), -1);
  183. }
  184. static int fpregs32_get(struct task_struct *target,
  185. const struct user_regset *regset,
  186. unsigned int pos, unsigned int count,
  187. void *kbuf, void __user *ubuf)
  188. {
  189. const unsigned long *fpregs = target->thread.float_regs;
  190. int ret = 0;
  191. #if 0
  192. if (target == current)
  193. save_and_clear_fpu();
  194. #endif
  195. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  196. fpregs,
  197. 0, 32 * sizeof(u32));
  198. if (!ret)
  199. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  200. 32 * sizeof(u32),
  201. 33 * sizeof(u32));
  202. if (!ret)
  203. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  204. &target->thread.fsr,
  205. 33 * sizeof(u32),
  206. 34 * sizeof(u32));
  207. if (!ret) {
  208. unsigned long val;
  209. val = (1 << 8) | (8 << 16);
  210. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  211. &val,
  212. 34 * sizeof(u32),
  213. 35 * sizeof(u32));
  214. }
  215. if (!ret)
  216. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  217. 35 * sizeof(u32), -1);
  218. return ret;
  219. }
  220. static int fpregs32_set(struct task_struct *target,
  221. const struct user_regset *regset,
  222. unsigned int pos, unsigned int count,
  223. const void *kbuf, const void __user *ubuf)
  224. {
  225. unsigned long *fpregs = target->thread.float_regs;
  226. int ret;
  227. #if 0
  228. if (target == current)
  229. save_and_clear_fpu();
  230. #endif
  231. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  232. fpregs,
  233. 0, 32 * sizeof(u32));
  234. if (!ret)
  235. user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  236. 32 * sizeof(u32),
  237. 33 * sizeof(u32));
  238. if (!ret && count > 0) {
  239. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  240. &target->thread.fsr,
  241. 33 * sizeof(u32),
  242. 34 * sizeof(u32));
  243. }
  244. if (!ret)
  245. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  246. 34 * sizeof(u32), -1);
  247. return ret;
  248. }
  249. static const struct user_regset sparc32_regsets[] = {
  250. /* Format is:
  251. * G0 --> G7
  252. * O0 --> O7
  253. * L0 --> L7
  254. * I0 --> I7
  255. * PSR, PC, nPC, Y, WIM, TBR
  256. */
  257. [REGSET_GENERAL] = {
  258. .core_note_type = NT_PRSTATUS,
  259. .n = 38,
  260. .size = sizeof(u32), .align = sizeof(u32),
  261. .get = genregs32_get, .set = genregs32_set
  262. },
  263. /* Format is:
  264. * F0 --> F31
  265. * empty 32-bit word
  266. * FSR (32--bit word)
  267. * FPU QUEUE COUNT (8-bit char)
  268. * FPU QUEUE ENTRYSIZE (8-bit char)
  269. * FPU ENABLED (8-bit char)
  270. * empty 8-bit char
  271. * FPU QUEUE (64 32-bit ints)
  272. */
  273. [REGSET_FP] = {
  274. .core_note_type = NT_PRFPREG,
  275. .n = 99,
  276. .size = sizeof(u32), .align = sizeof(u32),
  277. .get = fpregs32_get, .set = fpregs32_set
  278. },
  279. };
  280. static const struct user_regset_view user_sparc32_view = {
  281. .name = "sparc", .e_machine = EM_SPARC,
  282. .regsets = sparc32_regsets, .n = ARRAY_SIZE(sparc32_regsets)
  283. };
  284. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  285. {
  286. return &user_sparc32_view;
  287. }
  288. struct fps {
  289. unsigned long regs[32];
  290. unsigned long fsr;
  291. unsigned long flags;
  292. unsigned long extra;
  293. unsigned long fpqd;
  294. struct fq {
  295. unsigned long *insnaddr;
  296. unsigned long insn;
  297. } fpq[16];
  298. };
  299. long arch_ptrace(struct task_struct *child, long request,
  300. unsigned long addr, unsigned long data)
  301. {
  302. unsigned long addr2 = current->thread.kregs->u_regs[UREG_I4];
  303. void __user *addr2p;
  304. const struct user_regset_view *view;
  305. struct pt_regs __user *pregs;
  306. struct fps __user *fps;
  307. int ret;
  308. view = task_user_regset_view(current);
  309. addr2p = (void __user *) addr2;
  310. pregs = (struct pt_regs __user *) addr;
  311. fps = (struct fps __user *) addr;
  312. switch(request) {
  313. case PTRACE_GETREGS: {
  314. ret = copy_regset_to_user(child, view, REGSET_GENERAL,
  315. 32 * sizeof(u32),
  316. 4 * sizeof(u32),
  317. &pregs->psr);
  318. if (!ret)
  319. copy_regset_to_user(child, view, REGSET_GENERAL,
  320. 1 * sizeof(u32),
  321. 15 * sizeof(u32),
  322. &pregs->u_regs[0]);
  323. break;
  324. }
  325. case PTRACE_SETREGS: {
  326. ret = copy_regset_from_user(child, view, REGSET_GENERAL,
  327. 32 * sizeof(u32),
  328. 4 * sizeof(u32),
  329. &pregs->psr);
  330. if (!ret)
  331. copy_regset_from_user(child, view, REGSET_GENERAL,
  332. 1 * sizeof(u32),
  333. 15 * sizeof(u32),
  334. &pregs->u_regs[0]);
  335. break;
  336. }
  337. case PTRACE_GETFPREGS: {
  338. ret = copy_regset_to_user(child, view, REGSET_FP,
  339. 0 * sizeof(u32),
  340. 32 * sizeof(u32),
  341. &fps->regs[0]);
  342. if (!ret)
  343. ret = copy_regset_to_user(child, view, REGSET_FP,
  344. 33 * sizeof(u32),
  345. 1 * sizeof(u32),
  346. &fps->fsr);
  347. if (!ret) {
  348. if (__put_user(0, &fps->fpqd) ||
  349. __put_user(0, &fps->flags) ||
  350. __put_user(0, &fps->extra) ||
  351. clear_user(fps->fpq, sizeof(fps->fpq)))
  352. ret = -EFAULT;
  353. }
  354. break;
  355. }
  356. case PTRACE_SETFPREGS: {
  357. ret = copy_regset_from_user(child, view, REGSET_FP,
  358. 0 * sizeof(u32),
  359. 32 * sizeof(u32),
  360. &fps->regs[0]);
  361. if (!ret)
  362. ret = copy_regset_from_user(child, view, REGSET_FP,
  363. 33 * sizeof(u32),
  364. 1 * sizeof(u32),
  365. &fps->fsr);
  366. break;
  367. }
  368. case PTRACE_READTEXT:
  369. case PTRACE_READDATA:
  370. ret = ptrace_readdata(child, addr, addr2p, data);
  371. if (ret == data)
  372. ret = 0;
  373. else if (ret >= 0)
  374. ret = -EIO;
  375. break;
  376. case PTRACE_WRITETEXT:
  377. case PTRACE_WRITEDATA:
  378. ret = ptrace_writedata(child, addr2p, addr, data);
  379. if (ret == data)
  380. ret = 0;
  381. else if (ret >= 0)
  382. ret = -EIO;
  383. break;
  384. default:
  385. if (request == PTRACE_SPARC_DETACH)
  386. request = PTRACE_DETACH;
  387. ret = ptrace_request(child, request, addr, data);
  388. break;
  389. }
  390. return ret;
  391. }
  392. asmlinkage int syscall_trace(struct pt_regs *regs, int syscall_exit_p)
  393. {
  394. int ret = 0;
  395. if (test_thread_flag(TIF_SYSCALL_TRACE)) {
  396. if (syscall_exit_p)
  397. tracehook_report_syscall_exit(regs, 0);
  398. else
  399. ret = tracehook_report_syscall_entry(regs);
  400. }
  401. return ret;
  402. }