ptrace.c 9.0 KB

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