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