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