ptrace.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1992 Ross Biro
  7. * Copyright (C) Linus Torvalds
  8. * Copyright (C) 1994, 95, 96, 97, 98, 2000 Ralf Baechle
  9. * Copyright (C) 1996 David S. Miller
  10. * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
  11. * Copyright (C) 1999 MIPS Technologies, Inc.
  12. * Copyright (C) 2000 Ulf Carlsson
  13. *
  14. * At this time Linux/MIPS64 only supports syscall tracing, even for 32-bit
  15. * binaries.
  16. */
  17. #include <linux/config.h>
  18. #include <linux/compiler.h>
  19. #include <linux/kernel.h>
  20. #include <linux/sched.h>
  21. #include <linux/mm.h>
  22. #include <linux/errno.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/audit.h>
  25. #include <linux/smp.h>
  26. #include <linux/smp_lock.h>
  27. #include <linux/user.h>
  28. #include <linux/security.h>
  29. #include <asm/cpu.h>
  30. #include <asm/fpu.h>
  31. #include <asm/mipsregs.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/page.h>
  34. #include <asm/system.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/bootinfo.h>
  37. /*
  38. * Called by kernel/ptrace.c when detaching..
  39. *
  40. * Make sure single step bits etc are not set.
  41. */
  42. void ptrace_disable(struct task_struct *child)
  43. {
  44. /* Nothing to do.. */
  45. }
  46. asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
  47. {
  48. struct task_struct *child;
  49. int ret;
  50. #if 0
  51. printk("ptrace(r=%d,pid=%d,addr=%08lx,data=%08lx)\n",
  52. (int) request, (int) pid, (unsigned long) addr,
  53. (unsigned long) data);
  54. #endif
  55. lock_kernel();
  56. ret = -EPERM;
  57. if (request == PTRACE_TRACEME) {
  58. /* are we already being traced? */
  59. if (current->ptrace & PT_PTRACED)
  60. goto out;
  61. if ((ret = security_ptrace(current->parent, current)))
  62. goto out;
  63. /* set the ptrace bit in the process flags. */
  64. current->ptrace |= PT_PTRACED;
  65. ret = 0;
  66. goto out;
  67. }
  68. ret = -ESRCH;
  69. read_lock(&tasklist_lock);
  70. child = find_task_by_pid(pid);
  71. if (child)
  72. get_task_struct(child);
  73. read_unlock(&tasklist_lock);
  74. if (!child)
  75. goto out;
  76. ret = -EPERM;
  77. if (pid == 1) /* you may not mess with init */
  78. goto out_tsk;
  79. if (request == PTRACE_ATTACH) {
  80. ret = ptrace_attach(child);
  81. goto out_tsk;
  82. }
  83. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  84. if (ret < 0)
  85. goto out_tsk;
  86. switch (request) {
  87. /* when I and D space are separate, these will need to be fixed. */
  88. case PTRACE_PEEKTEXT: /* read word at location addr. */
  89. case PTRACE_PEEKDATA: {
  90. unsigned long tmp;
  91. int copied;
  92. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  93. ret = -EIO;
  94. if (copied != sizeof(tmp))
  95. break;
  96. ret = put_user(tmp,(unsigned long *) data);
  97. break;
  98. }
  99. /* Read the word at location addr in the USER area. */
  100. case PTRACE_PEEKUSR: {
  101. struct pt_regs *regs;
  102. unsigned long tmp = 0;
  103. regs = (struct pt_regs *) ((unsigned long) child->thread_info +
  104. THREAD_SIZE - 32 - sizeof(struct pt_regs));
  105. ret = 0; /* Default return value. */
  106. switch (addr) {
  107. case 0 ... 31:
  108. tmp = regs->regs[addr];
  109. break;
  110. case FPR_BASE ... FPR_BASE + 31:
  111. if (tsk_used_math(child)) {
  112. fpureg_t *fregs = get_fpu_regs(child);
  113. #ifdef CONFIG_MIPS32
  114. /*
  115. * The odd registers are actually the high
  116. * order bits of the values stored in the even
  117. * registers - unless we're using r2k_switch.S.
  118. */
  119. if (addr & 1)
  120. tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
  121. else
  122. tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
  123. #endif
  124. #ifdef CONFIG_MIPS64
  125. tmp = fregs[addr - FPR_BASE];
  126. #endif
  127. } else {
  128. tmp = -1; /* FP not yet used */
  129. }
  130. break;
  131. case PC:
  132. tmp = regs->cp0_epc;
  133. break;
  134. case CAUSE:
  135. tmp = regs->cp0_cause;
  136. break;
  137. case BADVADDR:
  138. tmp = regs->cp0_badvaddr;
  139. break;
  140. case MMHI:
  141. tmp = regs->hi;
  142. break;
  143. case MMLO:
  144. tmp = regs->lo;
  145. break;
  146. case FPC_CSR:
  147. if (cpu_has_fpu)
  148. tmp = child->thread.fpu.hard.fcr31;
  149. else
  150. tmp = child->thread.fpu.soft.fcr31;
  151. break;
  152. case FPC_EIR: { /* implementation / version register */
  153. unsigned int flags;
  154. if (!cpu_has_fpu)
  155. break;
  156. flags = read_c0_status();
  157. __enable_fpu();
  158. __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
  159. write_c0_status(flags);
  160. break;
  161. }
  162. default:
  163. tmp = 0;
  164. ret = -EIO;
  165. goto out_tsk;
  166. }
  167. ret = put_user(tmp, (unsigned long *) data);
  168. break;
  169. }
  170. /* when I and D space are separate, this will have to be fixed. */
  171. case PTRACE_POKETEXT: /* write the word at location addr. */
  172. case PTRACE_POKEDATA:
  173. ret = 0;
  174. if (access_process_vm(child, addr, &data, sizeof(data), 1)
  175. == sizeof(data))
  176. break;
  177. ret = -EIO;
  178. break;
  179. case PTRACE_POKEUSR: {
  180. struct pt_regs *regs;
  181. ret = 0;
  182. regs = (struct pt_regs *) ((unsigned long) child->thread_info +
  183. THREAD_SIZE - 32 - sizeof(struct pt_regs));
  184. switch (addr) {
  185. case 0 ... 31:
  186. regs->regs[addr] = data;
  187. break;
  188. case FPR_BASE ... FPR_BASE + 31: {
  189. fpureg_t *fregs = get_fpu_regs(child);
  190. if (!tsk_used_math(child)) {
  191. /* FP not yet used */
  192. memset(&child->thread.fpu.hard, ~0,
  193. sizeof(child->thread.fpu.hard));
  194. child->thread.fpu.hard.fcr31 = 0;
  195. }
  196. #ifdef CONFIG_MIPS32
  197. /*
  198. * The odd registers are actually the high order bits
  199. * of the values stored in the even registers - unless
  200. * we're using r2k_switch.S.
  201. */
  202. if (addr & 1) {
  203. fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
  204. fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
  205. } else {
  206. fregs[addr - FPR_BASE] &= ~0xffffffffLL;
  207. fregs[addr - FPR_BASE] |= data;
  208. }
  209. #endif
  210. #ifdef CONFIG_MIPS64
  211. fregs[addr - FPR_BASE] = data;
  212. #endif
  213. break;
  214. }
  215. case PC:
  216. regs->cp0_epc = data;
  217. break;
  218. case MMHI:
  219. regs->hi = data;
  220. break;
  221. case MMLO:
  222. regs->lo = data;
  223. break;
  224. case FPC_CSR:
  225. if (cpu_has_fpu)
  226. child->thread.fpu.hard.fcr31 = data;
  227. else
  228. child->thread.fpu.soft.fcr31 = data;
  229. break;
  230. default:
  231. /* The rest are not allowed. */
  232. ret = -EIO;
  233. break;
  234. }
  235. break;
  236. }
  237. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  238. case PTRACE_CONT: { /* restart after signal. */
  239. ret = -EIO;
  240. if ((unsigned long) data > _NSIG)
  241. break;
  242. if (request == PTRACE_SYSCALL) {
  243. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  244. }
  245. else {
  246. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  247. }
  248. child->exit_code = data;
  249. wake_up_process(child);
  250. ret = 0;
  251. break;
  252. }
  253. /*
  254. * make the child exit. Best I can do is send it a sigkill.
  255. * perhaps it should be put in the status that it wants to
  256. * exit.
  257. */
  258. case PTRACE_KILL:
  259. ret = 0;
  260. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  261. break;
  262. child->exit_code = SIGKILL;
  263. wake_up_process(child);
  264. break;
  265. case PTRACE_DETACH: /* detach a process that was attached. */
  266. ret = ptrace_detach(child, data);
  267. break;
  268. default:
  269. ret = ptrace_request(child, request, addr, data);
  270. break;
  271. }
  272. out_tsk:
  273. put_task_struct(child);
  274. out:
  275. unlock_kernel();
  276. return ret;
  277. }
  278. /*
  279. * Notification of system call entry/exit
  280. * - triggered by current->work.syscall_trace
  281. */
  282. asmlinkage void do_syscall_trace(struct pt_regs *regs, int entryexit)
  283. {
  284. if (unlikely(current->audit_context)) {
  285. if (!entryexit)
  286. audit_syscall_entry(current, regs->regs[2],
  287. regs->regs[4], regs->regs[5],
  288. regs->regs[6], regs->regs[7]);
  289. else
  290. audit_syscall_exit(current, regs->regs[2]);
  291. }
  292. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  293. return;
  294. if (!(current->ptrace & PT_PTRACED))
  295. return;
  296. /* The 0x80 provides a way for the tracing parent to distinguish
  297. between a syscall stop and SIGTRAP delivery */
  298. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ?
  299. 0x80 : 0));
  300. /*
  301. * this isn't the same as continuing with a signal, but it will do
  302. * for normal use. strace only continues with a signal if the
  303. * stopping signal is not SIGTRAP. -brl
  304. */
  305. if (current->exit_code) {
  306. send_sig(current->exit_code, current, 1);
  307. current->exit_code = 0;
  308. }
  309. }