ptrace32.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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/errno.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/regset.h>
  26. #include <linux/user.h>
  27. #include <linux/security.h>
  28. #include <linux/signal.h>
  29. #include <linux/compat.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/page.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/system.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. switch (request) {
  46. case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
  47. return copy_regset_to_user(child,
  48. task_user_regset_view(current), 0,
  49. 0, 32 * sizeof(compat_long_t),
  50. compat_ptr(data));
  51. case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
  52. return copy_regset_from_user(child,
  53. task_user_regset_view(current), 0,
  54. 0, 32 * sizeof(compat_long_t),
  55. compat_ptr(data));
  56. }
  57. return -EPERM;
  58. }
  59. /* Macros to workout the correct index for the FPR in the thread struct */
  60. #define FPRNUMBER(i) (((i) - PT_FPR0) >> 1)
  61. #define FPRHALF(i) (((i) - PT_FPR0) & 1)
  62. #define FPRINDEX(i) TS_FPRWIDTH * FPRNUMBER(i) * 2 + FPRHALF(i)
  63. #define FPRINDEX_3264(i) (TS_FPRWIDTH * ((i) - PT_FPR0))
  64. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  65. compat_ulong_t caddr, compat_ulong_t cdata)
  66. {
  67. unsigned long addr = caddr;
  68. unsigned long data = cdata;
  69. int ret;
  70. switch (request) {
  71. /*
  72. * Read 4 bytes of the other process' storage
  73. * data is a pointer specifying where the user wants the
  74. * 4 bytes copied into
  75. * addr is a pointer in the user's storage that contains an 8 byte
  76. * address in the other process of the 4 bytes that is to be read
  77. * (this is run in a 32-bit process looking at a 64-bit process)
  78. * when I and D space are separate, these will need to be fixed.
  79. */
  80. case PPC_PTRACE_PEEKTEXT_3264:
  81. case PPC_PTRACE_PEEKDATA_3264: {
  82. u32 tmp;
  83. int copied;
  84. u32 __user * addrOthers;
  85. ret = -EIO;
  86. /* Get the addr in the other process that we want to read */
  87. if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
  88. break;
  89. copied = access_process_vm(child, (u64)addrOthers, &tmp,
  90. sizeof(tmp), 0);
  91. if (copied != sizeof(tmp))
  92. break;
  93. ret = put_user(tmp, (u32 __user *)data);
  94. break;
  95. }
  96. /* Read a register (specified by ADDR) out of the "user area" */
  97. case PTRACE_PEEKUSR: {
  98. int index;
  99. unsigned long tmp;
  100. ret = -EIO;
  101. /* convert to index and check */
  102. index = (unsigned long) addr >> 2;
  103. if ((addr & 3) || (index > PT_FPSCR32))
  104. break;
  105. CHECK_FULL_REGS(child->thread.regs);
  106. if (index < PT_FPR0) {
  107. tmp = ptrace_get_reg(child, index);
  108. } else {
  109. flush_fp_to_thread(child);
  110. /*
  111. * the user space code considers the floating point
  112. * to be an array of unsigned int (32 bits) - the
  113. * index passed in is based on this assumption.
  114. */
  115. tmp = ((unsigned int *)child->thread.fpr)
  116. [FPRINDEX(index)];
  117. }
  118. ret = put_user((unsigned int)tmp, (u32 __user *)data);
  119. break;
  120. }
  121. /*
  122. * Read 4 bytes out of the other process' pt_regs area
  123. * data is a pointer specifying where the user wants the
  124. * 4 bytes copied into
  125. * addr is the offset into the other process' pt_regs structure
  126. * that is to be read
  127. * (this is run in a 32-bit process looking at a 64-bit process)
  128. */
  129. case PPC_PTRACE_PEEKUSR_3264: {
  130. u32 index;
  131. u32 reg32bits;
  132. u64 tmp;
  133. u32 numReg;
  134. u32 part;
  135. ret = -EIO;
  136. /* Determine which register the user wants */
  137. index = (u64)addr >> 2;
  138. numReg = index / 2;
  139. /* Determine which part of the register the user wants */
  140. if (index % 2)
  141. part = 1; /* want the 2nd half of the register (right-most). */
  142. else
  143. part = 0; /* want the 1st half of the register (left-most). */
  144. /* Validate the input - check to see if address is on the wrong boundary
  145. * or beyond the end of the user area
  146. */
  147. if ((addr & 3) || numReg > PT_FPSCR)
  148. break;
  149. CHECK_FULL_REGS(child->thread.regs);
  150. if (numReg >= PT_FPR0) {
  151. flush_fp_to_thread(child);
  152. /* get 64 bit FPR */
  153. tmp = ((u64 *)child->thread.fpr)
  154. [FPRINDEX_3264(numReg)];
  155. } else { /* register within PT_REGS struct */
  156. tmp = ptrace_get_reg(child, numReg);
  157. }
  158. reg32bits = ((u32*)&tmp)[part];
  159. ret = put_user(reg32bits, (u32 __user *)data);
  160. break;
  161. }
  162. /*
  163. * Write 4 bytes into the other process' storage
  164. * data is the 4 bytes that the user wants written
  165. * addr is a pointer in the user's storage that contains an
  166. * 8 byte address in the other process where the 4 bytes
  167. * that is to be written
  168. * (this is run in a 32-bit process looking at a 64-bit process)
  169. * when I and D space are separate, these will need to be fixed.
  170. */
  171. case PPC_PTRACE_POKETEXT_3264:
  172. case PPC_PTRACE_POKEDATA_3264: {
  173. u32 tmp = data;
  174. u32 __user * addrOthers;
  175. /* Get the addr in the other process that we want to write into */
  176. ret = -EIO;
  177. if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
  178. break;
  179. ret = 0;
  180. if (access_process_vm(child, (u64)addrOthers, &tmp,
  181. sizeof(tmp), 1) == sizeof(tmp))
  182. break;
  183. ret = -EIO;
  184. break;
  185. }
  186. /* write the word at location addr in the USER area */
  187. case PTRACE_POKEUSR: {
  188. unsigned long index;
  189. ret = -EIO;
  190. /* convert to index and check */
  191. index = (unsigned long) addr >> 2;
  192. if ((addr & 3) || (index > PT_FPSCR32))
  193. break;
  194. CHECK_FULL_REGS(child->thread.regs);
  195. if (index < PT_FPR0) {
  196. ret = ptrace_put_reg(child, index, data);
  197. } else {
  198. flush_fp_to_thread(child);
  199. /*
  200. * the user space code considers the floating point
  201. * to be an array of unsigned int (32 bits) - the
  202. * index passed in is based on this assumption.
  203. */
  204. ((unsigned int *)child->thread.fpr)
  205. [FPRINDEX(index)] = data;
  206. ret = 0;
  207. }
  208. break;
  209. }
  210. /*
  211. * Write 4 bytes into the other process' pt_regs area
  212. * data is the 4 bytes that the user wants written
  213. * addr is the offset into the other process' pt_regs structure
  214. * that is to be written into
  215. * (this is run in a 32-bit process looking at a 64-bit process)
  216. */
  217. case PPC_PTRACE_POKEUSR_3264: {
  218. u32 index;
  219. u32 numReg;
  220. ret = -EIO;
  221. /* Determine which register the user wants */
  222. index = (u64)addr >> 2;
  223. numReg = index / 2;
  224. /*
  225. * Validate the input - check to see if address is on the
  226. * wrong boundary or beyond the end of the user area
  227. */
  228. if ((addr & 3) || (numReg > PT_FPSCR))
  229. break;
  230. CHECK_FULL_REGS(child->thread.regs);
  231. if (numReg < PT_FPR0) {
  232. unsigned long freg = ptrace_get_reg(child, numReg);
  233. if (index % 2)
  234. freg = (freg & ~0xfffffffful) | (data & 0xfffffffful);
  235. else
  236. freg = (freg & 0xfffffffful) | (data << 32);
  237. ret = ptrace_put_reg(child, numReg, freg);
  238. } else {
  239. u64 *tmp;
  240. flush_fp_to_thread(child);
  241. /* get 64 bit FPR ... */
  242. tmp = &(((u64 *)child->thread.fpr)
  243. [FPRINDEX_3264(numReg)]);
  244. /* ... write the 32 bit part we want */
  245. ((u32 *)tmp)[index % 2] = data;
  246. ret = 0;
  247. }
  248. break;
  249. }
  250. case PTRACE_GET_DEBUGREG: {
  251. ret = -EINVAL;
  252. /* We only support one DABR and no IABRS at the moment */
  253. if (addr > 0)
  254. break;
  255. ret = put_user(child->thread.dabr, (u32 __user *)data);
  256. break;
  257. }
  258. case PTRACE_GETREGS: /* Get all pt_regs from the child. */
  259. return copy_regset_to_user(
  260. child, task_user_regset_view(current), 0,
  261. 0, PT_REGS_COUNT * sizeof(compat_long_t),
  262. compat_ptr(data));
  263. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  264. return copy_regset_from_user(
  265. child, task_user_regset_view(current), 0,
  266. 0, PT_REGS_COUNT * sizeof(compat_long_t),
  267. compat_ptr(data));
  268. case PTRACE_GETFPREGS:
  269. case PTRACE_SETFPREGS:
  270. case PTRACE_GETVRREGS:
  271. case PTRACE_SETVRREGS:
  272. case PTRACE_GETVSRREGS:
  273. case PTRACE_SETVSRREGS:
  274. case PTRACE_GETREGS64:
  275. case PTRACE_SETREGS64:
  276. case PPC_PTRACE_GETFPREGS:
  277. case PPC_PTRACE_SETFPREGS:
  278. case PTRACE_KILL:
  279. case PTRACE_SINGLESTEP:
  280. case PTRACE_DETACH:
  281. case PTRACE_SET_DEBUGREG:
  282. case PTRACE_SYSCALL:
  283. case PTRACE_CONT:
  284. ret = arch_ptrace(child, request, addr, data);
  285. break;
  286. /* Old reverse args ptrace callss */
  287. case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
  288. case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
  289. ret = compat_ptrace_old(child, request, addr, data);
  290. break;
  291. default:
  292. ret = compat_ptrace_request(child, request, addr, data);
  293. break;
  294. }
  295. return ret;
  296. }