ptrace.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2005-2017 Andes Technology Corporation
  3. #include <linux/ptrace.h>
  4. #include <linux/regset.h>
  5. #include <linux/tracehook.h>
  6. #include <linux/elf.h>
  7. #include <linux/sched/task_stack.h>
  8. enum nds32_regset {
  9. REGSET_GPR,
  10. };
  11. static int gpr_get(struct task_struct *target,
  12. const struct user_regset *regset,
  13. unsigned int pos, unsigned int count,
  14. void *kbuf, void __user * ubuf)
  15. {
  16. struct user_pt_regs *uregs = &task_pt_regs(target)->user_regs;
  17. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
  18. }
  19. static int gpr_set(struct task_struct *target, const struct user_regset *regset,
  20. unsigned int pos, unsigned int count,
  21. const void *kbuf, const void __user * ubuf)
  22. {
  23. int err;
  24. struct user_pt_regs newregs = task_pt_regs(target)->user_regs;
  25. err = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newregs, 0, -1);
  26. if (err)
  27. return err;
  28. task_pt_regs(target)->user_regs = newregs;
  29. return 0;
  30. }
  31. static const struct user_regset nds32_regsets[] = {
  32. [REGSET_GPR] = {
  33. .core_note_type = NT_PRSTATUS,
  34. .n = sizeof(struct user_pt_regs) / sizeof(u32),
  35. .size = sizeof(elf_greg_t),
  36. .align = sizeof(elf_greg_t),
  37. .get = gpr_get,
  38. .set = gpr_set}
  39. };
  40. static const struct user_regset_view nds32_user_view = {
  41. .name = "nds32",
  42. .e_machine = EM_NDS32,
  43. .regsets = nds32_regsets,
  44. .n = ARRAY_SIZE(nds32_regsets)
  45. };
  46. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  47. {
  48. return &nds32_user_view;
  49. }
  50. void ptrace_disable(struct task_struct *child)
  51. {
  52. user_disable_single_step(child);
  53. }
  54. /* do_ptrace()
  55. *
  56. * Provide ptrace defined service.
  57. */
  58. long arch_ptrace(struct task_struct *child, long request, unsigned long addr,
  59. unsigned long data)
  60. {
  61. int ret = -EIO;
  62. switch (request) {
  63. default:
  64. ret = ptrace_request(child, request, addr, data);
  65. break;
  66. }
  67. return ret;
  68. }
  69. void user_enable_single_step(struct task_struct *child)
  70. {
  71. struct pt_regs *regs;
  72. regs = task_pt_regs(child);
  73. regs->ipsw |= PSW_mskHSS;
  74. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  75. }
  76. void user_disable_single_step(struct task_struct *child)
  77. {
  78. struct pt_regs *regs;
  79. regs = task_pt_regs(child);
  80. regs->ipsw &= ~PSW_mskHSS;
  81. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  82. }
  83. /* sys_trace()
  84. *
  85. * syscall trace handler.
  86. */
  87. asmlinkage int syscall_trace_enter(struct pt_regs *regs)
  88. {
  89. if (test_thread_flag(TIF_SYSCALL_TRACE)) {
  90. if (tracehook_report_syscall_entry(regs))
  91. forget_syscall(regs);
  92. }
  93. return regs->syscallno;
  94. }
  95. asmlinkage void syscall_trace_leave(struct pt_regs *regs)
  96. {
  97. int step = test_thread_flag(TIF_SINGLESTEP);
  98. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  99. tracehook_report_syscall_exit(regs, step);
  100. }