ptrace.c 9.7 KB

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