ptrace32.c 10 KB

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