1 // SPDX-License-Identifier: GPL-2.0-or-later
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
9 #include <linux/compat.h>
10 #include <linux/signal.h>
11 #include <linux/uaccess.h>
12 #include <linux/syscalls.h>
13 #include <linux/resume_user_mode.h>
14 #include <linux/linkage.h>
16 #include <asm/ucontext.h>
18 #include <asm/signal.h>
19 #include <asm/signal32.h>
20 #include <asm/switch_to.h>
23 extern u32 __user_rt_sigreturn[2];
31 u32 sigreturn_code[2];
36 static long restore_fp_state(struct pt_regs *regs,
37 union __riscv_fp_state __user *sc_fpregs)
40 struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
43 err = __copy_from_user(¤t->thread.fstate, state, sizeof(*state));
47 fstate_restore(current, regs);
49 /* We support no other extension state at this time. */
50 for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) {
53 err = __get_user(value, &sc_fpregs->q.reserved[i]);
63 static long save_fp_state(struct pt_regs *regs,
64 union __riscv_fp_state __user *sc_fpregs)
67 struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
70 fstate_save(current, regs);
71 err = __copy_to_user(state, ¤t->thread.fstate, sizeof(*state));
75 /* We support no other extension state at this time. */
76 for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) {
77 err = __put_user(0, &sc_fpregs->q.reserved[i]);
85 #define save_fp_state(task, regs) (0)
86 #define restore_fp_state(task, regs) (0)
89 static long restore_sigcontext(struct pt_regs *regs,
90 struct sigcontext __user *sc)
93 /* sc_regs is structured the same as the start of pt_regs */
94 err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs));
95 /* Restore the floating-point state. */
97 err |= restore_fp_state(regs, &sc->sc_fpregs);
101 SYSCALL_DEFINE0(rt_sigreturn)
103 struct pt_regs *regs = current_pt_regs();
104 struct rt_sigframe __user *frame;
105 struct task_struct *task;
108 /* Always make any pending restarted system calls return -EINTR */
109 current->restart_block.fn = do_no_restart_syscall;
111 frame = (struct rt_sigframe __user *)regs->sp;
113 if (!access_ok(frame, sizeof(*frame)))
116 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
119 set_current_blocked(&set);
121 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
124 if (restore_altstack(&frame->uc.uc_stack))
133 if (show_unhandled_signals) {
135 "%s[%d]: bad frame in %s: frame=%p pc=%p sp=%p\n",
136 task->comm, task_pid_nr(task), __func__,
137 frame, (void *)regs->epc, (void *)regs->sp);
143 static long setup_sigcontext(struct rt_sigframe __user *frame,
144 struct pt_regs *regs)
146 struct sigcontext __user *sc = &frame->uc.uc_mcontext;
148 /* sc_regs is structured the same as the start of pt_regs */
149 err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs));
150 /* Save the floating-point state. */
152 err |= save_fp_state(regs, &sc->sc_fpregs);
156 static inline void __user *get_sigframe(struct ksignal *ksig,
157 struct pt_regs *regs, size_t framesize)
160 /* Default to using normal stack */
164 * If we are on the alternate signal stack and would overflow it, don't.
165 * Return an always-bogus address instead so we will die with SIGSEGV.
167 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
168 return (void __user __force *)(-1UL);
170 /* This is the X/Open sanctioned signal stack switching. */
171 sp = sigsp(sp, ksig) - framesize;
173 /* Align the stack frame. */
176 return (void __user *)sp;
179 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
180 struct pt_regs *regs)
182 struct rt_sigframe __user *frame;
185 frame = get_sigframe(ksig, regs, sizeof(*frame));
186 if (!access_ok(frame, sizeof(*frame)))
189 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
191 /* Create the ucontext. */
192 err |= __put_user(0, &frame->uc.uc_flags);
193 err |= __put_user(NULL, &frame->uc.uc_link);
194 err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
195 err |= setup_sigcontext(frame, regs);
196 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
200 /* Set up to return from userspace. */
202 regs->ra = (unsigned long)VDSO_SYMBOL(
203 current->mm->context.vdso, rt_sigreturn);
206 * For the nommu case we don't have a VDSO. Instead we push two
207 * instructions to call the rt_sigreturn syscall onto the user stack.
209 if (copy_to_user(&frame->sigreturn_code, __user_rt_sigreturn,
210 sizeof(frame->sigreturn_code)))
212 regs->ra = (unsigned long)&frame->sigreturn_code;
213 #endif /* CONFIG_MMU */
216 * Set up registers for signal handler.
217 * Registers that we don't modify keep the value they had from
218 * user-space at the time we took the signal.
219 * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
220 * since some things rely on this (e.g. glibc's debug/segfault.c).
222 regs->epc = (unsigned long)ksig->ka.sa.sa_handler;
223 regs->sp = (unsigned long)frame;
224 regs->a0 = ksig->sig; /* a0: signal number */
225 regs->a1 = (unsigned long)(&frame->info); /* a1: siginfo pointer */
226 regs->a2 = (unsigned long)(&frame->uc); /* a2: ucontext pointer */
229 pr_info("SIG deliver (%s:%d): sig=%d pc=%p ra=%p sp=%p\n",
230 current->comm, task_pid_nr(current), ksig->sig,
231 (void *)regs->epc, (void *)regs->ra, frame);
237 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
239 sigset_t *oldset = sigmask_to_save();
242 /* Are we from a system call? */
243 if (regs->cause == EXC_SYSCALL) {
244 /* Avoid additional syscall restarting via ret_from_exception */
246 /* If so, check system call restarting.. */
248 case -ERESTART_RESTARTBLOCK:
249 case -ERESTARTNOHAND:
254 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
259 case -ERESTARTNOINTR:
260 regs->a0 = regs->orig_a0;
266 rseq_signal_deliver(ksig, regs);
268 /* Set up the stack frame */
269 if (is_compat_task())
270 ret = compat_setup_rt_frame(ksig, oldset, regs);
272 ret = setup_rt_frame(ksig, oldset, regs);
274 signal_setup_done(ret, ksig, 0);
277 static void do_signal(struct pt_regs *regs)
281 if (get_signal(&ksig)) {
282 /* Actually deliver the signal */
283 handle_signal(&ksig, regs);
287 /* Did we come from a system call? */
288 if (regs->cause == EXC_SYSCALL) {
289 /* Avoid additional syscall restarting via ret_from_exception */
292 /* Restart the system call - no handlers present */
294 case -ERESTARTNOHAND:
296 case -ERESTARTNOINTR:
297 regs->a0 = regs->orig_a0;
300 case -ERESTART_RESTARTBLOCK:
301 regs->a0 = regs->orig_a0;
302 regs->a7 = __NR_restart_syscall;
309 * If there is no signal to deliver, we just put the saved
312 restore_saved_sigmask();
316 * notification of userspace execution resumption
317 * - triggered by the _TIF_WORK_MASK flags
319 asmlinkage __visible void do_notify_resume(struct pt_regs *regs,
320 unsigned long thread_info_flags)
322 if (thread_info_flags & _TIF_UPROBE)
323 uprobe_notify_resume(regs);
325 /* Handle pending signal delivery */
326 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
329 if (thread_info_flags & _TIF_NOTIFY_RESUME)
330 resume_user_mode_work(regs);