trap.c revision 1.18
11.18Sthorpej/*	$NetBSD: trap.c,v 1.18 1999/11/13 00:30: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.1Sdbj	int sig, s;
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.1Sdbj		 * Since we are curproc, clock will normally just change
2301.1Sdbj		 * our priority without moving us from one queue to another
2311.1Sdbj		 * (since the running process is not on a queue.)
2321.1Sdbj		 * If that happened after we put ourselves on the run queue
2331.1Sdbj		 * but before we mi_switch()'ed, we might not be on the queue
2341.1Sdbj		 * indicated by our priority.
2351.1Sdbj		 */
2361.1Sdbj		s = splstatclock();
2371.1Sdbj		setrunqueue(p);
2381.1Sdbj		p->p_stats->p_ru.ru_nivcsw++;
2391.1Sdbj		mi_switch();
2401.1Sdbj		splx(s);
2411.1Sdbj		while ((sig = CURSIG(p)) != 0)
2421.1Sdbj			postsig(sig);
2431.1Sdbj	}
2441.1Sdbj
2451.1Sdbj	/*
2461.1Sdbj	 * If profiling, charge system time to the trapped pc.
2471.1Sdbj	 */
2481.1Sdbj	if (p->p_flag & P_PROFIL) {
2491.1Sdbj		extern int psratio;
2501.1Sdbj
2511.1Sdbj		addupc_task(p, fp->f_pc,
2521.1Sdbj			    (int)(p->p_sticks - oticks) * psratio);
2531.1Sdbj	}
2541.1Sdbj#ifdef M68040
2551.1Sdbj	/*
2561.1Sdbj	 * Deal with user mode writebacks (from trap, or from sigreturn).
2571.1Sdbj	 * If any writeback fails, go back and attempt signal delivery.
2581.1Sdbj	 * unless we have already been here and attempted the writeback
2591.1Sdbj	 * (e.g. bad address with user ignoring SIGSEGV).  In that case
2601.1Sdbj	 * we just return to the user without sucessfully completing
2611.1Sdbj	 * the writebacks.  Maybe we should just drop the sucker?
2621.1Sdbj	 */
2631.16Sdbj	if (cputype == CPU_68040 && fp->f_format == FMT7) {
2641.1Sdbj		if (beenhere) {
2651.1Sdbj#ifdef DEBUG
2661.1Sdbj			if (mmudebug & MDB_WBFAILED)
2671.1Sdbj				printf(fromtrap ?
2681.1Sdbj		"pid %d(%s): writeback aborted, pc=%x, fa=%x\n" :
2691.1Sdbj		"pid %d(%s): writeback aborted in sigreturn, pc=%x\n",
2701.1Sdbj				    p->p_pid, p->p_comm, fp->f_pc, faultaddr);
2711.1Sdbj#endif
2721.16Sdbj		} else if ((sig = writeback(fp, fromtrap))) {
2731.1Sdbj			beenhere = 1;
2741.1Sdbj			oticks = p->p_sticks;
2751.1Sdbj			trapsignal(p, sig, faultaddr);
2761.1Sdbj			goto again;
2771.1Sdbj		}
2781.1Sdbj	}
2791.1Sdbj#endif
2801.1Sdbj	curpriority = p->p_priority;
2811.1Sdbj}
2821.1Sdbj
2831.1Sdbj/*
2841.1Sdbj * Trap is called from locore to handle most types of processor traps,
2851.1Sdbj * including events such as simulated software interrupts/AST's.
2861.1Sdbj * System calls are broken out for efficiency.
2871.1Sdbj */
2881.1Sdbj/*ARGSUSED*/
2891.16Sdbjvoid
2901.1Sdbjtrap(type, code, v, frame)
2911.1Sdbj	int type;
2921.1Sdbj	unsigned code;
2931.1Sdbj	unsigned v;
2941.1Sdbj	struct frame frame;
2951.1Sdbj{
2961.1Sdbj	extern char fubail[], subail[];
2971.1Sdbj	struct proc *p;
2981.16Sdbj	int i, s;
2991.1Sdbj	u_int ucode;
3001.16Sdbj	u_quad_t sticks = 0 /* XXX initialiser works around compiler bug */;
3011.7Sdbj	int bit;
3021.1Sdbj
3031.7Sdbj	uvmexp.traps++;
3041.1Sdbj	p = curproc;
3051.1Sdbj	ucode = 0;
3061.16Sdbj
3071.16Sdbj	/* I have verified that this DOES happen! -gwr */
3081.16Sdbj	if (p == NULL)
3091.16Sdbj		p = &proc0;
3101.16Sdbj#ifdef DIAGNOSTIC
3111.16Sdbj	if (p->p_addr == NULL)
3121.16Sdbj		panic("trap: no pcb");
3131.16Sdbj#endif
3141.16Sdbj
3151.1Sdbj	if (USERMODE(frame.f_sr)) {
3161.1Sdbj		type |= T_USER;
3171.1Sdbj		sticks = p->p_sticks;
3181.1Sdbj		p->p_md.md_regs = frame.f_regs;
3191.1Sdbj	}
3201.1Sdbj	switch (type) {
3211.1Sdbj
3221.1Sdbj	default:
3231.14Sdbj	dopanic:
3241.16Sdbj		printf("trap type %d, code = 0x%x, v = 0x%x\n", type, code, v);
3251.16Sdbj		printf("%s program counter = 0x%x\n",
3261.16Sdbj		    (type & T_USER) ? "user" : "kernel", frame.f_pc);
3271.14Sdbj		/*
3281.14Sdbj		 * Let the kernel debugger see the trap frame that
3291.14Sdbj		 * caused us to panic.  This is a convenience so
3301.14Sdbj		 * one can see registers at the point of failure.
3311.14Sdbj		 */
3321.16Sdbj		s = splhigh();
3331.14Sdbj#ifdef KGDB
3341.14Sdbj		/* If connected, step or cont returns 1 */
3351.16Sdbj		if (kgdb_trap(type, &frame))
3361.14Sdbj			goto kgdb_cont;
3371.14Sdbj#endif
3381.16Sdbj#ifdef DDB
3391.16Sdbj		(void)kdb_trap(type, (db_regs_t *)&frame);
3401.1Sdbj#endif
3411.14Sdbj#ifdef KGDB
3421.14Sdbj	kgdb_cont:
3431.14Sdbj#endif
3441.16Sdbj		splx(s);
3451.14Sdbj		if (panicstr) {
3461.16Sdbj			printf("trap during panic!\n");
3471.16Sdbj#ifdef DEBUG
3481.16Sdbj			/* XXX should be a machine-dependent hook */
3491.16Sdbj			printf("(press a key)\n"); (void)cngetc();
3501.16Sdbj#endif
3511.14Sdbj		}
3521.1Sdbj		regdump((struct trapframe *)&frame, 128);
3531.1Sdbj		type &= ~T_USER;
3541.16Sdbj		if ((u_int)type < trap_types)
3551.1Sdbj			panic(trap_type[type]);
3561.1Sdbj		panic("trap");
3571.1Sdbj
3581.1Sdbj	case T_BUSERR:		/* kernel bus error */
3591.16Sdbj		if (p->p_addr->u_pcb.pcb_onfault == 0)
3601.1Sdbj			goto dopanic;
3611.16Sdbj		/* FALLTHROUGH */
3621.16Sdbj
3631.16Sdbj	copyfault:
3641.1Sdbj		/*
3651.1Sdbj		 * If we have arranged to catch this fault in any of the
3661.1Sdbj		 * copy to/from user space routines, set PC to return to
3671.1Sdbj		 * indicated location and set flag informing buserror code
3681.1Sdbj		 * that it may need to clean up stack frame.
3691.1Sdbj		 */
3701.1Sdbj		frame.f_stackadj = exframesize[frame.f_format];
3711.1Sdbj		frame.f_format = frame.f_vector = 0;
3721.1Sdbj		frame.f_pc = (int) p->p_addr->u_pcb.pcb_onfault;
3731.1Sdbj		return;
3741.1Sdbj
3751.1Sdbj	case T_BUSERR|T_USER:	/* bus error */
3761.1Sdbj	case T_ADDRERR|T_USER:	/* address error */
3771.1Sdbj		ucode = v;
3781.1Sdbj		i = SIGBUS;
3791.1Sdbj		break;
3801.1Sdbj
3811.1Sdbj	case T_COPERR:		/* kernel coprocessor violation */
3821.1Sdbj	case T_FMTERR|T_USER:	/* do all RTE errors come in as T_USER? */
3831.1Sdbj	case T_FMTERR:		/* ...just in case... */
3841.1Sdbj	/*
3851.1Sdbj	 * The user has most likely trashed the RTE or FP state info
3861.1Sdbj	 * in the stack frame of a signal handler.
3871.1Sdbj	 */
3881.1Sdbj		printf("pid %d: kernel %s exception\n", p->p_pid,
3891.1Sdbj		       type==T_COPERR ? "coprocessor" : "format");
3901.1Sdbj		type |= T_USER;
3911.6Sthorpej		p->p_sigacts->ps_sigact[SIGILL].sa_handler = SIG_DFL;
3921.6Sthorpej		sigdelset(&p->p_sigignore, SIGILL);
3931.6Sthorpej		sigdelset(&p->p_sigcatch, SIGILL);
3941.6Sthorpej		sigdelset(&p->p_sigmask, SIGILL);
3951.1Sdbj		i = SIGILL;
3961.1Sdbj		ucode = frame.f_format;	/* XXX was ILL_RESAD_FAULT */
3971.1Sdbj		break;
3981.1Sdbj
3991.1Sdbj	case T_COPERR|T_USER:	/* user coprocessor violation */
4001.1Sdbj	/* What is a proper response here? */
4011.1Sdbj		ucode = 0;
4021.1Sdbj		i = SIGFPE;
4031.1Sdbj		break;
4041.1Sdbj
4051.1Sdbj	case T_FPERR|T_USER:	/* 68881 exceptions */
4061.1Sdbj	/*
4071.7Sdbj	 * We pass along the 68881 status register which locore stashed
4081.1Sdbj	 * in code for us.  Note that there is a possibility that the
4091.7Sdbj	 * bit pattern of this register will conflict with one of the
4101.1Sdbj	 * FPE_* codes defined in signal.h.  Fortunately for us, the
4111.1Sdbj	 * only such codes we use are all in the range 1-7 and the low
4121.7Sdbj	 * 3 bits of the status register are defined as 0 so there is
4131.1Sdbj	 * no clash.
4141.1Sdbj	 */
4151.1Sdbj		ucode = code;
4161.1Sdbj		i = SIGFPE;
4171.1Sdbj		break;
4181.1Sdbj
4191.1Sdbj#ifdef M68040
4201.1Sdbj	case T_FPEMULI|T_USER:	/* unimplemented FP instuction */
4211.1Sdbj	case T_FPEMULD|T_USER:	/* unimplemented FP data type */
4221.1Sdbj		/* XXX need to FSAVE */
4231.1Sdbj		printf("pid %d(%s): unimplemented FP %s at %x (EA %x)\n",
4241.1Sdbj		       p->p_pid, p->p_comm,
4251.1Sdbj		       frame.f_format == 2 ? "instruction" : "data type",
4261.1Sdbj		       frame.f_pc, frame.f_fmt2.f_iaddr);
4271.1Sdbj		/* XXX need to FRESTORE */
4281.1Sdbj		i = SIGFPE;
4291.1Sdbj		break;
4301.1Sdbj#endif
4311.1Sdbj
4321.1Sdbj	case T_ILLINST|T_USER:	/* illegal instruction fault */
4331.1Sdbj#ifdef COMPAT_HPUX
4341.1Sdbj		if (p->p_emul == &emul_hpux) {
4351.1Sdbj			ucode = HPUX_ILL_ILLINST_TRAP;
4361.1Sdbj			i = SIGILL;
4371.1Sdbj			break;
4381.1Sdbj		}
4391.1Sdbj		/* fall through */
4401.1Sdbj#endif
4411.1Sdbj	case T_PRIVINST|T_USER:	/* privileged instruction fault */
4421.1Sdbj#ifdef COMPAT_HPUX
4431.1Sdbj		if (p->p_emul == &emul_hpux)
4441.1Sdbj			ucode = HPUX_ILL_PRIV_TRAP;
4451.1Sdbj		else
4461.1Sdbj#endif
4471.1Sdbj		ucode = frame.f_format;	/* XXX was ILL_PRIVIN_FAULT */
4481.1Sdbj		i = SIGILL;
4491.1Sdbj		break;
4501.1Sdbj
4511.1Sdbj	case T_ZERODIV|T_USER:	/* Divide by zero */
4521.1Sdbj#ifdef COMPAT_HPUX
4531.1Sdbj		if (p->p_emul == &emul_hpux)
4541.1Sdbj			ucode = HPUX_FPE_INTDIV_TRAP;
4551.1Sdbj		else
4561.1Sdbj#endif
4571.1Sdbj		ucode = frame.f_format;	/* XXX was FPE_INTDIV_TRAP */
4581.1Sdbj		i = SIGFPE;
4591.1Sdbj		break;
4601.1Sdbj
4611.1Sdbj	case T_CHKINST|T_USER:	/* CHK instruction trap */
4621.1Sdbj#ifdef COMPAT_HPUX
4631.1Sdbj		if (p->p_emul == &emul_hpux) {
4641.1Sdbj			/* handled differently under hp-ux */
4651.1Sdbj			i = SIGILL;
4661.1Sdbj			ucode = HPUX_ILL_CHK_TRAP;
4671.1Sdbj			break;
4681.1Sdbj		}
4691.1Sdbj#endif
4701.1Sdbj		ucode = frame.f_format;	/* XXX was FPE_SUBRNG_TRAP */
4711.1Sdbj		i = SIGFPE;
4721.1Sdbj		break;
4731.1Sdbj
4741.1Sdbj	case T_TRAPVINST|T_USER:	/* TRAPV instruction trap */
4751.1Sdbj#ifdef COMPAT_HPUX
4761.1Sdbj		if (p->p_emul == &emul_hpux) {
4771.1Sdbj			/* handled differently under hp-ux */
4781.1Sdbj			i = SIGILL;
4791.1Sdbj			ucode = HPUX_ILL_TRAPV_TRAP;
4801.1Sdbj			break;
4811.1Sdbj		}
4821.1Sdbj#endif
4831.1Sdbj		ucode = frame.f_format;	/* XXX was FPE_INTOVF_TRAP */
4841.1Sdbj		i = SIGFPE;
4851.1Sdbj		break;
4861.1Sdbj
4871.1Sdbj	/*
4881.1Sdbj	 * XXX: Trace traps are a nightmare.
4891.1Sdbj	 *
4901.1Sdbj	 *	HP-UX uses trap #1 for breakpoints,
4911.16Sdbj	 *	NetBSD/m68k uses trap #2,
4921.1Sdbj	 *	SUN 3.x uses trap #15,
4931.16Sdbj	 *	DDB and KGDB uses trap #15 (for kernel breakpoints;
4941.16Sdbj	 *	handled elsewhere).
4951.1Sdbj	 *
4961.16Sdbj	 * NetBSD and HP-UX traps both get mapped by locore.s into T_TRACE.
4971.1Sdbj	 * SUN 3.x traps get passed through as T_TRAP15 and are not really
4981.1Sdbj	 * supported yet.
4991.16Sdbj	 *
5001.17Sitohy	 * XXX: We should never get kernel-mode T_TRAP15
5011.16Sdbj	 * XXX: because locore.s now gives them special treatment.
5021.1Sdbj	 */
5031.16Sdbj	case T_TRAP15:		/* kernel breakpoint */
5041.16Sdbj#ifdef DEBUG
5051.16Sdbj		printf("unexpected kernel trace trap, type = %d\n", type);
5061.16Sdbj		printf("program counter = 0x%x\n", frame.f_pc);
5071.1Sdbj#endif
5081.1Sdbj		frame.f_sr &= ~PSL_T;
5091.16Sdbj		return;
5101.1Sdbj
5111.1Sdbj	case T_TRACE|T_USER:	/* user trace trap */
5121.1Sdbj#ifdef COMPAT_SUNOS
5131.1Sdbj		/*
5141.1Sdbj		 * SunOS uses Trap #2 for a "CPU cache flush".
5151.1Sdbj		 * Just flush the on-chip caches and return.
5161.1Sdbj		 */
5171.1Sdbj		if (p->p_emul == &emul_sunos) {
5181.1Sdbj			ICIA();
5191.1Sdbj			DCIU();
5201.1Sdbj			return;
5211.1Sdbj		}
5221.16Sdbj#endif
5231.17Sitohy		/* FALLTHROUGH */
5241.17Sitohy	case T_TRACE:		/* tracing a trap instruction */
5251.17Sitohy	case T_TRAP15|T_USER:	/* SUN user trace trap */
5261.1Sdbj		frame.f_sr &= ~PSL_T;
5271.1Sdbj		i = SIGTRAP;
5281.1Sdbj		break;
5291.1Sdbj
5301.1Sdbj	case T_ASTFLT:		/* system async trap, cannot happen */
5311.1Sdbj		goto dopanic;
5321.1Sdbj
5331.1Sdbj	case T_ASTFLT|T_USER:	/* user async trap */
5341.1Sdbj		astpending = 0;
5351.1Sdbj		/*
5361.1Sdbj		 * We check for software interrupts first.  This is because
5371.1Sdbj		 * they are at a higher level than ASTs, and on a VAX would
5381.1Sdbj		 * interrupt the AST.  We assume that if we are processing
5391.1Sdbj		 * an AST that we must be at IPL0 so we don't bother to
5401.1Sdbj		 * check.  Note that we ensure that we are at least at SIR
5411.1Sdbj		 * IPL while processing the SIR.
5421.1Sdbj		 */
5431.1Sdbj		spl1();
5441.1Sdbj		/* fall into... */
5451.1Sdbj
5461.1Sdbj	case T_SSIR:		/* software interrupt */
5471.1Sdbj	case T_SSIR|T_USER:
5481.16Sdbj		while ((bit = ffs(ssir))) {
5491.1Sdbj			--bit;
5501.1Sdbj			ssir &= ~(1 << bit);
5511.7Sdbj			uvmexp.softs++;
5521.1Sdbj			if (sir_routines[bit])
5531.1Sdbj				sir_routines[bit](sir_args[bit]);
5541.1Sdbj		}
5551.1Sdbj		/*
5561.1Sdbj		 * If this was not an AST trap, we are all done.
5571.1Sdbj		 */
5581.1Sdbj		if (type != (T_ASTFLT|T_USER)) {
5591.16Sdbj			uvmexp.traps--;
5601.1Sdbj			return;
5611.1Sdbj		}
5621.1Sdbj		spl0();
5631.1Sdbj		if (p->p_flag & P_OWEUPC) {
5641.1Sdbj			p->p_flag &= ~P_OWEUPC;
5651.1Sdbj			ADDUPROF(p);
5661.1Sdbj		}
5671.1Sdbj		goto out;
5681.1Sdbj
5691.1Sdbj	case T_MMUFLT:		/* kernel mode page fault */
5701.1Sdbj		/*
5711.1Sdbj		 * If we were doing profiling ticks or other user mode
5721.1Sdbj		 * stuff from interrupt code, Just Say No.
5731.1Sdbj		 */
5741.1Sdbj		if (p->p_addr->u_pcb.pcb_onfault == fubail ||
5751.1Sdbj		    p->p_addr->u_pcb.pcb_onfault == subail)
5761.1Sdbj			goto copyfault;
5771.1Sdbj		/* fall into ... */
5781.1Sdbj
5791.1Sdbj	case T_MMUFLT|T_USER:	/* page fault */
5801.1Sdbj	    {
5811.7Sdbj		vaddr_t va;
5821.1Sdbj		struct vmspace *vm = p->p_vmspace;
5831.1Sdbj		vm_map_t map;
5841.1Sdbj		int rv;
5851.1Sdbj		vm_prot_t ftype;
5861.1Sdbj		extern vm_map_t kernel_map;
5871.1Sdbj
5881.1Sdbj#ifdef DEBUG
5891.1Sdbj		if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
5901.1Sdbj		printf("trap: T_MMUFLT pid=%d, code=%x, v=%x, pc=%x, sr=%x\n",
5911.1Sdbj		       p->p_pid, code, v, frame.f_pc, frame.f_sr);
5921.1Sdbj#endif
5931.1Sdbj		/*
5941.1Sdbj		 * It is only a kernel address space fault iff:
5951.1Sdbj		 * 	1. (type & T_USER) == 0  and
5961.1Sdbj		 * 	2. pcb_onfault not set or
5971.1Sdbj		 *	3. pcb_onfault set but supervisor space data fault
5981.1Sdbj		 * The last can occur during an exec() copyin where the
5991.1Sdbj		 * argument space is lazy-allocated.
6001.1Sdbj		 */
6011.16Sdbj		if ((type & T_USER) == 0 &&
6021.16Sdbj		    ((p->p_addr->u_pcb.pcb_onfault == 0) || KDFAULT(code)))
6031.1Sdbj			map = kernel_map;
6041.1Sdbj		else
6051.16Sdbj			map = vm ? &vm->vm_map : kernel_map;
6061.16Sdbj
6071.1Sdbj		if (WRFAULT(code))
6081.1Sdbj			ftype = VM_PROT_READ | VM_PROT_WRITE;
6091.1Sdbj		else
6101.1Sdbj			ftype = VM_PROT_READ;
6111.16Sdbj
6121.7Sdbj		va = trunc_page((vaddr_t)v);
6131.16Sdbj
6141.1Sdbj		if (map == kernel_map && va == 0) {
6151.16Sdbj			printf("trap: bad kernel %s access at 0x%x\n",
6161.16Sdbj			    (ftype & VM_PROT_WRITE) ? "read/write" :
6171.16Sdbj			    "read", v);
6181.1Sdbj			goto dopanic;
6191.1Sdbj		}
6201.16Sdbj
6211.1Sdbj#ifdef COMPAT_HPUX
6221.1Sdbj		if (ISHPMMADDR(va)) {
6231.16Sdbj			int pmap_mapmulti __P((pmap_t, vaddr_t));
6241.7Sdbj			vaddr_t bva;
6251.1Sdbj
6261.1Sdbj			rv = pmap_mapmulti(map->pmap, va);
6271.1Sdbj			if (rv != KERN_SUCCESS) {
6281.1Sdbj				bva = HPMMBASEADDR(va);
6291.7Sdbj				rv = uvm_fault(map, bva, 0, ftype);
6301.1Sdbj				if (rv == KERN_SUCCESS)
6311.1Sdbj					(void) pmap_mapmulti(map->pmap, va);
6321.1Sdbj			}
6331.1Sdbj		} else
6341.1Sdbj#endif
6351.7Sdbj		rv = uvm_fault(map, va, 0, ftype);
6361.7Sdbj#ifdef DEBUG
6371.7Sdbj		if (rv && MDB_ISPID(p->p_pid))
6381.7Sdbj			printf("uvm_fault(%p, 0x%lx, 0, 0x%x) -> 0x%x\n",
6391.16Sdbj			    map, va, ftype, rv);
6401.7Sdbj#endif
6411.1Sdbj		/*
6421.1Sdbj		 * If this was a stack access we keep track of the maximum
6431.1Sdbj		 * accessed stack size.  Also, if vm_fault gets a protection
6441.1Sdbj		 * failure it is due to accessing the stack region outside
6451.1Sdbj		 * the current limit and we need to reflect that as an access
6461.1Sdbj		 * error.
6471.1Sdbj		 */
6481.16Sdbj		if ((vm != NULL && (caddr_t)va >= vm->vm_maxsaddr)
6491.16Sdbj		    && map != kernel_map) {
6501.1Sdbj			if (rv == KERN_SUCCESS) {
6511.1Sdbj				unsigned nss;
6521.1Sdbj
6531.1Sdbj				nss = clrnd(btoc(USRSTACK-(unsigned)va));
6541.1Sdbj				if (nss > vm->vm_ssize)
6551.1Sdbj					vm->vm_ssize = nss;
6561.1Sdbj			} else if (rv == KERN_PROTECTION_FAILURE)
6571.1Sdbj				rv = KERN_INVALID_ADDRESS;
6581.1Sdbj		}
6591.1Sdbj		if (rv == KERN_SUCCESS) {
6601.1Sdbj			if (type == T_MMUFLT) {
6611.16Sdbj#ifdef M68040
6621.16Sdbj				if (cputype == CPU_68040)
6631.1Sdbj					(void) writeback(&frame, 1);
6641.1Sdbj#endif
6651.1Sdbj				return;
6661.1Sdbj			}
6671.1Sdbj			goto out;
6681.1Sdbj		}
6691.1Sdbj		if (type == T_MMUFLT) {
6701.1Sdbj			if (p->p_addr->u_pcb.pcb_onfault)
6711.1Sdbj				goto copyfault;
6721.7Sdbj			printf("uvm_fault(%p, 0x%lx, 0, 0x%x) -> 0x%x\n",
6731.16Sdbj			    map, va, ftype, rv);
6741.1Sdbj			printf("  type %x, code [mmu,,ssw]: %x\n",
6751.1Sdbj			       type, code);
6761.1Sdbj			goto dopanic;
6771.1Sdbj		}
6781.1Sdbj		ucode = v;
6791.11Schs		if (rv == KERN_RESOURCE_SHORTAGE) {
6801.11Schs			printf("UVM: pid %d (%s), uid %d killed: out of swap\n",
6811.11Schs			       p->p_pid, p->p_comm,
6821.11Schs			       p->p_cred && p->p_ucred ?
6831.11Schs			       p->p_ucred->cr_uid : -1);
6841.11Schs			i = SIGKILL;
6851.11Schs		} else {
6861.11Schs			i = SIGSEGV;
6871.11Schs		}
6881.1Sdbj		break;
6891.1Sdbj	    }
6901.1Sdbj	}
6911.1Sdbj	trapsignal(p, i, ucode);
6921.1Sdbj	if ((type & T_USER) == 0)
6931.1Sdbj		return;
6941.1Sdbjout:
6951.1Sdbj	userret(p, &frame, sticks, v, 1);
6961.1Sdbj}
6971.1Sdbj
6981.1Sdbj#ifdef M68040
6991.1Sdbj#ifdef DEBUG
7001.1Sdbjstruct writebackstats {
7011.1Sdbj	int calls;
7021.1Sdbj	int cpushes;
7031.1Sdbj	int move16s;
7041.1Sdbj	int wb1s, wb2s, wb3s;
7051.1Sdbj	int wbsize[4];
7061.1Sdbj} wbstats;
7071.1Sdbj
7081.1Sdbjchar *f7sz[] = { "longword", "byte", "word", "line" };
7091.1Sdbjchar *f7tt[] = { "normal", "MOVE16", "AFC", "ACK" };
7101.1Sdbjchar *f7tm[] = { "d-push", "u-data", "u-code", "M-data",
7111.1Sdbj		 "M-code", "k-data", "k-code", "RES" };
7121.1Sdbjchar wberrstr[] =
7131.16Sdbj    "WARNING: pid %d(%s) writeback [%s] failed, pc=%x fa=%x wba=%x wbd=%x\n";
7141.1Sdbj#endif
7151.1Sdbj
7161.16Sdbjint
7171.1Sdbjwriteback(fp, docachepush)
7181.1Sdbj	struct frame *fp;
7191.1Sdbj	int docachepush;
7201.1Sdbj{
7211.1Sdbj	struct fmt7 *f = &fp->f_fmt7;
7221.1Sdbj	struct proc *p = curproc;
7231.1Sdbj	int err = 0;
7241.1Sdbj	u_int fa;
7251.1Sdbj	caddr_t oonfault = p->p_addr->u_pcb.pcb_onfault;
7261.15Sthorpej	paddr_t pa;
7271.1Sdbj
7281.1Sdbj#ifdef DEBUG
7291.1Sdbj	if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid)) {
7301.1Sdbj		printf(" pid=%d, fa=%x,", p->p_pid, f->f_fa);
7311.1Sdbj		dumpssw(f->f_ssw);
7321.1Sdbj	}
7331.1Sdbj	wbstats.calls++;
7341.1Sdbj#endif
7351.1Sdbj	/*
7361.1Sdbj	 * Deal with special cases first.
7371.1Sdbj	 */
7381.1Sdbj	if ((f->f_ssw & SSW4_TMMASK) == SSW4_TMDCP) {
7391.1Sdbj		/*
7401.1Sdbj		 * Dcache push fault.
7411.1Sdbj		 * Line-align the address and write out the push data to
7421.1Sdbj		 * the indicated physical address.
7431.1Sdbj		 */
7441.1Sdbj#ifdef DEBUG
7451.1Sdbj		if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid)) {
7461.1Sdbj			printf(" pushing %s to PA %x, data %x",
7471.1Sdbj			       f7sz[(f->f_ssw & SSW4_SZMASK) >> 5],
7481.1Sdbj			       f->f_fa, f->f_pd0);
7491.1Sdbj			if ((f->f_ssw & SSW4_SZMASK) == SSW4_SZLN)
7501.1Sdbj				printf("/%x/%x/%x",
7511.1Sdbj				       f->f_pd1, f->f_pd2, f->f_pd3);
7521.1Sdbj			printf("\n");
7531.1Sdbj		}
7541.1Sdbj		if (f->f_wb1s & SSW4_WBSV)
7551.1Sdbj			panic("writeback: cache push with WB1S valid");
7561.1Sdbj		wbstats.cpushes++;
7571.1Sdbj#endif
7581.1Sdbj		/*
7591.1Sdbj		 * XXX there are security problems if we attempt to do a
7601.1Sdbj		 * cache push after a signal handler has been called.
7611.1Sdbj		 */
7621.1Sdbj		if (docachepush) {
7631.7Sdbj			pmap_enter(pmap_kernel(), (vaddr_t)vmmap,
7641.18Sthorpej			    trunc_page(f->f_fa), VM_PROT_WRITE,
7651.18Sthorpej			    VM_PROT_WRITE|PMAP_WIRED);
7661.1Sdbj			fa = (u_int)&vmmap[(f->f_fa & PGOFSET) & ~0xF];
7671.1Sdbj			bcopy((caddr_t)&f->f_pd0, (caddr_t)fa, 16);
7681.15Sthorpej			(void) pmap_extract(pmap_kernel(), (vaddr_t)fa, &pa);
7691.15Sthorpej			DCFL(pa);
7701.7Sdbj			pmap_remove(pmap_kernel(), (vaddr_t)vmmap,
7711.7Sdbj				    (vaddr_t)&vmmap[NBPG]);
7721.1Sdbj		} else
7731.1Sdbj			printf("WARNING: pid %d(%s) uid %d: CPUSH not done\n",
7741.1Sdbj			       p->p_pid, p->p_comm, p->p_ucred->cr_uid);
7751.1Sdbj	} else if ((f->f_ssw & (SSW4_RW|SSW4_TTMASK)) == SSW4_TTM16) {
7761.1Sdbj		/*
7771.1Sdbj		 * MOVE16 fault.
7781.1Sdbj		 * Line-align the address and write out the push data to
7791.1Sdbj		 * the indicated virtual address.
7801.1Sdbj		 */
7811.1Sdbj#ifdef DEBUG
7821.1Sdbj		if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
7831.1Sdbj			printf(" MOVE16 to VA %x(%x), data %x/%x/%x/%x\n",
7841.1Sdbj			       f->f_fa, f->f_fa & ~0xF, f->f_pd0, f->f_pd1,
7851.1Sdbj			       f->f_pd2, f->f_pd3);
7861.1Sdbj		if (f->f_wb1s & SSW4_WBSV)
7871.1Sdbj			panic("writeback: MOVE16 with WB1S valid");
7881.1Sdbj		wbstats.move16s++;
7891.1Sdbj#endif
7901.1Sdbj		if (KDFAULT(f->f_wb1s))
7911.1Sdbj			bcopy((caddr_t)&f->f_pd0, (caddr_t)(f->f_fa & ~0xF), 16);
7921.1Sdbj		else
7931.1Sdbj			err = suline((caddr_t)(f->f_fa & ~0xF), (caddr_t)&f->f_pd0);
7941.1Sdbj		if (err) {
7951.1Sdbj			fa = f->f_fa & ~0xF;
7961.1Sdbj#ifdef DEBUG
7971.1Sdbj			if (mmudebug & MDB_WBFAILED)
7981.1Sdbj				printf(wberrstr, p->p_pid, p->p_comm,
7991.1Sdbj				       "MOVE16", fp->f_pc, f->f_fa,
8001.1Sdbj				       f->f_fa & ~0xF, f->f_pd0);
8011.1Sdbj#endif
8021.1Sdbj		}
8031.1Sdbj	} else if (f->f_wb1s & SSW4_WBSV) {
8041.1Sdbj		/*
8051.1Sdbj		 * Writeback #1.
8061.1Sdbj		 * Position the "memory-aligned" data and write it out.
8071.1Sdbj		 */
8081.1Sdbj		u_int wb1d = f->f_wb1d;
8091.1Sdbj		int off;
8101.1Sdbj
8111.1Sdbj#ifdef DEBUG
8121.1Sdbj		if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
8131.1Sdbj			dumpwb(1, f->f_wb1s, f->f_wb1a, f->f_wb1d);
8141.1Sdbj		wbstats.wb1s++;
8151.1Sdbj		wbstats.wbsize[(f->f_wb2s&SSW4_SZMASK)>>5]++;
8161.1Sdbj#endif
8171.1Sdbj		off = (f->f_wb1a & 3) * 8;
8181.1Sdbj		switch (f->f_wb1s & SSW4_SZMASK) {
8191.1Sdbj		case SSW4_SZLW:
8201.1Sdbj			if (off)
8211.1Sdbj				wb1d = (wb1d >> (32 - off)) | (wb1d << off);
8221.1Sdbj			if (KDFAULT(f->f_wb1s))
8231.1Sdbj				*(long *)f->f_wb1a = wb1d;
8241.1Sdbj			else
8251.1Sdbj				err = suword((caddr_t)f->f_wb1a, wb1d);
8261.1Sdbj			break;
8271.1Sdbj		case SSW4_SZB:
8281.1Sdbj			off = 24 - off;
8291.1Sdbj			if (off)
8301.1Sdbj				wb1d >>= off;
8311.1Sdbj			if (KDFAULT(f->f_wb1s))
8321.1Sdbj				*(char *)f->f_wb1a = wb1d;
8331.1Sdbj			else
8341.1Sdbj				err = subyte((caddr_t)f->f_wb1a, wb1d);
8351.1Sdbj			break;
8361.1Sdbj		case SSW4_SZW:
8371.1Sdbj			off = (off + 16) % 32;
8381.1Sdbj			if (off)
8391.1Sdbj				wb1d = (wb1d >> (32 - off)) | (wb1d << off);
8401.1Sdbj			if (KDFAULT(f->f_wb1s))
8411.1Sdbj				*(short *)f->f_wb1a = wb1d;
8421.1Sdbj			else
8431.1Sdbj				err = susword((caddr_t)f->f_wb1a, wb1d);
8441.1Sdbj			break;
8451.1Sdbj		}
8461.1Sdbj		if (err) {
8471.1Sdbj			fa = f->f_wb1a;
8481.1Sdbj#ifdef DEBUG
8491.1Sdbj			if (mmudebug & MDB_WBFAILED)
8501.1Sdbj				printf(wberrstr, p->p_pid, p->p_comm,
8511.1Sdbj				       "#1", fp->f_pc, f->f_fa,
8521.1Sdbj				       f->f_wb1a, f->f_wb1d);
8531.1Sdbj#endif
8541.1Sdbj		}
8551.1Sdbj	}
8561.1Sdbj	/*
8571.1Sdbj	 * Deal with the "normal" writebacks.
8581.1Sdbj	 *
8591.1Sdbj	 * XXX writeback2 is known to reflect a LINE size writeback after
8601.1Sdbj	 * a MOVE16 was already dealt with above.  Ignore it.
8611.1Sdbj	 */
8621.1Sdbj	if (err == 0 && (f->f_wb2s & SSW4_WBSV) &&
8631.1Sdbj	    (f->f_wb2s & SSW4_SZMASK) != SSW4_SZLN) {
8641.1Sdbj#ifdef DEBUG
8651.1Sdbj		if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
8661.1Sdbj			dumpwb(2, f->f_wb2s, f->f_wb2a, f->f_wb2d);
8671.1Sdbj		wbstats.wb2s++;
8681.1Sdbj		wbstats.wbsize[(f->f_wb2s&SSW4_SZMASK)>>5]++;
8691.1Sdbj#endif
8701.1Sdbj		switch (f->f_wb2s & SSW4_SZMASK) {
8711.1Sdbj		case SSW4_SZLW:
8721.1Sdbj			if (KDFAULT(f->f_wb2s))
8731.1Sdbj				*(long *)f->f_wb2a = f->f_wb2d;
8741.1Sdbj			else
8751.1Sdbj				err = suword((caddr_t)f->f_wb2a, f->f_wb2d);
8761.1Sdbj			break;
8771.1Sdbj		case SSW4_SZB:
8781.1Sdbj			if (KDFAULT(f->f_wb2s))
8791.1Sdbj				*(char *)f->f_wb2a = f->f_wb2d;
8801.1Sdbj			else
8811.1Sdbj				err = subyte((caddr_t)f->f_wb2a, f->f_wb2d);
8821.1Sdbj			break;
8831.1Sdbj		case SSW4_SZW:
8841.1Sdbj			if (KDFAULT(f->f_wb2s))
8851.1Sdbj				*(short *)f->f_wb2a = f->f_wb2d;
8861.1Sdbj			else
8871.1Sdbj				err = susword((caddr_t)f->f_wb2a, f->f_wb2d);
8881.1Sdbj			break;
8891.1Sdbj		}
8901.1Sdbj		if (err) {
8911.1Sdbj			fa = f->f_wb2a;
8921.1Sdbj#ifdef DEBUG
8931.1Sdbj			if (mmudebug & MDB_WBFAILED) {
8941.1Sdbj				printf(wberrstr, p->p_pid, p->p_comm,
8951.1Sdbj				       "#2", fp->f_pc, f->f_fa,
8961.1Sdbj				       f->f_wb2a, f->f_wb2d);
8971.1Sdbj				dumpssw(f->f_ssw);
8981.1Sdbj				dumpwb(2, f->f_wb2s, f->f_wb2a, f->f_wb2d);
8991.1Sdbj			}
9001.1Sdbj#endif
9011.1Sdbj		}
9021.1Sdbj	}
9031.1Sdbj	if (err == 0 && (f->f_wb3s & SSW4_WBSV)) {
9041.1Sdbj#ifdef DEBUG
9051.1Sdbj		if ((mmudebug & MDB_WBFOLLOW) || MDB_ISPID(p->p_pid))
9061.1Sdbj			dumpwb(3, f->f_wb3s, f->f_wb3a, f->f_wb3d);
9071.1Sdbj		wbstats.wb3s++;
9081.1Sdbj		wbstats.wbsize[(f->f_wb3s&SSW4_SZMASK)>>5]++;
9091.1Sdbj#endif
9101.1Sdbj		switch (f->f_wb3s & SSW4_SZMASK) {
9111.1Sdbj		case SSW4_SZLW:
9121.1Sdbj			if (KDFAULT(f->f_wb3s))
9131.1Sdbj				*(long *)f->f_wb3a = f->f_wb3d;
9141.1Sdbj			else
9151.1Sdbj				err = suword((caddr_t)f->f_wb3a, f->f_wb3d);
9161.1Sdbj			break;
9171.1Sdbj		case SSW4_SZB:
9181.1Sdbj			if (KDFAULT(f->f_wb3s))
9191.1Sdbj				*(char *)f->f_wb3a = f->f_wb3d;
9201.1Sdbj			else
9211.1Sdbj				err = subyte((caddr_t)f->f_wb3a, f->f_wb3d);
9221.1Sdbj			break;
9231.1Sdbj		case SSW4_SZW:
9241.1Sdbj			if (KDFAULT(f->f_wb3s))
9251.1Sdbj				*(short *)f->f_wb3a = f->f_wb3d;
9261.1Sdbj			else
9271.1Sdbj				err = susword((caddr_t)f->f_wb3a, f->f_wb3d);
9281.1Sdbj			break;
9291.1Sdbj#ifdef DEBUG
9301.1Sdbj		case SSW4_SZLN:
9311.1Sdbj			panic("writeback: wb3s indicates LINE write");
9321.1Sdbj#endif
9331.1Sdbj		}
9341.1Sdbj		if (err) {
9351.1Sdbj			fa = f->f_wb3a;
9361.1Sdbj#ifdef DEBUG
9371.1Sdbj			if (mmudebug & MDB_WBFAILED)
9381.1Sdbj				printf(wberrstr, p->p_pid, p->p_comm,
9391.1Sdbj				       "#3", fp->f_pc, f->f_fa,
9401.1Sdbj				       f->f_wb3a, f->f_wb3d);
9411.1Sdbj#endif
9421.1Sdbj		}
9431.1Sdbj	}
9441.1Sdbj	p->p_addr->u_pcb.pcb_onfault = oonfault;
9451.1Sdbj	if (err)
9461.1Sdbj		err = SIGSEGV;
9471.16Sdbj	return (err);
9481.1Sdbj}
9491.1Sdbj
9501.1Sdbj#ifdef DEBUG
9511.16Sdbjvoid
9521.1Sdbjdumpssw(ssw)
9531.1Sdbj	u_short ssw;
9541.1Sdbj{
9551.1Sdbj	printf(" SSW: %x: ", ssw);
9561.1Sdbj	if (ssw & SSW4_CP)
9571.1Sdbj		printf("CP,");
9581.1Sdbj	if (ssw & SSW4_CU)
9591.1Sdbj		printf("CU,");
9601.1Sdbj	if (ssw & SSW4_CT)
9611.1Sdbj		printf("CT,");
9621.1Sdbj	if (ssw & SSW4_CM)
9631.1Sdbj		printf("CM,");
9641.1Sdbj	if (ssw & SSW4_MA)
9651.1Sdbj		printf("MA,");
9661.1Sdbj	if (ssw & SSW4_ATC)
9671.1Sdbj		printf("ATC,");
9681.1Sdbj	if (ssw & SSW4_LK)
9691.1Sdbj		printf("LK,");
9701.1Sdbj	if (ssw & SSW4_RW)
9711.1Sdbj		printf("RW,");
9721.1Sdbj	printf(" SZ=%s, TT=%s, TM=%s\n",
9731.1Sdbj	       f7sz[(ssw & SSW4_SZMASK) >> 5],
9741.1Sdbj	       f7tt[(ssw & SSW4_TTMASK) >> 3],
9751.1Sdbj	       f7tm[ssw & SSW4_TMMASK]);
9761.1Sdbj}
9771.1Sdbj
9781.16Sdbjvoid
9791.1Sdbjdumpwb(num, s, a, d)
9801.1Sdbj	int num;
9811.1Sdbj	u_short s;
9821.1Sdbj	u_int a, d;
9831.1Sdbj{
9841.1Sdbj	struct proc *p = curproc;
9851.7Sdbj	paddr_t pa;
9861.1Sdbj
9871.1Sdbj	printf(" writeback #%d: VA %x, data %x, SZ=%s, TT=%s, TM=%s\n",
9881.1Sdbj	       num, a, d, f7sz[(s & SSW4_SZMASK) >> 5],
9891.1Sdbj	       f7tt[(s & SSW4_TTMASK) >> 3], f7tm[s & SSW4_TMMASK]);
9901.16Sdbj	printf("               PA ");
9911.15Sthorpej	if (pmap_extract(p->p_vmspace->vm_map.pmap, (vaddr_t)a, &pa) == FALSE)
9921.1Sdbj		printf("<invalid address>");
9931.1Sdbj	else
9941.16Sdbj		printf("%lx, current value %lx", pa, fuword((caddr_t)a));
9951.1Sdbj	printf("\n");
9961.1Sdbj}
9971.1Sdbj#endif
9981.1Sdbj#endif
9991.1Sdbj
10001.1Sdbj/*
10011.1Sdbj * Process a system call.
10021.1Sdbj */
10031.16Sdbjvoid
10041.1Sdbjsyscall(code, frame)
10051.16Sdbj	register_t code;
10061.1Sdbj	struct frame frame;
10071.1Sdbj{
10081.1Sdbj	caddr_t params;
10091.1Sdbj	struct sysent *callp;
10101.1Sdbj	struct proc *p;
10111.1Sdbj	int error, opc, nsys;
10121.1Sdbj	size_t argsize;
10131.16Sdbj	register_t args[8], rval[2];
10141.1Sdbj	u_quad_t sticks;
10151.1Sdbj
10161.7Sdbj	uvmexp.syscalls++;
10171.1Sdbj	if (!USERMODE(frame.f_sr))
10181.1Sdbj		panic("syscall");
10191.1Sdbj	p = curproc;
10201.1Sdbj	sticks = p->p_sticks;
10211.1Sdbj	p->p_md.md_regs = frame.f_regs;
10221.1Sdbj	opc = frame.f_pc;
10231.1Sdbj
10241.1Sdbj	nsys = p->p_emul->e_nsysent;
10251.1Sdbj	callp = p->p_emul->e_sysent;
10261.1Sdbj
10271.1Sdbj#ifdef COMPAT_SUNOS
10281.1Sdbj	if (p->p_emul == &emul_sunos) {
10291.1Sdbj		/*
10301.1Sdbj		 * SunOS passes the syscall-number on the stack, whereas
10311.1Sdbj		 * BSD passes it in D0. So, we have to get the real "code"
10321.1Sdbj		 * from the stack, and clean up the stack, as SunOS glue
10331.1Sdbj		 * code assumes the kernel pops the syscall argument the
10341.1Sdbj		 * glue pushed on the stack. Sigh...
10351.1Sdbj		 */
10361.1Sdbj		code = fuword((caddr_t)frame.f_regs[SP]);
10371.1Sdbj
10381.1Sdbj		/*
10391.1Sdbj		 * XXX
10401.1Sdbj		 * Don't do this for sunos_sigreturn, as there's no stored pc
10411.1Sdbj		 * on the stack to skip, the argument follows the syscall
10421.1Sdbj		 * number without a gap.
10431.1Sdbj		 */
10441.1Sdbj		if (code != SUNOS_SYS_sigreturn) {
10451.1Sdbj			frame.f_regs[SP] += sizeof (int);
10461.1Sdbj			/*
10471.16Sdbj			 * remember that we adjusted the SP,
10481.1Sdbj			 * might have to undo this if the system call
10491.1Sdbj			 * returns ERESTART.
10501.1Sdbj			 */
10511.1Sdbj			p->p_md.md_flags |= MDP_STACKADJ;
10521.1Sdbj		} else
10531.1Sdbj			p->p_md.md_flags &= ~MDP_STACKADJ;
10541.1Sdbj	}
10551.1Sdbj#endif
10561.1Sdbj
10571.1Sdbj	params = (caddr_t)frame.f_regs[SP] + sizeof(int);
10581.1Sdbj
10591.1Sdbj	switch (code) {
10601.1Sdbj	case SYS_syscall:
10611.1Sdbj		/*
10621.1Sdbj		 * Code is first argument, followed by actual args.
10631.1Sdbj		 */
10641.1Sdbj		code = fuword(params);
10651.1Sdbj		params += sizeof(int);
10661.1Sdbj		/*
10671.1Sdbj		 * XXX sigreturn requires special stack manipulation
10681.1Sdbj		 * that is only done if entered via the sigreturn
10691.1Sdbj		 * trap.  Cannot allow it here so make sure we fail.
10701.1Sdbj		 */
10711.6Sthorpej		switch (code) {
10721.6Sthorpej#ifdef COMPAT_13
10731.6Sthorpej		case SYS_compat_13_sigreturn13:
10741.6Sthorpej#endif
10751.6Sthorpej		case SYS___sigreturn14:
10761.1Sdbj			code = nsys;
10771.6Sthorpej			break;
10781.6Sthorpej		}
10791.1Sdbj		break;
10801.1Sdbj	case SYS___syscall:
10811.1Sdbj		/*
10821.1Sdbj		 * Like syscall, but code is a quad, so as to maintain
10831.1Sdbj		 * quad alignment for the rest of the arguments.
10841.1Sdbj		 */
10851.1Sdbj		if (callp != sysent)
10861.1Sdbj			break;
10871.1Sdbj		code = fuword(params + _QUAD_LOWWORD * sizeof(int));
10881.1Sdbj		params += sizeof(quad_t);
10891.1Sdbj		break;
10901.1Sdbj	default:
10911.1Sdbj		break;
10921.1Sdbj	}
10931.1Sdbj	if (code < 0 || code >= nsys)
10941.1Sdbj		callp += p->p_emul->e_nosys;		/* illegal */
10951.1Sdbj	else
10961.1Sdbj		callp += code;
10971.1Sdbj	argsize = callp->sy_argsize;
10981.9Sitohy#ifdef COMPAT_LINUX
10991.9Sitohy	if (0
11001.9Sitohy# ifdef EXEC_AOUT
11011.9Sitohy	    || p->p_emul == &emul_linux_aout
11021.9Sitohy# endif
11031.9Sitohy# ifdef EXEC_ELF32
11041.9Sitohy	    || p->p_emul == &emul_linux_elf32
11051.9Sitohy# endif
11061.9Sitohy	     ) {
11071.9Sitohy		/*
11081.9Sitohy		 * Linux passes the args in d1-d5
11091.9Sitohy		 */
11101.9Sitohy		switch (argsize) {
11111.9Sitohy		case 20:
11121.9Sitohy			args[4] = frame.f_regs[D5];
11131.9Sitohy		case 16:
11141.9Sitohy			args[3] = frame.f_regs[D4];
11151.9Sitohy		case 12:
11161.9Sitohy			args[2] = frame.f_regs[D3];
11171.9Sitohy		case 8:
11181.9Sitohy			args[1] = frame.f_regs[D2];
11191.9Sitohy		case 4:
11201.9Sitohy			args[0] = frame.f_regs[D1];
11211.9Sitohy		case 0:
11221.9Sitohy			error = 0;
11231.9Sitohy			break;
11241.9Sitohy		default:
11251.9Sitohy#ifdef DEBUG
11261.9Sitohy			panic("linux syscall %d weird argsize %d",
11271.9Sitohy				code, argsize);
11281.9Sitohy#else
11291.9Sitohy			error = EINVAL;
11301.9Sitohy#endif
11311.9Sitohy			break;
11321.9Sitohy		}
11331.9Sitohy	} else
11341.9Sitohy#endif
11351.1Sdbj	if (argsize)
11361.1Sdbj		error = copyin(params, (caddr_t)args, argsize);
11371.1Sdbj	else
11381.1Sdbj		error = 0;
11391.1Sdbj#ifdef SYSCALL_DEBUG
11401.1Sdbj	scdebug_call(p, code, args);
11411.1Sdbj#endif
11421.1Sdbj#ifdef KTRACE
11431.1Sdbj	if (KTRPOINT(p, KTR_SYSCALL))
11441.1Sdbj		ktrsyscall(p->p_tracep, code, argsize, args);
11451.1Sdbj#endif
11461.1Sdbj	if (error)
11471.1Sdbj		goto bad;
11481.1Sdbj	rval[0] = 0;
11491.1Sdbj	rval[1] = frame.f_regs[D1];
11501.1Sdbj	error = (*callp->sy_call)(p, args, rval);
11511.1Sdbj	switch (error) {
11521.1Sdbj	case 0:
11531.1Sdbj		frame.f_regs[D0] = rval[0];
11541.1Sdbj		frame.f_regs[D1] = rval[1];
11551.1Sdbj		frame.f_sr &= ~PSL_C;	/* carry bit */
11561.1Sdbj		break;
11571.1Sdbj	case ERESTART:
11581.1Sdbj		/*
11591.1Sdbj		 * We always enter through a `trap' instruction, which is 2
11601.1Sdbj		 * bytes, so adjust the pc by that amount.
11611.1Sdbj		 */
11621.1Sdbj		frame.f_pc = opc - 2;
11631.1Sdbj		break;
11641.1Sdbj	case EJUSTRETURN:
11651.1Sdbj		/* nothing to do */
11661.1Sdbj		break;
11671.1Sdbj	default:
11681.1Sdbj	bad:
11691.1Sdbj		if (p->p_emul->e_errno)
11701.1Sdbj			error = p->p_emul->e_errno[error];
11711.1Sdbj		frame.f_regs[D0] = error;
11721.1Sdbj		frame.f_sr |= PSL_C;	/* carry bit */
11731.1Sdbj		break;
11741.1Sdbj	}
11751.1Sdbj
11761.1Sdbj#ifdef SYSCALL_DEBUG
11771.1Sdbj	scdebug_ret(p, code, error, rval);
11781.1Sdbj#endif
11791.1Sdbj#ifdef COMPAT_SUNOS
11801.1Sdbj	/* need new p-value for this */
11811.1Sdbj	if (error == ERESTART && (p->p_md.md_flags & MDP_STACKADJ))
11821.1Sdbj		frame.f_regs[SP] -= sizeof (int);
11831.1Sdbj#endif
11841.1Sdbj	userret(p, &frame, sticks, (u_int)0, 0);
11851.1Sdbj#ifdef KTRACE
11861.1Sdbj	if (KTRPOINT(p, KTR_SYSRET))
11871.1Sdbj		ktrsysret(p->p_tracep, code, error, rval[0]);
11881.1Sdbj#endif
11891.1Sdbj}
11901.1Sdbj
11911.1Sdbjvoid
11921.8Sthorpejchild_return(arg)
11931.8Sthorpej	void *arg;
11941.1Sdbj{
11951.8Sthorpej	struct proc *p = arg;
11961.8Sthorpej	/* See cpu_fork() */
11971.8Sthorpej	struct frame *f = (struct frame *)p->p_md.md_regs;
11981.8Sthorpej
11991.8Sthorpej	f->f_regs[D0] = 0;
12001.8Sthorpej	f->f_sr &= ~PSL_C;
12011.8Sthorpej	f->f_format = FMT0;
12021.1Sdbj
12031.16Sdbj	userret(p, f, 0, (u_int)0, 0);
12041.1Sdbj#ifdef KTRACE
12051.1Sdbj	if (KTRPOINT(p, KTR_SYSRET))
12061.1Sdbj		ktrsysret(p->p_tracep, SYS_fork, 0, 0);
12071.1Sdbj#endif
12081.1Sdbj}
12091.1Sdbj
12101.1Sdbj/*
12111.1Sdbj * Allocation routines for software interrupts.
12121.1Sdbj */
12131.1Sdbju_long
12141.1Sdbjallocate_sir(proc, arg)
12151.16Sdbj	void (*proc)(void *);
12161.1Sdbj	void *arg;
12171.1Sdbj{
12181.1Sdbj	int bit;
12191.1Sdbj
12201.1Sdbj	if( next_sir >= NSIR )
12211.1Sdbj		panic("allocate_sir: none left");
12221.1Sdbj	bit = next_sir++;
12231.1Sdbj	sir_routines[bit] = proc;
12241.1Sdbj	sir_args[bit] = arg;
12251.1Sdbj	return (1 << bit);
12261.1Sdbj}
12271.1Sdbj
12281.1Sdbjvoid
12291.1Sdbjinit_sir()
12301.1Sdbj{
12311.16Sdbj	extern void netintr(void);
12321.1Sdbj
12331.16Sdbj	sir_routines[0] = (void (*)(void *))netintr;
12341.16Sdbj	sir_routines[1] = (void (*)(void *))softclock;
12351.1Sdbj	next_sir = 2;
12361.1Sdbj}
1237