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