1/*
2 *  linux/arch/cris/kernel/signal.c
3 *
4 *  Based on arch/i386/kernel/signal.c by
5 *     Copyright (C) 1991, 1992  Linus Torvalds
6 *     1997-11-28  Modified for POSIX.1b signals by Richard Henderson *
7 *
8 *  Ideas also taken from arch/arm.
9 *
10 *  Copyright (C) 2000, 2001 Axis Communications AB
11 *
12 *  Authors:  Bjorn Wesen (bjornw@axis.com)
13 *
14 */
15
16#include <linux/sched.h>
17#include <linux/mm.h>
18#include <linux/smp.h>
19#include <linux/kernel.h>
20#include <linux/signal.h>
21#include <linux/errno.h>
22#include <linux/wait.h>
23#include <linux/ptrace.h>
24#include <linux/unistd.h>
25#include <linux/stddef.h>
26
27#include <asm/processor.h>
28#include <asm/ucontext.h>
29#include <asm/uaccess.h>
30
31#define DEBUG_SIG 0
32
33#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
34
35/* a syscall in Linux/CRIS is a break 13 instruction which is 2 bytes */
36/* manipulate regs so that upon return, it will be re-executed */
37
38/* We rely on that pc points to the instruction after "break 13", so the
39 * library must never do strange things like putting it in a delay slot.
40 */
41#define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->irp -= 2;
42
43int do_signal(int canrestart, sigset_t *oldset, struct pt_regs *regs);
44
45/*
46 * Atomically swap in the new signal mask, and wait for a signal.  Define
47 * dummy arguments to be able to reach the regs argument.  (Note that this
48 * arrangement relies on old_sigset_t occupying one register.)
49 */
50int
51sys_sigsuspend(old_sigset_t mask, long r11, long r12, long r13, long mof,
52               long srp, struct pt_regs *regs)
53{
54	sigset_t saveset;
55
56	mask &= _BLOCKABLE;
57	spin_lock_irq(&current->sighand->siglock);
58	saveset = current->blocked;
59	siginitset(&current->blocked, mask);
60	recalc_sigpending();
61	spin_unlock_irq(&current->sighand->siglock);
62
63	regs->r10 = -EINTR;
64	while (1) {
65		current->state = TASK_INTERRUPTIBLE;
66		schedule();
67		if (do_signal(0, &saveset, regs))
68			/* We will get here twice: once to call the signal
69			   handler, then again to return from the
70			   sigsuspend system call.  When calling the
71			   signal handler, R10 holds the signal number as
72			   set through do_signal.  The sigsuspend call
73			   will return with the restored value set above;
74			   always -EINTR.  */
75			return regs->r10;
76	}
77}
78
79/* Define dummy arguments to be able to reach the regs argument.  (Note that
80 * this arrangement relies on size_t occupying one register.)
81 */
82int
83sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize, long r12, long r13,
84                  long mof, long srp, struct pt_regs *regs)
85{
86	sigset_t saveset, newset;
87
88	if (sigsetsize != sizeof(sigset_t))
89		return -EINVAL;
90
91	if (copy_from_user(&newset, unewset, sizeof(newset)))
92		return -EFAULT;
93	sigdelsetmask(&newset, ~_BLOCKABLE);
94
95	spin_lock_irq(&current->sighand->siglock);
96	saveset = current->blocked;
97	current->blocked = newset;
98	recalc_sigpending();
99	spin_unlock_irq(&current->sighand->siglock);
100
101	regs->r10 = -EINTR;
102	while (1) {
103		current->state = TASK_INTERRUPTIBLE;
104		schedule();
105		if (do_signal(0, &saveset, regs))
106			/* We will get here twice: once to call the signal
107			   handler, then again to return from the
108			   sigsuspend system call.  When calling the
109			   signal handler, R10 holds the signal number as
110			   set through do_signal.  The sigsuspend call
111			   will return with the restored value set above;
112			   always -EINTR.  */
113			return regs->r10;
114	}
115}
116
117int
118sys_sigaction(int sig, const struct old_sigaction __user *act,
119	      struct old_sigaction *oact)
120{
121	struct k_sigaction new_ka, old_ka;
122	int ret;
123
124	if (act) {
125		old_sigset_t mask;
126		if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
127		    __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
128		    __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
129			return -EFAULT;
130		__get_user(new_ka.sa.sa_flags, &act->sa_flags);
131		__get_user(mask, &act->sa_mask);
132		siginitset(&new_ka.sa.sa_mask, mask);
133	}
134
135	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
136
137	if (!ret && oact) {
138		if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
139		    __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
140		    __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
141			return -EFAULT;
142		__put_user(old_ka.sa.sa_flags, &oact->sa_flags);
143		__put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
144	}
145
146	return ret;
147}
148
149int
150sys_sigaltstack(const stack_t *uss, stack_t __user *uoss)
151{
152	return do_sigaltstack(uss, uoss, rdusp());
153}
154
155
156/*
157 * Do a signal return; undo the signal stack.
158 */
159
160struct sigframe {
161	struct sigcontext sc;
162	unsigned long extramask[_NSIG_WORDS-1];
163	unsigned char retcode[8];  /* trampoline code */
164};
165
166struct rt_sigframe {
167	struct siginfo *pinfo;
168	void *puc;
169	struct siginfo info;
170	struct ucontext uc;
171	unsigned char retcode[8];  /* trampoline code */
172};
173
174
175static int
176restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
177{
178	unsigned int err = 0;
179	unsigned long old_usp;
180
181        /* Always make any pending restarted system calls return -EINTR */
182	current_thread_info()->restart_block.fn = do_no_restart_syscall;
183
184	/* restore the regs from &sc->regs (same as sc, since regs is first)
185	 * (sc is already checked for VERIFY_READ since the sigframe was
186	 *  checked in sys_sigreturn previously)
187	 */
188
189	if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
190                goto badframe;
191
192	/* make sure the U-flag is set so user-mode cannot fool us */
193
194	regs->dccr |= 1 << 8;
195
196	/* restore the old USP as it was before we stacked the sc etc.
197	 * (we cannot just pop the sigcontext since we aligned the sp and
198	 *  stuff after pushing it)
199	 */
200
201	err |= __get_user(old_usp, &sc->usp);
202
203	wrusp(old_usp);
204
205	/* TODO: the other ports use regs->orig_XX to disable syscall checks
206	 * after this completes, but we don't use that mechanism. maybe we can
207	 * use it now ?
208	 */
209
210	return err;
211
212badframe:
213	return 1;
214}
215
216/* Define dummy arguments to be able to reach the regs argument.  */
217
218asmlinkage int sys_sigreturn(long r10, long r11, long r12, long r13, long mof,
219                             long srp, struct pt_regs *regs)
220{
221	struct sigframe __user *frame = (struct sigframe *)rdusp();
222	sigset_t set;
223
224        /*
225         * Since we stacked the signal on a dword boundary,
226         * then frame should be dword aligned here.  If it's
227         * not, then the user is trying to mess with us.
228         */
229        if (((long)frame) & 3)
230                goto badframe;
231
232	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
233		goto badframe;
234	if (__get_user(set.sig[0], &frame->sc.oldmask)
235	    || (_NSIG_WORDS > 1
236		&& __copy_from_user(&set.sig[1], frame->extramask,
237				    sizeof(frame->extramask))))
238		goto badframe;
239
240	sigdelsetmask(&set, ~_BLOCKABLE);
241	spin_lock_irq(&current->sighand->siglock);
242	current->blocked = set;
243	recalc_sigpending();
244	spin_unlock_irq(&current->sighand->siglock);
245
246	if (restore_sigcontext(regs, &frame->sc))
247		goto badframe;
248
249	/* TODO: SIGTRAP when single-stepping as in arm ? */
250
251	return regs->r10;
252
253badframe:
254	force_sig(SIGSEGV, current);
255	return 0;
256}
257
258/* Define dummy arguments to be able to reach the regs argument.  */
259
260asmlinkage int sys_rt_sigreturn(long r10, long r11, long r12, long r13,
261                                long mof, long srp, struct pt_regs *regs)
262{
263	struct rt_sigframe __user *frame = (struct rt_sigframe *)rdusp();
264	sigset_t set;
265
266        /*
267         * Since we stacked the signal on a dword boundary,
268         * then frame should be dword aligned here.  If it's
269         * not, then the user is trying to mess with us.
270         */
271        if (((long)frame) & 3)
272                goto badframe;
273
274	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
275		goto badframe;
276	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
277		goto badframe;
278
279	sigdelsetmask(&set, ~_BLOCKABLE);
280	spin_lock_irq(&current->sighand->siglock);
281	current->blocked = set;
282	recalc_sigpending();
283	spin_unlock_irq(&current->sighand->siglock);
284
285	if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
286		goto badframe;
287
288	if (do_sigaltstack(&frame->uc.uc_stack, NULL, rdusp()) == -EFAULT)
289		goto badframe;
290
291	return regs->r10;
292
293badframe:
294	force_sig(SIGSEGV, current);
295	return 0;
296}
297
298/*
299 * Set up a signal frame.
300 */
301
302static int
303setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs, unsigned long mask)
304{
305	int err = 0;
306	unsigned long usp = rdusp();
307
308	/* copy the regs. they are first in sc so we can use sc directly */
309
310	err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
311
312        /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
313           the signal handler. The frametype will be restored to its previous
314           value in restore_sigcontext. */
315        regs->frametype = CRIS_FRAME_NORMAL;
316
317	/* then some other stuff */
318
319	err |= __put_user(mask, &sc->oldmask);
320
321	err |= __put_user(usp, &sc->usp);
322
323	return err;
324}
325
326/* figure out where we want to put the new signal frame - usually on the stack */
327
328static inline void __user *
329get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
330{
331	unsigned long sp = rdusp();
332
333	/* This is the X/Open sanctioned signal stack switching.  */
334	if (ka->sa.sa_flags & SA_ONSTACK) {
335		if (! on_sig_stack(sp))
336			sp = current->sas_ss_sp + current->sas_ss_size;
337	}
338
339	/* make sure the frame is dword-aligned */
340
341	sp &= ~3;
342
343	return (void __user*)(sp - frame_size);
344}
345
346/* grab and setup a signal frame.
347 *
348 * basically we stack a lot of state info, and arrange for the
349 * user-mode program to return to the kernel using either a
350 * trampoline which performs the syscall sigreturn, or a provided
351 * user-mode trampoline.
352 */
353
354static void setup_frame(int sig, struct k_sigaction *ka,
355			sigset_t *set, struct pt_regs * regs)
356{
357	struct sigframe __user *frame;
358	unsigned long return_ip;
359	int err = 0;
360
361	frame = get_sigframe(ka, regs, sizeof(*frame));
362
363	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
364		goto give_sigsegv;
365
366	err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
367	if (err)
368		goto give_sigsegv;
369
370	if (_NSIG_WORDS > 1) {
371		err |= __copy_to_user(frame->extramask, &set->sig[1],
372				      sizeof(frame->extramask));
373	}
374	if (err)
375		goto give_sigsegv;
376
377	/* Set up to return from userspace.  If provided, use a stub
378	   already in userspace.  */
379	if (ka->sa.sa_flags & SA_RESTORER) {
380		return_ip = (unsigned long)ka->sa.sa_restorer;
381	} else {
382		/* trampoline - the desired return ip is the retcode itself */
383		return_ip = (unsigned long)&frame->retcode;
384		/* This is movu.w __NR_sigreturn, r9; break 13; */
385		err |= __put_user(0x9c5f,         (short __user*)(frame->retcode+0));
386		err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));
387		err |= __put_user(0xe93d,         (short __user*)(frame->retcode+4));
388	}
389
390	if (err)
391		goto give_sigsegv;
392
393	/* Set up registers for signal handler */
394
395	regs->irp = (unsigned long) ka->sa.sa_handler;  /* what we enter NOW   */
396	regs->srp = return_ip;                          /* what we enter LATER */
397	regs->r10 = sig;                                /* first argument is signo */
398
399	/* actually move the usp to reflect the stacked frame */
400
401	wrusp((unsigned long)frame);
402
403	return;
404
405give_sigsegv:
406	force_sigsegv(sig, current);
407}
408
409static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
410			   sigset_t *set, struct pt_regs * regs)
411{
412	struct rt_sigframe __user *frame;
413	unsigned long return_ip;
414	int err = 0;
415
416	frame = get_sigframe(ka, regs, sizeof(*frame));
417
418	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
419		goto give_sigsegv;
420
421	err |= __put_user(&frame->info, &frame->pinfo);
422	err |= __put_user(&frame->uc, &frame->puc);
423	err |= copy_siginfo_to_user(&frame->info, info);
424	if (err)
425		goto give_sigsegv;
426
427	/* Clear all the bits of the ucontext we don't use.  */
428        err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
429
430	err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
431
432	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
433
434	if (err)
435		goto give_sigsegv;
436
437	/* Set up to return from userspace.  If provided, use a stub
438	   already in userspace.  */
439	if (ka->sa.sa_flags & SA_RESTORER) {
440		return_ip = (unsigned long)ka->sa.sa_restorer;
441	} else {
442		/* trampoline - the desired return ip is the retcode itself */
443		return_ip = (unsigned long)&frame->retcode;
444		/* This is movu.w __NR_rt_sigreturn, r9; break 13; */
445		err |= __put_user(0x9c5f,            (short __user*)(frame->retcode+0));
446		err |= __put_user(__NR_rt_sigreturn, (short __user*)(frame->retcode+2));
447		err |= __put_user(0xe93d,            (short __user*)(frame->retcode+4));
448	}
449
450	if (err)
451		goto give_sigsegv;
452
453	/* TODO what is the current->exec_domain stuff and invmap ? */
454
455	/* Set up registers for signal handler */
456
457	regs->irp = (unsigned long) ka->sa.sa_handler;  /* what we enter NOW   */
458	regs->srp = return_ip;                          /* what we enter LATER */
459	regs->r10 = sig;                                /* first argument is signo */
460        regs->r11 = (unsigned long) &frame->info;       /* second argument is (siginfo_t *) */
461        regs->r12 = 0;                                  /* third argument is unused */
462
463	/* actually move the usp to reflect the stacked frame */
464
465	wrusp((unsigned long)frame);
466
467	return;
468
469give_sigsegv:
470	force_sigsegv(sig, current);
471}
472
473/*
474 * OK, we're invoking a handler
475 */
476
477static inline void
478handle_signal(int canrestart, unsigned long sig,
479	      siginfo_t *info, struct k_sigaction *ka,
480              sigset_t *oldset, struct pt_regs * regs)
481{
482	/* Are we from a system call? */
483	if (canrestart) {
484		/* If so, check system call restarting.. */
485		switch (regs->r10) {
486			case -ERESTART_RESTARTBLOCK:
487			case -ERESTARTNOHAND:
488				/* ERESTARTNOHAND means that the syscall should only be
489				   restarted if there was no handler for the signal, and since
490				   we only get here if there is a handler, we don't restart */
491				regs->r10 = -EINTR;
492				break;
493
494			case -ERESTARTSYS:
495				/* ERESTARTSYS means to restart the syscall if there is no
496				   handler or the handler was registered with SA_RESTART */
497				if (!(ka->sa.sa_flags & SA_RESTART)) {
498					regs->r10 = -EINTR;
499					break;
500				}
501			/* fallthrough */
502			case -ERESTARTNOINTR:
503				/* ERESTARTNOINTR means that the syscall should be called again
504				   after the signal handler returns. */
505				RESTART_CRIS_SYS(regs);
506		}
507	}
508
509	/* Set up the stack frame */
510	if (ka->sa.sa_flags & SA_SIGINFO)
511		setup_rt_frame(sig, ka, info, oldset, regs);
512	else
513		setup_frame(sig, ka, oldset, regs);
514
515	if (ka->sa.sa_flags & SA_ONESHOT)
516		ka->sa.sa_handler = SIG_DFL;
517
518	spin_lock_irq(&current->sighand->siglock);
519	sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
520	if (!(ka->sa.sa_flags & SA_NODEFER))
521		sigaddset(&current->blocked,sig);
522	recalc_sigpending();
523	spin_unlock_irq(&current->sighand->siglock);
524}
525
526/*
527 * Note that 'init' is a special process: it doesn't get signals it doesn't
528 * want to handle. Thus you cannot kill init even with a SIGKILL even by
529 * mistake.
530 *
531 * Also note that the regs structure given here as an argument, is the latest
532 * pushed pt_regs. It may or may not be the same as the first pushed registers
533 * when the initial usermode->kernelmode transition took place. Therefore
534 * we can use user_mode(regs) to see if we came directly from kernel or user
535 * mode below.
536 */
537
538int do_signal(int canrestart, sigset_t *oldset, struct pt_regs *regs)
539{
540	siginfo_t info;
541	int signr;
542        struct k_sigaction ka;
543
544	/*
545	 * We want the common case to go fast, which
546	 * is why we may in certain cases get here from
547	 * kernel mode. Just return without doing anything
548	 * if so.
549	 */
550	if (!user_mode(regs))
551		return 1;
552
553	if (!oldset)
554		oldset = &current->blocked;
555
556	signr = get_signal_to_deliver(&info, &ka, regs, NULL);
557	if (signr > 0) {
558		/* Whee!  Actually deliver the signal.  */
559		handle_signal(canrestart, signr, &info, &ka, oldset, regs);
560		return 1;
561	}
562
563	/* Did we come from a system call? */
564	if (canrestart) {
565		/* Restart the system call - no handlers present */
566		if (regs->r10 == -ERESTARTNOHAND ||
567		    regs->r10 == -ERESTARTSYS ||
568		    regs->r10 == -ERESTARTNOINTR) {
569			RESTART_CRIS_SYS(regs);
570		}
571		if (regs->r10 == -ERESTART_RESTARTBLOCK){
572			regs->r10 = __NR_restart_syscall;
573			regs->irp -= 2;
574		}
575	}
576	return 0;
577}
578