linux/arch/frv/kernel/signal.c
<<
>>
Prefs
   1/* signal.c: FRV specific bits of signal handling
   2 *
   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
   6 *
   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.
  11 */
  12
  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/tracehook.h>
  24#include <asm/ucontext.h>
  25#include <asm/uaccess.h>
  26#include <asm/cacheflush.h>
  27
  28#define DEBUG_SIG 0
  29
  30struct fdpic_func_descriptor {
  31        unsigned long   text;
  32        unsigned long   GOT;
  33};
  34
  35/*
  36 * Do a signal return; undo the signal stack.
  37 */
  38
  39struct sigframe
  40{
  41        __sigrestore_t pretcode;
  42        int sig;
  43        struct sigcontext sc;
  44        unsigned long extramask[_NSIG_WORDS-1];
  45        uint32_t retcode[2];
  46};
  47
  48struct rt_sigframe
  49{
  50        __sigrestore_t pretcode;
  51        int sig;
  52        struct siginfo __user *pinfo;
  53        void __user *puc;
  54        struct siginfo info;
  55        struct ucontext uc;
  56        uint32_t retcode[2];
  57};
  58
  59static int restore_sigcontext(struct sigcontext __user *sc, int *_gr8)
  60{
  61        struct user_context *user = current->thread.user;
  62        unsigned long tbr, psr;
  63
  64        /* Always make any pending restarted system calls return -EINTR */
  65        current_thread_info()->restart_block.fn = do_no_restart_syscall;
  66
  67        tbr = user->i.tbr;
  68        psr = user->i.psr;
  69        if (copy_from_user(user, &sc->sc_context, sizeof(sc->sc_context)))
  70                goto badframe;
  71        user->i.tbr = tbr;
  72        user->i.psr = psr;
  73
  74        restore_user_regs(user);
  75
  76        user->i.syscallno = -1;         /* disable syscall checks */
  77
  78        *_gr8 = user->i.gr[8];
  79        return 0;
  80
  81 badframe:
  82        return 1;
  83}
  84
  85asmlinkage int sys_sigreturn(void)
  86{
  87        struct sigframe __user *frame = (struct sigframe __user *) __frame->sp;
  88        sigset_t set;
  89        int gr8;
  90
  91        if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  92                goto badframe;
  93        if (__get_user(set.sig[0], &frame->sc.sc_oldmask))
  94                goto badframe;
  95
  96        if (_NSIG_WORDS > 1 &&
  97            __copy_from_user(&set.sig[1], &frame->extramask, sizeof(frame->extramask)))
  98                goto badframe;
  99
 100        set_current_blocked(&set);
 101
 102        if (restore_sigcontext(&frame->sc, &gr8))
 103                goto badframe;
 104        return gr8;
 105
 106 badframe:
 107        force_sig(SIGSEGV, current);
 108        return 0;
 109}
 110
 111asmlinkage int sys_rt_sigreturn(void)
 112{
 113        struct rt_sigframe __user *frame = (struct rt_sigframe __user *) __frame->sp;
 114        sigset_t set;
 115        int gr8;
 116
 117        if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
 118                goto badframe;
 119        if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
 120                goto badframe;
 121
 122        set_current_blocked(&set);
 123
 124        if (restore_sigcontext(&frame->uc.uc_mcontext, &gr8))
 125                goto badframe;
 126
 127        if (restore_altstack(&frame->uc.uc_stack))
 128                goto badframe;
 129
 130        return gr8;
 131
 132badframe:
 133        force_sig(SIGSEGV, current);
 134        return 0;
 135}
 136
 137/*
 138 * Set up a signal frame
 139 */
 140static int setup_sigcontext(struct sigcontext __user *sc, unsigned long mask)
 141{
 142        save_user_regs(current->thread.user);
 143
 144        if (copy_to_user(&sc->sc_context, current->thread.user, sizeof(sc->sc_context)) != 0)
 145                goto badframe;
 146
 147        /* non-iBCS2 extensions.. */
 148        if (__put_user(mask, &sc->sc_oldmask) < 0)
 149                goto badframe;
 150
 151        return 0;
 152
 153 badframe:
 154        return 1;
 155}
 156
 157/*****************************************************************************/
 158/*
 159 * Determine which stack to use..
 160 */
 161static inline void __user *get_sigframe(struct ksignal *ksig,
 162                                        size_t frame_size)
 163{
 164        unsigned long sp = sigsp(__frame->sp, ksig);
 165
 166        return (void __user *) ((sp - frame_size) & ~7UL);
 167
 168} /* end get_sigframe() */
 169
 170/*****************************************************************************/
 171/*
 172 *
 173 */
 174static int setup_frame(struct ksignal *ksig, sigset_t *set)
 175{
 176        struct sigframe __user *frame;
 177        int rsig, sig = ksig->sig;
 178
 179        set_fs(USER_DS);
 180
 181        frame = get_sigframe(ksig, sizeof(*frame));
 182
 183        if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
 184                return -EFAULT;
 185
 186        rsig = sig;
 187        if (sig < 32 &&
 188            __current_thread_info->exec_domain &&
 189            __current_thread_info->exec_domain->signal_invmap)
 190                rsig = __current_thread_info->exec_domain->signal_invmap[sig];
 191
 192        if (__put_user(rsig, &frame->sig) < 0)
 193                return -EFAULT;
 194
 195        if (setup_sigcontext(&frame->sc, set->sig[0]))
 196                return -EFAULT;
 197
 198        if (_NSIG_WORDS > 1) {
 199                if (__copy_to_user(frame->extramask, &set->sig[1],
 200                                   sizeof(frame->extramask)))
 201                        return -EFAULT;
 202        }
 203
 204        /* Set up to return from userspace.  If provided, use a stub
 205         * already in userspace.  */
 206        if (ksig->ka.sa.sa_flags & SA_RESTORER) {
 207                if (__put_user(ksig->ka.sa.sa_restorer, &frame->pretcode) < 0)
 208                        return -EFAULT;
 209        }
 210        else {
 211                /* Set up the following code on the stack:
 212                 *      setlos  #__NR_sigreturn,gr7
 213                 *      tira    gr0,0
 214                 */
 215                if (__put_user((__sigrestore_t)frame->retcode, &frame->pretcode) ||
 216                    __put_user(0x8efc0000|__NR_sigreturn, &frame->retcode[0]) ||
 217                    __put_user(0xc0700000, &frame->retcode[1]))
 218                        return -EFAULT;
 219
 220                flush_icache_range((unsigned long) frame->retcode,
 221                                   (unsigned long) (frame->retcode + 2));
 222        }
 223
 224        /* Set up registers for the signal handler */
 225        if (current->personality & FDPIC_FUNCPTRS) {
 226                struct fdpic_func_descriptor __user *funcptr =
 227                        (struct fdpic_func_descriptor __user *) ksig->ka.sa.sa_handler;
 228                struct fdpic_func_descriptor desc;
 229                if (copy_from_user(&desc, funcptr, sizeof(desc)))
 230                        return -EFAULT;
 231                __frame->pc = desc.text;
 232                __frame->gr15 = desc.GOT;
 233        } else {
 234                __frame->pc   = (unsigned long) ksig->ka.sa.sa_handler;
 235                __frame->gr15 = 0;
 236        }
 237
 238        __frame->sp   = (unsigned long) frame;
 239        __frame->lr   = (unsigned long) &frame->retcode;
 240        __frame->gr8  = sig;
 241
 242#if DEBUG_SIG
 243        printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
 244               sig, current->comm, current->pid, frame, __frame->pc,
 245               frame->pretcode);
 246#endif
 247
 248        return 0;
 249} /* end setup_frame() */
 250
 251/*****************************************************************************/
 252/*
 253 *
 254 */
 255static int setup_rt_frame(struct ksignal *ksig, sigset_t *set)
 256{
 257        struct rt_sigframe __user *frame;
 258        int rsig, sig = ksig->sig;
 259
 260        set_fs(USER_DS);
 261
 262        frame = get_sigframe(ksig, sizeof(*frame));
 263
 264        if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
 265                return -EFAULT;
 266
 267        rsig = sig;
 268        if (sig < 32 &&
 269            __current_thread_info->exec_domain &&
 270            __current_thread_info->exec_domain->signal_invmap)
 271                rsig = __current_thread_info->exec_domain->signal_invmap[sig];
 272
 273        if (__put_user(rsig,            &frame->sig) ||
 274            __put_user(&frame->info,    &frame->pinfo) ||
 275            __put_user(&frame->uc,      &frame->puc))
 276                return -EFAULT;
 277
 278        if (copy_siginfo_to_user(&frame->info, &ksig->info))
 279                return -EFAULT;
 280
 281        /* Create the ucontext.  */
 282        if (__put_user(0, &frame->uc.uc_flags) ||
 283            __put_user(NULL, &frame->uc.uc_link) ||
 284            __save_altstack(&frame->uc.uc_stack, __frame->sp))
 285                return -EFAULT;
 286
 287        if (setup_sigcontext(&frame->uc.uc_mcontext, set->sig[0]))
 288                return -EFAULT;
 289
 290        if (__copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
 291                return -EFAULT;
 292
 293        /* Set up to return from userspace.  If provided, use a stub
 294         * already in userspace.  */
 295        if (ksig->ka.sa.sa_flags & SA_RESTORER) {
 296                if (__put_user(ksig->ka.sa.sa_restorer, &frame->pretcode))
 297                        return -EFAULT;
 298        }
 299        else {
 300                /* Set up the following code on the stack:
 301                 *      setlos  #__NR_sigreturn,gr7
 302                 *      tira    gr0,0
 303                 */
 304                if (__put_user((__sigrestore_t)frame->retcode, &frame->pretcode) ||
 305                    __put_user(0x8efc0000|__NR_rt_sigreturn, &frame->retcode[0]) ||
 306                    __put_user(0xc0700000, &frame->retcode[1]))
 307                        return -EFAULT;
 308
 309                flush_icache_range((unsigned long) frame->retcode,
 310                                   (unsigned long) (frame->retcode + 2));
 311        }
 312
 313        /* Set up registers for signal handler */
 314        if (current->personality & FDPIC_FUNCPTRS) {
 315                struct fdpic_func_descriptor __user *funcptr =
 316                        (struct fdpic_func_descriptor __user *) ksig->ka.sa.sa_handler;
 317                struct fdpic_func_descriptor desc;
 318                if (copy_from_user(&desc, funcptr, sizeof(desc)))
 319                        return -EFAULT;
 320                __frame->pc = desc.text;
 321                __frame->gr15 = desc.GOT;
 322        } else {
 323                __frame->pc   = (unsigned long) ksig->ka.sa.sa_handler;
 324                __frame->gr15 = 0;
 325        }
 326
 327        __frame->sp  = (unsigned long) frame;
 328        __frame->lr  = (unsigned long) &frame->retcode;
 329        __frame->gr8 = sig;
 330        __frame->gr9 = (unsigned long) &frame->info;
 331
 332#if DEBUG_SIG
 333        printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
 334               sig, current->comm, current->pid, frame, __frame->pc,
 335               frame->pretcode);
 336#endif
 337        return 0;
 338
 339} /* end setup_rt_frame() */
 340
 341/*****************************************************************************/
 342/*
 343 * OK, we're invoking a handler
 344 */
 345static void handle_signal(struct ksignal *ksig)
 346{
 347        sigset_t *oldset = sigmask_to_save();
 348        int ret;
 349
 350        /* Are we from a system call? */
 351        if (__frame->syscallno != -1) {
 352                /* If so, check system call restarting.. */
 353                switch (__frame->gr8) {
 354                case -ERESTART_RESTARTBLOCK:
 355                case -ERESTARTNOHAND:
 356                        __frame->gr8 = -EINTR;
 357                        break;
 358
 359                case -ERESTARTSYS:
 360                        if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
 361                                __frame->gr8 = -EINTR;
 362                                break;
 363                        }
 364
 365                        /* fallthrough */
 366                case -ERESTARTNOINTR:
 367                        __frame->gr8 = __frame->orig_gr8;
 368                        __frame->pc -= 4;
 369                }
 370                __frame->syscallno = -1;
 371        }
 372
 373        /* Set up the stack frame */
 374        if (ksig->ka.sa.sa_flags & SA_SIGINFO)
 375                ret = setup_rt_frame(ksig, oldset);
 376        else
 377                ret = setup_frame(ksig, oldset);
 378
 379        signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
 380} /* end handle_signal() */
 381
 382/*****************************************************************************/
 383/*
 384 * Note that 'init' is a special process: it doesn't get signals it doesn't
 385 * want to handle. Thus you cannot kill init even with a SIGKILL even by
 386 * mistake.
 387 */
 388static void do_signal(void)
 389{
 390        struct ksignal ksig;
 391
 392        if (get_signal(&ksig)) {
 393                handle_signal(&ksig);
 394                return;
 395        }
 396
 397        /* Did we come from a system call? */
 398        if (__frame->syscallno != -1) {
 399                /* Restart the system call - no handlers present */
 400                switch (__frame->gr8) {
 401                case -ERESTARTNOHAND:
 402                case -ERESTARTSYS:
 403                case -ERESTARTNOINTR:
 404                        __frame->gr8 = __frame->orig_gr8;
 405                        __frame->pc -= 4;
 406                        break;
 407
 408                case -ERESTART_RESTARTBLOCK:
 409                        __frame->gr7 = __NR_restart_syscall;
 410                        __frame->pc -= 4;
 411                        break;
 412                }
 413                __frame->syscallno = -1;
 414        }
 415
 416        /* if there's no signal to deliver, we just put the saved sigmask
 417         * back */
 418        restore_saved_sigmask();
 419} /* end do_signal() */
 420
 421/*****************************************************************************/
 422/*
 423 * notification of userspace execution resumption
 424 * - triggered by the TIF_WORK_MASK flags
 425 */
 426asmlinkage void do_notify_resume(__u32 thread_info_flags)
 427{
 428        /* pending single-step? */
 429        if (thread_info_flags & _TIF_SINGLESTEP)
 430                clear_thread_flag(TIF_SINGLESTEP);
 431
 432        /* deal with pending signal delivery */
 433        if (thread_info_flags & _TIF_SIGPENDING)
 434                do_signal();
 435
 436        /* deal with notification on about to resume userspace execution */
 437        if (thread_info_flags & _TIF_NOTIFY_RESUME) {
 438                clear_thread_flag(TIF_NOTIFY_RESUME);
 439                tracehook_notify_resume(__frame);
 440        }
 441
 442} /* end do_notify_resume() */
 443