ptrace.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/mm.h>
  24. #include <linux/smp.h>
  25. #include <linux/errno.h>
  26. #include <linux/ptrace.h>
  27. #include <linux/regset.h>
  28. #include <linux/user.h>
  29. #include <linux/elf.h>
  30. #include <asm/user.h>
  31. #if arch_has_single_step()
  32. /* Both called from ptrace_resume */
  33. void user_enable_single_step(struct task_struct *child)
  34. {
  35. pt_set_singlestep(task_pt_regs(child));
  36. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  37. }
  38. void user_disable_single_step(struct task_struct *child)
  39. {
  40. pt_clr_singlestep(task_pt_regs(child));
  41. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  42. }
  43. #endif
  44. static int genregs_get(struct task_struct *target,
  45. const struct user_regset *regset,
  46. unsigned int pos, unsigned int count,
  47. void *kbuf, void __user *ubuf)
  48. {
  49. int ret;
  50. unsigned int dummy;
  51. struct pt_regs *regs = task_pt_regs(target);
  52. if (!regs)
  53. return -EIO;
  54. /* The general idea here is that the copyout must happen in
  55. * exactly the same order in which the userspace expects these
  56. * regs. Now, the sequence in userspace does not match the
  57. * sequence in the kernel, so everything past the 32 gprs
  58. * happens one at a time.
  59. */
  60. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  61. &regs->r00, 0, 32*sizeof(unsigned long));
  62. #define ONEXT(KPT_REG, USR_REG) \
  63. if (!ret) \
  64. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, \
  65. KPT_REG, offsetof(struct user_regs_struct, USR_REG), \
  66. offsetof(struct user_regs_struct, USR_REG) + \
  67. sizeof(unsigned long));
  68. /* Must be exactly same sequence as struct user_regs_struct */
  69. ONEXT(&regs->sa0, sa0);
  70. ONEXT(&regs->lc0, lc0);
  71. ONEXT(&regs->sa1, sa1);
  72. ONEXT(&regs->lc1, lc1);
  73. ONEXT(&regs->m0, m0);
  74. ONEXT(&regs->m1, m1);
  75. ONEXT(&regs->usr, usr);
  76. ONEXT(&regs->preds, p3_0);
  77. ONEXT(&regs->gp, gp);
  78. ONEXT(&regs->ugp, ugp);
  79. ONEXT(&pt_elr(regs), pc);
  80. dummy = pt_cause(regs);
  81. ONEXT(&dummy, cause);
  82. ONEXT(&pt_badva(regs), badva);
  83. #if CONFIG_HEXAGON_ARCH_VERSION >=4
  84. ONEXT(&regs->cs0, cs0);
  85. ONEXT(&regs->cs1, cs1);
  86. #endif
  87. /* Pad the rest with zeros, if needed */
  88. if (!ret)
  89. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  90. offsetof(struct user_regs_struct, pad1), -1);
  91. return ret;
  92. }
  93. static int genregs_set(struct task_struct *target,
  94. const struct user_regset *regset,
  95. unsigned int pos, unsigned int count,
  96. const void *kbuf, const void __user *ubuf)
  97. {
  98. int ret;
  99. unsigned long bucket;
  100. struct pt_regs *regs = task_pt_regs(target);
  101. if (!regs)
  102. return -EIO;
  103. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  104. &regs->r00, 0, 32*sizeof(unsigned long));
  105. #define INEXT(KPT_REG, USR_REG) \
  106. if (!ret) \
  107. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, \
  108. KPT_REG, offsetof(struct user_regs_struct, USR_REG), \
  109. offsetof(struct user_regs_struct, USR_REG) + \
  110. sizeof(unsigned long));
  111. /* Must be exactly same sequence as struct user_regs_struct */
  112. INEXT(&regs->sa0, sa0);
  113. INEXT(&regs->lc0, lc0);
  114. INEXT(&regs->sa1, sa1);
  115. INEXT(&regs->lc1, lc1);
  116. INEXT(&regs->m0, m0);
  117. INEXT(&regs->m1, m1);
  118. INEXT(&regs->usr, usr);
  119. INEXT(&regs->preds, p3_0);
  120. INEXT(&regs->gp, gp);
  121. INEXT(&regs->ugp, ugp);
  122. INEXT(&pt_elr(regs), pc);
  123. /* CAUSE and BADVA aren't writeable. */
  124. INEXT(&bucket, cause);
  125. INEXT(&bucket, badva);
  126. #if CONFIG_HEXAGON_ARCH_VERSION >=4
  127. INEXT(&regs->cs0, cs0);
  128. INEXT(&regs->cs1, cs1);
  129. #endif
  130. /* Ignore the rest, if needed */
  131. if (!ret)
  132. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  133. offsetof(struct user_regs_struct, pad1), -1);
  134. if (ret)
  135. return ret;
  136. /*
  137. * This is special; SP is actually restored by the VM via the
  138. * special event record which is set by the special trap.
  139. */
  140. regs->hvmer.vmpsp = regs->r29;
  141. return 0;
  142. }
  143. enum hexagon_regset {
  144. REGSET_GENERAL,
  145. };
  146. static const struct user_regset hexagon_regsets[] = {
  147. [REGSET_GENERAL] = {
  148. .core_note_type = NT_PRSTATUS,
  149. .n = ELF_NGREG,
  150. .size = sizeof(unsigned long),
  151. .align = sizeof(unsigned long),
  152. .get = genregs_get,
  153. .set = genregs_set,
  154. },
  155. };
  156. static const struct user_regset_view hexagon_user_view = {
  157. .name = UTS_MACHINE,
  158. .e_machine = ELF_ARCH,
  159. .ei_osabi = ELF_OSABI,
  160. .regsets = hexagon_regsets,
  161. .e_flags = ELF_CORE_EFLAGS,
  162. .n = ARRAY_SIZE(hexagon_regsets)
  163. };
  164. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  165. {
  166. return &hexagon_user_view;
  167. }
  168. void ptrace_disable(struct task_struct *child)
  169. {
  170. /* Boilerplate - resolves to null inline if no HW single-step */
  171. user_disable_single_step(child);
  172. }
  173. long arch_ptrace(struct task_struct *child, long request,
  174. unsigned long addr, unsigned long data)
  175. {
  176. return ptrace_request(child, request, addr, data);
  177. }