trap.c revision 1.1
11.1Sdbj/*	$NetBSD: trap.c,v 1.1 1998/06/09 07:53:06 dbj Exp $	*/
21.1Sdbj
31.1Sdbj/*
41.1Sdbj * Copyright (c) 1988 University of Utah.
51.1Sdbj * Copyright (c) 1982, 1986, 1990, 1993
61.1Sdbj *	The Regents of the University of California.  All rights reserved.
71.1Sdbj *
81.1Sdbj * This code is derived from software contributed to Berkeley by
91.1Sdbj * the Systems Programming Group of the University of Utah Computer
101.1Sdbj * Science Department.
111.1Sdbj *
121.1Sdbj * Redistribution and use in source and binary forms, with or without
131.1Sdbj * modification, are permitted provided that the following conditions
141.1Sdbj * are met:
151.1Sdbj * 1. Redistributions of source code must retain the above copyright
161.1Sdbj *    notice, this list of conditions and the following disclaimer.
171.1Sdbj * 2. Redistributions in binary form must reproduce the above copyright
181.1Sdbj *    notice, this list of conditions and the following disclaimer in the
191.1Sdbj *    documentation and/or other materials provided with the distribution.
201.1Sdbj * 3. All advertising materials mentioning features or use of this software
211.1Sdbj *    must display the following acknowledgement:
221.1Sdbj *	This product includes software developed by the University of
231.1Sdbj *	California, Berkeley and its contributors.
241.1Sdbj * 4. Neither the name of the University nor the names of its contributors
251.1Sdbj *    may be used to endorse or promote products derived from this software
261.1Sdbj *    without specific prior written permission.
271.1Sdbj *
281.1Sdbj * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
291.1Sdbj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
301.1Sdbj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
311.1Sdbj * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
321.1Sdbj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
331.1Sdbj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
341.1Sdbj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
351.1Sdbj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
361.1Sdbj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
371.1Sdbj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
381.1Sdbj * SUCH DAMAGE.
391.1Sdbj *
401.1Sdbj * from: Utah $Hdr: trap.c 1.37 92/12/20$
411.1Sdbj *
421.1Sdbj *	@(#)trap.c	8.5 (Berkeley) 1/4/94
431.1Sdbj */
441.1Sdbj
451.1Sdbj#if 0
461.1Sdbj#include <machine/hp300spu.h>	/* XXX param.h includes cpu.h */
471.1Sdbj#endif
481.1Sdbj
491.1Sdbj#include <sys/param.h>
501.1Sdbj#include <sys/systm.h>
511.1Sdbj#include <sys/proc.h>
521.1Sdbj#include <sys/acct.h>
531.1Sdbj#include <sys/kernel.h>
541.1Sdbj#include <sys/signalvar.h>
551.1Sdbj#include <sys/resourcevar.h>
561.1Sdbj#include <sys/syscall.h>
571.1Sdbj#include <sys/syslog.h>
581.1Sdbj#include <sys/user.h>
591.1Sdbj#ifdef KTRACE
601.1Sdbj#include <sys/ktrace.h>
611.1Sdbj#endif
621.1Sdbj
631.1Sdbj#include <m68k/frame.h>
641.1Sdbj
651.1Sdbj#include <machine/db_machdep.h>
661.1Sdbj#include <machine/psl.h>
671.1Sdbj#include <machine/trap.h>
681.1Sdbj#include <machine/cpu.h>
691.1Sdbj#include <machine/reg.h>
701.1Sdbj#include <machine/intr.h>
711.1Sdbj
721.1Sdbj#include <vm/vm.h>
731.1Sdbj#include <vm/pmap.h>
741.1Sdbj
751.1Sdbj#include <dev/cons.h>
761.1Sdbj
771.1Sdbj#ifdef COMPAT_HPUX
781.1Sdbj#include <compat/hpux/hpux.h>
791.1Sdbjextern struct emul emul_hpux;
801.1Sdbj#endif
811.1Sdbj
821.1Sdbj#ifdef COMPAT_SUNOS
831.1Sdbj#include <compat/sunos/sunos_syscall.h>
841.1Sdbjextern struct emul emul_sunos;
851.1Sdbj#endif
861.1Sdbj
871.1Sdbjint	writeback __P((struct frame *fp, int docachepush));
881.1Sdbjvoid	trap __P((int type, u_int code, u_int v, struct frame frame));
891.1Sdbjvoid	syscall __P((register_t code, struct frame frame));
901.1Sdbjvoid	child_return __P((struct proc *, struct frame));
911.1Sdbj
921.1Sdbj#ifdef DEBUG
931.1Sdbjvoid	dumpssw __P((u_short));
941.1Sdbjvoid	dumpwb __P((int, u_short, u_int, u_int));
951.1Sdbj#endif
961.1Sdbj
971.1Sdbjstatic inline void userret __P((struct proc *p, struct frame *fp,
981.1Sdbj	    u_quad_t oticks, u_int faultaddr, int fromtrap));
991.1Sdbj
1001.1Sdbjchar	*trap_type[] = {
1011.1Sdbj	"Bus error",
1021.1Sdbj	"Address error",
1031.1Sdbj	"Illegal instruction",
1041.1Sdbj	"Zero divide",
1051.1Sdbj	"CHK instruction",
1061.1Sdbj	"TRAPV instruction",
1071.1Sdbj	"Privilege violation",
1081.1Sdbj	"Trace trap",
1091.1Sdbj	"MMU fault",
1101.1Sdbj	"SSIR trap",
1111.1Sdbj	"Format error",
1121.1Sdbj	"68881 exception",
1131.1Sdbj	"Coprocessor violation",
1141.1Sdbj	"Async system trap"
1151.1Sdbj};
1161.1Sdbjint	trap_types = sizeof trap_type / sizeof trap_type[0];
1171.1Sdbj
1181.1Sdbj/*
1191.1Sdbj * Size of various exception stack frames (minus the standard 8 bytes)
1201.1Sdbj */
1211.1Sdbjshort	exframesize[] = {
1221.1Sdbj	FMT0SIZE,	/* type 0 - normal (68020/030/040/060) */
1231.1Sdbj	FMT1SIZE,	/* type 1 - throwaway (68020/030/040) */
1241.1Sdbj	FMT2SIZE,	/* type 2 - normal 6-word (68020/030/040/060) */
1251.1Sdbj	FMT3SIZE,	/* type 3 - FP post-instruction (68040/060) */
1261.1Sdbj	FMT4SIZE,	/* type 4 - access error/fp disabled (68060) */
1271.1Sdbj	-1, -1,		/* type 5-6 - undefined */
1281.1Sdbj	FMT7SIZE,	/* type 7 - access error (68040) */
1291.1Sdbj	58,		/* type 8 - bus fault (68010) */
1301.1Sdbj	FMT9SIZE,	/* type 9 - coprocessor mid-instruction (68020/030) */
1311.1Sdbj	FMTASIZE,	/* type A - short bus fault (68020/030) */
1321.1Sdbj	FMTBSIZE,	/* type B - long bus fault (68020/030) */
1331.1Sdbj	-1, -1, -1, -1	/* type C-F - undefined */
1341.1Sdbj};
1351.1Sdbj
1361.1Sdbj#ifdef M68060
1371.1Sdbj#define	KDFAULT_060(c)	(cputype == CPU_68060 && ((c) & FSLW_TM_SV))
1381.1Sdbj#define	WRFAULT_060(c)	(cputype == CPU_68060 && ((c) & FSLW_RW_W))
1391.1Sdbj#else
1401.1Sdbj#define	KDFAULT_060(c)	0
1411.1Sdbj#define	WRFAULT_060(c)	0
1421.1Sdbj#endif
1431.1Sdbj
1441.1Sdbj#ifdef M68040
1451.1Sdbj#define	KDFAULT_040(c)	(cputype == CPU_68040 && \
1461.1Sdbj			 ((c) & SSW4_TMMASK) == SSW4_TMKD)
1471.1Sdbj#define	WRFAULT_040(c)	(cputype == CPU_68040 && \
1481.1Sdbj			 ((c) & SSW4_RW) == 0)
1491.1Sdbj#else
1501.1Sdbj#define	KDFAULT_040(c)	0
1511.1Sdbj#define	WRFAULT_040(c)	0
1521.1Sdbj#endif
1531.1Sdbj
1541.1Sdbj#if defined(M68030) || defined(M68020)
1551.1Sdbj#define	KDFAULT_OTH(c)	(cputype <= CPU_68030 && \
1561.1Sdbj			 ((c) & (SSW_DF|SSW_FCMASK)) == (SSW_DF|FC_SUPERD))
1571.1Sdbj#define	WRFAULT_OTH(c)	(cputype <= CPU_68030 && \
1581.1Sdbj			 ((c) & (SSW_DF|SSW_RW)) == SSW_DF)
1591.1Sdbj#else
1601.1Sdbj#define	KDFAULT_OTH(c)	0
1611.1Sdbj#define	WRFAULT_OTH(c)	0
1621.1Sdbj#endif
1631.1Sdbj
1641.1Sdbj#define	KDFAULT(c)	(KDFAULT_060(c) || KDFAULT_040(c) || KDFAULT_OTH(c))
1651.1Sdbj#define	WRFAULT(c)	(WRFAULT_060(c) || WRFAULT_040(c) || WRFAULT_OTH(c))
1661.1Sdbj
1671.1Sdbj#ifdef DEBUG
1681.1Sdbjint mmudebug = 0;
1691.1Sdbjint mmupid = -1;
1701.1Sdbj#define MDB_FOLLOW	1
1711.1Sdbj#define MDB_WBFOLLOW	2
1721.1Sdbj#define MDB_WBFAILED	4
1731.1Sdbj#define MDB_ISPID(p)	((p) == mmupid)
1741.1Sdbj#endif
1751.1Sdbj
1761.1Sdbj#define NSIR	32
1771.1Sdbjvoid (*sir_routines[NSIR])();
1781.1Sdbjvoid *sir_args[NSIR];
1791.1Sdbjint next_sir;
1801.1Sdbj
1811.1Sdbj/*
1821.1Sdbj * trap and syscall both need the following work done before returning
1831.1Sdbj * to user mode.
1841.1Sdbj */
1851.1Sdbjstatic inline void
1861.1Sdbjuserret(p, fp, oticks, faultaddr, fromtrap)
1871.1Sdbj	struct proc *p;
1881.1Sdbj	struct frame *fp;
1891.1Sdbj	u_quad_t oticks;
1901.1Sdbj	u_int faultaddr;
1911.1Sdbj	int fromtrap;
1921.1Sdbj{
1931.1Sdbj	int sig, s;
1941.1Sdbj#ifdef M68040
1951.1Sdbj	int beenhere = 0;
1961.1Sdbj
1971.1Sdbjagain:
1981.1Sdbj#endif
1991.1Sdbj	/* take pending signals */
2001.1Sdbj	while ((sig = CURSIG(p)) != 0)
2011.1Sdbj		postsig(sig);
2021.1Sdbj	p->p_priority = p->p_usrpri;
2031.1Sdbj	if (want_resched) {
2041.1Sdbj		/*
2051.1Sdbj		 * Since we are curproc, clock will normally just change
2061.1Sdbj		 * our priority without moving us from one queue to another
2071.1Sdbj		 * (since the running process is not on a queue.)
2081.1Sdbj		 * If that happened after we put ourselves on the run queue
2091.1Sdbj		 * but before we mi_switch()'ed, we might not be on the queue
2101.1Sdbj		 * indicated by our priority.
2111.1Sdbj		 */
2121.1Sdbj		s = splstatclock();
2131.1Sdbj		setrunqueue(p);
2141.1Sdbj		p->p_stats->p_ru.ru_nivcsw++;
2151.1Sdbj		mi_switch();
2161.1Sdbj		splx(s);
2171.1Sdbj		while ((sig = CURSIG(p)) != 0)
2181.1Sdbj			postsig(sig);
2191.1Sdbj	}
2201.1Sdbj
2211.1Sdbj	/*
2221.1Sdbj	 * If profiling, charge system time to the trapped pc.
2231.1Sdbj	 */
2241.1Sdbj	if (p->p_flag & P_PROFIL) {
2251.1Sdbj		extern int psratio;
2261.1Sdbj
2271.1Sdbj		addupc_task(p, fp->f_pc,
2281.1Sdbj			    (int)(p->p_sticks - oticks) * psratio);
2291.1Sdbj	}
2301.1Sdbj#ifdef M68040
2311.1Sdbj	/*
2321.1Sdbj	 * Deal with user mode writebacks (from trap, or from sigreturn).
2331.1Sdbj	 * If any writeback fails, go back and attempt signal delivery.
2341.1Sdbj	 * unless we have already been here and attempted the writeback
2351.1Sdbj	 * (e.g. bad address with user ignoring SIGSEGV).  In that case
2361.1Sdbj	 * we just return to the user without sucessfully completing
2371.1Sdbj	 * the writebacks.  Maybe we should just drop the sucker?
2381.1Sdbj	 */
2391.1Sdbj	if (cputype == CPU_68040 && fp->f_format == FMT7) {
2401.1Sdbj		if (beenhere) {
2411.1Sdbj#ifdef DEBUG
2421.1Sdbj			if (mmudebug & MDB_WBFAILED)
2431.1Sdbj				printf(fromtrap ?
2441.1Sdbj		"pid %d(%s): writeback aborted, pc=%x, fa=%x\n" :
2451.1Sdbj		"pid %d(%s): writeback aborted in sigreturn, pc=%x\n",
2461.1Sdbj				    p->p_pid, p->p_comm, fp->f_pc, faultaddr);
2471.1Sdbj#endif
2481.1Sdbj		} else if ((sig = writeback(fp, fromtrap))) {
2491.1Sdbj			beenhere = 1;
2501.1Sdbj			oticks = p->p_sticks;
2511.1Sdbj			trapsignal(p, sig, faultaddr);
2521.1Sdbj			goto again;
2531.1Sdbj		}
2541.1Sdbj	}
2551.1Sdbj#endif
2561.1Sdbj	curpriority = p->p_priority;
2571.1Sdbj}
2581.1Sdbj
2591.1Sdbj/*
2601.1Sdbj * Trap is called from locore to handle most types of processor traps,
2611.1Sdbj * including events such as simulated software interrupts/AST's.
2621.1Sdbj * System calls are broken out for efficiency.
2631.1Sdbj */
2641.1Sdbj/*ARGSUSED*/
2651.1Sdbjvoid
2661.1Sdbjtrap(type, code, v, frame)
2671.1Sdbj	int type;
2681.1Sdbj	unsigned code;
2691.1Sdbj	unsigned v;
2701.1Sdbj	struct frame frame;
2711.1Sdbj{
2721.1Sdbj	extern char fubail[], subail[];
2731.1Sdbj	struct proc *p;
2741.1Sdbj	int i, s;
2751.1Sdbj        int bit;
2761.1Sdbj	u_int ucode;
2771.1Sdbj	u_quad_t sticks = 0 /* XXX initializer works around compiler bug */;
2781.1Sdbj
2791.1Sdbj	cnt.v_trap++;
2801.1Sdbj	p = curproc;
2811.1Sdbj	ucode = 0;
2821.1Sdbj
2831.1Sdbj	/* I have verified that this DOES happen! -gwr */
2841.1Sdbj	if (p == NULL)
2851.1Sdbj		p = &proc0;
2861.1Sdbj#ifdef DIAGNOSTIC
2871.1Sdbj	if (p->p_addr == NULL)
2881.1Sdbj		panic("trap: no pcb");
2891.1Sdbj#endif
2901.1Sdbj
2911.1Sdbj	if (USERMODE(frame.f_sr)) {
2921.1Sdbj		type |= T_USER;
2931.1Sdbj		sticks = p->p_sticks;
2941.1Sdbj		p->p_md.md_regs = frame.f_regs;
2951.1Sdbj	}
2961.1Sdbj	switch (type) {
2971.1Sdbj
2981.1Sdbj	default:
2991.1Sdbj	dopanic:
3001.1Sdbj		printf("trap type %d, code = 0x%x, v = 0x%x\n", type, code, v);
3011.1Sdbj		printf("%s program counter = 0x%x\n",
3021.1Sdbj		    (type & T_USER) ? "user" : "kernel", frame.f_pc);
3031.1Sdbj		/*
3041.1Sdbj		 * Let the kernel debugger see the trap frame that
3051.1Sdbj		 * caused us to panic.  This is a convenience so
3061.1Sdbj		 * one can see registers at the point of failure.
3071.1Sdbj		 */
3081.1Sdbj		s = splhigh();
3091.1Sdbj#ifdef KGDB
3101.1Sdbj		/* If connected, step or cont returns 1 */
3111.1Sdbj		if (kgdb_trap(type, &frame))
3121.1Sdbj			goto kgdb_cont;
3131.1Sdbj#endif
3141.1Sdbj#ifdef DDB
3151.1Sdbj		(void)kdb_trap(type, (db_regs_t *)&frame);
3161.1Sdbj#endif
3171.1Sdbj#ifdef KGDB
3181.1Sdbj	kgdb_cont:
3191.1Sdbj#endif
3201.1Sdbj		splx(s);
3211.1Sdbj		if (panicstr) {
3221.1Sdbj			printf("trap during panic!\n");
3231.1Sdbj#ifdef DEBUG
3241.1Sdbj			/* XXX should be a machine-dependent hook */
3251.1Sdbj			printf("(press a key)\n"); (void)cngetc();
3261.1Sdbj#endif
3271.1Sdbj		}
3281.1Sdbj		regdump((struct trapframe *)&frame, 128);
3291.1Sdbj		type &= ~T_USER;
3301.1Sdbj		if ((u_int)type < trap_types)
3311.1Sdbj			panic(trap_type[type]);
3321.1Sdbj		panic("trap");
3331.1Sdbj
3341.1Sdbj	case T_BUSERR:		/* kernel bus error */
3351.1Sdbj		if (p->p_addr->u_pcb.pcb_onfault == 0)
3361.1Sdbj			goto dopanic;
3371.1Sdbj		/* FALLTHROUGH */
3381.1Sdbj
3391.1Sdbj	copyfault:
3401.1Sdbj		/*
3411.1Sdbj		 * If we have arranged to catch this fault in any of the
3421.1Sdbj		 * copy to/from user space routines, set PC to return to
3431.1Sdbj		 * indicated location and set flag informing buserror code
3441.1Sdbj		 * that it may need to clean up stack frame.
3451.1Sdbj		 */
3461.1Sdbj		frame.f_stackadj = exframesize[frame.f_format];
3471.1Sdbj		frame.f_format = frame.f_vector = 0;
3481.1Sdbj		frame.f_pc = (int) p->p_addr->u_pcb.pcb_onfault;
3491.1Sdbj		return;
3501.1Sdbj
3511.1Sdbj	case T_BUSERR|T_USER:	/* bus error */
3521.1Sdbj	case T_ADDRERR|T_USER:	/* address error */
3531.1Sdbj		ucode = v;
3541.1Sdbj		i = SIGBUS;
3551.1Sdbj		break;
3561.1Sdbj
3571.1Sdbj	case T_COPERR:		/* kernel coprocessor violation */
3581.1Sdbj	case T_FMTERR|T_USER:	/* do all RTE errors come in as T_USER? */
3591.1Sdbj	case T_FMTERR:		/* ...just in case... */
3601.1Sdbj	/*
3611.1Sdbj	 * The user has most likely trashed the RTE or FP state info
3621.1Sdbj	 * in the stack frame of a signal handler.
3631.1Sdbj	 */
3641.1Sdbj		printf("pid %d: kernel %s exception\n", p->p_pid,
3651.1Sdbj		       type==T_COPERR ? "coprocessor" : "format");
3661.1Sdbj		type |= T_USER;
3671.1Sdbj		p->p_sigacts->ps_sigact[SIGILL] = SIG_DFL;
3681.1Sdbj		i = sigmask(SIGILL);
3691.1Sdbj		p->p_sigignore &= ~i;
3701.1Sdbj		p->p_sigcatch &= ~i;
3711.1Sdbj		p->p_sigmask &= ~i;
3721.1Sdbj		i = SIGILL;
3731.1Sdbj		ucode = frame.f_format;	/* XXX was ILL_RESAD_FAULT */
3741.1Sdbj		break;
3751.1Sdbj
3761.1Sdbj	case T_COPERR|T_USER:	/* user coprocessor violation */
3771.1Sdbj	/* What is a proper response here? */
3781.1Sdbj		ucode = 0;
3791.1Sdbj		i = SIGFPE;
3801.1Sdbj		break;
3811.1Sdbj
3821.1Sdbj	case T_FPERR|T_USER:	/* 68881 exceptions */
3831.1Sdbj	/*
3841.1Sdbj	 * We pass along the 68881 status which locore stashed
3851.1Sdbj	 * in code for us.  Note that there is a possibility that the
3861.1Sdbj	 * bit pattern of this will conflict with one of the
3871.1Sdbj	 * FPE_* codes defined in signal.h.  Fortunately for us, the
3881.1Sdbj	 * only such codes we use are all in the range 1-7 and the low
3891.1Sdbj	 * 3 bits of the status are defined as 0 so there is
3901.1Sdbj	 * no clash.
3911.1Sdbj	 */
3921.1Sdbj		ucode = code;
3931.1Sdbj		i = SIGFPE;
3941.1Sdbj		break;
3951.1Sdbj
3961.1Sdbj#ifdef M68040
3971.1Sdbj	case T_FPEMULI|T_USER:	/* unimplemented FP instuction */
3981.1Sdbj	case T_FPEMULD|T_USER:	/* unimplemented FP data type */
3991.1Sdbj		/* XXX need to FSAVE */
4001.1Sdbj		printf("pid %d(%s): unimplemented FP %s at %x (EA %x)\n",
4011.1Sdbj		       p->p_pid, p->p_comm,
4021.1Sdbj		       frame.f_format == 2 ? "instruction" : "data type",
4031.1Sdbj		       frame.f_pc, frame.f_fmt2.f_iaddr);
4041.1Sdbj		/* XXX need to FRESTORE */
4051.1Sdbj		i = SIGFPE;
4061.1Sdbj		break;
4071.1Sdbj#endif
4081.1Sdbj
4091.1Sdbj	case T_ILLINST|T_USER:	/* illegal instruction fault */
4101.1Sdbj#ifdef COMPAT_HPUX
4111.1Sdbj		if (p->p_emul == &emul_hpux) {
4121.1Sdbj			ucode = HPUX_ILL_ILLINST_TRAP;
4131.1Sdbj			i = SIGILL;
4141.1Sdbj			break;
4151.1Sdbj		}
4161.1Sdbj		/* fall through */
4171.1Sdbj#endif
4181.1Sdbj	case T_PRIVINST|T_USER:	/* privileged instruction fault */
4191.1Sdbj#ifdef COMPAT_HPUX
4201.1Sdbj		if (p->p_emul == &emul_hpux)
4211.1Sdbj			ucode = HPUX_ILL_PRIV_TRAP;
4221.1Sdbj		else
4231.1Sdbj#endif
4241.1Sdbj		ucode = frame.f_format;	/* XXX was ILL_PRIVIN_FAULT */
4251.1Sdbj		i = SIGILL;
4261.1Sdbj		break;
4271.1Sdbj
4281.1Sdbj	case T_ZERODIV|T_USER:	/* Divide by zero */
4291.1Sdbj#ifdef COMPAT_HPUX
4301.1Sdbj		if (p->p_emul == &emul_hpux)
4311.1Sdbj			ucode = HPUX_FPE_INTDIV_TRAP;
4321.1Sdbj		else
4331.1Sdbj#endif
4341.1Sdbj		ucode = frame.f_format;	/* XXX was FPE_INTDIV_TRAP */
4351.1Sdbj		i = SIGFPE;
4361.1Sdbj		break;
4371.1Sdbj
4381.1Sdbj	case T_CHKINST|T_USER:	/* CHK instruction trap */
4391.1Sdbj#ifdef COMPAT_HPUX
4401.1Sdbj		if (p->p_emul == &emul_hpux) {
4411.1Sdbj			/* handled differently under hp-ux */
4421.1Sdbj			i = SIGILL;
4431.1Sdbj			ucode = HPUX_ILL_CHK_TRAP;
4441.1Sdbj			break;
4451.1Sdbj		}
4461.1Sdbj#endif
4471.1Sdbj		ucode = frame.f_format;	/* XXX was FPE_SUBRNG_TRAP */
4481.1Sdbj		i = SIGFPE;
4491.1Sdbj		break;
4501.1Sdbj
4511.1Sdbj	case T_TRAPVINST|T_USER:	/* TRAPV instruction trap */
4521.1Sdbj#ifdef COMPAT_HPUX
4531.1Sdbj		if (p->p_emul == &emul_hpux) {
4541.1Sdbj			/* handled differently under hp-ux */
4551.1Sdbj			i = SIGILL;
4561.1Sdbj			ucode = HPUX_ILL_TRAPV_TRAP;
4571.1Sdbj			break;
4581.1Sdbj		}
4591.1Sdbj#endif
4601.1Sdbj		ucode = frame.f_format;	/* XXX was FPE_INTOVF_TRAP */
4611.1Sdbj		i = SIGFPE;
4621.1Sdbj		break;
4631.1Sdbj
4641.1Sdbj	/*
4651.1Sdbj	 * XXX: Trace traps are a nightmare.
4661.1Sdbj	 *
4671.1Sdbj	 *	HP-UX uses trap #1 for breakpoints,
4681.1Sdbj	 *	NetBSD/m68k uses trap #2,
4691.1Sdbj	 *	SUN 3.x uses trap #15,
4701.1Sdbj	 *	DDB and KGDB uses trap #15 (for kernel breakpoints;
4711.1Sdbj	 *	handled elsewhere).
4721.1Sdbj	 *
4731.1Sdbj	 * NetBSD and HP-UX traps both get mapped by locore.s into T_TRACE.
4741.1Sdbj	 * SUN 3.x traps get passed through as T_TRAP15 and are not really
4751.1Sdbj	 * supported yet.
4761.1Sdbj	 *
4771.1Sdbj	 * XXX: We should never get kernel-mode T_TRACE or T_TRAP15
4781.1Sdbj	 * XXX: because locore.s now gives them special treatment.
4791.1Sdbj	 */
4801.1Sdbj	case T_TRACE:		/* kernel trace trap */
4811.1Sdbj	case T_TRAP15:		/* kernel breakpoint */
4821.1Sdbj#ifdef DEBUG
4831.1Sdbj		printf("unexpected kernel trace trap, type = %d\n", type);
4841.1Sdbj		printf("program counter = 0x%x\n", frame.f_pc);
4851.1Sdbj#endif
4861.1Sdbj		frame.f_sr &= ~PSL_T;
4871.1Sdbj		return;
4881.1Sdbj
4891.1Sdbj	case T_TRACE|T_USER:	/* user trace trap */
4901.1Sdbj	case T_TRAP15|T_USER:	/* SUN user trace trap */
4911.1Sdbj#ifdef COMPAT_SUNOS
4921.1Sdbj		/*
4931.1Sdbj		 * SunOS uses Trap #2 for a "CPU cache flush".
4941.1Sdbj		 * Just flush the on-chip caches and return.
4951.1Sdbj		 */
4961.1Sdbj		if (p->p_emul == &emul_sunos) {
4971.1Sdbj			ICIA();
4981.1Sdbj			DCIU();
4991.1Sdbj			return;
5001.1Sdbj		}
5011.1Sdbj#endif
5021.1Sdbj		frame.f_sr &= ~PSL_T;
5031.1Sdbj		i = SIGTRAP;
5041.1Sdbj		break;
5051.1Sdbj
5061.1Sdbj	case T_ASTFLT:		/* system async trap, cannot happen */
5071.1Sdbj		goto dopanic;
5081.1Sdbj
5091.1Sdbj	case T_ASTFLT|T_USER:	/* user async trap */
5101.1Sdbj		astpending = 0;
5111.1Sdbj		/*
5121.1Sdbj		 * We check for software interrupts first.  This is because
5131.1Sdbj		 * they are at a higher level than ASTs, and on a VAX would
5141.1Sdbj		 * interrupt the AST.  We assume that if we are processing
5151.1Sdbj		 * an AST that we must be at IPL0 so we don't bother to
5161.1Sdbj		 * check.  Note that we ensure that we are at least at SIR
5171.1Sdbj		 * IPL while processing the SIR.
5181.1Sdbj		 */
5191.1Sdbj		spl1();
5201.1Sdbj		/* fall into... */
5211.1Sdbj
5221.1Sdbj	case T_SSIR:		/* software interrupt */
5231.1Sdbj	case T_SSIR|T_USER:
5241.1Sdbj		while (bit = ffs(ssir)) {
5251.1Sdbj			--bit;
5261.1Sdbj			ssir &= ~(1 << bit);
5271.1Sdbj			cnt.v_soft++;
5281.1Sdbj			if (sir_routines[bit])
5291.1Sdbj				sir_routines[bit](sir_args[bit]);
5301.1Sdbj		}
5311.1Sdbj
5321.1Sdbj		/*
5331.1Sdbj		 * If this was not an AST trap, we are all done.
5341.1Sdbj		 */
5351.1Sdbj		if (type != (T_ASTFLT|T_USER)) {
5361.1Sdbj			cnt.v_trap--;
5371.1Sdbj			return;
5381.1Sdbj		}
5391.1Sdbj		spl0();
5401.1Sdbj		if (p->p_flag & P_OWEUPC) {
5411.1Sdbj			p->p_flag &= ~P_OWEUPC;
5421.1Sdbj			ADDUPROF(p);
5431.1Sdbj		}
5441.1Sdbj		goto out;
5451.1Sdbj
5461.1Sdbj	case T_MMUFLT:		/* kernel mode page fault */
5471.1Sdbj		/*
5481.1Sdbj		 * If we were doing profiling ticks or other user mode
5491.1Sdbj		 * stuff from interrupt code, Just Say No.
5501.1Sdbj		 */
5511.1Sdbj		if (p->p_addr->u_pcb.pcb_onfault == fubail ||
5521.1Sdbj		    p->p_addr->u_pcb.pcb_onfault == subail)
5531.1Sdbj			goto copyfault;
5541.1Sdbj		/* fall into ... */
5551.1Sdbj
5561.1Sdbj	case T_MMUFLT|T_USER:	/* page fault */
5571.1Sdbj	    {
5581.1Sdbj		vm_offset_t va;
5591.1Sdbj		struct vmspace *vm = p->p_vmspace;
5601.1Sdbj		vm_map_t map;
5611.1Sdbj		int rv;
5621.1Sdbj		vm_prot_t ftype;
5631.1Sdbj		extern vm_map_t kernel_map;
5641.1Sdbj
5651.1Sdbj#ifdef DEBUG
5661.1Sdbj		if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
5671.1Sdbj		printf("trap: T_MMUFLT pid=%d, code=%x, v=%x, pc=%x, sr=%x\n",
5681.1Sdbj		       p->p_pid, code, v, frame.f_pc, frame.f_sr);
5691.1Sdbj#endif
5701.1Sdbj		/*
5711.1Sdbj		 * It is only a kernel address space fault iff:
5721.1Sdbj		 * 	1. (type & T_USER) == 0  and
5731.1Sdbj		 * 	2. pcb_onfault not set or
5741.1Sdbj		 *	3. pcb_onfault set but supervisor space data fault
5751.1Sdbj		 * The last can occur during an exec() copyin where the
5761.1Sdbj		 * argument space is lazy-allocated.
5771.1Sdbj		 */
5781.1Sdbj		if ((type & T_USER) == 0 &&
5791.1Sdbj		    ((p->p_addr->u_pcb.pcb_onfault == 0) || KDFAULT(code)))
5801.1Sdbj			map = kernel_map;
5811.1Sdbj		else
5821.1Sdbj			map = vm ? &vm->vm_map : kernel_map;
5831.1Sdbj
5841.1Sdbj		if (WRFAULT(code))
5851.1Sdbj			ftype = VM_PROT_READ | VM_PROT_WRITE;
5861.1Sdbj		else
5871.1Sdbj			ftype = VM_PROT_READ;
5881.1Sdbj
5891.1Sdbj		va = trunc_page((vm_offset_t)v);
5901.1Sdbj
5911.1Sdbj		if (map == kernel_map && va == 0) {
5921.1Sdbj			printf("trap: bad kernel %s access at 0x%x\n",
5931.1Sdbj			    (ftype & VM_PROT_WRITE) ? "read/write" :
5941.1Sdbj			    "read", v);
5951.1Sdbj			goto dopanic;
5961.1Sdbj		}
5971.1Sdbj
5981.1Sdbj#ifdef COMPAT_HPUX
5991.1Sdbj		if (ISHPMMADDR(va)) {
6001.1Sdbj			int pmap_mapmulti __P((pmap_t, vm_offset_t));
6011.1Sdbj			vm_offset_t bva;
6021.1Sdbj
6031.1Sdbj			rv = pmap_mapmulti(map->pmap, va);
6041.1Sdbj			if (rv != KERN_SUCCESS) {
6051.1Sdbj				bva = HPMMBASEADDR(va);
6061.1Sdbj				rv = vm_fault(map, bva, ftype, FALSE);
6071.1Sdbj				if (rv == KERN_SUCCESS)
6081.1Sdbj					(void) pmap_mapmulti(map->pmap, va);
6091.1Sdbj			}
6101.1Sdbj		} else
6111.1Sdbj#endif
6121.1Sdbj		rv = vm_fault(map, va, ftype, FALSE);
6131.1Sdbj#ifdef DEBUG
6141.1Sdbj		if (rv && MDB_ISPID(p->p_pid))
6151.1Sdbj			printf("vm_fault(%p, %lx, %x, 0) -> %x\n",
6161.1Sdbj			       map, va, ftype, rv);
6171.1Sdbj#endif
6181.1Sdbj		/*
6191.1Sdbj		 * If this was a stack access we keep track of the maximum
6201.1Sdbj		 * accessed stack size.  Also, if vm_fault gets a protection
6211.1Sdbj		 * failure it is due to accessing the stack region outside
6221.1Sdbj		 * the current limit and we need to reflect that as an access
6231.1Sdbj		 * error.
6241.1Sdbj		 */
6251.1Sdbj		if ((vm != NULL && (caddr_t)va >= vm->vm_maxsaddr)
6261.1Sdbj		    && map != kernel_map) {
6271.1Sdbj			if (rv == KERN_SUCCESS) {
6281.1Sdbj				unsigned nss;
6291.1Sdbj
6301.1Sdbj				nss = clrnd(btoc(USRSTACK-(unsigned)va));
6311.1Sdbj				if (nss > vm->vm_ssize)
6321.1Sdbj					vm->vm_ssize = nss;
6331.1Sdbj			} else if (rv == KERN_PROTECTION_FAILURE)
6341.1Sdbj				rv = KERN_INVALID_ADDRESS;
6351.1Sdbj		}
6361.1Sdbj		if (rv == KERN_SUCCESS) {
6371.1Sdbj			if (type == T_MMUFLT) {
6381.1Sdbj#ifdef M68040
6391.1Sdbj				if (cputype == CPU_68040)
6401.1Sdbj					(void) writeback(&frame, 1);
6411.1Sdbj#endif
6421.1Sdbj				return;
6431.1Sdbj			}
6441.1Sdbj			goto out;
6451.1Sdbj		}
6461.1Sdbj		if (type == T_MMUFLT) {
6471.1Sdbj			if (p->p_addr->u_pcb.pcb_onfault)
6481.1Sdbj				goto copyfault;
6491.1Sdbj			printf("vm_fault(%p, %lx, %x, 0) -> %x\n",
6501.1Sdbj			       map, va, ftype, rv);
6511.1Sdbj			printf("  type %x, code [mmu,,ssw]: %x\n",
6521.1Sdbj			       type, code);
6531.1Sdbj			goto dopanic;
6541.1Sdbj		}
6551.1Sdbj		ucode = v;
6561.1Sdbj		i = SIGSEGV;
6571.1Sdbj		break;
6581.1Sdbj	    }
6591.1Sdbj	}
6601.1Sdbj	trapsignal(p, i, ucode);
6611.1Sdbj	if ((type & T_USER) == 0)
6621.1Sdbj		return;
6631.1Sdbjout:
6641.1Sdbj	userret(p, &frame, sticks, v, 1);
6651.1Sdbj}
6661.1Sdbj
6671.1Sdbj#ifdef M68040
6681.1Sdbj#ifdef DEBUG
6691.1Sdbjstruct writebackstats {
6701.1Sdbj	int calls;
6711.1Sdbj	int cpushes;
6721.1Sdbj	int move16s;
6731.1Sdbj	int wb1s, wb2s, wb3s;
6741.1Sdbj	int wbsize[4];
6751.1Sdbj} wbstats;
6761.1Sdbj
6771.1Sdbjchar *f7sz[] = { "longword", "byte", "word", "line" };
6781.1Sdbjchar *f7tt[] = { "normal", "MOVE16", "AFC", "ACK" };
6791.1Sdbjchar *f7tm[] = { "d-push", "u-data", "u-code", "M-data",
6801.1Sdbj		 "M-code", "k-data", "k-code", "RES" };
6811.1Sdbjchar wberrstr[] =
6821.1Sdbj    "WARNING: pid %d(%s) writeback [%s] failed, pc=%x fa=%x wba=%x wbd=%x\n";
6831.1Sdbj#endif
6841.1Sdbj
6851.1Sdbjint
6861.1Sdbjwriteback(fp, docachepush)
6871.1Sdbj	struct frame *fp;
6881.1Sdbj	int docachepush;
6891.1Sdbj{
6901.1Sdbj	struct fmt7 *f = &fp->f_fmt7;
6911.1Sdbj	struct proc *p = curproc;
6921.1Sdbj	int err = 0;
6931.1Sdbj	u_int fa;
6941.1Sdbj	caddr_t oonfault = p->p_addr->u_pcb.pcb_onfault;
6951.1Sdbj
6961.1Sdbj#ifdef DEBUG
6971.1Sdbj	if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid)) {
6981.1Sdbj		printf(" pid=%d, fa=%x,", p->p_pid, f->f_fa);
6991.1Sdbj		dumpssw(f->f_ssw);
7001.1Sdbj	}
7011.1Sdbj	wbstats.calls++;
7021.1Sdbj#endif
7031.1Sdbj	/*
7041.1Sdbj	 * Deal with special cases first.
7051.1Sdbj	 */
7061.1Sdbj	if ((f->f_ssw & SSW4_TMMASK) == SSW4_TMDCP) {
7071.1Sdbj		/*
7081.1Sdbj		 * Dcache push fault.
7091.1Sdbj		 * Line-align the address and write out the push data to
7101.1Sdbj		 * the indicated physical address.
7111.1Sdbj		 */
7121.1Sdbj#ifdef DEBUG
7131.1Sdbj		if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid)) {
7141.1Sdbj			printf(" pushing %s to PA %x, data %x",
7151.1Sdbj			       f7sz[(f->f_ssw & SSW4_SZMASK) >> 5],
7161.1Sdbj			       f->f_fa, f->f_pd0);
7171.1Sdbj			if ((f->f_ssw & SSW4_SZMASK) == SSW4_SZLN)
7181.1Sdbj				printf("/%x/%x/%x",
7191.1Sdbj				       f->f_pd1, f->f_pd2, f->f_pd3);
7201.1Sdbj			printf("\n");
7211.1Sdbj		}
7221.1Sdbj		if (f->f_wb1s & SSW4_WBSV)
7231.1Sdbj			panic("writeback: cache push with WB1S valid");
7241.1Sdbj		wbstats.cpushes++;
7251.1Sdbj#endif
7261.1Sdbj		/*
7271.1Sdbj		 * XXX there are security problems if we attempt to do a
7281.1Sdbj		 * cache push after a signal handler has been called.
7291.1Sdbj		 */
7301.1Sdbj		if (docachepush) {
7311.1Sdbj			pmap_enter(pmap_kernel(), (vm_offset_t)vmmap,
7321.1Sdbj				   trunc_page(f->f_fa), VM_PROT_WRITE, TRUE);
7331.1Sdbj			fa = (u_int)&vmmap[(f->f_fa & PGOFSET) & ~0xF];
7341.1Sdbj			bcopy((caddr_t)&f->f_pd0, (caddr_t)fa, 16);
7351.1Sdbj			DCFL(pmap_extract(pmap_kernel(), (vm_offset_t)fa));
7361.1Sdbj			pmap_remove(pmap_kernel(), (vm_offset_t)vmmap,
7371.1Sdbj				    (vm_offset_t)&vmmap[NBPG]);
7381.1Sdbj		} else
7391.1Sdbj			printf("WARNING: pid %d(%s) uid %d: CPUSH not done\n",
7401.1Sdbj			       p->p_pid, p->p_comm, p->p_ucred->cr_uid);
7411.1Sdbj	} else if ((f->f_ssw & (SSW4_RW|SSW4_TTMASK)) == SSW4_TTM16) {
7421.1Sdbj		/*
7431.1Sdbj		 * MOVE16 fault.
7441.1Sdbj		 * Line-align the address and write out the push data to
7451.1Sdbj		 * the indicated virtual address.
7461.1Sdbj		 */
7471.1Sdbj#ifdef DEBUG
7481.1Sdbj		if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
7491.1Sdbj			printf(" MOVE16 to VA %x(%x), data %x/%x/%x/%x\n",
7501.1Sdbj			       f->f_fa, f->f_fa & ~0xF, f->f_pd0, f->f_pd1,
7511.1Sdbj			       f->f_pd2, f->f_pd3);
7521.1Sdbj		if (f->f_wb1s & SSW4_WBSV)
7531.1Sdbj			panic("writeback: MOVE16 with WB1S valid");
7541.1Sdbj		wbstats.move16s++;
7551.1Sdbj#endif
7561.1Sdbj		if (KDFAULT(f->f_wb1s))
7571.1Sdbj			bcopy((caddr_t)&f->f_pd0, (caddr_t)(f->f_fa & ~0xF), 16);
7581.1Sdbj		else
7591.1Sdbj			err = suline((caddr_t)(f->f_fa & ~0xF), (caddr_t)&f->f_pd0);
7601.1Sdbj		if (err) {
7611.1Sdbj			fa = f->f_fa & ~0xF;
7621.1Sdbj#ifdef DEBUG
7631.1Sdbj			if (mmudebug & MDB_WBFAILED)
7641.1Sdbj				printf(wberrstr, p->p_pid, p->p_comm,
7651.1Sdbj				       "MOVE16", fp->f_pc, f->f_fa,
7661.1Sdbj				       f->f_fa & ~0xF, f->f_pd0);
7671.1Sdbj#endif
7681.1Sdbj		}
7691.1Sdbj	} else if (f->f_wb1s & SSW4_WBSV) {
7701.1Sdbj		/*
7711.1Sdbj		 * Writeback #1.
7721.1Sdbj		 * Position the "memory-aligned" data and write it out.
7731.1Sdbj		 */
7741.1Sdbj		u_int wb1d = f->f_wb1d;
7751.1Sdbj		int off;
7761.1Sdbj
7771.1Sdbj#ifdef DEBUG
7781.1Sdbj		if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
7791.1Sdbj			dumpwb(1, f->f_wb1s, f->f_wb1a, f->f_wb1d);
7801.1Sdbj		wbstats.wb1s++;
7811.1Sdbj		wbstats.wbsize[(f->f_wb2s&SSW4_SZMASK)>>5]++;
7821.1Sdbj#endif
7831.1Sdbj		off = (f->f_wb1a & 3) * 8;
7841.1Sdbj		switch (f->f_wb1s & SSW4_SZMASK) {
7851.1Sdbj		case SSW4_SZLW:
7861.1Sdbj			if (off)
7871.1Sdbj				wb1d = (wb1d >> (32 - off)) | (wb1d << off);
7881.1Sdbj			if (KDFAULT(f->f_wb1s))
7891.1Sdbj				*(long *)f->f_wb1a = wb1d;
7901.1Sdbj			else
7911.1Sdbj				err = suword((caddr_t)f->f_wb1a, wb1d);
7921.1Sdbj			break;
7931.1Sdbj		case SSW4_SZB:
7941.1Sdbj			off = 24 - off;
7951.1Sdbj			if (off)
7961.1Sdbj				wb1d >>= off;
7971.1Sdbj			if (KDFAULT(f->f_wb1s))
7981.1Sdbj				*(char *)f->f_wb1a = wb1d;
7991.1Sdbj			else
8001.1Sdbj				err = subyte((caddr_t)f->f_wb1a, wb1d);
8011.1Sdbj			break;
8021.1Sdbj		case SSW4_SZW:
8031.1Sdbj			off = (off + 16) % 32;
8041.1Sdbj			if (off)
8051.1Sdbj				wb1d = (wb1d >> (32 - off)) | (wb1d << off);
8061.1Sdbj			if (KDFAULT(f->f_wb1s))
8071.1Sdbj				*(short *)f->f_wb1a = wb1d;
8081.1Sdbj			else
8091.1Sdbj				err = susword((caddr_t)f->f_wb1a, wb1d);
8101.1Sdbj			break;
8111.1Sdbj		}
8121.1Sdbj		if (err) {
8131.1Sdbj			fa = f->f_wb1a;
8141.1Sdbj#ifdef DEBUG
8151.1Sdbj			if (mmudebug & MDB_WBFAILED)
8161.1Sdbj				printf(wberrstr, p->p_pid, p->p_comm,
8171.1Sdbj				       "#1", fp->f_pc, f->f_fa,
8181.1Sdbj				       f->f_wb1a, f->f_wb1d);
8191.1Sdbj#endif
8201.1Sdbj		}
8211.1Sdbj	}
8221.1Sdbj	/*
8231.1Sdbj	 * Deal with the "normal" writebacks.
8241.1Sdbj	 *
8251.1Sdbj	 * XXX writeback2 is known to reflect a LINE size writeback after
8261.1Sdbj	 * a MOVE16 was already dealt with above.  Ignore it.
8271.1Sdbj	 */
8281.1Sdbj	if (err == 0 && (f->f_wb2s & SSW4_WBSV) &&
8291.1Sdbj	    (f->f_wb2s & SSW4_SZMASK) != SSW4_SZLN) {
8301.1Sdbj#ifdef DEBUG
8311.1Sdbj		if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
8321.1Sdbj			dumpwb(2, f->f_wb2s, f->f_wb2a, f->f_wb2d);
8331.1Sdbj		wbstats.wb2s++;
8341.1Sdbj		wbstats.wbsize[(f->f_wb2s&SSW4_SZMASK)>>5]++;
8351.1Sdbj#endif
8361.1Sdbj		switch (f->f_wb2s & SSW4_SZMASK) {
8371.1Sdbj		case SSW4_SZLW:
8381.1Sdbj			if (KDFAULT(f->f_wb2s))
8391.1Sdbj				*(long *)f->f_wb2a = f->f_wb2d;
8401.1Sdbj			else
8411.1Sdbj				err = suword((caddr_t)f->f_wb2a, f->f_wb2d);
8421.1Sdbj			break;
8431.1Sdbj		case SSW4_SZB:
8441.1Sdbj			if (KDFAULT(f->f_wb2s))
8451.1Sdbj				*(char *)f->f_wb2a = f->f_wb2d;
8461.1Sdbj			else
8471.1Sdbj				err = subyte((caddr_t)f->f_wb2a, f->f_wb2d);
8481.1Sdbj			break;
8491.1Sdbj		case SSW4_SZW:
8501.1Sdbj			if (KDFAULT(f->f_wb2s))
8511.1Sdbj				*(short *)f->f_wb2a = f->f_wb2d;
8521.1Sdbj			else
8531.1Sdbj				err = susword((caddr_t)f->f_wb2a, f->f_wb2d);
8541.1Sdbj			break;
8551.1Sdbj		}
8561.1Sdbj		if (err) {
8571.1Sdbj			fa = f->f_wb2a;
8581.1Sdbj#ifdef DEBUG
8591.1Sdbj			if (mmudebug & MDB_WBFAILED) {
8601.1Sdbj				printf(wberrstr, p->p_pid, p->p_comm,
8611.1Sdbj				       "#2", fp->f_pc, f->f_fa,
8621.1Sdbj				       f->f_wb2a, f->f_wb2d);
8631.1Sdbj				dumpssw(f->f_ssw);
8641.1Sdbj				dumpwb(2, f->f_wb2s, f->f_wb2a, f->f_wb2d);
8651.1Sdbj			}
8661.1Sdbj#endif
8671.1Sdbj		}
8681.1Sdbj	}
8691.1Sdbj	if (err == 0 && (f->f_wb3s & SSW4_WBSV)) {
8701.1Sdbj#ifdef DEBUG
8711.1Sdbj		if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
8721.1Sdbj			dumpwb(3, f->f_wb3s, f->f_wb3a, f->f_wb3d);
8731.1Sdbj		wbstats.wb3s++;
8741.1Sdbj		wbstats.wbsize[(f->f_wb3s&SSW4_SZMASK)>>5]++;
8751.1Sdbj#endif
8761.1Sdbj		switch (f->f_wb3s & SSW4_SZMASK) {
8771.1Sdbj		case SSW4_SZLW:
8781.1Sdbj			if (KDFAULT(f->f_wb3s))
8791.1Sdbj				*(long *)f->f_wb3a = f->f_wb3d;
8801.1Sdbj			else
8811.1Sdbj				err = suword((caddr_t)f->f_wb3a, f->f_wb3d);
8821.1Sdbj			break;
8831.1Sdbj		case SSW4_SZB:
8841.1Sdbj			if (KDFAULT(f->f_wb3s))
8851.1Sdbj				*(char *)f->f_wb3a = f->f_wb3d;
8861.1Sdbj			else
8871.1Sdbj				err = subyte((caddr_t)f->f_wb3a, f->f_wb3d);
8881.1Sdbj			break;
8891.1Sdbj		case SSW4_SZW:
8901.1Sdbj			if (KDFAULT(f->f_wb3s))
8911.1Sdbj				*(short *)f->f_wb3a = f->f_wb3d;
8921.1Sdbj			else
8931.1Sdbj				err = susword((caddr_t)f->f_wb3a, f->f_wb3d);
8941.1Sdbj			break;
8951.1Sdbj#ifdef DEBUG
8961.1Sdbj		case SSW4_SZLN:
8971.1Sdbj			panic("writeback: wb3s indicates LINE write");
8981.1Sdbj#endif
8991.1Sdbj		}
9001.1Sdbj		if (err) {
9011.1Sdbj			fa = f->f_wb3a;
9021.1Sdbj#ifdef DEBUG
9031.1Sdbj			if (mmudebug & MDB_WBFAILED)
9041.1Sdbj				printf(wberrstr, p->p_pid, p->p_comm,
9051.1Sdbj				       "#3", fp->f_pc, f->f_fa,
9061.1Sdbj				       f->f_wb3a, f->f_wb3d);
9071.1Sdbj#endif
9081.1Sdbj		}
9091.1Sdbj	}
9101.1Sdbj	p->p_addr->u_pcb.pcb_onfault = oonfault;
9111.1Sdbj	if (err)
9121.1Sdbj		err = SIGSEGV;
9131.1Sdbj	return (err);
9141.1Sdbj}
9151.1Sdbj
9161.1Sdbj#ifdef DEBUG
9171.1Sdbjvoid
9181.1Sdbjdumpssw(ssw)
9191.1Sdbj	u_short ssw;
9201.1Sdbj{
9211.1Sdbj	printf(" SSW: %x: ", ssw);
9221.1Sdbj	if (ssw & SSW4_CP)
9231.1Sdbj		printf("CP,");
9241.1Sdbj	if (ssw & SSW4_CU)
9251.1Sdbj		printf("CU,");
9261.1Sdbj	if (ssw & SSW4_CT)
9271.1Sdbj		printf("CT,");
9281.1Sdbj	if (ssw & SSW4_CM)
9291.1Sdbj		printf("CM,");
9301.1Sdbj	if (ssw & SSW4_MA)
9311.1Sdbj		printf("MA,");
9321.1Sdbj	if (ssw & SSW4_ATC)
9331.1Sdbj		printf("ATC,");
9341.1Sdbj	if (ssw & SSW4_LK)
9351.1Sdbj		printf("LK,");
9361.1Sdbj	if (ssw & SSW4_RW)
9371.1Sdbj		printf("RW,");
9381.1Sdbj	printf(" SZ=%s, TT=%s, TM=%s\n",
9391.1Sdbj	       f7sz[(ssw & SSW4_SZMASK) >> 5],
9401.1Sdbj	       f7tt[(ssw & SSW4_TTMASK) >> 3],
9411.1Sdbj	       f7tm[ssw & SSW4_TMMASK]);
9421.1Sdbj}
9431.1Sdbj
9441.1Sdbjvoid
9451.1Sdbjdumpwb(num, s, a, d)
9461.1Sdbj	int num;
9471.1Sdbj	u_short s;
9481.1Sdbj	u_int a, d;
9491.1Sdbj{
9501.1Sdbj	struct proc *p = curproc;
9511.1Sdbj	vm_offset_t pa;
9521.1Sdbj
9531.1Sdbj	printf(" writeback #%d: VA %x, data %x, SZ=%s, TT=%s, TM=%s\n",
9541.1Sdbj	       num, a, d, f7sz[(s & SSW4_SZMASK) >> 5],
9551.1Sdbj	       f7tt[(s & SSW4_TTMASK) >> 3], f7tm[s & SSW4_TMMASK]);
9561.1Sdbj	printf("               PA ");
9571.1Sdbj	pa = pmap_extract(p->p_vmspace->vm_map.pmap, (vm_offset_t)a);
9581.1Sdbj	if (pa == 0)
9591.1Sdbj		printf("<invalid address>");
9601.1Sdbj	else
9611.1Sdbj		printf("%lx, current value %lx", pa, fuword((caddr_t)a));
9621.1Sdbj	printf("\n");
9631.1Sdbj}
9641.1Sdbj#endif
9651.1Sdbj#endif
9661.1Sdbj
9671.1Sdbj/*
9681.1Sdbj * Process a system call.
9691.1Sdbj */
9701.1Sdbjvoid
9711.1Sdbjsyscall(code, frame)
9721.1Sdbj	register_t code;
9731.1Sdbj	struct frame frame;
9741.1Sdbj{
9751.1Sdbj	caddr_t params;
9761.1Sdbj	struct sysent *callp;
9771.1Sdbj	struct proc *p;
9781.1Sdbj	int error, opc, nsys;
9791.1Sdbj	size_t argsize;
9801.1Sdbj	register_t args[8], rval[2];
9811.1Sdbj	u_quad_t sticks;
9821.1Sdbj
9831.1Sdbj	cnt.v_syscall++;
9841.1Sdbj	if (!USERMODE(frame.f_sr))
9851.1Sdbj		panic("syscall");
9861.1Sdbj	p = curproc;
9871.1Sdbj	sticks = p->p_sticks;
9881.1Sdbj	p->p_md.md_regs = frame.f_regs;
9891.1Sdbj	opc = frame.f_pc;
9901.1Sdbj
9911.1Sdbj	nsys = p->p_emul->e_nsysent;
9921.1Sdbj	callp = p->p_emul->e_sysent;
9931.1Sdbj
9941.1Sdbj#ifdef COMPAT_SUNOS
9951.1Sdbj	if (p->p_emul == &emul_sunos) {
9961.1Sdbj		/*
9971.1Sdbj		 * SunOS passes the syscall-number on the stack, whereas
9981.1Sdbj		 * BSD passes it in D0. So, we have to get the real "code"
9991.1Sdbj		 * from the stack, and clean up the stack, as SunOS glue
10001.1Sdbj		 * code assumes the kernel pops the syscall argument the
10011.1Sdbj		 * glue pushed on the stack. Sigh...
10021.1Sdbj		 */
10031.1Sdbj		code = fuword((caddr_t)frame.f_regs[SP]);
10041.1Sdbj
10051.1Sdbj		/*
10061.1Sdbj		 * XXX
10071.1Sdbj		 * Don't do this for sunos_sigreturn, as there's no stored pc
10081.1Sdbj		 * on the stack to skip, the argument follows the syscall
10091.1Sdbj		 * number without a gap.
10101.1Sdbj		 */
10111.1Sdbj		if (code != SUNOS_SYS_sigreturn) {
10121.1Sdbj			frame.f_regs[SP] += sizeof (int);
10131.1Sdbj			/*
10141.1Sdbj			 * remember that we adjusted the SP,
10151.1Sdbj			 * might have to undo this if the system call
10161.1Sdbj			 * returns ERESTART.
10171.1Sdbj			 */
10181.1Sdbj			p->p_md.md_flags |= MDP_STACKADJ;
10191.1Sdbj		} else
10201.1Sdbj			p->p_md.md_flags &= ~MDP_STACKADJ;
10211.1Sdbj	}
10221.1Sdbj#endif
10231.1Sdbj
10241.1Sdbj	params = (caddr_t)frame.f_regs[SP] + sizeof(int);
10251.1Sdbj
10261.1Sdbj	switch (code) {
10271.1Sdbj	case SYS_syscall:
10281.1Sdbj		/*
10291.1Sdbj		 * Code is first argument, followed by actual args.
10301.1Sdbj		 */
10311.1Sdbj		code = fuword(params);
10321.1Sdbj		params += sizeof(int);
10331.1Sdbj		/*
10341.1Sdbj		 * XXX sigreturn requires special stack manipulation
10351.1Sdbj		 * that is only done if entered via the sigreturn
10361.1Sdbj		 * trap.  Cannot allow it here so make sure we fail.
10371.1Sdbj		 */
10381.1Sdbj		if (code == SYS_sigreturn)
10391.1Sdbj			code = nsys;
10401.1Sdbj		break;
10411.1Sdbj	case SYS___syscall:
10421.1Sdbj		/*
10431.1Sdbj		 * Like syscall, but code is a quad, so as to maintain
10441.1Sdbj		 * quad alignment for the rest of the arguments.
10451.1Sdbj		 */
10461.1Sdbj		if (callp != sysent)
10471.1Sdbj			break;
10481.1Sdbj		code = fuword(params + _QUAD_LOWWORD * sizeof(int));
10491.1Sdbj		params += sizeof(quad_t);
10501.1Sdbj		break;
10511.1Sdbj	default:
10521.1Sdbj		break;
10531.1Sdbj	}
10541.1Sdbj	if (code < 0 || code >= nsys)
10551.1Sdbj		callp += p->p_emul->e_nosys;		/* illegal */
10561.1Sdbj	else
10571.1Sdbj		callp += code;
10581.1Sdbj	argsize = callp->sy_argsize;
10591.1Sdbj	if (argsize)
10601.1Sdbj		error = copyin(params, (caddr_t)args, argsize);
10611.1Sdbj	else
10621.1Sdbj		error = 0;
10631.1Sdbj#ifdef SYSCALL_DEBUG
10641.1Sdbj	scdebug_call(p, code, args);
10651.1Sdbj#endif
10661.1Sdbj#ifdef KTRACE
10671.1Sdbj	if (KTRPOINT(p, KTR_SYSCALL))
10681.1Sdbj		ktrsyscall(p->p_tracep, code, argsize, args);
10691.1Sdbj#endif
10701.1Sdbj	if (error)
10711.1Sdbj		goto bad;
10721.1Sdbj	rval[0] = 0;
10731.1Sdbj	rval[1] = frame.f_regs[D1];
10741.1Sdbj	error = (*callp->sy_call)(p, args, rval);
10751.1Sdbj	switch (error) {
10761.1Sdbj	case 0:
10771.1Sdbj		frame.f_regs[D0] = rval[0];
10781.1Sdbj		frame.f_regs[D1] = rval[1];
10791.1Sdbj		frame.f_sr &= ~PSL_C;	/* carry bit */
10801.1Sdbj		break;
10811.1Sdbj	case ERESTART:
10821.1Sdbj		/*
10831.1Sdbj		 * We always enter through a `trap' instruction, which is 2
10841.1Sdbj		 * bytes, so adjust the pc by that amount.
10851.1Sdbj		 */
10861.1Sdbj		frame.f_pc = opc - 2;
10871.1Sdbj		break;
10881.1Sdbj	case EJUSTRETURN:
10891.1Sdbj		/* nothing to do */
10901.1Sdbj		break;
10911.1Sdbj	default:
10921.1Sdbj	bad:
10931.1Sdbj		if (p->p_emul->e_errno)
10941.1Sdbj			error = p->p_emul->e_errno[error];
10951.1Sdbj		frame.f_regs[D0] = error;
10961.1Sdbj		frame.f_sr |= PSL_C;	/* carry bit */
10971.1Sdbj		break;
10981.1Sdbj	}
10991.1Sdbj
11001.1Sdbj#ifdef SYSCALL_DEBUG
11011.1Sdbj	scdebug_ret(p, code, error, rval);
11021.1Sdbj#endif
11031.1Sdbj#ifdef COMPAT_SUNOS
11041.1Sdbj	/* need new p-value for this */
11051.1Sdbj	if (error == ERESTART && (p->p_md.md_flags & MDP_STACKADJ))
11061.1Sdbj		frame.f_regs[SP] -= sizeof (int);
11071.1Sdbj#endif
11081.1Sdbj	userret(p, &frame, sticks, (u_int)0, 0);
11091.1Sdbj#ifdef KTRACE
11101.1Sdbj	if (KTRPOINT(p, KTR_SYSRET))
11111.1Sdbj		ktrsysret(p->p_tracep, code, error, rval[0]);
11121.1Sdbj#endif
11131.1Sdbj}
11141.1Sdbj
11151.1Sdbjvoid
11161.1Sdbjchild_return(p, frame)
11171.1Sdbj	struct proc *p;
11181.1Sdbj	struct frame frame;
11191.1Sdbj{
11201.1Sdbj
11211.1Sdbj	frame.f_regs[D0] = 0;
11221.1Sdbj	frame.f_sr &= ~PSL_C;
11231.1Sdbj	frame.f_format = FMT0;
11241.1Sdbj
11251.1Sdbj	userret(p, &frame, 0, (u_int)0, 0);
11261.1Sdbj#ifdef KTRACE
11271.1Sdbj	if (KTRPOINT(p, KTR_SYSRET))
11281.1Sdbj		ktrsysret(p->p_tracep, SYS_fork, 0, 0);
11291.1Sdbj#endif
11301.1Sdbj}
11311.1Sdbj
11321.1Sdbj/*
11331.1Sdbj * Allocation routines for software interrupts.
11341.1Sdbj */
11351.1Sdbju_long
11361.1Sdbjallocate_sir(proc, arg)
11371.1Sdbj	void (*proc)();
11381.1Sdbj	void *arg;
11391.1Sdbj{
11401.1Sdbj	int bit;
11411.1Sdbj
11421.1Sdbj	if( next_sir >= NSIR )
11431.1Sdbj		panic("allocate_sir: none left");
11441.1Sdbj	bit = next_sir++;
11451.1Sdbj	sir_routines[bit] = proc;
11461.1Sdbj	sir_args[bit] = arg;
11471.1Sdbj	return (1 << bit);
11481.1Sdbj}
11491.1Sdbj
11501.1Sdbjvoid
11511.1Sdbjinit_sir()
11521.1Sdbj{
11531.1Sdbj	extern void netintr();
11541.1Sdbj
11551.1Sdbj	sir_routines[0] = netintr;
11561.1Sdbj	sir_routines[1] = softclock;
11571.1Sdbj	next_sir = 2;
11581.1Sdbj}
1159