ptrace32.c 10 KB

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