1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
9 #include <asm/irqflags.h>
10 #include <asm/thread_info.h>
11 #include <uapi/asm/ptrace.h>
14 * This struct defines the way the registers are stored on the stack during
15 * a system call/exception. If you add a register here, please also add it to
16 * regoffset_table[] in arch/loongarch/kernel/ptrace.c.
19 /* Main processor registers. */
20 unsigned long regs
[32];
22 /* Original syscall arg0. */
23 unsigned long orig_a0
;
25 /* Special CSR registers. */
26 unsigned long csr_era
;
27 unsigned long csr_badvaddr
;
28 unsigned long csr_crmd
;
29 unsigned long csr_prmd
;
30 unsigned long csr_euen
;
31 unsigned long csr_ecfg
;
32 unsigned long csr_estat
;
33 unsigned long __last
[];
36 static inline int regs_irqs_disabled(struct pt_regs
*regs
)
38 return arch_irqs_disabled_flags(regs
->csr_prmd
);
41 static inline unsigned long kernel_stack_pointer(struct pt_regs
*regs
)
47 * Don't use asm-generic/ptrace.h it defines FP accessors that don't make
48 * sense on LoongArch. We rather want an error if they get invoked.
51 static inline void instruction_pointer_set(struct pt_regs
*regs
, unsigned long val
)
56 /* Query offset/name of register from its name/offset */
57 extern int regs_query_register_offset(const char *name
);
58 #define MAX_REG_OFFSET (offsetof(struct pt_regs, __last))
61 * regs_get_register() - get register value from its offset
62 * @regs: pt_regs from which register value is gotten.
63 * @offset: offset number of the register.
65 * regs_get_register returns the value of a register. The @offset is the
66 * offset of the register in struct pt_regs address which specified by @regs.
67 * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
69 static inline unsigned long regs_get_register(struct pt_regs
*regs
, unsigned int offset
)
71 if (unlikely(offset
> MAX_REG_OFFSET
))
74 return *(unsigned long *)((unsigned long)regs
+ offset
);
78 * regs_within_kernel_stack() - check the address in the stack
79 * @regs: pt_regs which contains kernel stack pointer.
80 * @addr: address which is checked.
82 * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
83 * If @addr is within the kernel stack, it returns true. If not, returns false.
85 static inline int regs_within_kernel_stack(struct pt_regs
*regs
, unsigned long addr
)
87 return ((addr
& ~(THREAD_SIZE
- 1)) ==
88 (kernel_stack_pointer(regs
) & ~(THREAD_SIZE
- 1)));
92 * regs_get_kernel_stack_nth() - get Nth entry of the stack
93 * @regs: pt_regs which contains kernel stack pointer.
94 * @n: stack entry number.
96 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
97 * is specified by @regs. If the @n th entry is NOT in the kernel stack,
100 static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs
*regs
, unsigned int n
)
102 unsigned long *addr
= (unsigned long *)kernel_stack_pointer(regs
);
105 if (regs_within_kernel_stack(regs
, (unsigned long)addr
))
114 * regs_get_kernel_argument() - get Nth function argument in kernel
115 * @regs: pt_regs of that context
116 * @n: function argument number (start from 0)
118 * regs_get_argument() returns @n th argument of the function call.
119 * Note that this chooses most probably assignment, in some case
120 * it can be incorrect.
121 * This is expected to be called from kprobes or ftrace with regs
122 * where the top of stack is the return address.
124 static inline unsigned long regs_get_kernel_argument(struct pt_regs
*regs
,
127 #define NR_REG_ARGUMENTS 8
128 static const unsigned int args
[] = {
129 offsetof(struct pt_regs
, regs
[4]),
130 offsetof(struct pt_regs
, regs
[5]),
131 offsetof(struct pt_regs
, regs
[6]),
132 offsetof(struct pt_regs
, regs
[7]),
133 offsetof(struct pt_regs
, regs
[8]),
134 offsetof(struct pt_regs
, regs
[9]),
135 offsetof(struct pt_regs
, regs
[10]),
136 offsetof(struct pt_regs
, regs
[11]),
139 if (n
< NR_REG_ARGUMENTS
)
140 return regs_get_register(regs
, args
[n
]);
142 n
-= NR_REG_ARGUMENTS
;
143 return regs_get_kernel_stack_nth(regs
, n
);
148 * Does the process account for user or for system time?
150 #define user_mode(regs) (((regs)->csr_prmd & PLV_MASK) == PLV_USER)
152 static inline long regs_return_value(struct pt_regs
*regs
)
154 return regs
->regs
[4];
157 static inline void regs_set_return_value(struct pt_regs
*regs
, unsigned long val
)
162 #define instruction_pointer(regs) ((regs)->csr_era)
163 #define profile_pc(regs) instruction_pointer(regs)
165 extern void die(const char *str
, struct pt_regs
*regs
);
167 static inline void die_if_kernel(const char *str
, struct pt_regs
*regs
)
169 if (unlikely(!user_mode(regs
)))
173 #define current_pt_regs() \
175 unsigned long sp = (unsigned long)__builtin_frame_address(0); \
176 (struct pt_regs *)((sp | (THREAD_SIZE - 1)) + 1) - 1; \
179 /* Helpers for working with the user stack pointer */
181 static inline unsigned long user_stack_pointer(struct pt_regs
*regs
)
183 return regs
->regs
[3];
186 static inline void user_stack_pointer_set(struct pt_regs
*regs
,
192 #ifdef CONFIG_HAVE_HW_BREAKPOINT
193 #define arch_has_single_step() (1)
196 #endif /* _ASM_PTRACE_H */