ptrace.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. * Copyright 2015 Regents of the University of California
  4. * Copyright 2017 SiFive
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation, version 2.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * Copied from arch/tile/kernel/ptrace.c
  16. */
  17. #include <asm/ptrace.h>
  18. #include <asm/syscall.h>
  19. #include <asm/thread_info.h>
  20. #include <linux/ptrace.h>
  21. #include <linux/elf.h>
  22. #include <linux/regset.h>
  23. #include <linux/sched.h>
  24. #include <linux/sched/task_stack.h>
  25. #include <linux/tracehook.h>
  26. #include <trace/events/syscalls.h>
  27. enum riscv_regset {
  28. REGSET_X,
  29. };
  30. static int riscv_gpr_get(struct task_struct *target,
  31. const struct user_regset *regset,
  32. unsigned int pos, unsigned int count,
  33. void *kbuf, void __user *ubuf)
  34. {
  35. struct pt_regs *regs;
  36. regs = task_pt_regs(target);
  37. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
  38. }
  39. static int riscv_gpr_set(struct task_struct *target,
  40. const struct user_regset *regset,
  41. unsigned int pos, unsigned int count,
  42. const void *kbuf, const void __user *ubuf)
  43. {
  44. int ret;
  45. struct pt_regs *regs;
  46. regs = task_pt_regs(target);
  47. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
  48. return ret;
  49. }
  50. static const struct user_regset riscv_user_regset[] = {
  51. [REGSET_X] = {
  52. .core_note_type = NT_PRSTATUS,
  53. .n = ELF_NGREG,
  54. .size = sizeof(elf_greg_t),
  55. .align = sizeof(elf_greg_t),
  56. .get = &riscv_gpr_get,
  57. .set = &riscv_gpr_set,
  58. },
  59. };
  60. static const struct user_regset_view riscv_user_native_view = {
  61. .name = "riscv",
  62. .e_machine = EM_RISCV,
  63. .regsets = riscv_user_regset,
  64. .n = ARRAY_SIZE(riscv_user_regset),
  65. };
  66. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  67. {
  68. return &riscv_user_native_view;
  69. }
  70. void ptrace_disable(struct task_struct *child)
  71. {
  72. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  73. }
  74. long arch_ptrace(struct task_struct *child, long request,
  75. unsigned long addr, unsigned long data)
  76. {
  77. long ret = -EIO;
  78. switch (request) {
  79. default:
  80. ret = ptrace_request(child, request, addr, data);
  81. break;
  82. }
  83. return ret;
  84. }
  85. /*
  86. * Allows PTRACE_SYSCALL to work. These are called from entry.S in
  87. * {handle,ret_from}_syscall.
  88. */
  89. void do_syscall_trace_enter(struct pt_regs *regs)
  90. {
  91. if (test_thread_flag(TIF_SYSCALL_TRACE))
  92. if (tracehook_report_syscall_entry(regs))
  93. syscall_set_nr(current, regs, -1);
  94. #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
  95. if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
  96. trace_sys_enter(regs, syscall_get_nr(current, regs));
  97. #endif
  98. }
  99. void do_syscall_trace_exit(struct pt_regs *regs)
  100. {
  101. if (test_thread_flag(TIF_SYSCALL_TRACE))
  102. tracehook_report_syscall_exit(regs, 0);
  103. #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
  104. if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
  105. trace_sys_exit(regs, regs_return_value(regs));
  106. #endif
  107. }