2 * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
3 * Chen Liqin <liqin.chen@sunplusct.com>
4 * Lennox Wu <lennox.wu@sunplusct.com>
5 * Copyright (C) 2012 Regents of the University of California
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.
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.
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.,
22 #include <linux/signal.h>
23 #include <linux/uaccess.h>
24 #include <linux/syscalls.h>
25 #include <linux/tracehook.h>
26 #include <linux/linkage.h>
28 #include <asm/ucontext.h>
30 #include <asm/switch_to.h>
41 static long restore_fp_state(struct pt_regs *regs,
42 union __riscv_fp_state *sc_fpregs)
45 struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
48 err = __copy_from_user(¤t->thread.fstate, state, sizeof(*state));
52 fstate_restore(current, regs);
54 /* We support no other extension state at this time. */
55 for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) {
58 err = __get_user(value, &sc_fpregs->q.reserved[i]);
68 static long save_fp_state(struct pt_regs *regs,
69 union __riscv_fp_state *sc_fpregs)
72 struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
75 fstate_save(current, regs);
76 err = __copy_to_user(state, ¤t->thread.fstate, sizeof(*state));
80 /* We support no other extension state at this time. */
81 for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) {
82 err = __put_user(0, &sc_fpregs->q.reserved[i]);
90 #define save_fp_state(task, regs) (0)
91 #define restore_fp_state(task, regs) (0)
94 static long restore_sigcontext(struct pt_regs *regs,
95 struct sigcontext __user *sc)
98 /* sc_regs is structured the same as the start of pt_regs */
99 err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs));
100 /* Restore the floating-point state. */
102 err |= restore_fp_state(regs, &sc->sc_fpregs);
106 SYSCALL_DEFINE0(rt_sigreturn)
108 struct pt_regs *regs = current_pt_regs();
109 struct rt_sigframe __user *frame;
110 struct task_struct *task;
113 /* Always make any pending restarted system calls return -EINTR */
114 current->restart_block.fn = do_no_restart_syscall;
116 frame = (struct rt_sigframe __user *)regs->sp;
118 if (!access_ok(frame, sizeof(*frame)))
121 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
124 set_current_blocked(&set);
126 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
129 if (restore_altstack(&frame->uc.uc_stack))
136 if (show_unhandled_signals) {
138 "%s[%d]: bad frame in %s: frame=%p pc=%p sp=%p\n",
139 task->comm, task_pid_nr(task), __func__,
140 frame, (void *)regs->sepc, (void *)regs->sp);
142 force_sig(SIGSEGV, task);
146 static long setup_sigcontext(struct rt_sigframe __user *frame,
147 struct pt_regs *regs)
149 struct sigcontext __user *sc = &frame->uc.uc_mcontext;
151 /* sc_regs is structured the same as the start of pt_regs */
152 err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs));
153 /* Save the floating-point state. */
155 err |= save_fp_state(regs, &sc->sc_fpregs);
159 static inline void __user *get_sigframe(struct ksignal *ksig,
160 struct pt_regs *regs, size_t framesize)
163 /* Default to using normal stack */
167 * If we are on the alternate signal stack and would overflow it, don't.
168 * Return an always-bogus address instead so we will die with SIGSEGV.
170 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
171 return (void __user __force *)(-1UL);
173 /* This is the X/Open sanctioned signal stack switching. */
174 sp = sigsp(sp, ksig) - framesize;
176 /* Align the stack frame. */
179 return (void __user *)sp;
183 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
184 struct pt_regs *regs)
186 struct rt_sigframe __user *frame;
189 frame = get_sigframe(ksig, regs, sizeof(*frame));
190 if (!access_ok(frame, sizeof(*frame)))
193 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
195 /* Create the ucontext. */
196 err |= __put_user(0, &frame->uc.uc_flags);
197 err |= __put_user(NULL, &frame->uc.uc_link);
198 err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
199 err |= setup_sigcontext(frame, regs);
200 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
204 /* Set up to return from userspace. */
205 regs->ra = (unsigned long)VDSO_SYMBOL(
206 current->mm->context.vdso, rt_sigreturn);
209 * Set up registers for signal handler.
210 * Registers that we don't modify keep the value they had from
211 * user-space at the time we took the signal.
212 * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
213 * since some things rely on this (e.g. glibc's debug/segfault.c).
215 regs->sepc = (unsigned long)ksig->ka.sa.sa_handler;
216 regs->sp = (unsigned long)frame;
217 regs->a0 = ksig->sig; /* a0: signal number */
218 regs->a1 = (unsigned long)(&frame->info); /* a1: siginfo pointer */
219 regs->a2 = (unsigned long)(&frame->uc); /* a2: ucontext pointer */
222 pr_info("SIG deliver (%s:%d): sig=%d pc=%p ra=%p sp=%p\n",
223 current->comm, task_pid_nr(current), ksig->sig,
224 (void *)regs->sepc, (void *)regs->ra, frame);
230 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
232 sigset_t *oldset = sigmask_to_save();
235 /* Are we from a system call? */
236 if (regs->scause == EXC_SYSCALL) {
237 /* Avoid additional syscall restarting via ret_from_exception */
240 /* If so, check system call restarting.. */
242 case -ERESTART_RESTARTBLOCK:
243 case -ERESTARTNOHAND:
248 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
253 case -ERESTARTNOINTR:
254 regs->a0 = regs->orig_a0;
260 /* Set up the stack frame */
261 ret = setup_rt_frame(ksig, oldset, regs);
263 signal_setup_done(ret, ksig, 0);
266 static void do_signal(struct pt_regs *regs)
270 if (get_signal(&ksig)) {
271 /* Actually deliver the signal */
272 handle_signal(&ksig, regs);
276 /* Did we come from a system call? */
277 if (regs->scause == EXC_SYSCALL) {
278 /* Avoid additional syscall restarting via ret_from_exception */
281 /* Restart the system call - no handlers present */
283 case -ERESTARTNOHAND:
285 case -ERESTARTNOINTR:
286 regs->a0 = regs->orig_a0;
289 case -ERESTART_RESTARTBLOCK:
290 regs->a0 = regs->orig_a0;
291 regs->a7 = __NR_restart_syscall;
298 * If there is no signal to deliver, we just put the saved
301 restore_saved_sigmask();
305 * notification of userspace execution resumption
306 * - triggered by the _TIF_WORK_MASK flags
308 asmlinkage void do_notify_resume(struct pt_regs *regs,
309 unsigned long thread_info_flags)
311 /* Handle pending signal delivery */
312 if (thread_info_flags & _TIF_SIGPENDING)
315 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
316 clear_thread_flag(TIF_NOTIFY_RESUME);
317 tracehook_notify_resume(regs);