ptrace.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
  3. * these modifications are Copyright 2004-2009 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/errno.h>
  12. #include <linux/ptrace.h>
  13. #include <linux/user.h>
  14. #include <linux/signal.h>
  15. #include <linux/uaccess.h>
  16. #include <asm/page.h>
  17. #include <asm/pgtable.h>
  18. #include <asm/system.h>
  19. #include <asm/processor.h>
  20. #include <asm/asm-offsets.h>
  21. #include <asm/dma.h>
  22. #include <asm/fixed_code.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/mem_map.h>
  25. #define TEXT_OFFSET 0
  26. /*
  27. * does not yet catch signals sent when the child dies.
  28. * in exit.c or in signal.c.
  29. */
  30. /* determines which bits in the SYSCFG reg the user has access to. */
  31. /* 1 = access 0 = no access */
  32. #define SYSCFG_MASK 0x0007 /* SYSCFG reg */
  33. /* sets the trace bits. */
  34. #define TRACE_BITS 0x0001
  35. /* Find the stack offset for a register, relative to thread.esp0. */
  36. #define PT_REG(reg) ((long)&((struct pt_regs *)0)->reg)
  37. /*
  38. * Get the address of the live pt_regs for the specified task.
  39. * These are saved onto the top kernel stack when the process
  40. * is not running.
  41. *
  42. * Note: if a user thread is execve'd from kernel space, the
  43. * kernel stack will not be empty on entry to the kernel, so
  44. * ptracing these tasks will fail.
  45. */
  46. static inline struct pt_regs *get_user_regs(struct task_struct *task)
  47. {
  48. return (struct pt_regs *)
  49. ((unsigned long)task_stack_page(task) +
  50. (THREAD_SIZE - sizeof(struct pt_regs)));
  51. }
  52. /*
  53. * Get all user integer registers.
  54. */
  55. static inline int ptrace_getregs(struct task_struct *tsk, void __user *uregs)
  56. {
  57. struct pt_regs regs;
  58. memcpy(&regs, get_user_regs(tsk), sizeof(regs));
  59. regs.usp = tsk->thread.usp;
  60. return copy_to_user(uregs, &regs, sizeof(struct pt_regs)) ? -EFAULT : 0;
  61. }
  62. /* Mapping from PT_xxx to the stack offset at which the register is
  63. * saved. Notice that usp has no stack-slot and needs to be treated
  64. * specially (see get_reg/put_reg below).
  65. */
  66. /*
  67. * Get contents of register REGNO in task TASK.
  68. */
  69. static inline long get_reg(struct task_struct *task, int regno)
  70. {
  71. unsigned char *reg_ptr;
  72. struct pt_regs *regs =
  73. (struct pt_regs *)((unsigned long)task_stack_page(task) +
  74. (THREAD_SIZE - sizeof(struct pt_regs)));
  75. reg_ptr = (char *)regs;
  76. switch (regno) {
  77. case PT_USP:
  78. return task->thread.usp;
  79. default:
  80. if (regno <= 216)
  81. return *(long *)(reg_ptr + regno);
  82. }
  83. /* slight mystery ... never seems to come here but kernel misbehaves without this code! */
  84. printk(KERN_WARNING "Request to get for unknown register %d\n", regno);
  85. return 0;
  86. }
  87. /*
  88. * Write contents of register REGNO in task TASK.
  89. */
  90. static inline int
  91. put_reg(struct task_struct *task, int regno, unsigned long data)
  92. {
  93. char *reg_ptr;
  94. struct pt_regs *regs =
  95. (struct pt_regs *)((unsigned long)task_stack_page(task) +
  96. (THREAD_SIZE - sizeof(struct pt_regs)));
  97. reg_ptr = (char *)regs;
  98. switch (regno) {
  99. case PT_PC:
  100. /*********************************************************************/
  101. /* At this point the kernel is most likely in exception. */
  102. /* The RETX register will be used to populate the pc of the process. */
  103. /*********************************************************************/
  104. regs->retx = data;
  105. regs->pc = data;
  106. break;
  107. case PT_RETX:
  108. break; /* regs->retx = data; break; */
  109. case PT_USP:
  110. regs->usp = data;
  111. task->thread.usp = data;
  112. break;
  113. default:
  114. if (regno <= 216)
  115. *(long *)(reg_ptr + regno) = data;
  116. }
  117. return 0;
  118. }
  119. /*
  120. * check that an address falls within the bounds of the target process's memory mappings
  121. */
  122. static inline int is_user_addr_valid(struct task_struct *child,
  123. unsigned long start, unsigned long len)
  124. {
  125. struct vm_area_struct *vma;
  126. struct sram_list_struct *sraml;
  127. /* overflow */
  128. if (start + len < start)
  129. return -EIO;
  130. vma = find_vma(child->mm, start);
  131. if (vma && start >= vma->vm_start && start + len <= vma->vm_end)
  132. return 0;
  133. for (sraml = child->mm->context.sram_list; sraml; sraml = sraml->next)
  134. if (start >= (unsigned long)sraml->addr
  135. && start + len < (unsigned long)sraml->addr + sraml->length)
  136. return 0;
  137. if (start >= FIXED_CODE_START && start + len < FIXED_CODE_END)
  138. return 0;
  139. return -EIO;
  140. }
  141. void ptrace_enable(struct task_struct *child)
  142. {
  143. unsigned long tmp;
  144. tmp = get_reg(child, PT_SYSCFG) | (TRACE_BITS);
  145. put_reg(child, PT_SYSCFG, tmp);
  146. }
  147. /*
  148. * Called by kernel/ptrace.c when detaching..
  149. *
  150. * Make sure the single step bit is not set.
  151. */
  152. void ptrace_disable(struct task_struct *child)
  153. {
  154. unsigned long tmp;
  155. /* make sure the single step bit is not set. */
  156. tmp = get_reg(child, PT_SYSCFG) & ~TRACE_BITS;
  157. put_reg(child, PT_SYSCFG, tmp);
  158. }
  159. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  160. {
  161. int ret;
  162. unsigned long __user *datap = (unsigned long __user *)data;
  163. void *paddr = (void *)addr;
  164. switch (request) {
  165. /* when I and D space are separate, these will need to be fixed. */
  166. case PTRACE_PEEKDATA:
  167. pr_debug("ptrace: PEEKDATA\n");
  168. /* fall through */
  169. case PTRACE_PEEKTEXT: /* read word at location addr. */
  170. {
  171. unsigned long tmp = 0;
  172. int copied = 0, to_copy = sizeof(tmp);
  173. ret = -EIO;
  174. pr_debug("ptrace: PEEKTEXT at addr 0x%08lx + %i\n", addr, to_copy);
  175. if (is_user_addr_valid(child, addr, to_copy) < 0)
  176. break;
  177. pr_debug("ptrace: user address is valid\n");
  178. switch (bfin_mem_access_type(addr, to_copy)) {
  179. case BFIN_MEM_ACCESS_CORE:
  180. case BFIN_MEM_ACCESS_CORE_ONLY:
  181. copied = access_process_vm(child, addr, &tmp,
  182. to_copy, 0);
  183. if (copied)
  184. break;
  185. /* hrm, why didn't that work ... maybe no mapping */
  186. if (addr >= FIXED_CODE_START &&
  187. addr + to_copy <= FIXED_CODE_END) {
  188. copy_from_user_page(0, 0, 0, &tmp, paddr, to_copy);
  189. copied = to_copy;
  190. } else if (addr >= BOOT_ROM_START) {
  191. memcpy(&tmp, paddr, to_copy);
  192. copied = to_copy;
  193. }
  194. break;
  195. case BFIN_MEM_ACCESS_DMA:
  196. if (safe_dma_memcpy(&tmp, paddr, to_copy))
  197. copied = to_copy;
  198. break;
  199. case BFIN_MEM_ACCESS_ITEST:
  200. if (isram_memcpy(&tmp, paddr, to_copy))
  201. copied = to_copy;
  202. break;
  203. default:
  204. copied = 0;
  205. break;
  206. }
  207. pr_debug("ptrace: copied size %d [0x%08lx]\n", copied, tmp);
  208. if (copied == to_copy)
  209. ret = put_user(tmp, datap);
  210. break;
  211. }
  212. /* read the word at location addr in the USER area. */
  213. case PTRACE_PEEKUSR:
  214. {
  215. unsigned long tmp;
  216. ret = -EIO;
  217. tmp = 0;
  218. if ((addr & 3) || (addr > (sizeof(struct pt_regs) + 16))) {
  219. printk(KERN_WARNING "ptrace error : PEEKUSR : temporarily returning "
  220. "0 - %x sizeof(pt_regs) is %lx\n",
  221. (int)addr, sizeof(struct pt_regs));
  222. break;
  223. }
  224. if (addr == sizeof(struct pt_regs)) {
  225. /* PT_TEXT_ADDR */
  226. tmp = child->mm->start_code + TEXT_OFFSET;
  227. } else if (addr == (sizeof(struct pt_regs) + 4)) {
  228. /* PT_TEXT_END_ADDR */
  229. tmp = child->mm->end_code;
  230. } else if (addr == (sizeof(struct pt_regs) + 8)) {
  231. /* PT_DATA_ADDR */
  232. tmp = child->mm->start_data;
  233. #ifdef CONFIG_BINFMT_ELF_FDPIC
  234. } else if (addr == (sizeof(struct pt_regs) + 12)) {
  235. goto case_PTRACE_GETFDPIC_EXEC;
  236. } else if (addr == (sizeof(struct pt_regs) + 16)) {
  237. goto case_PTRACE_GETFDPIC_INTERP;
  238. #endif
  239. } else {
  240. tmp = get_reg(child, addr);
  241. }
  242. ret = put_user(tmp, datap);
  243. break;
  244. }
  245. #ifdef CONFIG_BINFMT_ELF_FDPIC
  246. case PTRACE_GETFDPIC: {
  247. unsigned long tmp = 0;
  248. switch (addr) {
  249. case_PTRACE_GETFDPIC_EXEC:
  250. case PTRACE_GETFDPIC_EXEC:
  251. tmp = child->mm->context.exec_fdpic_loadmap;
  252. break;
  253. case_PTRACE_GETFDPIC_INTERP:
  254. case PTRACE_GETFDPIC_INTERP:
  255. tmp = child->mm->context.interp_fdpic_loadmap;
  256. break;
  257. default:
  258. break;
  259. }
  260. ret = put_user(tmp, datap);
  261. break;
  262. }
  263. #endif
  264. /* when I and D space are separate, this will have to be fixed. */
  265. case PTRACE_POKEDATA:
  266. pr_debug("ptrace: PTRACE_PEEKDATA\n");
  267. /* fall through */
  268. case PTRACE_POKETEXT: /* write the word at location addr. */
  269. {
  270. int copied = 0, to_copy = sizeof(data);
  271. ret = -EIO;
  272. pr_debug("ptrace: POKETEXT at addr 0x%08lx + %i bytes %lx\n",
  273. addr, to_copy, data);
  274. if (is_user_addr_valid(child, addr, to_copy) < 0)
  275. break;
  276. pr_debug("ptrace: user address is valid\n");
  277. switch (bfin_mem_access_type(addr, to_copy)) {
  278. case BFIN_MEM_ACCESS_CORE:
  279. case BFIN_MEM_ACCESS_CORE_ONLY:
  280. copied = access_process_vm(child, addr, &data,
  281. to_copy, 0);
  282. if (copied)
  283. break;
  284. /* hrm, why didn't that work ... maybe no mapping */
  285. if (addr >= FIXED_CODE_START &&
  286. addr + to_copy <= FIXED_CODE_END) {
  287. copy_to_user_page(0, 0, 0, paddr, &data, to_copy);
  288. copied = to_copy;
  289. } else if (addr >= BOOT_ROM_START) {
  290. memcpy(paddr, &data, to_copy);
  291. copied = to_copy;
  292. }
  293. break;
  294. case BFIN_MEM_ACCESS_DMA:
  295. if (safe_dma_memcpy(paddr, &data, to_copy))
  296. copied = to_copy;
  297. break;
  298. case BFIN_MEM_ACCESS_ITEST:
  299. if (isram_memcpy(paddr, &data, to_copy))
  300. copied = to_copy;
  301. break;
  302. default:
  303. copied = 0;
  304. break;
  305. }
  306. pr_debug("ptrace: copied size %d\n", copied);
  307. if (copied == to_copy)
  308. ret = 0;
  309. break;
  310. }
  311. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  312. ret = -EIO;
  313. if ((addr & 3) || (addr > (sizeof(struct pt_regs) + 16))) {
  314. printk(KERN_WARNING "ptrace error : POKEUSR: temporarily returning 0\n");
  315. break;
  316. }
  317. if (addr >= (sizeof(struct pt_regs))) {
  318. ret = 0;
  319. break;
  320. }
  321. if (addr == PT_SYSCFG) {
  322. data &= SYSCFG_MASK;
  323. data |= get_reg(child, PT_SYSCFG);
  324. }
  325. ret = put_reg(child, addr, data);
  326. break;
  327. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  328. case PTRACE_CONT: /* restart after signal. */
  329. pr_debug("ptrace: syscall/cont\n");
  330. ret = -EIO;
  331. if (!valid_signal(data))
  332. break;
  333. if (request == PTRACE_SYSCALL)
  334. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  335. else
  336. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  337. child->exit_code = data;
  338. ptrace_disable(child);
  339. pr_debug("ptrace: before wake_up_process\n");
  340. wake_up_process(child);
  341. ret = 0;
  342. break;
  343. /*
  344. * make the child exit. Best I can do is send it a sigkill.
  345. * perhaps it should be put in the status that it wants to
  346. * exit.
  347. */
  348. case PTRACE_KILL:
  349. ret = 0;
  350. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  351. break;
  352. child->exit_code = SIGKILL;
  353. ptrace_disable(child);
  354. wake_up_process(child);
  355. break;
  356. case PTRACE_SINGLESTEP: /* set the trap flag. */
  357. pr_debug("ptrace: single step\n");
  358. ret = -EIO;
  359. if (!valid_signal(data))
  360. break;
  361. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  362. ptrace_enable(child);
  363. child->exit_code = data;
  364. wake_up_process(child);
  365. ret = 0;
  366. break;
  367. case PTRACE_GETREGS:
  368. /* Get all gp regs from the child. */
  369. ret = ptrace_getregs(child, datap);
  370. break;
  371. case PTRACE_SETREGS:
  372. printk(KERN_WARNING "ptrace: SETREGS: **** NOT IMPLEMENTED ***\n");
  373. /* Set all gp regs in the child. */
  374. ret = 0;
  375. break;
  376. default:
  377. ret = ptrace_request(child, request, addr, data);
  378. break;
  379. }
  380. return ret;
  381. }
  382. asmlinkage void syscall_trace(void)
  383. {
  384. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  385. return;
  386. if (!(current->ptrace & PT_PTRACED))
  387. return;
  388. /* the 0x80 provides a way for the tracing parent to distinguish
  389. * between a syscall stop and SIGTRAP delivery
  390. */
  391. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  392. ? 0x80 : 0));
  393. /*
  394. * this isn't the same as continuing with a signal, but it will do
  395. * for normal use. strace only continues with a signal if the
  396. * stopping signal is not SIGTRAP. -brl
  397. */
  398. if (current->exit_code) {
  399. send_sig(current->exit_code, current, 1);
  400. current->exit_code = 0;
  401. }
  402. }