ptrace_64.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * arch/sh/kernel/ptrace_64.c
  3. *
  4. * Copyright (C) 2000, 2001 Paolo Alberelli
  5. * Copyright (C) 2003 - 2008 Paul Mundt
  6. *
  7. * Started from SH3/4 version:
  8. * SuperH version: Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
  9. *
  10. * Original x86 implementation:
  11. * By Ross Biro 1/23/92
  12. * edited by Linus Torvalds
  13. *
  14. * This file is subject to the terms and conditions of the GNU General Public
  15. * License. See the file "COPYING" in the main directory of this archive
  16. * for more details.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/rwsem.h>
  20. #include <linux/sched.h>
  21. #include <linux/mm.h>
  22. #include <linux/smp.h>
  23. #include <linux/smp_lock.h>
  24. #include <linux/errno.h>
  25. #include <linux/ptrace.h>
  26. #include <linux/user.h>
  27. #include <linux/signal.h>
  28. #include <linux/syscalls.h>
  29. #include <linux/audit.h>
  30. #include <linux/seccomp.h>
  31. #include <linux/tracehook.h>
  32. #include <linux/elf.h>
  33. #include <linux/regset.h>
  34. #include <asm/io.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/system.h>
  38. #include <asm/processor.h>
  39. #include <asm/mmu_context.h>
  40. #include <asm/syscalls.h>
  41. #include <asm/fpu.h>
  42. /* This mask defines the bits of the SR which the user is not allowed to
  43. change, which are everything except S, Q, M, PR, SZ, FR. */
  44. #define SR_MASK (0xffff8cfd)
  45. /*
  46. * does not yet catch signals sent when the child dies.
  47. * in exit.c or in signal.c.
  48. */
  49. /*
  50. * This routine will get a word from the user area in the process kernel stack.
  51. */
  52. static inline int get_stack_long(struct task_struct *task, int offset)
  53. {
  54. unsigned char *stack;
  55. stack = (unsigned char *)(task->thread.uregs);
  56. stack += offset;
  57. return (*((int *)stack));
  58. }
  59. static inline unsigned long
  60. get_fpu_long(struct task_struct *task, unsigned long addr)
  61. {
  62. unsigned long tmp;
  63. struct pt_regs *regs;
  64. regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
  65. if (!tsk_used_math(task)) {
  66. if (addr == offsetof(struct user_fpu_struct, fpscr)) {
  67. tmp = FPSCR_INIT;
  68. } else {
  69. tmp = 0xffffffffUL; /* matches initial value in fpu.c */
  70. }
  71. return tmp;
  72. }
  73. if (last_task_used_math == task) {
  74. enable_fpu();
  75. save_fpu(task, regs);
  76. disable_fpu();
  77. last_task_used_math = 0;
  78. regs->sr |= SR_FD;
  79. }
  80. tmp = ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)];
  81. return tmp;
  82. }
  83. /*
  84. * This routine will put a word into the user area in the process kernel stack.
  85. */
  86. static inline int put_stack_long(struct task_struct *task, int offset,
  87. unsigned long data)
  88. {
  89. unsigned char *stack;
  90. stack = (unsigned char *)(task->thread.uregs);
  91. stack += offset;
  92. *(unsigned long *) stack = data;
  93. return 0;
  94. }
  95. static inline int
  96. put_fpu_long(struct task_struct *task, unsigned long addr, unsigned long data)
  97. {
  98. struct pt_regs *regs;
  99. regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
  100. if (!tsk_used_math(task)) {
  101. fpinit(&task->thread.fpu.hard);
  102. set_stopped_child_used_math(task);
  103. } else if (last_task_used_math == task) {
  104. enable_fpu();
  105. save_fpu(task, regs);
  106. disable_fpu();
  107. last_task_used_math = 0;
  108. regs->sr |= SR_FD;
  109. }
  110. ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)] = data;
  111. return 0;
  112. }
  113. void user_enable_single_step(struct task_struct *child)
  114. {
  115. struct pt_regs *regs = child->thread.uregs;
  116. regs->sr |= SR_SSTEP; /* auto-resetting upon exception */
  117. }
  118. void user_disable_single_step(struct task_struct *child)
  119. {
  120. struct pt_regs *regs = child->thread.uregs;
  121. regs->sr &= ~SR_SSTEP;
  122. }
  123. static int genregs_get(struct task_struct *target,
  124. const struct user_regset *regset,
  125. unsigned int pos, unsigned int count,
  126. void *kbuf, void __user *ubuf)
  127. {
  128. const struct pt_regs *regs = task_pt_regs(target);
  129. int ret;
  130. /* PC, SR, SYSCALL */
  131. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  132. &regs->pc,
  133. 0, 3 * sizeof(unsigned long long));
  134. /* R1 -> R63 */
  135. if (!ret)
  136. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  137. regs->regs,
  138. offsetof(struct pt_regs, regs[0]),
  139. 63 * sizeof(unsigned long long));
  140. /* TR0 -> TR7 */
  141. if (!ret)
  142. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  143. regs->tregs,
  144. offsetof(struct pt_regs, tregs[0]),
  145. 8 * sizeof(unsigned long long));
  146. if (!ret)
  147. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  148. sizeof(struct pt_regs), -1);
  149. return ret;
  150. }
  151. static int genregs_set(struct task_struct *target,
  152. const struct user_regset *regset,
  153. unsigned int pos, unsigned int count,
  154. const void *kbuf, const void __user *ubuf)
  155. {
  156. struct pt_regs *regs = task_pt_regs(target);
  157. int ret;
  158. /* PC, SR, SYSCALL */
  159. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  160. &regs->pc,
  161. 0, 3 * sizeof(unsigned long long));
  162. /* R1 -> R63 */
  163. if (!ret && count > 0)
  164. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  165. regs->regs,
  166. offsetof(struct pt_regs, regs[0]),
  167. 63 * sizeof(unsigned long long));
  168. /* TR0 -> TR7 */
  169. if (!ret && count > 0)
  170. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  171. regs->tregs,
  172. offsetof(struct pt_regs, tregs[0]),
  173. 8 * sizeof(unsigned long long));
  174. if (!ret)
  175. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  176. sizeof(struct pt_regs), -1);
  177. return ret;
  178. }
  179. #ifdef CONFIG_SH_FPU
  180. int fpregs_get(struct task_struct *target,
  181. const struct user_regset *regset,
  182. unsigned int pos, unsigned int count,
  183. void *kbuf, void __user *ubuf)
  184. {
  185. int ret;
  186. ret = init_fpu(target);
  187. if (ret)
  188. return ret;
  189. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  190. &target->thread.fpu.hard, 0, -1);
  191. }
  192. static int fpregs_set(struct task_struct *target,
  193. const struct user_regset *regset,
  194. unsigned int pos, unsigned int count,
  195. const void *kbuf, const void __user *ubuf)
  196. {
  197. int ret;
  198. ret = init_fpu(target);
  199. if (ret)
  200. return ret;
  201. set_stopped_child_used_math(target);
  202. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  203. &target->thread.fpu.hard, 0, -1);
  204. }
  205. static int fpregs_active(struct task_struct *target,
  206. const struct user_regset *regset)
  207. {
  208. return tsk_used_math(target) ? regset->n : 0;
  209. }
  210. #endif
  211. /*
  212. * These are our native regset flavours.
  213. */
  214. enum sh_regset {
  215. REGSET_GENERAL,
  216. #ifdef CONFIG_SH_FPU
  217. REGSET_FPU,
  218. #endif
  219. };
  220. static const struct user_regset sh_regsets[] = {
  221. /*
  222. * Format is:
  223. * PC, SR, SYSCALL,
  224. * R1 --> R63,
  225. * TR0 --> TR7,
  226. */
  227. [REGSET_GENERAL] = {
  228. .core_note_type = NT_PRSTATUS,
  229. .n = ELF_NGREG,
  230. .size = sizeof(long long),
  231. .align = sizeof(long long),
  232. .get = genregs_get,
  233. .set = genregs_set,
  234. },
  235. #ifdef CONFIG_SH_FPU
  236. [REGSET_FPU] = {
  237. .core_note_type = NT_PRFPREG,
  238. .n = sizeof(struct user_fpu_struct) /
  239. sizeof(long long),
  240. .size = sizeof(long long),
  241. .align = sizeof(long long),
  242. .get = fpregs_get,
  243. .set = fpregs_set,
  244. .active = fpregs_active,
  245. },
  246. #endif
  247. };
  248. static const struct user_regset_view user_sh64_native_view = {
  249. .name = "sh64",
  250. .e_machine = EM_SH,
  251. .regsets = sh_regsets,
  252. .n = ARRAY_SIZE(sh_regsets),
  253. };
  254. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  255. {
  256. return &user_sh64_native_view;
  257. }
  258. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  259. {
  260. int ret;
  261. switch (request) {
  262. /* read the word at location addr in the USER area. */
  263. case PTRACE_PEEKUSR: {
  264. unsigned long tmp;
  265. ret = -EIO;
  266. if ((addr & 3) || addr < 0)
  267. break;
  268. if (addr < sizeof(struct pt_regs))
  269. tmp = get_stack_long(child, addr);
  270. else if ((addr >= offsetof(struct user, fpu)) &&
  271. (addr < offsetof(struct user, u_fpvalid))) {
  272. tmp = get_fpu_long(child, addr - offsetof(struct user, fpu));
  273. } else if (addr == offsetof(struct user, u_fpvalid)) {
  274. tmp = !!tsk_used_math(child);
  275. } else {
  276. break;
  277. }
  278. ret = put_user(tmp, (unsigned long *)data);
  279. break;
  280. }
  281. case PTRACE_POKEUSR:
  282. /* write the word at location addr in the USER area. We must
  283. disallow any changes to certain SR bits or u_fpvalid, since
  284. this could crash the kernel or result in a security
  285. loophole. */
  286. ret = -EIO;
  287. if ((addr & 3) || addr < 0)
  288. break;
  289. if (addr < sizeof(struct pt_regs)) {
  290. /* Ignore change of top 32 bits of SR */
  291. if (addr == offsetof (struct pt_regs, sr)+4)
  292. {
  293. ret = 0;
  294. break;
  295. }
  296. /* If lower 32 bits of SR, ignore non-user bits */
  297. if (addr == offsetof (struct pt_regs, sr))
  298. {
  299. long cursr = get_stack_long(child, addr);
  300. data &= ~(SR_MASK);
  301. data |= (cursr & SR_MASK);
  302. }
  303. ret = put_stack_long(child, addr, data);
  304. }
  305. else if ((addr >= offsetof(struct user, fpu)) &&
  306. (addr < offsetof(struct user, u_fpvalid))) {
  307. ret = put_fpu_long(child, addr - offsetof(struct user, fpu), data);
  308. }
  309. break;
  310. case PTRACE_GETREGS:
  311. return copy_regset_to_user(child, &user_sh64_native_view,
  312. REGSET_GENERAL,
  313. 0, sizeof(struct pt_regs),
  314. (void __user *)data);
  315. case PTRACE_SETREGS:
  316. return copy_regset_from_user(child, &user_sh64_native_view,
  317. REGSET_GENERAL,
  318. 0, sizeof(struct pt_regs),
  319. (const void __user *)data);
  320. #ifdef CONFIG_SH_FPU
  321. case PTRACE_GETFPREGS:
  322. return copy_regset_to_user(child, &user_sh64_native_view,
  323. REGSET_FPU,
  324. 0, sizeof(struct user_fpu_struct),
  325. (void __user *)data);
  326. case PTRACE_SETFPREGS:
  327. return copy_regset_from_user(child, &user_sh64_native_view,
  328. REGSET_FPU,
  329. 0, sizeof(struct user_fpu_struct),
  330. (const void __user *)data);
  331. #endif
  332. default:
  333. ret = ptrace_request(child, request, addr, data);
  334. break;
  335. }
  336. return ret;
  337. }
  338. asmlinkage int sh64_ptrace(long request, long pid, long addr, long data)
  339. {
  340. #define WPC_DBRMODE 0x0d104008
  341. static int first_call = 1;
  342. lock_kernel();
  343. if (first_call) {
  344. /* Set WPC.DBRMODE to 0. This makes all debug events get
  345. * delivered through RESVEC, i.e. into the handlers in entry.S.
  346. * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
  347. * would normally be left set to 1, which makes debug events get
  348. * delivered through DBRVEC, i.e. into the remote gdb's
  349. * handlers. This prevents ptrace getting them, and confuses
  350. * the remote gdb.) */
  351. printk("DBRMODE set to 0 to permit native debugging\n");
  352. poke_real_address_q(WPC_DBRMODE, 0);
  353. first_call = 0;
  354. }
  355. unlock_kernel();
  356. return sys_ptrace(request, pid, addr, data);
  357. }
  358. static inline int audit_arch(void)
  359. {
  360. int arch = EM_SH;
  361. #ifdef CONFIG_64BIT
  362. arch |= __AUDIT_ARCH_64BIT;
  363. #endif
  364. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  365. arch |= __AUDIT_ARCH_LE;
  366. #endif
  367. return arch;
  368. }
  369. asmlinkage long long do_syscall_trace_enter(struct pt_regs *regs)
  370. {
  371. long long ret = 0;
  372. secure_computing(regs->regs[9]);
  373. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  374. tracehook_report_syscall_entry(regs))
  375. /*
  376. * Tracing decided this syscall should not happen.
  377. * We'll return a bogus call number to get an ENOSYS
  378. * error, but leave the original number in regs->regs[0].
  379. */
  380. ret = -1LL;
  381. if (unlikely(current->audit_context))
  382. audit_syscall_entry(audit_arch(), regs->regs[1],
  383. regs->regs[2], regs->regs[3],
  384. regs->regs[4], regs->regs[5]);
  385. return ret ?: regs->regs[9];
  386. }
  387. asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
  388. {
  389. if (unlikely(current->audit_context))
  390. audit_syscall_exit(AUDITSC_RESULT(regs->regs[9]),
  391. regs->regs[9]);
  392. if (test_thread_flag(TIF_SYSCALL_TRACE))
  393. tracehook_report_syscall_exit(regs, 0);
  394. }
  395. /* Called with interrupts disabled */
  396. asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
  397. {
  398. /* This is called after a single step exception (DEBUGSS).
  399. There is no need to change the PC, as it is a post-execution
  400. exception, as entry.S does not do anything to the PC for DEBUGSS.
  401. We need to clear the Single Step setting in SR to avoid
  402. continually stepping. */
  403. local_irq_enable();
  404. regs->sr &= ~SR_SSTEP;
  405. force_sig(SIGTRAP, current);
  406. }
  407. /* Called with interrupts disabled */
  408. asmlinkage void do_software_break_point(unsigned long long vec,
  409. struct pt_regs *regs)
  410. {
  411. /* We need to forward step the PC, to counteract the backstep done
  412. in signal.c. */
  413. local_irq_enable();
  414. force_sig(SIGTRAP, current);
  415. regs->pc += 4;
  416. }
  417. /*
  418. * Called by kernel/ptrace.c when detaching..
  419. *
  420. * Make sure single step bits etc are not set.
  421. */
  422. void ptrace_disable(struct task_struct *child)
  423. {
  424. user_disable_single_step(child);
  425. }