ptrace.c 8.9 KB

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