ptrace.c 9.8 KB

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