linux/arch/blackfin/kernel/signal.c
<<
>>
Prefs
   1/*
   2 * Copyright 2004-2009 Analog Devices Inc.
   3 *
   4 * Licensed under the GPL-2 or later
   5 */
   6
   7#include <linux/signal.h>
   8#include <linux/syscalls.h>
   9#include <linux/ptrace.h>
  10#include <linux/tty.h>
  11#include <linux/personality.h>
  12#include <linux/binfmts.h>
  13#include <linux/freezer.h>
  14#include <linux/uaccess.h>
  15
  16#include <asm/cacheflush.h>
  17#include <asm/ucontext.h>
  18#include <asm/fixed_code.h>
  19
  20#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  21
  22/* Location of the trace bit in SYSCFG. */
  23#define TRACE_BITS 0x0001
  24
  25struct fdpic_func_descriptor {
  26        unsigned long   text;
  27        unsigned long   GOT;
  28};
  29
  30struct rt_sigframe {
  31        int sig;
  32        struct siginfo *pinfo;
  33        void *puc;
  34        /* This is no longer needed by the kernel, but unfortunately userspace
  35         * code expects it to be there.  */
  36        char retcode[8];
  37        struct siginfo info;
  38        struct ucontext uc;
  39};
  40
  41asmlinkage int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
  42{
  43        return do_sigaltstack(uss, uoss, rdusp());
  44}
  45
  46static inline int
  47rt_restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *pr0)
  48{
  49        unsigned long usp = 0;
  50        int err = 0;
  51
  52#define RESTORE(x) err |= __get_user(regs->x, &sc->sc_##x)
  53
  54        /* restore passed registers */
  55        RESTORE(r0); RESTORE(r1); RESTORE(r2); RESTORE(r3);
  56        RESTORE(r4); RESTORE(r5); RESTORE(r6); RESTORE(r7);
  57        RESTORE(p0); RESTORE(p1); RESTORE(p2); RESTORE(p3);
  58        RESTORE(p4); RESTORE(p5);
  59        err |= __get_user(usp, &sc->sc_usp);
  60        wrusp(usp);
  61        RESTORE(a0w); RESTORE(a1w);
  62        RESTORE(a0x); RESTORE(a1x);
  63        RESTORE(astat);
  64        RESTORE(rets);
  65        RESTORE(pc);
  66        RESTORE(retx);
  67        RESTORE(fp);
  68        RESTORE(i0); RESTORE(i1); RESTORE(i2); RESTORE(i3);
  69        RESTORE(m0); RESTORE(m1); RESTORE(m2); RESTORE(m3);
  70        RESTORE(l0); RESTORE(l1); RESTORE(l2); RESTORE(l3);
  71        RESTORE(b0); RESTORE(b1); RESTORE(b2); RESTORE(b3);
  72        RESTORE(lc0); RESTORE(lc1);
  73        RESTORE(lt0); RESTORE(lt1);
  74        RESTORE(lb0); RESTORE(lb1);
  75        RESTORE(seqstat);
  76
  77        regs->orig_p0 = -1;     /* disable syscall checks */
  78
  79        *pr0 = regs->r0;
  80        return err;
  81}
  82
  83asmlinkage int do_rt_sigreturn(unsigned long __unused)
  84{
  85        struct pt_regs *regs = (struct pt_regs *)__unused;
  86        unsigned long usp = rdusp();
  87        struct rt_sigframe *frame = (struct rt_sigframe *)(usp);
  88        sigset_t set;
  89        int r0;
  90
  91        if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  92                goto badframe;
  93        if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  94                goto badframe;
  95
  96        sigdelsetmask(&set, ~_BLOCKABLE);
  97        spin_lock_irq(&current->sighand->siglock);
  98        current->blocked = set;
  99        recalc_sigpending();
 100        spin_unlock_irq(&current->sighand->siglock);
 101
 102        if (rt_restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
 103                goto badframe;
 104
 105        if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->usp) == -EFAULT)
 106                goto badframe;
 107
 108        return r0;
 109
 110 badframe:
 111        force_sig(SIGSEGV, current);
 112        return 0;
 113}
 114
 115static inline int rt_setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs)
 116{
 117        int err = 0;
 118
 119#define SETUP(x) err |= __put_user(regs->x, &sc->sc_##x)
 120
 121        SETUP(r0); SETUP(r1); SETUP(r2); SETUP(r3);
 122        SETUP(r4); SETUP(r5); SETUP(r6); SETUP(r7);
 123        SETUP(p0); SETUP(p1); SETUP(p2); SETUP(p3);
 124        SETUP(p4); SETUP(p5);
 125        err |= __put_user(rdusp(), &sc->sc_usp);
 126        SETUP(a0w); SETUP(a1w);
 127        SETUP(a0x); SETUP(a1x);
 128        SETUP(astat);
 129        SETUP(rets);
 130        SETUP(pc);
 131        SETUP(retx);
 132        SETUP(fp);
 133        SETUP(i0); SETUP(i1); SETUP(i2); SETUP(i3);
 134        SETUP(m0); SETUP(m1); SETUP(m2); SETUP(m3);
 135        SETUP(l0); SETUP(l1); SETUP(l2); SETUP(l3);
 136        SETUP(b0); SETUP(b1); SETUP(b2); SETUP(b3);
 137        SETUP(lc0); SETUP(lc1);
 138        SETUP(lt0); SETUP(lt1);
 139        SETUP(lb0); SETUP(lb1);
 140        SETUP(seqstat);
 141
 142        return err;
 143}
 144
 145static inline void *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
 146                                 size_t frame_size)
 147{
 148        unsigned long usp;
 149
 150        /* Default to using normal stack.  */
 151        usp = rdusp();
 152
 153        /* This is the X/Open sanctioned signal stack switching.  */
 154        if (ka->sa.sa_flags & SA_ONSTACK) {
 155                if (!on_sig_stack(usp))
 156                        usp = current->sas_ss_sp + current->sas_ss_size;
 157        }
 158        return (void *)((usp - frame_size) & -8UL);
 159}
 160
 161static int
 162setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t * info,
 163               sigset_t * set, struct pt_regs *regs)
 164{
 165        struct rt_sigframe *frame;
 166        int err = 0;
 167
 168        frame = get_sigframe(ka, regs, sizeof(*frame));
 169
 170        err |= __put_user((current_thread_info()->exec_domain
 171                           && current_thread_info()->exec_domain->signal_invmap
 172                           && sig < 32
 173                           ? current_thread_info()->exec_domain->
 174                           signal_invmap[sig] : sig), &frame->sig);
 175
 176        err |= __put_user(&frame->info, &frame->pinfo);
 177        err |= __put_user(&frame->uc, &frame->puc);
 178        err |= copy_siginfo_to_user(&frame->info, info);
 179
 180        /* Create the ucontext.  */
 181        err |= __put_user(0, &frame->uc.uc_flags);
 182        err |= __put_user(0, &frame->uc.uc_link);
 183        err |=
 184            __put_user((void *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
 185        err |= __put_user(sas_ss_flags(rdusp()), &frame->uc.uc_stack.ss_flags);
 186        err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
 187        err |= rt_setup_sigcontext(&frame->uc.uc_mcontext, regs);
 188        err |= copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
 189
 190        if (err)
 191                goto give_sigsegv;
 192
 193        /* Set up registers for signal handler */
 194        wrusp((unsigned long)frame);
 195        if (current->personality & FDPIC_FUNCPTRS) {
 196                struct fdpic_func_descriptor __user *funcptr =
 197                        (struct fdpic_func_descriptor *) ka->sa.sa_handler;
 198                __get_user(regs->pc, &funcptr->text);
 199                __get_user(regs->p3, &funcptr->GOT);
 200        } else
 201                regs->pc = (unsigned long)ka->sa.sa_handler;
 202        regs->rets = SIGRETURN_STUB;
 203
 204        regs->r0 = frame->sig;
 205        regs->r1 = (unsigned long)(&frame->info);
 206        regs->r2 = (unsigned long)(&frame->uc);
 207
 208        /*
 209         * Clear the trace flag when entering the signal handler, but
 210         * notify any tracer that was single-stepping it. The tracer
 211         * may want to single-step inside the handler too.
 212         */
 213        if (regs->syscfg & TRACE_BITS) {
 214                regs->syscfg &= ~TRACE_BITS;
 215                ptrace_notify(SIGTRAP);
 216        }
 217
 218        return 0;
 219
 220 give_sigsegv:
 221        if (sig == SIGSEGV)
 222                ka->sa.sa_handler = SIG_DFL;
 223        force_sig(SIGSEGV, current);
 224        return -EFAULT;
 225}
 226
 227static inline void
 228handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
 229{
 230        switch (regs->r0) {
 231        case -ERESTARTNOHAND:
 232                if (!has_handler)
 233                        goto do_restart;
 234                regs->r0 = -EINTR;
 235                break;
 236
 237        case -ERESTARTSYS:
 238                if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
 239                        regs->r0 = -EINTR;
 240                        break;
 241                }
 242                /* fallthrough */
 243        case -ERESTARTNOINTR:
 244 do_restart:
 245                regs->p0 = regs->orig_p0;
 246                regs->r0 = regs->orig_r0;
 247                regs->pc -= 2;
 248                break;
 249        }
 250}
 251
 252/*
 253 * OK, we're invoking a handler
 254 */
 255static int
 256handle_signal(int sig, siginfo_t *info, struct k_sigaction *ka,
 257              sigset_t *oldset, struct pt_regs *regs)
 258{
 259        int ret;
 260
 261        /* are we from a system call? to see pt_regs->orig_p0 */
 262        if (regs->orig_p0 >= 0)
 263                /* If so, check system call restarting.. */
 264                handle_restart(regs, ka, 1);
 265
 266        /* set up the stack frame */
 267        ret = setup_rt_frame(sig, ka, info, oldset, regs);
 268
 269        if (ret == 0) {
 270                spin_lock_irq(&current->sighand->siglock);
 271                sigorsets(&current->blocked, &current->blocked,
 272                          &ka->sa.sa_mask);
 273                if (!(ka->sa.sa_flags & SA_NODEFER))
 274                        sigaddset(&current->blocked, sig);
 275                recalc_sigpending();
 276                spin_unlock_irq(&current->sighand->siglock);
 277        }
 278        return ret;
 279}
 280
 281/*
 282 * Note that 'init' is a special process: it doesn't get signals it doesn't
 283 * want to handle. Thus you cannot kill init even with a SIGKILL even by
 284 * mistake.
 285 *
 286 * Note that we go through the signals twice: once to check the signals
 287 * that the kernel can handle, and then we build all the user-level signal
 288 * handling stack-frames in one go after that.
 289 */
 290asmlinkage void do_signal(struct pt_regs *regs)
 291{
 292        siginfo_t info;
 293        int signr;
 294        struct k_sigaction ka;
 295        sigset_t *oldset;
 296
 297        current->thread.esp0 = (unsigned long)regs;
 298
 299        if (try_to_freeze())
 300                goto no_signal;
 301
 302        if (test_thread_flag(TIF_RESTORE_SIGMASK))
 303                oldset = &current->saved_sigmask;
 304        else
 305                oldset = &current->blocked;
 306
 307        signr = get_signal_to_deliver(&info, &ka, regs, NULL);
 308        if (signr > 0) {
 309                /* Whee!  Actually deliver the signal.  */
 310                if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
 311                        /* a signal was successfully delivered; the saved
 312                         * sigmask will have been stored in the signal frame,
 313                         * and will be restored by sigreturn, so we can simply
 314                         * clear the TIF_RESTORE_SIGMASK flag */
 315                        if (test_thread_flag(TIF_RESTORE_SIGMASK))
 316                                clear_thread_flag(TIF_RESTORE_SIGMASK);
 317                }
 318
 319                return;
 320        }
 321
 322 no_signal:
 323        /* Did we come from a system call? */
 324        if (regs->orig_p0 >= 0)
 325                /* Restart the system call - no handlers present */
 326                handle_restart(regs, NULL, 0);
 327
 328        /* if there's no signal to deliver, we just put the saved sigmask
 329         * back */
 330        if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
 331                clear_thread_flag(TIF_RESTORE_SIGMASK);
 332                sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
 333        }
 334}
 335