ptrace_64.c 12 KB

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