ptrace.c 12 KB

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