ptrace_64.c 12 KB

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