ptrace32.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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/kernel.h>
  20. #include <linux/sched.h>
  21. #include <linux/mm.h>
  22. #include <linux/smp.h>
  23. #include <linux/smp_lock.h>
  24. #include <linux/errno.h>
  25. #include <linux/ptrace.h>
  26. #include <linux/user.h>
  27. #include <linux/security.h>
  28. #include <linux/signal.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/page.h>
  31. #include <asm/pgtable.h>
  32. #include <asm/system.h>
  33. /*
  34. * does not yet catch signals sent when the child dies.
  35. * in exit.c or in signal.c.
  36. */
  37. /*
  38. * Here are the old "legacy" powerpc specific getregs/setregs ptrace calls,
  39. * we mark them as obsolete now, they will be removed in a future version
  40. */
  41. static long compat_ptrace_old(struct task_struct *child, long request,
  42. long addr, long data)
  43. {
  44. int ret = -EPERM;
  45. switch(request) {
  46. case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
  47. int i;
  48. unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
  49. unsigned int __user *tmp = (unsigned int __user *)addr;
  50. for (i = 0; i < 32; i++) {
  51. ret = put_user(*reg, tmp);
  52. if (ret)
  53. break;
  54. reg++;
  55. tmp++;
  56. }
  57. break;
  58. }
  59. case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
  60. int i;
  61. unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
  62. unsigned int __user *tmp = (unsigned int __user *)addr;
  63. for (i = 0; i < 32; i++) {
  64. ret = get_user(*reg, tmp);
  65. if (ret)
  66. break;
  67. reg++;
  68. tmp++;
  69. }
  70. break;
  71. }
  72. }
  73. return ret;
  74. }
  75. long compat_sys_ptrace(int request, int pid, unsigned long addr,
  76. unsigned long data)
  77. {
  78. struct task_struct *child;
  79. int ret;
  80. lock_kernel();
  81. if (request == PTRACE_TRACEME) {
  82. ret = ptrace_traceme();
  83. goto out;
  84. }
  85. child = ptrace_get_task_struct(pid);
  86. if (IS_ERR(child)) {
  87. ret = PTR_ERR(child);
  88. goto out;
  89. }
  90. if (request == PTRACE_ATTACH) {
  91. ret = ptrace_attach(child);
  92. goto out_tsk;
  93. }
  94. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  95. if (ret < 0)
  96. goto out_tsk;
  97. switch (request) {
  98. /* when I and D space are separate, these will need to be fixed. */
  99. case PTRACE_PEEKTEXT: /* read word at location addr. */
  100. case PTRACE_PEEKDATA: {
  101. unsigned int tmp;
  102. int copied;
  103. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  104. ret = -EIO;
  105. if (copied != sizeof(tmp))
  106. break;
  107. ret = put_user(tmp, (u32 __user *)data);
  108. break;
  109. }
  110. /*
  111. * Read 4 bytes of the other process' storage
  112. * data is a pointer specifying where the user wants the
  113. * 4 bytes copied into
  114. * addr is a pointer in the user's storage that contains an 8 byte
  115. * address in the other process of the 4 bytes that is to be read
  116. * (this is run in a 32-bit process looking at a 64-bit process)
  117. * when I and D space are separate, these will need to be fixed.
  118. */
  119. case PPC_PTRACE_PEEKTEXT_3264:
  120. case PPC_PTRACE_PEEKDATA_3264: {
  121. u32 tmp;
  122. int copied;
  123. u32 __user * addrOthers;
  124. ret = -EIO;
  125. /* Get the addr in the other process that we want to read */
  126. if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
  127. break;
  128. copied = access_process_vm(child, (u64)addrOthers, &tmp,
  129. sizeof(tmp), 0);
  130. if (copied != sizeof(tmp))
  131. break;
  132. ret = put_user(tmp, (u32 __user *)data);
  133. break;
  134. }
  135. /* Read a register (specified by ADDR) out of the "user area" */
  136. case PTRACE_PEEKUSR: {
  137. int index;
  138. unsigned long tmp;
  139. ret = -EIO;
  140. /* convert to index and check */
  141. index = (unsigned long) addr >> 2;
  142. if ((addr & 3) || (index > PT_FPSCR32))
  143. break;
  144. if (index < PT_FPR0) {
  145. tmp = ptrace_get_reg(child, index);
  146. } else {
  147. flush_fp_to_thread(child);
  148. /*
  149. * the user space code considers the floating point
  150. * to be an array of unsigned int (32 bits) - the
  151. * index passed in is based on this assumption.
  152. */
  153. tmp = ((unsigned int *)child->thread.fpr)[index - PT_FPR0];
  154. }
  155. ret = put_user((unsigned int)tmp, (u32 __user *)data);
  156. break;
  157. }
  158. /*
  159. * Read 4 bytes out of the other process' pt_regs area
  160. * data is a pointer specifying where the user wants the
  161. * 4 bytes copied into
  162. * addr is the offset into the other process' pt_regs structure
  163. * that is to be read
  164. * (this is run in a 32-bit process looking at a 64-bit process)
  165. */
  166. case PPC_PTRACE_PEEKUSR_3264: {
  167. u32 index;
  168. u32 reg32bits;
  169. u64 tmp;
  170. u32 numReg;
  171. u32 part;
  172. ret = -EIO;
  173. /* Determine which register the user wants */
  174. index = (u64)addr >> 2;
  175. numReg = index / 2;
  176. /* Determine which part of the register the user wants */
  177. if (index % 2)
  178. part = 1; /* want the 2nd half of the register (right-most). */
  179. else
  180. part = 0; /* want the 1st half of the register (left-most). */
  181. /* Validate the input - check to see if address is on the wrong boundary
  182. * or beyond the end of the user area
  183. */
  184. if ((addr & 3) || numReg > PT_FPSCR)
  185. break;
  186. if (numReg >= PT_FPR0) {
  187. flush_fp_to_thread(child);
  188. tmp = ((unsigned long int *)child->thread.fpr)[numReg - PT_FPR0];
  189. } else { /* register within PT_REGS struct */
  190. tmp = ptrace_get_reg(child, numReg);
  191. }
  192. reg32bits = ((u32*)&tmp)[part];
  193. ret = put_user(reg32bits, (u32 __user *)data);
  194. break;
  195. }
  196. /* If I and D space are separate, this will have to be fixed. */
  197. case PTRACE_POKETEXT: /* write the word at location addr. */
  198. case PTRACE_POKEDATA: {
  199. unsigned int tmp;
  200. tmp = data;
  201. ret = 0;
  202. if (access_process_vm(child, addr, &tmp, sizeof(tmp), 1)
  203. == sizeof(tmp))
  204. break;
  205. ret = -EIO;
  206. break;
  207. }
  208. /*
  209. * Write 4 bytes into the other process' storage
  210. * data is the 4 bytes that the user wants written
  211. * addr is a pointer in the user's storage that contains an
  212. * 8 byte address in the other process where the 4 bytes
  213. * that is to be written
  214. * (this is run in a 32-bit process looking at a 64-bit process)
  215. * when I and D space are separate, these will need to be fixed.
  216. */
  217. case PPC_PTRACE_POKETEXT_3264:
  218. case PPC_PTRACE_POKEDATA_3264: {
  219. u32 tmp = data;
  220. u32 __user * addrOthers;
  221. /* Get the addr in the other process that we want to write into */
  222. ret = -EIO;
  223. if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
  224. break;
  225. ret = 0;
  226. if (access_process_vm(child, (u64)addrOthers, &tmp,
  227. sizeof(tmp), 1) == sizeof(tmp))
  228. break;
  229. ret = -EIO;
  230. break;
  231. }
  232. /* write the word at location addr in the USER area */
  233. case PTRACE_POKEUSR: {
  234. unsigned long index;
  235. ret = -EIO;
  236. /* convert to index and check */
  237. index = (unsigned long) addr >> 2;
  238. if ((addr & 3) || (index > PT_FPSCR32))
  239. break;
  240. if (index < PT_FPR0) {
  241. ret = ptrace_put_reg(child, index, data);
  242. } else {
  243. flush_fp_to_thread(child);
  244. /*
  245. * the user space code considers the floating point
  246. * to be an array of unsigned int (32 bits) - the
  247. * index passed in is based on this assumption.
  248. */
  249. ((unsigned int *)child->thread.fpr)[index - PT_FPR0] = data;
  250. ret = 0;
  251. }
  252. break;
  253. }
  254. /*
  255. * Write 4 bytes into the other process' pt_regs area
  256. * data is the 4 bytes that the user wants written
  257. * addr is the offset into the other process' pt_regs structure
  258. * that is to be written into
  259. * (this is run in a 32-bit process looking at a 64-bit process)
  260. */
  261. case PPC_PTRACE_POKEUSR_3264: {
  262. u32 index;
  263. u32 numReg;
  264. ret = -EIO;
  265. /* Determine which register the user wants */
  266. index = (u64)addr >> 2;
  267. numReg = index / 2;
  268. /*
  269. * Validate the input - check to see if address is on the
  270. * wrong boundary or beyond the end of the user area
  271. */
  272. if ((addr & 3) || (numReg > PT_FPSCR))
  273. break;
  274. if (numReg < PT_FPR0) {
  275. unsigned long freg = ptrace_get_reg(child, numReg);
  276. if (index % 2)
  277. freg = (freg & ~0xfffffffful) | (data & 0xfffffffful);
  278. else
  279. freg = (freg & 0xfffffffful) | (data << 32);
  280. ret = ptrace_put_reg(child, numReg, freg);
  281. } else {
  282. flush_fp_to_thread(child);
  283. ((unsigned int *)child->thread.regs)[index] = data;
  284. ret = 0;
  285. }
  286. break;
  287. }
  288. case PTRACE_GET_DEBUGREG: {
  289. ret = -EINVAL;
  290. /* We only support one DABR and no IABRS at the moment */
  291. if (addr > 0)
  292. break;
  293. ret = put_user(child->thread.dabr, (u32 __user *)data);
  294. break;
  295. }
  296. case PTRACE_GETEVENTMSG:
  297. ret = put_user(child->ptrace_message, (unsigned int __user *) data);
  298. break;
  299. case PTRACE_GETREGS: { /* Get all pt_regs from the child. */
  300. int ui;
  301. if (!access_ok(VERIFY_WRITE, (void __user *)data,
  302. PT_REGS_COUNT * sizeof(int))) {
  303. ret = -EIO;
  304. break;
  305. }
  306. ret = 0;
  307. for (ui = 0; ui < PT_REGS_COUNT; ui ++) {
  308. ret |= __put_user(ptrace_get_reg(child, ui),
  309. (unsigned int __user *) data);
  310. data += sizeof(int);
  311. }
  312. break;
  313. }
  314. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  315. unsigned long tmp;
  316. int ui;
  317. if (!access_ok(VERIFY_READ, (void __user *)data,
  318. PT_REGS_COUNT * sizeof(int))) {
  319. ret = -EIO;
  320. break;
  321. }
  322. ret = 0;
  323. for (ui = 0; ui < PT_REGS_COUNT; ui ++) {
  324. ret = __get_user(tmp, (unsigned int __user *) data);
  325. if (ret)
  326. break;
  327. ptrace_put_reg(child, ui, tmp);
  328. data += sizeof(int);
  329. }
  330. break;
  331. }
  332. case PTRACE_GETFPREGS:
  333. case PTRACE_SETFPREGS:
  334. case PTRACE_GETVRREGS:
  335. case PTRACE_SETVRREGS:
  336. case PTRACE_GETREGS64:
  337. case PTRACE_SETREGS64:
  338. case PPC_PTRACE_GETFPREGS:
  339. case PPC_PTRACE_SETFPREGS:
  340. case PTRACE_KILL:
  341. case PTRACE_SINGLESTEP:
  342. case PTRACE_DETACH:
  343. case PTRACE_SET_DEBUGREG:
  344. case PTRACE_SYSCALL:
  345. case PTRACE_CONT:
  346. ret = arch_ptrace(child, request, addr, data);
  347. break;
  348. /* Old reverse args ptrace callss */
  349. case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
  350. case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
  351. ret = compat_ptrace_old(child, request, addr, data);
  352. break;
  353. default:
  354. ret = ptrace_request(child, request, addr, data);
  355. break;
  356. }
  357. out_tsk:
  358. put_task_struct(child);
  359. out:
  360. unlock_kernel();
  361. return ret;
  362. }