ptrace.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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,
  224. unsigned long addr, unsigned long data)
  225. {
  226. unsigned long tmp;
  227. int ret;
  228. int regno = addr >> 2;
  229. unsigned long __user *datap = (unsigned long __user *) data;
  230. switch (request) {
  231. /* read the word at location addr in the USER area. */
  232. case PTRACE_PEEKUSR: {
  233. tmp = 0;
  234. ret = -EIO;
  235. if (addr & 3)
  236. break;
  237. ret = 0;
  238. switch (regno) {
  239. case 0 ... PT__END - 1:
  240. tmp = get_reg(child, regno);
  241. break;
  242. case PT__END + 0:
  243. tmp = child->mm->end_code - child->mm->start_code;
  244. break;
  245. case PT__END + 1:
  246. tmp = child->mm->end_data - child->mm->start_data;
  247. break;
  248. case PT__END + 2:
  249. tmp = child->mm->start_stack - child->mm->start_brk;
  250. break;
  251. case PT__END + 3:
  252. tmp = child->mm->start_code;
  253. break;
  254. case PT__END + 4:
  255. tmp = child->mm->start_stack;
  256. break;
  257. default:
  258. ret = -EIO;
  259. break;
  260. }
  261. if (ret == 0)
  262. ret = put_user(tmp, datap);
  263. break;
  264. }
  265. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  266. ret = -EIO;
  267. if (addr & 3)
  268. break;
  269. switch (regno) {
  270. case 0 ... PT__END - 1:
  271. ret = put_reg(child, regno, data);
  272. break;
  273. }
  274. break;
  275. case PTRACE_GETREGS: /* Get all integer regs from the child. */
  276. return copy_regset_to_user(child, &user_frv_native_view,
  277. REGSET_GENERAL,
  278. 0, sizeof(child->thread.user->i),
  279. datap);
  280. case PTRACE_SETREGS: /* Set all integer regs in the child. */
  281. return copy_regset_from_user(child, &user_frv_native_view,
  282. REGSET_GENERAL,
  283. 0, sizeof(child->thread.user->i),
  284. datap);
  285. case PTRACE_GETFPREGS: /* Get the child FP/Media state. */
  286. return copy_regset_to_user(child, &user_frv_native_view,
  287. REGSET_FPMEDIA,
  288. 0, sizeof(child->thread.user->f),
  289. datap);
  290. case PTRACE_SETFPREGS: /* Set the child FP/Media state. */
  291. return copy_regset_from_user(child, &user_frv_native_view,
  292. REGSET_FPMEDIA,
  293. 0, sizeof(child->thread.user->f),
  294. datap);
  295. default:
  296. ret = ptrace_request(child, request, addr, data);
  297. break;
  298. }
  299. return ret;
  300. }
  301. /*
  302. * handle tracing of system call entry
  303. * - return the revised system call number or ULONG_MAX to cause ENOSYS
  304. */
  305. asmlinkage unsigned long syscall_trace_entry(void)
  306. {
  307. __frame->__status |= REG__STATUS_SYSC_ENTRY;
  308. if (tracehook_report_syscall_entry(__frame)) {
  309. /* tracing decided this syscall should not happen, so
  310. * We'll return a bogus call number to get an ENOSYS
  311. * error, but leave the original number in
  312. * __frame->syscallno
  313. */
  314. return ULONG_MAX;
  315. }
  316. return __frame->syscallno;
  317. }
  318. /*
  319. * handle tracing of system call exit
  320. */
  321. asmlinkage void syscall_trace_exit(void)
  322. {
  323. __frame->__status |= REG__STATUS_SYSC_EXIT;
  324. tracehook_report_syscall_exit(__frame, 0);
  325. }