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