kern_time.c revision 1.94
1/*	$NetBSD: kern_time.c,v 1.94 2005/10/02 17:51:27 chs Exp $	*/
2
3/*-
4 * Copyright (c) 2000, 2004, 2005 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christopher G. Demetriou.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the NetBSD
21 *	Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 *    contributors may be used to endorse or promote products derived
24 *    from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39/*
40 * Copyright (c) 1982, 1986, 1989, 1993
41 *	The Regents of the University of California.  All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 *    notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 *    notice, this list of conditions and the following disclaimer in the
50 *    documentation and/or other materials provided with the distribution.
51 * 3. Neither the name of the University nor the names of its contributors
52 *    may be used to endorse or promote products derived from this software
53 *    without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 *	@(#)kern_time.c	8.4 (Berkeley) 5/26/95
68 */
69
70#include <sys/cdefs.h>
71__KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.94 2005/10/02 17:51:27 chs Exp $");
72
73#include "fs_nfs.h"
74#include "opt_nfs.h"
75#include "opt_nfsserver.h"
76
77#include <sys/param.h>
78#include <sys/resourcevar.h>
79#include <sys/kernel.h>
80#include <sys/systm.h>
81#include <sys/malloc.h>
82#include <sys/proc.h>
83#include <sys/sa.h>
84#include <sys/savar.h>
85#include <sys/vnode.h>
86#include <sys/signalvar.h>
87#include <sys/syslog.h>
88
89#include <sys/mount.h>
90#include <sys/syscallargs.h>
91
92#include <uvm/uvm_extern.h>
93
94#if defined(NFS) || defined(NFSSERVER)
95#include <nfs/rpcv2.h>
96#include <nfs/nfsproto.h>
97#include <nfs/nfs.h>
98#include <nfs/nfs_var.h>
99#endif
100
101#include <machine/cpu.h>
102
103static void timerupcall(struct lwp *, void *);
104
105/* Time of day and interval timer support.
106 *
107 * These routines provide the kernel entry points to get and set
108 * the time-of-day and per-process interval timers.  Subroutines
109 * here provide support for adding and subtracting timeval structures
110 * and decrementing interval timers, optionally reloading the interval
111 * timers when they expire.
112 */
113
114/* This function is used by clock_settime and settimeofday */
115int
116settime(struct timeval *tv)
117{
118	struct timeval delta;
119	struct cpu_info *ci;
120	int s;
121
122	/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
123	s = splclock();
124	timersub(tv, &time, &delta);
125	if ((delta.tv_sec < 0 || delta.tv_usec < 0) && securelevel > 1) {
126		splx(s);
127		return (EPERM);
128	}
129#ifdef notyet
130	if ((delta.tv_sec < 86400) && securelevel > 0) {
131		splx(s);
132		return (EPERM);
133	}
134#endif
135	time = *tv;
136	(void) spllowersoftclock();
137	timeradd(&boottime, &delta, &boottime);
138	/*
139	 * XXXSMP
140	 * This is wrong.  We should traverse a list of all
141	 * CPUs and add the delta to the runtime of those
142	 * CPUs which have a process on them.
143	 */
144	ci = curcpu();
145	timeradd(&ci->ci_schedstate.spc_runtime, &delta,
146	    &ci->ci_schedstate.spc_runtime);
147#	if (defined(NFS) && !defined (NFS_V2_ONLY)) || defined(NFSSERVER)
148		nqnfs_lease_updatetime(delta.tv_sec);
149#	endif
150	splx(s);
151	resettodr();
152	return (0);
153}
154
155/* ARGSUSED */
156int
157sys_clock_gettime(struct lwp *l, void *v, register_t *retval)
158{
159	struct sys_clock_gettime_args /* {
160		syscallarg(clockid_t) clock_id;
161		syscallarg(struct timespec *) tp;
162	} */ *uap = v;
163	clockid_t clock_id;
164	struct timeval atv;
165	struct timespec ats;
166	int s;
167
168	clock_id = SCARG(uap, clock_id);
169	switch (clock_id) {
170	case CLOCK_REALTIME:
171		microtime(&atv);
172		TIMEVAL_TO_TIMESPEC(&atv,&ats);
173		break;
174	case CLOCK_MONOTONIC:
175		/* XXX "hz" granularity */
176		s = splclock();
177		atv = mono_time;
178		splx(s);
179		TIMEVAL_TO_TIMESPEC(&atv,&ats);
180		break;
181	default:
182		return (EINVAL);
183	}
184
185	return copyout(&ats, SCARG(uap, tp), sizeof(ats));
186}
187
188/* ARGSUSED */
189int
190sys_clock_settime(struct lwp *l, void *v, register_t *retval)
191{
192	struct sys_clock_settime_args /* {
193		syscallarg(clockid_t) clock_id;
194		syscallarg(const struct timespec *) tp;
195	} */ *uap = v;
196	struct proc *p = l->l_proc;
197	int error;
198
199	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
200		return (error);
201
202	return (clock_settime1(SCARG(uap, clock_id), SCARG(uap, tp)));
203}
204
205
206int
207clock_settime1(clockid_t clock_id, const struct timespec *tp)
208{
209	struct timespec ats;
210	struct timeval atv;
211	int error;
212
213	if ((error = copyin(tp, &ats, sizeof(ats))) != 0)
214		return (error);
215
216	switch (clock_id) {
217	case CLOCK_REALTIME:
218		TIMESPEC_TO_TIMEVAL(&atv, &ats);
219		if ((error = settime(&atv)) != 0)
220			return (error);
221		break;
222	case CLOCK_MONOTONIC:
223		return (EINVAL);	/* read-only clock */
224	default:
225		return (EINVAL);
226	}
227
228	return 0;
229}
230
231int
232sys_clock_getres(struct lwp *l, void *v, register_t *retval)
233{
234	struct sys_clock_getres_args /* {
235		syscallarg(clockid_t) clock_id;
236		syscallarg(struct timespec *) tp;
237	} */ *uap = v;
238	clockid_t clock_id;
239	struct timespec ts;
240	int error = 0;
241
242	clock_id = SCARG(uap, clock_id);
243	switch (clock_id) {
244	case CLOCK_REALTIME:
245	case CLOCK_MONOTONIC:
246		ts.tv_sec = 0;
247		ts.tv_nsec = 1000000000 / hz;
248		break;
249	default:
250		return (EINVAL);
251	}
252
253	if (SCARG(uap, tp))
254		error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
255
256	return error;
257}
258
259/* ARGSUSED */
260int
261sys_nanosleep(struct lwp *l, void *v, register_t *retval)
262{
263	static int nanowait;
264	struct sys_nanosleep_args/* {
265		syscallarg(struct timespec *) rqtp;
266		syscallarg(struct timespec *) rmtp;
267	} */ *uap = v;
268	struct timespec rqt;
269	struct timespec rmt;
270	struct timeval atv, utv;
271	int error, s, timo;
272
273	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(struct timespec));
274	if (error)
275		return (error);
276
277	TIMESPEC_TO_TIMEVAL(&atv,&rqt);
278	if (itimerfix(&atv))
279		return (EINVAL);
280
281	s = splclock();
282	timeradd(&atv,&time,&atv);
283	timo = hzto(&atv);
284	/*
285	 * Avoid inadvertantly sleeping forever
286	 */
287	if (timo == 0)
288		timo = 1;
289	splx(s);
290
291	error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep", timo);
292	if (error == ERESTART)
293		error = EINTR;
294	if (error == EWOULDBLOCK)
295		error = 0;
296
297	if (SCARG(uap, rmtp)) {
298		int error1;
299
300		s = splclock();
301		utv = time;
302		splx(s);
303
304		timersub(&atv, &utv, &utv);
305		if (utv.tv_sec < 0)
306			timerclear(&utv);
307
308		TIMEVAL_TO_TIMESPEC(&utv,&rmt);
309		error1 = copyout((caddr_t)&rmt, (caddr_t)SCARG(uap,rmtp),
310			sizeof(rmt));
311		if (error1)
312			return (error1);
313	}
314
315	return error;
316}
317
318/* ARGSUSED */
319int
320sys_gettimeofday(struct lwp *l, void *v, register_t *retval)
321{
322	struct sys_gettimeofday_args /* {
323		syscallarg(struct timeval *) tp;
324		syscallarg(void *) tzp;		really "struct timezone *"
325	} */ *uap = v;
326	struct timeval atv;
327	int error = 0;
328	struct timezone tzfake;
329
330	if (SCARG(uap, tp)) {
331		microtime(&atv);
332		error = copyout(&atv, SCARG(uap, tp), sizeof(atv));
333		if (error)
334			return (error);
335	}
336	if (SCARG(uap, tzp)) {
337		/*
338		 * NetBSD has no kernel notion of time zone, so we just
339		 * fake up a timezone struct and return it if demanded.
340		 */
341		tzfake.tz_minuteswest = 0;
342		tzfake.tz_dsttime = 0;
343		error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
344	}
345	return (error);
346}
347
348/* ARGSUSED */
349int
350sys_settimeofday(struct lwp *l, void *v, register_t *retval)
351{
352	struct sys_settimeofday_args /* {
353		syscallarg(const struct timeval *) tv;
354		syscallarg(const void *) tzp;	really "const struct timezone *"
355	} */ *uap = v;
356	struct proc *p = l->l_proc;
357	int error;
358
359	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
360		return (error);
361
362	return settimeofday1(SCARG(uap, tv), SCARG(uap, tzp), p);
363}
364
365int
366settimeofday1(const struct timeval *utv, const struct timezone *utzp,
367    struct proc *p)
368{
369	struct timeval atv;
370	struct timezone atz;
371	struct timeval *tv = NULL;
372	struct timezone *tzp = NULL;
373	int error;
374
375	/* Verify all parameters before changing time. */
376	if (utv) {
377		if ((error = copyin(utv, &atv, sizeof(atv))) != 0)
378			return (error);
379		tv = &atv;
380	}
381	/* XXX since we don't use tz, probably no point in doing copyin. */
382	if (utzp) {
383		if ((error = copyin(utzp, &atz, sizeof(atz))) != 0)
384			return (error);
385		tzp = &atz;
386	}
387
388	if (tv)
389		if ((error = settime(tv)) != 0)
390			return (error);
391	/*
392	 * NetBSD has no kernel notion of time zone, and only an
393	 * obsolete program would try to set it, so we log a warning.
394	 */
395	if (tzp)
396		log(LOG_WARNING, "pid %d attempted to set the "
397		    "(obsolete) kernel time zone\n", p->p_pid);
398	return (0);
399}
400
401int	tickdelta;			/* current clock skew, us. per tick */
402long	timedelta;			/* unapplied time correction, us. */
403long	bigadj = 1000000;		/* use 10x skew above bigadj us. */
404int	time_adjusted;			/* set if an adjustment is made */
405
406/* ARGSUSED */
407int
408sys_adjtime(struct lwp *l, void *v, register_t *retval)
409{
410	struct sys_adjtime_args /* {
411		syscallarg(const struct timeval *) delta;
412		syscallarg(struct timeval *) olddelta;
413	} */ *uap = v;
414	struct proc *p = l->l_proc;
415	int error;
416
417	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
418		return (error);
419
420	return adjtime1(SCARG(uap, delta), SCARG(uap, olddelta), p);
421}
422
423int
424adjtime1(const struct timeval *delta, struct timeval *olddelta, struct proc *p)
425{
426	struct timeval atv;
427	long ndelta, ntickdelta, odelta;
428	int error;
429	int s;
430
431	error = copyin(delta, &atv, sizeof(struct timeval));
432	if (error)
433		return (error);
434
435	/*
436	 * Compute the total correction and the rate at which to apply it.
437	 * Round the adjustment down to a whole multiple of the per-tick
438	 * delta, so that after some number of incremental changes in
439	 * hardclock(), tickdelta will become zero, lest the correction
440	 * overshoot and start taking us away from the desired final time.
441	 */
442	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
443	if (ndelta > bigadj || ndelta < -bigadj)
444		ntickdelta = 10 * tickadj;
445	else
446		ntickdelta = tickadj;
447	if (ndelta % ntickdelta)
448		ndelta = ndelta / ntickdelta * ntickdelta;
449
450	/*
451	 * To make hardclock()'s job easier, make the per-tick delta negative
452	 * if we want time to run slower; then hardclock can simply compute
453	 * tick + tickdelta, and subtract tickdelta from timedelta.
454	 */
455	if (ndelta < 0)
456		ntickdelta = -ntickdelta;
457	if (ndelta != 0)
458		/* We need to save the system clock time during shutdown */
459		time_adjusted |= 1;
460	s = splclock();
461	odelta = timedelta;
462	timedelta = ndelta;
463	tickdelta = ntickdelta;
464	splx(s);
465
466	if (olddelta) {
467		atv.tv_sec = odelta / 1000000;
468		atv.tv_usec = odelta % 1000000;
469		error = copyout(&atv, olddelta, sizeof(struct timeval));
470	}
471	return error;
472}
473
474/*
475 * Interval timer support. Both the BSD getitimer() family and the POSIX
476 * timer_*() family of routines are supported.
477 *
478 * All timers are kept in an array pointed to by p_timers, which is
479 * allocated on demand - many processes don't use timers at all. The
480 * first three elements in this array are reserved for the BSD timers:
481 * element 0 is ITIMER_REAL, element 1 is ITIMER_VIRTUAL, and element
482 * 2 is ITIMER_PROF. The rest may be allocated by the timer_create()
483 * syscall.
484 *
485 * Realtime timers are kept in the ptimer structure as an absolute
486 * time; virtual time timers are kept as a linked list of deltas.
487 * Virtual time timers are processed in the hardclock() routine of
488 * kern_clock.c.  The real time timer is processed by a callout
489 * routine, called from the softclock() routine.  Since a callout may
490 * be delayed in real time due to interrupt processing in the system,
491 * it is possible for the real time timeout routine (realtimeexpire,
492 * given below), to be delayed in real time past when it is supposed
493 * to occur.  It does not suffice, therefore, to reload the real timer
494 * .it_value from the real time timers .it_interval.  Rather, we
495 * compute the next time in absolute time the timer should go off.  */
496
497/* Allocate a POSIX realtime timer. */
498int
499sys_timer_create(struct lwp *l, void *v, register_t *retval)
500{
501	struct sys_timer_create_args /* {
502		syscallarg(clockid_t) clock_id;
503		syscallarg(struct sigevent *) evp;
504		syscallarg(timer_t *) timerid;
505	} */ *uap = v;
506
507	return timer_create1(SCARG(uap, timerid), SCARG(uap, clock_id),
508	    SCARG(uap, evp), copyin, l->l_proc);
509}
510
511int
512timer_create1(timer_t *tid, clockid_t id, struct sigevent *evp,
513    copyinout_t fetch_event, struct proc *p)
514{
515	int error;
516	timer_t timerid;
517	struct ptimer *pt;
518
519	if (id < CLOCK_REALTIME ||
520	    id > CLOCK_PROF)
521		return (EINVAL);
522
523	if (p->p_timers == NULL)
524		timers_alloc(p);
525
526	/* Find a free timer slot, skipping those reserved for setitimer(). */
527	for (timerid = 3; timerid < TIMER_MAX; timerid++)
528		if (p->p_timers->pts_timers[timerid] == NULL)
529			break;
530
531	if (timerid == TIMER_MAX)
532		return EAGAIN;
533
534	pt = pool_get(&ptimer_pool, PR_WAITOK);
535	if (evp) {
536		if (((error =
537		    (*fetch_event)(evp, &pt->pt_ev, sizeof(pt->pt_ev))) != 0) ||
538		    ((pt->pt_ev.sigev_notify < SIGEV_NONE) ||
539			(pt->pt_ev.sigev_notify > SIGEV_SA))) {
540			pool_put(&ptimer_pool, pt);
541			return (error ? error : EINVAL);
542		}
543	} else {
544		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
545		switch (id) {
546		case CLOCK_REALTIME:
547			pt->pt_ev.sigev_signo = SIGALRM;
548			break;
549		case CLOCK_VIRTUAL:
550			pt->pt_ev.sigev_signo = SIGVTALRM;
551			break;
552		case CLOCK_PROF:
553			pt->pt_ev.sigev_signo = SIGPROF;
554			break;
555		}
556		pt->pt_ev.sigev_value.sival_int = timerid;
557	}
558	pt->pt_info.ksi_signo = pt->pt_ev.sigev_signo;
559	pt->pt_info.ksi_errno = 0;
560	pt->pt_info.ksi_code = 0;
561	pt->pt_info.ksi_pid = p->p_pid;
562	pt->pt_info.ksi_uid = p->p_cred->p_ruid;
563	pt->pt_info.ksi_sigval = pt->pt_ev.sigev_value;
564
565	pt->pt_type = id;
566	pt->pt_proc = p;
567	pt->pt_overruns = 0;
568	pt->pt_poverruns = 0;
569	pt->pt_entry = timerid;
570	timerclear(&pt->pt_time.it_value);
571	if (id == CLOCK_REALTIME)
572		callout_init(&pt->pt_ch);
573	else
574		pt->pt_active = 0;
575
576	p->p_timers->pts_timers[timerid] = pt;
577
578	return copyout(&timerid, tid, sizeof(timerid));
579}
580
581/* Delete a POSIX realtime timer */
582int
583sys_timer_delete(struct lwp *l, void *v, register_t *retval)
584{
585	struct sys_timer_delete_args /*  {
586		syscallarg(timer_t) timerid;
587	} */ *uap = v;
588	struct proc *p = l->l_proc;
589	timer_t timerid;
590	struct ptimer *pt, *ptn;
591	int s;
592
593	timerid = SCARG(uap, timerid);
594
595	if ((p->p_timers == NULL) ||
596	    (timerid < 2) || (timerid >= TIMER_MAX) ||
597	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
598		return (EINVAL);
599
600	if (pt->pt_type == CLOCK_REALTIME)
601		callout_stop(&pt->pt_ch);
602	else if (pt->pt_active) {
603		s = splclock();
604		ptn = LIST_NEXT(pt, pt_list);
605		LIST_REMOVE(pt, pt_list);
606		for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
607			timeradd(&pt->pt_time.it_value, &ptn->pt_time.it_value,
608			    &ptn->pt_time.it_value);
609		splx(s);
610	}
611
612	p->p_timers->pts_timers[timerid] = NULL;
613	pool_put(&ptimer_pool, pt);
614
615	return (0);
616}
617
618/*
619 * Set up the given timer. The value in pt->pt_time.it_value is taken
620 * to be an absolute time for CLOCK_REALTIME timers and a relative
621 * time for virtual timers.
622 * Must be called at splclock().
623 */
624void
625timer_settime(struct ptimer *pt)
626{
627	struct ptimer *ptn, *pptn;
628	struct ptlist *ptl;
629
630	if (pt->pt_type == CLOCK_REALTIME) {
631		callout_stop(&pt->pt_ch);
632		if (timerisset(&pt->pt_time.it_value)) {
633			/*
634			 * Don't need to check hzto() return value, here.
635			 * callout_reset() does it for us.
636			 */
637			callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
638			    realtimerexpire, pt);
639		}
640	} else {
641		if (pt->pt_active) {
642			ptn = LIST_NEXT(pt, pt_list);
643			LIST_REMOVE(pt, pt_list);
644			for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
645				timeradd(&pt->pt_time.it_value,
646				    &ptn->pt_time.it_value,
647				    &ptn->pt_time.it_value);
648		}
649		if (timerisset(&pt->pt_time.it_value)) {
650			if (pt->pt_type == CLOCK_VIRTUAL)
651				ptl = &pt->pt_proc->p_timers->pts_virtual;
652			else
653				ptl = &pt->pt_proc->p_timers->pts_prof;
654
655			for (ptn = LIST_FIRST(ptl), pptn = NULL;
656			     ptn && timercmp(&pt->pt_time.it_value,
657				 &ptn->pt_time.it_value, >);
658			     pptn = ptn, ptn = LIST_NEXT(ptn, pt_list))
659				timersub(&pt->pt_time.it_value,
660				    &ptn->pt_time.it_value,
661				    &pt->pt_time.it_value);
662
663			if (pptn)
664				LIST_INSERT_AFTER(pptn, pt, pt_list);
665			else
666				LIST_INSERT_HEAD(ptl, pt, pt_list);
667
668			for ( ; ptn ; ptn = LIST_NEXT(ptn, pt_list))
669				timersub(&ptn->pt_time.it_value,
670				    &pt->pt_time.it_value,
671				    &ptn->pt_time.it_value);
672
673			pt->pt_active = 1;
674		} else
675			pt->pt_active = 0;
676	}
677}
678
679void
680timer_gettime(struct ptimer *pt, struct itimerval *aitv)
681{
682	struct ptimer *ptn;
683
684	*aitv = pt->pt_time;
685	if (pt->pt_type == CLOCK_REALTIME) {
686		/*
687		 * Convert from absolute to relative time in .it_value
688		 * part of real time timer.  If time for real time
689		 * timer has passed return 0, else return difference
690		 * between current time and time for the timer to go
691		 * off.
692		 */
693		if (timerisset(&aitv->it_value)) {
694			if (timercmp(&aitv->it_value, &time, <))
695				timerclear(&aitv->it_value);
696			else
697				timersub(&aitv->it_value, &time,
698				    &aitv->it_value);
699		}
700	} else if (pt->pt_active) {
701		if (pt->pt_type == CLOCK_VIRTUAL)
702			ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_virtual);
703		else
704			ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_prof);
705		for ( ; ptn && ptn != pt; ptn = LIST_NEXT(ptn, pt_list))
706			timeradd(&aitv->it_value,
707			    &ptn->pt_time.it_value, &aitv->it_value);
708		KASSERT(ptn != NULL); /* pt should be findable on the list */
709	} else
710		timerclear(&aitv->it_value);
711}
712
713
714
715/* Set and arm a POSIX realtime timer */
716int
717sys_timer_settime(struct lwp *l, void *v, register_t *retval)
718{
719	struct sys_timer_settime_args /* {
720		syscallarg(timer_t) timerid;
721		syscallarg(int) flags;
722		syscallarg(const struct itimerspec *) value;
723		syscallarg(struct itimerspec *) ovalue;
724	} */ *uap = v;
725	int error;
726	struct itimerspec value, ovalue, *ovp = NULL;
727
728	if ((error = copyin(SCARG(uap, value), &value,
729	    sizeof(struct itimerspec))) != 0)
730		return (error);
731
732	if (SCARG(uap, ovalue))
733		ovp = &ovalue;
734
735	if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
736	    SCARG(uap, flags), l->l_proc)) != 0)
737		return error;
738
739	if (ovp)
740		return copyout(&ovalue, SCARG(uap, ovalue),
741		    sizeof(struct itimerspec));
742	return 0;
743}
744
745int
746dotimer_settime(int timerid, struct itimerspec *value,
747    struct itimerspec *ovalue, int flags, struct proc *p)
748{
749	int s;
750	struct itimerval val, oval;
751	struct ptimer *pt;
752
753	if ((p->p_timers == NULL) ||
754	    (timerid < 2) || (timerid >= TIMER_MAX) ||
755	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
756		return (EINVAL);
757
758	TIMESPEC_TO_TIMEVAL(&val.it_value, &value->it_value);
759	TIMESPEC_TO_TIMEVAL(&val.it_interval, &value->it_interval);
760	if (itimerfix(&val.it_value) || itimerfix(&val.it_interval))
761		return (EINVAL);
762
763	oval = pt->pt_time;
764	pt->pt_time = val;
765
766	s = splclock();
767	/*
768	 * If we've been passed a relative time for a realtime timer,
769	 * convert it to absolute; if an absolute time for a virtual
770	 * timer, convert it to relative and make sure we don't set it
771	 * to zero, which would cancel the timer, or let it go
772	 * negative, which would confuse the comparison tests.
773	 */
774	if (timerisset(&pt->pt_time.it_value)) {
775		if (pt->pt_type == CLOCK_REALTIME) {
776			if ((flags & TIMER_ABSTIME) == 0)
777				timeradd(&pt->pt_time.it_value, &time,
778				    &pt->pt_time.it_value);
779		} else {
780			if ((flags & TIMER_ABSTIME) != 0) {
781				timersub(&pt->pt_time.it_value, &time,
782				    &pt->pt_time.it_value);
783				if (!timerisset(&pt->pt_time.it_value) ||
784				    pt->pt_time.it_value.tv_sec < 0) {
785					pt->pt_time.it_value.tv_sec = 0;
786					pt->pt_time.it_value.tv_usec = 1;
787				}
788			}
789		}
790	}
791
792	timer_settime(pt);
793	splx(s);
794
795	if (ovalue) {
796		TIMEVAL_TO_TIMESPEC(&oval.it_value, &ovalue->it_value);
797		TIMEVAL_TO_TIMESPEC(&oval.it_interval, &ovalue->it_interval);
798	}
799
800	return (0);
801}
802
803/* Return the time remaining until a POSIX timer fires. */
804int
805sys_timer_gettime(struct lwp *l, void *v, register_t *retval)
806{
807	struct sys_timer_gettime_args /* {
808		syscallarg(timer_t) timerid;
809		syscallarg(struct itimerspec *) value;
810	} */ *uap = v;
811	struct itimerspec its;
812	int error;
813
814	if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
815	    &its)) != 0)
816		return error;
817
818	return copyout(&its, SCARG(uap, value), sizeof(its));
819}
820
821int
822dotimer_gettime(int timerid, struct proc *p, struct itimerspec *its)
823{
824	int s;
825	struct ptimer *pt;
826	struct itimerval aitv;
827
828	if ((p->p_timers == NULL) ||
829	    (timerid < 2) || (timerid >= TIMER_MAX) ||
830	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
831		return (EINVAL);
832
833	s = splclock();
834	timer_gettime(pt, &aitv);
835	splx(s);
836
837	TIMEVAL_TO_TIMESPEC(&aitv.it_interval, &its->it_interval);
838	TIMEVAL_TO_TIMESPEC(&aitv.it_value, &its->it_value);
839
840	return 0;
841}
842
843/*
844 * Return the count of the number of times a periodic timer expired
845 * while a notification was already pending. The counter is reset when
846 * a timer expires and a notification can be posted.
847 */
848int
849sys_timer_getoverrun(struct lwp *l, void *v, register_t *retval)
850{
851	struct sys_timer_getoverrun_args /* {
852		syscallarg(timer_t) timerid;
853	} */ *uap = v;
854	struct proc *p = l->l_proc;
855	int timerid;
856	struct ptimer *pt;
857
858	timerid = SCARG(uap, timerid);
859
860	if ((p->p_timers == NULL) ||
861	    (timerid < 2) || (timerid >= TIMER_MAX) ||
862	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
863		return (EINVAL);
864
865	*retval = pt->pt_poverruns;
866
867	return (0);
868}
869
870/* Glue function that triggers an upcall; called from userret(). */
871static void
872timerupcall(struct lwp *l, void *arg)
873{
874	struct ptimers *pt = (struct ptimers *)arg;
875	unsigned int i, fired, done;
876
877	KDASSERT(l->l_proc->p_sa);
878	/* Bail out if we do not own the virtual processor */
879	if (l->l_savp->savp_lwp != l)
880		return ;
881
882	KERNEL_PROC_LOCK(l);
883
884	fired = pt->pts_fired;
885	done = 0;
886	while ((i = ffs(fired)) != 0) {
887		siginfo_t *si;
888		int mask = 1 << --i;
889		int f;
890
891		f = l->l_flag & L_SA;
892		l->l_flag &= ~L_SA;
893		si = siginfo_alloc(PR_WAITOK);
894		si->_info = pt->pts_timers[i]->pt_info.ksi_info;
895		if (sa_upcall(l, SA_UPCALL_SIGEV | SA_UPCALL_DEFER, NULL, l,
896		    sizeof(*si), si, siginfo_free) != 0) {
897			siginfo_free(si);
898			/* XXX What do we do here?? */
899		} else
900			done |= mask;
901		fired &= ~mask;
902		l->l_flag |= f;
903	}
904	pt->pts_fired &= ~done;
905	if (pt->pts_fired == 0)
906		l->l_proc->p_userret = NULL;
907
908	KERNEL_PROC_UNLOCK(l);
909}
910
911
912/*
913 * Real interval timer expired:
914 * send process whose timer expired an alarm signal.
915 * If time is not set up to reload, then just return.
916 * Else compute next time timer should go off which is > current time.
917 * This is where delay in processing this timeout causes multiple
918 * SIGALRM calls to be compressed into one.
919 */
920void
921realtimerexpire(void *arg)
922{
923	struct ptimer *pt;
924	int s;
925
926	pt = (struct ptimer *)arg;
927
928	itimerfire(pt);
929
930	if (!timerisset(&pt->pt_time.it_interval)) {
931		timerclear(&pt->pt_time.it_value);
932		return;
933	}
934	for (;;) {
935		s = splclock();
936		timeradd(&pt->pt_time.it_value,
937		    &pt->pt_time.it_interval, &pt->pt_time.it_value);
938		if (timercmp(&pt->pt_time.it_value, &time, >)) {
939			/*
940			 * Don't need to check hzto() return value, here.
941			 * callout_reset() does it for us.
942			 */
943			callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
944			    realtimerexpire, pt);
945			splx(s);
946			return;
947		}
948		splx(s);
949		pt->pt_overruns++;
950	}
951}
952
953/* BSD routine to get the value of an interval timer. */
954/* ARGSUSED */
955int
956sys_getitimer(struct lwp *l, void *v, register_t *retval)
957{
958	struct sys_getitimer_args /* {
959		syscallarg(int) which;
960		syscallarg(struct itimerval *) itv;
961	} */ *uap = v;
962	struct proc *p = l->l_proc;
963	struct itimerval aitv;
964	int error;
965
966	error = dogetitimer(p, SCARG(uap, which), &aitv);
967	if (error)
968		return error;
969	return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
970}
971
972int
973dogetitimer(struct proc *p, int which, struct itimerval *itvp)
974{
975	int s;
976
977	if ((u_int)which > ITIMER_PROF)
978		return (EINVAL);
979
980	if ((p->p_timers == NULL) || (p->p_timers->pts_timers[which] == NULL)){
981		timerclear(&itvp->it_value);
982		timerclear(&itvp->it_interval);
983	} else {
984		s = splclock();
985		timer_gettime(p->p_timers->pts_timers[which], itvp);
986		splx(s);
987	}
988
989	return 0;
990}
991
992/* BSD routine to set/arm an interval timer. */
993/* ARGSUSED */
994int
995sys_setitimer(struct lwp *l, void *v, register_t *retval)
996{
997	struct sys_setitimer_args /* {
998		syscallarg(int) which;
999		syscallarg(const struct itimerval *) itv;
1000		syscallarg(struct itimerval *) oitv;
1001	} */ *uap = v;
1002	struct proc *p = l->l_proc;
1003	int which = SCARG(uap, which);
1004	struct sys_getitimer_args getargs;
1005	const struct itimerval *itvp;
1006	struct itimerval aitv;
1007	int error;
1008
1009	if ((u_int)which > ITIMER_PROF)
1010		return (EINVAL);
1011	itvp = SCARG(uap, itv);
1012	if (itvp &&
1013	    (error = copyin(itvp, &aitv, sizeof(struct itimerval)) != 0))
1014		return (error);
1015	if (SCARG(uap, oitv) != NULL) {
1016		SCARG(&getargs, which) = which;
1017		SCARG(&getargs, itv) = SCARG(uap, oitv);
1018		if ((error = sys_getitimer(l, &getargs, retval)) != 0)
1019			return (error);
1020	}
1021	if (itvp == 0)
1022		return (0);
1023
1024	return dosetitimer(p, which, &aitv);
1025}
1026
1027int
1028dosetitimer(struct proc *p, int which, struct itimerval *itvp)
1029{
1030	struct ptimer *pt;
1031	int s;
1032
1033	if (itimerfix(&itvp->it_value) || itimerfix(&itvp->it_interval))
1034		return (EINVAL);
1035
1036	/*
1037	 * Don't bother allocating data structures if the process just
1038	 * wants to clear the timer.
1039	 */
1040	if (!timerisset(&itvp->it_value) &&
1041	    ((p->p_timers == NULL) ||(p->p_timers->pts_timers[which] == NULL)))
1042		return (0);
1043
1044	if (p->p_timers == NULL)
1045		timers_alloc(p);
1046	if (p->p_timers->pts_timers[which] == NULL) {
1047		pt = pool_get(&ptimer_pool, PR_WAITOK);
1048		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
1049		pt->pt_ev.sigev_value.sival_int = which;
1050		pt->pt_overruns = 0;
1051		pt->pt_proc = p;
1052		pt->pt_type = which;
1053		pt->pt_entry = which;
1054		switch (which) {
1055		case ITIMER_REAL:
1056			callout_init(&pt->pt_ch);
1057			pt->pt_ev.sigev_signo = SIGALRM;
1058			break;
1059		case ITIMER_VIRTUAL:
1060			pt->pt_active = 0;
1061			pt->pt_ev.sigev_signo = SIGVTALRM;
1062			break;
1063		case ITIMER_PROF:
1064			pt->pt_active = 0;
1065			pt->pt_ev.sigev_signo = SIGPROF;
1066			break;
1067		}
1068	} else
1069		pt = p->p_timers->pts_timers[which];
1070
1071	pt->pt_time = *itvp;
1072	p->p_timers->pts_timers[which] = pt;
1073
1074	s = splclock();
1075	if ((which == ITIMER_REAL) && timerisset(&pt->pt_time.it_value)) {
1076		/* Convert to absolute time */
1077		timeradd(&pt->pt_time.it_value, &time, &pt->pt_time.it_value);
1078	}
1079	timer_settime(pt);
1080	splx(s);
1081
1082	return (0);
1083}
1084
1085/* Utility routines to manage the array of pointers to timers. */
1086void
1087timers_alloc(struct proc *p)
1088{
1089	int i;
1090	struct ptimers *pts;
1091
1092	pts = malloc(sizeof (struct ptimers), M_SUBPROC, 0);
1093	LIST_INIT(&pts->pts_virtual);
1094	LIST_INIT(&pts->pts_prof);
1095	for (i = 0; i < TIMER_MAX; i++)
1096		pts->pts_timers[i] = NULL;
1097	pts->pts_fired = 0;
1098	p->p_timers = pts;
1099}
1100
1101/*
1102 * Clean up the per-process timers. If "which" is set to TIMERS_ALL,
1103 * then clean up all timers and free all the data structures. If
1104 * "which" is set to TIMERS_POSIX, only clean up the timers allocated
1105 * by timer_create(), not the BSD setitimer() timers, and only free the
1106 * structure if none of those remain.
1107 */
1108void
1109timers_free(struct proc *p, int which)
1110{
1111	int i, s;
1112	struct ptimers *pts;
1113	struct ptimer *pt, *ptn;
1114	struct timeval tv;
1115
1116	if (p->p_timers) {
1117		pts = p->p_timers;
1118		if (which == TIMERS_ALL)
1119			i = 0;
1120		else {
1121			s = splclock();
1122			timerclear(&tv);
1123			for (ptn = LIST_FIRST(&p->p_timers->pts_virtual);
1124			     ptn && ptn != pts->pts_timers[ITIMER_VIRTUAL];
1125			     ptn = LIST_NEXT(ptn, pt_list))
1126				timeradd(&tv, &ptn->pt_time.it_value, &tv);
1127			LIST_FIRST(&p->p_timers->pts_virtual) = NULL;
1128			if (ptn) {
1129				timeradd(&tv, &ptn->pt_time.it_value,
1130				    &ptn->pt_time.it_value);
1131				LIST_INSERT_HEAD(&p->p_timers->pts_virtual,
1132				    ptn, pt_list);
1133			}
1134
1135			timerclear(&tv);
1136			for (ptn = LIST_FIRST(&p->p_timers->pts_prof);
1137			     ptn && ptn != pts->pts_timers[ITIMER_PROF];
1138			     ptn = LIST_NEXT(ptn, pt_list))
1139				timeradd(&tv, &ptn->pt_time.it_value, &tv);
1140			LIST_FIRST(&p->p_timers->pts_prof) = NULL;
1141			if (ptn) {
1142				timeradd(&tv, &ptn->pt_time.it_value,
1143				    &ptn->pt_time.it_value);
1144				LIST_INSERT_HEAD(&p->p_timers->pts_prof, ptn,
1145				    pt_list);
1146			}
1147			splx(s);
1148			i = 3;
1149		}
1150		for ( ; i < TIMER_MAX; i++)
1151			if ((pt = pts->pts_timers[i]) != NULL) {
1152				if (pt->pt_type == CLOCK_REALTIME)
1153					callout_stop(&pt->pt_ch);
1154				pts->pts_timers[i] = NULL;
1155				pool_put(&ptimer_pool, pt);
1156			}
1157		if ((pts->pts_timers[0] == NULL) &&
1158		    (pts->pts_timers[1] == NULL) &&
1159		    (pts->pts_timers[2] == NULL)) {
1160			p->p_timers = NULL;
1161			free(pts, M_SUBPROC);
1162		}
1163	}
1164}
1165
1166/*
1167 * Check that a proposed value to load into the .it_value or
1168 * .it_interval part of an interval timer is acceptable, and
1169 * fix it to have at least minimal value (i.e. if it is less
1170 * than the resolution of the clock, round it up.)
1171 */
1172int
1173itimerfix(struct timeval *tv)
1174{
1175
1176	if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
1177		return (EINVAL);
1178	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
1179		tv->tv_usec = tick;
1180	return (0);
1181}
1182
1183/*
1184 * Decrement an interval timer by a specified number
1185 * of microseconds, which must be less than a second,
1186 * i.e. < 1000000.  If the timer expires, then reload
1187 * it.  In this case, carry over (usec - old value) to
1188 * reduce the value reloaded into the timer so that
1189 * the timer does not drift.  This routine assumes
1190 * that it is called in a context where the timers
1191 * on which it is operating cannot change in value.
1192 */
1193int
1194itimerdecr(struct ptimer *pt, int usec)
1195{
1196	struct itimerval *itp;
1197
1198	itp = &pt->pt_time;
1199	if (itp->it_value.tv_usec < usec) {
1200		if (itp->it_value.tv_sec == 0) {
1201			/* expired, and already in next interval */
1202			usec -= itp->it_value.tv_usec;
1203			goto expire;
1204		}
1205		itp->it_value.tv_usec += 1000000;
1206		itp->it_value.tv_sec--;
1207	}
1208	itp->it_value.tv_usec -= usec;
1209	usec = 0;
1210	if (timerisset(&itp->it_value))
1211		return (1);
1212	/* expired, exactly at end of interval */
1213expire:
1214	if (timerisset(&itp->it_interval)) {
1215		itp->it_value = itp->it_interval;
1216		itp->it_value.tv_usec -= usec;
1217		if (itp->it_value.tv_usec < 0) {
1218			itp->it_value.tv_usec += 1000000;
1219			itp->it_value.tv_sec--;
1220		}
1221		timer_settime(pt);
1222	} else
1223		itp->it_value.tv_usec = 0;		/* sec is already 0 */
1224	return (0);
1225}
1226
1227void
1228itimerfire(struct ptimer *pt)
1229{
1230	struct proc *p = pt->pt_proc;
1231	struct sadata_vp *vp;
1232	int s;
1233	unsigned int i;
1234
1235	if (pt->pt_ev.sigev_notify == SIGEV_SIGNAL) {
1236		/*
1237		 * No RT signal infrastructure exists at this time;
1238		 * just post the signal number and throw away the
1239		 * value.
1240		 */
1241		if (sigismember(&p->p_sigctx.ps_siglist, pt->pt_ev.sigev_signo))
1242			pt->pt_overruns++;
1243		else {
1244			ksiginfo_t ksi;
1245			(void)memset(&ksi, 0, sizeof(ksi));
1246			ksi.ksi_signo = pt->pt_ev.sigev_signo;
1247			ksi.ksi_code = SI_TIMER;
1248			ksi.ksi_sigval = pt->pt_ev.sigev_value;
1249			pt->pt_poverruns = pt->pt_overruns;
1250			pt->pt_overruns = 0;
1251			kpsignal(p, &ksi, NULL);
1252		}
1253	} else if (pt->pt_ev.sigev_notify == SIGEV_SA && (p->p_flag & P_SA)) {
1254		/* Cause the process to generate an upcall when it returns. */
1255
1256		if (p->p_userret == NULL) {
1257			/*
1258			 * XXX stop signals can be processed inside tsleep,
1259			 * which can be inside sa_yield's inner loop, which
1260			 * makes testing for sa_idle alone insuffucent to
1261			 * determine if we really should call setrunnable.
1262			 */
1263			pt->pt_poverruns = pt->pt_overruns;
1264			pt->pt_overruns = 0;
1265			i = 1 << pt->pt_entry;
1266			p->p_timers->pts_fired = i;
1267			p->p_userret = timerupcall;
1268			p->p_userret_arg = p->p_timers;
1269
1270			SCHED_LOCK(s);
1271			SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
1272				if (vp->savp_lwp->l_flag & L_SA_IDLE) {
1273					vp->savp_lwp->l_flag &= ~L_SA_IDLE;
1274					sched_wakeup(vp->savp_lwp);
1275					break;
1276				}
1277			}
1278			SCHED_UNLOCK(s);
1279		} else if (p->p_userret == timerupcall) {
1280			i = 1 << pt->pt_entry;
1281			if ((p->p_timers->pts_fired & i) == 0) {
1282				pt->pt_poverruns = pt->pt_overruns;
1283				pt->pt_overruns = 0;
1284				p->p_timers->pts_fired |= i;
1285			} else
1286				pt->pt_overruns++;
1287		} else {
1288			pt->pt_overruns++;
1289			if ((p->p_flag & P_WEXIT) == 0)
1290				printf("itimerfire(%d): overrun %d on timer %x (userret is %p)\n",
1291				    p->p_pid, pt->pt_overruns,
1292				    pt->pt_ev.sigev_value.sival_int,
1293				    p->p_userret);
1294		}
1295	}
1296
1297}
1298
1299/*
1300 * ratecheck(): simple time-based rate-limit checking.  see ratecheck(9)
1301 * for usage and rationale.
1302 */
1303int
1304ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
1305{
1306	struct timeval tv, delta;
1307	int s, rv = 0;
1308
1309	s = splclock();
1310	tv = mono_time;
1311	splx(s);
1312
1313	timersub(&tv, lasttime, &delta);
1314
1315	/*
1316	 * check for 0,0 is so that the message will be seen at least once,
1317	 * even if interval is huge.
1318	 */
1319	if (timercmp(&delta, mininterval, >=) ||
1320	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
1321		*lasttime = tv;
1322		rv = 1;
1323	}
1324
1325	return (rv);
1326}
1327
1328/*
1329 * ppsratecheck(): packets (or events) per second limitation.
1330 */
1331int
1332ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
1333{
1334	struct timeval tv, delta;
1335	int s, rv;
1336
1337	s = splclock();
1338	tv = mono_time;
1339	splx(s);
1340
1341	timersub(&tv, lasttime, &delta);
1342
1343	/*
1344	 * check for 0,0 is so that the message will be seen at least once.
1345	 * if more than one second have passed since the last update of
1346	 * lasttime, reset the counter.
1347	 *
1348	 * we do increment *curpps even in *curpps < maxpps case, as some may
1349	 * try to use *curpps for stat purposes as well.
1350	 */
1351	if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
1352	    delta.tv_sec >= 1) {
1353		*lasttime = tv;
1354		*curpps = 0;
1355	}
1356	if (maxpps < 0)
1357		rv = 1;
1358	else if (*curpps < maxpps)
1359		rv = 1;
1360	else
1361		rv = 0;
1362
1363#if 1 /*DIAGNOSTIC?*/
1364	/* be careful about wrap-around */
1365	if (*curpps + 1 > *curpps)
1366		*curpps = *curpps + 1;
1367#else
1368	/*
1369	 * assume that there's not too many calls to this function.
1370	 * not sure if the assumption holds, as it depends on *caller's*
1371	 * behavior, not the behavior of this function.
1372	 * IMHO it is wrong to make assumption on the caller's behavior,
1373	 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
1374	 */
1375	*curpps = *curpps + 1;
1376#endif
1377
1378	return (rv);
1379}
1380