ptrace.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
  3. * these modifications are Copyright 2004-2010 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/elf.h>
  12. #include <linux/errno.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/user.h>
  15. #include <linux/regset.h>
  16. #include <linux/signal.h>
  17. #include <linux/tracehook.h>
  18. #include <linux/uaccess.h>
  19. #include <asm/page.h>
  20. #include <asm/pgtable.h>
  21. #include <asm/system.h>
  22. #include <asm/processor.h>
  23. #include <asm/asm-offsets.h>
  24. #include <asm/dma.h>
  25. #include <asm/fixed_code.h>
  26. #include <asm/cacheflush.h>
  27. #include <asm/mem_map.h>
  28. #include <asm/mmu_context.h>
  29. /*
  30. * does not yet catch signals sent when the child dies.
  31. * in exit.c or in signal.c.
  32. */
  33. /*
  34. * Get contents of register REGNO in task TASK.
  35. */
  36. static inline long
  37. get_reg(struct task_struct *task, long regno, unsigned long __user *datap)
  38. {
  39. long tmp;
  40. struct pt_regs *regs = task_pt_regs(task);
  41. if (regno & 3 || regno > PT_LAST_PSEUDO || regno < 0)
  42. return -EIO;
  43. switch (regno) {
  44. case PT_TEXT_ADDR:
  45. tmp = task->mm->start_code;
  46. break;
  47. case PT_TEXT_END_ADDR:
  48. tmp = task->mm->end_code;
  49. break;
  50. case PT_DATA_ADDR:
  51. tmp = task->mm->start_data;
  52. break;
  53. case PT_USP:
  54. tmp = task->thread.usp;
  55. break;
  56. default:
  57. if (regno < sizeof(*regs)) {
  58. void *reg_ptr = regs;
  59. tmp = *(long *)(reg_ptr + regno);
  60. } else
  61. return -EIO;
  62. }
  63. return put_user(tmp, datap);
  64. }
  65. /*
  66. * Write contents of register REGNO in task TASK.
  67. */
  68. static inline int
  69. put_reg(struct task_struct *task, long regno, unsigned long data)
  70. {
  71. struct pt_regs *regs = task_pt_regs(task);
  72. if (regno & 3 || regno > PT_LAST_PSEUDO || regno < 0)
  73. return -EIO;
  74. switch (regno) {
  75. case PT_PC:
  76. /*********************************************************************/
  77. /* At this point the kernel is most likely in exception. */
  78. /* The RETX register will be used to populate the pc of the process. */
  79. /*********************************************************************/
  80. regs->retx = data;
  81. regs->pc = data;
  82. break;
  83. case PT_RETX:
  84. break; /* regs->retx = data; break; */
  85. case PT_USP:
  86. regs->usp = data;
  87. task->thread.usp = data;
  88. break;
  89. case PT_SYSCFG: /* don't let userspace screw with this */
  90. if ((data & ~1) != 0x6)
  91. pr_warning("ptrace: ignore syscfg write of %#lx\n", data);
  92. break; /* regs->syscfg = data; break; */
  93. default:
  94. if (regno < sizeof(*regs)) {
  95. void *reg_offset = regs;
  96. *(long *)(reg_offset + regno) = data;
  97. }
  98. /* Ignore writes to pseudo registers */
  99. }
  100. return 0;
  101. }
  102. /*
  103. * check that an address falls within the bounds of the target process's memory mappings
  104. */
  105. static inline int is_user_addr_valid(struct task_struct *child,
  106. unsigned long start, unsigned long len)
  107. {
  108. struct vm_area_struct *vma;
  109. struct sram_list_struct *sraml;
  110. /* overflow */
  111. if (start + len < start)
  112. return -EIO;
  113. vma = find_vma(child->mm, start);
  114. if (vma && start >= vma->vm_start && start + len <= vma->vm_end)
  115. return 0;
  116. for (sraml = child->mm->context.sram_list; sraml; sraml = sraml->next)
  117. if (start >= (unsigned long)sraml->addr
  118. && start + len < (unsigned long)sraml->addr + sraml->length)
  119. return 0;
  120. if (start >= FIXED_CODE_START && start + len < FIXED_CODE_END)
  121. return 0;
  122. #ifdef CONFIG_APP_STACK_L1
  123. if (child->mm->context.l1_stack_save)
  124. if (start >= (unsigned long)l1_stack_base &&
  125. start + len < (unsigned long)l1_stack_base + l1_stack_len)
  126. return 0;
  127. #endif
  128. return -EIO;
  129. }
  130. /*
  131. * retrieve the contents of Blackfin userspace general registers
  132. */
  133. static int genregs_get(struct task_struct *target,
  134. const struct user_regset *regset,
  135. unsigned int pos, unsigned int count,
  136. void *kbuf, void __user *ubuf)
  137. {
  138. struct pt_regs *regs = task_pt_regs(target);
  139. int ret;
  140. /* This sucks ... */
  141. regs->usp = target->thread.usp;
  142. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  143. regs, 0, sizeof(*regs));
  144. if (ret < 0)
  145. return ret;
  146. return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  147. sizeof(*regs), -1);
  148. }
  149. /*
  150. * update the contents of the Blackfin userspace general registers
  151. */
  152. static int genregs_set(struct task_struct *target,
  153. const struct user_regset *regset,
  154. unsigned int pos, unsigned int count,
  155. const void *kbuf, const void __user *ubuf)
  156. {
  157. struct pt_regs *regs = task_pt_regs(target);
  158. int ret;
  159. /* Don't let people set SYSCFG (it's at the end of pt_regs) */
  160. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  161. regs, 0, PT_SYSCFG);
  162. if (ret < 0)
  163. return ret;
  164. /* This sucks ... */
  165. target->thread.usp = regs->usp;
  166. /* regs->retx = regs->pc; */
  167. return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  168. PT_SYSCFG, -1);
  169. }
  170. /*
  171. * Define the register sets available on the Blackfin under Linux
  172. */
  173. enum bfin_regset {
  174. REGSET_GENERAL,
  175. };
  176. static const struct user_regset bfin_regsets[] = {
  177. [REGSET_GENERAL] = {
  178. .core_note_type = NT_PRSTATUS,
  179. .n = sizeof(struct pt_regs) / sizeof(long),
  180. .size = sizeof(long),
  181. .align = sizeof(long),
  182. .get = genregs_get,
  183. .set = genregs_set,
  184. },
  185. };
  186. static const struct user_regset_view user_bfin_native_view = {
  187. .name = "Blackfin",
  188. .e_machine = EM_BLACKFIN,
  189. .regsets = bfin_regsets,
  190. .n = ARRAY_SIZE(bfin_regsets),
  191. };
  192. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  193. {
  194. return &user_bfin_native_view;
  195. }
  196. void user_enable_single_step(struct task_struct *child)
  197. {
  198. struct pt_regs *regs = task_pt_regs(child);
  199. regs->syscfg |= SYSCFG_SSSTEP;
  200. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  201. }
  202. void user_disable_single_step(struct task_struct *child)
  203. {
  204. struct pt_regs *regs = task_pt_regs(child);
  205. regs->syscfg &= ~SYSCFG_SSSTEP;
  206. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  207. }
  208. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  209. {
  210. int ret;
  211. unsigned long __user *datap = (unsigned long __user *)data;
  212. void *paddr = (void *)addr;
  213. switch (request) {
  214. /* when I and D space are separate, these will need to be fixed. */
  215. case PTRACE_PEEKDATA:
  216. pr_debug("ptrace: PEEKDATA\n");
  217. /* fall through */
  218. case PTRACE_PEEKTEXT: /* read word at location addr. */
  219. {
  220. unsigned long tmp = 0;
  221. int copied = 0, to_copy = sizeof(tmp);
  222. ret = -EIO;
  223. pr_debug("ptrace: PEEKTEXT at addr 0x%08lx + %i\n", addr, to_copy);
  224. if (is_user_addr_valid(child, addr, to_copy) < 0)
  225. break;
  226. pr_debug("ptrace: user address is valid\n");
  227. switch (bfin_mem_access_type(addr, to_copy)) {
  228. case BFIN_MEM_ACCESS_CORE:
  229. case BFIN_MEM_ACCESS_CORE_ONLY:
  230. copied = access_process_vm(child, addr, &tmp,
  231. to_copy, 0);
  232. if (copied)
  233. break;
  234. /* hrm, why didn't that work ... maybe no mapping */
  235. if (addr >= FIXED_CODE_START &&
  236. addr + to_copy <= FIXED_CODE_END) {
  237. copy_from_user_page(0, 0, 0, &tmp, paddr, to_copy);
  238. copied = to_copy;
  239. } else if (addr >= BOOT_ROM_START) {
  240. memcpy(&tmp, paddr, to_copy);
  241. copied = to_copy;
  242. }
  243. break;
  244. case BFIN_MEM_ACCESS_DMA:
  245. if (safe_dma_memcpy(&tmp, paddr, to_copy))
  246. copied = to_copy;
  247. break;
  248. case BFIN_MEM_ACCESS_ITEST:
  249. if (isram_memcpy(&tmp, paddr, to_copy))
  250. copied = to_copy;
  251. break;
  252. default:
  253. copied = 0;
  254. break;
  255. }
  256. pr_debug("ptrace: copied size %d [0x%08lx]\n", copied, tmp);
  257. if (copied == to_copy)
  258. ret = put_user(tmp, datap);
  259. break;
  260. }
  261. /* when I and D space are separate, this will have to be fixed. */
  262. case PTRACE_POKEDATA:
  263. pr_debug("ptrace: PTRACE_PEEKDATA\n");
  264. /* fall through */
  265. case PTRACE_POKETEXT: /* write the word at location addr. */
  266. {
  267. int copied = 0, to_copy = sizeof(data);
  268. ret = -EIO;
  269. pr_debug("ptrace: POKETEXT at addr 0x%08lx + %i bytes %lx\n",
  270. addr, to_copy, data);
  271. if (is_user_addr_valid(child, addr, to_copy) < 0)
  272. break;
  273. pr_debug("ptrace: user address is valid\n");
  274. switch (bfin_mem_access_type(addr, to_copy)) {
  275. case BFIN_MEM_ACCESS_CORE:
  276. case BFIN_MEM_ACCESS_CORE_ONLY:
  277. copied = access_process_vm(child, addr, &data,
  278. to_copy, 1);
  279. break;
  280. case BFIN_MEM_ACCESS_DMA:
  281. if (safe_dma_memcpy(paddr, &data, to_copy))
  282. copied = to_copy;
  283. break;
  284. case BFIN_MEM_ACCESS_ITEST:
  285. if (isram_memcpy(paddr, &data, to_copy))
  286. copied = to_copy;
  287. break;
  288. default:
  289. copied = 0;
  290. break;
  291. }
  292. pr_debug("ptrace: copied size %d\n", copied);
  293. if (copied == to_copy)
  294. ret = 0;
  295. break;
  296. }
  297. case PTRACE_PEEKUSR:
  298. switch (addr) {
  299. #ifdef CONFIG_BINFMT_ELF_FDPIC /* backwards compat */
  300. case PT_FDPIC_EXEC:
  301. request = PTRACE_GETFDPIC;
  302. addr = PTRACE_GETFDPIC_EXEC;
  303. goto case_default;
  304. case PT_FDPIC_INTERP:
  305. request = PTRACE_GETFDPIC;
  306. addr = PTRACE_GETFDPIC_INTERP;
  307. goto case_default;
  308. #endif
  309. default:
  310. ret = get_reg(child, addr, datap);
  311. }
  312. pr_debug("ptrace: PEEKUSR reg %li with %#lx = %i\n", addr, data, ret);
  313. break;
  314. case PTRACE_POKEUSR:
  315. ret = put_reg(child, addr, data);
  316. pr_debug("ptrace: POKEUSR reg %li with %li = %i\n", addr, data, ret);
  317. break;
  318. case PTRACE_GETREGS:
  319. pr_debug("ptrace: PTRACE_GETREGS\n");
  320. return copy_regset_to_user(child, &user_bfin_native_view,
  321. REGSET_GENERAL,
  322. 0, sizeof(struct pt_regs),
  323. (void __user *)data);
  324. case PTRACE_SETREGS:
  325. pr_debug("ptrace: PTRACE_SETREGS\n");
  326. return copy_regset_from_user(child, &user_bfin_native_view,
  327. REGSET_GENERAL,
  328. 0, sizeof(struct pt_regs),
  329. (const void __user *)data);
  330. case_default:
  331. default:
  332. ret = ptrace_request(child, request, addr, data);
  333. break;
  334. }
  335. return ret;
  336. }
  337. asmlinkage int syscall_trace_enter(struct pt_regs *regs)
  338. {
  339. int ret = 0;
  340. if (test_thread_flag(TIF_SYSCALL_TRACE))
  341. ret = tracehook_report_syscall_entry(regs);
  342. return ret;
  343. }
  344. asmlinkage void syscall_trace_leave(struct pt_regs *regs)
  345. {
  346. int step;
  347. step = test_thread_flag(TIF_SINGLESTEP);
  348. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  349. tracehook_report_syscall_exit(regs, step);
  350. }