xref: /openbmc/linux/arch/xtensa/kernel/signal.c (revision 726ccdba)
1 /*
2  * arch/xtensa/kernel/signal.c
3  *
4  * Default platform functions.
5  *
6  * This file is subject to the terms and conditions of the GNU General Public
7  * License.  See the file "COPYING" in the main directory of this archive
8  * for more details.
9  *
10  * Copyright (C) 2005, 2006 Tensilica Inc.
11  * Copyright (C) 1991, 1992  Linus Torvalds
12  * 1997-11-28  Modified for POSIX.1b signals by Richard Henderson
13  *
14  * Chris Zankel <chris@zankel.net>
15  * Joe Taylor <joe@tensilica.com>
16  */
17 
18 #include <linux/signal.h>
19 #include <linux/errno.h>
20 #include <linux/ptrace.h>
21 #include <linux/personality.h>
22 #include <linux/resume_user_mode.h>
23 #include <linux/sched/task_stack.h>
24 
25 #include <asm/ucontext.h>
26 #include <linux/uaccess.h>
27 #include <asm/cacheflush.h>
28 #include <asm/coprocessor.h>
29 #include <asm/unistd.h>
30 
31 extern struct task_struct *coproc_owners[];
32 
33 struct rt_sigframe
34 {
35 	struct siginfo info;
36 	struct ucontext uc;
37 	struct {
38 		xtregs_opt_t opt;
39 		xtregs_user_t user;
40 #if XTENSA_HAVE_COPROCESSORS
41 		xtregs_coprocessor_t cp;
42 #endif
43 	} xtregs;
44 	unsigned char retcode[6];
45 	unsigned int window[4];
46 };
47 
48 #if defined(USER_SUPPORT_WINDOWED)
49 /*
50  * Flush register windows stored in pt_regs to stack.
51  * Returns 1 for errors.
52  */
53 
54 static int
55 flush_window_regs_user(struct pt_regs *regs)
56 {
57 	const unsigned long ws = regs->windowstart;
58 	const unsigned long wb = regs->windowbase;
59 	unsigned long sp = 0;
60 	unsigned long wm;
61 	int err = 1;
62 	int base;
63 
64 	/* Return if no other frames. */
65 
66 	if (regs->wmask == 1)
67 		return 0;
68 
69 	/* Rotate windowmask and skip empty frames. */
70 
71 	wm = (ws >> wb) | (ws << (XCHAL_NUM_AREGS / 4 - wb));
72 	base = (XCHAL_NUM_AREGS / 4) - (regs->wmask >> 4);
73 
74 	/* For call8 or call12 frames, we need the previous stack pointer. */
75 
76 	if ((regs->wmask & 2) == 0)
77 		if (__get_user(sp, (int*)(regs->areg[base * 4 + 1] - 12)))
78 			goto errout;
79 
80 	/* Spill frames to stack. */
81 
82 	while (base < XCHAL_NUM_AREGS / 4) {
83 
84 		int m = (wm >> base);
85 		int inc = 0;
86 
87 		/* Save registers a4..a7 (call8) or a4...a11 (call12) */
88 
89 		if (m & 2) {			/* call4 */
90 			inc = 1;
91 
92 		} else if (m & 4) {		/* call8 */
93 			if (copy_to_user(&SPILL_SLOT_CALL8(sp, 4),
94 					 &regs->areg[(base + 1) * 4], 16))
95 				goto errout;
96 			inc = 2;
97 
98 		} else if (m & 8) {	/* call12 */
99 			if (copy_to_user(&SPILL_SLOT_CALL12(sp, 4),
100 					 &regs->areg[(base + 1) * 4], 32))
101 				goto errout;
102 			inc = 3;
103 		}
104 
105 		/* Save current frame a0..a3 under next SP */
106 
107 		sp = regs->areg[((base + inc) * 4 + 1) % XCHAL_NUM_AREGS];
108 		if (copy_to_user(&SPILL_SLOT(sp, 0), &regs->areg[base * 4], 16))
109 			goto errout;
110 
111 		/* Get current stack pointer for next loop iteration. */
112 
113 		sp = regs->areg[base * 4 + 1];
114 		base += inc;
115 	}
116 
117 	regs->wmask = 1;
118 	regs->windowstart = 1 << wb;
119 
120 	return 0;
121 
122 errout:
123 	return err;
124 }
125 #else
126 static int
127 flush_window_regs_user(struct pt_regs *regs)
128 {
129 	return 0;
130 }
131 #endif
132 
133 /*
134  * Note: We don't copy double exception 'regs', we have to finish double exc.
135  * first before we return to signal handler! This dbl.exc.handler might cause
136  * another double exception, but I think we are fine as the situation is the
137  * same as if we had returned to the signal handerl and got an interrupt
138  * immediately...
139  */
140 
141 static int
142 setup_sigcontext(struct rt_sigframe __user *frame, struct pt_regs *regs)
143 {
144 	struct sigcontext __user *sc = &frame->uc.uc_mcontext;
145 	struct thread_info *ti = current_thread_info();
146 	int err = 0;
147 
148 #define COPY(x)	err |= __put_user(regs->x, &sc->sc_##x)
149 	COPY(pc);
150 	COPY(ps);
151 	COPY(lbeg);
152 	COPY(lend);
153 	COPY(lcount);
154 	COPY(sar);
155 #undef COPY
156 
157 	err |= flush_window_regs_user(regs);
158 	err |= __copy_to_user (sc->sc_a, regs->areg, 16 * 4);
159 	err |= __put_user(0, &sc->sc_xtregs);
160 
161 	if (err)
162 		return err;
163 
164 #if XTENSA_HAVE_COPROCESSORS
165 	coprocessor_flush_release_all(ti);
166 	err |= __copy_to_user(&frame->xtregs.cp, &ti->xtregs_cp,
167 			      sizeof (frame->xtregs.cp));
168 #endif
169 	err |= __copy_to_user(&frame->xtregs.opt, &regs->xtregs_opt,
170 			      sizeof (xtregs_opt_t));
171 	err |= __copy_to_user(&frame->xtregs.user, &ti->xtregs_user,
172 			      sizeof (xtregs_user_t));
173 
174 	err |= __put_user(err ? NULL : &frame->xtregs, &sc->sc_xtregs);
175 
176 	return err;
177 }
178 
179 static int
180 restore_sigcontext(struct pt_regs *regs, struct rt_sigframe __user *frame)
181 {
182 	struct sigcontext __user *sc = &frame->uc.uc_mcontext;
183 	struct thread_info *ti = current_thread_info();
184 	unsigned int err = 0;
185 	unsigned long ps;
186 
187 #define COPY(x)	err |= __get_user(regs->x, &sc->sc_##x)
188 	COPY(pc);
189 	COPY(lbeg);
190 	COPY(lend);
191 	COPY(lcount);
192 	COPY(sar);
193 #undef COPY
194 
195 	/* All registers were flushed to stack. Start with a pristine frame. */
196 
197 	regs->wmask = 1;
198 	regs->windowbase = 0;
199 	regs->windowstart = 1;
200 
201 	regs->syscall = NO_SYSCALL;	/* disable syscall checks */
202 
203 	/* For PS, restore only PS.CALLINC.
204 	 * Assume that all other bits are either the same as for the signal
205 	 * handler, or the user mode value doesn't matter (e.g. PS.OWB).
206 	 */
207 	err |= __get_user(ps, &sc->sc_ps);
208 	regs->ps = (regs->ps & ~PS_CALLINC_MASK) | (ps & PS_CALLINC_MASK);
209 
210 	/* Additional corruption checks */
211 
212 	if ((regs->lcount > 0)
213 	    && ((regs->lbeg > TASK_SIZE) || (regs->lend > TASK_SIZE)) )
214 		err = 1;
215 
216 	err |= __copy_from_user(regs->areg, sc->sc_a, 16 * 4);
217 
218 	if (err)
219 		return err;
220 
221 	/* The signal handler may have used coprocessors in which
222 	 * case they are still enabled.  We disable them to force a
223 	 * reloading of the original task's CP state by the lazy
224 	 * context-switching mechanisms of CP exception handling.
225 	 * Also, we essentially discard any coprocessor state that the
226 	 * signal handler created. */
227 
228 #if XTENSA_HAVE_COPROCESSORS
229 	coprocessor_release_all(ti);
230 	err |= __copy_from_user(&ti->xtregs_cp, &frame->xtregs.cp,
231 				sizeof (frame->xtregs.cp));
232 #endif
233 	err |= __copy_from_user(&ti->xtregs_user, &frame->xtregs.user,
234 				sizeof (xtregs_user_t));
235 	err |= __copy_from_user(&regs->xtregs_opt, &frame->xtregs.opt,
236 				sizeof (xtregs_opt_t));
237 
238 	return err;
239 }
240 
241 
242 /*
243  * Do a signal return; undo the signal stack.
244  */
245 
246 asmlinkage long xtensa_rt_sigreturn(void)
247 {
248 	struct pt_regs *regs = current_pt_regs();
249 	struct rt_sigframe __user *frame;
250 	sigset_t set;
251 	int ret;
252 
253 	/* Always make any pending restarted system calls return -EINTR */
254 	current->restart_block.fn = do_no_restart_syscall;
255 
256 	if (regs->depc > 64)
257 		panic("rt_sigreturn in double exception!\n");
258 
259 	frame = (struct rt_sigframe __user *) regs->areg[1];
260 
261 	if (!access_ok(frame, sizeof(*frame)))
262 		goto badframe;
263 
264 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
265 		goto badframe;
266 
267 	set_current_blocked(&set);
268 
269 	if (restore_sigcontext(regs, frame))
270 		goto badframe;
271 
272 	ret = regs->areg[2];
273 
274 	if (restore_altstack(&frame->uc.uc_stack))
275 		goto badframe;
276 
277 	return ret;
278 
279 badframe:
280 	force_sig(SIGSEGV);
281 	return 0;
282 }
283 
284 
285 
286 /*
287  * Set up a signal frame.
288  */
289 
290 static int
291 gen_return_code(unsigned char *codemem)
292 {
293 	int err = 0;
294 
295 	/*
296 	 * The 12-bit immediate is really split up within the 24-bit MOVI
297 	 * instruction.  As long as the above system call numbers fit within
298 	 * 8-bits, the following code works fine. See the Xtensa ISA for
299 	 * details.
300 	 */
301 
302 #if __NR_rt_sigreturn > 255
303 # error Generating the MOVI instruction below breaks!
304 #endif
305 
306 #ifdef __XTENSA_EB__   /* Big Endian version */
307 	/* Generate instruction:  MOVI a2, __NR_rt_sigreturn */
308 	err |= __put_user(0x22, &codemem[0]);
309 	err |= __put_user(0x0a, &codemem[1]);
310 	err |= __put_user(__NR_rt_sigreturn, &codemem[2]);
311 	/* Generate instruction:  SYSCALL */
312 	err |= __put_user(0x00, &codemem[3]);
313 	err |= __put_user(0x05, &codemem[4]);
314 	err |= __put_user(0x00, &codemem[5]);
315 
316 #elif defined __XTENSA_EL__   /* Little Endian version */
317 	/* Generate instruction:  MOVI a2, __NR_rt_sigreturn */
318 	err |= __put_user(0x22, &codemem[0]);
319 	err |= __put_user(0xa0, &codemem[1]);
320 	err |= __put_user(__NR_rt_sigreturn, &codemem[2]);
321 	/* Generate instruction:  SYSCALL */
322 	err |= __put_user(0x00, &codemem[3]);
323 	err |= __put_user(0x50, &codemem[4]);
324 	err |= __put_user(0x00, &codemem[5]);
325 #else
326 # error Must use compiler for Xtensa processors.
327 #endif
328 
329 	/* Flush generated code out of the data cache */
330 
331 	if (err == 0) {
332 		__invalidate_icache_range((unsigned long)codemem, 6UL);
333 		__flush_invalidate_dcache_range((unsigned long)codemem, 6UL);
334 	}
335 
336 	return err;
337 }
338 
339 
340 static int setup_frame(struct ksignal *ksig, sigset_t *set,
341 		       struct pt_regs *regs)
342 {
343 	struct rt_sigframe *frame;
344 	int err = 0, sig = ksig->sig;
345 	unsigned long sp, ra, tp, ps;
346 	unsigned long handler = (unsigned long)ksig->ka.sa.sa_handler;
347 	unsigned long handler_fdpic_GOT = 0;
348 	unsigned int base;
349 	bool fdpic = IS_ENABLED(CONFIG_BINFMT_ELF_FDPIC) &&
350 		(current->personality & FDPIC_FUNCPTRS);
351 
352 	if (fdpic) {
353 		unsigned long __user *fdpic_func_desc =
354 			(unsigned long __user *)handler;
355 		if (__get_user(handler, &fdpic_func_desc[0]) ||
356 		    __get_user(handler_fdpic_GOT, &fdpic_func_desc[1]))
357 			return -EFAULT;
358 	}
359 
360 	sp = regs->areg[1];
361 
362 	if ((ksig->ka.sa.sa_flags & SA_ONSTACK) != 0 && sas_ss_flags(sp) == 0) {
363 		sp = current->sas_ss_sp + current->sas_ss_size;
364 	}
365 
366 	frame = (void *)((sp - sizeof(*frame)) & -16ul);
367 
368 	if (regs->depc > 64)
369 		panic ("Double exception sys_sigreturn\n");
370 
371 	if (!access_ok(frame, sizeof(*frame))) {
372 		return -EFAULT;
373 	}
374 
375 	if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
376 		err |= copy_siginfo_to_user(&frame->info, &ksig->info);
377 	}
378 
379 	/* Create the user context.  */
380 
381 	err |= __put_user(0, &frame->uc.uc_flags);
382 	err |= __put_user(0, &frame->uc.uc_link);
383 	err |= __save_altstack(&frame->uc.uc_stack, regs->areg[1]);
384 	err |= setup_sigcontext(frame, regs);
385 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
386 
387 	if (ksig->ka.sa.sa_flags & SA_RESTORER) {
388 		if (fdpic) {
389 			unsigned long __user *fdpic_func_desc =
390 				(unsigned long __user *)ksig->ka.sa.sa_restorer;
391 
392 			err |= __get_user(ra, fdpic_func_desc);
393 		} else {
394 			ra = (unsigned long)ksig->ka.sa.sa_restorer;
395 		}
396 	} else {
397 
398 		/* Create sys_rt_sigreturn syscall in stack frame */
399 
400 		err |= gen_return_code(frame->retcode);
401 		ra = (unsigned long) frame->retcode;
402 	}
403 
404 	if (err)
405 		return -EFAULT;
406 
407 	/*
408 	 * Create signal handler execution context.
409 	 * Return context not modified until this point.
410 	 */
411 
412 	/* Set up registers for signal handler; preserve the threadptr */
413 	tp = regs->threadptr;
414 	ps = regs->ps;
415 	start_thread(regs, handler, (unsigned long)frame);
416 
417 	/* Set up a stack frame for a call4 if userspace uses windowed ABI */
418 	if (ps & PS_WOE_MASK) {
419 		base = 4;
420 		regs->areg[base] =
421 			(((unsigned long) ra) & 0x3fffffff) | 0x40000000;
422 		ps = (ps & ~(PS_CALLINC_MASK | PS_OWB_MASK)) |
423 			(1 << PS_CALLINC_SHIFT);
424 	} else {
425 		base = 0;
426 		regs->areg[base] = (unsigned long) ra;
427 	}
428 	regs->areg[base + 2] = (unsigned long) sig;
429 	regs->areg[base + 3] = (unsigned long) &frame->info;
430 	regs->areg[base + 4] = (unsigned long) &frame->uc;
431 	regs->threadptr = tp;
432 	regs->ps = ps;
433 	if (fdpic)
434 		regs->areg[base + 11] = handler_fdpic_GOT;
435 
436 	pr_debug("SIG rt deliver (%s:%d): signal=%d sp=%p pc=%08lx\n",
437 		 current->comm, current->pid, sig, frame, regs->pc);
438 
439 	return 0;
440 }
441 
442 /*
443  * Note that 'init' is a special process: it doesn't get signals it doesn't
444  * want to handle. Thus you cannot kill init even with a SIGKILL even by
445  * mistake.
446  *
447  * Note that we go through the signals twice: once to check the signals that
448  * the kernel can handle, and then we build all the user-level signal handling
449  * stack-frames in one go after that.
450  */
451 static void do_signal(struct pt_regs *regs)
452 {
453 	struct ksignal ksig;
454 
455 	task_pt_regs(current)->icountlevel = 0;
456 
457 	if (get_signal(&ksig)) {
458 		int ret;
459 
460 		/* Are we from a system call? */
461 
462 		if (regs->syscall != NO_SYSCALL) {
463 
464 			/* If so, check system call restarting.. */
465 
466 			switch (regs->areg[2]) {
467 				case -ERESTARTNOHAND:
468 				case -ERESTART_RESTARTBLOCK:
469 					regs->areg[2] = -EINTR;
470 					break;
471 
472 				case -ERESTARTSYS:
473 					if (!(ksig.ka.sa.sa_flags & SA_RESTART)) {
474 						regs->areg[2] = -EINTR;
475 						break;
476 					}
477 					fallthrough;
478 				case -ERESTARTNOINTR:
479 					regs->areg[2] = regs->syscall;
480 					regs->pc -= 3;
481 					break;
482 
483 				default:
484 					/* nothing to do */
485 					if (regs->areg[2] != 0)
486 					break;
487 			}
488 		}
489 
490 		/* Whee!  Actually deliver the signal.  */
491 		/* Set up the stack frame */
492 		ret = setup_frame(&ksig, sigmask_to_save(), regs);
493 		signal_setup_done(ret, &ksig, 0);
494 		if (test_thread_flag(TIF_SINGLESTEP))
495 			task_pt_regs(current)->icountlevel = 1;
496 
497 		return;
498 	}
499 
500 	/* Did we come from a system call? */
501 	if (regs->syscall != NO_SYSCALL) {
502 		/* Restart the system call - no handlers present */
503 		switch (regs->areg[2]) {
504 		case -ERESTARTNOHAND:
505 		case -ERESTARTSYS:
506 		case -ERESTARTNOINTR:
507 			regs->areg[2] = regs->syscall;
508 			regs->pc -= 3;
509 			break;
510 		case -ERESTART_RESTARTBLOCK:
511 			regs->areg[2] = __NR_restart_syscall;
512 			regs->pc -= 3;
513 			break;
514 		}
515 	}
516 
517 	/* If there's no signal to deliver, we just restore the saved mask.  */
518 	restore_saved_sigmask();
519 
520 	if (test_thread_flag(TIF_SINGLESTEP))
521 		task_pt_regs(current)->icountlevel = 1;
522 	return;
523 }
524 
525 void do_notify_resume(struct pt_regs *regs)
526 {
527 	if (test_thread_flag(TIF_SIGPENDING) ||
528 	    test_thread_flag(TIF_NOTIFY_SIGNAL))
529 		do_signal(regs);
530 
531 	if (test_thread_flag(TIF_NOTIFY_RESUME))
532 		resume_user_mode_work(regs);
533 }
534