ptrace.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/tracehook.h>
  24. #include <linux/elf.h>
  25. #include <asm/thread_info.h>
  26. #include <asm/page.h>
  27. /*
  28. * Copy the thread state to a regset that can be interpreted by userspace.
  29. *
  30. * It doesn't matter what our internal pt_regs structure looks like. The
  31. * important thing is that we export a consistent view of the thread state
  32. * to userspace. As such, we need to make sure that the regset remains
  33. * ABI compatible as defined by the struct user_regs_struct:
  34. *
  35. * (Each item is a 32-bit word)
  36. * r0 = 0 (exported for clarity)
  37. * 31 GPRS r1-r31
  38. * PC (Program counter)
  39. * SR (Supervision register)
  40. */
  41. static int genregs_get(struct task_struct *target,
  42. const struct user_regset *regset,
  43. struct membuf to)
  44. {
  45. const struct pt_regs *regs = task_pt_regs(target);
  46. /* r0 */
  47. membuf_zero(&to, 4);
  48. membuf_write(&to, regs->gpr + 1, 31 * 4);
  49. membuf_store(&to, regs->pc);
  50. return membuf_store(&to, regs->sr);
  51. }
  52. /*
  53. * Set the thread state from a regset passed in via ptrace
  54. */
  55. static int genregs_set(struct task_struct *target,
  56. const struct user_regset *regset,
  57. unsigned int pos, unsigned int count,
  58. const void *kbuf, const void __user * ubuf)
  59. {
  60. struct pt_regs *regs = task_pt_regs(target);
  61. int ret;
  62. /* ignore r0 */
  63. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, 0, 4);
  64. /* r1 - r31 */
  65. if (!ret)
  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. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  78. 4*33, -1);
  79. return ret;
  80. }
  81. /*
  82. * Define the register sets available on OpenRISC under Linux
  83. */
  84. enum or1k_regset {
  85. REGSET_GENERAL,
  86. };
  87. static const struct user_regset or1k_regsets[] = {
  88. [REGSET_GENERAL] = {
  89. .core_note_type = NT_PRSTATUS,
  90. .n = ELF_NGREG,
  91. .size = sizeof(long),
  92. .align = sizeof(long),
  93. .regset_get = genregs_get,
  94. .set = genregs_set,
  95. },
  96. };
  97. static const struct user_regset_view user_or1k_native_view = {
  98. .name = "or1k",
  99. .e_machine = EM_OPENRISC,
  100. .regsets = or1k_regsets,
  101. .n = ARRAY_SIZE(or1k_regsets),
  102. };
  103. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  104. {
  105. return &user_or1k_native_view;
  106. }
  107. /*
  108. * does not yet catch signals sent when the child dies.
  109. * in exit.c or in signal.c.
  110. */
  111. /*
  112. * Called by kernel/ptrace.c when detaching..
  113. *
  114. * Make sure the single step bit is not set.
  115. */
  116. void ptrace_disable(struct task_struct *child)
  117. {
  118. pr_debug("ptrace_disable(): TODO\n");
  119. user_disable_single_step(child);
  120. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  121. }
  122. long arch_ptrace(struct task_struct *child, long request, unsigned long addr,
  123. unsigned long data)
  124. {
  125. int ret;
  126. switch (request) {
  127. default:
  128. ret = ptrace_request(child, request, addr, data);
  129. break;
  130. }
  131. return ret;
  132. }
  133. /*
  134. * Notification of system call entry/exit
  135. * - triggered by current->work.syscall_trace
  136. */
  137. asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
  138. {
  139. long ret = 0;
  140. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  141. tracehook_report_syscall_entry(regs))
  142. /*
  143. * Tracing decided this syscall should not happen.
  144. * We'll return a bogus call number to get an ENOSYS
  145. * error, but leave the original number in <something>.
  146. */
  147. ret = -1L;
  148. audit_syscall_entry(regs->gpr[11], regs->gpr[3], regs->gpr[4],
  149. regs->gpr[5], regs->gpr[6]);
  150. return ret ? : regs->gpr[11];
  151. }
  152. asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
  153. {
  154. int step;
  155. audit_syscall_exit(regs);
  156. step = test_thread_flag(TIF_SINGLESTEP);
  157. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  158. tracehook_report_syscall_exit(regs, step);
  159. }