/arch/ppc64/kernel/ptrace32.c

https://bitbucket.org/evzijst/gittest · C · 420 lines · 283 code · 45 blank · 92 comment · 63 complexity · 5c3de20cfcb24df1d31384203080aa2c MD5 · raw file

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