ptrace32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * ptrace for 32-bit processes running on a 64-bit kernel.
  3. *
  4. * PowerPC version
  5. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  6. *
  7. * Derived from "arch/m68k/kernel/ptrace.c"
  8. * Copyright (C) 1994 by Hamish Macdonald
  9. * Taken from linux/kernel/ptrace.c and modified for M680x0.
  10. * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
  11. *
  12. * Modified by Cort Dougan (cort@hq.fsmlabs.com)
  13. * and Paul Mackerras (paulus@samba.org).
  14. *
  15. * This file is subject to the terms and conditions of the GNU General
  16. * Public License. See the file COPYING in the main directory of
  17. * this archive for more details.
  18. */
  19. #include <linux/config.h>
  20. #include <linux/kernel.h>
  21. #include <linux/sched.h>
  22. #include <linux/mm.h>
  23. #include <linux/smp.h>
  24. #include <linux/smp_lock.h>
  25. #include <linux/errno.h>
  26. #include <linux/ptrace.h>
  27. #include <linux/user.h>
  28. #include <linux/security.h>
  29. #include <linux/signal.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/page.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/system.h>
  34. #include "ptrace-common.h"
  35. /*
  36. * does not yet catch signals sent when the child dies.
  37. * in exit.c or in signal.c.
  38. */
  39. long compat_sys_ptrace(int request, int pid, unsigned long addr,
  40. unsigned long data)
  41. {
  42. struct task_struct *child;
  43. int ret;
  44. lock_kernel();
  45. if (request == PTRACE_TRACEME) {
  46. ret = ptrace_traceme();
  47. goto out;
  48. }
  49. child = ptrace_get_task_struct(pid);
  50. if (IS_ERR(child)) {
  51. ret = PTR_ERR(child);
  52. goto out;
  53. }
  54. if (request == PTRACE_ATTACH) {
  55. ret = ptrace_attach(child);
  56. goto out_tsk;
  57. }
  58. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  59. if (ret < 0)
  60. goto out_tsk;
  61. switch (request) {
  62. /* when I and D space are separate, these will need to be fixed. */
  63. case PTRACE_PEEKTEXT: /* read word at location addr. */
  64. case PTRACE_PEEKDATA: {
  65. unsigned int tmp;
  66. int copied;
  67. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  68. ret = -EIO;
  69. if (copied != sizeof(tmp))
  70. break;
  71. ret = put_user(tmp, (u32 __user *)data);
  72. break;
  73. }
  74. /*
  75. * Read 4 bytes of the other process' storage
  76. * data is a pointer specifying where the user wants the
  77. * 4 bytes copied into
  78. * addr is a pointer in the user's storage that contains an 8 byte
  79. * address in the other process of the 4 bytes that is to be read
  80. * (this is run in a 32-bit process looking at a 64-bit process)
  81. * when I and D space are separate, these will need to be fixed.
  82. */
  83. case PPC_PTRACE_PEEKTEXT_3264:
  84. case PPC_PTRACE_PEEKDATA_3264: {
  85. u32 tmp;
  86. int copied;
  87. u32 __user * addrOthers;
  88. ret = -EIO;
  89. /* Get the addr in the other process that we want to read */
  90. if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
  91. break;
  92. copied = access_process_vm(child, (u64)addrOthers, &tmp,
  93. sizeof(tmp), 0);
  94. if (copied != sizeof(tmp))
  95. break;
  96. ret = put_user(tmp, (u32 __user *)data);
  97. break;
  98. }
  99. /* Read a register (specified by ADDR) out of the "user area" */
  100. case PTRACE_PEEKUSR: {
  101. int index;
  102. unsigned long tmp;
  103. ret = -EIO;
  104. /* convert to index and check */
  105. index = (unsigned long) addr >> 2;
  106. if ((addr & 3) || (index > PT_FPSCR32))
  107. break;
  108. if (index < PT_FPR0) {
  109. tmp = get_reg(child, index);
  110. } else {
  111. flush_fp_to_thread(child);
  112. /*
  113. * the user space code considers the floating point
  114. * to be an array of unsigned int (32 bits) - the
  115. * index passed in is based on this assumption.
  116. */
  117. tmp = ((unsigned int *)child->thread.fpr)[index - PT_FPR0];
  118. }
  119. ret = put_user((unsigned int)tmp, (u32 __user *)data);
  120. break;
  121. }
  122. /*
  123. * Read 4 bytes out of the other process' pt_regs area
  124. * data is a pointer specifying where the user wants the
  125. * 4 bytes copied into
  126. * addr is the offset into the other process' pt_regs structure
  127. * that is to be read
  128. * (this is run in a 32-bit process looking at a 64-bit process)
  129. */
  130. case PPC_PTRACE_PEEKUSR_3264: {
  131. u32 index;
  132. u32 reg32bits;
  133. u64 tmp;
  134. u32 numReg;
  135. u32 part;
  136. ret = -EIO;
  137. /* Determine which register the user wants */
  138. index = (u64)addr >> 2;
  139. numReg = index / 2;
  140. /* Determine which part of the register the user wants */
  141. if (index % 2)
  142. part = 1; /* want the 2nd half of the register (right-most). */
  143. else
  144. part = 0; /* want the 1st half of the register (left-most). */
  145. /* Validate the input - check to see if address is on the wrong boundary or beyond the end of the user area */
  146. if ((addr & 3) || numReg > PT_FPSCR)
  147. break;
  148. if (numReg >= PT_FPR0) {
  149. flush_fp_to_thread(child);
  150. tmp = ((unsigned long int *)child->thread.fpr)[numReg - PT_FPR0];
  151. } else { /* register within PT_REGS struct */
  152. tmp = get_reg(child, numReg);
  153. }
  154. reg32bits = ((u32*)&tmp)[part];
  155. ret = put_user(reg32bits, (u32 __user *)data);
  156. break;
  157. }
  158. /* If I and D space are separate, this will have to be fixed. */
  159. case PTRACE_POKETEXT: /* write the word at location addr. */
  160. case PTRACE_POKEDATA: {
  161. unsigned int tmp;
  162. tmp = data;
  163. ret = 0;
  164. if (access_process_vm(child, addr, &tmp, sizeof(tmp), 1)
  165. == sizeof(tmp))
  166. break;
  167. ret = -EIO;
  168. break;
  169. }
  170. /*
  171. * Write 4 bytes into the other process' storage
  172. * data is the 4 bytes that the user wants written
  173. * addr is a pointer in the user's storage that contains an
  174. * 8 byte address in the other process where the 4 bytes
  175. * that is to be written
  176. * (this is run in a 32-bit process looking at a 64-bit process)
  177. * when I and D space are separate, these will need to be fixed.
  178. */
  179. case PPC_PTRACE_POKETEXT_3264:
  180. case PPC_PTRACE_POKEDATA_3264: {
  181. u32 tmp = data;
  182. u32 __user * addrOthers;
  183. /* Get the addr in the other process that we want to write into */
  184. ret = -EIO;
  185. if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
  186. break;
  187. ret = 0;
  188. if (access_process_vm(child, (u64)addrOthers, &tmp,
  189. sizeof(tmp), 1) == sizeof(tmp))
  190. break;
  191. ret = -EIO;
  192. break;
  193. }
  194. /* write the word at location addr in the USER area */
  195. case PTRACE_POKEUSR: {
  196. unsigned long index;
  197. ret = -EIO;
  198. /* convert to index and check */
  199. index = (unsigned long) addr >> 2;
  200. if ((addr & 3) || (index > PT_FPSCR32))
  201. break;
  202. if (index == PT_ORIG_R3)
  203. break;
  204. if (index < PT_FPR0) {
  205. ret = put_reg(child, index, data);
  206. } else {
  207. flush_fp_to_thread(child);
  208. /*
  209. * the user space code considers the floating point
  210. * to be an array of unsigned int (32 bits) - the
  211. * index passed in is based on this assumption.
  212. */
  213. ((unsigned int *)child->thread.fpr)[index - PT_FPR0] = data;
  214. ret = 0;
  215. }
  216. break;
  217. }
  218. /*
  219. * Write 4 bytes into the other process' pt_regs area
  220. * data is the 4 bytes that the user wants written
  221. * addr is the offset into the other process' pt_regs structure
  222. * that is to be written into
  223. * (this is run in a 32-bit process looking at a 64-bit process)
  224. */
  225. case PPC_PTRACE_POKEUSR_3264: {
  226. u32 index;
  227. u32 numReg;
  228. ret = -EIO;
  229. /* Determine which register the user wants */
  230. index = (u64)addr >> 2;
  231. numReg = index / 2;
  232. /*
  233. * Validate the input - check to see if address is on the
  234. * wrong boundary or beyond the end of the user area
  235. */
  236. if ((addr & 3) || (numReg > PT_FPSCR))
  237. break;
  238. /* Insure it is a register we let them change */
  239. if ((numReg == PT_ORIG_R3)
  240. || ((numReg > PT_CCR) && (numReg < PT_FPR0)))
  241. break;
  242. if (numReg >= PT_FPR0) {
  243. flush_fp_to_thread(child);
  244. }
  245. if (numReg == PT_MSR)
  246. data = (data & MSR_DEBUGCHANGE)
  247. | (child->thread.regs->msr & ~MSR_DEBUGCHANGE);
  248. ((u32*)child->thread.regs)[index] = data;
  249. ret = 0;
  250. break;
  251. }
  252. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  253. case PTRACE_CONT: { /* restart after signal. */
  254. ret = -EIO;
  255. if (!valid_signal(data))
  256. break;
  257. if (request == PTRACE_SYSCALL)
  258. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  259. else
  260. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  261. child->exit_code = data;
  262. /* make sure the single step bit is not set. */
  263. clear_single_step(child);
  264. wake_up_process(child);
  265. ret = 0;
  266. break;
  267. }
  268. /*
  269. * make the child exit. Best I can do is send it a sigkill.
  270. * perhaps it should be put in the status that it wants to
  271. * exit.
  272. */
  273. case PTRACE_KILL: {
  274. ret = 0;
  275. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  276. break;
  277. child->exit_code = SIGKILL;
  278. /* make sure the single step bit is not set. */
  279. clear_single_step(child);
  280. wake_up_process(child);
  281. break;
  282. }
  283. case PTRACE_SINGLESTEP: { /* set the trap flag. */
  284. ret = -EIO;
  285. if (!valid_signal(data))
  286. break;
  287. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  288. set_single_step(child);
  289. child->exit_code = data;
  290. /* give it a chance to run. */
  291. wake_up_process(child);
  292. ret = 0;
  293. break;
  294. }
  295. case PTRACE_GET_DEBUGREG: {
  296. ret = -EINVAL;
  297. /* We only support one DABR and no IABRS at the moment */
  298. if (addr > 0)
  299. break;
  300. ret = put_user(child->thread.dabr, (u32 __user *)data);
  301. break;
  302. }
  303. case PTRACE_SET_DEBUGREG:
  304. ret = ptrace_set_debugreg(child, addr, data);
  305. break;
  306. case PTRACE_DETACH:
  307. ret = ptrace_detach(child, data);
  308. break;
  309. case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
  310. int i;
  311. unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
  312. unsigned int __user *tmp = (unsigned int __user *)addr;
  313. for (i = 0; i < 32; i++) {
  314. ret = put_user(*reg, tmp);
  315. if (ret)
  316. break;
  317. reg++;
  318. tmp++;
  319. }
  320. break;
  321. }
  322. case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
  323. int i;
  324. unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
  325. unsigned int __user *tmp = (unsigned int __user *)addr;
  326. for (i = 0; i < 32; i++) {
  327. ret = get_user(*reg, tmp);
  328. if (ret)
  329. break;
  330. reg++;
  331. tmp++;
  332. }
  333. break;
  334. }
  335. case PPC_PTRACE_GETFPREGS: { /* Get FPRs 0 - 31. */
  336. int i;
  337. unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
  338. unsigned int __user *tmp = (unsigned int __user *)addr;
  339. flush_fp_to_thread(child);
  340. for (i = 0; i < 32; i++) {
  341. ret = put_user(*reg, tmp);
  342. if (ret)
  343. break;
  344. reg++;
  345. tmp++;
  346. }
  347. break;
  348. }
  349. case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */
  350. int i;
  351. unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
  352. unsigned int __user *tmp = (unsigned int __user *)addr;
  353. flush_fp_to_thread(child);
  354. for (i = 0; i < 32; i++) {
  355. ret = get_user(*reg, tmp);
  356. if (ret)
  357. break;
  358. reg++;
  359. tmp++;
  360. }
  361. break;
  362. }
  363. case PTRACE_GETEVENTMSG:
  364. ret = put_user(child->ptrace_message, (unsigned int __user *) data);
  365. break;
  366. #ifdef CONFIG_ALTIVEC
  367. case PTRACE_GETVRREGS:
  368. /* Get the child altivec register state. */
  369. flush_altivec_to_thread(child);
  370. ret = get_vrregs((unsigned long __user *)data, child);
  371. break;
  372. case PTRACE_SETVRREGS:
  373. /* Set the child altivec register state. */
  374. flush_altivec_to_thread(child);
  375. ret = set_vrregs(child, (unsigned long __user *)data);
  376. break;
  377. #endif
  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. }