1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Copyright (C) 2020 Loongson Technology Co., Ltd.
4 */
5 #ifndef _ASM_PTRACE_H
6 #define _ASM_PTRACE_H
7
8 #include <asm/page.h>
9 #include <asm/irqflags.h>
10 #include <asm/thread_info.h>
11 #include <uapi/asm/ptrace.h>
12
13 /*
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.
17 */
18 struct pt_regs {
19 /* Main processor registers. */
20 unsigned long regs[32];
21
22 /* Original syscall arg0. */
23 unsigned long orig_a0;
24
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[];
34 } __aligned(8);
35
regs_irqs_disabled(struct pt_regs * regs)36 static inline int regs_irqs_disabled(struct pt_regs *regs)
37 {
38 return arch_irqs_disabled_flags(regs->csr_prmd);
39 }
40
kernel_stack_pointer(struct pt_regs * regs)41 static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)
42 {
43 return regs->regs[3];
44 }
45
46 /*
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.
49 */
50
instruction_pointer_set(struct pt_regs * regs,unsigned long val)51 static inline void instruction_pointer_set(struct pt_regs *regs,
52 unsigned long val)
53 {
54 regs->csr_era = val;
55 }
56
57 /* Query offset/name of register from its name/offset */
58 extern int regs_query_register_offset(const char *name);
59 #define MAX_REG_OFFSET (offsetof(struct pt_regs, __last))
60
61 /**
62 * regs_get_register() - get register value from its offset
63 * @regs: pt_regs from which register value is gotten.
64 * @offset: offset number of the register.
65 *
66 * regs_get_register returns the value of a register. The @offset is the
67 * offset of the register in struct pt_regs address which specified by @regs.
68 * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
69 */
regs_get_register(struct pt_regs * regs,unsigned int offset)70 static inline unsigned long regs_get_register(struct pt_regs *regs,
71 unsigned int offset)
72 {
73 if (unlikely(offset > MAX_REG_OFFSET))
74 return 0;
75
76 return *(unsigned long *)((unsigned long)regs + offset);
77 }
78
79 /**
80 * regs_within_kernel_stack() - check the address in the stack
81 * @regs: pt_regs which contains kernel stack pointer.
82 * @addr: address which is checked.
83 *
84 * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
85 * If @addr is within the kernel stack, it returns true. If not, returns false.
86 */
regs_within_kernel_stack(struct pt_regs * regs,unsigned long addr)87 static inline int regs_within_kernel_stack(struct pt_regs *regs,
88 unsigned long addr)
89 {
90 return ((addr & ~(THREAD_SIZE - 1)) ==
91 (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
92 }
93
94 /**
95 * regs_get_kernel_stack_nth() - get Nth entry of the stack
96 * @regs: pt_regs which contains kernel stack pointer.
97 * @n: stack entry number.
98 *
99 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
100 * is specified by @regs. If the @n th entry is NOT in the kernel stack,
101 * this returns 0.
102 */
regs_get_kernel_stack_nth(struct pt_regs * regs,unsigned int n)103 static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
104 unsigned int n)
105 {
106 unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
107
108 addr += n;
109 if (regs_within_kernel_stack(regs, (unsigned long)addr))
110 return *addr;
111 else
112 return 0;
113 }
114
115 struct task_struct;
116
117 /*
118 * Does the process account for user or for system time?
119 */
120 #define user_mode(regs) (((regs)->csr_prmd & PLV_MASK) == PLV_USER)
121
regs_return_value(struct pt_regs * regs)122 static inline long regs_return_value(struct pt_regs *regs)
123 {
124 return regs->regs[4];
125 }
126
127 #define instruction_pointer(regs) ((regs)->csr_era)
128 #define profile_pc(regs) instruction_pointer(regs)
129
130 extern void die(const char *, struct pt_regs *) __noreturn;
131
die_if_kernel(const char * str,struct pt_regs * regs)132 static inline void die_if_kernel(const char *str, struct pt_regs *regs)
133 {
134 if (unlikely(!user_mode(regs)))
135 die(str, regs);
136 }
137
138 #define current_pt_regs() \
139 ({ \
140 unsigned long sp = (unsigned long)__builtin_frame_address(0); \
141 (struct pt_regs *)((sp | (THREAD_SIZE - 1)) + 1) - 1; \
142 })
143
144 /* Helpers for working with the user stack pointer */
145
user_stack_pointer(struct pt_regs * regs)146 static inline unsigned long user_stack_pointer(struct pt_regs *regs)
147 {
148 return regs->regs[3];
149 }
150
user_stack_pointer_set(struct pt_regs * regs,unsigned long val)151 static inline void user_stack_pointer_set(struct pt_regs *regs,
152 unsigned long val)
153 {
154 regs->regs[3] = val;
155 }
156
157 #define arch_has_single_step() (1)
158
159 #endif /* _ASM_PTRACE_H */
160