ptrace32.c 9.0 KB

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