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