ptrace.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * Copyright (C) 2005-2012 Imagination Technologies Ltd.
  3. *
  4. * This file is subject to the terms and conditions of the GNU General
  5. * Public License. See the file COPYING in the main directory of
  6. * this archive for more details.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/mm.h>
  10. #include <linux/errno.h>
  11. #include <linux/ptrace.h>
  12. #include <linux/user.h>
  13. #include <linux/regset.h>
  14. #include <linux/tracehook.h>
  15. #include <linux/elf.h>
  16. #include <linux/uaccess.h>
  17. #include <trace/syscall.h>
  18. #define CREATE_TRACE_POINTS
  19. #include <trace/events/syscalls.h>
  20. /*
  21. * user_regset definitions.
  22. */
  23. int metag_gp_regs_copyout(const struct pt_regs *regs,
  24. unsigned int pos, unsigned int count,
  25. void *kbuf, void __user *ubuf)
  26. {
  27. const void *ptr;
  28. unsigned long data;
  29. int ret;
  30. /* D{0-1}.{0-7} */
  31. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  32. regs->ctx.DX, 0, 4*16);
  33. if (ret)
  34. goto out;
  35. /* A{0-1}.{0-1} */
  36. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  37. regs->ctx.AX, 4*16, 4*20);
  38. if (ret)
  39. goto out;
  40. /* A{0-1}.2 */
  41. if (regs->ctx.SaveMask & TBICTX_XEXT_BIT)
  42. ptr = regs->ctx.Ext.Ctx.pExt;
  43. else
  44. ptr = &regs->ctx.Ext.AX2;
  45. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  46. ptr, 4*20, 4*22);
  47. if (ret)
  48. goto out;
  49. /* A{0-1}.3 */
  50. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  51. &regs->ctx.AX3, 4*22, 4*24);
  52. if (ret)
  53. goto out;
  54. /* PC */
  55. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  56. &regs->ctx.CurrPC, 4*24, 4*25);
  57. if (ret)
  58. goto out;
  59. /* TXSTATUS */
  60. data = (unsigned long)regs->ctx.Flags;
  61. if (regs->ctx.SaveMask & TBICTX_CBUF_BIT)
  62. data |= USER_GP_REGS_STATUS_CATCH_BIT;
  63. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  64. &data, 4*25, 4*26);
  65. if (ret)
  66. goto out;
  67. /* TXRPT, TXBPOBITS, TXMODE */
  68. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  69. &regs->ctx.CurrRPT, 4*26, 4*29);
  70. if (ret)
  71. goto out;
  72. /* Padding */
  73. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  74. 4*29, 4*30);
  75. out:
  76. return ret;
  77. }
  78. int metag_gp_regs_copyin(struct pt_regs *regs,
  79. unsigned int pos, unsigned int count,
  80. const void *kbuf, const void __user *ubuf)
  81. {
  82. void *ptr;
  83. unsigned long data;
  84. int ret;
  85. /* D{0-1}.{0-7} */
  86. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  87. regs->ctx.DX, 0, 4*16);
  88. if (ret)
  89. goto out;
  90. /* A{0-1}.{0-1} */
  91. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  92. regs->ctx.AX, 4*16, 4*20);
  93. if (ret)
  94. goto out;
  95. /* A{0-1}.2 */
  96. if (regs->ctx.SaveMask & TBICTX_XEXT_BIT)
  97. ptr = regs->ctx.Ext.Ctx.pExt;
  98. else
  99. ptr = &regs->ctx.Ext.AX2;
  100. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  101. ptr, 4*20, 4*22);
  102. if (ret)
  103. goto out;
  104. /* A{0-1}.3 */
  105. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  106. &regs->ctx.AX3, 4*22, 4*24);
  107. if (ret)
  108. goto out;
  109. /* PC */
  110. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  111. &regs->ctx.CurrPC, 4*24, 4*25);
  112. if (ret)
  113. goto out;
  114. /* TXSTATUS */
  115. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  116. &data, 4*25, 4*26);
  117. if (ret)
  118. goto out;
  119. regs->ctx.Flags = data & 0xffff;
  120. if (data & USER_GP_REGS_STATUS_CATCH_BIT)
  121. regs->ctx.SaveMask |= TBICTX_XCBF_BIT | TBICTX_CBUF_BIT;
  122. else
  123. regs->ctx.SaveMask &= ~TBICTX_CBUF_BIT;
  124. /* TXRPT, TXBPOBITS, TXMODE */
  125. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  126. &regs->ctx.CurrRPT, 4*26, 4*29);
  127. out:
  128. return ret;
  129. }
  130. static int metag_gp_regs_get(struct task_struct *target,
  131. const struct user_regset *regset,
  132. unsigned int pos, unsigned int count,
  133. void *kbuf, void __user *ubuf)
  134. {
  135. const struct pt_regs *regs = task_pt_regs(target);
  136. return metag_gp_regs_copyout(regs, pos, count, kbuf, ubuf);
  137. }
  138. static int metag_gp_regs_set(struct task_struct *target,
  139. const struct user_regset *regset,
  140. unsigned int pos, unsigned int count,
  141. const void *kbuf, const void __user *ubuf)
  142. {
  143. struct pt_regs *regs = task_pt_regs(target);
  144. return metag_gp_regs_copyin(regs, pos, count, kbuf, ubuf);
  145. }
  146. int metag_cb_regs_copyout(const struct pt_regs *regs,
  147. unsigned int pos, unsigned int count,
  148. void *kbuf, void __user *ubuf)
  149. {
  150. int ret;
  151. /* TXCATCH{0-3} */
  152. if (regs->ctx.SaveMask & TBICTX_XCBF_BIT)
  153. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  154. regs->extcb0, 0, 4*4);
  155. else
  156. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  157. 0, 4*4);
  158. return ret;
  159. }
  160. int metag_cb_regs_copyin(struct pt_regs *regs,
  161. unsigned int pos, unsigned int count,
  162. const void *kbuf, const void __user *ubuf)
  163. {
  164. int ret;
  165. /* TXCATCH{0-3} */
  166. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  167. regs->extcb0, 0, 4*4);
  168. return ret;
  169. }
  170. static int metag_cb_regs_get(struct task_struct *target,
  171. const struct user_regset *regset,
  172. unsigned int pos, unsigned int count,
  173. void *kbuf, void __user *ubuf)
  174. {
  175. const struct pt_regs *regs = task_pt_regs(target);
  176. return metag_cb_regs_copyout(regs, pos, count, kbuf, ubuf);
  177. }
  178. static int metag_cb_regs_set(struct task_struct *target,
  179. const struct user_regset *regset,
  180. unsigned int pos, unsigned int count,
  181. const void *kbuf, const void __user *ubuf)
  182. {
  183. struct pt_regs *regs = task_pt_regs(target);
  184. return metag_cb_regs_copyin(regs, pos, count, kbuf, ubuf);
  185. }
  186. int metag_rp_state_copyout(const struct pt_regs *regs,
  187. unsigned int pos, unsigned int count,
  188. void *kbuf, void __user *ubuf)
  189. {
  190. unsigned long mask;
  191. u64 *ptr;
  192. int ret, i;
  193. /* Empty read pipeline */
  194. if (!(regs->ctx.SaveMask & TBICTX_CBRP_BIT)) {
  195. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  196. 0, 4*13);
  197. goto out;
  198. }
  199. mask = (regs->ctx.CurrDIVTIME & TXDIVTIME_RPMASK_BITS) >>
  200. TXDIVTIME_RPMASK_S;
  201. /* Read pipeline entries */
  202. ptr = (void *)&regs->extcb0[1];
  203. for (i = 0; i < 6; ++i, ++ptr) {
  204. if (mask & (1 << i))
  205. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  206. ptr, 8*i, 8*(i + 1));
  207. else
  208. ret = user_regset_copyout_zero(&pos, &count, &kbuf,
  209. &ubuf, 8*i, 8*(i + 1));
  210. if (ret)
  211. goto out;
  212. }
  213. /* Mask of entries */
  214. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  215. &mask, 4*12, 4*13);
  216. out:
  217. return ret;
  218. }
  219. int metag_rp_state_copyin(struct pt_regs *regs,
  220. unsigned int pos, unsigned int count,
  221. const void *kbuf, const void __user *ubuf)
  222. {
  223. struct user_rp_state rp;
  224. unsigned long long *ptr;
  225. int ret, i;
  226. /* Read the entire pipeline before making any changes */
  227. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  228. &rp, 0, 4*13);
  229. if (ret)
  230. goto out;
  231. /* Write pipeline entries */
  232. ptr = (void *)&regs->extcb0[1];
  233. for (i = 0; i < 6; ++i, ++ptr)
  234. if (rp.mask & (1 << i))
  235. *ptr = rp.entries[i];
  236. /* Update RPMask in TXDIVTIME */
  237. regs->ctx.CurrDIVTIME &= ~TXDIVTIME_RPMASK_BITS;
  238. regs->ctx.CurrDIVTIME |= (rp.mask << TXDIVTIME_RPMASK_S)
  239. & TXDIVTIME_RPMASK_BITS;
  240. /* Set/clear flags to indicate catch/read pipeline state */
  241. if (rp.mask)
  242. regs->ctx.SaveMask |= TBICTX_XCBF_BIT | TBICTX_CBRP_BIT;
  243. else
  244. regs->ctx.SaveMask &= ~TBICTX_CBRP_BIT;
  245. out:
  246. return ret;
  247. }
  248. static int metag_rp_state_get(struct task_struct *target,
  249. const struct user_regset *regset,
  250. unsigned int pos, unsigned int count,
  251. void *kbuf, void __user *ubuf)
  252. {
  253. const struct pt_regs *regs = task_pt_regs(target);
  254. return metag_rp_state_copyout(regs, pos, count, kbuf, ubuf);
  255. }
  256. static int metag_rp_state_set(struct task_struct *target,
  257. const struct user_regset *regset,
  258. unsigned int pos, unsigned int count,
  259. const void *kbuf, const void __user *ubuf)
  260. {
  261. struct pt_regs *regs = task_pt_regs(target);
  262. return metag_rp_state_copyin(regs, pos, count, kbuf, ubuf);
  263. }
  264. static int metag_tls_get(struct task_struct *target,
  265. const struct user_regset *regset,
  266. unsigned int pos, unsigned int count,
  267. void *kbuf, void __user *ubuf)
  268. {
  269. void __user *tls = target->thread.tls_ptr;
  270. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
  271. }
  272. static int metag_tls_set(struct task_struct *target,
  273. const struct user_regset *regset,
  274. unsigned int pos, unsigned int count,
  275. const void *kbuf, const void __user *ubuf)
  276. {
  277. int ret;
  278. void __user *tls;
  279. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
  280. if (ret)
  281. return ret;
  282. target->thread.tls_ptr = tls;
  283. return ret;
  284. }
  285. enum metag_regset {
  286. REGSET_GENERAL,
  287. REGSET_CBUF,
  288. REGSET_READPIPE,
  289. REGSET_TLS,
  290. };
  291. static const struct user_regset metag_regsets[] = {
  292. [REGSET_GENERAL] = {
  293. .core_note_type = NT_PRSTATUS,
  294. .n = ELF_NGREG,
  295. .size = sizeof(long),
  296. .align = sizeof(long long),
  297. .get = metag_gp_regs_get,
  298. .set = metag_gp_regs_set,
  299. },
  300. [REGSET_CBUF] = {
  301. .core_note_type = NT_METAG_CBUF,
  302. .n = sizeof(struct user_cb_regs) / sizeof(long),
  303. .size = sizeof(long),
  304. .align = sizeof(long long),
  305. .get = metag_cb_regs_get,
  306. .set = metag_cb_regs_set,
  307. },
  308. [REGSET_READPIPE] = {
  309. .core_note_type = NT_METAG_RPIPE,
  310. .n = sizeof(struct user_rp_state) / sizeof(long),
  311. .size = sizeof(long),
  312. .align = sizeof(long long),
  313. .get = metag_rp_state_get,
  314. .set = metag_rp_state_set,
  315. },
  316. [REGSET_TLS] = {
  317. .core_note_type = NT_METAG_TLS,
  318. .n = 1,
  319. .size = sizeof(void *),
  320. .align = sizeof(void *),
  321. .get = metag_tls_get,
  322. .set = metag_tls_set,
  323. },
  324. };
  325. static const struct user_regset_view user_metag_view = {
  326. .name = "metag",
  327. .e_machine = EM_METAG,
  328. .regsets = metag_regsets,
  329. .n = ARRAY_SIZE(metag_regsets)
  330. };
  331. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  332. {
  333. return &user_metag_view;
  334. }
  335. /*
  336. * Called by kernel/ptrace.c when detaching..
  337. *
  338. * Make sure single step bits etc are not set.
  339. */
  340. void ptrace_disable(struct task_struct *child)
  341. {
  342. /* nothing to do.. */
  343. }
  344. long arch_ptrace(struct task_struct *child, long request, unsigned long addr,
  345. unsigned long data)
  346. {
  347. int ret;
  348. switch (request) {
  349. default:
  350. ret = ptrace_request(child, request, addr, data);
  351. break;
  352. }
  353. return ret;
  354. }
  355. int syscall_trace_enter(struct pt_regs *regs)
  356. {
  357. int ret = 0;
  358. if (test_thread_flag(TIF_SYSCALL_TRACE))
  359. ret = tracehook_report_syscall_entry(regs);
  360. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  361. trace_sys_enter(regs, regs->ctx.DX[0].U1);
  362. return ret ? -1 : regs->ctx.DX[0].U1;
  363. }
  364. void syscall_trace_leave(struct pt_regs *regs)
  365. {
  366. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  367. trace_sys_exit(regs, regs->ctx.DX[0].U1);
  368. if (test_thread_flag(TIF_SYSCALL_TRACE))
  369. tracehook_report_syscall_exit(regs, 0);
  370. }