ptrace.c 10 KB

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