ptrace.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/thread_info.h>
  9. #include <uapi/asm/ptrace.h>
  10. /*
  11. * This struct defines the way the registers are stored on the stack during
  12. * a system call/exception. If you add a register here, please also add it to
  13. * regoffset_table[] in arch/loongarch/kernel/ptrace.c.
  14. */
  15. struct pt_regs {
  16. /* Main processor registers. */
  17. unsigned long regs[32];
  18. /* Original syscall arg0. */
  19. unsigned long orig_a0;
  20. /* Special CSR registers. */
  21. unsigned long csr_era;
  22. unsigned long csr_badvaddr;
  23. unsigned long csr_crmd;
  24. unsigned long csr_prmd;
  25. unsigned long csr_euen;
  26. unsigned long csr_ecfg;
  27. unsigned long csr_estat;
  28. unsigned long __last[];
  29. } __aligned(8);
  30. static inline int regs_irqs_disabled(struct pt_regs *regs)
  31. {
  32. return arch_irqs_disabled_flags(regs->csr_prmd);
  33. }
  34. static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)
  35. {
  36. return regs->regs[3];
  37. }
  38. /*
  39. * Don't use asm-generic/ptrace.h it defines FP accessors that don't make
  40. * sense on LoongArch. We rather want an error if they get invoked.
  41. */
  42. static inline void instruction_pointer_set(struct pt_regs *regs, unsigned long val)
  43. {
  44. regs->csr_era = val;
  45. }
  46. /* Query offset/name of register from its name/offset */
  47. extern int regs_query_register_offset(const char *name);
  48. #define MAX_REG_OFFSET (offsetof(struct pt_regs, __last))
  49. /**
  50. * regs_get_register() - get register value from its offset
  51. * @regs: pt_regs from which register value is gotten.
  52. * @offset: offset number of the register.
  53. *
  54. * regs_get_register returns the value of a register. The @offset is the
  55. * offset of the register in struct pt_regs address which specified by @regs.
  56. * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
  57. */
  58. static inline unsigned long regs_get_register(struct pt_regs *regs, unsigned int offset)
  59. {
  60. if (unlikely(offset > MAX_REG_OFFSET))
  61. return 0;
  62. return *(unsigned long *)((unsigned long)regs + offset);
  63. }
  64. /**
  65. * regs_within_kernel_stack() - check the address in the stack
  66. * @regs: pt_regs which contains kernel stack pointer.
  67. * @addr: address which is checked.
  68. *
  69. * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
  70. * If @addr is within the kernel stack, it returns true. If not, returns false.
  71. */
  72. static inline int regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
  73. {
  74. return ((addr & ~(THREAD_SIZE - 1)) ==
  75. (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
  76. }
  77. /**
  78. * regs_get_kernel_stack_nth() - get Nth entry of the stack
  79. * @regs: pt_regs which contains kernel stack pointer.
  80. * @n: stack entry number.
  81. *
  82. * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
  83. * is specified by @regs. If the @n th entry is NOT in the kernel stack,
  84. * this returns 0.
  85. */
  86. static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
  87. {
  88. unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
  89. addr += n;
  90. if (regs_within_kernel_stack(regs, (unsigned long)addr))
  91. return *addr;
  92. else
  93. return 0;
  94. }
  95. struct task_struct;
  96. /*
  97. * Does the process account for user or for system time?
  98. */
  99. #define user_mode(regs) (((regs)->csr_prmd & PLV_MASK) == PLV_USER)
  100. static inline long regs_return_value(struct pt_regs *regs)
  101. {
  102. return regs->regs[4];
  103. }
  104. #define instruction_pointer(regs) ((regs)->csr_era)
  105. #define profile_pc(regs) instruction_pointer(regs)
  106. extern void die(const char *, struct pt_regs *) __noreturn;
  107. static inline void die_if_kernel(const char *str, struct pt_regs *regs)
  108. {
  109. if (unlikely(!user_mode(regs)))
  110. die(str, regs);
  111. }
  112. #define current_pt_regs() \
  113. ({ \
  114. unsigned long sp = (unsigned long)__builtin_frame_address(0); \
  115. (struct pt_regs *)((sp | (THREAD_SIZE - 1)) + 1) - 1; \
  116. })
  117. /* Helpers for working with the user stack pointer */
  118. static inline unsigned long user_stack_pointer(struct pt_regs *regs)
  119. {
  120. return regs->regs[3];
  121. }
  122. static inline void user_stack_pointer_set(struct pt_regs *regs,
  123. unsigned long val)
  124. {
  125. regs->regs[3] = val;
  126. }
  127. #endif /* _ASM_PTRACE_H */