[BACK]Return to locore.s CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / arch / sparc / sparc

Annotation of src/sys/arch/sparc/sparc/locore.s, Revision 1.125.2.2

1.125.2.2! bouyer      1: /*     $NetBSD: locore.s,v 1.125.2.1 2000/11/20 20:25:45 bouyer Exp $  */
1.70      mrg         2:
1.1       deraadt     3: /*
1.52      pk          4:  * Copyright (c) 1996 Paul Kranenburg
                      5:  * Copyright (c) 1996
1.55      abrown      6:  *     The President and Fellows of Harvard College. All rights reserved.
1.1       deraadt     7:  * Copyright (c) 1992, 1993
                      8:  *     The Regents of the University of California.  All rights reserved.
                      9:  *
                     10:  * This software was developed by the Computer Systems Engineering group
                     11:  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
                     12:  * contributed to Berkeley.
                     13:  *
                     14:  * All advertising materials mentioning features or use of this software
                     15:  * must display the following acknowledgement:
                     16:  *     This product includes software developed by the University of
                     17:  *     California, Lawrence Berkeley Laboratory.
1.52      pk         18:  *     This product includes software developed by Harvard University.
1.1       deraadt    19:  *
                     20:  * Redistribution and use in source and binary forms, with or without
                     21:  * modification, are permitted provided that the following conditions
                     22:  * are met:
                     23:  * 1. Redistributions of source code must retain the above copyright
                     24:  *    notice, this list of conditions and the following disclaimer.
                     25:  * 2. Redistributions in binary form must reproduce the above copyright
                     26:  *    notice, this list of conditions and the following disclaimer in the
                     27:  *    documentation and/or other materials provided with the distribution.
                     28:  * 3. All advertising materials mentioning features or use of this software
                     29:  *    must display the following acknowledgement:
                     30:  *     This product includes software developed by the University of
                     31:  *     California, Berkeley and its contributors.
1.52      pk         32:  *     This product includes software developed by Harvard University.
                     33:  *     This product includes software developed by Paul Kranenburg.
1.1       deraadt    34:  * 4. Neither the name of the University nor the names of its contributors
                     35:  *    may be used to endorse or promote products derived from this software
                     36:  *    without specific prior written permission.
                     37:  *
                     38:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     39:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     40:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     41:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     42:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     43:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     44:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     45:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     46:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     47:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     48:  * SUCH DAMAGE.
                     49:  *
1.10      deraadt    50:  *     @(#)locore.s    8.4 (Berkeley) 12/10/93
1.1       deraadt    51:  */
                     52:
1.85      jonathan   53: #include "opt_ddb.h"
1.84      thorpej    54: #include "opt_compat_svr4.h"
1.116     christos   55: #include "opt_compat_sunos.h"
1.97      pk         56: #include "opt_multiprocessor.h"
1.125.2.1  bouyer     57: #include "opt_lockdebug.h"
1.80      mrg        58:
1.47      mycroft    59: #include "assym.h"
1.52      pk         60: #include <machine/param.h>
1.111     pk         61: #include <machine/asm.h>
1.1       deraadt    62: #include <sparc/sparc/intreg.h>
                     63: #include <sparc/sparc/timerreg.h>
1.52      pk         64: #include <sparc/sparc/vaddrs.h>
1.1       deraadt    65: #ifdef notyet
                     66: #include <sparc/dev/zsreg.h>
                     67: #endif
                     68: #include <machine/ctlreg.h>
                     69: #include <machine/psl.h>
                     70: #include <machine/signal.h>
                     71: #include <machine/trap.h>
1.92      pk         72: #include <sys/syscall.h>
1.116     christos   73: #ifdef COMPAT_SUNOS
                     74: #include <compat/sunos/sunos_syscall.h>
                     75: #endif
1.41      christos   76: #ifdef COMPAT_SVR4
                     77: #include <compat/svr4/svr4_syscall.h>
                     78: #endif
1.1       deraadt    79:
                     80: /*
                     81:  * GNU assembler does not understand `.empty' directive; Sun assembler
                     82:  * gripes about labels without it.  To allow cross-compilation using
                     83:  * the Sun assembler, and because .empty directives are useful documentation,
                     84:  * we use this trick.
                     85:  */
                     86: #ifdef SUN_AS
                     87: #define        EMPTY   .empty
                     88: #else
                     89: #define        EMPTY   /* .empty */
                     90: #endif
                     91:
                     92: /* use as needed to align things on longword boundaries */
1.52      pk         93: #define        _ALIGN  .align 4
1.1       deraadt    94:
                     95: /*
                     96:  * CCFSZ (C Compiler Frame SiZe) is the size of a stack frame required if
                     97:  * a function is to call C code.  It should be just 64, but Sun defined
                     98:  * their frame with space to hold arguments 0 through 5 (plus some junk),
1.63      pk         99:  * and varargs routines (such as printf) demand this, and gcc uses this
1.1       deraadt   100:  * area at times anyway.
                    101:  */
                    102: #define        CCFSZ   96
                    103:
                    104: /*
                    105:  * A handy macro for maintaining instrumentation counters.
                    106:  * Note that this clobbers %o0 and %o1.  Normal usage is
                    107:  * something like:
                    108:  *     foointr:
                    109:  *             TRAP_SETUP(...)         ! makes %o registers safe
1.111     pk        110:  *             INCR(cnt+V_FOO) ! count a foo
1.1       deraadt   111:  */
                    112: #define INCR(what) \
                    113:        sethi   %hi(what), %o0; \
                    114:        ld      [%o0 + %lo(what)], %o1; \
                    115:        inc     %o1; \
                    116:        st      %o1, [%o0 + %lo(what)]
                    117:
                    118: /*
                    119:  * Another handy macro: load one register window, given `base' address.
                    120:  * This can be either a simple register (e.g., %sp) or include an initial
                    121:  * offset (e.g., %g6 + PCB_RW).
                    122:  */
                    123: #define        LOADWIN(addr) \
                    124:        ldd     [addr], %l0; \
                    125:        ldd     [addr + 8], %l2; \
                    126:        ldd     [addr + 16], %l4; \
                    127:        ldd     [addr + 24], %l6; \
                    128:        ldd     [addr + 32], %i0; \
                    129:        ldd     [addr + 40], %i2; \
                    130:        ldd     [addr + 48], %i4; \
                    131:        ldd     [addr + 56], %i6
                    132:
                    133: /*
                    134:  * To return from trap we need the two-instruction sequence
                    135:  * `jmp %l1; rett %l2', which is defined here for convenience.
                    136:  */
                    137: #define        RETT    jmp %l1; rett %l2
                    138:
                    139:        .data
                    140: /*
                    141:  * The interrupt stack.
                    142:  *
                    143:  * This is the very first thing in the data segment, and therefore has
                    144:  * the lowest kernel stack address.  We count on this in the interrupt
                    145:  * trap-frame setup code, since we may need to switch from the kernel
                    146:  * stack to the interrupt stack (iff we are not already on the interrupt
                    147:  * stack).  One sethi+cmp is all we need since this is so carefully
                    148:  * arranged.
1.98      pk        149:  *
                    150:  * In SMP kernels, each CPU has its own interrupt stack and the computation
                    151:  * to determine whether we're already on the interrupt stack is slightly
                    152:  * more time consuming (see INTR_SETUP() below).
1.1       deraadt   153:  */
1.111     pk        154:        .globl  _C_LABEL(intstack)
                    155:        .globl  _C_LABEL(eintstack)
                    156: _C_LABEL(intstack):
1.98      pk        157:        .skip   INT_STACK_SIZE          ! 16k = 128 128-byte stack frames
1.111     pk        158: _C_LABEL(eintstack):
1.1       deraadt   159:
1.101     pk        160: _EINTSTACKP = CPUINFO_VA + CPUINFO_EINTSTACK
                    161:
1.1       deraadt   162: /*
1.125.2.1  bouyer    163:  * CPUINFO_VA is a CPU-local virtual address; cpi->ci_self is a global
                    164:  * virtual address for the same structure.  It must be stored in p->p_cpu
                    165:  * upon context switch.
                    166:  */
                    167: _CISELFP = CPUINFO_VA + CPUINFO_SELF
                    168:
                    169: /*
1.1       deraadt   170:  * When a process exits and its u. area goes away, we set cpcb to point
                    171:  * to this `u.', leaving us with something to use for an interrupt stack,
                    172:  * and letting all the register save code have a pcb_uw to examine.
                    173:  * This is also carefully arranged (to come just before u0, so that
                    174:  * process 0's kernel stack can quietly overrun into it during bootup, if
                    175:  * we feel like doing that).
                    176:  */
1.111     pk        177:        .globl  _C_LABEL(idle_u)
                    178: _C_LABEL(idle_u):
1.13      deraadt   179:        .skip   USPACE
1.99      pk        180: /*
                    181:  * On SMP kernels, there's an idle u-area for each CPU and we must
                    182:  * read its location from cpuinfo.
                    183:  */
1.111     pk        184: IDLE_UP = CPUINFO_VA + CPUINFO_IDLE_U
1.1       deraadt   185:
                    186: /*
                    187:  * Process 0's u.
                    188:  *
                    189:  * This must be aligned on an 8 byte boundary.
                    190:  */
1.111     pk        191:        .globl  _C_LABEL(u0)
                    192: _C_LABEL(u0):  .skip   USPACE
1.1       deraadt   193: estack0:
                    194:
                    195: #ifdef KGDB
                    196: /*
                    197:  * Another item that must be aligned, easiest to put it here.
                    198:  */
                    199: KGDB_STACK_SIZE = 2048
1.111     pk        200:        .globl  _C_LABEL(kgdb_stack)
                    201: _C_LABEL(kgdb_stack):
1.1       deraadt   202:        .skip   KGDB_STACK_SIZE         ! hope this is enough
                    203: #endif
                    204:
                    205: /*
1.111     pk        206:  * cpcb points to the current pcb (and hence u. area).
1.1       deraadt   207:  * Initially this is the special one.
                    208:  */
1.111     pk        209: cpcb = CPUINFO_VA + CPUINFO_CURPCB
1.1       deraadt   210:
1.111     pk        211: /* curproc points to the current process that has the CPU */
                    212: curproc = CPUINFO_VA + CPUINFO_CURPROC
1.104     pk        213:
1.52      pk        214: /*
1.111     pk        215:  * cputyp is the current cpu type, used to distinguish between
1.13      deraadt   216:  * the many variations of different sun4* machines. It contains
                    217:  * the value CPU_SUN4, CPU_SUN4C, or CPU_SUN4M.
1.9       deraadt   218:  */
1.111     pk        219:        .globl  _C_LABEL(cputyp)
                    220: _C_LABEL(cputyp):
1.9       deraadt   221:        .word   1
1.52      pk        222:
1.18      deraadt   223: #if defined(SUN4C) || defined(SUN4M)
1.111     pk        224: cputypval:
1.18      deraadt   225:        .asciz  "sun4c"
                    226:        .ascii  "     "
1.111     pk        227: cputypvar:
1.37      pk        228:        .asciz  "compatible"
1.52      pk        229:        _ALIGN
1.18      deraadt   230: #endif
                    231:
1.13      deraadt   232: /*
                    233:  * There variables are pointed to by the cpp symbols PGSHIFT, NBPG,
                    234:  * and PGOFSET.
                    235:  */
1.111     pk        236:        .globl  _C_LABEL(pgshift), _C_LABEL(nbpg), _C_LABEL(pgofset)
                    237: _C_LABEL(pgshift):
1.52      pk        238:        .word   0
1.111     pk        239: _C_LABEL(nbpg):
1.52      pk        240:        .word   0
1.111     pk        241: _C_LABEL(pgofset):
1.52      pk        242:        .word   0
                    243:
1.111     pk        244:        .globl  _C_LABEL(trapbase)
                    245: _C_LABEL(trapbase):
1.52      pk        246:        .word   0
1.9       deraadt   247:
1.75      pk        248: #if 0
1.9       deraadt   249: #if defined(SUN4M)
                    250: _mapme:
                    251:        .asciz "0 0 f8000000 15c6a0 map-pages"
                    252: #endif
1.75      pk        253: #endif
1.9       deraadt   254:
                    255: #if !defined(SUN4M)
                    256: sun4m_notsup:
1.20      deraadt   257:        .asciz  "cr .( NetBSD/sparc: this kernel does not support the sun4m) cr"
1.9       deraadt   258: #endif
1.13      deraadt   259: #if !defined(SUN4C)
1.9       deraadt   260: sun4c_notsup:
1.20      deraadt   261:        .asciz  "cr .( NetBSD/sparc: this kernel does not support the sun4c) cr"
1.13      deraadt   262: #endif
                    263: #if !defined(SUN4)
                    264: sun4_notsup:
1.20      deraadt   265:        ! the extra characters at the end are to ensure the zs fifo drains
                    266:        ! before we halt. Sick, eh?
                    267:        .asciz  "NetBSD/sparc: this kernel does not support the sun4\n\r \b"
1.9       deraadt   268: #endif
1.52      pk        269:        _ALIGN
1.9       deraadt   270:
1.1       deraadt   271:        .text
                    272:
                    273: /*
1.26      deraadt   274:  * The first thing in the real text segment is the trap vector table,
                    275:  * which must be aligned on a 4096 byte boundary.  The text segment
                    276:  * starts beyond page 0 of KERNBASE so that there is a red zone
                    277:  * between user and kernel space.  Since the boot ROM loads us at
1.119     christos  278:  * PROM_LOADADDR, it is far easier to start at KERNBASE+PROM_LOADADDR than to
1.26      deraadt   279:  * buck the trend.  This is two or four pages in (depending on if
                    280:  * pagesize is 8192 or 4096).    We place two items in this area:
1.75      pk        281:  * the message buffer (phys addr 0) and the cpu_softc structure for
                    282:  * the first processor in the system (phys addr 0x2000).
                    283:  * Because the message buffer is in our "red zone" between user and
1.26      deraadt   284:  * kernel space we remap it in configure() to another location and
                    285:  * invalidate the mapping at KERNBASE.
                    286:  */
                    287:
1.1       deraadt   288: /*
                    289:  * Each trap has room for four instructions, of which one perforce must
                    290:  * be a branch.  On entry the hardware has copied pc and npc to %l1 and
                    291:  * %l2 respectively.  We use two more to read the psr into %l0, and to
                    292:  * put the trap type value into %l3 (with a few exceptions below).
                    293:  * We could read the trap type field of %tbr later in the code instead,
                    294:  * but there is no need, and that would require more instructions
                    295:  * (read+mask, vs 1 `mov' here).
                    296:  *
                    297:  * I used to generate these numbers by address arithmetic, but gas's
                    298:  * expression evaluator has about as much sense as your average slug
                    299:  * (oddly enough, the code looks about as slimy too).  Thus, all the
                    300:  * trap numbers are given as arguments to the trap macros.  This means
                    301:  * there is one line per trap.  Sigh.
                    302:  *
                    303:  * Note that only the local registers may be used, since the trap
                    304:  * window is potentially the last window.  Its `in' registers are
                    305:  * the previous window's outs (as usual), but more important, its
                    306:  * `out' registers may be in use as the `topmost' window's `in' registers.
                    307:  * The global registers are of course verboten (well, until we save
                    308:  * them away).
                    309:  *
                    310:  * Hardware interrupt vectors can be `linked'---the linkage is to regular
                    311:  * C code---or rewired to fast in-window handlers.  The latter are good
                    312:  * for unbuffered hardware like the Zilog serial chip and the AMD audio
                    313:  * chip, where many interrupts can be handled trivially with pseudo-DMA or
                    314:  * similar.  Only one `fast' interrupt can be used per level, however, and
                    315:  * direct and `fast' interrupts are incompatible.  Routines in intr.c
                    316:  * handle setting these, with optional paranoia.
                    317:  */
                    318:
                    319:        /* regular vectored traps */
                    320: #define        VTRAP(type, label) \
                    321:        mov (type), %l3; b label; mov %psr, %l0; nop
                    322:
                    323:        /* hardware interrupts (can be linked or made `fast') */
1.52      pk        324: #define        HARDINT44C(lev) \
1.111     pk        325:        mov (lev), %l3; b _C_LABEL(sparc_interrupt44c); mov %psr, %l0; nop
1.52      pk        326:
                    327:        /* hardware interrupts (can be linked or made `fast') */
                    328: #define        HARDINT4M(lev) \
1.111     pk        329:        mov (lev), %l3; b _C_LABEL(sparc_interrupt4m); mov %psr, %l0; nop
1.1       deraadt   330:
                    331:        /* software interrupts (may not be made direct, sorry---but you
                    332:           should not be using them trivially anyway) */
1.52      pk        333: #define        SOFTINT44C(lev, bit) \
                    334:        mov (lev), %l3; mov (bit), %l4; b softintr_sun44c; mov %psr, %l0
                    335:
                    336:        /* There's no SOFTINT4M(): both hard and soft vector the same way */
1.1       deraadt   337:
                    338:        /* traps that just call trap() */
                    339: #define        TRAP(type)      VTRAP(type, slowtrap)
                    340:
                    341:        /* architecturally undefined traps (cause panic) */
                    342: #define        UTRAP(type)     VTRAP(type, slowtrap)
                    343:
                    344:        /* software undefined traps (may be replaced) */
                    345: #define        STRAP(type)     VTRAP(type, slowtrap)
                    346:
                    347: /* breakpoint acts differently under kgdb */
                    348: #ifdef KGDB
                    349: #define        BPT             VTRAP(T_BREAKPOINT, bpt)
1.52      pk        350: #define        BPT_KGDB_EXEC   VTRAP(T_KGDB_EXEC, bpt)
                    351: #else
                    352: #define        BPT             TRAP(T_BREAKPOINT)
                    353: #define        BPT_KGDB_EXEC   TRAP(T_KGDB_EXEC)
                    354: #endif
                    355:
                    356: /* special high-speed 1-instruction-shaved-off traps (get nothing in %l3) */
1.122     christos  357: #define        SYSCALL         b _C_LABEL(_syscall); mov %psr, %l0; nop; nop
1.52      pk        358: #define        WINDOW_OF       b window_of; mov %psr, %l0; nop; nop
                    359: #define        WINDOW_UF       b window_uf; mov %psr, %l0; nop; nop
                    360: #ifdef notyet
                    361: #define        ZS_INTERRUPT    b zshard; mov %psr, %l0; nop; nop
                    362: #else
                    363: #define        ZS_INTERRUPT44C HARDINT44C(12)
                    364: #define        ZS_INTERRUPT4M  HARDINT4M(12)
                    365: #endif
                    366:
1.111     pk        367:        .globl  _ASM_LABEL(start), _C_LABEL(kernel_text)
                    368:        _C_LABEL(kernel_text) = start           ! for kvm_mkdb(8)
                    369: _ASM_LABEL(start):
1.52      pk        370: /*
                    371:  * Put sun4 traptable first, since it needs the most stringent aligment (8192)
                    372:  */
                    373: #if defined(SUN4)
                    374: trapbase_sun4:
                    375:        /* trap 0 is special since we cannot receive it */
                    376:        b dostart; nop; nop; nop        ! 00 = reset (fake)
                    377:        VTRAP(T_TEXTFAULT, memfault_sun4)       ! 01 = instr. fetch fault
                    378:        TRAP(T_ILLINST)                 ! 02 = illegal instruction
                    379:        TRAP(T_PRIVINST)                ! 03 = privileged instruction
                    380:        TRAP(T_FPDISABLED)              ! 04 = fp instr, but EF bit off in psr
                    381:        WINDOW_OF                       ! 05 = window overflow
                    382:        WINDOW_UF                       ! 06 = window underflow
                    383:        TRAP(T_ALIGN)                   ! 07 = address alignment error
                    384:        VTRAP(T_FPE, fp_exception)      ! 08 = fp exception
                    385:        VTRAP(T_DATAFAULT, memfault_sun4)       ! 09 = data fetch fault
                    386:        TRAP(T_TAGOF)                   ! 0a = tag overflow
                    387:        UTRAP(0x0b)
                    388:        UTRAP(0x0c)
                    389:        UTRAP(0x0d)
                    390:        UTRAP(0x0e)
                    391:        UTRAP(0x0f)
                    392:        UTRAP(0x10)
                    393:        SOFTINT44C(1, IE_L1)            ! 11 = level 1 interrupt
                    394:        HARDINT44C(2)                   ! 12 = level 2 interrupt
                    395:        HARDINT44C(3)                   ! 13 = level 3 interrupt
                    396:        SOFTINT44C(4, IE_L4)            ! 14 = level 4 interrupt
                    397:        HARDINT44C(5)                   ! 15 = level 5 interrupt
                    398:        SOFTINT44C(6, IE_L6)            ! 16 = level 6 interrupt
                    399:        HARDINT44C(7)                   ! 17 = level 7 interrupt
                    400:        HARDINT44C(8)                   ! 18 = level 8 interrupt
                    401:        HARDINT44C(9)                   ! 19 = level 9 interrupt
                    402:        HARDINT44C(10)                  ! 1a = level 10 interrupt
                    403:        HARDINT44C(11)                  ! 1b = level 11 interrupt
                    404:        ZS_INTERRUPT44C                 ! 1c = level 12 (zs) interrupt
                    405:        HARDINT44C(13)                  ! 1d = level 13 interrupt
                    406:        HARDINT44C(14)                  ! 1e = level 14 interrupt
                    407:        VTRAP(15, nmi_sun4)             ! 1f = nonmaskable interrupt
                    408:        UTRAP(0x20)
                    409:        UTRAP(0x21)
                    410:        UTRAP(0x22)
                    411:        UTRAP(0x23)
                    412:        TRAP(T_CPDISABLED)      ! 24 = coprocessor instr, EC bit off in psr
                    413:        UTRAP(0x25)
                    414:        UTRAP(0x26)
                    415:        UTRAP(0x27)
                    416:        TRAP(T_CPEXCEPTION)     ! 28 = coprocessor exception
                    417:        UTRAP(0x29)
                    418:        UTRAP(0x2a)
                    419:        UTRAP(0x2b)
                    420:        UTRAP(0x2c)
                    421:        UTRAP(0x2d)
                    422:        UTRAP(0x2e)
                    423:        UTRAP(0x2f)
                    424:        UTRAP(0x30)
                    425:        UTRAP(0x31)
                    426:        UTRAP(0x32)
                    427:        UTRAP(0x33)
                    428:        UTRAP(0x34)
                    429:        UTRAP(0x35)
                    430:        UTRAP(0x36)
                    431:        UTRAP(0x37)
                    432:        UTRAP(0x38)
                    433:        UTRAP(0x39)
                    434:        UTRAP(0x3a)
                    435:        UTRAP(0x3b)
                    436:        UTRAP(0x3c)
                    437:        UTRAP(0x3d)
                    438:        UTRAP(0x3e)
                    439:        UTRAP(0x3f)
                    440:        UTRAP(0x40)
                    441:        UTRAP(0x41)
                    442:        UTRAP(0x42)
                    443:        UTRAP(0x43)
                    444:        UTRAP(0x44)
                    445:        UTRAP(0x45)
                    446:        UTRAP(0x46)
                    447:        UTRAP(0x47)
                    448:        UTRAP(0x48)
                    449:        UTRAP(0x49)
                    450:        UTRAP(0x4a)
                    451:        UTRAP(0x4b)
                    452:        UTRAP(0x4c)
                    453:        UTRAP(0x4d)
                    454:        UTRAP(0x4e)
                    455:        UTRAP(0x4f)
                    456:        UTRAP(0x50)
                    457:        UTRAP(0x51)
                    458:        UTRAP(0x52)
                    459:        UTRAP(0x53)
                    460:        UTRAP(0x54)
                    461:        UTRAP(0x55)
                    462:        UTRAP(0x56)
                    463:        UTRAP(0x57)
                    464:        UTRAP(0x58)
                    465:        UTRAP(0x59)
                    466:        UTRAP(0x5a)
                    467:        UTRAP(0x5b)
                    468:        UTRAP(0x5c)
                    469:        UTRAP(0x5d)
                    470:        UTRAP(0x5e)
                    471:        UTRAP(0x5f)
                    472:        UTRAP(0x60)
                    473:        UTRAP(0x61)
                    474:        UTRAP(0x62)
                    475:        UTRAP(0x63)
                    476:        UTRAP(0x64)
                    477:        UTRAP(0x65)
                    478:        UTRAP(0x66)
                    479:        UTRAP(0x67)
                    480:        UTRAP(0x68)
                    481:        UTRAP(0x69)
                    482:        UTRAP(0x6a)
                    483:        UTRAP(0x6b)
                    484:        UTRAP(0x6c)
                    485:        UTRAP(0x6d)
                    486:        UTRAP(0x6e)
                    487:        UTRAP(0x6f)
                    488:        UTRAP(0x70)
                    489:        UTRAP(0x71)
                    490:        UTRAP(0x72)
                    491:        UTRAP(0x73)
                    492:        UTRAP(0x74)
                    493:        UTRAP(0x75)
                    494:        UTRAP(0x76)
                    495:        UTRAP(0x77)
                    496:        UTRAP(0x78)
                    497:        UTRAP(0x79)
                    498:        UTRAP(0x7a)
                    499:        UTRAP(0x7b)
                    500:        UTRAP(0x7c)
                    501:        UTRAP(0x7d)
                    502:        UTRAP(0x7e)
                    503:        UTRAP(0x7f)
                    504:        SYSCALL                 ! 80 = sun syscall
                    505:        BPT                     ! 81 = pseudo breakpoint instruction
                    506:        TRAP(T_DIV0)            ! 82 = divide by zero
                    507:        TRAP(T_FLUSHWIN)        ! 83 = flush windows
                    508:        TRAP(T_CLEANWIN)        ! 84 = provide clean windows
                    509:        TRAP(T_RANGECHECK)      ! 85 = ???
                    510:        TRAP(T_FIXALIGN)        ! 86 = fix up unaligned accesses
                    511:        TRAP(T_INTOF)           ! 87 = integer overflow
                    512:        SYSCALL                 ! 88 = svr4 syscall
                    513:        SYSCALL                 ! 89 = bsd syscall
                    514:        BPT_KGDB_EXEC           ! 8a = enter kernel gdb on kernel startup
                    515:        STRAP(0x8b)
                    516:        STRAP(0x8c)
                    517:        STRAP(0x8d)
                    518:        STRAP(0x8e)
                    519:        STRAP(0x8f)
                    520:        STRAP(0x90)
                    521:        STRAP(0x91)
                    522:        STRAP(0x92)
                    523:        STRAP(0x93)
                    524:        STRAP(0x94)
                    525:        STRAP(0x95)
                    526:        STRAP(0x96)
                    527:        STRAP(0x97)
                    528:        STRAP(0x98)
                    529:        STRAP(0x99)
                    530:        STRAP(0x9a)
                    531:        STRAP(0x9b)
                    532:        STRAP(0x9c)
                    533:        STRAP(0x9d)
                    534:        STRAP(0x9e)
                    535:        STRAP(0x9f)
                    536:        STRAP(0xa0)
                    537:        STRAP(0xa1)
                    538:        STRAP(0xa2)
                    539:        STRAP(0xa3)
                    540:        STRAP(0xa4)
                    541:        STRAP(0xa5)
                    542:        STRAP(0xa6)
                    543:        STRAP(0xa7)
                    544:        STRAP(0xa8)
                    545:        STRAP(0xa9)
                    546:        STRAP(0xaa)
                    547:        STRAP(0xab)
                    548:        STRAP(0xac)
                    549:        STRAP(0xad)
                    550:        STRAP(0xae)
                    551:        STRAP(0xaf)
                    552:        STRAP(0xb0)
                    553:        STRAP(0xb1)
                    554:        STRAP(0xb2)
                    555:        STRAP(0xb3)
                    556:        STRAP(0xb4)
                    557:        STRAP(0xb5)
                    558:        STRAP(0xb6)
                    559:        STRAP(0xb7)
                    560:        STRAP(0xb8)
                    561:        STRAP(0xb9)
                    562:        STRAP(0xba)
                    563:        STRAP(0xbb)
                    564:        STRAP(0xbc)
                    565:        STRAP(0xbd)
                    566:        STRAP(0xbe)
                    567:        STRAP(0xbf)
                    568:        STRAP(0xc0)
                    569:        STRAP(0xc1)
                    570:        STRAP(0xc2)
                    571:        STRAP(0xc3)
                    572:        STRAP(0xc4)
                    573:        STRAP(0xc5)
                    574:        STRAP(0xc6)
                    575:        STRAP(0xc7)
                    576:        STRAP(0xc8)
                    577:        STRAP(0xc9)
                    578:        STRAP(0xca)
                    579:        STRAP(0xcb)
                    580:        STRAP(0xcc)
                    581:        STRAP(0xcd)
                    582:        STRAP(0xce)
                    583:        STRAP(0xcf)
                    584:        STRAP(0xd0)
                    585:        STRAP(0xd1)
                    586:        STRAP(0xd2)
                    587:        STRAP(0xd3)
                    588:        STRAP(0xd4)
                    589:        STRAP(0xd5)
                    590:        STRAP(0xd6)
                    591:        STRAP(0xd7)
                    592:        STRAP(0xd8)
                    593:        STRAP(0xd9)
                    594:        STRAP(0xda)
                    595:        STRAP(0xdb)
                    596:        STRAP(0xdc)
                    597:        STRAP(0xdd)
                    598:        STRAP(0xde)
                    599:        STRAP(0xdf)
                    600:        STRAP(0xe0)
                    601:        STRAP(0xe1)
                    602:        STRAP(0xe2)
                    603:        STRAP(0xe3)
                    604:        STRAP(0xe4)
                    605:        STRAP(0xe5)
                    606:        STRAP(0xe6)
                    607:        STRAP(0xe7)
                    608:        STRAP(0xe8)
                    609:        STRAP(0xe9)
                    610:        STRAP(0xea)
                    611:        STRAP(0xeb)
                    612:        STRAP(0xec)
                    613:        STRAP(0xed)
                    614:        STRAP(0xee)
                    615:        STRAP(0xef)
                    616:        STRAP(0xf0)
                    617:        STRAP(0xf1)
                    618:        STRAP(0xf2)
                    619:        STRAP(0xf3)
                    620:        STRAP(0xf4)
                    621:        STRAP(0xf5)
                    622:        STRAP(0xf6)
                    623:        STRAP(0xf7)
                    624:        STRAP(0xf8)
                    625:        STRAP(0xf9)
                    626:        STRAP(0xfa)
                    627:        STRAP(0xfb)
                    628:        STRAP(0xfc)
                    629:        STRAP(0xfd)
                    630:        STRAP(0xfe)
                    631:        STRAP(0xff)
                    632: #endif
                    633:
                    634: #if defined(SUN4C)
                    635: trapbase_sun4c:
                    636: /* trap 0 is special since we cannot receive it */
                    637:        b dostart; nop; nop; nop        ! 00 = reset (fake)
                    638:        VTRAP(T_TEXTFAULT, memfault_sun4c)      ! 01 = instr. fetch fault
                    639:        TRAP(T_ILLINST)                 ! 02 = illegal instruction
                    640:        TRAP(T_PRIVINST)                ! 03 = privileged instruction
                    641:        TRAP(T_FPDISABLED)              ! 04 = fp instr, but EF bit off in psr
                    642:        WINDOW_OF                       ! 05 = window overflow
                    643:        WINDOW_UF                       ! 06 = window underflow
                    644:        TRAP(T_ALIGN)                   ! 07 = address alignment error
                    645:        VTRAP(T_FPE, fp_exception)      ! 08 = fp exception
                    646:        VTRAP(T_DATAFAULT, memfault_sun4c)      ! 09 = data fetch fault
                    647:        TRAP(T_TAGOF)                   ! 0a = tag overflow
                    648:        UTRAP(0x0b)
                    649:        UTRAP(0x0c)
                    650:        UTRAP(0x0d)
                    651:        UTRAP(0x0e)
                    652:        UTRAP(0x0f)
                    653:        UTRAP(0x10)
                    654:        SOFTINT44C(1, IE_L1)            ! 11 = level 1 interrupt
                    655:        HARDINT44C(2)                   ! 12 = level 2 interrupt
                    656:        HARDINT44C(3)                   ! 13 = level 3 interrupt
                    657:        SOFTINT44C(4, IE_L4)            ! 14 = level 4 interrupt
                    658:        HARDINT44C(5)                   ! 15 = level 5 interrupt
                    659:        SOFTINT44C(6, IE_L6)            ! 16 = level 6 interrupt
                    660:        HARDINT44C(7)                   ! 17 = level 7 interrupt
                    661:        HARDINT44C(8)                   ! 18 = level 8 interrupt
                    662:        HARDINT44C(9)                   ! 19 = level 9 interrupt
                    663:        HARDINT44C(10)                  ! 1a = level 10 interrupt
                    664:        HARDINT44C(11)                  ! 1b = level 11 interrupt
                    665:        ZS_INTERRUPT44C                 ! 1c = level 12 (zs) interrupt
                    666:        HARDINT44C(13)                  ! 1d = level 13 interrupt
                    667:        HARDINT44C(14)                  ! 1e = level 14 interrupt
                    668:        VTRAP(15, nmi_sun4c)            ! 1f = nonmaskable interrupt
                    669:        UTRAP(0x20)
                    670:        UTRAP(0x21)
                    671:        UTRAP(0x22)
                    672:        UTRAP(0x23)
                    673:        TRAP(T_CPDISABLED)      ! 24 = coprocessor instr, EC bit off in psr
                    674:        UTRAP(0x25)
                    675:        UTRAP(0x26)
                    676:        UTRAP(0x27)
                    677:        TRAP(T_CPEXCEPTION)     ! 28 = coprocessor exception
                    678:        UTRAP(0x29)
                    679:        UTRAP(0x2a)
                    680:        UTRAP(0x2b)
                    681:        UTRAP(0x2c)
                    682:        UTRAP(0x2d)
                    683:        UTRAP(0x2e)
                    684:        UTRAP(0x2f)
                    685:        UTRAP(0x30)
                    686:        UTRAP(0x31)
                    687:        UTRAP(0x32)
                    688:        UTRAP(0x33)
                    689:        UTRAP(0x34)
                    690:        UTRAP(0x35)
                    691:        UTRAP(0x36)
                    692:        UTRAP(0x37)
                    693:        UTRAP(0x38)
                    694:        UTRAP(0x39)
                    695:        UTRAP(0x3a)
                    696:        UTRAP(0x3b)
                    697:        UTRAP(0x3c)
                    698:        UTRAP(0x3d)
                    699:        UTRAP(0x3e)
                    700:        UTRAP(0x3f)
                    701:        UTRAP(0x40)
                    702:        UTRAP(0x41)
                    703:        UTRAP(0x42)
                    704:        UTRAP(0x43)
                    705:        UTRAP(0x44)
                    706:        UTRAP(0x45)
                    707:        UTRAP(0x46)
                    708:        UTRAP(0x47)
                    709:        UTRAP(0x48)
                    710:        UTRAP(0x49)
                    711:        UTRAP(0x4a)
                    712:        UTRAP(0x4b)
                    713:        UTRAP(0x4c)
                    714:        UTRAP(0x4d)
                    715:        UTRAP(0x4e)
                    716:        UTRAP(0x4f)
                    717:        UTRAP(0x50)
                    718:        UTRAP(0x51)
                    719:        UTRAP(0x52)
                    720:        UTRAP(0x53)
                    721:        UTRAP(0x54)
                    722:        UTRAP(0x55)
                    723:        UTRAP(0x56)
                    724:        UTRAP(0x57)
                    725:        UTRAP(0x58)
                    726:        UTRAP(0x59)
                    727:        UTRAP(0x5a)
                    728:        UTRAP(0x5b)
                    729:        UTRAP(0x5c)
                    730:        UTRAP(0x5d)
                    731:        UTRAP(0x5e)
                    732:        UTRAP(0x5f)
                    733:        UTRAP(0x60)
                    734:        UTRAP(0x61)
                    735:        UTRAP(0x62)
                    736:        UTRAP(0x63)
                    737:        UTRAP(0x64)
                    738:        UTRAP(0x65)
                    739:        UTRAP(0x66)
                    740:        UTRAP(0x67)
                    741:        UTRAP(0x68)
                    742:        UTRAP(0x69)
                    743:        UTRAP(0x6a)
                    744:        UTRAP(0x6b)
                    745:        UTRAP(0x6c)
                    746:        UTRAP(0x6d)
                    747:        UTRAP(0x6e)
                    748:        UTRAP(0x6f)
                    749:        UTRAP(0x70)
                    750:        UTRAP(0x71)
                    751:        UTRAP(0x72)
                    752:        UTRAP(0x73)
                    753:        UTRAP(0x74)
                    754:        UTRAP(0x75)
                    755:        UTRAP(0x76)
                    756:        UTRAP(0x77)
                    757:        UTRAP(0x78)
                    758:        UTRAP(0x79)
                    759:        UTRAP(0x7a)
                    760:        UTRAP(0x7b)
                    761:        UTRAP(0x7c)
                    762:        UTRAP(0x7d)
                    763:        UTRAP(0x7e)
                    764:        UTRAP(0x7f)
                    765:        SYSCALL                 ! 80 = sun syscall
                    766:        BPT                     ! 81 = pseudo breakpoint instruction
                    767:        TRAP(T_DIV0)            ! 82 = divide by zero
                    768:        TRAP(T_FLUSHWIN)        ! 83 = flush windows
                    769:        TRAP(T_CLEANWIN)        ! 84 = provide clean windows
                    770:        TRAP(T_RANGECHECK)      ! 85 = ???
                    771:        TRAP(T_FIXALIGN)        ! 86 = fix up unaligned accesses
                    772:        TRAP(T_INTOF)           ! 87 = integer overflow
                    773:        SYSCALL                 ! 88 = svr4 syscall
                    774:        SYSCALL                 ! 89 = bsd syscall
                    775:        BPT_KGDB_EXEC           ! 8a = enter kernel gdb on kernel startup
                    776:        STRAP(0x8b)
                    777:        STRAP(0x8c)
                    778:        STRAP(0x8d)
                    779:        STRAP(0x8e)
                    780:        STRAP(0x8f)
                    781:        STRAP(0x90)
                    782:        STRAP(0x91)
                    783:        STRAP(0x92)
                    784:        STRAP(0x93)
                    785:        STRAP(0x94)
                    786:        STRAP(0x95)
                    787:        STRAP(0x96)
                    788:        STRAP(0x97)
                    789:        STRAP(0x98)
                    790:        STRAP(0x99)
                    791:        STRAP(0x9a)
                    792:        STRAP(0x9b)
                    793:        STRAP(0x9c)
                    794:        STRAP(0x9d)
                    795:        STRAP(0x9e)
                    796:        STRAP(0x9f)
                    797:        STRAP(0xa0)
                    798:        STRAP(0xa1)
                    799:        STRAP(0xa2)
                    800:        STRAP(0xa3)
                    801:        STRAP(0xa4)
                    802:        STRAP(0xa5)
                    803:        STRAP(0xa6)
                    804:        STRAP(0xa7)
                    805:        STRAP(0xa8)
                    806:        STRAP(0xa9)
                    807:        STRAP(0xaa)
                    808:        STRAP(0xab)
                    809:        STRAP(0xac)
                    810:        STRAP(0xad)
                    811:        STRAP(0xae)
                    812:        STRAP(0xaf)
                    813:        STRAP(0xb0)
                    814:        STRAP(0xb1)
                    815:        STRAP(0xb2)
                    816:        STRAP(0xb3)
                    817:        STRAP(0xb4)
                    818:        STRAP(0xb5)
                    819:        STRAP(0xb6)
                    820:        STRAP(0xb7)
                    821:        STRAP(0xb8)
                    822:        STRAP(0xb9)
                    823:        STRAP(0xba)
                    824:        STRAP(0xbb)
                    825:        STRAP(0xbc)
                    826:        STRAP(0xbd)
                    827:        STRAP(0xbe)
                    828:        STRAP(0xbf)
                    829:        STRAP(0xc0)
                    830:        STRAP(0xc1)
                    831:        STRAP(0xc2)
                    832:        STRAP(0xc3)
                    833:        STRAP(0xc4)
                    834:        STRAP(0xc5)
                    835:        STRAP(0xc6)
                    836:        STRAP(0xc7)
                    837:        STRAP(0xc8)
                    838:        STRAP(0xc9)
                    839:        STRAP(0xca)
                    840:        STRAP(0xcb)
                    841:        STRAP(0xcc)
                    842:        STRAP(0xcd)
                    843:        STRAP(0xce)
                    844:        STRAP(0xcf)
                    845:        STRAP(0xd0)
                    846:        STRAP(0xd1)
                    847:        STRAP(0xd2)
                    848:        STRAP(0xd3)
                    849:        STRAP(0xd4)
                    850:        STRAP(0xd5)
                    851:        STRAP(0xd6)
                    852:        STRAP(0xd7)
                    853:        STRAP(0xd8)
                    854:        STRAP(0xd9)
                    855:        STRAP(0xda)
                    856:        STRAP(0xdb)
                    857:        STRAP(0xdc)
                    858:        STRAP(0xdd)
                    859:        STRAP(0xde)
                    860:        STRAP(0xdf)
                    861:        STRAP(0xe0)
                    862:        STRAP(0xe1)
                    863:        STRAP(0xe2)
                    864:        STRAP(0xe3)
                    865:        STRAP(0xe4)
                    866:        STRAP(0xe5)
                    867:        STRAP(0xe6)
                    868:        STRAP(0xe7)
                    869:        STRAP(0xe8)
                    870:        STRAP(0xe9)
                    871:        STRAP(0xea)
                    872:        STRAP(0xeb)
                    873:        STRAP(0xec)
                    874:        STRAP(0xed)
                    875:        STRAP(0xee)
                    876:        STRAP(0xef)
                    877:        STRAP(0xf0)
                    878:        STRAP(0xf1)
                    879:        STRAP(0xf2)
                    880:        STRAP(0xf3)
                    881:        STRAP(0xf4)
                    882:        STRAP(0xf5)
                    883:        STRAP(0xf6)
                    884:        STRAP(0xf7)
                    885:        STRAP(0xf8)
                    886:        STRAP(0xf9)
                    887:        STRAP(0xfa)
                    888:        STRAP(0xfb)
                    889:        STRAP(0xfc)
                    890:        STRAP(0xfd)
                    891:        STRAP(0xfe)
                    892:        STRAP(0xff)
1.1       deraadt   893: #endif
                    894:
1.52      pk        895: #if defined(SUN4M)
                    896: trapbase_sun4m:
1.1       deraadt   897: /* trap 0 is special since we cannot receive it */
                    898:        b dostart; nop; nop; nop        ! 00 = reset (fake)
1.52      pk        899:        VTRAP(T_TEXTFAULT, memfault_sun4m)      ! 01 = instr. fetch fault
1.1       deraadt   900:        TRAP(T_ILLINST)                 ! 02 = illegal instruction
                    901:        TRAP(T_PRIVINST)                ! 03 = privileged instruction
                    902:        TRAP(T_FPDISABLED)              ! 04 = fp instr, but EF bit off in psr
                    903:        WINDOW_OF                       ! 05 = window overflow
                    904:        WINDOW_UF                       ! 06 = window underflow
                    905:        TRAP(T_ALIGN)                   ! 07 = address alignment error
                    906:        VTRAP(T_FPE, fp_exception)      ! 08 = fp exception
1.52      pk        907:        VTRAP(T_DATAFAULT, memfault_sun4m)      ! 09 = data fetch fault
1.1       deraadt   908:        TRAP(T_TAGOF)                   ! 0a = tag overflow
                    909:        UTRAP(0x0b)
                    910:        UTRAP(0x0c)
                    911:        UTRAP(0x0d)
                    912:        UTRAP(0x0e)
                    913:        UTRAP(0x0f)
                    914:        UTRAP(0x10)
1.52      pk        915:        HARDINT4M(1)                    ! 11 = level 1 interrupt
                    916:        HARDINT4M(2)                    ! 12 = level 2 interrupt
                    917:        HARDINT4M(3)                    ! 13 = level 3 interrupt
                    918:        HARDINT4M(4)                    ! 14 = level 4 interrupt
                    919:        HARDINT4M(5)                    ! 15 = level 5 interrupt
                    920:        HARDINT4M(6)                    ! 16 = level 6 interrupt
                    921:        HARDINT4M(7)                    ! 17 = level 7 interrupt
                    922:        HARDINT4M(8)                    ! 18 = level 8 interrupt
                    923:        HARDINT4M(9)                    ! 19 = level 9 interrupt
                    924:        HARDINT4M(10)                   ! 1a = level 10 interrupt
                    925:        HARDINT4M(11)                   ! 1b = level 11 interrupt
                    926:        ZS_INTERRUPT4M                  ! 1c = level 12 (zs) interrupt
                    927:        HARDINT4M(13)                   ! 1d = level 13 interrupt
                    928:        HARDINT4M(14)                   ! 1e = level 14 interrupt
                    929:        VTRAP(15, nmi_sun4m)            ! 1f = nonmaskable interrupt
1.1       deraadt   930:        UTRAP(0x20)
                    931:        UTRAP(0x21)
                    932:        UTRAP(0x22)
                    933:        UTRAP(0x23)
1.25      deraadt   934:        TRAP(T_CPDISABLED)      ! 24 = coprocessor instr, EC bit off in psr
1.1       deraadt   935:        UTRAP(0x25)
                    936:        UTRAP(0x26)
                    937:        UTRAP(0x27)
1.25      deraadt   938:        TRAP(T_CPEXCEPTION)     ! 28 = coprocessor exception
1.1       deraadt   939:        UTRAP(0x29)
                    940:        UTRAP(0x2a)
1.52      pk        941:        VTRAP(T_STOREBUFFAULT, memfault_sun4m) ! 2b = SuperSPARC store buffer fault
1.1       deraadt   942:        UTRAP(0x2c)
                    943:        UTRAP(0x2d)
                    944:        UTRAP(0x2e)
                    945:        UTRAP(0x2f)
                    946:        UTRAP(0x30)
                    947:        UTRAP(0x31)
                    948:        UTRAP(0x32)
                    949:        UTRAP(0x33)
                    950:        UTRAP(0x34)
                    951:        UTRAP(0x35)
1.25      deraadt   952:        UTRAP(0x36)
1.1       deraadt   953:        UTRAP(0x37)
                    954:        UTRAP(0x38)
                    955:        UTRAP(0x39)
                    956:        UTRAP(0x3a)
                    957:        UTRAP(0x3b)
                    958:        UTRAP(0x3c)
                    959:        UTRAP(0x3d)
                    960:        UTRAP(0x3e)
                    961:        UTRAP(0x3f)
1.25      deraadt   962:        UTRAP(0x40)
1.1       deraadt   963:        UTRAP(0x41)
                    964:        UTRAP(0x42)
                    965:        UTRAP(0x43)
                    966:        UTRAP(0x44)
                    967:        UTRAP(0x45)
                    968:        UTRAP(0x46)
                    969:        UTRAP(0x47)
                    970:        UTRAP(0x48)
                    971:        UTRAP(0x49)
                    972:        UTRAP(0x4a)
                    973:        UTRAP(0x4b)
                    974:        UTRAP(0x4c)
                    975:        UTRAP(0x4d)
                    976:        UTRAP(0x4e)
                    977:        UTRAP(0x4f)
                    978:        UTRAP(0x50)
                    979:        UTRAP(0x51)
                    980:        UTRAP(0x52)
                    981:        UTRAP(0x53)
                    982:        UTRAP(0x54)
                    983:        UTRAP(0x55)
                    984:        UTRAP(0x56)
                    985:        UTRAP(0x57)
                    986:        UTRAP(0x58)
                    987:        UTRAP(0x59)
                    988:        UTRAP(0x5a)
                    989:        UTRAP(0x5b)
                    990:        UTRAP(0x5c)
                    991:        UTRAP(0x5d)
                    992:        UTRAP(0x5e)
                    993:        UTRAP(0x5f)
                    994:        UTRAP(0x60)
                    995:        UTRAP(0x61)
                    996:        UTRAP(0x62)
                    997:        UTRAP(0x63)
                    998:        UTRAP(0x64)
                    999:        UTRAP(0x65)
                   1000:        UTRAP(0x66)
                   1001:        UTRAP(0x67)
                   1002:        UTRAP(0x68)
                   1003:        UTRAP(0x69)
                   1004:        UTRAP(0x6a)
                   1005:        UTRAP(0x6b)
                   1006:        UTRAP(0x6c)
                   1007:        UTRAP(0x6d)
                   1008:        UTRAP(0x6e)
                   1009:        UTRAP(0x6f)
                   1010:        UTRAP(0x70)
                   1011:        UTRAP(0x71)
                   1012:        UTRAP(0x72)
                   1013:        UTRAP(0x73)
                   1014:        UTRAP(0x74)
                   1015:        UTRAP(0x75)
                   1016:        UTRAP(0x76)
                   1017:        UTRAP(0x77)
                   1018:        UTRAP(0x78)
                   1019:        UTRAP(0x79)
                   1020:        UTRAP(0x7a)
                   1021:        UTRAP(0x7b)
                   1022:        UTRAP(0x7c)
                   1023:        UTRAP(0x7d)
                   1024:        UTRAP(0x7e)
                   1025:        UTRAP(0x7f)
1.3       deraadt  1026:        SYSCALL                 ! 80 = sun syscall
1.1       deraadt  1027:        BPT                     ! 81 = pseudo breakpoint instruction
                   1028:        TRAP(T_DIV0)            ! 82 = divide by zero
                   1029:        TRAP(T_FLUSHWIN)        ! 83 = flush windows
                   1030:        TRAP(T_CLEANWIN)        ! 84 = provide clean windows
                   1031:        TRAP(T_RANGECHECK)      ! 85 = ???
                   1032:        TRAP(T_FIXALIGN)        ! 86 = fix up unaligned accesses
                   1033:        TRAP(T_INTOF)           ! 87 = integer overflow
1.33      christos 1034:        SYSCALL                 ! 88 = svr4 syscall
1.1       deraadt  1035:        SYSCALL                 ! 89 = bsd syscall
1.33      christos 1036:        BPT_KGDB_EXEC           ! 8a = enter kernel gdb on kernel startup
1.1       deraadt  1037:        STRAP(0x8b)
                   1038:        STRAP(0x8c)
                   1039:        STRAP(0x8d)
                   1040:        STRAP(0x8e)
                   1041:        STRAP(0x8f)
                   1042:        STRAP(0x90)
                   1043:        STRAP(0x91)
                   1044:        STRAP(0x92)
                   1045:        STRAP(0x93)
                   1046:        STRAP(0x94)
                   1047:        STRAP(0x95)
                   1048:        STRAP(0x96)
                   1049:        STRAP(0x97)
                   1050:        STRAP(0x98)
                   1051:        STRAP(0x99)
                   1052:        STRAP(0x9a)
                   1053:        STRAP(0x9b)
                   1054:        STRAP(0x9c)
                   1055:        STRAP(0x9d)
                   1056:        STRAP(0x9e)
                   1057:        STRAP(0x9f)
                   1058:        STRAP(0xa0)
                   1059:        STRAP(0xa1)
                   1060:        STRAP(0xa2)
                   1061:        STRAP(0xa3)
                   1062:        STRAP(0xa4)
                   1063:        STRAP(0xa5)
                   1064:        STRAP(0xa6)
                   1065:        STRAP(0xa7)
                   1066:        STRAP(0xa8)
                   1067:        STRAP(0xa9)
                   1068:        STRAP(0xaa)
                   1069:        STRAP(0xab)
                   1070:        STRAP(0xac)
                   1071:        STRAP(0xad)
                   1072:        STRAP(0xae)
                   1073:        STRAP(0xaf)
                   1074:        STRAP(0xb0)
                   1075:        STRAP(0xb1)
                   1076:        STRAP(0xb2)
                   1077:        STRAP(0xb3)
                   1078:        STRAP(0xb4)
                   1079:        STRAP(0xb5)
                   1080:        STRAP(0xb6)
                   1081:        STRAP(0xb7)
                   1082:        STRAP(0xb8)
                   1083:        STRAP(0xb9)
                   1084:        STRAP(0xba)
                   1085:        STRAP(0xbb)
                   1086:        STRAP(0xbc)
                   1087:        STRAP(0xbd)
                   1088:        STRAP(0xbe)
                   1089:        STRAP(0xbf)
                   1090:        STRAP(0xc0)
                   1091:        STRAP(0xc1)
                   1092:        STRAP(0xc2)
                   1093:        STRAP(0xc3)
                   1094:        STRAP(0xc4)
                   1095:        STRAP(0xc5)
                   1096:        STRAP(0xc6)
                   1097:        STRAP(0xc7)
                   1098:        STRAP(0xc8)
                   1099:        STRAP(0xc9)
                   1100:        STRAP(0xca)
                   1101:        STRAP(0xcb)
                   1102:        STRAP(0xcc)
                   1103:        STRAP(0xcd)
                   1104:        STRAP(0xce)
                   1105:        STRAP(0xcf)
                   1106:        STRAP(0xd0)
                   1107:        STRAP(0xd1)
                   1108:        STRAP(0xd2)
                   1109:        STRAP(0xd3)
                   1110:        STRAP(0xd4)
                   1111:        STRAP(0xd5)
                   1112:        STRAP(0xd6)
                   1113:        STRAP(0xd7)
                   1114:        STRAP(0xd8)
                   1115:        STRAP(0xd9)
                   1116:        STRAP(0xda)
                   1117:        STRAP(0xdb)
                   1118:        STRAP(0xdc)
                   1119:        STRAP(0xdd)
                   1120:        STRAP(0xde)
                   1121:        STRAP(0xdf)
                   1122:        STRAP(0xe0)
                   1123:        STRAP(0xe1)
                   1124:        STRAP(0xe2)
                   1125:        STRAP(0xe3)
                   1126:        STRAP(0xe4)
                   1127:        STRAP(0xe5)
                   1128:        STRAP(0xe6)
                   1129:        STRAP(0xe7)
                   1130:        STRAP(0xe8)
                   1131:        STRAP(0xe9)
                   1132:        STRAP(0xea)
                   1133:        STRAP(0xeb)
                   1134:        STRAP(0xec)
                   1135:        STRAP(0xed)
                   1136:        STRAP(0xee)
                   1137:        STRAP(0xef)
                   1138:        STRAP(0xf0)
                   1139:        STRAP(0xf1)
                   1140:        STRAP(0xf2)
                   1141:        STRAP(0xf3)
                   1142:        STRAP(0xf4)
                   1143:        STRAP(0xf5)
                   1144:        STRAP(0xf6)
                   1145:        STRAP(0xf7)
                   1146:        STRAP(0xf8)
                   1147:        STRAP(0xf9)
                   1148:        STRAP(0xfa)
                   1149:        STRAP(0xfb)
                   1150:        STRAP(0xfc)
                   1151:        STRAP(0xfd)
                   1152:        STRAP(0xfe)
                   1153:        STRAP(0xff)
1.52      pk       1154: #endif
1.1       deraadt  1155:
1.20      deraadt  1156: /*
1.52      pk       1157:  * Pad the trap table to max page size.
                   1158:  * Trap table size is 0x100 * 4instr * 4byte/instr = 4096 bytes;
                   1159:  * need to .skip 4096 to pad to page size iff. the number of trap tables
                   1160:  * defined above is odd.
1.20      deraadt  1161:  */
1.65      mycroft  1162: #if (defined(SUN4) + defined(SUN4C) + defined(SUN4M)) % 2 == 1
1.20      deraadt  1163:        .skip   4096
1.52      pk       1164: #endif
1.20      deraadt  1165:
1.98      pk       1166: #ifdef DEBUG
1.1       deraadt  1167: /*
                   1168:  * A hardware red zone is impossible.  We simulate one in software by
                   1169:  * keeping a `red zone' pointer; if %sp becomes less than this, we panic.
                   1170:  * This is expensive and is only enabled when debugging.
                   1171:  */
1.97      pk       1172:
1.99      pk       1173: /* `redzone' is located in the per-CPU information structure */
1.97      pk       1174: _redzone = CPUINFO_VA + CPUINFO_REDZONE
                   1175:        .data
1.1       deraadt  1176: #define        REDSTACK 2048           /* size of `panic: stack overflow' region */
                   1177: _redstack:
                   1178:        .skip   REDSTACK
                   1179:        .text
                   1180: Lpanic_red:
                   1181:        .asciz  "stack overflow"
1.52      pk       1182:        _ALIGN
1.1       deraadt  1183:
                   1184:        /* set stack pointer redzone to base+minstack; alters base */
                   1185: #define        SET_SP_REDZONE(base, tmp) \
                   1186:        add     base, REDSIZE, base; \
                   1187:        sethi   %hi(_redzone), tmp; \
                   1188:        st      base, [tmp + %lo(_redzone)]
                   1189:
                   1190:        /* variant with a constant */
                   1191: #define        SET_SP_REDZONE_CONST(const, tmp1, tmp2) \
                   1192:        set     (const) + REDSIZE, tmp1; \
                   1193:        sethi   %hi(_redzone), tmp2; \
                   1194:        st      tmp1, [tmp2 + %lo(_redzone)]
                   1195:
1.97      pk       1196:        /* variant with a variable & offset */
                   1197: #define        SET_SP_REDZONE_VAR(var, offset, tmp1, tmp2) \
                   1198:        sethi   %hi(var), tmp1; \
                   1199:        ld      [tmp1 + %lo(var)], tmp1; \
                   1200:        sethi   %hi(offset), tmp2; \
                   1201:        add     tmp1, tmp2, tmp1; \
                   1202:        SET_SP_REDZONE(tmp1, tmp2)
                   1203:
1.1       deraadt  1204:        /* check stack pointer against redzone (uses two temps) */
                   1205: #define        CHECK_SP_REDZONE(t1, t2) \
                   1206:        sethi   %hi(_redzone), t1; \
                   1207:        ld      [t1 + %lo(_redzone)], t2; \
                   1208:        cmp     %sp, t2;        /* if sp >= t2, not in red zone */ \
                   1209:        bgeu    7f; nop;        /* and can continue normally */ \
                   1210:        /* move to panic stack */ \
                   1211:        st      %g0, [t1 + %lo(_redzone)]; \
                   1212:        set     _redstack + REDSTACK - 96, %sp; \
                   1213:        /* prevent panic() from lowering ipl */ \
1.121     christos 1214:        sethi   %hi(_C_LABEL(panicstr)), t2; \
1.1       deraadt  1215:        set     Lpanic_red, t2; \
1.121     christos 1216:        st      t2, [t1 + %lo(_C_LABEL(panicstr))]; \
1.1       deraadt  1217:        rd      %psr, t1;               /* t1 = splhigh() */ \
                   1218:        or      t1, PSR_PIL, t2; \
                   1219:        wr      t2, 0, %psr; \
                   1220:        wr      t2, PSR_ET, %psr;       /* turn on traps */ \
                   1221:        nop; nop; nop; \
1.4       deraadt  1222:        save    %sp, -CCFSZ, %sp;       /* preserve current window */ \
1.1       deraadt  1223:        sethi   %hi(Lpanic_red), %o0; \
1.121     christos 1224:        call    _C_LABEL(panic); or %o0, %lo(Lpanic_red), %o0; \
1.1       deraadt  1225: 7:
                   1226:
                   1227: #else
                   1228:
                   1229: #define        SET_SP_REDZONE(base, tmp)
                   1230: #define        SET_SP_REDZONE_CONST(const, t1, t2)
1.98      pk       1231: #define        SET_SP_REDZONE_VAR(var, offset, t1, t2)
1.1       deraadt  1232: #define        CHECK_SP_REDZONE(t1, t2)
1.97      pk       1233: #endif /* DEBUG */
1.1       deraadt  1234:
                   1235: /*
                   1236:  * The window code must verify user stack addresses before using them.
                   1237:  * A user stack pointer is invalid if:
                   1238:  *     - it is not on an 8 byte boundary;
                   1239:  *     - its pages (a register window, being 64 bytes, can occupy
                   1240:  *       two pages) are not readable or writable.
                   1241:  * We define three separate macros here for testing user stack addresses.
                   1242:  *
                   1243:  * PTE_OF_ADDR locates a PTE, branching to a `bad address'
                   1244:  *     handler if the stack pointer points into the hole in the
                   1245:  *     address space (i.e., top 3 bits are not either all 1 or all 0);
                   1246:  * CMP_PTE_USER_READ compares the located PTE against `user read' mode;
                   1247:  * CMP_PTE_USER_WRITE compares the located PTE against `user write' mode.
                   1248:  * The compares give `equal' if read or write is OK.
                   1249:  *
                   1250:  * Note that the user stack pointer usually points into high addresses
                   1251:  * (top 3 bits all 1), so that is what we check first.
                   1252:  *
                   1253:  * The code below also assumes that PTE_OF_ADDR is safe in a delay
                   1254:  * slot; it is, at it merely sets its `pte' register to a temporary value.
                   1255:  */
1.52      pk       1256: #if defined(SUN4) || defined(SUN4C)
1.1       deraadt  1257:        /* input: addr, output: pte; aux: bad address label */
1.52      pk       1258: #define        PTE_OF_ADDR4_4C(addr, pte, bad, page_offset) \
1.1       deraadt  1259:        sra     addr, PG_VSHIFT, pte; \
                   1260:        cmp     pte, -1; \
1.13      deraadt  1261:        be,a    1f; andn addr, page_offset, pte; \
1.1       deraadt  1262:        tst     pte; \
                   1263:        bne     bad; EMPTY; \
1.13      deraadt  1264:        andn    addr, page_offset, pte; \
1.1       deraadt  1265: 1:
                   1266:
                   1267:        /* input: pte; output: condition codes */
1.52      pk       1268: #define        CMP_PTE_USER_READ4_4C(pte) \
1.1       deraadt  1269:        lda     [pte] ASI_PTE, pte; \
                   1270:        srl     pte, PG_PROTSHIFT, pte; \
                   1271:        andn    pte, (PG_W >> PG_PROTSHIFT), pte; \
                   1272:        cmp     pte, PG_PROTUREAD
                   1273:
                   1274:        /* input: pte; output: condition codes */
1.52      pk       1275: #define        CMP_PTE_USER_WRITE4_4C(pte) \
1.1       deraadt  1276:        lda     [pte] ASI_PTE, pte; \
                   1277:        srl     pte, PG_PROTSHIFT, pte; \
                   1278:        cmp     pte, PG_PROTUWRITE
1.9       deraadt  1279: #endif
1.1       deraadt  1280:
                   1281: /*
1.52      pk       1282:  * The Sun4M does not have the memory hole that the 4C does. Thus all
                   1283:  * we need to do here is clear the page offset from addr.
                   1284:  */
                   1285: #if defined(SUN4M)
                   1286: #define        PTE_OF_ADDR4M(addr, pte, bad, page_offset) \
                   1287:        andn    addr, page_offset, pte
                   1288:
1.94      pk       1289: /*
                   1290:  * After obtaining the PTE through ASI_SRMMUFP, we read the Sync Fault
                   1291:  * Status register. This is necessary on Hypersparcs which stores and
                   1292:  * locks the fault address and status registers if the translation
                   1293:  * fails (thanks to Chris Torek for finding this quirk).
                   1294:  */
1.59      pk       1295: /* note: pmap currently does not use the PPROT_R_R and PPROT_RW_RW cases */
1.94      pk       1296: #define CMP_PTE_USER_READ4M(pte, tmp) \
1.52      pk       1297:        or      pte, ASI_SRMMUFP_L3, pte; \
                   1298:        lda     [pte] ASI_SRMMUFP, pte; \
1.94      pk       1299:        set     SRMMU_SFSR, tmp; \
1.58      pk       1300:        and     pte, (SRMMU_TETYPE | SRMMU_PROT_MASK), pte; \
1.59      pk       1301:        cmp     pte, (SRMMU_TEPTE | PPROT_RWX_RWX); \
1.94      pk       1302:        be      8f; \
                   1303:         lda    [tmp] ASI_SRMMU, %g0; \
1.59      pk       1304:        cmp     pte, (SRMMU_TEPTE | PPROT_RX_RX); \
                   1305: 8:
1.52      pk       1306:
1.58      pk       1307:
                   1308: /* note: PTE bit 4 set implies no user writes */
1.94      pk       1309: #define CMP_PTE_USER_WRITE4M(pte, tmp) \
1.52      pk       1310:        or      pte, ASI_SRMMUFP_L3, pte; \
                   1311:        lda     [pte] ASI_SRMMUFP, pte; \
1.94      pk       1312:        set     SRMMU_SFSR, tmp; \
                   1313:        lda     [tmp] ASI_SRMMU, %g0; \
1.58      pk       1314:        and     pte, (SRMMU_TETYPE | 0x14), pte; \
                   1315:        cmp     pte, (SRMMU_TEPTE | PPROT_WRITE)
1.52      pk       1316: #endif /* 4m */
                   1317:
                   1318: #if defined(SUN4M) && !(defined(SUN4C) || defined(SUN4))
1.64      pk       1319:
1.62      pk       1320: #define PTE_OF_ADDR(addr, pte, bad, page_offset, label) \
                   1321:        PTE_OF_ADDR4M(addr, pte, bad, page_offset)
1.94      pk       1322: #define CMP_PTE_USER_WRITE(pte, tmp, label)    CMP_PTE_USER_WRITE4M(pte,tmp)
                   1323: #define CMP_PTE_USER_READ(pte, tmp, label)     CMP_PTE_USER_READ4M(pte,tmp)
1.64      pk       1324:
1.52      pk       1325: #elif (defined(SUN4C) || defined(SUN4)) && !defined(SUN4M)
1.64      pk       1326:
1.62      pk       1327: #define PTE_OF_ADDR(addr, pte, bad, page_offset,label) \
                   1328:        PTE_OF_ADDR4_4C(addr, pte, bad, page_offset)
                   1329: #define CMP_PTE_USER_WRITE(pte, tmp, label)    CMP_PTE_USER_WRITE4_4C(pte)
                   1330: #define CMP_PTE_USER_READ(pte, tmp, label)     CMP_PTE_USER_READ4_4C(pte)
1.64      pk       1331:
1.52      pk       1332: #else /* both defined, ugh */
1.64      pk       1333:
1.62      pk       1334: #define        PTE_OF_ADDR(addr, pte, bad, page_offset, label) \
                   1335: label: b,a     2f; \
                   1336:        PTE_OF_ADDR4M(addr, pte, bad, page_offset); \
                   1337:        b,a     3f; \
                   1338: 2: \
                   1339:        PTE_OF_ADDR4_4C(addr, pte, bad, page_offset); \
                   1340: 3:
1.52      pk       1341:
1.62      pk       1342: #define CMP_PTE_USER_READ(pte, tmp, label) \
                   1343: label: b,a     1f; \
1.94      pk       1344:        CMP_PTE_USER_READ4M(pte,tmp); \
1.62      pk       1345:        b,a     2f; \
                   1346: 1: \
                   1347:        CMP_PTE_USER_READ4_4C(pte); \
                   1348: 2:
1.52      pk       1349:
1.62      pk       1350: #define CMP_PTE_USER_WRITE(pte, tmp, label) \
                   1351: label: b,a     1f; \
1.94      pk       1352:        CMP_PTE_USER_WRITE4M(pte,tmp); \
1.62      pk       1353:        b,a     2f; \
                   1354: 1: \
                   1355:        CMP_PTE_USER_WRITE4_4C(pte); \
                   1356: 2:
1.52      pk       1357: #endif
                   1358:
                   1359:
                   1360: /*
1.1       deraadt  1361:  * The calculations in PTE_OF_ADDR and CMP_PTE_USER_* are rather slow:
                   1362:  * in particular, according to Gordon Irlam of the University of Adelaide
                   1363:  * in Australia, these consume at least 18 cycles on an SS1 and 37 on an
                   1364:  * SS2.  Hence, we try to avoid them in the common case.
                   1365:  *
                   1366:  * A chunk of 64 bytes is on a single page if and only if:
                   1367:  *
1.13      deraadt  1368:  *     ((base + 64 - 1) & ~(NBPG-1)) == (base & ~(NBPG-1))
1.1       deraadt  1369:  *
                   1370:  * Equivalently (and faster to test), the low order bits (base & 4095) must
                   1371:  * be small enough so that the sum (base + 63) does not carry out into the
                   1372:  * upper page-address bits, i.e.,
                   1373:  *
1.13      deraadt  1374:  *     (base & (NBPG-1)) < (NBPG - 63)
1.1       deraadt  1375:  *
                   1376:  * so we allow testing that here.  This macro is also assumed to be safe
                   1377:  * in a delay slot (modulo overwriting its temporary).
                   1378:  */
1.13      deraadt  1379: #define        SLT_IF_1PAGE_RW(addr, tmp, page_offset) \
                   1380:        and     addr, page_offset, tmp; \
                   1381:        sub     page_offset, 62, page_offset; \
                   1382:        cmp     tmp, page_offset
1.1       deraadt  1383:
                   1384: /*
                   1385:  * Every trap that enables traps must set up stack space.
                   1386:  * If the trap is from user mode, this involves switching to the kernel
                   1387:  * stack for the current process, and we must also set cpcb->pcb_uw
                   1388:  * so that the window overflow handler can tell user windows from kernel
                   1389:  * windows.
                   1390:  *
                   1391:  * The number of user windows is:
                   1392:  *
                   1393:  *     cpcb->pcb_uw = (cpcb->pcb_wim - 1 - CWP) % nwindows
                   1394:  *
                   1395:  * (where pcb_wim = log2(current %wim) and CWP = low 5 bits of %psr).
                   1396:  * We compute this expression by table lookup in uwtab[CWP - pcb_wim],
                   1397:  * which has been set up as:
                   1398:  *
                   1399:  *     for i in [-nwin+1 .. nwin-1]
                   1400:  *             uwtab[i] = (nwin - 1 - i) % nwin;
                   1401:  *
                   1402:  * (If you do not believe this works, try it for yourself.)
                   1403:  *
                   1404:  * We also keep one or two more tables:
                   1405:  *
                   1406:  *     for i in 0..nwin-1
                   1407:  *             wmask[i] = 1 << ((i + 1) % nwindows);
                   1408:  *
                   1409:  * wmask[CWP] tells whether a `rett' would return into the invalid window.
                   1410:  */
                   1411:        .data
                   1412:        .skip   32                      ! alignment byte & negative indicies
                   1413: uwtab: .skip   32                      ! u_char uwtab[-31..31];
                   1414: wmask: .skip   32                      ! u_char wmask[0..31];
                   1415:
                   1416:        .text
                   1417: /*
                   1418:  * Things begin to grow uglier....
                   1419:  *
                   1420:  * Each trap handler may (always) be running in the trap window.
                   1421:  * If this is the case, it cannot enable further traps until it writes
                   1422:  * the register windows into the stack (or, if the stack is no good,
                   1423:  * the current pcb).
                   1424:  *
                   1425:  * ASSUMPTIONS: TRAP_SETUP() is called with:
                   1426:  *     %l0 = %psr
                   1427:  *     %l1 = return pc
                   1428:  *     %l2 = return npc
                   1429:  *     %l3 = (some value that must not be altered)
                   1430:  * which means we have 4 registers to work with.
                   1431:  *
                   1432:  * The `stackspace' argument is the number of stack bytes to allocate
                   1433:  * for register-saving, and must be at least -64 (and typically more,
                   1434:  * for global registers and %y).
                   1435:  *
                   1436:  * Trapframes should use -CCFSZ-80.  (80 = sizeof(struct trapframe);
                   1437:  * see trap.h.  This basically means EVERYONE.  Interrupt frames could
                   1438:  * get away with less, but currently do not.)
                   1439:  *
                   1440:  * The basic outline here is:
                   1441:  *
                   1442:  *     if (trap came from kernel mode) {
                   1443:  *             if (we are in the trap window)
                   1444:  *                     save it away;
                   1445:  *             %sp = %fp - stackspace;
                   1446:  *     } else {
                   1447:  *             compute the number of user windows;
                   1448:  *             if (we are in the trap window)
                   1449:  *                     save it away;
                   1450:  *             %sp = (top of kernel stack) - stackspace;
                   1451:  *     }
                   1452:  *
                   1453:  * Again, the number of user windows is:
                   1454:  *
                   1455:  *     cpcb->pcb_uw = (cpcb->pcb_wim - 1 - CWP) % nwindows
                   1456:  *
                   1457:  * (where pcb_wim = log2(current %wim) and CWP is the low 5 bits of %psr),
                   1458:  * and this is computed as `uwtab[CWP - pcb_wim]'.
                   1459:  *
                   1460:  * NOTE: if you change this code, you will have to look carefully
                   1461:  * at the window overflow and underflow handlers and make sure they
                   1462:  * have similar changes made as needed.
                   1463:  */
                   1464: #define        CALL_CLEAN_TRAP_WINDOW \
                   1465:        sethi   %hi(clean_trap_window), %l7; \
                   1466:        jmpl    %l7 + %lo(clean_trap_window), %l4; \
                   1467:         mov    %g7, %l7        /* save %g7 in %l7 for clean_trap_window */
                   1468:
                   1469: #define        TRAP_SETUP(stackspace) \
                   1470:        rd      %wim, %l4; \
                   1471:        mov     1, %l5; \
                   1472:        sll     %l5, %l0, %l5; \
                   1473:        btst    PSR_PS, %l0; \
                   1474:        bz      1f; \
                   1475:         btst   %l5, %l4; \
                   1476:        /* came from kernel mode; cond codes indicate trap window */ \
                   1477:        bz,a    3f; \
                   1478:         add    %fp, stackspace, %sp;   /* want to just set %sp */ \
                   1479:        CALL_CLEAN_TRAP_WINDOW;         /* but maybe need to clean first */ \
                   1480:        b       3f; \
                   1481:         add    %fp, stackspace, %sp; \
                   1482: 1: \
                   1483:        /* came from user mode: compute pcb_nw */ \
1.111     pk       1484:        sethi   %hi(cpcb), %l6; \
                   1485:        ld      [%l6 + %lo(cpcb)], %l6; \
1.1       deraadt  1486:        ld      [%l6 + PCB_WIM], %l5; \
                   1487:        and     %l0, 31, %l4; \
                   1488:        sub     %l4, %l5, %l5; \
                   1489:        set     uwtab, %l4; \
                   1490:        ldub    [%l4 + %l5], %l5; \
                   1491:        st      %l5, [%l6 + PCB_UW]; \
                   1492:        /* cond codes still indicate whether in trap window */ \
                   1493:        bz,a    2f; \
1.13      deraadt  1494:         sethi  %hi(USPACE+(stackspace)), %l5; \
1.1       deraadt  1495:        /* yes, in trap window; must clean it */ \
                   1496:        CALL_CLEAN_TRAP_WINDOW; \
1.111     pk       1497:        sethi   %hi(cpcb), %l6; \
                   1498:        ld      [%l6 + %lo(cpcb)], %l6; \
1.13      deraadt  1499:        sethi   %hi(USPACE+(stackspace)), %l5; \
1.1       deraadt  1500: 2: \
                   1501:        /* trap window is (now) clean: set %sp */ \
1.13      deraadt  1502:        or      %l5, %lo(USPACE+(stackspace)), %l5; \
1.1       deraadt  1503:        add     %l6, %l5, %sp; \
                   1504:        SET_SP_REDZONE(%l6, %l5); \
                   1505: 3: \
                   1506:        CHECK_SP_REDZONE(%l6, %l5)
                   1507:
                   1508: /*
                   1509:  * Interrupt setup is almost exactly like trap setup, but we need to
                   1510:  * go to the interrupt stack if (a) we came from user mode or (b) we
                   1511:  * came from kernel mode on the kernel stack.
                   1512:  */
1.97      pk       1513: #ifdef MULTIPROCESSOR
1.98      pk       1514: /*
                   1515:  * SMP kernels: read `eintstack' from cpuinfo structure. Since the
                   1516:  * location of the interrupt stack is not known in advance, we need
                   1517:  * to check the current %fp against both ends of the stack space.
                   1518:  */
1.97      pk       1519: #define        INTR_SETUP(stackspace) \
                   1520:        rd      %wim, %l4; \
                   1521:        mov     1, %l5; \
                   1522:        sll     %l5, %l0, %l5; \
                   1523:        btst    PSR_PS, %l0; \
                   1524:        bz      1f; \
                   1525:         btst   %l5, %l4; \
                   1526:        /* came from kernel mode; cond codes still indicate trap window */ \
                   1527:        bz,a    0f; \
1.101     pk       1528:         sethi  %hi(_EINTSTACKP), %l7; \
1.97      pk       1529:        CALL_CLEAN_TRAP_WINDOW; \
1.101     pk       1530:        sethi   %hi(_EINTSTACKP), %l7; \
1.97      pk       1531: 0:     /* now if not intstack > %fp >= eintstack, we were on the kernel stack */ \
1.101     pk       1532:        ld      [%l7 + %lo(_EINTSTACKP)], %l7; \
1.97      pk       1533:        cmp     %fp, %l7; \
                   1534:        bge,a   3f;                     /* %fp >= eintstack */ \
                   1535:         add    %l7, stackspace, %sp;   /* so switch to intstack */ \
                   1536:        sethi   %hi(INT_STACK_SIZE), %l6; \
1.98      pk       1537:        sub     %l7, %l6, %l6; \
                   1538:        cmp     %fp, %l6; \
1.97      pk       1539:        blu,a   3f;                     /* %fp < intstack */ \
                   1540:         add    %l7, stackspace, %sp;   /* so switch to intstack */ \
                   1541:        b       4f; \
                   1542:         add    %fp, stackspace, %sp;   /* else stay on intstack */ \
                   1543: 1: \
                   1544:        /* came from user mode: compute pcb_nw */ \
1.111     pk       1545:        sethi   %hi(cpcb), %l6; \
                   1546:        ld      [%l6 + %lo(cpcb)], %l6; \
1.97      pk       1547:        ld      [%l6 + PCB_WIM], %l5; \
                   1548:        and     %l0, 31, %l4; \
                   1549:        sub     %l4, %l5, %l5; \
                   1550:        set     uwtab, %l4; \
                   1551:        ldub    [%l4 + %l5], %l5; \
                   1552:        st      %l5, [%l6 + PCB_UW]; \
                   1553:        /* cond codes still indicate whether in trap window */ \
                   1554:        bz,a    2f; \
1.101     pk       1555:         sethi  %hi(_EINTSTACKP), %l7; \
1.97      pk       1556:        /* yes, in trap window; must save regs */ \
                   1557:        CALL_CLEAN_TRAP_WINDOW; \
1.101     pk       1558:        sethi   %hi(_EINTSTACKP), %l7; \
1.97      pk       1559: 2: \
1.101     pk       1560:        ld      [%l7 + %lo(_EINTSTACKP)], %l7; \
1.97      pk       1561:        add     %l7, stackspace, %sp; \
                   1562: 3: \
1.101     pk       1563:        SET_SP_REDZONE_VAR(_EINTSTACKP, -INT_STACK_SIZE, %l6, %l5); \
1.97      pk       1564: 4: \
                   1565:        CHECK_SP_REDZONE(%l6, %l5)
1.98      pk       1566:
1.97      pk       1567: #else /* MULTIPROCESSOR */
1.98      pk       1568:
1.1       deraadt  1569: #define        INTR_SETUP(stackspace) \
                   1570:        rd      %wim, %l4; \
                   1571:        mov     1, %l5; \
                   1572:        sll     %l5, %l0, %l5; \
                   1573:        btst    PSR_PS, %l0; \
                   1574:        bz      1f; \
                   1575:         btst   %l5, %l4; \
                   1576:        /* came from kernel mode; cond codes still indicate trap window */ \
                   1577:        bz,a    0f; \
1.111     pk       1578:         sethi  %hi(_C_LABEL(eintstack)), %l7; \
1.1       deraadt  1579:        CALL_CLEAN_TRAP_WINDOW; \
1.111     pk       1580:        sethi   %hi(_C_LABEL(eintstack)), %l7; \
1.1       deraadt  1581: 0:     /* now if %fp >= eintstack, we were on the kernel stack */ \
                   1582:        cmp     %fp, %l7; \
                   1583:        bge,a   3f; \
                   1584:         add    %l7, stackspace, %sp;   /* so switch to intstack */ \
                   1585:        b       4f; \
                   1586:         add    %fp, stackspace, %sp;   /* else stay on intstack */ \
                   1587: 1: \
                   1588:        /* came from user mode: compute pcb_nw */ \
1.111     pk       1589:        sethi   %hi(cpcb), %l6; \
                   1590:        ld      [%l6 + %lo(cpcb)], %l6; \
1.1       deraadt  1591:        ld      [%l6 + PCB_WIM], %l5; \
                   1592:        and     %l0, 31, %l4; \
                   1593:        sub     %l4, %l5, %l5; \
                   1594:        set     uwtab, %l4; \
                   1595:        ldub    [%l4 + %l5], %l5; \
                   1596:        st      %l5, [%l6 + PCB_UW]; \
                   1597:        /* cond codes still indicate whether in trap window */ \
                   1598:        bz,a    2f; \
1.111     pk       1599:         sethi  %hi(_C_LABEL(eintstack)), %l7; \
1.1       deraadt  1600:        /* yes, in trap window; must save regs */ \
                   1601:        CALL_CLEAN_TRAP_WINDOW; \
1.111     pk       1602:        sethi   %hi(_C_LABEL(eintstack)), %l7; \
1.1       deraadt  1603: 2: \
                   1604:        add     %l7, stackspace, %sp; \
                   1605: 3: \
1.111     pk       1606:        SET_SP_REDZONE_CONST(_C_LABEL(intstack), %l6, %l5); \
1.1       deraadt  1607: 4: \
                   1608:        CHECK_SP_REDZONE(%l6, %l5)
1.97      pk       1609: #endif /* MULTIPROCESSOR */
1.1       deraadt  1610:
                   1611: /*
                   1612:  * Handler for making the trap window shiny clean.
                   1613:  *
                   1614:  * On entry:
                   1615:  *     cpcb->pcb_nw = number of user windows
                   1616:  *     %l0 = %psr
                   1617:  *     %l1 must not be clobbered
                   1618:  *     %l2 must not be clobbered
                   1619:  *     %l3 must not be clobbered
                   1620:  *     %l4 = address for `return'
                   1621:  *     %l7 = saved %g7 (we put this in a delay slot above, to save work)
                   1622:  *
                   1623:  * On return:
                   1624:  *     %wim has changed, along with cpcb->pcb_wim
                   1625:  *     %g7 has been restored
                   1626:  *
                   1627:  * Normally, we push only one window.
                   1628:  */
                   1629: clean_trap_window:
                   1630:        mov     %g5, %l5                ! save %g5
                   1631:        mov     %g6, %l6                ! ... and %g6
                   1632: /*     mov     %g7, %l7                ! ... and %g7 (already done for us) */
1.111     pk       1633:        sethi   %hi(cpcb), %g6          ! get current pcb
                   1634:        ld      [%g6 + %lo(cpcb)], %g6
1.1       deraadt  1635:
                   1636:        /* Figure out whether it is a user window (cpcb->pcb_uw > 0). */
                   1637:        ld      [%g6 + PCB_UW], %g7
                   1638:        deccc   %g7
                   1639:        bge     ctw_user
                   1640:         save   %g0, %g0, %g0           ! in any case, enter window to save
                   1641:
                   1642:        /* The window to be pushed is a kernel window. */
                   1643:        std     %l0, [%sp + (0*8)]
                   1644: ctw_merge:
                   1645:        std     %l2, [%sp + (1*8)]
                   1646:        std     %l4, [%sp + (2*8)]
                   1647:        std     %l6, [%sp + (3*8)]
                   1648:        std     %i0, [%sp + (4*8)]
                   1649:        std     %i2, [%sp + (5*8)]
                   1650:        std     %i4, [%sp + (6*8)]
                   1651:        std     %i6, [%sp + (7*8)]
                   1652:
                   1653:        /* Set up new window invalid mask, and update cpcb->pcb_wim. */
                   1654:        rd      %psr, %g7               ! g7 = (junk << 5) + new_cwp
                   1655:        mov     1, %g5                  ! g5 = 1 << new_cwp;
                   1656:        sll     %g5, %g7, %g5
                   1657:        wr      %g5, 0, %wim            ! setwim(g5);
                   1658:        and     %g7, 31, %g7            ! cpcb->pcb_wim = g7 & 31;
1.111     pk       1659:        sethi   %hi(cpcb), %g6          ! re-get current pcb
                   1660:        ld      [%g6 + %lo(cpcb)], %g6
1.1       deraadt  1661:        st      %g7, [%g6 + PCB_WIM]
                   1662:        nop
                   1663:        restore                         ! back to trap window
                   1664:
                   1665:        mov     %l5, %g5                ! restore g5
                   1666:        mov     %l6, %g6                ! ... and g6
                   1667:        jmp     %l4 + 8                 ! return to caller
                   1668:         mov    %l7, %g7                ! ... and g7
                   1669:        /* NOTREACHED */
                   1670:
                   1671: ctw_user:
                   1672:        /*
                   1673:         * The window to be pushed is a user window.
                   1674:         * We must verify the stack pointer (alignment & permissions).
                   1675:         * See comments above definition of PTE_OF_ADDR.
                   1676:         */
                   1677:        st      %g7, [%g6 + PCB_UW]     ! cpcb->pcb_uw--;
                   1678:        btst    7, %sp                  ! if not aligned,
                   1679:        bne     ctw_invalid             ! choke on it
                   1680:         EMPTY
1.13      deraadt  1681:
1.111     pk       1682:        sethi   %hi(_C_LABEL(pgofset)), %g6     ! trash %g6=curpcb
                   1683:        ld      [%g6 + %lo(_C_LABEL(pgofset))], %g6
1.62      pk       1684:        PTE_OF_ADDR(%sp, %g7, ctw_invalid, %g6, NOP_ON_4M_1)
                   1685:        CMP_PTE_USER_WRITE(%g7, %g5, NOP_ON_4M_2) ! likewise if not writable
1.1       deraadt  1686:        bne     ctw_invalid
                   1687:         EMPTY
1.52      pk       1688:        /* Note side-effect of SLT_IF_1PAGE_RW: decrements %g6 by 62 */
1.13      deraadt  1689:        SLT_IF_1PAGE_RW(%sp, %g7, %g6)
1.1       deraadt  1690:        bl,a    ctw_merge               ! all ok if only 1
                   1691:         std    %l0, [%sp]
                   1692:        add     %sp, 7*8, %g5           ! check last addr too
1.52      pk       1693:        add     %g6, 62, %g6            ! restore %g6 to `pgofset'
1.62      pk       1694:        PTE_OF_ADDR(%g5, %g7, ctw_invalid, %g6, NOP_ON_4M_3)
                   1695:        CMP_PTE_USER_WRITE(%g7, %g6, NOP_ON_4M_4)
1.1       deraadt  1696:        be,a    ctw_merge               ! all ok: store <l0,l1> and merge
                   1697:         std    %l0, [%sp]
                   1698:
                   1699:        /*
                   1700:         * The window we wanted to push could not be pushed.
                   1701:         * Instead, save ALL user windows into the pcb.
                   1702:         * We will notice later that we did this, when we
                   1703:         * get ready to return from our trap or syscall.
                   1704:         *
                   1705:         * The code here is run rarely and need not be optimal.
                   1706:         */
                   1707: ctw_invalid:
                   1708:        /*
                   1709:         * Reread cpcb->pcb_uw.  We decremented this earlier,
                   1710:         * so it is off by one.
                   1711:         */
1.111     pk       1712:        sethi   %hi(cpcb), %g6          ! re-get current pcb
                   1713:        ld      [%g6 + %lo(cpcb)], %g6
1.13      deraadt  1714:
1.1       deraadt  1715:        ld      [%g6 + PCB_UW], %g7     ! (number of user windows) - 1
                   1716:        add     %g6, PCB_RW, %g5
                   1717:
                   1718:        /* save g7+1 windows, starting with the current one */
                   1719: 1:                                     ! do {
                   1720:        std     %l0, [%g5 + (0*8)]      !       rw->rw_local[0] = l0;
                   1721:        std     %l2, [%g5 + (1*8)]      !       ...
                   1722:        std     %l4, [%g5 + (2*8)]
                   1723:        std     %l6, [%g5 + (3*8)]
                   1724:        std     %i0, [%g5 + (4*8)]
                   1725:        std     %i2, [%g5 + (5*8)]
                   1726:        std     %i4, [%g5 + (6*8)]
                   1727:        std     %i6, [%g5 + (7*8)]
                   1728:        deccc   %g7                     !       if (n > 0) save(), rw++;
                   1729:        bge,a   1b                      ! } while (--n >= 0);
                   1730:         save   %g5, 64, %g5
                   1731:
                   1732:        /* stash sp for bottommost window */
                   1733:        st      %sp, [%g5 + 64 + (7*8)]
                   1734:
                   1735:        /* set up new wim */
                   1736:        rd      %psr, %g7               ! g7 = (junk << 5) + new_cwp;
                   1737:        mov     1, %g5                  ! g5 = 1 << new_cwp;
                   1738:        sll     %g5, %g7, %g5
                   1739:        wr      %g5, 0, %wim            ! wim = g5;
                   1740:        and     %g7, 31, %g7
                   1741:        st      %g7, [%g6 + PCB_WIM]    ! cpcb->pcb_wim = new_cwp;
                   1742:
                   1743:        /* fix up pcb fields */
                   1744:        ld      [%g6 + PCB_UW], %g7     ! n = cpcb->pcb_uw;
                   1745:        add     %g7, 1, %g5
                   1746:        st      %g5, [%g6 + PCB_NSAVED] ! cpcb->pcb_nsaved = n + 1;
                   1747:        st      %g0, [%g6 + PCB_UW]     ! cpcb->pcb_uw = 0;
                   1748:
                   1749:        /* return to trap window */
                   1750: 1:     deccc   %g7                     ! do {
                   1751:        bge     1b                      !       restore();
                   1752:         restore                        ! } while (--n >= 0);
                   1753:
                   1754:        mov     %l5, %g5                ! restore g5, g6, & g7, and return
                   1755:        mov     %l6, %g6
                   1756:        jmp     %l4 + 8
                   1757:         mov    %l7, %g7
                   1758:        /* NOTREACHED */
                   1759:
                   1760:
                   1761: /*
                   1762:  * Each memory access (text or data) fault, from user or kernel mode,
                   1763:  * comes here.  We read the error register and figure out what has
                   1764:  * happened.
                   1765:  *
                   1766:  * This cannot be done from C code since we must not enable traps (and
                   1767:  * hence may not use the `save' instruction) until we have decided that
                   1768:  * the error is or is not an asynchronous one that showed up after a
                   1769:  * synchronous error, but which must be handled before the sync err.
                   1770:  *
                   1771:  * Most memory faults are user mode text or data faults, which can cause
                   1772:  * signal delivery or ptracing, for which we must build a full trapframe.
                   1773:  * It does not seem worthwhile to work to avoid this in the other cases,
                   1774:  * so we store all the %g registers on the stack immediately.
                   1775:  *
                   1776:  * On entry:
                   1777:  *     %l0 = %psr
                   1778:  *     %l1 = return pc
                   1779:  *     %l2 = return npc
                   1780:  *     %l3 = T_TEXTFAULT or T_DATAFAULT
                   1781:  *
                   1782:  * Internal:
                   1783:  *     %l4 = %y, until we call mem_access_fault (then onto trapframe)
                   1784:  *     %l5 = IE_reg_addr, if async mem error
                   1785:  *
                   1786:  */
1.52      pk       1787:
                   1788: #if defined(SUN4)
                   1789: memfault_sun4:
1.1       deraadt  1790:        TRAP_SETUP(-CCFSZ-80)
1.111     pk       1791:        INCR(_C_LABEL(uvmexp)+V_FAULTS) ! cnt.v_faults++ (clobbers %o0,%o1)
1.1       deraadt  1792:
                   1793:        st      %g1, [%sp + CCFSZ + 20] ! save g1
                   1794:        rd      %y, %l4                 ! save y
                   1795:
1.19      deraadt  1796:        /*
                   1797:         * registers:
                   1798:         * memerr.ctrl  = memory error control reg., error if 0x80 set
                   1799:         * memerr.vaddr = address of memory error
                   1800:         * buserr       = basically just like sun4c sync error reg but
                   1801:         *                no SER_WRITE bit (have to figure out from code).
                   1802:         */
1.111     pk       1803:        set     _C_LABEL(par_err_reg), %o0 ! memerr ctrl addr -- XXX mapped?
1.20      deraadt  1804:        ld      [%o0], %o0              ! get it
1.19      deraadt  1805:        std     %g2, [%sp + CCFSZ + 24] ! save g2, g3
                   1806:        ld      [%o0], %o1              ! memerr ctrl register
                   1807:        inc     4, %o0                  ! now VA of memerr vaddr register
                   1808:        std     %g4, [%sp + CCFSZ + 32] ! (sneak g4,g5 in here)
                   1809:        ld      [%o0], %o2              ! memerr virt addr
                   1810:        st      %g0, [%o0]              ! NOTE: this clears latching!!!
                   1811:        btst    ME_REG_IERR, %o1        ! memory error?
                   1812:                                        ! XXX this value may not be correct
                   1813:                                        ! as I got some parity errors and the
                   1814:                                        ! correct bits were not on?
                   1815:        std     %g6, [%sp + CCFSZ + 40]
1.52      pk       1816:        bz,a    0f                      ! no, just a regular fault
1.19      deraadt  1817:         wr     %l0, PSR_ET, %psr       ! (and reenable traps)
                   1818:
                   1819:        /* memory error = death for now XXX */
                   1820:        clr     %o3
                   1821:        clr     %o4
1.111     pk       1822:        call    _C_LABEL(memerr4_4c)    ! memerr(0, ser, sva, 0, 0)
1.19      deraadt  1823:         clr    %o0
1.111     pk       1824:        call    _C_LABEL(prom_halt)
1.19      deraadt  1825:         nop
                   1826:
1.52      pk       1827: 0:
1.19      deraadt  1828:        /*
                   1829:         * have to make SUN4 emulate SUN4C.   4C code expects
                   1830:         * SER in %o1 and the offending VA in %o2, everything else is ok.
                   1831:         * (must figure out if SER_WRITE should be set)
                   1832:         */
                   1833:        set     AC_BUS_ERR, %o0         ! bus error register
                   1834:        cmp     %l3, T_TEXTFAULT        ! text fault always on PC
1.50      pk       1835:        be      normal_mem_fault        ! go
1.21      deraadt  1836:         lduba  [%o0] ASI_CONTROL, %o1  ! get its value
1.19      deraadt  1837:
                   1838: #define STORE_BIT 21 /* bit that indicates a store instruction for sparc */
                   1839:        ld      [%l1], %o3              ! offending instruction in %o3 [l1=pc]
                   1840:        srl     %o3, STORE_BIT, %o3     ! get load/store bit (wont fit simm13)
                   1841:        btst    1, %o3                  ! test for store operation
                   1842:
                   1843:        bz      normal_mem_fault        ! if (z) is a load (so branch)
                   1844:         sethi  %hi(SER_WRITE), %o5     ! damn SER_WRITE wont fit simm13
                   1845: !      or      %lo(SER_WRITE), %o5, %o5! not necessary since %lo is zero
                   1846:        or      %o5, %o1, %o1           ! set SER_WRITE
                   1847: #if defined(SUN4C) || defined(SUN4M)
1.52      pk       1848:        ba,a    normal_mem_fault
                   1849:         !!nop                          ! XXX make efficient later
1.19      deraadt  1850: #endif /* SUN4C || SUN4M */
                   1851: #endif /* SUN4 */
1.52      pk       1852:
                   1853: memfault_sun4c:
                   1854: #if defined(SUN4C)
                   1855:        TRAP_SETUP(-CCFSZ-80)
1.111     pk       1856:        INCR(_C_LABEL(uvmexp)+V_FAULTS) ! cnt.v_faults++ (clobbers %o0,%o1)
1.52      pk       1857:
                   1858:        st      %g1, [%sp + CCFSZ + 20] ! save g1
                   1859:        rd      %y, %l4                 ! save y
                   1860:
                   1861:        /*
                   1862:         * We know about the layout of the error registers here.
                   1863:         *      addr    reg
                   1864:         *      ----    ---
                   1865:         *      a       AC_SYNC_ERR
                   1866:         *      a+4     AC_SYNC_VA
                   1867:         *      a+8     AC_ASYNC_ERR
                   1868:         *      a+12    AC_ASYNC_VA
                   1869:         */
1.19      deraadt  1870:
1.1       deraadt  1871: #if AC_SYNC_ERR + 4 != AC_SYNC_VA || \
                   1872:     AC_SYNC_ERR + 8 != AC_ASYNC_ERR || AC_SYNC_ERR + 12 != AC_ASYNC_VA
                   1873:        help help help          ! I, I, I wanna be a lifeguard
                   1874: #endif
                   1875:        set     AC_SYNC_ERR, %o0
                   1876:        std     %g2, [%sp + CCFSZ + 24] ! save g2, g3
                   1877:        lda     [%o0] ASI_CONTROL, %o1  ! sync err reg
                   1878:        inc     4, %o0
                   1879:        std     %g4, [%sp + CCFSZ + 32] ! (sneak g4,g5 in here)
                   1880:        lda     [%o0] ASI_CONTROL, %o2  ! sync virt addr
                   1881:        btst    SER_MEMERR, %o1         ! memory error?
                   1882:        std     %g6, [%sp + CCFSZ + 40]
                   1883:        bz,a    normal_mem_fault        ! no, just a regular fault
                   1884:         wr     %l0, PSR_ET, %psr       ! (and reenable traps)
                   1885:
                   1886:        /*
                   1887:         * We got a synchronous memory error.  It could be one that
                   1888:         * happened because there were two stores in a row, and the
                   1889:         * first went into the write buffer, and the second caused this
                   1890:         * synchronous trap; so there could now be a pending async error.
                   1891:         * This is in fact the case iff the two va's differ.
                   1892:         */
                   1893:        inc     4, %o0
                   1894:        lda     [%o0] ASI_CONTROL, %o3  ! async err reg
                   1895:        inc     4, %o0
                   1896:        lda     [%o0] ASI_CONTROL, %o4  ! async virt addr
                   1897:        cmp     %o2, %o4
                   1898:        be,a    1f                      ! no, not an async err
                   1899:         wr     %l0, PSR_ET, %psr       ! (and reenable traps)
                   1900:
                   1901:        /*
                   1902:         * Handle the async error; ignore the sync error for now
                   1903:         * (we may end up getting it again, but so what?).
                   1904:         * This code is essentially the same as that at `nmi' below,
                   1905:         * but the register usage is different and we cannot merge.
                   1906:         */
1.62      pk       1907:        sethi   %hi(INTRREG_VA), %l5    ! ienab_bic(IE_ALLIE);
                   1908:        ldub    [%l5 + %lo(INTRREG_VA)], %o0
1.1       deraadt  1909:        andn    %o0, IE_ALLIE, %o0
1.62      pk       1910:        stb     %o0, [%l5 + %lo(INTRREG_VA)]
1.1       deraadt  1911:
                   1912:        /*
                   1913:         * Now reenable traps and call C code.
                   1914:         * %o1 through %o4 still hold the error reg contents.
                   1915:         * If memerr() returns, return from the trap.
                   1916:         */
                   1917:        wr      %l0, PSR_ET, %psr
1.111     pk       1918:        call    _C_LABEL(memerr4_4c)    ! memerr(0, ser, sva, aer, ava)
1.1       deraadt  1919:         clr    %o0
                   1920:
                   1921:        ld      [%sp + CCFSZ + 20], %g1 ! restore g1 through g7
                   1922:        wr      %l0, 0, %psr            ! and disable traps, 3 instr delay
                   1923:        ldd     [%sp + CCFSZ + 24], %g2
                   1924:        ldd     [%sp + CCFSZ + 32], %g4
                   1925:        ldd     [%sp + CCFSZ + 40], %g6
                   1926:        /* now safe to set IE_ALLIE again */
1.62      pk       1927:        ldub    [%l5 + %lo(INTRREG_VA)], %o1
1.1       deraadt  1928:        or      %o1, IE_ALLIE, %o1
1.62      pk       1929:        stb     %o1, [%l5 + %lo(INTRREG_VA)]
1.1       deraadt  1930:        b       return_from_trap
                   1931:         wr     %l4, 0, %y              ! restore y
                   1932:
                   1933:        /*
                   1934:         * Trap was a synchronous memory error.
                   1935:         * %o1 through %o4 still hold the error reg contents.
                   1936:         */
                   1937: 1:
1.111     pk       1938:        call    _C_LABEL(memerr4_4c)    ! memerr(1, ser, sva, aer, ava)
1.1       deraadt  1939:         mov    1, %o0
                   1940:
                   1941:        ld      [%sp + CCFSZ + 20], %g1 ! restore g1 through g7
                   1942:        ldd     [%sp + CCFSZ + 24], %g2
                   1943:        ldd     [%sp + CCFSZ + 32], %g4
                   1944:        ldd     [%sp + CCFSZ + 40], %g6
                   1945:        wr      %l4, 0, %y              ! restore y
                   1946:        b       return_from_trap
                   1947:         wr     %l0, 0, %psr
                   1948:        /* NOTREACHED */
1.52      pk       1949: #endif /* SUN4C */
                   1950:
                   1951: #if defined(SUN4M)
                   1952: memfault_sun4m:
1.94      pk       1953:        ! DANGER: we use the fact that %lo(CPUINFO_VA) is zero
                   1954: .if CPUINFO_VA & 0x1fff
                   1955: BARF
                   1956: .endif
                   1957:        sethi   %hi(CPUINFO_VA), %l4
                   1958:        ld      [%l4 + %lo(CPUINFO_VA+CPUINFO_GETSYNCFLT)], %l5
                   1959:        jmpl    %l5, %l7
                   1960:         or     %l4, %lo(CPUINFO_SYNCFLTDUMP), %l4
1.52      pk       1961:        TRAP_SETUP(-CCFSZ-80)
1.111     pk       1962:        INCR(_C_LABEL(uvmexp)+V_FAULTS) ! cnt.v_faults++ (clobbers %o0,%o1)
1.52      pk       1963:
                   1964:        st      %g1, [%sp + CCFSZ + 20] ! save g1
                   1965:        rd      %y, %l4                 ! save y
                   1966:
                   1967:        std     %g2, [%sp + CCFSZ + 24] ! save g2, g3
1.62      pk       1968:        std     %g4, [%sp + CCFSZ + 32] ! save g4, g5
1.94      pk       1969:        std     %g6, [%sp + CCFSZ + 40] ! sneak in g6, g7
1.52      pk       1970:
1.94      pk       1971:        ! retrieve sync fault status/address
                   1972:        sethi   %hi(CPUINFO_VA+CPUINFO_SYNCFLTDUMP), %o0
                   1973:        ld      [%o0 + %lo(CPUINFO_VA+CPUINFO_SYNCFLTDUMP)], %o1
                   1974:        ld      [%o0 + %lo(CPUINFO_VA+CPUINFO_SYNCFLTDUMP+4)], %o2
1.52      pk       1975:
                   1976:        wr      %l0, PSR_ET, %psr       ! reenable traps
                   1977:
                   1978:        /* Finish stackframe, call C trap handler */
                   1979:        std     %l0, [%sp + CCFSZ + 0]  ! set tf.tf_psr, tf.tf_pc
                   1980:        mov     %l3, %o0                ! (argument: type)
                   1981:        st      %l2, [%sp + CCFSZ + 8]  ! set tf.tf_npc
                   1982:        st      %l4, [%sp + CCFSZ + 12] ! set tf.tf_y
                   1983:        std     %i0, [%sp + CCFSZ + 48] ! tf.tf_out[0], etc
                   1984:        std     %i2, [%sp + CCFSZ + 56]
                   1985:        std     %i4, [%sp + CCFSZ + 64]
                   1986:        std     %i6, [%sp + CCFSZ + 72]
1.111     pk       1987:                                        ! mem_access_fault(type,sfsr,sfva,&tf);
                   1988:        call    _C_LABEL(mem_access_fault4m)
1.94      pk       1989:         add    %sp, CCFSZ, %o3         ! (argument: &tf)
1.52      pk       1990:
                   1991:        ldd     [%sp + CCFSZ + 0], %l0  ! load new values
                   1992:        ldd     [%sp + CCFSZ + 8], %l2
                   1993:        wr      %l3, 0, %y
                   1994:        ld      [%sp + CCFSZ + 20], %g1
                   1995:        ldd     [%sp + CCFSZ + 24], %g2
                   1996:        ldd     [%sp + CCFSZ + 32], %g4
                   1997:        ldd     [%sp + CCFSZ + 40], %g6
                   1998:        ldd     [%sp + CCFSZ + 48], %i0
                   1999:        ldd     [%sp + CCFSZ + 56], %i2
                   2000:        ldd     [%sp + CCFSZ + 64], %i4
                   2001:        ldd     [%sp + CCFSZ + 72], %i6
                   2002:
                   2003:        b       return_from_trap        ! go return
                   2004:         wr     %l0, 0, %psr            ! (but first disable traps again)
                   2005: #endif /* SUN4M */
1.1       deraadt  2006:
                   2007: normal_mem_fault:
                   2008:        /*
                   2009:         * Trap was some other error; call C code to deal with it.
                   2010:         * Must finish trap frame (psr,pc,npc,%y,%o0..%o7) in case
                   2011:         * we decide to deliver a signal or ptrace the process.
                   2012:         * %g1..%g7 were already set up above.
                   2013:         */
                   2014:        std     %l0, [%sp + CCFSZ + 0]  ! set tf.tf_psr, tf.tf_pc
                   2015:        mov     %l3, %o0                ! (argument: type)
                   2016:        st      %l2, [%sp + CCFSZ + 8]  ! set tf.tf_npc
                   2017:        st      %l4, [%sp + CCFSZ + 12] ! set tf.tf_y
                   2018:        mov     %l1, %o3                ! (argument: pc)
                   2019:        std     %i0, [%sp + CCFSZ + 48] ! tf.tf_out[0], etc
                   2020:        std     %i2, [%sp + CCFSZ + 56]
                   2021:        mov     %l0, %o4                ! (argument: psr)
                   2022:        std     %i4, [%sp + CCFSZ + 64]
                   2023:        std     %i6, [%sp + CCFSZ + 72]
1.111     pk       2024:        call    _C_LABEL(mem_access_fault)! mem_access_fault(type, ser, sva,
1.1       deraadt  2025:                                        !               pc, psr, &tf);
                   2026:         add    %sp, CCFSZ, %o5         ! (argument: &tf)
                   2027:
                   2028:        ldd     [%sp + CCFSZ + 0], %l0  ! load new values
                   2029:        ldd     [%sp + CCFSZ + 8], %l2
                   2030:        wr      %l3, 0, %y
                   2031:        ld      [%sp + CCFSZ + 20], %g1
                   2032:        ldd     [%sp + CCFSZ + 24], %g2
                   2033:        ldd     [%sp + CCFSZ + 32], %g4
                   2034:        ldd     [%sp + CCFSZ + 40], %g6
                   2035:        ldd     [%sp + CCFSZ + 48], %i0
                   2036:        ldd     [%sp + CCFSZ + 56], %i2
                   2037:        ldd     [%sp + CCFSZ + 64], %i4
                   2038:        ldd     [%sp + CCFSZ + 72], %i6
                   2039:
                   2040:        b       return_from_trap        ! go return
                   2041:         wr     %l0, 0, %psr            ! (but first disable traps again)
                   2042:
                   2043:
                   2044: /*
                   2045:  * fp_exception has to check to see if we are trying to save
                   2046:  * the FP state, and if so, continue to save the FP state.
                   2047:  *
                   2048:  * We do not even bother checking to see if we were in kernel mode,
                   2049:  * since users have no access to the special_fp_store instruction.
                   2050:  *
                   2051:  * This whole idea was stolen from Sprite.
                   2052:  */
                   2053: fp_exception:
                   2054:        set     special_fp_store, %l4   ! see if we came from the special one
                   2055:        cmp     %l1, %l4                ! pc == special_fp_store?
                   2056:        bne     slowtrap                ! no, go handle per usual
                   2057:         EMPTY
                   2058:        sethi   %hi(savefpcont), %l4    ! yes, "return" to the special code
                   2059:        or      %lo(savefpcont), %l4, %l4
                   2060:        jmp     %l4
                   2061:         rett   %l4 + 4
                   2062:
                   2063: /*
                   2064:  * slowtrap() builds a trap frame and calls trap().
                   2065:  * This is called `slowtrap' because it *is*....
                   2066:  * We have to build a full frame for ptrace(), for instance.
                   2067:  *
                   2068:  * Registers:
                   2069:  *     %l0 = %psr
                   2070:  *     %l1 = return pc
                   2071:  *     %l2 = return npc
                   2072:  *     %l3 = trap code
                   2073:  */
                   2074: slowtrap:
                   2075:        TRAP_SETUP(-CCFSZ-80)
                   2076:        /*
                   2077:         * Phew, ready to enable traps and call C code.
                   2078:         */
                   2079:        mov     %l3, %o0                ! put type in %o0 for later
                   2080: Lslowtrap_reenter:
                   2081:        wr      %l0, PSR_ET, %psr       ! traps on again
                   2082:        std     %l0, [%sp + CCFSZ]      ! tf.tf_psr = psr; tf.tf_pc = ret_pc;
                   2083:        rd      %y, %l3
                   2084:        std     %l2, [%sp + CCFSZ + 8]  ! tf.tf_npc = return_npc; tf.tf_y = %y;
                   2085:        st      %g1, [%sp + CCFSZ + 20]
                   2086:        std     %g2, [%sp + CCFSZ + 24]
                   2087:        std     %g4, [%sp + CCFSZ + 32]
                   2088:        std     %g6, [%sp + CCFSZ + 40]
                   2089:        std     %i0, [%sp + CCFSZ + 48]
                   2090:        mov     %l0, %o1                ! (psr)
                   2091:        std     %i2, [%sp + CCFSZ + 56]
                   2092:        mov     %l1, %o2                ! (pc)
                   2093:        std     %i4, [%sp + CCFSZ + 64]
                   2094:        add     %sp, CCFSZ, %o3         ! (&tf)
1.111     pk       2095:        call    _C_LABEL(trap)          ! trap(type, psr, pc, &tf)
1.1       deraadt  2096:         std    %i6, [%sp + CCFSZ + 72]
                   2097:
                   2098:        ldd     [%sp + CCFSZ], %l0      ! load new values
                   2099:        ldd     [%sp + CCFSZ + 8], %l2
                   2100:        wr      %l3, 0, %y
                   2101:        ld      [%sp + CCFSZ + 20], %g1
                   2102:        ldd     [%sp + CCFSZ + 24], %g2
                   2103:        ldd     [%sp + CCFSZ + 32], %g4
                   2104:        ldd     [%sp + CCFSZ + 40], %g6
                   2105:        ldd     [%sp + CCFSZ + 48], %i0
                   2106:        ldd     [%sp + CCFSZ + 56], %i2
                   2107:        ldd     [%sp + CCFSZ + 64], %i4
                   2108:        ldd     [%sp + CCFSZ + 72], %i6
                   2109:        b       return_from_trap
                   2110:         wr     %l0, 0, %psr
                   2111:
                   2112: /*
                   2113:  * Do a `software' trap by re-entering the trap code, possibly first
                   2114:  * switching from interrupt stack to kernel stack.  This is used for
                   2115:  * scheduling and signal ASTs (which generally occur from softclock or
                   2116:  * tty or net interrupts) and register window saves (which might occur
                   2117:  * from anywhere).
                   2118:  *
                   2119:  * The current window is the trap window, and it is by definition clean.
                   2120:  * We enter with the trap type in %o0.  All we have to do is jump to
                   2121:  * Lslowtrap_reenter above, but maybe after switching stacks....
                   2122:  */
                   2123: softtrap:
1.97      pk       2124: #ifdef MULTIPROCESSOR
                   2125:        /*
                   2126:         * The interrupt stack is not at a fixed location
                   2127:         * and %sp must be checked against both ends.
                   2128:         */
1.101     pk       2129:        sethi   %hi(_EINTSTACKP), %l7
                   2130:        ld      [%l7 + %lo(_EINTSTACKP)], %l7
1.97      pk       2131:        cmp     %sp, %l7
                   2132:        bge     Lslowtrap_reenter
                   2133:         EMPTY
                   2134:        set     INT_STACK_SIZE, %l6
                   2135:        sub     %l7, %l6, %l7
                   2136:        cmp     %sp, %l7
                   2137:        blu     Lslowtrap_reenter
                   2138:         EMPTY
                   2139: #else
1.111     pk       2140:        sethi   %hi(_C_LABEL(eintstack)), %l7
1.1       deraadt  2141:        cmp     %sp, %l7
                   2142:        bge     Lslowtrap_reenter
                   2143:         EMPTY
1.97      pk       2144: #endif
1.111     pk       2145:        sethi   %hi(cpcb), %l6
                   2146:        ld      [%l6 + %lo(cpcb)], %l6
1.13      deraadt  2147:        set     USPACE-CCFSZ-80, %l5
1.1       deraadt  2148:        add     %l6, %l5, %l7
                   2149:        SET_SP_REDZONE(%l6, %l5)
                   2150:        b       Lslowtrap_reenter
                   2151:         mov    %l7, %sp
                   2152:
                   2153: #ifdef KGDB
                   2154: /*
                   2155:  * bpt is entered on all breakpoint traps.
                   2156:  * If this is a kernel breakpoint, we do not want to call trap().
                   2157:  * Among other reasons, this way we can set breakpoints in trap().
                   2158:  */
                   2159: bpt:
                   2160:        btst    PSR_PS, %l0             ! breakpoint from kernel?
                   2161:        bz      slowtrap                ! no, go do regular trap
                   2162:         nop
                   2163:
                   2164:        /*
                   2165:         * Build a trap frame for kgdb_trap_glue to copy.
                   2166:         * Enable traps but set ipl high so that we will not
                   2167:         * see interrupts from within breakpoints.
                   2168:         */
                   2169:        TRAP_SETUP(-CCFSZ-80)
                   2170:        or      %l0, PSR_PIL, %l4       ! splhigh()
                   2171:        wr      %l4, 0, %psr            ! the manual claims that this
                   2172:        wr      %l4, PSR_ET, %psr       ! song and dance is necessary
                   2173:        std     %l0, [%sp + CCFSZ + 0]  ! tf.tf_psr, tf.tf_pc
                   2174:        mov     %l3, %o0                ! trap type arg for kgdb_trap_glue
                   2175:        rd      %y, %l3
                   2176:        std     %l2, [%sp + CCFSZ + 8]  ! tf.tf_npc, tf.tf_y
                   2177:        rd      %wim, %l3
                   2178:        st      %l3, [%sp + CCFSZ + 16] ! tf.tf_wim (a kgdb-only r/o field)
                   2179:        st      %g1, [%sp + CCFSZ + 20] ! tf.tf_global[1]
                   2180:        std     %g2, [%sp + CCFSZ + 24] ! etc
                   2181:        std     %g4, [%sp + CCFSZ + 32]
                   2182:        std     %g6, [%sp + CCFSZ + 40]
                   2183:        std     %i0, [%sp + CCFSZ + 48] ! tf.tf_in[0..1]
                   2184:        std     %i2, [%sp + CCFSZ + 56] ! etc
                   2185:        std     %i4, [%sp + CCFSZ + 64]
                   2186:        std     %i6, [%sp + CCFSZ + 72]
                   2187:
                   2188:        /*
                   2189:         * Now call kgdb_trap_glue(); if it returns, call trap().
                   2190:         */
                   2191:        mov     %o0, %l3                ! gotta save trap type
1.111     pk       2192:        call    _C_LABEL(kgdb_trap_glue)! kgdb_trap_glue(type, &trapframe)
1.1       deraadt  2193:         add    %sp, CCFSZ, %o1         ! (&trapframe)
                   2194:
                   2195:        /*
                   2196:         * Use slowtrap to call trap---but first erase our tracks
                   2197:         * (put the registers back the way they were).
                   2198:         */
                   2199:        mov     %l3, %o0                ! slowtrap will need trap type
                   2200:        ld      [%sp + CCFSZ + 12], %l3
                   2201:        wr      %l3, 0, %y
                   2202:        ld      [%sp + CCFSZ + 20], %g1
                   2203:        ldd     [%sp + CCFSZ + 24], %g2
                   2204:        ldd     [%sp + CCFSZ + 32], %g4
                   2205:        b       Lslowtrap_reenter
                   2206:         ldd    [%sp + CCFSZ + 40], %g6
                   2207:
                   2208: /*
                   2209:  * Enter kernel breakpoint.  Write all the windows (not including the
                   2210:  * current window) into the stack, so that backtrace works.  Copy the
                   2211:  * supplied trap frame to the kgdb stack and switch stacks.
                   2212:  *
                   2213:  * kgdb_trap_glue(type, tf0)
                   2214:  *     int type;
                   2215:  *     struct trapframe *tf0;
                   2216:  */
1.111     pk       2217: _ENTRY(_C_LABEL(kgdb_trap_glue))
1.1       deraadt  2218:        save    %sp, -CCFSZ, %sp
                   2219:
1.111     pk       2220:        call    _C_LABEL(write_all_windows)
1.1       deraadt  2221:         mov    %sp, %l4                ! %l4 = current %sp
                   2222:
                   2223:        /* copy trapframe to top of kgdb stack */
1.125.2.1  bouyer   2224:        set     _C_LABEL(kgdb_stack) + KGDB_STACK_SIZE - 80, %l0
1.1       deraadt  2225:                                        ! %l0 = tfcopy -> end_of_kgdb_stack
                   2226:        mov     80, %l1
                   2227: 1:     ldd     [%i1], %l2
                   2228:        inc     8, %i1
                   2229:        deccc   8, %l1
                   2230:        std     %l2, [%l0]
                   2231:        bg      1b
                   2232:         inc    8, %l0
                   2233:
                   2234: #ifdef DEBUG
                   2235:        /* save old red zone and then turn it off */
                   2236:        sethi   %hi(_redzone), %l7
                   2237:        ld      [%l7 + %lo(_redzone)], %l6
                   2238:        st      %g0, [%l7 + %lo(_redzone)]
                   2239: #endif
                   2240:        /* switch to kgdb stack */
                   2241:        add     %l0, -CCFSZ-80, %sp
                   2242:
                   2243:        /* if (kgdb_trap(type, tfcopy)) kgdb_rett(tfcopy); */
                   2244:        mov     %i0, %o0
1.111     pk       2245:        call    _C_LABEL(kgdb_trap)
1.1       deraadt  2246:        add     %l0, -80, %o1
                   2247:        tst     %o0
                   2248:        bnz,a   kgdb_rett
                   2249:         add    %l0, -80, %g1
                   2250:
                   2251:        /*
                   2252:         * kgdb_trap() did not handle the trap at all so the stack is
                   2253:         * still intact.  A simple `restore' will put everything back,
                   2254:         * after we reset the stack pointer.
                   2255:         */
                   2256:        mov     %l4, %sp
                   2257: #ifdef DEBUG
                   2258:        st      %l6, [%l7 + %lo(_redzone)]      ! restore red zone
                   2259: #endif
                   2260:        ret
                   2261:        restore
                   2262:
                   2263: /*
                   2264:  * Return from kgdb trap.  This is sort of special.
                   2265:  *
                   2266:  * We know that kgdb_trap_glue wrote the window above it, so that we will
                   2267:  * be able to (and are sure to have to) load it up.  We also know that we
                   2268:  * came from kernel land and can assume that the %fp (%i6) we load here
                   2269:  * is proper.  We must also be sure not to lower ipl (it is at splhigh())
                   2270:  * until we have traps disabled, due to the SPARC taking traps at the
                   2271:  * new ipl before noticing that PSR_ET has been turned off.  We are on
                   2272:  * the kgdb stack, so this could be disastrous.
                   2273:  *
                   2274:  * Note that the trapframe argument in %g1 points into the current stack
                   2275:  * frame (current window).  We abandon this window when we move %g1->tf_psr
                   2276:  * into %psr, but we will not have loaded the new %sp yet, so again traps
                   2277:  * must be disabled.
                   2278:  */
                   2279: kgdb_rett:
                   2280:        rd      %psr, %g4               ! turn off traps
                   2281:        wr      %g4, PSR_ET, %psr
                   2282:        /* use the three-instruction delay to do something useful */
                   2283:        ld      [%g1], %g2              ! pick up new %psr
                   2284:        ld      [%g1 + 12], %g3         ! set %y
                   2285:        wr      %g3, 0, %y
                   2286: #ifdef DEBUG
                   2287:        st      %l6, [%l7 + %lo(_redzone)] ! and restore red zone
                   2288: #endif
                   2289:        wr      %g0, 0, %wim            ! enable window changes
                   2290:        nop; nop; nop
                   2291:        /* now safe to set the new psr (changes CWP, leaves traps disabled) */
                   2292:        wr      %g2, 0, %psr            ! set rett psr (including cond codes)
                   2293:        /* 3 instruction delay before we can use the new window */
                   2294: /*1*/  ldd     [%g1 + 24], %g2         ! set new %g2, %g3
                   2295: /*2*/  ldd     [%g1 + 32], %g4         ! set new %g4, %g5
                   2296: /*3*/  ldd     [%g1 + 40], %g6         ! set new %g6, %g7
                   2297:
                   2298:        /* now we can use the new window */
                   2299:        mov     %g1, %l4
                   2300:        ld      [%l4 + 4], %l1          ! get new pc
                   2301:        ld      [%l4 + 8], %l2          ! get new npc
                   2302:        ld      [%l4 + 20], %g1         ! set new %g1
                   2303:
                   2304:        /* set up returnee's out registers, including its %sp */
                   2305:        ldd     [%l4 + 48], %i0
                   2306:        ldd     [%l4 + 56], %i2
                   2307:        ldd     [%l4 + 64], %i4
                   2308:        ldd     [%l4 + 72], %i6
                   2309:
                   2310:        /* load returnee's window, making the window above it be invalid */
                   2311:        restore
                   2312:        restore %g0, 1, %l1             ! move to inval window and set %l1 = 1
                   2313:        rd      %psr, %l0
                   2314:        sll     %l1, %l0, %l1
                   2315:        wr      %l1, 0, %wim            ! %wim = 1 << (%psr & 31)
1.111     pk       2316:        sethi   %hi(cpcb), %l1
                   2317:        ld      [%l1 + %lo(cpcb)], %l1
1.1       deraadt  2318:        and     %l0, 31, %l0            ! CWP = %psr & 31;
                   2319:        st      %l0, [%l1 + PCB_WIM]    ! cpcb->pcb_wim = CWP;
                   2320:        save    %g0, %g0, %g0           ! back to window to reload
                   2321:        LOADWIN(%sp)
                   2322:        save    %g0, %g0, %g0           ! back to trap window
                   2323:        /* note, we have not altered condition codes; safe to just rett */
                   2324:        RETT
                   2325: #endif
                   2326:
                   2327: /*
                   2328:  * syscall() builds a trap frame and calls syscall().
                   2329:  * sun_syscall is same but delivers sun system call number
                   2330:  * XXX should not have to save&reload ALL the registers just for
                   2331:  *     ptrace...
                   2332:  */
1.122     christos 2333: _C_LABEL(_syscall):
1.1       deraadt  2334:        TRAP_SETUP(-CCFSZ-80)
                   2335:        wr      %l0, PSR_ET, %psr
                   2336:        std     %l0, [%sp + CCFSZ + 0]  ! tf_psr, tf_pc
                   2337:        rd      %y, %l3
                   2338:        std     %l2, [%sp + CCFSZ + 8]  ! tf_npc, tf_y
                   2339:        st      %g1, [%sp + CCFSZ + 20] ! tf_g[1]
                   2340:        std     %g2, [%sp + CCFSZ + 24] ! tf_g[2], tf_g[3]
                   2341:        std     %g4, [%sp + CCFSZ + 32] ! etc
                   2342:        std     %g6, [%sp + CCFSZ + 40]
                   2343:        mov     %g1, %o0                ! (code)
                   2344:        std     %i0, [%sp + CCFSZ + 48]
                   2345:        add     %sp, CCFSZ, %o1         ! (&tf)
                   2346:        std     %i2, [%sp + CCFSZ + 56]
                   2347:        mov     %l1, %o2                ! (pc)
                   2348:        std     %i4, [%sp + CCFSZ + 64]
1.111     pk       2349:        call    _C_LABEL(syscall)       ! syscall(code, &tf, pc, suncompat)
1.1       deraadt  2350:         std    %i6, [%sp + CCFSZ + 72]
                   2351:        ! now load em all up again, sigh
                   2352:        ldd     [%sp + CCFSZ + 0], %l0  ! new %psr, new pc
                   2353:        ldd     [%sp + CCFSZ + 8], %l2  ! new npc, new %y
                   2354:        wr      %l3, 0, %y
1.51      pk       2355:        /* see `proc_trampoline' for the reason for this label */
                   2356: return_from_syscall:
1.1       deraadt  2357:        ld      [%sp + CCFSZ + 20], %g1
                   2358:        ldd     [%sp + CCFSZ + 24], %g2
                   2359:        ldd     [%sp + CCFSZ + 32], %g4
                   2360:        ldd     [%sp + CCFSZ + 40], %g6
                   2361:        ldd     [%sp + CCFSZ + 48], %i0
                   2362:        ldd     [%sp + CCFSZ + 56], %i2
                   2363:        ldd     [%sp + CCFSZ + 64], %i4
                   2364:        ldd     [%sp + CCFSZ + 72], %i6
                   2365:        b       return_from_trap
                   2366:         wr     %l0, 0, %psr
                   2367:
                   2368: /*
                   2369:  * Interrupts.  Software interrupts must be cleared from the software
                   2370:  * interrupt enable register.  Rather than calling ienab_bic for each,
                   2371:  * we do them in-line before enabling traps.
                   2372:  *
                   2373:  * After preliminary setup work, the interrupt is passed to each
                   2374:  * registered handler in turn.  These are expected to return nonzero if
                   2375:  * they took care of the interrupt.  If a handler claims the interrupt,
                   2376:  * we exit (hardware interrupts are latched in the requestor so we'll
                   2377:  * just take another interrupt in the unlikely event of simultaneous
                   2378:  * interrupts from two different devices at the same level).  If we go
                   2379:  * through all the registered handlers and no one claims it, we report a
                   2380:  * stray interrupt.  This is more or less done as:
                   2381:  *
                   2382:  *     for (ih = intrhand[intlev]; ih; ih = ih->ih_next)
                   2383:  *             if ((*ih->ih_fun)(ih->ih_arg ? ih->ih_arg : &frame))
                   2384:  *                     return;
                   2385:  *     strayintr(&frame);
                   2386:  *
                   2387:  * Software interrupts are almost the same with three exceptions:
                   2388:  * (1) we clear the interrupt from the software interrupt enable
                   2389:  *     register before calling any handler (we have to clear it first
                   2390:  *     to avoid an interrupt-losing race),
                   2391:  * (2) we always call all the registered handlers (there is no way
                   2392:  *     to tell if the single bit in the software interrupt register
                   2393:  *     represents one or many requests)
                   2394:  * (3) we never announce a stray interrupt (because of (1), another
                   2395:  *     interrupt request can come in while we're in the handler.  If
1.52      pk       2396:  *     the handler deals with everything for both the original & the
1.1       deraadt  2397:  *     new request, we'll erroneously report a stray interrupt when
                   2398:  *     we take the software interrupt for the new request.
                   2399:  *
                   2400:  * Inputs:
                   2401:  *     %l0 = %psr
                   2402:  *     %l1 = return pc
                   2403:  *     %l2 = return npc
                   2404:  *     %l3 = interrupt level
                   2405:  *     (software interrupt only) %l4 = bits to clear in interrupt register
                   2406:  *
                   2407:  * Internal:
                   2408:  *     %l4, %l5: local variables
                   2409:  *     %l6 = %y
                   2410:  *     %l7 = %g1
                   2411:  *     %g2..%g7 go to stack
                   2412:  *
                   2413:  * An interrupt frame is built in the space for a full trapframe;
                   2414:  * this contains the psr, pc, npc, and interrupt level.
                   2415:  */
1.52      pk       2416: softintr_sun44c:
1.62      pk       2417:        sethi   %hi(INTRREG_VA), %l6
                   2418:        ldub    [%l6 + %lo(INTRREG_VA)], %l5
1.1       deraadt  2419:        andn    %l5, %l4, %l5
1.62      pk       2420:        stb     %l5, [%l6 + %lo(INTRREG_VA)]
1.52      pk       2421:
                   2422: softintr_common:
1.1       deraadt  2423:        INTR_SETUP(-CCFSZ-80)
                   2424:        std     %g2, [%sp + CCFSZ + 24] ! save registers
1.111     pk       2425:        INCR(_C_LABEL(uvmexp)+V_INTR)   ! cnt.v_intr++; (clobbers %o0,%o1)
1.1       deraadt  2426:        mov     %g1, %l7
                   2427:        rd      %y, %l6
                   2428:        std     %g4, [%sp + CCFSZ + 32]
                   2429:        andn    %l0, PSR_PIL, %l4       ! %l4 = psr & ~PSR_PIL |
                   2430:        sll     %l3, 8, %l5             !       intlev << IPLSHIFT
                   2431:        std     %g6, [%sp + CCFSZ + 40]
                   2432:        or      %l5, %l4, %l4           !                       ;
                   2433:        wr      %l4, 0, %psr            ! the manual claims this
                   2434:        wr      %l4, PSR_ET, %psr       ! song and dance is necessary
                   2435:        std     %l0, [%sp + CCFSZ + 0]  ! set up intrframe/clockframe
                   2436:        sll     %l3, 2, %l5
1.111     pk       2437:        set     _C_LABEL(intrcnt), %l4  ! intrcnt[intlev]++;
1.1       deraadt  2438:        ld      [%l4 + %l5], %o0
                   2439:        std     %l2, [%sp + CCFSZ + 8]
                   2440:        inc     %o0
                   2441:        st      %o0, [%l4 + %l5]
1.111     pk       2442:        set     _C_LABEL(intrhand), %l4 ! %l4 = intrhand[intlev];
1.1       deraadt  2443:        ld      [%l4 + %l5], %l4
                   2444:        b       3f
                   2445:         st     %fp, [%sp + CCFSZ + 16]
                   2446:
                   2447: 1:     ld      [%l4], %o1
                   2448:        ld      [%l4 + 4], %o0
                   2449:        tst     %o0
                   2450:        bz,a    2f
                   2451:         add    %sp, CCFSZ, %o0
                   2452: 2:     jmpl    %o1, %o7                !       (void)(*ih->ih_fun)(...)
                   2453:         ld     [%l4 + 8], %l4          !       and ih = ih->ih_next
                   2454: 3:     tst     %l4                     ! while ih != NULL
                   2455:        bnz     1b
                   2456:         nop
                   2457:        mov     %l7, %g1
                   2458:        wr      %l6, 0, %y
                   2459:        ldd     [%sp + CCFSZ + 24], %g2
                   2460:        ldd     [%sp + CCFSZ + 32], %g4
                   2461:        ldd     [%sp + CCFSZ + 40], %g6
                   2462:        b       return_from_trap
                   2463:         wr     %l0, 0, %psr
                   2464:
                   2465:        /*
1.52      pk       2466:         * _sparc_interrupt{44c,4m} is exported for paranoia checking
                   2467:         * (see intr.c).
1.1       deraadt  2468:         */
1.52      pk       2469: #if defined(SUN4M)
1.111     pk       2470: _ENTRY(_C_LABEL(sparc_interrupt4m))
1.52      pk       2471:        mov     1, %l4
1.96      pk       2472:        sethi   %hi(CPUINFO_VA+CPUINFO_INTREG), %l6
                   2473:        ld      [%l6 + %lo(CPUINFO_VA+CPUINFO_INTREG)], %l6
                   2474:        ld      [%l6 + ICR_PI_PEND_OFFSET], %l5 ! get pending interrupts
                   2475:        sll     %l4, %l3, %l4                   ! test SOFTINT bit
1.52      pk       2476:        andcc   %l5, %l4, %g0
1.111     pk       2477:        bne     sparc_interrupt_common
1.52      pk       2478:         nop
                   2479:
                   2480:        ! a soft interrupt; clear bit in interrupt-pending register
                   2481:        sll     %l4, 16, %l5
1.96      pk       2482:        st      %l5, [%l6 + ICR_PI_CLR_OFFSET]
1.52      pk       2483:        b,a     softintr_common
                   2484: #endif
                   2485:
1.111     pk       2486: _ENTRY(_C_LABEL(sparc_interrupt44c))
                   2487: sparc_interrupt_common:
1.1       deraadt  2488:        INTR_SETUP(-CCFSZ-80)
                   2489:        std     %g2, [%sp + CCFSZ + 24] ! save registers
1.111     pk       2490:        INCR(_C_LABEL(uvmexp)+V_INTR)   ! cnt.v_intr++; (clobbers %o0,%o1)
1.1       deraadt  2491:        mov     %g1, %l7
                   2492:        rd      %y, %l6
                   2493:        std     %g4, [%sp + CCFSZ + 32]
                   2494:        andn    %l0, PSR_PIL, %l4       ! %l4 = psr & ~PSR_PIL |
                   2495:        sll     %l3, 8, %l5             !       intlev << IPLSHIFT
                   2496:        std     %g6, [%sp + CCFSZ + 40]
                   2497:        or      %l5, %l4, %l4           !                       ;
                   2498:        wr      %l4, 0, %psr            ! the manual claims this
                   2499:        wr      %l4, PSR_ET, %psr       ! song and dance is necessary
                   2500:        std     %l0, [%sp + CCFSZ + 0]  ! set up intrframe/clockframe
                   2501:        sll     %l3, 2, %l5
1.111     pk       2502:        set     _C_LABEL(intrcnt), %l4  ! intrcnt[intlev]++;
1.1       deraadt  2503:        ld      [%l4 + %l5], %o0
                   2504:        std     %l2, [%sp + CCFSZ + 8]  ! set up intrframe/clockframe
                   2505:        inc     %o0
                   2506:        st      %o0, [%l4 + %l5]
1.111     pk       2507:        set     _C_LABEL(intrhand), %l4 ! %l4 = intrhand[intlev];
1.1       deraadt  2508:        ld      [%l4 + %l5], %l4
                   2509:        b       3f
                   2510:         st     %fp, [%sp + CCFSZ + 16]
                   2511:
                   2512: 1:     ld      [%l4], %o1
                   2513:        ld      [%l4 + 4], %o0
                   2514:        tst     %o0
                   2515:        bz,a    2f
                   2516:         add    %sp, CCFSZ, %o0
                   2517: 2:     jmpl    %o1, %o7                !       handled = (*ih->ih_fun)(...)
                   2518:         ld     [%l4 + 8], %l4          !       and ih = ih->ih_next
                   2519:        tst     %o0
                   2520:        bnz     4f                      ! if (handled) break
                   2521:         nop
                   2522: 3:     tst     %l4
                   2523:        bnz     1b                      ! while (ih)
                   2524:         nop
1.76      pk       2525:
                   2526:        /* Unhandled interrupts while cold cause IPL to be raised to `high' */
1.111     pk       2527:        sethi   %hi(_C_LABEL(cold)), %o0
                   2528:        ld      [%o0 + %lo(_C_LABEL(cold))], %o0
1.76      pk       2529:        tst     %o0                     ! if (cold) {
                   2530:        bnz,a   4f                      !       splhigh();
                   2531:         or     %l0, 0xf00, %l0         ! } else
                   2532:
1.111     pk       2533:        call    _C_LABEL(strayintr)     !       strayintr(&intrframe)
1.1       deraadt  2534:         add    %sp, CCFSZ, %o0
                   2535:        /* all done: restore registers and go return */
                   2536: 4:     mov     %l7, %g1
                   2537:        wr      %l6, 0, %y
                   2538:        ldd     [%sp + CCFSZ + 24], %g2
                   2539:        ldd     [%sp + CCFSZ + 32], %g4
                   2540:        ldd     [%sp + CCFSZ + 40], %g6
                   2541:        b       return_from_trap
                   2542:         wr     %l0, 0, %psr
                   2543:
                   2544: #ifdef notyet
                   2545: /*
                   2546:  * Level 12 (ZS serial) interrupt.  Handle it quickly, schedule a
                   2547:  * software interrupt, and get out.  Do the software interrupt directly
                   2548:  * if we would just take it on the way out.
                   2549:  *
                   2550:  * Input:
                   2551:  *     %l0 = %psr
                   2552:  *     %l1 = return pc
                   2553:  *     %l2 = return npc
                   2554:  * Internal:
                   2555:  *     %l3 = zs device
                   2556:  *     %l4, %l5 = temporary
                   2557:  *     %l6 = rr3 (or temporary data) + 0x100 => need soft int
                   2558:  *     %l7 = zs soft status
                   2559:  */
                   2560: zshard:
                   2561: #endif /* notyet */
                   2562:
                   2563: /*
                   2564:  * Level 15 interrupt.  An async memory error has occurred;
                   2565:  * take care of it (typically by panicking, but hey...).
                   2566:  *     %l0 = %psr
                   2567:  *     %l1 = return pc
                   2568:  *     %l2 = return npc
                   2569:  *     %l3 = 15 * 4 (why? just because!)
                   2570:  *
                   2571:  * Internal:
                   2572:  *     %l4 = %y
                   2573:  *     %l5 = %g1
                   2574:  *     %l6 = %g6
                   2575:  *     %l7 = %g7
                   2576:  *  g2, g3, g4, g5 go to stack
                   2577:  *
                   2578:  * This code is almost the same as that in mem_access_fault,
                   2579:  * except that we already know the problem is not a `normal' fault,
                   2580:  * and that we must be extra-careful with interrupt enables.
                   2581:  */
1.52      pk       2582:
                   2583: #if defined(SUN4)
                   2584: nmi_sun4:
1.1       deraadt  2585:        INTR_SETUP(-CCFSZ-80)
1.111     pk       2586:        INCR(_C_LABEL(uvmexp)+V_INTR)   ! cnt.v_intr++; (clobbers %o0,%o1)
1.1       deraadt  2587:        /*
                   2588:         * Level 15 interrupts are nonmaskable, so with traps off,
                   2589:         * disable all interrupts to prevent recursion.
                   2590:         */
1.62      pk       2591:        sethi   %hi(INTRREG_VA), %o0
                   2592:        ldub    [%o0 + %lo(INTRREG_VA)], %o1
1.52      pk       2593:        andn    %o0, IE_ALLIE, %o1
1.62      pk       2594:        stb     %o1, [%o0 + %lo(INTRREG_VA)]
1.1       deraadt  2595:        wr      %l0, PSR_ET, %psr       ! okay, turn traps on again
                   2596:
                   2597:        std     %g2, [%sp + CCFSZ + 0]  ! save g2, g3
                   2598:        rd      %y, %l4                 ! save y
                   2599:
1.19      deraadt  2600:        std     %g4, [%sp + CCFSZ + 8]  ! save g4, g5
                   2601:        mov     %g1, %l5                ! save g1, g6, g7
                   2602:        mov     %g6, %l6
                   2603:        mov     %g7, %l7
                   2604: #if defined(SUN4C) || defined(SUN4M)
1.52      pk       2605:        b,a     nmi_common
1.19      deraadt  2606: #endif /* SUN4C || SUN4M */
1.52      pk       2607: #endif
                   2608:
                   2609: #if defined(SUN4C)
                   2610: nmi_sun4c:
                   2611:        INTR_SETUP(-CCFSZ-80)
1.111     pk       2612:        INCR(_C_LABEL(uvmexp)+V_INTR)   ! cnt.v_intr++; (clobbers %o0,%o1)
1.52      pk       2613:        /*
                   2614:         * Level 15 interrupts are nonmaskable, so with traps off,
                   2615:         * disable all interrupts to prevent recursion.
                   2616:         */
1.62      pk       2617:        sethi   %hi(INTRREG_VA), %o0
                   2618:        ldub    [%o0 + %lo(INTRREG_VA)], %o1
1.52      pk       2619:        andn    %o0, IE_ALLIE, %o1
1.62      pk       2620:        stb     %o1, [%o0 + %lo(INTRREG_VA)]
1.52      pk       2621:        wr      %l0, PSR_ET, %psr       ! okay, turn traps on again
                   2622:
                   2623:        std     %g2, [%sp + CCFSZ + 0]  ! save g2, g3
                   2624:        rd      %y, %l4                 ! save y
                   2625:
                   2626:        ! must read the sync error register too.
1.1       deraadt  2627:        set     AC_SYNC_ERR, %o0
                   2628:        lda     [%o0] ASI_CONTROL, %o1  ! sync err reg
                   2629:        inc     4, %o0
                   2630:        lda     [%o0] ASI_CONTROL, %o2  ! sync virt addr
                   2631:        std     %g4, [%sp + CCFSZ + 8]  ! save g4,g5
                   2632:        mov     %g1, %l5                ! save g1,g6,g7
                   2633:        mov     %g6, %l6
                   2634:        mov     %g7, %l7
                   2635:        inc     4, %o0
                   2636:        lda     [%o0] ASI_CONTROL, %o3  ! async err reg
                   2637:        inc     4, %o0
                   2638:        lda     [%o0] ASI_CONTROL, %o4  ! async virt addr
1.52      pk       2639: #if defined(SUN4M)
                   2640:        !!b,a   nmi_common
                   2641: #endif /* SUN4M */
                   2642: #endif /* SUN4C */
                   2643:
                   2644: nmi_common:
1.1       deraadt  2645:        ! and call C code
1.111     pk       2646:        call    _C_LABEL(memerr4_4c)    ! memerr(0, ser, sva, aer, ava)
1.95      pk       2647:         clr    %o0
1.1       deraadt  2648:
                   2649:        mov     %l5, %g1                ! restore g1 through g7
                   2650:        ldd     [%sp + CCFSZ + 0], %g2
                   2651:        ldd     [%sp + CCFSZ + 8], %g4
                   2652:        wr      %l0, 0, %psr            ! re-disable traps
                   2653:        mov     %l6, %g6
                   2654:        mov     %l7, %g7
                   2655:
                   2656:        ! set IE_ALLIE again (safe, we disabled traps again above)
1.62      pk       2657:        sethi   %hi(INTRREG_VA), %o0
                   2658:        ldub    [%o0 + %lo(INTRREG_VA)], %o1
1.1       deraadt  2659:        or      %o1, IE_ALLIE, %o1
1.62      pk       2660:        stb     %o1, [%o0 + %lo(INTRREG_VA)]
1.1       deraadt  2661:        b       return_from_trap
                   2662:         wr     %l4, 0, %y              ! restore y
                   2663:
1.52      pk       2664: #if defined(SUN4M)
                   2665: nmi_sun4m:
                   2666:        INTR_SETUP(-CCFSZ-80)
1.111     pk       2667:        INCR(_C_LABEL(uvmexp)+V_INTR)   ! cnt.v_intr++; (clobbers %o0,%o1)
1.94      pk       2668:
                   2669:        /* Read the Pending Interrupts register */
1.96      pk       2670:        sethi   %hi(CPUINFO_VA+CPUINFO_INTREG), %l6
                   2671:        ld      [%l6 + %lo(CPUINFO_VA+CPUINFO_INTREG)], %l6
                   2672:        ld      [%l6 + ICR_PI_PEND_OFFSET], %l5 ! get pending interrupts
                   2673:
1.111     pk       2674:        set     _C_LABEL(nmi_soft), %o3         ! assume a softint
1.105     pk       2675:        set     PINTR_IC, %o1                   ! hard lvl 15 bit
                   2676:        sethi   %hi(PINTR_SINTRLEV(15)), %o0    ! soft lvl 15 bit
1.94      pk       2677:        btst    %o0, %l5                ! soft level 15?
1.101     pk       2678:        bnz,a   1f                      !
1.105     pk       2679:         mov    %o0, %o1                ! shift int clear bit to SOFTINT 15
                   2680:
1.111     pk       2681:        set     _C_LABEL(nmi_hard), %o3 ! it's a hardint; switch handler
1.94      pk       2682:
1.52      pk       2683:        /*
                   2684:         * Level 15 interrupts are nonmaskable, so with traps off,
                   2685:         * disable all interrupts to prevent recursion.
                   2686:         */
                   2687:        sethi   %hi(ICR_SI_SET), %o0
1.101     pk       2688:        set     SINTR_MA, %o2
                   2689:        st      %o2, [%o0 + %lo(ICR_SI_SET)]
1.52      pk       2690:
1.101     pk       2691: 1:
1.105     pk       2692:        /*
                   2693:         * Now clear the NMI. Apparently, we must allow some time
                   2694:         * to let the bits sink in..
                   2695:         */
1.96      pk       2696:        st      %o1, [%l6 + ICR_PI_CLR_OFFSET]
1.105     pk       2697:         nop; nop; nop;
                   2698:        ld      [%l6 + ICR_PI_PEND_OFFSET], %g0 ! drain register!?
                   2699:         nop; nop; nop;
1.52      pk       2700:
                   2701:        wr      %l0, PSR_ET, %psr       ! okay, turn traps on again
                   2702:
                   2703:        std     %g2, [%sp + CCFSZ + 0]  ! save g2, g3
                   2704:        rd      %y, %l4                 ! save y
1.94      pk       2705:        std     %g4, [%sp + CCFSZ + 8]  ! save g4,g5
1.52      pk       2706:
                   2707:        /* Finish stackframe, call C trap handler */
                   2708:        mov     %g1, %l5                ! save g1,g6,g7
                   2709:        mov     %g6, %l6
                   2710:        mov     %g7, %l7
                   2711:
1.105     pk       2712:        jmpl    %o3, %o7                ! handler(0);
1.94      pk       2713:         clr    %o0
1.105     pk       2714:
1.52      pk       2715:        mov     %l5, %g1                ! restore g1 through g7
                   2716:        ldd     [%sp + CCFSZ + 0], %g2
                   2717:        ldd     [%sp + CCFSZ + 8], %g4
                   2718:        wr      %l0, 0, %psr            ! re-disable traps
                   2719:        mov     %l6, %g6
                   2720:        mov     %l7, %g7
                   2721:
1.105     pk       2722:        !cmp    %o0, 0                  ! was this a soft nmi
                   2723:        !be     4f
                   2724:        !XXX - we need to unblock `mask all ints' only on a hard nmi
1.101     pk       2725:
1.52      pk       2726:        ! enable interrupts again (safe, we disabled traps again above)
                   2727:        sethi   %hi(ICR_SI_CLR), %o0
                   2728:        set     SINTR_MA, %o1
                   2729:        st      %o1, [%o0 + %lo(ICR_SI_CLR)]
                   2730:
1.101     pk       2731: 4:
1.52      pk       2732:        b       return_from_trap
                   2733:         wr     %l4, 0, %y              ! restore y
                   2734: #endif /* SUN4M */
                   2735:
                   2736: #ifdef GPROF
                   2737:        .globl  window_of, winof_user
                   2738:        .globl  window_uf, winuf_user, winuf_ok, winuf_invalid
                   2739:        .globl  return_from_trap, rft_kernel, rft_user, rft_invalid
                   2740:        .globl  softtrap, slowtrap
1.122     christos 2741:        .globl  clean_trap_window, _C_LABEL(_syscall)
1.52      pk       2742: #endif
1.1       deraadt  2743:
                   2744: /*
                   2745:  * Window overflow trap handler.
                   2746:  *     %l0 = %psr
                   2747:  *     %l1 = return pc
                   2748:  *     %l2 = return npc
                   2749:  */
                   2750: window_of:
                   2751: #ifdef TRIVIAL_WINDOW_OVERFLOW_HANDLER
                   2752:        /* a trivial version that assumes %sp is ok */
                   2753:        /* (for testing only!) */
                   2754:        save    %g0, %g0, %g0
                   2755:        std     %l0, [%sp + (0*8)]
                   2756:        rd      %psr, %l0
                   2757:        mov     1, %l1
                   2758:        sll     %l1, %l0, %l0
                   2759:        wr      %l0, 0, %wim
                   2760:        std     %l2, [%sp + (1*8)]
                   2761:        std     %l4, [%sp + (2*8)]
                   2762:        std     %l6, [%sp + (3*8)]
                   2763:        std     %i0, [%sp + (4*8)]
                   2764:        std     %i2, [%sp + (5*8)]
                   2765:        std     %i4, [%sp + (6*8)]
                   2766:        std     %i6, [%sp + (7*8)]
                   2767:        restore
                   2768:        RETT
                   2769: #else
                   2770:        /*
                   2771:         * This is similar to TRAP_SETUP, but we do not want to spend
                   2772:         * a lot of time, so we have separate paths for kernel and user.
                   2773:         * We also know for sure that the window has overflowed.
                   2774:         */
                   2775:        btst    PSR_PS, %l0
                   2776:        bz      winof_user
                   2777:         sethi  %hi(clean_trap_window), %l7
                   2778:
                   2779:        /*
                   2780:         * Overflow from kernel mode.  Call clean_trap_window to
                   2781:         * do the dirty work, then just return, since we know prev
                   2782:         * window is valid.  clean_trap_windows might dump all *user*
                   2783:         * windows into the pcb, but we do not care: there is at
                   2784:         * least one kernel window (a trap or interrupt frame!)
                   2785:         * above us.
                   2786:         */
                   2787:        jmpl    %l7 + %lo(clean_trap_window), %l4
                   2788:         mov    %g7, %l7                ! for clean_trap_window
                   2789:
                   2790:        wr      %l0, 0, %psr            ! put back the @%*! cond. codes
                   2791:        nop                             ! (let them settle in)
                   2792:        RETT
                   2793:
                   2794: winof_user:
                   2795:        /*
                   2796:         * Overflow from user mode.
                   2797:         * If clean_trap_window dumps the registers into the pcb,
                   2798:         * rft_user will need to call trap(), so we need space for
                   2799:         * a trap frame.  We also have to compute pcb_nw.
                   2800:         *
                   2801:         * SHOULD EXPAND IN LINE TO AVOID BUILDING TRAP FRAME ON
                   2802:         * `EASY' SAVES
                   2803:         */
1.111     pk       2804:        sethi   %hi(cpcb), %l6
                   2805:        ld      [%l6 + %lo(cpcb)], %l6
1.1       deraadt  2806:        ld      [%l6 + PCB_WIM], %l5
                   2807:        and     %l0, 31, %l3
                   2808:        sub     %l3, %l5, %l5           /* l5 = CWP - pcb_wim */
                   2809:        set     uwtab, %l4
                   2810:        ldub    [%l4 + %l5], %l5        /* l5 = uwtab[l5] */
                   2811:        st      %l5, [%l6 + PCB_UW]
                   2812:        jmpl    %l7 + %lo(clean_trap_window), %l4
                   2813:         mov    %g7, %l7                ! for clean_trap_window
1.111     pk       2814:        sethi   %hi(cpcb), %l6
                   2815:        ld      [%l6 + %lo(cpcb)], %l6
1.13      deraadt  2816:        set     USPACE-CCFSZ-80, %l5
1.1       deraadt  2817:        add     %l6, %l5, %sp           /* over to kernel stack */
                   2818:        CHECK_SP_REDZONE(%l6, %l5)
                   2819:
                   2820:        /*
                   2821:         * Copy return_from_trap far enough to allow us
                   2822:         * to jump directly to rft_user_or_recover_pcb_windows
                   2823:         * (since we know that is where we are headed).
                   2824:         */
                   2825: !      and     %l0, 31, %l3            ! still set (clean_trap_window
                   2826:                                        ! leaves this register alone)
                   2827:        set     wmask, %l6
                   2828:        ldub    [%l6 + %l3], %l5        ! %l5 = 1 << ((CWP + 1) % nwindows)
                   2829:        b       rft_user_or_recover_pcb_windows
                   2830:         rd     %wim, %l4               ! (read %wim first)
                   2831: #endif /* end `real' version of window overflow trap handler */
                   2832:
                   2833: /*
                   2834:  * Window underflow trap handler.
                   2835:  *     %l0 = %psr
                   2836:  *     %l1 = return pc
                   2837:  *     %l2 = return npc
                   2838:  *
                   2839:  * A picture:
                   2840:  *
                   2841:  *       T R I X
                   2842:  *     0 0 0 1 0 0 0   (%wim)
                   2843:  * [bit numbers increase towards the right;
                   2844:  * `restore' moves right & `save' moves left]
                   2845:  *
                   2846:  * T is the current (Trap) window, R is the window that attempted
                   2847:  * a `Restore' instruction, I is the Invalid window, and X is the
                   2848:  * window we want to make invalid before we return.
                   2849:  *
                   2850:  * Since window R is valid, we cannot use rft_user to restore stuff
                   2851:  * for us.  We have to duplicate its logic.  YUCK.
                   2852:  *
                   2853:  * Incidentally, TRIX are for kids.  Silly rabbit!
                   2854:  */
                   2855: window_uf:
                   2856: #ifdef TRIVIAL_WINDOW_UNDERFLOW_HANDLER
                   2857:        wr      %g0, 0, %wim            ! allow us to enter I
                   2858:        restore                         ! to R
                   2859:        nop
                   2860:        nop
                   2861:        restore                         ! to I
                   2862:        restore %g0, 1, %l1             ! to X
                   2863:        rd      %psr, %l0
                   2864:        sll     %l1, %l0, %l0
                   2865:        wr      %l0, 0, %wim
                   2866:        save    %g0, %g0, %g0           ! back to I
                   2867:        LOADWIN(%sp)
                   2868:        save    %g0, %g0, %g0           ! back to R
                   2869:        save    %g0, %g0, %g0           ! back to T
                   2870:        RETT
                   2871: #else
                   2872:        wr      %g0, 0, %wim            ! allow us to enter I
                   2873:        btst    PSR_PS, %l0
                   2874:        restore                         ! enter window R
                   2875:        bz      winuf_user
                   2876:         restore                        ! enter window I
                   2877:
                   2878:        /*
                   2879:         * Underflow from kernel mode.  Just recover the
                   2880:         * registers and go (except that we have to update
                   2881:         * the blasted user pcb fields).
                   2882:         */
                   2883:        restore %g0, 1, %l1             ! enter window X, then set %l1 to 1
                   2884:        rd      %psr, %l0               ! cwp = %psr & 31;
                   2885:        and     %l0, 31, %l0
                   2886:        sll     %l1, %l0, %l1           ! wim = 1 << cwp;
                   2887:        wr      %l1, 0, %wim            ! setwim(wim);
1.111     pk       2888:        sethi   %hi(cpcb), %l1
                   2889:        ld      [%l1 + %lo(cpcb)], %l1
1.1       deraadt  2890:        st      %l0, [%l1 + PCB_WIM]    ! cpcb->pcb_wim = cwp;
                   2891:        save    %g0, %g0, %g0           ! back to window I
                   2892:        LOADWIN(%sp)
                   2893:        save    %g0, %g0, %g0           ! back to R
                   2894:        save    %g0, %g0, %g0           ! and then to T
                   2895:        wr      %l0, 0, %psr            ! fix those cond codes....
                   2896:        nop                             ! (let them settle in)
                   2897:        RETT
                   2898:
                   2899: winuf_user:
                   2900:        /*
                   2901:         * Underflow from user mode.
                   2902:         *
                   2903:         * We cannot use rft_user (as noted above) because
                   2904:         * we must re-execute the `restore' instruction.
                   2905:         * Since it could be, e.g., `restore %l0,0,%l0',
                   2906:         * it is not okay to touch R's registers either.
                   2907:         *
                   2908:         * We are now in window I.
                   2909:         */
                   2910:        btst    7, %sp                  ! if unaligned, it is invalid
                   2911:        bne     winuf_invalid
                   2912:         EMPTY
                   2913:
1.111     pk       2914:        sethi   %hi(_C_LABEL(pgofset)), %l4
                   2915:        ld      [%l4 + %lo(_C_LABEL(pgofset))], %l4
1.62      pk       2916:        PTE_OF_ADDR(%sp, %l7, winuf_invalid, %l4, NOP_ON_4M_5)
                   2917:        CMP_PTE_USER_READ(%l7, %l5, NOP_ON_4M_6) ! if first page not readable,
1.1       deraadt  2918:        bne     winuf_invalid           ! it is invalid
                   2919:         EMPTY
1.13      deraadt  2920:        SLT_IF_1PAGE_RW(%sp, %l7, %l4)  ! first page is readable
1.1       deraadt  2921:        bl,a    winuf_ok                ! if only one page, enter window X
                   2922:         restore %g0, 1, %l1            ! and goto ok, & set %l1 to 1
                   2923:        add     %sp, 7*8, %l5
1.13      deraadt  2924:        add     %l4, 62, %l4
1.62      pk       2925:        PTE_OF_ADDR(%l5, %l7, winuf_invalid, %l4, NOP_ON_4M_7)
                   2926:        CMP_PTE_USER_READ(%l7, %l5, NOP_ON_4M_8) ! check second page too
1.1       deraadt  2927:        be,a    winuf_ok                ! enter window X and goto ok
                   2928:         restore %g0, 1, %l1            ! (and then set %l1 to 1)
                   2929:
                   2930: winuf_invalid:
                   2931:        /*
                   2932:         * We were unable to restore the window because %sp
                   2933:         * is invalid or paged out.  Return to the trap window
                   2934:         * and call trap(T_WINUF).  This will save R to the user
                   2935:         * stack, then load both R and I into the pcb rw[] area,
                   2936:         * and return with pcb_nsaved set to -1 for success, 0 for
                   2937:         * failure.  `Failure' indicates that someone goofed with the
                   2938:         * trap registers (e.g., signals), so that we need to return
                   2939:         * from the trap as from a syscall (probably to a signal handler)
                   2940:         * and let it retry the restore instruction later.  Note that
                   2941:         * window R will have been pushed out to user space, and thus
                   2942:         * be the invalid window, by the time we get back here.  (We
                   2943:         * continue to label it R anyway.)  We must also set %wim again,
                   2944:         * and set pcb_uw to 1, before enabling traps.  (Window R is the
                   2945:         * only window, and it is a user window).
                   2946:         */
                   2947:        save    %g0, %g0, %g0           ! back to R
                   2948:        save    %g0, 1, %l4             ! back to T, then %l4 = 1
1.111     pk       2949:        sethi   %hi(cpcb), %l6
                   2950:        ld      [%l6 + %lo(cpcb)], %l6
1.1       deraadt  2951:        st      %l4, [%l6 + PCB_UW]     ! pcb_uw = 1
                   2952:        ld      [%l6 + PCB_WIM], %l5    ! get log2(%wim)
                   2953:        sll     %l4, %l5, %l4           ! %l4 = old %wim
                   2954:        wr      %l4, 0, %wim            ! window I is now invalid again
1.13      deraadt  2955:        set     USPACE-CCFSZ-80, %l5
1.1       deraadt  2956:        add     %l6, %l5, %sp           ! get onto kernel stack
                   2957:        CHECK_SP_REDZONE(%l6, %l5)
                   2958:
                   2959:        /*
                   2960:         * Okay, call trap(T_WINUF, psr, pc, &tf).
                   2961:         * See `slowtrap' above for operation.
                   2962:         */
                   2963:        wr      %l0, PSR_ET, %psr
                   2964:        std     %l0, [%sp + CCFSZ + 0]  ! tf.tf_psr, tf.tf_pc
                   2965:        rd      %y, %l3
                   2966:        std     %l2, [%sp + CCFSZ + 8]  ! tf.tf_npc, tf.tf_y
                   2967:        mov     T_WINUF, %o0
                   2968:        st      %g1, [%sp + CCFSZ + 20] ! tf.tf_global[1]
                   2969:        mov     %l0, %o1
                   2970:        std     %g2, [%sp + CCFSZ + 24] ! etc
                   2971:        mov     %l1, %o2
                   2972:        std     %g4, [%sp + CCFSZ + 32]
                   2973:        add     %sp, CCFSZ, %o3
                   2974:        std     %g6, [%sp + CCFSZ + 40]
                   2975:        std     %i0, [%sp + CCFSZ + 48] ! tf.tf_out[0], etc
                   2976:        std     %i2, [%sp + CCFSZ + 56]
                   2977:        std     %i4, [%sp + CCFSZ + 64]
1.111     pk       2978:        call    _C_LABEL(trap)          ! trap(T_WINUF, pc, psr, &tf)
1.1       deraadt  2979:         std    %i6, [%sp + CCFSZ + 72] ! tf.tf_out[6]
                   2980:
                   2981:        ldd     [%sp + CCFSZ + 0], %l0  ! new psr, pc
                   2982:        ldd     [%sp + CCFSZ + 8], %l2  ! new npc, %y
                   2983:        wr      %l3, 0, %y
                   2984:        ld      [%sp + CCFSZ + 20], %g1
                   2985:        ldd     [%sp + CCFSZ + 24], %g2
                   2986:        ldd     [%sp + CCFSZ + 32], %g4
                   2987:        ldd     [%sp + CCFSZ + 40], %g6
                   2988:        ldd     [%sp + CCFSZ + 48], %i0 ! %o0 for window R, etc
                   2989:        ldd     [%sp + CCFSZ + 56], %i2
                   2990:        ldd     [%sp + CCFSZ + 64], %i4
                   2991:        wr      %l0, 0, %psr            ! disable traps: test must be atomic
                   2992:        ldd     [%sp + CCFSZ + 72], %i6
1.111     pk       2993:        sethi   %hi(cpcb), %l6
                   2994:        ld      [%l6 + %lo(cpcb)], %l6
1.1       deraadt  2995:        ld      [%l6 + PCB_NSAVED], %l7 ! if nsaved is -1, we have our regs
                   2996:        tst     %l7
                   2997:        bl,a    1f                      ! got them
                   2998:         wr     %g0, 0, %wim            ! allow us to enter windows R, I
                   2999:        b,a     return_from_trap
                   3000:
                   3001:        /*
                   3002:         * Got 'em.  Load 'em up.
                   3003:         */
                   3004: 1:
                   3005:        mov     %g6, %l3                ! save %g6; set %g6 = cpcb
                   3006:        mov     %l6, %g6
                   3007:        st      %g0, [%g6 + PCB_NSAVED] ! and clear magic flag
                   3008:        restore                         ! from T to R
                   3009:        restore                         ! from R to I
                   3010:        restore %g0, 1, %l1             ! from I to X, then %l1 = 1
                   3011:        rd      %psr, %l0               ! cwp = %psr;
                   3012:        sll     %l1, %l0, %l1
                   3013:        wr      %l1, 0, %wim            ! make window X invalid
                   3014:        and     %l0, 31, %l0
                   3015:        st      %l0, [%g6 + PCB_WIM]    ! cpcb->pcb_wim = cwp;
                   3016:        nop                             ! unnecessary? old wim was 0...
                   3017:        save    %g0, %g0, %g0           ! back to I
                   3018:        LOADWIN(%g6 + PCB_RW + 64)      ! load from rw[1]
                   3019:        save    %g0, %g0, %g0           ! back to R
                   3020:        LOADWIN(%g6 + PCB_RW)           ! load from rw[0]
                   3021:        save    %g0, %g0, %g0           ! back to T
                   3022:        wr      %l0, 0, %psr            ! restore condition codes
                   3023:        mov     %l3, %g6                ! fix %g6
                   3024:        RETT
                   3025:
                   3026:        /*
                   3027:         * Restoring from user stack, but everything has checked out
                   3028:         * as good.  We are now in window X, and %l1 = 1.  Window R
                   3029:         * is still valid and holds user values.
                   3030:         */
                   3031: winuf_ok:
                   3032:        rd      %psr, %l0
                   3033:        sll     %l1, %l0, %l1
                   3034:        wr      %l1, 0, %wim            ! make this one invalid
1.111     pk       3035:        sethi   %hi(cpcb), %l2
                   3036:        ld      [%l2 + %lo(cpcb)], %l2
1.1       deraadt  3037:        and     %l0, 31, %l0
                   3038:        st      %l0, [%l2 + PCB_WIM]    ! cpcb->pcb_wim = cwp;
                   3039:        save    %g0, %g0, %g0           ! back to I
                   3040:        LOADWIN(%sp)
                   3041:        save    %g0, %g0, %g0           ! back to R
                   3042:        save    %g0, %g0, %g0           ! back to T
                   3043:        wr      %l0, 0, %psr            ! restore condition codes
                   3044:        nop                             ! it takes three to tangle
                   3045:        RETT
                   3046: #endif /* end `real' version of window underflow trap handler */
                   3047:
                   3048: /*
                   3049:  * Various return-from-trap routines (see return_from_trap).
                   3050:  */
                   3051:
                   3052: /*
                   3053:  * Return from trap, to kernel.
                   3054:  *     %l0 = %psr
                   3055:  *     %l1 = return pc
                   3056:  *     %l2 = return npc
                   3057:  *     %l4 = %wim
                   3058:  *     %l5 = bit for previous window
                   3059:  */
                   3060: rft_kernel:
                   3061:        btst    %l5, %l4                ! if (wim & l5)
                   3062:        bnz     1f                      !       goto reload;
                   3063:         wr     %l0, 0, %psr            ! but first put !@#*% cond codes back
                   3064:
                   3065:        /* previous window is valid; just rett */
                   3066:        nop                             ! wait for cond codes to settle in
                   3067:        RETT
                   3068:
                   3069:        /*
                   3070:         * Previous window is invalid.
                   3071:         * Update %wim and then reload l0..i7 from frame.
                   3072:         *
                   3073:         *        T I X
                   3074:         *      0 0 1 0 0   (%wim)
                   3075:         * [see picture in window_uf handler]
                   3076:         *
                   3077:         * T is the current (Trap) window, I is the Invalid window,
                   3078:         * and X is the window we want to make invalid.  Window X
                   3079:         * currently has no useful values.
                   3080:         */
                   3081: 1:
                   3082:        wr      %g0, 0, %wim            ! allow us to enter window I
                   3083:        nop; nop; nop                   ! (it takes a while)
                   3084:        restore                         ! enter window I
                   3085:        restore %g0, 1, %l1             ! enter window X, then %l1 = 1
                   3086:        rd      %psr, %l0               ! CWP = %psr & 31;
                   3087:        and     %l0, 31, %l0
                   3088:        sll     %l1, %l0, %l1           ! wim = 1 << CWP;
                   3089:        wr      %l1, 0, %wim            ! setwim(wim);
1.111     pk       3090:        sethi   %hi(cpcb), %l1
                   3091:        ld      [%l1 + %lo(cpcb)], %l1
1.1       deraadt  3092:        st      %l0, [%l1 + PCB_WIM]    ! cpcb->pcb_wim = l0 & 31;
                   3093:        save    %g0, %g0, %g0           ! back to window I
                   3094:        LOADWIN(%sp)
                   3095:        save    %g0, %g0, %g0           ! back to window T
                   3096:        /*
                   3097:         * Note that the condition codes are still set from
                   3098:         * the code at rft_kernel; we can simply return.
                   3099:         */
                   3100:        RETT
                   3101:
                   3102: /*
                   3103:  * Return from trap, to user.  Checks for scheduling trap (`ast') first;
                   3104:  * will re-enter trap() if set.  Note that we may have to switch from
                   3105:  * the interrupt stack to the kernel stack in this case.
                   3106:  *     %l0 = %psr
                   3107:  *     %l1 = return pc
                   3108:  *     %l2 = return npc
                   3109:  *     %l4 = %wim
                   3110:  *     %l5 = bit for previous window
                   3111:  *     %l6 = cpcb
                   3112:  * If returning to a valid window, just set psr and return.
                   3113:  */
                   3114: rft_user:
1.111     pk       3115: !      sethi   %hi(_C_LABEL(want_ast)), %l7    ! (done below)
                   3116:        ld      [%l7 + %lo(_C_LABEL(want_ast))], %l7
1.1       deraadt  3117:        tst     %l7                     ! want AST trap?
                   3118:        bne,a   softtrap                ! yes, re-enter trap with type T_AST
                   3119:         mov    T_AST, %o0
                   3120:
                   3121:        btst    %l5, %l4                ! if (wim & l5)
                   3122:        bnz     1f                      !       goto reload;
                   3123:         wr     %l0, 0, %psr            ! restore cond codes
                   3124:        nop                             ! (three instruction delay)
                   3125:        RETT
                   3126:
                   3127:        /*
                   3128:         * Previous window is invalid.
                   3129:         * Before we try to load it, we must verify its stack pointer.
                   3130:         * This is much like the underflow handler, but a bit easier
                   3131:         * since we can use our own local registers.
                   3132:         */
                   3133: 1:
                   3134:        btst    7, %fp                  ! if unaligned, address is invalid
                   3135:        bne     rft_invalid
                   3136:         EMPTY
                   3137:
1.111     pk       3138:        sethi   %hi(_C_LABEL(pgofset)), %l3
                   3139:        ld      [%l3 + %lo(_C_LABEL(pgofset))], %l3
1.62      pk       3140:        PTE_OF_ADDR(%fp, %l7, rft_invalid, %l3, NOP_ON_4M_9)
                   3141:        CMP_PTE_USER_READ(%l7, %l5, NOP_ON_4M_10)       ! try first page
1.1       deraadt  3142:        bne     rft_invalid             ! no good
                   3143:         EMPTY
1.13      deraadt  3144:        SLT_IF_1PAGE_RW(%fp, %l7, %l3)
1.1       deraadt  3145:        bl,a    rft_user_ok             ! only 1 page: ok
                   3146:         wr     %g0, 0, %wim
                   3147:        add     %fp, 7*8, %l5
1.13      deraadt  3148:        add     %l3, 62, %l3
1.62      pk       3149:        PTE_OF_ADDR(%l5, %l7, rft_invalid, %l3, NOP_ON_4M_11)
                   3150:        CMP_PTE_USER_READ(%l7, %l5, NOP_ON_4M_12)       ! check 2nd page too
1.1       deraadt  3151:        be,a    rft_user_ok
                   3152:         wr     %g0, 0, %wim
                   3153:
                   3154:        /*
                   3155:         * The window we wanted to pull could not be pulled.  Instead,
                   3156:         * re-enter trap with type T_RWRET.  This will pull the window
                   3157:         * into cpcb->pcb_rw[0] and set cpcb->pcb_nsaved to -1, which we
                   3158:         * will detect when we try to return again.
                   3159:         */
                   3160: rft_invalid:
                   3161:        b       softtrap
                   3162:         mov    T_RWRET, %o0
                   3163:
                   3164:        /*
                   3165:         * The window we want to pull can be pulled directly.
                   3166:         */
                   3167: rft_user_ok:
                   3168: !      wr      %g0, 0, %wim            ! allow us to get into it
                   3169:        wr      %l0, 0, %psr            ! fix up the cond codes now
                   3170:        nop; nop; nop
                   3171:        restore                         ! enter window I
                   3172:        restore %g0, 1, %l1             ! enter window X, then %l1 = 1
                   3173:        rd      %psr, %l0               ! l0 = (junk << 5) + CWP;
                   3174:        sll     %l1, %l0, %l1           ! %wim = 1 << CWP;
                   3175:        wr      %l1, 0, %wim
1.111     pk       3176:        sethi   %hi(cpcb), %l1
                   3177:        ld      [%l1 + %lo(cpcb)], %l1
1.1       deraadt  3178:        and     %l0, 31, %l0
                   3179:        st      %l0, [%l1 + PCB_WIM]    ! cpcb->pcb_wim = l0 & 31;
                   3180:        save    %g0, %g0, %g0           ! back to window I
                   3181:        LOADWIN(%sp)                    ! suck hard
                   3182:        save    %g0, %g0, %g0           ! back to window T
                   3183:        RETT
                   3184:
                   3185: /*
                   3186:  * Return from trap.  Entered after a
                   3187:  *     wr      %l0, 0, %psr
                   3188:  * which disables traps so that we can rett; registers are:
                   3189:  *
                   3190:  *     %l0 = %psr
                   3191:  *     %l1 = return pc
                   3192:  *     %l2 = return npc
                   3193:  *
                   3194:  * (%l3..%l7 anything).
                   3195:  *
                   3196:  * If we are returning to user code, we must:
                   3197:  *  1.  Check for register windows in the pcb that belong on the stack.
                   3198:  *     If there are any, reenter trap with type T_WINOF.
                   3199:  *  2.  Make sure the register windows will not underflow.  This is
                   3200:  *     much easier in kernel mode....
                   3201:  */
                   3202: return_from_trap:
                   3203: !      wr      %l0, 0, %psr            ! disable traps so we can rett
                   3204: ! (someone else did this already)
                   3205:        and     %l0, 31, %l5
                   3206:        set     wmask, %l6
                   3207:        ldub    [%l6 + %l5], %l5        ! %l5 = 1 << ((CWP + 1) % nwindows)
                   3208:        btst    PSR_PS, %l0             ! returning to userland?
                   3209:        bnz     rft_kernel              ! no, go return to kernel
                   3210:         rd     %wim, %l4               ! (read %wim in any case)
                   3211:
                   3212: rft_user_or_recover_pcb_windows:
                   3213:        /*
                   3214:         * (entered with %l4=%wim, %l5=wmask[cwp]; %l0..%l2 as usual)
                   3215:         *
                   3216:         * check cpcb->pcb_nsaved:
                   3217:         * if 0, do a `normal' return to user (see rft_user);
                   3218:         * if > 0, cpcb->pcb_rw[] holds registers to be copied to stack;
                   3219:         * if -1, cpcb->pcb_rw[0] holds user registers for rett window
                   3220:         * from an earlier T_RWRET pseudo-trap.
                   3221:         */
1.111     pk       3222:        sethi   %hi(cpcb), %l6
                   3223:        ld      [%l6 + %lo(cpcb)], %l6
1.1       deraadt  3224:        ld      [%l6 + PCB_NSAVED], %l7
                   3225:        tst     %l7
                   3226:        bz,a    rft_user
1.111     pk       3227:         sethi  %hi(_C_LABEL(want_ast)), %l7    ! first instr of rft_user
1.1       deraadt  3228:
                   3229:        bg,a    softtrap                ! if (pcb_nsaved > 0)
                   3230:         mov    T_WINOF, %o0            !       trap(T_WINOF);
                   3231:
                   3232:        /*
                   3233:         * To get here, we must have tried to return from a previous
                   3234:         * trap and discovered that it would cause a window underflow.
                   3235:         * We then must have tried to pull the registers out of the
                   3236:         * user stack (from the address in %fp==%i6) and discovered
                   3237:         * that it was either unaligned or not loaded in memory, and
                   3238:         * therefore we ran a trap(T_RWRET), which loaded one set of
                   3239:         * registers into cpcb->pcb_pcb_rw[0] (if it had killed the
                   3240:         * process due to a bad stack, we would not be here).
                   3241:         *
                   3242:         * We want to load pcb_rw[0] into the previous window, which
                   3243:         * we know is currently invalid.  In other words, we want
                   3244:         * %wim to be 1 << ((cwp + 2) % nwindows).
                   3245:         */
                   3246:        wr      %g0, 0, %wim            ! enable restores
                   3247:        mov     %g6, %l3                ! save g6 in l3
                   3248:        mov     %l6, %g6                ! set g6 = &u
                   3249:        st      %g0, [%g6 + PCB_NSAVED] ! clear cpcb->pcb_nsaved
                   3250:        restore                         ! enter window I
                   3251:        restore %g0, 1, %l1             ! enter window X, then %l1 = 1
                   3252:        rd      %psr, %l0
                   3253:        sll     %l1, %l0, %l1           ! %wim = 1 << CWP;
                   3254:        wr      %l1, 0, %wim
                   3255:        and     %l0, 31, %l0
                   3256:        st      %l0, [%g6 + PCB_WIM]    ! cpcb->pcb_wim = CWP;
                   3257:        nop                             ! unnecessary? old wim was 0...
                   3258:        save    %g0, %g0, %g0           ! back to window I
                   3259:        LOADWIN(%g6 + PCB_RW)
                   3260:        save    %g0, %g0, %g0           ! back to window T (trap window)
                   3261:        wr      %l0, 0, %psr            ! cond codes, cond codes everywhere
                   3262:        mov     %l3, %g6                ! restore g6
                   3263:        RETT
                   3264:
                   3265: ! exported end marker for kernel gdb
1.111     pk       3266:        .globl  _C_LABEL(endtrapcode)
                   3267: _C_LABEL(endtrapcode):
1.1       deraadt  3268:
                   3269: /*
                   3270:  * init_tables(nwin) int nwin;
                   3271:  *
                   3272:  * Set up the uwtab and wmask tables.
                   3273:  * We know nwin > 1.
                   3274:  */
                   3275: init_tables:
                   3276:        /*
                   3277:         * for (i = -nwin, j = nwin - 2; ++i < 0; j--)
                   3278:         *      uwtab[i] = j;
                   3279:         * (loop runs at least once)
                   3280:         */
                   3281:        set     uwtab, %o3
                   3282:        sub     %g0, %o0, %o1           ! i = -nwin + 1
                   3283:        inc     %o1
                   3284:        add     %o0, -2, %o2            ! j = nwin - 2;
                   3285: 0:
                   3286:        stb     %o2, [%o3 + %o1]        ! uwtab[i] = j;
                   3287: 1:
                   3288:        inccc   %o1                     ! ++i < 0?
                   3289:        bl      0b                      ! yes, continue loop
                   3290:         dec    %o2                     ! in any case, j--
                   3291:
                   3292:        /*
                   3293:         * (i now equals 0)
                   3294:         * for (j = nwin - 1; i < nwin; i++, j--)
                   3295:         *      uwtab[i] = j;
                   3296:         * (loop runs at least twice)
                   3297:         */
                   3298:        sub     %o0, 1, %o2             ! j = nwin - 1
                   3299: 0:
                   3300:        stb     %o2, [%o3 + %o1]        ! uwtab[i] = j
                   3301:        inc     %o1                     ! i++
                   3302: 1:
                   3303:        cmp     %o1, %o0                ! i < nwin?
                   3304:        bl      0b                      ! yes, continue
                   3305:         dec    %o2                     ! in any case, j--
                   3306:
                   3307:        /*
                   3308:         * We observe that, for i in 0..nwin-2, (i+1)%nwin == i+1;
                   3309:         * for i==nwin-1, (i+1)%nwin == 0.
                   3310:         * To avoid adding 1, we run i from 1 to nwin and set
                   3311:         * wmask[i-1].
                   3312:         *
                   3313:         * for (i = j = 1; i < nwin; i++) {
                   3314:         *      j <<= 1;        (j now == 1 << i)
                   3315:         *      wmask[i - 1] = j;
                   3316:         * }
                   3317:         * (loop runs at least once)
                   3318:         */
                   3319:        set     wmask - 1, %o3
                   3320:        mov     1, %o1                  ! i = 1;
                   3321:        mov     2, %o2                  ! j = 2;
                   3322: 0:
                   3323:        stb     %o2, [%o3 + %o1]        ! (wmask - 1)[i] = j;
                   3324:        inc     %o1                     ! i++
                   3325:        cmp     %o1, %o0                ! i < nwin?
                   3326:        bl,a    0b                      ! yes, continue
                   3327:         sll    %o2, 1, %o2             ! (and j <<= 1)
                   3328:
                   3329:        /*
                   3330:         * Now i==nwin, so we want wmask[i-1] = 1.
                   3331:         */
                   3332:        mov     1, %o2                  ! j = 1;
                   3333:        retl
                   3334:         stb    %o2, [%o3 + %o1]        ! (wmask - 1)[i] = j;
                   3335:
1.13      deraadt  3336: #ifdef SUN4
                   3337: /*
                   3338:  * getidprom(struct idprom *, sizeof(struct idprom))
                   3339:  */
1.111     pk       3340: _ENTRY(_C_LABEL(getidprom))
1.13      deraadt  3341:        set     AC_IDPROM, %o2
                   3342: 1:     lduba   [%o2] ASI_CONTROL, %o3
                   3343:        stb     %o3, [%o0]
                   3344:        inc     %o0
                   3345:        inc     %o2
                   3346:        dec     %o1
                   3347:        cmp     %o1, 0
                   3348:        bne     1b
                   3349:         nop
                   3350:        retl
                   3351:         nop
                   3352: #endif
                   3353:
1.1       deraadt  3354: dostart:
1.32      pk       3355:        /*
                   3356:         * Startup.
                   3357:         *
                   3358:         * We have been loaded in low RAM, at some address which
1.119     christos 3359:         * is page aligned (PROM_LOADADDR actually) rather than where we
                   3360:         * want to run (KERNBASE+PROM_LOADADDR).  Until we get everything set,
1.32      pk       3361:         * we have to be sure to use only pc-relative addressing.
                   3362:         */
                   3363:
1.27      pk       3364: #ifdef DDB
                   3365:        /*
1.117     christos 3366:         * We now use the bootinfo method to pass arguments, and the new
                   3367:         * magic number indicates that. A pointer to esym is passed in
                   3368:         * %o4[0] and the bootinfo structure is passed in %o4[1].
                   3369:         *
                   3370:         * For compatibility with older versions, we check for DDB arguments
                   3371:         * if the older magic number is there. The loader passes `esym' in
                   3372:         * %o4.
1.40      pk       3373:         * A DDB magic number is passed in %o5 to allow for bootloaders
1.27      pk       3374:         * that know nothing about DDB symbol loading conventions.
1.40      pk       3375:         * Note: we don't touch %o1-%o3; SunOS bootloaders seem to use them
                   3376:         * for their own mirky business.
1.73      pk       3377:         *
                   3378:         * Pre-NetBSD 1.3 bootblocks had KERNBASE compiled in, and used
1.112     pk       3379:         * it to compute the value of `esym'. In order to successfully
1.73      pk       3380:         * boot a kernel built with a different value for KERNBASE using
1.112     pk       3381:         * old bootblocks, we fixup `esym' here by the difference between
1.73      pk       3382:         * KERNBASE and the old value (known to be 0xf8000000) compiled
                   3383:         * into pre-1.3 bootblocks.
                   3384:         * We use the magic number passed as the sixth argument to
                   3385:         * distinguish bootblock versions.
1.27      pk       3386:         */
1.117     christos 3387:        set     KERNBASE, %l4
                   3388:
                   3389:        set     0x44444232, %l3         ! bootinfo magic
                   3390:        cmp     %o5, %l3
                   3391:        bne     1f
1.118     pk       3392:         nop
                   3393:
                   3394:        /* The loader has passed to us a `bootinfo' structure */
                   3395:        ld      [%o4], %l3              ! 1st word is esym
1.117     christos 3396:        add     %l3, %l4, %o5           ! relocate
                   3397:        sethi   %hi(_C_LABEL(esym) - KERNBASE), %l3     ! store esym
                   3398:        st      %o5, [%l3 + %lo(_C_LABEL(esym) - KERNBASE)]
1.120     pk       3399:
                   3400:        ld      [%o4 + 4], %l3          ! 2nd word is bootinfo
1.117     christos 3401:        add     %l3, %l4, %o5           ! relocate
                   3402:        sethi   %hi(_C_LABEL(bootinfo) - KERNBASE), %l3 ! store bootinfo
                   3403:        st      %o5, [%l3 + %lo(_C_LABEL(bootinfo) - KERNBASE)]
1.118     pk       3404:        b,a     3f
1.117     christos 3405:
1.118     pk       3406: 1:
1.120     pk       3407:        /* Check for old-style DDB loader magic */
1.117     christos 3408:        set     0x44444231, %l3         ! ddb magic
                   3409:        cmp     %o5, %l3
1.118     pk       3410:        be,a    2f
                   3411:         clr    %l4                     ! if DDB_MAGIC1, clear %l4
1.115     christos 3412:
1.117     christos 3413:        set     0x44444230, %l3         ! compat magic
                   3414:        cmp     %o5, %l3
                   3415:        bne     3f
1.73      pk       3416:
1.118     pk       3417:                                        ! note: %l4 set to KERNBASE above.
1.73      pk       3418:        set     0xf8000000, %l5         ! compute correction term:
                   3419:        sub     %l5, %l4, %l4           !  old KERNBASE (0xf8000000 ) - KERNBASE
                   3420:
1.117     christos 3421: 2:
1.40      pk       3422:        tst     %o4                     ! do we have the symbols?
1.117     christos 3423:        bz      3f
1.73      pk       3424:         sub    %o4, %l4, %o4           ! apply compat correction
1.112     pk       3425:        sethi   %hi(_C_LABEL(esym) - KERNBASE), %l3     ! store esym
                   3426:        st      %o4, [%l3 + %lo(_C_LABEL(esym) - KERNBASE)]
1.117     christos 3427: 3:
1.27      pk       3428: #endif
1.13      deraadt  3429:        /*
                   3430:         * Sun4 passes in the `load address'.  Although possible, its highly
                   3431:         * unlikely that OpenBoot would place the prom vector there.
                   3432:         */
1.119     christos 3433:        set     PROM_LOADADDR, %g7
1.17      pk       3434:        cmp     %o0, %g7
1.50      pk       3435:        be      is_sun4
1.14      deraadt  3436:         nop
                   3437:
1.20      deraadt  3438: #if defined(SUN4C) || defined(SUN4M)
1.14      deraadt  3439:        mov     %o0, %g7                ! save prom vector pointer
1.9       deraadt  3440:
1.109     pk       3441:        /* First, check `romp->pv_magic' */
                   3442:        ld      [%g7 + PV_MAGIC], %o0   ! v = pv->pv_magic
                   3443:        set     OBP_MAGIC, %o1
                   3444:        cmp     %o0, %o1                ! if ( v != OBP_MAGIC) {
                   3445:        bne     is_openfirm             !    assume this is an OPENFIRM machine
                   3446:         nop                            ! }
                   3447:
1.13      deraadt  3448:        /*
                   3449:         * are we on a sun4c or a sun4m?
                   3450:         */
1.28      deraadt  3451:        ld      [%g7 + PV_NODEOPS], %o4 ! node = pv->pv_nodeops->no_nextnode(0)
                   3452:        ld      [%o4 + NO_NEXTNODE], %o4
1.18      deraadt  3453:        call    %o4
                   3454:         mov    0, %o0                  ! node
1.37      pk       3455:
                   3456:        mov     %o0, %l0
1.111     pk       3457:        set     cputypvar-KERNBASE, %o1 ! name = "compatible"
                   3458:        set     cputypval-KERNBASE, %o2 ! buffer ptr (assume buffer long enough)
1.28      deraadt  3459:        ld      [%g7 + PV_NODEOPS], %o4 ! (void)pv->pv_nodeops->no_getprop(...)
                   3460:        ld      [%o4 + NO_GETPROP], %o4
1.18      deraadt  3461:        call     %o4
                   3462:         nop
1.111     pk       3463:        set     cputypval-KERNBASE, %o2 ! buffer ptr
1.18      deraadt  3464:        ldub    [%o2 + 4], %o0          ! which is it... "sun4c", "sun4m", "sun4d"?
                   3465:        cmp     %o0, 'c'
1.50      pk       3466:        be      is_sun4c
1.13      deraadt  3467:         nop
1.18      deraadt  3468:        cmp     %o0, 'm'
1.50      pk       3469:        be      is_sun4m
1.18      deraadt  3470:         nop
1.20      deraadt  3471: #endif /* SUN4C || SUN4M */
1.18      deraadt  3472:
                   3473:        ! ``on a sun4d?!  hell no!''
1.28      deraadt  3474:        ld      [%g7 + PV_HALT], %o1    ! by this kernel, then halt
1.18      deraadt  3475:        call    %o1
                   3476:         nop
                   3477:
1.109     pk       3478: is_openfirm:
                   3479:        mov     %o3, %g7                ! OPENFIRMWARE entry point is in %o3
                   3480:        /* FALLTHROUGH to sun4m case */
                   3481:
1.18      deraadt  3482: is_sun4m:
1.13      deraadt  3483: #if defined(SUN4M)
1.52      pk       3484:        set     trapbase_sun4m, %g6
1.13      deraadt  3485:        mov     SUN4CM_PGSHIFT, %g5
                   3486:        b       start_havetype
                   3487:         mov    CPU_SUN4M, %g4
                   3488: #else
1.9       deraadt  3489:        set     sun4m_notsup-KERNBASE, %o0
1.28      deraadt  3490:        ld      [%g7 + PV_EVAL], %o1
1.9       deraadt  3491:        call    %o1                     ! print a message saying that the
                   3492:         nop                            ! sun4m architecture is not supported
1.28      deraadt  3493:        ld      [%g7 + PV_HALT], %o1    ! by this kernel, then halt
1.9       deraadt  3494:        call    %o1
                   3495:         nop
1.13      deraadt  3496:        /*NOTREACHED*/
                   3497: #endif
                   3498: is_sun4c:
                   3499: #if defined(SUN4C)
1.52      pk       3500:        set     trapbase_sun4c, %g6
1.13      deraadt  3501:        mov     SUN4CM_PGSHIFT, %g5
                   3502:
                   3503:        set     AC_CONTEXT, %g1         ! paranoia: set context to kernel
                   3504:        stba    %g0, [%g1] ASI_CONTROL
                   3505:
                   3506:        b       start_havetype
                   3507:         mov    CPU_SUN4C, %g4          ! XXX CPU_SUN4
1.9       deraadt  3508: #else
                   3509:        set     sun4c_notsup-KERNBASE, %o0
1.28      deraadt  3510:
                   3511:        ld      [%g7 + PV_ROMVEC_VERS], %o1
                   3512:        cmp     %o1, 0
                   3513:        bne     1f
                   3514:         nop
                   3515:
                   3516:        ! stupid version 0 rom interface is pv_eval(int length, char *string)
                   3517:        mov     %o0, %o1
                   3518: 2:     ldub    [%o0], %o4
                   3519:        bne     2b
                   3520:         inc    %o0
                   3521:        dec     %o0
                   3522:        sub     %o0, %o1, %o0
                   3523:
                   3524: 1:     ld      [%g7 + PV_EVAL], %o2
                   3525:        call    %o2                     ! print a message saying that the
1.9       deraadt  3526:         nop                            ! sun4c architecture is not supported
1.28      deraadt  3527:        ld      [%g7 + PV_HALT], %o1    ! by this kernel, then halt
1.9       deraadt  3528:        call    %o1
                   3529:         nop
1.13      deraadt  3530:        /*NOTREACHED*/
1.9       deraadt  3531: #endif
1.13      deraadt  3532: is_sun4:
                   3533: #if defined(SUN4)
1.52      pk       3534:        set     trapbase_sun4, %g6
1.13      deraadt  3535:        mov     SUN4_PGSHIFT, %g5
1.1       deraadt  3536:
1.13      deraadt  3537:        set     AC_CONTEXT, %g1         ! paranoia: set context to kernel
                   3538:        stba    %g0, [%g1] ASI_CONTROL
                   3539:
                   3540:        b       start_havetype
1.14      deraadt  3541:         mov    CPU_SUN4, %g4
1.13      deraadt  3542: #else
1.14      deraadt  3543:        set     PROM_BASE, %g7
                   3544:
1.13      deraadt  3545:        set     sun4_notsup-KERNBASE, %o0
1.28      deraadt  3546:        ld      [%g7 + OLDMON_PRINTF], %o1
1.13      deraadt  3547:        call    %o1                     ! print a message saying that the
                   3548:         nop                            ! sun4 architecture is not supported
1.28      deraadt  3549:        ld      [%g7 + OLDMON_HALT], %o1 ! by this kernel, then halt
1.13      deraadt  3550:        call    %o1
                   3551:         nop
                   3552:        /*NOTREACHED*/
                   3553: #endif
                   3554:
                   3555: start_havetype:
1.1       deraadt  3556:        /*
                   3557:         * Step 1: double map low RAM (addresses [0.._end-start-1])
                   3558:         * to KERNBASE (addresses [KERNBASE.._end-1]).  None of these
                   3559:         * are `bad' aliases (since they are all on segment boundaries)
                   3560:         * so we do not have to worry about cache aliasing.
                   3561:         *
                   3562:         * We map in another couple of segments just to have some
                   3563:         * more memory (512K, actually) guaranteed available for
                   3564:         * bootstrap code (pmap_bootstrap needs memory to hold MMU
1.39      pk       3565:         * and context data structures). Note: this is only relevant
                   3566:         * for 2-level MMU sun4/sun4c machines.
1.1       deraadt  3567:         */
                   3568:        clr     %l0                     ! lowva
                   3569:        set     KERNBASE, %l1           ! highva
                   3570:        set     _end + (2 << 18), %l2   ! last va that must be remapped
1.8       pk       3571: #ifdef DDB
1.112     pk       3572:        sethi   %hi(_C_LABEL(esym) - KERNBASE), %o1
                   3573:        ld      [%o1+%lo(_C_LABEL(esym) - KERNBASE)], %o1
1.27      pk       3574:        tst     %o1
1.8       pk       3575:        bz      1f
                   3576:         nop
1.27      pk       3577:        set     (2 << 18), %l2
                   3578:        add     %l2, %o1, %l2           ! last va that must be remapped
1.8       pk       3579: 1:
                   3580: #endif
1.13      deraadt  3581:        /*
                   3582:         * Need different initial mapping functions for different
                   3583:         * types of machines.
                   3584:         */
                   3585: #if defined(SUN4C)
                   3586:        cmp     %g4, CPU_SUN4C
1.9       deraadt  3587:        bne     1f
1.14      deraadt  3588:         set    1 << 18, %l3            ! segment size in bytes
1.1       deraadt  3589: 0:
                   3590:        lduba   [%l0] ASI_SEGMAP, %l4   ! segmap[highva] = segmap[lowva];
                   3591:        stba    %l4, [%l1] ASI_SEGMAP
                   3592:        add     %l3, %l1, %l1           ! highva += segsiz;
                   3593:        cmp     %l1, %l2                ! done?
1.34      pk       3594:        blu     0b                      ! no, loop
1.1       deraadt  3595:         add    %l3, %l0, %l0           ! (and lowva += segsz)
1.125.2.2! bouyer   3596:        b,a     startmap_done
1.52      pk       3597: 1:
1.13      deraadt  3598: #endif /* SUN4C */
1.125.2.2! bouyer   3599:
1.13      deraadt  3600: #if defined(SUN4)
                   3601:        cmp     %g4, CPU_SUN4
                   3602:        bne     2f
1.114     pk       3603: #if defined(SUN4_MMU3L)
1.34      pk       3604:        set     AC_IDPROM+1, %l3
                   3605:        lduba   [%l3] ASI_CONTROL, %l3
                   3606:        cmp     %l3, 0x24 ! XXX - SUN4_400
                   3607:        bne     no_3mmu
1.125.2.1  bouyer   3608:         nop
1.125.2.2! bouyer   3609:
        !          3610:        /*
        !          3611:         * Three-level sun4 MMU.
        !          3612:         * Double-map by duplicating a single region entry (which covers
        !          3613:         * 16MB) corresponding to the kernel's virtual load address.
        !          3614:         */
1.34      pk       3615:        add     %l0, 2, %l0             ! get to proper half-word in RG space
                   3616:        add     %l1, 2, %l1
                   3617:        lduha   [%l0] ASI_REGMAP, %l4   ! regmap[highva] = regmap[lowva];
                   3618:        stha    %l4, [%l1] ASI_REGMAP
1.125.2.2! bouyer   3619:        b,a     startmap_done
1.34      pk       3620: no_3mmu:
                   3621: #endif
1.125.2.2! bouyer   3622:
        !          3623:        /*
        !          3624:         * Three-level sun4 MMU.
        !          3625:         * Double-map by duplicating the required number of segment
        !          3626:         * entries corresponding to the kernel's virtual load address.
        !          3627:         */
        !          3628:        set     1 << 18, %l3            ! segment size in bytes
1.13      deraadt  3629: 0:
                   3630:        lduha   [%l0] ASI_SEGMAP, %l4   ! segmap[highva] = segmap[lowva];
                   3631:        stha    %l4, [%l1] ASI_SEGMAP
                   3632:        add     %l3, %l1, %l1           ! highva += segsiz;
                   3633:        cmp     %l1, %l2                ! done?
1.34      pk       3634:        blu     0b                      ! no, loop
1.13      deraadt  3635:         add    %l3, %l0, %l0           ! (and lowva += segsz)
1.37      pk       3636:        b,a     startmap_done
1.52      pk       3637: 2:
1.13      deraadt  3638: #endif /* SUN4 */
1.125.2.2! bouyer   3639:
1.9       deraadt  3640: #if defined(SUN4M)
1.13      deraadt  3641:        cmp     %g4, CPU_SUN4M          ! skip for sun4m!
                   3642:        bne     3f
                   3643:
1.37      pk       3644:        /*
1.38      pk       3645:         * The OBP guarantees us a 16MB mapping using a level 1 PTE at
1.125.2.2! bouyer   3646:         * the start of the memory bank in which we were loaded. All we
        !          3647:         * have to do is copy the entry.
        !          3648:         * Also, we must check to see if we have a TI Viking in non-mbus mode,
        !          3649:         * and if so do appropriate flipping and turning off traps before
1.38      pk       3650:         * we dork with MMU passthrough.  -grrr
1.37      pk       3651:         */
                   3652:
1.38      pk       3653:        sethi   %hi(0x40000000), %o1    ! TI version bit
                   3654:        rd      %psr, %o0
                   3655:        andcc   %o0, %o1, %g0
                   3656:        be      remap_notvik            ! is non-TI normal MBUS module
                   3657:        lda     [%g0] ASI_SRMMU, %o0    ! load MMU
                   3658:        andcc   %o0, 0x800, %g0
                   3659:        bne     remap_notvik            ! It is a viking MBUS module
                   3660:        nop
                   3661:
                   3662:        /*
                   3663:         * Ok, we have a non-Mbus TI Viking, a MicroSparc.
                   3664:         * In this scenerio, in order to play with the MMU
                   3665:         * passthrough safely, we need turn off traps, flip
                   3666:         * the AC bit on in the mmu status register, do our
                   3667:         * passthroughs, then restore the mmu reg and %psr
                   3668:         */
                   3669:        rd      %psr, %o4               ! saved here till done
                   3670:        andn    %o4, 0x20, %o5
                   3671:        wr      %o5, 0x0, %psr
                   3672:        nop; nop; nop;
                   3673:        set     SRMMU_CXTPTR, %o0
                   3674:        lda     [%o0] ASI_SRMMU, %o0    ! get context table ptr
                   3675:        sll     %o0, 4, %o0             ! make physical
                   3676:        lda     [%g0] ASI_SRMMU, %o3    ! hold mmu-sreg here
                   3677:        /* 0x8000 is AC bit in Viking mmu-ctl reg */
                   3678:        set     0x8000, %o2
                   3679:        or      %o3, %o2, %o2
                   3680:        sta     %o2, [%g0] ASI_SRMMU    ! AC bit on
1.125.2.2! bouyer   3681:
1.38      pk       3682:        lda     [%o0] ASI_BYPASS, %o1
                   3683:        srl     %o1, 4, %o1
                   3684:        sll     %o1, 8, %o1             ! get phys addr of l1 entry
                   3685:        lda     [%o1] ASI_BYPASS, %l4
                   3686:        srl     %l1, 22, %o2            ! note: 22 == RGSHIFT - 2
                   3687:        add     %o1, %o2, %o1
                   3688:        sta     %l4, [%o1] ASI_BYPASS
1.125.2.2! bouyer   3689:
1.38      pk       3690:        sta     %o3, [%g0] ASI_SRMMU    ! restore mmu-sreg
                   3691:        wr      %o4, 0x0, %psr          ! restore psr
1.37      pk       3692:        b,a     startmap_done
1.38      pk       3693:
                   3694:        /*
                   3695:         * The following is generic and should work on all
                   3696:         * Mbus based SRMMU's.
                   3697:         */
                   3698: remap_notvik:
                   3699:        set     SRMMU_CXTPTR, %o0
                   3700:        lda     [%o0] ASI_SRMMU, %o0    ! get context table ptr
                   3701:        sll     %o0, 4, %o0             ! make physical
                   3702:        lda     [%o0] ASI_BYPASS, %o1
                   3703:        srl     %o1, 4, %o1
                   3704:        sll     %o1, 8, %o1             ! get phys addr of l1 entry
                   3705:        lda     [%o1] ASI_BYPASS, %l4
                   3706:        srl     %l1, 22, %o2            ! note: 22 == RGSHIFT - 2
                   3707:        add     %o1, %o2, %o1
                   3708:        sta     %l4, [%o1] ASI_BYPASS
1.52      pk       3709:        !b,a    startmap_done
1.38      pk       3710:
1.52      pk       3711: 3:
1.9       deraadt  3712: #endif /* SUN4M */
1.13      deraadt  3713:        ! botch! We should blow up.
                   3714:
                   3715: startmap_done:
1.1       deraadt  3716:        /*
                   3717:         * All set, fix pc and npc.  Once we are where we should be,
                   3718:         * we can give ourselves a stack and enable traps.
                   3719:         */
1.9       deraadt  3720:        set     1f, %g1
                   3721:        jmp     %g1
1.1       deraadt  3722:         nop
                   3723: 1:
1.111     pk       3724:        sethi   %hi(_C_LABEL(cputyp)), %o0      ! what type of cpu we are on
                   3725:        st      %g4, [%o0 + %lo(_C_LABEL(cputyp))]
1.9       deraadt  3726:
1.111     pk       3727:        sethi   %hi(_C_LABEL(pgshift)), %o0     ! pgshift = log2(nbpg)
                   3728:        st      %g5, [%o0 + %lo(_C_LABEL(pgshift))]
1.13      deraadt  3729:
                   3730:        mov     1, %o0                  ! nbpg = 1 << pgshift
                   3731:        sll     %o0, %g5, %g5
1.111     pk       3732:        sethi   %hi(_C_LABEL(nbpg)), %o0        ! nbpg = bytes in a page
                   3733:        st      %g5, [%o0 + %lo(_C_LABEL(nbpg))]
1.13      deraadt  3734:
                   3735:        sub     %g5, 1, %g5
1.111     pk       3736:        sethi   %hi(_C_LABEL(pgofset)), %o0 ! page offset = bytes in a page - 1
                   3737:        st      %g5, [%o0 + %lo(_C_LABEL(pgofset))]
1.13      deraadt  3738:
1.9       deraadt  3739:        rd      %psr, %g3               ! paranoia: make sure ...
                   3740:        andn    %g3, PSR_ET, %g3        ! we have traps off
                   3741:        wr      %g3, 0, %psr            ! so that we can fiddle safely
                   3742:        nop; nop; nop
                   3743:
                   3744:        wr      %g0, 0, %wim            ! make sure we can set psr
                   3745:        nop; nop; nop
                   3746:        wr      %g0, PSR_S|PSR_PS|PSR_PIL, %psr ! set initial psr
                   3747:         nop; nop; nop
                   3748:
                   3749:        wr      %g0, 2, %wim            ! set initial %wim (w1 invalid)
                   3750:        mov     1, %g1                  ! set pcb_wim (log2(%wim) = 1)
1.111     pk       3751:        sethi   %hi(_C_LABEL(u0) + PCB_WIM), %g2
                   3752:        st      %g1, [%g2 + %lo(_C_LABEL(u0) + PCB_WIM)]
1.9       deraadt  3753:
1.1       deraadt  3754:        set     USRSTACK - CCFSZ, %fp   ! as if called from user code
                   3755:        set     estack0 - CCFSZ - 80, %sp ! via syscall(boot_me_up) or somesuch
                   3756:        rd      %psr, %l0
                   3757:        wr      %l0, PSR_ET, %psr
1.9       deraadt  3758:        nop; nop; nop
1.1       deraadt  3759:
1.52      pk       3760:        /* Export actual trapbase */
1.111     pk       3761:        sethi   %hi(_C_LABEL(trapbase)), %o0
                   3762:        st      %g6, [%o0+%lo(_C_LABEL(trapbase))]
1.52      pk       3763:
1.117     christos 3764: #ifdef notdef
1.1       deraadt  3765:        /*
                   3766:         * Step 2: clear BSS.  This may just be paranoia; the boot
                   3767:         * loader might already do it for us; but what the hell.
                   3768:         */
                   3769:        set     _edata, %o0             ! bzero(edata, end - edata)
                   3770:        set     _end, %o1
1.111     pk       3771:        call    _C_LABEL(bzero)
1.1       deraadt  3772:         sub    %o1, %o0, %o1
1.117     christos 3773: #endif
1.1       deraadt  3774:
                   3775:        /*
                   3776:         * Stash prom vectors now, after bzero, as it lives in bss
                   3777:         * (which we just zeroed).
                   3778:         * This depends on the fact that bzero does not use %g7.
                   3779:         */
1.111     pk       3780:        sethi   %hi(_C_LABEL(romp)), %l0
                   3781:        st      %g7, [%l0 + %lo(_C_LABEL(romp))]
1.1       deraadt  3782:
                   3783:        /*
                   3784:         * Step 3: compute number of windows and set up tables.
                   3785:         * We could do some of this later.
                   3786:         */
                   3787:        save    %sp, -64, %sp
                   3788:        rd      %psr, %g1
                   3789:        restore
                   3790:        and     %g1, 31, %g1            ! want just the CWP bits
                   3791:        add     %g1, 1, %o0             ! compute nwindows
1.111     pk       3792:        sethi   %hi(_C_LABEL(nwindows)), %o1    ! may as well tell everyone
1.1       deraadt  3793:        call    init_tables
1.111     pk       3794:         st     %o0, [%o1 + %lo(_C_LABEL(nwindows))]
1.1       deraadt  3795:
1.52      pk       3796: #if defined(SUN4)
1.29      deraadt  3797:        /*
                   3798:         * Some sun4 models have fewer than 8 windows. For extra
                   3799:         * speed, we do not need to save/restore those windows
                   3800:         * The save/restore code has 7 "save"'s followed by 7
                   3801:         * "restore"'s -- we "nop" out the last "save" and first
                   3802:         * "restore"
                   3803:         */
                   3804:        cmp     %o0, 8
1.50      pk       3805:        be      1f
1.29      deraadt  3806: noplab:         nop
                   3807:        set     noplab, %l0
                   3808:        ld      [%l0], %l1
                   3809:        set     wb1, %l0
                   3810:        st      %l1, [%l0 + 6*4]
                   3811:        st      %l1, [%l0 + 7*4]
                   3812: 1:
                   3813: #endif
                   3814:
1.62      pk       3815: #if ((defined(SUN4) || defined(SUN4C)) && defined(SUN4M))
                   3816:
                   3817:        /*
                   3818:         * Patch instructions at specified labels that start
                   3819:         * per-architecture code-paths.
                   3820:         */
                   3821: Lgandul:       nop
                   3822:
                   3823: #define MUNGE(label) \
                   3824:        sethi   %hi(label), %o0; \
                   3825:        st      %l0, [%o0 + %lo(label)]
                   3826:
                   3827:        sethi   %hi(Lgandul), %o0
                   3828:        ld      [%o0 + %lo(Lgandul)], %l0       ! %l0 = NOP
                   3829:
                   3830:        cmp     %g4, CPU_SUN4M
                   3831:        bne,a   1f
                   3832:         nop
                   3833:
                   3834:        ! this should be automated!
                   3835:        MUNGE(NOP_ON_4M_1)
                   3836:        MUNGE(NOP_ON_4M_2)
                   3837:        MUNGE(NOP_ON_4M_3)
                   3838:        MUNGE(NOP_ON_4M_4)
                   3839:        MUNGE(NOP_ON_4M_5)
                   3840:        MUNGE(NOP_ON_4M_6)
                   3841:        MUNGE(NOP_ON_4M_7)
                   3842:        MUNGE(NOP_ON_4M_8)
                   3843:        MUNGE(NOP_ON_4M_9)
                   3844:        MUNGE(NOP_ON_4M_10)
                   3845:        MUNGE(NOP_ON_4M_11)
                   3846:        MUNGE(NOP_ON_4M_12)
                   3847:        MUNGE(NOP_ON_4M_13)
                   3848:        MUNGE(NOP_ON_4M_14)
                   3849:        b,a     2f
                   3850:
                   3851: 1:
1.68      mycroft  3852:        MUNGE(NOP_ON_4_4C_1)
1.62      pk       3853:
                   3854: 2:
                   3855:
                   3856: #undef MUNGE
                   3857: #endif
                   3858:
1.1       deraadt  3859:        /*
                   3860:         * Step 4: change the trap base register, now that our trap handlers
                   3861:         * will function (they need the tables we just set up).
1.52      pk       3862:         * This depends on the fact that bzero does not use %g6.
1.1       deraadt  3863:         */
1.52      pk       3864:        wr      %g6, 0, %tbr
1.9       deraadt  3865:        nop; nop; nop                   ! paranoia
1.37      pk       3866:
1.98      pk       3867:
                   3868:        /* Clear `cpuinfo' */
                   3869:        sethi   %hi(CPUINFO_VA), %o0            ! bzero(&cpuinfo, NBPG)
                   3870:        sethi   %hi(CPUINFO_STRUCTSIZE), %o1
1.111     pk       3871:        call    _C_LABEL(bzero)
1.98      pk       3872:         add    %o1, %lo(CPUINFO_STRUCTSIZE), %o1
                   3873:
1.125.2.1  bouyer   3874:        /*
                   3875:         * Initialize `cpuinfo' fields which are needed early.  Note
                   3876:         * we make the cpuinfo self-reference at the local VA for now.
                   3877:         * It may be changed to reference a global VA later.
                   3878:         */
1.111     pk       3879:        set     _C_LABEL(u0), %o0               ! cpuinfo.curpcb = u0;
                   3880:        sethi   %hi(cpcb), %l0
                   3881:        st      %o0, [%l0 + %lo(cpcb)]
1.98      pk       3882:
1.125.2.1  bouyer   3883:        sethi   %hi(CPUINFO_VA), %o0            ! cpuinfo.ci_self = &cpuinfo;
                   3884:        sethi   %hi(_CISELFP), %l0
                   3885:        st      %o0, [%l0 + %lo(_CISELFP)]
                   3886:
1.111     pk       3887:        set     _C_LABEL(eintstack), %o0        ! cpuinfo.eintstack= _eintstack;
1.101     pk       3888:        sethi   %hi(_EINTSTACKP), %l0
                   3889:        st      %o0, [%l0 + %lo(_EINTSTACKP)]
1.1       deraadt  3890:
                   3891:        /*
1.11      deraadt  3892:         * Ready to run C code; finish bootstrap.
1.1       deraadt  3893:         */
1.111     pk       3894:        call    _C_LABEL(bootstrap)
1.1       deraadt  3895:         nop
1.11      deraadt  3896:
                   3897:        /*
                   3898:         * Call main.  This returns to us after loading /sbin/init into
                   3899:         * user space.  (If the exec fails, main() does not return.)
                   3900:         */
1.111     pk       3901:        call    _C_LABEL(main)
1.11      deraadt  3902:         clr    %o0                     ! our frame arg is ignored
1.89      pk       3903:        /*NOTREACHED*/
                   3904:
                   3905:        /*
                   3906:         * Entry point for non-boot CPUs in MP systems.
                   3907:         */
1.111     pk       3908:        .globl  _C_LABEL(cpu_hatch)
                   3909: _C_LABEL(cpu_hatch):
1.89      pk       3910:        rd      %psr, %g3               ! paranoia: make sure ...
                   3911:        andn    %g3, PSR_ET, %g3        ! we have traps off
                   3912:        wr      %g3, 0, %psr            ! so that we can fiddle safely
                   3913:        nop; nop; nop
                   3914:
                   3915:        wr      %g0, 0, %wim            ! make sure we can set psr
                   3916:        nop; nop; nop
                   3917:        wr      %g0, PSR_S|PSR_PS|PSR_PIL, %psr ! set initial psr
                   3918:        nop; nop; nop
                   3919:
                   3920:        wr      %g0, 2, %wim            ! set initial %wim (w1 invalid)
                   3921:
                   3922:        /* Initialize Trap Base register */
1.111     pk       3923:        sethi   %hi(_C_LABEL(trapbase)), %o0
                   3924:        ld      [%o0+%lo(_C_LABEL(trapbase))], %g6
1.89      pk       3925:        wr      %g6, 0, %tbr
                   3926:        nop; nop; nop                   ! paranoia
                   3927:
                   3928:        /* Set up a stack */
                   3929:        set     USRSTACK - CCFSZ, %fp   ! as if called from user code
1.111     pk       3930:        sethi   %hi(_C_LABEL(cpu_hatchstack)), %o0
                   3931:        ld      [%o0+%lo(_C_LABEL(cpu_hatchstack))], %o0
1.102     pk       3932:        set     USPACE - CCFSZ - 80, %sp
                   3933:        add     %sp, %o0, %sp
1.89      pk       3934:
                   3935:        /* Enable traps */
                   3936:        rd      %psr, %l0
                   3937:        wr      %l0, PSR_ET, %psr
                   3938:        nop; nop; nop
                   3939:
                   3940:        /* Call C code */
1.111     pk       3941:        sethi   %hi(_C_LABEL(cpu_hatch_sc)), %o0
                   3942:        call    _C_LABEL(cpu_setup)
                   3943:         ld     [%o0+%lo(_C_LABEL(cpu_hatch_sc))], %o0
1.89      pk       3944:
                   3945:        /* Idle here .. */
1.102     pk       3946:        rd      %psr, %l0
                   3947:        andn    %l0, PSR_PIL, %l0       ! psr &= ~PSR_PIL;
                   3948:        wr      %l0, 0, %psr            ! (void) spl0();
                   3949:        nop; nop; nop
1.89      pk       3950: 9:     ba 9b
                   3951:         nop
1.51      pk       3952:        /*NOTREACHED*/
1.1       deraadt  3953:
1.63      pk       3954:
1.1       deraadt  3955: /*
                   3956:  * The following code is copied to the top of the user stack when each
                   3957:  * process is exec'ed, and signals are `trampolined' off it.
                   3958:  *
                   3959:  * When this code is run, the stack looks like:
                   3960:  *     [%sp]           64 bytes to which registers can be dumped
                   3961:  *     [%sp + 64]      signal number (goes in %o0)
                   3962:  *     [%sp + 64 + 4]  signal code (goes in %o1)
                   3963:  *     [%sp + 64 + 8]  placeholder
                   3964:  *     [%sp + 64 + 12] argument for %o3, currently unsupported (always 0)
                   3965:  *     [%sp + 64 + 16] first word of saved state (sigcontext)
                   3966:  *         .
                   3967:  *         .
                   3968:  *         .
                   3969:  *     [%sp + NNN]     last word of saved state
                   3970:  * (followed by previous stack contents or top of signal stack).
                   3971:  * The address of the function to call is in %g1; the old %g1 and %o0
                   3972:  * have already been saved in the sigcontext.  We are running in a clean
                   3973:  * window, all previous windows now being saved to the stack.
                   3974:  *
                   3975:  * Note that [%sp + 64 + 8] == %sp + 64 + 16.  The copy at %sp+64+8
                   3976:  * will eventually be removed, with a hole left in its place, if things
                   3977:  * work out.
                   3978:  */
1.122     christos 3979: #define SAVE_STATE \
                   3980:        /* \
                   3981:         * XXX  the `save' and `restore' below are unnecessary: should \
                   3982:         *      replace with simple arithmetic on %sp \
                   3983:         * \
                   3984:         * Make room on the stack for 32 %f registers + %fsr.  This comes \
                   3985:         * out to 33*4 or 132 bytes, but this must be aligned to a multiple \
                   3986:         * of 8, or 136 bytes. \
                   3987:         */ \
                   3988:        save    %sp, -CCFSZ - 136, %sp; \
                   3989:        mov     %g2, %l2;               /* save globals in %l registers */ \
                   3990:        mov     %g3, %l3; \
                   3991:        mov     %g4, %l4; \
                   3992:        mov     %g5, %l5; \
                   3993:        mov     %g6, %l6; \
                   3994:        mov     %g7, %l7; \
                   3995:        /* \
                   3996:         * Saving the fpu registers is expensive, so do it iff the fsr \
                   3997:         * stored in the sigcontext shows that the fpu is enabled. \
                   3998:         */ \
                   3999:        ld      [%fp + 64 + 16 + SC_PSR_OFFSET], %l0; \
                   4000:        sethi   %hi(PSR_EF), %l1;       /* FPU enable is too high for andcc */ \
                   4001:        andcc   %l0, %l1, %l0;          /* %l0 = fpu enable bit */ \
                   4002:        be      1f;                     /* if not set, skip the saves */ \
                   4003:         rd     %y, %l1;                /* in any case, save %y */ \
                   4004:        /* fpu is enabled, oh well */ \
                   4005:        st      %fsr, [%sp + CCFSZ + 0]; \
                   4006:        std     %f0, [%sp + CCFSZ + 8]; \
                   4007:        std     %f2, [%sp + CCFSZ + 16]; \
                   4008:        std     %f4, [%sp + CCFSZ + 24]; \
                   4009:        std     %f6, [%sp + CCFSZ + 32]; \
                   4010:        std     %f8, [%sp + CCFSZ + 40]; \
                   4011:        std     %f10, [%sp + CCFSZ + 48]; \
                   4012:        std     %f12, [%sp + CCFSZ + 56]; \
                   4013:        std     %f14, [%sp + CCFSZ + 64]; \
                   4014:        std     %f16, [%sp + CCFSZ + 72]; \
                   4015:        std     %f18, [%sp + CCFSZ + 80]; \
                   4016:        std     %f20, [%sp + CCFSZ + 88]; \
                   4017:        std     %f22, [%sp + CCFSZ + 96]; \
                   4018:        std     %f24, [%sp + CCFSZ + 104]; \
                   4019:        std     %f26, [%sp + CCFSZ + 112]; \
                   4020:        std     %f28, [%sp + CCFSZ + 120]; \
                   4021:        std     %f30, [%sp + CCFSZ + 128]; \
                   4022: 1:
                   4023:
                   4024: #define RESTORE_STATE \
                   4025:        /* \
                   4026:         * Now that the handler has returned, re-establish all the state \
                   4027:         * we just saved above, then do a sigreturn. \
                   4028:         */ \
                   4029:        tst     %l0;                    /* reload fpu registers? */ \
                   4030:        be      1f;                     /* if not, skip the loads */ \
                   4031:         wr     %l1, %g0, %y;           /* in any case, restore %y */ \
                   4032:        ld      [%sp + CCFSZ + 0], %fsr; \
                   4033:        ldd     [%sp + CCFSZ + 8], %f0; \
                   4034:        ldd     [%sp + CCFSZ + 16], %f2; \
                   4035:        ldd     [%sp + CCFSZ + 24], %f4; \
                   4036:        ldd     [%sp + CCFSZ + 32], %f6; \
                   4037:        ldd     [%sp + CCFSZ + 40], %f8; \
                   4038:        ldd     [%sp + CCFSZ + 48], %f10; \
                   4039:        ldd     [%sp + CCFSZ + 56], %f12; \
                   4040:        ldd     [%sp + CCFSZ + 64], %f14; \
                   4041:        ldd     [%sp + CCFSZ + 72], %f16; \
                   4042:        ldd     [%sp + CCFSZ + 80], %f18; \
                   4043:        ldd     [%sp + CCFSZ + 88], %f20; \
                   4044:        ldd     [%sp + CCFSZ + 96], %f22; \
                   4045:        ldd     [%sp + CCFSZ + 104], %f24; \
                   4046:        ldd     [%sp + CCFSZ + 112], %f26; \
                   4047:        ldd     [%sp + CCFSZ + 120], %f28; \
                   4048:        ldd     [%sp + CCFSZ + 128], %f30; \
                   4049: 1: \
                   4050:        mov     %l2, %g2; \
                   4051:        mov     %l3, %g3; \
                   4052:        mov     %l4, %g4; \
                   4053:        mov     %l5, %g5; \
                   4054:        mov     %l6, %g6; \
                   4055:        mov     %l7, %g7
                   4056:
1.111     pk       4057:        .globl  _C_LABEL(sigcode)
                   4058:        .globl  _C_LABEL(esigcode)
                   4059: _C_LABEL(sigcode):
1.1       deraadt  4060:
1.122     christos 4061:        SAVE_STATE
                   4062:
1.1       deraadt  4063:        ldd     [%fp + 64], %o0         ! sig, code
                   4064:        ld      [%fp + 76], %o3         ! arg3
                   4065:        call    %g1                     ! (*sa->sa_handler)(sig,code,scp,arg3)
                   4066:         add    %fp, 64 + 16, %o2       ! scp
                   4067:
1.122     christos 4068:        RESTORE_STATE
1.1       deraadt  4069:
1.92      pk       4070:        ! get registers back & set syscall #
                   4071:        restore %g0, SYS___sigreturn14, %g1
1.1       deraadt  4072:        add     %sp, 64 + 16, %o0       ! compute scp
                   4073:        t       ST_SYSCALL              ! sigreturn(scp)
                   4074:        ! sigreturn does not return unless it fails
                   4075:        mov     SYS_exit, %g1           ! exit(errno)
                   4076:        t       ST_SYSCALL
1.111     pk       4077: _C_LABEL(esigcode):
1.41      christos 4078:
1.116     christos 4079: #ifdef COMPAT_SUNOS
                   4080:        .globl  _C_LABEL(sunos_sigcode)
                   4081:        .globl  _C_LABEL(sunos_esigcode)
                   4082: _C_LABEL(sunos_sigcode):
                   4083:
1.122     christos 4084:        SAVE_STATE
                   4085:
1.116     christos 4086:        ldd     [%fp + 64], %o0         ! sig, code
                   4087:        ld      [%fp + 76], %o3         ! arg3
                   4088:        call    %g1                     ! (*sa->sa_handler)(sig,code,scp,arg3)
                   4089:         add    %fp, 64 + 16, %o2       ! scp
                   4090:
1.122     christos 4091:        RESTORE_STATE
1.116     christos 4092:
                   4093:        ! get registers back & set syscall #
                   4094:        restore %g0, SUNOS_SYS_sigreturn, %g1
                   4095:        add     %sp, 64 + 16, %o0       ! compute scp
                   4096:        t       ST_SYSCALL              ! sigreturn(scp)
                   4097:        ! sigreturn does not return unless it fails
                   4098:        mov     SUNOS_SYS_exit, %g1             ! exit(errno)
                   4099:        t       ST_SYSCALL
                   4100: _C_LABEL(sunos_esigcode):
                   4101: #endif /* COMPAT_SUNOS */
                   4102:
1.36      christos 4103: #ifdef COMPAT_SVR4
1.111     pk       4104:        .globl  _C_LABEL(svr4_sigcode)
                   4105:        .globl  _C_LABEL(svr4_esigcode)
                   4106: _C_LABEL(svr4_sigcode):
1.41      christos 4107:
1.122     christos 4108:        SAVE_STATE
                   4109:
1.41      christos 4110:        ldd     [%fp + 64], %o0         ! sig, siginfo
                   4111:        ld      [%fp + 72], %o2         ! uctx
                   4112:        call    %g1                     ! (*sa->sa_handler)(sig,siginfo,uctx)
                   4113:         nop
                   4114:
1.122     christos 4115:        RESTORE_STATE
1.41      christos 4116:
1.42      mycroft  4117:        restore %g0, SVR4_SYS_context, %g1      ! get registers & set syscall #
1.41      christos 4118:        mov     1, %o0
                   4119:        add     %sp, 64 + 16, %o1       ! compute ucontextp
                   4120:        t       ST_SYSCALL              ! svr4_context(1, ucontextp)
                   4121:        ! setcontext does not return unless it fails
                   4122:        mov     SYS_exit, %g1           ! exit(errno)
                   4123:        t       ST_SYSCALL
1.111     pk       4124: _C_LABEL(svr4_esigcode):
1.116     christos 4125: #endif /* COMPAT_SVR4 */
1.1       deraadt  4126:
                   4127: /*
                   4128:  * Primitives
1.52      pk       4129:  */
1.1       deraadt  4130:
1.63      pk       4131: /*
                   4132:  * General-purpose NULL routine.
                   4133:  */
                   4134: ENTRY(sparc_noop)
                   4135:        retl
                   4136:         nop
1.1       deraadt  4137:
                   4138: /*
1.24      deraadt  4139:  * getfp() - get stack frame pointer
                   4140:  */
                   4141: ENTRY(getfp)
                   4142:        retl
                   4143:         mov %fp, %o0
                   4144:
                   4145: /*
1.1       deraadt  4146:  * copyinstr(fromaddr, toaddr, maxlength, &lencopied)
                   4147:  *
                   4148:  * Copy a null terminated string from the user address space into
                   4149:  * the kernel address space.
                   4150:  */
                   4151: ENTRY(copyinstr)
                   4152:        ! %o0 = fromaddr, %o1 = toaddr, %o2 = maxlen, %o3 = &lencopied
1.125.2.1  bouyer   4153:        mov     %o1, %o5                ! save = toaddr;
                   4154:        tst     %o2                     ! maxlen == 0?
                   4155:        beq,a   Lcstoolong              ! yes, return ENAMETOOLONG
                   4156:         sethi  %hi(cpcb), %o4
                   4157:
1.1       deraadt  4158:        set     KERNBASE, %o4
                   4159:        cmp     %o0, %o4                ! fromaddr < KERNBASE?
1.125.2.1  bouyer   4160:        blu     Lcsdocopy               ! yes, go do it
                   4161:         sethi  %hi(cpcb), %o4          ! (first instr of copy)
1.1       deraadt  4162:
                   4163:        b       Lcsdone                 ! no, return EFAULT
                   4164:         mov    EFAULT, %o0
                   4165:
                   4166: /*
                   4167:  * copyoutstr(fromaddr, toaddr, maxlength, &lencopied)
                   4168:  *
                   4169:  * Copy a null terminated string from the kernel
                   4170:  * address space to the user address space.
                   4171:  */
                   4172: ENTRY(copyoutstr)
                   4173:        ! %o0 = fromaddr, %o1 = toaddr, %o2 = maxlen, %o3 = &lencopied
1.125.2.1  bouyer   4174:        mov     %o1, %o5                ! save = toaddr;
                   4175:        tst     %o2                     ! maxlen == 0?
                   4176:        beq,a   Lcstoolong              ! yes, return ENAMETOOLONG
                   4177:         sethi  %hi(cpcb), %o4
                   4178:
1.1       deraadt  4179:        set     KERNBASE, %o4
                   4180:        cmp     %o1, %o4                ! toaddr < KERNBASE?
1.125.2.1  bouyer   4181:        blu     Lcsdocopy               ! yes, go do it
1.111     pk       4182:         sethi  %hi(cpcb), %o4          ! (first instr of copy)
1.1       deraadt  4183:
                   4184:        b       Lcsdone                 ! no, return EFAULT
                   4185:         mov    EFAULT, %o0
                   4186:
                   4187: Lcsdocopy:
1.111     pk       4188: !      sethi   %hi(cpcb), %o4          ! (done earlier)
                   4189:        ld      [%o4 + %lo(cpcb)], %o4  ! catch faults
1.125.2.1  bouyer   4190:        set     Lcsfault, %g1
                   4191:        st      %g1, [%o4 + PCB_ONFAULT]
1.1       deraadt  4192:
                   4193: ! XXX should do this in bigger chunks when possible
                   4194: 0:                                     ! loop:
                   4195:        ldsb    [%o0], %g1              !       c = *fromaddr;
                   4196:        tst     %g1
                   4197:        stb     %g1, [%o1]              !       *toaddr++ = c;
                   4198:        be      1f                      !       if (c == NULL)
                   4199:         inc    %o1                     !               goto ok;
                   4200:        deccc   %o2                     !       if (--len > 0) {
1.125.2.1  bouyer   4201:        bgu     0b                      !               fromaddr++;
1.1       deraadt  4202:         inc    %o0                     !               goto loop;
                   4203:                                        !       }
1.125.2.1  bouyer   4204: Lcstoolong:                            !
1.1       deraadt  4205:        b       Lcsdone                 !       error = ENAMETOOLONG;
                   4206:         mov    ENAMETOOLONG, %o0       !       goto done;
                   4207: 1:                                     ! ok:
                   4208:        clr     %o0                     !    error = 0;
                   4209: Lcsdone:                               ! done:
                   4210:        sub     %o1, %o5, %o1           !       len = to - save;
                   4211:        tst     %o3                     !       if (lencopied)
                   4212:        bnz,a   3f
                   4213:         st     %o1, [%o3]              !               *lencopied = len;
                   4214: 3:
                   4215:        retl                            ! cpcb->pcb_onfault = 0;
                   4216:         st     %g0, [%o4 + PCB_ONFAULT]! return (error);
                   4217:
                   4218: Lcsfault:
                   4219:        b       Lcsdone                 ! error = EFAULT;
                   4220:         mov    EFAULT, %o0             ! goto ret;
                   4221:
                   4222: /*
                   4223:  * copystr(fromaddr, toaddr, maxlength, &lencopied)
                   4224:  *
                   4225:  * Copy a null terminated string from one point to another in
                   4226:  * the kernel address space.  (This is a leaf procedure, but
                   4227:  * it does not seem that way to the C compiler.)
                   4228:  */
                   4229: ENTRY(copystr)
                   4230:        mov     %o1, %o5                !       to0 = to;
1.125.2.1  bouyer   4231:        tst     %o2                     ! if (maxlength == 0)
                   4232:        beq,a   2f                      !
                   4233:         mov    ENAMETOOLONG, %o0       !       ret = ENAMETOOLONG; goto done;
                   4234:
1.1       deraadt  4235: 0:                                     ! loop:
                   4236:        ldsb    [%o0], %o4              !       c = *from;
                   4237:        tst     %o4
                   4238:        stb     %o4, [%o1]              !       *to++ = c;
                   4239:        be      1f                      !       if (c == 0)
                   4240:         inc    %o1                     !               goto ok;
                   4241:        deccc   %o2                     !       if (--len > 0) {
1.125.2.1  bouyer   4242:        bgu,a   0b                      !               from++;
1.1       deraadt  4243:         inc    %o0                     !               goto loop;
                   4244:        b       2f                      !       }
                   4245:         mov    ENAMETOOLONG, %o0       !       ret = ENAMETOOLONG; goto done;
                   4246: 1:                                     ! ok:
                   4247:        clr     %o0                     !       ret = 0;
                   4248: 2:
                   4249:        sub     %o1, %o5, %o1           !       len = to - to0;
                   4250:        tst     %o3                     !       if (lencopied)
                   4251:        bnz,a   3f
                   4252:         st     %o1, [%o3]              !               *lencopied = len;
                   4253: 3:
                   4254:        retl
                   4255:         nop
                   4256:
1.52      pk       4257: /*
1.1       deraadt  4258:  * Copyin(src, dst, len)
                   4259:  *
                   4260:  * Copy specified amount of data from user space into the kernel.
                   4261:  */
                   4262: ENTRY(copyin)
                   4263:        set     KERNBASE, %o3
                   4264:        cmp     %o0, %o3                ! src < KERNBASE?
                   4265:        blu,a   Ldocopy                 ! yes, can try it
1.111     pk       4266:         sethi  %hi(cpcb), %o3
1.1       deraadt  4267:
                   4268:        /* source address points into kernel space: return EFAULT */
                   4269:        retl
                   4270:         mov    EFAULT, %o0
                   4271:
                   4272: /*
                   4273:  * Copyout(src, dst, len)
                   4274:  *
                   4275:  * Copy specified amount of data from kernel to user space.
                   4276:  * Just like copyin, except that the `dst' addresses are user space
                   4277:  * rather than the `src' addresses.
                   4278:  */
                   4279: ENTRY(copyout)
                   4280:        set     KERNBASE, %o3
                   4281:        cmp     %o1, %o3                ! dst < KERBASE?
                   4282:        blu,a   Ldocopy
1.111     pk       4283:         sethi  %hi(cpcb), %o3
1.1       deraadt  4284:
                   4285:        /* destination address points into kernel space: return EFAULT */
                   4286:        retl
                   4287:         mov    EFAULT, %o0
                   4288:
                   4289:        /*
                   4290:         * ******NOTE****** this depends on bcopy() not using %g7
                   4291:         */
                   4292: Ldocopy:
1.111     pk       4293: !      sethi   %hi(cpcb), %o3
                   4294:        ld      [%o3 + %lo(cpcb)], %o3
1.1       deraadt  4295:        set     Lcopyfault, %o4
                   4296:        mov     %o7, %g7                ! save return address
1.111     pk       4297:        call    _C_LABEL(bcopy)         ! bcopy(src, dst, len)
1.1       deraadt  4298:         st     %o4, [%o3 + PCB_ONFAULT]
                   4299:
1.111     pk       4300:        sethi   %hi(cpcb), %o3
                   4301:        ld      [%o3 + %lo(cpcb)], %o3
1.1       deraadt  4302:        st      %g0, [%o3 + PCB_ONFAULT]
                   4303:        jmp     %g7 + 8
                   4304:         clr    %o0                     ! return 0
                   4305:
                   4306: ! Copyin or copyout fault.  Clear cpcb->pcb_onfault and return EFAULT.
                   4307: ! Note that although we were in bcopy, there is no state to clean up;
                   4308: ! the only special thing is that we have to return to [g7 + 8] rather than
                   4309: ! [o7 + 8].
                   4310: Lcopyfault:
1.111     pk       4311:        sethi   %hi(cpcb), %o3
                   4312:        ld      [%o3 + %lo(cpcb)], %o3
1.1       deraadt  4313:        st      %g0, [%o3 + PCB_ONFAULT]
                   4314:        jmp     %g7 + 8
                   4315:         mov    EFAULT, %o0
                   4316:
                   4317:
                   4318: /*
                   4319:  * Write all user windows presently in the CPU back to the user's stack.
                   4320:  * We just do `save' instructions until pcb_uw == 0.
                   4321:  *
                   4322:  *     p = cpcb;
                   4323:  *     nsaves = 0;
                   4324:  *     while (p->pcb_uw > 0)
                   4325:  *             save(), nsaves++;
                   4326:  *     while (--nsaves >= 0)
                   4327:  *             restore();
                   4328:  */
                   4329: ENTRY(write_user_windows)
1.111     pk       4330:        sethi   %hi(cpcb), %g6
                   4331:        ld      [%g6 + %lo(cpcb)], %g6
1.1       deraadt  4332:        b       2f
                   4333:         clr    %g5
                   4334: 1:
                   4335:        save    %sp, -64, %sp
                   4336: 2:
                   4337:        ld      [%g6 + PCB_UW], %g7
                   4338:        tst     %g7
                   4339:        bg,a    1b
                   4340:         inc    %g5
                   4341: 3:
                   4342:        deccc   %g5
                   4343:        bge,a   3b
                   4344:         restore
                   4345:        retl
                   4346:         nop
                   4347:
                   4348:
1.111     pk       4349:        .comm   _C_LABEL(want_resched),4
                   4350:        .comm   _C_LABEL(want_ast),4
1.1       deraadt  4351: /*
                   4352:  * Masterpaddr is the p->p_addr of the last process on the processor.
                   4353:  * XXX masterpaddr is almost the same as cpcb
                   4354:  * XXX should delete this entirely
                   4355:  */
1.111     pk       4356:        .comm   _C_LABEL(masterpaddr), 4
1.1       deraadt  4357:
                   4358: /*
                   4359:  * Switch statistics (for later tweaking):
                   4360:  *     nswitchdiff = p1 => p2 (i.e., chose different process)
1.10      deraadt  4361:  *     nswitchexit = number of calls to switchexit()
1.111     pk       4362:  *     cnt.v_swtch = total calls to swtch+swtchexit
1.1       deraadt  4363:  */
1.111     pk       4364:        .comm   _C_LABEL(nswitchdiff), 4
                   4365:        .comm   _C_LABEL(nswitchexit), 4
1.1       deraadt  4366:
                   4367: /*
1.10      deraadt  4368:  * REGISTER USAGE IN cpu_switch AND switchexit:
1.1       deraadt  4369:  * This is split into two phases, more or less
                   4370:  * `before we locate a new proc' and `after'.
                   4371:  * Some values are the same in both phases.
                   4372:  * Note that the %o0-registers are not preserved across
                   4373:  * the psr change when entering a new process, since this
                   4374:  * usually changes the CWP field (hence heavy usage of %g's).
                   4375:  *
                   4376:  *     %g1 = oldpsr (excluding ipl bits)
1.111     pk       4377:  *     %g2 = %hi(whichqs); newpsr
1.1       deraadt  4378:  *     %g3 = p
                   4379:  *     %g4 = lastproc
                   4380:  *     %g5 = <free>; newpcb
1.111     pk       4381:  *     %g6 = %hi(cpcb)
                   4382:  *     %g7 = %hi(curproc)
1.1       deraadt  4383:  *     %o0 = tmp 1
                   4384:  *     %o1 = tmp 2
                   4385:  *     %o2 = tmp 3
                   4386:  *     %o3 = tmp 4; whichqs; vm
                   4387:  *     %o4 = tmp 4; which; sswap
                   4388:  *     %o5 = tmp 5; q; <free>
                   4389:  */
                   4390:
                   4391: /*
1.125.2.1  bouyer   4392:  * When calling external functions from cpu_switch() and idle(), we must
                   4393:  * preserve the global registers mentioned above across the call.  We also
                   4394:  * set up a stack frame since we will be running in our caller's frame
                   4395:  * in cpu_switch().
                   4396:  */
                   4397: #define SAVE_GLOBALS_AND_CALL(name)    \
                   4398:        save    %sp, -CCFSZ, %sp;       \
                   4399:        mov     %g1, %i0;               \
                   4400:        mov     %g2, %i1;               \
                   4401:        mov     %g3, %i2;               \
                   4402:        mov     %g4, %i3;               \
                   4403:        mov     %g6, %i4;               \
                   4404:        call    _C_LABEL(name);         \
                   4405:         mov    %g7, %i5;               \
                   4406:        mov     %i5, %g7;               \
                   4407:        mov     %i4, %g6;               \
                   4408:        mov     %i3, %g4;               \
                   4409:        mov     %i2, %g3;               \
                   4410:        mov     %i1, %g2;               \
                   4411:        mov     %i0, %g1;               \
                   4412:        restore
                   4413:
                   4414:
                   4415: /*
1.10      deraadt  4416:  * switchexit is called only from cpu_exit() before the current process
1.88      thorpej  4417:  * has freed its vmspace and kernel stack; we must schedule them to be
                   4418:  * freed.  (curproc is already NULL.)
1.1       deraadt  4419:  *
                   4420:  * We lay the process to rest by changing to the `idle' kernel stack,
                   4421:  * and note that the `last loaded process' is nonexistent.
                   4422:  */
1.10      deraadt  4423: ENTRY(switchexit)
1.88      thorpej  4424:        mov     %o0, %g2                ! save proc for exit2() call
1.1       deraadt  4425:
                   4426:        /*
                   4427:         * Change pcb to idle u. area, i.e., set %sp to top of stack
1.111     pk       4428:         * and %psr to PSR_S|PSR_ET, and set cpcb to point to idle_u.
1.125.2.1  bouyer   4429:         * Once we have left the old stack, we can call exit2() to
1.1       deraadt  4430:         * destroy it.  Call it any sooner and the register windows
                   4431:         * go bye-bye.
                   4432:         */
1.99      pk       4433: #if defined(MULTIPROCESSOR)
1.111     pk       4434:        sethi   %hi(IDLE_UP), %g5
                   4435:        ld      [%g5 + %lo(IDLE_UP)], %g5
1.99      pk       4436: #else
1.111     pk       4437:        set     _C_LABEL(idle_u), %g5
1.99      pk       4438: #endif
1.111     pk       4439:        sethi   %hi(cpcb), %g6
1.1       deraadt  4440:        mov     1, %g7
                   4441:        wr      %g0, PSR_S, %psr        ! change to window 0, traps off
                   4442:        wr      %g0, 2, %wim            ! and make window 1 the trap window
1.111     pk       4443:        st      %g5, [%g6 + %lo(cpcb)]  ! cpcb = &idle_u
1.1       deraadt  4444:        st      %g7, [%g5 + PCB_WIM]    ! idle_u.pcb_wim = log2(2) = 1
1.100     pk       4445: #if defined(MULTIPROCESSOR)
                   4446:        set     USPACE-CCFSZ, %o1       !
                   4447:        add     %g5, %o1, %sp           ! set new %sp
                   4448: #else
1.111     pk       4449:        set     _C_LABEL(idle_u) + USPACE-CCFSZ, %sp    ! set new %sp
1.100     pk       4450: #endif
                   4451:
1.1       deraadt  4452: #ifdef DEBUG
1.99      pk       4453:        mov     %g5, %l6                ! %l6 = _idle_u
1.1       deraadt  4454:        SET_SP_REDZONE(%l6, %l5)
                   4455: #endif
                   4456:        wr      %g0, PSR_S|PSR_ET, %psr ! and then enable traps
1.111     pk       4457:        call    _C_LABEL(exit2)         ! exit2(p)
1.88      thorpej  4458:         mov    %g2, %o0
1.1       deraadt  4459:
                   4460:        /*
1.10      deraadt  4461:         * Now fall through to `the last switch'.  %g6 was set to
1.125.2.1  bouyer   4462:         * %hi(cpcb), but may have been clobbered in exit2(),
1.1       deraadt  4463:         * so all the registers described below will be set here.
                   4464:         *
                   4465:         * REGISTER USAGE AT THIS POINT:
                   4466:         *      %g1 = oldpsr (excluding ipl bits)
1.111     pk       4467:         *      %g2 = %hi(whichqs)
1.1       deraadt  4468:         *      %g4 = lastproc
1.111     pk       4469:         *      %g6 = %hi(cpcb)
                   4470:         *      %g7 = %hi(curproc)
1.1       deraadt  4471:         *      %o0 = tmp 1
                   4472:         *      %o1 = tmp 2
                   4473:         *      %o3 = whichqs
                   4474:         */
                   4475:
1.111     pk       4476:        INCR(_C_LABEL(nswitchexit))     ! nswitchexit++;
                   4477:        INCR(_C_LABEL(uvmexp)+V_SWTCH)  ! cnt.v_switch++;
1.1       deraadt  4478:
                   4479:        mov     PSR_S|PSR_ET, %g1       ! oldpsr = PSR_S | PSR_ET;
1.125.2.1  bouyer   4480:        sethi   %hi(_C_LABEL(sched_whichqs)), %g2
1.1       deraadt  4481:        clr     %g4                     ! lastproc = NULL;
1.111     pk       4482:        sethi   %hi(cpcb), %g6
                   4483:        sethi   %hi(curproc), %g7
1.125.2.1  bouyer   4484:        st      %g0, [%g7 + %lo(curproc)]       ! curproc = NULL;
                   4485:        b,a     idle_enter_no_schedlock
1.1       deraadt  4486:        /* FALLTHROUGH */
                   4487:
                   4488: /*
1.10      deraadt  4489:  * When no processes are on the runq, switch
1.52      pk       4490:  * idles here waiting for something to come ready.
1.1       deraadt  4491:  * The registers are set up as noted above.
                   4492:  */
1.125.2.1  bouyer   4493: idle:
                   4494: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
                   4495:        /* Release the scheduler lock */
                   4496:        SAVE_GLOBALS_AND_CALL(sched_unlock_idle)
                   4497: #endif
                   4498: idle_enter_no_schedlock:
1.1       deraadt  4499:        wr      %g1, 0, %psr            ! (void) spl0();
1.111     pk       4500: 1:                                     ! spin reading whichqs until nonzero
1.125.2.1  bouyer   4501:        ld      [%g2 + %lo(_C_LABEL(sched_whichqs))], %o3
1.1       deraadt  4502:        tst     %o3
1.125.2.1  bouyer   4503: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
                   4504:        bnz,a   idle_leave
                   4505: #else
1.1       deraadt  4506:        bnz,a   Lsw_scan
1.125.2.1  bouyer   4507: #endif
                   4508:         wr     %g1, PSR_PIL, %psr      ! (void) splhigh();
                   4509:
                   4510:        ! Check uvm.page_idle_zero
                   4511:        sethi   %hi(_C_LABEL(uvm) + UVM_PAGE_IDLE_ZERO), %o3
                   4512:        ld      [%o3 + %lo(_C_LABEL(uvm) + UVM_PAGE_IDLE_ZERO)], %o3
                   4513:        tst     %o3
                   4514:        bz      1b
                   4515:         nop
                   4516:
                   4517:        SAVE_GLOBALS_AND_CALL(uvm_pageidlezero)
1.1       deraadt  4518:        b,a     1b
                   4519:
1.125.2.1  bouyer   4520: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
                   4521: idle_leave:
                   4522:        /* Before we leave the idle loop, detain the scheduler lock */
                   4523:        nop;nop;nop;    ! just wrote to %psr; delay before doing a `save'
                   4524:        SAVE_GLOBALS_AND_CALL(sched_lock_idle)
                   4525:        b,a     Lsw_scan
                   4526: #endif
                   4527:
1.1       deraadt  4528: Lsw_panic_rq:
                   4529:        sethi   %hi(1f), %o0
1.111     pk       4530:        call    _C_LABEL(panic)
1.1       deraadt  4531:         or     %lo(1f), %o0, %o0
                   4532: Lsw_panic_wchan:
                   4533:        sethi   %hi(2f), %o0
1.111     pk       4534:        call    _C_LABEL(panic)
1.1       deraadt  4535:         or     %lo(2f), %o0, %o0
                   4536: Lsw_panic_srun:
                   4537:        sethi   %hi(3f), %o0
1.111     pk       4538:        call    _C_LABEL(panic)
1.1       deraadt  4539:         or     %lo(3f), %o0, %o0
1.10      deraadt  4540: 1:     .asciz  "switch rq"
                   4541: 2:     .asciz  "switch wchan"
                   4542: 3:     .asciz  "switch SRUN"
1.52      pk       4543:        _ALIGN
1.1       deraadt  4544:
                   4545: /*
1.10      deraadt  4546:  * cpu_switch() picks a process to run and runs it, saving the current
1.1       deraadt  4547:  * one away.  On the assumption that (since most workstations are
                   4548:  * single user machines) the chances are quite good that the new
                   4549:  * process will turn out to be the current process, we defer saving
                   4550:  * it here until we have found someone to load.  If that someone
                   4551:  * is the current process we avoid both store and load.
                   4552:  *
1.10      deraadt  4553:  * cpu_switch() is always entered at splstatclock or splhigh.
1.1       deraadt  4554:  *
                   4555:  * IT MIGHT BE WORTH SAVING BEFORE ENTERING idle TO AVOID HAVING TO
                   4556:  * SAVE LATER WHEN SOMEONE ELSE IS READY ... MUST MEASURE!
                   4557:  */
1.111     pk       4558:        .globl  _C_LABEL(__ffstab)
1.10      deraadt  4559: ENTRY(cpu_switch)
1.1       deraadt  4560:        /*
                   4561:         * REGISTER USAGE AT THIS POINT:
                   4562:         *      %g1 = oldpsr (excluding ipl bits)
1.111     pk       4563:         *      %g2 = %hi(whichqs)
1.1       deraadt  4564:         *      %g3 = p
                   4565:         *      %g4 = lastproc
                   4566:         *      %g5 = tmp 0
1.111     pk       4567:         *      %g6 = %hi(cpcb)
                   4568:         *      %g7 = %hi(curproc)
1.1       deraadt  4569:         *      %o0 = tmp 1
                   4570:         *      %o1 = tmp 2
                   4571:         *      %o2 = tmp 3
                   4572:         *      %o3 = tmp 4, then at Lsw_scan, whichqs
                   4573:         *      %o4 = tmp 5, then at Lsw_scan, which
                   4574:         *      %o5 = tmp 6, then at Lsw_scan, q
                   4575:         */
1.125.2.1  bouyer   4576:        sethi   %hi(_C_LABEL(sched_whichqs)), %g2       ! set up addr regs
1.111     pk       4577:        sethi   %hi(cpcb), %g6
                   4578:        ld      [%g6 + %lo(cpcb)], %o0
1.125.2.1  bouyer   4579:        std     %o6, [%o0 + PCB_SP]             ! cpcb->pcb_<sp,pc> = <sp,pc>;
                   4580:        rd      %psr, %g1                       ! oldpsr = %psr;
1.111     pk       4581:        sethi   %hi(curproc), %g7
                   4582:        ld      [%g7 + %lo(curproc)], %g4       ! lastproc = curproc;
1.125.2.1  bouyer   4583:        st      %g1, [%o0 + PCB_PSR]            ! cpcb->pcb_psr = oldpsr;
                   4584:        andn    %g1, PSR_PIL, %g1               ! oldpsr &= ~PSR_PIL;
1.111     pk       4585:        st      %g0, [%g7 + %lo(curproc)]       ! curproc = NULL;
1.1       deraadt  4586:
                   4587: Lsw_scan:
                   4588:        nop; nop; nop                           ! paranoia
1.125.2.1  bouyer   4589:        ld      [%g2 + %lo(_C_LABEL(sched_whichqs))], %o3
1.1       deraadt  4590:
                   4591:        /*
                   4592:         * Optimized inline expansion of `which = ffs(whichqs) - 1';
                   4593:         * branches to idle if ffs(whichqs) was 0.
                   4594:         */
1.111     pk       4595:        set     _C_LABEL(__ffstab), %o2
1.1       deraadt  4596:        andcc   %o3, 0xff, %o1          ! byte 0 zero?
                   4597:        bz,a    1f                      ! yes, try byte 1
                   4598:         srl    %o3, 8, %o0
                   4599:        b       2f                      ! ffs = ffstab[byte0]; which = ffs - 1;
                   4600:         ldsb   [%o2 + %o1], %o0
                   4601: 1:     andcc   %o0, 0xff, %o1          ! byte 1 zero?
                   4602:        bz,a    1f                      ! yes, try byte 2
                   4603:         srl    %o0, 8, %o0
                   4604:        ldsb    [%o2 + %o1], %o0        ! which = ffstab[byte1] + 7;
                   4605:        b       3f
                   4606:         add    %o0, 7, %o4
                   4607: 1:     andcc   %o0, 0xff, %o1          ! byte 2 zero?
                   4608:        bz,a    1f                      ! yes, try byte 3
                   4609:         srl    %o0, 8, %o0
                   4610:        ldsb    [%o2 + %o1], %o0        ! which = ffstab[byte2] + 15;
                   4611:        b       3f
                   4612:         add    %o0, 15, %o4
                   4613: 1:     ldsb    [%o2 + %o0], %o0        ! ffs = ffstab[byte3] + 24
                   4614:        addcc   %o0, 24, %o0            ! (note that ffstab[0] == -24)
                   4615:        bz      idle                    ! if answer was 0, go idle
                   4616:         EMPTY
                   4617: 2:     sub     %o0, 1, %o4             ! which = ffs(whichqs) - 1
                   4618: 3:     /* end optimized inline expansion */
                   4619:
                   4620:        /*
                   4621:         * We found a nonempty run queue.  Take its first process.
                   4622:         */
1.125.2.1  bouyer   4623:        set     _C_LABEL(sched_qs), %o5 ! q = &qs[which];
1.1       deraadt  4624:        sll     %o4, 3, %o0
                   4625:        add     %o0, %o5, %o5
                   4626:        ld      [%o5], %g3              ! p = q->ph_link;
                   4627:        cmp     %g3, %o5                ! if (p == q)
1.10      deraadt  4628:        be      Lsw_panic_rq            !       panic("switch rq");
1.1       deraadt  4629:         EMPTY
1.10      deraadt  4630:        ld      [%g3], %o0              ! tmp0 = p->p_forw;
1.1       deraadt  4631:        st      %o0, [%o5]              ! q->ph_link = tmp0;
1.10      deraadt  4632:        st      %o5, [%o0 + 4]          ! tmp0->p_back = q;
1.1       deraadt  4633:        cmp     %o0, %o5                ! if (tmp0 == q)
                   4634:        bne     1f
                   4635:         EMPTY
                   4636:        mov     1, %o1                  !       whichqs &= ~(1 << which);
                   4637:        sll     %o1, %o4, %o1
                   4638:        andn    %o3, %o1, %o3
1.125.2.1  bouyer   4639:        st      %o3, [%g2 + %lo(_C_LABEL(sched_whichqs))]
1.1       deraadt  4640: 1:
                   4641:        /*
                   4642:         * PHASE TWO: NEW REGISTER USAGE:
                   4643:         *      %g1 = oldpsr (excluding ipl bits)
                   4644:         *      %g2 = newpsr
                   4645:         *      %g3 = p
                   4646:         *      %g4 = lastproc
                   4647:         *      %g5 = newpcb
1.111     pk       4648:         *      %g6 = %hi(cpcb)
                   4649:         *      %g7 = %hi(curproc)
1.1       deraadt  4650:         *      %o0 = tmp 1
                   4651:         *      %o1 = tmp 2
                   4652:         *      %o2 = tmp 3
                   4653:         *      %o3 = vm
                   4654:         */
                   4655:
                   4656:        /* firewalls */
                   4657:        ld      [%g3 + P_WCHAN], %o0    ! if (p->p_wchan)
                   4658:        tst     %o0
1.10      deraadt  4659:        bne     Lsw_panic_wchan         !       panic("switch wchan");
1.1       deraadt  4660:         EMPTY
                   4661:        ldsb    [%g3 + P_STAT], %o0     ! if (p->p_stat != SRUN)
                   4662:        cmp     %o0, SRUN
1.10      deraadt  4663:        bne     Lsw_panic_srun          !       panic("switch SRUN");
1.1       deraadt  4664:         EMPTY
                   4665:
                   4666:        /*
                   4667:         * Committed to running process p.
                   4668:         * It may be the same as the one we were running before.
                   4669:         */
1.125.2.1  bouyer   4670:        mov     SONPROC, %o0                    ! p->p_stat = SONPROC;
                   4671:        stb     %o0, [%g3 + P_STAT]
                   4672:
                   4673:        /* p->p_cpu initialized in fork1() for single-processor */
                   4674: #if defined(MULTIPROCESSOR)
                   4675:        sethi   %hi(_CISELFP), %o0              ! p->p_cpu = cpuinfo.ci_self;
                   4676:        ld      [%o0 + %lo(_CISELFP)], %o0
                   4677:        st      %o0, [%g3 + P_CPU]
                   4678: #endif
                   4679:
1.111     pk       4680:        sethi   %hi(_C_LABEL(want_resched)), %o0        ! want_resched = 0;
                   4681:        st      %g0, [%o0 + %lo(_C_LABEL(want_resched))]
1.125.2.1  bouyer   4682: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
                   4683:        /* Done with the run queues; release the scheduler lock */
                   4684:        SAVE_GLOBALS_AND_CALL(sched_unlock_idle)
                   4685: #endif
1.1       deraadt  4686:        ld      [%g3 + P_ADDR], %g5             ! newpcb = p->p_addr;
1.10      deraadt  4687:        st      %g0, [%g3 + 4]                  ! p->p_back = NULL;
1.1       deraadt  4688:        ld      [%g5 + PCB_PSR], %g2            ! newpsr = newpcb->pcb_psr;
1.111     pk       4689:        st      %g3, [%g7 + %lo(curproc)]       ! curproc = p;
1.1       deraadt  4690:
                   4691:        cmp     %g3, %g4                ! p == lastproc?
                   4692:        be,a    Lsw_sameproc            ! yes, go return 0
                   4693:         wr     %g2, 0, %psr            ! (after restoring ipl)
                   4694:
                   4695:        /*
                   4696:         * Not the old process.  Save the old process, if any;
                   4697:         * then load p.
                   4698:         */
                   4699:        tst     %g4
                   4700:        be,a    Lsw_load                ! if no old process, go load
                   4701:         wr     %g1, (PIL_CLOCK << 8) | PSR_ET, %psr
                   4702:
1.111     pk       4703:        INCR(_C_LABEL(nswitchdiff))     ! clobbers %o0,%o1
1.1       deraadt  4704:        /*
                   4705:         * save: write back all windows (including the current one).
                   4706:         * XXX  crude; knows nwindows <= 8
                   4707:         */
                   4708: #define        SAVE save %sp, -64, %sp
1.29      deraadt  4709: wb1:   SAVE; SAVE; SAVE; SAVE; SAVE; SAVE; SAVE        /* 7 of each: */
1.1       deraadt  4710:        restore; restore; restore; restore; restore; restore; restore
                   4711:
                   4712:        /*
                   4713:         * Load the new process.  To load, we must change stacks and
                   4714:         * alter cpcb and %wim, hence we must disable traps.  %psr is
                   4715:         * currently equal to oldpsr (%g1) ^ (PIL_CLOCK << 8);
                   4716:         * this means that PSR_ET is on.  Likewise, PSR_ET is on
                   4717:         * in newpsr (%g2), although we do not know newpsr's ipl.
                   4718:         *
                   4719:         * We also must load up the `in' and `local' registers.
                   4720:         */
                   4721:        wr      %g1, (PIL_CLOCK << 8) | PSR_ET, %psr
                   4722: Lsw_load:
                   4723: !      wr      %g1, (PIL_CLOCK << 8) | PSR_ET, %psr    ! done above
                   4724:        /* compute new wim */
                   4725:        ld      [%g5 + PCB_WIM], %o0
                   4726:        mov     1, %o1
                   4727:        sll     %o1, %o0, %o0
                   4728:        wr      %o0, 0, %wim            ! %wim = 1 << newpcb->pcb_wim;
1.76      pk       4729:        /* Clear FP & CP enable bits, as well as the PIL field */
1.1       deraadt  4730:        /* now must not change %psr for 3 more instrs */
1.76      pk       4731: /*1*/  set     PSR_EF|PSR_EC|PSR_PIL, %o0
                   4732: /*2*/  andn    %g2, %o0, %g2           ! newpsr &= ~(PSR_EF|PSR_EC|PSR_PIL);
1.1       deraadt  4733: /*3*/  nop
                   4734:        /* set new psr, but with traps disabled */
                   4735:        wr      %g2, PSR_ET, %psr       ! %psr = newpsr ^ PSR_ET;
                   4736:        /* set new cpcb */
1.111     pk       4737:        st      %g5, [%g6 + %lo(cpcb)]  ! cpcb = newpcb;
1.1       deraadt  4738:        ldd     [%g5 + PCB_SP], %o6     ! <sp,pc> = newpcb->pcb_<sp,pc>
                   4739:        /* load window */
                   4740:        ldd     [%sp + (0*8)], %l0
                   4741:        ldd     [%sp + (1*8)], %l2
                   4742:        ldd     [%sp + (2*8)], %l4
                   4743:        ldd     [%sp + (3*8)], %l6
                   4744:        ldd     [%sp + (4*8)], %i0
                   4745:        ldd     [%sp + (5*8)], %i2
                   4746:        ldd     [%sp + (6*8)], %i4
                   4747:        ldd     [%sp + (7*8)], %i6
                   4748: #ifdef DEBUG
                   4749:        mov     %g5, %o0
                   4750:        SET_SP_REDZONE(%o0, %o1)
                   4751:        CHECK_SP_REDZONE(%o0, %o1)
                   4752: #endif
1.76      pk       4753:        /* finally, enable traps and continue at splclock() */
                   4754:        wr      %g2, PIL_CLOCK << 8 , %psr      ! psr = newpsr;
1.1       deraadt  4755:
                   4756:        /*
                   4757:         * Now running p.  Make sure it has a context so that it
                   4758:         * can talk about user space stuff.  (Its pcb_uw is currently
                   4759:         * zero so it is safe to have interrupts going here.)
                   4760:         */
                   4761:        ld      [%g3 + P_VMSPACE], %o3  ! vm = p->p_vmspace;
1.71      pk       4762:        ld      [%o3 + VM_PMAP], %o3    ! pm = vm->vm_map.vm_pmap;
                   4763:        ld      [%o3 + PMAP_CTX], %o0   ! if (pm->pm_ctx != NULL)
1.1       deraadt  4764:        tst     %o0
                   4765:        bnz,a   Lsw_havectx             !       goto havecontext;
1.71      pk       4766:         ld     [%o3 + PMAP_CTXNUM], %o0        ! load context number
1.1       deraadt  4767:
                   4768:        /* p does not have a context: call ctx_alloc to get one */
                   4769:        save    %sp, -CCFSZ, %sp
1.111     pk       4770:        call    _C_LABEL(ctx_alloc)     ! ctx_alloc(pm);
1.71      pk       4771:         mov    %i3, %o0
                   4772:
1.1       deraadt  4773:        ret
                   4774:         restore
                   4775:
                   4776:        /* p does have a context: just switch to it */
                   4777: Lsw_havectx:
1.52      pk       4778:        ! context is in %o0
1.103     pk       4779:        ! pmap is in %o3
1.9       deraadt  4780: #if (defined(SUN4) || defined(SUN4C)) && defined(SUN4M)
1.111     pk       4781:        sethi   %hi(_C_LABEL(cputyp)), %o1      ! what cpu are we running on?
                   4782:        ld      [%o1 + %lo(_C_LABEL(cputyp))], %o1
1.13      deraadt  4783:        cmp     %o1, CPU_SUN4M
1.52      pk       4784:        be      1f
1.9       deraadt  4785:         nop
                   4786: #endif
                   4787: #if defined(SUN4) || defined(SUN4C)
1.1       deraadt  4788:        set     AC_CONTEXT, %o1
                   4789:        retl
1.52      pk       4790:         stba   %o0, [%o1] ASI_CONTROL  ! setcontext(vm->vm_pmap.pm_ctxnum);
1.9       deraadt  4791: #endif
                   4792: 1:
                   4793: #if defined(SUN4M)
1.86      pk       4794:        /*
                   4795:         * Flush caches that need to be flushed on context switch.
                   4796:         * We know this is currently only necessary on the sun4m hypersparc.
                   4797:         */
                   4798:        set     CPUINFO_VA+CPUINFO_PURE_VCACHE_FLS, %o2
                   4799:        ld      [%o2], %o2
                   4800:        mov     %o7, %g7        ! save return address
                   4801:        jmpl    %o2, %o7        ! this function must not clobber %o0 and %g7
                   4802:         nop
                   4803:
1.9       deraadt  4804:        set     SRMMU_CXR, %o1
1.86      pk       4805:        jmp     %g7 + 8
1.52      pk       4806:         sta    %o0, [%o1] ASI_SRMMU    ! setcontext(vm->vm_pmap.pm_ctxnum);
1.9       deraadt  4807: #endif
1.1       deraadt  4808:
                   4809: Lsw_sameproc:
                   4810:        /*
                   4811:         * We are resuming the process that was running at the
1.10      deraadt  4812:         * call to switch().  Just set psr ipl and return.
1.1       deraadt  4813:         */
                   4814: !      wr      %g2, 0 %psr             ! %psr = newpsr; (done earlier)
                   4815:        nop
                   4816:        retl
                   4817:         nop
                   4818:
                   4819:
                   4820: /*
                   4821:  * Snapshot the current process so that stack frames are up to date.
1.51      pk       4822:  * Only used just before a crash dump.
1.1       deraadt  4823:  */
                   4824: ENTRY(snapshot)
1.67      pk       4825:        std     %o6, [%o0 + PCB_SP]     ! save sp
1.1       deraadt  4826:        rd      %psr, %o1               ! save psr
                   4827:        st      %o1, [%o0 + PCB_PSR]
                   4828:
                   4829:        /*
1.10      deraadt  4830:         * Just like switch(); same XXX comments apply.
1.1       deraadt  4831:         * 7 of each.  Minor tweak: the 7th restore is
                   4832:         * done after a ret.
                   4833:         */
1.51      pk       4834:        SAVE; SAVE; SAVE; SAVE; SAVE; SAVE; SAVE
1.1       deraadt  4835:        restore; restore; restore; restore; restore; restore; ret; restore
                   4836:
1.51      pk       4837:
                   4838: /*
1.125.2.2! bouyer   4839:  * cpu_fork() arrange for proc_trampoline() to run after a process gets
        !          4840:  * chosen in switch(). The stack frame will contain a function pointer
        !          4841:  * in %l0, and an argument to pass to it in %l2.
1.51      pk       4842:  *
                   4843:  * If the function *(%l0) returns, we arrange for an immediate return
                   4844:  * to user mode. This happens in two known cases: after execve(2) of init,
                   4845:  * and when returning a child to user mode after a fork(2).
1.125.2.2! bouyer   4846:  *
        !          4847:  * If were setting up a kernel thread, the function *(%l0) will not return.
1.51      pk       4848:  */
                   4849: ENTRY(proc_trampoline)
1.125.2.2! bouyer   4850:        /*
        !          4851:         * Note: cpu_fork() has set up a stack frame for us to run in,
        !          4852:         * so we can call other functions from here without using
        !          4853:         * `save ... restore'.
        !          4854:         */
        !          4855: #ifdef MULTIPROCESSOR
        !          4856:        /* Finish setup in SMP environment: acquire locks etc. */
        !          4857:        call _C_LABEL(proc_trampoline_mp)
        !          4858:         nop
        !          4859: #endif
        !          4860:
        !          4861:        call    %l0
1.51      pk       4862:         mov    %l1, %o0
                   4863:
                   4864:        /*
                   4865:         * Here we finish up as in syscall, but simplified.  We need to
1.125.2.2! bouyer   4866:         * fiddle pc and npc a bit, as execve() / setregs() will have
        !          4867:         * set npc only, anticipating that trap.c will advance past the
        !          4868:         * trap instruction; but we bypass that, so we must do it manually.
1.51      pk       4869:         */
                   4870:        mov     PSR_S, %l0              ! user psr (no need to load it)
                   4871:        !?wr    %g0, 2, %wim            ! %wim = 2
                   4872:        ld      [%sp + CCFSZ + 8], %l1  ! pc = tf->tf_npc from execve/fork
                   4873:        b       return_from_syscall
                   4874:         add    %l1, 4, %l2             ! npc = pc+4
1.1       deraadt  4875:
                   4876: /*
                   4877:  * {fu,su}{,i}{byte,word}
                   4878:  */
1.111     pk       4879: _ENTRY(fuiword)
1.1       deraadt  4880: ENTRY(fuword)
                   4881:        set     KERNBASE, %o2
                   4882:        cmp     %o0, %o2                ! if addr >= KERNBASE...
                   4883:        bgeu    Lfsbadaddr
                   4884:        EMPTY
                   4885:        btst    3, %o0                  ! or has low bits set...
                   4886:        bnz     Lfsbadaddr              !       go return -1
                   4887:        EMPTY
1.111     pk       4888:        sethi   %hi(cpcb), %o2          ! cpcb->pcb_onfault = Lfserr;
                   4889:        ld      [%o2 + %lo(cpcb)], %o2
1.1       deraadt  4890:        set     Lfserr, %o3
                   4891:        st      %o3, [%o2 + PCB_ONFAULT]
                   4892:        ld      [%o0], %o0              ! fetch the word
                   4893:        retl                            ! phew, made it, return the word
                   4894:        st      %g0, [%o2 + PCB_ONFAULT]! but first clear onfault
                   4895:
                   4896: Lfserr:
                   4897:        st      %g0, [%o2 + PCB_ONFAULT]! error in r/w, clear pcb_onfault
                   4898: Lfsbadaddr:
                   4899:        retl                            ! and return error indicator
1.21      deraadt  4900:         mov    -1, %o0
1.1       deraadt  4901:
                   4902:        /*
                   4903:         * This is just like Lfserr, but it's a global label that allows
                   4904:         * mem_access_fault() to check to see that we don't want to try to
                   4905:         * page in the fault.  It's used by fuswintr() etc.
                   4906:         */
1.111     pk       4907:        .globl  _C_LABEL(Lfsbail)
                   4908: _C_LABEL(Lfsbail):
1.1       deraadt  4909:        st      %g0, [%o2 + PCB_ONFAULT]! error in r/w, clear pcb_onfault
                   4910:        retl                            ! and return error indicator
1.21      deraadt  4911:         mov    -1, %o0
1.1       deraadt  4912:
                   4913:        /*
                   4914:         * Like fusword but callable from interrupt context.
                   4915:         * Fails if data isn't resident.
                   4916:         */
                   4917: ENTRY(fuswintr)
                   4918:        set     KERNBASE, %o2
                   4919:        cmp     %o0, %o2                ! if addr >= KERNBASE
                   4920:        bgeu    Lfsbadaddr              !       return error
                   4921:        EMPTY
1.111     pk       4922:        sethi   %hi(cpcb), %o2          ! cpcb->pcb_onfault = Lfsbail;
                   4923:        ld      [%o2 + %lo(cpcb)], %o2
                   4924:        set     _C_LABEL(Lfsbail), %o3
1.1       deraadt  4925:        st      %o3, [%o2 + PCB_ONFAULT]
                   4926:        lduh    [%o0], %o0              ! fetch the halfword
                   4927:        retl                            ! made it
                   4928:        st      %g0, [%o2 + PCB_ONFAULT]! but first clear onfault
                   4929:
                   4930: ENTRY(fusword)
                   4931:        set     KERNBASE, %o2
                   4932:        cmp     %o0, %o2                ! if addr >= KERNBASE
                   4933:        bgeu    Lfsbadaddr              !       return error
                   4934:        EMPTY
1.111     pk       4935:        sethi   %hi(cpcb), %o2          ! cpcb->pcb_onfault = Lfserr;
                   4936:        ld      [%o2 + %lo(cpcb)], %o2
1.1       deraadt  4937:        set     Lfserr, %o3
                   4938:        st      %o3, [%o2 + PCB_ONFAULT]
                   4939:        lduh    [%o0], %o0              ! fetch the halfword
                   4940:        retl                            ! made it
                   4941:        st      %g0, [%o2 + PCB_ONFAULT]! but first clear onfault
                   4942:
1.111     pk       4943: _ENTRY(fuibyte)
1.1       deraadt  4944: ENTRY(fubyte)
                   4945:        set     KERNBASE, %o2
                   4946:        cmp     %o0, %o2                ! if addr >= KERNBASE
                   4947:        bgeu    Lfsbadaddr              !       return error
                   4948:        EMPTY
1.111     pk       4949:        sethi   %hi(cpcb), %o2          ! cpcb->pcb_onfault = Lfserr;
                   4950:        ld      [%o2 + %lo(cpcb)], %o2
1.1       deraadt  4951:        set     Lfserr, %o3
                   4952:        st      %o3, [%o2 + PCB_ONFAULT]
                   4953:        ldub    [%o0], %o0              ! fetch the byte
                   4954:        retl                            ! made it
                   4955:        st      %g0, [%o2 + PCB_ONFAULT]! but first clear onfault
                   4956:
1.111     pk       4957: _ENTRY(suiword)
1.1       deraadt  4958: ENTRY(suword)
                   4959:        set     KERNBASE, %o2
                   4960:        cmp     %o0, %o2                ! if addr >= KERNBASE ...
                   4961:        bgeu    Lfsbadaddr
                   4962:        EMPTY
                   4963:        btst    3, %o0                  ! or has low bits set ...
                   4964:        bnz     Lfsbadaddr              !       go return error
                   4965:        EMPTY
1.111     pk       4966:        sethi   %hi(cpcb), %o2          ! cpcb->pcb_onfault = Lfserr;
                   4967:        ld      [%o2 + %lo(cpcb)], %o2
1.1       deraadt  4968:        set     Lfserr, %o3
                   4969:        st      %o3, [%o2 + PCB_ONFAULT]
                   4970:        st      %o1, [%o0]              ! store the word
                   4971:        st      %g0, [%o2 + PCB_ONFAULT]! made it, clear onfault
                   4972:        retl                            ! and return 0
                   4973:        clr     %o0
                   4974:
                   4975: ENTRY(suswintr)
                   4976:        set     KERNBASE, %o2
                   4977:        cmp     %o0, %o2                ! if addr >= KERNBASE
                   4978:        bgeu    Lfsbadaddr              !       go return error
                   4979:        EMPTY
1.111     pk       4980:        sethi   %hi(cpcb), %o2          ! cpcb->pcb_onfault = Lfsbail;
                   4981:        ld      [%o2 + %lo(cpcb)], %o2
                   4982:        set     _C_LABEL(Lfsbail), %o3
1.1       deraadt  4983:        st      %o3, [%o2 + PCB_ONFAULT]
                   4984:        sth     %o1, [%o0]              ! store the halfword
                   4985:        st      %g0, [%o2 + PCB_ONFAULT]! made it, clear onfault
                   4986:        retl                            ! and return 0
                   4987:        clr     %o0
                   4988:
                   4989: ENTRY(susword)
                   4990:        set     KERNBASE, %o2
                   4991:        cmp     %o0, %o2                ! if addr >= KERNBASE
                   4992:        bgeu    Lfsbadaddr              !       go return error
                   4993:        EMPTY
1.111     pk       4994:        sethi   %hi(cpcb), %o2          ! cpcb->pcb_onfault = Lfserr;
                   4995:        ld      [%o2 + %lo(cpcb)], %o2
1.1       deraadt  4996:        set     Lfserr, %o3
                   4997:        st      %o3, [%o2 + PCB_ONFAULT]
                   4998:        sth     %o1, [%o0]              ! store the halfword
                   4999:        st      %g0, [%o2 + PCB_ONFAULT]! made it, clear onfault
                   5000:        retl                            ! and return 0
                   5001:        clr     %o0
                   5002:
1.111     pk       5003: _ENTRY(suibyte)
1.1       deraadt  5004: ENTRY(subyte)
                   5005:        set     KERNBASE, %o2
                   5006:        cmp     %o0, %o2                ! if addr >= KERNBASE
                   5007:        bgeu    Lfsbadaddr              !       go return error
                   5008:        EMPTY
1.111     pk       5009:        sethi   %hi(cpcb), %o2          ! cpcb->pcb_onfault = Lfserr;
                   5010:        ld      [%o2 + %lo(cpcb)], %o2
1.1       deraadt  5011:        set     Lfserr, %o3
                   5012:        st      %o3, [%o2 + PCB_ONFAULT]
                   5013:        stb     %o1, [%o0]              ! store the byte
                   5014:        st      %g0, [%o2 + PCB_ONFAULT]! made it, clear onfault
                   5015:        retl                            ! and return 0
                   5016:        clr     %o0
                   5017:
                   5018: /* probeget and probeset are meant to be used during autoconfiguration */
                   5019:
                   5020: /*
                   5021:  * probeget(addr, size) caddr_t addr; int size;
                   5022:  *
                   5023:  * Read or write a (byte,word,longword) from the given address.
                   5024:  * Like {fu,su}{byte,halfword,word} but our caller is supposed
                   5025:  * to know what he is doing... the address can be anywhere.
                   5026:  *
                   5027:  * We optimize for space, rather than time, here.
                   5028:  */
                   5029: ENTRY(probeget)
                   5030:        ! %o0 = addr, %o1 = (1,2,4)
1.111     pk       5031:        sethi   %hi(cpcb), %o2
                   5032:        ld      [%o2 + %lo(cpcb)], %o2  ! cpcb->pcb_onfault = Lfserr;
1.1       deraadt  5033:        set     Lfserr, %o5
                   5034:        st      %o5, [%o2 + PCB_ONFAULT]
                   5035:        btst    1, %o1
                   5036:        bnz,a   0f                      ! if (len & 1)
                   5037:         ldub   [%o0], %o0              !       value = *(char *)addr;
                   5038: 0:     btst    2, %o1
                   5039:        bnz,a   0f                      ! if (len & 2)
                   5040:         lduh   [%o0], %o0              !       value = *(short *)addr;
                   5041: 0:     btst    4, %o1
                   5042:        bnz,a   0f                      ! if (len & 4)
                   5043:         ld     [%o0], %o0              !       value = *(int *)addr;
                   5044: 0:     retl                            ! made it, clear onfault and return
                   5045:         st     %g0, [%o2 + PCB_ONFAULT]
                   5046:
                   5047: /*
                   5048:  * probeset(addr, size, val) caddr_t addr; int size, val;
                   5049:  *
                   5050:  * As above, but we return 0 on success.
                   5051:  */
                   5052: ENTRY(probeset)
                   5053:        ! %o0 = addr, %o1 = (1,2,4), %o2 = val
1.111     pk       5054:        sethi   %hi(cpcb), %o3
                   5055:        ld      [%o3 + %lo(cpcb)], %o3  ! cpcb->pcb_onfault = Lfserr;
1.1       deraadt  5056:        set     Lfserr, %o5
1.35      pk       5057:        st      %o5, [%o3 + PCB_ONFAULT]
1.1       deraadt  5058:        btst    1, %o1
                   5059:        bnz,a   0f                      ! if (len & 1)
                   5060:         stb    %o2, [%o0]              !       *(char *)addr = value;
                   5061: 0:     btst    2, %o1
                   5062:        bnz,a   0f                      ! if (len & 2)
                   5063:         sth    %o2, [%o0]              !       *(short *)addr = value;
                   5064: 0:     btst    4, %o1
                   5065:        bnz,a   0f                      ! if (len & 4)
                   5066:         st     %o2, [%o0]              !       *(int *)addr = value;
                   5067: 0:     clr     %o0                     ! made it, clear onfault and return 0
                   5068:        retl
1.35      pk       5069:         st     %g0, [%o3 + PCB_ONFAULT]
1.21      deraadt  5070:
                   5071: /*
1.22      deraadt  5072:  * int xldcontrolb(caddr_t, pcb)
                   5073:  *                 %o0     %o1
1.21      deraadt  5074:  *
                   5075:  * read a byte from the specified address in ASI_CONTROL space.
                   5076:  */
1.22      deraadt  5077: ENTRY(xldcontrolb)
1.111     pk       5078:        !sethi  %hi(cpcb), %o2
                   5079:        !ld     [%o2 + %lo(cpcb)], %o2  ! cpcb->pcb_onfault = Lfsbail;
1.22      deraadt  5080:        or      %o1, %g0, %o2           ! %o2 = %o1
1.111     pk       5081:        set     _C_LABEL(Lfsbail), %o5
1.21      deraadt  5082:        st      %o5, [%o2 + PCB_ONFAULT]
                   5083:        lduba   [%o0] ASI_CONTROL, %o0  ! read
                   5084: 0:     retl
1.1       deraadt  5085:         st     %g0, [%o2 + PCB_ONFAULT]
1.78      pk       5086:
                   5087: /*
                   5088:  * int fkbyte(caddr_t, pcb)
                   5089:  *           %o0      %o1
                   5090:  *
                   5091:  * Just like fubyte(), but for kernel space.
                   5092:  * (currently used to work around unexplained transient bus errors
                   5093:  *  when reading the VME interrupt vector)
                   5094:  */
                   5095: ENTRY(fkbyte)
                   5096:        or      %o1, %g0, %o2           ! %o2 = %o1
1.111     pk       5097:        set     _C_LABEL(Lfsbail), %o5
1.78      pk       5098:        st      %o5, [%o2 + PCB_ONFAULT]
                   5099:        ldub    [%o0], %o0              ! fetch the byte
                   5100:        retl                            ! made it
                   5101:         st     %g0, [%o2 + PCB_ONFAULT]! but first clear onfault
1.1       deraadt  5102:
1.93      pk       5103:
                   5104: /*
                   5105:  * Insert entry into doubly-linked queue.
                   5106:  * We could just do this in C, but gcc does not do leaves well (yet).
                   5107:  */
                   5108: ENTRY(_insque)
                   5109:        ! %o0 = e = what to insert; %o1 = after = entry to insert after
                   5110:        st      %o1, [%o0 + 4]          ! e->prev = after;
                   5111:        ld      [%o1], %o2              ! tmp = after->next;
                   5112:        st      %o2, [%o0]              ! e->next = tmp;
                   5113:        st      %o0, [%o1]              ! after->next = e;
                   5114:        retl
                   5115:        st      %o0, [%o2 + 4]          ! tmp->prev = e;
                   5116:
                   5117:
                   5118: /*
                   5119:  * Remove entry from doubly-linked queue.
                   5120:  */
                   5121: ENTRY(_remque)
                   5122:        ! %o0 = e = what to remove
                   5123:        ld      [%o0], %o1              ! n = e->next;
                   5124:        ld      [%o0 + 4], %o2          ! p = e->prev;
                   5125:        st      %o2, [%o1 + 4]          ! n->prev = p;
                   5126:        retl
                   5127:        st      %o1, [%o2]              ! p->next = n;
1.1       deraadt  5128:
                   5129: /*
                   5130:  * copywords(src, dst, nbytes)
                   5131:  *
                   5132:  * Copy `nbytes' bytes from src to dst, both of which are word-aligned;
                   5133:  * nbytes is a multiple of four.  It may, however, be zero, in which case
                   5134:  * nothing is to be copied.
                   5135:  */
                   5136: ENTRY(copywords)
                   5137:        ! %o0 = src, %o1 = dst, %o2 = nbytes
                   5138:        b       1f
                   5139:        deccc   4, %o2
                   5140: 0:
                   5141:        st      %o3, [%o1 + %o2]
                   5142:        deccc   4, %o2                  ! while ((n -= 4) >= 0)
                   5143: 1:
                   5144:        bge,a   0b                      !    *(int *)(dst+n) = *(int *)(src+n);
                   5145:        ld      [%o0 + %o2], %o3
                   5146:        retl
                   5147:        nop
                   5148:
                   5149: /*
                   5150:  * qcopy(src, dst, nbytes)
                   5151:  *
                   5152:  * (q for `quad' or `quick', as opposed to b for byte/block copy)
                   5153:  *
                   5154:  * Just like copywords, but everything is multiples of 8.
                   5155:  */
                   5156: ENTRY(qcopy)
                   5157:        b       1f
                   5158:        deccc   8, %o2
                   5159: 0:
                   5160:        std     %o4, [%o1 + %o2]
                   5161:        deccc   8, %o2
                   5162: 1:
                   5163:        bge,a   0b
                   5164:        ldd     [%o0 + %o2], %o4
                   5165:        retl
                   5166:        nop
                   5167:
                   5168: /*
                   5169:  * qzero(addr, nbytes)
                   5170:  *
                   5171:  * Zeroes `nbytes' bytes of a quad-aligned virtual address,
                   5172:  * where nbytes is itself a multiple of 8.
                   5173:  */
                   5174: ENTRY(qzero)
                   5175:        ! %o0 = addr, %o1 = len (in bytes)
                   5176:        clr     %g1
                   5177: 0:
                   5178:        deccc   8, %o1                  ! while ((n =- 8) >= 0)
                   5179:        bge,a   0b
                   5180:        std     %g0, [%o0 + %o1]        !       *(quad *)(addr + n) = 0;
                   5181:        retl
                   5182:        nop
                   5183:
                   5184: /*
1.83      mycroft  5185:  * kernel bcopy
1.1       deraadt  5186:  * Assumes regions do not overlap; has no useful return value.
                   5187:  *
                   5188:  * Must not use %g7 (see copyin/copyout above).
                   5189:  */
                   5190:
                   5191: #define        BCOPY_SMALL     32      /* if < 32, copy by bytes */
                   5192:
                   5193: ENTRY(bcopy)
                   5194:        cmp     %o2, BCOPY_SMALL
                   5195: Lbcopy_start:
                   5196:        bge,a   Lbcopy_fancy    ! if >= this many, go be fancy.
                   5197:        btst    7, %o0          ! (part of being fancy)
                   5198:
                   5199:        /*
                   5200:         * Not much to copy, just do it a byte at a time.
                   5201:         */
                   5202:        deccc   %o2             ! while (--len >= 0)
                   5203:        bl      1f
                   5204:        EMPTY
                   5205: 0:
                   5206:        inc     %o0
                   5207:        ldsb    [%o0 - 1], %o4  !       (++dst)[-1] = *src++;
                   5208:        stb     %o4, [%o1]
                   5209:        deccc   %o2
                   5210:        bge     0b
                   5211:        inc     %o1
                   5212: 1:
                   5213:        retl
1.80      mrg      5214:         nop
1.1       deraadt  5215:        /* NOTREACHED */
                   5216:
                   5217:        /*
                   5218:         * Plenty of data to copy, so try to do it optimally.
                   5219:         */
                   5220: Lbcopy_fancy:
                   5221:        ! check for common case first: everything lines up.
                   5222: !      btst    7, %o0          ! done already
                   5223:        bne     1f
                   5224:        EMPTY
                   5225:        btst    7, %o1
                   5226:        be,a    Lbcopy_doubles
                   5227:        dec     8, %o2          ! if all lined up, len -= 8, goto bcopy_doubes
                   5228:
                   5229:        ! If the low bits match, we can make these line up.
                   5230: 1:
                   5231:        xor     %o0, %o1, %o3   ! t = src ^ dst;
                   5232:        btst    1, %o3          ! if (t & 1) {
                   5233:        be,a    1f
                   5234:        btst    1, %o0          ! [delay slot: if (src & 1)]
                   5235:
                   5236:        ! low bits do not match, must copy by bytes.
                   5237: 0:
                   5238:        ldsb    [%o0], %o4      !       do {
                   5239:        inc     %o0             !               (++dst)[-1] = *src++;
                   5240:        inc     %o1
                   5241:        deccc   %o2
                   5242:        bnz     0b              !       } while (--len != 0);
                   5243:        stb     %o4, [%o1 - 1]
                   5244:        retl
1.80      mrg      5245:         nop
1.1       deraadt  5246:        /* NOTREACHED */
                   5247:
                   5248:        ! lowest bit matches, so we can copy by words, if nothing else
                   5249: 1:
                   5250:        be,a    1f              ! if (src & 1) {
                   5251:        btst    2, %o3          ! [delay slot: if (t & 2)]
                   5252:
                   5253:        ! although low bits match, both are 1: must copy 1 byte to align
                   5254:        ldsb    [%o0], %o4      !       *dst++ = *src++;
                   5255:        stb     %o4, [%o1]
                   5256:        inc     %o0
                   5257:        inc     %o1
                   5258:        dec     %o2             !       len--;
                   5259:        btst    2, %o3          ! } [if (t & 2)]
                   5260: 1:
                   5261:        be,a    1f              ! if (t & 2) {
                   5262:        btst    2, %o0          ! [delay slot: if (src & 2)]
                   5263:        dec     2, %o2          !       len -= 2;
                   5264: 0:
                   5265:        ldsh    [%o0], %o4      !       do {
                   5266:        sth     %o4, [%o1]      !               *(short *)dst = *(short *)src;
                   5267:        inc     2, %o0          !               dst += 2, src += 2;
                   5268:        deccc   2, %o2          !       } while ((len -= 2) >= 0);
                   5269:        bge     0b
                   5270:        inc     2, %o1
                   5271:        b       Lbcopy_mopb     !       goto mop_up_byte;
                   5272:        btst    1, %o2          ! } [delay slot: if (len & 1)]
                   5273:        /* NOTREACHED */
                   5274:
                   5275:        ! low two bits match, so we can copy by longwords
                   5276: 1:
                   5277:        be,a    1f              ! if (src & 2) {
                   5278:        btst    4, %o3          ! [delay slot: if (t & 4)]
                   5279:
                   5280:        ! although low 2 bits match, they are 10: must copy one short to align
                   5281:        ldsh    [%o0], %o4      !       (*short *)dst = *(short *)src;
                   5282:        sth     %o4, [%o1]
                   5283:        inc     2, %o0          !       dst += 2;
                   5284:        inc     2, %o1          !       src += 2;
                   5285:        dec     2, %o2          !       len -= 2;
                   5286:        btst    4, %o3          ! } [if (t & 4)]
                   5287: 1:
                   5288:        be,a    1f              ! if (t & 4) {
                   5289:        btst    4, %o0          ! [delay slot: if (src & 4)]
                   5290:        dec     4, %o2          !       len -= 4;
                   5291: 0:
                   5292:        ld      [%o0], %o4      !       do {
                   5293:        st      %o4, [%o1]      !               *(int *)dst = *(int *)src;
                   5294:        inc     4, %o0          !               dst += 4, src += 4;
                   5295:        deccc   4, %o2          !       } while ((len -= 4) >= 0);
                   5296:        bge     0b
                   5297:        inc     4, %o1
                   5298:        b       Lbcopy_mopw     !       goto mop_up_word_and_byte;
                   5299:        btst    2, %o2          ! } [delay slot: if (len & 2)]
                   5300:        /* NOTREACHED */
                   5301:
                   5302:        ! low three bits match, so we can copy by doublewords
                   5303: 1:
                   5304:        be      1f              ! if (src & 4) {
                   5305:        dec     8, %o2          ! [delay slot: len -= 8]
                   5306:        ld      [%o0], %o4      !       *(int *)dst = *(int *)src;
                   5307:        st      %o4, [%o1]
                   5308:        inc     4, %o0          !       dst += 4, src += 4, len -= 4;
                   5309:        inc     4, %o1
                   5310:        dec     4, %o2          ! }
                   5311: 1:
                   5312: Lbcopy_doubles:
                   5313:        ldd     [%o0], %o4      ! do {
                   5314:        std     %o4, [%o1]      !       *(double *)dst = *(double *)src;
                   5315:        inc     8, %o0          !       dst += 8, src += 8;
                   5316:        deccc   8, %o2          ! } while ((len -= 8) >= 0);
                   5317:        bge     Lbcopy_doubles
                   5318:        inc     8, %o1
                   5319:
                   5320:        ! check for a usual case again (save work)
                   5321:        btst    7, %o2          ! if ((len & 7) == 0)
                   5322:        be      Lbcopy_done     !       goto bcopy_done;
                   5323:
                   5324:        btst    4, %o2          ! if ((len & 4)) == 0)
                   5325:        be,a    Lbcopy_mopw     !       goto mop_up_word_and_byte;
                   5326:        btst    2, %o2          ! [delay slot: if (len & 2)]
                   5327:        ld      [%o0], %o4      !       *(int *)dst = *(int *)src;
                   5328:        st      %o4, [%o1]
                   5329:        inc     4, %o0          !       dst += 4;
                   5330:        inc     4, %o1          !       src += 4;
                   5331:        btst    2, %o2          ! } [if (len & 2)]
                   5332:
                   5333: 1:
                   5334:        ! mop up trailing word (if present) and byte (if present).
                   5335: Lbcopy_mopw:
                   5336:        be      Lbcopy_mopb     ! no word, go mop up byte
                   5337:        btst    1, %o2          ! [delay slot: if (len & 1)]
                   5338:        ldsh    [%o0], %o4      ! *(short *)dst = *(short *)src;
                   5339:        be      Lbcopy_done     ! if ((len & 1) == 0) goto done;
                   5340:        sth     %o4, [%o1]
                   5341:        ldsb    [%o0 + 2], %o4  ! dst[2] = src[2];
                   5342:        retl
1.80      mrg      5343:         stb    %o4, [%o1 + 2]
1.1       deraadt  5344:        /* NOTREACHED */
                   5345:
                   5346:        ! mop up trailing byte (if present).
                   5347: Lbcopy_mopb:
                   5348:        bne,a   1f
                   5349:        ldsb    [%o0], %o4
                   5350:
                   5351: Lbcopy_done:
                   5352:        retl
1.80      mrg      5353:         nop
1.1       deraadt  5354:
                   5355: 1:
                   5356:        retl
1.80      mrg      5357:         stb    %o4,[%o1]
1.1       deraadt  5358: /*
                   5359:  * ovbcopy(src, dst, len): like bcopy, but regions may overlap.
                   5360:  */
                   5361: ENTRY(ovbcopy)
                   5362:        cmp     %o0, %o1        ! src < dst?
                   5363:        bgeu    Lbcopy_start    ! no, go copy forwards as via bcopy
                   5364:        cmp     %o2, BCOPY_SMALL! (check length for doublecopy first)
                   5365:
                   5366:        /*
                   5367:         * Since src comes before dst, and the regions might overlap,
                   5368:         * we have to do the copy starting at the end and working backwards.
                   5369:         */
                   5370:        add     %o2, %o0, %o0   ! src += len
                   5371:        add     %o2, %o1, %o1   ! dst += len
                   5372:        bge,a   Lback_fancy     ! if len >= BCOPY_SMALL, go be fancy
                   5373:        btst    3, %o0
                   5374:
                   5375:        /*
                   5376:         * Not much to copy, just do it a byte at a time.
                   5377:         */
                   5378:        deccc   %o2             ! while (--len >= 0)
                   5379:        bl      1f
                   5380:        EMPTY
                   5381: 0:
                   5382:        dec     %o0             !       *--dst = *--src;
                   5383:        ldsb    [%o0], %o4
                   5384:        dec     %o1
                   5385:        deccc   %o2
                   5386:        bge     0b
                   5387:        stb     %o4, [%o1]
                   5388: 1:
                   5389:        retl
                   5390:        nop
                   5391:
                   5392:        /*
                   5393:         * Plenty to copy, try to be optimal.
                   5394:         * We only bother with word/halfword/byte copies here.
                   5395:         */
                   5396: Lback_fancy:
                   5397: !      btst    3, %o0          ! done already
                   5398:        bnz     1f              ! if ((src & 3) == 0 &&
                   5399:        btst    3, %o1          !     (dst & 3) == 0)
                   5400:        bz,a    Lback_words     !       goto words;
                   5401:        dec     4, %o2          ! (done early for word copy)
                   5402:
                   5403: 1:
                   5404:        /*
                   5405:         * See if the low bits match.
                   5406:         */
                   5407:        xor     %o0, %o1, %o3   ! t = src ^ dst;
                   5408:        btst    1, %o3
                   5409:        bz,a    3f              ! if (t & 1) == 0, can do better
                   5410:        btst    1, %o0
                   5411:
                   5412:        /*
                   5413:         * Nope; gotta do byte copy.
                   5414:         */
                   5415: 2:
                   5416:        dec     %o0             ! do {
                   5417:        ldsb    [%o0], %o4      !       *--dst = *--src;
                   5418:        dec     %o1
                   5419:        deccc   %o2             ! } while (--len != 0);
                   5420:        bnz     2b
                   5421:        stb     %o4, [%o1]
                   5422:        retl
                   5423:        nop
                   5424:
                   5425: 3:
                   5426:        /*
                   5427:         * Can do halfword or word copy, but might have to copy 1 byte first.
                   5428:         */
                   5429: !      btst    1, %o0          ! done earlier
                   5430:        bz,a    4f              ! if (src & 1) {        /* copy 1 byte */
                   5431:        btst    2, %o3          ! (done early)
                   5432:        dec     %o0             !       *--dst = *--src;
                   5433:        ldsb    [%o0], %o4
                   5434:        dec     %o1
                   5435:        stb     %o4, [%o1]
                   5436:        dec     %o2             !       len--;
                   5437:        btst    2, %o3          ! }
                   5438:
                   5439: 4:
                   5440:        /*
                   5441:         * See if we can do a word copy ((t&2) == 0).
                   5442:         */
                   5443: !      btst    2, %o3          ! done earlier
                   5444:        bz,a    6f              ! if (t & 2) == 0, can do word copy
                   5445:        btst    2, %o0          ! (src&2, done early)
                   5446:
                   5447:        /*
                   5448:         * Gotta do halfword copy.
                   5449:         */
                   5450:        dec     2, %o2          ! len -= 2;
                   5451: 5:
                   5452:        dec     2, %o0          ! do {
                   5453:        ldsh    [%o0], %o4      !       src -= 2;
                   5454:        dec     2, %o1          !       dst -= 2;
                   5455:        deccc   2, %o0          !       *(short *)dst = *(short *)src;
                   5456:        bge     5b              ! } while ((len -= 2) >= 0);
                   5457:        sth     %o4, [%o1]
                   5458:        b       Lback_mopb      ! goto mop_up_byte;
                   5459:        btst    1, %o2          ! (len&1, done early)
                   5460:
                   5461: 6:
                   5462:        /*
                   5463:         * We can do word copies, but we might have to copy
                   5464:         * one halfword first.
                   5465:         */
                   5466: !      btst    2, %o0          ! done already
                   5467:        bz      7f              ! if (src & 2) {
                   5468:        dec     4, %o2          ! (len -= 4, done early)
                   5469:        dec     2, %o0          !       src -= 2, dst -= 2;
                   5470:        ldsh    [%o0], %o4      !       *(short *)dst = *(short *)src;
                   5471:        dec     2, %o1
                   5472:        sth     %o4, [%o1]
                   5473:        dec     2, %o2          !       len -= 2;
                   5474:                                ! }
                   5475:
                   5476: 7:
                   5477: Lback_words:
                   5478:        /*
                   5479:         * Do word copies (backwards), then mop up trailing halfword
                   5480:         * and byte if any.
                   5481:         */
                   5482: !      dec     4, %o2          ! len -= 4, done already
                   5483: 0:                             ! do {
                   5484:        dec     4, %o0          !       src -= 4;
                   5485:        dec     4, %o1          !       src -= 4;
                   5486:        ld      [%o0], %o4      !       *(int *)dst = *(int *)src;
                   5487:        deccc   4, %o2          ! } while ((len -= 4) >= 0);
                   5488:        bge     0b
                   5489:        st      %o4, [%o1]
                   5490:
                   5491:        /*
                   5492:         * Check for trailing shortword.
                   5493:         */
                   5494:        btst    2, %o2          ! if (len & 2) {
                   5495:        bz,a    1f
                   5496:        btst    1, %o2          ! (len&1, done early)
                   5497:        dec     2, %o0          !       src -= 2, dst -= 2;
                   5498:        ldsh    [%o0], %o4      !       *(short *)dst = *(short *)src;
                   5499:        dec     2, %o1
                   5500:        sth     %o4, [%o1]      ! }
                   5501:        btst    1, %o2
                   5502:
                   5503:        /*
                   5504:         * Check for trailing byte.
                   5505:         */
                   5506: 1:
                   5507: Lback_mopb:
                   5508: !      btst    1, %o2          ! (done already)
                   5509:        bnz,a   1f              ! if (len & 1) {
                   5510:        ldsb    [%o0 - 1], %o4  !       b = src[-1];
                   5511:        retl
                   5512:        nop
                   5513: 1:
                   5514:        retl                    !       dst[-1] = b;
                   5515:        stb     %o4, [%o1 - 1]  ! }
                   5516:
1.79      mrg      5517: /*
                   5518:  * kcopy() is exactly like bcopy except that it set pcb_onfault such that
                   5519:  * when a fault occurs, it is able to return -1 to indicate this to the
                   5520:  * caller.
                   5521:  */
                   5522: ENTRY(kcopy)
1.111     pk       5523:        sethi   %hi(cpcb), %o5          ! cpcb->pcb_onfault = Lkcerr;
                   5524:        ld      [%o5 + %lo(cpcb)], %o5
1.79      mrg      5525:        set     Lkcerr, %o3
1.107     mycroft  5526:        ld      [%o5 + PCB_ONFAULT], %g1! save current onfault handler
1.79      mrg      5527:        st      %o3, [%o5 + PCB_ONFAULT]
                   5528:
                   5529:        cmp     %o2, BCOPY_SMALL
                   5530: Lkcopy_start:
                   5531:        bge,a   Lkcopy_fancy    ! if >= this many, go be fancy.
1.106     pk       5532:         btst   7, %o0          ! (part of being fancy)
1.79      mrg      5533:
                   5534:        /*
                   5535:         * Not much to copy, just do it a byte at a time.
                   5536:         */
                   5537:        deccc   %o2             ! while (--len >= 0)
1.108     mycroft  5538:        bl      1f
                   5539:         EMPTY
1.79      mrg      5540: 0:
1.107     mycroft  5541:        ldsb    [%o0], %o4      !       *dst++ = *src++;
1.79      mrg      5542:        inc     %o0
                   5543:        stb     %o4, [%o1]
                   5544:        deccc   %o2
                   5545:        bge     0b
1.106     pk       5546:         inc    %o1
1.79      mrg      5547: 1:
1.106     pk       5548:        st      %g1, [%o5 + PCB_ONFAULT]        ! restore onfault
1.79      mrg      5549:        retl
1.106     pk       5550:         mov    0, %o0          ! delay slot: return success
1.79      mrg      5551:        /* NOTREACHED */
                   5552:
                   5553:        /*
                   5554:         * Plenty of data to copy, so try to do it optimally.
                   5555:         */
                   5556: Lkcopy_fancy:
                   5557:        ! check for common case first: everything lines up.
                   5558: !      btst    7, %o0          ! done already
                   5559:        bne     1f
1.108     mycroft  5560:         EMPTY
1.79      mrg      5561:        btst    7, %o1
                   5562:        be,a    Lkcopy_doubles
1.106     pk       5563:         dec    8, %o2          ! if all lined up, len -= 8, goto bcopy_doubes
1.79      mrg      5564:
                   5565:        ! If the low bits match, we can make these line up.
                   5566: 1:
                   5567:        xor     %o0, %o1, %o3   ! t = src ^ dst;
                   5568:        btst    1, %o3          ! if (t & 1) {
                   5569:        be,a    1f
1.106     pk       5570:         btst   1, %o0          ! [delay slot: if (src & 1)]
1.79      mrg      5571:
                   5572:        ! low bits do not match, must copy by bytes.
                   5573: 0:
                   5574:        ldsb    [%o0], %o4      !       do {
1.107     mycroft  5575:        inc     %o0             !               *dst++ = *src++;
                   5576:        stb     %o4, [%o1]
1.79      mrg      5577:        deccc   %o2
                   5578:        bnz     0b              !       } while (--len != 0);
1.107     mycroft  5579:         inc    %o1
1.106     pk       5580:        st      %g1, [%o5 + PCB_ONFAULT]        ! restore onfault
1.79      mrg      5581:        retl
1.106     pk       5582:         mov    0, %o0          ! delay slot: return success
1.79      mrg      5583:        /* NOTREACHED */
                   5584:
                   5585:        ! lowest bit matches, so we can copy by words, if nothing else
                   5586: 1:
                   5587:        be,a    1f              ! if (src & 1) {
1.106     pk       5588:         btst   2, %o3          ! [delay slot: if (t & 2)]
1.79      mrg      5589:
                   5590:        ! although low bits match, both are 1: must copy 1 byte to align
                   5591:        ldsb    [%o0], %o4      !       *dst++ = *src++;
1.107     mycroft  5592:        inc     %o0
1.79      mrg      5593:        stb     %o4, [%o1]
1.107     mycroft  5594:        dec     %o2             !       len--;
1.79      mrg      5595:        inc     %o1
                   5596:        btst    2, %o3          ! } [if (t & 2)]
                   5597: 1:
                   5598:        be,a    1f              ! if (t & 2) {
1.106     pk       5599:         btst   2, %o0          ! [delay slot: if (src & 2)]
1.79      mrg      5600:        dec     2, %o2          !       len -= 2;
                   5601: 0:
                   5602:        ldsh    [%o0], %o4      !       do {
1.107     mycroft  5603:        inc     2, %o0          !               dst += 2, src += 2;
1.79      mrg      5604:        sth     %o4, [%o1]      !               *(short *)dst = *(short *)src;
                   5605:        deccc   2, %o2          !       } while ((len -= 2) >= 0);
                   5606:        bge     0b
1.106     pk       5607:         inc    2, %o1
1.79      mrg      5608:        b       Lkcopy_mopb     !       goto mop_up_byte;
1.106     pk       5609:         btst   1, %o2          ! } [delay slot: if (len & 1)]
1.79      mrg      5610:        /* NOTREACHED */
                   5611:
                   5612:        ! low two bits match, so we can copy by longwords
                   5613: 1:
                   5614:        be,a    1f              ! if (src & 2) {
1.106     pk       5615:         btst   4, %o3          ! [delay slot: if (t & 4)]
1.79      mrg      5616:
                   5617:        ! although low 2 bits match, they are 10: must copy one short to align
                   5618:        ldsh    [%o0], %o4      !       (*short *)dst = *(short *)src;
1.107     mycroft  5619:        inc     2, %o0          !       dst += 2;
1.79      mrg      5620:        sth     %o4, [%o1]
1.107     mycroft  5621:        dec     2, %o2          !       len -= 2;
1.79      mrg      5622:        inc     2, %o1          !       src += 2;
                   5623:        btst    4, %o3          ! } [if (t & 4)]
                   5624: 1:
                   5625:        be,a    1f              ! if (t & 4) {
1.106     pk       5626:         btst   4, %o0          ! [delay slot: if (src & 4)]
1.79      mrg      5627:        dec     4, %o2          !       len -= 4;
                   5628: 0:
                   5629:        ld      [%o0], %o4      !       do {
1.107     mycroft  5630:        inc     4, %o0          !               dst += 4, src += 4;
1.79      mrg      5631:        st      %o4, [%o1]      !               *(int *)dst = *(int *)src;
                   5632:        deccc   4, %o2          !       } while ((len -= 4) >= 0);
                   5633:        bge     0b
1.106     pk       5634:         inc    4, %o1
1.79      mrg      5635:        b       Lkcopy_mopw     !       goto mop_up_word_and_byte;
1.106     pk       5636:         btst   2, %o2          ! } [delay slot: if (len & 2)]
1.79      mrg      5637:        /* NOTREACHED */
                   5638:
                   5639:        ! low three bits match, so we can copy by doublewords
                   5640: 1:
                   5641:        be      1f              ! if (src & 4) {
1.106     pk       5642:         dec    8, %o2          ! [delay slot: len -= 8]
1.79      mrg      5643:        ld      [%o0], %o4      !       *(int *)dst = *(int *)src;
1.107     mycroft  5644:        inc     4, %o0          !       dst += 4, src += 4, len -= 4;
1.79      mrg      5645:        st      %o4, [%o1]
1.107     mycroft  5646:        dec     4, %o2          ! }
1.79      mrg      5647:        inc     4, %o1
                   5648: 1:
                   5649: Lkcopy_doubles:
                   5650:        ! swap %o4 with %o2 during doubles copy, since %o5 is verboten
                   5651:        mov     %o2, %o4
                   5652: Lkcopy_doubles2:
                   5653:        ldd     [%o0], %o2      ! do {
1.107     mycroft  5654:        inc     8, %o0          !       dst += 8, src += 8;
1.79      mrg      5655:        std     %o2, [%o1]      !       *(double *)dst = *(double *)src;
                   5656:        deccc   8, %o4          ! } while ((len -= 8) >= 0);
                   5657:        bge     Lkcopy_doubles2
                   5658:         inc    8, %o1
                   5659:        mov     %o4, %o2        ! restore len
                   5660:
                   5661:        ! check for a usual case again (save work)
                   5662:        btst    7, %o2          ! if ((len & 7) == 0)
                   5663:        be      Lkcopy_done     !       goto bcopy_done;
                   5664:
1.106     pk       5665:         btst   4, %o2          ! if ((len & 4)) == 0)
1.79      mrg      5666:        be,a    Lkcopy_mopw     !       goto mop_up_word_and_byte;
1.106     pk       5667:         btst   2, %o2          ! [delay slot: if (len & 2)]
1.79      mrg      5668:        ld      [%o0], %o4      !       *(int *)dst = *(int *)src;
1.107     mycroft  5669:        inc     4, %o0          !       dst += 4;
1.79      mrg      5670:        st      %o4, [%o1]
                   5671:        inc     4, %o1          !       src += 4;
                   5672:        btst    2, %o2          ! } [if (len & 2)]
                   5673:
                   5674: 1:
                   5675:        ! mop up trailing word (if present) and byte (if present).
                   5676: Lkcopy_mopw:
                   5677:        be      Lkcopy_mopb     ! no word, go mop up byte
1.106     pk       5678:         btst   1, %o2          ! [delay slot: if (len & 1)]
1.79      mrg      5679:        ldsh    [%o0], %o4      ! *(short *)dst = *(short *)src;
                   5680:        be      Lkcopy_done     ! if ((len & 1) == 0) goto done;
1.106     pk       5681:         sth    %o4, [%o1]
1.79      mrg      5682:        ldsb    [%o0 + 2], %o4  ! dst[2] = src[2];
                   5683:        stb     %o4, [%o1 + 2]
1.106     pk       5684:        st      %g1, [%o5 + PCB_ONFAULT]! restore onfault
1.79      mrg      5685:        retl
1.106     pk       5686:         mov    0, %o0          ! delay slot: return success
1.79      mrg      5687:        /* NOTREACHED */
                   5688:
                   5689:        ! mop up trailing byte (if present).
                   5690: Lkcopy_mopb:
                   5691:        bne,a   1f
1.106     pk       5692:         ldsb   [%o0], %o4
1.79      mrg      5693:
                   5694: Lkcopy_done:
1.106     pk       5695:        st      %g1, [%o5 + PCB_ONFAULT]        ! restore onfault
1.79      mrg      5696:        retl
1.106     pk       5697:         mov    0, %o0          ! delay slot: return success
1.108     mycroft  5698:        /* NOTREACHED */
1.79      mrg      5699:
                   5700: 1:
1.107     mycroft  5701:        stb     %o4, [%o1]
                   5702:        st      %g1, [%o5 + PCB_ONFAULT]        ! restore onfault
1.79      mrg      5703:        retl
1.107     mycroft  5704:         mov    0, %o0          ! delay slot: return success
1.108     mycroft  5705:        /* NOTREACHED */
1.107     mycroft  5706:
1.79      mrg      5707: Lkcerr:
1.107     mycroft  5708:        st      %g1, [%o5 + PCB_ONFAULT]        ! restore onfault
                   5709:        retl
                   5710:         mov    EFAULT, %o0     ! delay slot: return error indicator
1.108     mycroft  5711:        /* NOTREACHED */
1.1       deraadt  5712:
                   5713: /*
                   5714:  * savefpstate(f) struct fpstate *f;
                   5715:  *
                   5716:  * Store the current FPU state.  The first `st %fsr' may cause a trap;
                   5717:  * our trap handler knows how to recover (by `returning' to savefpcont).
                   5718:  */
                   5719: ENTRY(savefpstate)
                   5720:        rd      %psr, %o1               ! enable FP before we begin
                   5721:        set     PSR_EF, %o2
                   5722:        or      %o1, %o2, %o1
                   5723:        wr      %o1, 0, %psr
                   5724:        /* do some setup work while we wait for PSR_EF to turn on */
                   5725:        set     FSR_QNE, %o5            ! QNE = 0x2000, too big for immediate
                   5726:        clr     %o3                     ! qsize = 0;
                   5727:        nop                             ! (still waiting for PSR_EF)
                   5728: special_fp_store:
                   5729:        st      %fsr, [%o0 + FS_FSR]    ! f->fs_fsr = getfsr();
                   5730:        /*
                   5731:         * Even if the preceding instruction did not trap, the queue
                   5732:         * is not necessarily empty: this state save might be happening
                   5733:         * because user code tried to store %fsr and took the FPU
                   5734:         * from `exception pending' mode to `exception' mode.
                   5735:         * So we still have to check the blasted QNE bit.
                   5736:         * With any luck it will usually not be set.
                   5737:         */
                   5738:        ld      [%o0 + FS_FSR], %o4     ! if (f->fs_fsr & QNE)
                   5739:        btst    %o5, %o4
                   5740:        bnz     Lfp_storeq              !       goto storeq;
                   5741:         std    %f0, [%o0 + FS_REGS + (4*0)]    ! f->fs_f0 = etc;
                   5742: Lfp_finish:
                   5743:        st      %o3, [%o0 + FS_QSIZE]   ! f->fs_qsize = qsize;
                   5744:        std     %f2, [%o0 + FS_REGS + (4*2)]
                   5745:        std     %f4, [%o0 + FS_REGS + (4*4)]
                   5746:        std     %f6, [%o0 + FS_REGS + (4*6)]
                   5747:        std     %f8, [%o0 + FS_REGS + (4*8)]
                   5748:        std     %f10, [%o0 + FS_REGS + (4*10)]
                   5749:        std     %f12, [%o0 + FS_REGS + (4*12)]
                   5750:        std     %f14, [%o0 + FS_REGS + (4*14)]
                   5751:        std     %f16, [%o0 + FS_REGS + (4*16)]
                   5752:        std     %f18, [%o0 + FS_REGS + (4*18)]
                   5753:        std     %f20, [%o0 + FS_REGS + (4*20)]
                   5754:        std     %f22, [%o0 + FS_REGS + (4*22)]
                   5755:        std     %f24, [%o0 + FS_REGS + (4*24)]
                   5756:        std     %f26, [%o0 + FS_REGS + (4*26)]
                   5757:        std     %f28, [%o0 + FS_REGS + (4*28)]
                   5758:        retl
                   5759:         std    %f30, [%o0 + FS_REGS + (4*30)]
                   5760:
                   5761: /*
                   5762:  * Store the (now known nonempty) FP queue.
                   5763:  * We have to reread the fsr each time in order to get the new QNE bit.
                   5764:  */
                   5765: Lfp_storeq:
                   5766:        add     %o0, FS_QUEUE, %o1      ! q = &f->fs_queue[0];
                   5767: 1:
                   5768:        std     %fq, [%o1 + %o3]        ! q[qsize++] = fsr_qfront();
                   5769:        st      %fsr, [%o0 + FS_FSR]    ! reread fsr
                   5770:        ld      [%o0 + FS_FSR], %o4     ! if fsr & QNE, loop
                   5771:        btst    %o5, %o4
                   5772:        bnz     1b
                   5773:         inc    8, %o3
                   5774:        b       Lfp_finish              ! set qsize and finish storing fregs
                   5775:         srl    %o3, 3, %o3             ! (but first fix qsize)
                   5776:
                   5777: /*
                   5778:  * The fsr store trapped.  Do it again; this time it will not trap.
                   5779:  * We could just have the trap handler return to the `st %fsr', but
                   5780:  * if for some reason it *does* trap, that would lock us into a tight
                   5781:  * loop.  This way we panic instead.  Whoopee.
                   5782:  */
                   5783: savefpcont:
                   5784:        b       special_fp_store + 4    ! continue
                   5785:         st     %fsr, [%o0 + FS_FSR]    ! but first finish the %fsr store
                   5786:
                   5787: /*
                   5788:  * Load FPU state.
                   5789:  */
                   5790: ENTRY(loadfpstate)
                   5791:        rd      %psr, %o1               ! enable FP before we begin
                   5792:        set     PSR_EF, %o2
                   5793:        or      %o1, %o2, %o1
                   5794:        wr      %o1, 0, %psr
                   5795:        nop; nop; nop                   ! paranoia
                   5796:        ldd     [%o0 + FS_REGS + (4*0)], %f0
                   5797:        ldd     [%o0 + FS_REGS + (4*2)], %f2
                   5798:        ldd     [%o0 + FS_REGS + (4*4)], %f4
                   5799:        ldd     [%o0 + FS_REGS + (4*6)], %f6
                   5800:        ldd     [%o0 + FS_REGS + (4*8)], %f8
                   5801:        ldd     [%o0 + FS_REGS + (4*10)], %f10
                   5802:        ldd     [%o0 + FS_REGS + (4*12)], %f12
                   5803:        ldd     [%o0 + FS_REGS + (4*14)], %f14
                   5804:        ldd     [%o0 + FS_REGS + (4*16)], %f16
                   5805:        ldd     [%o0 + FS_REGS + (4*18)], %f18
                   5806:        ldd     [%o0 + FS_REGS + (4*20)], %f20
                   5807:        ldd     [%o0 + FS_REGS + (4*22)], %f22
                   5808:        ldd     [%o0 + FS_REGS + (4*24)], %f24
                   5809:        ldd     [%o0 + FS_REGS + (4*26)], %f26
                   5810:        ldd     [%o0 + FS_REGS + (4*28)], %f28
                   5811:        ldd     [%o0 + FS_REGS + (4*30)], %f30
                   5812:        retl
                   5813:         ld     [%o0 + FS_FSR], %fsr    ! setfsr(f->fs_fsr);
                   5814:
                   5815: /*
                   5816:  * ienab_bis(bis) int bis;
                   5817:  * ienab_bic(bic) int bic;
                   5818:  *
                   5819:  * Set and clear bits in the interrupt register.
1.52      pk       5820:  */
                   5821:
                   5822: #if defined(SUN4M) && (defined(SUN4) || defined(SUN4C))
1.62      pk       5823: ENTRY(ienab_bis)
                   5824: NOP_ON_4M_13:
1.111     pk       5825:        b,a     _C_LABEL(ienab_bis_4_4c)
                   5826:        b,a     _C_LABEL(ienab_bis_4m)
1.62      pk       5827:
                   5828: ENTRY(ienab_bic)
                   5829: NOP_ON_4M_14:
1.111     pk       5830:        b,a     _C_LABEL(ienab_bic_4_4c)
                   5831:        b,a     _C_LABEL(ienab_bic_4m)
1.62      pk       5832: #endif
1.52      pk       5833:
                   5834: #if defined(SUN4) || defined(SUN4C)
                   5835: /*
1.1       deraadt  5836:  * Since there are no read-modify-write instructions for this,
                   5837:  * and one of the interrupts is nonmaskable, we must disable traps.
                   5838:  */
1.52      pk       5839: #if defined(SUN4M)
1.68      mycroft  5840: ENTRY(ienab_bis_4_4c)
1.52      pk       5841: #else
1.1       deraadt  5842: ENTRY(ienab_bis)
1.52      pk       5843: #endif
1.1       deraadt  5844:        ! %o0 = bits to set
                   5845:        rd      %psr, %o2
                   5846:        wr      %o2, PSR_ET, %psr       ! disable traps
                   5847:        nop; nop                        ! 3-instr delay until ET turns off
1.62      pk       5848:        sethi   %hi(INTRREG_VA), %o3
                   5849:        ldub    [%o3 + %lo(INTRREG_VA)], %o4
                   5850:        or      %o4, %o0, %o4           ! *INTRREG_VA |= bis;
                   5851:        stb     %o4, [%o3 + %lo(INTRREG_VA)]
1.1       deraadt  5852:        wr      %o2, 0, %psr            ! reenable traps
                   5853:        nop
                   5854:        retl
                   5855:         nop
                   5856:
1.52      pk       5857: #if defined(SUN4M)
1.68      mycroft  5858: ENTRY(ienab_bic_4_4c)
1.52      pk       5859: #else
1.1       deraadt  5860: ENTRY(ienab_bic)
1.52      pk       5861: #endif
1.1       deraadt  5862:        ! %o0 = bits to clear
                   5863:        rd      %psr, %o2
                   5864:        wr      %o2, PSR_ET, %psr       ! disable traps
                   5865:        nop; nop
1.62      pk       5866:        sethi   %hi(INTRREG_VA), %o3
                   5867:        ldub    [%o3 + %lo(INTRREG_VA)], %o4
                   5868:        andn    %o4, %o0, %o4           ! *INTRREG_VA &=~ bic;
                   5869:        stb     %o4, [%o3 + %lo(INTRREG_VA)]
1.1       deraadt  5870:        wr      %o2, 0, %psr            ! reenable traps
                   5871:        nop
                   5872:        retl
                   5873:         nop
1.52      pk       5874: #endif
                   5875:
                   5876: #if defined(SUN4M)
                   5877: /*
                   5878:  * sun4m has separate registers for clearing/setting the interrupt mask.
                   5879:  */
                   5880: #if defined(SUN4) || defined(SUN4C)
                   5881: ENTRY(ienab_bis_4m)
                   5882: #else
                   5883: ENTRY(ienab_bis)
                   5884: #endif
                   5885:        set     ICR_SI_SET, %o1
                   5886:        retl
                   5887:         st     %o0, [%o1]
                   5888:
                   5889: #if defined(SUN4) || defined(SUN4C)
                   5890: ENTRY(ienab_bic_4m)
                   5891: #else
                   5892: ENTRY(ienab_bic)
                   5893: #endif
                   5894:        set     ICR_SI_CLR, %o1
                   5895:        retl
                   5896:         st     %o0, [%o1]
                   5897:
                   5898: /*
                   5899:  * raise(cpu, level)
                   5900:  */
                   5901: ENTRY(raise)
                   5902:        ! *(ICR_PI_SET + cpu*_MAXNBPG) = PINTR_SINTRLEV(level)
                   5903:        sethi   %hi(1 << 16), %o2
                   5904:        sll     %o2, %o1, %o2
                   5905:        set     ICR_PI_SET, %o1
                   5906:        set     _MAXNBPG, %o3
                   5907: 1:
                   5908:        subcc   %o0, 1, %o0
                   5909:        bpos,a  1b
                   5910:         add    %o1, %o3, %o1
                   5911:        retl
                   5912:         st     %o2, [%o1]
                   5913:
1.62      pk       5914:
                   5915: /*
1.94      pk       5916:  * Read Synchronous Fault Status registers.
                   5917:  * On entry: %l1 == PC, %l3 == fault type, %l4 == storage, %l7 == return address
                   5918:  * Only use %l5 and %l6.
                   5919:  * Note: not C callable.
                   5920:  */
1.111     pk       5921: _ENTRY(_C_LABEL(srmmu_get_syncflt))
                   5922: _ENTRY(_C_LABEL(hypersparc_get_syncflt))
1.94      pk       5923:        set     SRMMU_SFAR, %l5
                   5924:        lda     [%l5] ASI_SRMMU, %l5    ! sync virt addr; must be read first
                   5925:        st      %l5, [%l4 + 4]          ! => dump.sfva
                   5926:        set     SRMMU_SFSR, %l5
                   5927:        lda     [%l5] ASI_SRMMU, %l5    ! get sync fault status register
                   5928:        jmp     %l7 + 8                 ! return to caller
                   5929:         st     %l5, [%l4]              ! => dump.sfsr
                   5930:
1.111     pk       5931: _ENTRY(_C_LABEL(viking_get_syncflt))
                   5932: _ENTRY(_C_LABEL(ms1_get_syncflt))
                   5933: _ENTRY(_C_LABEL(swift_get_syncflt))
                   5934: _ENTRY(_C_LABEL(turbosparc_get_syncflt))
                   5935: _ENTRY(_C_LABEL(cypress_get_syncflt))
1.62      pk       5936:        cmp     %l3, T_TEXTFAULT
                   5937:        be,a    1f
1.94      pk       5938:         mov    %l1, %l5                ! use PC if type == T_TEXTFAULT
1.62      pk       5939:
1.94      pk       5940:        set     SRMMU_SFAR, %l5
                   5941:        lda     [%l5] ASI_SRMMU, %l5    ! sync virt addr; must be read first
1.62      pk       5942: 1:
1.94      pk       5943:        st      %l5, [%l4 + 4]          ! => dump.sfva
1.62      pk       5944:
1.94      pk       5945:        set     SRMMU_SFSR, %l5
                   5946:        lda     [%l5] ASI_SRMMU, %l5    ! get sync fault status register
                   5947:        jmp     %l7 + 8                 ! return to caller
                   5948:         st     %l5, [%l4]              ! => dump.sfsr
1.62      pk       5949:
                   5950:
1.94      pk       5951: /*
                   5952:  * Read Asynchronous Fault Status registers.
                   5953:  * On entry: %o0 == &afsr, %o1 == &afar
                   5954:  * Return 0 if async register are present.
                   5955:  */
1.111     pk       5956: _ENTRY(_C_LABEL(srmmu_get_asyncflt))
1.94      pk       5957:        set     SRMMU_AFAR, %o4
                   5958:        lda     [%o4] ASI_SRMMU, %o4    ! get async fault address
                   5959:        set     SRMMU_AFSR, %o3 !
                   5960:        st      %o4, [%o1]
                   5961:        lda     [%o3] ASI_SRMMU, %o3    ! get async fault status
                   5962:        st      %o3, [%o0]
                   5963:        retl
                   5964:         clr    %o0                     ! return value
1.62      pk       5965:
1.111     pk       5966: _ENTRY(_C_LABEL(cypress_get_asyncflt))
                   5967: _ENTRY(_C_LABEL(hypersparc_get_asyncflt))
1.94      pk       5968:        set     SRMMU_AFSR, %o3         ! must read status before fault on HS
                   5969:        lda     [%o3] ASI_SRMMU, %o3    ! get async fault status
                   5970:        st      %o3, [%o0]
                   5971:        btst    AFSR_AFO, %o3           ! and only read fault address
                   5972:        bz      1f                      ! if valid.
                   5973:        set     SRMMU_AFAR, %o4
                   5974:        lda     [%o4] ASI_SRMMU, %o4    ! get async fault address
                   5975:        clr     %o0                     ! return value
1.62      pk       5976:        retl
1.94      pk       5977:         st     %o4, [%o1]
1.62      pk       5978: 1:
                   5979:        retl
1.94      pk       5980:         clr    %o0                     ! return value
1.62      pk       5981:
1.111     pk       5982: _ENTRY(_C_LABEL(no_asyncflt_regs))
1.62      pk       5983:        retl
1.94      pk       5984:         mov    1, %o0                  ! return value
1.86      pk       5985:
1.111     pk       5986: _ENTRY(_C_LABEL(hypersparc_pure_vcache_flush))
1.86      pk       5987:        /*
                   5988:         * Flush entire on-chip instruction cache, which is
                   5989:         * a pure vitually-indexed/virtually-tagged cache.
                   5990:         */
                   5991:        retl
                   5992:         sta    %g0, [%g0] ASI_HICACHECLR
1.62      pk       5993:
1.52      pk       5994: #endif /* SUN4M */
1.1       deraadt  5995:
                   5996: /*
1.29      deraadt  5997:  * void lo_microtime(struct timeval *tv)
1.1       deraadt  5998:  *
                   5999:  * LBL's sparc bsd 'microtime': We don't need to spl (so this routine
                   6000:  * can be a leaf routine) and we don't keep a 'last' timeval (there
                   6001:  * can't be two calls to this routine in a microsecond).  This seems to
                   6002:  * be about 20 times faster than the Sun code on an SS-2. - vj
                   6003:  *
                   6004:  * Read time values from slowest-changing to fastest-changing,
                   6005:  * then re-read out to slowest.  If the values read before
                   6006:  * the innermost match those read after, the innermost value
                   6007:  * is consistent with the outer values.  If not, it may not
                   6008:  * be and we must retry.  Typically this loop runs only once;
                   6009:  * occasionally it runs twice, and only rarely does it run longer.
                   6010:  */
1.30      deraadt  6011: #if defined(SUN4)
1.29      deraadt  6012: ENTRY(lo_microtime)
1.30      deraadt  6013: #else
                   6014: ENTRY(microtime)
                   6015: #endif
1.111     pk       6016:        sethi   %hi(_C_LABEL(time)), %g2
1.68      mycroft  6017:
                   6018: #if defined(SUN4M) && !(defined(SUN4C) || defined(SUN4))
                   6019:        sethi   %hi(TIMERREG_VA+4), %g3
                   6020:        or      %g3, %lo(TIMERREG_VA+4), %g3
                   6021: #elif (defined(SUN4C) || defined(SUN4)) && !defined(SUN4M)
                   6022:        sethi   %hi(TIMERREG_VA), %g3
                   6023:        or      %g3, %lo(TIMERREG_VA), %g3
                   6024: #else
1.1       deraadt  6025:        sethi   %hi(TIMERREG_VA), %g3
1.62      pk       6026:        or      %g3, %lo(TIMERREG_VA), %g3
1.68      mycroft  6027: NOP_ON_4_4C_1:
1.62      pk       6028:         add    %g3, 4, %g3
1.68      mycroft  6029: #endif
1.62      pk       6030:
1.69      mycroft  6031: 2:
1.111     pk       6032:        ldd     [%g2+%lo(_C_LABEL(time))], %o2  ! time.tv_sec & time.tv_usec
1.62      pk       6033:        ld      [%g3], %o4                      ! usec counter
1.111     pk       6034:        ldd     [%g2+%lo(_C_LABEL(time))], %g4  ! see if time values changed
1.1       deraadt  6035:        cmp     %g4, %o2
1.52      pk       6036:        bne     2b                              ! if time.tv_sec changed
1.1       deraadt  6037:         cmp    %g5, %o3
1.52      pk       6038:        bne     2b                              ! if time.tv_usec changed
1.1       deraadt  6039:         tst    %o4
                   6040:
1.52      pk       6041:        bpos    3f                              ! reached limit?
1.1       deraadt  6042:         srl    %o4, TMR_SHIFT, %o4             ! convert counter to usec
1.111     pk       6043:        sethi   %hi(_C_LABEL(tick)), %g4        ! bump usec by 1 tick
                   6044:        ld      [%g4+%lo(_C_LABEL(tick))], %o1
1.1       deraadt  6045:        set     TMR_MASK, %g5
                   6046:        add     %o1, %o3, %o3
                   6047:        and     %o4, %g5, %o4
1.52      pk       6048: 3:
1.1       deraadt  6049:        add     %o4, %o3, %o3
                   6050:        set     1000000, %g5                    ! normalize usec value
                   6051:        cmp     %o3, %g5
1.52      pk       6052:        bl,a    4f
1.1       deraadt  6053:         st     %o2, [%o0]                      ! (should be able to std here)
                   6054:        add     %o2, 1, %o2                     ! overflow
                   6055:        sub     %o3, %g5, %o3
                   6056:        st      %o2, [%o0]                      ! (should be able to std here)
1.52      pk       6057: 4:
1.1       deraadt  6058:        retl
                   6059:         st     %o3, [%o0+4]
                   6060:
1.54      pk       6061: /*
                   6062:  * delay function
                   6063:  *
                   6064:  * void delay(N)  -- delay N microseconds
                   6065:  *
                   6066:  * Register usage: %o0 = "N" number of usecs to go (counts down to zero)
                   6067:  *                %o1 = "timerblurb" (stays constant)
                   6068:  *                %o2 = counter for 1 usec (counts down from %o1 to zero)
                   6069:  *
                   6070:  */
                   6071:
                   6072: ENTRY(delay)                   ! %o0 = n
1.57      pk       6073:        subcc   %o0, %g0, %g0
                   6074:        be      2f
                   6075:
1.111     pk       6076:        sethi   %hi(_C_LABEL(timerblurb)), %o1
                   6077:        ld      [%o1 + %lo(_C_LABEL(timerblurb))], %o1  ! %o1 = timerblurb
1.53      pk       6078:
1.57      pk       6079:         addcc  %o1, %g0, %o2           ! %o2 = cntr (start @ %o1), clear CCs
1.54      pk       6080:                                        ! first time through only
                   6081:
                   6082:                                        ! delay 1 usec
                   6083: 1:     bne     1b                      ! come back here if not done
                   6084:         subcc  %o2, 1, %o2             ! %o2 = %o2 - 1 [delay slot]
1.53      pk       6085:
1.54      pk       6086:        subcc   %o0, 1, %o0             ! %o0 = %o0 - 1
                   6087:        bne     1b                      ! done yet?
                   6088:         addcc  %o1, %g0, %o2           ! reinit %o2 and CCs  [delay slot]
                   6089:                                        ! harmless if not branching
1.57      pk       6090: 2:
1.54      pk       6091:        retl                            ! return
                   6092:         nop                            ! [delay slot]
1.53      pk       6093:
1.60      pk       6094: #if defined(KGDB) || defined(DDB) || defined(DIAGNOSTIC)
1.1       deraadt  6095: /*
                   6096:  * Write all windows (user or otherwise), except the current one.
                   6097:  *
                   6098:  * THIS COULD BE DONE IN USER CODE
                   6099:  */
                   6100: ENTRY(write_all_windows)
                   6101:        /*
                   6102:         * g2 = g1 = nwindows - 1;
                   6103:         * while (--g1 > 0) save();
                   6104:         * while (--g2 > 0) restore();
                   6105:         */
1.111     pk       6106:        sethi   %hi(_C_LABEL(nwindows)), %g1
                   6107:        ld      [%g1 + %lo(_C_LABEL(nwindows))], %g1
1.1       deraadt  6108:        dec     %g1
                   6109:        mov     %g1, %g2
                   6110:
                   6111: 1:     deccc   %g1
                   6112:        bg,a    1b
                   6113:         save   %sp, -64, %sp
                   6114:
                   6115: 2:     deccc   %g2
                   6116:        bg,a    2b
                   6117:         restore
                   6118:
                   6119:        retl
                   6120:        nop
                   6121: #endif /* KGDB */
                   6122:
1.8       pk       6123: ENTRY(setjmp)
                   6124:        std     %sp, [%o0+0]    ! stack pointer & return pc
                   6125:        st      %fp, [%o0+8]    ! frame pointer
                   6126:        retl
                   6127:         clr    %o0
                   6128:
                   6129: Lpanic_ljmp:
                   6130:        .asciz  "longjmp botch"
1.52      pk       6131:        _ALIGN
1.8       pk       6132:
                   6133: ENTRY(longjmp)
                   6134:        addcc   %o1, %g0, %g6   ! compute v ? v : 1 in a global register
                   6135:        be,a    0f
                   6136:         mov    1, %g6
                   6137: 0:
                   6138:        mov     %o0, %g1        ! save a in another global register
                   6139:        ld      [%g1+8], %g7    /* get caller's frame */
                   6140: 1:
                   6141:        cmp     %fp, %g7        ! compare against desired frame
                   6142:        bl,a    1b              ! if below,
                   6143:         restore                !    pop frame and loop
                   6144:        be,a    2f              ! if there,
                   6145:         ldd    [%g1+0], %o2    !    fetch return %sp and pc, and get out
                   6146:
                   6147: Llongjmpbotch:
                   6148:                                ! otherwise, went too far; bomb out
                   6149:        save    %sp, -CCFSZ, %sp        /* preserve current window */
                   6150:        sethi   %hi(Lpanic_ljmp), %o0
1.111     pk       6151:        call    _C_LABEL(panic)
1.8       pk       6152:        or %o0, %lo(Lpanic_ljmp), %o0;
                   6153:        unimp   0
                   6154:
                   6155: 2:
                   6156:        cmp     %o2, %sp        ! %sp must not decrease
                   6157:        bge,a   3f
                   6158:         mov    %o2, %sp        ! it is OK, put it in place
                   6159:        b,a     Llongjmpbotch
1.52      pk       6160: 3:
1.8       pk       6161:        jmp     %o3 + 8         ! success, return %g6
                   6162:         mov    %g6, %o0
                   6163:
1.1       deraadt  6164:        .data
1.8       pk       6165: #ifdef DDB
1.111     pk       6166:        .globl  _C_LABEL(esym)
                   6167: _C_LABEL(esym):
1.117     christos 6168:        .word   0
1.123     pk       6169: #endif
1.117     christos 6170:        .globl  _C_LABEL(bootinfo)
                   6171: _C_LABEL(bootinfo):
1.8       pk       6172:        .word   0
1.1       deraadt  6173:
1.111     pk       6174:        .globl  _C_LABEL(proc0paddr)
                   6175: _C_LABEL(proc0paddr):
                   6176:        .word   _C_LABEL(u0)    ! KVA of proc0 uarea
1.1       deraadt  6177:
                   6178: /* interrupt counters  XXX THESE BELONG ELSEWHERE (if anywhere) */
1.111     pk       6179:        .globl  _C_LABEL(intrcnt), _C_LABEL(eintrcnt)
                   6180:        .globl  _C_LABEL(intrnames), _C_LABEL(eintrnames)
                   6181: _C_LABEL(intrnames):
1.1       deraadt  6182:        .asciz  "spur"
                   6183:        .asciz  "lev1"
                   6184:        .asciz  "lev2"
                   6185:        .asciz  "lev3"
                   6186:        .asciz  "lev4"
                   6187:        .asciz  "lev5"
                   6188:        .asciz  "lev6"
                   6189:        .asciz  "lev7"
                   6190:        .asciz  "lev8"
                   6191:        .asciz  "lev9"
                   6192:        .asciz  "clock"
                   6193:        .asciz  "lev11"
                   6194:        .asciz  "lev12"
                   6195:        .asciz  "lev13"
                   6196:        .asciz  "prof"
1.111     pk       6197: _C_LABEL(eintrnames):
1.52      pk       6198:        _ALIGN
1.111     pk       6199: _C_LABEL(intrcnt):
1.1       deraadt  6200:        .skip   4*15
1.111     pk       6201: _C_LABEL(eintrcnt):
1.1       deraadt  6202:
1.111     pk       6203:        .comm   _C_LABEL(nwindows), 4
                   6204:        .comm   _C_LABEL(romp), 4

CVSweb <webmaster@jp.NetBSD.org>