Thomas Gleixner | 588cb88 | 2019-05-23 11:14:57 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2009 Sunplus Core Technology Co., Ltd. |
| 4 | * Chen Liqin <liqin.chen@sunplusct.com> |
| 5 | * Lennox Wu <lennox.wu@sunplusct.com> |
| 6 | * Copyright (C) 2012 Regents of the University of California |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/signal.h> |
| 10 | #include <linux/uaccess.h> |
| 11 | #include <linux/syscalls.h> |
| 12 | #include <linux/tracehook.h> |
| 13 | #include <linux/linkage.h> |
| 14 | |
| 15 | #include <asm/ucontext.h> |
| 16 | #include <asm/vdso.h> |
| 17 | #include <asm/switch_to.h> |
| 18 | #include <asm/csr.h> |
| 19 | |
Christoph Hellwig | 6bd33e1 | 2019-10-28 13:10:41 +0100 | [diff] [blame] | 20 | extern u32 __user_rt_sigreturn[2]; |
| 21 | |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 22 | #define DEBUG_SIG 0 |
| 23 | |
| 24 | struct rt_sigframe { |
| 25 | struct siginfo info; |
| 26 | struct ucontext uc; |
Christoph Hellwig | 6bd33e1 | 2019-10-28 13:10:41 +0100 | [diff] [blame] | 27 | #ifndef CONFIG_MMU |
| 28 | u32 sigreturn_code[2]; |
| 29 | #endif |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 30 | }; |
| 31 | |
Alan Kao | 9671f70 | 2018-10-09 10:18:33 +0800 | [diff] [blame] | 32 | #ifdef CONFIG_FPU |
Alan Kao | 007f5c3 | 2018-10-09 10:18:31 +0800 | [diff] [blame] | 33 | static long restore_fp_state(struct pt_regs *regs, |
Paul Walmsley | a48dac4 | 2019-10-17 15:41:25 -0700 | [diff] [blame] | 34 | union __riscv_fp_state __user *sc_fpregs) |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 35 | { |
| 36 | long err; |
Alan Kao | 007f5c3 | 2018-10-09 10:18:31 +0800 | [diff] [blame] | 37 | struct __riscv_d_ext_state __user *state = &sc_fpregs->d; |
| 38 | size_t i; |
| 39 | |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 40 | err = __copy_from_user(¤t->thread.fstate, state, sizeof(*state)); |
Alan Kao | 007f5c3 | 2018-10-09 10:18:31 +0800 | [diff] [blame] | 41 | if (unlikely(err)) |
| 42 | return err; |
| 43 | |
| 44 | fstate_restore(current, regs); |
| 45 | |
| 46 | /* We support no other extension state at this time. */ |
| 47 | for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) { |
| 48 | u32 value; |
| 49 | |
| 50 | err = __get_user(value, &sc_fpregs->q.reserved[i]); |
| 51 | if (unlikely(err)) |
| 52 | break; |
| 53 | if (value != 0) |
| 54 | return -EINVAL; |
| 55 | } |
| 56 | |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 57 | return err; |
| 58 | } |
| 59 | |
Alan Kao | 007f5c3 | 2018-10-09 10:18:31 +0800 | [diff] [blame] | 60 | static long save_fp_state(struct pt_regs *regs, |
Paul Walmsley | a48dac4 | 2019-10-17 15:41:25 -0700 | [diff] [blame] | 61 | union __riscv_fp_state __user *sc_fpregs) |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 62 | { |
Alan Kao | 007f5c3 | 2018-10-09 10:18:31 +0800 | [diff] [blame] | 63 | long err; |
| 64 | struct __riscv_d_ext_state __user *state = &sc_fpregs->d; |
| 65 | size_t i; |
| 66 | |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 67 | fstate_save(current, regs); |
Alan Kao | 007f5c3 | 2018-10-09 10:18:31 +0800 | [diff] [blame] | 68 | err = __copy_to_user(state, ¤t->thread.fstate, sizeof(*state)); |
| 69 | if (unlikely(err)) |
| 70 | return err; |
| 71 | |
| 72 | /* We support no other extension state at this time. */ |
| 73 | for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) { |
| 74 | err = __put_user(0, &sc_fpregs->q.reserved[i]); |
| 75 | if (unlikely(err)) |
| 76 | break; |
| 77 | } |
| 78 | |
| 79 | return err; |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 80 | } |
Alan Kao | 9671f70 | 2018-10-09 10:18:33 +0800 | [diff] [blame] | 81 | #else |
| 82 | #define save_fp_state(task, regs) (0) |
| 83 | #define restore_fp_state(task, regs) (0) |
| 84 | #endif |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 85 | |
| 86 | static long restore_sigcontext(struct pt_regs *regs, |
| 87 | struct sigcontext __user *sc) |
| 88 | { |
| 89 | long err; |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 90 | /* sc_regs is structured the same as the start of pt_regs */ |
| 91 | err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs)); |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 92 | /* Restore the floating-point state. */ |
Jisheng Zhang | 37a7a2a | 2021-05-12 22:55:45 +0800 | [diff] [blame] | 93 | if (has_fpu()) |
Alan Kao | 9411ec6 | 2018-10-09 10:18:34 +0800 | [diff] [blame] | 94 | err |= restore_fp_state(regs, &sc->sc_fpregs); |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 95 | return err; |
| 96 | } |
| 97 | |
| 98 | SYSCALL_DEFINE0(rt_sigreturn) |
| 99 | { |
| 100 | struct pt_regs *regs = current_pt_regs(); |
| 101 | struct rt_sigframe __user *frame; |
| 102 | struct task_struct *task; |
| 103 | sigset_t set; |
| 104 | |
| 105 | /* Always make any pending restarted system calls return -EINTR */ |
| 106 | current->restart_block.fn = do_no_restart_syscall; |
| 107 | |
| 108 | frame = (struct rt_sigframe __user *)regs->sp; |
| 109 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 110 | if (!access_ok(frame, sizeof(*frame))) |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 111 | goto badframe; |
| 112 | |
| 113 | if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) |
| 114 | goto badframe; |
| 115 | |
| 116 | set_current_blocked(&set); |
| 117 | |
| 118 | if (restore_sigcontext(regs, &frame->uc.uc_mcontext)) |
| 119 | goto badframe; |
| 120 | |
| 121 | if (restore_altstack(&frame->uc.uc_stack)) |
| 122 | goto badframe; |
| 123 | |
| 124 | return regs->a0; |
| 125 | |
| 126 | badframe: |
| 127 | task = current; |
| 128 | if (show_unhandled_signals) { |
| 129 | pr_info_ratelimited( |
| 130 | "%s[%d]: bad frame in %s: frame=%p pc=%p sp=%p\n", |
| 131 | task->comm, task_pid_nr(task), __func__, |
Christoph Hellwig | a4c3733 | 2019-10-28 13:10:32 +0100 | [diff] [blame] | 132 | frame, (void *)regs->epc, (void *)regs->sp); |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 133 | } |
Eric W. Biederman | 3cf5d07 | 2019-05-23 10:17:27 -0500 | [diff] [blame] | 134 | force_sig(SIGSEGV); |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static long setup_sigcontext(struct rt_sigframe __user *frame, |
| 139 | struct pt_regs *regs) |
| 140 | { |
| 141 | struct sigcontext __user *sc = &frame->uc.uc_mcontext; |
| 142 | long err; |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 143 | /* sc_regs is structured the same as the start of pt_regs */ |
| 144 | err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs)); |
| 145 | /* Save the floating-point state. */ |
Jisheng Zhang | 37a7a2a | 2021-05-12 22:55:45 +0800 | [diff] [blame] | 146 | if (has_fpu()) |
Alan Kao | 9411ec6 | 2018-10-09 10:18:34 +0800 | [diff] [blame] | 147 | err |= save_fp_state(regs, &sc->sc_fpregs); |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 148 | return err; |
| 149 | } |
| 150 | |
| 151 | static inline void __user *get_sigframe(struct ksignal *ksig, |
| 152 | struct pt_regs *regs, size_t framesize) |
| 153 | { |
| 154 | unsigned long sp; |
| 155 | /* Default to using normal stack */ |
| 156 | sp = regs->sp; |
| 157 | |
| 158 | /* |
| 159 | * If we are on the alternate signal stack and would overflow it, don't. |
| 160 | * Return an always-bogus address instead so we will die with SIGSEGV. |
| 161 | */ |
| 162 | if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize))) |
| 163 | return (void __user __force *)(-1UL); |
| 164 | |
| 165 | /* This is the X/Open sanctioned signal stack switching. */ |
| 166 | sp = sigsp(sp, ksig) - framesize; |
| 167 | |
| 168 | /* Align the stack frame. */ |
| 169 | sp &= ~0xfUL; |
| 170 | |
| 171 | return (void __user *)sp; |
| 172 | } |
| 173 | |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 174 | static int setup_rt_frame(struct ksignal *ksig, sigset_t *set, |
| 175 | struct pt_regs *regs) |
| 176 | { |
| 177 | struct rt_sigframe __user *frame; |
| 178 | long err = 0; |
| 179 | |
| 180 | frame = get_sigframe(ksig, regs, sizeof(*frame)); |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 181 | if (!access_ok(frame, sizeof(*frame))) |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 182 | return -EFAULT; |
| 183 | |
| 184 | err |= copy_siginfo_to_user(&frame->info, &ksig->info); |
| 185 | |
| 186 | /* Create the ucontext. */ |
| 187 | err |= __put_user(0, &frame->uc.uc_flags); |
| 188 | err |= __put_user(NULL, &frame->uc.uc_link); |
| 189 | err |= __save_altstack(&frame->uc.uc_stack, regs->sp); |
| 190 | err |= setup_sigcontext(frame, regs); |
| 191 | err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); |
| 192 | if (err) |
| 193 | return -EFAULT; |
| 194 | |
| 195 | /* Set up to return from userspace. */ |
Christoph Hellwig | 6bd33e1 | 2019-10-28 13:10:41 +0100 | [diff] [blame] | 196 | #ifdef CONFIG_MMU |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 197 | regs->ra = (unsigned long)VDSO_SYMBOL( |
| 198 | current->mm->context.vdso, rt_sigreturn); |
Christoph Hellwig | 6bd33e1 | 2019-10-28 13:10:41 +0100 | [diff] [blame] | 199 | #else |
| 200 | /* |
| 201 | * For the nommu case we don't have a VDSO. Instead we push two |
| 202 | * instructions to call the rt_sigreturn syscall onto the user stack. |
| 203 | */ |
| 204 | if (copy_to_user(&frame->sigreturn_code, __user_rt_sigreturn, |
| 205 | sizeof(frame->sigreturn_code))) |
| 206 | return -EFAULT; |
| 207 | regs->ra = (unsigned long)&frame->sigreturn_code; |
| 208 | #endif /* CONFIG_MMU */ |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 209 | |
| 210 | /* |
| 211 | * Set up registers for signal handler. |
| 212 | * Registers that we don't modify keep the value they had from |
| 213 | * user-space at the time we took the signal. |
| 214 | * We always pass siginfo and mcontext, regardless of SA_SIGINFO, |
| 215 | * since some things rely on this (e.g. glibc's debug/segfault.c). |
| 216 | */ |
Christoph Hellwig | a4c3733 | 2019-10-28 13:10:32 +0100 | [diff] [blame] | 217 | regs->epc = (unsigned long)ksig->ka.sa.sa_handler; |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 218 | regs->sp = (unsigned long)frame; |
| 219 | regs->a0 = ksig->sig; /* a0: signal number */ |
| 220 | regs->a1 = (unsigned long)(&frame->info); /* a1: siginfo pointer */ |
| 221 | regs->a2 = (unsigned long)(&frame->uc); /* a2: ucontext pointer */ |
| 222 | |
| 223 | #if DEBUG_SIG |
| 224 | pr_info("SIG deliver (%s:%d): sig=%d pc=%p ra=%p sp=%p\n", |
| 225 | current->comm, task_pid_nr(current), ksig->sig, |
Christoph Hellwig | a4c3733 | 2019-10-28 13:10:32 +0100 | [diff] [blame] | 226 | (void *)regs->epc, (void *)regs->ra, frame); |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 227 | #endif |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | static void handle_signal(struct ksignal *ksig, struct pt_regs *regs) |
| 233 | { |
| 234 | sigset_t *oldset = sigmask_to_save(); |
| 235 | int ret; |
| 236 | |
| 237 | /* Are we from a system call? */ |
Christoph Hellwig | a4c3733 | 2019-10-28 13:10:32 +0100 | [diff] [blame] | 238 | if (regs->cause == EXC_SYSCALL) { |
Guo Ren | 7a64f3f | 2019-04-02 16:02:33 +0800 | [diff] [blame] | 239 | /* Avoid additional syscall restarting via ret_from_exception */ |
Christoph Hellwig | a4c3733 | 2019-10-28 13:10:32 +0100 | [diff] [blame] | 240 | regs->cause = -1UL; |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 241 | /* If so, check system call restarting.. */ |
| 242 | switch (regs->a0) { |
| 243 | case -ERESTART_RESTARTBLOCK: |
| 244 | case -ERESTARTNOHAND: |
| 245 | regs->a0 = -EINTR; |
| 246 | break; |
| 247 | |
| 248 | case -ERESTARTSYS: |
| 249 | if (!(ksig->ka.sa.sa_flags & SA_RESTART)) { |
| 250 | regs->a0 = -EINTR; |
| 251 | break; |
| 252 | } |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 253 | fallthrough; |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 254 | case -ERESTARTNOINTR: |
| 255 | regs->a0 = regs->orig_a0; |
Christoph Hellwig | a4c3733 | 2019-10-28 13:10:32 +0100 | [diff] [blame] | 256 | regs->epc -= 0x4; |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 257 | break; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /* Set up the stack frame */ |
| 262 | ret = setup_rt_frame(ksig, oldset, regs); |
| 263 | |
| 264 | signal_setup_done(ret, ksig, 0); |
| 265 | } |
| 266 | |
| 267 | static void do_signal(struct pt_regs *regs) |
| 268 | { |
| 269 | struct ksignal ksig; |
| 270 | |
| 271 | if (get_signal(&ksig)) { |
| 272 | /* Actually deliver the signal */ |
| 273 | handle_signal(&ksig, regs); |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | /* Did we come from a system call? */ |
Christoph Hellwig | a4c3733 | 2019-10-28 13:10:32 +0100 | [diff] [blame] | 278 | if (regs->cause == EXC_SYSCALL) { |
Guo Ren | 7a64f3f | 2019-04-02 16:02:33 +0800 | [diff] [blame] | 279 | /* Avoid additional syscall restarting via ret_from_exception */ |
Christoph Hellwig | a4c3733 | 2019-10-28 13:10:32 +0100 | [diff] [blame] | 280 | regs->cause = -1UL; |
Guo Ren | 7a64f3f | 2019-04-02 16:02:33 +0800 | [diff] [blame] | 281 | |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 282 | /* Restart the system call - no handlers present */ |
| 283 | switch (regs->a0) { |
| 284 | case -ERESTARTNOHAND: |
| 285 | case -ERESTARTSYS: |
| 286 | case -ERESTARTNOINTR: |
| 287 | regs->a0 = regs->orig_a0; |
Christoph Hellwig | a4c3733 | 2019-10-28 13:10:32 +0100 | [diff] [blame] | 288 | regs->epc -= 0x4; |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 289 | break; |
| 290 | case -ERESTART_RESTARTBLOCK: |
| 291 | regs->a0 = regs->orig_a0; |
| 292 | regs->a7 = __NR_restart_syscall; |
Christoph Hellwig | a4c3733 | 2019-10-28 13:10:32 +0100 | [diff] [blame] | 293 | regs->epc -= 0x4; |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 294 | break; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * If there is no signal to deliver, we just put the saved |
| 300 | * sigmask back. |
| 301 | */ |
| 302 | restore_saved_sigmask(); |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * notification of userspace execution resumption |
| 307 | * - triggered by the _TIF_WORK_MASK flags |
| 308 | */ |
Paul Walmsley | f307307 | 2019-10-17 22:20:05 -0700 | [diff] [blame] | 309 | asmlinkage __visible void do_notify_resume(struct pt_regs *regs, |
| 310 | unsigned long thread_info_flags) |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 311 | { |
Guo Ren | 7478408 | 2020-12-17 16:01:44 +0000 | [diff] [blame] | 312 | if (thread_info_flags & _TIF_UPROBE) |
| 313 | uprobe_notify_resume(regs); |
| 314 | |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 315 | /* Handle pending signal delivery */ |
Jens Axboe | 24a31b8 | 2020-10-09 14:29:17 -0600 | [diff] [blame] | 316 | if (thread_info_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL)) |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 317 | do_signal(regs); |
| 318 | |
Jens Axboe | 3c53279 | 2020-10-03 10:49:22 -0600 | [diff] [blame] | 319 | if (thread_info_flags & _TIF_NOTIFY_RESUME) |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 320 | tracehook_notify_resume(regs); |
Palmer Dabbelt | e2c0cdf | 2017-07-10 18:07:09 -0700 | [diff] [blame] | 321 | } |