trap.c revision 1.13
11.13Smycroft/*	$NetBSD: trap.c,v 1.13 1999/03/26 23:41:32 mycroft Exp $ */
21.7Sdbj
31.7Sdbj/*
41.10Sabs * This file was taken from mvme68k/mvme68k/trap.c
51.7Sdbj * should probably be re-synced when needed.
61.7Sdbj * Darrin B. Jewell <jewell@mit.edu>  Tue Nov 10 05:07:16 1998
71.7Sdbj * original cvs id: NetBSD: trap.c,v 1.24 1998/10/01 02:53:54 thorpej Exp
81.7Sdbj */
91.1Sdbj
101.1Sdbj/*
111.1Sdbj * Copyright (c) 1988 University of Utah.
121.1Sdbj * Copyright (c) 1982, 1986, 1990, 1993
131.1Sdbj *	The Regents of the University of California.  All rights reserved.
141.1Sdbj *
151.1Sdbj * This code is derived from software contributed to Berkeley by
161.1Sdbj * the Systems Programming Group of the University of Utah Computer
171.1Sdbj * Science Department.
181.1Sdbj *
191.1Sdbj * Redistribution and use in source and binary forms, with or without
201.1Sdbj * modification, are permitted provided that the following conditions
211.1Sdbj * are met:
221.1Sdbj * 1. Redistributions of source code must retain the above copyright
231.1Sdbj *    notice, this list of conditions and the following disclaimer.
241.1Sdbj * 2. Redistributions in binary form must reproduce the above copyright
251.1Sdbj *    notice, this list of conditions and the following disclaimer in the
261.1Sdbj *    documentation and/or other materials provided with the distribution.
271.1Sdbj * 3. All advertising materials mentioning features or use of this software
281.1Sdbj *    must display the following acknowledgement:
291.1Sdbj *	This product includes software developed by the University of
301.1Sdbj *	California, Berkeley and its contributors.
311.1Sdbj * 4. Neither the name of the University nor the names of its contributors
321.1Sdbj *    may be used to endorse or promote products derived from this software
331.1Sdbj *    without specific prior written permission.
341.1Sdbj *
351.1Sdbj * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
361.1Sdbj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
371.1Sdbj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
381.1Sdbj * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
391.1Sdbj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
401.1Sdbj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
411.1Sdbj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
421.1Sdbj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
431.1Sdbj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
441.1Sdbj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
451.1Sdbj * SUCH DAMAGE.
461.1Sdbj *
471.1Sdbj * from: Utah $Hdr: trap.c 1.37 92/12/20$
481.1Sdbj *
491.1Sdbj *	@(#)trap.c	8.5 (Berkeley) 1/4/94
501.1Sdbj */
511.2Sthorpej
521.5Sjonathan#include "opt_ddb.h"
531.9Sitohy#include "opt_execfmt.h"
541.2Sthorpej#include "opt_ktrace.h"
551.6Sthorpej#include "opt_compat_netbsd.h"
561.3Sthorpej#include "opt_compat_sunos.h"
571.4Sthorpej#include "opt_compat_hpux.h"
581.9Sitohy#include "opt_compat_linux.h"
591.1Sdbj
601.1Sdbj#include <sys/param.h>
611.1Sdbj#include <sys/systm.h>
621.1Sdbj#include <sys/proc.h>
631.1Sdbj#include <sys/acct.h>
641.1Sdbj#include <sys/kernel.h>
651.1Sdbj#include <sys/signalvar.h>
661.1Sdbj#include <sys/resourcevar.h>
671.1Sdbj#include <sys/syscall.h>
681.1Sdbj#include <sys/syslog.h>
691.1Sdbj#include <sys/user.h>
701.1Sdbj#ifdef KTRACE
711.1Sdbj#include <sys/ktrace.h>
721.1Sdbj#endif
731.1Sdbj
741.1Sdbj#include <machine/psl.h>
751.1Sdbj#include <machine/trap.h>
761.1Sdbj#include <machine/cpu.h>
771.1Sdbj#include <machine/reg.h>
781.1Sdbj
791.1Sdbj#include <vm/vm.h>
801.1Sdbj#include <vm/pmap.h>
811.1Sdbj
821.7Sdbj#include <uvm/uvm_extern.h>
831.1Sdbj
841.1Sdbj#ifdef COMPAT_HPUX
851.1Sdbj#include <compat/hpux/hpux.h>
861.1Sdbj#endif
871.1Sdbj
881.1Sdbj#ifdef COMPAT_SUNOS
891.1Sdbj#include <compat/sunos/sunos_syscall.h>
901.1Sdbjextern struct emul emul_sunos;
911.1Sdbj#endif
921.1Sdbj
931.9Sitohy#ifdef COMPAT_LINUX
941.9Sitohy#ifdef EXEC_AOUT
951.9Sitohyextern struct emul emul_linux_aout;
961.9Sitohy#endif
971.9Sitohy#ifdef EXEC_ELF32
981.9Sitohyextern struct emul emul_linux_elf32;
991.9Sitohy#endif
1001.9Sitohy#endif
1011.9Sitohy
1021.7Sdbj#include <m68k/cacheops.h>
1031.1Sdbj
1041.7Sdbjint	astpending;
1051.1Sdbj
1061.1Sdbjchar	*trap_type[] = {
1071.1Sdbj	"Bus error",
1081.1Sdbj	"Address error",
1091.1Sdbj	"Illegal instruction",
1101.1Sdbj	"Zero divide",
1111.1Sdbj	"CHK instruction",
1121.1Sdbj	"TRAPV instruction",
1131.1Sdbj	"Privilege violation",
1141.1Sdbj	"Trace trap",
1151.1Sdbj	"MMU fault",
1161.1Sdbj	"SSIR trap",
1171.1Sdbj	"Format error",
1181.1Sdbj	"68881 exception",
1191.1Sdbj	"Coprocessor violation",
1201.1Sdbj	"Async system trap"
1211.1Sdbj};
1221.1Sdbjint	trap_types = sizeof trap_type / sizeof trap_type[0];
1231.1Sdbj
1241.1Sdbj/*
1251.1Sdbj * Size of various exception stack frames (minus the standard 8 bytes)
1261.1Sdbj */
1271.1Sdbjshort	exframesize[] = {
1281.7Sdbj	FMT0SIZE,	/* type 0 - normal (68020/030/040) */
1291.1Sdbj	FMT1SIZE,	/* type 1 - throwaway (68020/030/040) */
1301.7Sdbj	FMT2SIZE,	/* type 2 - normal 6-word (68020/030/040) */
1311.7Sdbj	FMT3SIZE,	/* type 3 - FP post-instruction (68040) */
1321.7Sdbj	-1, -1, -1,	/* type 4-6 - undefined */
1331.1Sdbj	FMT7SIZE,	/* type 7 - access error (68040) */
1341.1Sdbj	58,		/* type 8 - bus fault (68010) */
1351.1Sdbj	FMT9SIZE,	/* type 9 - coprocessor mid-instruction (68020/030) */
1361.1Sdbj	FMTASIZE,	/* type A - short bus fault (68020/030) */
1371.1Sdbj	FMTBSIZE,	/* type B - long bus fault (68020/030) */
1381.1Sdbj	-1, -1, -1, -1	/* type C-F - undefined */
1391.1Sdbj};
1401.1Sdbj
1411.1Sdbj#ifdef M68040
1421.7Sdbj#define KDFAULT(c)    (mmutype == MMU_68040 ? \
1431.7Sdbj			    ((c) & SSW4_TMMASK) == SSW4_TMKD : \
1441.7Sdbj			    ((c) & (SSW_DF|FC_SUPERD)) == (SSW_DF|FC_SUPERD))
1451.7Sdbj#define WRFAULT(c)    (mmutype == MMU_68040 ? \
1461.7Sdbj			    ((c) & SSW4_RW) == 0 : \
1471.7Sdbj			    ((c) & (SSW_DF|SSW_RW)) == SSW_DF)
1481.1Sdbj#else
1491.7Sdbj#define KDFAULT(c)	(((c) & (SSW_DF|SSW_FCMASK)) == (SSW_DF|FC_SUPERD))
1501.7Sdbj#define WRFAULT(c)	(((c) & (SSW_DF|SSW_RW)) == SSW_DF)
1511.1Sdbj#endif
1521.1Sdbj
1531.1Sdbj#ifdef DEBUG
1541.1Sdbjint mmudebug = 0;
1551.1Sdbjint mmupid = -1;
1561.1Sdbj#define MDB_FOLLOW	1
1571.1Sdbj#define MDB_WBFOLLOW	2
1581.1Sdbj#define MDB_WBFAILED	4
1591.7Sdbj#define MDB_ISPID(p)	(p) == mmupid
1601.1Sdbj#endif
1611.1Sdbj
1621.1Sdbj#define NSIR	32
1631.1Sdbjvoid (*sir_routines[NSIR])();
1641.1Sdbjvoid *sir_args[NSIR];
1651.1Sdbjint next_sir;
1661.1Sdbj
1671.1Sdbj/*
1681.1Sdbj * trap and syscall both need the following work done before returning
1691.1Sdbj * to user mode.
1701.1Sdbj */
1711.1Sdbjstatic inline void
1721.1Sdbjuserret(p, fp, oticks, faultaddr, fromtrap)
1731.1Sdbj	struct proc *p;
1741.1Sdbj	struct frame *fp;
1751.1Sdbj	u_quad_t oticks;
1761.1Sdbj	u_int faultaddr;
1771.1Sdbj	int fromtrap;
1781.1Sdbj{
1791.1Sdbj	int sig, s;
1801.1Sdbj#ifdef M68040
1811.1Sdbj	int beenhere = 0;
1821.1Sdbj
1831.1Sdbjagain:
1841.1Sdbj#endif
1851.1Sdbj	/* take pending signals */
1861.1Sdbj	while ((sig = CURSIG(p)) != 0)
1871.1Sdbj		postsig(sig);
1881.1Sdbj	p->p_priority = p->p_usrpri;
1891.1Sdbj	if (want_resched) {
1901.1Sdbj		/*
1911.1Sdbj		 * Since we are curproc, clock will normally just change
1921.1Sdbj		 * our priority without moving us from one queue to another
1931.1Sdbj		 * (since the running process is not on a queue.)
1941.1Sdbj		 * If that happened after we put ourselves on the run queue
1951.1Sdbj		 * but before we mi_switch()'ed, we might not be on the queue
1961.1Sdbj		 * indicated by our priority.
1971.1Sdbj		 */
1981.1Sdbj		s = splstatclock();
1991.1Sdbj		setrunqueue(p);
2001.1Sdbj		p->p_stats->p_ru.ru_nivcsw++;
2011.1Sdbj		mi_switch();
2021.1Sdbj		splx(s);
2031.1Sdbj		while ((sig = CURSIG(p)) != 0)
2041.1Sdbj			postsig(sig);
2051.1Sdbj	}
2061.1Sdbj
2071.1Sdbj	/*
2081.1Sdbj	 * If profiling, charge system time to the trapped pc.
2091.1Sdbj	 */
2101.1Sdbj	if (p->p_flag & P_PROFIL) {
2111.1Sdbj		extern int psratio;
2121.1Sdbj
2131.1Sdbj		addupc_task(p, fp->f_pc,
2141.1Sdbj			    (int)(p->p_sticks - oticks) * psratio);
2151.1Sdbj	}
2161.1Sdbj#ifdef M68040
2171.1Sdbj	/*
2181.1Sdbj	 * Deal with user mode writebacks (from trap, or from sigreturn).
2191.1Sdbj	 * If any writeback fails, go back and attempt signal delivery.
2201.1Sdbj	 * unless we have already been here and attempted the writeback
2211.1Sdbj	 * (e.g. bad address with user ignoring SIGSEGV).  In that case
2221.1Sdbj	 * we just return to the user without sucessfully completing
2231.1Sdbj	 * the writebacks.  Maybe we should just drop the sucker?
2241.1Sdbj	 */
2251.7Sdbj	if (mmutype == MMU_68040 && fp->f_format == FMT7) {
2261.1Sdbj		if (beenhere) {
2271.1Sdbj#ifdef DEBUG
2281.1Sdbj			if (mmudebug & MDB_WBFAILED)
2291.1Sdbj				printf(fromtrap ?
2301.1Sdbj		"pid %d(%s): writeback aborted, pc=%x, fa=%x\n" :
2311.1Sdbj		"pid %d(%s): writeback aborted in sigreturn, pc=%x\n",
2321.1Sdbj				    p->p_pid, p->p_comm, fp->f_pc, faultaddr);
2331.1Sdbj#endif
2341.7Sdbj		} else if (sig = writeback(fp, fromtrap)) {
2351.1Sdbj			beenhere = 1;
2361.1Sdbj			oticks = p->p_sticks;
2371.1Sdbj			trapsignal(p, sig, faultaddr);
2381.1Sdbj			goto again;
2391.1Sdbj		}
2401.1Sdbj	}
2411.1Sdbj#endif
2421.1Sdbj	curpriority = p->p_priority;
2431.1Sdbj}
2441.1Sdbj
2451.1Sdbj/*
2461.1Sdbj * Trap is called from locore to handle most types of processor traps,
2471.1Sdbj * including events such as simulated software interrupts/AST's.
2481.1Sdbj * System calls are broken out for efficiency.
2491.1Sdbj */
2501.1Sdbj/*ARGSUSED*/
2511.1Sdbjtrap(type, code, v, frame)
2521.1Sdbj	int type;
2531.1Sdbj	unsigned code;
2541.1Sdbj	unsigned v;
2551.1Sdbj	struct frame frame;
2561.1Sdbj{
2571.1Sdbj	extern char fubail[], subail[];
2581.7Sdbj#ifdef DDB
2591.7Sdbj	extern char trap0[], trap1[], trap2[], trap12[], trap15[], illinst[];
2601.7Sdbj#endif
2611.1Sdbj	struct proc *p;
2621.7Sdbj	int i;
2631.1Sdbj	u_int ucode;
2641.7Sdbj	u_quad_t sticks;
2651.7Sdbj#ifdef COMPAT_HPUX
2661.7Sdbj	extern struct emul emul_hpux;
2671.7Sdbj#endif
2681.7Sdbj	int bit;
2691.1Sdbj
2701.7Sdbj	uvmexp.traps++;
2711.1Sdbj	p = curproc;
2721.1Sdbj	ucode = 0;
2731.1Sdbj	if (USERMODE(frame.f_sr)) {
2741.1Sdbj		type |= T_USER;
2751.1Sdbj		sticks = p->p_sticks;
2761.1Sdbj		p->p_md.md_regs = frame.f_regs;
2771.1Sdbj	}
2781.1Sdbj	switch (type) {
2791.1Sdbj
2801.1Sdbj	default:
2811.7Sdbjdopanic:
2821.7Sdbj		printf("trap type %d, code = %x, v = %x\n", type, code, v);
2831.1Sdbj#ifdef DDB
2841.7Sdbj		if (kdb_trap(type, &frame))
2851.7Sdbj			return;
2861.1Sdbj#endif
2871.1Sdbj		regdump((struct trapframe *)&frame, 128);
2881.1Sdbj		type &= ~T_USER;
2891.7Sdbj		if ((unsigned)type < trap_types)
2901.1Sdbj			panic(trap_type[type]);
2911.1Sdbj		panic("trap");
2921.1Sdbj
2931.1Sdbj	case T_BUSERR:		/* kernel bus error */
2941.7Sdbj		if (!p->p_addr->u_pcb.pcb_onfault)
2951.1Sdbj			goto dopanic;
2961.1Sdbj		/*
2971.1Sdbj		 * If we have arranged to catch this fault in any of the
2981.1Sdbj		 * copy to/from user space routines, set PC to return to
2991.1Sdbj		 * indicated location and set flag informing buserror code
3001.1Sdbj		 * that it may need to clean up stack frame.
3011.1Sdbj		 */
3021.7Sdbjcopyfault:
3031.1Sdbj		frame.f_stackadj = exframesize[frame.f_format];
3041.1Sdbj		frame.f_format = frame.f_vector = 0;
3051.1Sdbj		frame.f_pc = (int) p->p_addr->u_pcb.pcb_onfault;
3061.1Sdbj		return;
3071.1Sdbj
3081.1Sdbj	case T_BUSERR|T_USER:	/* bus error */
3091.1Sdbj	case T_ADDRERR|T_USER:	/* address error */
3101.1Sdbj		ucode = v;
3111.1Sdbj		i = SIGBUS;
3121.1Sdbj		break;
3131.1Sdbj
3141.1Sdbj	case T_COPERR:		/* kernel coprocessor violation */
3151.1Sdbj	case T_FMTERR|T_USER:	/* do all RTE errors come in as T_USER? */
3161.1Sdbj	case T_FMTERR:		/* ...just in case... */
3171.1Sdbj	/*
3181.1Sdbj	 * The user has most likely trashed the RTE or FP state info
3191.1Sdbj	 * in the stack frame of a signal handler.
3201.1Sdbj	 */
3211.1Sdbj		printf("pid %d: kernel %s exception\n", p->p_pid,
3221.1Sdbj		       type==T_COPERR ? "coprocessor" : "format");
3231.1Sdbj		type |= T_USER;
3241.6Sthorpej		p->p_sigacts->ps_sigact[SIGILL].sa_handler = SIG_DFL;
3251.6Sthorpej		sigdelset(&p->p_sigignore, SIGILL);
3261.6Sthorpej		sigdelset(&p->p_sigcatch, SIGILL);
3271.6Sthorpej		sigdelset(&p->p_sigmask, SIGILL);
3281.1Sdbj		i = SIGILL;
3291.1Sdbj		ucode = frame.f_format;	/* XXX was ILL_RESAD_FAULT */
3301.1Sdbj		break;
3311.1Sdbj
3321.1Sdbj	case T_COPERR|T_USER:	/* user coprocessor violation */
3331.1Sdbj	/* What is a proper response here? */
3341.1Sdbj		ucode = 0;
3351.1Sdbj		i = SIGFPE;
3361.1Sdbj		break;
3371.1Sdbj
3381.1Sdbj	case T_FPERR|T_USER:	/* 68881 exceptions */
3391.1Sdbj	/*
3401.7Sdbj	 * We pass along the 68881 status register which locore stashed
3411.1Sdbj	 * in code for us.  Note that there is a possibility that the
3421.7Sdbj	 * bit pattern of this register will conflict with one of the
3431.1Sdbj	 * FPE_* codes defined in signal.h.  Fortunately for us, the
3441.1Sdbj	 * only such codes we use are all in the range 1-7 and the low
3451.7Sdbj	 * 3 bits of the status register are defined as 0 so there is
3461.1Sdbj	 * no clash.
3471.1Sdbj	 */
3481.1Sdbj		ucode = code;
3491.1Sdbj		i = SIGFPE;
3501.1Sdbj		break;
3511.1Sdbj
3521.1Sdbj#ifdef M68040
3531.1Sdbj	case T_FPEMULI|T_USER:	/* unimplemented FP instuction */
3541.1Sdbj	case T_FPEMULD|T_USER:	/* unimplemented FP data type */
3551.1Sdbj		/* XXX need to FSAVE */
3561.1Sdbj		printf("pid %d(%s): unimplemented FP %s at %x (EA %x)\n",
3571.1Sdbj		       p->p_pid, p->p_comm,
3581.1Sdbj		       frame.f_format == 2 ? "instruction" : "data type",
3591.1Sdbj		       frame.f_pc, frame.f_fmt2.f_iaddr);
3601.1Sdbj		/* XXX need to FRESTORE */
3611.1Sdbj		i = SIGFPE;
3621.1Sdbj		break;
3631.1Sdbj#endif
3641.1Sdbj
3651.1Sdbj	case T_ILLINST|T_USER:	/* illegal instruction fault */
3661.1Sdbj#ifdef COMPAT_HPUX
3671.1Sdbj		if (p->p_emul == &emul_hpux) {
3681.1Sdbj			ucode = HPUX_ILL_ILLINST_TRAP;
3691.1Sdbj			i = SIGILL;
3701.1Sdbj			break;
3711.1Sdbj		}
3721.1Sdbj		/* fall through */
3731.1Sdbj#endif
3741.1Sdbj	case T_PRIVINST|T_USER:	/* privileged instruction fault */
3751.1Sdbj#ifdef COMPAT_HPUX
3761.1Sdbj		if (p->p_emul == &emul_hpux)
3771.1Sdbj			ucode = HPUX_ILL_PRIV_TRAP;
3781.1Sdbj		else
3791.1Sdbj#endif
3801.1Sdbj		ucode = frame.f_format;	/* XXX was ILL_PRIVIN_FAULT */
3811.1Sdbj		i = SIGILL;
3821.1Sdbj		break;
3831.1Sdbj
3841.1Sdbj	case T_ZERODIV|T_USER:	/* Divide by zero */
3851.1Sdbj#ifdef COMPAT_HPUX
3861.1Sdbj		if (p->p_emul == &emul_hpux)
3871.1Sdbj			ucode = HPUX_FPE_INTDIV_TRAP;
3881.1Sdbj		else
3891.1Sdbj#endif
3901.1Sdbj		ucode = frame.f_format;	/* XXX was FPE_INTDIV_TRAP */
3911.1Sdbj		i = SIGFPE;
3921.1Sdbj		break;
3931.1Sdbj
3941.1Sdbj	case T_CHKINST|T_USER:	/* CHK instruction trap */
3951.1Sdbj#ifdef COMPAT_HPUX
3961.1Sdbj		if (p->p_emul == &emul_hpux) {
3971.1Sdbj			/* handled differently under hp-ux */
3981.1Sdbj			i = SIGILL;
3991.1Sdbj			ucode = HPUX_ILL_CHK_TRAP;
4001.1Sdbj			break;
4011.1Sdbj		}
4021.1Sdbj#endif
4031.1Sdbj		ucode = frame.f_format;	/* XXX was FPE_SUBRNG_TRAP */
4041.1Sdbj		i = SIGFPE;
4051.1Sdbj		break;
4061.1Sdbj
4071.1Sdbj	case T_TRAPVINST|T_USER:	/* TRAPV instruction trap */
4081.1Sdbj#ifdef COMPAT_HPUX
4091.1Sdbj		if (p->p_emul == &emul_hpux) {
4101.1Sdbj			/* handled differently under hp-ux */
4111.1Sdbj			i = SIGILL;
4121.1Sdbj			ucode = HPUX_ILL_TRAPV_TRAP;
4131.1Sdbj			break;
4141.1Sdbj		}
4151.1Sdbj#endif
4161.1Sdbj		ucode = frame.f_format;	/* XXX was FPE_INTOVF_TRAP */
4171.1Sdbj		i = SIGFPE;
4181.1Sdbj		break;
4191.1Sdbj
4201.1Sdbj	/*
4211.1Sdbj	 * XXX: Trace traps are a nightmare.
4221.1Sdbj	 *
4231.1Sdbj	 *	HP-UX uses trap #1 for breakpoints,
4241.7Sdbj	 *	HPBSD uses trap #2,
4251.1Sdbj	 *	SUN 3.x uses trap #15,
4261.7Sdbj	 *	KGDB uses trap #15 (for kernel breakpoints; handled elsewhere).
4271.1Sdbj	 *
4281.7Sdbj	 * HPBSD and HP-UX traps both get mapped by locore.s into T_TRACE.
4291.1Sdbj	 * SUN 3.x traps get passed through as T_TRAP15 and are not really
4301.1Sdbj	 * supported yet.
4311.1Sdbj	 */
4321.1Sdbj	case T_TRACE:		/* kernel trace trap */
4331.7Sdbj	case T_TRAP15:		/* SUN trace trap */
4341.7Sdbj#ifdef DDB
4351.7Sdbj		if (type == T_TRAP15 ||
4361.7Sdbj		    ((caddr_t)frame.f_pc != trap0 &&
4371.7Sdbj		     (caddr_t)frame.f_pc != trap1 &&
4381.7Sdbj		     (caddr_t)frame.f_pc != trap2 &&
4391.7Sdbj		     (caddr_t)frame.f_pc != trap12 &&
4401.7Sdbj		     (caddr_t)frame.f_pc != trap15 &&
4411.7Sdbj		     (caddr_t)frame.f_pc != illinst)) {
4421.7Sdbj			if (kdb_trap(type, &frame))
4431.7Sdbj				return;
4441.7Sdbj		}
4451.1Sdbj#endif
4461.1Sdbj		frame.f_sr &= ~PSL_T;
4471.7Sdbj		i = SIGTRAP;
4481.7Sdbj		break;
4491.1Sdbj
4501.1Sdbj	case T_TRACE|T_USER:	/* user trace trap */
4511.1Sdbj	case T_TRAP15|T_USER:	/* SUN user trace trap */
4521.1Sdbj#ifdef COMPAT_SUNOS
4531.1Sdbj		/*
4541.1Sdbj		 * SunOS uses Trap #2 for a "CPU cache flush".
4551.1Sdbj		 * Just flush the on-chip caches and return.
4561.1Sdbj		 */
4571.1Sdbj		if (p->p_emul == &emul_sunos) {
4581.1Sdbj			ICIA();
4591.1Sdbj			DCIU();
4601.1Sdbj			return;
4611.1Sdbj		}
4621.7Sdbj#endif COMPAT_SUNOS
4631.1Sdbj		frame.f_sr &= ~PSL_T;
4641.1Sdbj		i = SIGTRAP;
4651.1Sdbj		break;
4661.1Sdbj
4671.1Sdbj	case T_ASTFLT:		/* system async trap, cannot happen */
4681.1Sdbj		goto dopanic;
4691.1Sdbj
4701.1Sdbj	case T_ASTFLT|T_USER:	/* user async trap */
4711.1Sdbj		astpending = 0;
4721.1Sdbj		/*
4731.1Sdbj		 * We check for software interrupts first.  This is because
4741.1Sdbj		 * they are at a higher level than ASTs, and on a VAX would
4751.1Sdbj		 * interrupt the AST.  We assume that if we are processing
4761.1Sdbj		 * an AST that we must be at IPL0 so we don't bother to
4771.1Sdbj		 * check.  Note that we ensure that we are at least at SIR
4781.1Sdbj		 * IPL while processing the SIR.
4791.1Sdbj		 */
4801.1Sdbj		spl1();
4811.1Sdbj		/* fall into... */
4821.1Sdbj
4831.1Sdbj	case T_SSIR:		/* software interrupt */
4841.1Sdbj	case T_SSIR|T_USER:
4851.1Sdbj		while (bit = ffs(ssir)) {
4861.1Sdbj			--bit;
4871.1Sdbj			ssir &= ~(1 << bit);
4881.7Sdbj			uvmexp.softs++;
4891.1Sdbj			if (sir_routines[bit])
4901.1Sdbj				sir_routines[bit](sir_args[bit]);
4911.1Sdbj		}
4921.1Sdbj
4931.1Sdbj		/*
4941.1Sdbj		 * If this was not an AST trap, we are all done.
4951.1Sdbj		 */
4961.1Sdbj		if (type != (T_ASTFLT|T_USER)) {
4971.7Sdbj			uvmexp.traps++;
4981.1Sdbj			return;
4991.1Sdbj		}
5001.1Sdbj		spl0();
5011.1Sdbj		if (p->p_flag & P_OWEUPC) {
5021.1Sdbj			p->p_flag &= ~P_OWEUPC;
5031.1Sdbj			ADDUPROF(p);
5041.1Sdbj		}
5051.1Sdbj		goto out;
5061.1Sdbj
5071.1Sdbj	case T_MMUFLT:		/* kernel mode page fault */
5081.1Sdbj		/*
5091.1Sdbj		 * If we were doing profiling ticks or other user mode
5101.1Sdbj		 * stuff from interrupt code, Just Say No.
5111.1Sdbj		 */
5121.1Sdbj		if (p->p_addr->u_pcb.pcb_onfault == fubail ||
5131.1Sdbj		    p->p_addr->u_pcb.pcb_onfault == subail)
5141.1Sdbj			goto copyfault;
5151.1Sdbj		/* fall into ... */
5161.1Sdbj
5171.1Sdbj	case T_MMUFLT|T_USER:	/* page fault */
5181.1Sdbj	    {
5191.7Sdbj		vaddr_t va;
5201.1Sdbj		struct vmspace *vm = p->p_vmspace;
5211.1Sdbj		vm_map_t map;
5221.1Sdbj		int rv;
5231.1Sdbj		vm_prot_t ftype;
5241.1Sdbj		extern vm_map_t kernel_map;
5251.1Sdbj
5261.1Sdbj#ifdef DEBUG
5271.1Sdbj		if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
5281.1Sdbj		printf("trap: T_MMUFLT pid=%d, code=%x, v=%x, pc=%x, sr=%x\n",
5291.1Sdbj		       p->p_pid, code, v, frame.f_pc, frame.f_sr);
5301.1Sdbj#endif
5311.1Sdbj		/*
5321.1Sdbj		 * It is only a kernel address space fault iff:
5331.1Sdbj		 * 	1. (type & T_USER) == 0  and
5341.1Sdbj		 * 	2. pcb_onfault not set or
5351.1Sdbj		 *	3. pcb_onfault set but supervisor space data fault
5361.1Sdbj		 * The last can occur during an exec() copyin where the
5371.1Sdbj		 * argument space is lazy-allocated.
5381.1Sdbj		 */
5391.7Sdbj		if (type == T_MMUFLT &&
5401.7Sdbj		    (!p->p_addr->u_pcb.pcb_onfault || KDFAULT(code)))
5411.1Sdbj			map = kernel_map;
5421.1Sdbj		else
5431.7Sdbj			map = &vm->vm_map;
5441.1Sdbj		if (WRFAULT(code))
5451.1Sdbj			ftype = VM_PROT_READ | VM_PROT_WRITE;
5461.1Sdbj		else
5471.1Sdbj			ftype = VM_PROT_READ;
5481.7Sdbj		va = trunc_page((vaddr_t)v);
5491.7Sdbj#ifdef DEBUG
5501.1Sdbj		if (map == kernel_map && va == 0) {
5511.7Sdbj			printf("trap: bad kernel access at %x\n", v);
5521.1Sdbj			goto dopanic;
5531.1Sdbj		}
5541.7Sdbj#endif
5551.1Sdbj#ifdef COMPAT_HPUX
5561.1Sdbj		if (ISHPMMADDR(va)) {
5571.7Sdbj			vaddr_t bva;
5581.1Sdbj
5591.1Sdbj			rv = pmap_mapmulti(map->pmap, va);
5601.1Sdbj			if (rv != KERN_SUCCESS) {
5611.1Sdbj				bva = HPMMBASEADDR(va);
5621.7Sdbj				rv = uvm_fault(map, bva, 0, ftype);
5631.1Sdbj				if (rv == KERN_SUCCESS)
5641.1Sdbj					(void) pmap_mapmulti(map->pmap, va);
5651.1Sdbj			}
5661.1Sdbj		} else
5671.1Sdbj#endif
5681.7Sdbj		rv = uvm_fault(map, va, 0, ftype);
5691.7Sdbj#ifdef DEBUG
5701.7Sdbj		if (rv && MDB_ISPID(p->p_pid))
5711.7Sdbj			printf("uvm_fault(%p, 0x%lx, 0, 0x%x) -> 0x%x\n",
5721.7Sdbj			       map, va, ftype, rv);
5731.7Sdbj#endif
5741.1Sdbj		/*
5751.1Sdbj		 * If this was a stack access we keep track of the maximum
5761.1Sdbj		 * accessed stack size.  Also, if vm_fault gets a protection
5771.1Sdbj		 * failure it is due to accessing the stack region outside
5781.1Sdbj		 * the current limit and we need to reflect that as an access
5791.1Sdbj		 * error.
5801.1Sdbj		 */
5811.7Sdbj		if ((caddr_t)va >= vm->vm_maxsaddr && map != kernel_map) {
5821.1Sdbj			if (rv == KERN_SUCCESS) {
5831.1Sdbj				unsigned nss;
5841.1Sdbj
5851.1Sdbj				nss = clrnd(btoc(USRSTACK-(unsigned)va));
5861.1Sdbj				if (nss > vm->vm_ssize)
5871.1Sdbj					vm->vm_ssize = nss;
5881.1Sdbj			} else if (rv == KERN_PROTECTION_FAILURE)
5891.1Sdbj				rv = KERN_INVALID_ADDRESS;
5901.1Sdbj		}
5911.1Sdbj		if (rv == KERN_SUCCESS) {
5921.1Sdbj			if (type == T_MMUFLT) {
5931.7Sdbj#if defined(M68040)
5941.7Sdbj				if (mmutype == MMU_68040)
5951.1Sdbj					(void) writeback(&frame, 1);
5961.1Sdbj#endif
5971.1Sdbj				return;
5981.1Sdbj			}
5991.1Sdbj			goto out;
6001.1Sdbj		}
6011.1Sdbj		if (type == T_MMUFLT) {
6021.1Sdbj			if (p->p_addr->u_pcb.pcb_onfault)
6031.1Sdbj				goto copyfault;
6041.7Sdbj			printf("uvm_fault(%p, 0x%lx, 0, 0x%x) -> 0x%x\n",
6051.7Sdbj			       map, va, ftype, rv);
6061.1Sdbj			printf("  type %x, code [mmu,,ssw]: %x\n",
6071.1Sdbj			       type, code);
6081.1Sdbj			goto dopanic;
6091.1Sdbj		}
6101.1Sdbj		ucode = v;
6111.11Schs		if (rv == KERN_RESOURCE_SHORTAGE) {
6121.11Schs			printf("UVM: pid %d (%s), uid %d killed: out of swap\n",
6131.11Schs			       p->p_pid, p->p_comm,
6141.11Schs			       p->p_cred && p->p_ucred ?
6151.11Schs			       p->p_ucred->cr_uid : -1);
6161.11Schs			i = SIGKILL;
6171.11Schs		} else {
6181.11Schs			i = SIGSEGV;
6191.11Schs		}
6201.1Sdbj		break;
6211.1Sdbj	    }
6221.1Sdbj	}
6231.1Sdbj	trapsignal(p, i, ucode);
6241.1Sdbj	if ((type & T_USER) == 0)
6251.1Sdbj		return;
6261.1Sdbjout:
6271.1Sdbj	userret(p, &frame, sticks, v, 1);
6281.1Sdbj}
6291.1Sdbj
6301.1Sdbj#ifdef M68040
6311.1Sdbj#ifdef DEBUG
6321.1Sdbjstruct writebackstats {
6331.1Sdbj	int calls;
6341.1Sdbj	int cpushes;
6351.1Sdbj	int move16s;
6361.1Sdbj	int wb1s, wb2s, wb3s;
6371.1Sdbj	int wbsize[4];
6381.1Sdbj} wbstats;
6391.1Sdbj
6401.1Sdbjchar *f7sz[] = { "longword", "byte", "word", "line" };
6411.1Sdbjchar *f7tt[] = { "normal", "MOVE16", "AFC", "ACK" };
6421.1Sdbjchar *f7tm[] = { "d-push", "u-data", "u-code", "M-data",
6431.1Sdbj		 "M-code", "k-data", "k-code", "RES" };
6441.1Sdbjchar wberrstr[] =
6451.7Sdbj	"WARNING: pid %d(%s) writeback [%s] failed, pc=%x fa=%x wba=%x wbd=%x\n";
6461.1Sdbj#endif
6471.1Sdbj
6481.1Sdbjwriteback(fp, docachepush)
6491.1Sdbj	struct frame *fp;
6501.1Sdbj	int docachepush;
6511.1Sdbj{
6521.1Sdbj	struct fmt7 *f = &fp->f_fmt7;
6531.1Sdbj	struct proc *p = curproc;
6541.1Sdbj	int err = 0;
6551.1Sdbj	u_int fa;
6561.1Sdbj	caddr_t oonfault = p->p_addr->u_pcb.pcb_onfault;
6571.1Sdbj
6581.1Sdbj#ifdef DEBUG
6591.1Sdbj	if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid)) {
6601.1Sdbj		printf(" pid=%d, fa=%x,", p->p_pid, f->f_fa);
6611.1Sdbj		dumpssw(f->f_ssw);
6621.1Sdbj	}
6631.1Sdbj	wbstats.calls++;
6641.1Sdbj#endif
6651.1Sdbj	/*
6661.1Sdbj	 * Deal with special cases first.
6671.1Sdbj	 */
6681.1Sdbj	if ((f->f_ssw & SSW4_TMMASK) == SSW4_TMDCP) {
6691.1Sdbj		/*
6701.1Sdbj		 * Dcache push fault.
6711.1Sdbj		 * Line-align the address and write out the push data to
6721.1Sdbj		 * the indicated physical address.
6731.1Sdbj		 */
6741.1Sdbj#ifdef DEBUG
6751.1Sdbj		if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid)) {
6761.1Sdbj			printf(" pushing %s to PA %x, data %x",
6771.1Sdbj			       f7sz[(f->f_ssw & SSW4_SZMASK) >> 5],
6781.1Sdbj			       f->f_fa, f->f_pd0);
6791.1Sdbj			if ((f->f_ssw & SSW4_SZMASK) == SSW4_SZLN)
6801.1Sdbj				printf("/%x/%x/%x",
6811.1Sdbj				       f->f_pd1, f->f_pd2, f->f_pd3);
6821.1Sdbj			printf("\n");
6831.1Sdbj		}
6841.1Sdbj		if (f->f_wb1s & SSW4_WBSV)
6851.1Sdbj			panic("writeback: cache push with WB1S valid");
6861.1Sdbj		wbstats.cpushes++;
6871.1Sdbj#endif
6881.1Sdbj		/*
6891.1Sdbj		 * XXX there are security problems if we attempt to do a
6901.1Sdbj		 * cache push after a signal handler has been called.
6911.1Sdbj		 */
6921.1Sdbj		if (docachepush) {
6931.7Sdbj			pmap_enter(pmap_kernel(), (vaddr_t)vmmap,
6941.13Smycroft			    trunc_page(f->f_fa), VM_PROT_WRITE, TRUE,
6951.13Smycroft			    VM_PROT_WRITE);
6961.1Sdbj			fa = (u_int)&vmmap[(f->f_fa & PGOFSET) & ~0xF];
6971.1Sdbj			bcopy((caddr_t)&f->f_pd0, (caddr_t)fa, 16);
6981.7Sdbj			DCFL(pmap_extract(pmap_kernel(), (vaddr_t)fa));
6991.7Sdbj			pmap_remove(pmap_kernel(), (vaddr_t)vmmap,
7001.7Sdbj				    (vaddr_t)&vmmap[NBPG]);
7011.1Sdbj		} else
7021.1Sdbj			printf("WARNING: pid %d(%s) uid %d: CPUSH not done\n",
7031.1Sdbj			       p->p_pid, p->p_comm, p->p_ucred->cr_uid);
7041.1Sdbj	} else if ((f->f_ssw & (SSW4_RW|SSW4_TTMASK)) == SSW4_TTM16) {
7051.1Sdbj		/*
7061.1Sdbj		 * MOVE16 fault.
7071.1Sdbj		 * Line-align the address and write out the push data to
7081.1Sdbj		 * the indicated virtual address.
7091.1Sdbj		 */
7101.1Sdbj#ifdef DEBUG
7111.1Sdbj		if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
7121.1Sdbj			printf(" MOVE16 to VA %x(%x), data %x/%x/%x/%x\n",
7131.1Sdbj			       f->f_fa, f->f_fa & ~0xF, f->f_pd0, f->f_pd1,
7141.1Sdbj			       f->f_pd2, f->f_pd3);
7151.1Sdbj		if (f->f_wb1s & SSW4_WBSV)
7161.1Sdbj			panic("writeback: MOVE16 with WB1S valid");
7171.1Sdbj		wbstats.move16s++;
7181.1Sdbj#endif
7191.1Sdbj		if (KDFAULT(f->f_wb1s))
7201.1Sdbj			bcopy((caddr_t)&f->f_pd0, (caddr_t)(f->f_fa & ~0xF), 16);
7211.1Sdbj		else
7221.1Sdbj			err = suline((caddr_t)(f->f_fa & ~0xF), (caddr_t)&f->f_pd0);
7231.1Sdbj		if (err) {
7241.1Sdbj			fa = f->f_fa & ~0xF;
7251.1Sdbj#ifdef DEBUG
7261.1Sdbj			if (mmudebug & MDB_WBFAILED)
7271.1Sdbj				printf(wberrstr, p->p_pid, p->p_comm,
7281.1Sdbj				       "MOVE16", fp->f_pc, f->f_fa,
7291.1Sdbj				       f->f_fa & ~0xF, f->f_pd0);
7301.1Sdbj#endif
7311.1Sdbj		}
7321.1Sdbj	} else if (f->f_wb1s & SSW4_WBSV) {
7331.1Sdbj		/*
7341.1Sdbj		 * Writeback #1.
7351.1Sdbj		 * Position the "memory-aligned" data and write it out.
7361.1Sdbj		 */
7371.1Sdbj		u_int wb1d = f->f_wb1d;
7381.1Sdbj		int off;
7391.1Sdbj
7401.1Sdbj#ifdef DEBUG
7411.1Sdbj		if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
7421.1Sdbj			dumpwb(1, f->f_wb1s, f->f_wb1a, f->f_wb1d);
7431.1Sdbj		wbstats.wb1s++;
7441.1Sdbj		wbstats.wbsize[(f->f_wb2s&SSW4_SZMASK)>>5]++;
7451.1Sdbj#endif
7461.1Sdbj		off = (f->f_wb1a & 3) * 8;
7471.1Sdbj		switch (f->f_wb1s & SSW4_SZMASK) {
7481.1Sdbj		case SSW4_SZLW:
7491.1Sdbj			if (off)
7501.1Sdbj				wb1d = (wb1d >> (32 - off)) | (wb1d << off);
7511.1Sdbj			if (KDFAULT(f->f_wb1s))
7521.1Sdbj				*(long *)f->f_wb1a = wb1d;
7531.1Sdbj			else
7541.1Sdbj				err = suword((caddr_t)f->f_wb1a, wb1d);
7551.1Sdbj			break;
7561.1Sdbj		case SSW4_SZB:
7571.1Sdbj			off = 24 - off;
7581.1Sdbj			if (off)
7591.1Sdbj				wb1d >>= off;
7601.1Sdbj			if (KDFAULT(f->f_wb1s))
7611.1Sdbj				*(char *)f->f_wb1a = wb1d;
7621.1Sdbj			else
7631.1Sdbj				err = subyte((caddr_t)f->f_wb1a, wb1d);
7641.1Sdbj			break;
7651.1Sdbj		case SSW4_SZW:
7661.1Sdbj			off = (off + 16) % 32;
7671.1Sdbj			if (off)
7681.1Sdbj				wb1d = (wb1d >> (32 - off)) | (wb1d << off);
7691.1Sdbj			if (KDFAULT(f->f_wb1s))
7701.1Sdbj				*(short *)f->f_wb1a = wb1d;
7711.1Sdbj			else
7721.1Sdbj				err = susword((caddr_t)f->f_wb1a, wb1d);
7731.1Sdbj			break;
7741.1Sdbj		}
7751.1Sdbj		if (err) {
7761.1Sdbj			fa = f->f_wb1a;
7771.1Sdbj#ifdef DEBUG
7781.1Sdbj			if (mmudebug & MDB_WBFAILED)
7791.1Sdbj				printf(wberrstr, p->p_pid, p->p_comm,
7801.1Sdbj				       "#1", fp->f_pc, f->f_fa,
7811.1Sdbj				       f->f_wb1a, f->f_wb1d);
7821.1Sdbj#endif
7831.1Sdbj		}
7841.1Sdbj	}
7851.1Sdbj	/*
7861.1Sdbj	 * Deal with the "normal" writebacks.
7871.1Sdbj	 *
7881.1Sdbj	 * XXX writeback2 is known to reflect a LINE size writeback after
7891.1Sdbj	 * a MOVE16 was already dealt with above.  Ignore it.
7901.1Sdbj	 */
7911.1Sdbj	if (err == 0 && (f->f_wb2s & SSW4_WBSV) &&
7921.1Sdbj	    (f->f_wb2s & SSW4_SZMASK) != SSW4_SZLN) {
7931.1Sdbj#ifdef DEBUG
7941.1Sdbj		if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
7951.1Sdbj			dumpwb(2, f->f_wb2s, f->f_wb2a, f->f_wb2d);
7961.1Sdbj		wbstats.wb2s++;
7971.1Sdbj		wbstats.wbsize[(f->f_wb2s&SSW4_SZMASK)>>5]++;
7981.1Sdbj#endif
7991.1Sdbj		switch (f->f_wb2s & SSW4_SZMASK) {
8001.1Sdbj		case SSW4_SZLW:
8011.1Sdbj			if (KDFAULT(f->f_wb2s))
8021.1Sdbj				*(long *)f->f_wb2a = f->f_wb2d;
8031.1Sdbj			else
8041.1Sdbj				err = suword((caddr_t)f->f_wb2a, f->f_wb2d);
8051.1Sdbj			break;
8061.1Sdbj		case SSW4_SZB:
8071.1Sdbj			if (KDFAULT(f->f_wb2s))
8081.1Sdbj				*(char *)f->f_wb2a = f->f_wb2d;
8091.1Sdbj			else
8101.1Sdbj				err = subyte((caddr_t)f->f_wb2a, f->f_wb2d);
8111.1Sdbj			break;
8121.1Sdbj		case SSW4_SZW:
8131.1Sdbj			if (KDFAULT(f->f_wb2s))
8141.1Sdbj				*(short *)f->f_wb2a = f->f_wb2d;
8151.1Sdbj			else
8161.1Sdbj				err = susword((caddr_t)f->f_wb2a, f->f_wb2d);
8171.1Sdbj			break;
8181.1Sdbj		}
8191.1Sdbj		if (err) {
8201.1Sdbj			fa = f->f_wb2a;
8211.1Sdbj#ifdef DEBUG
8221.1Sdbj			if (mmudebug & MDB_WBFAILED) {
8231.1Sdbj				printf(wberrstr, p->p_pid, p->p_comm,
8241.1Sdbj				       "#2", fp->f_pc, f->f_fa,
8251.1Sdbj				       f->f_wb2a, f->f_wb2d);
8261.1Sdbj				dumpssw(f->f_ssw);
8271.1Sdbj				dumpwb(2, f->f_wb2s, f->f_wb2a, f->f_wb2d);
8281.1Sdbj			}
8291.1Sdbj#endif
8301.1Sdbj		}
8311.1Sdbj	}
8321.1Sdbj	if (err == 0 && (f->f_wb3s & SSW4_WBSV)) {
8331.1Sdbj#ifdef DEBUG
8341.1Sdbj		if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
8351.1Sdbj			dumpwb(3, f->f_wb3s, f->f_wb3a, f->f_wb3d);
8361.1Sdbj		wbstats.wb3s++;
8371.1Sdbj		wbstats.wbsize[(f->f_wb3s&SSW4_SZMASK)>>5]++;
8381.1Sdbj#endif
8391.1Sdbj		switch (f->f_wb3s & SSW4_SZMASK) {
8401.1Sdbj		case SSW4_SZLW:
8411.1Sdbj			if (KDFAULT(f->f_wb3s))
8421.1Sdbj				*(long *)f->f_wb3a = f->f_wb3d;
8431.1Sdbj			else
8441.1Sdbj				err = suword((caddr_t)f->f_wb3a, f->f_wb3d);
8451.1Sdbj			break;
8461.1Sdbj		case SSW4_SZB:
8471.1Sdbj			if (KDFAULT(f->f_wb3s))
8481.1Sdbj				*(char *)f->f_wb3a = f->f_wb3d;
8491.1Sdbj			else
8501.1Sdbj				err = subyte((caddr_t)f->f_wb3a, f->f_wb3d);
8511.1Sdbj			break;
8521.1Sdbj		case SSW4_SZW:
8531.1Sdbj			if (KDFAULT(f->f_wb3s))
8541.1Sdbj				*(short *)f->f_wb3a = f->f_wb3d;
8551.1Sdbj			else
8561.1Sdbj				err = susword((caddr_t)f->f_wb3a, f->f_wb3d);
8571.1Sdbj			break;
8581.1Sdbj#ifdef DEBUG
8591.1Sdbj		case SSW4_SZLN:
8601.1Sdbj			panic("writeback: wb3s indicates LINE write");
8611.1Sdbj#endif
8621.1Sdbj		}
8631.1Sdbj		if (err) {
8641.1Sdbj			fa = f->f_wb3a;
8651.1Sdbj#ifdef DEBUG
8661.1Sdbj			if (mmudebug & MDB_WBFAILED)
8671.1Sdbj				printf(wberrstr, p->p_pid, p->p_comm,
8681.1Sdbj				       "#3", fp->f_pc, f->f_fa,
8691.1Sdbj				       f->f_wb3a, f->f_wb3d);
8701.1Sdbj#endif
8711.1Sdbj		}
8721.1Sdbj	}
8731.1Sdbj	p->p_addr->u_pcb.pcb_onfault = oonfault;
8741.1Sdbj	if (err)
8751.1Sdbj		err = SIGSEGV;
8761.7Sdbj	return(err);
8771.1Sdbj}
8781.1Sdbj
8791.1Sdbj#ifdef DEBUG
8801.1Sdbjdumpssw(ssw)
8811.1Sdbj	u_short ssw;
8821.1Sdbj{
8831.1Sdbj	printf(" SSW: %x: ", ssw);
8841.1Sdbj	if (ssw & SSW4_CP)
8851.1Sdbj		printf("CP,");
8861.1Sdbj	if (ssw & SSW4_CU)
8871.1Sdbj		printf("CU,");
8881.1Sdbj	if (ssw & SSW4_CT)
8891.1Sdbj		printf("CT,");
8901.1Sdbj	if (ssw & SSW4_CM)
8911.1Sdbj		printf("CM,");
8921.1Sdbj	if (ssw & SSW4_MA)
8931.1Sdbj		printf("MA,");
8941.1Sdbj	if (ssw & SSW4_ATC)
8951.1Sdbj		printf("ATC,");
8961.1Sdbj	if (ssw & SSW4_LK)
8971.1Sdbj		printf("LK,");
8981.1Sdbj	if (ssw & SSW4_RW)
8991.1Sdbj		printf("RW,");
9001.1Sdbj	printf(" SZ=%s, TT=%s, TM=%s\n",
9011.1Sdbj	       f7sz[(ssw & SSW4_SZMASK) >> 5],
9021.1Sdbj	       f7tt[(ssw & SSW4_TTMASK) >> 3],
9031.1Sdbj	       f7tm[ssw & SSW4_TMMASK]);
9041.1Sdbj}
9051.1Sdbj
9061.1Sdbjdumpwb(num, s, a, d)
9071.1Sdbj	int num;
9081.1Sdbj	u_short s;
9091.1Sdbj	u_int a, d;
9101.1Sdbj{
9111.1Sdbj	struct proc *p = curproc;
9121.7Sdbj	paddr_t pa;
9131.1Sdbj
9141.1Sdbj	printf(" writeback #%d: VA %x, data %x, SZ=%s, TT=%s, TM=%s\n",
9151.1Sdbj	       num, a, d, f7sz[(s & SSW4_SZMASK) >> 5],
9161.1Sdbj	       f7tt[(s & SSW4_TTMASK) >> 3], f7tm[s & SSW4_TMMASK]);
9171.7Sdbj	printf("	       PA ");
9181.7Sdbj	pa = pmap_extract(p->p_vmspace->vm_map.pmap, (vaddr_t)a);
9191.1Sdbj	if (pa == 0)
9201.1Sdbj		printf("<invalid address>");
9211.1Sdbj	else
9221.7Sdbj		printf("%x, current value %x", pa, fuword((caddr_t)a));
9231.1Sdbj	printf("\n");
9241.1Sdbj}
9251.1Sdbj#endif
9261.1Sdbj#endif
9271.1Sdbj
9281.1Sdbj/*
9291.1Sdbj * Process a system call.
9301.1Sdbj */
9311.1Sdbjsyscall(code, frame)
9321.7Sdbj	int code;
9331.1Sdbj	struct frame frame;
9341.1Sdbj{
9351.1Sdbj	caddr_t params;
9361.1Sdbj	struct sysent *callp;
9371.1Sdbj	struct proc *p;
9381.1Sdbj	int error, opc, nsys;
9391.1Sdbj	size_t argsize;
9401.7Sdbj	int args[8], rval[2];
9411.1Sdbj	u_quad_t sticks;
9421.1Sdbj
9431.7Sdbj	uvmexp.syscalls++;
9441.1Sdbj	if (!USERMODE(frame.f_sr))
9451.1Sdbj		panic("syscall");
9461.1Sdbj	p = curproc;
9471.1Sdbj	sticks = p->p_sticks;
9481.1Sdbj	p->p_md.md_regs = frame.f_regs;
9491.1Sdbj	opc = frame.f_pc;
9501.1Sdbj
9511.1Sdbj	nsys = p->p_emul->e_nsysent;
9521.1Sdbj	callp = p->p_emul->e_sysent;
9531.1Sdbj
9541.1Sdbj#ifdef COMPAT_SUNOS
9551.1Sdbj	if (p->p_emul == &emul_sunos) {
9561.1Sdbj		/*
9571.1Sdbj		 * SunOS passes the syscall-number on the stack, whereas
9581.1Sdbj		 * BSD passes it in D0. So, we have to get the real "code"
9591.1Sdbj		 * from the stack, and clean up the stack, as SunOS glue
9601.1Sdbj		 * code assumes the kernel pops the syscall argument the
9611.1Sdbj		 * glue pushed on the stack. Sigh...
9621.1Sdbj		 */
9631.1Sdbj		code = fuword((caddr_t)frame.f_regs[SP]);
9641.1Sdbj
9651.1Sdbj		/*
9661.1Sdbj		 * XXX
9671.1Sdbj		 * Don't do this for sunos_sigreturn, as there's no stored pc
9681.1Sdbj		 * on the stack to skip, the argument follows the syscall
9691.1Sdbj		 * number without a gap.
9701.1Sdbj		 */
9711.1Sdbj		if (code != SUNOS_SYS_sigreturn) {
9721.1Sdbj			frame.f_regs[SP] += sizeof (int);
9731.1Sdbj			/*
9741.7Sdbj			 * remember that we adjusted the SP,
9751.1Sdbj			 * might have to undo this if the system call
9761.1Sdbj			 * returns ERESTART.
9771.1Sdbj			 */
9781.1Sdbj			p->p_md.md_flags |= MDP_STACKADJ;
9791.1Sdbj		} else
9801.1Sdbj			p->p_md.md_flags &= ~MDP_STACKADJ;
9811.1Sdbj	}
9821.1Sdbj#endif
9831.1Sdbj
9841.1Sdbj	params = (caddr_t)frame.f_regs[SP] + sizeof(int);
9851.1Sdbj
9861.1Sdbj	switch (code) {
9871.1Sdbj	case SYS_syscall:
9881.1Sdbj		/*
9891.1Sdbj		 * Code is first argument, followed by actual args.
9901.1Sdbj		 */
9911.1Sdbj		code = fuword(params);
9921.1Sdbj		params += sizeof(int);
9931.1Sdbj		/*
9941.1Sdbj		 * XXX sigreturn requires special stack manipulation
9951.1Sdbj		 * that is only done if entered via the sigreturn
9961.1Sdbj		 * trap.  Cannot allow it here so make sure we fail.
9971.1Sdbj		 */
9981.6Sthorpej		switch (code) {
9991.6Sthorpej#ifdef COMPAT_13
10001.6Sthorpej		case SYS_compat_13_sigreturn13:
10011.6Sthorpej#endif
10021.6Sthorpej		case SYS___sigreturn14:
10031.1Sdbj			code = nsys;
10041.6Sthorpej			break;
10051.6Sthorpej		}
10061.1Sdbj		break;
10071.1Sdbj	case SYS___syscall:
10081.1Sdbj		/*
10091.1Sdbj		 * Like syscall, but code is a quad, so as to maintain
10101.1Sdbj		 * quad alignment for the rest of the arguments.
10111.1Sdbj		 */
10121.1Sdbj		if (callp != sysent)
10131.1Sdbj			break;
10141.1Sdbj		code = fuword(params + _QUAD_LOWWORD * sizeof(int));
10151.1Sdbj		params += sizeof(quad_t);
10161.1Sdbj		break;
10171.1Sdbj	default:
10181.1Sdbj		break;
10191.1Sdbj	}
10201.1Sdbj	if (code < 0 || code >= nsys)
10211.1Sdbj		callp += p->p_emul->e_nosys;		/* illegal */
10221.1Sdbj	else
10231.1Sdbj		callp += code;
10241.1Sdbj	argsize = callp->sy_argsize;
10251.9Sitohy#ifdef COMPAT_LINUX
10261.9Sitohy	if (0
10271.9Sitohy# ifdef EXEC_AOUT
10281.9Sitohy	    || p->p_emul == &emul_linux_aout
10291.9Sitohy# endif
10301.9Sitohy# ifdef EXEC_ELF32
10311.9Sitohy	    || p->p_emul == &emul_linux_elf32
10321.9Sitohy# endif
10331.9Sitohy	     ) {
10341.9Sitohy		/*
10351.9Sitohy		 * Linux passes the args in d1-d5
10361.9Sitohy		 */
10371.9Sitohy		switch (argsize) {
10381.9Sitohy		case 20:
10391.9Sitohy			args[4] = frame.f_regs[D5];
10401.9Sitohy		case 16:
10411.9Sitohy			args[3] = frame.f_regs[D4];
10421.9Sitohy		case 12:
10431.9Sitohy			args[2] = frame.f_regs[D3];
10441.9Sitohy		case 8:
10451.9Sitohy			args[1] = frame.f_regs[D2];
10461.9Sitohy		case 4:
10471.9Sitohy			args[0] = frame.f_regs[D1];
10481.9Sitohy		case 0:
10491.9Sitohy			error = 0;
10501.9Sitohy			break;
10511.9Sitohy		default:
10521.9Sitohy#ifdef DEBUG
10531.9Sitohy			panic("linux syscall %d weird argsize %d",
10541.9Sitohy				code, argsize);
10551.9Sitohy#else
10561.9Sitohy			error = EINVAL;
10571.9Sitohy#endif
10581.9Sitohy			break;
10591.9Sitohy		}
10601.9Sitohy	} else
10611.9Sitohy#endif
10621.1Sdbj	if (argsize)
10631.1Sdbj		error = copyin(params, (caddr_t)args, argsize);
10641.1Sdbj	else
10651.1Sdbj		error = 0;
10661.1Sdbj#ifdef SYSCALL_DEBUG
10671.1Sdbj	scdebug_call(p, code, args);
10681.1Sdbj#endif
10691.1Sdbj#ifdef KTRACE
10701.1Sdbj	if (KTRPOINT(p, KTR_SYSCALL))
10711.1Sdbj		ktrsyscall(p->p_tracep, code, argsize, args);
10721.1Sdbj#endif
10731.1Sdbj	if (error)
10741.1Sdbj		goto bad;
10751.1Sdbj	rval[0] = 0;
10761.1Sdbj	rval[1] = frame.f_regs[D1];
10771.1Sdbj	error = (*callp->sy_call)(p, args, rval);
10781.1Sdbj	switch (error) {
10791.1Sdbj	case 0:
10801.1Sdbj		frame.f_regs[D0] = rval[0];
10811.1Sdbj		frame.f_regs[D1] = rval[1];
10821.1Sdbj		frame.f_sr &= ~PSL_C;	/* carry bit */
10831.1Sdbj		break;
10841.1Sdbj	case ERESTART:
10851.1Sdbj		/*
10861.1Sdbj		 * We always enter through a `trap' instruction, which is 2
10871.1Sdbj		 * bytes, so adjust the pc by that amount.
10881.1Sdbj		 */
10891.1Sdbj		frame.f_pc = opc - 2;
10901.1Sdbj		break;
10911.1Sdbj	case EJUSTRETURN:
10921.1Sdbj		/* nothing to do */
10931.1Sdbj		break;
10941.1Sdbj	default:
10951.1Sdbj	bad:
10961.1Sdbj		if (p->p_emul->e_errno)
10971.1Sdbj			error = p->p_emul->e_errno[error];
10981.1Sdbj		frame.f_regs[D0] = error;
10991.1Sdbj		frame.f_sr |= PSL_C;	/* carry bit */
11001.1Sdbj		break;
11011.1Sdbj	}
11021.1Sdbj
11031.1Sdbj#ifdef SYSCALL_DEBUG
11041.1Sdbj	scdebug_ret(p, code, error, rval);
11051.1Sdbj#endif
11061.1Sdbj#ifdef COMPAT_SUNOS
11071.1Sdbj	/* need new p-value for this */
11081.1Sdbj	if (error == ERESTART && (p->p_md.md_flags & MDP_STACKADJ))
11091.1Sdbj		frame.f_regs[SP] -= sizeof (int);
11101.1Sdbj#endif
11111.1Sdbj	userret(p, &frame, sticks, (u_int)0, 0);
11121.1Sdbj#ifdef KTRACE
11131.1Sdbj	if (KTRPOINT(p, KTR_SYSRET))
11141.1Sdbj		ktrsysret(p->p_tracep, code, error, rval[0]);
11151.1Sdbj#endif
11161.1Sdbj}
11171.1Sdbj
11181.1Sdbjvoid
11191.8Sthorpejchild_return(arg)
11201.8Sthorpej	void *arg;
11211.1Sdbj{
11221.8Sthorpej	struct proc *p = arg;
11231.8Sthorpej	/* See cpu_fork() */
11241.8Sthorpej	struct frame *f = (struct frame *)p->p_md.md_regs;
11251.8Sthorpej
11261.8Sthorpej	f->f_regs[D0] = 0;
11271.8Sthorpej	f->f_sr &= ~PSL_C;
11281.8Sthorpej	f->f_format = FMT0;
11291.1Sdbj
11301.8Sthorpej	userret(p, f, p->p_sticks, (u_int)0, 0);
11311.1Sdbj#ifdef KTRACE
11321.1Sdbj	if (KTRPOINT(p, KTR_SYSRET))
11331.1Sdbj		ktrsysret(p->p_tracep, SYS_fork, 0, 0);
11341.1Sdbj#endif
11351.1Sdbj}
11361.1Sdbj
11371.1Sdbj/*
11381.1Sdbj * Allocation routines for software interrupts.
11391.1Sdbj */
11401.1Sdbju_long
11411.1Sdbjallocate_sir(proc, arg)
11421.1Sdbj	void (*proc)();
11431.1Sdbj	void *arg;
11441.1Sdbj{
11451.1Sdbj	int bit;
11461.1Sdbj
11471.1Sdbj	if( next_sir >= NSIR )
11481.1Sdbj		panic("allocate_sir: none left");
11491.1Sdbj	bit = next_sir++;
11501.1Sdbj	sir_routines[bit] = proc;
11511.1Sdbj	sir_args[bit] = arg;
11521.1Sdbj	return (1 << bit);
11531.1Sdbj}
11541.1Sdbj
11551.1Sdbjvoid
11561.1Sdbjinit_sir()
11571.1Sdbj{
11581.1Sdbj	extern void netintr();
11591.1Sdbj
11601.1Sdbj	sir_routines[0] = netintr;
11611.1Sdbj	sir_routines[1] = softclock;
11621.1Sdbj	next_sir = 2;
11631.1Sdbj}
1164