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> |
David Abdurachmanov | 0aea894 | 2018-10-29 11:48:54 +0100 | [diff] [blame] | 13 | #include <linux/audit.h> |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 14 | #include <linux/ptrace.h> |
| 15 | #include <linux/elf.h> |
| 16 | #include <linux/regset.h> |
| 17 | #include <linux/sched.h> |
| 18 | #include <linux/sched/task_stack.h> |
| 19 | #include <linux/tracehook.h> |
David Abdurachmanov | 008e901 | 2018-12-10 21:43:55 +0100 | [diff] [blame] | 20 | |
| 21 | #define CREATE_TRACE_POINTS |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 22 | #include <trace/events/syscalls.h> |
| 23 | |
| 24 | enum riscv_regset { |
| 25 | REGSET_X, |
Jim Wilson | b8c8a95 | 2018-10-17 17:59:05 -0700 | [diff] [blame] | 26 | #ifdef CONFIG_FPU |
| 27 | REGSET_F, |
| 28 | #endif |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | static int riscv_gpr_get(struct task_struct *target, |
| 32 | const struct user_regset *regset, |
| 33 | unsigned int pos, unsigned int count, |
| 34 | void *kbuf, void __user *ubuf) |
| 35 | { |
| 36 | struct pt_regs *regs; |
| 37 | |
| 38 | regs = task_pt_regs(target); |
| 39 | return user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs, 0, -1); |
| 40 | } |
| 41 | |
| 42 | static int riscv_gpr_set(struct task_struct *target, |
| 43 | const struct user_regset *regset, |
| 44 | unsigned int pos, unsigned int count, |
| 45 | const void *kbuf, const void __user *ubuf) |
| 46 | { |
| 47 | int ret; |
| 48 | struct pt_regs *regs; |
| 49 | |
| 50 | regs = task_pt_regs(target); |
Jim Wilson | 1db9b80 | 2018-06-11 14:48:22 -0700 | [diff] [blame] | 51 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1); |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 52 | return ret; |
| 53 | } |
| 54 | |
Jim Wilson | b8c8a95 | 2018-10-17 17:59:05 -0700 | [diff] [blame] | 55 | #ifdef CONFIG_FPU |
| 56 | static int riscv_fpr_get(struct task_struct *target, |
| 57 | const struct user_regset *regset, |
| 58 | unsigned int pos, unsigned int count, |
| 59 | void *kbuf, void __user *ubuf) |
| 60 | { |
| 61 | int ret; |
| 62 | struct __riscv_d_ext_state *fstate = &target->thread.fstate; |
| 63 | |
| 64 | ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, fstate, 0, |
| 65 | offsetof(struct __riscv_d_ext_state, fcsr)); |
| 66 | if (!ret) { |
| 67 | ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, fstate, 0, |
| 68 | offsetof(struct __riscv_d_ext_state, fcsr) + |
| 69 | sizeof(fstate->fcsr)); |
| 70 | } |
| 71 | |
| 72 | return ret; |
| 73 | } |
| 74 | |
| 75 | static int riscv_fpr_set(struct task_struct *target, |
| 76 | const struct user_regset *regset, |
| 77 | unsigned int pos, unsigned int count, |
| 78 | const void *kbuf, const void __user *ubuf) |
| 79 | { |
| 80 | int ret; |
| 81 | struct __riscv_d_ext_state *fstate = &target->thread.fstate; |
| 82 | |
| 83 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0, |
| 84 | offsetof(struct __riscv_d_ext_state, fcsr)); |
| 85 | if (!ret) { |
| 86 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0, |
| 87 | offsetof(struct __riscv_d_ext_state, fcsr) + |
| 88 | sizeof(fstate->fcsr)); |
| 89 | } |
| 90 | |
| 91 | return ret; |
| 92 | } |
| 93 | #endif |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 94 | |
| 95 | static const struct user_regset riscv_user_regset[] = { |
| 96 | [REGSET_X] = { |
| 97 | .core_note_type = NT_PRSTATUS, |
| 98 | .n = ELF_NGREG, |
| 99 | .size = sizeof(elf_greg_t), |
| 100 | .align = sizeof(elf_greg_t), |
| 101 | .get = &riscv_gpr_get, |
| 102 | .set = &riscv_gpr_set, |
| 103 | }, |
Jim Wilson | b8c8a95 | 2018-10-17 17:59:05 -0700 | [diff] [blame] | 104 | #ifdef CONFIG_FPU |
| 105 | [REGSET_F] = { |
| 106 | .core_note_type = NT_PRFPREG, |
| 107 | .n = ELF_NFPREG, |
| 108 | .size = sizeof(elf_fpreg_t), |
| 109 | .align = sizeof(elf_fpreg_t), |
| 110 | .get = &riscv_fpr_get, |
| 111 | .set = &riscv_fpr_set, |
| 112 | }, |
| 113 | #endif |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | static const struct user_regset_view riscv_user_native_view = { |
| 117 | .name = "riscv", |
| 118 | .e_machine = EM_RISCV, |
| 119 | .regsets = riscv_user_regset, |
| 120 | .n = ARRAY_SIZE(riscv_user_regset), |
| 121 | }; |
| 122 | |
| 123 | const struct user_regset_view *task_user_regset_view(struct task_struct *task) |
| 124 | { |
| 125 | return &riscv_user_native_view; |
| 126 | } |
| 127 | |
| 128 | void ptrace_disable(struct task_struct *child) |
| 129 | { |
| 130 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 131 | } |
| 132 | |
| 133 | long arch_ptrace(struct task_struct *child, long request, |
| 134 | unsigned long addr, unsigned long data) |
| 135 | { |
| 136 | long ret = -EIO; |
| 137 | |
| 138 | switch (request) { |
| 139 | default: |
| 140 | ret = ptrace_request(child, request, addr, data); |
| 141 | break; |
| 142 | } |
| 143 | |
| 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Allows PTRACE_SYSCALL to work. These are called from entry.S in |
| 149 | * {handle,ret_from}_syscall. |
| 150 | */ |
Paul Walmsley | f307307 | 2019-10-17 22:20:05 -0700 | [diff] [blame] | 151 | __visible void do_syscall_trace_enter(struct pt_regs *regs) |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 152 | { |
| 153 | if (test_thread_flag(TIF_SYSCALL_TRACE)) |
| 154 | if (tracehook_report_syscall_entry(regs)) |
| 155 | syscall_set_nr(current, regs, -1); |
| 156 | |
David Abdurachmanov | 5340627 | 2019-10-04 17:12:22 -0700 | [diff] [blame^] | 157 | /* |
| 158 | * Do the secure computing after ptrace; failures should be fast. |
| 159 | * If this fails we might have return value in a0 from seccomp |
| 160 | * (via SECCOMP_RET_ERRNO/TRACE). |
| 161 | */ |
| 162 | if (secure_computing(NULL) == -1) { |
| 163 | syscall_set_nr(current, regs, -1); |
| 164 | return; |
| 165 | } |
| 166 | |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 167 | #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS |
| 168 | if (test_thread_flag(TIF_SYSCALL_TRACEPOINT)) |
| 169 | trace_sys_enter(regs, syscall_get_nr(current, regs)); |
| 170 | #endif |
David Abdurachmanov | 0aea894 | 2018-10-29 11:48:54 +0100 | [diff] [blame] | 171 | |
| 172 | audit_syscall_entry(regs->a7, regs->a0, regs->a1, regs->a2, regs->a3); |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Paul Walmsley | f307307 | 2019-10-17 22:20:05 -0700 | [diff] [blame] | 175 | __visible void do_syscall_trace_exit(struct pt_regs *regs) |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 176 | { |
David Abdurachmanov | 0aea894 | 2018-10-29 11:48:54 +0100 | [diff] [blame] | 177 | audit_syscall_exit(regs); |
| 178 | |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 179 | if (test_thread_flag(TIF_SYSCALL_TRACE)) |
| 180 | tracehook_report_syscall_exit(regs, 0); |
| 181 | |
| 182 | #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS |
| 183 | if (test_thread_flag(TIF_SYSCALL_TRACEPOINT)) |
David Abdurachmanov | 775800b | 2018-12-06 16:26:34 +0100 | [diff] [blame] | 184 | trace_sys_exit(regs, regs_return_value(regs)); |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 185 | #endif |
| 186 | } |