ptrace.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
  3. #include <linux/elf.h>
  4. #include <linux/errno.h>
  5. #include <linux/kernel.h>
  6. #include <linux/mm.h>
  7. #include <linux/ptrace.h>
  8. #include <linux/regset.h>
  9. #include <linux/sched.h>
  10. #include <linux/signal.h>
  11. #include <linux/smp.h>
  12. #include <linux/uaccess.h>
  13. #include <linux/user.h>
  14. #include <asm/thread_info.h>
  15. #include <asm/page.h>
  16. #include <asm/pgtable.h>
  17. #include <asm/processor.h>
  18. #include <asm/asm-offsets.h>
  19. #include <abi/regdef.h>
  20. /* sets the trace bits. */
  21. #define TRACE_MODE_SI (1 << 14)
  22. #define TRACE_MODE_RUN 0
  23. #define TRACE_MODE_MASK ~(0x3 << 14)
  24. /*
  25. * Make sure the single step bit is not set.
  26. */
  27. static void singlestep_disable(struct task_struct *tsk)
  28. {
  29. struct pt_regs *regs;
  30. regs = task_pt_regs(tsk);
  31. regs->sr = (regs->sr & TRACE_MODE_MASK) | TRACE_MODE_RUN;
  32. }
  33. static void singlestep_enable(struct task_struct *tsk)
  34. {
  35. struct pt_regs *regs;
  36. regs = task_pt_regs(tsk);
  37. regs->sr = (regs->sr & TRACE_MODE_MASK) | TRACE_MODE_SI;
  38. }
  39. /*
  40. * Make sure the single step bit is set.
  41. */
  42. void user_enable_single_step(struct task_struct *child)
  43. {
  44. if (child->thread.esp0 == 0)
  45. return;
  46. singlestep_enable(child);
  47. }
  48. void user_disable_single_step(struct task_struct *child)
  49. {
  50. if (child->thread.esp0 == 0)
  51. return;
  52. singlestep_disable(child);
  53. }
  54. enum csky_regset {
  55. REGSET_GPR,
  56. REGSET_FPR,
  57. };
  58. static int gpr_get(struct task_struct *target,
  59. const struct user_regset *regset,
  60. unsigned int pos, unsigned int count,
  61. void *kbuf, void __user *ubuf)
  62. {
  63. struct pt_regs *regs;
  64. regs = task_pt_regs(target);
  65. /* Abiv1 regs->tls is fake and we need sync here. */
  66. regs->tls = task_thread_info(target)->tp_value;
  67. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
  68. }
  69. static int gpr_set(struct task_struct *target,
  70. const struct user_regset *regset,
  71. unsigned int pos, unsigned int count,
  72. const void *kbuf, const void __user *ubuf)
  73. {
  74. int ret;
  75. struct pt_regs regs;
  76. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &regs, 0, -1);
  77. if (ret)
  78. return ret;
  79. regs.sr = task_pt_regs(target)->sr;
  80. task_thread_info(target)->tp_value = regs.tls;
  81. *task_pt_regs(target) = regs;
  82. return 0;
  83. }
  84. static int fpr_get(struct task_struct *target,
  85. const struct user_regset *regset,
  86. unsigned int pos, unsigned int count,
  87. void *kbuf, void __user *ubuf)
  88. {
  89. struct user_fp *regs = (struct user_fp *)&target->thread.user_fp;
  90. #if defined(CONFIG_CPU_HAS_FPUV2) && !defined(CONFIG_CPU_HAS_VDSP)
  91. int i;
  92. struct user_fp tmp = *regs;
  93. for (i = 0; i < 16; i++) {
  94. tmp.vr[i*4] = regs->vr[i*2];
  95. tmp.vr[i*4 + 1] = regs->vr[i*2 + 1];
  96. }
  97. for (i = 0; i < 32; i++)
  98. tmp.vr[64 + i] = regs->vr[32 + i];
  99. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &tmp, 0, -1);
  100. #else
  101. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
  102. #endif
  103. }
  104. static int fpr_set(struct task_struct *target,
  105. const struct user_regset *regset,
  106. unsigned int pos, unsigned int count,
  107. const void *kbuf, const void __user *ubuf)
  108. {
  109. int ret;
  110. struct user_fp *regs = (struct user_fp *)&target->thread.user_fp;
  111. #if defined(CONFIG_CPU_HAS_FPUV2) && !defined(CONFIG_CPU_HAS_VDSP)
  112. int i;
  113. struct user_fp tmp;
  114. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tmp, 0, -1);
  115. *regs = tmp;
  116. for (i = 0; i < 16; i++) {
  117. regs->vr[i*2] = tmp.vr[i*4];
  118. regs->vr[i*2 + 1] = tmp.vr[i*4 + 1];
  119. }
  120. for (i = 0; i < 32; i++)
  121. regs->vr[32 + i] = tmp.vr[64 + i];
  122. #else
  123. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
  124. #endif
  125. return ret;
  126. }
  127. static const struct user_regset csky_regsets[] = {
  128. [REGSET_GPR] = {
  129. .core_note_type = NT_PRSTATUS,
  130. .n = ELF_NGREG,
  131. .size = sizeof(u32),
  132. .align = sizeof(u32),
  133. .get = &gpr_get,
  134. .set = &gpr_set,
  135. },
  136. [REGSET_FPR] = {
  137. .core_note_type = NT_PRFPREG,
  138. .n = sizeof(struct user_fp) / sizeof(u32),
  139. .size = sizeof(u32),
  140. .align = sizeof(u32),
  141. .get = &fpr_get,
  142. .set = &fpr_set,
  143. },
  144. };
  145. static const struct user_regset_view user_csky_view = {
  146. .name = "csky",
  147. .e_machine = ELF_ARCH,
  148. .regsets = csky_regsets,
  149. .n = ARRAY_SIZE(csky_regsets),
  150. };
  151. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  152. {
  153. return &user_csky_view;
  154. }
  155. void ptrace_disable(struct task_struct *child)
  156. {
  157. singlestep_disable(child);
  158. }
  159. long arch_ptrace(struct task_struct *child, long request,
  160. unsigned long addr, unsigned long data)
  161. {
  162. long ret = -EIO;
  163. switch (request) {
  164. default:
  165. ret = ptrace_request(child, request, addr, data);
  166. break;
  167. }
  168. return ret;
  169. }
  170. /*
  171. * If process's system calls is traces, do some corresponding handles in this
  172. * function before entering system call function and after exiting system call
  173. * function.
  174. */
  175. asmlinkage void syscall_trace(int why, struct pt_regs *regs)
  176. {
  177. long saved_why;
  178. /*
  179. * Save saved_why, why is used to denote syscall entry/exit;
  180. * why = 0:entry, why = 1: exit
  181. */
  182. saved_why = regs->regs[SYSTRACE_SAVENUM];
  183. regs->regs[SYSTRACE_SAVENUM] = why;
  184. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  185. ? 0x80 : 0));
  186. /*
  187. * this isn't the same as continuing with a signal, but it will do
  188. * for normal use. strace only continues with a signal if the
  189. * stopping signal is not SIGTRAP. -brl
  190. */
  191. if (current->exit_code) {
  192. send_sig(current->exit_code, current, 1);
  193. current->exit_code = 0;
  194. }
  195. regs->regs[SYSTRACE_SAVENUM] = saved_why;
  196. }
  197. void show_regs(struct pt_regs *fp)
  198. {
  199. unsigned long *sp;
  200. unsigned char *tp;
  201. int i;
  202. pr_info("\nCURRENT PROCESS:\n\n");
  203. pr_info("COMM=%s PID=%d\n", current->comm, current->pid);
  204. if (current->mm) {
  205. pr_info("TEXT=%08x-%08x DATA=%08x-%08x BSS=%08x-%08x\n",
  206. (int) current->mm->start_code,
  207. (int) current->mm->end_code,
  208. (int) current->mm->start_data,
  209. (int) current->mm->end_data,
  210. (int) current->mm->end_data,
  211. (int) current->mm->brk);
  212. pr_info("USER-STACK=%08x KERNEL-STACK=%08x\n\n",
  213. (int) current->mm->start_stack,
  214. (int) (((unsigned long) current) + 2 * PAGE_SIZE));
  215. }
  216. pr_info("PC: 0x%08lx\n", (long)fp->pc);
  217. pr_info("orig_a0: 0x%08lx\n", fp->orig_a0);
  218. pr_info("PSR: 0x%08lx\n", (long)fp->sr);
  219. pr_info("a0: 0x%08lx a1: 0x%08lx a2: 0x%08lx a3: 0x%08lx\n",
  220. fp->a0, fp->a1, fp->a2, fp->a3);
  221. #if defined(__CSKYABIV2__)
  222. pr_info("r4: 0x%08lx r5: 0x%08lx r6: 0x%08lx r7: 0x%08lx\n",
  223. fp->regs[0], fp->regs[1], fp->regs[2], fp->regs[3]);
  224. pr_info("r8: 0x%08lx r9: 0x%08lx r10: 0x%08lx r11: 0x%08lx\n",
  225. fp->regs[4], fp->regs[5], fp->regs[6], fp->regs[7]);
  226. pr_info("r12 0x%08lx r13: 0x%08lx r15: 0x%08lx\n",
  227. fp->regs[8], fp->regs[9], fp->lr);
  228. pr_info("r16:0x%08lx r17: 0x%08lx r18: 0x%08lx r19: 0x%08lx\n",
  229. fp->exregs[0], fp->exregs[1], fp->exregs[2], fp->exregs[3]);
  230. pr_info("r20 0x%08lx r21: 0x%08lx r22: 0x%08lx r23: 0x%08lx\n",
  231. fp->exregs[4], fp->exregs[5], fp->exregs[6], fp->exregs[7]);
  232. pr_info("r24 0x%08lx r25: 0x%08lx r26: 0x%08lx r27: 0x%08lx\n",
  233. fp->exregs[8], fp->exregs[9], fp->exregs[10], fp->exregs[11]);
  234. pr_info("r28 0x%08lx r29: 0x%08lx r30: 0x%08lx tls: 0x%08lx\n",
  235. fp->exregs[12], fp->exregs[13], fp->exregs[14], fp->tls);
  236. pr_info("hi 0x%08lx lo: 0x%08lx\n",
  237. fp->rhi, fp->rlo);
  238. #else
  239. pr_info("r6: 0x%08lx r7: 0x%08lx r8: 0x%08lx r9: 0x%08lx\n",
  240. fp->regs[0], fp->regs[1], fp->regs[2], fp->regs[3]);
  241. pr_info("r10: 0x%08lx r11: 0x%08lx r12: 0x%08lx r13: 0x%08lx\n",
  242. fp->regs[4], fp->regs[5], fp->regs[6], fp->regs[7]);
  243. pr_info("r14 0x%08lx r1: 0x%08lx r15: 0x%08lx\n",
  244. fp->regs[8], fp->regs[9], fp->lr);
  245. #endif
  246. pr_info("\nCODE:");
  247. tp = ((unsigned char *) fp->pc) - 0x20;
  248. tp += ((int)tp % 4) ? 2 : 0;
  249. for (sp = (unsigned long *) tp, i = 0; (i < 0x40); i += 4) {
  250. if ((i % 0x10) == 0)
  251. pr_cont("\n%08x: ", (int) (tp + i));
  252. pr_cont("%08x ", (int) *sp++);
  253. }
  254. pr_cont("\n");
  255. pr_info("\nKERNEL STACK:");
  256. tp = ((unsigned char *) fp) - 0x40;
  257. for (sp = (unsigned long *) tp, i = 0; (i < 0xc0); i += 4) {
  258. if ((i % 0x10) == 0)
  259. pr_cont("\n%08x: ", (int) (tp + i));
  260. pr_cont("%08x ", (int) *sp++);
  261. }
  262. pr_cont("\n");
  263. }