linux/arch/avr32/kernel/signal.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2004-2006 Atmel Corporation
   3 *
   4 * Based on linux/arch/sh/kernel/signal.c
   5 *  Copyright (C) 1999, 2000  Niibe Yutaka & Kaz Kojima
   6 *  Copyright (C) 1991, 1992  Linus Torvalds
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License version 2 as
  10 * published by the Free Software Foundation.
  11 */
  12
  13#include <linux/sched.h>
  14#include <linux/mm.h>
  15#include <linux/errno.h>
  16#include <linux/ptrace.h>
  17#include <linux/unistd.h>
  18#include <linux/freezer.h>
  19
  20#include <asm/uaccess.h>
  21#include <asm/ucontext.h>
  22
  23#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  24
  25asmlinkage int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
  26                               struct pt_regs *regs)
  27{
  28        return do_sigaltstack(uss, uoss, regs->sp);
  29}
  30
  31struct rt_sigframe
  32{
  33        struct siginfo info;
  34        struct ucontext uc;
  35        unsigned long retcode;
  36};
  37
  38static int
  39restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
  40{
  41        int err = 0;
  42
  43#define COPY(x)         err |= __get_user(regs->x, &sc->x)
  44        COPY(sr);
  45        COPY(pc);
  46        COPY(lr);
  47        COPY(sp);
  48        COPY(r12);
  49        COPY(r11);
  50        COPY(r10);
  51        COPY(r9);
  52        COPY(r8);
  53        COPY(r7);
  54        COPY(r6);
  55        COPY(r5);
  56        COPY(r4);
  57        COPY(r3);
  58        COPY(r2);
  59        COPY(r1);
  60        COPY(r0);
  61#undef  COPY
  62
  63        /*
  64         * Don't allow anyone to pretend they're running in supervisor
  65         * mode or something...
  66         */
  67        err |= !valid_user_regs(regs);
  68
  69        return err;
  70}
  71
  72
  73asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
  74{
  75        struct rt_sigframe __user *frame;
  76        sigset_t set;
  77
  78        frame = (struct rt_sigframe __user *)regs->sp;
  79        pr_debug("SIG return: frame = %p\n", frame);
  80
  81        if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  82                goto badframe;
  83
  84        if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  85                goto badframe;
  86
  87        sigdelsetmask(&set, ~_BLOCKABLE);
  88        spin_lock_irq(&current->sighand->siglock);
  89        current->blocked = set;
  90        recalc_sigpending();
  91        spin_unlock_irq(&current->sighand->siglock);
  92
  93        if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
  94                goto badframe;
  95
  96        if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
  97                goto badframe;
  98
  99        pr_debug("Context restored: pc = %08lx, lr = %08lx, sp = %08lx\n",
 100                 regs->pc, regs->lr, regs->sp);
 101
 102        return regs->r12;
 103
 104badframe:
 105        force_sig(SIGSEGV, current);
 106        return 0;
 107}
 108
 109static int
 110setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
 111{
 112        int err = 0;
 113
 114#define COPY(x)         err |= __put_user(regs->x, &sc->x)
 115        COPY(sr);
 116        COPY(pc);
 117        COPY(lr);
 118        COPY(sp);
 119        COPY(r12);
 120        COPY(r11);
 121        COPY(r10);
 122        COPY(r9);
 123        COPY(r8);
 124        COPY(r7);
 125        COPY(r6);
 126        COPY(r5);
 127        COPY(r4);
 128        COPY(r3);
 129        COPY(r2);
 130        COPY(r1);
 131        COPY(r0);
 132#undef  COPY
 133
 134        return err;
 135}
 136
 137static inline void __user *
 138get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
 139{
 140        unsigned long sp = regs->sp;
 141
 142        if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
 143                sp = current->sas_ss_sp + current->sas_ss_size;
 144
 145        return (void __user *)((sp - framesize) & ~3);
 146}
 147
 148static int
 149setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
 150               sigset_t *set, struct pt_regs *regs)
 151{
 152        struct rt_sigframe __user *frame;
 153        int err = 0;
 154
 155        frame = get_sigframe(ka, regs, sizeof(*frame));
 156        err = -EFAULT;
 157        if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
 158                goto out;
 159
 160        /*
 161         * Set up the return code:
 162         *
 163         *      mov     r8, __NR_rt_sigreturn
 164         *      scall
 165         *
 166         * Note: This will blow up since we're using a non-executable
 167         * stack. Better use SA_RESTORER.
 168         */
 169#if __NR_rt_sigreturn > 127
 170# error __NR_rt_sigreturn must be < 127 to fit in a short mov
 171#endif
 172        err = __put_user(0x3008d733 | (__NR_rt_sigreturn << 20),
 173                         &frame->retcode);
 174
 175        err |= copy_siginfo_to_user(&frame->info, info);
 176
 177        /* Set up the ucontext */
 178        err |= __put_user(0, &frame->uc.uc_flags);
 179        err |= __put_user(NULL, &frame->uc.uc_link);
 180        err |= __put_user((void __user *)current->sas_ss_sp,
 181                          &frame->uc.uc_stack.ss_sp);
 182        err |= __put_user(sas_ss_flags(regs->sp),
 183                          &frame->uc.uc_stack.ss_flags);
 184        err |= __put_user(current->sas_ss_size,
 185                          &frame->uc.uc_stack.ss_size);
 186        err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);
 187        err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
 188
 189        if (err)
 190                goto out;
 191
 192        regs->r12 = sig;
 193        regs->r11 = (unsigned long) &frame->info;
 194        regs->r10 = (unsigned long) &frame->uc;
 195        regs->sp = (unsigned long) frame;
 196        if (ka->sa.sa_flags & SA_RESTORER)
 197                regs->lr = (unsigned long)ka->sa.sa_restorer;
 198        else {
 199                printk(KERN_NOTICE "[%s:%d] did not set SA_RESTORER\n",
 200                       current->comm, current->pid);
 201                regs->lr = (unsigned long) &frame->retcode;
 202        }
 203
 204        pr_debug("SIG deliver [%s:%d]: sig=%d sp=0x%lx pc=0x%lx->0x%p lr=0x%lx\n",
 205                 current->comm, current->pid, sig, regs->sp,
 206                 regs->pc, ka->sa.sa_handler, regs->lr);
 207
 208        regs->pc = (unsigned long) ka->sa.sa_handler;
 209
 210out:
 211        return err;
 212}
 213
 214static inline void restart_syscall(struct pt_regs *regs)
 215{
 216        if (regs->r12 == -ERESTART_RESTARTBLOCK)
 217                regs->r8 = __NR_restart_syscall;
 218        else
 219                regs->r12 = regs->r12_orig;
 220        regs->pc -= 2;
 221}
 222
 223static inline void
 224handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
 225              sigset_t *oldset, struct pt_regs *regs, int syscall)
 226{
 227        int ret;
 228
 229        /*
 230         * Set up the stack frame
 231         */
 232        ret = setup_rt_frame(sig, ka, info, oldset, regs);
 233
 234        /*
 235         * Check that the resulting registers are sane
 236         */
 237        ret |= !valid_user_regs(regs);
 238
 239        /*
 240         * Block the signal if we were unsuccessful.
 241         */
 242        if (ret != 0 || !(ka->sa.sa_flags & SA_NODEFER)) {
 243                spin_lock_irq(&current->sighand->siglock);
 244                sigorsets(&current->blocked, &current->blocked,
 245                          &ka->sa.sa_mask);
 246                sigaddset(&current->blocked, sig);
 247                recalc_sigpending();
 248                spin_unlock_irq(&current->sighand->siglock);
 249        }
 250
 251        if (ret == 0)
 252                return;
 253
 254        force_sigsegv(sig, current);
 255}
 256
 257/*
 258 * Note that 'init' is a special process: it doesn't get signals it
 259 * doesn't want to handle. Thus you cannot kill init even with a
 260 * SIGKILL even by mistake.
 261 */
 262int do_signal(struct pt_regs *regs, sigset_t *oldset, int syscall)
 263{
 264        siginfo_t info;
 265        int signr;
 266        struct k_sigaction ka;
 267
 268        /*
 269         * We want the common case to go fast, which is why we may in
 270         * certain cases get here from kernel mode. Just return
 271         * without doing anything if so.
 272         */
 273        if (!user_mode(regs))
 274                return 0;
 275
 276        if (test_thread_flag(TIF_RESTORE_SIGMASK))
 277                oldset = &current->saved_sigmask;
 278        else if (!oldset)
 279                oldset = &current->blocked;
 280
 281        signr = get_signal_to_deliver(&info, &ka, regs, NULL);
 282        if (syscall) {
 283                switch (regs->r12) {
 284                case -ERESTART_RESTARTBLOCK:
 285                case -ERESTARTNOHAND:
 286                        if (signr > 0) {
 287                                regs->r12 = -EINTR;
 288                                break;
 289                        }
 290                        /* fall through */
 291                case -ERESTARTSYS:
 292                        if (signr > 0 && !(ka.sa.sa_flags & SA_RESTART)) {
 293                                regs->r12 = -EINTR;
 294                                break;
 295                        }
 296                        /* fall through */
 297                case -ERESTARTNOINTR:
 298                        restart_syscall(regs);
 299                }
 300        }
 301
 302        if (signr == 0) {
 303                /* No signal to deliver -- put the saved sigmask back */
 304                if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
 305                        clear_thread_flag(TIF_RESTORE_SIGMASK);
 306                        sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
 307                }
 308                return 0;
 309        }
 310
 311        handle_signal(signr, &ka, &info, oldset, regs, syscall);
 312        return 1;
 313}
 314
 315asmlinkage void do_notify_resume(struct pt_regs *regs, struct thread_info *ti)
 316{
 317        int syscall = 0;
 318
 319        if ((sysreg_read(SR) & MODE_MASK) == MODE_SUPERVISOR)
 320                syscall = 1;
 321
 322        if (ti->flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
 323                do_signal(regs, &current->blocked, syscall);
 324}
 325