drm/i915: Apply i830 errata for cursor alignment
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / frv / kernel / signal.c
blob0974c0ecc594817ee9d8067aeba0eee7e3379dbf
1 /* signal.c: FRV specific bits of signal handling
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
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.
13 #include <linux/sched.h>
14 #include <linux/mm.h>
15 #include <linux/smp.h>
16 #include <linux/kernel.h>
17 #include <linux/signal.h>
18 #include <linux/errno.h>
19 #include <linux/wait.h>
20 #include <linux/ptrace.h>
21 #include <linux/unistd.h>
22 #include <linux/personality.h>
23 #include <linux/freezer.h>
24 #include <linux/tracehook.h>
25 #include <asm/ucontext.h>
26 #include <asm/uaccess.h>
27 #include <asm/cacheflush.h>
29 #define DEBUG_SIG 0
31 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
33 struct fdpic_func_descriptor {
34 unsigned long text;
35 unsigned long GOT;
39 * Atomically swap in the new signal mask, and wait for a signal.
41 asmlinkage int sys_sigsuspend(int history0, int history1, old_sigset_t mask)
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);
50 current->state = TASK_INTERRUPTIBLE;
51 schedule();
52 set_thread_flag(TIF_RESTORE_SIGMASK);
53 return -ERESTARTNOHAND;
56 asmlinkage int sys_sigaction(int sig,
57 const struct old_sigaction __user *act,
58 struct old_sigaction __user *oact)
60 struct k_sigaction new_ka, old_ka;
61 int ret;
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);
74 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
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);
85 return ret;
88 asmlinkage
89 int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
91 return do_sigaltstack(uss, uoss, __frame->sp);
96 * Do a signal return; undo the signal stack.
99 struct sigframe
101 __sigrestore_t pretcode;
102 int sig;
103 struct sigcontext sc;
104 unsigned long extramask[_NSIG_WORDS-1];
105 uint32_t retcode[2];
108 struct rt_sigframe
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];
119 static int restore_sigcontext(struct sigcontext __user *sc, int *_gr8)
121 struct user_context *user = current->thread.user;
122 unsigned long tbr, psr;
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;
131 restore_user_regs(user);
133 user->i.syscallno = -1; /* disable syscall checks */
135 *_gr8 = user->i.gr[8];
136 return 0;
138 badframe:
139 return 1;
142 asmlinkage int sys_sigreturn(void)
144 struct sigframe __user *frame = (struct sigframe __user *) __frame->sp;
145 sigset_t set;
146 int gr8;
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;
153 if (_NSIG_WORDS > 1 &&
154 __copy_from_user(&set.sig[1], &frame->extramask, sizeof(frame->extramask)))
155 goto badframe;
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);
163 if (restore_sigcontext(&frame->sc, &gr8))
164 goto badframe;
165 return gr8;
167 badframe:
168 force_sig(SIGSEGV, current);
169 return 0;
172 asmlinkage int sys_rt_sigreturn(void)
174 struct rt_sigframe __user *frame = (struct rt_sigframe __user *) __frame->sp;
175 sigset_t set;
176 int gr8;
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;
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);
189 if (restore_sigcontext(&frame->uc.uc_mcontext, &gr8))
190 goto badframe;
192 if (do_sigaltstack(&frame->uc.uc_stack, NULL, __frame->sp) == -EFAULT)
193 goto badframe;
195 return gr8;
197 badframe:
198 force_sig(SIGSEGV, current);
199 return 0;
203 * Set up a signal frame
205 static int setup_sigcontext(struct sigcontext __user *sc, unsigned long mask)
207 save_user_regs(current->thread.user);
209 if (copy_to_user(&sc->sc_context, current->thread.user, sizeof(sc->sc_context)) != 0)
210 goto badframe;
212 /* non-iBCS2 extensions.. */
213 if (__put_user(mask, &sc->sc_oldmask) < 0)
214 goto badframe;
216 return 0;
218 badframe:
219 return 1;
222 /*****************************************************************************/
224 * Determine which stack to use..
226 static inline void __user *get_sigframe(struct k_sigaction *ka,
227 size_t frame_size)
229 unsigned long sp;
231 /* Default to using normal stack */
232 sp = __frame->sp;
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;
240 return (void __user *) ((sp - frame_size) & ~7UL);
242 } /* end get_sigframe() */
244 /*****************************************************************************/
248 static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set)
250 struct sigframe __user *frame;
251 int rsig;
253 frame = get_sigframe(ka, sizeof(*frame));
255 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
256 goto give_sigsegv;
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];
264 if (__put_user(rsig, &frame->sig) < 0)
265 goto give_sigsegv;
267 if (setup_sigcontext(&frame->sc, set->sig[0]))
268 goto give_sigsegv;
270 if (_NSIG_WORDS > 1) {
271 if (__copy_to_user(frame->extramask, &set->sig[1],
272 sizeof(frame->extramask)))
273 goto give_sigsegv;
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;
282 else {
283 /* Set up the following code on the stack:
284 * setlos #__NR_sigreturn,gr7
285 * tira gr0,0
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;
292 flush_icache_range((unsigned long) frame->retcode,
293 (unsigned long) (frame->retcode + 2));
296 /* set up registers for signal handler */
297 __frame->sp = (unsigned long) frame;
298 __frame->lr = (unsigned long) &frame->retcode;
299 __frame->gr8 = sig;
301 if (current->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;
311 set_fs(USER_DS);
313 /* the tracer may want to single-step inside the handler */
314 if (test_thread_flag(TIF_SINGLESTEP))
315 ptrace_notify(SIGTRAP);
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
323 return 0;
325 give_sigsegv:
326 force_sig(SIGSEGV, current);
327 return -EFAULT;
329 } /* end setup_frame() */
331 /*****************************************************************************/
335 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
336 sigset_t *set)
338 struct rt_sigframe __user *frame;
339 int rsig;
341 frame = get_sigframe(ka, sizeof(*frame));
343 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
344 goto give_sigsegv;
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];
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;
357 if (copy_siginfo_to_user(&frame->info, info))
358 goto give_sigsegv;
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;
368 if (setup_sigcontext(&frame->uc.uc_mcontext, set->sig[0]))
369 goto give_sigsegv;
371 if (__copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
372 goto give_sigsegv;
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;
380 else {
381 /* Set up the following code on the stack:
382 * setlos #__NR_sigreturn,gr7
383 * tira gr0,0
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;
390 flush_icache_range((unsigned long) frame->retcode,
391 (unsigned long) (frame->retcode + 2));
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;
400 if (current->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;
410 set_fs(USER_DS);
412 /* the tracer may want to single-step inside the handler */
413 if (test_thread_flag(TIF_SINGLESTEP))
414 ptrace_notify(SIGTRAP);
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
422 return 0;
424 give_sigsegv:
425 force_sig(SIGSEGV, current);
426 return -EFAULT;
428 } /* end setup_rt_frame() */
430 /*****************************************************************************/
432 * OK, we're invoking a handler
434 static int handle_signal(unsigned long sig, siginfo_t *info,
435 struct k_sigaction *ka, sigset_t *oldset)
437 int ret;
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;
448 case -ERESTARTSYS:
449 if (!(ka->sa.sa_flags & SA_RESTART)) {
450 __frame->gr8 = -EINTR;
451 break;
454 /* fallthrough */
455 case -ERESTARTNOINTR:
456 __frame->gr8 = __frame->orig_gr8;
457 __frame->pc -= 4;
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);
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);
477 return ret;
479 } /* end handle_signal() */
481 /*****************************************************************************/
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.
487 static void do_signal(void)
489 struct k_sigaction ka;
490 siginfo_t info;
491 sigset_t *oldset;
492 int signr;
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.
500 if (!user_mode(__frame))
501 return;
503 if (try_to_freeze())
504 goto no_signal;
506 if (test_thread_flag(TIF_RESTORE_SIGMASK))
507 oldset = &current->saved_sigmask;
508 else
509 oldset = &current->blocked;
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);
521 tracehook_signal_handler(signr, &info, &ka, __frame,
522 test_thread_flag(TIF_SINGLESTEP));
525 return;
528 no_signal:
529 /* Did we come from a system call? */
530 if (__frame->syscallno != -1) {
531 /* Restart the system call - no handlers present */
532 switch (__frame->gr8) {
533 case -ERESTARTNOHAND:
534 case -ERESTARTSYS:
535 case -ERESTARTNOINTR:
536 __frame->gr8 = __frame->orig_gr8;
537 __frame->pc -= 4;
538 break;
540 case -ERESTART_RESTARTBLOCK:
541 __frame->gr8 = __NR_restart_syscall;
542 __frame->pc -= 4;
543 break;
547 /* if there's no signal to deliver, we just put the saved sigmask
548 * back */
549 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
550 clear_thread_flag(TIF_RESTORE_SIGMASK);
551 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
554 } /* end do_signal() */
556 /*****************************************************************************/
558 * notification of userspace execution resumption
559 * - triggered by the TIF_WORK_MASK flags
561 asmlinkage void do_notify_resume(__u32 thread_info_flags)
563 /* pending single-step? */
564 if (thread_info_flags & _TIF_SINGLESTEP)
565 clear_thread_flag(TIF_SINGLESTEP);
567 /* deal with pending signal delivery */
568 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
569 do_signal();
571 /* deal with notification on about to resume userspace execution */
572 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
573 clear_thread_flag(TIF_NOTIFY_RESUME);
574 tracehook_notify_resume(__frame);
575 if (current->replacement_session_keyring)
576 key_replace_session_keyring();
579 } /* end do_notify_resume() */