xref: /openbmc/linux/arch/riscv/kernel/signal.c (revision b664e06d)
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
6  *
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.
11  *
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.
16  *
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.,
20  */
21 
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>
27 
28 #include <asm/ucontext.h>
29 #include <asm/vdso.h>
30 #include <asm/switch_to.h>
31 #include <asm/csr.h>
32 
33 #define DEBUG_SIG 0
34 
35 struct rt_sigframe {
36 	struct siginfo info;
37 	struct ucontext uc;
38 };
39 
40 #ifdef CONFIG_FPU
41 static long restore_fp_state(struct pt_regs *regs,
42 			     union __riscv_fp_state *sc_fpregs)
43 {
44 	long err;
45 	struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
46 	size_t i;
47 
48 	err = __copy_from_user(&current->thread.fstate, state, sizeof(*state));
49 	if (unlikely(err))
50 		return err;
51 
52 	fstate_restore(current, regs);
53 
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;
57 
58 		err = __get_user(value, &sc_fpregs->q.reserved[i]);
59 		if (unlikely(err))
60 			break;
61 		if (value != 0)
62 			return -EINVAL;
63 	}
64 
65 	return err;
66 }
67 
68 static long save_fp_state(struct pt_regs *regs,
69 			  union __riscv_fp_state *sc_fpregs)
70 {
71 	long err;
72 	struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
73 	size_t i;
74 
75 	fstate_save(current, regs);
76 	err = __copy_to_user(state, &current->thread.fstate, sizeof(*state));
77 	if (unlikely(err))
78 		return err;
79 
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;
85 	}
86 
87 	return err;
88 }
89 #else
90 #define save_fp_state(task, regs) (0)
91 #define restore_fp_state(task, regs) (0)
92 #endif
93 
94 static long restore_sigcontext(struct pt_regs *regs,
95 	struct sigcontext __user *sc)
96 {
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;
104 }
105 
106 SYSCALL_DEFINE0(rt_sigreturn)
107 {
108 	struct pt_regs *regs = current_pt_regs();
109 	struct rt_sigframe __user *frame;
110 	struct task_struct *task;
111 	sigset_t set;
112 
113 	/* Always make any pending restarted system calls return -EINTR */
114 	current->restart_block.fn = do_no_restart_syscall;
115 
116 	frame = (struct rt_sigframe __user *)regs->sp;
117 
118 	if (!access_ok(frame, sizeof(*frame)))
119 		goto badframe;
120 
121 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
122 		goto badframe;
123 
124 	set_current_blocked(&set);
125 
126 	if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
127 		goto badframe;
128 
129 	if (restore_altstack(&frame->uc.uc_stack))
130 		goto badframe;
131 
132 	return regs->a0;
133 
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);
141 	}
142 	force_sig(SIGSEGV, task);
143 	return 0;
144 }
145 
146 static long setup_sigcontext(struct rt_sigframe __user *frame,
147 	struct pt_regs *regs)
148 {
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;
157 }
158 
159 static inline void __user *get_sigframe(struct ksignal *ksig,
160 	struct pt_regs *regs, size_t framesize)
161 {
162 	unsigned long sp;
163 	/* Default to using normal stack */
164 	sp = regs->sp;
165 
166 	/*
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.
169 	 */
170 	if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
171 		return (void __user __force *)(-1UL);
172 
173 	/* This is the X/Open sanctioned signal stack switching. */
174 	sp = sigsp(sp, ksig) - framesize;
175 
176 	/* Align the stack frame. */
177 	sp &= ~0xfUL;
178 
179 	return (void __user *)sp;
180 }
181 
182 
183 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
184 	struct pt_regs *regs)
185 {
186 	struct rt_sigframe __user *frame;
187 	long err = 0;
188 
189 	frame = get_sigframe(ksig, regs, sizeof(*frame));
190 	if (!access_ok(frame, sizeof(*frame)))
191 		return -EFAULT;
192 
193 	err |= copy_siginfo_to_user(&frame->info, &ksig->info);
194 
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;
203 
204 	/* Set up to return from userspace. */
205 	regs->ra = (unsigned long)VDSO_SYMBOL(
206 		current->mm->context.vdso, rt_sigreturn);
207 
208 	/*
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).
214 	 */
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 */
220 
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
226 
227 	return 0;
228 }
229 
230 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
231 {
232 	sigset_t *oldset = sigmask_to_save();
233 	int ret;
234 
235 	/* Are we from a system call? */
236 	if (regs->scause == EXC_SYSCALL) {
237 		/* Avoid additional syscall restarting via ret_from_exception */
238 		regs->scause = -1UL;
239 
240 		/* If so, check system call restarting.. */
241 		switch (regs->a0) {
242 		case -ERESTART_RESTARTBLOCK:
243 		case -ERESTARTNOHAND:
244 			regs->a0 = -EINTR;
245 			break;
246 
247 		case -ERESTARTSYS:
248 			if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
249 				regs->a0 = -EINTR;
250 				break;
251 			}
252 			/* fallthrough */
253 		case -ERESTARTNOINTR:
254                         regs->a0 = regs->orig_a0;
255 			regs->sepc -= 0x4;
256 			break;
257 		}
258 	}
259 
260 	/* Set up the stack frame */
261 	ret = setup_rt_frame(ksig, oldset, regs);
262 
263 	signal_setup_done(ret, ksig, 0);
264 }
265 
266 static void do_signal(struct pt_regs *regs)
267 {
268 	struct ksignal ksig;
269 
270 	if (get_signal(&ksig)) {
271 		/* Actually deliver the signal */
272 		handle_signal(&ksig, regs);
273 		return;
274 	}
275 
276 	/* Did we come from a system call? */
277 	if (regs->scause == EXC_SYSCALL) {
278 		/* Avoid additional syscall restarting via ret_from_exception */
279 		regs->scause = -1UL;
280 
281 		/* Restart the system call - no handlers present */
282 		switch (regs->a0) {
283 		case -ERESTARTNOHAND:
284 		case -ERESTARTSYS:
285 		case -ERESTARTNOINTR:
286                         regs->a0 = regs->orig_a0;
287 			regs->sepc -= 0x4;
288 			break;
289 		case -ERESTART_RESTARTBLOCK:
290                         regs->a0 = regs->orig_a0;
291 			regs->a7 = __NR_restart_syscall;
292 			regs->sepc -= 0x4;
293 			break;
294 		}
295 	}
296 
297 	/*
298 	 * If there is no signal to deliver, we just put the saved
299 	 * sigmask back.
300 	 */
301 	restore_saved_sigmask();
302 }
303 
304 /*
305  * notification of userspace execution resumption
306  * - triggered by the _TIF_WORK_MASK flags
307  */
308 asmlinkage void do_notify_resume(struct pt_regs *regs,
309 	unsigned long thread_info_flags)
310 {
311 	/* Handle pending signal delivery */
312 	if (thread_info_flags & _TIF_SIGPENDING)
313 		do_signal(regs);
314 
315 	if (thread_info_flags & _TIF_NOTIFY_RESUME) {
316 		clear_thread_flag(TIF_NOTIFY_RESUME);
317 		tracehook_notify_resume(regs);
318 	}
319 }
320