ptrace.c 9.9 KB

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