ptrace32.c 10 KB

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