ptrace.c 12 KB

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