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