ptrace32.c 9.1 KB

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