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