xref: /openbmc/linux/arch/riscv/kernel/signal.c (revision 54a611b6)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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
7  */
8 
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 
16 #include <asm/ucontext.h>
17 #include <asm/vdso.h>
18 #include <asm/signal.h>
19 #include <asm/signal32.h>
20 #include <asm/switch_to.h>
21 #include <asm/csr.h>
22 
23 extern u32 __user_rt_sigreturn[2];
24 
25 #define DEBUG_SIG 0
26 
27 struct rt_sigframe {
28 	struct siginfo info;
29 	struct ucontext uc;
30 #ifndef CONFIG_MMU
31 	u32 sigreturn_code[2];
32 #endif
33 };
34 
35 #ifdef CONFIG_FPU
36 static long restore_fp_state(struct pt_regs *regs,
37 			     union __riscv_fp_state __user *sc_fpregs)
38 {
39 	long err;
40 	struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
41 	size_t i;
42 
43 	err = __copy_from_user(&current->thread.fstate, state, sizeof(*state));
44 	if (unlikely(err))
45 		return err;
46 
47 	fstate_restore(current, regs);
48 
49 	/* We support no other extension state at this time. */
50 	for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) {
51 		u32 value;
52 
53 		err = __get_user(value, &sc_fpregs->q.reserved[i]);
54 		if (unlikely(err))
55 			break;
56 		if (value != 0)
57 			return -EINVAL;
58 	}
59 
60 	return err;
61 }
62 
63 static long save_fp_state(struct pt_regs *regs,
64 			  union __riscv_fp_state __user *sc_fpregs)
65 {
66 	long err;
67 	struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
68 	size_t i;
69 
70 	fstate_save(current, regs);
71 	err = __copy_to_user(state, &current->thread.fstate, sizeof(*state));
72 	if (unlikely(err))
73 		return err;
74 
75 	/* We support no other extension state at this time. */
76 	for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) {
77 		err = __put_user(0, &sc_fpregs->q.reserved[i]);
78 		if (unlikely(err))
79 			break;
80 	}
81 
82 	return err;
83 }
84 #else
85 #define save_fp_state(task, regs) (0)
86 #define restore_fp_state(task, regs) (0)
87 #endif
88 
89 static long restore_sigcontext(struct pt_regs *regs,
90 	struct sigcontext __user *sc)
91 {
92 	long err;
93 	/* sc_regs is structured the same as the start of pt_regs */
94 	err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs));
95 	/* Restore the floating-point state. */
96 	if (has_fpu())
97 		err |= restore_fp_state(regs, &sc->sc_fpregs);
98 	return err;
99 }
100 
101 SYSCALL_DEFINE0(rt_sigreturn)
102 {
103 	struct pt_regs *regs = current_pt_regs();
104 	struct rt_sigframe __user *frame;
105 	struct task_struct *task;
106 	sigset_t set;
107 
108 	/* Always make any pending restarted system calls return -EINTR */
109 	current->restart_block.fn = do_no_restart_syscall;
110 
111 	frame = (struct rt_sigframe __user *)regs->sp;
112 
113 	if (!access_ok(frame, sizeof(*frame)))
114 		goto badframe;
115 
116 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
117 		goto badframe;
118 
119 	set_current_blocked(&set);
120 
121 	if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
122 		goto badframe;
123 
124 	if (restore_altstack(&frame->uc.uc_stack))
125 		goto badframe;
126 
127 	return regs->a0;
128 
129 badframe:
130 	task = current;
131 	if (show_unhandled_signals) {
132 		pr_info_ratelimited(
133 			"%s[%d]: bad frame in %s: frame=%p pc=%p sp=%p\n",
134 			task->comm, task_pid_nr(task), __func__,
135 			frame, (void *)regs->epc, (void *)regs->sp);
136 	}
137 	force_sig(SIGSEGV);
138 	return 0;
139 }
140 
141 static long setup_sigcontext(struct rt_sigframe __user *frame,
142 	struct pt_regs *regs)
143 {
144 	struct sigcontext __user *sc = &frame->uc.uc_mcontext;
145 	long err;
146 	/* sc_regs is structured the same as the start of pt_regs */
147 	err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs));
148 	/* Save the floating-point state. */
149 	if (has_fpu())
150 		err |= save_fp_state(regs, &sc->sc_fpregs);
151 	return err;
152 }
153 
154 static inline void __user *get_sigframe(struct ksignal *ksig,
155 	struct pt_regs *regs, size_t framesize)
156 {
157 	unsigned long sp;
158 	/* Default to using normal stack */
159 	sp = regs->sp;
160 
161 	/*
162 	 * If we are on the alternate signal stack and would overflow it, don't.
163 	 * Return an always-bogus address instead so we will die with SIGSEGV.
164 	 */
165 	if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
166 		return (void __user __force *)(-1UL);
167 
168 	/* This is the X/Open sanctioned signal stack switching. */
169 	sp = sigsp(sp, ksig) - framesize;
170 
171 	/* Align the stack frame. */
172 	sp &= ~0xfUL;
173 
174 	return (void __user *)sp;
175 }
176 
177 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
178 	struct pt_regs *regs)
179 {
180 	struct rt_sigframe __user *frame;
181 	long err = 0;
182 
183 	frame = get_sigframe(ksig, regs, sizeof(*frame));
184 	if (!access_ok(frame, sizeof(*frame)))
185 		return -EFAULT;
186 
187 	err |= copy_siginfo_to_user(&frame->info, &ksig->info);
188 
189 	/* Create the ucontext. */
190 	err |= __put_user(0, &frame->uc.uc_flags);
191 	err |= __put_user(NULL, &frame->uc.uc_link);
192 	err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
193 	err |= setup_sigcontext(frame, regs);
194 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
195 	if (err)
196 		return -EFAULT;
197 
198 	/* Set up to return from userspace. */
199 #ifdef CONFIG_MMU
200 	regs->ra = (unsigned long)VDSO_SYMBOL(
201 		current->mm->context.vdso, rt_sigreturn);
202 #else
203 	/*
204 	 * For the nommu case we don't have a VDSO.  Instead we push two
205 	 * instructions to call the rt_sigreturn syscall onto the user stack.
206 	 */
207 	if (copy_to_user(&frame->sigreturn_code, __user_rt_sigreturn,
208 			 sizeof(frame->sigreturn_code)))
209 		return -EFAULT;
210 	regs->ra = (unsigned long)&frame->sigreturn_code;
211 #endif /* CONFIG_MMU */
212 
213 	/*
214 	 * Set up registers for signal handler.
215 	 * Registers that we don't modify keep the value they had from
216 	 * user-space at the time we took the signal.
217 	 * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
218 	 * since some things rely on this (e.g. glibc's debug/segfault.c).
219 	 */
220 	regs->epc = (unsigned long)ksig->ka.sa.sa_handler;
221 	regs->sp = (unsigned long)frame;
222 	regs->a0 = ksig->sig;                     /* a0: signal number */
223 	regs->a1 = (unsigned long)(&frame->info); /* a1: siginfo pointer */
224 	regs->a2 = (unsigned long)(&frame->uc);   /* a2: ucontext pointer */
225 
226 #if DEBUG_SIG
227 	pr_info("SIG deliver (%s:%d): sig=%d pc=%p ra=%p sp=%p\n",
228 		current->comm, task_pid_nr(current), ksig->sig,
229 		(void *)regs->epc, (void *)regs->ra, frame);
230 #endif
231 
232 	return 0;
233 }
234 
235 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
236 {
237 	sigset_t *oldset = sigmask_to_save();
238 	int ret;
239 
240 	/* Are we from a system call? */
241 	if (regs->cause == EXC_SYSCALL) {
242 		/* Avoid additional syscall restarting via ret_from_exception */
243 		regs->cause = -1UL;
244 		/* If so, check system call restarting.. */
245 		switch (regs->a0) {
246 		case -ERESTART_RESTARTBLOCK:
247 		case -ERESTARTNOHAND:
248 			regs->a0 = -EINTR;
249 			break;
250 
251 		case -ERESTARTSYS:
252 			if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
253 				regs->a0 = -EINTR;
254 				break;
255 			}
256 			fallthrough;
257 		case -ERESTARTNOINTR:
258                         regs->a0 = regs->orig_a0;
259 			regs->epc -= 0x4;
260 			break;
261 		}
262 	}
263 
264 	rseq_signal_deliver(ksig, regs);
265 
266 	/* Set up the stack frame */
267 	if (is_compat_task())
268 		ret = compat_setup_rt_frame(ksig, oldset, regs);
269 	else
270 		ret = setup_rt_frame(ksig, oldset, regs);
271 
272 	signal_setup_done(ret, ksig, 0);
273 }
274 
275 static void do_signal(struct pt_regs *regs)
276 {
277 	struct ksignal ksig;
278 
279 	if (get_signal(&ksig)) {
280 		/* Actually deliver the signal */
281 		handle_signal(&ksig, regs);
282 		return;
283 	}
284 
285 	/* Did we come from a system call? */
286 	if (regs->cause == EXC_SYSCALL) {
287 		/* Avoid additional syscall restarting via ret_from_exception */
288 		regs->cause = -1UL;
289 
290 		/* Restart the system call - no handlers present */
291 		switch (regs->a0) {
292 		case -ERESTARTNOHAND:
293 		case -ERESTARTSYS:
294 		case -ERESTARTNOINTR:
295                         regs->a0 = regs->orig_a0;
296 			regs->epc -= 0x4;
297 			break;
298 		case -ERESTART_RESTARTBLOCK:
299                         regs->a0 = regs->orig_a0;
300 			regs->a7 = __NR_restart_syscall;
301 			regs->epc -= 0x4;
302 			break;
303 		}
304 	}
305 
306 	/*
307 	 * If there is no signal to deliver, we just put the saved
308 	 * sigmask back.
309 	 */
310 	restore_saved_sigmask();
311 }
312 
313 /*
314  * notification of userspace execution resumption
315  * - triggered by the _TIF_WORK_MASK flags
316  */
317 asmlinkage __visible void do_notify_resume(struct pt_regs *regs,
318 					   unsigned long thread_info_flags)
319 {
320 	if (thread_info_flags & _TIF_UPROBE)
321 		uprobe_notify_resume(regs);
322 
323 	/* Handle pending signal delivery */
324 	if (thread_info_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
325 		do_signal(regs);
326 
327 	if (thread_info_flags & _TIF_NOTIFY_RESUME)
328 		resume_user_mode_work(regs);
329 }
330