ptrace.c 5.3 KB

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