ptrace.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * OpenRISC ptrace.c
  4. *
  5. * Linux architectural port borrowing liberally from similar works of
  6. * others. All original copyrights apply as per the original source
  7. * declaration.
  8. *
  9. * Modifications for the OpenRISC architecture:
  10. * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
  11. * Copyright (C) 2005 Gyorgy Jeney <nog@bsemi.com>
  12. * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/sched.h>
  16. #include <linux/sched/task_stack.h>
  17. #include <linux/string.h>
  18. #include <linux/mm.h>
  19. #include <linux/errno.h>
  20. #include <linux/ptrace.h>
  21. #include <linux/audit.h>
  22. #include <linux/regset.h>
  23. #include <linux/elf.h>
  24. #include <asm/thread_info.h>
  25. #include <asm/page.h>
  26. asmlinkage long do_syscall_trace_enter(struct pt_regs *regs);
  27. asmlinkage void do_syscall_trace_leave(struct pt_regs *regs);
  28. /*
  29. * Copy the thread state to a regset that can be interpreted by userspace.
  30. *
  31. * It doesn't matter what our internal pt_regs structure looks like. The
  32. * important thing is that we export a consistent view of the thread state
  33. * to userspace. As such, we need to make sure that the regset remains
  34. * ABI compatible as defined by the struct user_regs_struct:
  35. *
  36. * (Each item is a 32-bit word)
  37. * r0 = 0 (exported for clarity)
  38. * 31 GPRS r1-r31
  39. * PC (Program counter)
  40. * SR (Supervision register)
  41. */
  42. static int genregs_get(struct task_struct *target,
  43. const struct user_regset *regset,
  44. struct membuf to)
  45. {
  46. const struct pt_regs *regs = task_pt_regs(target);
  47. /* r0 */
  48. membuf_zero(&to, 4);
  49. membuf_write(&to, regs->gpr + 1, 31 * 4);
  50. membuf_store(&to, regs->pc);
  51. return membuf_store(&to, regs->sr);
  52. }
  53. /*
  54. * Set the thread state from a regset passed in via ptrace
  55. */
  56. static int genregs_set(struct task_struct *target,
  57. const struct user_regset *regset,
  58. unsigned int pos, unsigned int count,
  59. const void *kbuf, const void __user * ubuf)
  60. {
  61. struct pt_regs *regs = task_pt_regs(target);
  62. int ret;
  63. /* ignore r0 */
  64. user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, 0, 4);
  65. /* r1 - r31 */
  66. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  67. regs->gpr+1, 4, 4*32);
  68. /* PC */
  69. if (!ret)
  70. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  71. &regs->pc, 4*32, 4*33);
  72. /*
  73. * Skip SR and padding... userspace isn't allowed to changes bits in
  74. * the Supervision register
  75. */
  76. if (!ret)
  77. user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, 4*33, -1);
  78. return ret;
  79. }
  80. #ifdef CONFIG_FPU
  81. /*
  82. * As OpenRISC shares GPRs and floating point registers we don't need to export
  83. * the floating point registers again. So here we only export the fpcsr special
  84. * purpose register.
  85. */
  86. static int fpregs_get(struct task_struct *target,
  87. const struct user_regset *regset,
  88. struct membuf to)
  89. {
  90. return membuf_store(&to, target->thread.fpcsr);
  91. }
  92. static int fpregs_set(struct task_struct *target,
  93. const struct user_regset *regset,
  94. unsigned int pos, unsigned int count,
  95. const void *kbuf, const void __user *ubuf)
  96. {
  97. /* FPCSR */
  98. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  99. &target->thread.fpcsr, 0, 4);
  100. }
  101. #endif
  102. /*
  103. * Define the register sets available on OpenRISC under Linux
  104. */
  105. enum or1k_regset {
  106. REGSET_GENERAL,
  107. #ifdef CONFIG_FPU
  108. REGSET_FPU,
  109. #endif
  110. };
  111. static const struct user_regset or1k_regsets[] = {
  112. [REGSET_GENERAL] = {
  113. .core_note_type = NT_PRSTATUS,
  114. .n = ELF_NGREG,
  115. .size = sizeof(long),
  116. .align = sizeof(long),
  117. .regset_get = genregs_get,
  118. .set = genregs_set,
  119. },
  120. #ifdef CONFIG_FPU
  121. [REGSET_FPU] = {
  122. .core_note_type = NT_PRFPREG,
  123. .n = sizeof(struct __or1k_fpu_state) / sizeof(long),
  124. .size = sizeof(long),
  125. .align = sizeof(long),
  126. .regset_get = fpregs_get,
  127. .set = fpregs_set,
  128. },
  129. #endif
  130. };
  131. static const struct user_regset_view user_or1k_native_view = {
  132. .name = "or1k",
  133. .e_machine = EM_OPENRISC,
  134. .regsets = or1k_regsets,
  135. .n = ARRAY_SIZE(or1k_regsets),
  136. };
  137. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  138. {
  139. return &user_or1k_native_view;
  140. }
  141. /*
  142. * does not yet catch signals sent when the child dies.
  143. * in exit.c or in signal.c.
  144. */
  145. /*
  146. * Called by kernel/ptrace.c when detaching..
  147. *
  148. * Make sure the single step bit is not set.
  149. */
  150. void ptrace_disable(struct task_struct *child)
  151. {
  152. pr_debug("ptrace_disable(): TODO\n");
  153. user_disable_single_step(child);
  154. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  155. }
  156. long arch_ptrace(struct task_struct *child, long request, unsigned long addr,
  157. unsigned long data)
  158. {
  159. int ret;
  160. switch (request) {
  161. default:
  162. ret = ptrace_request(child, request, addr, data);
  163. break;
  164. }
  165. return ret;
  166. }
  167. /*
  168. * Notification of system call entry/exit
  169. * - triggered by current->work.syscall_trace
  170. */
  171. asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
  172. {
  173. long ret = 0;
  174. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  175. ptrace_report_syscall_entry(regs))
  176. /*
  177. * Tracing decided this syscall should not happen.
  178. * We'll return a bogus call number to get an ENOSYS
  179. * error, but leave the original number in <something>.
  180. */
  181. ret = -1L;
  182. audit_syscall_entry(regs->gpr[11], regs->gpr[3], regs->gpr[4],
  183. regs->gpr[5], regs->gpr[6]);
  184. return ret ? : regs->gpr[11];
  185. }
  186. asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
  187. {
  188. int step;
  189. audit_syscall_exit(regs);
  190. step = test_thread_flag(TIF_SINGLESTEP);
  191. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  192. ptrace_report_syscall_exit(regs, step);
  193. }