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/tracehook.h>
  19
  20#include <asm/uaccess.h>
  21#include <asm/ucontext.h>
  22#include <asm/syscalls.h>
  23
  24struct rt_sigframe
  25{
  26        struct siginfo info;
  27        struct ucontext uc;
  28        unsigned long retcode;
  29};
  30
  31static int
  32restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
  33{
  34        int err = 0;
  35
  36#define COPY(x)         err |= __get_user(regs->x, &sc->x)
  37        COPY(sr);
  38        COPY(pc);
  39        COPY(lr);
  40        COPY(sp);
  41        COPY(r12);
  42        COPY(r11);
  43        COPY(r10);
  44        COPY(r9);
  45        COPY(r8);
  46        COPY(r7);
  47        COPY(r6);
  48        COPY(r5);
  49        COPY(r4);
  50        COPY(r3);
  51        COPY(r2);
  52        COPY(r1);
  53        COPY(r0);
  54#undef  COPY
  55
  56        /*
  57         * Don't allow anyone to pretend they're running in supervisor
  58         * mode or something...
  59         */
  60        err |= !valid_user_regs(regs);
  61
  62        return err;
  63}
  64
  65
  66asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
  67{
  68        struct rt_sigframe __user *frame;
  69        sigset_t set;
  70
  71        /* Always make any pending restarted system calls return -EINTR */
  72        current_thread_info()->restart_block.fn = do_no_restart_syscall;
  73
  74        frame = (struct rt_sigframe __user *)regs->sp;
  75        pr_debug("SIG return: frame = %p\n", frame);
  76
  77        if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  78                goto badframe;
  79
  80        if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  81                goto badframe;
  82
  83        set_current_blocked(&set);
  84
  85        if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
  86                goto badframe;
  87
  88        if (restore_altstack(&frame->uc.uc_stack))
  89                goto badframe;
  90
  91        pr_debug("Context restored: pc = %08lx, lr = %08lx, sp = %08lx\n",
  92                 regs->pc, regs->lr, regs->sp);
  93
  94        return regs->r12;
  95
  96badframe:
  97        force_sig(SIGSEGV, current);
  98        return 0;
  99}
 100
 101static int
 102setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
 103{
 104        int err = 0;
 105
 106#define COPY(x)         err |= __put_user(regs->x, &sc->x)
 107        COPY(sr);
 108        COPY(pc);
 109        COPY(lr);
 110        COPY(sp);
 111        COPY(r12);
 112        COPY(r11);
 113        COPY(r10);
 114        COPY(r9);
 115        COPY(r8);
 116        COPY(r7);
 117        COPY(r6);
 118        COPY(r5);
 119        COPY(r4);
 120        COPY(r3);
 121        COPY(r2);
 122        COPY(r1);
 123        COPY(r0);
 124#undef  COPY
 125
 126        return err;
 127}
 128
 129static inline void __user *
 130get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
 131{
 132        unsigned long sp = regs->sp;
 133
 134        if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
 135                sp = current->sas_ss_sp + current->sas_ss_size;
 136
 137        return (void __user *)((sp - framesize) & ~3);
 138}
 139
 140static int
 141setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
 142               sigset_t *set, struct pt_regs *regs)
 143{
 144        struct rt_sigframe __user *frame;
 145        int err = 0;
 146
 147        frame = get_sigframe(ka, regs, sizeof(*frame));
 148        err = -EFAULT;
 149        if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
 150                goto out;
 151
 152        /*
 153         * Set up the return code:
 154         *
 155         *      mov     r8, __NR_rt_sigreturn
 156         *      scall
 157         *
 158         * Note: This will blow up since we're using a non-executable
 159         * stack. Better use SA_RESTORER.
 160         */
 161#if __NR_rt_sigreturn > 127
 162# error __NR_rt_sigreturn must be < 127 to fit in a short mov
 163#endif
 164        err = __put_user(0x3008d733 | (__NR_rt_sigreturn << 20),
 165                         &frame->retcode);
 166
 167        err |= copy_siginfo_to_user(&frame->info, info);
 168
 169        /* Set up the ucontext */
 170        err |= __put_user(0, &frame->uc.uc_flags);
 171        err |= __put_user(NULL, &frame->uc.uc_link);
 172        err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
 173        err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);
 174        err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
 175
 176        if (err)
 177                goto out;
 178
 179        regs->r12 = sig;
 180        regs->r11 = (unsigned long) &frame->info;
 181        regs->r10 = (unsigned long) &frame->uc;
 182        regs->sp = (unsigned long) frame;
 183        if (ka->sa.sa_flags & SA_RESTORER)
 184                regs->lr = (unsigned long)ka->sa.sa_restorer;
 185        else {
 186                printk(KERN_NOTICE "[%s:%d] did not set SA_RESTORER\n",
 187                       current->comm, current->pid);
 188                regs->lr = (unsigned long) &frame->retcode;
 189        }
 190
 191        pr_debug("SIG deliver [%s:%d]: sig=%d sp=0x%lx pc=0x%lx->0x%p lr=0x%lx\n",
 192                 current->comm, current->pid, sig, regs->sp,
 193                 regs->pc, ka->sa.sa_handler, regs->lr);
 194
 195        regs->pc = (unsigned long) ka->sa.sa_handler;
 196
 197out:
 198        return err;
 199}
 200
 201static inline void setup_syscall_restart(struct pt_regs *regs)
 202{
 203        if (regs->r12 == -ERESTART_RESTARTBLOCK)
 204                regs->r8 = __NR_restart_syscall;
 205        else
 206                regs->r12 = regs->r12_orig;
 207        regs->pc -= 2;
 208}
 209
 210static inline void
 211handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
 212              struct pt_regs *regs, int syscall)
 213{
 214        int ret;
 215
 216        /*
 217         * Set up the stack frame
 218         */
 219        ret = setup_rt_frame(sig, ka, info, sigmask_to_save(), regs);
 220
 221        /*
 222         * Check that the resulting registers are sane
 223         */
 224        ret |= !valid_user_regs(regs);
 225
 226        /*
 227         * Block the signal if we were successful.
 228         */
 229        if (ret != 0)
 230                force_sigsegv(sig, current);
 231        else
 232                signal_delivered(sig, info, ka, regs, 0);
 233}
 234
 235/*
 236 * Note that 'init' is a special process: it doesn't get signals it
 237 * doesn't want to handle. Thus you cannot kill init even with a
 238 * SIGKILL even by mistake.
 239 */
 240static void do_signal(struct pt_regs *regs, int syscall)
 241{
 242        siginfo_t info;
 243        int signr;
 244        struct k_sigaction ka;
 245
 246        /*
 247         * We want the common case to go fast, which is why we may in
 248         * certain cases get here from kernel mode. Just return
 249         * without doing anything if so.
 250         */
 251        if (!user_mode(regs))
 252                return;
 253
 254        signr = get_signal_to_deliver(&info, &ka, regs, NULL);
 255        if (syscall) {
 256                switch (regs->r12) {
 257                case -ERESTART_RESTARTBLOCK:
 258                case -ERESTARTNOHAND:
 259                        if (signr > 0) {
 260                                regs->r12 = -EINTR;
 261                                break;
 262                        }
 263                        /* fall through */
 264                case -ERESTARTSYS:
 265                        if (signr > 0 && !(ka.sa.sa_flags & SA_RESTART)) {
 266                                regs->r12 = -EINTR;
 267                                break;
 268                        }
 269                        /* fall through */
 270                case -ERESTARTNOINTR:
 271                        setup_syscall_restart(regs);
 272                }
 273        }
 274
 275        if (signr == 0) {
 276                /* No signal to deliver -- put the saved sigmask back */
 277                restore_saved_sigmask();
 278                return;
 279        }
 280
 281        handle_signal(signr, &ka, &info, regs, syscall);
 282}
 283
 284asmlinkage void do_notify_resume(struct pt_regs *regs, struct thread_info *ti)
 285{
 286        int syscall = 0;
 287
 288        if ((sysreg_read(SR) & MODE_MASK) == MODE_SUPERVISOR)
 289                syscall = 1;
 290
 291        if (ti->flags & _TIF_SIGPENDING)
 292                do_signal(regs, syscall);
 293
 294        if (ti->flags & _TIF_NOTIFY_RESUME) {
 295                clear_thread_flag(TIF_NOTIFY_RESUME);
 296                tracehook_notify_resume(regs);
 297        }
 298}
 299