ptrace32.c 7.3 KB

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