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