ptrace.c 9.7 KB

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