ptrace.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /* ptrace.c: FRV specific parts of process tracing
  2. *
  3. * Copyright (C) 2003-5 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. * - Derived from arch/m68k/kernel/ptrace.c
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/mm.h>
  15. #include <linux/smp.h>
  16. #include <linux/errno.h>
  17. #include <linux/ptrace.h>
  18. #include <linux/user.h>
  19. #include <linux/security.h>
  20. #include <linux/signal.h>
  21. #include <linux/regset.h>
  22. #include <linux/elf.h>
  23. #include <linux/tracehook.h>
  24. #include <asm/uaccess.h>
  25. #include <asm/page.h>
  26. #include <asm/pgtable.h>
  27. #include <asm/processor.h>
  28. #include <asm/unistd.h>
  29. /*
  30. * does not yet catch signals sent when the child dies.
  31. * in exit.c or in signal.c.
  32. */
  33. /*
  34. * retrieve the contents of FRV userspace general registers
  35. */
  36. static int genregs_get(struct task_struct *target,
  37. const struct user_regset *regset,
  38. unsigned int pos, unsigned int count,
  39. void *kbuf, void __user *ubuf)
  40. {
  41. const struct user_int_regs *iregs = &target->thread.user->i;
  42. int ret;
  43. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  44. iregs, 0, sizeof(*iregs));
  45. if (ret < 0)
  46. return ret;
  47. return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  48. sizeof(*iregs), -1);
  49. }
  50. /*
  51. * update the contents of the FRV userspace general registers
  52. */
  53. static int genregs_set(struct task_struct *target,
  54. const struct user_regset *regset,
  55. unsigned int pos, unsigned int count,
  56. const void *kbuf, const void __user *ubuf)
  57. {
  58. struct user_int_regs *iregs = &target->thread.user->i;
  59. unsigned int offs_gr0, offs_gr1;
  60. int ret;
  61. /* not allowed to set PSR or __status */
  62. if (pos < offsetof(struct user_int_regs, psr) + sizeof(long) &&
  63. pos + count > offsetof(struct user_int_regs, psr))
  64. return -EIO;
  65. if (pos < offsetof(struct user_int_regs, __status) + sizeof(long) &&
  66. pos + count > offsetof(struct user_int_regs, __status))
  67. return -EIO;
  68. /* set the control regs */
  69. offs_gr0 = offsetof(struct user_int_regs, gr[0]);
  70. offs_gr1 = offsetof(struct user_int_regs, gr[1]);
  71. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  72. iregs, 0, offs_gr0);
  73. if (ret < 0)
  74. return ret;
  75. /* skip GR0/TBR */
  76. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  77. offs_gr0, offs_gr1);
  78. if (ret < 0)
  79. return ret;
  80. /* set the general regs */
  81. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  82. &iregs->gr[1], offs_gr1, sizeof(*iregs));
  83. if (ret < 0)
  84. return ret;
  85. return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  86. sizeof(*iregs), -1);
  87. }
  88. /*
  89. * retrieve the contents of FRV userspace FP/Media registers
  90. */
  91. static int fpmregs_get(struct task_struct *target,
  92. const struct user_regset *regset,
  93. unsigned int pos, unsigned int count,
  94. void *kbuf, void __user *ubuf)
  95. {
  96. const struct user_fpmedia_regs *fpregs = &target->thread.user->f;
  97. int ret;
  98. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  99. fpregs, 0, sizeof(*fpregs));
  100. if (ret < 0)
  101. return ret;
  102. return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  103. sizeof(*fpregs), -1);
  104. }
  105. /*
  106. * update the contents of the FRV userspace FP/Media registers
  107. */
  108. static int fpmregs_set(struct task_struct *target,
  109. const struct user_regset *regset,
  110. unsigned int pos, unsigned int count,
  111. const void *kbuf, const void __user *ubuf)
  112. {
  113. struct user_fpmedia_regs *fpregs = &target->thread.user->f;
  114. int ret;
  115. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  116. fpregs, 0, sizeof(*fpregs));
  117. if (ret < 0)
  118. return ret;
  119. return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  120. sizeof(*fpregs), -1);
  121. }
  122. /*
  123. * determine if the FP/Media registers have actually been used
  124. */
  125. static int fpmregs_active(struct task_struct *target,
  126. const struct user_regset *regset)
  127. {
  128. return tsk_used_math(target) ? regset->n : 0;
  129. }
  130. /*
  131. * Define the register sets available on the FRV under Linux
  132. */
  133. enum frv_regset {
  134. REGSET_GENERAL,
  135. REGSET_FPMEDIA,
  136. };
  137. static const struct user_regset frv_regsets[] = {
  138. /*
  139. * General register format is:
  140. * PSR, ISR, CCR, CCCR, LR, LCR, PC, (STATUS), SYSCALLNO, ORIG_G8
  141. * GNER0-1, IACC0, TBR, GR1-63
  142. */
  143. [REGSET_GENERAL] = {
  144. .core_note_type = NT_PRSTATUS,
  145. .n = ELF_NGREG,
  146. .size = sizeof(long),
  147. .align = sizeof(long),
  148. .get = genregs_get,
  149. .set = genregs_set,
  150. },
  151. /*
  152. * FPU/Media register format is:
  153. * FR0-63, FNER0-1, MSR0-1, ACC0-7, ACCG0-8, FSR
  154. */
  155. [REGSET_FPMEDIA] = {
  156. .core_note_type = NT_PRFPREG,
  157. .n = sizeof(struct user_fpmedia_regs) / sizeof(long),
  158. .size = sizeof(long),
  159. .align = sizeof(long),
  160. .get = fpmregs_get,
  161. .set = fpmregs_set,
  162. .active = fpmregs_active,
  163. },
  164. };
  165. static const struct user_regset_view user_frv_native_view = {
  166. .name = "frv",
  167. .e_machine = EM_FRV,
  168. .regsets = frv_regsets,
  169. .n = ARRAY_SIZE(frv_regsets),
  170. };
  171. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  172. {
  173. return &user_frv_native_view;
  174. }
  175. /*
  176. * Get contents of register REGNO in task TASK.
  177. */
  178. static inline long get_reg(struct task_struct *task, int regno)
  179. {
  180. struct user_context *user = task->thread.user;
  181. if (regno < 0 || regno >= PT__END)
  182. return 0;
  183. return ((unsigned long *) user)[regno];
  184. }
  185. /*
  186. * Write contents of register REGNO in task TASK.
  187. */
  188. static inline int put_reg(struct task_struct *task, int regno,
  189. unsigned long data)
  190. {
  191. struct user_context *user = task->thread.user;
  192. if (regno < 0 || regno >= PT__END)
  193. return -EIO;
  194. switch (regno) {
  195. case PT_GR(0):
  196. return 0;
  197. case PT_PSR:
  198. case PT__STATUS:
  199. return -EIO;
  200. default:
  201. ((unsigned long *) user)[regno] = data;
  202. return 0;
  203. }
  204. }
  205. /*
  206. * Called by kernel/ptrace.c when detaching..
  207. *
  208. * Control h/w single stepping
  209. */
  210. void user_enable_single_step(struct task_struct *child)
  211. {
  212. child->thread.frame0->__status |= REG__STATUS_STEP;
  213. }
  214. void user_disable_single_step(struct task_struct *child)
  215. {
  216. child->thread.frame0->__status &= ~REG__STATUS_STEP;
  217. }
  218. void ptrace_disable(struct task_struct *child)
  219. {
  220. user_disable_single_step(child);
  221. }
  222. long arch_ptrace(struct task_struct *child, long request,
  223. unsigned long addr, unsigned long data)
  224. {
  225. unsigned long tmp;
  226. int ret;
  227. int regno = addr >> 2;
  228. unsigned long __user *datap = (unsigned long __user *) data;
  229. switch (request) {
  230. /* read the word at location addr in the USER area. */
  231. case PTRACE_PEEKUSR: {
  232. tmp = 0;
  233. ret = -EIO;
  234. if (addr & 3)
  235. break;
  236. ret = 0;
  237. switch (regno) {
  238. case 0 ... PT__END - 1:
  239. tmp = get_reg(child, regno);
  240. break;
  241. case PT__END + 0:
  242. tmp = child->mm->end_code - child->mm->start_code;
  243. break;
  244. case PT__END + 1:
  245. tmp = child->mm->end_data - child->mm->start_data;
  246. break;
  247. case PT__END + 2:
  248. tmp = child->mm->start_stack - child->mm->start_brk;
  249. break;
  250. case PT__END + 3:
  251. tmp = child->mm->start_code;
  252. break;
  253. case PT__END + 4:
  254. tmp = child->mm->start_stack;
  255. break;
  256. default:
  257. ret = -EIO;
  258. break;
  259. }
  260. if (ret == 0)
  261. ret = put_user(tmp, datap);
  262. break;
  263. }
  264. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  265. ret = -EIO;
  266. if (addr & 3)
  267. break;
  268. switch (regno) {
  269. case 0 ... PT__END - 1:
  270. ret = put_reg(child, regno, data);
  271. break;
  272. }
  273. break;
  274. case PTRACE_GETREGS: /* Get all integer regs from the child. */
  275. return copy_regset_to_user(child, &user_frv_native_view,
  276. REGSET_GENERAL,
  277. 0, sizeof(child->thread.user->i),
  278. datap);
  279. case PTRACE_SETREGS: /* Set all integer regs in the child. */
  280. return copy_regset_from_user(child, &user_frv_native_view,
  281. REGSET_GENERAL,
  282. 0, sizeof(child->thread.user->i),
  283. datap);
  284. case PTRACE_GETFPREGS: /* Get the child FP/Media state. */
  285. return copy_regset_to_user(child, &user_frv_native_view,
  286. REGSET_FPMEDIA,
  287. 0, sizeof(child->thread.user->f),
  288. datap);
  289. case PTRACE_SETFPREGS: /* Set the child FP/Media state. */
  290. return copy_regset_from_user(child, &user_frv_native_view,
  291. REGSET_FPMEDIA,
  292. 0, sizeof(child->thread.user->f),
  293. datap);
  294. default:
  295. ret = ptrace_request(child, request, addr, data);
  296. break;
  297. }
  298. return ret;
  299. }
  300. /*
  301. * handle tracing of system call entry
  302. * - return the revised system call number or ULONG_MAX to cause ENOSYS
  303. */
  304. asmlinkage unsigned long syscall_trace_entry(void)
  305. {
  306. __frame->__status |= REG__STATUS_SYSC_ENTRY;
  307. if (tracehook_report_syscall_entry(__frame)) {
  308. /* tracing decided this syscall should not happen, so
  309. * We'll return a bogus call number to get an ENOSYS
  310. * error, but leave the original number in
  311. * __frame->syscallno
  312. */
  313. return ULONG_MAX;
  314. }
  315. return __frame->syscallno;
  316. }
  317. /*
  318. * handle tracing of system call exit
  319. */
  320. asmlinkage void syscall_trace_exit(void)
  321. {
  322. __frame->__status |= REG__STATUS_SYSC_EXIT;
  323. tracehook_report_syscall_exit(__frame, 0);
  324. }