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