ptrace32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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-common.h"
  34. /*
  35. * does not yet catch signals sent when the child dies.
  36. * in exit.c or in signal.c.
  37. */
  38. long compat_sys_ptrace(int request, int pid, unsigned long addr,
  39. unsigned long data)
  40. {
  41. struct task_struct *child;
  42. int ret;
  43. lock_kernel();
  44. if (request == PTRACE_TRACEME) {
  45. ret = ptrace_traceme();
  46. goto out;
  47. }
  48. child = ptrace_get_task_struct(pid);
  49. if (IS_ERR(child)) {
  50. ret = PTR_ERR(child);
  51. goto out;
  52. }
  53. if (request == PTRACE_ATTACH) {
  54. ret = ptrace_attach(child);
  55. goto out_tsk;
  56. }
  57. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  58. if (ret < 0)
  59. goto out_tsk;
  60. switch (request) {
  61. /* when I and D space are separate, these will need to be fixed. */
  62. case PTRACE_PEEKTEXT: /* read word at location addr. */
  63. case PTRACE_PEEKDATA: {
  64. unsigned int tmp;
  65. int copied;
  66. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  67. ret = -EIO;
  68. if (copied != sizeof(tmp))
  69. break;
  70. ret = put_user(tmp, (u32 __user *)data);
  71. break;
  72. }
  73. /*
  74. * Read 4 bytes of the other process' storage
  75. * data is a pointer specifying where the user wants the
  76. * 4 bytes copied into
  77. * addr is a pointer in the user's storage that contains an 8 byte
  78. * address in the other process of the 4 bytes that is to be read
  79. * (this is run in a 32-bit process looking at a 64-bit process)
  80. * when I and D space are separate, these will need to be fixed.
  81. */
  82. case PPC_PTRACE_PEEKTEXT_3264:
  83. case PPC_PTRACE_PEEKDATA_3264: {
  84. u32 tmp;
  85. int copied;
  86. u32 __user * addrOthers;
  87. ret = -EIO;
  88. /* Get the addr in the other process that we want to read */
  89. if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
  90. break;
  91. copied = access_process_vm(child, (u64)addrOthers, &tmp,
  92. sizeof(tmp), 0);
  93. if (copied != sizeof(tmp))
  94. break;
  95. ret = put_user(tmp, (u32 __user *)data);
  96. break;
  97. }
  98. /* Read a register (specified by ADDR) out of the "user area" */
  99. case PTRACE_PEEKUSR: {
  100. int index;
  101. unsigned long tmp;
  102. ret = -EIO;
  103. /* convert to index and check */
  104. index = (unsigned long) addr >> 2;
  105. if ((addr & 3) || (index > PT_FPSCR32))
  106. break;
  107. if (index < PT_FPR0) {
  108. tmp = 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)[index - PT_FPR0];
  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 or beyond the end of the user area */
  145. if ((addr & 3) || numReg > PT_FPSCR)
  146. break;
  147. if (numReg >= PT_FPR0) {
  148. flush_fp_to_thread(child);
  149. tmp = ((unsigned long int *)child->thread.fpr)[numReg - PT_FPR0];
  150. } else { /* register within PT_REGS struct */
  151. tmp = get_reg(child, numReg);
  152. }
  153. reg32bits = ((u32*)&tmp)[part];
  154. ret = put_user(reg32bits, (u32 __user *)data);
  155. break;
  156. }
  157. /* If I and D space are separate, this will have to be fixed. */
  158. case PTRACE_POKETEXT: /* write the word at location addr. */
  159. case PTRACE_POKEDATA: {
  160. unsigned int tmp;
  161. tmp = data;
  162. ret = 0;
  163. if (access_process_vm(child, addr, &tmp, sizeof(tmp), 1)
  164. == sizeof(tmp))
  165. break;
  166. ret = -EIO;
  167. break;
  168. }
  169. /*
  170. * Write 4 bytes into the other process' storage
  171. * data is the 4 bytes that the user wants written
  172. * addr is a pointer in the user's storage that contains an
  173. * 8 byte address in the other process where the 4 bytes
  174. * that is to be written
  175. * (this is run in a 32-bit process looking at a 64-bit process)
  176. * when I and D space are separate, these will need to be fixed.
  177. */
  178. case PPC_PTRACE_POKETEXT_3264:
  179. case PPC_PTRACE_POKEDATA_3264: {
  180. u32 tmp = data;
  181. u32 __user * addrOthers;
  182. /* Get the addr in the other process that we want to write into */
  183. ret = -EIO;
  184. if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
  185. break;
  186. ret = 0;
  187. if (access_process_vm(child, (u64)addrOthers, &tmp,
  188. sizeof(tmp), 1) == sizeof(tmp))
  189. break;
  190. ret = -EIO;
  191. break;
  192. }
  193. /* write the word at location addr in the USER area */
  194. case PTRACE_POKEUSR: {
  195. unsigned long index;
  196. ret = -EIO;
  197. /* convert to index and check */
  198. index = (unsigned long) addr >> 2;
  199. if ((addr & 3) || (index > PT_FPSCR32))
  200. break;
  201. if (index == PT_ORIG_R3)
  202. break;
  203. if (index < PT_FPR0) {
  204. ret = put_reg(child, index, data);
  205. } else {
  206. flush_fp_to_thread(child);
  207. /*
  208. * the user space code considers the floating point
  209. * to be an array of unsigned int (32 bits) - the
  210. * index passed in is based on this assumption.
  211. */
  212. ((unsigned int *)child->thread.fpr)[index - PT_FPR0] = data;
  213. ret = 0;
  214. }
  215. break;
  216. }
  217. /*
  218. * Write 4 bytes into the other process' pt_regs area
  219. * data is the 4 bytes that the user wants written
  220. * addr is the offset into the other process' pt_regs structure
  221. * that is to be written into
  222. * (this is run in a 32-bit process looking at a 64-bit process)
  223. */
  224. case PPC_PTRACE_POKEUSR_3264: {
  225. u32 index;
  226. u32 numReg;
  227. ret = -EIO;
  228. /* Determine which register the user wants */
  229. index = (u64)addr >> 2;
  230. numReg = index / 2;
  231. /*
  232. * Validate the input - check to see if address is on the
  233. * wrong boundary or beyond the end of the user area
  234. */
  235. if ((addr & 3) || (numReg > PT_FPSCR))
  236. break;
  237. /* Insure it is a register we let them change */
  238. if ((numReg == PT_ORIG_R3)
  239. || ((numReg > PT_CCR) && (numReg < PT_FPR0)))
  240. break;
  241. if (numReg >= PT_FPR0) {
  242. flush_fp_to_thread(child);
  243. }
  244. if (numReg == PT_MSR)
  245. data = (data & MSR_DEBUGCHANGE)
  246. | (child->thread.regs->msr & ~MSR_DEBUGCHANGE);
  247. ((u32*)child->thread.regs)[index] = data;
  248. ret = 0;
  249. break;
  250. }
  251. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  252. case PTRACE_CONT: { /* restart after signal. */
  253. ret = -EIO;
  254. if (!valid_signal(data))
  255. break;
  256. if (request == PTRACE_SYSCALL)
  257. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  258. else
  259. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  260. child->exit_code = data;
  261. /* make sure the single step bit is not set. */
  262. clear_single_step(child);
  263. wake_up_process(child);
  264. ret = 0;
  265. break;
  266. }
  267. /*
  268. * make the child exit. Best I can do is send it a sigkill.
  269. * perhaps it should be put in the status that it wants to
  270. * exit.
  271. */
  272. case PTRACE_KILL: {
  273. ret = 0;
  274. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  275. break;
  276. child->exit_code = SIGKILL;
  277. /* make sure the single step bit is not set. */
  278. clear_single_step(child);
  279. wake_up_process(child);
  280. break;
  281. }
  282. case PTRACE_SINGLESTEP: { /* set the trap flag. */
  283. ret = -EIO;
  284. if (!valid_signal(data))
  285. break;
  286. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  287. set_single_step(child);
  288. child->exit_code = data;
  289. /* give it a chance to run. */
  290. wake_up_process(child);
  291. ret = 0;
  292. break;
  293. }
  294. case PTRACE_GET_DEBUGREG: {
  295. ret = -EINVAL;
  296. /* We only support one DABR and no IABRS at the moment */
  297. if (addr > 0)
  298. break;
  299. ret = put_user(child->thread.dabr, (u32 __user *)data);
  300. break;
  301. }
  302. case PTRACE_SET_DEBUGREG:
  303. ret = ptrace_set_debugreg(child, addr, data);
  304. break;
  305. case PTRACE_DETACH:
  306. ret = ptrace_detach(child, data);
  307. break;
  308. case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
  309. int i;
  310. unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
  311. unsigned int __user *tmp = (unsigned int __user *)addr;
  312. for (i = 0; i < 32; i++) {
  313. ret = put_user(*reg, tmp);
  314. if (ret)
  315. break;
  316. reg++;
  317. tmp++;
  318. }
  319. break;
  320. }
  321. case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
  322. int i;
  323. unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
  324. unsigned int __user *tmp = (unsigned int __user *)addr;
  325. for (i = 0; i < 32; i++) {
  326. ret = get_user(*reg, tmp);
  327. if (ret)
  328. break;
  329. reg++;
  330. tmp++;
  331. }
  332. break;
  333. }
  334. case PPC_PTRACE_GETFPREGS: { /* Get FPRs 0 - 31. */
  335. int i;
  336. unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
  337. unsigned int __user *tmp = (unsigned int __user *)addr;
  338. flush_fp_to_thread(child);
  339. for (i = 0; i < 32; i++) {
  340. ret = put_user(*reg, tmp);
  341. if (ret)
  342. break;
  343. reg++;
  344. tmp++;
  345. }
  346. break;
  347. }
  348. case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */
  349. int i;
  350. unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
  351. unsigned int __user *tmp = (unsigned int __user *)addr;
  352. flush_fp_to_thread(child);
  353. for (i = 0; i < 32; i++) {
  354. ret = get_user(*reg, tmp);
  355. if (ret)
  356. break;
  357. reg++;
  358. tmp++;
  359. }
  360. break;
  361. }
  362. case PTRACE_GETEVENTMSG:
  363. ret = put_user(child->ptrace_message, (unsigned int __user *) data);
  364. break;
  365. #ifdef CONFIG_ALTIVEC
  366. case PTRACE_GETVRREGS:
  367. /* Get the child altivec register state. */
  368. flush_altivec_to_thread(child);
  369. ret = get_vrregs((unsigned long __user *)data, child);
  370. break;
  371. case PTRACE_SETVRREGS:
  372. /* Set the child altivec register state. */
  373. flush_altivec_to_thread(child);
  374. ret = set_vrregs(child, (unsigned long __user *)data);
  375. break;
  376. #endif
  377. default:
  378. ret = ptrace_request(child, request, addr, data);
  379. break;
  380. }
  381. out_tsk:
  382. put_task_struct(child);
  383. out:
  384. unlock_kernel();
  385. return ret;
  386. }