ptrace.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Ptrace support for Hexagon
  4. *
  5. * Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/sched.h>
  9. #include <linux/sched/task_stack.h>
  10. #include <linux/mm.h>
  11. #include <linux/smp.h>
  12. #include <linux/errno.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/regset.h>
  15. #include <linux/user.h>
  16. #include <linux/elf.h>
  17. #include <asm/user.h>
  18. #if arch_has_single_step()
  19. /* Both called from ptrace_resume */
  20. void user_enable_single_step(struct task_struct *child)
  21. {
  22. pt_set_singlestep(task_pt_regs(child));
  23. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  24. }
  25. void user_disable_single_step(struct task_struct *child)
  26. {
  27. pt_clr_singlestep(task_pt_regs(child));
  28. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  29. }
  30. #endif
  31. static int genregs_get(struct task_struct *target,
  32. const struct user_regset *regset,
  33. struct membuf to)
  34. {
  35. struct pt_regs *regs = task_pt_regs(target);
  36. /* The general idea here is that the copyout must happen in
  37. * exactly the same order in which the userspace expects these
  38. * regs. Now, the sequence in userspace does not match the
  39. * sequence in the kernel, so everything past the 32 gprs
  40. * happens one at a time.
  41. */
  42. membuf_write(&to, &regs->r00, 32*sizeof(unsigned long));
  43. /* Must be exactly same sequence as struct user_regs_struct */
  44. membuf_store(&to, regs->sa0);
  45. membuf_store(&to, regs->lc0);
  46. membuf_store(&to, regs->sa1);
  47. membuf_store(&to, regs->lc1);
  48. membuf_store(&to, regs->m0);
  49. membuf_store(&to, regs->m1);
  50. membuf_store(&to, regs->usr);
  51. membuf_store(&to, regs->preds);
  52. membuf_store(&to, regs->gp);
  53. membuf_store(&to, regs->ugp);
  54. membuf_store(&to, pt_elr(regs)); // pc
  55. membuf_store(&to, (unsigned long)pt_cause(regs)); // cause
  56. membuf_store(&to, pt_badva(regs)); // badva
  57. #if CONFIG_HEXAGON_ARCH_VERSION >=4
  58. membuf_store(&to, regs->cs0);
  59. membuf_store(&to, regs->cs1);
  60. return membuf_zero(&to, sizeof(unsigned long));
  61. #else
  62. return membuf_zero(&to, 3 * sizeof(unsigned long));
  63. #endif
  64. }
  65. static int genregs_set(struct task_struct *target,
  66. const struct user_regset *regset,
  67. unsigned int pos, unsigned int count,
  68. const void *kbuf, const void __user *ubuf)
  69. {
  70. int ret;
  71. unsigned long bucket;
  72. struct pt_regs *regs = task_pt_regs(target);
  73. if (!regs)
  74. return -EIO;
  75. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  76. &regs->r00, 0, 32*sizeof(unsigned long));
  77. #define INEXT(KPT_REG, USR_REG) \
  78. if (!ret) \
  79. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, \
  80. KPT_REG, offsetof(struct user_regs_struct, USR_REG), \
  81. offsetof(struct user_regs_struct, USR_REG) + \
  82. sizeof(unsigned long));
  83. /* Must be exactly same sequence as struct user_regs_struct */
  84. INEXT(&regs->sa0, sa0);
  85. INEXT(&regs->lc0, lc0);
  86. INEXT(&regs->sa1, sa1);
  87. INEXT(&regs->lc1, lc1);
  88. INEXT(&regs->m0, m0);
  89. INEXT(&regs->m1, m1);
  90. INEXT(&regs->usr, usr);
  91. INEXT(&regs->preds, p3_0);
  92. INEXT(&regs->gp, gp);
  93. INEXT(&regs->ugp, ugp);
  94. INEXT(&pt_elr(regs), pc);
  95. /* CAUSE and BADVA aren't writeable. */
  96. INEXT(&bucket, cause);
  97. INEXT(&bucket, badva);
  98. #if CONFIG_HEXAGON_ARCH_VERSION >=4
  99. INEXT(&regs->cs0, cs0);
  100. INEXT(&regs->cs1, cs1);
  101. #endif
  102. /* Ignore the rest, if needed */
  103. if (!ret)
  104. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  105. offsetof(struct user_regs_struct, pad1), -1);
  106. if (ret)
  107. return ret;
  108. /*
  109. * This is special; SP is actually restored by the VM via the
  110. * special event record which is set by the special trap.
  111. */
  112. regs->hvmer.vmpsp = regs->r29;
  113. return 0;
  114. }
  115. enum hexagon_regset {
  116. REGSET_GENERAL,
  117. };
  118. static const struct user_regset hexagon_regsets[] = {
  119. [REGSET_GENERAL] = {
  120. .core_note_type = NT_PRSTATUS,
  121. .n = ELF_NGREG,
  122. .size = sizeof(unsigned long),
  123. .align = sizeof(unsigned long),
  124. .regset_get = genregs_get,
  125. .set = genregs_set,
  126. },
  127. };
  128. static const struct user_regset_view hexagon_user_view = {
  129. .name = "hexagon",
  130. .e_machine = ELF_ARCH,
  131. .ei_osabi = ELF_OSABI,
  132. .regsets = hexagon_regsets,
  133. .e_flags = ELF_CORE_EFLAGS,
  134. .n = ARRAY_SIZE(hexagon_regsets)
  135. };
  136. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  137. {
  138. return &hexagon_user_view;
  139. }
  140. void ptrace_disable(struct task_struct *child)
  141. {
  142. /* Boilerplate - resolves to null inline if no HW single-step */
  143. user_disable_single_step(child);
  144. }
  145. long arch_ptrace(struct task_struct *child, long request,
  146. unsigned long addr, unsigned long data)
  147. {
  148. return ptrace_request(child, request, addr, data);
  149. }