ptrace.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /* MN10300 Process tracing
  2. *
  3. * Copyright (C) 2007 Matsushita Electric Industrial Co., Ltd.
  4. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  5. * Modified by David Howells (dhowells@redhat.com)
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public Licence
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the Licence, 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/regset.h>
  20. #include <linux/elf.h>
  21. #include <linux/tracehook.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/processor.h>
  25. #include <asm/cacheflush.h>
  26. #include <asm/fpu.h>
  27. #include <asm/asm-offsets.h>
  28. /*
  29. * translate ptrace register IDs into struct pt_regs offsets
  30. */
  31. static const u8 ptrace_regid_to_frame[] = {
  32. [PT_A3 << 2] = REG_A3,
  33. [PT_A2 << 2] = REG_A2,
  34. [PT_D3 << 2] = REG_D3,
  35. [PT_D2 << 2] = REG_D2,
  36. [PT_MCVF << 2] = REG_MCVF,
  37. [PT_MCRL << 2] = REG_MCRL,
  38. [PT_MCRH << 2] = REG_MCRH,
  39. [PT_MDRQ << 2] = REG_MDRQ,
  40. [PT_E1 << 2] = REG_E1,
  41. [PT_E0 << 2] = REG_E0,
  42. [PT_E7 << 2] = REG_E7,
  43. [PT_E6 << 2] = REG_E6,
  44. [PT_E5 << 2] = REG_E5,
  45. [PT_E4 << 2] = REG_E4,
  46. [PT_E3 << 2] = REG_E3,
  47. [PT_E2 << 2] = REG_E2,
  48. [PT_SP << 2] = REG_SP,
  49. [PT_LAR << 2] = REG_LAR,
  50. [PT_LIR << 2] = REG_LIR,
  51. [PT_MDR << 2] = REG_MDR,
  52. [PT_A1 << 2] = REG_A1,
  53. [PT_A0 << 2] = REG_A0,
  54. [PT_D1 << 2] = REG_D1,
  55. [PT_D0 << 2] = REG_D0,
  56. [PT_ORIG_D0 << 2] = REG_ORIG_D0,
  57. [PT_EPSW << 2] = REG_EPSW,
  58. [PT_PC << 2] = REG_PC,
  59. };
  60. static inline int get_stack_long(struct task_struct *task, int offset)
  61. {
  62. return *(unsigned long *)
  63. ((unsigned long) task->thread.uregs + offset);
  64. }
  65. static inline
  66. int put_stack_long(struct task_struct *task, int offset, unsigned long data)
  67. {
  68. unsigned long stack;
  69. stack = (unsigned long) task->thread.uregs + offset;
  70. *(unsigned long *) stack = data;
  71. return 0;
  72. }
  73. /*
  74. * retrieve the contents of MN10300 userspace general registers
  75. */
  76. static int genregs_get(struct task_struct *target,
  77. const struct user_regset *regset,
  78. unsigned int pos, unsigned int count,
  79. void *kbuf, void __user *ubuf)
  80. {
  81. const struct pt_regs *regs = task_pt_regs(target);
  82. int ret;
  83. /* we need to skip regs->next */
  84. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  85. regs, 0, PT_ORIG_D0 * sizeof(long));
  86. if (ret < 0)
  87. return ret;
  88. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  89. &regs->orig_d0, PT_ORIG_D0 * sizeof(long),
  90. NR_PTREGS * sizeof(long));
  91. if (ret < 0)
  92. return ret;
  93. return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  94. NR_PTREGS * sizeof(long), -1);
  95. }
  96. /*
  97. * update the contents of the MN10300 userspace general registers
  98. */
  99. static int genregs_set(struct task_struct *target,
  100. const struct user_regset *regset,
  101. unsigned int pos, unsigned int count,
  102. const void *kbuf, const void __user *ubuf)
  103. {
  104. struct pt_regs *regs = task_pt_regs(target);
  105. unsigned long tmp;
  106. int ret;
  107. /* we need to skip regs->next */
  108. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  109. regs, 0, PT_ORIG_D0 * sizeof(long));
  110. if (ret < 0)
  111. return ret;
  112. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  113. &regs->orig_d0, PT_ORIG_D0 * sizeof(long),
  114. PT_EPSW * sizeof(long));
  115. if (ret < 0)
  116. return ret;
  117. /* we need to mask off changes to EPSW */
  118. tmp = regs->epsw;
  119. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  120. &tmp, PT_EPSW * sizeof(long),
  121. PT_PC * sizeof(long));
  122. tmp &= EPSW_FLAG_V | EPSW_FLAG_C | EPSW_FLAG_N | EPSW_FLAG_Z;
  123. tmp |= regs->epsw & ~(EPSW_FLAG_V | EPSW_FLAG_C | EPSW_FLAG_N |
  124. EPSW_FLAG_Z);
  125. regs->epsw = tmp;
  126. if (ret < 0)
  127. return ret;
  128. /* and finally load the PC */
  129. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  130. &regs->pc, PT_PC * sizeof(long),
  131. NR_PTREGS * sizeof(long));
  132. if (ret < 0)
  133. return ret;
  134. return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  135. NR_PTREGS * sizeof(long), -1);
  136. }
  137. /*
  138. * retrieve the contents of MN10300 userspace FPU registers
  139. */
  140. static int fpuregs_get(struct task_struct *target,
  141. const struct user_regset *regset,
  142. unsigned int pos, unsigned int count,
  143. void *kbuf, void __user *ubuf)
  144. {
  145. const struct fpu_state_struct *fpregs = &target->thread.fpu_state;
  146. int ret;
  147. unlazy_fpu(target);
  148. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  149. fpregs, 0, sizeof(*fpregs));
  150. if (ret < 0)
  151. return ret;
  152. return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  153. sizeof(*fpregs), -1);
  154. }
  155. /*
  156. * update the contents of the MN10300 userspace FPU registers
  157. */
  158. static int fpuregs_set(struct task_struct *target,
  159. const struct user_regset *regset,
  160. unsigned int pos, unsigned int count,
  161. const void *kbuf, const void __user *ubuf)
  162. {
  163. struct fpu_state_struct fpu_state = target->thread.fpu_state;
  164. int ret;
  165. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  166. &fpu_state, 0, sizeof(fpu_state));
  167. if (ret < 0)
  168. return ret;
  169. fpu_kill_state(target);
  170. target->thread.fpu_state = fpu_state;
  171. set_using_fpu(target);
  172. return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  173. sizeof(fpu_state), -1);
  174. }
  175. /*
  176. * determine if the FPU registers have actually been used
  177. */
  178. static int fpuregs_active(struct task_struct *target,
  179. const struct user_regset *regset)
  180. {
  181. return is_using_fpu(target) ? regset->n : 0;
  182. }
  183. /*
  184. * Define the register sets available on the MN10300 under Linux
  185. */
  186. enum mn10300_regset {
  187. REGSET_GENERAL,
  188. REGSET_FPU,
  189. };
  190. static const struct user_regset mn10300_regsets[] = {
  191. /*
  192. * General register format is:
  193. * A3, A2, D3, D2, MCVF, MCRL, MCRH, MDRQ
  194. * E1, E0, E7...E2, SP, LAR, LIR, MDR
  195. * A1, A0, D1, D0, ORIG_D0, EPSW, PC
  196. */
  197. [REGSET_GENERAL] = {
  198. .core_note_type = NT_PRSTATUS,
  199. .n = ELF_NGREG,
  200. .size = sizeof(long),
  201. .align = sizeof(long),
  202. .get = genregs_get,
  203. .set = genregs_set,
  204. },
  205. /*
  206. * FPU register format is:
  207. * FS0-31, FPCR
  208. */
  209. [REGSET_FPU] = {
  210. .core_note_type = NT_PRFPREG,
  211. .n = sizeof(struct fpu_state_struct) / sizeof(long),
  212. .size = sizeof(long),
  213. .align = sizeof(long),
  214. .get = fpuregs_get,
  215. .set = fpuregs_set,
  216. .active = fpuregs_active,
  217. },
  218. };
  219. static const struct user_regset_view user_mn10300_native_view = {
  220. .name = "mn10300",
  221. .e_machine = EM_MN10300,
  222. .regsets = mn10300_regsets,
  223. .n = ARRAY_SIZE(mn10300_regsets),
  224. };
  225. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  226. {
  227. return &user_mn10300_native_view;
  228. }
  229. /*
  230. * set the single-step bit
  231. */
  232. void user_enable_single_step(struct task_struct *child)
  233. {
  234. #ifndef CONFIG_MN10300_USING_JTAG
  235. struct user *dummy = NULL;
  236. long tmp;
  237. tmp = get_stack_long(child, (unsigned long) &dummy->regs.epsw);
  238. tmp |= EPSW_T;
  239. put_stack_long(child, (unsigned long) &dummy->regs.epsw, tmp);
  240. #endif
  241. }
  242. /*
  243. * make sure the single-step bit is not set
  244. */
  245. void user_disable_single_step(struct task_struct *child)
  246. {
  247. #ifndef CONFIG_MN10300_USING_JTAG
  248. struct user *dummy = NULL;
  249. long tmp;
  250. tmp = get_stack_long(child, (unsigned long) &dummy->regs.epsw);
  251. tmp &= ~EPSW_T;
  252. put_stack_long(child, (unsigned long) &dummy->regs.epsw, tmp);
  253. #endif
  254. }
  255. void ptrace_disable(struct task_struct *child)
  256. {
  257. user_disable_single_step(child);
  258. }
  259. /*
  260. * handle the arch-specific side of process tracing
  261. */
  262. long arch_ptrace(struct task_struct *child, long request,
  263. unsigned long addr, unsigned long data)
  264. {
  265. unsigned long tmp;
  266. int ret;
  267. unsigned long __user *datap = (unsigned long __user *) data;
  268. switch (request) {
  269. /* read the word at location addr in the USER area. */
  270. case PTRACE_PEEKUSR:
  271. ret = -EIO;
  272. if ((addr & 3) || addr > sizeof(struct user) - 3)
  273. break;
  274. tmp = 0; /* Default return condition */
  275. if (addr < NR_PTREGS << 2)
  276. tmp = get_stack_long(child,
  277. ptrace_regid_to_frame[addr]);
  278. ret = put_user(tmp, datap);
  279. break;
  280. /* write the word at location addr in the USER area */
  281. case PTRACE_POKEUSR:
  282. ret = -EIO;
  283. if ((addr & 3) || addr > sizeof(struct user) - 3)
  284. break;
  285. ret = 0;
  286. if (addr < NR_PTREGS << 2)
  287. ret = put_stack_long(child, ptrace_regid_to_frame[addr],
  288. data);
  289. break;
  290. case PTRACE_GETREGS: /* Get all integer regs from the child. */
  291. return copy_regset_to_user(child, &user_mn10300_native_view,
  292. REGSET_GENERAL,
  293. 0, NR_PTREGS * sizeof(long),
  294. datap);
  295. case PTRACE_SETREGS: /* Set all integer regs in the child. */
  296. return copy_regset_from_user(child, &user_mn10300_native_view,
  297. REGSET_GENERAL,
  298. 0, NR_PTREGS * sizeof(long),
  299. datap);
  300. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  301. return copy_regset_to_user(child, &user_mn10300_native_view,
  302. REGSET_FPU,
  303. 0, sizeof(struct fpu_state_struct),
  304. datap);
  305. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  306. return copy_regset_from_user(child, &user_mn10300_native_view,
  307. REGSET_FPU,
  308. 0, sizeof(struct fpu_state_struct),
  309. datap);
  310. default:
  311. ret = ptrace_request(child, request, addr, data);
  312. break;
  313. }
  314. return ret;
  315. }
  316. /*
  317. * handle tracing of system call entry
  318. * - return the revised system call number or ULONG_MAX to cause ENOSYS
  319. */
  320. asmlinkage unsigned long syscall_trace_entry(struct pt_regs *regs)
  321. {
  322. if (tracehook_report_syscall_entry(regs))
  323. /* tracing decided this syscall should not happen, so
  324. * We'll return a bogus call number to get an ENOSYS
  325. * error, but leave the original number in
  326. * regs->orig_d0
  327. */
  328. return ULONG_MAX;
  329. return regs->orig_d0;
  330. }
  331. /*
  332. * handle tracing of system call exit
  333. */
  334. asmlinkage void syscall_trace_exit(struct pt_regs *regs)
  335. {
  336. tracehook_report_syscall_exit(regs, 0);
  337. }