subr_syscall.c revision 2257
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.29 1994/08/18 22:34:43 wollman 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_map.h>
60#include <vm/vm_page.h>
61
62#include <machine/cpu.h>
63#include <machine/psl.h>
64#include <machine/reg.h>
65#include <machine/eflags.h>
66#include <machine/trap.h>
67
68#include "isa.h"
69#include "npx.h"
70#include "ddb.h"
71
72int	trap_pfault	__P((struct trapframe *, int));
73void	trap_fatal	__P((struct trapframe *));
74
75#define MAX_TRAP_MSG		27
76char *trap_msg[] = {
77	"reserved addressing fault",		/*  0 T_RESADFLT */
78	"privileged instruction fault",		/*  1 T_PRIVINFLT */
79	"reserved operand fault",		/*  2 T_RESOPFLT */
80	"breakpoint instruction fault",		/*  3 T_BPTFLT */
81	"",					/*  4 unused */
82	"system call trap",			/*  5 T_SYSCALL */
83	"arithmetic trap",			/*  6 T_ARITHTRAP */
84	"system forced exception",		/*  7 T_ASTFLT */
85	"segmentation (limit) fault",		/*  8 T_SEGFLT */
86	"general protection fault",		/*  9 T_PROTFLT */
87	"trace trap",				/* 10 T_TRCTRAP */
88	"",					/* 11 unused */
89	"page fault",				/* 12 T_PAGEFLT */
90	"page table fault",			/* 13 T_TABLEFLT */
91	"alignment fault",			/* 14 T_ALIGNFLT */
92	"kernel stack pointer not valid",	/* 15 T_KSPNOTVAL */
93	"bus error",				/* 16 T_BUSERR */
94	"kernel debugger fault",		/* 17 T_KDBTRAP */
95	"integer divide fault",			/* 18 T_DIVIDE */
96	"non-maskable interrupt trap",		/* 19 T_NMI */
97	"overflow trap",			/* 20 T_OFLOW */
98	"FPU bounds check fault",		/* 21 T_BOUND */
99	"FPU device not available",		/* 22 T_DNA */
100	"double fault",				/* 23 T_DOUBLEFLT */
101	"FPU operand fetch fault",		/* 24 T_FPOPFLT */
102	"invalid TSS fault",			/* 25 T_TSSFLT */
103	"segment not present fault",		/* 26 T_SEGNPFLT */
104	"stack fault",				/* 27 T_STKFLT */
105};
106
107static inline void
108userret(p, frame, oticks)
109	struct proc *p;
110	struct trapframe *frame;
111	u_quad_t oticks;
112{
113	int sig, s;
114
115	while (sig = CURSIG(p))
116		postsig(sig);
117	p->p_priority = p->p_usrpri;
118	if (want_resched) {
119		/*
120		 * Since we are curproc, clock will normally just change
121		 * our priority without moving us from one queue to another
122		 * (since the running process is not on a queue.)
123		 * If that happened after we setrunqueue ourselves but before we
124		 * mi_switch()'ed, we might not be on the queue indicated by
125		 * our priority.
126		 */
127		s = splclock();
128		setrunqueue(p);
129		p->p_stats->p_ru.ru_nivcsw++;
130		mi_switch();
131		splx(s);
132		while (sig = CURSIG(p))
133			postsig(sig);
134	}
135	if (p->p_stats->p_prof.pr_scale) {
136		u_quad_t ticks = p->p_sticks - oticks;
137
138		if (ticks) {
139#ifdef PROFTIMER
140			extern int profscale;
141			addupc(frame->tf_eip, &p->p_stats->p_prof,
142			    ticks * profscale);
143#else
144			addupc(frame->tf_eip, &p->p_stats->p_prof, ticks);
145#endif
146		}
147	}
148	curpriority = p->p_priority;
149}
150
151/*
152 * trap(frame):
153 *	Exception, fault, and trap interface to the FreeBSD kernel.
154 * This common code is called from assembly language IDT gate entry
155 * routines that prepare a suitable stack frame, and restore this
156 * frame after the exception has been processed.
157 */
158
159/*ARGSUSED*/
160void
161trap(frame)
162	struct trapframe frame;
163{
164	struct proc *p = curproc;
165	u_quad_t sticks = 0;
166	int i = 0, ucode = 0, type, code, eva, fault_type;
167
168	frame.tf_eflags &= ~PSL_NT;	/* clear nested trap XXX */
169	type = frame.tf_trapno;
170	code = frame.tf_err;
171
172	if (ISPL(frame.tf_cs) == SEL_UPL) {
173		/* user trap */
174
175		sticks = p->p_sticks;
176		p->p_md.md_regs = (int *)&frame;
177
178		switch (type) {
179		case T_RESADFLT:	/* reserved addressing fault */
180		case T_PRIVINFLT:	/* privileged instruction fault */
181		case T_RESOPFLT:	/* reserved operand fault */
182			ucode = type;
183			i = SIGILL;
184			break;
185
186		case T_BPTFLT:		/* bpt instruction fault */
187		case T_TRCTRAP:		/* trace trap */
188			frame.tf_eflags &= ~PSL_T;
189			i = SIGTRAP;
190			break;
191
192		case T_ARITHTRAP:	/* arithmetic trap */
193			ucode = code;
194			i = SIGFPE;
195			break;
196
197		case T_ASTFLT:		/* Allow process switch */
198			astoff();
199			cnt.v_soft++;
200			if ((p->p_flag & P_OWEUPC) && p->p_stats->p_prof.pr_scale) {
201				addupc(frame.tf_eip, &p->p_stats->p_prof, 1);
202				p->p_flag &= ~P_OWEUPC;
203			}
204			goto out;
205
206		case T_PROTFLT:		/* general protection fault */
207		case T_SEGNPFLT:	/* segment not present fault */
208		case T_STKFLT:		/* stack fault */
209			ucode = code + BUS_SEGM_FAULT ;
210			i = SIGBUS;
211			break;
212
213		case T_PAGEFLT:		/* page fault */
214			i = trap_pfault(&frame, TRUE);
215			if (i == 0)
216				goto out;
217
218			ucode = T_PAGEFLT;
219			break;
220
221		case T_DIVIDE:		/* integer divide fault */
222			ucode = FPE_INTDIV_TRAP;
223			i = SIGFPE;
224			break;
225
226#if NISA > 0
227		case T_NMI:
228#if NDDB > 0
229			/* NMI can be hooked up to a pushbutton for debugging */
230			printf ("NMI ... going to debugger\n");
231			if (kdb_trap (type, 0, &frame))
232				return;
233#endif
234			/* machine/parity/power fail/"kitchen sink" faults */
235			if (isa_nmi(code) == 0) return;
236			panic("NMI indicates hardware failure");
237#endif
238
239		case T_OFLOW:		/* integer overflow fault */
240			ucode = FPE_INTOVF_TRAP;
241			i = SIGFPE;
242			break;
243
244		case T_BOUND:		/* bounds check fault */
245			ucode = FPE_SUBRNG_TRAP;
246			i = SIGFPE;
247			break;
248
249		case T_DNA:
250#if NNPX > 0
251			/* if a transparent fault (due to context switch "late") */
252			if (npxdna())
253				return;
254#endif	/* NNPX > 0 */
255
256#if defined(MATH_EMULATE) || defined(GPL_MATH_EMULATE)
257			i = math_emulate(&frame);
258			if (i == 0) return;
259#else	/* MATH_EMULATE || GPL_MATH_EMULATE */
260			panic("trap: math emulation necessary!");
261#endif	/* MATH_EMULATE || GPL_MATH_EMULATE */
262			ucode = FPE_FPU_NP_TRAP;
263			break;
264
265		case T_FPOPFLT:		/* FPU operand fetch fault */
266			ucode = T_FPOPFLT;
267			i = SIGILL;
268			break;
269
270		default:
271			trap_fatal(&frame);
272		}
273	} else {
274		/* kernel trap */
275
276		switch (type) {
277		case T_PAGEFLT:			/* page fault */
278			(void) trap_pfault(&frame, FALSE);
279			return;
280
281		case T_PROTFLT:		/* general protection fault */
282		case T_SEGNPFLT:	/* segment not present fault */
283			if (curpcb && curpcb->pcb_onfault) {
284				frame.tf_eip = (int)curpcb->pcb_onfault;
285				return;
286			}
287			break;
288
289#if NDDB > 0
290		case T_BPTFLT:
291		case T_TRCTRAP:
292			if (kdb_trap (type, 0, &frame))
293				return;
294			break;
295#else
296		case T_TRCTRAP:	 /* trace trap -- someone single stepping lcall's */
297			/* Q: how do we turn it on again? */
298			frame.tf_eflags &= ~PSL_T;
299			return;
300#endif
301
302#if NISA > 0
303		case T_NMI:
304#if NDDB > 0
305			/* NMI can be hooked up to a pushbutton for debugging */
306			printf ("NMI ... going to debugger\n");
307			if (kdb_trap (type, 0, &frame))
308				return;
309#endif
310			/* machine/parity/power fail/"kitchen sink" faults */
311			if (isa_nmi(code) == 0) return;
312			/* FALL THROUGH */
313#endif
314		}
315
316		trap_fatal(&frame);
317	}
318
319	trapsignal(p, i, ucode);
320
321#ifdef DIAGNOSTIC
322	eva = rcr2();
323	if (type <= MAX_TRAP_MSG) {
324		uprintf("fatal process exception: %s",
325			trap_msg[type]);
326		if ((type == T_PAGEFLT) || (type == T_PROTFLT))
327			uprintf(", fault VA = 0x%x", eva);
328		uprintf("\n");
329	}
330#endif
331
332out:
333	userret(p, &frame, sticks);
334}
335
336int
337trap_pfault(frame, usermode)
338	struct trapframe *frame;
339	int usermode;
340{
341	vm_offset_t va;
342	struct vmspace *vm;
343	vm_map_t map = 0;
344	int rv = 0, oldflags;
345	vm_prot_t ftype;
346	extern vm_map_t kernel_map;
347	int eva;
348	struct proc *p = curproc;
349
350	eva = rcr2();
351	va = trunc_page((vm_offset_t)eva);
352
353	/*
354	 * Don't allow user-mode faults in kernel address space
355	 */
356	if (usermode && (va >= KERNBASE)) {
357        	goto nogo;
358	}
359
360	if ((p == 0) || (va >= KERNBASE)) {
361		vm = 0;
362		map = kernel_map;
363	} else {
364		vm = p->p_vmspace;
365		map = &vm->vm_map;
366	}
367
368	if (frame->tf_err & PGEX_W)
369		ftype = VM_PROT_READ | VM_PROT_WRITE;
370	else
371		ftype = VM_PROT_READ;
372
373	if (map != kernel_map) {
374		vm_offset_t pa;
375		vm_offset_t v = (vm_offset_t) vtopte(va);
376		vm_page_t ptepg;
377
378		/*
379		 * Keep swapout from messing with us during this
380		 *	critical time.
381		 */
382		++p->p_lock;
383
384		/*
385		 * Grow the stack if necessary
386		 */
387		if ((caddr_t)va > vm->vm_maxsaddr
388		    && (caddr_t)va < (caddr_t)USRSTACK) {
389			if (!grow(p, va)) {
390				rv = KERN_FAILURE;
391				--p->p_lock;
392				goto nogo;
393			}
394		}
395
396		/*
397		 * Check if page table is mapped, if not,
398		 *	fault it first
399		 */
400
401		/* Fault the pte only if needed: */
402		*(volatile char *)v += 0;
403
404		ptepg = (vm_page_t) pmap_pte_vm_page(vm_map_pmap(map), v);
405		if( ptepg->hold_count == 0)
406			ptepg->act_count += 3;
407		vm_page_hold(ptepg);
408
409		/* Fault in the user page: */
410		rv = vm_fault(map, va, ftype, FALSE);
411
412		vm_page_unhold(ptepg);
413
414		/*
415		 * page table pages don't need to be kept if they
416		 * are not held
417		 */
418		if( ptepg->hold_count == 0 && ptepg->wire_count == 0) {
419			pmap_page_protect( VM_PAGE_TO_PHYS(ptepg),
420				VM_PROT_NONE);
421			vm_page_free(ptepg);
422		}
423
424		--p->p_lock;
425	} else {
426		/*
427		 * Since we know that kernel virtual address addresses
428		 * always have pte pages mapped, we just have to fault
429		 * the page.
430		 */
431		rv = vm_fault(map, va, ftype, FALSE);
432	}
433
434	if (rv == KERN_SUCCESS)
435		return (0);
436nogo:
437	if (!usermode) {
438		if (curpcb->pcb_onfault) {
439			frame->tf_eip = (int)curpcb->pcb_onfault;
440			return (0);
441		}
442		trap_fatal(frame);
443	}
444
445	/* kludge to pass faulting virtual address to sendsig */
446	frame->tf_err = eva;
447
448	return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
449}
450
451void
452trap_fatal(frame)
453	struct trapframe *frame;
454{
455	int code, type, eva;
456
457	code = frame->tf_err;
458	type = frame->tf_trapno;
459	eva = rcr2();
460
461	if (type <= MAX_TRAP_MSG)
462		printf("\n\nFatal trap %d: %s while in %s mode\n",
463			type, trap_msg[type],
464			ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
465	if (type == T_PAGEFLT) {
466		printf("fault virtual address	= 0x%x\n", eva);
467		printf("fault code		= %s %s, %s\n",
468			code & PGEX_U ? "user" : "supervisor",
469			code & PGEX_W ? "write" : "read",
470			code & PGEX_P ? "protection violation" : "page not present");
471	}
472	printf("instruction pointer	= 0x%x\n", frame->tf_eip);
473	printf("processor eflags	= ");
474	if (frame->tf_eflags & EFL_TF)
475		printf("trace/trap, ");
476	if (frame->tf_eflags & EFL_IF)
477		printf("interrupt enabled, ");
478	if (frame->tf_eflags & EFL_NT)
479		printf("nested task, ");
480	if (frame->tf_eflags & EFL_RF)
481		printf("resume, ");
482	if (frame->tf_eflags & EFL_VM)
483		printf("vm86, ");
484	printf("IOPL = %d\n", (frame->tf_eflags & EFL_IOPL) >> 12);
485	printf("current process		= ");
486	if (curproc) {
487		printf("%d (%s)\n",
488		    curproc->p_pid, curproc->p_comm ?
489		    curproc->p_comm : "");
490	} else {
491		printf("Idle\n");
492	}
493	printf("interrupt mask		= ");
494	if ((cpl & net_imask) == net_imask)
495		printf("net ");
496	if ((cpl & tty_imask) == tty_imask)
497		printf("tty ");
498	if ((cpl & bio_imask) == bio_imask)
499		printf("bio ");
500	if (cpl == 0)
501		printf("none");
502	printf("\n");
503
504#ifdef KDB
505	if (kdb_trap(&psl))
506		return;
507#endif
508#if NDDB > 0
509	if (kdb_trap (type, 0, frame))
510		return;
511#endif
512	if (type <= MAX_TRAP_MSG)
513		panic(trap_msg[type]);
514	else
515		panic("unknown/reserved trap");
516}
517
518/*
519 * Compensate for 386 brain damage (missing URKR).
520 * This is a little simpler than the pagefault handler in trap() because
521 * it the page tables have already been faulted in and high addresses
522 * are thrown out early for other reasons.
523 */
524int trapwrite(addr)
525	unsigned addr;
526{
527	struct proc *p;
528	vm_offset_t va, v;
529	struct vmspace *vm;
530	int oldflags;
531	int rv;
532
533	va = trunc_page((vm_offset_t)addr);
534	/*
535	 * XXX - MAX is END.  Changed > to >= for temp. fix.
536	 */
537	if (va >= VM_MAXUSER_ADDRESS)
538		return (1);
539
540	p = curproc;
541	vm = p->p_vmspace;
542
543	++p->p_lock;
544
545	if ((caddr_t)va >= vm->vm_maxsaddr
546	    && (caddr_t)va < (caddr_t)USRSTACK) {
547		if (!grow(p, va)) {
548			--p->p_lock;
549			return (1);
550		}
551	}
552
553	v = trunc_page(vtopte(va));
554
555	/*
556	 * wire the pte page
557	 */
558	if (va < USRSTACK) {
559		vm_map_pageable(&vm->vm_map, v, round_page(v+1), FALSE);
560	}
561
562	/*
563	 * fault the data page
564	 */
565	rv = vm_fault(&vm->vm_map, va, VM_PROT_READ|VM_PROT_WRITE, FALSE);
566
567	/*
568	 * unwire the pte page
569	 */
570	if (va < USRSTACK) {
571		vm_map_pageable(&vm->vm_map, v, round_page(v+1), TRUE);
572	}
573
574	--p->p_lock;
575
576	if (rv != KERN_SUCCESS)
577		return 1;
578
579	return (0);
580}
581
582/*
583 * syscall(frame):
584 *	System call request from POSIX system call gate interface to kernel.
585 * Like trap(), argument is call by reference.
586 */
587/*ARGSUSED*/
588void
589syscall(frame)
590	struct trapframe frame;
591{
592	caddr_t params;
593	int i;
594	struct sysent *callp;
595	struct proc *p = curproc;
596	u_quad_t sticks;
597	int error, opc;
598	int args[8], rval[2];
599	u_int code;
600
601	sticks = p->p_sticks;
602	if (ISPL(frame.tf_cs) != SEL_UPL)
603		panic("syscall");
604
605	code = frame.tf_eax;
606	p->p_md.md_regs = (int *)&frame;
607	params = (caddr_t)frame.tf_esp + sizeof (int) ;
608
609	/*
610	 * Reconstruct pc, assuming lcall $X,y is 7 bytes, as it is always.
611	 */
612	opc = frame.tf_eip - 7;
613	/*
614	 * Need to check if this is a 32 bit or 64 bit syscall.
615	 */
616	if (code == SYS_syscall) {
617		/*
618		 * Code is first argument, followed by actual args.
619		 */
620		code = fuword(params);
621		params += sizeof (int);
622	} else if (code == SYS___syscall) {
623		/*
624		 * Like syscall, but code is a quad, so as to maintain
625		 * quad alignment for the rest of the arguments.
626		 */
627		code = fuword(params + _QUAD_LOWWORD * sizeof(int));
628		params += sizeof(quad_t);
629	}
630
631 	if (p->p_sysent->sv_mask)
632 		code = code & p->p_sysent->sv_mask;
633
634 	if (code < 0 || code >= p->p_sysent->sv_size)
635 		callp = &p->p_sysent->sv_table[0];
636  	else
637 		callp = &p->p_sysent->sv_table[code];
638
639	if ((i = callp->sy_narg * sizeof (int)) &&
640	    (error = copyin(params, (caddr_t)args, (u_int)i))) {
641#ifdef KTRACE
642		if (KTRPOINT(p, KTR_SYSCALL))
643			ktrsyscall(p->p_tracep, code, callp->sy_narg, args);
644#endif
645		goto bad;
646	}
647#ifdef KTRACE
648	if (KTRPOINT(p, KTR_SYSCALL))
649		ktrsyscall(p->p_tracep, code, callp->sy_narg, args);
650#endif
651	rval[0] = 0;
652	rval[1] = frame.tf_edx;
653
654	error = (*callp->sy_call)(p, args, rval);
655
656	switch (error) {
657
658	case 0:
659		/*
660		 * Reinitialize proc pointer `p' as it may be different
661		 * if this is a child returning from fork syscall.
662		 */
663		p = curproc;
664		frame.tf_eax = rval[0];
665		frame.tf_edx = rval[1];
666		frame.tf_eflags &= ~PSL_C;	/* carry bit */
667		break;
668
669	case ERESTART:
670		frame.tf_eip = opc;
671		break;
672
673	case EJUSTRETURN:
674		break;
675
676	default:
677	bad:
678		frame.tf_eax = error;
679		frame.tf_eflags |= PSL_C;	/* carry bit */
680		break;
681	}
682
683	userret(p, &frame, sticks);
684
685#ifdef KTRACE
686	if (KTRPOINT(p, KTR_SYSRET))
687		ktrsysret(p->p_tracep, code, error, rval[0]);
688#endif
689}
690