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 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 36 static inline int regs_irqs_disabled(struct pt_regs *regs) 37 { 38 return arch_irqs_disabled_flags(regs->csr_prmd); 39 } 40 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 51 static inline void instruction_pointer_set(struct pt_regs *regs, unsigned long val) 52 { 53 regs->csr_era = val; 54 } 55 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)) 59 60 /** 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. 64 * 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. 68 */ 69 static inline unsigned long regs_get_register(struct pt_regs *regs, unsigned int offset) 70 { 71 if (unlikely(offset > MAX_REG_OFFSET)) 72 return 0; 73 74 return *(unsigned long *)((unsigned long)regs + offset); 75 } 76 77 /** 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. 81 * 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. 84 */ 85 static inline int regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr) 86 { 87 return ((addr & ~(THREAD_SIZE - 1)) == 88 (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1))); 89 } 90 91 /** 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. 95 * 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, 98 * this returns 0. 99 */ 100 static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n) 101 { 102 unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs); 103 104 addr += n; 105 if (regs_within_kernel_stack(regs, (unsigned long)addr)) 106 return *addr; 107 else 108 return 0; 109 } 110 111 struct task_struct; 112 113 /** 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) 117 * 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. 123 */ 124 static inline unsigned long regs_get_kernel_argument(struct pt_regs *regs, 125 unsigned int n) 126 { 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]), 137 }; 138 139 if (n < NR_REG_ARGUMENTS) 140 return regs_get_register(regs, args[n]); 141 else { 142 n -= NR_REG_ARGUMENTS; 143 return regs_get_kernel_stack_nth(regs, n); 144 } 145 } 146 147 /* 148 * Does the process account for user or for system time? 149 */ 150 #define user_mode(regs) (((regs)->csr_prmd & PLV_MASK) == PLV_USER) 151 152 static inline long regs_return_value(struct pt_regs *regs) 153 { 154 return regs->regs[4]; 155 } 156 157 static inline void regs_set_return_value(struct pt_regs *regs, unsigned long val) 158 { 159 regs->regs[4] = val; 160 } 161 162 #define instruction_pointer(regs) ((regs)->csr_era) 163 #define profile_pc(regs) instruction_pointer(regs) 164 165 extern void die(const char *str, struct pt_regs *regs); 166 167 static inline void die_if_kernel(const char *str, struct pt_regs *regs) 168 { 169 if (unlikely(!user_mode(regs))) 170 die(str, regs); 171 } 172 173 #define current_pt_regs() \ 174 ({ \ 175 unsigned long sp = (unsigned long)__builtin_frame_address(0); \ 176 (struct pt_regs *)((sp | (THREAD_SIZE - 1)) + 1) - 1; \ 177 }) 178 179 /* Helpers for working with the user stack pointer */ 180 181 static inline unsigned long user_stack_pointer(struct pt_regs *regs) 182 { 183 return regs->regs[3]; 184 } 185 186 static inline void user_stack_pointer_set(struct pt_regs *regs, 187 unsigned long val) 188 { 189 regs->regs[3] = val; 190 } 191 192 #ifdef CONFIG_HAVE_HW_BREAKPOINT 193 #define arch_has_single_step() (1) 194 #endif 195 196 #endif /* _ASM_PTRACE_H */ 197