ptrace.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* ptrace.c */
  2. /* By Ross Biro 1/23/92 */
  3. /* edited by Linus Torvalds */
  4. /* mangled further by Bob Manson (manson@santafe.edu) */
  5. /* more mutilation by David Mosberger (davidm@azstarnet.com) */
  6. #include <linux/kernel.h>
  7. #include <linux/sched.h>
  8. #include <linux/mm.h>
  9. #include <linux/smp.h>
  10. #include <linux/errno.h>
  11. #include <linux/ptrace.h>
  12. #include <linux/user.h>
  13. #include <linux/security.h>
  14. #include <linux/signal.h>
  15. #include <asm/uaccess.h>
  16. #include <asm/pgtable.h>
  17. #include <asm/fpu.h>
  18. #include "proto.h"
  19. #define DEBUG DBG_MEM
  20. #undef DEBUG
  21. #ifdef DEBUG
  22. enum {
  23. DBG_MEM = (1<<0),
  24. DBG_BPT = (1<<1),
  25. DBG_MEM_ALL = (1<<2)
  26. };
  27. #define DBG(fac,args) {if ((fac) & DEBUG) printk args;}
  28. #else
  29. #define DBG(fac,args)
  30. #endif
  31. #define BREAKINST 0x00000080 /* call_pal bpt */
  32. /*
  33. * does not yet catch signals sent when the child dies.
  34. * in exit.c or in signal.c.
  35. */
  36. /*
  37. * Processes always block with the following stack-layout:
  38. *
  39. * +================================+ <---- task + 2*PAGE_SIZE
  40. * | PALcode saved frame (ps, pc, | ^
  41. * | gp, a0, a1, a2) | |
  42. * +================================+ | struct pt_regs
  43. * | | |
  44. * | frame generated by SAVE_ALL | |
  45. * | | v
  46. * +================================+
  47. * | | ^
  48. * | frame saved by do_switch_stack | | struct switch_stack
  49. * | | v
  50. * +================================+
  51. */
  52. /*
  53. * The following table maps a register index into the stack offset at
  54. * which the register is saved. Register indices are 0-31 for integer
  55. * regs, 32-63 for fp regs, and 64 for the pc. Notice that sp and
  56. * zero have no stack-slot and need to be treated specially (see
  57. * get_reg/put_reg below).
  58. */
  59. enum {
  60. REG_R0 = 0, REG_F0 = 32, REG_FPCR = 63, REG_PC = 64
  61. };
  62. #define PT_REG(reg) \
  63. (PAGE_SIZE*2 - sizeof(struct pt_regs) + offsetof(struct pt_regs, reg))
  64. #define SW_REG(reg) \
  65. (PAGE_SIZE*2 - sizeof(struct pt_regs) - sizeof(struct switch_stack) \
  66. + offsetof(struct switch_stack, reg))
  67. static int regoff[] = {
  68. PT_REG( r0), PT_REG( r1), PT_REG( r2), PT_REG( r3),
  69. PT_REG( r4), PT_REG( r5), PT_REG( r6), PT_REG( r7),
  70. PT_REG( r8), SW_REG( r9), SW_REG( r10), SW_REG( r11),
  71. SW_REG( r12), SW_REG( r13), SW_REG( r14), SW_REG( r15),
  72. PT_REG( r16), PT_REG( r17), PT_REG( r18), PT_REG( r19),
  73. PT_REG( r20), PT_REG( r21), PT_REG( r22), PT_REG( r23),
  74. PT_REG( r24), PT_REG( r25), PT_REG( r26), PT_REG( r27),
  75. PT_REG( r28), PT_REG( gp), -1, -1,
  76. SW_REG(fp[ 0]), SW_REG(fp[ 1]), SW_REG(fp[ 2]), SW_REG(fp[ 3]),
  77. SW_REG(fp[ 4]), SW_REG(fp[ 5]), SW_REG(fp[ 6]), SW_REG(fp[ 7]),
  78. SW_REG(fp[ 8]), SW_REG(fp[ 9]), SW_REG(fp[10]), SW_REG(fp[11]),
  79. SW_REG(fp[12]), SW_REG(fp[13]), SW_REG(fp[14]), SW_REG(fp[15]),
  80. SW_REG(fp[16]), SW_REG(fp[17]), SW_REG(fp[18]), SW_REG(fp[19]),
  81. SW_REG(fp[20]), SW_REG(fp[21]), SW_REG(fp[22]), SW_REG(fp[23]),
  82. SW_REG(fp[24]), SW_REG(fp[25]), SW_REG(fp[26]), SW_REG(fp[27]),
  83. SW_REG(fp[28]), SW_REG(fp[29]), SW_REG(fp[30]), SW_REG(fp[31]),
  84. PT_REG( pc)
  85. };
  86. static unsigned long zero;
  87. /*
  88. * Get address of register REGNO in task TASK.
  89. */
  90. static unsigned long *
  91. get_reg_addr(struct task_struct * task, unsigned long regno)
  92. {
  93. unsigned long *addr;
  94. if (regno == 30) {
  95. addr = &task_thread_info(task)->pcb.usp;
  96. } else if (regno == 65) {
  97. addr = &task_thread_info(task)->pcb.unique;
  98. } else if (regno == 31 || regno > 65) {
  99. zero = 0;
  100. addr = &zero;
  101. } else {
  102. addr = task_stack_page(task) + regoff[regno];
  103. }
  104. return addr;
  105. }
  106. /*
  107. * Get contents of register REGNO in task TASK.
  108. */
  109. static unsigned long
  110. get_reg(struct task_struct * task, unsigned long regno)
  111. {
  112. /* Special hack for fpcr -- combine hardware and software bits. */
  113. if (regno == 63) {
  114. unsigned long fpcr = *get_reg_addr(task, regno);
  115. unsigned long swcr
  116. = task_thread_info(task)->ieee_state & IEEE_SW_MASK;
  117. swcr = swcr_update_status(swcr, fpcr);
  118. return fpcr | swcr;
  119. }
  120. return *get_reg_addr(task, regno);
  121. }
  122. /*
  123. * Write contents of register REGNO in task TASK.
  124. */
  125. static int
  126. put_reg(struct task_struct *task, unsigned long regno, unsigned long data)
  127. {
  128. if (regno == 63) {
  129. task_thread_info(task)->ieee_state
  130. = ((task_thread_info(task)->ieee_state & ~IEEE_SW_MASK)
  131. | (data & IEEE_SW_MASK));
  132. data = (data & FPCR_DYN_MASK) | ieee_swcr_to_fpcr(data);
  133. }
  134. *get_reg_addr(task, regno) = data;
  135. return 0;
  136. }
  137. static inline int
  138. read_int(struct task_struct *task, unsigned long addr, int * data)
  139. {
  140. int copied = access_process_vm(task, addr, data, sizeof(int), 0);
  141. return (copied == sizeof(int)) ? 0 : -EIO;
  142. }
  143. static inline int
  144. write_int(struct task_struct *task, unsigned long addr, int data)
  145. {
  146. int copied = access_process_vm(task, addr, &data, sizeof(int), 1);
  147. return (copied == sizeof(int)) ? 0 : -EIO;
  148. }
  149. /*
  150. * Set breakpoint.
  151. */
  152. int
  153. ptrace_set_bpt(struct task_struct * child)
  154. {
  155. int displ, i, res, reg_b, nsaved = 0;
  156. unsigned int insn, op_code;
  157. unsigned long pc;
  158. pc = get_reg(child, REG_PC);
  159. res = read_int(child, pc, (int *) &insn);
  160. if (res < 0)
  161. return res;
  162. op_code = insn >> 26;
  163. if (op_code >= 0x30) {
  164. /*
  165. * It's a branch: instead of trying to figure out
  166. * whether the branch will be taken or not, we'll put
  167. * a breakpoint at either location. This is simpler,
  168. * more reliable, and probably not a whole lot slower
  169. * than the alternative approach of emulating the
  170. * branch (emulation can be tricky for fp branches).
  171. */
  172. displ = ((s32)(insn << 11)) >> 9;
  173. task_thread_info(child)->bpt_addr[nsaved++] = pc + 4;
  174. if (displ) /* guard against unoptimized code */
  175. task_thread_info(child)->bpt_addr[nsaved++]
  176. = pc + 4 + displ;
  177. DBG(DBG_BPT, ("execing branch\n"));
  178. } else if (op_code == 0x1a) {
  179. reg_b = (insn >> 16) & 0x1f;
  180. task_thread_info(child)->bpt_addr[nsaved++] = get_reg(child, reg_b);
  181. DBG(DBG_BPT, ("execing jump\n"));
  182. } else {
  183. task_thread_info(child)->bpt_addr[nsaved++] = pc + 4;
  184. DBG(DBG_BPT, ("execing normal insn\n"));
  185. }
  186. /* install breakpoints: */
  187. for (i = 0; i < nsaved; ++i) {
  188. res = read_int(child, task_thread_info(child)->bpt_addr[i],
  189. (int *) &insn);
  190. if (res < 0)
  191. return res;
  192. task_thread_info(child)->bpt_insn[i] = insn;
  193. DBG(DBG_BPT, (" -> next_pc=%lx\n",
  194. task_thread_info(child)->bpt_addr[i]));
  195. res = write_int(child, task_thread_info(child)->bpt_addr[i],
  196. BREAKINST);
  197. if (res < 0)
  198. return res;
  199. }
  200. task_thread_info(child)->bpt_nsaved = nsaved;
  201. return 0;
  202. }
  203. /*
  204. * Ensure no single-step breakpoint is pending. Returns non-zero
  205. * value if child was being single-stepped.
  206. */
  207. int
  208. ptrace_cancel_bpt(struct task_struct * child)
  209. {
  210. int i, nsaved = task_thread_info(child)->bpt_nsaved;
  211. task_thread_info(child)->bpt_nsaved = 0;
  212. if (nsaved > 2) {
  213. printk("ptrace_cancel_bpt: bogus nsaved: %d!\n", nsaved);
  214. nsaved = 2;
  215. }
  216. for (i = 0; i < nsaved; ++i) {
  217. write_int(child, task_thread_info(child)->bpt_addr[i],
  218. task_thread_info(child)->bpt_insn[i]);
  219. }
  220. return (nsaved != 0);
  221. }
  222. void user_enable_single_step(struct task_struct *child)
  223. {
  224. /* Mark single stepping. */
  225. task_thread_info(child)->bpt_nsaved = -1;
  226. }
  227. void user_disable_single_step(struct task_struct *child)
  228. {
  229. ptrace_cancel_bpt(child);
  230. }
  231. /*
  232. * Called by kernel/ptrace.c when detaching..
  233. *
  234. * Make sure the single step bit is not set.
  235. */
  236. void ptrace_disable(struct task_struct *child)
  237. {
  238. user_disable_single_step(child);
  239. }
  240. long arch_ptrace(struct task_struct *child, long request,
  241. unsigned long addr, unsigned long data)
  242. {
  243. unsigned long tmp;
  244. size_t copied;
  245. long ret;
  246. switch (request) {
  247. /* When I and D space are separate, these will need to be fixed. */
  248. case PTRACE_PEEKTEXT: /* read word at location addr. */
  249. case PTRACE_PEEKDATA:
  250. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  251. ret = -EIO;
  252. if (copied != sizeof(tmp))
  253. break;
  254. force_successful_syscall_return();
  255. ret = tmp;
  256. break;
  257. /* Read register number ADDR. */
  258. case PTRACE_PEEKUSR:
  259. force_successful_syscall_return();
  260. ret = get_reg(child, addr);
  261. DBG(DBG_MEM, ("peek $%lu->%#lx\n", addr, ret));
  262. break;
  263. /* When I and D space are separate, this will have to be fixed. */
  264. case PTRACE_POKETEXT: /* write the word at location addr. */
  265. case PTRACE_POKEDATA:
  266. ret = generic_ptrace_pokedata(child, addr, data);
  267. break;
  268. case PTRACE_POKEUSR: /* write the specified register */
  269. DBG(DBG_MEM, ("poke $%lu<-%#lx\n", addr, data));
  270. ret = put_reg(child, addr, data);
  271. break;
  272. default:
  273. ret = ptrace_request(child, request, addr, data);
  274. break;
  275. }
  276. return ret;
  277. }
  278. asmlinkage void
  279. syscall_trace(void)
  280. {
  281. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  282. return;
  283. if (!(current->ptrace & PT_PTRACED))
  284. return;
  285. /* The 0x80 provides a way for the tracing parent to distinguish
  286. between a syscall stop and SIGTRAP delivery */
  287. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  288. ? 0x80 : 0));
  289. /*
  290. * This isn't the same as continuing with a signal, but it will do
  291. * for normal use. strace only continues with a signal if the
  292. * stopping signal is not SIGTRAP. -brl
  293. */
  294. if (current->exit_code) {
  295. send_sig(current->exit_code, current, 1);
  296. current->exit_code = 0;
  297. }
  298. }