ptrace32.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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, [email protected] and Carsten Langgaard, [email protected]
  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/sched/task_stack.h>
  22. #include <linux/mm.h>
  23. #include <linux/errno.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/smp.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/page.h>
  33. #include <asm/reg.h>
  34. #include <asm/syscall.h>
  35. #include <linux/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 = ptrace_access_vm(child, (u64)addrOthers, &tmp,
  67. sizeof(tmp), FOLL_FORCE);
  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. #ifdef CONFIG_MIPS_FP_SUPPORT
  84. case FPR_BASE ... FPR_BASE + 31: {
  85. union fpureg *fregs;
  86. if (!tsk_used_math(child)) {
  87. /* FP not yet used */
  88. tmp = -1;
  89. break;
  90. }
  91. fregs = get_fpu_regs(child);
  92. if (test_tsk_thread_flag(child, TIF_32BIT_FPREGS)) {
  93. /*
  94. * The odd registers are actually the high
  95. * order bits of the values stored in the even
  96. * registers.
  97. */
  98. tmp = get_fpr32(&fregs[(addr & ~1) - FPR_BASE],
  99. addr & 1);
  100. break;
  101. }
  102. tmp = get_fpr64(&fregs[addr - FPR_BASE], 0);
  103. break;
  104. }
  105. case FPC_CSR:
  106. tmp = child->thread.fpu.fcr31;
  107. break;
  108. case FPC_EIR:
  109. /* implementation / version register */
  110. tmp = boot_cpu_data.fpu_id;
  111. break;
  112. #endif /* CONFIG_MIPS_FP_SUPPORT */
  113. case PC:
  114. tmp = regs->cp0_epc;
  115. break;
  116. case CAUSE:
  117. tmp = regs->cp0_cause;
  118. break;
  119. case BADVADDR:
  120. tmp = regs->cp0_badvaddr;
  121. break;
  122. case MMHI:
  123. tmp = regs->hi;
  124. break;
  125. case MMLO:
  126. tmp = regs->lo;
  127. break;
  128. case DSP_BASE ... DSP_BASE + 5: {
  129. dspreg_t *dregs;
  130. if (!cpu_has_dsp) {
  131. tmp = 0;
  132. ret = -EIO;
  133. goto out;
  134. }
  135. dregs = __get_dsp_regs(child);
  136. tmp = dregs[addr - DSP_BASE];
  137. break;
  138. }
  139. case DSP_CONTROL:
  140. if (!cpu_has_dsp) {
  141. tmp = 0;
  142. ret = -EIO;
  143. goto out;
  144. }
  145. tmp = child->thread.dsp.dspcontrol;
  146. break;
  147. default:
  148. tmp = 0;
  149. ret = -EIO;
  150. goto out;
  151. }
  152. ret = put_user(tmp, (unsigned __user *) (unsigned long) data);
  153. break;
  154. }
  155. /*
  156. * Write 4 bytes into the other process' storage
  157. * data is the 4 bytes that the user wants written
  158. * addr is a pointer in the user's storage that contains an
  159. * 8 byte address in the other process where the 4 bytes
  160. * that is to be written
  161. * (this is run in a 32-bit process looking at a 64-bit process)
  162. * when I and D space are separate, these will need to be fixed.
  163. */
  164. case PTRACE_POKETEXT_3264:
  165. case PTRACE_POKEDATA_3264: {
  166. u32 __user * addrOthers;
  167. /* Get the addr in the other process that we want to write into */
  168. ret = -EIO;
  169. if (get_user(addrOthers, (u32 __user * __user *) (unsigned long) addr) != 0)
  170. break;
  171. ret = 0;
  172. if (ptrace_access_vm(child, (u64)addrOthers, &data,
  173. sizeof(data),
  174. FOLL_FORCE | FOLL_WRITE) == sizeof(data))
  175. break;
  176. ret = -EIO;
  177. break;
  178. }
  179. case PTRACE_POKEUSR: {
  180. struct pt_regs *regs;
  181. ret = 0;
  182. regs = task_pt_regs(child);
  183. switch (addr) {
  184. case 0 ... 31:
  185. regs->regs[addr] = data;
  186. /* System call number may have been changed */
  187. if (addr == 2)
  188. mips_syscall_update_nr(child, regs);
  189. else if (addr == 4 &&
  190. mips_syscall_is_indirect(child, regs))
  191. mips_syscall_update_nr(child, regs);
  192. break;
  193. #ifdef CONFIG_MIPS_FP_SUPPORT
  194. case FPR_BASE ... FPR_BASE + 31: {
  195. union fpureg *fregs = get_fpu_regs(child);
  196. if (!tsk_used_math(child)) {
  197. /* FP not yet used */
  198. memset(&child->thread.fpu, ~0,
  199. sizeof(child->thread.fpu));
  200. child->thread.fpu.fcr31 = 0;
  201. }
  202. if (test_tsk_thread_flag(child, TIF_32BIT_FPREGS)) {
  203. /*
  204. * The odd registers are actually the high
  205. * order bits of the values stored in the even
  206. * registers.
  207. */
  208. set_fpr32(&fregs[(addr & ~1) - FPR_BASE],
  209. addr & 1, data);
  210. break;
  211. }
  212. set_fpr64(&fregs[addr - FPR_BASE], 0, data);
  213. break;
  214. }
  215. case FPC_CSR:
  216. child->thread.fpu.fcr31 = data;
  217. break;
  218. #endif /* CONFIG_MIPS_FP_SUPPORT */
  219. case PC:
  220. regs->cp0_epc = data;
  221. break;
  222. case MMHI:
  223. regs->hi = data;
  224. break;
  225. case MMLO:
  226. regs->lo = data;
  227. break;
  228. case DSP_BASE ... DSP_BASE + 5: {
  229. dspreg_t *dregs;
  230. if (!cpu_has_dsp) {
  231. ret = -EIO;
  232. break;
  233. }
  234. dregs = __get_dsp_regs(child);
  235. dregs[addr - DSP_BASE] = data;
  236. break;
  237. }
  238. case DSP_CONTROL:
  239. if (!cpu_has_dsp) {
  240. ret = -EIO;
  241. break;
  242. }
  243. child->thread.dsp.dspcontrol = data;
  244. break;
  245. default:
  246. /* The rest are not allowed. */
  247. ret = -EIO;
  248. break;
  249. }
  250. break;
  251. }
  252. case PTRACE_GETREGS:
  253. ret = ptrace_getregs(child,
  254. (struct user_pt_regs __user *) (__u64) data);
  255. break;
  256. case PTRACE_SETREGS:
  257. ret = ptrace_setregs(child,
  258. (struct user_pt_regs __user *) (__u64) data);
  259. break;
  260. #ifdef CONFIG_MIPS_FP_SUPPORT
  261. case PTRACE_GETFPREGS:
  262. ret = ptrace_getfpregs(child, (__u32 __user *) (__u64) data);
  263. break;
  264. case PTRACE_SETFPREGS:
  265. ret = ptrace_setfpregs(child, (__u32 __user *) (__u64) data);
  266. break;
  267. #endif
  268. case PTRACE_GET_THREAD_AREA:
  269. ret = put_user(task_thread_info(child)->tp_value,
  270. (unsigned int __user *) (unsigned long) data);
  271. break;
  272. case PTRACE_GET_THREAD_AREA_3264:
  273. ret = put_user(task_thread_info(child)->tp_value,
  274. (unsigned long __user *) (unsigned long) data);
  275. break;
  276. case PTRACE_GET_WATCH_REGS:
  277. ret = ptrace_get_watch_regs(child,
  278. (struct pt_watch_regs __user *) (unsigned long) addr);
  279. break;
  280. case PTRACE_SET_WATCH_REGS:
  281. ret = ptrace_set_watch_regs(child,
  282. (struct pt_watch_regs __user *) (unsigned long) addr);
  283. break;
  284. default:
  285. ret = compat_ptrace_request(child, request, addr, data);
  286. break;
  287. }
  288. out:
  289. return ret;
  290. }