linux/arch/sparc/kernel/signal_32.c
<<
>>
Prefs
   1/*  linux/arch/sparc/kernel/signal.c
   2 *
   3 *  Copyright (C) 1991, 1992  Linus Torvalds
   4 *  Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
   5 *  Copyright (C) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx)
   6 *  Copyright (C) 1997 Eddie C. Dost   (ecd@skynet.be)
   7 */
   8
   9#include <linux/sched.h>
  10#include <linux/kernel.h>
  11#include <linux/signal.h>
  12#include <linux/errno.h>
  13#include <linux/wait.h>
  14#include <linux/ptrace.h>
  15#include <linux/unistd.h>
  16#include <linux/mm.h>
  17#include <linux/tty.h>
  18#include <linux/smp.h>
  19#include <linux/binfmts.h>      /* do_coredum */
  20#include <linux/bitops.h>
  21#include <linux/tracehook.h>
  22
  23#include <asm/uaccess.h>
  24#include <asm/ptrace.h>
  25#include <asm/pgalloc.h>
  26#include <asm/pgtable.h>
  27#include <asm/cacheflush.h>     /* flush_sig_insns */
  28#include <asm/switch_to.h>
  29
  30#include "sigutil.h"
  31
  32extern void fpsave(unsigned long *fpregs, unsigned long *fsr,
  33                   void *fpqueue, unsigned long *fpqdepth);
  34extern void fpload(unsigned long *fpregs, unsigned long *fsr);
  35
  36struct signal_frame {
  37        struct sparc_stackf     ss;
  38        __siginfo32_t           info;
  39        __siginfo_fpu_t __user  *fpu_save;
  40        unsigned long           insns[2] __attribute__ ((aligned (8)));
  41        unsigned int            extramask[_NSIG_WORDS - 1];
  42        unsigned int            extra_size; /* Should be 0 */
  43        __siginfo_rwin_t __user *rwin_save;
  44} __attribute__((aligned(8)));
  45
  46struct rt_signal_frame {
  47        struct sparc_stackf     ss;
  48        siginfo_t               info;
  49        struct pt_regs          regs;
  50        sigset_t                mask;
  51        __siginfo_fpu_t __user  *fpu_save;
  52        unsigned int            insns[2];
  53        stack_t                 stack;
  54        unsigned int            extra_size; /* Should be 0 */
  55        __siginfo_rwin_t __user *rwin_save;
  56} __attribute__((aligned(8)));
  57
  58/* Align macros */
  59#define SF_ALIGNEDSZ  (((sizeof(struct signal_frame) + 7) & (~7)))
  60#define RT_ALIGNEDSZ  (((sizeof(struct rt_signal_frame) + 7) & (~7)))
  61
  62static int _sigpause_common(old_sigset_t set)
  63{
  64        sigset_t blocked;
  65        siginitset(&blocked, set);
  66        return sigsuspend(&blocked);
  67}
  68
  69asmlinkage int sys_sigsuspend(old_sigset_t set)
  70{
  71        return _sigpause_common(set);
  72}
  73
  74asmlinkage void do_sigreturn(struct pt_regs *regs)
  75{
  76        struct signal_frame __user *sf;
  77        unsigned long up_psr, pc, npc;
  78        sigset_t set;
  79        __siginfo_fpu_t __user *fpu_save;
  80        __siginfo_rwin_t __user *rwin_save;
  81        int err;
  82
  83        /* Always make any pending restarted system calls return -EINTR */
  84        current_thread_info()->restart_block.fn = do_no_restart_syscall;
  85
  86        synchronize_user_stack();
  87
  88        sf = (struct signal_frame __user *) regs->u_regs[UREG_FP];
  89
  90        /* 1. Make sure we are not getting garbage from the user */
  91        if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
  92                goto segv_and_exit;
  93
  94        if (((unsigned long) sf) & 3)
  95                goto segv_and_exit;
  96
  97        err = __get_user(pc,  &sf->info.si_regs.pc);
  98        err |= __get_user(npc, &sf->info.si_regs.npc);
  99
 100        if ((pc | npc) & 3)
 101                goto segv_and_exit;
 102
 103        /* 2. Restore the state */
 104        up_psr = regs->psr;
 105        err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs));
 106
 107        /* User can only change condition codes and FPU enabling in %psr. */
 108        regs->psr = (up_psr & ~(PSR_ICC | PSR_EF))
 109                  | (regs->psr & (PSR_ICC | PSR_EF));
 110
 111        /* Prevent syscall restart.  */
 112        pt_regs_clear_syscall(regs);
 113
 114        err |= __get_user(fpu_save, &sf->fpu_save);
 115        if (fpu_save)
 116                err |= restore_fpu_state(regs, fpu_save);
 117        err |= __get_user(rwin_save, &sf->rwin_save);
 118        if (rwin_save)
 119                err |= restore_rwin_state(rwin_save);
 120
 121        /* This is pretty much atomic, no amount locking would prevent
 122         * the races which exist anyways.
 123         */
 124        err |= __get_user(set.sig[0], &sf->info.si_mask);
 125        err |= __copy_from_user(&set.sig[1], &sf->extramask,
 126                                (_NSIG_WORDS-1) * sizeof(unsigned int));
 127                           
 128        if (err)
 129                goto segv_and_exit;
 130
 131        set_current_blocked(&set);
 132        return;
 133
 134segv_and_exit:
 135        force_sig(SIGSEGV, current);
 136}
 137
 138asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
 139{
 140        struct rt_signal_frame __user *sf;
 141        unsigned int psr, pc, npc;
 142        __siginfo_fpu_t __user *fpu_save;
 143        __siginfo_rwin_t __user *rwin_save;
 144        mm_segment_t old_fs;
 145        sigset_t set;
 146        stack_t st;
 147        int err;
 148
 149        synchronize_user_stack();
 150        sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
 151        if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
 152            (((unsigned long) sf) & 0x03))
 153                goto segv;
 154
 155        err = __get_user(pc, &sf->regs.pc);
 156        err |= __get_user(npc, &sf->regs.npc);
 157        err |= ((pc | npc) & 0x03);
 158
 159        err |= __get_user(regs->y, &sf->regs.y);
 160        err |= __get_user(psr, &sf->regs.psr);
 161
 162        err |= __copy_from_user(&regs->u_regs[UREG_G1],
 163                                &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
 164
 165        regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
 166
 167        /* Prevent syscall restart.  */
 168        pt_regs_clear_syscall(regs);
 169
 170        err |= __get_user(fpu_save, &sf->fpu_save);
 171        if (!err && fpu_save)
 172                err |= restore_fpu_state(regs, fpu_save);
 173        err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
 174        
 175        err |= __copy_from_user(&st, &sf->stack, sizeof(stack_t));
 176        
 177        if (err)
 178                goto segv;
 179                
 180        regs->pc = pc;
 181        regs->npc = npc;
 182        
 183        /* It is more difficult to avoid calling this function than to
 184         * call it and ignore errors.
 185         */
 186        old_fs = get_fs();
 187        set_fs(KERNEL_DS);
 188        do_sigaltstack((const stack_t __user *) &st, NULL, (unsigned long)sf);
 189        set_fs(old_fs);
 190
 191        err |= __get_user(rwin_save, &sf->rwin_save);
 192        if (!err && rwin_save) {
 193                if (restore_rwin_state(rwin_save))
 194                        goto segv;
 195        }
 196
 197        set_current_blocked(&set);
 198        return;
 199segv:
 200        force_sig(SIGSEGV, current);
 201}
 202
 203/* Checks if the fp is valid */
 204static inline int invalid_frame_pointer(void __user *fp, int fplen)
 205{
 206        if ((((unsigned long) fp) & 7) || !__access_ok((unsigned long)fp, fplen))
 207                return 1;
 208
 209        return 0;
 210}
 211
 212static inline void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
 213{
 214        unsigned long sp = regs->u_regs[UREG_FP];
 215
 216        /*
 217         * If we are on the alternate signal stack and would overflow it, don't.
 218         * Return an always-bogus address instead so we will die with SIGSEGV.
 219         */
 220        if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
 221                return (void __user *) -1L;
 222
 223        /* This is the X/Open sanctioned signal stack switching.  */
 224        if (sa->sa_flags & SA_ONSTACK) {
 225                if (sas_ss_flags(sp) == 0)
 226                        sp = current->sas_ss_sp + current->sas_ss_size;
 227        }
 228
 229        sp -= framesize;
 230
 231        /* Always align the stack frame.  This handles two cases.  First,
 232         * sigaltstack need not be mindful of platform specific stack
 233         * alignment.  Second, if we took this signal because the stack
 234         * is not aligned properly, we'd like to take the signal cleanly
 235         * and report that.
 236         */
 237        sp &= ~15UL;
 238
 239        return (void __user *) sp;
 240}
 241
 242static int setup_frame(struct k_sigaction *ka, struct pt_regs *regs,
 243                       int signo, sigset_t *oldset)
 244{
 245        struct signal_frame __user *sf;
 246        int sigframe_size, err, wsaved;
 247        void __user *tail;
 248
 249        /* 1. Make sure everything is clean */
 250        synchronize_user_stack();
 251
 252        wsaved = current_thread_info()->w_saved;
 253
 254        sigframe_size = sizeof(*sf);
 255        if (used_math())
 256                sigframe_size += sizeof(__siginfo_fpu_t);
 257        if (wsaved)
 258                sigframe_size += sizeof(__siginfo_rwin_t);
 259
 260        sf = (struct signal_frame __user *)
 261                get_sigframe(&ka->sa, regs, sigframe_size);
 262
 263        if (invalid_frame_pointer(sf, sigframe_size))
 264                goto sigill_and_return;
 265
 266        tail = sf + 1;
 267
 268        /* 2. Save the current process state */
 269        err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
 270        
 271        err |= __put_user(0, &sf->extra_size);
 272
 273        if (used_math()) {
 274                __siginfo_fpu_t __user *fp = tail;
 275                tail += sizeof(*fp);
 276                err |= save_fpu_state(regs, fp);
 277                err |= __put_user(fp, &sf->fpu_save);
 278        } else {
 279                err |= __put_user(0, &sf->fpu_save);
 280        }
 281        if (wsaved) {
 282                __siginfo_rwin_t __user *rwp = tail;
 283                tail += sizeof(*rwp);
 284                err |= save_rwin_state(wsaved, rwp);
 285                err |= __put_user(rwp, &sf->rwin_save);
 286        } else {
 287                err |= __put_user(0, &sf->rwin_save);
 288        }
 289
 290        err |= __put_user(oldset->sig[0], &sf->info.si_mask);
 291        err |= __copy_to_user(sf->extramask, &oldset->sig[1],
 292                              (_NSIG_WORDS - 1) * sizeof(unsigned int));
 293        if (!wsaved) {
 294                err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
 295                                      sizeof(struct reg_window32));
 296        } else {
 297                struct reg_window32 *rp;
 298
 299                rp = &current_thread_info()->reg_window[wsaved - 1];
 300                err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
 301        }
 302        if (err)
 303                goto sigsegv;
 304        
 305        /* 3. signal handler back-trampoline and parameters */
 306        regs->u_regs[UREG_FP] = (unsigned long) sf;
 307        regs->u_regs[UREG_I0] = signo;
 308        regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
 309        regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
 310
 311        /* 4. signal handler */
 312        regs->pc = (unsigned long) ka->sa.sa_handler;
 313        regs->npc = (regs->pc + 4);
 314
 315        /* 5. return to kernel instructions */
 316        if (ka->ka_restorer)
 317                regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
 318        else {
 319                regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
 320
 321                /* mov __NR_sigreturn, %g1 */
 322                err |= __put_user(0x821020d8, &sf->insns[0]);
 323
 324                /* t 0x10 */
 325                err |= __put_user(0x91d02010, &sf->insns[1]);
 326                if (err)
 327                        goto sigsegv;
 328
 329                /* Flush instruction space. */
 330                flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
 331        }
 332        return 0;
 333
 334sigill_and_return:
 335        do_exit(SIGILL);
 336        return -EINVAL;
 337
 338sigsegv:
 339        force_sigsegv(signo, current);
 340        return -EFAULT;
 341}
 342
 343static int setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
 344                          int signo, sigset_t *oldset, siginfo_t *info)
 345{
 346        struct rt_signal_frame __user *sf;
 347        int sigframe_size, wsaved;
 348        void __user *tail;
 349        unsigned int psr;
 350        int err;
 351
 352        synchronize_user_stack();
 353        wsaved = current_thread_info()->w_saved;
 354        sigframe_size = sizeof(*sf);
 355        if (used_math())
 356                sigframe_size += sizeof(__siginfo_fpu_t);
 357        if (wsaved)
 358                sigframe_size += sizeof(__siginfo_rwin_t);
 359        sf = (struct rt_signal_frame __user *)
 360                get_sigframe(&ka->sa, regs, sigframe_size);
 361        if (invalid_frame_pointer(sf, sigframe_size))
 362                goto sigill;
 363
 364        tail = sf + 1;
 365        err  = __put_user(regs->pc, &sf->regs.pc);
 366        err |= __put_user(regs->npc, &sf->regs.npc);
 367        err |= __put_user(regs->y, &sf->regs.y);
 368        psr = regs->psr;
 369        if (used_math())
 370                psr |= PSR_EF;
 371        err |= __put_user(psr, &sf->regs.psr);
 372        err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
 373        err |= __put_user(0, &sf->extra_size);
 374
 375        if (psr & PSR_EF) {
 376                __siginfo_fpu_t *fp = tail;
 377                tail += sizeof(*fp);
 378                err |= save_fpu_state(regs, fp);
 379                err |= __put_user(fp, &sf->fpu_save);
 380        } else {
 381                err |= __put_user(0, &sf->fpu_save);
 382        }
 383        if (wsaved) {
 384                __siginfo_rwin_t *rwp = tail;
 385                tail += sizeof(*rwp);
 386                err |= save_rwin_state(wsaved, rwp);
 387                err |= __put_user(rwp, &sf->rwin_save);
 388        } else {
 389                err |= __put_user(0, &sf->rwin_save);
 390        }
 391        err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
 392        
 393        /* Setup sigaltstack */
 394        err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
 395        err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
 396        err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
 397        
 398        if (!wsaved) {
 399                err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
 400                                      sizeof(struct reg_window32));
 401        } else {
 402                struct reg_window32 *rp;
 403
 404                rp = &current_thread_info()->reg_window[wsaved - 1];
 405                err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
 406        }
 407
 408        err |= copy_siginfo_to_user(&sf->info, info);
 409
 410        if (err)
 411                goto sigsegv;
 412
 413        regs->u_regs[UREG_FP] = (unsigned long) sf;
 414        regs->u_regs[UREG_I0] = signo;
 415        regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
 416        regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
 417
 418        regs->pc = (unsigned long) ka->sa.sa_handler;
 419        regs->npc = (regs->pc + 4);
 420
 421        if (ka->ka_restorer)
 422                regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
 423        else {
 424                regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
 425
 426                /* mov __NR_sigreturn, %g1 */
 427                err |= __put_user(0x821020d8, &sf->insns[0]);
 428
 429                /* t 0x10 */
 430                err |= __put_user(0x91d02010, &sf->insns[1]);
 431                if (err)
 432                        goto sigsegv;
 433
 434                /* Flush instruction space. */
 435                flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
 436        }
 437        return 0;
 438
 439sigill:
 440        do_exit(SIGILL);
 441        return -EINVAL;
 442
 443sigsegv:
 444        force_sigsegv(signo, current);
 445        return -EFAULT;
 446}
 447
 448static inline void
 449handle_signal(unsigned long signr, struct k_sigaction *ka,
 450              siginfo_t *info, struct pt_regs *regs)
 451{
 452        sigset_t *oldset = sigmask_to_save();
 453        int err;
 454
 455        if (ka->sa.sa_flags & SA_SIGINFO)
 456                err = setup_rt_frame(ka, regs, signr, oldset, info);
 457        else
 458                err = setup_frame(ka, regs, signr, oldset);
 459
 460        if (err)
 461                return;
 462
 463        signal_delivered(signr, info, ka, regs, 0);
 464}
 465
 466static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
 467                                   struct sigaction *sa)
 468{
 469        switch(regs->u_regs[UREG_I0]) {
 470        case ERESTART_RESTARTBLOCK:
 471        case ERESTARTNOHAND:
 472        no_system_call_restart:
 473                regs->u_regs[UREG_I0] = EINTR;
 474                regs->psr |= PSR_C;
 475                break;
 476        case ERESTARTSYS:
 477                if (!(sa->sa_flags & SA_RESTART))
 478                        goto no_system_call_restart;
 479                /* fallthrough */
 480        case ERESTARTNOINTR:
 481                regs->u_regs[UREG_I0] = orig_i0;
 482                regs->pc -= 4;
 483                regs->npc -= 4;
 484        }
 485}
 486
 487/* Note that 'init' is a special process: it doesn't get signals it doesn't
 488 * want to handle. Thus you cannot kill init even with a SIGKILL even by
 489 * mistake.
 490 */
 491static void do_signal(struct pt_regs *regs, unsigned long orig_i0)
 492{
 493        struct k_sigaction ka;
 494        int restart_syscall;
 495        siginfo_t info;
 496        int signr;
 497
 498        /* It's a lot of work and synchronization to add a new ptrace
 499         * register for GDB to save and restore in order to get
 500         * orig_i0 correct for syscall restarts when debugging.
 501         *
 502         * Although it should be the case that most of the global
 503         * registers are volatile across a system call, glibc already
 504         * depends upon that fact that we preserve them.  So we can't
 505         * just use any global register to save away the orig_i0 value.
 506         *
 507         * In particular %g2, %g3, %g4, and %g5 are all assumed to be
 508         * preserved across a system call trap by various pieces of
 509         * code in glibc.
 510         *
 511         * %g7 is used as the "thread register".   %g6 is not used in
 512         * any fixed manner.  %g6 is used as a scratch register and
 513         * a compiler temporary, but it's value is never used across
 514         * a system call.  Therefore %g6 is usable for orig_i0 storage.
 515         */
 516        if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C))
 517                regs->u_regs[UREG_G6] = orig_i0;
 518
 519        signr = get_signal_to_deliver(&info, &ka, regs, NULL);
 520
 521        /* If the debugger messes with the program counter, it clears
 522         * the software "in syscall" bit, directing us to not perform
 523         * a syscall restart.
 524         */
 525        restart_syscall = 0;
 526        if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C)) {
 527                restart_syscall = 1;
 528                orig_i0 = regs->u_regs[UREG_G6];
 529        }
 530
 531
 532        if (signr > 0) {
 533                if (restart_syscall)
 534                        syscall_restart(orig_i0, regs, &ka.sa);
 535                handle_signal(signr, &ka, &info, regs);
 536                return;
 537        }
 538        if (restart_syscall &&
 539            (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
 540             regs->u_regs[UREG_I0] == ERESTARTSYS ||
 541             regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
 542                /* replay the system call when we are done */
 543                regs->u_regs[UREG_I0] = orig_i0;
 544                regs->pc -= 4;
 545                regs->npc -= 4;
 546                pt_regs_clear_syscall(regs);
 547        }
 548        if (restart_syscall &&
 549            regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
 550                regs->u_regs[UREG_G1] = __NR_restart_syscall;
 551                regs->pc -= 4;
 552                regs->npc -= 4;
 553                pt_regs_clear_syscall(regs);
 554        }
 555
 556        /* if there's no signal to deliver, we just put the saved sigmask
 557         * back
 558         */
 559        restore_saved_sigmask();
 560}
 561
 562void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0,
 563                      unsigned long thread_info_flags)
 564{
 565        if (thread_info_flags & _TIF_SIGPENDING)
 566                do_signal(regs, orig_i0);
 567        if (thread_info_flags & _TIF_NOTIFY_RESUME) {
 568                clear_thread_flag(TIF_NOTIFY_RESUME);
 569                tracehook_notify_resume(regs);
 570        }
 571}
 572
 573asmlinkage int
 574do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr,
 575                unsigned long sp)
 576{
 577        int ret = -EFAULT;
 578
 579        /* First see if old state is wanted. */
 580        if (ossptr) {
 581                if (put_user(current->sas_ss_sp + current->sas_ss_size,
 582                             &ossptr->the_stack) ||
 583                    __put_user(on_sig_stack(sp), &ossptr->cur_status))
 584                        goto out;
 585        }
 586
 587        /* Now see if we want to update the new state. */
 588        if (ssptr) {
 589                char *ss_sp;
 590
 591                if (get_user(ss_sp, &ssptr->the_stack))
 592                        goto out;
 593                /* If the current stack was set with sigaltstack, don't
 594                   swap stacks while we are on it.  */
 595                ret = -EPERM;
 596                if (current->sas_ss_sp && on_sig_stack(sp))
 597                        goto out;
 598
 599                /* Since we don't know the extent of the stack, and we don't
 600                   track onstack-ness, but rather calculate it, we must
 601                   presume a size.  Ho hum this interface is lossy.  */
 602                current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
 603                current->sas_ss_size = SIGSTKSZ;
 604        }
 605        ret = 0;
 606out:
 607        return ret;
 608}
 609