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