1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright 2010 Tilera Corporation. All Rights Reserved. 4 * Copyright 2015 Regents of the University of California 5 * Copyright 2017 SiFive 6 * 7 * Copied from arch/tile/kernel/ptrace.c 8 */ 9 10 #include <asm/ptrace.h> 11 #include <asm/syscall.h> 12 #include <asm/thread_info.h> 13 #include <asm/switch_to.h> 14 #include <linux/audit.h> 15 #include <linux/ptrace.h> 16 #include <linux/elf.h> 17 #include <linux/regset.h> 18 #include <linux/sched.h> 19 #include <linux/sched/task_stack.h> 20 21 #define CREATE_TRACE_POINTS 22 #include <trace/events/syscalls.h> 23 24 enum riscv_regset { 25 REGSET_X, 26 #ifdef CONFIG_FPU 27 REGSET_F, 28 #endif 29 }; 30 31 static int riscv_gpr_get(struct task_struct *target, 32 const struct user_regset *regset, 33 struct membuf to) 34 { 35 return membuf_write(&to, task_pt_regs(target), 36 sizeof(struct user_regs_struct)); 37 } 38 39 static int riscv_gpr_set(struct task_struct *target, 40 const struct user_regset *regset, 41 unsigned int pos, unsigned int count, 42 const void *kbuf, const void __user *ubuf) 43 { 44 struct pt_regs *regs; 45 46 regs = task_pt_regs(target); 47 return user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1); 48 } 49 50 #ifdef CONFIG_FPU 51 static int riscv_fpr_get(struct task_struct *target, 52 const struct user_regset *regset, 53 struct membuf to) 54 { 55 struct __riscv_d_ext_state *fstate = &target->thread.fstate; 56 57 if (target == current) 58 fstate_save(current, task_pt_regs(current)); 59 60 membuf_write(&to, fstate, offsetof(struct __riscv_d_ext_state, fcsr)); 61 membuf_store(&to, fstate->fcsr); 62 return membuf_zero(&to, 4); // explicitly pad 63 } 64 65 static int riscv_fpr_set(struct task_struct *target, 66 const struct user_regset *regset, 67 unsigned int pos, unsigned int count, 68 const void *kbuf, const void __user *ubuf) 69 { 70 int ret; 71 struct __riscv_d_ext_state *fstate = &target->thread.fstate; 72 73 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0, 74 offsetof(struct __riscv_d_ext_state, fcsr)); 75 if (!ret) { 76 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0, 77 offsetof(struct __riscv_d_ext_state, fcsr) + 78 sizeof(fstate->fcsr)); 79 } 80 81 return ret; 82 } 83 #endif 84 85 static const struct user_regset riscv_user_regset[] = { 86 [REGSET_X] = { 87 .core_note_type = NT_PRSTATUS, 88 .n = ELF_NGREG, 89 .size = sizeof(elf_greg_t), 90 .align = sizeof(elf_greg_t), 91 .regset_get = riscv_gpr_get, 92 .set = riscv_gpr_set, 93 }, 94 #ifdef CONFIG_FPU 95 [REGSET_F] = { 96 .core_note_type = NT_PRFPREG, 97 .n = ELF_NFPREG, 98 .size = sizeof(elf_fpreg_t), 99 .align = sizeof(elf_fpreg_t), 100 .regset_get = riscv_fpr_get, 101 .set = riscv_fpr_set, 102 }, 103 #endif 104 }; 105 106 static const struct user_regset_view riscv_user_native_view = { 107 .name = "riscv", 108 .e_machine = EM_RISCV, 109 .regsets = riscv_user_regset, 110 .n = ARRAY_SIZE(riscv_user_regset), 111 }; 112 113 const struct user_regset_view *task_user_regset_view(struct task_struct *task) 114 { 115 return &riscv_user_native_view; 116 } 117 118 struct pt_regs_offset { 119 const char *name; 120 int offset; 121 }; 122 123 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)} 124 #define REG_OFFSET_END {.name = NULL, .offset = 0} 125 126 static const struct pt_regs_offset regoffset_table[] = { 127 REG_OFFSET_NAME(epc), 128 REG_OFFSET_NAME(ra), 129 REG_OFFSET_NAME(sp), 130 REG_OFFSET_NAME(gp), 131 REG_OFFSET_NAME(tp), 132 REG_OFFSET_NAME(t0), 133 REG_OFFSET_NAME(t1), 134 REG_OFFSET_NAME(t2), 135 REG_OFFSET_NAME(s0), 136 REG_OFFSET_NAME(s1), 137 REG_OFFSET_NAME(a0), 138 REG_OFFSET_NAME(a1), 139 REG_OFFSET_NAME(a2), 140 REG_OFFSET_NAME(a3), 141 REG_OFFSET_NAME(a4), 142 REG_OFFSET_NAME(a5), 143 REG_OFFSET_NAME(a6), 144 REG_OFFSET_NAME(a7), 145 REG_OFFSET_NAME(s2), 146 REG_OFFSET_NAME(s3), 147 REG_OFFSET_NAME(s4), 148 REG_OFFSET_NAME(s5), 149 REG_OFFSET_NAME(s6), 150 REG_OFFSET_NAME(s7), 151 REG_OFFSET_NAME(s8), 152 REG_OFFSET_NAME(s9), 153 REG_OFFSET_NAME(s10), 154 REG_OFFSET_NAME(s11), 155 REG_OFFSET_NAME(t3), 156 REG_OFFSET_NAME(t4), 157 REG_OFFSET_NAME(t5), 158 REG_OFFSET_NAME(t6), 159 REG_OFFSET_NAME(status), 160 REG_OFFSET_NAME(badaddr), 161 REG_OFFSET_NAME(cause), 162 REG_OFFSET_NAME(orig_a0), 163 REG_OFFSET_END, 164 }; 165 166 /** 167 * regs_query_register_offset() - query register offset from its name 168 * @name: the name of a register 169 * 170 * regs_query_register_offset() returns the offset of a register in struct 171 * pt_regs from its name. If the name is invalid, this returns -EINVAL; 172 */ 173 int regs_query_register_offset(const char *name) 174 { 175 const struct pt_regs_offset *roff; 176 177 for (roff = regoffset_table; roff->name != NULL; roff++) 178 if (!strcmp(roff->name, name)) 179 return roff->offset; 180 return -EINVAL; 181 } 182 183 /** 184 * regs_within_kernel_stack() - check the address in the stack 185 * @regs: pt_regs which contains kernel stack pointer. 186 * @addr: address which is checked. 187 * 188 * regs_within_kernel_stack() checks @addr is within the kernel stack page(s). 189 * If @addr is within the kernel stack, it returns true. If not, returns false. 190 */ 191 static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr) 192 { 193 return (addr & ~(THREAD_SIZE - 1)) == 194 (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)); 195 } 196 197 /** 198 * regs_get_kernel_stack_nth() - get Nth entry of the stack 199 * @regs: pt_regs which contains kernel stack pointer. 200 * @n: stack entry number. 201 * 202 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which 203 * is specified by @regs. If the @n th entry is NOT in the kernel stack, 204 * this returns 0. 205 */ 206 unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n) 207 { 208 unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs); 209 210 addr += n; 211 if (regs_within_kernel_stack(regs, (unsigned long)addr)) 212 return *addr; 213 else 214 return 0; 215 } 216 217 void ptrace_disable(struct task_struct *child) 218 { 219 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); 220 } 221 222 long arch_ptrace(struct task_struct *child, long request, 223 unsigned long addr, unsigned long data) 224 { 225 long ret = -EIO; 226 227 switch (request) { 228 default: 229 ret = ptrace_request(child, request, addr, data); 230 break; 231 } 232 233 return ret; 234 } 235 236 /* 237 * Allows PTRACE_SYSCALL to work. These are called from entry.S in 238 * {handle,ret_from}_syscall. 239 */ 240 __visible int do_syscall_trace_enter(struct pt_regs *regs) 241 { 242 if (test_thread_flag(TIF_SYSCALL_TRACE)) 243 if (ptrace_report_syscall_entry(regs)) 244 return -1; 245 246 /* 247 * Do the secure computing after ptrace; failures should be fast. 248 * If this fails we might have return value in a0 from seccomp 249 * (via SECCOMP_RET_ERRNO/TRACE). 250 */ 251 if (secure_computing() == -1) 252 return -1; 253 254 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS 255 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT)) 256 trace_sys_enter(regs, syscall_get_nr(current, regs)); 257 #endif 258 259 audit_syscall_entry(regs->a7, regs->a0, regs->a1, regs->a2, regs->a3); 260 return 0; 261 } 262 263 __visible void do_syscall_trace_exit(struct pt_regs *regs) 264 { 265 audit_syscall_exit(regs); 266 267 if (test_thread_flag(TIF_SYSCALL_TRACE)) 268 ptrace_report_syscall_exit(regs, 0); 269 270 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS 271 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT)) 272 trace_sys_exit(regs, regs_return_value(regs)); 273 #endif 274 } 275