Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / frv / kernel / signal.c
1 /* signal.c: FRV specific bits of signal handling
2 *
3 * Copyright (C) 2003-5 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 * - Derived from arch/m68k/kernel/signal.c
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
13 #include <linux/sched.h>
14 #include <linux/mm.h>
15 #include <linux/smp.h>
16 #include <linux/smp_lock.h>
17 #include <linux/kernel.h>
18 #include <linux/signal.h>
19 #include <linux/errno.h>
20 #include <linux/wait.h>
21 #include <linux/ptrace.h>
22 #include <linux/unistd.h>
23 #include <linux/personality.h>
24 #include <linux/suspend.h>
25 #include <asm/ucontext.h>
26 #include <asm/uaccess.h>
27 #include <asm/cacheflush.h>
28
29 #define DEBUG_SIG 0
30
31 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
32
33 struct fdpic_func_descriptor {
34 unsigned long text;
35 unsigned long GOT;
36 };
37
38 /*
39 * Atomically swap in the new signal mask, and wait for a signal.
40 */
41 asmlinkage int sys_sigsuspend(int history0, int history1, old_sigset_t mask)
42 {
43 mask &= _BLOCKABLE;
44 spin_lock_irq(&current->sighand->siglock);
45 current->saved_sigmask = current->blocked;
46 siginitset(&current->blocked, mask);
47 recalc_sigpending();
48 spin_unlock_irq(&current->sighand->siglock);
49
50 current->state = TASK_INTERRUPTIBLE;
51 schedule();
52 set_thread_flag(TIF_RESTORE_SIGMASK);
53 return -ERESTARTNOHAND;
54 }
55
56 asmlinkage int sys_sigaction(int sig,
57 const struct old_sigaction __user *act,
58 struct old_sigaction __user *oact)
59 {
60 struct k_sigaction new_ka, old_ka;
61 int ret;
62
63 if (act) {
64 old_sigset_t mask;
65 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
66 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
67 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
68 return -EFAULT;
69 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
70 __get_user(mask, &act->sa_mask);
71 siginitset(&new_ka.sa.sa_mask, mask);
72 }
73
74 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
75
76 if (!ret && oact) {
77 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
78 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
79 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
80 return -EFAULT;
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 }
84
85 return ret;
86 }
87
88 asmlinkage
89 int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
90 {
91 return do_sigaltstack(uss, uoss, __frame->sp);
92 }
93
94
95 /*
96 * Do a signal return; undo the signal stack.
97 */
98
99 struct sigframe
100 {
101 __sigrestore_t pretcode;
102 int sig;
103 struct sigcontext sc;
104 unsigned long extramask[_NSIG_WORDS-1];
105 uint32_t retcode[2];
106 };
107
108 struct rt_sigframe
109 {
110 __sigrestore_t pretcode;
111 int sig;
112 struct siginfo __user *pinfo;
113 void __user *puc;
114 struct siginfo info;
115 struct ucontext uc;
116 uint32_t retcode[2];
117 };
118
119 static int restore_sigcontext(struct sigcontext __user *sc, int *_gr8)
120 {
121 struct user_context *user = current->thread.user;
122 unsigned long tbr, psr;
123
124 tbr = user->i.tbr;
125 psr = user->i.psr;
126 if (copy_from_user(user, &sc->sc_context, sizeof(sc->sc_context)))
127 goto badframe;
128 user->i.tbr = tbr;
129 user->i.psr = psr;
130
131 restore_user_regs(user);
132
133 user->i.syscallno = -1; /* disable syscall checks */
134
135 *_gr8 = user->i.gr[8];
136 return 0;
137
138 badframe:
139 return 1;
140 }
141
142 asmlinkage int sys_sigreturn(void)
143 {
144 struct sigframe __user *frame = (struct sigframe __user *) __frame->sp;
145 sigset_t set;
146 int gr8;
147
148 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
149 goto badframe;
150 if (__get_user(set.sig[0], &frame->sc.sc_oldmask))
151 goto badframe;
152
153 if (_NSIG_WORDS > 1 &&
154 __copy_from_user(&set.sig[1], &frame->extramask, sizeof(frame->extramask)))
155 goto badframe;
156
157 sigdelsetmask(&set, ~_BLOCKABLE);
158 spin_lock_irq(&current->sighand->siglock);
159 current->blocked = set;
160 recalc_sigpending();
161 spin_unlock_irq(&current->sighand->siglock);
162
163 if (restore_sigcontext(&frame->sc, &gr8))
164 goto badframe;
165 return gr8;
166
167 badframe:
168 force_sig(SIGSEGV, current);
169 return 0;
170 }
171
172 asmlinkage int sys_rt_sigreturn(void)
173 {
174 struct rt_sigframe __user *frame = (struct rt_sigframe __user *) __frame->sp;
175 sigset_t set;
176 int gr8;
177
178 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
179 goto badframe;
180 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
181 goto badframe;
182
183 sigdelsetmask(&set, ~_BLOCKABLE);
184 spin_lock_irq(&current->sighand->siglock);
185 current->blocked = set;
186 recalc_sigpending();
187 spin_unlock_irq(&current->sighand->siglock);
188
189 if (restore_sigcontext(&frame->uc.uc_mcontext, &gr8))
190 goto badframe;
191
192 if (do_sigaltstack(&frame->uc.uc_stack, NULL, __frame->sp) == -EFAULT)
193 goto badframe;
194
195 return gr8;
196
197 badframe:
198 force_sig(SIGSEGV, current);
199 return 0;
200 }
201
202 /*
203 * Set up a signal frame
204 */
205 static int setup_sigcontext(struct sigcontext __user *sc, unsigned long mask)
206 {
207 save_user_regs(current->thread.user);
208
209 if (copy_to_user(&sc->sc_context, current->thread.user, sizeof(sc->sc_context)) != 0)
210 goto badframe;
211
212 /* non-iBCS2 extensions.. */
213 if (__put_user(mask, &sc->sc_oldmask) < 0)
214 goto badframe;
215
216 return 0;
217
218 badframe:
219 return 1;
220 }
221
222 /*****************************************************************************/
223 /*
224 * Determine which stack to use..
225 */
226 static inline void __user *get_sigframe(struct k_sigaction *ka,
227 size_t frame_size)
228 {
229 unsigned long sp;
230
231 /* Default to using normal stack */
232 sp = __frame->sp;
233
234 /* This is the X/Open sanctioned signal stack switching. */
235 if (ka->sa.sa_flags & SA_ONSTACK) {
236 if (! sas_ss_flags(sp))
237 sp = current->sas_ss_sp + current->sas_ss_size;
238 }
239
240 return (void __user *) ((sp - frame_size) & ~7UL);
241
242 } /* end get_sigframe() */
243
244 /*****************************************************************************/
245 /*
246 *
247 */
248 static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set)
249 {
250 struct sigframe __user *frame;
251 int rsig;
252
253 frame = get_sigframe(ka, sizeof(*frame));
254
255 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
256 goto give_sigsegv;
257
258 rsig = sig;
259 if (sig < 32 &&
260 __current_thread_info->exec_domain &&
261 __current_thread_info->exec_domain->signal_invmap)
262 rsig = __current_thread_info->exec_domain->signal_invmap[sig];
263
264 if (__put_user(rsig, &frame->sig) < 0)
265 goto give_sigsegv;
266
267 if (setup_sigcontext(&frame->sc, set->sig[0]))
268 goto give_sigsegv;
269
270 if (_NSIG_WORDS > 1) {
271 if (__copy_to_user(frame->extramask, &set->sig[1],
272 sizeof(frame->extramask)))
273 goto give_sigsegv;
274 }
275
276 /* Set up to return from userspace. If provided, use a stub
277 * already in userspace. */
278 if (ka->sa.sa_flags & SA_RESTORER) {
279 if (__put_user(ka->sa.sa_restorer, &frame->pretcode) < 0)
280 goto give_sigsegv;
281 }
282 else {
283 /* Set up the following code on the stack:
284 * setlos #__NR_sigreturn,gr7
285 * tira gr0,0
286 */
287 if (__put_user((__sigrestore_t)frame->retcode, &frame->pretcode) ||
288 __put_user(0x8efc0000|__NR_sigreturn, &frame->retcode[0]) ||
289 __put_user(0xc0700000, &frame->retcode[1]))
290 goto give_sigsegv;
291
292 flush_icache_range((unsigned long) frame->retcode,
293 (unsigned long) (frame->retcode + 2));
294 }
295
296 /* set up registers for signal handler */
297 __frame->sp = (unsigned long) frame;
298 __frame->lr = (unsigned long) &frame->retcode;
299 __frame->gr8 = sig;
300
301 if (get_personality & FDPIC_FUNCPTRS) {
302 struct fdpic_func_descriptor __user *funcptr =
303 (struct fdpic_func_descriptor __user *) ka->sa.sa_handler;
304 __get_user(__frame->pc, &funcptr->text);
305 __get_user(__frame->gr15, &funcptr->GOT);
306 } else {
307 __frame->pc = (unsigned long) ka->sa.sa_handler;
308 __frame->gr15 = 0;
309 }
310
311 set_fs(USER_DS);
312
313 /* the tracer may want to single-step inside the handler */
314 if (test_thread_flag(TIF_SINGLESTEP))
315 ptrace_notify(SIGTRAP);
316
317 #if DEBUG_SIG
318 printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
319 sig, current->comm, current->pid, frame, __frame->pc,
320 frame->pretcode);
321 #endif
322
323 return 0;
324
325 give_sigsegv:
326 force_sig(SIGSEGV, current);
327 return -EFAULT;
328
329 } /* end setup_frame() */
330
331 /*****************************************************************************/
332 /*
333 *
334 */
335 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
336 sigset_t *set)
337 {
338 struct rt_sigframe __user *frame;
339 int rsig;
340
341 frame = get_sigframe(ka, sizeof(*frame));
342
343 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
344 goto give_sigsegv;
345
346 rsig = sig;
347 if (sig < 32 &&
348 __current_thread_info->exec_domain &&
349 __current_thread_info->exec_domain->signal_invmap)
350 rsig = __current_thread_info->exec_domain->signal_invmap[sig];
351
352 if (__put_user(rsig, &frame->sig) ||
353 __put_user(&frame->info, &frame->pinfo) ||
354 __put_user(&frame->uc, &frame->puc))
355 goto give_sigsegv;
356
357 if (copy_siginfo_to_user(&frame->info, info))
358 goto give_sigsegv;
359
360 /* Create the ucontext. */
361 if (__put_user(0, &frame->uc.uc_flags) ||
362 __put_user(NULL, &frame->uc.uc_link) ||
363 __put_user((void __user *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp) ||
364 __put_user(sas_ss_flags(__frame->sp), &frame->uc.uc_stack.ss_flags) ||
365 __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size))
366 goto give_sigsegv;
367
368 if (setup_sigcontext(&frame->uc.uc_mcontext, set->sig[0]))
369 goto give_sigsegv;
370
371 if (__copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
372 goto give_sigsegv;
373
374 /* Set up to return from userspace. If provided, use a stub
375 * already in userspace. */
376 if (ka->sa.sa_flags & SA_RESTORER) {
377 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
378 goto give_sigsegv;
379 }
380 else {
381 /* Set up the following code on the stack:
382 * setlos #__NR_sigreturn,gr7
383 * tira gr0,0
384 */
385 if (__put_user((__sigrestore_t)frame->retcode, &frame->pretcode) ||
386 __put_user(0x8efc0000|__NR_rt_sigreturn, &frame->retcode[0]) ||
387 __put_user(0xc0700000, &frame->retcode[1]))
388 goto give_sigsegv;
389
390 flush_icache_range((unsigned long) frame->retcode,
391 (unsigned long) (frame->retcode + 2));
392 }
393
394 /* Set up registers for signal handler */
395 __frame->sp = (unsigned long) frame;
396 __frame->lr = (unsigned long) &frame->retcode;
397 __frame->gr8 = sig;
398 __frame->gr9 = (unsigned long) &frame->info;
399
400 if (get_personality & FDPIC_FUNCPTRS) {
401 struct fdpic_func_descriptor __user *funcptr =
402 (struct fdpic_func_descriptor __user *) ka->sa.sa_handler;
403 __get_user(__frame->pc, &funcptr->text);
404 __get_user(__frame->gr15, &funcptr->GOT);
405 } else {
406 __frame->pc = (unsigned long) ka->sa.sa_handler;
407 __frame->gr15 = 0;
408 }
409
410 set_fs(USER_DS);
411
412 /* the tracer may want to single-step inside the handler */
413 if (test_thread_flag(TIF_SINGLESTEP))
414 ptrace_notify(SIGTRAP);
415
416 #if DEBUG_SIG
417 printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
418 sig, current->comm, current->pid, frame, __frame->pc,
419 frame->pretcode);
420 #endif
421
422 return 0;
423
424 give_sigsegv:
425 force_sig(SIGSEGV, current);
426 return -EFAULT;
427
428 } /* end setup_rt_frame() */
429
430 /*****************************************************************************/
431 /*
432 * OK, we're invoking a handler
433 */
434 static int handle_signal(unsigned long sig, siginfo_t *info,
435 struct k_sigaction *ka, sigset_t *oldset)
436 {
437 int ret;
438
439 /* Are we from a system call? */
440 if (in_syscall(__frame)) {
441 /* If so, check system call restarting.. */
442 switch (__frame->gr8) {
443 case -ERESTART_RESTARTBLOCK:
444 case -ERESTARTNOHAND:
445 __frame->gr8 = -EINTR;
446 break;
447
448 case -ERESTARTSYS:
449 if (!(ka->sa.sa_flags & SA_RESTART)) {
450 __frame->gr8 = -EINTR;
451 break;
452 }
453
454 /* fallthrough */
455 case -ERESTARTNOINTR:
456 __frame->gr8 = __frame->orig_gr8;
457 __frame->pc -= 4;
458 }
459 }
460
461 /* Set up the stack frame */
462 if (ka->sa.sa_flags & SA_SIGINFO)
463 ret = setup_rt_frame(sig, ka, info, oldset);
464 else
465 ret = setup_frame(sig, ka, oldset);
466
467 if (ret == 0) {
468 spin_lock_irq(&current->sighand->siglock);
469 sigorsets(&current->blocked, &current->blocked,
470 &ka->sa.sa_mask);
471 if (!(ka->sa.sa_flags & SA_NODEFER))
472 sigaddset(&current->blocked, sig);
473 recalc_sigpending();
474 spin_unlock_irq(&current->sighand->siglock);
475 }
476
477 return ret;
478
479 } /* end handle_signal() */
480
481 /*****************************************************************************/
482 /*
483 * Note that 'init' is a special process: it doesn't get signals it doesn't
484 * want to handle. Thus you cannot kill init even with a SIGKILL even by
485 * mistake.
486 */
487 static void do_signal(void)
488 {
489 struct k_sigaction ka;
490 siginfo_t info;
491 sigset_t *oldset;
492 int signr;
493
494 /*
495 * We want the common case to go fast, which
496 * is why we may in certain cases get here from
497 * kernel mode. Just return without doing anything
498 * if so.
499 */
500 if (!user_mode(__frame))
501 return;
502
503 if (try_to_freeze())
504 goto no_signal;
505
506 if (test_thread_flag(TIF_RESTORE_SIGMASK))
507 oldset = &current->saved_sigmask;
508 else
509 oldset = &current->blocked;
510
511 signr = get_signal_to_deliver(&info, &ka, __frame, NULL);
512 if (signr > 0) {
513 if (handle_signal(signr, &info, &ka, oldset) == 0) {
514 /* a signal was successfully delivered; the saved
515 * sigmask will have been stored in the signal frame,
516 * and will be restored by sigreturn, so we can simply
517 * clear the TIF_RESTORE_SIGMASK flag */
518 if (test_thread_flag(TIF_RESTORE_SIGMASK))
519 clear_thread_flag(TIF_RESTORE_SIGMASK);
520 }
521
522 return;
523 }
524
525 no_signal:
526 /* Did we come from a system call? */
527 if (__frame->syscallno >= 0) {
528 /* Restart the system call - no handlers present */
529 switch (__frame->gr8) {
530 case -ERESTARTNOHAND:
531 case -ERESTARTSYS:
532 case -ERESTARTNOINTR:
533 __frame->gr8 = __frame->orig_gr8;
534 __frame->pc -= 4;
535 break;
536
537 case -ERESTART_RESTARTBLOCK:
538 __frame->gr8 = __NR_restart_syscall;
539 __frame->pc -= 4;
540 break;
541 }
542 }
543
544 /* if there's no signal to deliver, we just put the saved sigmask
545 * back */
546 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
547 clear_thread_flag(TIF_RESTORE_SIGMASK);
548 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
549 }
550
551 } /* end do_signal() */
552
553 /*****************************************************************************/
554 /*
555 * notification of userspace execution resumption
556 * - triggered by the TIF_WORK_MASK flags
557 */
558 asmlinkage void do_notify_resume(__u32 thread_info_flags)
559 {
560 /* pending single-step? */
561 if (thread_info_flags & _TIF_SINGLESTEP)
562 clear_thread_flag(TIF_SINGLESTEP);
563
564 /* deal with pending signal delivery */
565 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
566 do_signal();
567
568 } /* end do_notify_resume() */