xref: /linux/arch/openrisc/kernel/signal.c (revision 570f7e331f5febb30f1384817463c7e42b65ca7d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * OpenRISC signal.c
4  *
5  * Linux architectural port borrowing liberally from similar works of
6  * others.  All original copyrights apply as per the original source
7  * declaration.
8  *
9  * Modifications for the OpenRISC architecture:
10  * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
11  * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
12  */
13 
14 #include <linux/sched.h>
15 #include <linux/mm.h>
16 #include <linux/smp.h>
17 #include <linux/kernel.h>
18 #include <linux/signal.h>
19 #include <linux/errno.h>
20 #include <linux/wait.h>
21 #include <linux/ptrace.h>
22 #include <linux/unistd.h>
23 #include <linux/stddef.h>
24 #include <linux/resume_user_mode.h>
25 
26 #include <asm/fpu.h>
27 #include <asm/processor.h>
28 #include <asm/syscall.h>
29 #include <asm/ucontext.h>
30 #include <linux/uaccess.h>
31 
32 struct rt_sigframe {
33 	struct siginfo info;
34 	struct ucontext uc;
35 	unsigned char retcode[16];	/* trampoline code */
36 };
37 
38 asmlinkage long _sys_rt_sigreturn(struct pt_regs *regs);
39 
40 asmlinkage int do_work_pending(struct pt_regs *regs, unsigned int thread_flags,
41 			       int syscall);
42 
43 #ifdef CONFIG_FPU
44 static long restore_fp_state(struct sigcontext __user *sc)
45 {
46 	long err;
47 
48 	err = __copy_from_user(&current->thread.fpcsr, &sc->fpcsr, sizeof(unsigned long));
49 	if (unlikely(err))
50 		return err;
51 
52 	/* Restore the FPU state */
53 	restore_fpu(current);
54 
55 	return 0;
56 }
57 
58 static long save_fp_state(struct sigcontext __user *sc)
59 {
60 	long err;
61 
62 	/* Sync the user FPU state so we can copy to sigcontext */
63 	save_fpu(current);
64 
65 	err = __copy_to_user(&sc->fpcsr, &current->thread.fpcsr, sizeof(unsigned long));
66 
67 	return err;
68 }
69 #else
70 #define save_fp_state(sc) (0)
71 #define restore_fp_state(sc) (0)
72 #endif
73 
74 static int restore_sigcontext(struct pt_regs *regs,
75 			      struct sigcontext __user *sc)
76 {
77 	unsigned long old_sr = regs->sr;
78 	int err = 0;
79 
80 	/* Always make any pending restarted system calls return -EINTR */
81 	current->restart_block.fn = do_no_restart_syscall;
82 
83 	/*
84 	 * Restore the regs from &sc->regs.
85 	 * (sc is already checked since the sigframe was
86 	 *  checked in sys_sigreturn previously)
87 	 */
88 	err |= __copy_from_user(regs, sc->regs.gpr, 32 * sizeof(unsigned long));
89 	err |= __copy_from_user(&regs->pc, &sc->regs.pc, sizeof(unsigned long));
90 	err |= __copy_from_user(&regs->sr, &sc->regs.sr, sizeof(unsigned long));
91 	err |= restore_fp_state(sc);
92 
93 	/* keep the privileged SR bits kernel owned, restore only user flags */
94 	regs->sr = (old_sr & ~SPR_SR_USER_MASK) | (regs->sr & SPR_SR_USER_MASK);
95 
96 	regs->orig_gpr11 = -1;	/* Avoid syscall restart checks */
97 
98 	/* TODO: the other ports use regs->orig_XX to disable syscall checks
99 	 * after this completes, but we don't use that mechanism. maybe we can
100 	 * use it now ?
101 	 */
102 
103 	return err;
104 }
105 
106 asmlinkage long _sys_rt_sigreturn(struct pt_regs *regs)
107 {
108 	struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->sp;
109 	sigset_t set;
110 
111 	/*
112 	 * Since we stacked the signal on a dword boundary,
113 	 * then frame should be dword aligned here.  If it's
114 	 * not, then the user is trying to mess with us.
115 	 */
116 	if (((unsigned long)frame) & 3)
117 		goto badframe;
118 
119 	if (!access_ok(frame, sizeof(*frame)))
120 		goto badframe;
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->gpr[11];
133 
134 badframe:
135 	force_sig(SIGSEGV);
136 	return 0;
137 }
138 
139 /*
140  * Set up a signal frame.
141  */
142 
143 static int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
144 {
145 	int err = 0;
146 
147 	/* copy the regs */
148 	/* There should be no need to save callee-saved registers here...
149 	 * ...but we save them anyway.  Revisit this
150 	 */
151 	err |= __copy_to_user(sc->regs.gpr, regs, 32 * sizeof(unsigned long));
152 	err |= __copy_to_user(&sc->regs.pc, &regs->pc, sizeof(unsigned long));
153 	err |= __copy_to_user(&sc->regs.sr, &regs->sr, sizeof(unsigned long));
154 	err |= save_fp_state(sc);
155 
156 	return err;
157 }
158 
159 static inline unsigned long align_sigframe(unsigned long sp)
160 {
161 	return sp & ~3UL;
162 }
163 
164 /*
165  * Work out where the signal frame should go.  It's either on the user stack
166  * or the alternate stack.
167  */
168 
169 static inline void __user *get_sigframe(struct ksignal *ksig,
170 					struct pt_regs *regs, size_t frame_size)
171 {
172 	unsigned long sp = regs->sp;
173 
174 	/* redzone */
175 	sp -= STACK_FRAME_OVERHEAD;
176 	sp = sigsp(sp, ksig);
177 	sp = align_sigframe(sp - frame_size);
178 
179 	return (void __user *)sp;
180 }
181 
182 /* grab and setup a signal frame.
183  *
184  * basically we stack a lot of state info, and arrange for the
185  * user-mode program to return to the kernel using either a
186  * trampoline which performs the syscall sigreturn, or a provided
187  * user-mode trampoline.
188  */
189 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
190 			  struct pt_regs *regs)
191 {
192 	struct rt_sigframe __user *frame;
193 	unsigned long return_ip;
194 	int err = 0;
195 
196 	frame = get_sigframe(ksig, regs, sizeof(*frame));
197 
198 	if (!access_ok(frame, sizeof(*frame)))
199 		return -EFAULT;
200 
201 	/* Create siginfo.  */
202 	if (ksig->ka.sa.sa_flags & SA_SIGINFO)
203 		err |= copy_siginfo_to_user(&frame->info, &ksig->info);
204 
205 	/* Create the ucontext.  */
206 	err |= __put_user(0, &frame->uc.uc_flags);
207 	err |= __put_user(NULL, &frame->uc.uc_link);
208 	err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
209 	err |= setup_sigcontext(regs, &frame->uc.uc_mcontext);
210 
211 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
212 
213 	if (err)
214 		return -EFAULT;
215 
216 	/* trampoline - the desired return ip is the retcode itself */
217 	return_ip = (unsigned long)&frame->retcode;
218 	/* This is:
219 		l.ori r11,r0,__NR_sigreturn
220 		l.sys 1
221 	 */
222 	err |= __put_user(0xa960,             (short __user *)(frame->retcode + 0));
223 	err |= __put_user(__NR_rt_sigreturn,  (short __user *)(frame->retcode + 2));
224 	err |= __put_user(0x20000001, (unsigned long __user *)(frame->retcode + 4));
225 	err |= __put_user(0x15000000, (unsigned long __user *)(frame->retcode + 8));
226 
227 	if (err)
228 		return -EFAULT;
229 
230 	/* Set up registers for signal handler */
231 	regs->pc = (unsigned long)ksig->ka.sa.sa_handler; /* what we enter NOW */
232 	regs->gpr[9] = (unsigned long)return_ip;     /* what we enter LATER */
233 	regs->gpr[3] = (unsigned long)ksig->sig;           /* arg 1: signo */
234 	regs->gpr[4] = (unsigned long)&frame->info;  /* arg 2: (siginfo_t*) */
235 	regs->gpr[5] = (unsigned long)&frame->uc;    /* arg 3: ucontext */
236 
237 	/* actually move the usp to reflect the stacked frame */
238 	regs->sp = (unsigned long)frame;
239 
240 	return 0;
241 }
242 
243 static inline void
244 handle_signal(struct ksignal *ksig, struct pt_regs *regs)
245 {
246 	int ret;
247 
248 	rseq_signal_deliver(ksig, regs);
249 
250 	ret = setup_rt_frame(ksig, sigmask_to_save(), regs);
251 
252 	signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
253 }
254 
255 /*
256  * Note that 'init' is a special process: it doesn't get signals it doesn't
257  * want to handle. Thus you cannot kill init even with a SIGKILL even by
258  * mistake.
259  *
260  * Also note that the regs structure given here as an argument, is the latest
261  * pushed pt_regs. It may or may not be the same as the first pushed registers
262  * when the initial usermode->kernelmode transition took place. Therefore
263  * we can use user_mode(regs) to see if we came directly from kernel or user
264  * mode below.
265  */
266 
267 static int do_signal(struct pt_regs *regs, int syscall)
268 {
269 	struct ksignal ksig;
270 	unsigned long continue_addr = 0;
271 	unsigned long restart_addr = 0;
272 	unsigned long retval = 0;
273 	int restart = 0;
274 
275 	if (syscall) {
276 		continue_addr = regs->pc;
277 		restart_addr = continue_addr - 4;
278 		retval = regs->gpr[11];
279 
280 		/*
281 		 * Setup syscall restart here so that a debugger will
282 		 * see the already changed PC.
283 		 */
284 		switch (retval) {
285 		case -ERESTART_RESTARTBLOCK:
286 			restart = -2;
287 			fallthrough;
288 		case -ERESTARTNOHAND:
289 		case -ERESTARTSYS:
290 		case -ERESTARTNOINTR:
291 			restart++;
292 			regs->gpr[11] = regs->orig_gpr11;
293 			regs->pc = restart_addr;
294 			break;
295 		}
296 	}
297 
298 	/*
299 	 * Get the signal to deliver.  During the call to get_signal the
300 	 * debugger may change all our registers so we may need to revert
301 	 * the decision to restart the syscall; specifically, if the PC is
302 	 * changed, don't restart the syscall.
303 	 */
304 	if (get_signal(&ksig)) {
305 		if (unlikely(restart) && regs->pc == restart_addr) {
306 			if (retval == -ERESTARTNOHAND ||
307 			    retval == -ERESTART_RESTARTBLOCK
308 			    || (retval == -ERESTARTSYS
309 			        && !(ksig.ka.sa.sa_flags & SA_RESTART))) {
310 				/* No automatic restart */
311 				regs->gpr[11] = -EINTR;
312 				regs->pc = continue_addr;
313 			}
314 		}
315 		handle_signal(&ksig, regs);
316 	} else {
317 		/* no handler */
318 		restore_saved_sigmask();
319 		/*
320 		 * Restore pt_regs PC as syscall restart will be handled by
321 		 * kernel without return to userspace
322 		 */
323 		if (unlikely(restart) && regs->pc == restart_addr) {
324 			regs->pc = continue_addr;
325 			return restart;
326 		}
327 	}
328 
329 	return 0;
330 }
331 
332 asmlinkage int
333 do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
334 {
335 	do {
336 		if (likely(thread_flags & _TIF_NEED_RESCHED)) {
337 			schedule();
338 		} else {
339 			if (unlikely(!user_mode(regs)))
340 				return 0;
341 			local_irq_enable();
342 			if (thread_flags & (_TIF_SIGPENDING|_TIF_NOTIFY_SIGNAL)) {
343 				int restart = do_signal(regs, syscall);
344 				if (unlikely(restart)) {
345 					/*
346 					 * Restart without handlers.
347 					 * Deal with it without leaving
348 					 * the kernel space.
349 					 */
350 					return restart;
351 				}
352 				syscall = 0;
353 			} else {
354 				resume_user_mode_work(regs);
355 			}
356 		}
357 		local_irq_disable();
358 		thread_flags = read_thread_flags();
359 	} while (thread_flags & _TIF_WORK_MASK);
360 	return 0;
361 }
362