trap.c revision 1.20
11.20Sthorpej/*	$NetBSD: trap.c,v 1.20 2000/05/24 16:48:41 thorpej 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.16Sdbj * Darrin B. Jewell <jewell@mit.edu> Tue Aug  3 10:53:12 UTC 1999
71.16Sdbj * original cvs id: NetBSD: trap.c,v 1.32 1999/08/03 10:52:06 dbj 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.16Sdbj
741.16Sdbj#ifdef DEBUG
751.16Sdbj#include <dev/cons.h>
761.14Sdbj#endif
771.1Sdbj
781.16Sdbj#include <machine/db_machdep.h>
791.1Sdbj#include <machine/psl.h>
801.1Sdbj#include <machine/trap.h>
811.1Sdbj#include <machine/cpu.h>
821.1Sdbj#include <machine/reg.h>
831.1Sdbj
841.1Sdbj#include <vm/vm.h>
851.1Sdbj#include <vm/pmap.h>
861.1Sdbj
871.16Sdbj#include <m68k/cacheops.h>
881.16Sdbj
891.7Sdbj#include <uvm/uvm_extern.h>
901.1Sdbj
911.1Sdbj#ifdef COMPAT_HPUX
921.1Sdbj#include <compat/hpux/hpux.h>
931.1Sdbj#endif
941.1Sdbj
951.1Sdbj#ifdef COMPAT_SUNOS
961.1Sdbj#include <compat/sunos/sunos_syscall.h>
971.1Sdbjextern struct emul emul_sunos;
981.1Sdbj#endif
991.1Sdbj
1001.9Sitohy#ifdef COMPAT_LINUX
1011.9Sitohy#ifdef EXEC_AOUT
1021.9Sitohyextern struct emul emul_linux_aout;
1031.9Sitohy#endif
1041.9Sitohy#ifdef EXEC_ELF32
1051.9Sitohyextern struct emul emul_linux_elf32;
1061.9Sitohy#endif
1071.9Sitohy#endif
1081.9Sitohy
1091.16Sdbjint	writeback __P((struct frame *fp, int docachepush));
1101.16Sdbjvoid	trap __P((int type, u_int code, u_int v, struct frame frame));
1111.16Sdbjvoid	syscall __P((register_t code, struct frame frame));
1121.16Sdbj
1131.16Sdbj#ifdef DEBUG
1141.16Sdbjvoid	dumpssw __P((u_short));
1151.16Sdbjvoid	dumpwb __P((int, u_short, u_int, u_int));
1161.16Sdbj#endif
1171.16Sdbj
1181.16Sdbjstatic inline void userret __P((struct proc *p, struct frame *fp,
1191.16Sdbj	    u_quad_t oticks, u_int faultaddr, int fromtrap));
1201.1Sdbj
1211.7Sdbjint	astpending;
1221.1Sdbj
1231.1Sdbjchar	*trap_type[] = {
1241.1Sdbj	"Bus error",
1251.1Sdbj	"Address error",
1261.1Sdbj	"Illegal instruction",
1271.1Sdbj	"Zero divide",
1281.1Sdbj	"CHK instruction",
1291.1Sdbj	"TRAPV instruction",
1301.1Sdbj	"Privilege violation",
1311.1Sdbj	"Trace trap",
1321.1Sdbj	"MMU fault",
1331.1Sdbj	"SSIR trap",
1341.1Sdbj	"Format error",
1351.1Sdbj	"68881 exception",
1361.1Sdbj	"Coprocessor violation",
1371.1Sdbj	"Async system trap"
1381.1Sdbj};
1391.1Sdbjint	trap_types = sizeof trap_type / sizeof trap_type[0];
1401.1Sdbj
1411.1Sdbj/*
1421.1Sdbj * Size of various exception stack frames (minus the standard 8 bytes)
1431.1Sdbj */
1441.1Sdbjshort	exframesize[] = {
1451.16Sdbj	FMT0SIZE,	/* type 0 - normal (68020/030/040/060) */
1461.1Sdbj	FMT1SIZE,	/* type 1 - throwaway (68020/030/040) */
1471.16Sdbj	FMT2SIZE,	/* type 2 - normal 6-word (68020/030/040/060) */
1481.16Sdbj	FMT3SIZE,	/* type 3 - FP post-instruction (68040/060) */
1491.16Sdbj	FMT4SIZE,	/* type 4 - access error/fp disabled (68060) */
1501.16Sdbj	-1, -1,		/* type 5-6 - undefined */
1511.1Sdbj	FMT7SIZE,	/* type 7 - access error (68040) */
1521.1Sdbj	58,		/* type 8 - bus fault (68010) */
1531.1Sdbj	FMT9SIZE,	/* type 9 - coprocessor mid-instruction (68020/030) */
1541.1Sdbj	FMTASIZE,	/* type A - short bus fault (68020/030) */
1551.1Sdbj	FMTBSIZE,	/* type B - long bus fault (68020/030) */
1561.1Sdbj	-1, -1, -1, -1	/* type C-F - undefined */
1571.1Sdbj};
1581.1Sdbj
1591.16Sdbj#ifdef M68060
1601.16Sdbj#define	KDFAULT_060(c)	(cputype == CPU_68060 && ((c) & FSLW_TM_SV))
1611.16Sdbj#define	WRFAULT_060(c)	(cputype == CPU_68060 && ((c) & FSLW_RW_W))
1621.16Sdbj#else
1631.16Sdbj#define	KDFAULT_060(c)	0
1641.16Sdbj#define	WRFAULT_060(c)	0
1651.16Sdbj#endif
1661.16Sdbj
1671.1Sdbj#ifdef M68040
1681.16Sdbj#define	KDFAULT_040(c)	(cputype == CPU_68040 && \
1691.16Sdbj			 ((c) & SSW4_TMMASK) == SSW4_TMKD)
1701.16Sdbj#define	WRFAULT_040(c)	(cputype == CPU_68040 && \
1711.16Sdbj			 ((c) & SSW4_RW) == 0)
1721.16Sdbj#else
1731.16Sdbj#define	KDFAULT_040(c)	0
1741.16Sdbj#define	WRFAULT_040(c)	0
1751.16Sdbj#endif
1761.16Sdbj
1771.16Sdbj#if defined(M68030) || defined(M68020)
1781.16Sdbj#define	KDFAULT_OTH(c)	(cputype <= CPU_68030 && \
1791.16Sdbj			 ((c) & (SSW_DF|SSW_FCMASK)) == (SSW_DF|FC_SUPERD))
1801.16Sdbj#define	WRFAULT_OTH(c)	(cputype <= CPU_68030 && \
1811.16Sdbj			 ((c) & (SSW_DF|SSW_RW)) == SSW_DF)
1821.1Sdbj#else
1831.16Sdbj#define	KDFAULT_OTH(c)	0
1841.16Sdbj#define	WRFAULT_OTH(c)	0
1851.1Sdbj#endif
1861.1Sdbj
1871.16Sdbj#define	KDFAULT(c)	(KDFAULT_060(c) || KDFAULT_040(c) || KDFAULT_OTH(c))
1881.16Sdbj#define	WRFAULT(c)	(WRFAULT_060(c) || WRFAULT_040(c) || WRFAULT_OTH(c))
1891.16Sdbj
1901.1Sdbj#ifdef DEBUG
1911.1Sdbjint mmudebug = 0;
1921.1Sdbjint mmupid = -1;
1931.1Sdbj#define MDB_FOLLOW	1
1941.1Sdbj#define MDB_WBFOLLOW	2
1951.1Sdbj#define MDB_WBFAILED	4
1961.16Sdbj#define MDB_ISPID(p)	((p) == mmupid)
1971.1Sdbj#endif
1981.1Sdbj
1991.16Sdbj
2001.1Sdbj#define NSIR	32
2011.16Sdbjvoid (*sir_routines[NSIR])(void *);
2021.1Sdbjvoid *sir_args[NSIR];
2031.1Sdbjint next_sir;
2041.1Sdbj
2051.1Sdbj/*
2061.1Sdbj * trap and syscall both need the following work done before returning
2071.1Sdbj * to user mode.
2081.1Sdbj */
2091.1Sdbjstatic inline void
2101.1Sdbjuserret(p, fp, oticks, faultaddr, fromtrap)
2111.1Sdbj	struct proc *p;
2121.1Sdbj	struct frame *fp;
2131.1Sdbj	u_quad_t oticks;
2141.1Sdbj	u_int faultaddr;
2151.1Sdbj	int fromtrap;
2161.1Sdbj{
2171.20Sthorpej	int sig;
2181.1Sdbj#ifdef M68040
2191.1Sdbj	int beenhere = 0;
2201.1Sdbj
2211.1Sdbjagain:
2221.1Sdbj#endif
2231.1Sdbj	/* take pending signals */
2241.1Sdbj	while ((sig = CURSIG(p)) != 0)
2251.1Sdbj		postsig(sig);
2261.1Sdbj	p->p_priority = p->p_usrpri;
2271.1Sdbj	if (want_resched) {
2281.1Sdbj		/*
2291.20Sthorpej		 * We are being preempted.
2301.1Sdbj		 */
2311.20Sthorpej		preempt(NULL);
2321.1Sdbj		while ((sig = CURSIG(p)) != 0)
2331.1Sdbj			postsig(sig);
2341.1Sdbj	}
2351.1Sdbj
2361.1Sdbj	/*
2371.1Sdbj	 * If profiling, charge system time to the trapped pc.
2381.1Sdbj	 */
2391.1Sdbj	if (p->p_flag & P_PROFIL) {
2401.1Sdbj		extern int psratio;
2411.1Sdbj
2421.1Sdbj		addupc_task(p, fp->f_pc,
2431.1Sdbj			    (int)(p->p_sticks - oticks) * psratio);
2441.1Sdbj	}
2451.1Sdbj#ifdef M68040
2461.1Sdbj	/*
2471.1Sdbj	 * Deal with user mode writebacks (from trap, or from sigreturn).
2481.1Sdbj	 * If any writeback fails, go back and attempt signal delivery.
2491.1Sdbj	 * unless we have already been here and attempted the writeback
2501.1Sdbj	 * (e.g. bad address with user ignoring SIGSEGV).  In that case
2511.1Sdbj	 * we just return to the user without sucessfully completing
2521.1Sdbj	 * the writebacks.  Maybe we should just drop the sucker?
2531.1Sdbj	 */
2541.16Sdbj	if (cputype == CPU_68040 && fp->f_format == FMT7) {
2551.1Sdbj		if (beenhere) {
2561.1Sdbj#ifdef DEBUG
2571.1Sdbj			if (mmudebug & MDB_WBFAILED)
2581.1Sdbj				printf(fromtrap ?
2591.1Sdbj		"pid %d(%s): writeback aborted, pc=%x, fa=%x\n" :
2601.1Sdbj		"pid %d(%s): writeback aborted in sigreturn, pc=%x\n",
2611.1Sdbj				    p->p_pid, p->p_comm, fp->f_pc, faultaddr);
2621.1Sdbj#endif
2631.16Sdbj		} else if ((sig = writeback(fp, fromtrap))) {
2641.1Sdbj			beenhere = 1;
2651.1Sdbj			oticks = p->p_sticks;
2661.1Sdbj			trapsignal(p, sig, faultaddr);
2671.1Sdbj			goto again;
2681.1Sdbj		}
2691.1Sdbj	}
2701.1Sdbj#endif
2711.1Sdbj	curpriority = p->p_priority;
2721.1Sdbj}
2731.1Sdbj
2741.1Sdbj/*
2751.1Sdbj * Trap is called from locore to handle most types of processor traps,
2761.1Sdbj * including events such as simulated software interrupts/AST's.
2771.1Sdbj * System calls are broken out for efficiency.
2781.1Sdbj */
2791.1Sdbj/*ARGSUSED*/
2801.16Sdbjvoid
2811.1Sdbjtrap(type, code, v, frame)
2821.1Sdbj	int type;
2831.1Sdbj	unsigned code;
2841.1Sdbj	unsigned v;
2851.1Sdbj	struct frame frame;
2861.1Sdbj{
2871.1Sdbj	extern char fubail[], subail[];
2881.1Sdbj	struct proc *p;
2891.16Sdbj	int i, s;
2901.1Sdbj	u_int ucode;
2911.16Sdbj	u_quad_t sticks = 0 /* XXX initialiser works around compiler bug */;
2921.7Sdbj	int bit;
2931.1Sdbj
2941.7Sdbj	uvmexp.traps++;
2951.1Sdbj	p = curproc;
2961.1Sdbj	ucode = 0;
2971.16Sdbj
2981.16Sdbj	/* I have verified that this DOES happen! -gwr */
2991.16Sdbj	if (p == NULL)
3001.16Sdbj		p = &proc0;
3011.16Sdbj#ifdef DIAGNOSTIC
3021.16Sdbj	if (p->p_addr == NULL)
3031.16Sdbj		panic("trap: no pcb");
3041.16Sdbj#endif
3051.16Sdbj
3061.1Sdbj	if (USERMODE(frame.f_sr)) {
3071.1Sdbj		type |= T_USER;
3081.1Sdbj		sticks = p->p_sticks;
3091.1Sdbj		p->p_md.md_regs = frame.f_regs;
3101.1Sdbj	}
3111.1Sdbj	switch (type) {
3121.1Sdbj
3131.1Sdbj	default:
3141.14Sdbj	dopanic:
3151.16Sdbj		printf("trap type %d, code = 0x%x, v = 0x%x\n", type, code, v);
3161.16Sdbj		printf("%s program counter = 0x%x\n",
3171.16Sdbj		    (type & T_USER) ? "user" : "kernel", frame.f_pc);
3181.14Sdbj		/*
3191.14Sdbj		 * Let the kernel debugger see the trap frame that
3201.14Sdbj		 * caused us to panic.  This is a convenience so
3211.14Sdbj		 * one can see registers at the point of failure.
3221.14Sdbj		 */
3231.16Sdbj		s = splhigh();
3241.14Sdbj#ifdef KGDB
3251.14Sdbj		/* If connected, step or cont returns 1 */
3261.16Sdbj		if (kgdb_trap(type, &frame))
3271.14Sdbj			goto kgdb_cont;
3281.14Sdbj#endif
3291.16Sdbj#ifdef DDB
3301.16Sdbj		(void)kdb_trap(type, (db_regs_t *)&frame);
3311.1Sdbj#endif
3321.14Sdbj#ifdef KGDB
3331.14Sdbj	kgdb_cont:
3341.14Sdbj#endif
3351.16Sdbj		splx(s);
3361.14Sdbj		if (panicstr) {
3371.16Sdbj			printf("trap during panic!\n");
3381.16Sdbj#ifdef DEBUG
3391.16Sdbj			/* XXX should be a machine-dependent hook */
3401.16Sdbj			printf("(press a key)\n"); (void)cngetc();
3411.16Sdbj#endif
3421.14Sdbj		}
3431.1Sdbj		regdump((struct trapframe *)&frame, 128);
3441.1Sdbj		type &= ~T_USER;
3451.16Sdbj		if ((u_int)type < trap_types)
3461.1Sdbj			panic(trap_type[type]);
3471.1Sdbj		panic("trap");
3481.1Sdbj
3491.1Sdbj	case T_BUSERR:		/* kernel bus error */
3501.16Sdbj		if (p->p_addr->u_pcb.pcb_onfault == 0)
3511.1Sdbj			goto dopanic;
3521.16Sdbj		/* FALLTHROUGH */
3531.16Sdbj
3541.16Sdbj	copyfault:
3551.1Sdbj		/*
3561.1Sdbj		 * If we have arranged to catch this fault in any of the
3571.1Sdbj		 * copy to/from user space routines, set PC to return to
3581.1Sdbj		 * indicated location and set flag informing buserror code
3591.1Sdbj		 * that it may need to clean up stack frame.
3601.1Sdbj		 */
3611.1Sdbj		frame.f_stackadj = exframesize[frame.f_format];
3621.1Sdbj		frame.f_format = frame.f_vector = 0;
3631.1Sdbj		frame.f_pc = (int) p->p_addr->u_pcb.pcb_onfault;
3641.1Sdbj		return;
3651.1Sdbj
3661.1Sdbj	case T_BUSERR|T_USER:	/* bus error */
3671.1Sdbj	case T_ADDRERR|T_USER:	/* address error */
3681.1Sdbj		ucode = v;
3691.1Sdbj		i = SIGBUS;
3701.1Sdbj		break;
3711.1Sdbj
3721.1Sdbj	case T_COPERR:		/* kernel coprocessor violation */
3731.1Sdbj	case T_FMTERR|T_USER:	/* do all RTE errors come in as T_USER? */
3741.1Sdbj	case T_FMTERR:		/* ...just in case... */
3751.1Sdbj	/*
3761.1Sdbj	 * The user has most likely trashed the RTE or FP state info
3771.1Sdbj	 * in the stack frame of a signal handler.
3781.1Sdbj	 */
3791.1Sdbj		printf("pid %d: kernel %s exception\n", p->p_pid,
3801.1Sdbj		       type==T_COPERR ? "coprocessor" : "format");
3811.1Sdbj		type |= T_USER;
3821.6Sthorpej		p->p_sigacts->ps_sigact[SIGILL].sa_handler = SIG_DFL;
3831.6Sthorpej		sigdelset(&p->p_sigignore, SIGILL);
3841.6Sthorpej		sigdelset(&p->p_sigcatch, SIGILL);
3851.6Sthorpej		sigdelset(&p->p_sigmask, SIGILL);
3861.1Sdbj		i = SIGILL;
3871.1Sdbj		ucode = frame.f_format;	/* XXX was ILL_RESAD_FAULT */
3881.1Sdbj		break;
3891.1Sdbj
3901.1Sdbj	case T_COPERR|T_USER:	/* user coprocessor violation */
3911.1Sdbj	/* What is a proper response here? */
3921.1Sdbj		ucode = 0;
3931.1Sdbj		i = SIGFPE;
3941.1Sdbj		break;
3951.1Sdbj
3961.1Sdbj	case T_FPERR|T_USER:	/* 68881 exceptions */
3971.1Sdbj	/*
3981.7Sdbj	 * We pass along the 68881 status register which locore stashed
3991.1Sdbj	 * in code for us.  Note that there is a possibility that the
4001.7Sdbj	 * bit pattern of this register will conflict with one of the
4011.1Sdbj	 * FPE_* codes defined in signal.h.  Fortunately for us, the
4021.1Sdbj	 * only such codes we use are all in the range 1-7 and the low
4031.7Sdbj	 * 3 bits of the status register are defined as 0 so there is
4041.1Sdbj	 * no clash.
4051.1Sdbj	 */
4061.1Sdbj		ucode = code;
4071.1Sdbj		i = SIGFPE;
4081.1Sdbj		break;
4091.1Sdbj
4101.1Sdbj#ifdef M68040
4111.1Sdbj	case T_FPEMULI|T_USER:	/* unimplemented FP instuction */
4121.1Sdbj	case T_FPEMULD|T_USER:	/* unimplemented FP data type */
4131.1Sdbj		/* XXX need to FSAVE */
4141.1Sdbj		printf("pid %d(%s): unimplemented FP %s at %x (EA %x)\n",
4151.1Sdbj		       p->p_pid, p->p_comm,
4161.1Sdbj		       frame.f_format == 2 ? "instruction" : "data type",
4171.1Sdbj		       frame.f_pc, frame.f_fmt2.f_iaddr);
4181.1Sdbj		/* XXX need to FRESTORE */
4191.1Sdbj		i = SIGFPE;
4201.1Sdbj		break;
4211.1Sdbj#endif
4221.1Sdbj
4231.1Sdbj	case T_ILLINST|T_USER:	/* illegal instruction fault */
4241.1Sdbj#ifdef COMPAT_HPUX
4251.1Sdbj		if (p->p_emul == &emul_hpux) {
4261.1Sdbj			ucode = HPUX_ILL_ILLINST_TRAP;
4271.1Sdbj			i = SIGILL;
4281.1Sdbj			break;
4291.1Sdbj		}
4301.1Sdbj		/* fall through */
4311.1Sdbj#endif
4321.1Sdbj	case T_PRIVINST|T_USER:	/* privileged instruction fault */
4331.1Sdbj#ifdef COMPAT_HPUX
4341.1Sdbj		if (p->p_emul == &emul_hpux)
4351.1Sdbj			ucode = HPUX_ILL_PRIV_TRAP;
4361.1Sdbj		else
4371.1Sdbj#endif
4381.1Sdbj		ucode = frame.f_format;	/* XXX was ILL_PRIVIN_FAULT */
4391.1Sdbj		i = SIGILL;
4401.1Sdbj		break;
4411.1Sdbj
4421.1Sdbj	case T_ZERODIV|T_USER:	/* Divide by zero */
4431.1Sdbj#ifdef COMPAT_HPUX
4441.1Sdbj		if (p->p_emul == &emul_hpux)
4451.1Sdbj			ucode = HPUX_FPE_INTDIV_TRAP;
4461.1Sdbj		else
4471.1Sdbj#endif
4481.1Sdbj		ucode = frame.f_format;	/* XXX was FPE_INTDIV_TRAP */
4491.1Sdbj		i = SIGFPE;
4501.1Sdbj		break;
4511.1Sdbj
4521.1Sdbj	case T_CHKINST|T_USER:	/* CHK instruction trap */
4531.1Sdbj#ifdef COMPAT_HPUX
4541.1Sdbj		if (p->p_emul == &emul_hpux) {
4551.1Sdbj			/* handled differently under hp-ux */
4561.1Sdbj			i = SIGILL;
4571.1Sdbj			ucode = HPUX_ILL_CHK_TRAP;
4581.1Sdbj			break;
4591.1Sdbj		}
4601.1Sdbj#endif
4611.1Sdbj		ucode = frame.f_format;	/* XXX was FPE_SUBRNG_TRAP */
4621.1Sdbj		i = SIGFPE;
4631.1Sdbj		break;
4641.1Sdbj
4651.1Sdbj	case T_TRAPVINST|T_USER:	/* TRAPV instruction trap */
4661.1Sdbj#ifdef COMPAT_HPUX
4671.1Sdbj		if (p->p_emul == &emul_hpux) {
4681.1Sdbj			/* handled differently under hp-ux */
4691.1Sdbj			i = SIGILL;
4701.1Sdbj			ucode = HPUX_ILL_TRAPV_TRAP;
4711.1Sdbj			break;
4721.1Sdbj		}
4731.1Sdbj#endif
4741.1Sdbj		ucode = frame.f_format;	/* XXX was FPE_INTOVF_TRAP */
4751.1Sdbj		i = SIGFPE;
4761.1Sdbj		break;
4771.1Sdbj
4781.1Sdbj	/*
4791.1Sdbj	 * XXX: Trace traps are a nightmare.
4801.1Sdbj	 *
4811.1Sdbj	 *	HP-UX uses trap #1 for breakpoints,
4821.16Sdbj	 *	NetBSD/m68k uses trap #2,
4831.1Sdbj	 *	SUN 3.x uses trap #15,
4841.16Sdbj	 *	DDB and KGDB uses trap #15 (for kernel breakpoints;
4851.16Sdbj	 *	handled elsewhere).
4861.1Sdbj	 *
4871.16Sdbj	 * NetBSD and HP-UX traps both get mapped by locore.s into T_TRACE.
4881.1Sdbj	 * SUN 3.x traps get passed through as T_TRAP15 and are not really
4891.1Sdbj	 * supported yet.
4901.16Sdbj	 *
4911.17Sitohy	 * XXX: We should never get kernel-mode T_TRAP15
4921.16Sdbj	 * XXX: because locore.s now gives them special treatment.
4931.1Sdbj	 */
4941.16Sdbj	case T_TRAP15:		/* kernel breakpoint */
4951.16Sdbj#ifdef DEBUG
4961.16Sdbj		printf("unexpected kernel trace trap, type = %d\n", type);
4971.16Sdbj		printf("program counter = 0x%x\n", frame.f_pc);
4981.1Sdbj#endif
4991.1Sdbj		frame.f_sr &= ~PSL_T;
5001.16Sdbj		return;
5011.1Sdbj
5021.1Sdbj	case T_TRACE|T_USER:	/* user trace trap */
5031.1Sdbj#ifdef COMPAT_SUNOS
5041.1Sdbj		/*
5051.1Sdbj		 * SunOS uses Trap #2 for a "CPU cache flush".
5061.1Sdbj		 * Just flush the on-chip caches and return.
5071.1Sdbj		 */
5081.1Sdbj		if (p->p_emul == &emul_sunos) {
5091.1Sdbj			ICIA();
5101.1Sdbj			DCIU();
5111.1Sdbj			return;
5121.1Sdbj		}
5131.16Sdbj#endif
5141.17Sitohy		/* FALLTHROUGH */
5151.17Sitohy	case T_TRACE:		/* tracing a trap instruction */
5161.17Sitohy	case T_TRAP15|T_USER:	/* SUN user trace trap */
5171.1Sdbj		frame.f_sr &= ~PSL_T;
5181.1Sdbj		i = SIGTRAP;
5191.1Sdbj		break;
5201.1Sdbj
5211.1Sdbj	case T_ASTFLT:		/* system async trap, cannot happen */
5221.1Sdbj		goto dopanic;
5231.1Sdbj
5241.1Sdbj	case T_ASTFLT|T_USER:	/* user async trap */
5251.1Sdbj		astpending = 0;
5261.1Sdbj		/*
5271.1Sdbj		 * We check for software interrupts first.  This is because
5281.1Sdbj		 * they are at a higher level than ASTs, and on a VAX would
5291.1Sdbj		 * interrupt the AST.  We assume that if we are processing
5301.1Sdbj		 * an AST that we must be at IPL0 so we don't bother to
5311.1Sdbj		 * check.  Note that we ensure that we are at least at SIR
5321.1Sdbj		 * IPL while processing the SIR.
5331.1Sdbj		 */
5341.1Sdbj		spl1();
5351.1Sdbj		/* fall into... */
5361.1Sdbj
5371.1Sdbj	case T_SSIR:		/* software interrupt */
5381.1Sdbj	case T_SSIR|T_USER:
5391.16Sdbj		while ((bit = ffs(ssir))) {
5401.1Sdbj			--bit;
5411.1Sdbj			ssir &= ~(1 << bit);
5421.7Sdbj			uvmexp.softs++;
5431.1Sdbj			if (sir_routines[bit])
5441.1Sdbj				sir_routines[bit](sir_args[bit]);
5451.1Sdbj		}
5461.1Sdbj		/*
5471.1Sdbj		 * If this was not an AST trap, we are all done.
5481.1Sdbj		 */
5491.1Sdbj		if (type != (T_ASTFLT|T_USER)) {
5501.16Sdbj			uvmexp.traps--;
5511.1Sdbj			return;
5521.1Sdbj		}
5531.1Sdbj		spl0();
5541.1Sdbj		if (p->p_flag & P_OWEUPC) {
5551.1Sdbj			p->p_flag &= ~P_OWEUPC;
5561.1Sdbj			ADDUPROF(p);
5571.1Sdbj		}
5581.1Sdbj		goto out;
5591.1Sdbj
5601.1Sdbj	case T_MMUFLT:		/* kernel mode page fault */
5611.1Sdbj		/*
5621.1Sdbj		 * If we were doing profiling ticks or other user mode
5631.1Sdbj		 * stuff from interrupt code, Just Say No.
5641.1Sdbj		 */
5651.1Sdbj		if (p->p_addr->u_pcb.pcb_onfault == fubail ||
5661.1Sdbj		    p->p_addr->u_pcb.pcb_onfault == subail)
5671.1Sdbj			goto copyfault;
5681.1Sdbj		/* fall into ... */
5691.1Sdbj
5701.1Sdbj	case T_MMUFLT|T_USER:	/* page fault */
5711.1Sdbj	    {
5721.7Sdbj		vaddr_t va;
5731.1Sdbj		struct vmspace *vm = p->p_vmspace;
5741.1Sdbj		vm_map_t map;
5751.1Sdbj		int rv;
5761.1Sdbj		vm_prot_t ftype;
5771.1Sdbj		extern vm_map_t kernel_map;
5781.1Sdbj
5791.1Sdbj#ifdef DEBUG
5801.1Sdbj		if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
5811.1Sdbj		printf("trap: T_MMUFLT pid=%d, code=%x, v=%x, pc=%x, sr=%x\n",
5821.1Sdbj		       p->p_pid, code, v, frame.f_pc, frame.f_sr);
5831.1Sdbj#endif
5841.1Sdbj		/*
5851.1Sdbj		 * It is only a kernel address space fault iff:
5861.1Sdbj		 * 	1. (type & T_USER) == 0  and
5871.1Sdbj		 * 	2. pcb_onfault not set or
5881.1Sdbj		 *	3. pcb_onfault set but supervisor space data fault
5891.1Sdbj		 * The last can occur during an exec() copyin where the
5901.1Sdbj		 * argument space is lazy-allocated.
5911.1Sdbj		 */
5921.16Sdbj		if ((type & T_USER) == 0 &&
5931.16Sdbj		    ((p->p_addr->u_pcb.pcb_onfault == 0) || KDFAULT(code)))
5941.1Sdbj			map = kernel_map;
5951.1Sdbj		else
5961.16Sdbj			map = vm ? &vm->vm_map : kernel_map;
5971.16Sdbj
5981.1Sdbj		if (WRFAULT(code))
5991.1Sdbj			ftype = VM_PROT_READ | VM_PROT_WRITE;
6001.1Sdbj		else
6011.1Sdbj			ftype = VM_PROT_READ;
6021.16Sdbj
6031.7Sdbj		va = trunc_page((vaddr_t)v);
6041.16Sdbj
6051.1Sdbj		if (map == kernel_map && va == 0) {
6061.16Sdbj			printf("trap: bad kernel %s access at 0x%x\n",
6071.16Sdbj			    (ftype & VM_PROT_WRITE) ? "read/write" :
6081.16Sdbj			    "read", v);
6091.1Sdbj			goto dopanic;
6101.1Sdbj		}
6111.16Sdbj
6121.1Sdbj#ifdef COMPAT_HPUX
6131.1Sdbj		if (ISHPMMADDR(va)) {
6141.16Sdbj			int pmap_mapmulti __P((pmap_t, vaddr_t));
6151.7Sdbj			vaddr_t bva;
6161.1Sdbj
6171.1Sdbj			rv = pmap_mapmulti(map->pmap, va);
6181.1Sdbj			if (rv != KERN_SUCCESS) {
6191.1Sdbj				bva = HPMMBASEADDR(va);
6201.7Sdbj				rv = uvm_fault(map, bva, 0, ftype);
6211.1Sdbj				if (rv == KERN_SUCCESS)
6221.1Sdbj					(void) pmap_mapmulti(map->pmap, va);
6231.1Sdbj			}
6241.1Sdbj		} else
6251.1Sdbj#endif
6261.7Sdbj		rv = uvm_fault(map, va, 0, ftype);
6271.7Sdbj#ifdef DEBUG
6281.7Sdbj		if (rv && MDB_ISPID(p->p_pid))
6291.7Sdbj			printf("uvm_fault(%p, 0x%lx, 0, 0x%x) -> 0x%x\n",
6301.16Sdbj			    map, va, ftype, rv);
6311.7Sdbj#endif
6321.1Sdbj		/*
6331.1Sdbj		 * If this was a stack access we keep track of the maximum
6341.1Sdbj		 * accessed stack size.  Also, if vm_fault gets a protection
6351.1Sdbj		 * failure it is due to accessing the stack region outside
6361.1Sdbj		 * the current limit and we need to reflect that as an access
6371.1Sdbj		 * error.
6381.1Sdbj		 */
6391.16Sdbj		if ((vm != NULL && (caddr_t)va >= vm->vm_maxsaddr)
6401.16Sdbj		    && map != kernel_map) {
6411.1Sdbj			if (rv == KERN_SUCCESS) {
6421.1Sdbj				unsigned nss;
6431.1Sdbj
6441.19Sragge				nss = btoc(USRSTACK-(unsigned)va);
6451.1Sdbj				if (nss > vm->vm_ssize)
6461.1Sdbj					vm->vm_ssize = nss;
6471.1Sdbj			} else if (rv == KERN_PROTECTION_FAILURE)
6481.1Sdbj				rv = KERN_INVALID_ADDRESS;
6491.1Sdbj		}
6501.1Sdbj		if (rv == KERN_SUCCESS) {
6511.1Sdbj			if (type == T_MMUFLT) {
6521.16Sdbj#ifdef M68040
6531.16Sdbj				if (cputype == CPU_68040)
6541.1Sdbj					(void) writeback(&frame, 1);
6551.1Sdbj#endif
6561.1Sdbj				return;
6571.1Sdbj			}
6581.1Sdbj			goto out;
6591.1Sdbj		}
6601.1Sdbj		if (type == T_MMUFLT) {
6611.1Sdbj			if (p->p_addr->u_pcb.pcb_onfault)
6621.1Sdbj				goto copyfault;
6631.7Sdbj			printf("uvm_fault(%p, 0x%lx, 0, 0x%x) -> 0x%x\n",
6641.16Sdbj			    map, va, ftype, rv);
6651.1Sdbj			printf("  type %x, code [mmu,,ssw]: %x\n",
6661.1Sdbj			       type, code);
6671.1Sdbj			goto dopanic;
6681.1Sdbj		}
6691.1Sdbj		ucode = v;
6701.11Schs		if (rv == KERN_RESOURCE_SHORTAGE) {
6711.11Schs			printf("UVM: pid %d (%s), uid %d killed: out of swap\n",
6721.11Schs			       p->p_pid, p->p_comm,
6731.11Schs			       p->p_cred && p->p_ucred ?
6741.11Schs			       p->p_ucred->cr_uid : -1);
6751.11Schs			i = SIGKILL;
6761.11Schs		} else {
6771.11Schs			i = SIGSEGV;
6781.11Schs		}
6791.1Sdbj		break;
6801.1Sdbj	    }
6811.1Sdbj	}
6821.1Sdbj	trapsignal(p, i, ucode);
6831.1Sdbj	if ((type & T_USER) == 0)
6841.1Sdbj		return;
6851.1Sdbjout:
6861.1Sdbj	userret(p, &frame, sticks, v, 1);
6871.1Sdbj}
6881.1Sdbj
6891.1Sdbj#ifdef M68040
6901.1Sdbj#ifdef DEBUG
6911.1Sdbjstruct writebackstats {
6921.1Sdbj	int calls;
6931.1Sdbj	int cpushes;
6941.1Sdbj	int move16s;
6951.1Sdbj	int wb1s, wb2s, wb3s;
6961.1Sdbj	int wbsize[4];
6971.1Sdbj} wbstats;
6981.1Sdbj
6991.1Sdbjchar *f7sz[] = { "longword", "byte", "word", "line" };
7001.1Sdbjchar *f7tt[] = { "normal", "MOVE16", "AFC", "ACK" };
7011.1Sdbjchar *f7tm[] = { "d-push", "u-data", "u-code", "M-data",
7021.1Sdbj		 "M-code", "k-data", "k-code", "RES" };
7031.1Sdbjchar wberrstr[] =
7041.16Sdbj    "WARNING: pid %d(%s) writeback [%s] failed, pc=%x fa=%x wba=%x wbd=%x\n";
7051.1Sdbj#endif
7061.1Sdbj
7071.16Sdbjint
7081.1Sdbjwriteback(fp, docachepush)
7091.1Sdbj	struct frame *fp;
7101.1Sdbj	int docachepush;
7111.1Sdbj{
7121.1Sdbj	struct fmt7 *f = &fp->f_fmt7;
7131.1Sdbj	struct proc *p = curproc;
7141.1Sdbj	int err = 0;
7151.1Sdbj	u_int fa;
7161.1Sdbj	caddr_t oonfault = p->p_addr->u_pcb.pcb_onfault;
7171.15Sthorpej	paddr_t pa;
7181.1Sdbj
7191.1Sdbj#ifdef DEBUG
7201.1Sdbj	if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid)) {
7211.1Sdbj		printf(" pid=%d, fa=%x,", p->p_pid, f->f_fa);
7221.1Sdbj		dumpssw(f->f_ssw);
7231.1Sdbj	}
7241.1Sdbj	wbstats.calls++;
7251.1Sdbj#endif
7261.1Sdbj	/*
7271.1Sdbj	 * Deal with special cases first.
7281.1Sdbj	 */
7291.1Sdbj	if ((f->f_ssw & SSW4_TMMASK) == SSW4_TMDCP) {
7301.1Sdbj		/*
7311.1Sdbj		 * Dcache push fault.
7321.1Sdbj		 * Line-align the address and write out the push data to
7331.1Sdbj		 * the indicated physical address.
7341.1Sdbj		 */
7351.1Sdbj#ifdef DEBUG
7361.1Sdbj		if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid)) {
7371.1Sdbj			printf(" pushing %s to PA %x, data %x",
7381.1Sdbj			       f7sz[(f->f_ssw & SSW4_SZMASK) >> 5],
7391.1Sdbj			       f->f_fa, f->f_pd0);
7401.1Sdbj			if ((f->f_ssw & SSW4_SZMASK) == SSW4_SZLN)
7411.1Sdbj				printf("/%x/%x/%x",
7421.1Sdbj				       f->f_pd1, f->f_pd2, f->f_pd3);
7431.1Sdbj			printf("\n");
7441.1Sdbj		}
7451.1Sdbj		if (f->f_wb1s & SSW4_WBSV)
7461.1Sdbj			panic("writeback: cache push with WB1S valid");
7471.1Sdbj		wbstats.cpushes++;
7481.1Sdbj#endif
7491.1Sdbj		/*
7501.1Sdbj		 * XXX there are security problems if we attempt to do a
7511.1Sdbj		 * cache push after a signal handler has been called.
7521.1Sdbj		 */
7531.1Sdbj		if (docachepush) {
7541.7Sdbj			pmap_enter(pmap_kernel(), (vaddr_t)vmmap,
7551.18Sthorpej			    trunc_page(f->f_fa), VM_PROT_WRITE,
7561.18Sthorpej			    VM_PROT_WRITE|PMAP_WIRED);
7571.1Sdbj			fa = (u_int)&vmmap[(f->f_fa & PGOFSET) & ~0xF];
7581.1Sdbj			bcopy((caddr_t)&f->f_pd0, (caddr_t)fa, 16);
7591.15Sthorpej			(void) pmap_extract(pmap_kernel(), (vaddr_t)fa, &pa);
7601.15Sthorpej			DCFL(pa);
7611.7Sdbj			pmap_remove(pmap_kernel(), (vaddr_t)vmmap,
7621.7Sdbj				    (vaddr_t)&vmmap[NBPG]);
7631.1Sdbj		} else
7641.1Sdbj			printf("WARNING: pid %d(%s) uid %d: CPUSH not done\n",
7651.1Sdbj			       p->p_pid, p->p_comm, p->p_ucred->cr_uid);
7661.1Sdbj	} else if ((f->f_ssw & (SSW4_RW|SSW4_TTMASK)) == SSW4_TTM16) {
7671.1Sdbj		/*
7681.1Sdbj		 * MOVE16 fault.
7691.1Sdbj		 * Line-align the address and write out the push data to
7701.1Sdbj		 * the indicated virtual address.
7711.1Sdbj		 */
7721.1Sdbj#ifdef DEBUG
7731.1Sdbj		if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
7741.1Sdbj			printf(" MOVE16 to VA %x(%x), data %x/%x/%x/%x\n",
7751.1Sdbj			       f->f_fa, f->f_fa & ~0xF, f->f_pd0, f->f_pd1,
7761.1Sdbj			       f->f_pd2, f->f_pd3);
7771.1Sdbj		if (f->f_wb1s & SSW4_WBSV)
7781.1Sdbj			panic("writeback: MOVE16 with WB1S valid");
7791.1Sdbj		wbstats.move16s++;
7801.1Sdbj#endif
7811.1Sdbj		if (KDFAULT(f->f_wb1s))
7821.1Sdbj			bcopy((caddr_t)&f->f_pd0, (caddr_t)(f->f_fa & ~0xF), 16);
7831.1Sdbj		else
7841.1Sdbj			err = suline((caddr_t)(f->f_fa & ~0xF), (caddr_t)&f->f_pd0);
7851.1Sdbj		if (err) {
7861.1Sdbj			fa = f->f_fa & ~0xF;
7871.1Sdbj#ifdef DEBUG
7881.1Sdbj			if (mmudebug & MDB_WBFAILED)
7891.1Sdbj				printf(wberrstr, p->p_pid, p->p_comm,
7901.1Sdbj				       "MOVE16", fp->f_pc, f->f_fa,
7911.1Sdbj				       f->f_fa & ~0xF, f->f_pd0);
7921.1Sdbj#endif
7931.1Sdbj		}
7941.1Sdbj	} else if (f->f_wb1s & SSW4_WBSV) {
7951.1Sdbj		/*
7961.1Sdbj		 * Writeback #1.
7971.1Sdbj		 * Position the "memory-aligned" data and write it out.
7981.1Sdbj		 */
7991.1Sdbj		u_int wb1d = f->f_wb1d;
8001.1Sdbj		int off;
8011.1Sdbj
8021.1Sdbj#ifdef DEBUG
8031.1Sdbj		if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
8041.1Sdbj			dumpwb(1, f->f_wb1s, f->f_wb1a, f->f_wb1d);
8051.1Sdbj		wbstats.wb1s++;
8061.1Sdbj		wbstats.wbsize[(f->f_wb2s&SSW4_SZMASK)>>5]++;
8071.1Sdbj#endif
8081.1Sdbj		off = (f->f_wb1a & 3) * 8;
8091.1Sdbj		switch (f->f_wb1s & SSW4_SZMASK) {
8101.1Sdbj		case SSW4_SZLW:
8111.1Sdbj			if (off)
8121.1Sdbj				wb1d = (wb1d >> (32 - off)) | (wb1d << off);
8131.1Sdbj			if (KDFAULT(f->f_wb1s))
8141.1Sdbj				*(long *)f->f_wb1a = wb1d;
8151.1Sdbj			else
8161.1Sdbj				err = suword((caddr_t)f->f_wb1a, wb1d);
8171.1Sdbj			break;
8181.1Sdbj		case SSW4_SZB:
8191.1Sdbj			off = 24 - off;
8201.1Sdbj			if (off)
8211.1Sdbj				wb1d >>= off;
8221.1Sdbj			if (KDFAULT(f->f_wb1s))
8231.1Sdbj				*(char *)f->f_wb1a = wb1d;
8241.1Sdbj			else
8251.1Sdbj				err = subyte((caddr_t)f->f_wb1a, wb1d);
8261.1Sdbj			break;
8271.1Sdbj		case SSW4_SZW:
8281.1Sdbj			off = (off + 16) % 32;
8291.1Sdbj			if (off)
8301.1Sdbj				wb1d = (wb1d >> (32 - off)) | (wb1d << off);
8311.1Sdbj			if (KDFAULT(f->f_wb1s))
8321.1Sdbj				*(short *)f->f_wb1a = wb1d;
8331.1Sdbj			else
8341.1Sdbj				err = susword((caddr_t)f->f_wb1a, wb1d);
8351.1Sdbj			break;
8361.1Sdbj		}
8371.1Sdbj		if (err) {
8381.1Sdbj			fa = f->f_wb1a;
8391.1Sdbj#ifdef DEBUG
8401.1Sdbj			if (mmudebug & MDB_WBFAILED)
8411.1Sdbj				printf(wberrstr, p->p_pid, p->p_comm,
8421.1Sdbj				       "#1", fp->f_pc, f->f_fa,
8431.1Sdbj				       f->f_wb1a, f->f_wb1d);
8441.1Sdbj#endif
8451.1Sdbj		}
8461.1Sdbj	}
8471.1Sdbj	/*
8481.1Sdbj	 * Deal with the "normal" writebacks.
8491.1Sdbj	 *
8501.1Sdbj	 * XXX writeback2 is known to reflect a LINE size writeback after
8511.1Sdbj	 * a MOVE16 was already dealt with above.  Ignore it.
8521.1Sdbj	 */
8531.1Sdbj	if (err == 0 && (f->f_wb2s & SSW4_WBSV) &&
8541.1Sdbj	    (f->f_wb2s & SSW4_SZMASK) != SSW4_SZLN) {
8551.1Sdbj#ifdef DEBUG
8561.1Sdbj		if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
8571.1Sdbj			dumpwb(2, f->f_wb2s, f->f_wb2a, f->f_wb2d);
8581.1Sdbj		wbstats.wb2s++;
8591.1Sdbj		wbstats.wbsize[(f->f_wb2s&SSW4_SZMASK)>>5]++;
8601.1Sdbj#endif
8611.1Sdbj		switch (f->f_wb2s & SSW4_SZMASK) {
8621.1Sdbj		case SSW4_SZLW:
8631.1Sdbj			if (KDFAULT(f->f_wb2s))
8641.1Sdbj				*(long *)f->f_wb2a = f->f_wb2d;
8651.1Sdbj			else
8661.1Sdbj				err = suword((caddr_t)f->f_wb2a, f->f_wb2d);
8671.1Sdbj			break;
8681.1Sdbj		case SSW4_SZB:
8691.1Sdbj			if (KDFAULT(f->f_wb2s))
8701.1Sdbj				*(char *)f->f_wb2a = f->f_wb2d;
8711.1Sdbj			else
8721.1Sdbj				err = subyte((caddr_t)f->f_wb2a, f->f_wb2d);
8731.1Sdbj			break;
8741.1Sdbj		case SSW4_SZW:
8751.1Sdbj			if (KDFAULT(f->f_wb2s))
8761.1Sdbj				*(short *)f->f_wb2a = f->f_wb2d;
8771.1Sdbj			else
8781.1Sdbj				err = susword((caddr_t)f->f_wb2a, f->f_wb2d);
8791.1Sdbj			break;
8801.1Sdbj		}
8811.1Sdbj		if (err) {
8821.1Sdbj			fa = f->f_wb2a;
8831.1Sdbj#ifdef DEBUG
8841.1Sdbj			if (mmudebug & MDB_WBFAILED) {
8851.1Sdbj				printf(wberrstr, p->p_pid, p->p_comm,
8861.1Sdbj				       "#2", fp->f_pc, f->f_fa,
8871.1Sdbj				       f->f_wb2a, f->f_wb2d);
8881.1Sdbj				dumpssw(f->f_ssw);
8891.1Sdbj				dumpwb(2, f->f_wb2s, f->f_wb2a, f->f_wb2d);
8901.1Sdbj			}
8911.1Sdbj#endif
8921.1Sdbj		}
8931.1Sdbj	}
8941.1Sdbj	if (err == 0 && (f->f_wb3s & SSW4_WBSV)) {
8951.1Sdbj#ifdef DEBUG
8961.1Sdbj		if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
8971.1Sdbj			dumpwb(3, f->f_wb3s, f->f_wb3a, f->f_wb3d);
8981.1Sdbj		wbstats.wb3s++;
8991.1Sdbj		wbstats.wbsize[(f->f_wb3s&SSW4_SZMASK)>>5]++;
9001.1Sdbj#endif
9011.1Sdbj		switch (f->f_wb3s & SSW4_SZMASK) {
9021.1Sdbj		case SSW4_SZLW:
9031.1Sdbj			if (KDFAULT(f->f_wb3s))
9041.1Sdbj				*(long *)f->f_wb3a = f->f_wb3d;
9051.1Sdbj			else
9061.1Sdbj				err = suword((caddr_t)f->f_wb3a, f->f_wb3d);
9071.1Sdbj			break;
9081.1Sdbj		case SSW4_SZB:
9091.1Sdbj			if (KDFAULT(f->f_wb3s))
9101.1Sdbj				*(char *)f->f_wb3a = f->f_wb3d;
9111.1Sdbj			else
9121.1Sdbj				err = subyte((caddr_t)f->f_wb3a, f->f_wb3d);
9131.1Sdbj			break;
9141.1Sdbj		case SSW4_SZW:
9151.1Sdbj			if (KDFAULT(f->f_wb3s))
9161.1Sdbj				*(short *)f->f_wb3a = f->f_wb3d;
9171.1Sdbj			else
9181.1Sdbj				err = susword((caddr_t)f->f_wb3a, f->f_wb3d);
9191.1Sdbj			break;
9201.1Sdbj#ifdef DEBUG
9211.1Sdbj		case SSW4_SZLN:
9221.1Sdbj			panic("writeback: wb3s indicates LINE write");
9231.1Sdbj#endif
9241.1Sdbj		}
9251.1Sdbj		if (err) {
9261.1Sdbj			fa = f->f_wb3a;
9271.1Sdbj#ifdef DEBUG
9281.1Sdbj			if (mmudebug & MDB_WBFAILED)
9291.1Sdbj				printf(wberrstr, p->p_pid, p->p_comm,
9301.1Sdbj				       "#3", fp->f_pc, f->f_fa,
9311.1Sdbj				       f->f_wb3a, f->f_wb3d);
9321.1Sdbj#endif
9331.1Sdbj		}
9341.1Sdbj	}
9351.1Sdbj	p->p_addr->u_pcb.pcb_onfault = oonfault;
9361.1Sdbj	if (err)
9371.1Sdbj		err = SIGSEGV;
9381.16Sdbj	return (err);
9391.1Sdbj}
9401.1Sdbj
9411.1Sdbj#ifdef DEBUG
9421.16Sdbjvoid
9431.1Sdbjdumpssw(ssw)
9441.1Sdbj	u_short ssw;
9451.1Sdbj{
9461.1Sdbj	printf(" SSW: %x: ", ssw);
9471.1Sdbj	if (ssw & SSW4_CP)
9481.1Sdbj		printf("CP,");
9491.1Sdbj	if (ssw & SSW4_CU)
9501.1Sdbj		printf("CU,");
9511.1Sdbj	if (ssw & SSW4_CT)
9521.1Sdbj		printf("CT,");
9531.1Sdbj	if (ssw & SSW4_CM)
9541.1Sdbj		printf("CM,");
9551.1Sdbj	if (ssw & SSW4_MA)
9561.1Sdbj		printf("MA,");
9571.1Sdbj	if (ssw & SSW4_ATC)
9581.1Sdbj		printf("ATC,");
9591.1Sdbj	if (ssw & SSW4_LK)
9601.1Sdbj		printf("LK,");
9611.1Sdbj	if (ssw & SSW4_RW)
9621.1Sdbj		printf("RW,");
9631.1Sdbj	printf(" SZ=%s, TT=%s, TM=%s\n",
9641.1Sdbj	       f7sz[(ssw & SSW4_SZMASK) >> 5],
9651.1Sdbj	       f7tt[(ssw & SSW4_TTMASK) >> 3],
9661.1Sdbj	       f7tm[ssw & SSW4_TMMASK]);
9671.1Sdbj}
9681.1Sdbj
9691.16Sdbjvoid
9701.1Sdbjdumpwb(num, s, a, d)
9711.1Sdbj	int num;
9721.1Sdbj	u_short s;
9731.1Sdbj	u_int a, d;
9741.1Sdbj{
9751.1Sdbj	struct proc *p = curproc;
9761.7Sdbj	paddr_t pa;
9771.1Sdbj
9781.1Sdbj	printf(" writeback #%d: VA %x, data %x, SZ=%s, TT=%s, TM=%s\n",
9791.1Sdbj	       num, a, d, f7sz[(s & SSW4_SZMASK) >> 5],
9801.1Sdbj	       f7tt[(s & SSW4_TTMASK) >> 3], f7tm[s & SSW4_TMMASK]);
9811.16Sdbj	printf("               PA ");
9821.15Sthorpej	if (pmap_extract(p->p_vmspace->vm_map.pmap, (vaddr_t)a, &pa) == FALSE)
9831.1Sdbj		printf("<invalid address>");
9841.1Sdbj	else
9851.16Sdbj		printf("%lx, current value %lx", pa, fuword((caddr_t)a));
9861.1Sdbj	printf("\n");
9871.1Sdbj}
9881.1Sdbj#endif
9891.1Sdbj#endif
9901.1Sdbj
9911.1Sdbj/*
9921.1Sdbj * Process a system call.
9931.1Sdbj */
9941.16Sdbjvoid
9951.1Sdbjsyscall(code, frame)
9961.16Sdbj	register_t code;
9971.1Sdbj	struct frame frame;
9981.1Sdbj{
9991.1Sdbj	caddr_t params;
10001.1Sdbj	struct sysent *callp;
10011.1Sdbj	struct proc *p;
10021.1Sdbj	int error, opc, nsys;
10031.1Sdbj	size_t argsize;
10041.16Sdbj	register_t args[8], rval[2];
10051.1Sdbj	u_quad_t sticks;
10061.1Sdbj
10071.7Sdbj	uvmexp.syscalls++;
10081.1Sdbj	if (!USERMODE(frame.f_sr))
10091.1Sdbj		panic("syscall");
10101.1Sdbj	p = curproc;
10111.1Sdbj	sticks = p->p_sticks;
10121.1Sdbj	p->p_md.md_regs = frame.f_regs;
10131.1Sdbj	opc = frame.f_pc;
10141.1Sdbj
10151.1Sdbj	nsys = p->p_emul->e_nsysent;
10161.1Sdbj	callp = p->p_emul->e_sysent;
10171.1Sdbj
10181.1Sdbj#ifdef COMPAT_SUNOS
10191.1Sdbj	if (p->p_emul == &emul_sunos) {
10201.1Sdbj		/*
10211.1Sdbj		 * SunOS passes the syscall-number on the stack, whereas
10221.1Sdbj		 * BSD passes it in D0. So, we have to get the real "code"
10231.1Sdbj		 * from the stack, and clean up the stack, as SunOS glue
10241.1Sdbj		 * code assumes the kernel pops the syscall argument the
10251.1Sdbj		 * glue pushed on the stack. Sigh...
10261.1Sdbj		 */
10271.1Sdbj		code = fuword((caddr_t)frame.f_regs[SP]);
10281.1Sdbj
10291.1Sdbj		/*
10301.1Sdbj		 * XXX
10311.1Sdbj		 * Don't do this for sunos_sigreturn, as there's no stored pc
10321.1Sdbj		 * on the stack to skip, the argument follows the syscall
10331.1Sdbj		 * number without a gap.
10341.1Sdbj		 */
10351.1Sdbj		if (code != SUNOS_SYS_sigreturn) {
10361.1Sdbj			frame.f_regs[SP] += sizeof (int);
10371.1Sdbj			/*
10381.16Sdbj			 * remember that we adjusted the SP,
10391.1Sdbj			 * might have to undo this if the system call
10401.1Sdbj			 * returns ERESTART.
10411.1Sdbj			 */
10421.1Sdbj			p->p_md.md_flags |= MDP_STACKADJ;
10431.1Sdbj		} else
10441.1Sdbj			p->p_md.md_flags &= ~MDP_STACKADJ;
10451.1Sdbj	}
10461.1Sdbj#endif
10471.1Sdbj
10481.1Sdbj	params = (caddr_t)frame.f_regs[SP] + sizeof(int);
10491.1Sdbj
10501.1Sdbj	switch (code) {
10511.1Sdbj	case SYS_syscall:
10521.1Sdbj		/*
10531.1Sdbj		 * Code is first argument, followed by actual args.
10541.1Sdbj		 */
10551.1Sdbj		code = fuword(params);
10561.1Sdbj		params += sizeof(int);
10571.1Sdbj		/*
10581.1Sdbj		 * XXX sigreturn requires special stack manipulation
10591.1Sdbj		 * that is only done if entered via the sigreturn
10601.1Sdbj		 * trap.  Cannot allow it here so make sure we fail.
10611.1Sdbj		 */
10621.6Sthorpej		switch (code) {
10631.6Sthorpej#ifdef COMPAT_13
10641.6Sthorpej		case SYS_compat_13_sigreturn13:
10651.6Sthorpej#endif
10661.6Sthorpej		case SYS___sigreturn14:
10671.1Sdbj			code = nsys;
10681.6Sthorpej			break;
10691.6Sthorpej		}
10701.1Sdbj		break;
10711.1Sdbj	case SYS___syscall:
10721.1Sdbj		/*
10731.1Sdbj		 * Like syscall, but code is a quad, so as to maintain
10741.1Sdbj		 * quad alignment for the rest of the arguments.
10751.1Sdbj		 */
10761.1Sdbj		if (callp != sysent)
10771.1Sdbj			break;
10781.1Sdbj		code = fuword(params + _QUAD_LOWWORD * sizeof(int));
10791.1Sdbj		params += sizeof(quad_t);
10801.1Sdbj		break;
10811.1Sdbj	default:
10821.1Sdbj		break;
10831.1Sdbj	}
10841.1Sdbj	if (code < 0 || code >= nsys)
10851.1Sdbj		callp += p->p_emul->e_nosys;		/* illegal */
10861.1Sdbj	else
10871.1Sdbj		callp += code;
10881.1Sdbj	argsize = callp->sy_argsize;
10891.9Sitohy#ifdef COMPAT_LINUX
10901.9Sitohy	if (0
10911.9Sitohy# ifdef EXEC_AOUT
10921.9Sitohy	    || p->p_emul == &emul_linux_aout
10931.9Sitohy# endif
10941.9Sitohy# ifdef EXEC_ELF32
10951.9Sitohy	    || p->p_emul == &emul_linux_elf32
10961.9Sitohy# endif
10971.9Sitohy	     ) {
10981.9Sitohy		/*
10991.9Sitohy		 * Linux passes the args in d1-d5
11001.9Sitohy		 */
11011.9Sitohy		switch (argsize) {
11021.9Sitohy		case 20:
11031.9Sitohy			args[4] = frame.f_regs[D5];
11041.9Sitohy		case 16:
11051.9Sitohy			args[3] = frame.f_regs[D4];
11061.9Sitohy		case 12:
11071.9Sitohy			args[2] = frame.f_regs[D3];
11081.9Sitohy		case 8:
11091.9Sitohy			args[1] = frame.f_regs[D2];
11101.9Sitohy		case 4:
11111.9Sitohy			args[0] = frame.f_regs[D1];
11121.9Sitohy		case 0:
11131.9Sitohy			error = 0;
11141.9Sitohy			break;
11151.9Sitohy		default:
11161.9Sitohy#ifdef DEBUG
11171.9Sitohy			panic("linux syscall %d weird argsize %d",
11181.9Sitohy				code, argsize);
11191.9Sitohy#else
11201.9Sitohy			error = EINVAL;
11211.9Sitohy#endif
11221.9Sitohy			break;
11231.9Sitohy		}
11241.9Sitohy	} else
11251.9Sitohy#endif
11261.1Sdbj	if (argsize)
11271.1Sdbj		error = copyin(params, (caddr_t)args, argsize);
11281.1Sdbj	else
11291.1Sdbj		error = 0;
11301.1Sdbj#ifdef SYSCALL_DEBUG
11311.1Sdbj	scdebug_call(p, code, args);
11321.1Sdbj#endif
11331.1Sdbj#ifdef KTRACE
11341.1Sdbj	if (KTRPOINT(p, KTR_SYSCALL))
11351.1Sdbj		ktrsyscall(p->p_tracep, code, argsize, args);
11361.1Sdbj#endif
11371.1Sdbj	if (error)
11381.1Sdbj		goto bad;
11391.1Sdbj	rval[0] = 0;
11401.1Sdbj	rval[1] = frame.f_regs[D1];
11411.1Sdbj	error = (*callp->sy_call)(p, args, rval);
11421.1Sdbj	switch (error) {
11431.1Sdbj	case 0:
11441.1Sdbj		frame.f_regs[D0] = rval[0];
11451.1Sdbj		frame.f_regs[D1] = rval[1];
11461.1Sdbj		frame.f_sr &= ~PSL_C;	/* carry bit */
11471.1Sdbj		break;
11481.1Sdbj	case ERESTART:
11491.1Sdbj		/*
11501.1Sdbj		 * We always enter through a `trap' instruction, which is 2
11511.1Sdbj		 * bytes, so adjust the pc by that amount.
11521.1Sdbj		 */
11531.1Sdbj		frame.f_pc = opc - 2;
11541.1Sdbj		break;
11551.1Sdbj	case EJUSTRETURN:
11561.1Sdbj		/* nothing to do */
11571.1Sdbj		break;
11581.1Sdbj	default:
11591.1Sdbj	bad:
11601.1Sdbj		if (p->p_emul->e_errno)
11611.1Sdbj			error = p->p_emul->e_errno[error];
11621.1Sdbj		frame.f_regs[D0] = error;
11631.1Sdbj		frame.f_sr |= PSL_C;	/* carry bit */
11641.1Sdbj		break;
11651.1Sdbj	}
11661.1Sdbj
11671.1Sdbj#ifdef SYSCALL_DEBUG
11681.1Sdbj	scdebug_ret(p, code, error, rval);
11691.1Sdbj#endif
11701.1Sdbj#ifdef COMPAT_SUNOS
11711.1Sdbj	/* need new p-value for this */
11721.1Sdbj	if (error == ERESTART && (p->p_md.md_flags & MDP_STACKADJ))
11731.1Sdbj		frame.f_regs[SP] -= sizeof (int);
11741.1Sdbj#endif
11751.1Sdbj	userret(p, &frame, sticks, (u_int)0, 0);
11761.1Sdbj#ifdef KTRACE
11771.1Sdbj	if (KTRPOINT(p, KTR_SYSRET))
11781.1Sdbj		ktrsysret(p->p_tracep, code, error, rval[0]);
11791.1Sdbj#endif
11801.1Sdbj}
11811.1Sdbj
11821.1Sdbjvoid
11831.8Sthorpejchild_return(arg)
11841.8Sthorpej	void *arg;
11851.1Sdbj{
11861.8Sthorpej	struct proc *p = arg;
11871.8Sthorpej	/* See cpu_fork() */
11881.8Sthorpej	struct frame *f = (struct frame *)p->p_md.md_regs;
11891.8Sthorpej
11901.8Sthorpej	f->f_regs[D0] = 0;
11911.8Sthorpej	f->f_sr &= ~PSL_C;
11921.8Sthorpej	f->f_format = FMT0;
11931.1Sdbj
11941.16Sdbj	userret(p, f, 0, (u_int)0, 0);
11951.1Sdbj#ifdef KTRACE
11961.1Sdbj	if (KTRPOINT(p, KTR_SYSRET))
11971.1Sdbj		ktrsysret(p->p_tracep, SYS_fork, 0, 0);
11981.1Sdbj#endif
11991.1Sdbj}
12001.1Sdbj
12011.1Sdbj/*
12021.1Sdbj * Allocation routines for software interrupts.
12031.1Sdbj */
12041.1Sdbju_long
12051.1Sdbjallocate_sir(proc, arg)
12061.16Sdbj	void (*proc)(void *);
12071.1Sdbj	void *arg;
12081.1Sdbj{
12091.1Sdbj	int bit;
12101.1Sdbj
12111.1Sdbj	if( next_sir >= NSIR )
12121.1Sdbj		panic("allocate_sir: none left");
12131.1Sdbj	bit = next_sir++;
12141.1Sdbj	sir_routines[bit] = proc;
12151.1Sdbj	sir_args[bit] = arg;
12161.1Sdbj	return (1 << bit);
12171.1Sdbj}
12181.1Sdbj
12191.1Sdbjvoid
12201.1Sdbjinit_sir()
12211.1Sdbj{
12221.16Sdbj	extern void netintr(void);
12231.1Sdbj
12241.16Sdbj	sir_routines[0] = (void (*)(void *))netintr;
12251.16Sdbj	sir_routines[1] = (void (*)(void *))softclock;
12261.1Sdbj	next_sir = 2;
12271.1Sdbj}
1228