ptrace32.c 9.5 KB

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