]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - arch/cris/arch-v10/kernel/signal.c
Merge tag 'v3.4-rc3' into staging/for_v3.5
[mirror_ubuntu-zesty-kernel.git] / arch / cris / arch-v10 / kernel / signal.c
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-2007 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 #include <arch/system.h>
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
44 void do_signal(int canrestart, struct pt_regs *regs);
45
46 /*
47 * Atomically swap in the new signal mask, and wait for a signal. Define
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 */
51 int sys_sigsuspend(old_sigset_t mask, long r11, long r12, long r13, long mof,
52 long srp, struct pt_regs *regs)
53 {
54 mask &= _BLOCKABLE;
55 spin_lock_irq(&current->sighand->siglock);
56 current->saved_sigmask = current->blocked;
57 siginitset(&current->blocked, mask);
58 recalc_sigpending();
59 spin_unlock_irq(&current->sighand->siglock);
60 current->state = TASK_INTERRUPTIBLE;
61 schedule();
62 set_thread_flag(TIF_RESTORE_SIGMASK);
63 return -ERESTARTNOHAND;
64 }
65
66 int sys_sigaction(int sig, const struct old_sigaction __user *act,
67 struct old_sigaction *oact)
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
97 int sys_sigaltstack(const stack_t *uss, stack_t __user *uoss)
98 {
99 return do_sigaltstack(uss, uoss, rdusp());
100 }
101
102
103 /*
104 * Do a signal return; undo the signal stack.
105 */
106
107 struct sigframe {
108 struct sigcontext sc;
109 unsigned long extramask[_NSIG_WORDS-1];
110 unsigned char retcode[8]; /* trampoline code */
111 };
112
113 struct 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
122 static int
123 restore_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
154 * use it now ?
155 */
156
157 return err;
158
159 badframe:
160 return 1;
161 }
162
163 /* Define dummy arguments to be able to reach the regs argument. */
164
165 asmlinkage int sys_sigreturn(long r10, long r11, long r12, long r13, long mof,
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);
192
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
200 badframe:
201 force_sig(SIGSEGV, current);
202 return 0;
203 }
204
205 /* Define dummy arguments to be able to reach the regs argument. */
206
207 asmlinkage int sys_rt_sigreturn(long r10, long r11, long r12, long r13,
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);
231
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
240 badframe:
241 force_sig(SIGSEGV, current);
242 return 0;
243 }
244
245 /*
246 * Set up a signal frame.
247 */
248
249 static int setup_sigcontext(struct sigcontext __user *sc,
250 struct pt_regs *regs, unsigned long mask)
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
273 /* Figure out where we want to put the new signal frame
274 * - usually on the stack. */
275
276 static inline void __user *
277 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
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.
295 *
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
302 static int setup_frame(int sig, struct k_sigaction *ka,
303 sigset_t *set, struct pt_regs *regs)
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
351 return 0;
352
353 give_sigsegv:
354 force_sigsegv(sig, current);
355 return -EFAULT;
356 }
357
358 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
359 sigset_t *set, struct pt_regs *regs)
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; */
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));
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
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 */
419 wrusp((unsigned long)frame);
420
421 return 0;
422
423 give_sigsegv:
424 force_sigsegv(sig, current);
425 return -EFAULT;
426 }
427
428 /*
429 * OK, we're invoking a handler
430 */
431
432 static 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)
435 {
436 int ret;
437
438 /* Are we from a system call? */
439 if (canrestart) {
440 /* If so, check system call restarting.. */
441 switch (regs->r10) {
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)) {
455 regs->r10 = -EINTR;
456 break;
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);
463 }
464 }
465
466 /* Set up the stack frame */
467 if (ka->sa.sa_flags & SA_SIGINFO)
468 ret = setup_rt_frame(sig, ka, info, oldset, regs);
469 else
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;
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
496 void do_signal(int canrestart, struct pt_regs *regs)
497 {
498 siginfo_t info;
499 int signr;
500 struct k_sigaction ka;
501 sigset_t *oldset;
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))
510 return;
511
512 if (test_thread_flag(TIF_RESTORE_SIGMASK))
513 oldset = &current->saved_sigmask;
514 else
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. */
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;
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 }
540 if (regs->r10 == -ERESTART_RESTARTBLOCK) {
541 regs->r9 = __NR_restart_syscall;
542 regs->irp -= 2;
543 }
544 }
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 }
552 }