ptrace.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. #define MAX_SHARED_LIBS 3
  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 = get_user_regs(tsk);
  80. return copy_to_user(uregs, regs, sizeof(struct pt_regs)) ? -EFAULT : 0;
  81. }
  82. /* Mapping from PT_xxx to the stack offset at which the register is
  83. * saved. Notice that usp has no stack-slot and needs to be treated
  84. * specially (see get_reg/put_reg below).
  85. */
  86. /*
  87. * Get contents of register REGNO in task TASK.
  88. */
  89. static inline long get_reg(struct task_struct *task, int regno)
  90. {
  91. unsigned char *reg_ptr;
  92. struct pt_regs *regs =
  93. (struct pt_regs *)((unsigned long)task_stack_page(task) +
  94. (THREAD_SIZE - sizeof(struct pt_regs)));
  95. reg_ptr = (char *)regs;
  96. switch (regno) {
  97. case PT_USP:
  98. return task->thread.usp;
  99. default:
  100. if (regno <= 216)
  101. return *(long *)(reg_ptr + regno);
  102. }
  103. /* slight mystery ... never seems to come here but kernel misbehaves without this code! */
  104. printk(KERN_WARNING "Request to get for unknown register %d\n", regno);
  105. return 0;
  106. }
  107. /*
  108. * Write contents of register REGNO in task TASK.
  109. */
  110. static inline int
  111. put_reg(struct task_struct *task, int regno, unsigned long data)
  112. {
  113. char *reg_ptr;
  114. struct pt_regs *regs =
  115. (struct pt_regs *)((unsigned long)task_stack_page(task) +
  116. (THREAD_SIZE - sizeof(struct pt_regs)));
  117. reg_ptr = (char *)regs;
  118. switch (regno) {
  119. case PT_PC:
  120. /*********************************************************************/
  121. /* At this point the kernel is most likely in exception. */
  122. /* The RETX register will be used to populate the pc of the process. */
  123. /*********************************************************************/
  124. regs->retx = data;
  125. regs->pc = data;
  126. break;
  127. case PT_RETX:
  128. break; /* regs->retx = data; break; */
  129. case PT_USP:
  130. regs->usp = data;
  131. task->thread.usp = data;
  132. break;
  133. default:
  134. if (regno <= 216)
  135. *(long *)(reg_ptr + regno) = data;
  136. }
  137. return 0;
  138. }
  139. /*
  140. * check that an address falls within the bounds of the target process's memory mappings
  141. */
  142. static inline int is_user_addr_valid(struct task_struct *child,
  143. unsigned long start, unsigned long len)
  144. {
  145. struct vm_list_struct *vml;
  146. struct sram_list_struct *sraml;
  147. for (vml = child->mm->context.vmlist; vml; vml = vml->next)
  148. if (start >= vml->vma->vm_start && start + len <= vml->vma->vm_end)
  149. return 0;
  150. for (sraml = child->mm->context.sram_list; sraml; sraml = sraml->next)
  151. if (start >= (unsigned long)sraml->addr
  152. && start + len <= (unsigned long)sraml->addr + sraml->length)
  153. return 0;
  154. if (start >= FIXED_CODE_START && start + len <= FIXED_CODE_END)
  155. return 0;
  156. return -EIO;
  157. }
  158. /*
  159. * Called by kernel/ptrace.c when detaching..
  160. *
  161. * Make sure the single step bit is not set.
  162. */
  163. void ptrace_disable(struct task_struct *child)
  164. {
  165. unsigned long tmp;
  166. /* make sure the single step bit is not set. */
  167. tmp = get_reg(child, PT_SYSCFG) & ~TRACE_BITS;
  168. put_reg(child, PT_SYSCFG, tmp);
  169. }
  170. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  171. {
  172. int ret;
  173. int add = 0;
  174. unsigned long __user *datap = (unsigned long __user *)data;
  175. switch (request) {
  176. /* when I and D space are separate, these will need to be fixed. */
  177. case PTRACE_PEEKDATA:
  178. pr_debug("ptrace: PEEKDATA\n");
  179. add = MAX_SHARED_LIBS * 4; /* space between text and data */
  180. /* fall through */
  181. case PTRACE_PEEKTEXT: /* read word at location addr. */
  182. {
  183. unsigned long tmp = 0;
  184. int copied;
  185. ret = -EIO;
  186. pr_debug("ptrace: PEEKTEXT at addr 0x%08lx + add %d %ld\n", addr, add,
  187. sizeof(data));
  188. if (is_user_addr_valid(child, addr + add, sizeof(tmp)) < 0)
  189. break;
  190. pr_debug("ptrace: user address is valid\n");
  191. #if L1_CODE_LENGTH != 0
  192. if (addr + add >= L1_CODE_START
  193. && addr + add + sizeof(tmp) <= L1_CODE_START + L1_CODE_LENGTH) {
  194. safe_dma_memcpy (&tmp, (const void *)(addr + add), sizeof(tmp));
  195. copied = sizeof(tmp);
  196. } else
  197. #endif
  198. #if L1_DATA_A_LENGTH != 0
  199. if (addr + add >= L1_DATA_A_START
  200. && addr + add + sizeof(tmp) <= L1_DATA_A_START + L1_DATA_A_LENGTH) {
  201. memcpy(&tmp, (const void *)(addr + add), sizeof(tmp));
  202. copied = sizeof(tmp);
  203. } else
  204. #endif
  205. #if L1_DATA_B_LENGTH != 0
  206. if (addr + add >= L1_DATA_B_START
  207. && addr + add + sizeof(tmp) <= L1_DATA_B_START + L1_DATA_B_LENGTH) {
  208. memcpy(&tmp, (const void *)(addr + add), sizeof(tmp));
  209. copied = sizeof(tmp);
  210. } else
  211. #endif
  212. if (addr + add >= FIXED_CODE_START
  213. && addr + add + sizeof(tmp) <= FIXED_CODE_END) {
  214. memcpy(&tmp, (const void *)(addr + add), sizeof(tmp));
  215. copied = sizeof(tmp);
  216. } else
  217. copied = access_process_vm(child, addr + add, &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. printk(KERN_NOTICE "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 + add %d %ld bytes %lx\n",
  267. addr, add, sizeof(data), data);
  268. if (is_user_addr_valid(child, addr + add, sizeof(data)) < 0)
  269. break;
  270. pr_debug("ptrace: user address is valid\n");
  271. #if L1_CODE_LENGTH != 0
  272. if (addr + add >= L1_CODE_START
  273. && addr + add + sizeof(data) <= L1_CODE_START + L1_CODE_LENGTH) {
  274. safe_dma_memcpy ((void *)(addr + add), &data, sizeof(data));
  275. copied = sizeof(data);
  276. } else
  277. #endif
  278. #if L1_DATA_A_LENGTH != 0
  279. if (addr + add >= L1_DATA_A_START
  280. && addr + add + sizeof(data) <= L1_DATA_A_START + L1_DATA_A_LENGTH) {
  281. memcpy((void *)(addr + add), &data, sizeof(data));
  282. copied = sizeof(data);
  283. } else
  284. #endif
  285. #if L1_DATA_B_LENGTH != 0
  286. if (addr + add >= L1_DATA_B_START
  287. && addr + add + sizeof(data) <= L1_DATA_B_START + L1_DATA_B_LENGTH) {
  288. memcpy((void *)(addr + add), &data, sizeof(data));
  289. copied = sizeof(data);
  290. } else
  291. #endif
  292. if (addr + add >= FIXED_CODE_START
  293. && addr + add + sizeof(data) <= FIXED_CODE_END) {
  294. memcpy((void *)(addr + add), &data, sizeof(data));
  295. copied = sizeof(data);
  296. } else
  297. copied = access_process_vm(child, addr + add, &data,
  298. sizeof(data), 1);
  299. pr_debug("ptrace: copied size %d\n", copied);
  300. if (copied != sizeof(data))
  301. break;
  302. ret = 0;
  303. break;
  304. }
  305. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  306. ret = -EIO;
  307. if ((addr & 3) || (addr > (sizeof(struct pt_regs) + 16))) {
  308. printk(KERN_WARNING "ptrace error : POKEUSR: temporarily returning 0\n");
  309. break;
  310. }
  311. if (addr >= (sizeof(struct pt_regs))) {
  312. ret = 0;
  313. break;
  314. }
  315. if (addr == PT_SYSCFG) {
  316. data &= SYSCFG_MASK;
  317. data |= get_reg(child, PT_SYSCFG);
  318. }
  319. ret = put_reg(child, addr, data);
  320. break;
  321. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  322. case PTRACE_CONT:
  323. { /* restart after signal. */
  324. long tmp;
  325. pr_debug("ptrace_cont\n");
  326. ret = -EIO;
  327. if (!valid_signal(data))
  328. break;
  329. if (request == PTRACE_SYSCALL)
  330. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  331. else
  332. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  333. child->exit_code = data;
  334. /* make sure the single step bit is not set. */
  335. tmp = get_reg(child, PT_SYSCFG) & ~(TRACE_BITS);
  336. put_reg(child, PT_SYSCFG, tmp);
  337. pr_debug("before wake_up_process\n");
  338. wake_up_process(child);
  339. ret = 0;
  340. break;
  341. }
  342. /*
  343. * make the child exit. Best I can do is send it a sigkill.
  344. * perhaps it should be put in the status that it wants to
  345. * exit.
  346. */
  347. case PTRACE_KILL:
  348. {
  349. long tmp;
  350. ret = 0;
  351. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  352. break;
  353. child->exit_code = SIGKILL;
  354. /* make sure the single step bit is not set. */
  355. tmp = get_reg(child, PT_SYSCFG) & ~(TRACE_BITS);
  356. put_reg(child, PT_SYSCFG, tmp);
  357. wake_up_process(child);
  358. break;
  359. }
  360. case PTRACE_SINGLESTEP:
  361. { /* set the trap flag. */
  362. long tmp;
  363. pr_debug("single step\n");
  364. ret = -EIO;
  365. if (!valid_signal(data))
  366. break;
  367. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  368. tmp = get_reg(child, PT_SYSCFG) | (TRACE_BITS);
  369. put_reg(child, PT_SYSCFG, tmp);
  370. child->exit_code = data;
  371. /* give it a chance to run. */
  372. wake_up_process(child);
  373. ret = 0;
  374. break;
  375. }
  376. case PTRACE_GETREGS:
  377. {
  378. /* Get all gp regs from the child. */
  379. ret = ptrace_getregs(child, datap);
  380. break;
  381. }
  382. case PTRACE_SETREGS:
  383. {
  384. printk(KERN_NOTICE
  385. "ptrace: SETREGS: **** NOT IMPLEMENTED ***\n");
  386. /* Set all gp regs in the child. */
  387. ret = 0;
  388. break;
  389. }
  390. default:
  391. ret = ptrace_request(child, request, addr, data);
  392. break;
  393. }
  394. return ret;
  395. }
  396. asmlinkage void syscall_trace(void)
  397. {
  398. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  399. return;
  400. if (!(current->ptrace & PT_PTRACED))
  401. return;
  402. /* the 0x80 provides a way for the tracing parent to distinguish
  403. * between a syscall stop and SIGTRAP delivery
  404. */
  405. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  406. ? 0x80 : 0));
  407. /*
  408. * this isn't the same as continuing with a signal, but it will do
  409. * for normal use. strace only continues with a signal if the
  410. * stopping signal is not SIGTRAP. -brl
  411. */
  412. if (current->exit_code) {
  413. send_sig(current->exit_code, current, 1);
  414. current->exit_code = 0;
  415. }
  416. }