ptrace.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/ptrace.h>
  9. #include <linux/tracehook.h>
  10. #include <linux/sched/task_stack.h>
  11. #include <linux/regset.h>
  12. #include <linux/unistd.h>
  13. #include <linux/elf.h>
  14. static struct callee_regs *task_callee_regs(struct task_struct *tsk)
  15. {
  16. struct callee_regs *tmp = (struct callee_regs *)tsk->thread.callee_reg;
  17. return tmp;
  18. }
  19. static int genregs_get(struct task_struct *target,
  20. const struct user_regset *regset,
  21. unsigned int pos, unsigned int count,
  22. void *kbuf, void __user *ubuf)
  23. {
  24. const struct pt_regs *ptregs = task_pt_regs(target);
  25. const struct callee_regs *cregs = task_callee_regs(target);
  26. int ret = 0;
  27. unsigned int stop_pc_val;
  28. #define REG_O_CHUNK(START, END, PTR) \
  29. if (!ret) \
  30. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, PTR, \
  31. offsetof(struct user_regs_struct, START), \
  32. offsetof(struct user_regs_struct, END));
  33. #define REG_O_ONE(LOC, PTR) \
  34. if (!ret) \
  35. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, PTR, \
  36. offsetof(struct user_regs_struct, LOC), \
  37. offsetof(struct user_regs_struct, LOC) + 4);
  38. #define REG_O_ZERO(LOC) \
  39. if (!ret) \
  40. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, \
  41. offsetof(struct user_regs_struct, LOC), \
  42. offsetof(struct user_regs_struct, LOC) + 4);
  43. REG_O_ZERO(pad);
  44. REG_O_ONE(scratch.bta, &ptregs->bta);
  45. REG_O_ONE(scratch.lp_start, &ptregs->lp_start);
  46. REG_O_ONE(scratch.lp_end, &ptregs->lp_end);
  47. REG_O_ONE(scratch.lp_count, &ptregs->lp_count);
  48. REG_O_ONE(scratch.status32, &ptregs->status32);
  49. REG_O_ONE(scratch.ret, &ptregs->ret);
  50. REG_O_ONE(scratch.blink, &ptregs->blink);
  51. REG_O_ONE(scratch.fp, &ptregs->fp);
  52. REG_O_ONE(scratch.gp, &ptregs->r26);
  53. REG_O_ONE(scratch.r12, &ptregs->r12);
  54. REG_O_ONE(scratch.r11, &ptregs->r11);
  55. REG_O_ONE(scratch.r10, &ptregs->r10);
  56. REG_O_ONE(scratch.r9, &ptregs->r9);
  57. REG_O_ONE(scratch.r8, &ptregs->r8);
  58. REG_O_ONE(scratch.r7, &ptregs->r7);
  59. REG_O_ONE(scratch.r6, &ptregs->r6);
  60. REG_O_ONE(scratch.r5, &ptregs->r5);
  61. REG_O_ONE(scratch.r4, &ptregs->r4);
  62. REG_O_ONE(scratch.r3, &ptregs->r3);
  63. REG_O_ONE(scratch.r2, &ptregs->r2);
  64. REG_O_ONE(scratch.r1, &ptregs->r1);
  65. REG_O_ONE(scratch.r0, &ptregs->r0);
  66. REG_O_ONE(scratch.sp, &ptregs->sp);
  67. REG_O_ZERO(pad2);
  68. REG_O_ONE(callee.r25, &cregs->r25);
  69. REG_O_ONE(callee.r24, &cregs->r24);
  70. REG_O_ONE(callee.r23, &cregs->r23);
  71. REG_O_ONE(callee.r22, &cregs->r22);
  72. REG_O_ONE(callee.r21, &cregs->r21);
  73. REG_O_ONE(callee.r20, &cregs->r20);
  74. REG_O_ONE(callee.r19, &cregs->r19);
  75. REG_O_ONE(callee.r18, &cregs->r18);
  76. REG_O_ONE(callee.r17, &cregs->r17);
  77. REG_O_ONE(callee.r16, &cregs->r16);
  78. REG_O_ONE(callee.r15, &cregs->r15);
  79. REG_O_ONE(callee.r14, &cregs->r14);
  80. REG_O_ONE(callee.r13, &cregs->r13);
  81. REG_O_ONE(efa, &target->thread.fault_address);
  82. if (!ret) {
  83. if (in_brkpt_trap(ptregs)) {
  84. stop_pc_val = target->thread.fault_address;
  85. pr_debug("\t\tstop_pc (brk-pt)\n");
  86. } else {
  87. stop_pc_val = ptregs->ret;
  88. pr_debug("\t\tstop_pc (others)\n");
  89. }
  90. REG_O_ONE(stop_pc, &stop_pc_val);
  91. }
  92. return ret;
  93. }
  94. static int genregs_set(struct task_struct *target,
  95. const struct user_regset *regset,
  96. unsigned int pos, unsigned int count,
  97. const void *kbuf, const void __user *ubuf)
  98. {
  99. const struct pt_regs *ptregs = task_pt_regs(target);
  100. const struct callee_regs *cregs = task_callee_regs(target);
  101. int ret = 0;
  102. #define REG_IN_CHUNK(FIRST, NEXT, PTR) \
  103. if (!ret) \
  104. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, \
  105. (void *)(PTR), \
  106. offsetof(struct user_regs_struct, FIRST), \
  107. offsetof(struct user_regs_struct, NEXT));
  108. #define REG_IN_ONE(LOC, PTR) \
  109. if (!ret) \
  110. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, \
  111. (void *)(PTR), \
  112. offsetof(struct user_regs_struct, LOC), \
  113. offsetof(struct user_regs_struct, LOC) + 4);
  114. #define REG_IGNORE_ONE(LOC) \
  115. if (!ret) \
  116. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, \
  117. offsetof(struct user_regs_struct, LOC), \
  118. offsetof(struct user_regs_struct, LOC) + 4);
  119. REG_IGNORE_ONE(pad);
  120. REG_IN_ONE(scratch.bta, &ptregs->bta);
  121. REG_IN_ONE(scratch.lp_start, &ptregs->lp_start);
  122. REG_IN_ONE(scratch.lp_end, &ptregs->lp_end);
  123. REG_IN_ONE(scratch.lp_count, &ptregs->lp_count);
  124. REG_IGNORE_ONE(scratch.status32);
  125. REG_IN_ONE(scratch.ret, &ptregs->ret);
  126. REG_IN_ONE(scratch.blink, &ptregs->blink);
  127. REG_IN_ONE(scratch.fp, &ptregs->fp);
  128. REG_IN_ONE(scratch.gp, &ptregs->r26);
  129. REG_IN_ONE(scratch.r12, &ptregs->r12);
  130. REG_IN_ONE(scratch.r11, &ptregs->r11);
  131. REG_IN_ONE(scratch.r10, &ptregs->r10);
  132. REG_IN_ONE(scratch.r9, &ptregs->r9);
  133. REG_IN_ONE(scratch.r8, &ptregs->r8);
  134. REG_IN_ONE(scratch.r7, &ptregs->r7);
  135. REG_IN_ONE(scratch.r6, &ptregs->r6);
  136. REG_IN_ONE(scratch.r5, &ptregs->r5);
  137. REG_IN_ONE(scratch.r4, &ptregs->r4);
  138. REG_IN_ONE(scratch.r3, &ptregs->r3);
  139. REG_IN_ONE(scratch.r2, &ptregs->r2);
  140. REG_IN_ONE(scratch.r1, &ptregs->r1);
  141. REG_IN_ONE(scratch.r0, &ptregs->r0);
  142. REG_IN_ONE(scratch.sp, &ptregs->sp);
  143. REG_IGNORE_ONE(pad2);
  144. REG_IN_ONE(callee.r25, &cregs->r25);
  145. REG_IN_ONE(callee.r24, &cregs->r24);
  146. REG_IN_ONE(callee.r23, &cregs->r23);
  147. REG_IN_ONE(callee.r22, &cregs->r22);
  148. REG_IN_ONE(callee.r21, &cregs->r21);
  149. REG_IN_ONE(callee.r20, &cregs->r20);
  150. REG_IN_ONE(callee.r19, &cregs->r19);
  151. REG_IN_ONE(callee.r18, &cregs->r18);
  152. REG_IN_ONE(callee.r17, &cregs->r17);
  153. REG_IN_ONE(callee.r16, &cregs->r16);
  154. REG_IN_ONE(callee.r15, &cregs->r15);
  155. REG_IN_ONE(callee.r14, &cregs->r14);
  156. REG_IN_ONE(callee.r13, &cregs->r13);
  157. REG_IGNORE_ONE(efa); /* efa update invalid */
  158. REG_IGNORE_ONE(stop_pc); /* PC updated via @ret */
  159. return ret;
  160. }
  161. #ifdef CONFIG_ISA_ARCV2
  162. static int arcv2regs_get(struct task_struct *target,
  163. const struct user_regset *regset,
  164. unsigned int pos, unsigned int count,
  165. void *kbuf, void __user *ubuf)
  166. {
  167. const struct pt_regs *regs = task_pt_regs(target);
  168. int ret, copy_sz;
  169. if (IS_ENABLED(CONFIG_ARC_HAS_ACCL_REGS))
  170. copy_sz = sizeof(struct user_regs_arcv2);
  171. else
  172. copy_sz = 4; /* r30 only */
  173. /*
  174. * itemized copy not needed like above as layout of regs (r30,r58,r59)
  175. * is exactly same in kernel (pt_regs) and userspace (user_regs_arcv2)
  176. */
  177. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &regs->r30,
  178. 0, copy_sz);
  179. return ret;
  180. }
  181. static int arcv2regs_set(struct task_struct *target,
  182. const struct user_regset *regset,
  183. unsigned int pos, unsigned int count,
  184. const void *kbuf, const void __user *ubuf)
  185. {
  186. const struct pt_regs *regs = task_pt_regs(target);
  187. int ret, copy_sz;
  188. if (IS_ENABLED(CONFIG_ARC_HAS_ACCL_REGS))
  189. copy_sz = sizeof(struct user_regs_arcv2);
  190. else
  191. copy_sz = 4; /* r30 only */
  192. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, (void *)&regs->r30,
  193. 0, copy_sz);
  194. return ret;
  195. }
  196. #endif
  197. enum arc_getset {
  198. REGSET_CMN,
  199. REGSET_ARCV2,
  200. };
  201. static const struct user_regset arc_regsets[] = {
  202. [REGSET_CMN] = {
  203. .core_note_type = NT_PRSTATUS,
  204. .n = ELF_NGREG,
  205. .size = sizeof(unsigned long),
  206. .align = sizeof(unsigned long),
  207. .get = genregs_get,
  208. .set = genregs_set,
  209. },
  210. #ifdef CONFIG_ISA_ARCV2
  211. [REGSET_ARCV2] = {
  212. .core_note_type = NT_ARC_V2,
  213. .n = ELF_ARCV2REG,
  214. .size = sizeof(unsigned long),
  215. .align = sizeof(unsigned long),
  216. .get = arcv2regs_get,
  217. .set = arcv2regs_set,
  218. },
  219. #endif
  220. };
  221. static const struct user_regset_view user_arc_view = {
  222. .name = UTS_MACHINE,
  223. .e_machine = EM_ARC_INUSE,
  224. .regsets = arc_regsets,
  225. .n = ARRAY_SIZE(arc_regsets)
  226. };
  227. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  228. {
  229. return &user_arc_view;
  230. }
  231. void ptrace_disable(struct task_struct *child)
  232. {
  233. }
  234. long arch_ptrace(struct task_struct *child, long request,
  235. unsigned long addr, unsigned long data)
  236. {
  237. int ret = -EIO;
  238. pr_debug("REQ=%ld: ADDR =0x%lx, DATA=0x%lx)\n", request, addr, data);
  239. switch (request) {
  240. case PTRACE_GET_THREAD_AREA:
  241. ret = put_user(task_thread_info(child)->thr_ptr,
  242. (unsigned long __user *)data);
  243. break;
  244. default:
  245. ret = ptrace_request(child, request, addr, data);
  246. break;
  247. }
  248. return ret;
  249. }
  250. asmlinkage int syscall_trace_entry(struct pt_regs *regs)
  251. {
  252. if (tracehook_report_syscall_entry(regs))
  253. return ULONG_MAX;
  254. return regs->r8;
  255. }
  256. asmlinkage void syscall_trace_exit(struct pt_regs *regs)
  257. {
  258. tracehook_report_syscall_exit(regs, 0);
  259. }