subr_syscall.c revision 13203
1/*-
2 * Copyright (C) 1994, David Greenman
3 * Copyright (c) 1990, 1993
4 *	The Regents of the University of California.  All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * the University of Utah, and William Jolitz.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *	This product includes software developed by the University of
20 *	California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 *    may be used to endorse or promote products derived from this software
23 *    without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 *	from: @(#)trap.c	7.4 (Berkeley) 5/13/91
38 *	$Id: trap.c,v 1.68 1995/12/19 14:47:41 davidg Exp $
39 */
40
41/*
42 * 386 Trap and System call handling
43 */
44
45#include "opt_ktrace.h"
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/proc.h>
50#include <sys/acct.h>
51#include <sys/kernel.h>
52#include <sys/syscall.h>
53#include <sys/sysent.h>
54#include <sys/queue.h>
55#include <sys/vmmeter.h>
56#ifdef KTRACE
57#include <sys/ktrace.h>
58#endif
59
60#include <vm/vm.h>
61#include <vm/vm_param.h>
62#include <vm/vm_prot.h>
63#include <vm/lock.h>
64#include <vm/pmap.h>
65#include <vm/vm_kern.h>
66#include <vm/vm_map.h>
67#include <vm/vm_page.h>
68#include <vm/vm_extern.h>
69
70#include <sys/user.h>
71
72#include <machine/cpu.h>
73#include <machine/md_var.h>
74#include <machine/psl.h>
75#include <machine/reg.h>
76#include <machine/trap.h>
77#include <machine/../isa/isa_device.h>
78
79#ifdef POWERFAIL_NMI
80# include <syslog.h>
81# include <machine/clock.h>
82#endif
83
84#include "isa.h"
85#include "npx.h"
86
87int (*pmath_emulate) __P((struct trapframe *));
88
89extern void trap __P((struct trapframe frame));
90extern int trapwrite __P((unsigned addr));
91extern void syscall __P((struct trapframe frame));
92extern void linux_syscall __P((struct trapframe frame));
93
94static int trap_pfault __P((struct trapframe *, int));
95static void trap_fatal __P((struct trapframe *));
96void dblfault_handler __P((void));
97
98extern inthand_t IDTVEC(syscall);
99
100#define MAX_TRAP_MSG		27
101static char *trap_msg[] = {
102	"",					/*  0 unused */
103	"privileged instruction fault",		/*  1 T_PRIVINFLT */
104	"",					/*  2 unused */
105	"breakpoint instruction fault",		/*  3 T_BPTFLT */
106	"",					/*  4 unused */
107	"",					/*  5 unused */
108	"arithmetic trap",			/*  6 T_ARITHTRAP */
109	"system forced exception",		/*  7 T_ASTFLT */
110	"",					/*  8 unused */
111	"general protection fault",		/*  9 T_PROTFLT */
112	"trace trap",				/* 10 T_TRCTRAP */
113	"",					/* 11 unused */
114	"page fault",				/* 12 T_PAGEFLT */
115	"",					/* 13 unused */
116	"alignment fault",			/* 14 T_ALIGNFLT */
117	"",					/* 15 unused */
118	"",					/* 16 unused */
119	"",					/* 17 unused */
120	"integer divide fault",			/* 18 T_DIVIDE */
121	"non-maskable interrupt trap",		/* 19 T_NMI */
122	"overflow trap",			/* 20 T_OFLOW */
123	"FPU bounds check fault",		/* 21 T_BOUND */
124	"FPU device not available",		/* 22 T_DNA */
125	"double fault",				/* 23 T_DOUBLEFLT */
126	"FPU operand fetch fault",		/* 24 T_FPOPFLT */
127	"invalid TSS fault",			/* 25 T_TSSFLT */
128	"segment not present fault",		/* 26 T_SEGNPFLT */
129	"stack fault",				/* 27 T_STKFLT */
130};
131
132static void userret __P((struct proc *p, struct trapframe *frame,
133			 u_quad_t oticks));
134
135static inline void
136userret(p, frame, oticks)
137	struct proc *p;
138	struct trapframe *frame;
139	u_quad_t oticks;
140{
141	int sig, s;
142
143	while ((sig = CURSIG(p)) != 0)
144		postsig(sig);
145	p->p_priority = p->p_usrpri;
146	if (want_resched) {
147		/*
148		 * Since we are curproc, clock will normally just change
149		 * our priority without moving us from one queue to another
150		 * (since the running process is not on a queue.)
151		 * If that happened after we setrunqueue ourselves but before we
152		 * mi_switch()'ed, we might not be on the queue indicated by
153		 * our priority.
154		 */
155		s = splclock();
156		setrunqueue(p);
157		p->p_stats->p_ru.ru_nivcsw++;
158		mi_switch();
159		splx(s);
160		while ((sig = CURSIG(p)) != 0)
161			postsig(sig);
162	}
163	/*
164	 * Charge system time if profiling.
165	 */
166	if (p->p_flag & P_PROFIL) {
167		u_quad_t ticks = p->p_sticks - oticks;
168
169		if (ticks) {
170#ifdef PROFTIMER
171			extern int profscale;
172			addupc(frame->tf_eip, &p->p_stats->p_prof,
173			    ticks * profscale);
174#else
175			addupc(frame->tf_eip, &p->p_stats->p_prof, ticks);
176#endif
177		}
178	}
179	curpriority = p->p_priority;
180}
181
182/*
183 * Exception, fault, and trap interface to the FreeBSD kernel.
184 * This common code is called from assembly language IDT gate entry
185 * routines that prepare a suitable stack frame, and restore this
186 * frame after the exception has been processed.
187 */
188
189void
190trap(frame)
191	struct trapframe frame;
192{
193	struct proc *p = curproc;
194	u_quad_t sticks = 0;
195	int i = 0, ucode = 0, type, code;
196#ifdef DEBUG
197	u_long eva;
198#endif
199
200	type = frame.tf_trapno;
201	code = frame.tf_err;
202
203	if (ISPL(frame.tf_cs) == SEL_UPL) {
204		/* user trap */
205
206		sticks = p->p_sticks;
207		p->p_md.md_regs = (int *)&frame;
208
209		switch (type) {
210		case T_PRIVINFLT:	/* privileged instruction fault */
211			ucode = type;
212			i = SIGILL;
213			break;
214
215		case T_BPTFLT:		/* bpt instruction fault */
216		case T_TRCTRAP:		/* trace trap */
217			frame.tf_eflags &= ~PSL_T;
218			i = SIGTRAP;
219			break;
220
221		case T_ARITHTRAP:	/* arithmetic trap */
222			ucode = code;
223			i = SIGFPE;
224			break;
225
226		case T_ASTFLT:		/* Allow process switch */
227			astoff();
228			cnt.v_soft++;
229			if (p->p_flag & P_OWEUPC) {
230				addupc(frame.tf_eip, &p->p_stats->p_prof, 1);
231				p->p_flag &= ~P_OWEUPC;
232			}
233			goto out;
234
235		case T_PROTFLT:		/* general protection fault */
236		case T_SEGNPFLT:	/* segment not present fault */
237		case T_STKFLT:		/* stack fault */
238		case T_TSSFLT:		/* invalid TSS fault */
239		case T_DOUBLEFLT:	/* double fault */
240		default:
241			ucode = code + BUS_SEGM_FAULT ;
242			i = SIGBUS;
243			break;
244
245		case T_PAGEFLT:		/* page fault */
246			i = trap_pfault(&frame, TRUE);
247			if (i == -1)
248				return;
249			if (i == 0)
250				goto out;
251
252			ucode = T_PAGEFLT;
253			break;
254
255		case T_DIVIDE:		/* integer divide fault */
256			ucode = FPE_INTDIV_TRAP;
257			i = SIGFPE;
258			break;
259
260#if NISA > 0
261		case T_NMI:
262#ifdef POWERFAIL_NMI
263			goto handle_powerfail;
264#else /* !POWERFAIL_NMI */
265#ifdef DDB
266			/* NMI can be hooked up to a pushbutton for debugging */
267			printf ("NMI ... going to debugger\n");
268			if (kdb_trap (type, 0, &frame))
269				return;
270#endif /* DDB */
271			/* machine/parity/power fail/"kitchen sink" faults */
272			if (isa_nmi(code) == 0) return;
273			panic("NMI indicates hardware failure");
274#endif /* POWERFAIL_NMI */
275#endif /* NISA > 0 */
276
277		case T_OFLOW:		/* integer overflow fault */
278			ucode = FPE_INTOVF_TRAP;
279			i = SIGFPE;
280			break;
281
282		case T_BOUND:		/* bounds check fault */
283			ucode = FPE_SUBRNG_TRAP;
284			i = SIGFPE;
285			break;
286
287		case T_DNA:
288#if NNPX > 0
289			/* if a transparent fault (due to context switch "late") */
290			if (npxdna())
291				return;
292#endif	/* NNPX > 0 */
293
294			if (!pmath_emulate) {
295				i = SIGFPE;
296				ucode = FPE_FPU_NP_TRAP;
297				break;
298			}
299			i = (*pmath_emulate)(&frame);
300			if (i == 0) {
301				if (!(frame.tf_eflags & PSL_T))
302					return;
303				frame.tf_eflags &= ~PSL_T;
304				i = SIGTRAP;
305			}
306			/* else ucode = emulator_only_knows() XXX */
307			break;
308
309		case T_FPOPFLT:		/* FPU operand fetch fault */
310			ucode = T_FPOPFLT;
311			i = SIGILL;
312			break;
313		}
314	} else {
315		/* kernel trap */
316
317		switch (type) {
318		case T_PAGEFLT:			/* page fault */
319			(void) trap_pfault(&frame, FALSE);
320			return;
321
322		case T_PROTFLT:		/* general protection fault */
323		case T_SEGNPFLT:	/* segment not present fault */
324			/*
325			 * Invalid segment selectors and out of bounds
326			 * %eip's and %esp's can be set up in user mode.
327			 * This causes a fault in kernel mode when the
328			 * kernel tries to return to user mode.  We want
329			 * to get this fault so that we can fix the
330			 * problem here and not have to check all the
331			 * selectors and pointers when the user changes
332			 * them.
333			 */
334#define	MAYBE_DORETI_FAULT(where, whereto)				\
335	do {								\
336		if (frame.tf_eip == (int)where) {			\
337			frame.tf_eip = (int)whereto;			\
338			return;						\
339		}							\
340	} while (0)
341
342			if (intr_nesting_level == 0) {
343				MAYBE_DORETI_FAULT(doreti_iret,
344						   doreti_iret_fault);
345				MAYBE_DORETI_FAULT(doreti_popl_ds,
346						   doreti_popl_ds_fault);
347				MAYBE_DORETI_FAULT(doreti_popl_es,
348						   doreti_popl_es_fault);
349			}
350			if (curpcb && curpcb->pcb_onfault) {
351				frame.tf_eip = (int)curpcb->pcb_onfault;
352				return;
353			}
354			break;
355
356		case T_TSSFLT:
357			/*
358			 * PSL_NT can be set in user mode and isn't cleared
359			 * automatically when the kernel is entered.  This
360			 * causes a TSS fault when the kernel attempts to
361			 * `iret' because the TSS link is uninitialized.  We
362			 * want to get this fault so that we can fix the
363			 * problem here and not every time the kernel is
364			 * entered.
365			 */
366			if (frame.tf_eflags & PSL_NT) {
367				frame.tf_eflags &= ~PSL_NT;
368				return;
369			}
370			break;
371
372		case T_TRCTRAP:	 /* trace trap */
373			if (frame.tf_eip == (int)IDTVEC(syscall)) {
374				/*
375				 * We've just entered system mode via the
376				 * syscall lcall.  Continue single stepping
377				 * silently until the syscall handler has
378				 * saved the flags.
379				 */
380				return;
381			}
382			if (frame.tf_eip == (int)IDTVEC(syscall) + 1) {
383				/*
384				 * The syscall handler has now saved the
385				 * flags.  Stop single stepping it.
386				 */
387				frame.tf_eflags &= ~PSL_T;
388				return;
389			}
390			/*
391			 * Fall through.
392			 */
393		case T_BPTFLT:
394			/*
395			 * If DDB is enabled, let it handle the debugger trap.
396			 * Otherwise, debugger traps "can't happen".
397			 */
398#ifdef DDB
399			if (kdb_trap (type, 0, &frame))
400				return;
401#endif
402			break;
403
404#if NISA > 0
405		case T_NMI:
406#ifdef POWERFAIL_NMI
407#ifndef TIMER_FREQ
408#  define TIMER_FREQ 1193182
409#endif
410	handle_powerfail:
411		{
412		  static unsigned lastalert = 0;
413
414		  if(time.tv_sec - lastalert > 10)
415		    {
416		      log(LOG_WARNING, "NMI: power fail\n");
417		      sysbeep(TIMER_FREQ/880, hz);
418		      lastalert = time.tv_sec;
419		    }
420		  return;
421		}
422#else /* !POWERFAIL_NMI */
423#ifdef DDB
424			/* NMI can be hooked up to a pushbutton for debugging */
425			printf ("NMI ... going to debugger\n");
426			if (kdb_trap (type, 0, &frame))
427				return;
428#endif /* DDB */
429			/* machine/parity/power fail/"kitchen sink" faults */
430			if (isa_nmi(code) == 0) return;
431			/* FALL THROUGH */
432#endif /* POWERFAIL_NMI */
433#endif /* NISA > 0 */
434		}
435
436		trap_fatal(&frame);
437		return;
438	}
439
440	trapsignal(p, i, ucode);
441
442#ifdef DEBUG
443	eva = rcr2();
444	if (type <= MAX_TRAP_MSG) {
445		uprintf("fatal process exception: %s",
446			trap_msg[type]);
447		if ((type == T_PAGEFLT) || (type == T_PROTFLT))
448			uprintf(", fault VA = 0x%x", eva);
449		uprintf("\n");
450	}
451#endif
452
453out:
454	userret(p, &frame, sticks);
455}
456
457#ifdef notyet
458/*
459 * This version doesn't allow a page fault to user space while
460 * in the kernel. The rest of the kernel needs to be made "safe"
461 * before this can be used. I think the only things remaining
462 * to be made safe are the iBCS2 code and the process tracing/
463 * debugging code.
464 */
465static int
466trap_pfault(frame, usermode)
467	struct trapframe *frame;
468	int usermode;
469{
470	vm_offset_t va;
471	struct vmspace *vm = NULL;
472	vm_map_t map = 0;
473	int rv = 0;
474	vm_prot_t ftype;
475	int eva;
476	struct proc *p = curproc;
477
478	if (frame->tf_err & PGEX_W)
479		ftype = VM_PROT_READ | VM_PROT_WRITE;
480	else
481		ftype = VM_PROT_READ;
482
483	eva = rcr2();
484	va = trunc_page((vm_offset_t)eva);
485
486	if (va < VM_MIN_KERNEL_ADDRESS) {
487		vm_offset_t v;
488		vm_page_t ptepg;
489
490		if (p == NULL ||
491		    (!usermode && va < VM_MAXUSER_ADDRESS &&
492		    (curpcb == NULL || curpcb->pcb_onfault == NULL))) {
493			trap_fatal(frame);
494			return (-1);
495		}
496
497		/*
498		 * This is a fault on non-kernel virtual memory.
499		 * vm is initialized above to NULL. If curproc is NULL
500		 * or curproc->p_vmspace is NULL the fault is fatal.
501		 */
502		vm = p->p_vmspace;
503		if (vm == NULL)
504			goto nogo;
505
506		map = &vm->vm_map;
507
508		/*
509		 * Keep swapout from messing with us during this
510		 *	critical time.
511		 */
512		++p->p_lock;
513
514		/*
515		 * Grow the stack if necessary
516		 */
517		if ((caddr_t)va > vm->vm_maxsaddr
518		    && (caddr_t)va < (caddr_t)USRSTACK) {
519			if (!grow(p, va)) {
520				rv = KERN_FAILURE;
521				--p->p_lock;
522				goto nogo;
523			}
524		}
525
526		/*
527		 * Check if page table is mapped, if not,
528		 *	fault it first
529		 */
530		v = (vm_offset_t) vtopte(va);
531
532		/* Fault the pte only if needed: */
533		if (*((int *)vtopte(v)) == 0)
534			(void) vm_fault(map, trunc_page(v), VM_PROT_WRITE, FALSE);
535
536		pmap_use_pt( vm_map_pmap(map), va);
537
538		/* Fault in the user page: */
539		rv = vm_fault(map, va, ftype, FALSE);
540
541		pmap_unuse_pt( vm_map_pmap(map), va);
542
543		--p->p_lock;
544	} else {
545		/*
546		 * Don't allow user-mode faults in kernel address space.
547		 */
548		if (usermode)
549			goto nogo;
550
551		/*
552		 * Since we know that kernel virtual address addresses
553		 * always have pte pages mapped, we just have to fault
554		 * the page.
555		 */
556		rv = vm_fault(kernel_map, va, ftype, FALSE);
557	}
558
559	if (rv == KERN_SUCCESS)
560		return (0);
561nogo:
562	if (!usermode) {
563		if (curpcb && curpcb->pcb_onfault) {
564			frame->tf_eip = (int)curpcb->pcb_onfault;
565			return (0);
566		}
567		trap_fatal(frame);
568		return (-1);
569	}
570
571	/* kludge to pass faulting virtual address to sendsig */
572	frame->tf_err = eva;
573
574	return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
575}
576#endif
577
578int
579trap_pfault(frame, usermode)
580	struct trapframe *frame;
581	int usermode;
582{
583	vm_offset_t va;
584	struct vmspace *vm = NULL;
585	vm_map_t map = 0;
586	int rv = 0;
587	vm_prot_t ftype;
588	int eva;
589	struct proc *p = curproc;
590
591	eva = rcr2();
592	va = trunc_page((vm_offset_t)eva);
593
594	if (va >= KERNBASE) {
595		/*
596		 * Don't allow user-mode faults in kernel address space.
597		 */
598		if (usermode)
599			goto nogo;
600
601		map = kernel_map;
602	} else {
603		/*
604		 * This is a fault on non-kernel virtual memory.
605		 * vm is initialized above to NULL. If curproc is NULL
606		 * or curproc->p_vmspace is NULL the fault is fatal.
607		 */
608		if (p != NULL)
609			vm = p->p_vmspace;
610
611		if (vm == NULL)
612			goto nogo;
613
614		map = &vm->vm_map;
615	}
616
617	if (frame->tf_err & PGEX_W)
618		ftype = VM_PROT_READ | VM_PROT_WRITE;
619	else
620		ftype = VM_PROT_READ;
621
622	if (map != kernel_map) {
623		vm_offset_t v;
624
625		/*
626		 * Keep swapout from messing with us during this
627		 *	critical time.
628		 */
629		++p->p_lock;
630
631		/*
632		 * Grow the stack if necessary
633		 */
634		if ((caddr_t)va > vm->vm_maxsaddr
635		    && (caddr_t)va < (caddr_t)USRSTACK) {
636			if (!grow(p, va)) {
637				rv = KERN_FAILURE;
638				--p->p_lock;
639				goto nogo;
640			}
641		}
642
643		/*
644		 * Check if page table is mapped, if not,
645		 *	fault it first
646		 */
647		v = (vm_offset_t) vtopte(va);
648
649		/* Fault the pte only if needed: */
650		if (*((int *)vtopte(v)) == 0)
651			(void) vm_fault(map, trunc_page(v), VM_PROT_WRITE, FALSE);
652
653		pmap_use_pt( vm_map_pmap(map), va);
654
655		/* Fault in the user page: */
656		rv = vm_fault(map, va, ftype, FALSE);
657
658		pmap_unuse_pt( vm_map_pmap(map), va);
659
660		--p->p_lock;
661	} else {
662		/*
663		 * Since we know that kernel virtual address addresses
664		 * always have pte pages mapped, we just have to fault
665		 * the page.
666		 */
667		rv = vm_fault(map, va, ftype, FALSE);
668	}
669
670	if (rv == KERN_SUCCESS)
671		return (0);
672nogo:
673	if (!usermode) {
674		if (curpcb && curpcb->pcb_onfault) {
675			frame->tf_eip = (int)curpcb->pcb_onfault;
676			return (0);
677		}
678		trap_fatal(frame);
679		return (-1);
680	}
681
682	/* kludge to pass faulting virtual address to sendsig */
683	frame->tf_err = eva;
684
685	return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
686}
687
688static void
689trap_fatal(frame)
690	struct trapframe *frame;
691{
692	int code, type, eva;
693	struct soft_segment_descriptor softseg;
694
695	code = frame->tf_err;
696	type = frame->tf_trapno;
697	eva = rcr2();
698	sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
699
700	if (type <= MAX_TRAP_MSG)
701		printf("\n\nFatal trap %d: %s while in %s mode\n",
702			type, trap_msg[type],
703			ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
704	if (type == T_PAGEFLT) {
705		printf("fault virtual address	= 0x%x\n", eva);
706		printf("fault code		= %s %s, %s\n",
707			code & PGEX_U ? "user" : "supervisor",
708			code & PGEX_W ? "write" : "read",
709			code & PGEX_P ? "protection violation" : "page not present");
710	}
711	printf("instruction pointer	= 0x%x:0x%x\n", frame->tf_cs & 0xffff, frame->tf_eip);
712	printf("code segment		= base 0x%x, limit 0x%x, type 0x%x\n",
713	    softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
714	printf("			= DPL %d, pres %d, def32 %d, gran %d\n",
715	    softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32, softseg.ssd_gran);
716	printf("processor eflags	= ");
717	if (frame->tf_eflags & PSL_T)
718		printf("trace/trap, ");
719	if (frame->tf_eflags & PSL_I)
720		printf("interrupt enabled, ");
721	if (frame->tf_eflags & PSL_NT)
722		printf("nested task, ");
723	if (frame->tf_eflags & PSL_RF)
724		printf("resume, ");
725	if (frame->tf_eflags & PSL_VM)
726		printf("vm86, ");
727	printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
728	printf("current process		= ");
729	if (curproc) {
730		printf("%lu (%s)\n",
731		    (u_long)curproc->p_pid, curproc->p_comm ?
732		    curproc->p_comm : "");
733	} else {
734		printf("Idle\n");
735	}
736	printf("interrupt mask		= ");
737	if ((cpl & net_imask) == net_imask)
738		printf("net ");
739	if ((cpl & tty_imask) == tty_imask)
740		printf("tty ");
741	if ((cpl & bio_imask) == bio_imask)
742		printf("bio ");
743	if (cpl == 0)
744		printf("none");
745	printf("\n");
746
747#ifdef KDB
748	if (kdb_trap(&psl))
749		return;
750#endif
751#ifdef DDB
752	if (kdb_trap (type, 0, frame))
753		return;
754#endif
755	if (type <= MAX_TRAP_MSG)
756		panic(trap_msg[type]);
757	else
758		panic("unknown/reserved trap");
759}
760
761/*
762 * Double fault handler. Called when a fault occurs while writing
763 * a frame for a trap/exception onto the stack. This usually occurs
764 * when the stack overflows (such is the case with infinite recursion,
765 * for example).
766 *
767 * XXX Note that the current PTD gets replaced by IdlePTD when the
768 * task switch occurs. This means that the stack that was active at
769 * the time of the double fault is not available at <kstack> unless
770 * the machine was idle when the double fault occurred. The downside
771 * of this is that "trace <ebp>" in ddb won't work.
772 */
773void
774dblfault_handler()
775{
776	struct pcb *pcb = curpcb;
777
778	if (pcb != NULL) {
779		printf("\nFatal double fault:\n");
780		printf("eip = 0x%x\n", pcb->pcb_tss.tss_eip);
781		printf("esp = 0x%x\n", pcb->pcb_tss.tss_esp);
782		printf("ebp = 0x%x\n", pcb->pcb_tss.tss_ebp);
783	}
784
785	panic("double fault");
786}
787
788/*
789 * Compensate for 386 brain damage (missing URKR).
790 * This is a little simpler than the pagefault handler in trap() because
791 * it the page tables have already been faulted in and high addresses
792 * are thrown out early for other reasons.
793 */
794int trapwrite(addr)
795	unsigned addr;
796{
797	struct proc *p;
798	vm_offset_t va, v;
799	struct vmspace *vm;
800	int rv;
801
802	va = trunc_page((vm_offset_t)addr);
803	/*
804	 * XXX - MAX is END.  Changed > to >= for temp. fix.
805	 */
806	if (va >= VM_MAXUSER_ADDRESS)
807		return (1);
808
809	p = curproc;
810	vm = p->p_vmspace;
811
812	++p->p_lock;
813
814	if ((caddr_t)va >= vm->vm_maxsaddr
815	    && (caddr_t)va < (caddr_t)USRSTACK) {
816		if (!grow(p, va)) {
817			--p->p_lock;
818			return (1);
819		}
820	}
821
822	v = trunc_page(vtopte(va));
823
824	/*
825	 * wire the pte page
826	 */
827	if (va < USRSTACK) {
828		vm_map_pageable(&vm->vm_map, v, round_page(v+1), FALSE);
829	}
830
831	/*
832	 * fault the data page
833	 */
834	rv = vm_fault(&vm->vm_map, va, VM_PROT_READ|VM_PROT_WRITE, FALSE);
835
836	/*
837	 * unwire the pte page
838	 */
839	if (va < USRSTACK) {
840		vm_map_pageable(&vm->vm_map, v, round_page(v+1), TRUE);
841	}
842
843	--p->p_lock;
844
845	if (rv != KERN_SUCCESS)
846		return 1;
847
848	return (0);
849}
850
851/*
852 * System call request from POSIX system call gate interface to kernel.
853 * Like trap(), argument is call by reference.
854 */
855void
856syscall(frame)
857	struct trapframe frame;
858{
859	caddr_t params;
860	int i;
861	struct sysent *callp;
862	struct proc *p = curproc;
863	u_quad_t sticks;
864	int error;
865	int args[8], rval[2];
866	u_int code;
867
868	sticks = p->p_sticks;
869	if (ISPL(frame.tf_cs) != SEL_UPL)
870		panic("syscall");
871
872	p->p_md.md_regs = (int *)&frame;
873	params = (caddr_t)frame.tf_esp + sizeof(int);
874	code = frame.tf_eax;
875	/*
876	 * Need to check if this is a 32 bit or 64 bit syscall.
877	 */
878	if (code == SYS_syscall) {
879		/*
880		 * Code is first argument, followed by actual args.
881		 */
882		code = fuword(params);
883		params += sizeof(int);
884	} else if (code == SYS___syscall) {
885		/*
886		 * Like syscall, but code is a quad, so as to maintain
887		 * quad alignment for the rest of the arguments.
888		 */
889		code = fuword(params);
890		params += sizeof(quad_t);
891	}
892
893 	if (p->p_sysent->sv_mask)
894 		code &= p->p_sysent->sv_mask;
895
896 	if (code >= p->p_sysent->sv_size)
897 		callp = &p->p_sysent->sv_table[0];
898  	else
899 		callp = &p->p_sysent->sv_table[code];
900
901	if ((i = callp->sy_narg * sizeof(int)) &&
902	    (error = copyin(params, (caddr_t)args, (u_int)i))) {
903#ifdef KTRACE
904		if (KTRPOINT(p, KTR_SYSCALL))
905			ktrsyscall(p->p_tracep, code, callp->sy_narg, args);
906#endif
907		goto bad;
908	}
909#ifdef KTRACE
910	if (KTRPOINT(p, KTR_SYSCALL))
911		ktrsyscall(p->p_tracep, code, callp->sy_narg, args);
912#endif
913	rval[0] = 0;
914	rval[1] = frame.tf_edx;
915
916	error = (*callp->sy_call)(p, args, rval);
917
918	switch (error) {
919
920	case 0:
921		/*
922		 * Reinitialize proc pointer `p' as it may be different
923		 * if this is a child returning from fork syscall.
924		 */
925		p = curproc;
926		frame.tf_eax = rval[0];
927		frame.tf_edx = rval[1];
928		frame.tf_eflags &= ~PSL_C;
929		break;
930
931	case ERESTART:
932		/*
933		 * Reconstruct pc, assuming lcall $X,y is 7 bytes.
934		 */
935		frame.tf_eip -= 7;
936		break;
937
938	case EJUSTRETURN:
939		break;
940
941	default:
942bad:
943 		if (p->p_sysent->sv_errsize)
944 			if (error >= p->p_sysent->sv_errsize)
945  				error = -1;	/* XXX */
946   			else
947  				error = p->p_sysent->sv_errtbl[error];
948		frame.tf_eax = error;
949		frame.tf_eflags |= PSL_C;
950		break;
951	}
952
953	if (frame.tf_eflags & PSL_T) {
954		/* Traced syscall. */
955		frame.tf_eflags &= ~PSL_T;
956		trapsignal(p, SIGTRAP, 0);
957	}
958
959	userret(p, &frame, sticks);
960
961#ifdef KTRACE
962	if (KTRPOINT(p, KTR_SYSRET))
963		ktrsysret(p->p_tracep, code, error, rval[0]);
964#endif
965}
966
967#if defined(COMPAT_LINUX) || defined(LINUX)
968void
969linux_syscall(frame)
970	struct trapframe frame;
971{
972	struct proc *p = curproc;
973	struct sysent *callp;
974	u_quad_t sticks;
975	int error;
976	int rval[2];
977	u_int code;
978	struct linux_syscall_args {
979		int arg1;
980		int arg2;
981		int arg3;
982		int arg4;
983		int arg5;
984	} args;
985
986	args.arg1 = frame.tf_ebx;
987	args.arg2 = frame.tf_ecx;
988	args.arg3 = frame.tf_edx;
989	args.arg4 = frame.tf_esi;
990	args.arg5 = frame.tf_edi;
991
992	sticks = p->p_sticks;
993	if (ISPL(frame.tf_cs) != SEL_UPL)
994		panic("linux syscall");
995
996	p->p_md.md_regs = (int *)&frame;
997	code = frame.tf_eax;
998
999	if (p->p_sysent->sv_mask)
1000		code &= p->p_sysent->sv_mask;
1001
1002	if (code >= p->p_sysent->sv_size)
1003		callp = &p->p_sysent->sv_table[0];
1004	else
1005		callp = &p->p_sysent->sv_table[code];
1006
1007#ifdef KTRACE
1008	if (KTRPOINT(p, KTR_SYSCALL))
1009		ktrsyscall(p->p_tracep, code, callp->sy_narg, (int *)&args);
1010#endif
1011
1012	rval[0] = 0;
1013
1014	error = (*callp->sy_call)(p, &args, rval);
1015
1016	switch (error) {
1017
1018	case 0:
1019		/*
1020		 * Reinitialize proc pointer `p' as it may be different
1021		 * if this is a child returning from fork syscall.
1022		 */
1023		p = curproc;
1024		frame.tf_eax = rval[0];
1025		frame.tf_eflags &= ~PSL_C;
1026		break;
1027
1028	case ERESTART:
1029		/* Reconstruct pc, subtract size of int 0x80 */
1030		frame.tf_eip -= 2;
1031		break;
1032
1033	case EJUSTRETURN:
1034		break;
1035
1036	default:
1037 		if (p->p_sysent->sv_errsize)
1038 			if (error >= p->p_sysent->sv_errsize)
1039  				error = -1;	/* XXX */
1040   			else
1041  				error = p->p_sysent->sv_errtbl[error];
1042		frame.tf_eax = -error;
1043		frame.tf_eflags |= PSL_C;
1044		break;
1045	}
1046
1047	if (frame.tf_eflags & PSL_T) {
1048		/* Traced syscall. */
1049		frame.tf_eflags &= ~PSL_T;
1050		trapsignal(p, SIGTRAP, 0);
1051	}
1052
1053	userret(p, &frame, sticks);
1054
1055#ifdef KTRACE
1056	if (KTRPOINT(p, KTR_SYSRET))
1057		ktrsysret(p->p_tracep, code, error, rval[0]);
1058#endif
1059}
1060#endif /* COMPAT_LINUX || LINUX */
1061