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