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