1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
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>
15 #include <linux/entry-common.h>
17 #include <asm/ucontext.h>
19 #include <asm/signal.h>
20 #include <asm/signal32.h>
21 #include <asm/switch_to.h>
23 #include <asm/cacheflush.h>
25 extern u32 __user_rt_sigreturn[2];
33 u32 sigreturn_code[2];
38 static long restore_fp_state(struct pt_regs *regs,
39 union __riscv_fp_state __user *sc_fpregs)
42 struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
45 err = __copy_from_user(¤t->thread.fstate, state, sizeof(*state));
49 fstate_restore(current, regs);
51 /* We support no other extension state at this time. */
52 for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) {
55 err = __get_user(value, &sc_fpregs->q.reserved[i]);
65 static long save_fp_state(struct pt_regs *regs,
66 union __riscv_fp_state __user *sc_fpregs)
69 struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
72 fstate_save(current, regs);
73 err = __copy_to_user(state, ¤t->thread.fstate, sizeof(*state));
77 /* We support no other extension state at this time. */
78 for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) {
79 err = __put_user(0, &sc_fpregs->q.reserved[i]);
87 #define save_fp_state(task, regs) (0)
88 #define restore_fp_state(task, regs) (0)
91 static long restore_sigcontext(struct pt_regs *regs,
92 struct sigcontext __user *sc)
95 /* sc_regs is structured the same as the start of pt_regs */
96 err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs));
97 /* Restore the floating-point state. */
99 err |= restore_fp_state(regs, &sc->sc_fpregs);
103 SYSCALL_DEFINE0(rt_sigreturn)
105 struct pt_regs *regs = current_pt_regs();
106 struct rt_sigframe __user *frame;
107 struct task_struct *task;
110 /* Always make any pending restarted system calls return -EINTR */
111 current->restart_block.fn = do_no_restart_syscall;
113 frame = (struct rt_sigframe __user *)regs->sp;
115 if (!access_ok(frame, sizeof(*frame)))
118 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
121 set_current_blocked(&set);
123 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
126 if (restore_altstack(&frame->uc.uc_stack))
135 if (show_unhandled_signals) {
137 "%s[%d]: bad frame in %s: frame=%p pc=%p sp=%p\n",
138 task->comm, task_pid_nr(task), __func__,
139 frame, (void *)regs->epc, (void *)regs->sp);
145 static long setup_sigcontext(struct rt_sigframe __user *frame,
146 struct pt_regs *regs)
148 struct sigcontext __user *sc = &frame->uc.uc_mcontext;
150 /* sc_regs is structured the same as the start of pt_regs */
151 err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs));
152 /* Save the floating-point state. */
154 err |= save_fp_state(regs, &sc->sc_fpregs);
158 static inline void __user *get_sigframe(struct ksignal *ksig,
159 struct pt_regs *regs, size_t framesize)
162 /* Default to using normal stack */
166 * If we are on the alternate signal stack and would overflow it, don't.
167 * Return an always-bogus address instead so we will die with SIGSEGV.
169 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
170 return (void __user __force *)(-1UL);
172 /* This is the X/Open sanctioned signal stack switching. */
173 sp = sigsp(sp, ksig) - framesize;
175 /* Align the stack frame. */
178 return (void __user *)sp;
181 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
182 struct pt_regs *regs)
184 struct rt_sigframe __user *frame;
186 unsigned long __maybe_unused addr;
188 frame = get_sigframe(ksig, regs, sizeof(*frame));
189 if (!access_ok(frame, sizeof(*frame)))
192 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
194 /* Create the ucontext. */
195 err |= __put_user(0, &frame->uc.uc_flags);
196 err |= __put_user(NULL, &frame->uc.uc_link);
197 err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
198 err |= setup_sigcontext(frame, regs);
199 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
203 /* Set up to return from userspace. */
205 regs->ra = (unsigned long)VDSO_SYMBOL(
206 current->mm->context.vdso, rt_sigreturn);
209 * For the nommu case we don't have a VDSO. Instead we push two
210 * instructions to call the rt_sigreturn syscall onto the user stack.
212 if (copy_to_user(&frame->sigreturn_code, __user_rt_sigreturn,
213 sizeof(frame->sigreturn_code)))
216 addr = (unsigned long)&frame->sigreturn_code;
217 /* Make sure the two instructions are pushed to icache. */
218 flush_icache_range(addr, addr + sizeof(frame->sigreturn_code));
221 #endif /* CONFIG_MMU */
224 * Set up registers for signal handler.
225 * Registers that we don't modify keep the value they had from
226 * user-space at the time we took the signal.
227 * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
228 * since some things rely on this (e.g. glibc's debug/segfault.c).
230 regs->epc = (unsigned long)ksig->ka.sa.sa_handler;
231 regs->sp = (unsigned long)frame;
232 regs->a0 = ksig->sig; /* a0: signal number */
233 regs->a1 = (unsigned long)(&frame->info); /* a1: siginfo pointer */
234 regs->a2 = (unsigned long)(&frame->uc); /* a2: ucontext pointer */
237 pr_info("SIG deliver (%s:%d): sig=%d pc=%p ra=%p sp=%p\n",
238 current->comm, task_pid_nr(current), ksig->sig,
239 (void *)regs->epc, (void *)regs->ra, frame);
245 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
247 sigset_t *oldset = sigmask_to_save();
250 /* Are we from a system call? */
251 if (regs->cause == EXC_SYSCALL) {
252 /* Avoid additional syscall restarting via ret_from_exception */
254 /* If so, check system call restarting.. */
256 case -ERESTART_RESTARTBLOCK:
257 case -ERESTARTNOHAND:
262 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
267 case -ERESTARTNOINTR:
268 regs->a0 = regs->orig_a0;
274 rseq_signal_deliver(ksig, regs);
276 /* Set up the stack frame */
277 if (is_compat_task())
278 ret = compat_setup_rt_frame(ksig, oldset, regs);
280 ret = setup_rt_frame(ksig, oldset, regs);
282 signal_setup_done(ret, ksig, 0);
285 void arch_do_signal_or_restart(struct pt_regs *regs)
289 if (get_signal(&ksig)) {
290 /* Actually deliver the signal */
291 handle_signal(&ksig, regs);
295 /* Did we come from a system call? */
296 if (regs->cause == EXC_SYSCALL) {
297 /* Avoid additional syscall restarting via ret_from_exception */
300 /* Restart the system call - no handlers present */
302 case -ERESTARTNOHAND:
304 case -ERESTARTNOINTR:
305 regs->a0 = regs->orig_a0;
308 case -ERESTART_RESTARTBLOCK:
309 regs->a0 = regs->orig_a0;
310 regs->a7 = __NR_restart_syscall;
317 * If there is no signal to deliver, we just put the saved
320 restore_saved_sigmask();