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