ptrace.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * linux/arch/h8300/kernel/ptrace.c
  3. *
  4. * Copyright 2015 Yoshinori Sato <ysato@users.sourceforge.jp>
  5. *
  6. * This file is subject to the terms and conditions of the GNU General
  7. * Public License. See the file COPYING in the main directory of
  8. * this archive for more details.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/ptrace.h>
  13. #include <linux/audit.h>
  14. #include <linux/tracehook.h>
  15. #include <linux/regset.h>
  16. #include <linux/elf.h>
  17. #define CCR_MASK 0x6f /* mode/imask not set */
  18. #define EXR_MASK 0x80 /* modify only T */
  19. #define PT_REG(r) offsetof(struct pt_regs, r)
  20. extern void user_disable_single_step(struct task_struct *child);
  21. /* Mapping from PT_xxx to the stack offset at which the register is
  22. saved. Notice that usp has no stack-slot and needs to be treated
  23. specially (see get_reg/put_reg below). */
  24. static const int register_offset[] = {
  25. PT_REG(er1), PT_REG(er2), PT_REG(er3), PT_REG(er4),
  26. PT_REG(er5), PT_REG(er6), PT_REG(er0), -1,
  27. PT_REG(orig_er0), PT_REG(ccr), PT_REG(pc),
  28. #if defined(CONFIG_CPU_H8S)
  29. PT_REG(exr),
  30. #endif
  31. };
  32. /* read register */
  33. long h8300_get_reg(struct task_struct *task, int regno)
  34. {
  35. switch (regno) {
  36. case PT_USP:
  37. return task->thread.usp + sizeof(long)*2;
  38. case PT_CCR:
  39. case PT_EXR:
  40. return *(unsigned short *)(task->thread.esp0 +
  41. register_offset[regno]);
  42. default:
  43. return *(unsigned long *)(task->thread.esp0 +
  44. register_offset[regno]);
  45. }
  46. }
  47. int h8300_put_reg(struct task_struct *task, int regno, unsigned long data)
  48. {
  49. unsigned short oldccr;
  50. unsigned short oldexr;
  51. switch (regno) {
  52. case PT_USP:
  53. task->thread.usp = data - sizeof(long)*2;
  54. case PT_CCR:
  55. oldccr = *(unsigned short *)(task->thread.esp0 +
  56. register_offset[regno]);
  57. oldccr &= ~CCR_MASK;
  58. data &= CCR_MASK;
  59. data |= oldccr;
  60. *(unsigned short *)(task->thread.esp0 +
  61. register_offset[regno]) = data;
  62. break;
  63. case PT_EXR:
  64. oldexr = *(unsigned short *)(task->thread.esp0 +
  65. register_offset[regno]);
  66. oldccr &= ~EXR_MASK;
  67. data &= EXR_MASK;
  68. data |= oldexr;
  69. *(unsigned short *)(task->thread.esp0 +
  70. register_offset[regno]) = data;
  71. break;
  72. default:
  73. *(unsigned long *)(task->thread.esp0 +
  74. register_offset[regno]) = data;
  75. break;
  76. }
  77. return 0;
  78. }
  79. static int regs_get(struct task_struct *target,
  80. const struct user_regset *regset,
  81. unsigned int pos, unsigned int count,
  82. void *kbuf, void __user *ubuf)
  83. {
  84. int r;
  85. struct user_regs_struct regs;
  86. long *reg = (long *)&regs;
  87. /* build user regs in buffer */
  88. BUILD_BUG_ON(sizeof(regs) % sizeof(long) != 0);
  89. for (r = 0; r < sizeof(regs) / sizeof(long); r++)
  90. *reg++ = h8300_get_reg(target, r);
  91. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  92. &regs, 0, sizeof(regs));
  93. }
  94. static int regs_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 r;
  100. int ret;
  101. struct user_regs_struct regs;
  102. long *reg;
  103. /* build user regs in buffer */
  104. BUILD_BUG_ON(sizeof(regs) % sizeof(long) != 0);
  105. for (reg = (long *)&regs, r = 0; r < sizeof(regs) / sizeof(long); r++)
  106. *reg++ = h8300_get_reg(target, r);
  107. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  108. &regs, 0, sizeof(regs));
  109. if (ret)
  110. return ret;
  111. /* write back to pt_regs */
  112. for (reg = (long *)&regs, r = 0; r < sizeof(regs) / sizeof(long); r++)
  113. h8300_put_reg(target, r, *reg++);
  114. return 0;
  115. }
  116. enum h8300_regset {
  117. REGSET_GENERAL,
  118. };
  119. static const struct user_regset h8300_regsets[] = {
  120. [REGSET_GENERAL] = {
  121. .core_note_type = NT_PRSTATUS,
  122. .n = ELF_NGREG,
  123. .size = sizeof(long),
  124. .align = sizeof(long),
  125. .get = regs_get,
  126. .set = regs_set,
  127. },
  128. };
  129. static const struct user_regset_view user_h8300_native_view = {
  130. .name = "h8300",
  131. .e_machine = EM_H8_300,
  132. .regsets = h8300_regsets,
  133. .n = ARRAY_SIZE(h8300_regsets),
  134. };
  135. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  136. {
  137. return &user_h8300_native_view;
  138. }
  139. void ptrace_disable(struct task_struct *child)
  140. {
  141. user_disable_single_step(child);
  142. }
  143. long arch_ptrace(struct task_struct *child, long request,
  144. unsigned long addr, unsigned long data)
  145. {
  146. int ret;
  147. switch (request) {
  148. default:
  149. ret = ptrace_request(child, request, addr, data);
  150. break;
  151. }
  152. return ret;
  153. }
  154. asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
  155. {
  156. long ret = 0;
  157. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  158. tracehook_report_syscall_entry(regs))
  159. /*
  160. * Tracing decided this syscall should not happen.
  161. * We'll return a bogus call number to get an ENOSYS
  162. * error, but leave the original number in regs->regs[0].
  163. */
  164. ret = -1L;
  165. audit_syscall_entry(regs->er1, regs->er2, regs->er3,
  166. regs->er4, regs->er5);
  167. return ret ?: regs->er0;
  168. }
  169. asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
  170. {
  171. int step;
  172. audit_syscall_exit(regs);
  173. step = test_thread_flag(TIF_SINGLESTEP);
  174. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  175. tracehook_report_syscall_exit(regs, step);
  176. }