ptrace.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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/processor.h>
  22. #include <asm/asm-offsets.h>
  23. #include <asm/dma.h>
  24. #include <asm/fixed_code.h>
  25. #include <asm/cacheflush.h>
  26. #include <asm/mem_map.h>
  27. #include <asm/mmu_context.h>
  28. /*
  29. * does not yet catch signals sent when the child dies.
  30. * in exit.c or in signal.c.
  31. */
  32. /*
  33. * Get contents of register REGNO in task TASK.
  34. */
  35. static inline long
  36. get_reg(struct task_struct *task, unsigned long regno,
  37. 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)
  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, unsigned long regno, unsigned long data)
  70. {
  71. struct pt_regs *regs = task_pt_regs(task);
  72. if (regno & 3 || regno > PT_LAST_PSEUDO)
  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. int
  106. is_user_addr_valid(struct task_struct *child, unsigned long start, unsigned long len)
  107. {
  108. bool valid;
  109. struct vm_area_struct *vma;
  110. struct sram_list_struct *sraml;
  111. /* overflow */
  112. if (start + len < start)
  113. return -EIO;
  114. down_read(&child->mm->mmap_sem);
  115. vma = find_vma(child->mm, start);
  116. valid = vma && start >= vma->vm_start && start + len <= vma->vm_end;
  117. up_read(&child->mm->mmap_sem);
  118. if (valid)
  119. return 0;
  120. for (sraml = child->mm->context.sram_list; sraml; sraml = sraml->next)
  121. if (start >= (unsigned long)sraml->addr
  122. && start + len < (unsigned long)sraml->addr + sraml->length)
  123. return 0;
  124. if (start >= FIXED_CODE_START && start + len < FIXED_CODE_END)
  125. return 0;
  126. #ifdef CONFIG_APP_STACK_L1
  127. if (child->mm->context.l1_stack_save)
  128. if (start >= (unsigned long)l1_stack_base &&
  129. start + len < (unsigned long)l1_stack_base + l1_stack_len)
  130. return 0;
  131. #endif
  132. return -EIO;
  133. }
  134. /*
  135. * retrieve the contents of Blackfin userspace general registers
  136. */
  137. static int genregs_get(struct task_struct *target,
  138. const struct user_regset *regset,
  139. unsigned int pos, unsigned int count,
  140. void *kbuf, void __user *ubuf)
  141. {
  142. struct pt_regs *regs = task_pt_regs(target);
  143. int ret;
  144. /* This sucks ... */
  145. regs->usp = target->thread.usp;
  146. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  147. regs, 0, sizeof(*regs));
  148. if (ret < 0)
  149. return ret;
  150. return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  151. sizeof(*regs), -1);
  152. }
  153. /*
  154. * update the contents of the Blackfin userspace general registers
  155. */
  156. static int genregs_set(struct task_struct *target,
  157. const struct user_regset *regset,
  158. unsigned int pos, unsigned int count,
  159. const void *kbuf, const void __user *ubuf)
  160. {
  161. struct pt_regs *regs = task_pt_regs(target);
  162. int ret;
  163. /* Don't let people set SYSCFG (it's at the end of pt_regs) */
  164. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  165. regs, 0, PT_SYSCFG);
  166. if (ret < 0)
  167. return ret;
  168. /* This sucks ... */
  169. target->thread.usp = regs->usp;
  170. /* regs->retx = regs->pc; */
  171. return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  172. PT_SYSCFG, -1);
  173. }
  174. /*
  175. * Define the register sets available on the Blackfin under Linux
  176. */
  177. enum bfin_regset {
  178. REGSET_GENERAL,
  179. };
  180. static const struct user_regset bfin_regsets[] = {
  181. [REGSET_GENERAL] = {
  182. .core_note_type = NT_PRSTATUS,
  183. .n = sizeof(struct pt_regs) / sizeof(long),
  184. .size = sizeof(long),
  185. .align = sizeof(long),
  186. .get = genregs_get,
  187. .set = genregs_set,
  188. },
  189. };
  190. static const struct user_regset_view user_bfin_native_view = {
  191. .name = "Blackfin",
  192. .e_machine = EM_BLACKFIN,
  193. .regsets = bfin_regsets,
  194. .n = ARRAY_SIZE(bfin_regsets),
  195. };
  196. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  197. {
  198. return &user_bfin_native_view;
  199. }
  200. void user_enable_single_step(struct task_struct *child)
  201. {
  202. struct pt_regs *regs = task_pt_regs(child);
  203. regs->syscfg |= SYSCFG_SSSTEP;
  204. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  205. }
  206. void user_disable_single_step(struct task_struct *child)
  207. {
  208. struct pt_regs *regs = task_pt_regs(child);
  209. regs->syscfg &= ~SYSCFG_SSSTEP;
  210. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  211. }
  212. long arch_ptrace(struct task_struct *child, long request,
  213. unsigned long addr, unsigned long data)
  214. {
  215. int ret;
  216. unsigned long __user *datap = (unsigned long __user *)data;
  217. void *paddr = (void *)addr;
  218. switch (request) {
  219. /* when I and D space are separate, these will need to be fixed. */
  220. case PTRACE_PEEKDATA:
  221. pr_debug("ptrace: PEEKDATA\n");
  222. /* fall through */
  223. case PTRACE_PEEKTEXT: /* read word at location addr. */
  224. {
  225. unsigned long tmp = 0;
  226. int copied = 0, to_copy = sizeof(tmp);
  227. ret = -EIO;
  228. pr_debug("ptrace: PEEKTEXT at addr 0x%08lx + %i\n", addr, to_copy);
  229. if (is_user_addr_valid(child, addr, to_copy) < 0)
  230. break;
  231. pr_debug("ptrace: user address is valid\n");
  232. switch (bfin_mem_access_type(addr, to_copy)) {
  233. case BFIN_MEM_ACCESS_CORE:
  234. case BFIN_MEM_ACCESS_CORE_ONLY:
  235. copied = ptrace_access_vm(child, addr, &tmp,
  236. to_copy, FOLL_FORCE);
  237. if (copied)
  238. break;
  239. /* hrm, why didn't that work ... maybe no mapping */
  240. if (addr >= FIXED_CODE_START &&
  241. addr + to_copy <= FIXED_CODE_END) {
  242. copy_from_user_page(0, 0, 0, &tmp, paddr, to_copy);
  243. copied = to_copy;
  244. } else if (addr >= BOOT_ROM_START) {
  245. memcpy(&tmp, paddr, to_copy);
  246. copied = to_copy;
  247. }
  248. break;
  249. case BFIN_MEM_ACCESS_DMA:
  250. if (safe_dma_memcpy(&tmp, paddr, to_copy))
  251. copied = to_copy;
  252. break;
  253. case BFIN_MEM_ACCESS_ITEST:
  254. if (isram_memcpy(&tmp, paddr, to_copy))
  255. copied = to_copy;
  256. break;
  257. default:
  258. copied = 0;
  259. break;
  260. }
  261. pr_debug("ptrace: copied size %d [0x%08lx]\n", copied, tmp);
  262. if (copied == to_copy)
  263. ret = put_user(tmp, datap);
  264. break;
  265. }
  266. /* when I and D space are separate, this will have to be fixed. */
  267. case PTRACE_POKEDATA:
  268. pr_debug("ptrace: PTRACE_PEEKDATA\n");
  269. /* fall through */
  270. case PTRACE_POKETEXT: /* write the word at location addr. */
  271. {
  272. int copied = 0, to_copy = sizeof(data);
  273. ret = -EIO;
  274. pr_debug("ptrace: POKETEXT at addr 0x%08lx + %i bytes %lx\n",
  275. addr, to_copy, data);
  276. if (is_user_addr_valid(child, addr, to_copy) < 0)
  277. break;
  278. pr_debug("ptrace: user address is valid\n");
  279. switch (bfin_mem_access_type(addr, to_copy)) {
  280. case BFIN_MEM_ACCESS_CORE:
  281. case BFIN_MEM_ACCESS_CORE_ONLY:
  282. copied = ptrace_access_vm(child, addr, &data,
  283. to_copy,
  284. FOLL_FORCE | FOLL_WRITE);
  285. break;
  286. case BFIN_MEM_ACCESS_DMA:
  287. if (safe_dma_memcpy(paddr, &data, to_copy))
  288. copied = to_copy;
  289. break;
  290. case BFIN_MEM_ACCESS_ITEST:
  291. if (isram_memcpy(paddr, &data, to_copy))
  292. copied = to_copy;
  293. break;
  294. default:
  295. copied = 0;
  296. break;
  297. }
  298. pr_debug("ptrace: copied size %d\n", copied);
  299. if (copied == to_copy)
  300. ret = 0;
  301. break;
  302. }
  303. case PTRACE_PEEKUSR:
  304. switch (addr) {
  305. #ifdef CONFIG_BINFMT_ELF_FDPIC /* backwards compat */
  306. case PT_FDPIC_EXEC:
  307. request = PTRACE_GETFDPIC;
  308. addr = PTRACE_GETFDPIC_EXEC;
  309. goto case_default;
  310. case PT_FDPIC_INTERP:
  311. request = PTRACE_GETFDPIC;
  312. addr = PTRACE_GETFDPIC_INTERP;
  313. goto case_default;
  314. #endif
  315. default:
  316. ret = get_reg(child, addr, datap);
  317. }
  318. pr_debug("ptrace: PEEKUSR reg %li with %#lx = %i\n", addr, data, ret);
  319. break;
  320. case PTRACE_POKEUSR:
  321. ret = put_reg(child, addr, data);
  322. pr_debug("ptrace: POKEUSR reg %li with %li = %i\n", addr, data, ret);
  323. break;
  324. case PTRACE_GETREGS:
  325. pr_debug("ptrace: PTRACE_GETREGS\n");
  326. return copy_regset_to_user(child, &user_bfin_native_view,
  327. REGSET_GENERAL,
  328. 0, sizeof(struct pt_regs),
  329. datap);
  330. case PTRACE_SETREGS:
  331. pr_debug("ptrace: PTRACE_SETREGS\n");
  332. return copy_regset_from_user(child, &user_bfin_native_view,
  333. REGSET_GENERAL,
  334. 0, sizeof(struct pt_regs),
  335. datap);
  336. case_default:
  337. default:
  338. ret = ptrace_request(child, request, addr, data);
  339. break;
  340. }
  341. return ret;
  342. }
  343. asmlinkage int syscall_trace_enter(struct pt_regs *regs)
  344. {
  345. int ret = 0;
  346. if (test_thread_flag(TIF_SYSCALL_TRACE))
  347. ret = tracehook_report_syscall_entry(regs);
  348. return ret;
  349. }
  350. asmlinkage void syscall_trace_leave(struct pt_regs *regs)
  351. {
  352. int step;
  353. step = test_thread_flag(TIF_SINGLESTEP);
  354. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  355. tracehook_report_syscall_exit(regs, step);
  356. }