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
  62asmlinkage void do_sigreturn(struct pt_regs *regs)
  63{
  64        struct signal_frame __user *sf;
  65        unsigned long up_psr, pc, npc;
  66        sigset_t set;
  67        __siginfo_fpu_t __user *fpu_save;
  68        __siginfo_rwin_t __user *rwin_save;
  69        int err;
  70
  71        /* Always make any pending restarted system calls return -EINTR */
  72        current_thread_info()->restart_block.fn = do_no_restart_syscall;
  73
  74        synchronize_user_stack();
  75
  76        sf = (struct signal_frame __user *) regs->u_regs[UREG_FP];
  77
  78        /* 1. Make sure we are not getting garbage from the user */
  79        if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
  80                goto segv_and_exit;
  81
  82        if (((unsigned long) sf) & 3)
  83                goto segv_and_exit;
  84
  85        err = __get_user(pc,  &sf->info.si_regs.pc);
  86        err |= __get_user(npc, &sf->info.si_regs.npc);
  87
  88        if ((pc | npc) & 3)
  89                goto segv_and_exit;
  90
  91        /* 2. Restore the state */
  92        up_psr = regs->psr;
  93        err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs));
  94
  95        /* User can only change condition codes and FPU enabling in %psr. */
  96        regs->psr = (up_psr & ~(PSR_ICC | PSR_EF))
  97                  | (regs->psr & (PSR_ICC | PSR_EF));
  98
  99        /* Prevent syscall restart.  */
 100        pt_regs_clear_syscall(regs);
 101
 102        err |= __get_user(fpu_save, &sf->fpu_save);
 103        if (fpu_save)
 104                err |= restore_fpu_state(regs, fpu_save);
 105        err |= __get_user(rwin_save, &sf->rwin_save);
 106        if (rwin_save)
 107                err |= restore_rwin_state(rwin_save);
 108
 109        /* This is pretty much atomic, no amount locking would prevent
 110         * the races which exist anyways.
 111         */
 112        err |= __get_user(set.sig[0], &sf->info.si_mask);
 113        err |= __copy_from_user(&set.sig[1], &sf->extramask,
 114                                (_NSIG_WORDS-1) * sizeof(unsigned int));
 115                           
 116        if (err)
 117                goto segv_and_exit;
 118
 119        set_current_blocked(&set);
 120        return;
 121
 122segv_and_exit:
 123        force_sig(SIGSEGV, current);
 124}
 125
 126asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
 127{
 128        struct rt_signal_frame __user *sf;
 129        unsigned int psr, pc, npc;
 130        __siginfo_fpu_t __user *fpu_save;
 131        __siginfo_rwin_t __user *rwin_save;
 132        sigset_t set;
 133        int err;
 134
 135        synchronize_user_stack();
 136        sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
 137        if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
 138            (((unsigned long) sf) & 0x03))
 139                goto segv;
 140
 141        err = __get_user(pc, &sf->regs.pc);
 142        err |= __get_user(npc, &sf->regs.npc);
 143        err |= ((pc | npc) & 0x03);
 144
 145        err |= __get_user(regs->y, &sf->regs.y);
 146        err |= __get_user(psr, &sf->regs.psr);
 147
 148        err |= __copy_from_user(&regs->u_regs[UREG_G1],
 149                                &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
 150
 151        regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
 152
 153        /* Prevent syscall restart.  */
 154        pt_regs_clear_syscall(regs);
 155
 156        err |= __get_user(fpu_save, &sf->fpu_save);
 157        if (!err && fpu_save)
 158                err |= restore_fpu_state(regs, fpu_save);
 159        err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
 160        err |= restore_altstack(&sf->stack);
 161        
 162        if (err)
 163                goto segv;
 164                
 165        regs->pc = pc;
 166        regs->npc = npc;
 167        
 168        err |= __get_user(rwin_save, &sf->rwin_save);
 169        if (!err && rwin_save) {
 170                if (restore_rwin_state(rwin_save))
 171                        goto segv;
 172        }
 173
 174        set_current_blocked(&set);
 175        return;
 176segv:
 177        force_sig(SIGSEGV, current);
 178}
 179
 180/* Checks if the fp is valid */
 181static inline int invalid_frame_pointer(void __user *fp, int fplen)
 182{
 183        if ((((unsigned long) fp) & 7) || !__access_ok((unsigned long)fp, fplen))
 184                return 1;
 185
 186        return 0;
 187}
 188
 189static inline void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs, unsigned long framesize)
 190{
 191        unsigned long sp = regs->u_regs[UREG_FP];
 192
 193        /*
 194         * If we are on the alternate signal stack and would overflow it, don't.
 195         * Return an always-bogus address instead so we will die with SIGSEGV.
 196         */
 197        if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
 198                return (void __user *) -1L;
 199
 200        /* This is the X/Open sanctioned signal stack switching.  */
 201        sp = sigsp(sp, ksig) - framesize;
 202
 203        /* Always align the stack frame.  This handles two cases.  First,
 204         * sigaltstack need not be mindful of platform specific stack
 205         * alignment.  Second, if we took this signal because the stack
 206         * is not aligned properly, we'd like to take the signal cleanly
 207         * and report that.
 208         */
 209        sp &= ~15UL;
 210
 211        return (void __user *) sp;
 212}
 213
 214static int setup_frame(struct ksignal *ksig, struct pt_regs *regs,
 215                       sigset_t *oldset)
 216{
 217        struct signal_frame __user *sf;
 218        int sigframe_size, err, wsaved;
 219        void __user *tail;
 220
 221        /* 1. Make sure everything is clean */
 222        synchronize_user_stack();
 223
 224        wsaved = current_thread_info()->w_saved;
 225
 226        sigframe_size = sizeof(*sf);
 227        if (used_math())
 228                sigframe_size += sizeof(__siginfo_fpu_t);
 229        if (wsaved)
 230                sigframe_size += sizeof(__siginfo_rwin_t);
 231
 232        sf = (struct signal_frame __user *)
 233                get_sigframe(ksig, regs, sigframe_size);
 234
 235        if (invalid_frame_pointer(sf, sigframe_size)) {
 236                do_exit(SIGILL);
 237                return -EINVAL;
 238        }
 239
 240        tail = sf + 1;
 241
 242        /* 2. Save the current process state */
 243        err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
 244        
 245        err |= __put_user(0, &sf->extra_size);
 246
 247        if (used_math()) {
 248                __siginfo_fpu_t __user *fp = tail;
 249                tail += sizeof(*fp);
 250                err |= save_fpu_state(regs, fp);
 251                err |= __put_user(fp, &sf->fpu_save);
 252        } else {
 253                err |= __put_user(0, &sf->fpu_save);
 254        }
 255        if (wsaved) {
 256                __siginfo_rwin_t __user *rwp = tail;
 257                tail += sizeof(*rwp);
 258                err |= save_rwin_state(wsaved, rwp);
 259                err |= __put_user(rwp, &sf->rwin_save);
 260        } else {
 261                err |= __put_user(0, &sf->rwin_save);
 262        }
 263
 264        err |= __put_user(oldset->sig[0], &sf->info.si_mask);
 265        err |= __copy_to_user(sf->extramask, &oldset->sig[1],
 266                              (_NSIG_WORDS - 1) * sizeof(unsigned int));
 267        if (!wsaved) {
 268                err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
 269                                      sizeof(struct reg_window32));
 270        } else {
 271                struct reg_window32 *rp;
 272
 273                rp = &current_thread_info()->reg_window[wsaved - 1];
 274                err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
 275        }
 276        if (err)
 277                return err;
 278        
 279        /* 3. signal handler back-trampoline and parameters */
 280        regs->u_regs[UREG_FP] = (unsigned long) sf;
 281        regs->u_regs[UREG_I0] = ksig->sig;
 282        regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
 283        regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
 284
 285        /* 4. signal handler */
 286        regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
 287        regs->npc = (regs->pc + 4);
 288
 289        /* 5. return to kernel instructions */
 290        if (ksig->ka.ka_restorer)
 291                regs->u_regs[UREG_I7] = (unsigned long)ksig->ka.ka_restorer;
 292        else {
 293                regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
 294
 295                /* mov __NR_sigreturn, %g1 */
 296                err |= __put_user(0x821020d8, &sf->insns[0]);
 297
 298                /* t 0x10 */
 299                err |= __put_user(0x91d02010, &sf->insns[1]);
 300                if (err)
 301                        return err;
 302
 303                /* Flush instruction space. */
 304                flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
 305        }
 306        return 0;
 307}
 308
 309static int setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs,
 310                          sigset_t *oldset)
 311{
 312        struct rt_signal_frame __user *sf;
 313        int sigframe_size, wsaved;
 314        void __user *tail;
 315        unsigned int psr;
 316        int err;
 317
 318        synchronize_user_stack();
 319        wsaved = current_thread_info()->w_saved;
 320        sigframe_size = sizeof(*sf);
 321        if (used_math())
 322                sigframe_size += sizeof(__siginfo_fpu_t);
 323        if (wsaved)
 324                sigframe_size += sizeof(__siginfo_rwin_t);
 325        sf = (struct rt_signal_frame __user *)
 326                get_sigframe(ksig, regs, sigframe_size);
 327        if (invalid_frame_pointer(sf, sigframe_size)) {
 328                do_exit(SIGILL);
 329                return -EINVAL;
 330        }
 331
 332        tail = sf + 1;
 333        err  = __put_user(regs->pc, &sf->regs.pc);
 334        err |= __put_user(regs->npc, &sf->regs.npc);
 335        err |= __put_user(regs->y, &sf->regs.y);
 336        psr = regs->psr;
 337        if (used_math())
 338                psr |= PSR_EF;
 339        err |= __put_user(psr, &sf->regs.psr);
 340        err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
 341        err |= __put_user(0, &sf->extra_size);
 342
 343        if (psr & PSR_EF) {
 344                __siginfo_fpu_t *fp = tail;
 345                tail += sizeof(*fp);
 346                err |= save_fpu_state(regs, fp);
 347                err |= __put_user(fp, &sf->fpu_save);
 348        } else {
 349                err |= __put_user(0, &sf->fpu_save);
 350        }
 351        if (wsaved) {
 352                __siginfo_rwin_t *rwp = tail;
 353                tail += sizeof(*rwp);
 354                err |= save_rwin_state(wsaved, rwp);
 355                err |= __put_user(rwp, &sf->rwin_save);
 356        } else {
 357                err |= __put_user(0, &sf->rwin_save);
 358        }
 359        err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
 360        
 361        /* Setup sigaltstack */
 362        err |= __save_altstack(&sf->stack, regs->u_regs[UREG_FP]);
 363        
 364        if (!wsaved) {
 365                err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
 366                                      sizeof(struct reg_window32));
 367        } else {
 368                struct reg_window32 *rp;
 369
 370                rp = &current_thread_info()->reg_window[wsaved - 1];
 371                err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
 372        }
 373
 374        err |= copy_siginfo_to_user(&sf->info, &ksig->info);
 375
 376        if (err)
 377                return err;
 378
 379        regs->u_regs[UREG_FP] = (unsigned long) sf;
 380        regs->u_regs[UREG_I0] = ksig->sig;
 381        regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
 382        regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
 383
 384        regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
 385        regs->npc = (regs->pc + 4);
 386
 387        if (ksig->ka.ka_restorer)
 388                regs->u_regs[UREG_I7] = (unsigned long)ksig->ka.ka_restorer;
 389        else {
 390                regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
 391
 392                /* mov __NR_sigreturn, %g1 */
 393                err |= __put_user(0x821020d8, &sf->insns[0]);
 394
 395                /* t 0x10 */
 396                err |= __put_user(0x91d02010, &sf->insns[1]);
 397                if (err)
 398                        return err;
 399
 400                /* Flush instruction space. */
 401                flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
 402        }
 403        return 0;
 404}
 405
 406static inline void
 407handle_signal(struct ksignal *ksig, struct pt_regs *regs)
 408{
 409        sigset_t *oldset = sigmask_to_save();
 410        int err;
 411
 412        if (ksig->ka.sa.sa_flags & SA_SIGINFO)
 413                err = setup_rt_frame(ksig, regs, oldset);
 414        else
 415                err = setup_frame(ksig, regs, oldset);
 416        signal_setup_done(err, ksig, 0);
 417}
 418
 419static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
 420                                   struct sigaction *sa)
 421{
 422        switch(regs->u_regs[UREG_I0]) {
 423        case ERESTART_RESTARTBLOCK:
 424        case ERESTARTNOHAND:
 425        no_system_call_restart:
 426                regs->u_regs[UREG_I0] = EINTR;
 427                regs->psr |= PSR_C;
 428                break;
 429        case ERESTARTSYS:
 430                if (!(sa->sa_flags & SA_RESTART))
 431                        goto no_system_call_restart;
 432                /* fallthrough */
 433        case ERESTARTNOINTR:
 434                regs->u_regs[UREG_I0] = orig_i0;
 435                regs->pc -= 4;
 436                regs->npc -= 4;
 437        }
 438}
 439
 440/* Note that 'init' is a special process: it doesn't get signals it doesn't
 441 * want to handle. Thus you cannot kill init even with a SIGKILL even by
 442 * mistake.
 443 */
 444static void do_signal(struct pt_regs *regs, unsigned long orig_i0)
 445{
 446        struct ksignal ksig;
 447        int restart_syscall;
 448        bool has_handler;
 449
 450        /* It's a lot of work and synchronization to add a new ptrace
 451         * register for GDB to save and restore in order to get
 452         * orig_i0 correct for syscall restarts when debugging.
 453         *
 454         * Although it should be the case that most of the global
 455         * registers are volatile across a system call, glibc already
 456         * depends upon that fact that we preserve them.  So we can't
 457         * just use any global register to save away the orig_i0 value.
 458         *
 459         * In particular %g2, %g3, %g4, and %g5 are all assumed to be
 460         * preserved across a system call trap by various pieces of
 461         * code in glibc.
 462         *
 463         * %g7 is used as the "thread register".   %g6 is not used in
 464         * any fixed manner.  %g6 is used as a scratch register and
 465         * a compiler temporary, but it's value is never used across
 466         * a system call.  Therefore %g6 is usable for orig_i0 storage.
 467         */
 468        if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C))
 469                regs->u_regs[UREG_G6] = orig_i0;
 470
 471        has_handler = get_signal(&ksig);
 472
 473        /* If the debugger messes with the program counter, it clears
 474         * the software "in syscall" bit, directing us to not perform
 475         * a syscall restart.
 476         */
 477        restart_syscall = 0;
 478        if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C)) {
 479                restart_syscall = 1;
 480                orig_i0 = regs->u_regs[UREG_G6];
 481        }
 482
 483        if (has_handler) {
 484                if (restart_syscall)
 485                        syscall_restart(orig_i0, regs, &ksig.ka.sa);
 486                handle_signal(&ksig, regs);
 487        } else {
 488                if (restart_syscall) {
 489                        switch (regs->u_regs[UREG_I0]) {
 490                        case ERESTARTNOHAND:
 491                        case ERESTARTSYS:
 492                        case ERESTARTNOINTR:
 493                                /* replay the system call when we are done */
 494                                regs->u_regs[UREG_I0] = orig_i0;
 495                                regs->pc -= 4;
 496                                regs->npc -= 4;
 497                                pt_regs_clear_syscall(regs);
 498                        case ERESTART_RESTARTBLOCK:
 499                                regs->u_regs[UREG_G1] = __NR_restart_syscall;
 500                                regs->pc -= 4;
 501                                regs->npc -= 4;
 502                                pt_regs_clear_syscall(regs);
 503                        }
 504                }
 505                restore_saved_sigmask();
 506        }
 507}
 508
 509void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0,
 510                      unsigned long thread_info_flags)
 511{
 512        if (thread_info_flags & _TIF_SIGPENDING)
 513                do_signal(regs, orig_i0);
 514        if (thread_info_flags & _TIF_NOTIFY_RESUME) {
 515                clear_thread_flag(TIF_NOTIFY_RESUME);
 516                tracehook_notify_resume(regs);
 517        }
 518}
 519
 520asmlinkage int
 521do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr,
 522                unsigned long sp)
 523{
 524        int ret = -EFAULT;
 525
 526        /* First see if old state is wanted. */
 527        if (ossptr) {
 528                if (put_user(current->sas_ss_sp + current->sas_ss_size,
 529                             &ossptr->the_stack) ||
 530                    __put_user(on_sig_stack(sp), &ossptr->cur_status))
 531                        goto out;
 532        }
 533
 534        /* Now see if we want to update the new state. */
 535        if (ssptr) {
 536                char *ss_sp;
 537
 538                if (get_user(ss_sp, &ssptr->the_stack))
 539                        goto out;
 540                /* If the current stack was set with sigaltstack, don't
 541                   swap stacks while we are on it.  */
 542                ret = -EPERM;
 543                if (current->sas_ss_sp && on_sig_stack(sp))
 544                        goto out;
 545
 546                /* Since we don't know the extent of the stack, and we don't
 547                   track onstack-ness, but rather calculate it, we must
 548                   presume a size.  Ho hum this interface is lossy.  */
 549                current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
 550                current->sas_ss_size = SIGSTKSZ;
 551        }
 552        ret = 0;
 553out:
 554        return ret;
 555}
 556