ptrace.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. __mtdr(DBGREG_DC, __mfdr(DBGREG_DC) | DC_MM | DC_DBE);
  135. switch (request) {
  136. /* Read the word at location addr in the child process */
  137. case PTRACE_PEEKTEXT:
  138. case PTRACE_PEEKDATA:
  139. ret = generic_ptrace_peekdata(child, addr, data);
  140. break;
  141. case PTRACE_PEEKUSR:
  142. ret = ptrace_read_user(child, addr,
  143. (unsigned long __user *)data);
  144. break;
  145. /* Write the word in data at location addr */
  146. case PTRACE_POKETEXT:
  147. case PTRACE_POKEDATA:
  148. ret = generic_ptrace_pokedata(child, addr, data);
  149. break;
  150. case PTRACE_POKEUSR:
  151. ret = ptrace_write_user(child, addr, data);
  152. break;
  153. /* continue and stop at next (return from) syscall */
  154. case PTRACE_SYSCALL:
  155. /* restart after signal */
  156. case PTRACE_CONT:
  157. ret = -EIO;
  158. if (!valid_signal(data))
  159. break;
  160. if (request == PTRACE_SYSCALL)
  161. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  162. else
  163. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  164. child->exit_code = data;
  165. /* XXX: Are we sure no breakpoints are active here? */
  166. wake_up_process(child);
  167. ret = 0;
  168. break;
  169. /*
  170. * Make the child exit. Best I can do is send it a
  171. * SIGKILL. Perhaps it should be put in the status that it
  172. * wants to exit.
  173. */
  174. case PTRACE_KILL:
  175. ret = 0;
  176. if (child->exit_state == EXIT_ZOMBIE)
  177. break;
  178. child->exit_code = SIGKILL;
  179. wake_up_process(child);
  180. break;
  181. /*
  182. * execute single instruction.
  183. */
  184. case PTRACE_SINGLESTEP:
  185. ret = -EIO;
  186. if (!valid_signal(data))
  187. break;
  188. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  189. ptrace_single_step(child);
  190. child->exit_code = data;
  191. wake_up_process(child);
  192. ret = 0;
  193. break;
  194. /* Detach a process that was attached */
  195. case PTRACE_DETACH:
  196. ret = ptrace_detach(child, data);
  197. break;
  198. case PTRACE_GETREGS:
  199. ret = ptrace_getregs(child, (void __user *)data);
  200. break;
  201. case PTRACE_SETREGS:
  202. ret = ptrace_setregs(child, (const void __user *)data);
  203. break;
  204. default:
  205. ret = ptrace_request(child, request, addr, data);
  206. break;
  207. }
  208. pr_debug("sys_ptrace returning %d (DC = 0x%08lx)\n", ret, __mfdr(DBGREG_DC));
  209. return ret;
  210. }
  211. asmlinkage void syscall_trace(void)
  212. {
  213. pr_debug("syscall_trace called\n");
  214. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  215. return;
  216. if (!(current->ptrace & PT_PTRACED))
  217. return;
  218. pr_debug("syscall_trace: notifying parent\n");
  219. /* The 0x80 provides a way for the tracing parent to
  220. * distinguish between a syscall stop and SIGTRAP delivery */
  221. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  222. ? 0x80 : 0));
  223. /*
  224. * this isn't the same as continuing with a signal, but it
  225. * will do for normal use. strace only continues with a
  226. * signal if the stopping signal is not SIGTRAP. -brl
  227. */
  228. if (current->exit_code) {
  229. pr_debug("syscall_trace: sending signal %d to PID %u\n",
  230. current->exit_code, current->pid);
  231. send_sig(current->exit_code, current, 1);
  232. current->exit_code = 0;
  233. }
  234. }
  235. asmlinkage void do_debug_priv(struct pt_regs *regs)
  236. {
  237. unsigned long dc, ds;
  238. unsigned long die_val;
  239. ds = __mfdr(DBGREG_DS);
  240. pr_debug("do_debug_priv: pc = %08lx, ds = %08lx\n", regs->pc, ds);
  241. if (ds & DS_SSS)
  242. die_val = DIE_SSTEP;
  243. else
  244. die_val = DIE_BREAKPOINT;
  245. if (notify_die(die_val, "ptrace", regs, 0, 0, SIGTRAP) == NOTIFY_STOP)
  246. return;
  247. if (likely(ds & DS_SSS)) {
  248. extern void itlb_miss(void);
  249. extern void tlb_miss_common(void);
  250. struct thread_info *ti;
  251. dc = __mfdr(DBGREG_DC);
  252. dc &= ~DC_SS;
  253. __mtdr(DBGREG_DC, dc);
  254. ti = current_thread_info();
  255. set_ti_thread_flag(ti, TIF_BREAKPOINT);
  256. /* The TLB miss handlers don't check thread flags */
  257. if ((regs->pc >= (unsigned long)&itlb_miss)
  258. && (regs->pc <= (unsigned long)&tlb_miss_common)) {
  259. __mtdr(DBGREG_BWA2A, sysreg_read(RAR_EX));
  260. __mtdr(DBGREG_BWC2A, 0x40000001 | (get_asid() << 1));
  261. }
  262. /*
  263. * If we're running in supervisor mode, the breakpoint
  264. * will take us where we want directly, no need to
  265. * single step.
  266. */
  267. if ((regs->sr & MODE_MASK) != MODE_SUPERVISOR)
  268. set_ti_thread_flag(ti, TIF_SINGLE_STEP);
  269. } else {
  270. panic("Unable to handle debug trap at pc = %08lx\n",
  271. regs->pc);
  272. }
  273. }
  274. /*
  275. * Handle breakpoints, single steps and other debuggy things. To keep
  276. * things simple initially, we run with interrupts and exceptions
  277. * disabled all the time.
  278. */
  279. asmlinkage void do_debug(struct pt_regs *regs)
  280. {
  281. unsigned long dc, ds;
  282. ds = __mfdr(DBGREG_DS);
  283. pr_debug("do_debug: pc = %08lx, ds = %08lx\n", regs->pc, ds);
  284. if (test_thread_flag(TIF_BREAKPOINT)) {
  285. pr_debug("TIF_BREAKPOINT set\n");
  286. /* We're taking care of it */
  287. clear_thread_flag(TIF_BREAKPOINT);
  288. __mtdr(DBGREG_BWC2A, 0);
  289. }
  290. if (test_thread_flag(TIF_SINGLE_STEP)) {
  291. pr_debug("TIF_SINGLE_STEP set, ds = 0x%08lx\n", ds);
  292. if (ds & DS_SSS) {
  293. dc = __mfdr(DBGREG_DC);
  294. dc &= ~DC_SS;
  295. __mtdr(DBGREG_DC, dc);
  296. clear_thread_flag(TIF_SINGLE_STEP);
  297. ptrace_break(current, regs);
  298. }
  299. } else {
  300. /* regular breakpoint */
  301. ptrace_break(current, regs);
  302. }
  303. }