subr_syscall.c revision 879
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * the University of Utah, and William Jolitz.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *	from: @(#)trap.c	7.4 (Berkeley) 5/13/91
37 *	$Id: trap.c,v 1.11 1993/12/12 12:22:57 davidg Exp $
38 */
39
40/*
41 * 386 Trap and System call handleing
42 */
43
44#include "npx.h"
45#include "machine/cpu.h"
46#include "machine/psl.h"
47#include "machine/reg.h"
48
49#include "param.h"
50#include "systm.h"
51#include "proc.h"
52#include "user.h"
53#include "acct.h"
54#include "kernel.h"
55#ifdef KTRACE
56#include "ktrace.h"
57#endif
58
59#include "vm/vm_param.h"
60#include "vm/pmap.h"
61#include "vm/vm_map.h"
62#include "vm/vm_user.h"
63#include "sys/vmmeter.h"
64
65#include "machine/trap.h"
66
67#ifdef	__GNUC__
68
69/*
70 * The "r" contraint could be "rm" except for fatal bugs in gas.  As usual,
71 * we omit the size from the mov instruction to avoid nonfatal bugs in gas.
72 */
73#define	read_gs()	({ u_short gs; __asm("mov %%gs,%0" : "=r" (gs)); gs; })
74#define	write_gs(newgs)	__asm("mov %0,%%gs" : : "r" ((u_short) newgs))
75
76#else	/* not __GNUC__ */
77
78u_short	read_gs		__P((void));
79void	write_gs	__P((/* promoted u_short */ int gs));
80
81#endif	/* __GNUC__ */
82
83struct	sysent sysent[];
84int	nsysent;
85extern short cpl;
86
87#define MAX_TRAP_MSG		27
88char *trap_msg[] = {
89	"reserved addressing fault",		/*  0 T_RESADFLT */
90	"privileged instruction fault",		/*  1 T_PRIVINFLT */
91	"reserved operand fault",		/*  2 T_RESOPFLT */
92	"breakpoint instruction fault",		/*  3 T_BPTFLT */
93	"",					/*  4 unused */
94	"system call trap",			/*  5 T_SYSCALL */
95	"arithmetic trap",			/*  6 T_ARITHTRAP */
96	"system forced exception",		/*  7 T_ASTFLT */
97	"segmentation (limit) fault",		/*  8 T_SEGFLT */
98	"protection fault",			/*  9 T_PROTFLT */
99	"trace trap",				/* 10 T_TRCTRAP */
100	"",					/* 11 unused */
101	"page fault",				/* 12 T_PAGEFLT */
102	"page table fault",			/* 13 T_TABLEFLT */
103	"alignment fault",			/* 14 T_ALIGNFLT */
104	"kernel stack pointer not valid",	/* 15 T_KSPNOTVAL */
105	"bus error",				/* 16 T_BUSERR */
106	"kernel debugger fault",		/* 17 T_KDBTRAP */
107	"integer divide fault",			/* 18 T_DIVIDE */
108	"non-maskable interrupt trap",		/* 19 T_NMI */
109	"overflow trap",			/* 20 T_OFLOW */
110	"FPU bounds check fault",		/* 21 T_BOUND */
111	"FPU device not available",		/* 22 T_DNA */
112	"double fault",				/* 23 T_DOUBLEFLT */
113	"FPU operand fetch fault",		/* 24 T_FPOPFLT */
114	"invalid TSS fault",			/* 25 T_TSSFLT */
115	"segment not present fault",		/* 26 T_SEGNPFLT */
116	"stack fault",				/* 27 T_STKFLT */
117};
118
119#define pde_v(v) (PTD[((v)>>PD_SHIFT)&1023].pd_v)
120
121/*
122 * trap(frame):
123 *	Exception, fault, and trap interface to BSD kernel. This
124 * common code is called from assembly language IDT gate entry
125 * routines that prepare a suitable stack frame, and restore this
126 * frame after the exception has been processed. Note that the
127 * effect is as if the arguments were passed call by reference.
128 */
129
130/*ARGSUSED*/
131void
132trap(frame)
133	struct trapframe frame;
134{
135	register int i;
136	register struct proc *p = curproc;
137	struct timeval syst;
138	int ucode, type, code, eva;
139
140	frame.tf_eflags &= ~PSL_NT;	/* clear nested trap XXX */
141	type = frame.tf_trapno;
142#include "ddb.h"
143#if NDDB > 0
144	if (curpcb && curpcb->pcb_onfault) {
145		if (frame.tf_trapno == T_BPTFLT
146		    || frame.tf_trapno == T_TRCTRAP)
147			if (kdb_trap (type, 0, &frame))
148				return;
149	}
150#endif
151
152/*pg("trap type %d code = %x eip = %x cs = %x eva = %x esp %x",
153			frame.tf_trapno, frame.tf_err, frame.tf_eip,
154			frame.tf_cs, rcr2(), frame.tf_esp);*/
155if(curpcb == 0 || curproc == 0) goto we_re_toast;
156	if (curpcb->pcb_onfault && frame.tf_trapno != T_PAGEFLT) {
157		extern int _udatasel;
158
159		if (read_gs() != (u_short) _udatasel)
160			/*
161			 * Some user has corrupted %gs but we depend on it in
162			 * copyout() etc.  Fix it up and retry.
163			 *
164			 * (We don't preserve %fs or %gs, so users can change
165			 * them to either _ucodesel, _udatasel or a not-present
166			 * selector, possibly ORed with 0 to 3, making them
167			 * volatile for other users.  Not preserving them saves
168			 * time and doesn't lose functionality or open security
169			 * holes.)
170			 */
171			write_gs(_udatasel);
172		else
173copyfault:
174			frame.tf_eip = (int)curpcb->pcb_onfault;
175		return;
176	}
177
178	syst = p->p_stime;
179	if (ISPL(frame.tf_cs) == SEL_UPL) {
180		type |= T_USER;
181		p->p_regs = (int *)&frame;
182		curpcb->pcb_flags |= FM_TRAP;	/* used by sendsig */
183	}
184
185	ucode=0;
186	eva = rcr2();
187	code = frame.tf_err;
188	switch (type) {
189
190	default:
191	we_re_toast:
192#ifdef KDB
193		if (kdb_trap(&psl))
194			return;
195#endif
196#if NDDB > 0
197		if (kdb_trap (type, 0, &frame))
198			return;
199#endif
200
201		if ((type & ~T_USER) <= MAX_TRAP_MSG)
202			printf("\n\nFatal trap %d: %s while in %s mode\n",
203				type & ~T_USER, trap_msg[type & ~T_USER],
204				(type & T_USER) ? "user" : "kernel");
205
206		printf("trap type = %d, code = %x\n     eip = %x, cs = %x, eflags = %x, ",
207			frame.tf_trapno, frame.tf_err, frame.tf_eip,
208			frame.tf_cs, frame.tf_eflags);
209		eva = rcr2();
210		printf("cr2 = %x, current priority = %x\n", eva, cpl);
211
212		type &= ~T_USER;
213		if (type <= MAX_TRAP_MSG)
214			panic(trap_msg[type]);
215		else
216			panic("unknown/reserved trap");
217
218		/*NOTREACHED*/
219
220	case T_SEGNPFLT|T_USER:
221	case T_STKFLT|T_USER:
222	case T_PROTFLT|T_USER:		/* protection fault */
223		ucode = code + BUS_SEGM_FAULT ;
224		i = SIGBUS;
225		break;
226
227	case T_PRIVINFLT|T_USER:	/* privileged instruction fault */
228	case T_RESADFLT|T_USER:		/* reserved addressing fault */
229	case T_RESOPFLT|T_USER:		/* reserved operand fault */
230	case T_FPOPFLT|T_USER:		/* coprocessor operand fault */
231		ucode = type &~ T_USER;
232		i = SIGILL;
233		break;
234
235	case T_ASTFLT|T_USER:		/* Allow process switch */
236		astoff();
237		cnt.v_soft++;
238		if ((p->p_flag & SOWEUPC) && p->p_stats->p_prof.pr_scale) {
239			addupc(frame.tf_eip, &p->p_stats->p_prof, 1);
240			p->p_flag &= ~SOWEUPC;
241		}
242		goto out;
243
244	case T_DNA|T_USER:
245#if NNPX > 0
246		/* if a transparent fault (due to context switch "late") */
247		if (npxdna()) return;
248#endif	/* NNPX > 0 */
249#ifdef	MATH_EMULATE
250		i = math_emulate(&frame);
251		if (i == 0) return;
252#else	/* MATH_EMULTATE */
253		panic("trap: math emulation necessary!");
254#endif	/* MATH_EMULTATE */
255		ucode = FPE_FPU_NP_TRAP;
256		break;
257
258	case T_BOUND|T_USER:
259		ucode = FPE_SUBRNG_TRAP;
260		i = SIGFPE;
261		break;
262
263	case T_OFLOW|T_USER:
264		ucode = FPE_INTOVF_TRAP;
265		i = SIGFPE;
266		break;
267
268	case T_DIVIDE|T_USER:
269		ucode = FPE_INTDIV_TRAP;
270		i = SIGFPE;
271		break;
272
273	case T_ARITHTRAP|T_USER:
274		ucode = code;
275		i = SIGFPE;
276		break;
277
278	case T_PAGEFLT:			/* allow page faults in kernel mode */
279#if 0
280		/* XXX - check only applies to 386's and 486's with WP off */
281		if (code & PGEX_P) goto we_re_toast;
282#endif
283
284		/* fall into */
285	case T_PAGEFLT|T_USER:		/* page fault */
286	    {
287		register vm_offset_t va;
288		register struct vmspace *vm = p->p_vmspace;
289		register vm_map_t map;
290		int rv = 0;
291		vm_prot_t ftype;
292		extern vm_map_t kernel_map;
293		unsigned nss;
294		char *v;
295
296		va = trunc_page((vm_offset_t)eva);
297		/*
298		 * It is only a kernel address space fault iff:
299		 * 	1. (type & T_USER) == 0  and
300		 * 	2. pcb_onfault not set or
301		 *	3. pcb_onfault set but supervisor space fault
302		 * The last can occur during an exec() copyin where the
303		 * argument space is lazy-allocated.
304		 */
305		if (type == T_PAGEFLT && va >= KERNBASE)
306			map = kernel_map;
307		else
308			map = &vm->vm_map;
309		if (code & PGEX_W)
310			ftype = VM_PROT_READ | VM_PROT_WRITE;
311		else
312			ftype = VM_PROT_READ;
313
314#ifdef DEBUG
315		if (map == kernel_map && va == 0) {
316			printf("trap: bad kernel access at %x\n", va);
317			goto we_re_toast;
318		}
319#endif
320
321		/*
322		 * XXX: rude hack to make stack limits "work"
323		 */
324		nss = 0;
325		if ((caddr_t)va >= vm->vm_maxsaddr
326			&& (caddr_t)va < (caddr_t)USRSTACK
327			&& map != kernel_map) {
328			nss = roundup(USRSTACK - (unsigned)va, PAGE_SIZE);
329			if (nss > p->p_rlimit[RLIMIT_STACK].rlim_cur) {
330				rv = KERN_FAILURE;
331				goto nogo;
332			}
333
334			if (vm->vm_ssize && roundup(vm->vm_ssize << PGSHIFT,
335			    DFLSSIZ) < nss) {
336				int grow_amount;
337				/*
338				 * If necessary, grow the VM that the stack occupies
339				 * to allow for the rlimit. This allows us to not have
340				 * to allocate all of the VM up-front in execve (which
341				 * is expensive).
342				 * Grow the VM by the amount requested rounded up to
343				 * the nearest DFLSSIZ to provide for some hysteresis.
344				 */
345				grow_amount = roundup(nss, DFLSSIZ);
346				v = (char *)USRSTACK - roundup(vm->vm_ssize << PGSHIFT,
347				    DFLSSIZ) - grow_amount;
348				/*
349				 * If there isn't enough room to extend by DFLSSIZ, then
350				 * just extend to the maximum size
351				 */
352				if (v < vm->vm_maxsaddr) {
353					v = vm->vm_maxsaddr;
354					grow_amount = MAXSSIZ - (vm->vm_ssize << PGSHIFT);
355				}
356				if (vm_allocate(&vm->vm_map, (vm_offset_t *)&v,
357						grow_amount, FALSE) !=
358				    KERN_SUCCESS) {
359					goto nogo;
360				}
361			}
362		}
363
364		/* check if page table is mapped, if not, fault it first */
365		if (!pde_v(va)) {
366			v = (char *)trunc_page(vtopte(va));
367			rv = vm_fault(map, (vm_offset_t)v, ftype, FALSE);
368			if (rv != KERN_SUCCESS) goto nogo;
369			/* check if page table fault, increment wiring */
370			vm_map_pageable(map, (vm_offset_t)v,
371					round_page(v+1), FALSE);
372		} else v=0;
373		rv = vm_fault(map, va, ftype, FALSE);
374		if (rv == KERN_SUCCESS) {
375			/*
376			 * XXX: continuation of rude stack hack
377			 */
378			nss = nss >> PGSHIFT;
379			if (nss > vm->vm_ssize)
380				vm->vm_ssize = nss;
381 			/*
382 			 * va could be a page table address, if the fault
383 			 * occurred from within copyout.  In that case,
384 			 * we have to wire it. (EWS 12/11/93)
385 			 */
386 			if (ispt(va))
387 				vm_map_pageable(map, va, round_page(va+1), FALSE);
388			va = trunc_page(vtopte(va));
389			/*
390			 * for page table, increment wiring
391			 * as long as not a page table fault as well
392			 */
393			if (!v && type != T_PAGEFLT)
394				vm_map_pageable(map, va, round_page(va+1), FALSE);
395			if (type == T_PAGEFLT)
396				return;
397			goto out;
398		}
399nogo:
400		if (type == T_PAGEFLT) {
401			if (curpcb->pcb_onfault)
402				goto copyfault;
403			printf("vm_fault(%x, %x, %x, 0) -> %x\n",
404			       map, va, ftype, rv);
405			printf("  type %x, code %x\n",
406			       type, code);
407			goto we_re_toast;
408		}
409		i = (rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV;
410
411		/* kludge to pass faulting virtual address to sendsig */
412		ucode = type &~ T_USER;
413		frame.tf_err = eva;
414
415		break;
416	    }
417
418#if NDDB == 0
419	case T_TRCTRAP:	 /* trace trap -- someone single stepping lcall's */
420		frame.tf_eflags &= ~PSL_T;
421
422			/* Q: how do we turn it on again? */
423		return;
424#endif
425
426	case T_BPTFLT|T_USER:		/* bpt instruction fault */
427	case T_TRCTRAP|T_USER:		/* trace trap */
428		frame.tf_eflags &= ~PSL_T;
429		i = SIGTRAP;
430		break;
431
432#include "isa.h"
433#if	NISA > 0
434	case T_NMI:
435	case T_NMI|T_USER:
436#if NDDB > 0
437		/* NMI can be hooked up to a pushbutton for debugging */
438		printf ("NMI ... going to debugger\n");
439		if (kdb_trap (type, 0, &frame))
440			return;
441#endif
442		/* machine/parity/power fail/"kitchen sink" faults */
443		if(isa_nmi(code) == 0) return;
444		else goto we_re_toast;
445#endif
446	}
447
448	trapsignal(p, i, ucode);
449	if ((type & T_USER) == 0)
450		return;
451out:
452	while (i = CURSIG(p))
453		psig(i);
454	p->p_pri = p->p_usrpri;
455	if (want_resched) {
456		int s;
457		/*
458		 * Since we are curproc, clock will normally just change
459		 * our priority without moving us from one queue to another
460		 * (since the running process is not on a queue.)
461		 * If that happened after we setrq ourselves but before we
462		 * swtch()'ed, we might not be on the queue indicated by
463		 * our priority.
464		 */
465		s = splclock();
466		setrq(p);
467		p->p_stats->p_ru.ru_nivcsw++;
468		swtch();
469		splx(s);
470		while (i = CURSIG(p))
471			psig(i);
472	}
473	if (p->p_stats->p_prof.pr_scale) {
474		int ticks;
475		struct timeval *tv = &p->p_stime;
476
477		ticks = ((tv->tv_sec - syst.tv_sec) * 1000 +
478			(tv->tv_usec - syst.tv_usec) / 1000) / (tick / 1000);
479		if (ticks) {
480#ifdef PROFTIMER
481			extern int profscale;
482			addupc(frame.tf_eip, &p->p_stats->p_prof,
483			    ticks * profscale);
484#else
485			addupc(frame.tf_eip, &p->p_stats->p_prof, ticks);
486#endif
487		}
488	}
489	curpri = p->p_pri;
490	curpcb->pcb_flags &= ~FM_TRAP;	/* used by sendsig */
491}
492
493/*
494 * Compensate for 386 brain damage (missing URKR).
495 * This is a little simpler than the pagefault handler in trap() because
496 * it the page tables have already been faulted in and high addresses
497 * are thrown out early for other reasons.
498 */
499int trapwrite(addr)
500	unsigned addr;
501{
502	unsigned nss;
503	struct proc *p;
504	vm_offset_t va;
505	struct vmspace *vm;
506	char *v;
507
508	va = trunc_page((vm_offset_t)addr);
509	/*
510	 * XXX - MAX is END.  Changed > to >= for temp. fix.
511	 */
512	if (va >= VM_MAXUSER_ADDRESS)
513		return (1);
514	/*
515	 * XXX: rude stack hack adapted from trap().
516	 */
517	nss = 0;
518	p = curproc;
519	vm = p->p_vmspace;
520	if ((caddr_t)va >= vm->vm_maxsaddr
521	    && (caddr_t)va < (caddr_t)USRSTACK) {
522		nss = roundup(USRSTACK - (unsigned)va, PAGE_SIZE);
523		if (nss > p->p_rlimit[RLIMIT_STACK].rlim_cur)
524			return (1);
525
526		if (vm->vm_ssize && roundup(vm->vm_ssize << PGSHIFT,
527			DFLSSIZ) < nss) {
528			int grow_amount;
529			/*
530			 * If necessary, grow the VM that the stack occupies
531			 * to allow for the rlimit. This allows us to not have
532			 * to allocate all of the VM up-front in execve (which
533			 * is expensive).
534			 * Grow the VM by the amount requested rounded up to
535			 * the nearest DFLSSIZ to provide for some hysteresis.
536			 */
537			grow_amount = roundup(nss, DFLSSIZ);
538			v = (char *)USRSTACK - roundup(vm->vm_ssize << PGSHIFT, DFLSSIZ) -
539				grow_amount;
540			/*
541			 * If there isn't enough room to extend by DFLSSIZ, then
542			 * just extend to the maximum size
543			 */
544			if (v < vm->vm_maxsaddr) {
545				v = vm->vm_maxsaddr;
546				grow_amount = MAXSSIZ - (vm->vm_ssize << PGSHIFT);
547			}
548			if (vm_allocate(&vm->vm_map, (vm_offset_t *)&v,
549					grow_amount, FALSE)
550			    != KERN_SUCCESS) {
551				return(1);
552			}
553		}
554	}
555
556	if (vm_fault(&vm->vm_map, va, VM_PROT_READ | VM_PROT_WRITE, FALSE)
557	    != KERN_SUCCESS)
558		return (1);
559
560	/*
561	 * XXX: continuation of rude stack hack
562	 */
563	nss = nss >> PGSHIFT;
564	if (nss > vm->vm_ssize)
565		vm->vm_ssize = nss;
566
567	return (0);
568}
569
570/*
571 * syscall(frame):
572 *	System call request from POSIX system call gate interface to kernel.
573 * Like trap(), argument is call by reference.
574 */
575/*ARGSUSED*/
576void
577syscall(frame)
578	volatile struct syscframe frame;
579{
580	register int *locr0 = ((int *)&frame);
581	register caddr_t params;
582	register int i;
583	register struct sysent *callp;
584	register struct proc *p = curproc;
585	struct timeval syst;
586	int error, opc;
587	int args[8], rval[2];
588	int code;
589
590#ifdef lint
591	r0 = 0; r0 = r0; r1 = 0; r1 = r1;
592#endif
593	syst = p->p_stime;
594	if (ISPL(frame.sf_cs) != SEL_UPL)
595		panic("syscall");
596
597	code = frame.sf_eax;
598	curpcb->pcb_flags &= ~FM_TRAP;	/* used by sendsig */
599	p->p_regs = (int *)&frame;
600	params = (caddr_t)frame.sf_esp + sizeof (int) ;
601
602	/*
603	 * Reconstruct pc, assuming lcall $X,y is 7 bytes, as it is always.
604	 */
605	opc = frame.sf_eip - 7;
606	callp = (code >= nsysent) ? &sysent[63] : &sysent[code];
607	if (callp == sysent) {
608		i = fuword(params);
609		params += sizeof (int);
610		callp = (code >= nsysent) ? &sysent[63] : &sysent[code];
611	}
612
613	if ((i = callp->sy_narg * sizeof (int)) &&
614	    (error = copyin(params, (caddr_t)args, (u_int)i))) {
615		frame.sf_eax = error;
616		frame.sf_eflags |= PSL_C;	/* carry bit */
617#ifdef KTRACE
618		if (KTRPOINT(p, KTR_SYSCALL))
619			ktrsyscall(p->p_tracep, code, callp->sy_narg, args);
620#endif
621		goto done;
622	}
623#ifdef KTRACE
624	if (KTRPOINT(p, KTR_SYSCALL))
625		ktrsyscall(p->p_tracep, code, callp->sy_narg, args);
626#endif
627	rval[0] = 0;
628	rval[1] = frame.sf_edx;
629/*pg("%d. s %d\n", p->p_pid, code);*/
630	error = (*callp->sy_call)(p, args, rval);
631	if (error == ERESTART)
632		frame.sf_eip = opc;
633	else if (error != EJUSTRETURN) {
634		if (error) {
635/*pg("error %d", error);*/
636			frame.sf_eax = error;
637			frame.sf_eflags |= PSL_C;	/* carry bit */
638		} else {
639			frame.sf_eax = rval[0];
640			frame.sf_edx = rval[1];
641			frame.sf_eflags &= ~PSL_C;	/* carry bit */
642		}
643	}
644	/* else if (error == EJUSTRETURN) */
645		/* nothing to do */
646done:
647	/*
648	 * Reinitialize proc pointer `p' as it may be different
649	 * if this is a child returning from fork syscall.
650	 */
651	p = curproc;
652	while (i = CURSIG(p))
653		psig(i);
654	p->p_pri = p->p_usrpri;
655	if (want_resched) {
656		int s;
657		/*
658		 * Since we are curproc, clock will normally just change
659		 * our priority without moving us from one queue to another
660		 * (since the running process is not on a queue.)
661		 * If that happened after we setrq ourselves but before we
662		 * swtch()'ed, we might not be on the queue indicated by
663		 * our priority.
664		 */
665		s = splclock();
666		setrq(p);
667		p->p_stats->p_ru.ru_nivcsw++;
668		swtch();
669		splx(s);
670		while (i = CURSIG(p))
671			psig(i);
672	}
673	if (p->p_stats->p_prof.pr_scale) {
674		int ticks;
675		struct timeval *tv = &p->p_stime;
676
677		ticks = ((tv->tv_sec - syst.tv_sec) * 1000 +
678			(tv->tv_usec - syst.tv_usec) / 1000) / (tick / 1000);
679		if (ticks) {
680#ifdef PROFTIMER
681			extern int profscale;
682			addupc(frame.sf_eip, &p->p_stats->p_prof,
683			    ticks * profscale);
684#else
685			addupc(frame.sf_eip, &p->p_stats->p_prof, ticks);
686#endif
687		}
688	}
689	curpri = p->p_pri;
690#ifdef KTRACE
691	if (KTRPOINT(p, KTR_SYSRET))
692		ktrsysret(p->p_tracep, code, error, rval[0]);
693#endif
694#ifdef	DIAGNOSTICx
695{ extern int _udatasel, _ucodesel;
696	if (frame.sf_ss != _udatasel)
697		printf("ss %x call %d\n", frame.sf_ss, code);
698	if ((frame.sf_cs&0xffff) != _ucodesel)
699		printf("cs %x call %d\n", frame.sf_cs, code);
700	if (frame.sf_eip > VM_MAXUSER_ADDRESS) {
701		printf("eip %x call %d\n", frame.sf_eip, code);
702		frame.sf_eip = 0;
703	}
704}
705#endif
706}
707