ptrace.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
  3. * these modifications are Copyright 2004-2009 Analog Devices Inc.
  4. *
  5. * Licensed under the GPL-2
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/sched.h>
  9. #include <linux/mm.h>
  10. #include <linux/smp.h>
  11. #include <linux/errno.h>
  12. #include <linux/ptrace.h>
  13. #include <linux/user.h>
  14. #include <linux/signal.h>
  15. #include <linux/uaccess.h>
  16. #include <asm/page.h>
  17. #include <asm/pgtable.h>
  18. #include <asm/system.h>
  19. #include <asm/processor.h>
  20. #include <asm/asm-offsets.h>
  21. #include <asm/dma.h>
  22. #include <asm/fixed_code.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/mem_map.h>
  25. /*
  26. * does not yet catch signals sent when the child dies.
  27. * in exit.c or in signal.c.
  28. */
  29. /* Find the stack offset for a register, relative to thread.esp0. */
  30. #define PT_REG(reg) ((long)&((struct pt_regs *)0)->reg)
  31. /*
  32. * Get the address of the live pt_regs for the specified task.
  33. * These are saved onto the top kernel stack when the process
  34. * is not running.
  35. *
  36. * Note: if a user thread is execve'd from kernel space, the
  37. * kernel stack will not be empty on entry to the kernel, so
  38. * ptracing these tasks will fail.
  39. */
  40. static inline struct pt_regs *task_pt_regs(struct task_struct *task)
  41. {
  42. return (struct pt_regs *)
  43. ((unsigned long)task_stack_page(task) +
  44. (THREAD_SIZE - sizeof(struct pt_regs)));
  45. }
  46. /*
  47. * Get all user integer registers.
  48. */
  49. static inline int ptrace_getregs(struct task_struct *tsk, void __user *uregs)
  50. {
  51. struct pt_regs regs;
  52. memcpy(&regs, task_pt_regs(tsk), sizeof(regs));
  53. regs.usp = tsk->thread.usp;
  54. return copy_to_user(uregs, &regs, sizeof(struct pt_regs)) ? -EFAULT : 0;
  55. }
  56. /* Mapping from PT_xxx to the stack offset at which the register is
  57. * saved. Notice that usp has no stack-slot and needs to be treated
  58. * specially (see get_reg/put_reg below).
  59. */
  60. /*
  61. * Get contents of register REGNO in task TASK.
  62. */
  63. static inline long
  64. get_reg(struct task_struct *task, long regno, unsigned long __user *datap)
  65. {
  66. long tmp;
  67. struct pt_regs *regs = task_pt_regs(task);
  68. if (regno & 3 || regno > PT_LAST_PSEUDO || regno < 0)
  69. return -EIO;
  70. switch (regno) {
  71. case PT_TEXT_ADDR:
  72. tmp = task->mm->start_code;
  73. break;
  74. case PT_TEXT_END_ADDR:
  75. tmp = task->mm->end_code;
  76. break;
  77. case PT_DATA_ADDR:
  78. tmp = task->mm->start_data;
  79. break;
  80. case PT_USP:
  81. tmp = task->thread.usp;
  82. break;
  83. default:
  84. if (regno < sizeof(*regs)) {
  85. void *reg_ptr = regs;
  86. tmp = *(long *)(reg_ptr + regno);
  87. } else
  88. return -EIO;
  89. }
  90. return put_user(tmp, datap);
  91. }
  92. /*
  93. * Write contents of register REGNO in task TASK.
  94. */
  95. static inline int
  96. put_reg(struct task_struct *task, long regno, unsigned long data)
  97. {
  98. struct pt_regs *regs = task_pt_regs(task);
  99. if (regno & 3 || regno > PT_LAST_PSEUDO || regno < 0)
  100. return -EIO;
  101. switch (regno) {
  102. case PT_PC:
  103. /*********************************************************************/
  104. /* At this point the kernel is most likely in exception. */
  105. /* The RETX register will be used to populate the pc of the process. */
  106. /*********************************************************************/
  107. regs->retx = data;
  108. regs->pc = data;
  109. break;
  110. case PT_RETX:
  111. break; /* regs->retx = data; break; */
  112. case PT_USP:
  113. regs->usp = data;
  114. task->thread.usp = data;
  115. break;
  116. case PT_SYSCFG: /* don't let userspace screw with this */
  117. if ((data & ~1) != 0x6)
  118. pr_warning("ptrace: ignore syscfg write of %#lx\n", data);
  119. break; /* regs->syscfg = data; break; */
  120. default:
  121. if (regno < sizeof(*regs)) {
  122. void *reg_offset = regs;
  123. *(long *)(reg_offset + regno) = data;
  124. }
  125. /* Ignore writes to pseudo registers */
  126. }
  127. return 0;
  128. }
  129. /*
  130. * check that an address falls within the bounds of the target process's memory mappings
  131. */
  132. static inline int is_user_addr_valid(struct task_struct *child,
  133. unsigned long start, unsigned long len)
  134. {
  135. struct vm_area_struct *vma;
  136. struct sram_list_struct *sraml;
  137. /* overflow */
  138. if (start + len < start)
  139. return -EIO;
  140. vma = find_vma(child->mm, start);
  141. if (vma && start >= vma->vm_start && start + len <= vma->vm_end)
  142. return 0;
  143. for (sraml = child->mm->context.sram_list; sraml; sraml = sraml->next)
  144. if (start >= (unsigned long)sraml->addr
  145. && start + len < (unsigned long)sraml->addr + sraml->length)
  146. return 0;
  147. if (start >= FIXED_CODE_START && start + len < FIXED_CODE_END)
  148. return 0;
  149. return -EIO;
  150. }
  151. void ptrace_enable(struct task_struct *child)
  152. {
  153. struct pt_regs *regs = task_pt_regs(child);
  154. regs->syscfg |= SYSCFG_SSSTEP;
  155. }
  156. /*
  157. * Called by kernel/ptrace.c when detaching..
  158. *
  159. * Make sure the single step bit is not set.
  160. */
  161. void ptrace_disable(struct task_struct *child)
  162. {
  163. struct pt_regs *regs = task_pt_regs(child);
  164. regs->syscfg &= ~SYSCFG_SSSTEP;
  165. }
  166. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  167. {
  168. int ret;
  169. unsigned long __user *datap = (unsigned long __user *)data;
  170. void *paddr = (void *)addr;
  171. switch (request) {
  172. /* when I and D space are separate, these will need to be fixed. */
  173. case PTRACE_PEEKDATA:
  174. pr_debug("ptrace: PEEKDATA\n");
  175. /* fall through */
  176. case PTRACE_PEEKTEXT: /* read word at location addr. */
  177. {
  178. unsigned long tmp = 0;
  179. int copied = 0, to_copy = sizeof(tmp);
  180. ret = -EIO;
  181. pr_debug("ptrace: PEEKTEXT at addr 0x%08lx + %i\n", addr, to_copy);
  182. if (is_user_addr_valid(child, addr, to_copy) < 0)
  183. break;
  184. pr_debug("ptrace: user address is valid\n");
  185. switch (bfin_mem_access_type(addr, to_copy)) {
  186. case BFIN_MEM_ACCESS_CORE:
  187. case BFIN_MEM_ACCESS_CORE_ONLY:
  188. copied = access_process_vm(child, addr, &tmp,
  189. to_copy, 0);
  190. if (copied)
  191. break;
  192. /* hrm, why didn't that work ... maybe no mapping */
  193. if (addr >= FIXED_CODE_START &&
  194. addr + to_copy <= FIXED_CODE_END) {
  195. copy_from_user_page(0, 0, 0, &tmp, paddr, to_copy);
  196. copied = to_copy;
  197. } else if (addr >= BOOT_ROM_START) {
  198. memcpy(&tmp, paddr, to_copy);
  199. copied = to_copy;
  200. }
  201. break;
  202. case BFIN_MEM_ACCESS_DMA:
  203. if (safe_dma_memcpy(&tmp, paddr, to_copy))
  204. copied = to_copy;
  205. break;
  206. case BFIN_MEM_ACCESS_ITEST:
  207. if (isram_memcpy(&tmp, paddr, to_copy))
  208. copied = to_copy;
  209. break;
  210. default:
  211. copied = 0;
  212. break;
  213. }
  214. pr_debug("ptrace: copied size %d [0x%08lx]\n", copied, tmp);
  215. if (copied == to_copy)
  216. ret = put_user(tmp, datap);
  217. break;
  218. }
  219. #ifdef CONFIG_BINFMT_ELF_FDPIC
  220. case PTRACE_GETFDPIC: {
  221. unsigned long tmp = 0;
  222. switch (addr) {
  223. case_PTRACE_GETFDPIC_EXEC:
  224. case PTRACE_GETFDPIC_EXEC:
  225. tmp = child->mm->context.exec_fdpic_loadmap;
  226. break;
  227. case_PTRACE_GETFDPIC_INTERP:
  228. case PTRACE_GETFDPIC_INTERP:
  229. tmp = child->mm->context.interp_fdpic_loadmap;
  230. break;
  231. default:
  232. break;
  233. }
  234. ret = put_user(tmp, datap);
  235. break;
  236. }
  237. #endif
  238. /* when I and D space are separate, this will have to be fixed. */
  239. case PTRACE_POKEDATA:
  240. pr_debug("ptrace: PTRACE_PEEKDATA\n");
  241. /* fall through */
  242. case PTRACE_POKETEXT: /* write the word at location addr. */
  243. {
  244. int copied = 0, to_copy = sizeof(data);
  245. ret = -EIO;
  246. pr_debug("ptrace: POKETEXT at addr 0x%08lx + %i bytes %lx\n",
  247. addr, to_copy, data);
  248. if (is_user_addr_valid(child, addr, to_copy) < 0)
  249. break;
  250. pr_debug("ptrace: user address is valid\n");
  251. switch (bfin_mem_access_type(addr, to_copy)) {
  252. case BFIN_MEM_ACCESS_CORE:
  253. case BFIN_MEM_ACCESS_CORE_ONLY:
  254. copied = access_process_vm(child, addr, &data,
  255. to_copy, 1);
  256. break;
  257. case BFIN_MEM_ACCESS_DMA:
  258. if (safe_dma_memcpy(paddr, &data, to_copy))
  259. copied = to_copy;
  260. break;
  261. case BFIN_MEM_ACCESS_ITEST:
  262. if (isram_memcpy(paddr, &data, to_copy))
  263. copied = to_copy;
  264. break;
  265. default:
  266. copied = 0;
  267. break;
  268. }
  269. pr_debug("ptrace: copied size %d\n", copied);
  270. if (copied == to_copy)
  271. ret = 0;
  272. break;
  273. }
  274. case PTRACE_PEEKUSR:
  275. switch (addr) {
  276. #ifdef CONFIG_BINFMT_ELF_FDPIC /* backwards compat */
  277. case PT_FDPIC_EXEC: goto case_PTRACE_GETFDPIC_EXEC;
  278. case PT_FDPIC_INTERP: goto case_PTRACE_GETFDPIC_INTERP;
  279. #endif
  280. default:
  281. ret = get_reg(child, addr, datap);
  282. }
  283. pr_debug("ptrace: PEEKUSR reg %li with %#lx = %i\n", addr, data, ret);
  284. break;
  285. case PTRACE_POKEUSR:
  286. ret = put_reg(child, addr, data);
  287. pr_debug("ptrace: POKEUSR reg %li with %li = %i\n", addr, data, ret);
  288. break;
  289. case PTRACE_GETREGS:
  290. /* Get all gp regs from the child. */
  291. ret = ptrace_getregs(child, datap);
  292. break;
  293. case PTRACE_SETREGS:
  294. printk(KERN_WARNING "ptrace: SETREGS: **** NOT IMPLEMENTED ***\n");
  295. /* Set all gp regs in the child. */
  296. ret = 0;
  297. break;
  298. default:
  299. ret = ptrace_request(child, request, addr, data);
  300. break;
  301. }
  302. return ret;
  303. }
  304. asmlinkage void syscall_trace(void)
  305. {
  306. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  307. return;
  308. if (!(current->ptrace & PT_PTRACED))
  309. return;
  310. /* the 0x80 provides a way for the tracing parent to distinguish
  311. * between a syscall stop and SIGTRAP delivery
  312. */
  313. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  314. ? 0x80 : 0));
  315. /*
  316. * this isn't the same as continuing with a signal, but it will do
  317. * for normal use. strace only continues with a signal if the
  318. * stopping signal is not SIGTRAP. -brl
  319. */
  320. if (current->exit_code) {
  321. send_sig(current->exit_code, current, 1);
  322. current->exit_code = 0;
  323. }
  324. }