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>
22 #include <asm/cacheflush.h>
24 extern u32 __user_rt_sigreturn[2];
32 u32 sigreturn_code[2];
37 static long restore_fp_state(struct pt_regs *regs,
38 union __riscv_fp_state __user *sc_fpregs)
41 struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
44 err = __copy_from_user(¤t->thread.fstate, state, sizeof(*state));
48 fstate_restore(current, regs);
50 /* We support no other extension state at this time. */
51 for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) {
54 err = __get_user(value, &sc_fpregs->q.reserved[i]);
64 static long save_fp_state(struct pt_regs *regs,
65 union __riscv_fp_state __user *sc_fpregs)
68 struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
71 fstate_save(current, regs);
72 err = __copy_to_user(state, ¤t->thread.fstate, sizeof(*state));
76 /* We support no other extension state at this time. */
77 for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) {
78 err = __put_user(0, &sc_fpregs->q.reserved[i]);
86 #define save_fp_state(task, regs) (0)
87 #define restore_fp_state(task, regs) (0)
90 static long restore_sigcontext(struct pt_regs *regs,
91 struct sigcontext __user *sc)
94 /* sc_regs is structured the same as the start of pt_regs */
95 err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs));
96 /* Restore the floating-point state. */
98 err |= restore_fp_state(regs, &sc->sc_fpregs);
102 SYSCALL_DEFINE0(rt_sigreturn)
104 struct pt_regs *regs = current_pt_regs();
105 struct rt_sigframe __user *frame;
106 struct task_struct *task;
109 /* Always make any pending restarted system calls return -EINTR */
110 current->restart_block.fn = do_no_restart_syscall;
112 frame = (struct rt_sigframe __user *)regs->sp;
114 if (!access_ok(frame, sizeof(*frame)))
117 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
120 set_current_blocked(&set);
122 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
125 if (restore_altstack(&frame->uc.uc_stack))
134 if (show_unhandled_signals) {
136 "%s[%d]: bad frame in %s: frame=%p pc=%p sp=%p\n",
137 task->comm, task_pid_nr(task), __func__,
138 frame, (void *)regs->epc, (void *)regs->sp);
144 static long setup_sigcontext(struct rt_sigframe __user *frame,
145 struct pt_regs *regs)
147 struct sigcontext __user *sc = &frame->uc.uc_mcontext;
149 /* sc_regs is structured the same as the start of pt_regs */
150 err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs));
151 /* Save the floating-point state. */
153 err |= save_fp_state(regs, &sc->sc_fpregs);
157 static inline void __user *get_sigframe(struct ksignal *ksig,
158 struct pt_regs *regs, size_t framesize)
161 /* Default to using normal stack */
165 * If we are on the alternate signal stack and would overflow it, don't.
166 * Return an always-bogus address instead so we will die with SIGSEGV.
168 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
169 return (void __user __force *)(-1UL);
171 /* This is the X/Open sanctioned signal stack switching. */
172 sp = sigsp(sp, ksig) - framesize;
174 /* Align the stack frame. */
177 return (void __user *)sp;
180 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
181 struct pt_regs *regs)
183 struct rt_sigframe __user *frame;
185 unsigned long __maybe_unused addr;
187 frame = get_sigframe(ksig, regs, sizeof(*frame));
188 if (!access_ok(frame, sizeof(*frame)))
191 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
193 /* Create the ucontext. */
194 err |= __put_user(0, &frame->uc.uc_flags);
195 err |= __put_user(NULL, &frame->uc.uc_link);
196 err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
197 err |= setup_sigcontext(frame, regs);
198 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
202 /* Set up to return from userspace. */
204 regs->ra = (unsigned long)VDSO_SYMBOL(
205 current->mm->context.vdso, rt_sigreturn);
208 * For the nommu case we don't have a VDSO. Instead we push two
209 * instructions to call the rt_sigreturn syscall onto the user stack.
211 if (copy_to_user(&frame->sigreturn_code, __user_rt_sigreturn,
212 sizeof(frame->sigreturn_code)))
215 addr = (unsigned long)&frame->sigreturn_code;
216 /* Make sure the two instructions are pushed to icache. */
217 flush_icache_range(addr, addr + sizeof(frame->sigreturn_code));
220 #endif /* CONFIG_MMU */
223 * Set up registers for signal handler.
224 * Registers that we don't modify keep the value they had from
225 * user-space at the time we took the signal.
226 * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
227 * since some things rely on this (e.g. glibc's debug/segfault.c).
229 regs->epc = (unsigned long)ksig->ka.sa.sa_handler;
230 regs->sp = (unsigned long)frame;
231 regs->a0 = ksig->sig; /* a0: signal number */
232 regs->a1 = (unsigned long)(&frame->info); /* a1: siginfo pointer */
233 regs->a2 = (unsigned long)(&frame->uc); /* a2: ucontext pointer */
236 pr_info("SIG deliver (%s:%d): sig=%d pc=%p ra=%p sp=%p\n",
237 current->comm, task_pid_nr(current), ksig->sig,
238 (void *)regs->epc, (void *)regs->ra, frame);
244 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
246 sigset_t *oldset = sigmask_to_save();
249 /* Are we from a system call? */
250 if (regs->cause == EXC_SYSCALL) {
251 /* Avoid additional syscall restarting via ret_from_exception */
253 /* If so, check system call restarting.. */
255 case -ERESTART_RESTARTBLOCK:
256 case -ERESTARTNOHAND:
261 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
266 case -ERESTARTNOINTR:
267 regs->a0 = regs->orig_a0;
273 rseq_signal_deliver(ksig, regs);
275 /* Set up the stack frame */
276 if (is_compat_task())
277 ret = compat_setup_rt_frame(ksig, oldset, regs);
279 ret = setup_rt_frame(ksig, oldset, regs);
281 signal_setup_done(ret, ksig, 0);
284 static void do_signal(struct pt_regs *regs)
288 if (get_signal(&ksig)) {
289 /* Actually deliver the signal */
290 handle_signal(&ksig, regs);
294 /* Did we come from a system call? */
295 if (regs->cause == EXC_SYSCALL) {
296 /* Avoid additional syscall restarting via ret_from_exception */
299 /* Restart the system call - no handlers present */
301 case -ERESTARTNOHAND:
303 case -ERESTARTNOINTR:
304 regs->a0 = regs->orig_a0;
307 case -ERESTART_RESTARTBLOCK:
308 regs->a0 = regs->orig_a0;
309 regs->a7 = __NR_restart_syscall;
316 * If there is no signal to deliver, we just put the saved
319 restore_saved_sigmask();
323 * Handle any pending work on the resume-to-userspace path, as indicated by
324 * _TIF_WORK_MASK. Entered from assembly with IRQs off.
326 asmlinkage __visible void do_work_pending(struct pt_regs *regs,
327 unsigned long thread_info_flags)
330 if (thread_info_flags & _TIF_NEED_RESCHED) {
334 if (thread_info_flags & _TIF_UPROBE)
335 uprobe_notify_resume(regs);
336 /* Handle pending signal delivery */
337 if (thread_info_flags & (_TIF_SIGPENDING |
340 if (thread_info_flags & _TIF_NOTIFY_RESUME)
341 resume_user_mode_work(regs);
344 thread_info_flags = read_thread_flags();
345 } while (thread_info_flags & _TIF_WORK_MASK);