ptrace.c 5.1 KB

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