ptrace.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright (C) 2004-2006 Atmel Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #undef DEBUG
  9. #include <linux/kernel.h>
  10. #include <linux/sched.h>
  11. #include <linux/mm.h>
  12. #include <linux/ptrace.h>
  13. #include <linux/errno.h>
  14. #include <linux/user.h>
  15. #include <linux/security.h>
  16. #include <linux/unistd.h>
  17. #include <linux/notifier.h>
  18. #include <asm/traps.h>
  19. #include <asm/uaccess.h>
  20. #include <asm/ocd.h>
  21. #include <asm/mmu_context.h>
  22. #include <linux/kdebug.h>
  23. static struct pt_regs *get_user_regs(struct task_struct *tsk)
  24. {
  25. return (struct pt_regs *)((unsigned long)task_stack_page(tsk) +
  26. THREAD_SIZE - sizeof(struct pt_regs));
  27. }
  28. static void ptrace_single_step(struct task_struct *tsk)
  29. {
  30. pr_debug("ptrace_single_step: pid=%u, SR=0x%08lx\n",
  31. tsk->pid, tsk->thread.cpu_context.sr);
  32. if (!(tsk->thread.cpu_context.sr & SR_D)) {
  33. /*
  34. * Set a breakpoint at the current pc to force the
  35. * process into debug mode. The syscall/exception
  36. * exit code will set a breakpoint at the return
  37. * address when this flag is set.
  38. */
  39. pr_debug("ptrace_single_step: Setting TIF_BREAKPOINT\n");
  40. set_tsk_thread_flag(tsk, TIF_BREAKPOINT);
  41. }
  42. /* The monitor code will do the actual step for us */
  43. set_tsk_thread_flag(tsk, TIF_SINGLE_STEP);
  44. }
  45. /*
  46. * Called by kernel/ptrace.c when detaching
  47. *
  48. * Make sure any single step bits, etc. are not set
  49. */
  50. void ptrace_disable(struct task_struct *child)
  51. {
  52. clear_tsk_thread_flag(child, TIF_SINGLE_STEP);
  53. }
  54. /*
  55. * Handle hitting a breakpoint
  56. */
  57. static void ptrace_break(struct task_struct *tsk, struct pt_regs *regs)
  58. {
  59. siginfo_t info;
  60. info.si_signo = SIGTRAP;
  61. info.si_errno = 0;
  62. info.si_code = TRAP_BRKPT;
  63. info.si_addr = (void __user *)instruction_pointer(regs);
  64. pr_debug("ptrace_break: Sending SIGTRAP to PID %u (pc = 0x%p)\n",
  65. tsk->pid, info.si_addr);
  66. force_sig_info(SIGTRAP, &info, tsk);
  67. }
  68. /*
  69. * Read the word at offset "offset" into the task's "struct user". We
  70. * actually access the pt_regs struct stored on the kernel stack.
  71. */
  72. static int ptrace_read_user(struct task_struct *tsk, unsigned long offset,
  73. unsigned long __user *data)
  74. {
  75. unsigned long *regs;
  76. unsigned long value;
  77. pr_debug("ptrace_read_user(%p, %#lx, %p)\n",
  78. tsk, offset, data);
  79. if (offset & 3 || offset >= sizeof(struct user)) {
  80. printk("ptrace_read_user: invalid offset 0x%08lx\n", offset);
  81. return -EIO;
  82. }
  83. regs = (unsigned long *)get_user_regs(tsk);
  84. value = 0;
  85. if (offset < sizeof(struct pt_regs))
  86. value = regs[offset / sizeof(regs[0])];
  87. return put_user(value, data);
  88. }
  89. /*
  90. * Write the word "value" to offset "offset" into the task's "struct
  91. * user". We actually access the pt_regs struct stored on the kernel
  92. * stack.
  93. */
  94. static int ptrace_write_user(struct task_struct *tsk, unsigned long offset,
  95. unsigned long value)
  96. {
  97. unsigned long *regs;
  98. if (offset & 3 || offset >= sizeof(struct user)) {
  99. printk("ptrace_write_user: invalid offset 0x%08lx\n", offset);
  100. return -EIO;
  101. }
  102. if (offset >= sizeof(struct pt_regs))
  103. return 0;
  104. regs = (unsigned long *)get_user_regs(tsk);
  105. regs[offset / sizeof(regs[0])] = value;
  106. return 0;
  107. }
  108. static int ptrace_getregs(struct task_struct *tsk, void __user *uregs)
  109. {
  110. struct pt_regs *regs = get_user_regs(tsk);
  111. return copy_to_user(uregs, regs, sizeof(*regs)) ? -EFAULT : 0;
  112. }
  113. static int ptrace_setregs(struct task_struct *tsk, const void __user *uregs)
  114. {
  115. struct pt_regs newregs;
  116. int ret;
  117. ret = -EFAULT;
  118. if (copy_from_user(&newregs, uregs, sizeof(newregs)) == 0) {
  119. struct pt_regs *regs = get_user_regs(tsk);
  120. ret = -EINVAL;
  121. if (valid_user_regs(&newregs)) {
  122. *regs = newregs;
  123. ret = 0;
  124. }
  125. }
  126. return ret;
  127. }
  128. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  129. {
  130. int ret;
  131. pr_debug("arch_ptrace(%ld, %d, %#lx, %#lx)\n",
  132. request, child->pid, addr, data);
  133. pr_debug("ptrace: Enabling monitor mode...\n");
  134. ocd_write(DC, ocd_read(DC) | (1 << OCD_DC_MM_BIT)
  135. | (1 << OCD_DC_DBE_BIT));
  136. switch (request) {
  137. /* Read the word at location addr in the child process */
  138. case PTRACE_PEEKTEXT:
  139. case PTRACE_PEEKDATA:
  140. ret = generic_ptrace_peekdata(child, addr, data);
  141. break;
  142. case PTRACE_PEEKUSR:
  143. ret = ptrace_read_user(child, addr,
  144. (unsigned long __user *)data);
  145. break;
  146. /* Write the word in data at location addr */
  147. case PTRACE_POKETEXT:
  148. case PTRACE_POKEDATA:
  149. ret = generic_ptrace_pokedata(child, addr, data);
  150. break;
  151. case PTRACE_POKEUSR:
  152. ret = ptrace_write_user(child, addr, data);
  153. break;
  154. /* continue and stop at next (return from) syscall */
  155. case PTRACE_SYSCALL:
  156. /* restart after signal */
  157. case PTRACE_CONT:
  158. ret = -EIO;
  159. if (!valid_signal(data))
  160. break;
  161. if (request == PTRACE_SYSCALL)
  162. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  163. else
  164. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  165. child->exit_code = data;
  166. /* XXX: Are we sure no breakpoints are active here? */
  167. wake_up_process(child);
  168. ret = 0;
  169. break;
  170. /*
  171. * Make the child exit. Best I can do is send it a
  172. * SIGKILL. Perhaps it should be put in the status that it
  173. * wants to exit.
  174. */
  175. case PTRACE_KILL:
  176. ret = 0;
  177. if (child->exit_state == EXIT_ZOMBIE)
  178. break;
  179. child->exit_code = SIGKILL;
  180. wake_up_process(child);
  181. break;
  182. /*
  183. * execute single instruction.
  184. */
  185. case PTRACE_SINGLESTEP:
  186. ret = -EIO;
  187. if (!valid_signal(data))
  188. break;
  189. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  190. ptrace_single_step(child);
  191. child->exit_code = data;
  192. wake_up_process(child);
  193. ret = 0;
  194. break;
  195. case PTRACE_GETREGS:
  196. ret = ptrace_getregs(child, (void __user *)data);
  197. break;
  198. case PTRACE_SETREGS:
  199. ret = ptrace_setregs(child, (const void __user *)data);
  200. break;
  201. default:
  202. ret = ptrace_request(child, request, addr, data);
  203. break;
  204. }
  205. pr_debug("sys_ptrace returning %d (DC = 0x%08lx)\n",
  206. ret, ocd_read(DC));
  207. return ret;
  208. }
  209. asmlinkage void syscall_trace(void)
  210. {
  211. pr_debug("syscall_trace called\n");
  212. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  213. return;
  214. if (!(current->ptrace & PT_PTRACED))
  215. return;
  216. pr_debug("syscall_trace: notifying parent\n");
  217. /* The 0x80 provides a way for the tracing parent to
  218. * distinguish between a syscall stop and SIGTRAP delivery */
  219. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  220. ? 0x80 : 0));
  221. /*
  222. * this isn't the same as continuing with a signal, but it
  223. * will do for normal use. strace only continues with a
  224. * signal if the stopping signal is not SIGTRAP. -brl
  225. */
  226. if (current->exit_code) {
  227. pr_debug("syscall_trace: sending signal %d to PID %u\n",
  228. current->exit_code, current->pid);
  229. send_sig(current->exit_code, current, 1);
  230. current->exit_code = 0;
  231. }
  232. }
  233. asmlinkage void do_debug_priv(struct pt_regs *regs)
  234. {
  235. unsigned long dc, ds;
  236. unsigned long die_val;
  237. ds = ocd_read(DS);
  238. pr_debug("do_debug_priv: pc = %08lx, ds = %08lx\n", regs->pc, ds);
  239. if (ds & (1 << OCD_DS_SSS_BIT))
  240. die_val = DIE_SSTEP;
  241. else
  242. die_val = DIE_BREAKPOINT;
  243. if (notify_die(die_val, "ptrace", regs, 0, 0, SIGTRAP) == NOTIFY_STOP)
  244. return;
  245. if (likely(ds & (1 << OCD_DS_SSS_BIT))) {
  246. extern void itlb_miss(void);
  247. extern void tlb_miss_common(void);
  248. struct thread_info *ti;
  249. dc = ocd_read(DC);
  250. dc &= ~(1 << OCD_DC_SS_BIT);
  251. ocd_write(DC, dc);
  252. ti = current_thread_info();
  253. set_ti_thread_flag(ti, TIF_BREAKPOINT);
  254. /* The TLB miss handlers don't check thread flags */
  255. if ((regs->pc >= (unsigned long)&itlb_miss)
  256. && (regs->pc <= (unsigned long)&tlb_miss_common)) {
  257. ocd_write(BWA2A, sysreg_read(RAR_EX));
  258. ocd_write(BWC2A, 0x40000001 | (get_asid() << 1));
  259. }
  260. /*
  261. * If we're running in supervisor mode, the breakpoint
  262. * will take us where we want directly, no need to
  263. * single step.
  264. */
  265. if ((regs->sr & MODE_MASK) != MODE_SUPERVISOR)
  266. set_ti_thread_flag(ti, TIF_SINGLE_STEP);
  267. } else {
  268. panic("Unable to handle debug trap at pc = %08lx\n",
  269. regs->pc);
  270. }
  271. }
  272. /*
  273. * Handle breakpoints, single steps and other debuggy things. To keep
  274. * things simple initially, we run with interrupts and exceptions
  275. * disabled all the time.
  276. */
  277. asmlinkage void do_debug(struct pt_regs *regs)
  278. {
  279. unsigned long dc, ds;
  280. ds = ocd_read(DS);
  281. pr_debug("do_debug: pc = %08lx, ds = %08lx\n", regs->pc, ds);
  282. if (test_thread_flag(TIF_BREAKPOINT)) {
  283. pr_debug("TIF_BREAKPOINT set\n");
  284. /* We're taking care of it */
  285. clear_thread_flag(TIF_BREAKPOINT);
  286. ocd_write(BWC2A, 0);
  287. }
  288. if (test_thread_flag(TIF_SINGLE_STEP)) {
  289. pr_debug("TIF_SINGLE_STEP set, ds = 0x%08lx\n", ds);
  290. if (ds & (1 << OCD_DS_SSS_BIT)) {
  291. dc = ocd_read(DC);
  292. dc &= ~(1 << OCD_DC_SS_BIT);
  293. ocd_write(DC, dc);
  294. clear_thread_flag(TIF_SINGLE_STEP);
  295. ptrace_break(current, regs);
  296. }
  297. } else {
  298. /* regular breakpoint */
  299. ptrace_break(current, regs);
  300. }
  301. }