ptrace.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (C) 2014 Altera Corporation
  3. * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
  4. *
  5. * This file is subject to the terms and conditions of the GNU General
  6. * Public License. See the file COPYING in the main directory of this
  7. * archive for more details.
  8. */
  9. #include <linux/elf.h>
  10. #include <linux/errno.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mm.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/regset.h>
  15. #include <linux/sched.h>
  16. #include <linux/sched/task_stack.h>
  17. #include <linux/tracehook.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/user.h>
  20. static int genregs_get(struct task_struct *target,
  21. const struct user_regset *regset,
  22. struct membuf to)
  23. {
  24. const struct pt_regs *regs = task_pt_regs(target);
  25. const struct switch_stack *sw = (struct switch_stack *)regs - 1;
  26. membuf_zero(&to, 4); // R0
  27. membuf_write(&to, &regs->r1, 7 * 4); // R1..R7
  28. membuf_write(&to, &regs->r8, 8 * 4); // R8..R15
  29. membuf_write(&to, sw, 8 * 4); // R16..R23
  30. membuf_zero(&to, 2 * 4); /* et and bt */
  31. membuf_store(&to, regs->gp);
  32. membuf_store(&to, regs->sp);
  33. membuf_store(&to, regs->fp);
  34. membuf_store(&to, regs->ea);
  35. membuf_zero(&to, 4); // PTR_BA
  36. membuf_store(&to, regs->ra);
  37. membuf_store(&to, regs->ea); /* use ea for PC */
  38. return membuf_zero(&to, (NUM_PTRACE_REG - PTR_PC) * 4);
  39. }
  40. /*
  41. * Set the thread state from a regset passed in via ptrace
  42. */
  43. static int genregs_set(struct task_struct *target,
  44. const struct user_regset *regset,
  45. unsigned int pos, unsigned int count,
  46. const void *kbuf, const void __user *ubuf)
  47. {
  48. struct pt_regs *regs = task_pt_regs(target);
  49. const struct switch_stack *sw = (struct switch_stack *)regs - 1;
  50. int ret = 0;
  51. #define REG_IGNORE_RANGE(START, END) \
  52. if (!ret) \
  53. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, \
  54. START * 4, (END * 4) + 4);
  55. #define REG_IN_ONE(PTR, LOC) \
  56. if (!ret) \
  57. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, \
  58. (void *)(PTR), LOC * 4, (LOC * 4) + 4);
  59. #define REG_IN_RANGE(PTR, START, END) \
  60. if (!ret) \
  61. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, \
  62. (void *)(PTR), START * 4, (END * 4) + 4);
  63. REG_IGNORE_RANGE(PTR_R0, PTR_R0);
  64. REG_IN_RANGE(&regs->r1, PTR_R1, PTR_R7);
  65. REG_IN_RANGE(&regs->r8, PTR_R8, PTR_R15);
  66. REG_IN_RANGE(sw, PTR_R16, PTR_R23);
  67. REG_IGNORE_RANGE(PTR_R24, PTR_R25); /* et and bt */
  68. REG_IN_ONE(&regs->gp, PTR_GP);
  69. REG_IN_ONE(&regs->sp, PTR_SP);
  70. REG_IN_ONE(&regs->fp, PTR_FP);
  71. REG_IN_ONE(&regs->ea, PTR_EA);
  72. REG_IGNORE_RANGE(PTR_BA, PTR_BA);
  73. REG_IN_ONE(&regs->ra, PTR_RA);
  74. REG_IN_ONE(&regs->ea, PTR_PC); /* use ea for PC */
  75. if (!ret)
  76. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  77. PTR_STATUS * 4, -1);
  78. return ret;
  79. }
  80. /*
  81. * Define the register sets available on Nios2 under Linux
  82. */
  83. enum nios2_regset {
  84. REGSET_GENERAL,
  85. };
  86. static const struct user_regset nios2_regsets[] = {
  87. [REGSET_GENERAL] = {
  88. .core_note_type = NT_PRSTATUS,
  89. .n = NUM_PTRACE_REG,
  90. .size = sizeof(unsigned long),
  91. .align = sizeof(unsigned long),
  92. .regset_get = genregs_get,
  93. .set = genregs_set,
  94. }
  95. };
  96. static const struct user_regset_view nios2_user_view = {
  97. .name = "nios2",
  98. .e_machine = ELF_ARCH,
  99. .ei_osabi = ELF_OSABI,
  100. .regsets = nios2_regsets,
  101. .n = ARRAY_SIZE(nios2_regsets)
  102. };
  103. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  104. {
  105. return &nios2_user_view;
  106. }
  107. void ptrace_disable(struct task_struct *child)
  108. {
  109. }
  110. long arch_ptrace(struct task_struct *child, long request, unsigned long addr,
  111. unsigned long data)
  112. {
  113. return ptrace_request(child, request, addr, data);
  114. }
  115. asmlinkage int do_syscall_trace_enter(void)
  116. {
  117. int ret = 0;
  118. if (test_thread_flag(TIF_SYSCALL_TRACE))
  119. ret = tracehook_report_syscall_entry(task_pt_regs(current));
  120. return ret;
  121. }
  122. asmlinkage void do_syscall_trace_exit(void)
  123. {
  124. if (test_thread_flag(TIF_SYSCALL_TRACE))
  125. tracehook_report_syscall_exit(task_pt_regs(current), 0);
  126. }