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