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