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