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