ptrace32.c 11 KB

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