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