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