hugetlb: introduce generic version of hugetlb_free_pgd_range
[linux/fpc-iii.git] / arch / riscv / kernel / signal.c
blobf9b5e7e352ef7c489a582edf4982cfddeb1e457a
1 /*
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>
29 #include <asm/vdso.h>
30 #include <asm/switch_to.h>
31 #include <asm/csr.h>
33 #define DEBUG_SIG 0
35 struct rt_sigframe {
36 struct siginfo info;
37 struct ucontext uc;
40 #ifdef CONFIG_FPU
41 static long restore_fp_state(struct pt_regs *regs,
42 union __riscv_fp_state *sc_fpregs)
44 long err;
45 struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
46 size_t i;
48 err = __copy_from_user(&current->thread.fstate, state, sizeof(*state));
49 if (unlikely(err))
50 return err;
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++) {
56 u32 value;
58 err = __get_user(value, &sc_fpregs->q.reserved[i]);
59 if (unlikely(err))
60 break;
61 if (value != 0)
62 return -EINVAL;
65 return err;
68 static long save_fp_state(struct pt_regs *regs,
69 union __riscv_fp_state *sc_fpregs)
71 long err;
72 struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
73 size_t i;
75 fstate_save(current, regs);
76 err = __copy_to_user(state, &current->thread.fstate, sizeof(*state));
77 if (unlikely(err))
78 return err;
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]);
83 if (unlikely(err))
84 break;
87 return err;
89 #else
90 #define save_fp_state(task, regs) (0)
91 #define restore_fp_state(task, regs) (0)
92 #endif
94 static long restore_sigcontext(struct pt_regs *regs,
95 struct sigcontext __user *sc)
97 long err;
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. */
101 if (has_fpu)
102 err |= restore_fp_state(regs, &sc->sc_fpregs);
103 return err;
106 SYSCALL_DEFINE0(rt_sigreturn)
108 struct pt_regs *regs = current_pt_regs();
109 struct rt_sigframe __user *frame;
110 struct task_struct *task;
111 sigset_t set;
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(VERIFY_READ, frame, sizeof(*frame)))
119 goto badframe;
121 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
122 goto badframe;
124 set_current_blocked(&set);
126 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
127 goto badframe;
129 if (restore_altstack(&frame->uc.uc_stack))
130 goto badframe;
132 return regs->a0;
134 badframe:
135 task = current;
136 if (show_unhandled_signals) {
137 pr_info_ratelimited(
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);
143 return 0;
146 static long setup_sigcontext(struct rt_sigframe __user *frame,
147 struct pt_regs *regs)
149 struct sigcontext __user *sc = &frame->uc.uc_mcontext;
150 long err;
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. */
154 if (has_fpu)
155 err |= save_fp_state(regs, &sc->sc_fpregs);
156 return err;
159 static inline void __user *get_sigframe(struct ksignal *ksig,
160 struct pt_regs *regs, size_t framesize)
162 unsigned long sp;
163 /* Default to using normal stack */
164 sp = regs->sp;
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. */
177 sp &= ~0xfUL;
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;
187 long err = 0;
189 frame = get_sigframe(ksig, regs, sizeof(*frame));
190 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
191 return -EFAULT;
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));
201 if (err)
202 return -EFAULT;
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 */
221 #if DEBUG_SIG
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);
225 #endif
227 return 0;
230 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
232 sigset_t *oldset = sigmask_to_save();
233 int ret;
235 /* Are we from a system call? */
236 if (regs->scause == EXC_SYSCALL) {
237 /* If so, check system call restarting.. */
238 switch (regs->a0) {
239 case -ERESTART_RESTARTBLOCK:
240 case -ERESTARTNOHAND:
241 regs->a0 = -EINTR;
242 break;
244 case -ERESTARTSYS:
245 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
246 regs->a0 = -EINTR;
247 break;
249 /* fallthrough */
250 case -ERESTARTNOINTR:
251 regs->a0 = regs->orig_a0;
252 regs->sepc -= 0x4;
253 break;
257 /* Set up the stack frame */
258 ret = setup_rt_frame(ksig, oldset, regs);
260 signal_setup_done(ret, ksig, 0);
263 static void do_signal(struct pt_regs *regs)
265 struct ksignal ksig;
267 if (get_signal(&ksig)) {
268 /* Actually deliver the signal */
269 handle_signal(&ksig, regs);
270 return;
273 /* Did we come from a system call? */
274 if (regs->scause == EXC_SYSCALL) {
275 /* Restart the system call - no handlers present */
276 switch (regs->a0) {
277 case -ERESTARTNOHAND:
278 case -ERESTARTSYS:
279 case -ERESTARTNOINTR:
280 regs->a0 = regs->orig_a0;
281 regs->sepc -= 0x4;
282 break;
283 case -ERESTART_RESTARTBLOCK:
284 regs->a0 = regs->orig_a0;
285 regs->a7 = __NR_restart_syscall;
286 regs->sepc -= 0x4;
287 break;
292 * If there is no signal to deliver, we just put the saved
293 * sigmask back.
295 restore_saved_sigmask();
299 * notification of userspace execution resumption
300 * - triggered by the _TIF_WORK_MASK flags
302 asmlinkage void do_notify_resume(struct pt_regs *regs,
303 unsigned long thread_info_flags)
305 /* Handle pending signal delivery */
306 if (thread_info_flags & _TIF_SIGPENDING)
307 do_signal(regs);
309 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
310 clear_thread_flag(TIF_NOTIFY_RESUME);
311 tracehook_notify_resume(regs);