ptrace.c 8.9 KB

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