Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010 Tilera Corporation. All Rights Reserved. |
| 3 | * Copyright 2015 Regents of the University of California |
| 4 | * Copyright 2017 SiFive |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation, version 2. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * Copied from arch/tile/kernel/ptrace.c |
| 16 | */ |
| 17 | |
| 18 | #include <asm/ptrace.h> |
| 19 | #include <asm/syscall.h> |
| 20 | #include <asm/thread_info.h> |
David Abdurachmanov | 0aea894 | 2018-10-29 11:48:54 +0100 | [diff] [blame] | 21 | #include <linux/audit.h> |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 22 | #include <linux/ptrace.h> |
| 23 | #include <linux/elf.h> |
| 24 | #include <linux/regset.h> |
| 25 | #include <linux/sched.h> |
| 26 | #include <linux/sched/task_stack.h> |
| 27 | #include <linux/tracehook.h> |
David Abdurachmanov | 008e901 | 2018-12-10 21:43:55 +0100 | [diff] [blame] | 28 | |
| 29 | #define CREATE_TRACE_POINTS |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 30 | #include <trace/events/syscalls.h> |
| 31 | |
| 32 | enum riscv_regset { |
| 33 | REGSET_X, |
Jim Wilson | b8c8a95 | 2018-10-17 17:59:05 -0700 | [diff] [blame] | 34 | #ifdef CONFIG_FPU |
| 35 | REGSET_F, |
| 36 | #endif |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | static int riscv_gpr_get(struct task_struct *target, |
| 40 | const struct user_regset *regset, |
| 41 | unsigned int pos, unsigned int count, |
| 42 | void *kbuf, void __user *ubuf) |
| 43 | { |
| 44 | struct pt_regs *regs; |
| 45 | |
| 46 | regs = task_pt_regs(target); |
| 47 | return user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs, 0, -1); |
| 48 | } |
| 49 | |
| 50 | static int riscv_gpr_set(struct task_struct *target, |
| 51 | const struct user_regset *regset, |
| 52 | unsigned int pos, unsigned int count, |
| 53 | const void *kbuf, const void __user *ubuf) |
| 54 | { |
| 55 | int ret; |
| 56 | struct pt_regs *regs; |
| 57 | |
| 58 | regs = task_pt_regs(target); |
Jim Wilson | 1db9b80 | 2018-06-11 14:48:22 -0700 | [diff] [blame] | 59 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1); |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 60 | return ret; |
| 61 | } |
| 62 | |
Jim Wilson | b8c8a95 | 2018-10-17 17:59:05 -0700 | [diff] [blame] | 63 | #ifdef CONFIG_FPU |
| 64 | static int riscv_fpr_get(struct task_struct *target, |
| 65 | const struct user_regset *regset, |
| 66 | unsigned int pos, unsigned int count, |
| 67 | void *kbuf, void __user *ubuf) |
| 68 | { |
| 69 | int ret; |
| 70 | struct __riscv_d_ext_state *fstate = &target->thread.fstate; |
| 71 | |
| 72 | ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, fstate, 0, |
| 73 | offsetof(struct __riscv_d_ext_state, fcsr)); |
| 74 | if (!ret) { |
| 75 | ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, fstate, 0, |
| 76 | offsetof(struct __riscv_d_ext_state, fcsr) + |
| 77 | sizeof(fstate->fcsr)); |
| 78 | } |
| 79 | |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | static int riscv_fpr_set(struct task_struct *target, |
| 84 | const struct user_regset *regset, |
| 85 | unsigned int pos, unsigned int count, |
| 86 | const void *kbuf, const void __user *ubuf) |
| 87 | { |
| 88 | int ret; |
| 89 | struct __riscv_d_ext_state *fstate = &target->thread.fstate; |
| 90 | |
| 91 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0, |
| 92 | offsetof(struct __riscv_d_ext_state, fcsr)); |
| 93 | if (!ret) { |
| 94 | ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0, |
| 95 | offsetof(struct __riscv_d_ext_state, fcsr) + |
| 96 | sizeof(fstate->fcsr)); |
| 97 | } |
| 98 | |
| 99 | return ret; |
| 100 | } |
| 101 | #endif |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 102 | |
| 103 | static const struct user_regset riscv_user_regset[] = { |
| 104 | [REGSET_X] = { |
| 105 | .core_note_type = NT_PRSTATUS, |
| 106 | .n = ELF_NGREG, |
| 107 | .size = sizeof(elf_greg_t), |
| 108 | .align = sizeof(elf_greg_t), |
| 109 | .get = &riscv_gpr_get, |
| 110 | .set = &riscv_gpr_set, |
| 111 | }, |
Jim Wilson | b8c8a95 | 2018-10-17 17:59:05 -0700 | [diff] [blame] | 112 | #ifdef CONFIG_FPU |
| 113 | [REGSET_F] = { |
| 114 | .core_note_type = NT_PRFPREG, |
| 115 | .n = ELF_NFPREG, |
| 116 | .size = sizeof(elf_fpreg_t), |
| 117 | .align = sizeof(elf_fpreg_t), |
| 118 | .get = &riscv_fpr_get, |
| 119 | .set = &riscv_fpr_set, |
| 120 | }, |
| 121 | #endif |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | static const struct user_regset_view riscv_user_native_view = { |
| 125 | .name = "riscv", |
| 126 | .e_machine = EM_RISCV, |
| 127 | .regsets = riscv_user_regset, |
| 128 | .n = ARRAY_SIZE(riscv_user_regset), |
| 129 | }; |
| 130 | |
| 131 | const struct user_regset_view *task_user_regset_view(struct task_struct *task) |
| 132 | { |
| 133 | return &riscv_user_native_view; |
| 134 | } |
| 135 | |
| 136 | void ptrace_disable(struct task_struct *child) |
| 137 | { |
| 138 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 139 | } |
| 140 | |
| 141 | long arch_ptrace(struct task_struct *child, long request, |
| 142 | unsigned long addr, unsigned long data) |
| 143 | { |
| 144 | long ret = -EIO; |
| 145 | |
| 146 | switch (request) { |
| 147 | default: |
| 148 | ret = ptrace_request(child, request, addr, data); |
| 149 | break; |
| 150 | } |
| 151 | |
| 152 | return ret; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Allows PTRACE_SYSCALL to work. These are called from entry.S in |
| 157 | * {handle,ret_from}_syscall. |
| 158 | */ |
| 159 | void do_syscall_trace_enter(struct pt_regs *regs) |
| 160 | { |
| 161 | if (test_thread_flag(TIF_SYSCALL_TRACE)) |
| 162 | if (tracehook_report_syscall_entry(regs)) |
| 163 | syscall_set_nr(current, regs, -1); |
| 164 | |
| 165 | #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS |
| 166 | if (test_thread_flag(TIF_SYSCALL_TRACEPOINT)) |
| 167 | trace_sys_enter(regs, syscall_get_nr(current, regs)); |
| 168 | #endif |
David Abdurachmanov | 0aea894 | 2018-10-29 11:48:54 +0100 | [diff] [blame] | 169 | |
| 170 | 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] | 171 | } |
| 172 | |
| 173 | void do_syscall_trace_exit(struct pt_regs *regs) |
| 174 | { |
David Abdurachmanov | 0aea894 | 2018-10-29 11:48:54 +0100 | [diff] [blame] | 175 | audit_syscall_exit(regs); |
| 176 | |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 177 | if (test_thread_flag(TIF_SYSCALL_TRACE)) |
| 178 | tracehook_report_syscall_exit(regs, 0); |
| 179 | |
| 180 | #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS |
| 181 | if (test_thread_flag(TIF_SYSCALL_TRACEPOINT)) |
David Abdurachmanov | 775800b | 2018-12-06 16:26:34 +0100 | [diff] [blame^] | 182 | trace_sys_exit(regs, regs_return_value(regs)); |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 183 | #endif |
| 184 | } |