ptrace32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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/config.h>
  20. #include <linux/kernel.h>
  21. #include <linux/sched.h>
  22. #include <linux/mm.h>
  23. #include <linux/smp.h>
  24. #include <linux/smp_lock.h>
  25. #include <linux/errno.h>
  26. #include <linux/ptrace.h>
  27. #include <linux/user.h>
  28. #include <linux/security.h>
  29. #include <linux/signal.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/page.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/system.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. long compat_sys_ptrace(int request, int pid, unsigned long addr,
  40. unsigned long data)
  41. {
  42. struct task_struct *child;
  43. int ret = -EPERM;
  44. lock_kernel();
  45. if (request == PTRACE_TRACEME) {
  46. /* are we already being traced? */
  47. if (current->ptrace & PT_PTRACED)
  48. goto out;
  49. ret = security_ptrace(current->parent, current);
  50. if (ret)
  51. goto out;
  52. /* set the ptrace bit in the process flags. */
  53. current->ptrace |= PT_PTRACED;
  54. ret = 0;
  55. goto out;
  56. }
  57. ret = -ESRCH;
  58. read_lock(&tasklist_lock);
  59. child = find_task_by_pid(pid);
  60. if (child)
  61. get_task_struct(child);
  62. read_unlock(&tasklist_lock);
  63. if (!child)
  64. goto out;
  65. ret = -EPERM;
  66. if (pid == 1) /* you may not mess with init */
  67. goto out_tsk;
  68. if (request == PTRACE_ATTACH) {
  69. ret = ptrace_attach(child);
  70. goto out_tsk;
  71. }
  72. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  73. if (ret < 0)
  74. goto out_tsk;
  75. switch (request) {
  76. /* when I and D space are separate, these will need to be fixed. */
  77. case PTRACE_PEEKTEXT: /* read word at location addr. */
  78. case PTRACE_PEEKDATA: {
  79. unsigned int tmp;
  80. int copied;
  81. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  82. ret = -EIO;
  83. if (copied != sizeof(tmp))
  84. break;
  85. ret = put_user(tmp, (u32 __user *)data);
  86. break;
  87. }
  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. if (index < PT_FPR0) {
  123. tmp = get_reg(child, index);
  124. } else {
  125. flush_fp_to_thread(child);
  126. /*
  127. * the user space code considers the floating point
  128. * to be an array of unsigned int (32 bits) - the
  129. * index passed in is based on this assumption.
  130. */
  131. tmp = ((unsigned int *)child->thread.fpr)[index - PT_FPR0];
  132. }
  133. ret = put_user((unsigned int)tmp, (u32 __user *)data);
  134. break;
  135. }
  136. /*
  137. * Read 4 bytes out of the other process' pt_regs area
  138. * data is a pointer specifying where the user wants the
  139. * 4 bytes copied into
  140. * addr is the offset into the other process' pt_regs structure
  141. * that is to be read
  142. * (this is run in a 32-bit process looking at a 64-bit process)
  143. */
  144. case PPC_PTRACE_PEEKUSR_3264: {
  145. u32 index;
  146. u32 reg32bits;
  147. u64 tmp;
  148. u32 numReg;
  149. u32 part;
  150. ret = -EIO;
  151. /* Determine which register the user wants */
  152. index = (u64)addr >> 2;
  153. numReg = index / 2;
  154. /* Determine which part of the register the user wants */
  155. if (index % 2)
  156. part = 1; /* want the 2nd half of the register (right-most). */
  157. else
  158. part = 0; /* want the 1st half of the register (left-most). */
  159. /* Validate the input - check to see if address is on the wrong boundary or beyond the end of the user area */
  160. if ((addr & 3) || numReg > PT_FPSCR)
  161. break;
  162. if (numReg >= PT_FPR0) {
  163. flush_fp_to_thread(child);
  164. tmp = ((unsigned long int *)child->thread.fpr)[numReg - PT_FPR0];
  165. } else { /* register within PT_REGS struct */
  166. tmp = get_reg(child, numReg);
  167. }
  168. reg32bits = ((u32*)&tmp)[part];
  169. ret = put_user(reg32bits, (u32 __user *)data);
  170. break;
  171. }
  172. /* If I and D space are separate, this will have to be fixed. */
  173. case PTRACE_POKETEXT: /* write the word at location addr. */
  174. case PTRACE_POKEDATA: {
  175. unsigned int tmp;
  176. tmp = data;
  177. ret = 0;
  178. if (access_process_vm(child, addr, &tmp, sizeof(tmp), 1)
  179. == sizeof(tmp))
  180. break;
  181. ret = -EIO;
  182. break;
  183. }
  184. /*
  185. * Write 4 bytes into the other process' storage
  186. * data is the 4 bytes that the user wants written
  187. * addr is a pointer in the user's storage that contains an
  188. * 8 byte address in the other process where the 4 bytes
  189. * that is to be written
  190. * (this is run in a 32-bit process looking at a 64-bit process)
  191. * when I and D space are separate, these will need to be fixed.
  192. */
  193. case PPC_PTRACE_POKETEXT_3264:
  194. case PPC_PTRACE_POKEDATA_3264: {
  195. u32 tmp = data;
  196. u32 __user * addrOthers;
  197. /* Get the addr in the other process that we want to write into */
  198. ret = -EIO;
  199. if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
  200. break;
  201. ret = 0;
  202. if (access_process_vm(child, (u64)addrOthers, &tmp,
  203. sizeof(tmp), 1) == sizeof(tmp))
  204. break;
  205. ret = -EIO;
  206. break;
  207. }
  208. /* write the word at location addr in the USER area */
  209. case PTRACE_POKEUSR: {
  210. unsigned long index;
  211. ret = -EIO;
  212. /* convert to index and check */
  213. index = (unsigned long) addr >> 2;
  214. if ((addr & 3) || (index > PT_FPSCR32))
  215. break;
  216. if (index == PT_ORIG_R3)
  217. break;
  218. if (index < PT_FPR0) {
  219. ret = put_reg(child, index, data);
  220. } else {
  221. flush_fp_to_thread(child);
  222. /*
  223. * the user space code considers the floating point
  224. * to be an array of unsigned int (32 bits) - the
  225. * index passed in is based on this assumption.
  226. */
  227. ((unsigned int *)child->thread.fpr)[index - PT_FPR0] = data;
  228. ret = 0;
  229. }
  230. break;
  231. }
  232. /*
  233. * Write 4 bytes into the other process' pt_regs area
  234. * data is the 4 bytes that the user wants written
  235. * addr is the offset into the other process' pt_regs structure
  236. * that is to be written into
  237. * (this is run in a 32-bit process looking at a 64-bit process)
  238. */
  239. case PPC_PTRACE_POKEUSR_3264: {
  240. u32 index;
  241. u32 numReg;
  242. ret = -EIO;
  243. /* Determine which register the user wants */
  244. index = (u64)addr >> 2;
  245. numReg = index / 2;
  246. /*
  247. * Validate the input - check to see if address is on the
  248. * wrong boundary or beyond the end of the user area
  249. */
  250. if ((addr & 3) || (numReg > PT_FPSCR))
  251. break;
  252. /* Insure it is a register we let them change */
  253. if ((numReg == PT_ORIG_R3)
  254. || ((numReg > PT_CCR) && (numReg < PT_FPR0)))
  255. break;
  256. if (numReg >= PT_FPR0) {
  257. flush_fp_to_thread(child);
  258. }
  259. if (numReg == PT_MSR)
  260. data = (data & MSR_DEBUGCHANGE)
  261. | (child->thread.regs->msr & ~MSR_DEBUGCHANGE);
  262. ((u32*)child->thread.regs)[index] = data;
  263. ret = 0;
  264. break;
  265. }
  266. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  267. case PTRACE_CONT: { /* restart after signal. */
  268. ret = -EIO;
  269. if (!valid_signal(data))
  270. break;
  271. if (request == PTRACE_SYSCALL)
  272. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  273. else
  274. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  275. child->exit_code = data;
  276. /* make sure the single step bit is not set. */
  277. clear_single_step(child);
  278. wake_up_process(child);
  279. ret = 0;
  280. break;
  281. }
  282. /*
  283. * make the child exit. Best I can do is send it a sigkill.
  284. * perhaps it should be put in the status that it wants to
  285. * exit.
  286. */
  287. case PTRACE_KILL: {
  288. ret = 0;
  289. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  290. break;
  291. child->exit_code = SIGKILL;
  292. /* make sure the single step bit is not set. */
  293. clear_single_step(child);
  294. wake_up_process(child);
  295. break;
  296. }
  297. case PTRACE_SINGLESTEP: { /* set the trap flag. */
  298. ret = -EIO;
  299. if (!valid_signal(data))
  300. break;
  301. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  302. set_single_step(child);
  303. child->exit_code = data;
  304. /* give it a chance to run. */
  305. wake_up_process(child);
  306. ret = 0;
  307. break;
  308. }
  309. case PTRACE_GET_DEBUGREG: {
  310. ret = -EINVAL;
  311. /* We only support one DABR and no IABRS at the moment */
  312. if (addr > 0)
  313. break;
  314. ret = put_user(child->thread.dabr, (u32 __user *)data);
  315. break;
  316. }
  317. case PTRACE_SET_DEBUGREG:
  318. ret = ptrace_set_debugreg(child, addr, data);
  319. break;
  320. case PTRACE_DETACH:
  321. ret = ptrace_detach(child, data);
  322. break;
  323. case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
  324. int i;
  325. unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
  326. unsigned int __user *tmp = (unsigned int __user *)addr;
  327. for (i = 0; i < 32; i++) {
  328. ret = put_user(*reg, tmp);
  329. if (ret)
  330. break;
  331. reg++;
  332. tmp++;
  333. }
  334. break;
  335. }
  336. case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
  337. int i;
  338. unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
  339. unsigned int __user *tmp = (unsigned int __user *)addr;
  340. for (i = 0; i < 32; i++) {
  341. ret = get_user(*reg, tmp);
  342. if (ret)
  343. break;
  344. reg++;
  345. tmp++;
  346. }
  347. break;
  348. }
  349. case PPC_PTRACE_GETFPREGS: { /* Get FPRs 0 - 31. */
  350. int i;
  351. unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
  352. unsigned int __user *tmp = (unsigned int __user *)addr;
  353. flush_fp_to_thread(child);
  354. for (i = 0; i < 32; i++) {
  355. ret = put_user(*reg, tmp);
  356. if (ret)
  357. break;
  358. reg++;
  359. tmp++;
  360. }
  361. break;
  362. }
  363. case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */
  364. int i;
  365. unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
  366. unsigned int __user *tmp = (unsigned int __user *)addr;
  367. flush_fp_to_thread(child);
  368. for (i = 0; i < 32; i++) {
  369. ret = get_user(*reg, tmp);
  370. if (ret)
  371. break;
  372. reg++;
  373. tmp++;
  374. }
  375. break;
  376. }
  377. case PTRACE_GETEVENTMSG:
  378. ret = put_user(child->ptrace_message, (unsigned int __user *) data);
  379. break;
  380. #ifdef CONFIG_ALTIVEC
  381. case PTRACE_GETVRREGS:
  382. /* Get the child altivec register state. */
  383. flush_altivec_to_thread(child);
  384. ret = get_vrregs((unsigned long __user *)data, child);
  385. break;
  386. case PTRACE_SETVRREGS:
  387. /* Set the child altivec register state. */
  388. flush_altivec_to_thread(child);
  389. ret = set_vrregs(child, (unsigned long __user *)data);
  390. break;
  391. #endif
  392. default:
  393. ret = ptrace_request(child, request, addr, data);
  394. break;
  395. }
  396. out_tsk:
  397. put_task_struct(child);
  398. out:
  399. unlock_kernel();
  400. return ret;
  401. }