ptrace_32.c 9.8 KB

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