ptrace.c 4.4 KB

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