ptrace32.c 9.5 KB

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