ptrace.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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,
  125. unsigned long addr, unsigned long data)
  126. {
  127. int ret;
  128. void __user *datap = (void __user *) data;
  129. switch (request) {
  130. /* Read the word at location addr in the child process */
  131. case PTRACE_PEEKTEXT:
  132. case PTRACE_PEEKDATA:
  133. ret = generic_ptrace_peekdata(child, addr, data);
  134. break;
  135. case PTRACE_PEEKUSR:
  136. ret = ptrace_read_user(child, addr, datap);
  137. break;
  138. /* Write the word in data at location addr */
  139. case PTRACE_POKETEXT:
  140. case PTRACE_POKEDATA:
  141. ret = generic_ptrace_pokedata(child, addr, data);
  142. break;
  143. case PTRACE_POKEUSR:
  144. ret = ptrace_write_user(child, addr, data);
  145. break;
  146. case PTRACE_GETREGS:
  147. ret = ptrace_getregs(child, datap);
  148. break;
  149. case PTRACE_SETREGS:
  150. ret = ptrace_setregs(child, datap);
  151. break;
  152. default:
  153. ret = ptrace_request(child, request, addr, data);
  154. break;
  155. }
  156. return ret;
  157. }
  158. asmlinkage void syscall_trace(void)
  159. {
  160. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  161. return;
  162. if (!(current->ptrace & PT_PTRACED))
  163. return;
  164. /* The 0x80 provides a way for the tracing parent to
  165. * distinguish between a syscall stop and SIGTRAP delivery */
  166. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  167. ? 0x80 : 0));
  168. /*
  169. * this isn't the same as continuing with a signal, but it
  170. * will do for normal use. strace only continues with a
  171. * signal if the stopping signal is not SIGTRAP. -brl
  172. */
  173. if (current->exit_code) {
  174. pr_debug("syscall_trace: sending signal %d to PID %u\n",
  175. current->exit_code, current->pid);
  176. send_sig(current->exit_code, current, 1);
  177. current->exit_code = 0;
  178. }
  179. }
  180. /*
  181. * debug_trampoline() is an assembly stub which will store all user
  182. * registers on the stack and execute a breakpoint instruction.
  183. *
  184. * If we single-step into an exception handler which runs with
  185. * interrupts disabled the whole time so it doesn't have to check for
  186. * pending work, its return address will be modified so that it ends
  187. * up returning to debug_trampoline.
  188. *
  189. * If the exception handler decides to store the user context and
  190. * enable interrupts after all, it will restore the original return
  191. * address and status register value. Before it returns, it will
  192. * notice that TIF_BREAKPOINT is set and execute a breakpoint
  193. * instruction.
  194. */
  195. extern void debug_trampoline(void);
  196. asmlinkage struct pt_regs *do_debug(struct pt_regs *regs)
  197. {
  198. struct thread_info *ti;
  199. unsigned long trampoline_addr;
  200. u32 status;
  201. u32 ctrl;
  202. int code;
  203. status = ocd_read(DS);
  204. ti = current_thread_info();
  205. code = TRAP_BRKPT;
  206. pr_debug("do_debug: status=0x%08x PC=0x%08lx SR=0x%08lx tif=0x%08lx\n",
  207. status, regs->pc, regs->sr, ti->flags);
  208. if (!user_mode(regs)) {
  209. unsigned long die_val = DIE_BREAKPOINT;
  210. if (status & (1 << OCD_DS_SSS_BIT))
  211. die_val = DIE_SSTEP;
  212. if (notify_die(die_val, "ptrace", regs, 0, 0, SIGTRAP)
  213. == NOTIFY_STOP)
  214. return regs;
  215. if ((status & (1 << OCD_DS_SWB_BIT))
  216. && test_and_clear_ti_thread_flag(
  217. ti, TIF_BREAKPOINT)) {
  218. /*
  219. * Explicit breakpoint from trampoline or
  220. * exception/syscall/interrupt handler.
  221. *
  222. * The real saved regs are on the stack right
  223. * after the ones we saved on entry.
  224. */
  225. regs++;
  226. pr_debug(" -> TIF_BREAKPOINT done, adjusted regs:"
  227. "PC=0x%08lx SR=0x%08lx\n",
  228. regs->pc, regs->sr);
  229. BUG_ON(!user_mode(regs));
  230. if (test_thread_flag(TIF_SINGLE_STEP)) {
  231. pr_debug("Going to do single step...\n");
  232. return regs;
  233. }
  234. /*
  235. * No TIF_SINGLE_STEP means we're done
  236. * stepping over a syscall. Do the trap now.
  237. */
  238. code = TRAP_TRACE;
  239. } else if ((status & (1 << OCD_DS_SSS_BIT))
  240. && test_ti_thread_flag(ti, TIF_SINGLE_STEP)) {
  241. pr_debug("Stepped into something, "
  242. "setting TIF_BREAKPOINT...\n");
  243. set_ti_thread_flag(ti, TIF_BREAKPOINT);
  244. /*
  245. * We stepped into an exception, interrupt or
  246. * syscall handler. Some exception handlers
  247. * don't check for pending work, so we need to
  248. * set up a trampoline just in case.
  249. *
  250. * The exception entry code will undo the
  251. * trampoline stuff if it does a full context
  252. * save (which also means that it'll check for
  253. * pending work later.)
  254. */
  255. if ((regs->sr & MODE_MASK) == MODE_EXCEPTION) {
  256. trampoline_addr
  257. = (unsigned long)&debug_trampoline;
  258. pr_debug("Setting up trampoline...\n");
  259. ti->rar_saved = sysreg_read(RAR_EX);
  260. ti->rsr_saved = sysreg_read(RSR_EX);
  261. sysreg_write(RAR_EX, trampoline_addr);
  262. sysreg_write(RSR_EX, (MODE_EXCEPTION
  263. | SR_EM | SR_GM));
  264. BUG_ON(ti->rsr_saved & MODE_MASK);
  265. }
  266. /*
  267. * If we stepped into a system call, we
  268. * shouldn't do a single step after we return
  269. * since the return address is right after the
  270. * "scall" instruction we were told to step
  271. * over.
  272. */
  273. if ((regs->sr & MODE_MASK) == MODE_SUPERVISOR) {
  274. pr_debug("Supervisor; no single step\n");
  275. clear_ti_thread_flag(ti, TIF_SINGLE_STEP);
  276. }
  277. ctrl = ocd_read(DC);
  278. ctrl &= ~(1 << OCD_DC_SS_BIT);
  279. ocd_write(DC, ctrl);
  280. return regs;
  281. } else {
  282. printk(KERN_ERR "Unexpected OCD_DS value: 0x%08x\n",
  283. status);
  284. printk(KERN_ERR "Thread flags: 0x%08lx\n", ti->flags);
  285. die("Unhandled debug trap in kernel mode",
  286. regs, SIGTRAP);
  287. }
  288. } else if (status & (1 << OCD_DS_SSS_BIT)) {
  289. /* Single step in user mode */
  290. code = TRAP_TRACE;
  291. ctrl = ocd_read(DC);
  292. ctrl &= ~(1 << OCD_DC_SS_BIT);
  293. ocd_write(DC, ctrl);
  294. }
  295. pr_debug("Sending SIGTRAP: code=%d PC=0x%08lx SR=0x%08lx\n",
  296. code, regs->pc, regs->sr);
  297. clear_thread_flag(TIF_SINGLE_STEP);
  298. _exception(SIGTRAP, regs, code, instruction_pointer(regs));
  299. return regs;
  300. }