ptrace.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2012 Regents of the University of California
  4. */
  5. #ifndef _ASM_RISCV_PTRACE_H
  6. #define _ASM_RISCV_PTRACE_H
  7. #include <uapi/asm/ptrace.h>
  8. #include <asm/csr.h>
  9. #include <linux/compiler.h>
  10. #ifndef __ASSEMBLY__
  11. struct pt_regs {
  12. unsigned long epc;
  13. unsigned long ra;
  14. unsigned long sp;
  15. unsigned long gp;
  16. unsigned long tp;
  17. unsigned long t0;
  18. unsigned long t1;
  19. unsigned long t2;
  20. unsigned long s0;
  21. unsigned long s1;
  22. unsigned long a0;
  23. unsigned long a1;
  24. unsigned long a2;
  25. unsigned long a3;
  26. unsigned long a4;
  27. unsigned long a5;
  28. unsigned long a6;
  29. unsigned long a7;
  30. unsigned long s2;
  31. unsigned long s3;
  32. unsigned long s4;
  33. unsigned long s5;
  34. unsigned long s6;
  35. unsigned long s7;
  36. unsigned long s8;
  37. unsigned long s9;
  38. unsigned long s10;
  39. unsigned long s11;
  40. unsigned long t3;
  41. unsigned long t4;
  42. unsigned long t5;
  43. unsigned long t6;
  44. /* Supervisor/Machine CSRs */
  45. unsigned long status;
  46. unsigned long badaddr;
  47. unsigned long cause;
  48. /* a0 value before the syscall */
  49. unsigned long orig_a0;
  50. };
  51. #ifdef CONFIG_64BIT
  52. #define REG_FMT "%016lx"
  53. #else
  54. #define REG_FMT "%08lx"
  55. #endif
  56. #define user_mode(regs) (((regs)->status & SR_PP) == 0)
  57. #define MAX_REG_OFFSET offsetof(struct pt_regs, orig_a0)
  58. /* Helpers for working with the instruction pointer */
  59. static inline unsigned long instruction_pointer(struct pt_regs *regs)
  60. {
  61. return regs->epc;
  62. }
  63. static inline void instruction_pointer_set(struct pt_regs *regs,
  64. unsigned long val)
  65. {
  66. regs->epc = val;
  67. }
  68. #define profile_pc(regs) instruction_pointer(regs)
  69. /* Helpers for working with the user stack pointer */
  70. static inline unsigned long user_stack_pointer(struct pt_regs *regs)
  71. {
  72. return regs->sp;
  73. }
  74. static inline void user_stack_pointer_set(struct pt_regs *regs,
  75. unsigned long val)
  76. {
  77. regs->sp = val;
  78. }
  79. /* Valid only for Kernel mode traps. */
  80. static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)
  81. {
  82. return regs->sp;
  83. }
  84. /* Helpers for working with the frame pointer */
  85. static inline unsigned long frame_pointer(struct pt_regs *regs)
  86. {
  87. return regs->s0;
  88. }
  89. static inline void frame_pointer_set(struct pt_regs *regs,
  90. unsigned long val)
  91. {
  92. regs->s0 = val;
  93. }
  94. static inline unsigned long regs_return_value(struct pt_regs *regs)
  95. {
  96. return regs->a0;
  97. }
  98. static inline void regs_set_return_value(struct pt_regs *regs,
  99. unsigned long val)
  100. {
  101. regs->a0 = val;
  102. }
  103. extern int regs_query_register_offset(const char *name);
  104. extern unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
  105. unsigned int n);
  106. void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
  107. unsigned long frame_pointer);
  108. int do_syscall_trace_enter(struct pt_regs *regs);
  109. void do_syscall_trace_exit(struct pt_regs *regs);
  110. /**
  111. * regs_get_register() - get register value from its offset
  112. * @regs: pt_regs from which register value is gotten
  113. * @offset: offset of the register.
  114. *
  115. * regs_get_register returns the value of a register whose offset from @regs.
  116. * The @offset is the offset of the register in struct pt_regs.
  117. * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
  118. */
  119. static inline unsigned long regs_get_register(struct pt_regs *regs,
  120. unsigned int offset)
  121. {
  122. if (unlikely(offset > MAX_REG_OFFSET))
  123. return 0;
  124. return *(unsigned long *)((unsigned long)regs + offset);
  125. }
  126. /**
  127. * regs_get_kernel_argument() - get Nth function argument in kernel
  128. * @regs: pt_regs of that context
  129. * @n: function argument number (start from 0)
  130. *
  131. * regs_get_argument() returns @n th argument of the function call.
  132. *
  133. * Note you can get the parameter correctly if the function has no
  134. * more than eight arguments.
  135. */
  136. static inline unsigned long regs_get_kernel_argument(struct pt_regs *regs,
  137. unsigned int n)
  138. {
  139. static const int nr_reg_arguments = 8;
  140. static const unsigned int argument_offs[] = {
  141. offsetof(struct pt_regs, a0),
  142. offsetof(struct pt_regs, a1),
  143. offsetof(struct pt_regs, a2),
  144. offsetof(struct pt_regs, a3),
  145. offsetof(struct pt_regs, a4),
  146. offsetof(struct pt_regs, a5),
  147. offsetof(struct pt_regs, a6),
  148. offsetof(struct pt_regs, a7),
  149. };
  150. if (n < nr_reg_arguments)
  151. return regs_get_register(regs, argument_offs[n]);
  152. return 0;
  153. }
  154. #endif /* __ASSEMBLY__ */
  155. #endif /* _ASM_RISCV_PTRACE_H */