ptrace_32.c 9.8 KB

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