subr_syscall.c revision 849
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.10 1993/12/03 05:07:45 alm 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 "sys/vmmeter.h"
63
64#include "machine/trap.h"
65
66#ifdef	__GNUC__
67
68/*
69 * The "r" contraint could be "rm" except for fatal bugs in gas.  As usual,
70 * we omit the size from the mov instruction to avoid nonfatal bugs in gas.
71 */
72#define	read_gs()	({ u_short gs; __asm("mov %%gs,%0" : "=r" (gs)); gs; })
73#define	write_gs(gs)	__asm("mov %0,%%gs" : : "r" ((u_short) gs))
74
75#else	/* not __GNUC__ */
76
77u_short	read_gs		__P((void));
78void	write_gs	__P((/* promoted u_short */ int gs));
79
80#endif	/* __GNUC__ */
81
82struct	sysent sysent[];
83int	nsysent;
84unsigned rcr2();
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, &v, grow_amount, FALSE) !=
357				    KERN_SUCCESS) {
358					goto nogo;
359				}
360			}
361		}
362
363		/* check if page table is mapped, if not, fault it first */
364		if (!pde_v(va)) {
365			v = (char *)trunc_page(vtopte(va));
366			rv = vm_fault(map, v, ftype, FALSE);
367			if (rv != KERN_SUCCESS) goto nogo;
368			/* check if page table fault, increment wiring */
369			vm_map_pageable(map, v, round_page(v+1), FALSE);
370		} else v=0;
371		rv = vm_fault(map, va, ftype, FALSE);
372		if (rv == KERN_SUCCESS) {
373			/*
374			 * XXX: continuation of rude stack hack
375			 */
376			nss = nss >> PGSHIFT;
377			if (nss > vm->vm_ssize)
378				vm->vm_ssize = nss;
379 			/*
380 			 * va could be a page table address, if the fault
381 			 * occurred from within copyout.  In that case,
382 			 * we have to wire it. (EWS 12/11/93)
383 			 */
384 			if (ispt(va))
385 				vm_map_pageable(map, va, round_page(va+1), FALSE);
386			va = trunc_page(vtopte(va));
387			/*
388			 * for page table, increment wiring
389			 * as long as not a page table fault as well
390			 */
391			if (!v && type != T_PAGEFLT)
392				vm_map_pageable(map, va, round_page(va+1), FALSE);
393			if (type == T_PAGEFLT)
394				return;
395			goto out;
396		}
397nogo:
398		if (type == T_PAGEFLT) {
399			if (curpcb->pcb_onfault)
400				goto copyfault;
401			printf("vm_fault(%x, %x, %x, 0) -> %x\n",
402			       map, va, ftype, rv);
403			printf("  type %x, code %x\n",
404			       type, code);
405			goto we_re_toast;
406		}
407		i = (rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV;
408
409		/* kludge to pass faulting virtual address to sendsig */
410		ucode = type &~ T_USER;
411		frame.tf_err = eva;
412
413		break;
414	    }
415
416#if NDDB == 0
417	case T_TRCTRAP:	 /* trace trap -- someone single stepping lcall's */
418		frame.tf_eflags &= ~PSL_T;
419
420			/* Q: how do we turn it on again? */
421		return;
422#endif
423
424	case T_BPTFLT|T_USER:		/* bpt instruction fault */
425	case T_TRCTRAP|T_USER:		/* trace trap */
426		frame.tf_eflags &= ~PSL_T;
427		i = SIGTRAP;
428		break;
429
430#include "isa.h"
431#if	NISA > 0
432	case T_NMI:
433	case T_NMI|T_USER:
434#if NDDB > 0
435		/* NMI can be hooked up to a pushbutton for debugging */
436		printf ("NMI ... going to debugger\n");
437		if (kdb_trap (type, 0, &frame))
438			return;
439#endif
440		/* machine/parity/power fail/"kitchen sink" faults */
441		if(isa_nmi(code) == 0) return;
442		else goto we_re_toast;
443#endif
444	}
445
446	trapsignal(p, i, ucode);
447	if ((type & T_USER) == 0)
448		return;
449out:
450	while (i = CURSIG(p))
451		psig(i);
452	p->p_pri = p->p_usrpri;
453	if (want_resched) {
454		int s;
455		/*
456		 * Since we are curproc, clock will normally just change
457		 * our priority without moving us from one queue to another
458		 * (since the running process is not on a queue.)
459		 * If that happened after we setrq ourselves but before we
460		 * swtch()'ed, we might not be on the queue indicated by
461		 * our priority.
462		 */
463		s = splclock();
464		setrq(p);
465		p->p_stats->p_ru.ru_nivcsw++;
466		swtch();
467		splx(s);
468		while (i = CURSIG(p))
469			psig(i);
470	}
471	if (p->p_stats->p_prof.pr_scale) {
472		int ticks;
473		struct timeval *tv = &p->p_stime;
474
475		ticks = ((tv->tv_sec - syst.tv_sec) * 1000 +
476			(tv->tv_usec - syst.tv_usec) / 1000) / (tick / 1000);
477		if (ticks) {
478#ifdef PROFTIMER
479			extern int profscale;
480			addupc(frame.tf_eip, &p->p_stats->p_prof,
481			    ticks * profscale);
482#else
483			addupc(frame.tf_eip, &p->p_stats->p_prof, ticks);
484#endif
485		}
486	}
487	curpri = p->p_pri;
488	curpcb->pcb_flags &= ~FM_TRAP;	/* used by sendsig */
489}
490
491/*
492 * Compensate for 386 brain damage (missing URKR).
493 * This is a little simpler than the pagefault handler in trap() because
494 * it the page tables have already been faulted in and high addresses
495 * are thrown out early for other reasons.
496 */
497int trapwrite(addr)
498	unsigned addr;
499{
500	unsigned nss;
501	struct proc *p;
502	vm_offset_t va;
503	struct vmspace *vm;
504	char *v;
505
506	va = trunc_page((vm_offset_t)addr);
507	/*
508	 * XXX - MAX is END.  Changed > to >= for temp. fix.
509	 */
510	if (va >= VM_MAXUSER_ADDRESS)
511		return (1);
512	/*
513	 * XXX: rude stack hack adapted from trap().
514	 */
515	nss = 0;
516	p = curproc;
517	vm = p->p_vmspace;
518	if ((caddr_t)va >= vm->vm_maxsaddr
519	    && (caddr_t)va < (caddr_t)USRSTACK) {
520		nss = roundup(USRSTACK - (unsigned)va, PAGE_SIZE);
521		if (nss > p->p_rlimit[RLIMIT_STACK].rlim_cur)
522			return (1);
523
524		if (vm->vm_ssize && roundup(vm->vm_ssize << PGSHIFT,
525			DFLSSIZ) < nss) {
526			int grow_amount;
527			/*
528			 * If necessary, grow the VM that the stack occupies
529			 * to allow for the rlimit. This allows us to not have
530			 * to allocate all of the VM up-front in execve (which
531			 * is expensive).
532			 * Grow the VM by the amount requested rounded up to
533			 * the nearest DFLSSIZ to provide for some hysteresis.
534			 */
535			grow_amount = roundup(nss, DFLSSIZ);
536			v = (char *)USRSTACK - roundup(vm->vm_ssize << PGSHIFT, DFLSSIZ) -
537				grow_amount;
538			/*
539			 * If there isn't enough room to extend by DFLSSIZ, then
540			 * just extend to the maximum size
541			 */
542			if (v < vm->vm_maxsaddr) {
543				v = vm->vm_maxsaddr;
544				grow_amount = MAXSSIZ - (vm->vm_ssize << PGSHIFT);
545			}
546			if (vm_allocate(&vm->vm_map, &v, grow_amount, FALSE) !=
547			    KERN_SUCCESS) {
548				return(1);
549			}
550		}
551	}
552
553	if (vm_fault(&vm->vm_map, va, VM_PROT_READ | VM_PROT_WRITE, FALSE)
554	    != KERN_SUCCESS)
555		return (1);
556
557	/*
558	 * XXX: continuation of rude stack hack
559	 */
560	nss = nss >> PGSHIFT;
561	if (nss > vm->vm_ssize)
562		vm->vm_ssize = nss;
563
564	return (0);
565}
566
567/*
568 * syscall(frame):
569 *	System call request from POSIX system call gate interface to kernel.
570 * Like trap(), argument is call by reference.
571 */
572/*ARGSUSED*/
573void
574syscall(frame)
575	volatile struct syscframe frame;
576{
577	register int *locr0 = ((int *)&frame);
578	register caddr_t params;
579	register int i;
580	register struct sysent *callp;
581	register struct proc *p = curproc;
582	struct timeval syst;
583	int error, opc;
584	int args[8], rval[2];
585	int code;
586
587#ifdef lint
588	r0 = 0; r0 = r0; r1 = 0; r1 = r1;
589#endif
590	syst = p->p_stime;
591	if (ISPL(frame.sf_cs) != SEL_UPL)
592		panic("syscall");
593
594	code = frame.sf_eax;
595	curpcb->pcb_flags &= ~FM_TRAP;	/* used by sendsig */
596	p->p_regs = (int *)&frame;
597	params = (caddr_t)frame.sf_esp + sizeof (int) ;
598
599	/*
600	 * Reconstruct pc, assuming lcall $X,y is 7 bytes, as it is always.
601	 */
602	opc = frame.sf_eip - 7;
603	callp = (code >= nsysent) ? &sysent[63] : &sysent[code];
604	if (callp == sysent) {
605		i = fuword(params);
606		params += sizeof (int);
607		callp = (code >= nsysent) ? &sysent[63] : &sysent[code];
608	}
609
610	if ((i = callp->sy_narg * sizeof (int)) &&
611	    (error = copyin(params, (caddr_t)args, (u_int)i))) {
612		frame.sf_eax = error;
613		frame.sf_eflags |= PSL_C;	/* carry bit */
614#ifdef KTRACE
615		if (KTRPOINT(p, KTR_SYSCALL))
616			ktrsyscall(p->p_tracep, code, callp->sy_narg, &args);
617#endif
618		goto done;
619	}
620#ifdef KTRACE
621	if (KTRPOINT(p, KTR_SYSCALL))
622		ktrsyscall(p->p_tracep, code, callp->sy_narg, &args);
623#endif
624	rval[0] = 0;
625	rval[1] = frame.sf_edx;
626/*pg("%d. s %d\n", p->p_pid, code);*/
627	error = (*callp->sy_call)(p, args, rval);
628	if (error == ERESTART)
629		frame.sf_eip = opc;
630	else if (error != EJUSTRETURN) {
631		if (error) {
632/*pg("error %d", error);*/
633			frame.sf_eax = error;
634			frame.sf_eflags |= PSL_C;	/* carry bit */
635		} else {
636			frame.sf_eax = rval[0];
637			frame.sf_edx = rval[1];
638			frame.sf_eflags &= ~PSL_C;	/* carry bit */
639		}
640	}
641	/* else if (error == EJUSTRETURN) */
642		/* nothing to do */
643done:
644	/*
645	 * Reinitialize proc pointer `p' as it may be different
646	 * if this is a child returning from fork syscall.
647	 */
648	p = curproc;
649	while (i = CURSIG(p))
650		psig(i);
651	p->p_pri = p->p_usrpri;
652	if (want_resched) {
653		int s;
654		/*
655		 * Since we are curproc, clock will normally just change
656		 * our priority without moving us from one queue to another
657		 * (since the running process is not on a queue.)
658		 * If that happened after we setrq ourselves but before we
659		 * swtch()'ed, we might not be on the queue indicated by
660		 * our priority.
661		 */
662		s = splclock();
663		setrq(p);
664		p->p_stats->p_ru.ru_nivcsw++;
665		swtch();
666		splx(s);
667		while (i = CURSIG(p))
668			psig(i);
669	}
670	if (p->p_stats->p_prof.pr_scale) {
671		int ticks;
672		struct timeval *tv = &p->p_stime;
673
674		ticks = ((tv->tv_sec - syst.tv_sec) * 1000 +
675			(tv->tv_usec - syst.tv_usec) / 1000) / (tick / 1000);
676		if (ticks) {
677#ifdef PROFTIMER
678			extern int profscale;
679			addupc(frame.sf_eip, &p->p_stats->p_prof,
680			    ticks * profscale);
681#else
682			addupc(frame.sf_eip, &p->p_stats->p_prof, ticks);
683#endif
684		}
685	}
686	curpri = p->p_pri;
687#ifdef KTRACE
688	if (KTRPOINT(p, KTR_SYSRET))
689		ktrsysret(p->p_tracep, code, error, rval[0]);
690#endif
691#ifdef	DIAGNOSTICx
692{ extern int _udatasel, _ucodesel;
693	if (frame.sf_ss != _udatasel)
694		printf("ss %x call %d\n", frame.sf_ss, code);
695	if ((frame.sf_cs&0xffff) != _ucodesel)
696		printf("cs %x call %d\n", frame.sf_cs, code);
697	if (frame.sf_eip > VM_MAXUSER_ADDRESS) {
698		printf("eip %x call %d\n", frame.sf_eip, code);
699		frame.sf_eip = 0;
700	}
701}
702#endif
703}
704