ptrace.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  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. #include <linux/ptrace.h>
  9. #include <linux/tracehook.h>
  10. #include <linux/regset.h>
  11. #include <linux/unistd.h>
  12. #include <linux/elf.h>
  13. static struct callee_regs *task_callee_regs(struct task_struct *tsk)
  14. {
  15. struct callee_regs *tmp = (struct callee_regs *)tsk->thread.callee_reg;
  16. return tmp;
  17. }
  18. static int genregs_get(struct task_struct *target,
  19. const struct user_regset *regset,
  20. unsigned int pos, unsigned int count,
  21. void *kbuf, void __user *ubuf)
  22. {
  23. const struct pt_regs *ptregs = task_pt_regs(target);
  24. const struct callee_regs *cregs = task_callee_regs(target);
  25. int ret = 0;
  26. unsigned int stop_pc_val;
  27. #define REG_O_CHUNK(START, END, PTR) \
  28. if (!ret) \
  29. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, PTR, \
  30. offsetof(struct user_regs_struct, START), \
  31. offsetof(struct user_regs_struct, END));
  32. #define REG_O_ONE(LOC, PTR) \
  33. if (!ret) \
  34. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, PTR, \
  35. offsetof(struct user_regs_struct, LOC), \
  36. offsetof(struct user_regs_struct, LOC) + 4);
  37. #define REG_O_ZERO(LOC) \
  38. if (!ret) \
  39. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, \
  40. offsetof(struct user_regs_struct, LOC), \
  41. offsetof(struct user_regs_struct, LOC) + 4);
  42. REG_O_ZERO(pad);
  43. REG_O_ONE(scratch.bta, &ptregs->bta);
  44. REG_O_ONE(scratch.lp_start, &ptregs->lp_start);
  45. REG_O_ONE(scratch.lp_end, &ptregs->lp_end);
  46. REG_O_ONE(scratch.lp_count, &ptregs->lp_count);
  47. REG_O_ONE(scratch.status32, &ptregs->status32);
  48. REG_O_ONE(scratch.ret, &ptregs->ret);
  49. REG_O_ONE(scratch.blink, &ptregs->blink);
  50. REG_O_ONE(scratch.fp, &ptregs->fp);
  51. REG_O_ONE(scratch.gp, &ptregs->r26);
  52. REG_O_ONE(scratch.r12, &ptregs->r12);
  53. REG_O_ONE(scratch.r11, &ptregs->r11);
  54. REG_O_ONE(scratch.r10, &ptregs->r10);
  55. REG_O_ONE(scratch.r9, &ptregs->r9);
  56. REG_O_ONE(scratch.r8, &ptregs->r8);
  57. REG_O_ONE(scratch.r7, &ptregs->r7);
  58. REG_O_ONE(scratch.r6, &ptregs->r6);
  59. REG_O_ONE(scratch.r5, &ptregs->r5);
  60. REG_O_ONE(scratch.r4, &ptregs->r4);
  61. REG_O_ONE(scratch.r3, &ptregs->r3);
  62. REG_O_ONE(scratch.r2, &ptregs->r2);
  63. REG_O_ONE(scratch.r1, &ptregs->r1);
  64. REG_O_ONE(scratch.r0, &ptregs->r0);
  65. REG_O_ONE(scratch.sp, &ptregs->sp);
  66. REG_O_ZERO(pad2);
  67. REG_O_ONE(callee.r25, &cregs->r25);
  68. REG_O_ONE(callee.r24, &cregs->r24);
  69. REG_O_ONE(callee.r23, &cregs->r23);
  70. REG_O_ONE(callee.r22, &cregs->r22);
  71. REG_O_ONE(callee.r21, &cregs->r21);
  72. REG_O_ONE(callee.r20, &cregs->r20);
  73. REG_O_ONE(callee.r19, &cregs->r19);
  74. REG_O_ONE(callee.r18, &cregs->r18);
  75. REG_O_ONE(callee.r17, &cregs->r17);
  76. REG_O_ONE(callee.r16, &cregs->r16);
  77. REG_O_ONE(callee.r15, &cregs->r15);
  78. REG_O_ONE(callee.r14, &cregs->r14);
  79. REG_O_ONE(callee.r13, &cregs->r13);
  80. REG_O_ONE(efa, &target->thread.fault_address);
  81. if (!ret) {
  82. if (in_brkpt_trap(ptregs)) {
  83. stop_pc_val = target->thread.fault_address;
  84. pr_debug("\t\tstop_pc (brk-pt)\n");
  85. } else {
  86. stop_pc_val = ptregs->ret;
  87. pr_debug("\t\tstop_pc (others)\n");
  88. }
  89. REG_O_ONE(stop_pc, &stop_pc_val);
  90. }
  91. return ret;
  92. }
  93. static int genregs_set(struct task_struct *target,
  94. const struct user_regset *regset,
  95. unsigned int pos, unsigned int count,
  96. const void *kbuf, const void __user *ubuf)
  97. {
  98. const struct pt_regs *ptregs = task_pt_regs(target);
  99. const struct callee_regs *cregs = task_callee_regs(target);
  100. int ret = 0;
  101. #define REG_IN_CHUNK(FIRST, NEXT, PTR) \
  102. if (!ret) \
  103. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, \
  104. (void *)(PTR), \
  105. offsetof(struct user_regs_struct, FIRST), \
  106. offsetof(struct user_regs_struct, NEXT));
  107. #define REG_IN_ONE(LOC, PTR) \
  108. if (!ret) \
  109. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, \
  110. (void *)(PTR), \
  111. offsetof(struct user_regs_struct, LOC), \
  112. offsetof(struct user_regs_struct, LOC) + 4);
  113. #define REG_IGNORE_ONE(LOC) \
  114. if (!ret) \
  115. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, \
  116. offsetof(struct user_regs_struct, LOC), \
  117. offsetof(struct user_regs_struct, LOC) + 4);
  118. REG_IGNORE_ONE(pad);
  119. REG_IN_ONE(scratch.bta, &ptregs->bta);
  120. REG_IN_ONE(scratch.lp_start, &ptregs->lp_start);
  121. REG_IN_ONE(scratch.lp_end, &ptregs->lp_end);
  122. REG_IN_ONE(scratch.lp_count, &ptregs->lp_count);
  123. REG_IGNORE_ONE(scratch.status32);
  124. REG_IN_ONE(scratch.ret, &ptregs->ret);
  125. REG_IN_ONE(scratch.blink, &ptregs->blink);
  126. REG_IN_ONE(scratch.fp, &ptregs->fp);
  127. REG_IN_ONE(scratch.gp, &ptregs->r26);
  128. REG_IN_ONE(scratch.r12, &ptregs->r12);
  129. REG_IN_ONE(scratch.r11, &ptregs->r11);
  130. REG_IN_ONE(scratch.r10, &ptregs->r10);
  131. REG_IN_ONE(scratch.r9, &ptregs->r9);
  132. REG_IN_ONE(scratch.r8, &ptregs->r8);
  133. REG_IN_ONE(scratch.r7, &ptregs->r7);
  134. REG_IN_ONE(scratch.r6, &ptregs->r6);
  135. REG_IN_ONE(scratch.r5, &ptregs->r5);
  136. REG_IN_ONE(scratch.r4, &ptregs->r4);
  137. REG_IN_ONE(scratch.r3, &ptregs->r3);
  138. REG_IN_ONE(scratch.r2, &ptregs->r2);
  139. REG_IN_ONE(scratch.r1, &ptregs->r1);
  140. REG_IN_ONE(scratch.r0, &ptregs->r0);
  141. REG_IN_ONE(scratch.sp, &ptregs->sp);
  142. REG_IGNORE_ONE(pad2);
  143. REG_IN_ONE(callee.r25, &cregs->r25);
  144. REG_IN_ONE(callee.r24, &cregs->r24);
  145. REG_IN_ONE(callee.r23, &cregs->r23);
  146. REG_IN_ONE(callee.r22, &cregs->r22);
  147. REG_IN_ONE(callee.r21, &cregs->r21);
  148. REG_IN_ONE(callee.r20, &cregs->r20);
  149. REG_IN_ONE(callee.r19, &cregs->r19);
  150. REG_IN_ONE(callee.r18, &cregs->r18);
  151. REG_IN_ONE(callee.r17, &cregs->r17);
  152. REG_IN_ONE(callee.r16, &cregs->r16);
  153. REG_IN_ONE(callee.r15, &cregs->r15);
  154. REG_IN_ONE(callee.r14, &cregs->r14);
  155. REG_IN_ONE(callee.r13, &cregs->r13);
  156. REG_IGNORE_ONE(efa); /* efa update invalid */
  157. REG_IGNORE_ONE(stop_pc); /* PC updated via @ret */
  158. return ret;
  159. }
  160. enum arc_getset {
  161. REGSET_GENERAL,
  162. };
  163. static const struct user_regset arc_regsets[] = {
  164. [REGSET_GENERAL] = {
  165. .core_note_type = NT_PRSTATUS,
  166. .n = ELF_NGREG,
  167. .size = sizeof(unsigned long),
  168. .align = sizeof(unsigned long),
  169. .get = genregs_get,
  170. .set = genregs_set,
  171. }
  172. };
  173. static const struct user_regset_view user_arc_view = {
  174. .name = UTS_MACHINE,
  175. .e_machine = EM_ARC_INUSE,
  176. .regsets = arc_regsets,
  177. .n = ARRAY_SIZE(arc_regsets)
  178. };
  179. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  180. {
  181. return &user_arc_view;
  182. }
  183. void ptrace_disable(struct task_struct *child)
  184. {
  185. }
  186. long arch_ptrace(struct task_struct *child, long request,
  187. unsigned long addr, unsigned long data)
  188. {
  189. int ret = -EIO;
  190. pr_debug("REQ=%ld: ADDR =0x%lx, DATA=0x%lx)\n", request, addr, data);
  191. switch (request) {
  192. case PTRACE_GET_THREAD_AREA:
  193. ret = put_user(task_thread_info(child)->thr_ptr,
  194. (unsigned long __user *)data);
  195. break;
  196. default:
  197. ret = ptrace_request(child, request, addr, data);
  198. break;
  199. }
  200. return ret;
  201. }
  202. asmlinkage int syscall_trace_entry(struct pt_regs *regs)
  203. {
  204. if (tracehook_report_syscall_entry(regs))
  205. return ULONG_MAX;
  206. return regs->r8;
  207. }
  208. asmlinkage void syscall_trace_exit(struct pt_regs *regs)
  209. {
  210. tracehook_report_syscall_exit(regs, 0);
  211. }