ptrace32.c 8.5 KB

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