ptrace32.c 7.9 KB

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