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