subr_syscall.c revision 16725
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.78 1996/06/13 07:17:21 asami 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	/* NNPX > 0 */
285
286			if (!pmath_emulate) {
287				i = SIGFPE;
288				ucode = FPE_FPU_NP_TRAP;
289				break;
290			}
291			i = (*pmath_emulate)(&frame);
292			if (i == 0) {
293				if (!(frame.tf_eflags & PSL_T))
294					return;
295				frame.tf_eflags &= ~PSL_T;
296				i = SIGTRAP;
297			}
298			/* else ucode = emulator_only_knows() XXX */
299			break;
300
301		case T_FPOPFLT:		/* FPU operand fetch fault */
302			ucode = T_FPOPFLT;
303			i = SIGILL;
304			break;
305		}
306	} else {
307		/* kernel trap */
308
309		switch (type) {
310		case T_PAGEFLT:			/* page fault */
311			(void) trap_pfault(&frame, FALSE);
312			return;
313
314		case T_DNA:
315#if NNPX > 0
316			/* if a transparent fault (due to context switch "late") */
317			if (npxdna())
318				return;
319#endif	/* NNPX > 0 */
320			break;
321
322		case T_PROTFLT:		/* general protection fault */
323		case T_SEGNPFLT:	/* segment not present fault */
324			/*
325			 * Invalid segment selectors and out of bounds
326			 * %eip's and %esp's can be set up in user mode.
327			 * This causes a fault in kernel mode when the
328			 * kernel tries to return to user mode.  We want
329			 * to get this fault so that we can fix the
330			 * problem here and not have to check all the
331			 * selectors and pointers when the user changes
332			 * them.
333			 */
334#define	MAYBE_DORETI_FAULT(where, whereto)				\
335	do {								\
336		if (frame.tf_eip == (int)where) {			\
337			frame.tf_eip = (int)whereto;			\
338			return;						\
339		}							\
340	} while (0)
341
342			if (intr_nesting_level == 0) {
343				MAYBE_DORETI_FAULT(doreti_iret,
344						   doreti_iret_fault);
345				MAYBE_DORETI_FAULT(doreti_popl_ds,
346						   doreti_popl_ds_fault);
347				MAYBE_DORETI_FAULT(doreti_popl_es,
348						   doreti_popl_es_fault);
349			}
350			if (curpcb && curpcb->pcb_onfault) {
351				frame.tf_eip = (int)curpcb->pcb_onfault;
352				return;
353			}
354			break;
355
356		case T_TSSFLT:
357			/*
358			 * PSL_NT can be set in user mode and isn't cleared
359			 * automatically when the kernel is entered.  This
360			 * causes a TSS fault when the kernel attempts to
361			 * `iret' because the TSS link is uninitialized.  We
362			 * want to get this fault so that we can fix the
363			 * problem here and not every time the kernel is
364			 * entered.
365			 */
366			if (frame.tf_eflags & PSL_NT) {
367				frame.tf_eflags &= ~PSL_NT;
368				return;
369			}
370			break;
371
372		case T_TRCTRAP:	 /* trace trap */
373			if (frame.tf_eip == (int)IDTVEC(syscall)) {
374				/*
375				 * We've just entered system mode via the
376				 * syscall lcall.  Continue single stepping
377				 * silently until the syscall handler has
378				 * saved the flags.
379				 */
380				return;
381			}
382			if (frame.tf_eip == (int)IDTVEC(syscall) + 1) {
383				/*
384				 * The syscall handler has now saved the
385				 * flags.  Stop single stepping it.
386				 */
387				frame.tf_eflags &= ~PSL_T;
388				return;
389			}
390			/*
391			 * Fall through.
392			 */
393		case T_BPTFLT:
394			/*
395			 * If DDB is enabled, let it handle the debugger trap.
396			 * Otherwise, debugger traps "can't happen".
397			 */
398#ifdef DDB
399			if (kdb_trap (type, 0, &frame))
400				return;
401#endif
402			break;
403
404#if NISA > 0
405		case T_NMI:
406#ifdef POWERFAIL_NMI
407#ifndef TIMER_FREQ
408#  define TIMER_FREQ 1193182
409#endif
410	handle_powerfail:
411		{
412		  static unsigned lastalert = 0;
413
414		  if(time.tv_sec - lastalert > 10)
415		    {
416		      log(LOG_WARNING, "NMI: power fail\n");
417		      sysbeep(TIMER_FREQ/880, hz);
418		      lastalert = time.tv_sec;
419		    }
420		  return;
421		}
422#else /* !POWERFAIL_NMI */
423#ifdef DDB
424			/* NMI can be hooked up to a pushbutton for debugging */
425			printf ("NMI ... going to debugger\n");
426			if (kdb_trap (type, 0, &frame))
427				return;
428#endif /* DDB */
429			/* machine/parity/power fail/"kitchen sink" faults */
430			if (isa_nmi(code) == 0) return;
431			/* FALL THROUGH */
432#endif /* POWERFAIL_NMI */
433#endif /* NISA > 0 */
434		}
435
436		trap_fatal(&frame);
437		return;
438	}
439
440	trapsignal(p, i, ucode);
441
442#ifdef DEBUG
443	eva = rcr2();
444	if (type <= MAX_TRAP_MSG) {
445		uprintf("fatal process exception: %s",
446			trap_msg[type]);
447		if ((type == T_PAGEFLT) || (type == T_PROTFLT))
448			uprintf(", fault VA = 0x%x", eva);
449		uprintf("\n");
450	}
451#endif
452
453out:
454	userret(p, &frame, sticks);
455}
456
457#ifdef notyet
458/*
459 * This version doesn't allow a page fault to user space while
460 * in the kernel. The rest of the kernel needs to be made "safe"
461 * before this can be used. I think the only things remaining
462 * to be made safe are the iBCS2 code and the process tracing/
463 * debugging code.
464 */
465static int
466trap_pfault(frame, usermode)
467	struct trapframe *frame;
468	int usermode;
469{
470	vm_offset_t va;
471	struct vmspace *vm = NULL;
472	vm_map_t map = 0;
473	int rv = 0;
474	vm_prot_t ftype;
475	int eva;
476	struct proc *p = curproc;
477
478	if (frame->tf_err & PGEX_W)
479		ftype = VM_PROT_READ | VM_PROT_WRITE;
480	else
481		ftype = VM_PROT_READ;
482
483	eva = rcr2();
484	va = trunc_page((vm_offset_t)eva);
485
486	if (va < VM_MIN_KERNEL_ADDRESS) {
487		vm_offset_t v;
488		vm_page_t mpte;
489
490		if (p == NULL ||
491		    (!usermode && va < VM_MAXUSER_ADDRESS &&
492		    (curpcb == NULL || curpcb->pcb_onfault == NULL))) {
493			trap_fatal(frame);
494			return (-1);
495		}
496
497		/*
498		 * This is a fault on non-kernel virtual memory.
499		 * vm is initialized above to NULL. If curproc is NULL
500		 * or curproc->p_vmspace is NULL the fault is fatal.
501		 */
502		vm = p->p_vmspace;
503		if (vm == NULL)
504			goto nogo;
505
506		map = &vm->vm_map;
507
508		/*
509		 * Keep swapout from messing with us during this
510		 *	critical time.
511		 */
512		++p->p_lock;
513
514		/*
515		 * Grow the stack if necessary
516		 */
517		if ((caddr_t)va > vm->vm_maxsaddr
518		    && (caddr_t)va < (caddr_t)USRSTACK) {
519			if (!grow(p, va)) {
520				rv = KERN_FAILURE;
521				--p->p_lock;
522				goto nogo;
523			}
524		}
525
526		/* Fault in the user page: */
527		rv = vm_fault(map, va, ftype, FALSE);
528
529		--p->p_lock;
530	} else {
531		/*
532		 * Don't allow user-mode faults in kernel address space.
533		 */
534		if (usermode)
535			goto nogo;
536
537		/*
538		 * Since we know that kernel virtual address addresses
539		 * always have pte pages mapped, we just have to fault
540		 * the page.
541		 */
542		rv = vm_fault(kernel_map, va, ftype, FALSE);
543	}
544
545	if (rv == KERN_SUCCESS)
546		return (0);
547nogo:
548	if (!usermode) {
549		if (curpcb && curpcb->pcb_onfault) {
550			frame->tf_eip = (int)curpcb->pcb_onfault;
551			return (0);
552		}
553		trap_fatal(frame);
554		return (-1);
555	}
556
557	/* kludge to pass faulting virtual address to sendsig */
558	frame->tf_err = eva;
559
560	return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
561}
562#endif
563
564int
565trap_pfault(frame, usermode)
566	struct trapframe *frame;
567	int usermode;
568{
569	vm_offset_t va;
570	struct vmspace *vm = NULL;
571	vm_map_t map = 0;
572	int rv = 0;
573	vm_prot_t ftype;
574	int eva;
575	struct proc *p = curproc;
576
577	eva = rcr2();
578	va = trunc_page((vm_offset_t)eva);
579
580	if (va >= KERNBASE) {
581		/*
582		 * Don't allow user-mode faults in kernel address space.
583		 */
584		if (usermode)
585			goto nogo;
586
587		map = kernel_map;
588	} else {
589		/*
590		 * This is a fault on non-kernel virtual memory.
591		 * vm is initialized above to NULL. If curproc is NULL
592		 * or curproc->p_vmspace is NULL the fault is fatal.
593		 */
594		if (p != NULL)
595			vm = p->p_vmspace;
596
597		if (vm == NULL)
598			goto nogo;
599
600		map = &vm->vm_map;
601	}
602
603	if (frame->tf_err & PGEX_W)
604		ftype = VM_PROT_READ | VM_PROT_WRITE;
605	else
606		ftype = VM_PROT_READ;
607
608	if (map != kernel_map) {
609		/*
610		 * Keep swapout from messing with us during this
611		 *	critical time.
612		 */
613		++p->p_lock;
614
615		/*
616		 * Grow the stack if necessary
617		 */
618		if ((caddr_t)va > vm->vm_maxsaddr
619		    && (caddr_t)va < (caddr_t)USRSTACK) {
620			if (!grow(p, va)) {
621				rv = KERN_FAILURE;
622				--p->p_lock;
623				goto nogo;
624			}
625		}
626
627		/* Fault in the user page: */
628		rv = vm_fault(map, va, ftype, FALSE);
629
630		--p->p_lock;
631	} else {
632		/*
633		 * Since we know that kernel virtual address addresses
634		 * always have pte pages mapped, we just have to fault
635		 * the page.
636		 */
637		rv = vm_fault(map, va, ftype, FALSE);
638	}
639
640	if (rv == KERN_SUCCESS)
641		return (0);
642nogo:
643	if (!usermode) {
644		if (curpcb && curpcb->pcb_onfault) {
645			frame->tf_eip = (int)curpcb->pcb_onfault;
646			return (0);
647		}
648		trap_fatal(frame);
649		return (-1);
650	}
651
652	/* kludge to pass faulting virtual address to sendsig */
653	frame->tf_err = eva;
654
655	return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
656}
657
658static void
659trap_fatal(frame)
660	struct trapframe *frame;
661{
662	int code, type, eva, ss, esp;
663	struct soft_segment_descriptor softseg;
664
665	code = frame->tf_err;
666	type = frame->tf_trapno;
667	eva = rcr2();
668	sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
669
670	if (type <= MAX_TRAP_MSG)
671		printf("\n\nFatal trap %d: %s while in %s mode\n",
672			type, trap_msg[type],
673			ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
674	if (type == T_PAGEFLT) {
675		printf("fault virtual address	= 0x%x\n", eva);
676		printf("fault code		= %s %s, %s\n",
677			code & PGEX_U ? "user" : "supervisor",
678			code & PGEX_W ? "write" : "read",
679			code & PGEX_P ? "protection violation" : "page not present");
680	}
681	printf("instruction pointer	= 0x%x:0x%x\n",
682	       frame->tf_cs & 0xffff, frame->tf_eip);
683	if (ISPL(frame->tf_cs) == SEL_UPL) {
684		ss = frame->tf_ss & 0xffff;
685		esp = frame->tf_esp;
686	} else {
687		ss = GSEL(GDATA_SEL, SEL_KPL);
688		esp = (int)&frame->tf_esp;
689	}
690	printf("stack pointer	        = 0x%x:0x%x\n", ss, esp);
691	printf("frame pointer	        = 0x%x:0x%x\n", ss, frame->tf_ebp);
692	printf("code segment		= base 0x%x, limit 0x%x, type 0x%x\n",
693	       softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
694	printf("			= DPL %d, pres %d, def32 %d, gran %d\n",
695	       softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
696	       softseg.ssd_gran);
697	printf("processor eflags	= ");
698	if (frame->tf_eflags & PSL_T)
699		printf("trace trap, ");
700	if (frame->tf_eflags & PSL_I)
701		printf("interrupt enabled, ");
702	if (frame->tf_eflags & PSL_NT)
703		printf("nested task, ");
704	if (frame->tf_eflags & PSL_RF)
705		printf("resume, ");
706	if (frame->tf_eflags & PSL_VM)
707		printf("vm86, ");
708	printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
709	printf("current process		= ");
710	if (curproc) {
711		printf("%lu (%s)\n",
712		    (u_long)curproc->p_pid, curproc->p_comm ?
713		    curproc->p_comm : "");
714	} else {
715		printf("Idle\n");
716	}
717	printf("interrupt mask		= ");
718	if ((cpl & net_imask) == net_imask)
719		printf("net ");
720	if ((cpl & tty_imask) == tty_imask)
721		printf("tty ");
722	if ((cpl & bio_imask) == bio_imask)
723		printf("bio ");
724	if (cpl == 0)
725		printf("none");
726	printf("\n");
727
728#ifdef KDB
729	if (kdb_trap(&psl))
730		return;
731#endif
732#ifdef DDB
733	if (kdb_trap (type, 0, frame))
734		return;
735#endif
736	if (type <= MAX_TRAP_MSG)
737		panic(trap_msg[type]);
738	else
739		panic("unknown/reserved trap");
740}
741
742/*
743 * Double fault handler. Called when a fault occurs while writing
744 * a frame for a trap/exception onto the stack. This usually occurs
745 * when the stack overflows (such is the case with infinite recursion,
746 * for example).
747 *
748 * XXX Note that the current PTD gets replaced by IdlePTD when the
749 * task switch occurs. This means that the stack that was active at
750 * the time of the double fault is not available at <kstack> unless
751 * the machine was idle when the double fault occurred. The downside
752 * of this is that "trace <ebp>" in ddb won't work.
753 */
754void
755dblfault_handler()
756{
757	struct pcb *pcb = curpcb;
758
759	if (pcb != NULL) {
760		printf("\nFatal double fault:\n");
761		printf("eip = 0x%x\n", pcb->pcb_tss.tss_eip);
762		printf("esp = 0x%x\n", pcb->pcb_tss.tss_esp);
763		printf("ebp = 0x%x\n", pcb->pcb_tss.tss_ebp);
764	}
765
766	panic("double fault");
767}
768
769/*
770 * Compensate for 386 brain damage (missing URKR).
771 * This is a little simpler than the pagefault handler in trap() because
772 * it the page tables have already been faulted in and high addresses
773 * are thrown out early for other reasons.
774 */
775int trapwrite(addr)
776	unsigned addr;
777{
778	struct proc *p;
779	vm_offset_t va, v;
780	struct vmspace *vm;
781	int rv;
782
783	va = trunc_page((vm_offset_t)addr);
784	/*
785	 * XXX - MAX is END.  Changed > to >= for temp. fix.
786	 */
787	if (va >= VM_MAXUSER_ADDRESS)
788		return (1);
789
790	p = curproc;
791	vm = p->p_vmspace;
792
793	++p->p_lock;
794
795	if ((caddr_t)va >= vm->vm_maxsaddr
796	    && (caddr_t)va < (caddr_t)USRSTACK) {
797		if (!grow(p, va)) {
798			--p->p_lock;
799			return (1);
800		}
801	}
802
803	v = trunc_page(vtopte(va));
804
805	/*
806	 * fault the data page
807	 */
808	rv = vm_fault(&vm->vm_map, va, VM_PROT_READ|VM_PROT_WRITE, FALSE);
809
810	--p->p_lock;
811
812	if (rv != KERN_SUCCESS)
813		return 1;
814
815	return (0);
816}
817
818/*
819 * System call request from POSIX system call gate interface to kernel.
820 * Like trap(), argument is call by reference.
821 */
822void
823syscall(frame)
824	struct trapframe frame;
825{
826	caddr_t params;
827	int i;
828	struct sysent *callp;
829	struct proc *p = curproc;
830	u_quad_t sticks;
831	int error;
832	int args[8], rval[2];
833	u_int code;
834
835	sticks = p->p_sticks;
836	if (ISPL(frame.tf_cs) != SEL_UPL)
837		panic("syscall");
838
839	p->p_md.md_regs = (int *)&frame;
840	params = (caddr_t)frame.tf_esp + sizeof(int);
841	code = frame.tf_eax;
842	if (p->p_sysent->sv_prepsyscall) {
843		(*p->p_sysent->sv_prepsyscall)(&frame, args, &code, &params);
844	} else {
845		/*
846		 * Need to check if this is a 32 bit or 64 bit syscall.
847		 */
848		if (code == SYS_syscall) {
849			/*
850			 * Code is first argument, followed by actual args.
851			 */
852			code = fuword(params);
853			params += sizeof(int);
854		} else if (code == SYS___syscall) {
855			/*
856			 * Like syscall, but code is a quad, so as to maintain
857			 * quad alignment for the rest of the arguments.
858			 */
859			code = fuword(params);
860			params += sizeof(quad_t);
861		}
862	}
863
864 	if (p->p_sysent->sv_mask)
865 		code &= p->p_sysent->sv_mask;
866
867 	if (code >= p->p_sysent->sv_size)
868 		callp = &p->p_sysent->sv_table[0];
869  	else
870 		callp = &p->p_sysent->sv_table[code];
871
872	if (params && (i = callp->sy_narg * sizeof(int)) &&
873	    (error = copyin(params, (caddr_t)args, (u_int)i))) {
874#ifdef KTRACE
875		if (KTRPOINT(p, KTR_SYSCALL))
876			ktrsyscall(p->p_tracep, code, callp->sy_narg, args);
877#endif
878		goto bad;
879	}
880#ifdef KTRACE
881	if (KTRPOINT(p, KTR_SYSCALL))
882		ktrsyscall(p->p_tracep, code, callp->sy_narg, args);
883#endif
884	rval[0] = 0;
885	rval[1] = frame.tf_edx;
886
887	error = (*callp->sy_call)(p, args, rval);
888
889	switch (error) {
890
891	case 0:
892		/*
893		 * Reinitialize proc pointer `p' as it may be different
894		 * if this is a child returning from fork syscall.
895		 */
896		p = curproc;
897		frame.tf_eax = rval[0];
898		frame.tf_edx = rval[1];
899		frame.tf_eflags &= ~PSL_C;
900		break;
901
902	case ERESTART:
903		/*
904		 * Reconstruct pc, assuming lcall $X,y is 7 bytes,
905		 * int 0x80 is 2 bytes. We saved this in tf_err.
906		 */
907		frame.tf_eip -= frame.tf_err;
908		break;
909
910	case EJUSTRETURN:
911		break;
912
913	default:
914bad:
915 		if (p->p_sysent->sv_errsize)
916 			if (error >= p->p_sysent->sv_errsize)
917  				error = -1;	/* XXX */
918   			else
919  				error = p->p_sysent->sv_errtbl[error];
920		frame.tf_eax = error;
921		frame.tf_eflags |= PSL_C;
922		break;
923	}
924
925	if (frame.tf_eflags & PSL_T) {
926		/* Traced syscall. */
927		frame.tf_eflags &= ~PSL_T;
928		trapsignal(p, SIGTRAP, 0);
929	}
930
931	userret(p, &frame, sticks);
932
933#ifdef KTRACE
934	if (KTRPOINT(p, KTR_SYSRET))
935		ktrsysret(p->p_tracep, code, error, rval[0]);
936#endif
937}
938