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