[BACK]Return to trap.c CVS log [TXT][DIR] Up to [local] / sys / arch / mvme68k / mvme68k

Annotation of sys/arch/mvme68k/mvme68k/trap.c, Revision 1.1.1.1

1.1       nbrk        1: /*     $OpenBSD: trap.c,v 1.67 2007/05/15 13:46:22 martin Exp $ */
                      2:
                      3: /*
                      4:  * Copyright (c) 1995 Theo de Raadt
                      5:  * Copyright (c) 1999 Steve Murphree, Jr. (68060 support)
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  *
                     16:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
                     17:  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
                     18:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     19:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
                     20:  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     21:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     22:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     23:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     24:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     25:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     26:  * SUCH DAMAGE.
                     27:  *
                     28:  * Copyright (c) 1988 University of Utah.
                     29:  * Copyright (c) 1982, 1986, 1990, 1993
                     30:  *     The Regents of the University of California.  All rights reserved.
                     31:  *
                     32:  * This code is derived from software contributed to Berkeley by
                     33:  * the Systems Programming Group of the University of Utah Computer
                     34:  * Science Department.
                     35:  *
                     36:  * Redistribution and use in source and binary forms, with or without
                     37:  * modification, are permitted provided that the following conditions
                     38:  * are met:
                     39:  * 1. Redistributions of source code must retain the above copyright
                     40:  *    notice, this list of conditions and the following disclaimer.
                     41:  * 2. Redistributions in binary form must reproduce the above copyright
                     42:  *    notice, this list of conditions and the following disclaimer in the
                     43:  *    documentation and/or other materials provided with the distribution.
                     44:  * 3. Neither the name of the University nor the names of its contributors
                     45:  *    may be used to endorse or promote products derived from this software
                     46:  *    without specific prior written permission.
                     47:  *
                     48:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     49:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     50:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     51:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     52:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     53:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     54:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     55:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     56:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     57:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     58:  * SUCH DAMAGE.
                     59:  *
                     60:  * from: Utah $Hdr: trap.c 1.37 92/12/20$
                     61:  *
                     62:  *     @(#)trap.c      8.5 (Berkeley) 1/4/94
                     63:  */
                     64:
                     65: #include <sys/param.h>
                     66: #include <sys/systm.h>
                     67: #include <sys/proc.h>
                     68: #include <sys/acct.h>
                     69: #include <sys/kernel.h>
                     70: #include <sys/signalvar.h>
                     71: #include <sys/resourcevar.h>
                     72: #include <sys/syscall.h>
                     73: #include <sys/syslog.h>
                     74: #include <sys/user.h>
                     75: #ifdef KTRACE
                     76: #include <sys/ktrace.h>
                     77: #endif
                     78:
                     79: #include <machine/db_machdep.h>
                     80: #include <machine/cpu.h>
                     81: #include <machine/psl.h>
                     82: #include <machine/reg.h>
                     83: #include <machine/trap.h>
                     84:
                     85: #ifdef COMPAT_SUNOS
                     86: #include <compat/sunos/sunos_syscall.h>
                     87: extern struct emul emul_sunos;
                     88: #endif
                     89:
                     90: #include "systrace.h"
                     91: #include <dev/systrace.h>
                     92:
                     93: #include <uvm/uvm_extern.h>
                     94: #include <uvm/uvm_pmap.h>
                     95:
                     96: #ifdef COMPAT_HPUX
                     97: #include <compat/hpux/hpux.h>
                     98: #endif
                     99:
                    100: int    astpending;
                    101: int    want_resched;
                    102:
                    103: char  *trap_type[] = {
                    104:        "Bus error",
                    105:        "Address error",
                    106:        "Illegal instruction",
                    107:        "Zero divide",
                    108:        "CHK instruction",
                    109:        "TRAPV instruction",
                    110:        "Privilege violation",
                    111:        "Trace trap",
                    112:        "MMU fault",
                    113:        "SSIR trap",
                    114:        "Format error",
                    115:        "68881 exception",
                    116:        "Coprocessor violation",
                    117:        "Async system trap"
                    118: };
                    119: int   trap_types = sizeof trap_type / sizeof trap_type[0];
                    120:
                    121: /*
                    122:  * Size of various exception stack frames (minus the standard 8 bytes)
                    123:  */
                    124: short exframesize[] = {
                    125:        FMT0SIZE,       /* type 0 - normal (68020/030/040/060) */
                    126:        FMT1SIZE,       /* type 1 - throwaway (68020/030/040) */
                    127:        FMT2SIZE,       /* type 2 - normal 6-word (68020/030/040/060) */
                    128:        FMT3SIZE,       /* type 3 - FP post-instruction (68040/060) */
                    129:        FMT4SIZE,       /* type 4 - access error/fp disabled (68060) */
                    130:        -1, -1,         /* type 5-6 - undefined */
                    131:        FMT7SIZE,       /* type 7 - access error (68040) */
                    132:        58,                     /* type 8 - bus fault (68010) */
                    133:        FMT9SIZE,       /* type 9 - coprocessor mid-instruction (68020/030) */
                    134:        FMTASIZE,       /* type A - short bus fault (68020/030) */
                    135:        FMTBSIZE,       /* type B - long bus fault (68020/030) */
                    136:        -1, -1, -1, -1  /* type C-F - undefined */
                    137: };
                    138:
                    139:
                    140: #if defined(M68040) || defined(M68060)
                    141: #define KDFAULT(c)    (mmutype == MMU_68060 ? ((c) & FSLW_TM_SV) : \
                    142:              mmutype == MMU_68040 ? ((c) & SSW4_TMMASK) == SSW4_TMKD : \
                    143:                            ((c) & (SSW_DF|FC_SUPERD)) == (SSW_DF|FC_SUPERD))
                    144: #define WRFAULT(c)    (mmutype == MMU_68060 ? ((c) & FSLW_RW_W) : \
                    145:              mmutype == MMU_68040 ? ((c) & SSW4_RW) == 0 : \
                    146:                            ((c) & (SSW_DF|SSW_RW)) == SSW_DF)
                    147: #else
                    148: #define KDFAULT(c)     (((c) & (SSW_DF|SSW_FCMASK)) == (SSW_DF|FC_SUPERD))
                    149: #define WRFAULT(c)     (((c) & (SSW_DF|SSW_RW)) == SSW_DF)
                    150: #endif
                    151:
                    152: #ifdef DEBUG
                    153: int mmudebug = 0;
                    154: int mmupid = -1;
                    155: #define MDB_FOLLOW     1
                    156: #define MDB_WBFOLLOW   2
                    157: #define MDB_WBFAILED   4
                    158: #define MDB_ISPID(p)   (p) == mmupid
                    159: #endif
                    160:
                    161: #define NSIR   8
                    162: void (*sir_routines[NSIR])(void *);
                    163: void *sir_args[NSIR];
                    164: u_char next_sir;
                    165:
                    166: void trap(int, u_int, u_int, struct frame);
                    167: void syscall(register_t, struct frame);
                    168: void init_intrs(void);
                    169: void hardintr(int, int, void *);
                    170: int writeback(struct frame *);
                    171: void wb_userret(struct proc *, struct frame *);
                    172:
                    173: /*
                    174:  * trap and syscall both need the following work done before returning
                    175:  * to user mode.
                    176:  */
                    177: void
                    178: userret(struct proc *p)
                    179: {
                    180:        int sig;
                    181:
                    182:        /* take pending signals */
                    183:        while ((sig = CURSIG(p)) != 0)
                    184:                postsig(sig);
                    185:        curcpu()->ci_schedstate.spc_curpriority = p->p_priority = p->p_usrpri;
                    186: }
                    187:
                    188: #ifdef M68040
                    189: /*
                    190:  * Same as above, but also handles writeback completion on 68040.
                    191:  */
                    192: void
                    193: wb_userret(struct proc *p, struct frame *fp)
                    194: {
                    195:        int sig;
                    196:        union sigval sv;
                    197:
                    198:        /* take pending signals */
                    199:        while ((sig = CURSIG(p)) != 0)
                    200:                postsig(sig);
                    201:        p->p_priority = p->p_usrpri;
                    202:
                    203:        /*
                    204:         * Deal with user mode writebacks (from trap, or from sigreturn).
                    205:         * If any writeback fails, go back and attempt signal delivery.
                    206:         * unless we have already been here and attempted the writeback
                    207:         * (e.g. bad address with user ignoring SIGSEGV).  In that case
                    208:         * we just return to the user without successfully completing
                    209:         * the writebacks.  Maybe we should just drop the sucker?
                    210:         */
                    211:        if (mmutype == MMU_68040 && fp->f_format == FMT7) {
                    212:                if ((sig = writeback(fp)) != 0) {
                    213:                        sv.sival_int = fp->f_fmt7.f_fa;
                    214:                        trapsignal(p, sig, T_MMUFLT, SEGV_MAPERR, sv);
                    215:
                    216:                        while ((sig = CURSIG(p)) != 0)
                    217:                                postsig(sig);
                    218:                        p->p_priority = p->p_usrpri;
                    219:                }
                    220:        }
                    221:        curcpu()->ci_schedstate.spc_curpriority = p->p_priority;
                    222: }
                    223: #endif
                    224:
                    225: /*
                    226:  * Trap is called from locore to handle most types of processor traps,
                    227:  * including events such as simulated software interrupts/AST's.
                    228:  * System calls are broken out for efficiency.
                    229:  */
                    230: /*ARGSUSED*/
                    231: void
                    232: trap(type, code, v, frame)
                    233:        int type;
                    234:        u_int code;
                    235:        register u_int v;
                    236:        struct frame frame;
                    237: {
                    238:        register struct proc *p;
                    239:        register int i;
                    240:        u_int ucode;
                    241:        int typ = 0, bit;
                    242: #ifdef COMPAT_HPUX
                    243:        extern struct emul emul_hpux;
                    244: #endif
                    245: #ifdef COMPAT_SUNOS
                    246:        extern struct emul emul_sunos;
                    247: #endif
                    248:        register union sigval sv;
                    249:
                    250:        uvmexp.traps++;
                    251:        p = curproc;
                    252:        ucode = 0;
                    253:        if (USERMODE(frame.f_sr)) {
                    254:                type |= T_USER;
                    255:                p->p_md.md_regs = frame.f_regs;
                    256:        }
                    257:        switch (type) {
                    258:        default:
                    259: dopanic:
                    260:                printf("trap type %d, code = %x, v = %x\n", type, code, v);
                    261: #ifdef DDB
                    262:                if (kdb_trap(type, (db_regs_t *)&frame))
                    263:                        return;
                    264: #endif
                    265:                regdump(&(frame.F_t), 128);
                    266:                type &= ~T_USER;
                    267:                if ((u_int)type < trap_types)
                    268:                        panic(trap_type[type]);
                    269:                panic("trap");
                    270:
                    271:        case T_BUSERR:          /* kernel bus error */
                    272:                if (!p || !p->p_addr->u_pcb.pcb_onfault)
                    273:                        goto dopanic;
                    274: copyfault:
                    275:                /*
                    276:                 * If we have arranged to catch this fault in any of the
                    277:                 * copy to/from user space routines, set PC to return to
                    278:                 * indicated location and set flag informing buserror code
                    279:                 * that it may need to clean up stack frame.
                    280:                 */
                    281:                frame.f_stackadj = exframesize[frame.f_format];
                    282:                frame.f_format = frame.f_vector = 0;
                    283:                frame.f_pc = (int) p->p_addr->u_pcb.pcb_onfault;
                    284:                return;
                    285:
                    286:        case T_BUSERR|T_USER:   /* bus error */
                    287:                typ = BUS_OBJERR;
                    288:                ucode = code & ~T_USER;
                    289:                i = SIGBUS;
                    290:                break;
                    291:        case T_ADDRERR|T_USER:  /* address error */
                    292:                typ = BUS_ADRALN;
                    293:                ucode = code & ~T_USER;
                    294:                i = SIGBUS;
                    295:                break;
                    296:
                    297:        case T_COPERR:          /* kernel coprocessor violation */
                    298:        case T_FMTERR|T_USER:   /* do all RTE errors come in as T_USER? */
                    299:        case T_FMTERR:          /* ...just in case... */
                    300:                /*
                    301:                 * The user has most likely trashed the RTE or FP state info
                    302:                 * in the stack frame of a signal handler.
                    303:                 */
                    304:                printf("pid %d: kernel %s exception\n", p->p_pid,
                    305:                                 type==T_COPERR ? "coprocessor" : "format");
                    306:                type |= T_USER;
                    307:                p->p_sigacts->ps_sigact[SIGILL] = SIG_DFL;
                    308:                i = sigmask(SIGILL);
                    309:                p->p_sigignore &= ~i;
                    310:                p->p_sigcatch &= ~i;
                    311:                p->p_sigmask &= ~i;
                    312:                i = SIGILL;
                    313:                ucode = frame.f_format; /* XXX was ILL_RESAD_FAULT */
                    314:                typ = ILL_COPROC;
                    315:                v = frame.f_pc;
                    316:                break;
                    317:
                    318:        case T_COPERR|T_USER:   /* user coprocessor violation */
                    319:                /* What is a proper response here? */
                    320:                typ = FPE_FLTINV;
                    321:                ucode = 0;
                    322:                i = SIGFPE;
                    323:                break;
                    324:
                    325:        case T_FPERR|T_USER:    /* 68881 exceptions */
                    326:                typ = FPE_FLTRES;
                    327:                ucode = code;
                    328:                i = SIGFPE;
                    329:                v = frame.f_pc;
                    330:                break;
                    331:
                    332: #if defined(M68040) || defined(M68060)
                    333:        case T_FPEMULI|T_USER:  /* unimplemented FP instruction */
                    334:        case T_FPEMULD|T_USER:  /* unimplemented FP data type */
                    335:                /* XXX need to FSAVE */
                    336:                printf("pid %d(%s): unimplemented FP %s at %x (EA %x)\n",
                    337:                                 p->p_pid, p->p_comm,
                    338:                                 frame.f_format == 2 ? "instruction" : "data type",
                    339:                                 frame.f_pc, frame.f_fmt2.f_iaddr);
                    340:                /* XXX need to FRESTORE */
                    341:                typ = FPE_FLTINV;
                    342:                i = SIGFPE;
                    343:                v = frame.f_pc;
                    344:                break;
                    345: #endif
                    346:
                    347:        case T_ILLINST|T_USER:  /* illegal instruction fault */
                    348: #ifdef COMPAT_HPUX
                    349:                if (p->p_emul == &emul_hpux) {
                    350:                        typ = 0;
                    351:                        ucode = HPUX_ILL_ILLINST_TRAP;
                    352:                        i = SIGILL;
                    353:                        break;
                    354:                }
                    355: #endif
                    356:                ucode = frame.f_format; /* XXX was ILL_PRIVIN_FAULT */
                    357:                typ = ILL_ILLOPC;
                    358:                i = SIGILL;
                    359:                v = frame.f_pc;
                    360:                break;
                    361:
                    362:        case T_PRIVINST|T_USER: /* privileged instruction fault */
                    363: #ifdef COMPAT_HPUX
                    364:                if (p->p_emul == &emul_hpux)
                    365:                        ucode = HPUX_ILL_PRIV_TRAP;
                    366:                else
                    367: #endif
                    368:                ucode   = frame.f_format;       /* XXX was ILL_PRIVIN_FAULT */
                    369:                typ = ILL_PRVOPC;
                    370:                i = SIGILL;
                    371:                v = frame.f_pc;
                    372:                break;
                    373:
                    374:        case T_ZERODIV|T_USER:  /* Divide by zero */
                    375: #ifdef COMPAT_HPUX
                    376:                if (p->p_emul == &emul_hpux)
                    377:                        ucode = HPUX_FPE_INTDIV_TRAP;
                    378:                else
                    379: #endif
                    380:                ucode   = frame.f_format;       /* XXX was FPE_INTDIV_TRAP */
                    381:                typ = FPE_INTDIV;
                    382:                i = SIGFPE;
                    383:                v = frame.f_pc;
                    384:                break;
                    385:
                    386:        case T_CHKINST|T_USER:  /* CHK instruction trap */
                    387: #ifdef COMPAT_HPUX
                    388:                if (p->p_emul == &emul_hpux) {
                    389:                        /* handled differently under hp-ux */
                    390:                        i = SIGILL;
                    391:                        ucode = HPUX_ILL_CHK_TRAP;
                    392:                        break;
                    393:                }
                    394: #endif
                    395:                ucode = frame.f_format; /* XXX was FPE_SUBRNG_TRAP */
                    396:                typ = FPE_FLTSUB;
                    397:                i = SIGFPE;
                    398:                v = frame.f_pc;
                    399:                break;
                    400:
                    401:        case T_TRAPVINST|T_USER:        /* TRAPV instruction trap */
                    402: #ifdef COMPAT_HPUX
                    403:                if (p->p_emul == &emul_hpux) {
                    404:                        /* handled differently under hp-ux */
                    405:                        i = SIGILL;
                    406:                        ucode = HPUX_ILL_TRAPV_TRAP;
                    407:                        break;
                    408:                }
                    409: #endif
                    410:                ucode = frame.f_format; /* XXX was FPE_INTOVF_TRAP */
                    411:                typ = ILL_ILLTRP;
                    412:                i = SIGILL;
                    413:                v = frame.f_pc;
                    414:                break;
                    415:
                    416:                /*
                    417:                 * XXX: Trace traps are a nightmare.
                    418:                 *
                    419:                 *      HP-UX uses trap #1 for breakpoints,
                    420:                 *      OpenBSD/m68k uses trap #2,
                    421:                 *      SUN 3.x uses trap #15,
                    422:                 *      KGDB uses trap #15 (for kernel breakpoints; handled elsewhere).
                    423:                 *
                    424:                 * OpenBSD and HP-UX traps both get mapped by locore.s into
                    425:                 * T_TRACE.
                    426:                 * SUN 3.x traps get passed through as T_TRAP15 and are not really
                    427:                 * supported yet.
                    428:                 */
                    429:        case T_TRAP15:          /* kernel breakpoint */
                    430: #ifdef DEBUG
                    431:                printf("unexpected kernel trace trap, type = %d\n", type);
                    432:                printf("program counter = 0x%x\n", frame.f_pc);
                    433: #endif
                    434:                frame.f_sr &= ~PSL_T;
                    435:                return;
                    436:
                    437:        case T_TRACE|T_USER:    /* user trace trap */
                    438: #ifdef COMPAT_SUNOS
                    439:                /*
                    440:                 * SunOS uses Trap #2 for a "CPU cache flush"
                    441:                 * Just flush the on-chip caches and return.
                    442:                 */
                    443:                if (p->p_emul == &emul_sunos) {
                    444:                        ICIA();
                    445:                        DCIU();
                    446:                        return;
                    447:                }
                    448: #endif
                    449:                /* FALLTHROUGH */
                    450:
                    451:        case T_TRACE:
                    452:        case T_TRAP15|T_USER:   /* SUN user trace trap */
                    453:                frame.f_sr &= ~PSL_T;
                    454:                i = SIGTRAP;
                    455:                typ = TRAP_TRACE;
                    456:                break;
                    457:
                    458:        case T_ASTFLT:          /* system async trap, cannot happen */
                    459:                goto dopanic;
                    460:
                    461:        case T_ASTFLT|T_USER:   /* user async trap */
                    462:                astpending = 0;
                    463:                /*
                    464:                 * We check for software interrupts first.  This is because
                    465:                 * they are at a higher level than ASTs, and on a VAX would
                    466:                 * interrupt the AST.  We assume that if we are processing
                    467:                 * an AST that we must be at IPL0 so we don't bother to
                    468:                 * check.  Note that we ensure that we are at least at SIR
                    469:                 * IPL while processing the SIR.
                    470:                 */
                    471:                splsoft();
                    472:                /* FALLTHROUGH */
                    473:
                    474:        case T_SSIR:            /* software interrupt */
                    475:        case T_SSIR|T_USER:
                    476:                while ((bit = ffs(ssir))) {
                    477:                        --bit;
                    478:                        ssir &= ~(1 << bit);
                    479:                        uvmexp.softs++;
                    480:                        if (sir_routines[bit])
                    481:                                sir_routines[bit](sir_args[bit]);
                    482:                }
                    483:                /*
                    484:                 * If this was not an AST trap, we are all done.
                    485:                 */
                    486:                if (type != (T_ASTFLT|T_USER)) {
                    487:                        uvmexp.traps--;
                    488:                        return;
                    489:                }
                    490:                spl0();
                    491:                if (p->p_flag & P_OWEUPC) {
                    492:                        ADDUPROF(p);
                    493:                }
                    494:                if (type == (T_ASTFLT | T_USER) && want_resched) {
                    495:                        preempt(NULL);
                    496:                }
                    497:                goto out;
                    498:
                    499:        case T_MMUFLT:          /* kernel mode page fault */
                    500:        case T_MMUFLT|T_USER:   /* page fault */
                    501:            {
                    502:                vaddr_t va;
                    503:                struct vmspace *vm = NULL;
                    504:                struct vm_map *map;
                    505:                int rv;
                    506:                vm_prot_t ftype, vftype;
                    507:                extern struct vm_map *kernel_map;
                    508:
                    509:                /* vmspace only significant if T_USER */
                    510:                if (p)
                    511:                        vm = p->p_vmspace;
                    512:
                    513: #ifdef DEBUG
                    514:                if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
                    515:                        printf("trap: T_MMUFLT pid=%d, code=%x, v=%x, pc=%x, sr=%x\n",
                    516:                            p->p_pid, code, v, frame.f_pc, frame.f_sr);
                    517: #endif
                    518:                /*
                    519:                 * It is only a kernel address space fault iff:
                    520:                 *      1. (type & T_USER) == 0  and
                    521:                 *      2. pcb_onfault not set or
                    522:                 *      3. pcb_onfault set but supervisor space data fault
                    523:                 * The last can occur during an exec() copyin where the
                    524:                 * argument space is lazy-allocated.
                    525:                 */
                    526:                if (type == T_MMUFLT &&
                    527:                    ((p && !p->p_addr->u_pcb.pcb_onfault) || KDFAULT(code)))
                    528:                        map = kernel_map;
                    529:                else
                    530:                        map = vm ? &vm->vm_map : kernel_map;
                    531:                if (WRFAULT(code)) {
                    532:                        vftype = VM_PROT_WRITE;
                    533:                        ftype = VM_PROT_READ | VM_PROT_WRITE;
                    534:                } else
                    535:                        vftype = ftype = VM_PROT_READ;
                    536:                va = trunc_page((vaddr_t)v);
                    537:
                    538:                if (map == kernel_map && va == 0) {
                    539:                        printf("trap: bad kernel access at %x\n", v);
                    540:                        goto dopanic;
                    541:                }
                    542: #ifdef COMPAT_HPUX
                    543:                if (ISHPMMADDR(p, va)) {
                    544:                        vaddr_t bva;
                    545:
                    546:                        rv = pmap_mapmulti(map->pmap, va);
                    547:                        if (rv) {
                    548:                                bva = HPMMBASEADDR(va);
                    549:                                rv = uvm_fault(map, bva, 0, ftype);
                    550:                                if (rv == 0)
                    551:                                        (void)pmap_mapmulti(map->pmap, va);
                    552:                        }
                    553:                } else
                    554: #endif
                    555:                rv = uvm_fault(map, va, 0, ftype);
                    556: #ifdef DEBUG
                    557:                if (rv && MDB_ISPID(p->p_pid))
                    558:                        printf("uvm_fault(%x, %x, 0, %x) -> %x\n",
                    559:                                 map, va, ftype, rv);
                    560: #endif
                    561:                /*
                    562:                 * If this was a stack access we keep track of the maximum
                    563:                 * accessed stack size.  Also, if vm_fault gets a protection
                    564:                 * failure it is due to accessing the stack region outside
                    565:                 * the current limit and we need to reflect that as an access
                    566:                 * error.
                    567:                 */
                    568:                if ((vm != NULL && (caddr_t)va >= vm->vm_maxsaddr)
                    569:                    && map != kernel_map) {
                    570:                        if (rv == 0)
                    571:                                uvm_grow(p, va);
                    572:                        else if (rv == EACCES)
                    573:                                rv = EFAULT;
                    574:                }
                    575:                if (rv == 0) {
                    576:                        if (type == T_MMUFLT) {
                    577: #if defined(M68040)
                    578:                                if (mmutype == MMU_68040)
                    579:                                        (void)writeback(&frame);
                    580: #endif
                    581:                                return;
                    582:                        }
                    583:                        goto out;
                    584:                }
                    585:                if (type == T_MMUFLT) {
                    586:                        if (p && p->p_addr->u_pcb.pcb_onfault)
                    587:                                goto copyfault;
                    588:                        printf("uvm_fault(%p, %lx, 0, %x) -> %x\n",
                    589:                                 map, va, ftype, rv);
                    590:                        printf("  type %x, code [mmu,,ssw]: %x\n",
                    591:                                 type, code);
                    592:                        goto dopanic;
                    593:                }
                    594:                frame.f_pad = code & 0xffff;
                    595:                ucode = vftype;
                    596:                typ = SEGV_MAPERR;
                    597:                i = SIGSEGV;
                    598:            }
                    599:                break;
                    600:        }
                    601:        sv.sival_int = v;
                    602:        trapsignal(p, i, ucode, typ, sv);
                    603: out:
                    604:        if ((type & T_USER) == 0)
                    605:                return;
                    606: #ifdef M68040
                    607:        wb_userret(p, &frame);
                    608: #else
                    609:        userret(p);
                    610: #endif
                    611: }
                    612:
                    613: #if defined(M68040)
                    614: #ifdef DEBUG
                    615: struct writebackstats {
                    616:        int calls;
                    617:        int cpushes;
                    618:        int move16s;
                    619:        int wb1s, wb2s, wb3s;
                    620:        int wbsize[4];
                    621: } wbstats;
                    622:
                    623: char *f7sz[] = { "longword", "byte", "word", "line"};
                    624: char *f7tt[] = { "normal", "MOVE16", "AFC", "ACK"};
                    625: char *f7tm[] = { "d-push", "u-data", "u-code", "M-data",
                    626:        "M-code", "k-data", "k-code", "RES"};
                    627: char wberrstr[] =
                    628: "WARNING: pid %d(%s) writeback [%s] failed, pc=%x fa=%x wba=%x wbd=%x\n";
                    629: #endif
                    630:
                    631: int
                    632: writeback(struct frame *fp)
                    633: {
                    634:        struct fmt7 *f = &fp->f_fmt7;
                    635:        struct proc *p = curproc;
                    636:        int err = 0;
                    637:        u_int fa;
                    638:        paddr_t pa;
                    639:        caddr_t oonfault = p->p_addr->u_pcb.pcb_onfault;
                    640:
                    641: #ifdef DEBUG
                    642:        if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid)) {
                    643:                printf(" pid=%d, fa=%x,", p->p_pid, f->f_fa);
                    644:                dumpssw(f->f_ssw);
                    645:        }
                    646:        wbstats.calls++;
                    647: #endif
                    648:        /*
                    649:         * Deal with special cases first.
                    650:         */
                    651:        if ((f->f_ssw & SSW4_TMMASK) == SSW4_TMDCP) {
                    652:                /*
                    653:                 * Dcache push fault.
                    654:                 * Line-align the address and write out the push data to
                    655:                 * the indicated physical address.
                    656:                 */
                    657: #ifdef DEBUG
                    658:                if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid)) {
                    659:                        printf(" pushing %s to PA %x, data %x",
                    660:                                         f7sz[(f->f_ssw & SSW4_SZMASK) >> 5],
                    661:                                         f->f_fa, f->f_pd0);
                    662:                        if ((f->f_ssw & SSW4_SZMASK) == SSW4_SZLN)
                    663:                                printf("/%x/%x/%x",
                    664:                                                 f->f_pd1, f->f_pd2, f->f_pd3);
                    665:                        printf("\n");
                    666:                }
                    667:                if (f->f_wb1s & SSW4_WBSV)
                    668:                        panic("writeback: cache push with WB1S valid");
                    669:                wbstats.cpushes++;
                    670: #endif
                    671:                /*
                    672:                 * XXX there are security problems if we attempt to do a
                    673:                 * cache push after a signal handler has been called.
                    674:                 */
                    675:                pmap_kenter_pa((vaddr_t)vmmap,
                    676:                    trunc_page(f->f_fa), VM_PROT_WRITE);
                    677:                pmap_update(pmap_kernel());
                    678:                fa = (u_int)&vmmap[(f->f_fa & PGOFSET) & ~0x000f];
                    679:                bcopy((caddr_t)&f->f_pd0, (caddr_t)fa, 16);
                    680:                pmap_extract(pmap_kernel(), (vaddr_t)fa, &pa);
                    681:                DCFL(pa);
                    682:                pmap_kremove((vaddr_t)vmmap, PAGE_SIZE);
                    683:                pmap_update(pmap_kernel());
                    684:        } else if ((f->f_ssw & (SSW4_RW|SSW4_TTMASK)) == SSW4_TTM16) {
                    685:                /*
                    686:                 * MOVE16 fault.
                    687:                 * Line-align the address and write out the push data to
                    688:                 * the indicated virtual address.
                    689:                 */
                    690: #ifdef DEBUG
                    691:                if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
                    692:                        printf(" MOVE16 to VA %x(%x), data %x/%x/%x/%x\n",
                    693:                                         f->f_fa, f->f_fa & ~0xF, f->f_pd0, f->f_pd1,
                    694:                                         f->f_pd2, f->f_pd3);
                    695:                if (f->f_wb1s & SSW4_WBSV)
                    696:                        panic("writeback: MOVE16 with WB1S valid");
                    697:                wbstats.move16s++;
                    698: #endif
                    699:                if (KDFAULT(f->f_wb1s))
                    700:                        bcopy((caddr_t)&f->f_pd0, (caddr_t)(f->f_fa & ~0xF), 16);
                    701:                else
                    702:                        err = suline((caddr_t)(f->f_fa & ~0xF), (caddr_t)&f->f_pd0);
                    703:                if (err) {
                    704:                        fa = f->f_fa & ~0xF;
                    705: #ifdef DEBUG
                    706:                        if (mmudebug & MDB_WBFAILED)
                    707:                                printf(wberrstr, p->p_pid, p->p_comm,
                    708:                                                 "MOVE16", fp->f_pc, f->f_fa,
                    709:                                                 f->f_fa & ~0xF, f->f_pd0);
                    710: #endif
                    711:                }
                    712:        } else if (f->f_wb1s & SSW4_WBSV) {
                    713:                /*
                    714:                 * Writeback #1.
                    715:                 * Position the "memory-aligned" data and write it out.
                    716:                 */
                    717:                u_int wb1d = f->f_wb1d;
                    718:                int off;
                    719:
                    720: #ifdef DEBUG
                    721:                if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
                    722:                        dumpwb(1, f->f_wb1s, f->f_wb1a, f->f_wb1d);
                    723:                wbstats.wb1s++;
                    724:                wbstats.wbsize[(f->f_wb2s&SSW4_SZMASK)>>5]++;
                    725: #endif
                    726:                off = (f->f_wb1a & 3) * 8;
                    727:                switch (f->f_wb1s & SSW4_SZMASK) {
                    728:                        case SSW4_SZLW:
                    729:                                if (off)
                    730:                                        wb1d = (wb1d >> (32 - off)) | (wb1d << off);
                    731:                                if (KDFAULT(f->f_wb1s))
                    732:                                        *(long *)f->f_wb1a = wb1d;
                    733:                                else
                    734:                                        err = copyout(&wb1d,
                    735:                                            (caddr_t)f->f_wb1a, sizeof(int));
                    736:                                break;
                    737:                        case SSW4_SZB:
                    738:                                off = 24 - off;
                    739:                                if (off)
                    740:                                        wb1d >>= off;
                    741:                                if (KDFAULT(f->f_wb1s))
                    742:                                        *(char *)f->f_wb1a = wb1d;
                    743:                                else {
                    744:                                        char tmp = wb1d;
                    745:
                    746:                                        err = copyout(&tmp,
                    747:                                            (caddr_t)f->f_wb1a, sizeof(char));
                    748:                                }
                    749:                                break;
                    750:                        case SSW4_SZW:
                    751:                                off = (off + 16) % 32;
                    752:                                if (off)
                    753:                                        wb1d = (wb1d >> (32 - off)) | (wb1d << off);
                    754:                                if (KDFAULT(f->f_wb1s))
                    755:                                        *(short *)f->f_wb1a = wb1d;
                    756:                                else {
                    757:                                        short tmp = wb1d;
                    758:
                    759:                                        err = copyout(&tmp,
                    760:                                            (caddr_t)f->f_wb1a, sizeof(short));
                    761:                                }
                    762:                                break;
                    763:                }
                    764:                if (err) {
                    765:                        fa = f->f_wb1a;
                    766: #ifdef DEBUG
                    767:                        if (mmudebug & MDB_WBFAILED)
                    768:                                printf(wberrstr, p->p_pid, p->p_comm,
                    769:                                                 "#1", fp->f_pc, f->f_fa,
                    770:                                                 f->f_wb1a, f->f_wb1d);
                    771: #endif
                    772:                }
                    773:        }
                    774:        /*
                    775:         * Deal with the "normal" writebacks.
                    776:         *
                    777:         * XXX writeback2 is known to reflect a LINE size writeback after
                    778:         * a MOVE16 was already dealt with above.  Ignore it.
                    779:         */
                    780:        if (err == 0 && (f->f_wb2s & SSW4_WBSV) &&
                    781:                 (f->f_wb2s & SSW4_SZMASK) != SSW4_SZLN) {
                    782: #ifdef DEBUG
                    783:                if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
                    784:                        dumpwb(2, f->f_wb2s, f->f_wb2a, f->f_wb2d);
                    785:                wbstats.wb2s++;
                    786:                wbstats.wbsize[(f->f_wb2s&SSW4_SZMASK)>>5]++;
                    787: #endif
                    788:                switch (f->f_wb2s & SSW4_SZMASK) {
                    789:                        case SSW4_SZLW:
                    790:                                if (KDFAULT(f->f_wb2s))
                    791:                                        *(long *)f->f_wb2a = f->f_wb2d;
                    792:                                else
                    793:                                        err = copyout(&f->f_wb2d,
                    794:                                            (caddr_t)f->f_wb2a, sizeof(int));
                    795:                                break;
                    796:                        case SSW4_SZB:
                    797:                                if (KDFAULT(f->f_wb2s))
                    798:                                        *(char *)f->f_wb2a = f->f_wb2d;
                    799:                                else {
                    800:                                        char tmp = f->f_wb2d;
                    801:
                    802:                                        err = copyout(&tmp,
                    803:                                            (caddr_t)f->f_wb2a, sizeof(char));
                    804:                                }
                    805:                                break;
                    806:                        case SSW4_SZW:
                    807:                                if (KDFAULT(f->f_wb2s))
                    808:                                        *(short *)f->f_wb2a = f->f_wb2d;
                    809:                                else {
                    810:                                        short tmp = f->f_wb2d;
                    811:
                    812:                                        err = copyout(&tmp,
                    813:                                            (caddr_t)f->f_wb2a, sizeof(short));
                    814:                                }
                    815:                                break;
                    816:                }
                    817:                if (err) {
                    818:                        fa = f->f_wb2a;
                    819: #ifdef DEBUG
                    820:                        if (mmudebug & MDB_WBFAILED) {
                    821:                                printf(wberrstr, p->p_pid, p->p_comm,
                    822:                                                 "#2", fp->f_pc, f->f_fa,
                    823:                                                 f->f_wb2a, f->f_wb2d);
                    824:                                dumpssw(f->f_ssw);
                    825:                                dumpwb(2, f->f_wb2s, f->f_wb2a, f->f_wb2d);
                    826:                        }
                    827: #endif
                    828:                }
                    829:        }
                    830:        if (err == 0 && (f->f_wb3s & SSW4_WBSV)) {
                    831: #ifdef DEBUG
                    832:                if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
                    833:                        dumpwb(3, f->f_wb3s, f->f_wb3a, f->f_wb3d);
                    834:                wbstats.wb3s++;
                    835:                wbstats.wbsize[(f->f_wb3s&SSW4_SZMASK)>>5]++;
                    836: #endif
                    837:                switch (f->f_wb3s & SSW4_SZMASK) {
                    838:                        case SSW4_SZLW:
                    839:                                if (KDFAULT(f->f_wb3s))
                    840:                                        *(long *)f->f_wb3a = f->f_wb3d;
                    841:                                else
                    842:                                        err = copyout(&f->f_wb3d,
                    843:                                            (caddr_t)f->f_wb3a, sizeof(int));
                    844:                                break;
                    845:                        case SSW4_SZB:
                    846:                                if (KDFAULT(f->f_wb3s))
                    847:                                        *(char *)f->f_wb3a = f->f_wb3d;
                    848:                                else {
                    849:                                        char tmp = f->f_wb3d;
                    850:
                    851:                                        err = copyout(&tmp,
                    852:                                            (caddr_t)f->f_wb3a, sizeof(char));
                    853:                                }
                    854:                                break;
                    855:                        case SSW4_SZW:
                    856:                                if (KDFAULT(f->f_wb3s))
                    857:                                        *(short *)f->f_wb3a = f->f_wb3d;
                    858:                                else {
                    859:                                        short tmp = f->f_wb3d;
                    860:
                    861:                                        err = copyout(&tmp,
                    862:                                            (caddr_t)f->f_wb3a, sizeof(short));
                    863:                                }
                    864:                                break;
                    865: #ifdef DEBUG
                    866:                        case SSW4_SZLN:
                    867:                                panic("writeback: wb3s indicates LINE write");
                    868: #endif
                    869:                }
                    870:                if (err) {
                    871:                        fa = f->f_wb3a;
                    872: #ifdef DEBUG
                    873:                        if (mmudebug & MDB_WBFAILED)
                    874:                                printf(wberrstr, p->p_pid, p->p_comm,
                    875:                                                 "#3", fp->f_pc, f->f_fa,
                    876:                                                 f->f_wb3a, f->f_wb3d);
                    877: #endif
                    878:                }
                    879:        }
                    880:        p->p_addr->u_pcb.pcb_onfault = oonfault;
                    881:        /*
                    882:         * Any problems are SIGSEGV's
                    883:         */
                    884:        if (err)
                    885:                err = SIGSEGV;
                    886:        return (err);
                    887: }
                    888:
                    889: #ifdef DEBUG
                    890: void
                    891: dumpssw(ssw)
                    892:        register u_short ssw;
                    893: {
                    894:        printf(" SSW: %x: ", ssw);
                    895:        if (ssw & SSW4_CP)
                    896:                printf("CP,");
                    897:        if (ssw & SSW4_CU)
                    898:                printf("CU,");
                    899:        if (ssw & SSW4_CT)
                    900:                printf("CT,");
                    901:        if (ssw & SSW4_CM)
                    902:                printf("CM,");
                    903:        if (ssw & SSW4_MA)
                    904:                printf("MA,");
                    905:        if (ssw & SSW4_ATC)
                    906:                printf("ATC,");
                    907:        if (ssw & SSW4_LK)
                    908:                printf("LK,");
                    909:        if (ssw & SSW4_RW)
                    910:                printf("RW,");
                    911:        printf(" SZ=%s, TT=%s, TM=%s\n",
                    912:                         f7sz[(ssw & SSW4_SZMASK) >> 5],
                    913:                         f7tt[(ssw & SSW4_TTMASK) >> 3],
                    914:                         f7tm[ssw & SSW4_TMMASK]);
                    915: }
                    916:
                    917: void
                    918: dumpwb(num, s, a, d)
                    919:        int num;
                    920:        u_short s;
                    921:        u_int a, d;
                    922: {
                    923:        register struct proc *p = curproc;
                    924:        paddr_t pa;
                    925:        int tmp;
                    926:
                    927:        printf(" writeback #%d: VA %x, data %x, SZ=%s, TT=%s, TM=%s\n",
                    928:                         num, a, d, f7sz[(s & SSW4_SZMASK) >> 5],
                    929:                         f7tt[(s & SSW4_TTMASK) >> 3], f7tm[s & SSW4_TMMASK]);
                    930:        printf("               PA ");
                    931:        if (pmap_extract(p->p_vmspace->vm_map.pmap, (vaddr_t)a, &pa) == FALSE)
                    932:                printf("<invalid address>");
                    933:        else {
                    934:                if (copyin((caddr_t)a, &tmp, sizeof(int)) == 0)
                    935:                        printf("%lx, current value %lx", pa, tmp);
                    936:                else
                    937:                        printf("%lx, current value inaccessible", pa);
                    938:        }
                    939:        printf("\n");
                    940: }
                    941: #endif
                    942: #endif
                    943:
                    944: /*
                    945:  * Process a system call.
                    946:  */
                    947: void
                    948: syscall(code, frame)
                    949:        register_t code;
                    950:        struct frame frame;
                    951: {
                    952:        register caddr_t params;
                    953:        register struct sysent *callp;
                    954:        register struct proc *p;
                    955:        int error, opc, nsys;
                    956:        size_t argsize;
                    957:        register_t args[8], rval[2];
                    958: #ifdef COMPAT_SUNOS
                    959:        extern struct emul emul_sunos;
                    960: #endif
                    961:        uvmexp.syscalls++;
                    962:
                    963:        if (!USERMODE(frame.f_sr))
                    964:                panic("syscall");
                    965:        p = curproc;
                    966:        p->p_md.md_regs = frame.f_regs;
                    967:        opc = frame.f_pc;
                    968:
                    969:        nsys = p->p_emul->e_nsysent;
                    970:        callp = p->p_emul->e_sysent;
                    971:
                    972: #ifdef COMPAT_SUNOS
                    973:        if (p->p_emul == &emul_sunos) {
                    974:                /*
                    975:                 * SunOS passes the syscall-number on the stack, whereas
                    976:                 * BSD passes it in D0. So, we have to get the real "code"
                    977:                 * from the stack, and clean up the stack, as SunOS glue
                    978:                 * code assumes the kernel pops the syscall argument the
                    979:                 * glue pushed on the stack. Sigh...
                    980:                 */
                    981:                if (copyin((caddr_t)frame.f_regs[SP], &code,
                    982:                    sizeof(register_t)) != 0)
                    983:                        code = -1;
                    984:
                    985:                /*
                    986:                 * XXX
                    987:                 * Don't do this for sunos_sigreturn, as there's no stored pc
                    988:                 * on the stack to skip, the argument follows the syscall
                    989:                 * number without a gap.
                    990:                 */
                    991:                if (code != SUNOS_SYS_sigreturn) {
                    992:                        frame.f_regs[SP] += sizeof (int);
                    993:                        /*
                    994:                         * remember that we adjusted the SP,
                    995:                         * might have to undo this if the system call
                    996:                         * returns ERESTART.
                    997:                         */
                    998:                        p->p_md.md_flags |= MDP_STACKADJ;
                    999:                } else
                   1000:                        p->p_md.md_flags &= ~MDP_STACKADJ;
                   1001:        }
                   1002: #endif
                   1003:
                   1004:        params = (caddr_t)frame.f_regs[SP] + sizeof(int);
                   1005:
                   1006:        switch (code) {
                   1007:        case SYS_syscall:
                   1008:                /*
                   1009:                 * Code is first argument, followed by actual args.
                   1010:                 */
                   1011:                if (copyin(params, &code, sizeof(register_t)) != 0)
                   1012:                        code = -1;
                   1013:                params += sizeof(int);
                   1014:                /*
                   1015:                 * XXX sigreturn requires special stack manipulation
                   1016:                 * that is only done if entered via the sigreturn
                   1017:                 * trap.  Cannot allow it here so make sure we fail.
                   1018:                 */
                   1019:                if (code == SYS_sigreturn)
                   1020:                        code = nsys;
                   1021:                break;
                   1022:        case SYS___syscall:
                   1023:                /*
                   1024:                 * Like syscall, but code is a quad, so as to maintain
                   1025:                 * quad alignment for the rest of the arguments.
                   1026:                 */
                   1027:                if (callp != sysent)
                   1028:                        break;
                   1029:                if (copyin(params + _QUAD_LOWWORD * sizeof(int), &code,
                   1030:                    sizeof(register_t)) != 0)
                   1031:                        code = -1;
                   1032:                params += sizeof(quad_t);
                   1033:                break;
                   1034:        default:
                   1035:                break;
                   1036:        }
                   1037:        if (code < 0 || code >= nsys)
                   1038:                callp += p->p_emul->e_nosys;            /* illegal */
                   1039:        else
                   1040:                callp   += code;
                   1041:        argsize = callp->sy_argsize;
                   1042:        if (argsize)
                   1043:                error = copyin(params, (caddr_t)args, argsize);
                   1044:        else
                   1045:                error   = 0;
                   1046: #ifdef SYSCALL_DEBUG
                   1047:        scdebug_call(p, code, args);
                   1048: #endif
                   1049: #ifdef KTRACE
                   1050:        if (KTRPOINT(p, KTR_SYSCALL))
                   1051:                ktrsyscall(p, code, argsize, args);
                   1052: #endif
                   1053:        if (error)
                   1054:                goto bad;
                   1055:        rval[0] = 0;
                   1056:        rval[1] = frame.f_regs[D1];
                   1057: #if NSYSTRACE > 0
                   1058:        if (ISSET(p->p_flag, P_SYSTRACE))
                   1059:                error = systrace_redirect(code, p, args, rval);
                   1060:        else
                   1061: #endif
                   1062:                error = (*callp->sy_call)(p, args, rval);
                   1063:        switch (error) {
                   1064:        case 0:
                   1065:                frame.f_regs[D0] = rval[0];
                   1066:                frame.f_regs[D1] = rval[1];
                   1067:                frame.f_sr &= ~PSL_C;   /* carry bit */
                   1068:                break;
                   1069:        case ERESTART:
                   1070:                /*
                   1071:                 * We always enter through a `trap' instruction, which is 2
                   1072:                 * bytes, so adjust the pc by that amount.
                   1073:                 */
                   1074:                frame.f_pc = opc - 2;
                   1075:                break;
                   1076:        case EJUSTRETURN:
                   1077:                /* nothing to do */
                   1078:                break;
                   1079:        default:
                   1080: bad:
                   1081:                if (p->p_emul->e_errno)
                   1082:                        error = p->p_emul->e_errno[error];
                   1083:                frame.f_regs[D0] = error;
                   1084:                frame.f_sr |= PSL_C;    /* carry bit */
                   1085:                break;
                   1086:        }
                   1087:
                   1088: #ifdef SYSCALL_DEBUG
                   1089:        scdebug_ret(p, code, error, rval);
                   1090: #endif
                   1091: #ifdef COMPAT_SUNOS
                   1092:        /* need new p-value for this */
                   1093:        if (error == ERESTART && (p->p_md.md_flags & MDP_STACKADJ))
                   1094:                frame.f_regs[SP] -= sizeof (int);
                   1095: #endif
                   1096:        userret(p);
                   1097: #ifdef KTRACE
                   1098:        if (KTRPOINT(p, KTR_SYSRET))
                   1099:                ktrsysret(p, code, error, rval[0]);
                   1100: #endif
                   1101: }
                   1102:
                   1103: /*
                   1104:  * Allocation routines for software interrupts.
                   1105:  */
                   1106: u_int8_t
                   1107: allocate_sir(proc, arg)
                   1108:        void (*proc)(void *);
                   1109:        void *arg;
                   1110: {
                   1111:        int bit;
                   1112:
                   1113:        if (next_sir >= NSIR)
                   1114:                panic("allocate_sir: none left");
                   1115:        bit = next_sir++;
                   1116:        sir_routines[bit] = proc;
                   1117:        sir_args[bit] = arg;
                   1118:        return (1 << bit);
                   1119: }
                   1120:
                   1121: typedef SLIST_HEAD(,intrhand) intrhand_t;
                   1122: intrhand_t intrs[NVMEINTR];
                   1123:
                   1124: void
                   1125: init_intrs()
                   1126: {
                   1127:        int i;
                   1128:        extern void netintr(void *);
                   1129:
                   1130:        /* hard interrupts... */
                   1131:        for (i = 0; i < NVMEINTR; i++)
                   1132:                SLIST_INIT(&intrs[i]);
                   1133:
                   1134:        /* soft interrupts... */
                   1135:        sir_routines[0] = netintr;
                   1136:        sir_routines[1] = (void (*)(void *))softclock;
                   1137:        next_sir = 2;
                   1138: }
                   1139:
                   1140: void
                   1141: hardintr(pc, evec, frame)
                   1142:        int pc;
                   1143:        int evec;
                   1144:        void *frame;
                   1145: {
                   1146:        extern void straytrap(int, u_short);
                   1147:        int vec = (evec & 0xfff) >> 2;  /* XXX should be m68k macro? */
                   1148:        struct intrhand *ih;
                   1149:        intrhand_t *list;
                   1150:        int count = 0;
                   1151:        int r;
                   1152:
                   1153:        uvmexp.intrs++;
                   1154:
                   1155:        list = &intrs[vec];
                   1156:        if (SLIST_EMPTY(list)) {
                   1157:                straytrap(pc, evec);
                   1158:        } else {
                   1159:                SLIST_FOREACH(ih, list, ih_link) {
                   1160:                        r = (*ih->ih_fn)(ih->ih_wantframe ? frame : ih->ih_arg);
                   1161:                        if (r != 0) {
                   1162:                                ih->ih_count.ec_count++;
                   1163:                                count++;
                   1164:                        }
                   1165:                }
                   1166:
                   1167:                if (count == 0) {
                   1168:                        printf("Unclaimed interrupt (vector %d) from %x\n",
                   1169:                            evec, pc);
                   1170:                }
                   1171:        }
                   1172: }
                   1173:
                   1174: /*
                   1175:  * find a useable interrupt vector in the range start, end. It starts at
                   1176:  * the end of the range, and searches backwards (to increase the chances
                   1177:  * of not conflicting with more normal users)
                   1178:  */
                   1179: int
                   1180: intr_findvec(start, end)
                   1181:        int start, end;
                   1182: {
                   1183:        extern u_long *vectab[], hardtrap, badtrap;
                   1184:        int vec;
                   1185:
                   1186:        if (start < 0 || end >= NVMEINTR || start > end)
                   1187:                return (-1);
                   1188:        for (vec = end; vec > start; --vec)
                   1189:                if (vectab[vec] == &badtrap || vectab[vec] == &hardtrap)
                   1190:                        return (vec);
                   1191:        return (-1);
                   1192: }
                   1193:
                   1194: /*
                   1195:  * Chain the interrupt handler in. But first check if the vector
                   1196:  * offset chosen is legal. It either must be a badtrap (not allocated
                   1197:  * for a `system' purpose), or it must be a hardtrap (ie. already
                   1198:  * allocated to deal with chained interrupt handlers).
                   1199:  */
                   1200: int
                   1201: intr_establish(vec, ih, name)
                   1202:        int vec;
                   1203:        struct intrhand *ih;
                   1204:        const char *name;
                   1205: {
                   1206:        extern u_long *vectab[], hardtrap, badtrap;
                   1207:        struct intrhand *intr;
                   1208:        intrhand_t *list;
                   1209:
                   1210: #ifdef DIAGNOSTIC
                   1211:        if (vec < 0 || vec >= NVMEINTR) {
                   1212:                panic("intr_establish: vec (0x%x) out of bounds", vec);
                   1213:                return (-1);
                   1214:        }
                   1215: #endif
                   1216:
                   1217:        if (vectab[vec] != &badtrap && vectab[vec] != &hardtrap) {
                   1218: #ifdef DIAGNOSTIC
                   1219:                panic("intr_establish: vec (%x) unavailable for devices", vec);
                   1220: #endif
                   1221:                return (-1);
                   1222:        }
                   1223:        vectab[vec] = &hardtrap;
                   1224:
                   1225:        list = &intrs[vec];
                   1226:        if (!SLIST_EMPTY(list)) {
                   1227:                intr = SLIST_FIRST(list);
                   1228:                if (intr->ih_ipl != ih->ih_ipl) {
                   1229: #ifdef DIAGNOSTIC
                   1230:                        panic("intr_establish: there are other handlers with "
                   1231:                            "vec (0x%x) at ipl %x, but you want it at %x",
                   1232:                            vec, intr->ih_ipl, ih->ih_ipl);
                   1233: #endif
                   1234:                        return (-1);
                   1235:                }
                   1236:        }
                   1237:
                   1238:        evcount_attach(&ih->ih_count, name, &ih->ih_ipl, &evcount_intr);
                   1239:        SLIST_INSERT_HEAD(list, ih, ih_link);
                   1240:        return (0);
                   1241: }
                   1242:
                   1243: #ifdef DDB
                   1244: #include <sys/reboot.h>
                   1245: #include <machine/db_machdep.h>
                   1246: #include <ddb/db_command.h>
                   1247:
                   1248: void db_prom_cmd(db_expr_t, int, db_expr_t, char *);
                   1249: void db_machine_init(void);
                   1250:
                   1251: /* ARGSUSED */
                   1252: void
                   1253: db_prom_cmd(addr, have_addr, count, modif)
                   1254:        db_expr_t addr;
                   1255:        int have_addr;
                   1256:        db_expr_t count;
                   1257:        char *modif;
                   1258: {
                   1259:        doboot();
                   1260: }
                   1261:
                   1262: struct db_command db_machine_cmds[] = {
                   1263:        { "prom",   db_prom_cmd,   0, 0},
                   1264:        { (char *)0,}
                   1265: };
                   1266:
                   1267: void
                   1268: db_machine_init()
                   1269: {
                   1270:        db_machine_commands_install(db_machine_cmds);
                   1271: }
                   1272: #endif /* DDB */

CVSweb