linux/arch/sparc/kernel/signal32.c
<<
>>
Prefs
   1/*  arch/sparc64/kernel/signal32.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 *  Copyright (C) 1997,1998 Jakub Jelinek   (jj@sunsite.mff.cuni.cz)
   8 */
   9
  10#include <linux/sched.h>
  11#include <linux/kernel.h>
  12#include <linux/signal.h>
  13#include <linux/errno.h>
  14#include <linux/wait.h>
  15#include <linux/ptrace.h>
  16#include <linux/unistd.h>
  17#include <linux/mm.h>
  18#include <linux/tty.h>
  19#include <linux/binfmts.h>
  20#include <linux/compat.h>
  21#include <linux/bitops.h>
  22#include <linux/tracehook.h>
  23
  24#include <asm/uaccess.h>
  25#include <asm/ptrace.h>
  26#include <asm/pgtable.h>
  27#include <asm/psrcompat.h>
  28#include <asm/fpumacro.h>
  29#include <asm/visasm.h>
  30#include <asm/compat_signal.h>
  31#include <asm/switch_to.h>
  32
  33#include "sigutil.h"
  34
  35#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  36
  37/* This magic should be in g_upper[0] for all upper parts
  38 * to be valid.
  39 */
  40#define SIGINFO_EXTRA_V8PLUS_MAGIC      0x130e269
  41typedef struct {
  42        unsigned int g_upper[8];
  43        unsigned int o_upper[8];
  44        unsigned int asi;
  45} siginfo_extra_v8plus_t;
  46
  47struct signal_frame32 {
  48        struct sparc_stackf32   ss;
  49        __siginfo32_t           info;
  50        /* __siginfo_fpu_t * */ u32 fpu_save;
  51        unsigned int            insns[2];
  52        unsigned int            extramask[_COMPAT_NSIG_WORDS - 1];
  53        unsigned int            extra_size; /* Should be sizeof(siginfo_extra_v8plus_t) */
  54        /* Only valid if (info.si_regs.psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS */
  55        siginfo_extra_v8plus_t  v8plus;
  56        /* __siginfo_rwin_t * */u32 rwin_save;
  57} __attribute__((aligned(8)));
  58
  59typedef struct compat_siginfo{
  60        int si_signo;
  61        int si_errno;
  62        int si_code;
  63
  64        union {
  65                int _pad[SI_PAD_SIZE32];
  66
  67                /* kill() */
  68                struct {
  69                        compat_pid_t _pid;              /* sender's pid */
  70                        unsigned int _uid;              /* sender's uid */
  71                } _kill;
  72
  73                /* POSIX.1b timers */
  74                struct {
  75                        compat_timer_t _tid;                    /* timer id */
  76                        int _overrun;                   /* overrun count */
  77                        compat_sigval_t _sigval;                /* same as below */
  78                        int _sys_private;               /* not to be passed to user */
  79                } _timer;
  80
  81                /* POSIX.1b signals */
  82                struct {
  83                        compat_pid_t _pid;              /* sender's pid */
  84                        unsigned int _uid;              /* sender's uid */
  85                        compat_sigval_t _sigval;
  86                } _rt;
  87
  88                /* SIGCHLD */
  89                struct {
  90                        compat_pid_t _pid;              /* which child */
  91                        unsigned int _uid;              /* sender's uid */
  92                        int _status;                    /* exit code */
  93                        compat_clock_t _utime;
  94                        compat_clock_t _stime;
  95                } _sigchld;
  96
  97                /* SIGILL, SIGFPE, SIGSEGV, SIGBUS, SIGEMT */
  98                struct {
  99                        u32 _addr; /* faulting insn/memory ref. */
 100                        int _trapno;
 101                } _sigfault;
 102
 103                /* SIGPOLL */
 104                struct {
 105                        int _band;      /* POLL_IN, POLL_OUT, POLL_MSG */
 106                        int _fd;
 107                } _sigpoll;
 108        } _sifields;
 109}compat_siginfo_t;
 110
 111struct rt_signal_frame32 {
 112        struct sparc_stackf32   ss;
 113        compat_siginfo_t        info;
 114        struct pt_regs32        regs;
 115        compat_sigset_t         mask;
 116        /* __siginfo_fpu_t * */ u32 fpu_save;
 117        unsigned int            insns[2];
 118        stack_t32               stack;
 119        unsigned int            extra_size; /* Should be sizeof(siginfo_extra_v8plus_t) */
 120        /* Only valid if (regs.psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS */
 121        siginfo_extra_v8plus_t  v8plus;
 122        /* __siginfo_rwin_t * */u32 rwin_save;
 123} __attribute__((aligned(8)));
 124
 125int copy_siginfo_to_user32(compat_siginfo_t __user *to, siginfo_t *from)
 126{
 127        int err;
 128
 129        if (!access_ok(VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
 130                return -EFAULT;
 131
 132        /* If you change siginfo_t structure, please be sure
 133           this code is fixed accordingly.
 134           It should never copy any pad contained in the structure
 135           to avoid security leaks, but must copy the generic
 136           3 ints plus the relevant union member.
 137           This routine must convert siginfo from 64bit to 32bit as well
 138           at the same time.  */
 139        err = __put_user(from->si_signo, &to->si_signo);
 140        err |= __put_user(from->si_errno, &to->si_errno);
 141        err |= __put_user((short)from->si_code, &to->si_code);
 142        if (from->si_code < 0)
 143                err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
 144        else {
 145                switch (from->si_code >> 16) {
 146                case __SI_TIMER >> 16:
 147                        err |= __put_user(from->si_tid, &to->si_tid);
 148                        err |= __put_user(from->si_overrun, &to->si_overrun);
 149                        err |= __put_user(from->si_int, &to->si_int);
 150                        break;
 151                case __SI_CHLD >> 16:
 152                        err |= __put_user(from->si_utime, &to->si_utime);
 153                        err |= __put_user(from->si_stime, &to->si_stime);
 154                        err |= __put_user(from->si_status, &to->si_status);
 155                default:
 156                        err |= __put_user(from->si_pid, &to->si_pid);
 157                        err |= __put_user(from->si_uid, &to->si_uid);
 158                        break;
 159                case __SI_FAULT >> 16:
 160                        err |= __put_user(from->si_trapno, &to->si_trapno);
 161                        err |= __put_user((unsigned long)from->si_addr, &to->si_addr);
 162                        break;
 163                case __SI_POLL >> 16:
 164                        err |= __put_user(from->si_band, &to->si_band);
 165                        err |= __put_user(from->si_fd, &to->si_fd);
 166                        break;
 167                case __SI_RT >> 16: /* This is not generated by the kernel as of now.  */
 168                case __SI_MESGQ >> 16:
 169                        err |= __put_user(from->si_pid, &to->si_pid);
 170                        err |= __put_user(from->si_uid, &to->si_uid);
 171                        err |= __put_user(from->si_int, &to->si_int);
 172                        break;
 173                }
 174        }
 175        return err;
 176}
 177
 178/* CAUTION: This is just a very minimalist implementation for the
 179 *          sake of compat_sys_rt_sigqueueinfo()
 180 */
 181int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
 182{
 183        if (!access_ok(VERIFY_WRITE, from, sizeof(compat_siginfo_t)))
 184                return -EFAULT;
 185
 186        if (copy_from_user(to, from, 3*sizeof(int)) ||
 187            copy_from_user(to->_sifields._pad, from->_sifields._pad,
 188                           SI_PAD_SIZE))
 189                return -EFAULT;
 190
 191        return 0;
 192}
 193
 194void do_sigreturn32(struct pt_regs *regs)
 195{
 196        struct signal_frame32 __user *sf;
 197        compat_uptr_t fpu_save;
 198        compat_uptr_t rwin_save;
 199        unsigned int psr;
 200        unsigned pc, npc;
 201        sigset_t set;
 202        unsigned seta[_COMPAT_NSIG_WORDS];
 203        int err, i;
 204        
 205        /* Always make any pending restarted system calls return -EINTR */
 206        current_thread_info()->restart_block.fn = do_no_restart_syscall;
 207
 208        synchronize_user_stack();
 209
 210        regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
 211        sf = (struct signal_frame32 __user *) regs->u_regs[UREG_FP];
 212
 213        /* 1. Make sure we are not getting garbage from the user */
 214        if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
 215            (((unsigned long) sf) & 3))
 216                goto segv;
 217
 218        get_user(pc, &sf->info.si_regs.pc);
 219        __get_user(npc, &sf->info.si_regs.npc);
 220
 221        if ((pc | npc) & 3)
 222                goto segv;
 223
 224        if (test_thread_flag(TIF_32BIT)) {
 225                pc &= 0xffffffff;
 226                npc &= 0xffffffff;
 227        }
 228        regs->tpc = pc;
 229        regs->tnpc = npc;
 230
 231        /* 2. Restore the state */
 232        err = __get_user(regs->y, &sf->info.si_regs.y);
 233        err |= __get_user(psr, &sf->info.si_regs.psr);
 234
 235        for (i = UREG_G1; i <= UREG_I7; i++)
 236                err |= __get_user(regs->u_regs[i], &sf->info.si_regs.u_regs[i]);
 237        if ((psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS) {
 238                err |= __get_user(i, &sf->v8plus.g_upper[0]);
 239                if (i == SIGINFO_EXTRA_V8PLUS_MAGIC) {
 240                        unsigned long asi;
 241
 242                        for (i = UREG_G1; i <= UREG_I7; i++)
 243                                err |= __get_user(((u32 *)regs->u_regs)[2*i], &sf->v8plus.g_upper[i]);
 244                        err |= __get_user(asi, &sf->v8plus.asi);
 245                        regs->tstate &= ~TSTATE_ASI;
 246                        regs->tstate |= ((asi & 0xffUL) << 24UL);
 247                }
 248        }
 249
 250        /* User can only change condition codes in %tstate. */
 251        regs->tstate &= ~(TSTATE_ICC|TSTATE_XCC);
 252        regs->tstate |= psr_to_tstate_icc(psr);
 253
 254        /* Prevent syscall restart.  */
 255        pt_regs_clear_syscall(regs);
 256
 257        err |= __get_user(fpu_save, &sf->fpu_save);
 258        if (!err && fpu_save)
 259                err |= restore_fpu_state(regs, compat_ptr(fpu_save));
 260        err |= __get_user(rwin_save, &sf->rwin_save);
 261        if (!err && rwin_save) {
 262                if (restore_rwin_state(compat_ptr(rwin_save)))
 263                        goto segv;
 264        }
 265        err |= __get_user(seta[0], &sf->info.si_mask);
 266        err |= copy_from_user(seta+1, &sf->extramask,
 267                              (_COMPAT_NSIG_WORDS - 1) * sizeof(unsigned int));
 268        if (err)
 269                goto segv;
 270        switch (_NSIG_WORDS) {
 271                case 4: set.sig[3] = seta[6] + (((long)seta[7]) << 32);
 272                case 3: set.sig[2] = seta[4] + (((long)seta[5]) << 32);
 273                case 2: set.sig[1] = seta[2] + (((long)seta[3]) << 32);
 274                case 1: set.sig[0] = seta[0] + (((long)seta[1]) << 32);
 275        }
 276        sigdelsetmask(&set, ~_BLOCKABLE);
 277        set_current_blocked(&set);
 278        return;
 279
 280segv:
 281        force_sig(SIGSEGV, current);
 282}
 283
 284asmlinkage void do_rt_sigreturn32(struct pt_regs *regs)
 285{
 286        struct rt_signal_frame32 __user *sf;
 287        unsigned int psr, pc, npc, u_ss_sp;
 288        compat_uptr_t fpu_save;
 289        compat_uptr_t rwin_save;
 290        mm_segment_t old_fs;
 291        sigset_t set;
 292        compat_sigset_t seta;
 293        stack_t st;
 294        int err, i;
 295        
 296        /* Always make any pending restarted system calls return -EINTR */
 297        current_thread_info()->restart_block.fn = do_no_restart_syscall;
 298
 299        synchronize_user_stack();
 300        regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
 301        sf = (struct rt_signal_frame32 __user *) regs->u_regs[UREG_FP];
 302
 303        /* 1. Make sure we are not getting garbage from the user */
 304        if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
 305            (((unsigned long) sf) & 3))
 306                goto segv;
 307
 308        get_user(pc, &sf->regs.pc);
 309        __get_user(npc, &sf->regs.npc);
 310
 311        if ((pc | npc) & 3)
 312                goto segv;
 313
 314        if (test_thread_flag(TIF_32BIT)) {
 315                pc &= 0xffffffff;
 316                npc &= 0xffffffff;
 317        }
 318        regs->tpc = pc;
 319        regs->tnpc = npc;
 320
 321        /* 2. Restore the state */
 322        err = __get_user(regs->y, &sf->regs.y);
 323        err |= __get_user(psr, &sf->regs.psr);
 324        
 325        for (i = UREG_G1; i <= UREG_I7; i++)
 326                err |= __get_user(regs->u_regs[i], &sf->regs.u_regs[i]);
 327        if ((psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS) {
 328                err |= __get_user(i, &sf->v8plus.g_upper[0]);
 329                if (i == SIGINFO_EXTRA_V8PLUS_MAGIC) {
 330                        unsigned long asi;
 331
 332                        for (i = UREG_G1; i <= UREG_I7; i++)
 333                                err |= __get_user(((u32 *)regs->u_regs)[2*i], &sf->v8plus.g_upper[i]);
 334                        err |= __get_user(asi, &sf->v8plus.asi);
 335                        regs->tstate &= ~TSTATE_ASI;
 336                        regs->tstate |= ((asi & 0xffUL) << 24UL);
 337                }
 338        }
 339
 340        /* User can only change condition codes in %tstate. */
 341        regs->tstate &= ~(TSTATE_ICC|TSTATE_XCC);
 342        regs->tstate |= psr_to_tstate_icc(psr);
 343
 344        /* Prevent syscall restart.  */
 345        pt_regs_clear_syscall(regs);
 346
 347        err |= __get_user(fpu_save, &sf->fpu_save);
 348        if (!err && fpu_save)
 349                err |= restore_fpu_state(regs, compat_ptr(fpu_save));
 350        err |= copy_from_user(&seta, &sf->mask, sizeof(compat_sigset_t));
 351        err |= __get_user(u_ss_sp, &sf->stack.ss_sp);
 352        st.ss_sp = compat_ptr(u_ss_sp);
 353        err |= __get_user(st.ss_flags, &sf->stack.ss_flags);
 354        err |= __get_user(st.ss_size, &sf->stack.ss_size);
 355        if (err)
 356                goto segv;
 357                
 358        /* It is more difficult to avoid calling this function than to
 359           call it and ignore errors.  */
 360        old_fs = get_fs();
 361        set_fs(KERNEL_DS);
 362        do_sigaltstack((stack_t __user *) &st, NULL, (unsigned long)sf);
 363        set_fs(old_fs);
 364        
 365        err |= __get_user(rwin_save, &sf->rwin_save);
 366        if (!err && rwin_save) {
 367                if (restore_rwin_state(compat_ptr(rwin_save)))
 368                        goto segv;
 369        }
 370
 371        switch (_NSIG_WORDS) {
 372                case 4: set.sig[3] = seta.sig[6] + (((long)seta.sig[7]) << 32);
 373                case 3: set.sig[2] = seta.sig[4] + (((long)seta.sig[5]) << 32);
 374                case 2: set.sig[1] = seta.sig[2] + (((long)seta.sig[3]) << 32);
 375                case 1: set.sig[0] = seta.sig[0] + (((long)seta.sig[1]) << 32);
 376        }
 377        sigdelsetmask(&set, ~_BLOCKABLE);
 378        set_current_blocked(&set);
 379        return;
 380segv:
 381        force_sig(SIGSEGV, current);
 382}
 383
 384/* Checks if the fp is valid */
 385static int invalid_frame_pointer(void __user *fp, int fplen)
 386{
 387        if ((((unsigned long) fp) & 7) || ((unsigned long)fp) > 0x100000000ULL - fplen)
 388                return 1;
 389        return 0;
 390}
 391
 392static void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
 393{
 394        unsigned long sp;
 395        
 396        regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
 397        sp = regs->u_regs[UREG_FP];
 398        
 399        /*
 400         * If we are on the alternate signal stack and would overflow it, don't.
 401         * Return an always-bogus address instead so we will die with SIGSEGV.
 402         */
 403        if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
 404                return (void __user *) -1L;
 405
 406        /* This is the X/Open sanctioned signal stack switching.  */
 407        if (sa->sa_flags & SA_ONSTACK) {
 408                if (sas_ss_flags(sp) == 0)
 409                        sp = current->sas_ss_sp + current->sas_ss_size;
 410        }
 411
 412        sp -= framesize;
 413
 414        /* Always align the stack frame.  This handles two cases.  First,
 415         * sigaltstack need not be mindful of platform specific stack
 416         * alignment.  Second, if we took this signal because the stack
 417         * is not aligned properly, we'd like to take the signal cleanly
 418         * and report that.
 419         */
 420        sp &= ~15UL;
 421
 422        return (void __user *) sp;
 423}
 424
 425/* The I-cache flush instruction only works in the primary ASI, which
 426 * right now is the nucleus, aka. kernel space.
 427 *
 428 * Therefore we have to kick the instructions out using the kernel
 429 * side linear mapping of the physical address backing the user
 430 * instructions.
 431 */
 432static void flush_signal_insns(unsigned long address)
 433{
 434        unsigned long pstate, paddr;
 435        pte_t *ptep, pte;
 436        pgd_t *pgdp;
 437        pud_t *pudp;
 438        pmd_t *pmdp;
 439
 440        /* Commit all stores of the instructions we are about to flush.  */
 441        wmb();
 442
 443        /* Disable cross-call reception.  In this way even a very wide
 444         * munmap() on another cpu can't tear down the page table
 445         * hierarchy from underneath us, since that can't complete
 446         * until the IPI tlb flush returns.
 447         */
 448
 449        __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
 450        __asm__ __volatile__("wrpr %0, %1, %%pstate"
 451                                : : "r" (pstate), "i" (PSTATE_IE));
 452
 453        pgdp = pgd_offset(current->mm, address);
 454        if (pgd_none(*pgdp))
 455                goto out_irqs_on;
 456        pudp = pud_offset(pgdp, address);
 457        if (pud_none(*pudp))
 458                goto out_irqs_on;
 459        pmdp = pmd_offset(pudp, address);
 460        if (pmd_none(*pmdp))
 461                goto out_irqs_on;
 462
 463        ptep = pte_offset_map(pmdp, address);
 464        pte = *ptep;
 465        if (!pte_present(pte))
 466                goto out_unmap;
 467
 468        paddr = (unsigned long) page_address(pte_page(pte));
 469
 470        __asm__ __volatile__("flush     %0 + %1"
 471                             : /* no outputs */
 472                             : "r" (paddr),
 473                               "r" (address & (PAGE_SIZE - 1))
 474                             : "memory");
 475
 476out_unmap:
 477        pte_unmap(ptep);
 478out_irqs_on:
 479        __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
 480
 481}
 482
 483static int setup_frame32(struct k_sigaction *ka, struct pt_regs *regs,
 484                         int signo, sigset_t *oldset)
 485{
 486        struct signal_frame32 __user *sf;
 487        int i, err, wsaved;
 488        void __user *tail;
 489        int sigframe_size;
 490        u32 psr;
 491        unsigned int seta[_COMPAT_NSIG_WORDS];
 492
 493        /* 1. Make sure everything is clean */
 494        synchronize_user_stack();
 495        save_and_clear_fpu();
 496        
 497        wsaved = get_thread_wsaved();
 498
 499        sigframe_size = sizeof(*sf);
 500        if (current_thread_info()->fpsaved[0] & FPRS_FEF)
 501                sigframe_size += sizeof(__siginfo_fpu_t);
 502        if (wsaved)
 503                sigframe_size += sizeof(__siginfo_rwin_t);
 504
 505        sf = (struct signal_frame32 __user *)
 506                get_sigframe(&ka->sa, regs, sigframe_size);
 507        
 508        if (invalid_frame_pointer(sf, sigframe_size))
 509                goto sigill;
 510
 511        tail = (sf + 1);
 512
 513        /* 2. Save the current process state */
 514        if (test_thread_flag(TIF_32BIT)) {
 515                regs->tpc &= 0xffffffff;
 516                regs->tnpc &= 0xffffffff;
 517        }
 518        err  = put_user(regs->tpc, &sf->info.si_regs.pc);
 519        err |= __put_user(regs->tnpc, &sf->info.si_regs.npc);
 520        err |= __put_user(regs->y, &sf->info.si_regs.y);
 521        psr = tstate_to_psr(regs->tstate);
 522        if (current_thread_info()->fpsaved[0] & FPRS_FEF)
 523                psr |= PSR_EF;
 524        err |= __put_user(psr, &sf->info.si_regs.psr);
 525        for (i = 0; i < 16; i++)
 526                err |= __put_user(regs->u_regs[i], &sf->info.si_regs.u_regs[i]);
 527        err |= __put_user(sizeof(siginfo_extra_v8plus_t), &sf->extra_size);
 528        err |= __put_user(SIGINFO_EXTRA_V8PLUS_MAGIC, &sf->v8plus.g_upper[0]);
 529        for (i = 1; i < 16; i++)
 530                err |= __put_user(((u32 *)regs->u_regs)[2*i],
 531                                  &sf->v8plus.g_upper[i]);
 532        err |= __put_user((regs->tstate & TSTATE_ASI) >> 24UL,
 533                          &sf->v8plus.asi);
 534
 535        if (psr & PSR_EF) {
 536                __siginfo_fpu_t __user *fp = tail;
 537                tail += sizeof(*fp);
 538                err |= save_fpu_state(regs, fp);
 539                err |= __put_user((u64)fp, &sf->fpu_save);
 540        } else {
 541                err |= __put_user(0, &sf->fpu_save);
 542        }
 543        if (wsaved) {
 544                __siginfo_rwin_t __user *rwp = tail;
 545                tail += sizeof(*rwp);
 546                err |= save_rwin_state(wsaved, rwp);
 547                err |= __put_user((u64)rwp, &sf->rwin_save);
 548                set_thread_wsaved(0);
 549        } else {
 550                err |= __put_user(0, &sf->rwin_save);
 551        }
 552
 553        switch (_NSIG_WORDS) {
 554        case 4: seta[7] = (oldset->sig[3] >> 32);
 555                seta[6] = oldset->sig[3];
 556        case 3: seta[5] = (oldset->sig[2] >> 32);
 557                seta[4] = oldset->sig[2];
 558        case 2: seta[3] = (oldset->sig[1] >> 32);
 559                seta[2] = oldset->sig[1];
 560        case 1: seta[1] = (oldset->sig[0] >> 32);
 561                seta[0] = oldset->sig[0];
 562        }
 563        err |= __put_user(seta[0], &sf->info.si_mask);
 564        err |= __copy_to_user(sf->extramask, seta + 1,
 565                              (_COMPAT_NSIG_WORDS - 1) * sizeof(unsigned int));
 566
 567        if (!wsaved) {
 568                err |= copy_in_user((u32 __user *)sf,
 569                                    (u32 __user *)(regs->u_regs[UREG_FP]),
 570                                    sizeof(struct reg_window32));
 571        } else {
 572                struct reg_window *rp;
 573
 574                rp = &current_thread_info()->reg_window[wsaved - 1];
 575                for (i = 0; i < 8; i++)
 576                        err |= __put_user(rp->locals[i], &sf->ss.locals[i]);
 577                for (i = 0; i < 6; i++)
 578                        err |= __put_user(rp->ins[i], &sf->ss.ins[i]);
 579                err |= __put_user(rp->ins[6], &sf->ss.fp);
 580                err |= __put_user(rp->ins[7], &sf->ss.callers_pc);
 581        }       
 582        if (err)
 583                goto sigsegv;
 584
 585        /* 3. signal handler back-trampoline and parameters */
 586        regs->u_regs[UREG_FP] = (unsigned long) sf;
 587        regs->u_regs[UREG_I0] = signo;
 588        regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
 589        regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
 590
 591        /* 4. signal handler */
 592        regs->tpc = (unsigned long) ka->sa.sa_handler;
 593        regs->tnpc = (regs->tpc + 4);
 594        if (test_thread_flag(TIF_32BIT)) {
 595                regs->tpc &= 0xffffffff;
 596                regs->tnpc &= 0xffffffff;
 597        }
 598
 599        /* 5. return to kernel instructions */
 600        if (ka->ka_restorer) {
 601                regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
 602        } else {
 603                unsigned long address = ((unsigned long)&(sf->insns[0]));
 604
 605                regs->u_regs[UREG_I7] = (unsigned long) (&(sf->insns[0]) - 2);
 606        
 607                err  = __put_user(0x821020d8, &sf->insns[0]); /*mov __NR_sigreturn, %g1*/
 608                err |= __put_user(0x91d02010, &sf->insns[1]); /*t 0x10*/
 609                if (err)
 610                        goto sigsegv;
 611                flush_signal_insns(address);
 612        }
 613        return 0;
 614
 615sigill:
 616        do_exit(SIGILL);
 617        return -EINVAL;
 618
 619sigsegv:
 620        force_sigsegv(signo, current);
 621        return -EFAULT;
 622}
 623
 624static int setup_rt_frame32(struct k_sigaction *ka, struct pt_regs *regs,
 625                            unsigned long signr, sigset_t *oldset,
 626                            siginfo_t *info)
 627{
 628        struct rt_signal_frame32 __user *sf;
 629        int i, err, wsaved;
 630        void __user *tail;
 631        int sigframe_size;
 632        u32 psr;
 633        compat_sigset_t seta;
 634
 635        /* 1. Make sure everything is clean */
 636        synchronize_user_stack();
 637        save_and_clear_fpu();
 638        
 639        wsaved = get_thread_wsaved();
 640
 641        sigframe_size = sizeof(*sf);
 642        if (current_thread_info()->fpsaved[0] & FPRS_FEF)
 643                sigframe_size += sizeof(__siginfo_fpu_t);
 644        if (wsaved)
 645                sigframe_size += sizeof(__siginfo_rwin_t);
 646
 647        sf = (struct rt_signal_frame32 __user *)
 648                get_sigframe(&ka->sa, regs, sigframe_size);
 649        
 650        if (invalid_frame_pointer(sf, sigframe_size))
 651                goto sigill;
 652
 653        tail = (sf + 1);
 654
 655        /* 2. Save the current process state */
 656        if (test_thread_flag(TIF_32BIT)) {
 657                regs->tpc &= 0xffffffff;
 658                regs->tnpc &= 0xffffffff;
 659        }
 660        err  = put_user(regs->tpc, &sf->regs.pc);
 661        err |= __put_user(regs->tnpc, &sf->regs.npc);
 662        err |= __put_user(regs->y, &sf->regs.y);
 663        psr = tstate_to_psr(regs->tstate);
 664        if (current_thread_info()->fpsaved[0] & FPRS_FEF)
 665                psr |= PSR_EF;
 666        err |= __put_user(psr, &sf->regs.psr);
 667        for (i = 0; i < 16; i++)
 668                err |= __put_user(regs->u_regs[i], &sf->regs.u_regs[i]);
 669        err |= __put_user(sizeof(siginfo_extra_v8plus_t), &sf->extra_size);
 670        err |= __put_user(SIGINFO_EXTRA_V8PLUS_MAGIC, &sf->v8plus.g_upper[0]);
 671        for (i = 1; i < 16; i++)
 672                err |= __put_user(((u32 *)regs->u_regs)[2*i],
 673                                  &sf->v8plus.g_upper[i]);
 674        err |= __put_user((regs->tstate & TSTATE_ASI) >> 24UL,
 675                          &sf->v8plus.asi);
 676
 677        if (psr & PSR_EF) {
 678                __siginfo_fpu_t __user *fp = tail;
 679                tail += sizeof(*fp);
 680                err |= save_fpu_state(regs, fp);
 681                err |= __put_user((u64)fp, &sf->fpu_save);
 682        } else {
 683                err |= __put_user(0, &sf->fpu_save);
 684        }
 685        if (wsaved) {
 686                __siginfo_rwin_t __user *rwp = tail;
 687                tail += sizeof(*rwp);
 688                err |= save_rwin_state(wsaved, rwp);
 689                err |= __put_user((u64)rwp, &sf->rwin_save);
 690                set_thread_wsaved(0);
 691        } else {
 692                err |= __put_user(0, &sf->rwin_save);
 693        }
 694
 695        /* Update the siginfo structure.  */
 696        err |= copy_siginfo_to_user32(&sf->info, info);
 697        
 698        /* Setup sigaltstack */
 699        err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
 700        err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
 701        err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
 702
 703        switch (_NSIG_WORDS) {
 704        case 4: seta.sig[7] = (oldset->sig[3] >> 32);
 705                seta.sig[6] = oldset->sig[3];
 706        case 3: seta.sig[5] = (oldset->sig[2] >> 32);
 707                seta.sig[4] = oldset->sig[2];
 708        case 2: seta.sig[3] = (oldset->sig[1] >> 32);
 709                seta.sig[2] = oldset->sig[1];
 710        case 1: seta.sig[1] = (oldset->sig[0] >> 32);
 711                seta.sig[0] = oldset->sig[0];
 712        }
 713        err |= __copy_to_user(&sf->mask, &seta, sizeof(compat_sigset_t));
 714
 715        if (!wsaved) {
 716                err |= copy_in_user((u32 __user *)sf,
 717                                    (u32 __user *)(regs->u_regs[UREG_FP]),
 718                                    sizeof(struct reg_window32));
 719        } else {
 720                struct reg_window *rp;
 721
 722                rp = &current_thread_info()->reg_window[wsaved - 1];
 723                for (i = 0; i < 8; i++)
 724                        err |= __put_user(rp->locals[i], &sf->ss.locals[i]);
 725                for (i = 0; i < 6; i++)
 726                        err |= __put_user(rp->ins[i], &sf->ss.ins[i]);
 727                err |= __put_user(rp->ins[6], &sf->ss.fp);
 728                err |= __put_user(rp->ins[7], &sf->ss.callers_pc);
 729        }
 730        if (err)
 731                goto sigsegv;
 732        
 733        /* 3. signal handler back-trampoline and parameters */
 734        regs->u_regs[UREG_FP] = (unsigned long) sf;
 735        regs->u_regs[UREG_I0] = signr;
 736        regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
 737        regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
 738
 739        /* 4. signal handler */
 740        regs->tpc = (unsigned long) ka->sa.sa_handler;
 741        regs->tnpc = (regs->tpc + 4);
 742        if (test_thread_flag(TIF_32BIT)) {
 743                regs->tpc &= 0xffffffff;
 744                regs->tnpc &= 0xffffffff;
 745        }
 746
 747        /* 5. return to kernel instructions */
 748        if (ka->ka_restorer)
 749                regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
 750        else {
 751                unsigned long address = ((unsigned long)&(sf->insns[0]));
 752
 753                regs->u_regs[UREG_I7] = (unsigned long) (&(sf->insns[0]) - 2);
 754        
 755                /* mov __NR_rt_sigreturn, %g1 */
 756                err |= __put_user(0x82102065, &sf->insns[0]);
 757
 758                /* t 0x10 */
 759                err |= __put_user(0x91d02010, &sf->insns[1]);
 760                if (err)
 761                        goto sigsegv;
 762
 763                flush_signal_insns(address);
 764        }
 765        return 0;
 766
 767sigill:
 768        do_exit(SIGILL);
 769        return -EINVAL;
 770
 771sigsegv:
 772        force_sigsegv(signr, current);
 773        return -EFAULT;
 774}
 775
 776static inline int handle_signal32(unsigned long signr, struct k_sigaction *ka,
 777                                  siginfo_t *info,
 778                                  sigset_t *oldset, struct pt_regs *regs)
 779{
 780        int err;
 781
 782        if (ka->sa.sa_flags & SA_SIGINFO)
 783                err = setup_rt_frame32(ka, regs, signr, oldset, info);
 784        else
 785                err = setup_frame32(ka, regs, signr, oldset);
 786
 787        if (err)
 788                return err;
 789
 790        block_sigmask(ka, signr);
 791        tracehook_signal_handler(signr, info, ka, regs, 0);
 792
 793        return 0;
 794}
 795
 796static inline void syscall_restart32(unsigned long orig_i0, struct pt_regs *regs,
 797                                     struct sigaction *sa)
 798{
 799        switch (regs->u_regs[UREG_I0]) {
 800        case ERESTART_RESTARTBLOCK:
 801        case ERESTARTNOHAND:
 802        no_system_call_restart:
 803                regs->u_regs[UREG_I0] = EINTR;
 804                regs->tstate |= TSTATE_ICARRY;
 805                break;
 806        case ERESTARTSYS:
 807                if (!(sa->sa_flags & SA_RESTART))
 808                        goto no_system_call_restart;
 809                /* fallthrough */
 810        case ERESTARTNOINTR:
 811                regs->u_regs[UREG_I0] = orig_i0;
 812                regs->tpc -= 4;
 813                regs->tnpc -= 4;
 814        }
 815}
 816
 817/* Note that 'init' is a special process: it doesn't get signals it doesn't
 818 * want to handle. Thus you cannot kill init even with a SIGKILL even by
 819 * mistake.
 820 */
 821void do_signal32(sigset_t *oldset, struct pt_regs * regs)
 822{
 823        struct k_sigaction ka;
 824        unsigned long orig_i0;
 825        int restart_syscall;
 826        siginfo_t info;
 827        int signr;
 828        
 829        signr = get_signal_to_deliver(&info, &ka, regs, NULL);
 830
 831        restart_syscall = 0;
 832        orig_i0 = 0;
 833        if (pt_regs_is_syscall(regs) &&
 834            (regs->tstate & (TSTATE_XCARRY | TSTATE_ICARRY))) {
 835                restart_syscall = 1;
 836                orig_i0 = regs->u_regs[UREG_G6];
 837        }
 838
 839        if (signr > 0) {
 840                if (restart_syscall)
 841                        syscall_restart32(orig_i0, regs, &ka.sa);
 842                if (handle_signal32(signr, &ka, &info, oldset, regs) == 0) {
 843                        /* A signal was successfully delivered; the saved
 844                         * sigmask will have been stored in the signal frame,
 845                         * and will be restored by sigreturn, so we can simply
 846                         * clear the TS_RESTORE_SIGMASK flag.
 847                         */
 848                        current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
 849                }
 850                return;
 851        }
 852        if (restart_syscall &&
 853            (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
 854             regs->u_regs[UREG_I0] == ERESTARTSYS ||
 855             regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
 856                /* replay the system call when we are done */
 857                regs->u_regs[UREG_I0] = orig_i0;
 858                regs->tpc -= 4;
 859                regs->tnpc -= 4;
 860                pt_regs_clear_syscall(regs);
 861        }
 862        if (restart_syscall &&
 863            regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
 864                regs->u_regs[UREG_G1] = __NR_restart_syscall;
 865                regs->tpc -= 4;
 866                regs->tnpc -= 4;
 867                pt_regs_clear_syscall(regs);
 868        }
 869
 870        /* If there's no signal to deliver, we just put the saved sigmask
 871         * back
 872         */
 873        if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
 874                current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
 875                set_current_blocked(&current->saved_sigmask);
 876        }
 877}
 878
 879struct sigstack32 {
 880        u32 the_stack;
 881        int cur_status;
 882};
 883
 884asmlinkage int do_sys32_sigstack(u32 u_ssptr, u32 u_ossptr, unsigned long sp)
 885{
 886        struct sigstack32 __user *ssptr =
 887                (struct sigstack32 __user *)((unsigned long)(u_ssptr));
 888        struct sigstack32 __user *ossptr =
 889                (struct sigstack32 __user *)((unsigned long)(u_ossptr));
 890        int ret = -EFAULT;
 891
 892        /* First see if old state is wanted. */
 893        if (ossptr) {
 894                if (put_user(current->sas_ss_sp + current->sas_ss_size,
 895                             &ossptr->the_stack) ||
 896                    __put_user(on_sig_stack(sp), &ossptr->cur_status))
 897                        goto out;
 898        }
 899        
 900        /* Now see if we want to update the new state. */
 901        if (ssptr) {
 902                u32 ss_sp;
 903
 904                if (get_user(ss_sp, &ssptr->the_stack))
 905                        goto out;
 906
 907                /* If the current stack was set with sigaltstack, don't
 908                 * swap stacks while we are on it.
 909                 */
 910                ret = -EPERM;
 911                if (current->sas_ss_sp && on_sig_stack(sp))
 912                        goto out;
 913                        
 914                /* Since we don't know the extent of the stack, and we don't
 915                 * track onstack-ness, but rather calculate it, we must
 916                 * presume a size.  Ho hum this interface is lossy.
 917                 */
 918                current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
 919                current->sas_ss_size = SIGSTKSZ;
 920        }
 921        
 922        ret = 0;
 923out:
 924        return ret;
 925}
 926
 927asmlinkage long do_sys32_sigaltstack(u32 ussa, u32 uossa, unsigned long sp)
 928{
 929        stack_t uss, uoss;
 930        u32 u_ss_sp = 0;
 931        int ret;
 932        mm_segment_t old_fs;
 933        stack_t32 __user *uss32 = compat_ptr(ussa);
 934        stack_t32 __user *uoss32 = compat_ptr(uossa);
 935        
 936        if (ussa && (get_user(u_ss_sp, &uss32->ss_sp) ||
 937                    __get_user(uss.ss_flags, &uss32->ss_flags) ||
 938                    __get_user(uss.ss_size, &uss32->ss_size)))
 939                return -EFAULT;
 940        uss.ss_sp = compat_ptr(u_ss_sp);
 941        old_fs = get_fs();
 942        set_fs(KERNEL_DS);
 943        ret = do_sigaltstack(ussa ? (stack_t __user *) &uss : NULL,
 944                             uossa ? (stack_t __user *) &uoss : NULL, sp);
 945        set_fs(old_fs);
 946        if (!ret && uossa && (put_user(ptr_to_compat(uoss.ss_sp), &uoss32->ss_sp) ||
 947                    __put_user(uoss.ss_flags, &uoss32->ss_flags) ||
 948                    __put_user(uoss.ss_size, &uoss32->ss_size)))
 949                return -EFAULT;
 950        return ret;
 951}
 952