ptrace.c 9.3 KB

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