ptrace_64.c 12 KB

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