ptrace32.c 7.1 KB

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