ptrace32.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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/compat.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/smp.h>
  25. #include <linux/smp_lock.h>
  26. #include <linux/user.h>
  27. #include <linux/security.h>
  28. #include <asm/cpu.h>
  29. #include <asm/dsp.h>
  30. #include <asm/fpu.h>
  31. #include <asm/mipsregs.h>
  32. #include <asm/mipsmtregs.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. * Tracing a 32-bit process with a 64-bit strace and vice versa will not
  40. * work. I don't know how to fix this.
  41. */
  42. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  43. compat_ulong_t caddr, compat_ulong_t cdata)
  44. {
  45. int addr = caddr;
  46. int data = cdata;
  47. int ret;
  48. switch (request) {
  49. /*
  50. * Read 4 bytes of the other process' storage
  51. * data is a pointer specifying where the user wants the
  52. * 4 bytes copied into
  53. * addr is a pointer in the user's storage that contains an 8 byte
  54. * address in the other process of the 4 bytes that is to be read
  55. * (this is run in a 32-bit process looking at a 64-bit process)
  56. * when I and D space are separate, these will need to be fixed.
  57. */
  58. case PTRACE_PEEKTEXT_3264:
  59. case PTRACE_PEEKDATA_3264: {
  60. u32 tmp;
  61. int copied;
  62. u32 __user * addrOthers;
  63. ret = -EIO;
  64. /* Get the addr in the other process that we want to read */
  65. if (get_user(addrOthers, (u32 __user * __user *) (unsigned long) addr) != 0)
  66. break;
  67. copied = access_process_vm(child, (u64)addrOthers, &tmp,
  68. sizeof(tmp), 0);
  69. if (copied != sizeof(tmp))
  70. break;
  71. ret = put_user(tmp, (u32 __user *) (unsigned long) data);
  72. break;
  73. }
  74. /* Read the word at location addr in the USER area. */
  75. case PTRACE_PEEKUSR: {
  76. struct pt_regs *regs;
  77. unsigned int tmp;
  78. regs = task_pt_regs(child);
  79. ret = 0; /* Default return value. */
  80. switch (addr) {
  81. case 0 ... 31:
  82. tmp = regs->regs[addr];
  83. break;
  84. case FPR_BASE ... FPR_BASE + 31:
  85. if (tsk_used_math(child)) {
  86. fpureg_t *fregs = get_fpu_regs(child);
  87. /*
  88. * The odd registers are actually the high
  89. * order bits of the values stored in the even
  90. * registers - unless we're using r2k_switch.S.
  91. */
  92. if (addr & 1)
  93. tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
  94. else
  95. tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
  96. } else {
  97. tmp = -1; /* FP not yet used */
  98. }
  99. break;
  100. case PC:
  101. tmp = regs->cp0_epc;
  102. break;
  103. case CAUSE:
  104. tmp = regs->cp0_cause;
  105. break;
  106. case BADVADDR:
  107. tmp = regs->cp0_badvaddr;
  108. break;
  109. case MMHI:
  110. tmp = regs->hi;
  111. break;
  112. case MMLO:
  113. tmp = regs->lo;
  114. break;
  115. case FPC_CSR:
  116. tmp = child->thread.fpu.fcr31;
  117. break;
  118. case FPC_EIR: { /* implementation / version register */
  119. unsigned int flags;
  120. #ifdef CONFIG_MIPS_MT_SMTC
  121. unsigned int irqflags;
  122. unsigned int mtflags;
  123. #endif /* CONFIG_MIPS_MT_SMTC */
  124. preempt_disable();
  125. if (!cpu_has_fpu) {
  126. preempt_enable();
  127. tmp = 0;
  128. break;
  129. }
  130. #ifdef CONFIG_MIPS_MT_SMTC
  131. /* Read-modify-write of Status must be atomic */
  132. local_irq_save(irqflags);
  133. mtflags = dmt();
  134. #endif /* CONFIG_MIPS_MT_SMTC */
  135. if (cpu_has_mipsmt) {
  136. unsigned int vpflags = dvpe();
  137. flags = read_c0_status();
  138. __enable_fpu();
  139. __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
  140. write_c0_status(flags);
  141. evpe(vpflags);
  142. } else {
  143. flags = read_c0_status();
  144. __enable_fpu();
  145. __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
  146. write_c0_status(flags);
  147. }
  148. #ifdef CONFIG_MIPS_MT_SMTC
  149. emt(mtflags);
  150. local_irq_restore(irqflags);
  151. #endif /* CONFIG_MIPS_MT_SMTC */
  152. preempt_enable();
  153. break;
  154. }
  155. case DSP_BASE ... DSP_BASE + 5: {
  156. dspreg_t *dregs;
  157. if (!cpu_has_dsp) {
  158. tmp = 0;
  159. ret = -EIO;
  160. goto out;
  161. }
  162. dregs = __get_dsp_regs(child);
  163. tmp = (unsigned long) (dregs[addr - DSP_BASE]);
  164. break;
  165. }
  166. case DSP_CONTROL:
  167. if (!cpu_has_dsp) {
  168. tmp = 0;
  169. ret = -EIO;
  170. goto out;
  171. }
  172. tmp = child->thread.dsp.dspcontrol;
  173. break;
  174. default:
  175. tmp = 0;
  176. ret = -EIO;
  177. goto out;
  178. }
  179. ret = put_user(tmp, (unsigned __user *) (unsigned long) data);
  180. break;
  181. }
  182. /*
  183. * Write 4 bytes into the other process' storage
  184. * data is the 4 bytes that the user wants written
  185. * addr is a pointer in the user's storage that contains an
  186. * 8 byte address in the other process where the 4 bytes
  187. * that is to be written
  188. * (this is run in a 32-bit process looking at a 64-bit process)
  189. * when I and D space are separate, these will need to be fixed.
  190. */
  191. case PTRACE_POKETEXT_3264:
  192. case PTRACE_POKEDATA_3264: {
  193. u32 __user * addrOthers;
  194. /* Get the addr in the other process that we want to write into */
  195. ret = -EIO;
  196. if (get_user(addrOthers, (u32 __user * __user *) (unsigned long) addr) != 0)
  197. break;
  198. ret = 0;
  199. if (access_process_vm(child, (u64)addrOthers, &data,
  200. sizeof(data), 1) == sizeof(data))
  201. break;
  202. ret = -EIO;
  203. break;
  204. }
  205. case PTRACE_POKEUSR: {
  206. struct pt_regs *regs;
  207. ret = 0;
  208. regs = task_pt_regs(child);
  209. switch (addr) {
  210. case 0 ... 31:
  211. regs->regs[addr] = data;
  212. break;
  213. case FPR_BASE ... FPR_BASE + 31: {
  214. fpureg_t *fregs = get_fpu_regs(child);
  215. if (!tsk_used_math(child)) {
  216. /* FP not yet used */
  217. memset(&child->thread.fpu, ~0,
  218. sizeof(child->thread.fpu));
  219. child->thread.fpu.fcr31 = 0;
  220. }
  221. /*
  222. * The odd registers are actually the high order bits
  223. * of the values stored in the even registers - unless
  224. * we're using r2k_switch.S.
  225. */
  226. if (addr & 1) {
  227. fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
  228. fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
  229. } else {
  230. fregs[addr - FPR_BASE] &= ~0xffffffffLL;
  231. /* Must cast, lest sign extension fill upper
  232. bits! */
  233. fregs[addr - FPR_BASE] |= (unsigned int)data;
  234. }
  235. break;
  236. }
  237. case PC:
  238. regs->cp0_epc = data;
  239. break;
  240. case MMHI:
  241. regs->hi = data;
  242. break;
  243. case MMLO:
  244. regs->lo = data;
  245. break;
  246. case FPC_CSR:
  247. child->thread.fpu.fcr31 = data;
  248. break;
  249. case DSP_BASE ... DSP_BASE + 5: {
  250. dspreg_t *dregs;
  251. if (!cpu_has_dsp) {
  252. ret = -EIO;
  253. break;
  254. }
  255. dregs = __get_dsp_regs(child);
  256. dregs[addr - DSP_BASE] = data;
  257. break;
  258. }
  259. case DSP_CONTROL:
  260. if (!cpu_has_dsp) {
  261. ret = -EIO;
  262. break;
  263. }
  264. child->thread.dsp.dspcontrol = data;
  265. break;
  266. default:
  267. /* The rest are not allowed. */
  268. ret = -EIO;
  269. break;
  270. }
  271. break;
  272. }
  273. case PTRACE_GETREGS:
  274. ret = ptrace_getregs(child, (__s64 __user *) (__u64) data);
  275. break;
  276. case PTRACE_SETREGS:
  277. ret = ptrace_setregs(child, (__s64 __user *) (__u64) data);
  278. break;
  279. case PTRACE_GETFPREGS:
  280. ret = ptrace_getfpregs(child, (__u32 __user *) (__u64) data);
  281. break;
  282. case PTRACE_SETFPREGS:
  283. ret = ptrace_setfpregs(child, (__u32 __user *) (__u64) data);
  284. break;
  285. case PTRACE_GET_THREAD_AREA:
  286. ret = put_user(task_thread_info(child)->tp_value,
  287. (unsigned int __user *) (unsigned long) data);
  288. break;
  289. case PTRACE_GET_THREAD_AREA_3264:
  290. ret = put_user(task_thread_info(child)->tp_value,
  291. (unsigned long __user *) (unsigned long) data);
  292. break;
  293. case PTRACE_GET_WATCH_REGS:
  294. ret = ptrace_get_watch_regs(child,
  295. (struct pt_watch_regs __user *) (unsigned long) addr);
  296. break;
  297. case PTRACE_SET_WATCH_REGS:
  298. ret = ptrace_set_watch_regs(child,
  299. (struct pt_watch_regs __user *) (unsigned long) addr);
  300. break;
  301. default:
  302. ret = compat_ptrace_request(child, request, addr, data);
  303. break;
  304. }
  305. out:
  306. return ret;
  307. }