ptrace.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * `ptrace' system call
  3. *
  4. * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
  5. * Copyright (C) 2007-2009 PetaLogix
  6. * Copyright (C) 2004-2007 John Williams <john.williams@petalogix.com>
  7. *
  8. * derived from arch/v850/kernel/ptrace.c
  9. *
  10. * Copyright (C) 2002,03 NEC Electronics Corporation
  11. * Copyright (C) 2002,03 Miles Bader <miles@gnu.org>
  12. *
  13. * Derived from arch/mips/kernel/ptrace.c:
  14. *
  15. * Copyright (C) 1992 Ross Biro
  16. * Copyright (C) Linus Torvalds
  17. * Copyright (C) 1994, 95, 96, 97, 98, 2000 Ralf Baechle
  18. * Copyright (C) 1996 David S. Miller
  19. * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
  20. * Copyright (C) 1999 MIPS Technologies, Inc.
  21. *
  22. * This file is subject to the terms and conditions of the GNU General
  23. * Public License. See the file COPYING in the main directory of this
  24. * archive for more details.
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/mm.h>
  28. #include <linux/sched.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/signal.h>
  31. #include <linux/elf.h>
  32. #include <linux/audit.h>
  33. #include <linux/seccomp.h>
  34. #include <linux/tracehook.h>
  35. #include <linux/errno.h>
  36. #include <asm/processor.h>
  37. #include <linux/uaccess.h>
  38. #include <asm/asm-offsets.h>
  39. #include <asm/cacheflush.h>
  40. #include <asm/io.h>
  41. /* Returns the address where the register at REG_OFFS in P is stashed away. */
  42. static microblaze_reg_t *reg_save_addr(unsigned reg_offs,
  43. struct task_struct *t)
  44. {
  45. struct pt_regs *regs;
  46. /*
  47. * Three basic cases:
  48. *
  49. * (1) A register normally saved before calling the scheduler, is
  50. * available in the kernel entry pt_regs structure at the top
  51. * of the kernel stack. The kernel trap/irq exit path takes
  52. * care to save/restore almost all registers for ptrace'd
  53. * processes.
  54. *
  55. * (2) A call-clobbered register, where the process P entered the
  56. * kernel via [syscall] trap, is not stored anywhere; that's
  57. * OK, because such registers are not expected to be preserved
  58. * when the trap returns anyway (so we don't actually bother to
  59. * test for this case).
  60. *
  61. * (3) A few registers not used at all by the kernel, and so
  62. * normally never saved except by context-switches, are in the
  63. * context switch state.
  64. */
  65. /* Register saved during kernel entry (or not available). */
  66. regs = task_pt_regs(t);
  67. return (microblaze_reg_t *)((char *)regs + reg_offs);
  68. }
  69. long arch_ptrace(struct task_struct *child, long request,
  70. unsigned long addr, unsigned long data)
  71. {
  72. int rval;
  73. unsigned long val = 0;
  74. switch (request) {
  75. /* Read/write the word at location ADDR in the registers. */
  76. case PTRACE_PEEKUSR:
  77. case PTRACE_POKEUSR:
  78. pr_debug("PEEKUSR/POKEUSR : 0x%08lx\n", addr);
  79. rval = 0;
  80. if (addr >= PT_SIZE && request == PTRACE_PEEKUSR) {
  81. /*
  82. * Special requests that don't actually correspond
  83. * to offsets in struct pt_regs.
  84. */
  85. if (addr == PT_TEXT_ADDR) {
  86. val = child->mm->start_code;
  87. } else if (addr == PT_DATA_ADDR) {
  88. val = child->mm->start_data;
  89. } else if (addr == PT_TEXT_LEN) {
  90. val = child->mm->end_code
  91. - child->mm->start_code;
  92. } else {
  93. rval = -EIO;
  94. }
  95. } else if (addr < PT_SIZE && (addr & 0x3) == 0) {
  96. microblaze_reg_t *reg_addr = reg_save_addr(addr, child);
  97. if (request == PTRACE_PEEKUSR)
  98. val = *reg_addr;
  99. else {
  100. #if 1
  101. *reg_addr = data;
  102. #else
  103. /* MS potential problem on WB system
  104. * Be aware that reg_addr is virtual address
  105. * virt_to_phys conversion is necessary.
  106. * This could be sensible solution.
  107. */
  108. u32 paddr = virt_to_phys((u32)reg_addr);
  109. invalidate_icache_range(paddr, paddr + 4);
  110. *reg_addr = data;
  111. flush_dcache_range(paddr, paddr + 4);
  112. #endif
  113. }
  114. } else
  115. rval = -EIO;
  116. if (rval == 0 && request == PTRACE_PEEKUSR)
  117. rval = put_user(val, (unsigned long *)data);
  118. break;
  119. default:
  120. rval = ptrace_request(child, request, addr, data);
  121. }
  122. return rval;
  123. }
  124. asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
  125. {
  126. long ret = 0;
  127. secure_computing(regs->r12);
  128. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  129. tracehook_report_syscall_entry(regs))
  130. /*
  131. * Tracing decided this syscall should not happen.
  132. * We'll return a bogus call number to get an ENOSYS
  133. * error, but leave the original number in regs->regs[0].
  134. */
  135. ret = -1L;
  136. if (unlikely(current->audit_context))
  137. audit_syscall_entry(EM_XILINX_MICROBLAZE, regs->r12,
  138. regs->r5, regs->r6,
  139. regs->r7, regs->r8);
  140. return ret ?: regs->r12;
  141. }
  142. asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
  143. {
  144. int step;
  145. if (unlikely(current->audit_context))
  146. audit_syscall_exit(AUDITSC_RESULT(regs->r3), regs->r3);
  147. step = test_thread_flag(TIF_SINGLESTEP);
  148. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  149. tracehook_report_syscall_exit(regs, step);
  150. }
  151. #if 0
  152. static asmlinkage void syscall_trace(void)
  153. {
  154. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  155. return;
  156. if (!(current->ptrace & PT_PTRACED))
  157. return;
  158. /* The 0x80 provides a way for the tracing parent to distinguish
  159. between a syscall stop and SIGTRAP delivery */
  160. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  161. ? 0x80 : 0));
  162. /*
  163. * this isn't the same as continuing with a signal, but it will do
  164. * for normal use. strace only continues with a signal if the
  165. * stopping signal is not SIGTRAP. -brl
  166. */
  167. if (current->exit_code) {
  168. send_sig(current->exit_code, current, 1);
  169. current->exit_code = 0;
  170. }
  171. }
  172. #endif
  173. void ptrace_disable(struct task_struct *child)
  174. {
  175. /* nothing to do */
  176. }