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