ptrace.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. for (r = 0; r < ARRAY_SIZE(register_offset); r++)
  89. *reg++ = h8300_get_reg(target, r);
  90. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  91. &regs, 0, sizeof(regs));
  92. }
  93. static int regs_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 r;
  99. int ret;
  100. struct user_regs_struct regs;
  101. long *reg;
  102. /* build user regs in buffer */
  103. for (reg = (long *)&regs, r = 0; r < ARRAY_SIZE(register_offset); r++)
  104. *reg++ = h8300_get_reg(target, r);
  105. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  106. &regs, 0, sizeof(regs));
  107. if (ret)
  108. return ret;
  109. /* write back to pt_regs */
  110. for (reg = (long *)&regs, r = 0; r < ARRAY_SIZE(register_offset); r++)
  111. h8300_put_reg(target, r, *reg++);
  112. return 0;
  113. }
  114. enum h8300_regset {
  115. REGSET_GENERAL,
  116. };
  117. static const struct user_regset h8300_regsets[] = {
  118. [REGSET_GENERAL] = {
  119. .core_note_type = NT_PRSTATUS,
  120. .n = ELF_NGREG,
  121. .size = sizeof(long),
  122. .align = sizeof(long),
  123. .get = regs_get,
  124. .set = regs_set,
  125. },
  126. };
  127. static const struct user_regset_view user_h8300_native_view = {
  128. .name = "h8300",
  129. .e_machine = EM_H8_300,
  130. .regsets = h8300_regsets,
  131. .n = ARRAY_SIZE(h8300_regsets),
  132. };
  133. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  134. {
  135. return &user_h8300_native_view;
  136. }
  137. void ptrace_disable(struct task_struct *child)
  138. {
  139. user_disable_single_step(child);
  140. }
  141. long arch_ptrace(struct task_struct *child, long request,
  142. unsigned long addr, unsigned long data)
  143. {
  144. int ret;
  145. switch (request) {
  146. default:
  147. ret = ptrace_request(child, request, addr, data);
  148. break;
  149. }
  150. return ret;
  151. }
  152. asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
  153. {
  154. long ret = 0;
  155. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  156. tracehook_report_syscall_entry(regs))
  157. /*
  158. * Tracing decided this syscall should not happen.
  159. * We'll return a bogus call number to get an ENOSYS
  160. * error, but leave the original number in regs->regs[0].
  161. */
  162. ret = -1L;
  163. audit_syscall_entry(regs->er1, regs->er2, regs->er3,
  164. regs->er4, regs->er5);
  165. return ret ?: regs->er0;
  166. }
  167. asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
  168. {
  169. int step;
  170. audit_syscall_exit(regs);
  171. step = test_thread_flag(TIF_SINGLESTEP);
  172. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  173. tracehook_report_syscall_exit(regs, step);
  174. }