ptrace32.c 10 KB

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