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