ptrace32.c 10 KB

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