ptrace.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Ptrace support for Hexagon
  3. *
  4. * Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 and
  8. * only version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA.
  19. */
  20. #include <generated/compile.h>
  21. #include <linux/kernel.h>
  22. #include <linux/sched.h>
  23. #include <linux/sched/task_stack.h>
  24. #include <linux/mm.h>
  25. #include <linux/smp.h>
  26. #include <linux/errno.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/regset.h>
  29. #include <linux/user.h>
  30. #include <linux/elf.h>
  31. #include <asm/user.h>
  32. #if arch_has_single_step()
  33. /* Both called from ptrace_resume */
  34. void user_enable_single_step(struct task_struct *child)
  35. {
  36. pt_set_singlestep(task_pt_regs(child));
  37. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  38. }
  39. void user_disable_single_step(struct task_struct *child)
  40. {
  41. pt_clr_singlestep(task_pt_regs(child));
  42. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  43. }
  44. #endif
  45. static int genregs_get(struct task_struct *target,
  46. const struct user_regset *regset,
  47. unsigned int pos, unsigned int count,
  48. void *kbuf, void __user *ubuf)
  49. {
  50. int ret;
  51. unsigned int dummy;
  52. struct pt_regs *regs = task_pt_regs(target);
  53. if (!regs)
  54. return -EIO;
  55. /* The general idea here is that the copyout must happen in
  56. * exactly the same order in which the userspace expects these
  57. * regs. Now, the sequence in userspace does not match the
  58. * sequence in the kernel, so everything past the 32 gprs
  59. * happens one at a time.
  60. */
  61. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  62. &regs->r00, 0, 32*sizeof(unsigned long));
  63. #define ONEXT(KPT_REG, USR_REG) \
  64. if (!ret) \
  65. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, \
  66. KPT_REG, offsetof(struct user_regs_struct, USR_REG), \
  67. offsetof(struct user_regs_struct, USR_REG) + \
  68. sizeof(unsigned long));
  69. /* Must be exactly same sequence as struct user_regs_struct */
  70. ONEXT(&regs->sa0, sa0);
  71. ONEXT(&regs->lc0, lc0);
  72. ONEXT(&regs->sa1, sa1);
  73. ONEXT(&regs->lc1, lc1);
  74. ONEXT(&regs->m0, m0);
  75. ONEXT(&regs->m1, m1);
  76. ONEXT(&regs->usr, usr);
  77. ONEXT(&regs->preds, p3_0);
  78. ONEXT(&regs->gp, gp);
  79. ONEXT(&regs->ugp, ugp);
  80. ONEXT(&pt_elr(regs), pc);
  81. dummy = pt_cause(regs);
  82. ONEXT(&dummy, cause);
  83. ONEXT(&pt_badva(regs), badva);
  84. #if CONFIG_HEXAGON_ARCH_VERSION >=4
  85. ONEXT(&regs->cs0, cs0);
  86. ONEXT(&regs->cs1, cs1);
  87. #endif
  88. /* Pad the rest with zeros, if needed */
  89. if (!ret)
  90. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  91. offsetof(struct user_regs_struct, pad1), -1);
  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. int ret;
  100. unsigned long bucket;
  101. struct pt_regs *regs = task_pt_regs(target);
  102. if (!regs)
  103. return -EIO;
  104. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  105. &regs->r00, 0, 32*sizeof(unsigned long));
  106. #define INEXT(KPT_REG, USR_REG) \
  107. if (!ret) \
  108. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, \
  109. KPT_REG, offsetof(struct user_regs_struct, USR_REG), \
  110. offsetof(struct user_regs_struct, USR_REG) + \
  111. sizeof(unsigned long));
  112. /* Must be exactly same sequence as struct user_regs_struct */
  113. INEXT(&regs->sa0, sa0);
  114. INEXT(&regs->lc0, lc0);
  115. INEXT(&regs->sa1, sa1);
  116. INEXT(&regs->lc1, lc1);
  117. INEXT(&regs->m0, m0);
  118. INEXT(&regs->m1, m1);
  119. INEXT(&regs->usr, usr);
  120. INEXT(&regs->preds, p3_0);
  121. INEXT(&regs->gp, gp);
  122. INEXT(&regs->ugp, ugp);
  123. INEXT(&pt_elr(regs), pc);
  124. /* CAUSE and BADVA aren't writeable. */
  125. INEXT(&bucket, cause);
  126. INEXT(&bucket, badva);
  127. #if CONFIG_HEXAGON_ARCH_VERSION >=4
  128. INEXT(&regs->cs0, cs0);
  129. INEXT(&regs->cs1, cs1);
  130. #endif
  131. /* Ignore the rest, if needed */
  132. if (!ret)
  133. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  134. offsetof(struct user_regs_struct, pad1), -1);
  135. if (ret)
  136. return ret;
  137. /*
  138. * This is special; SP is actually restored by the VM via the
  139. * special event record which is set by the special trap.
  140. */
  141. regs->hvmer.vmpsp = regs->r29;
  142. return 0;
  143. }
  144. enum hexagon_regset {
  145. REGSET_GENERAL,
  146. };
  147. static const struct user_regset hexagon_regsets[] = {
  148. [REGSET_GENERAL] = {
  149. .core_note_type = NT_PRSTATUS,
  150. .n = ELF_NGREG,
  151. .size = sizeof(unsigned long),
  152. .align = sizeof(unsigned long),
  153. .get = genregs_get,
  154. .set = genregs_set,
  155. },
  156. };
  157. static const struct user_regset_view hexagon_user_view = {
  158. .name = UTS_MACHINE,
  159. .e_machine = ELF_ARCH,
  160. .ei_osabi = ELF_OSABI,
  161. .regsets = hexagon_regsets,
  162. .e_flags = ELF_CORE_EFLAGS,
  163. .n = ARRAY_SIZE(hexagon_regsets)
  164. };
  165. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  166. {
  167. return &hexagon_user_view;
  168. }
  169. void ptrace_disable(struct task_struct *child)
  170. {
  171. /* Boilerplate - resolves to null inline if no HW single-step */
  172. user_disable_single_step(child);
  173. }
  174. long arch_ptrace(struct task_struct *child, long request,
  175. unsigned long addr, unsigned long data)
  176. {
  177. return ptrace_request(child, request, addr, data);
  178. }