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