ptrace.c 10 KB

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