ptrace.c 8.3 KB

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