ptrace_64.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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/bitops.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/processor.h>
  38. #include <asm/mmu_context.h>
  39. #include <asm/syscalls.h>
  40. #include <asm/fpu.h>
  41. #include <asm/traps.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. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  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. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  125. }
  126. static int genregs_get(struct task_struct *target,
  127. const struct user_regset *regset,
  128. unsigned int pos, unsigned int count,
  129. void *kbuf, void __user *ubuf)
  130. {
  131. const struct pt_regs *regs = task_pt_regs(target);
  132. int ret;
  133. /* PC, SR, SYSCALL */
  134. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  135. &regs->pc,
  136. 0, 3 * sizeof(unsigned long long));
  137. /* R1 -> R63 */
  138. if (!ret)
  139. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  140. regs->regs,
  141. offsetof(struct pt_regs, regs[0]),
  142. 63 * sizeof(unsigned long long));
  143. /* TR0 -> TR7 */
  144. if (!ret)
  145. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  146. regs->tregs,
  147. offsetof(struct pt_regs, tregs[0]),
  148. 8 * sizeof(unsigned long long));
  149. if (!ret)
  150. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  151. sizeof(struct pt_regs), -1);
  152. return ret;
  153. }
  154. static int genregs_set(struct task_struct *target,
  155. const struct user_regset *regset,
  156. unsigned int pos, unsigned int count,
  157. const void *kbuf, const void __user *ubuf)
  158. {
  159. struct pt_regs *regs = task_pt_regs(target);
  160. int ret;
  161. /* PC, SR, SYSCALL */
  162. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  163. &regs->pc,
  164. 0, 3 * sizeof(unsigned long long));
  165. /* R1 -> R63 */
  166. if (!ret && count > 0)
  167. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  168. regs->regs,
  169. offsetof(struct pt_regs, regs[0]),
  170. 63 * sizeof(unsigned long long));
  171. /* TR0 -> TR7 */
  172. if (!ret && count > 0)
  173. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  174. regs->tregs,
  175. offsetof(struct pt_regs, tregs[0]),
  176. 8 * sizeof(unsigned long long));
  177. if (!ret)
  178. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  179. sizeof(struct pt_regs), -1);
  180. return ret;
  181. }
  182. #ifdef CONFIG_SH_FPU
  183. int fpregs_get(struct task_struct *target,
  184. const struct user_regset *regset,
  185. unsigned int pos, unsigned int count,
  186. void *kbuf, void __user *ubuf)
  187. {
  188. int ret;
  189. ret = init_fpu(target);
  190. if (ret)
  191. return ret;
  192. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  193. &target->thread.xstate->hardfpu, 0, -1);
  194. }
  195. static int fpregs_set(struct task_struct *target,
  196. const struct user_regset *regset,
  197. unsigned int pos, unsigned int count,
  198. const void *kbuf, const void __user *ubuf)
  199. {
  200. int ret;
  201. ret = init_fpu(target);
  202. if (ret)
  203. return ret;
  204. set_stopped_child_used_math(target);
  205. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  206. &target->thread.xstate->hardfpu, 0, -1);
  207. }
  208. static int fpregs_active(struct task_struct *target,
  209. const struct user_regset *regset)
  210. {
  211. return tsk_used_math(target) ? regset->n : 0;
  212. }
  213. #endif
  214. const struct pt_regs_offset regoffset_table[] = {
  215. REG_OFFSET_NAME(pc),
  216. REG_OFFSET_NAME(sr),
  217. REG_OFFSET_NAME(syscall_nr),
  218. REGS_OFFSET_NAME(0),
  219. REGS_OFFSET_NAME(1),
  220. REGS_OFFSET_NAME(2),
  221. REGS_OFFSET_NAME(3),
  222. REGS_OFFSET_NAME(4),
  223. REGS_OFFSET_NAME(5),
  224. REGS_OFFSET_NAME(6),
  225. REGS_OFFSET_NAME(7),
  226. REGS_OFFSET_NAME(8),
  227. REGS_OFFSET_NAME(9),
  228. REGS_OFFSET_NAME(10),
  229. REGS_OFFSET_NAME(11),
  230. REGS_OFFSET_NAME(12),
  231. REGS_OFFSET_NAME(13),
  232. REGS_OFFSET_NAME(14),
  233. REGS_OFFSET_NAME(15),
  234. REGS_OFFSET_NAME(16),
  235. REGS_OFFSET_NAME(17),
  236. REGS_OFFSET_NAME(18),
  237. REGS_OFFSET_NAME(19),
  238. REGS_OFFSET_NAME(20),
  239. REGS_OFFSET_NAME(21),
  240. REGS_OFFSET_NAME(22),
  241. REGS_OFFSET_NAME(23),
  242. REGS_OFFSET_NAME(24),
  243. REGS_OFFSET_NAME(25),
  244. REGS_OFFSET_NAME(26),
  245. REGS_OFFSET_NAME(27),
  246. REGS_OFFSET_NAME(28),
  247. REGS_OFFSET_NAME(29),
  248. REGS_OFFSET_NAME(30),
  249. REGS_OFFSET_NAME(31),
  250. REGS_OFFSET_NAME(32),
  251. REGS_OFFSET_NAME(33),
  252. REGS_OFFSET_NAME(34),
  253. REGS_OFFSET_NAME(35),
  254. REGS_OFFSET_NAME(36),
  255. REGS_OFFSET_NAME(37),
  256. REGS_OFFSET_NAME(38),
  257. REGS_OFFSET_NAME(39),
  258. REGS_OFFSET_NAME(40),
  259. REGS_OFFSET_NAME(41),
  260. REGS_OFFSET_NAME(42),
  261. REGS_OFFSET_NAME(43),
  262. REGS_OFFSET_NAME(44),
  263. REGS_OFFSET_NAME(45),
  264. REGS_OFFSET_NAME(46),
  265. REGS_OFFSET_NAME(47),
  266. REGS_OFFSET_NAME(48),
  267. REGS_OFFSET_NAME(49),
  268. REGS_OFFSET_NAME(50),
  269. REGS_OFFSET_NAME(51),
  270. REGS_OFFSET_NAME(52),
  271. REGS_OFFSET_NAME(53),
  272. REGS_OFFSET_NAME(54),
  273. REGS_OFFSET_NAME(55),
  274. REGS_OFFSET_NAME(56),
  275. REGS_OFFSET_NAME(57),
  276. REGS_OFFSET_NAME(58),
  277. REGS_OFFSET_NAME(59),
  278. REGS_OFFSET_NAME(60),
  279. REGS_OFFSET_NAME(61),
  280. REGS_OFFSET_NAME(62),
  281. REGS_OFFSET_NAME(63),
  282. TREGS_OFFSET_NAME(0),
  283. TREGS_OFFSET_NAME(1),
  284. TREGS_OFFSET_NAME(2),
  285. TREGS_OFFSET_NAME(3),
  286. TREGS_OFFSET_NAME(4),
  287. TREGS_OFFSET_NAME(5),
  288. TREGS_OFFSET_NAME(6),
  289. TREGS_OFFSET_NAME(7),
  290. REG_OFFSET_END,
  291. };
  292. /*
  293. * These are our native regset flavours.
  294. */
  295. enum sh_regset {
  296. REGSET_GENERAL,
  297. #ifdef CONFIG_SH_FPU
  298. REGSET_FPU,
  299. #endif
  300. };
  301. static const struct user_regset sh_regsets[] = {
  302. /*
  303. * Format is:
  304. * PC, SR, SYSCALL,
  305. * R1 --> R63,
  306. * TR0 --> TR7,
  307. */
  308. [REGSET_GENERAL] = {
  309. .core_note_type = NT_PRSTATUS,
  310. .n = ELF_NGREG,
  311. .size = sizeof(long long),
  312. .align = sizeof(long long),
  313. .get = genregs_get,
  314. .set = genregs_set,
  315. },
  316. #ifdef CONFIG_SH_FPU
  317. [REGSET_FPU] = {
  318. .core_note_type = NT_PRFPREG,
  319. .n = sizeof(struct user_fpu_struct) /
  320. sizeof(long long),
  321. .size = sizeof(long long),
  322. .align = sizeof(long long),
  323. .get = fpregs_get,
  324. .set = fpregs_set,
  325. .active = fpregs_active,
  326. },
  327. #endif
  328. };
  329. static const struct user_regset_view user_sh64_native_view = {
  330. .name = "sh64",
  331. .e_machine = EM_SH,
  332. .regsets = sh_regsets,
  333. .n = ARRAY_SIZE(sh_regsets),
  334. };
  335. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  336. {
  337. return &user_sh64_native_view;
  338. }
  339. long arch_ptrace(struct task_struct *child, long request,
  340. unsigned long addr, unsigned long data)
  341. {
  342. int ret;
  343. unsigned long __user *datap = (unsigned long __user *) data;
  344. switch (request) {
  345. /* read the word at location addr in the USER area. */
  346. case PTRACE_PEEKUSR: {
  347. unsigned long tmp;
  348. ret = -EIO;
  349. if ((addr & 3) || addr < 0)
  350. break;
  351. if (addr < sizeof(struct pt_regs))
  352. tmp = get_stack_long(child, addr);
  353. else if ((addr >= offsetof(struct user, fpu)) &&
  354. (addr < offsetof(struct user, u_fpvalid))) {
  355. unsigned long index;
  356. ret = init_fpu(child);
  357. if (ret)
  358. break;
  359. index = addr - offsetof(struct user, fpu);
  360. tmp = get_fpu_long(child, index);
  361. } else if (addr == offsetof(struct user, u_fpvalid)) {
  362. tmp = !!tsk_used_math(child);
  363. } else {
  364. break;
  365. }
  366. ret = put_user(tmp, datap);
  367. break;
  368. }
  369. case PTRACE_POKEUSR:
  370. /* write the word at location addr in the USER area. We must
  371. disallow any changes to certain SR bits or u_fpvalid, since
  372. this could crash the kernel or result in a security
  373. loophole. */
  374. ret = -EIO;
  375. if ((addr & 3) || addr < 0)
  376. break;
  377. if (addr < sizeof(struct pt_regs)) {
  378. /* Ignore change of top 32 bits of SR */
  379. if (addr == offsetof (struct pt_regs, sr)+4)
  380. {
  381. ret = 0;
  382. break;
  383. }
  384. /* If lower 32 bits of SR, ignore non-user bits */
  385. if (addr == offsetof (struct pt_regs, sr))
  386. {
  387. long cursr = get_stack_long(child, addr);
  388. data &= ~(SR_MASK);
  389. data |= (cursr & SR_MASK);
  390. }
  391. ret = put_stack_long(child, addr, data);
  392. }
  393. else if ((addr >= offsetof(struct user, fpu)) &&
  394. (addr < offsetof(struct user, u_fpvalid))) {
  395. unsigned long index;
  396. ret = init_fpu(child);
  397. if (ret)
  398. break;
  399. index = addr - offsetof(struct user, fpu);
  400. ret = put_fpu_long(child, index, data);
  401. }
  402. break;
  403. case PTRACE_GETREGS:
  404. return copy_regset_to_user(child, &user_sh64_native_view,
  405. REGSET_GENERAL,
  406. 0, sizeof(struct pt_regs),
  407. datap);
  408. case PTRACE_SETREGS:
  409. return copy_regset_from_user(child, &user_sh64_native_view,
  410. REGSET_GENERAL,
  411. 0, sizeof(struct pt_regs),
  412. datap);
  413. #ifdef CONFIG_SH_FPU
  414. case PTRACE_GETFPREGS:
  415. return copy_regset_to_user(child, &user_sh64_native_view,
  416. REGSET_FPU,
  417. 0, sizeof(struct user_fpu_struct),
  418. datap);
  419. case PTRACE_SETFPREGS:
  420. return copy_regset_from_user(child, &user_sh64_native_view,
  421. REGSET_FPU,
  422. 0, sizeof(struct user_fpu_struct),
  423. datap);
  424. #endif
  425. default:
  426. ret = ptrace_request(child, request, addr, data);
  427. break;
  428. }
  429. return ret;
  430. }
  431. asmlinkage int sh64_ptrace(long request, long pid,
  432. unsigned long addr, unsigned long data)
  433. {
  434. #define WPC_DBRMODE 0x0d104008
  435. static unsigned long first_call;
  436. if (!test_and_set_bit(0, &first_call)) {
  437. /* Set WPC.DBRMODE to 0. This makes all debug events get
  438. * delivered through RESVEC, i.e. into the handlers in entry.S.
  439. * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
  440. * would normally be left set to 1, which makes debug events get
  441. * delivered through DBRVEC, i.e. into the remote gdb's
  442. * handlers. This prevents ptrace getting them, and confuses
  443. * the remote gdb.) */
  444. printk("DBRMODE set to 0 to permit native debugging\n");
  445. poke_real_address_q(WPC_DBRMODE, 0);
  446. }
  447. return sys_ptrace(request, pid, addr, data);
  448. }
  449. asmlinkage long long do_syscall_trace_enter(struct pt_regs *regs)
  450. {
  451. long long ret = 0;
  452. secure_computing_strict(regs->regs[9]);
  453. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  454. tracehook_report_syscall_entry(regs))
  455. /*
  456. * Tracing decided this syscall should not happen.
  457. * We'll return a bogus call number to get an ENOSYS
  458. * error, but leave the original number in regs->regs[0].
  459. */
  460. ret = -1LL;
  461. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  462. trace_sys_enter(regs, regs->regs[9]);
  463. audit_syscall_entry(regs->regs[1], regs->regs[2], regs->regs[3],
  464. regs->regs[4], regs->regs[5]);
  465. return ret ?: regs->regs[9];
  466. }
  467. asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
  468. {
  469. int step;
  470. audit_syscall_exit(regs);
  471. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  472. trace_sys_exit(regs, regs->regs[9]);
  473. step = test_thread_flag(TIF_SINGLESTEP);
  474. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  475. tracehook_report_syscall_exit(regs, step);
  476. }
  477. /* Called with interrupts disabled */
  478. asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
  479. {
  480. /* This is called after a single step exception (DEBUGSS).
  481. There is no need to change the PC, as it is a post-execution
  482. exception, as entry.S does not do anything to the PC for DEBUGSS.
  483. We need to clear the Single Step setting in SR to avoid
  484. continually stepping. */
  485. local_irq_enable();
  486. regs->sr &= ~SR_SSTEP;
  487. force_sig(SIGTRAP, current);
  488. }
  489. /* Called with interrupts disabled */
  490. BUILD_TRAP_HANDLER(breakpoint)
  491. {
  492. TRAP_HANDLER_DECL;
  493. /* We need to forward step the PC, to counteract the backstep done
  494. in signal.c. */
  495. local_irq_enable();
  496. force_sig(SIGTRAP, current);
  497. regs->pc += 4;
  498. }
  499. /*
  500. * Called by kernel/ptrace.c when detaching..
  501. *
  502. * Make sure single step bits etc are not set.
  503. */
  504. void ptrace_disable(struct task_struct *child)
  505. {
  506. user_disable_single_step(child);
  507. }