ptrace.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  4. */
  5. #ifndef _ASM_PTRACE_H
  6. #define _ASM_PTRACE_H
  7. #include <asm/page.h>
  8. #include <asm/irqflags.h>
  9. #include <asm/thread_info.h>
  10. #include <uapi/asm/ptrace.h>
  11. /*
  12. * This struct defines the way the registers are stored on the stack during
  13. * a system call/exception. If you add a register here, please also add it to
  14. * regoffset_table[] in arch/loongarch/kernel/ptrace.c.
  15. */
  16. struct pt_regs {
  17. /* Main processor registers. */
  18. unsigned long regs[32];
  19. /* Original syscall arg0. */
  20. unsigned long orig_a0;
  21. /* Special CSR registers. */
  22. unsigned long csr_era;
  23. unsigned long csr_badvaddr;
  24. unsigned long csr_crmd;
  25. unsigned long csr_prmd;
  26. unsigned long csr_euen;
  27. unsigned long csr_ecfg;
  28. unsigned long csr_estat;
  29. unsigned long __last[];
  30. } __aligned(8);
  31. static __always_inline bool regs_irqs_disabled(struct pt_regs *regs)
  32. {
  33. return !(regs->csr_prmd & CSR_PRMD_PIE);
  34. }
  35. static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)
  36. {
  37. return regs->regs[3];
  38. }
  39. /*
  40. * Don't use asm-generic/ptrace.h it defines FP accessors that don't make
  41. * sense on LoongArch. We rather want an error if they get invoked.
  42. */
  43. static inline void instruction_pointer_set(struct pt_regs *regs, unsigned long val)
  44. {
  45. regs->csr_era = val;
  46. }
  47. /* Query offset/name of register from its name/offset */
  48. extern int regs_query_register_offset(const char *name);
  49. #define MAX_REG_OFFSET (offsetof(struct pt_regs, __last) - sizeof(unsigned long))
  50. /**
  51. * regs_get_register() - get register value from its offset
  52. * @regs: pt_regs from which register value is gotten.
  53. * @offset: offset number of the register.
  54. *
  55. * regs_get_register returns the value of a register. The @offset is the
  56. * offset of the register in struct pt_regs address which specified by @regs.
  57. * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
  58. */
  59. static inline unsigned long regs_get_register(struct pt_regs *regs, unsigned int offset)
  60. {
  61. if (unlikely(offset > MAX_REG_OFFSET))
  62. return 0;
  63. return *(unsigned long *)((unsigned long)regs + offset);
  64. }
  65. /**
  66. * regs_within_kernel_stack() - check the address in the stack
  67. * @regs: pt_regs which contains kernel stack pointer.
  68. * @addr: address which is checked.
  69. *
  70. * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
  71. * If @addr is within the kernel stack, it returns true. If not, returns false.
  72. */
  73. static inline int regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
  74. {
  75. return ((addr & ~(THREAD_SIZE - 1)) ==
  76. (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
  77. }
  78. /**
  79. * regs_get_kernel_stack_nth() - get Nth entry of the stack
  80. * @regs: pt_regs which contains kernel stack pointer.
  81. * @n: stack entry number.
  82. *
  83. * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
  84. * is specified by @regs. If the @n th entry is NOT in the kernel stack,
  85. * this returns 0.
  86. */
  87. static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
  88. {
  89. unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
  90. addr += n;
  91. if (regs_within_kernel_stack(regs, (unsigned long)addr))
  92. return *addr;
  93. else
  94. return 0;
  95. }
  96. struct task_struct;
  97. /**
  98. * regs_get_kernel_argument() - get Nth function argument in kernel
  99. * @regs: pt_regs of that context
  100. * @n: function argument number (start from 0)
  101. *
  102. * regs_get_argument() returns @n th argument of the function call.
  103. * Note that this chooses most probably assignment, in some case
  104. * it can be incorrect.
  105. * This is expected to be called from kprobes or ftrace with regs
  106. * where the top of stack is the return address.
  107. */
  108. static inline unsigned long regs_get_kernel_argument(struct pt_regs *regs,
  109. unsigned int n)
  110. {
  111. #define NR_REG_ARGUMENTS 8
  112. static const unsigned int args[] = {
  113. offsetof(struct pt_regs, regs[4]),
  114. offsetof(struct pt_regs, regs[5]),
  115. offsetof(struct pt_regs, regs[6]),
  116. offsetof(struct pt_regs, regs[7]),
  117. offsetof(struct pt_regs, regs[8]),
  118. offsetof(struct pt_regs, regs[9]),
  119. offsetof(struct pt_regs, regs[10]),
  120. offsetof(struct pt_regs, regs[11]),
  121. };
  122. if (n < NR_REG_ARGUMENTS)
  123. return regs_get_register(regs, args[n]);
  124. else {
  125. n -= NR_REG_ARGUMENTS;
  126. return regs_get_kernel_stack_nth(regs, n);
  127. }
  128. }
  129. /*
  130. * Does the process account for user or for system time?
  131. */
  132. #define user_mode(regs) (((regs)->csr_prmd & PLV_MASK) == PLV_USER)
  133. static inline long regs_return_value(struct pt_regs *regs)
  134. {
  135. return regs->regs[4];
  136. }
  137. static inline void regs_set_return_value(struct pt_regs *regs, unsigned long val)
  138. {
  139. regs->regs[4] = val;
  140. }
  141. #define instruction_pointer(regs) ((regs)->csr_era)
  142. #define profile_pc(regs) instruction_pointer(regs)
  143. extern void die(const char *str, struct pt_regs *regs);
  144. static inline void die_if_kernel(const char *str, struct pt_regs *regs)
  145. {
  146. if (unlikely(!user_mode(regs)))
  147. die(str, regs);
  148. }
  149. #define current_pt_regs() \
  150. ({ \
  151. unsigned long sp = (unsigned long)__builtin_frame_address(0); \
  152. (struct pt_regs *)((sp | (THREAD_SIZE - 1)) + 1) - 1; \
  153. })
  154. /* Helpers for working with the user stack pointer */
  155. static inline unsigned long user_stack_pointer(struct pt_regs *regs)
  156. {
  157. return regs->regs[3];
  158. }
  159. static inline void user_stack_pointer_set(struct pt_regs *regs,
  160. unsigned long val)
  161. {
  162. regs->regs[3] = val;
  163. }
  164. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  165. #define arch_has_single_step() (1)
  166. #endif
  167. #endif /* _ASM_PTRACE_H */