ptrace.c 12 KB

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