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