ptrace32.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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/compiler.h>
  18. #include <linux/kernel.h>
  19. #include <linux/sched.h>
  20. #include <linux/mm.h>
  21. #include <linux/errno.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/smp.h>
  24. #include <linux/smp_lock.h>
  25. #include <linux/user.h>
  26. #include <linux/security.h>
  27. #include <asm/cpu.h>
  28. #include <asm/fpu.h>
  29. #include <asm/mipsregs.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/page.h>
  32. #include <asm/system.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/bootinfo.h>
  35. /*
  36. * Tracing a 32-bit process with a 64-bit strace and vice versa will not
  37. * work. I don't know how to fix this.
  38. */
  39. asmlinkage int sys32_ptrace(int request, int pid, int addr, int data)
  40. {
  41. struct task_struct *child;
  42. int ret;
  43. #if 0
  44. printk("ptrace(r=%d,pid=%d,addr=%08lx,data=%08lx)\n",
  45. (int) request, (int) pid, (unsigned long) addr,
  46. (unsigned long) data);
  47. #endif
  48. lock_kernel();
  49. ret = -EPERM;
  50. if (request == PTRACE_TRACEME) {
  51. /* are we already being traced? */
  52. if (current->ptrace & PT_PTRACED)
  53. goto out;
  54. if ((ret = security_ptrace(current->parent, current)))
  55. goto out;
  56. /* set the ptrace bit in the process flags. */
  57. current->ptrace |= PT_PTRACED;
  58. ret = 0;
  59. goto out;
  60. }
  61. ret = -ESRCH;
  62. read_lock(&tasklist_lock);
  63. child = find_task_by_pid(pid);
  64. if (child)
  65. get_task_struct(child);
  66. read_unlock(&tasklist_lock);
  67. if (!child)
  68. goto out;
  69. ret = -EPERM;
  70. if (pid == 1) /* you may not mess with init */
  71. goto out_tsk;
  72. if (request == PTRACE_ATTACH) {
  73. ret = ptrace_attach(child);
  74. goto out_tsk;
  75. }
  76. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  77. if (ret < 0)
  78. goto out_tsk;
  79. switch (request) {
  80. /* when I and D space are separate, these will need to be fixed. */
  81. case PTRACE_PEEKTEXT: /* read word at location addr. */
  82. case PTRACE_PEEKDATA: {
  83. unsigned int tmp;
  84. int copied;
  85. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  86. ret = -EIO;
  87. if (copied != sizeof(tmp))
  88. break;
  89. ret = put_user(tmp, (unsigned int *) (unsigned long) data);
  90. break;
  91. }
  92. /* Read the word at location addr in the USER area. */
  93. case PTRACE_PEEKUSR: {
  94. struct pt_regs *regs;
  95. unsigned int tmp;
  96. regs = (struct pt_regs *) ((unsigned long) child->thread_info +
  97. THREAD_SIZE - 32 - sizeof(struct pt_regs));
  98. ret = 0; /* Default return value. */
  99. switch (addr) {
  100. case 0 ... 31:
  101. tmp = regs->regs[addr];
  102. break;
  103. case FPR_BASE ... FPR_BASE + 31:
  104. if (tsk_used_math(child)) {
  105. fpureg_t *fregs = get_fpu_regs(child);
  106. /*
  107. * The odd registers are actually the high
  108. * order bits of the values stored in the even
  109. * registers - unless we're using r2k_switch.S.
  110. */
  111. if (addr & 1)
  112. tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
  113. else
  114. tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
  115. } else {
  116. tmp = -1; /* FP not yet used */
  117. }
  118. break;
  119. case PC:
  120. tmp = regs->cp0_epc;
  121. break;
  122. case CAUSE:
  123. tmp = regs->cp0_cause;
  124. break;
  125. case BADVADDR:
  126. tmp = regs->cp0_badvaddr;
  127. break;
  128. case MMHI:
  129. tmp = regs->hi;
  130. break;
  131. case MMLO:
  132. tmp = regs->lo;
  133. break;
  134. case FPC_CSR:
  135. if (cpu_has_fpu)
  136. tmp = child->thread.fpu.hard.fcr31;
  137. else
  138. tmp = child->thread.fpu.soft.fcr31;
  139. break;
  140. case FPC_EIR: { /* implementation / version register */
  141. unsigned int flags;
  142. if (!cpu_has_fpu)
  143. break;
  144. flags = read_c0_status();
  145. __enable_fpu();
  146. __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
  147. write_c0_status(flags);
  148. break;
  149. }
  150. default:
  151. tmp = 0;
  152. ret = -EIO;
  153. goto out_tsk;
  154. }
  155. ret = put_user(tmp, (unsigned *) (unsigned long) data);
  156. break;
  157. }
  158. /* when I and D space are separate, this will have to be fixed. */
  159. case PTRACE_POKETEXT: /* write the word at location addr. */
  160. case PTRACE_POKEDATA:
  161. ret = 0;
  162. if (access_process_vm(child, addr, &data, sizeof(data), 1)
  163. == sizeof(data))
  164. break;
  165. ret = -EIO;
  166. break;
  167. case PTRACE_POKEUSR: {
  168. struct pt_regs *regs;
  169. ret = 0;
  170. regs = (struct pt_regs *) ((unsigned long) child->thread_info +
  171. THREAD_SIZE - 32 - sizeof(struct pt_regs));
  172. switch (addr) {
  173. case 0 ... 31:
  174. regs->regs[addr] = data;
  175. break;
  176. case FPR_BASE ... FPR_BASE + 31: {
  177. fpureg_t *fregs = get_fpu_regs(child);
  178. if (!tsk_used_math(child)) {
  179. /* FP not yet used */
  180. memset(&child->thread.fpu.hard, ~0,
  181. sizeof(child->thread.fpu.hard));
  182. child->thread.fpu.hard.fcr31 = 0;
  183. }
  184. /*
  185. * The odd registers are actually the high order bits
  186. * of the values stored in the even registers - unless
  187. * we're using r2k_switch.S.
  188. */
  189. if (addr & 1) {
  190. fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
  191. fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
  192. } else {
  193. fregs[addr - FPR_BASE] &= ~0xffffffffLL;
  194. /* Must cast, lest sign extension fill upper
  195. bits! */
  196. fregs[addr - FPR_BASE] |= (unsigned int)data;
  197. }
  198. break;
  199. }
  200. case PC:
  201. regs->cp0_epc = data;
  202. break;
  203. case MMHI:
  204. regs->hi = data;
  205. break;
  206. case MMLO:
  207. regs->lo = data;
  208. break;
  209. case FPC_CSR:
  210. if (cpu_has_fpu)
  211. child->thread.fpu.hard.fcr31 = data;
  212. else
  213. child->thread.fpu.soft.fcr31 = data;
  214. break;
  215. default:
  216. /* The rest are not allowed. */
  217. ret = -EIO;
  218. break;
  219. }
  220. break;
  221. }
  222. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  223. case PTRACE_CONT: { /* restart after signal. */
  224. ret = -EIO;
  225. if ((unsigned int) data > _NSIG)
  226. break;
  227. if (request == PTRACE_SYSCALL) {
  228. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  229. }
  230. else {
  231. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  232. }
  233. child->exit_code = data;
  234. wake_up_process(child);
  235. ret = 0;
  236. break;
  237. }
  238. /*
  239. * make the child exit. Best I can do is send it a sigkill.
  240. * perhaps it should be put in the status that it wants to
  241. * exit.
  242. */
  243. case PTRACE_KILL:
  244. ret = 0;
  245. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  246. break;
  247. child->exit_code = SIGKILL;
  248. wake_up_process(child);
  249. break;
  250. case PTRACE_DETACH: /* detach a process that was attached. */
  251. ret = ptrace_detach(child, data);
  252. break;
  253. default:
  254. ret = ptrace_request(child, request, addr, data);
  255. break;
  256. }
  257. out_tsk:
  258. put_task_struct(child);
  259. out:
  260. unlock_kernel();
  261. return ret;
  262. }