ptrace32.c 6.7 KB

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