.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | 3 | * Copyright (C) 2009 Sunplus Core Technology Co., Ltd. |
---|
3 | 4 | * Chen Liqin <liqin.chen@sunplusct.com> |
---|
4 | 5 | * Lennox Wu <lennox.wu@sunplusct.com> |
---|
5 | 6 | * Copyright (C) 2012 Regents of the University of California |
---|
6 | | - * |
---|
7 | | - * This program is free software; you can redistribute it and/or modify |
---|
8 | | - * it under the terms of the GNU General Public License as published by |
---|
9 | | - * the Free Software Foundation; either version 2 of the License, or |
---|
10 | | - * (at your option) any later version. |
---|
11 | | - * |
---|
12 | | - * This program is distributed in the hope that it will be useful, |
---|
13 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | | - * GNU General Public License for more details. |
---|
16 | | - * |
---|
17 | | - * You should have received a copy of the GNU General Public License |
---|
18 | | - * along with this program; if not, see the file COPYING, or write |
---|
19 | | - * to the Free Software Foundation, Inc., |
---|
20 | 7 | */ |
---|
21 | 8 | |
---|
22 | 9 | #include <linux/signal.h> |
---|
.. | .. |
---|
29 | 16 | #include <asm/vdso.h> |
---|
30 | 17 | #include <asm/switch_to.h> |
---|
31 | 18 | #include <asm/csr.h> |
---|
| 19 | +#include <asm/cacheflush.h> |
---|
| 20 | + |
---|
| 21 | +extern u32 __user_rt_sigreturn[2]; |
---|
32 | 22 | |
---|
33 | 23 | #define DEBUG_SIG 0 |
---|
34 | 24 | |
---|
35 | 25 | struct rt_sigframe { |
---|
36 | 26 | struct siginfo info; |
---|
37 | 27 | struct ucontext uc; |
---|
| 28 | +#ifndef CONFIG_MMU |
---|
| 29 | + u32 sigreturn_code[2]; |
---|
| 30 | +#endif |
---|
38 | 31 | }; |
---|
39 | 32 | |
---|
40 | | -static long restore_d_state(struct pt_regs *regs, |
---|
41 | | - struct __riscv_d_ext_state __user *state) |
---|
| 33 | +#ifdef CONFIG_FPU |
---|
| 34 | +static long restore_fp_state(struct pt_regs *regs, |
---|
| 35 | + union __riscv_fp_state __user *sc_fpregs) |
---|
42 | 36 | { |
---|
43 | 37 | long err; |
---|
44 | | - err = __copy_from_user(¤t->thread.fstate, state, sizeof(*state)); |
---|
45 | | - if (likely(!err)) |
---|
46 | | - fstate_restore(current, regs); |
---|
47 | | - return err; |
---|
48 | | -} |
---|
49 | | - |
---|
50 | | -static long save_d_state(struct pt_regs *regs, |
---|
51 | | - struct __riscv_d_ext_state __user *state) |
---|
52 | | -{ |
---|
53 | | - fstate_save(current, regs); |
---|
54 | | - return __copy_to_user(state, ¤t->thread.fstate, sizeof(*state)); |
---|
55 | | -} |
---|
56 | | - |
---|
57 | | -static long restore_sigcontext(struct pt_regs *regs, |
---|
58 | | - struct sigcontext __user *sc) |
---|
59 | | -{ |
---|
60 | | - long err; |
---|
| 38 | + struct __riscv_d_ext_state __user *state = &sc_fpregs->d; |
---|
61 | 39 | size_t i; |
---|
62 | | - /* sc_regs is structured the same as the start of pt_regs */ |
---|
63 | | - err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs)); |
---|
| 40 | + |
---|
| 41 | + err = __copy_from_user(¤t->thread.fstate, state, sizeof(*state)); |
---|
64 | 42 | if (unlikely(err)) |
---|
65 | 43 | return err; |
---|
66 | | - /* Restore the floating-point state. */ |
---|
67 | | - err = restore_d_state(regs, &sc->sc_fpregs.d); |
---|
68 | | - if (unlikely(err)) |
---|
69 | | - return err; |
---|
| 44 | + |
---|
| 45 | + fstate_restore(current, regs); |
---|
| 46 | + |
---|
70 | 47 | /* We support no other extension state at this time. */ |
---|
71 | | - for (i = 0; i < ARRAY_SIZE(sc->sc_fpregs.q.reserved); i++) { |
---|
| 48 | + for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) { |
---|
72 | 49 | u32 value; |
---|
73 | | - err = __get_user(value, &sc->sc_fpregs.q.reserved[i]); |
---|
| 50 | + |
---|
| 51 | + err = __get_user(value, &sc_fpregs->q.reserved[i]); |
---|
74 | 52 | if (unlikely(err)) |
---|
75 | 53 | break; |
---|
76 | 54 | if (value != 0) |
---|
77 | 55 | return -EINVAL; |
---|
78 | 56 | } |
---|
| 57 | + |
---|
| 58 | + return err; |
---|
| 59 | +} |
---|
| 60 | + |
---|
| 61 | +static long save_fp_state(struct pt_regs *regs, |
---|
| 62 | + union __riscv_fp_state __user *sc_fpregs) |
---|
| 63 | +{ |
---|
| 64 | + long err; |
---|
| 65 | + struct __riscv_d_ext_state __user *state = &sc_fpregs->d; |
---|
| 66 | + size_t i; |
---|
| 67 | + |
---|
| 68 | + fstate_save(current, regs); |
---|
| 69 | + err = __copy_to_user(state, ¤t->thread.fstate, sizeof(*state)); |
---|
| 70 | + if (unlikely(err)) |
---|
| 71 | + return err; |
---|
| 72 | + |
---|
| 73 | + /* We support no other extension state at this time. */ |
---|
| 74 | + for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) { |
---|
| 75 | + err = __put_user(0, &sc_fpregs->q.reserved[i]); |
---|
| 76 | + if (unlikely(err)) |
---|
| 77 | + break; |
---|
| 78 | + } |
---|
| 79 | + |
---|
| 80 | + return err; |
---|
| 81 | +} |
---|
| 82 | +#else |
---|
| 83 | +#define save_fp_state(task, regs) (0) |
---|
| 84 | +#define restore_fp_state(task, regs) (0) |
---|
| 85 | +#endif |
---|
| 86 | + |
---|
| 87 | +static long restore_sigcontext(struct pt_regs *regs, |
---|
| 88 | + struct sigcontext __user *sc) |
---|
| 89 | +{ |
---|
| 90 | + long err; |
---|
| 91 | + /* sc_regs is structured the same as the start of pt_regs */ |
---|
| 92 | + err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs)); |
---|
| 93 | + /* Restore the floating-point state. */ |
---|
| 94 | + if (has_fpu) |
---|
| 95 | + err |= restore_fp_state(regs, &sc->sc_fpregs); |
---|
79 | 96 | return err; |
---|
80 | 97 | } |
---|
81 | 98 | |
---|
.. | .. |
---|
91 | 108 | |
---|
92 | 109 | frame = (struct rt_sigframe __user *)regs->sp; |
---|
93 | 110 | |
---|
94 | | - if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) |
---|
| 111 | + if (!access_ok(frame, sizeof(*frame))) |
---|
95 | 112 | goto badframe; |
---|
96 | 113 | |
---|
97 | 114 | if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) |
---|
.. | .. |
---|
105 | 122 | if (restore_altstack(&frame->uc.uc_stack)) |
---|
106 | 123 | goto badframe; |
---|
107 | 124 | |
---|
| 125 | + regs->cause = -1UL; |
---|
| 126 | + |
---|
108 | 127 | return regs->a0; |
---|
109 | 128 | |
---|
110 | 129 | badframe: |
---|
.. | .. |
---|
113 | 132 | pr_info_ratelimited( |
---|
114 | 133 | "%s[%d]: bad frame in %s: frame=%p pc=%p sp=%p\n", |
---|
115 | 134 | task->comm, task_pid_nr(task), __func__, |
---|
116 | | - frame, (void *)regs->sepc, (void *)regs->sp); |
---|
| 135 | + frame, (void *)regs->epc, (void *)regs->sp); |
---|
117 | 136 | } |
---|
118 | | - force_sig(SIGSEGV, task); |
---|
| 137 | + force_sig(SIGSEGV); |
---|
119 | 138 | return 0; |
---|
120 | 139 | } |
---|
121 | 140 | |
---|
.. | .. |
---|
124 | 143 | { |
---|
125 | 144 | struct sigcontext __user *sc = &frame->uc.uc_mcontext; |
---|
126 | 145 | long err; |
---|
127 | | - size_t i; |
---|
128 | 146 | /* sc_regs is structured the same as the start of pt_regs */ |
---|
129 | 147 | err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs)); |
---|
130 | 148 | /* Save the floating-point state. */ |
---|
131 | | - err |= save_d_state(regs, &sc->sc_fpregs.d); |
---|
132 | | - /* We support no other extension state at this time. */ |
---|
133 | | - for (i = 0; i < ARRAY_SIZE(sc->sc_fpregs.q.reserved); i++) |
---|
134 | | - err |= __put_user(0, &sc->sc_fpregs.q.reserved[i]); |
---|
| 149 | + if (has_fpu) |
---|
| 150 | + err |= save_fp_state(regs, &sc->sc_fpregs); |
---|
135 | 151 | return err; |
---|
136 | 152 | } |
---|
137 | 153 | |
---|
.. | .. |
---|
158 | 174 | return (void __user *)sp; |
---|
159 | 175 | } |
---|
160 | 176 | |
---|
161 | | - |
---|
162 | 177 | static int setup_rt_frame(struct ksignal *ksig, sigset_t *set, |
---|
163 | 178 | struct pt_regs *regs) |
---|
164 | 179 | { |
---|
165 | 180 | struct rt_sigframe __user *frame; |
---|
166 | 181 | long err = 0; |
---|
| 182 | + unsigned long __maybe_unused addr; |
---|
167 | 183 | |
---|
168 | 184 | frame = get_sigframe(ksig, regs, sizeof(*frame)); |
---|
169 | | - if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) |
---|
| 185 | + if (!access_ok(frame, sizeof(*frame))) |
---|
170 | 186 | return -EFAULT; |
---|
171 | 187 | |
---|
172 | 188 | err |= copy_siginfo_to_user(&frame->info, &ksig->info); |
---|
.. | .. |
---|
181 | 197 | return -EFAULT; |
---|
182 | 198 | |
---|
183 | 199 | /* Set up to return from userspace. */ |
---|
| 200 | +#ifdef CONFIG_MMU |
---|
184 | 201 | regs->ra = (unsigned long)VDSO_SYMBOL( |
---|
185 | 202 | current->mm->context.vdso, rt_sigreturn); |
---|
| 203 | +#else |
---|
| 204 | + /* |
---|
| 205 | + * For the nommu case we don't have a VDSO. Instead we push two |
---|
| 206 | + * instructions to call the rt_sigreturn syscall onto the user stack. |
---|
| 207 | + */ |
---|
| 208 | + if (copy_to_user(&frame->sigreturn_code, __user_rt_sigreturn, |
---|
| 209 | + sizeof(frame->sigreturn_code))) |
---|
| 210 | + return -EFAULT; |
---|
| 211 | + |
---|
| 212 | + addr = (unsigned long)&frame->sigreturn_code; |
---|
| 213 | + /* Make sure the two instructions are pushed to icache. */ |
---|
| 214 | + flush_icache_range(addr, addr + sizeof(frame->sigreturn_code)); |
---|
| 215 | + |
---|
| 216 | + regs->ra = addr; |
---|
| 217 | +#endif /* CONFIG_MMU */ |
---|
186 | 218 | |
---|
187 | 219 | /* |
---|
188 | 220 | * Set up registers for signal handler. |
---|
.. | .. |
---|
191 | 223 | * We always pass siginfo and mcontext, regardless of SA_SIGINFO, |
---|
192 | 224 | * since some things rely on this (e.g. glibc's debug/segfault.c). |
---|
193 | 225 | */ |
---|
194 | | - regs->sepc = (unsigned long)ksig->ka.sa.sa_handler; |
---|
| 226 | + regs->epc = (unsigned long)ksig->ka.sa.sa_handler; |
---|
195 | 227 | regs->sp = (unsigned long)frame; |
---|
196 | 228 | regs->a0 = ksig->sig; /* a0: signal number */ |
---|
197 | 229 | regs->a1 = (unsigned long)(&frame->info); /* a1: siginfo pointer */ |
---|
.. | .. |
---|
200 | 232 | #if DEBUG_SIG |
---|
201 | 233 | pr_info("SIG deliver (%s:%d): sig=%d pc=%p ra=%p sp=%p\n", |
---|
202 | 234 | current->comm, task_pid_nr(current), ksig->sig, |
---|
203 | | - (void *)regs->sepc, (void *)regs->ra, frame); |
---|
| 235 | + (void *)regs->epc, (void *)regs->ra, frame); |
---|
204 | 236 | #endif |
---|
205 | 237 | |
---|
206 | 238 | return 0; |
---|
.. | .. |
---|
212 | 244 | int ret; |
---|
213 | 245 | |
---|
214 | 246 | /* Are we from a system call? */ |
---|
215 | | - if (regs->scause == EXC_SYSCALL) { |
---|
| 247 | + if (regs->cause == EXC_SYSCALL) { |
---|
| 248 | + /* Avoid additional syscall restarting via ret_from_exception */ |
---|
| 249 | + regs->cause = -1UL; |
---|
216 | 250 | /* If so, check system call restarting.. */ |
---|
217 | 251 | switch (regs->a0) { |
---|
218 | 252 | case -ERESTART_RESTARTBLOCK: |
---|
.. | .. |
---|
225 | 259 | regs->a0 = -EINTR; |
---|
226 | 260 | break; |
---|
227 | 261 | } |
---|
228 | | - /* fallthrough */ |
---|
| 262 | + fallthrough; |
---|
229 | 263 | case -ERESTARTNOINTR: |
---|
230 | 264 | regs->a0 = regs->orig_a0; |
---|
231 | | - regs->sepc -= 0x4; |
---|
| 265 | + regs->epc -= 0x4; |
---|
232 | 266 | break; |
---|
233 | 267 | } |
---|
234 | 268 | } |
---|
.. | .. |
---|
250 | 284 | } |
---|
251 | 285 | |
---|
252 | 286 | /* Did we come from a system call? */ |
---|
253 | | - if (regs->scause == EXC_SYSCALL) { |
---|
| 287 | + if (regs->cause == EXC_SYSCALL) { |
---|
| 288 | + /* Avoid additional syscall restarting via ret_from_exception */ |
---|
| 289 | + regs->cause = -1UL; |
---|
| 290 | + |
---|
254 | 291 | /* Restart the system call - no handlers present */ |
---|
255 | 292 | switch (regs->a0) { |
---|
256 | 293 | case -ERESTARTNOHAND: |
---|
257 | 294 | case -ERESTARTSYS: |
---|
258 | 295 | case -ERESTARTNOINTR: |
---|
259 | 296 | regs->a0 = regs->orig_a0; |
---|
260 | | - regs->sepc -= 0x4; |
---|
| 297 | + regs->epc -= 0x4; |
---|
261 | 298 | break; |
---|
262 | 299 | case -ERESTART_RESTARTBLOCK: |
---|
263 | 300 | regs->a0 = regs->orig_a0; |
---|
264 | 301 | regs->a7 = __NR_restart_syscall; |
---|
265 | | - regs->sepc -= 0x4; |
---|
| 302 | + regs->epc -= 0x4; |
---|
266 | 303 | break; |
---|
267 | 304 | } |
---|
268 | 305 | } |
---|
.. | .. |
---|
278 | 315 | * notification of userspace execution resumption |
---|
279 | 316 | * - triggered by the _TIF_WORK_MASK flags |
---|
280 | 317 | */ |
---|
281 | | -asmlinkage void do_notify_resume(struct pt_regs *regs, |
---|
282 | | - unsigned long thread_info_flags) |
---|
| 318 | +asmlinkage __visible void do_notify_resume(struct pt_regs *regs, |
---|
| 319 | + unsigned long thread_info_flags) |
---|
283 | 320 | { |
---|
284 | 321 | /* Handle pending signal delivery */ |
---|
285 | | - if (thread_info_flags & _TIF_SIGPENDING) |
---|
| 322 | + if (thread_info_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL)) |
---|
286 | 323 | do_signal(regs); |
---|
287 | 324 | |
---|
288 | | - if (thread_info_flags & _TIF_NOTIFY_RESUME) { |
---|
289 | | - clear_thread_flag(TIF_NOTIFY_RESUME); |
---|
| 325 | + if (thread_info_flags & _TIF_NOTIFY_RESUME) |
---|
290 | 326 | tracehook_notify_resume(regs); |
---|
291 | | - } |
---|
292 | 327 | } |
---|