ptrace.c 10 KB

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