linux/arch/openrisc/kernel/signal.c
<<
>>
Prefs
   1/*
   2 * OpenRISC signal.c
   3 *
   4 * Linux architectural port borrowing liberally from similar works of
   5 * others.  All original copyrights apply as per the original source
   6 * declaration.
   7 *
   8 * Modifications for the OpenRISC architecture:
   9 * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
  10 * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
  11 *
  12 *      This program is free software; you can redistribute it and/or
  13 *      modify it under the terms of the GNU General Public License
  14 *      as published by the Free Software Foundation; either version
  15 *      2 of the License, or (at your option) any later version.
  16 */
  17
  18#include <linux/sched.h>
  19#include <linux/mm.h>
  20#include <linux/smp.h>
  21#include <linux/kernel.h>
  22#include <linux/signal.h>
  23#include <linux/errno.h>
  24#include <linux/wait.h>
  25#include <linux/ptrace.h>
  26#include <linux/unistd.h>
  27#include <linux/stddef.h>
  28#include <linux/tracehook.h>
  29
  30#include <asm/processor.h>
  31#include <asm/ucontext.h>
  32#include <asm/uaccess.h>
  33
  34#define DEBUG_SIG 0
  35
  36struct rt_sigframe {
  37        struct siginfo *pinfo;
  38        void *puc;
  39        struct siginfo info;
  40        struct ucontext uc;
  41        unsigned char retcode[16];      /* trampoline code */
  42};
  43
  44static int restore_sigcontext(struct pt_regs *regs, struct sigcontext *sc)
  45{
  46        unsigned int err = 0;
  47
  48        /* Alwys make any pending restarted system call return -EINTR */
  49        current_thread_info()->restart_block.fn = do_no_restart_syscall;
  50
  51        /*
  52         * Restore the regs from &sc->regs.
  53         * (sc is already checked for VERIFY_READ since the sigframe was
  54         *  checked in sys_sigreturn previously)
  55         */
  56        if (__copy_from_user(regs, sc->regs.gpr, 32 * sizeof(unsigned long)))
  57                goto badframe;
  58        if (__copy_from_user(&regs->pc, &sc->regs.pc, sizeof(unsigned long)))
  59                goto badframe;
  60        if (__copy_from_user(&regs->sr, &sc->regs.sr, sizeof(unsigned long)))
  61                goto badframe;
  62
  63        /* make sure the SM-bit is cleared so user-mode cannot fool us */
  64        regs->sr &= ~SPR_SR_SM;
  65
  66        /* TODO: the other ports use regs->orig_XX to disable syscall checks
  67         * after this completes, but we don't use that mechanism. maybe we can
  68         * use it now ?
  69         */
  70
  71        return err;
  72
  73badframe:
  74        return 1;
  75}
  76
  77asmlinkage long _sys_rt_sigreturn(struct pt_regs *regs)
  78{
  79        struct rt_sigframe *frame = (struct rt_sigframe __user *)regs->sp;
  80        sigset_t set;
  81
  82        /*
  83         * Since we stacked the signal on a dword boundary,
  84         * then frame should be dword aligned here.  If it's
  85         * not, then the user is trying to mess with us.
  86         */
  87        if (((long)frame) & 3)
  88                goto badframe;
  89
  90        if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  91                goto badframe;
  92        if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  93                goto badframe;
  94
  95        set_current_blocked(&set);
  96
  97        if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
  98                goto badframe;
  99
 100        if (restore_altstack(&frame->uc.uc_stack))
 101                goto badframe;
 102
 103        return regs->gpr[11];
 104
 105badframe:
 106        force_sig(SIGSEGV, current);
 107        return 0;
 108}
 109
 110/*
 111 * Set up a signal frame.
 112 */
 113
 114static int setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs,
 115                            unsigned long mask)
 116{
 117        int err = 0;
 118
 119        /* copy the regs */
 120
 121        err |= __copy_to_user(sc->regs.gpr, regs, 32 * sizeof(unsigned long));
 122        err |= __copy_to_user(&sc->regs.pc, &regs->pc, sizeof(unsigned long));
 123        err |= __copy_to_user(&sc->regs.sr, &regs->sr, sizeof(unsigned long));
 124
 125        /* then some other stuff */
 126
 127        err |= __put_user(mask, &sc->oldmask);
 128
 129        return err;
 130}
 131
 132static inline unsigned long align_sigframe(unsigned long sp)
 133{
 134        return sp & ~3UL;
 135}
 136
 137/*
 138 * Work out where the signal frame should go.  It's either on the user stack
 139 * or the alternate stack.
 140 */
 141
 142static inline void __user *get_sigframe(struct k_sigaction *ka,
 143                                        struct pt_regs *regs, size_t frame_size)
 144{
 145        unsigned long sp = regs->sp;
 146        int onsigstack = on_sig_stack(sp);
 147
 148        /* redzone */
 149        sp -= STACK_FRAME_OVERHEAD;
 150
 151        /* This is the X/Open sanctioned signal stack switching.  */
 152        if ((ka->sa.sa_flags & SA_ONSTACK) && !onsigstack) {
 153                if (current->sas_ss_size)
 154                        sp = current->sas_ss_sp + current->sas_ss_size;
 155        }
 156
 157        sp = align_sigframe(sp - frame_size);
 158
 159        /*
 160         * If we are on the alternate signal stack and would overflow it, don't.
 161         * Return an always-bogus address instead so we will die with SIGSEGV.
 162         */
 163        if (onsigstack && !likely(on_sig_stack(sp)))
 164                return (void __user *)-1L;
 165
 166        return (void __user *)sp;
 167}
 168
 169/* grab and setup a signal frame.
 170 *
 171 * basically we stack a lot of state info, and arrange for the
 172 * user-mode program to return to the kernel using either a
 173 * trampoline which performs the syscall sigreturn, or a provided
 174 * user-mode trampoline.
 175 */
 176static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
 177                          sigset_t *set, struct pt_regs *regs)
 178{
 179        struct rt_sigframe *frame;
 180        unsigned long return_ip;
 181        int err = 0;
 182
 183        frame = get_sigframe(ka, regs, sizeof(*frame));
 184
 185        if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
 186                goto give_sigsegv;
 187
 188        err |= __put_user(&frame->info, &frame->pinfo);
 189        err |= __put_user(&frame->uc, &frame->puc);
 190
 191        if (ka->sa.sa_flags & SA_SIGINFO)
 192                err |= copy_siginfo_to_user(&frame->info, info);
 193        if (err)
 194                goto give_sigsegv;
 195
 196        /* Clear all the bits of the ucontext we don't use.  */
 197        err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
 198        err |= __put_user(0, &frame->uc.uc_flags);
 199        err |= __put_user(NULL, &frame->uc.uc_link);
 200        err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
 201        err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
 202
 203        err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
 204
 205        if (err)
 206                goto give_sigsegv;
 207
 208        /* trampoline - the desired return ip is the retcode itself */
 209        return_ip = (unsigned long)&frame->retcode;
 210        /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */
 211        err |= __put_user(0xa960, (short *)(frame->retcode + 0));
 212        err |= __put_user(__NR_rt_sigreturn, (short *)(frame->retcode + 2));
 213        err |= __put_user(0x20000001, (unsigned long *)(frame->retcode + 4));
 214        err |= __put_user(0x15000000, (unsigned long *)(frame->retcode + 8));
 215
 216        if (err)
 217                goto give_sigsegv;
 218
 219        /* TODO what is the current->exec_domain stuff and invmap ? */
 220
 221        /* Set up registers for signal handler */
 222        regs->pc = (unsigned long)ka->sa.sa_handler; /* what we enter NOW */
 223        regs->gpr[9] = (unsigned long)return_ip;     /* what we enter LATER */
 224        regs->gpr[3] = (unsigned long)sig;           /* arg 1: signo */
 225        regs->gpr[4] = (unsigned long)&frame->info;  /* arg 2: (siginfo_t*) */
 226        regs->gpr[5] = (unsigned long)&frame->uc;    /* arg 3: ucontext */
 227
 228        /* actually move the usp to reflect the stacked frame */
 229        regs->sp = (unsigned long)frame;
 230
 231        return 0;
 232
 233give_sigsegv:
 234        force_sigsegv(sig, current);
 235        return -EFAULT;
 236}
 237
 238static inline void
 239handle_signal(unsigned long sig,
 240              siginfo_t *info, struct k_sigaction *ka,
 241              struct pt_regs *regs)
 242{
 243        int ret;
 244
 245        ret = setup_rt_frame(sig, ka, info, sigmask_to_save(), regs);
 246        if (ret)
 247                return;
 248
 249        signal_delivered(sig, info, ka, regs,
 250                                 test_thread_flag(TIF_SINGLESTEP));
 251}
 252
 253/*
 254 * Note that 'init' is a special process: it doesn't get signals it doesn't
 255 * want to handle. Thus you cannot kill init even with a SIGKILL even by
 256 * mistake.
 257 *
 258 * Also note that the regs structure given here as an argument, is the latest
 259 * pushed pt_regs. It may or may not be the same as the first pushed registers
 260 * when the initial usermode->kernelmode transition took place. Therefore
 261 * we can use user_mode(regs) to see if we came directly from kernel or user
 262 * mode below.
 263 */
 264
 265void do_signal(struct pt_regs *regs)
 266{
 267        siginfo_t info;
 268        int signr;
 269        struct k_sigaction ka;
 270
 271        /*
 272         * We want the common case to go fast, which
 273         * is why we may in certain cases get here from
 274         * kernel mode. Just return without doing anything
 275         * if so.
 276         */
 277        if (!user_mode(regs))
 278                return;
 279
 280        signr = get_signal_to_deliver(&info, &ka, regs, NULL);
 281
 282        /* If we are coming out of a syscall then we need
 283         * to check if the syscall was interrupted and wants to be
 284         * restarted after handling the signal.  If so, the original
 285         * syscall number is put back into r11 and the PC rewound to
 286         * point at the l.sys instruction that resulted in the
 287         * original syscall.  Syscall results other than the four
 288         * below mean that the syscall executed to completion and no
 289         * restart is necessary.
 290         */
 291        if (regs->orig_gpr11) {
 292                int restart = 0;
 293
 294                switch (regs->gpr[11]) {
 295                case -ERESTART_RESTARTBLOCK:
 296                case -ERESTARTNOHAND:
 297                        /* Restart if there is no signal handler */
 298                        restart = (signr <= 0);
 299                        break;
 300                case -ERESTARTSYS:
 301                        /* Restart if there no signal handler or
 302                         * SA_RESTART flag is set */
 303                        restart = (signr <= 0 || (ka.sa.sa_flags & SA_RESTART));
 304                        break;
 305                case -ERESTARTNOINTR:
 306                        /* Always restart */
 307                        restart = 1;
 308                        break;
 309                }
 310
 311                if (restart) {
 312                        if (regs->gpr[11] == -ERESTART_RESTARTBLOCK)
 313                                regs->gpr[11] = __NR_restart_syscall;
 314                        else
 315                                regs->gpr[11] = regs->orig_gpr11;
 316                        regs->pc -= 4;
 317                } else {
 318                        regs->gpr[11] = -EINTR;
 319                }
 320        }
 321
 322        if (signr <= 0) {
 323                /* no signal to deliver so we just put the saved sigmask
 324                 * back */
 325                restore_saved_sigmask();
 326        } else {                /* signr > 0 */
 327                /* Whee!  Actually deliver the signal.  */
 328                handle_signal(signr, &info, &ka, regs);
 329        }
 330
 331        return;
 332}
 333
 334asmlinkage void do_notify_resume(struct pt_regs *regs)
 335{
 336        if (current_thread_info()->flags & _TIF_SIGPENDING)
 337                do_signal(regs);
 338
 339        if (current_thread_info()->flags & _TIF_NOTIFY_RESUME) {
 340                clear_thread_flag(TIF_NOTIFY_RESUME);
 341                tracehook_notify_resume(regs);
 342        }
 343}
 344