ptrace.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. void user_enable_single_step(struct task_struct *tsk)
  29. {
  30. pr_debug("user_enable_single_step: pid=%u, PC=0x%08lx, SR=0x%08lx\n",
  31. tsk->pid, task_pt_regs(tsk)->pc, task_pt_regs(tsk)->sr);
  32. /*
  33. * We can't schedule in Debug mode, so when TIF_BREAKPOINT is
  34. * set, the system call or exception handler will do a
  35. * breakpoint to enter monitor mode before returning to
  36. * userspace.
  37. *
  38. * The monitor code will then notice that TIF_SINGLE_STEP is
  39. * set and return to userspace with single stepping enabled.
  40. * The CPU will then enter monitor mode again after exactly
  41. * one instruction has been executed, and the monitor code
  42. * will then send a SIGTRAP to the process.
  43. */
  44. set_tsk_thread_flag(tsk, TIF_BREAKPOINT);
  45. set_tsk_thread_flag(tsk, TIF_SINGLE_STEP);
  46. }
  47. void user_disable_single_step(struct task_struct *child)
  48. {
  49. /* XXX(hch): a no-op here seems wrong.. */
  50. }
  51. /*
  52. * Called by kernel/ptrace.c when detaching
  53. *
  54. * Make sure any single step bits, etc. are not set
  55. */
  56. void ptrace_disable(struct task_struct *child)
  57. {
  58. clear_tsk_thread_flag(child, TIF_SINGLE_STEP);
  59. clear_tsk_thread_flag(child, TIF_BREAKPOINT);
  60. ocd_disable(child);
  61. }
  62. /*
  63. * Read the word at offset "offset" into the task's "struct user". We
  64. * actually access the pt_regs struct stored on the kernel stack.
  65. */
  66. static int ptrace_read_user(struct task_struct *tsk, unsigned long offset,
  67. unsigned long __user *data)
  68. {
  69. unsigned long *regs;
  70. unsigned long value;
  71. if (offset & 3 || offset >= sizeof(struct user)) {
  72. printk("ptrace_read_user: invalid offset 0x%08lx\n", offset);
  73. return -EIO;
  74. }
  75. regs = (unsigned long *)get_user_regs(tsk);
  76. value = 0;
  77. if (offset < sizeof(struct pt_regs))
  78. value = regs[offset / sizeof(regs[0])];
  79. pr_debug("ptrace_read_user(%s[%u], %#lx, %p) -> %#lx\n",
  80. tsk->comm, tsk->pid, offset, data, value);
  81. return put_user(value, data);
  82. }
  83. /*
  84. * Write the word "value" to offset "offset" into the task's "struct
  85. * user". We actually access the pt_regs struct stored on the kernel
  86. * stack.
  87. */
  88. static int ptrace_write_user(struct task_struct *tsk, unsigned long offset,
  89. unsigned long value)
  90. {
  91. unsigned long *regs;
  92. pr_debug("ptrace_write_user(%s[%u], %#lx, %#lx)\n",
  93. tsk->comm, tsk->pid, offset, value);
  94. if (offset & 3 || offset >= sizeof(struct user)) {
  95. pr_debug(" invalid offset 0x%08lx\n", offset);
  96. return -EIO;
  97. }
  98. if (offset >= sizeof(struct pt_regs))
  99. return 0;
  100. regs = (unsigned long *)get_user_regs(tsk);
  101. regs[offset / sizeof(regs[0])] = value;
  102. return 0;
  103. }
  104. static int ptrace_getregs(struct task_struct *tsk, void __user *uregs)
  105. {
  106. struct pt_regs *regs = get_user_regs(tsk);
  107. return copy_to_user(uregs, regs, sizeof(*regs)) ? -EFAULT : 0;
  108. }
  109. static int ptrace_setregs(struct task_struct *tsk, const void __user *uregs)
  110. {
  111. struct pt_regs newregs;
  112. int ret;
  113. ret = -EFAULT;
  114. if (copy_from_user(&newregs, uregs, sizeof(newregs)) == 0) {
  115. struct pt_regs *regs = get_user_regs(tsk);
  116. ret = -EINVAL;
  117. if (valid_user_regs(&newregs)) {
  118. *regs = newregs;
  119. ret = 0;
  120. }
  121. }
  122. return ret;
  123. }
  124. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  125. {
  126. int ret;
  127. switch (request) {
  128. /* Read the word at location addr in the child process */
  129. case PTRACE_PEEKTEXT:
  130. case PTRACE_PEEKDATA:
  131. ret = generic_ptrace_peekdata(child, addr, data);
  132. break;
  133. case PTRACE_PEEKUSR:
  134. ret = ptrace_read_user(child, addr,
  135. (unsigned long __user *)data);
  136. break;
  137. /* Write the word in data at location addr */
  138. case PTRACE_POKETEXT:
  139. case PTRACE_POKEDATA:
  140. ret = generic_ptrace_pokedata(child, addr, data);
  141. break;
  142. case PTRACE_POKEUSR:
  143. ret = ptrace_write_user(child, addr, data);
  144. break;
  145. case PTRACE_GETREGS:
  146. ret = ptrace_getregs(child, (void __user *)data);
  147. break;
  148. case PTRACE_SETREGS:
  149. ret = ptrace_setregs(child, (const void __user *)data);
  150. break;
  151. default:
  152. ret = ptrace_request(child, request, addr, data);
  153. break;
  154. }
  155. return ret;
  156. }
  157. asmlinkage void syscall_trace(void)
  158. {
  159. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  160. return;
  161. if (!(current->ptrace & PT_PTRACED))
  162. return;
  163. /* The 0x80 provides a way for the tracing parent to
  164. * distinguish between a syscall stop and SIGTRAP delivery */
  165. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  166. ? 0x80 : 0));
  167. /*
  168. * this isn't the same as continuing with a signal, but it
  169. * will do for normal use. strace only continues with a
  170. * signal if the stopping signal is not SIGTRAP. -brl
  171. */
  172. if (current->exit_code) {
  173. pr_debug("syscall_trace: sending signal %d to PID %u\n",
  174. current->exit_code, current->pid);
  175. send_sig(current->exit_code, current, 1);
  176. current->exit_code = 0;
  177. }
  178. }
  179. /*
  180. * debug_trampoline() is an assembly stub which will store all user
  181. * registers on the stack and execute a breakpoint instruction.
  182. *
  183. * If we single-step into an exception handler which runs with
  184. * interrupts disabled the whole time so it doesn't have to check for
  185. * pending work, its return address will be modified so that it ends
  186. * up returning to debug_trampoline.
  187. *
  188. * If the exception handler decides to store the user context and
  189. * enable interrupts after all, it will restore the original return
  190. * address and status register value. Before it returns, it will
  191. * notice that TIF_BREAKPOINT is set and execute a breakpoint
  192. * instruction.
  193. */
  194. extern void debug_trampoline(void);
  195. asmlinkage struct pt_regs *do_debug(struct pt_regs *regs)
  196. {
  197. struct thread_info *ti;
  198. unsigned long trampoline_addr;
  199. u32 status;
  200. u32 ctrl;
  201. int code;
  202. status = ocd_read(DS);
  203. ti = current_thread_info();
  204. code = TRAP_BRKPT;
  205. pr_debug("do_debug: status=0x%08x PC=0x%08lx SR=0x%08lx tif=0x%08lx\n",
  206. status, regs->pc, regs->sr, ti->flags);
  207. if (!user_mode(regs)) {
  208. unsigned long die_val = DIE_BREAKPOINT;
  209. if (status & (1 << OCD_DS_SSS_BIT))
  210. die_val = DIE_SSTEP;
  211. if (notify_die(die_val, "ptrace", regs, 0, 0, SIGTRAP)
  212. == NOTIFY_STOP)
  213. return regs;
  214. if ((status & (1 << OCD_DS_SWB_BIT))
  215. && test_and_clear_ti_thread_flag(
  216. ti, TIF_BREAKPOINT)) {
  217. /*
  218. * Explicit breakpoint from trampoline or
  219. * exception/syscall/interrupt handler.
  220. *
  221. * The real saved regs are on the stack right
  222. * after the ones we saved on entry.
  223. */
  224. regs++;
  225. pr_debug(" -> TIF_BREAKPOINT done, adjusted regs:"
  226. "PC=0x%08lx SR=0x%08lx\n",
  227. regs->pc, regs->sr);
  228. BUG_ON(!user_mode(regs));
  229. if (test_thread_flag(TIF_SINGLE_STEP)) {
  230. pr_debug("Going to do single step...\n");
  231. return regs;
  232. }
  233. /*
  234. * No TIF_SINGLE_STEP means we're done
  235. * stepping over a syscall. Do the trap now.
  236. */
  237. code = TRAP_TRACE;
  238. } else if ((status & (1 << OCD_DS_SSS_BIT))
  239. && test_ti_thread_flag(ti, TIF_SINGLE_STEP)) {
  240. pr_debug("Stepped into something, "
  241. "setting TIF_BREAKPOINT...\n");
  242. set_ti_thread_flag(ti, TIF_BREAKPOINT);
  243. /*
  244. * We stepped into an exception, interrupt or
  245. * syscall handler. Some exception handlers
  246. * don't check for pending work, so we need to
  247. * set up a trampoline just in case.
  248. *
  249. * The exception entry code will undo the
  250. * trampoline stuff if it does a full context
  251. * save (which also means that it'll check for
  252. * pending work later.)
  253. */
  254. if ((regs->sr & MODE_MASK) == MODE_EXCEPTION) {
  255. trampoline_addr
  256. = (unsigned long)&debug_trampoline;
  257. pr_debug("Setting up trampoline...\n");
  258. ti->rar_saved = sysreg_read(RAR_EX);
  259. ti->rsr_saved = sysreg_read(RSR_EX);
  260. sysreg_write(RAR_EX, trampoline_addr);
  261. sysreg_write(RSR_EX, (MODE_EXCEPTION
  262. | SR_EM | SR_GM));
  263. BUG_ON(ti->rsr_saved & MODE_MASK);
  264. }
  265. /*
  266. * If we stepped into a system call, we
  267. * shouldn't do a single step after we return
  268. * since the return address is right after the
  269. * "scall" instruction we were told to step
  270. * over.
  271. */
  272. if ((regs->sr & MODE_MASK) == MODE_SUPERVISOR) {
  273. pr_debug("Supervisor; no single step\n");
  274. clear_ti_thread_flag(ti, TIF_SINGLE_STEP);
  275. }
  276. ctrl = ocd_read(DC);
  277. ctrl &= ~(1 << OCD_DC_SS_BIT);
  278. ocd_write(DC, ctrl);
  279. return regs;
  280. } else {
  281. printk(KERN_ERR "Unexpected OCD_DS value: 0x%08x\n",
  282. status);
  283. printk(KERN_ERR "Thread flags: 0x%08lx\n", ti->flags);
  284. die("Unhandled debug trap in kernel mode",
  285. regs, SIGTRAP);
  286. }
  287. } else if (status & (1 << OCD_DS_SSS_BIT)) {
  288. /* Single step in user mode */
  289. code = TRAP_TRACE;
  290. ctrl = ocd_read(DC);
  291. ctrl &= ~(1 << OCD_DC_SS_BIT);
  292. ocd_write(DC, ctrl);
  293. }
  294. pr_debug("Sending SIGTRAP: code=%d PC=0x%08lx SR=0x%08lx\n",
  295. code, regs->pc, regs->sr);
  296. clear_thread_flag(TIF_SINGLE_STEP);
  297. _exception(SIGTRAP, regs, code, instruction_pointer(regs));
  298. return regs;
  299. }