Home | History | Annotate | Line # | Download | only in kern
kern_time.c revision 1.140
      1 /*	$NetBSD: kern_time.c,v 1.140 2008/02/19 14:21:56 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000, 2004, 2005, 2007 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.140 2008/02/19 14:21:56 yamt Exp $");
     72 
     73 #include <sys/param.h>
     74 #include <sys/resourcevar.h>
     75 #include <sys/kernel.h>
     76 #include <sys/systm.h>
     77 #include <sys/proc.h>
     78 #include <sys/vnode.h>
     79 #include <sys/signalvar.h>
     80 #include <sys/syslog.h>
     81 #include <sys/timetc.h>
     82 #include <sys/kauth.h>
     83 
     84 #include <sys/mount.h>
     85 #include <sys/syscallargs.h>
     86 
     87 #include <uvm/uvm_extern.h>
     88 
     89 #include <sys/cpu.h>
     90 
     91 kmutex_t	time_lock;
     92 
     93 POOL_INIT(ptimer_pool, sizeof(struct ptimer), 0, 0, 0, "ptimerpl",
     94     &pool_allocator_nointr, IPL_NONE);
     95 POOL_INIT(ptimers_pool, sizeof(struct ptimers), 0, 0, 0, "ptimerspl",
     96     &pool_allocator_nointr, IPL_NONE);
     97 
     98 /*
     99  * Initialize timekeeping.
    100  */
    101 void
    102 time_init(void)
    103 {
    104 
    105 	mutex_init(&time_lock, MUTEX_DEFAULT, IPL_NONE);
    106 }
    107 
    108 /* Time of day and interval timer support.
    109  *
    110  * These routines provide the kernel entry points to get and set
    111  * the time-of-day and per-process interval timers.  Subroutines
    112  * here provide support for adding and subtracting timeval structures
    113  * and decrementing interval timers, optionally reloading the interval
    114  * timers when they expire.
    115  */
    116 
    117 /* This function is used by clock_settime and settimeofday */
    118 static int
    119 settime1(struct proc *p, struct timespec *ts, bool check_kauth)
    120 {
    121 	struct timeval delta, tv;
    122 	struct timeval now;
    123 	struct timespec ts1;
    124 	struct bintime btdelta;
    125 	lwp_t *l;
    126 	int s;
    127 
    128 	TIMESPEC_TO_TIMEVAL(&tv, ts);
    129 
    130 	/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
    131 	s = splclock();
    132 	microtime(&now);
    133 	timersub(&tv, &now, &delta);
    134 
    135 	if (check_kauth && kauth_authorize_system(kauth_cred_get(),
    136 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_SYSTEM, ts, &delta,
    137 	    KAUTH_ARG(check_kauth ? false : true)) != 0) {
    138 		splx(s);
    139 		return (EPERM);
    140 	}
    141 
    142 #ifdef notyet
    143 	if ((delta.tv_sec < 86400) && securelevel > 0) { /* XXX elad - notyet */
    144 		splx(s);
    145 		return (EPERM);
    146 	}
    147 #endif
    148 
    149 	TIMEVAL_TO_TIMESPEC(&tv, &ts1);
    150 	tc_setclock(&ts1);
    151 
    152 	timeradd(&boottime, &delta, &boottime);
    153 
    154 	/*
    155 	 * XXXSMP: There is a short race between setting the time above
    156 	 * and adjusting LWP's run times.  Fixing this properly means
    157 	 * pausing all CPUs while we adjust the clock.
    158 	 */
    159 	timeval2bintime(&delta, &btdelta);
    160 	mutex_enter(&proclist_lock);
    161 	LIST_FOREACH(l, &alllwp, l_list) {
    162 		lwp_lock(l);
    163 		bintime_add(&l->l_stime, &btdelta);
    164 		lwp_unlock(l);
    165 	}
    166 	mutex_exit(&proclist_lock);
    167 	resettodr();
    168 	splx(s);
    169 
    170 	return (0);
    171 }
    172 
    173 int
    174 settime(struct proc *p, struct timespec *ts)
    175 {
    176 	return (settime1(p, ts, true));
    177 }
    178 
    179 /* ARGSUSED */
    180 int
    181 sys_clock_gettime(struct lwp *l, const struct sys_clock_gettime_args *uap,
    182     register_t *retval)
    183 {
    184 	/* {
    185 		syscallarg(clockid_t) clock_id;
    186 		syscallarg(struct timespec *) tp;
    187 	} */
    188 	clockid_t clock_id;
    189 	struct timespec ats;
    190 
    191 	clock_id = SCARG(uap, clock_id);
    192 	switch (clock_id) {
    193 	case CLOCK_REALTIME:
    194 		nanotime(&ats);
    195 		break;
    196 	case CLOCK_MONOTONIC:
    197 		nanouptime(&ats);
    198 		break;
    199 	default:
    200 		return (EINVAL);
    201 	}
    202 
    203 	return copyout(&ats, SCARG(uap, tp), sizeof(ats));
    204 }
    205 
    206 /* ARGSUSED */
    207 int
    208 sys_clock_settime(struct lwp *l, const struct sys_clock_settime_args *uap,
    209     register_t *retval)
    210 {
    211 	/* {
    212 		syscallarg(clockid_t) clock_id;
    213 		syscallarg(const struct timespec *) tp;
    214 	} */
    215 
    216 	return clock_settime1(l->l_proc, SCARG(uap, clock_id), SCARG(uap, tp),
    217 	    true);
    218 }
    219 
    220 
    221 int
    222 clock_settime1(struct proc *p, clockid_t clock_id, const struct timespec *tp,
    223     bool check_kauth)
    224 {
    225 	struct timespec ats;
    226 	int error;
    227 
    228 	if ((error = copyin(tp, &ats, sizeof(ats))) != 0)
    229 		return (error);
    230 
    231 	switch (clock_id) {
    232 	case CLOCK_REALTIME:
    233 		if ((error = settime1(p, &ats, check_kauth)) != 0)
    234 			return (error);
    235 		break;
    236 	case CLOCK_MONOTONIC:
    237 		return (EINVAL);	/* read-only clock */
    238 	default:
    239 		return (EINVAL);
    240 	}
    241 
    242 	return 0;
    243 }
    244 
    245 int
    246 sys_clock_getres(struct lwp *l, const struct sys_clock_getres_args *uap,
    247     register_t *retval)
    248 {
    249 	/* {
    250 		syscallarg(clockid_t) clock_id;
    251 		syscallarg(struct timespec *) tp;
    252 	} */
    253 	clockid_t clock_id;
    254 	struct timespec ts;
    255 	int error = 0;
    256 
    257 	clock_id = SCARG(uap, clock_id);
    258 	switch (clock_id) {
    259 	case CLOCK_REALTIME:
    260 	case CLOCK_MONOTONIC:
    261 		ts.tv_sec = 0;
    262 		if (tc_getfrequency() > 1000000000)
    263 			ts.tv_nsec = 1;
    264 		else
    265 			ts.tv_nsec = 1000000000 / tc_getfrequency();
    266 		break;
    267 	default:
    268 		return (EINVAL);
    269 	}
    270 
    271 	if (SCARG(uap, tp))
    272 		error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
    273 
    274 	return error;
    275 }
    276 
    277 /* ARGSUSED */
    278 int
    279 sys_nanosleep(struct lwp *l, const struct sys_nanosleep_args *uap,
    280     register_t *retval)
    281 {
    282 	/* {
    283 		syscallarg(struct timespec *) rqtp;
    284 		syscallarg(struct timespec *) rmtp;
    285 	} */
    286 	struct timespec rmt, rqt;
    287 	int error, error1;
    288 
    289 	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(struct timespec));
    290 	if (error)
    291 		return (error);
    292 
    293 	error = nanosleep1(l, &rqt, SCARG(uap, rmtp) ? &rmt : NULL);
    294 	if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
    295 		return error;
    296 
    297 	error1 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
    298 	return error1 ? error1 : error;
    299 }
    300 
    301 int
    302 nanosleep1(struct lwp *l, struct timespec *rqt, struct timespec *rmt)
    303 {
    304 	int error, timo;
    305 
    306 	if (itimespecfix(rqt))
    307 		return (EINVAL);
    308 
    309 	timo = tstohz(rqt);
    310 	/*
    311 	 * Avoid inadvertantly sleeping forever
    312 	 */
    313 	if (timo == 0)
    314 		timo = 1;
    315 
    316 	if (rmt != NULL)
    317 		getnanouptime(rmt);
    318 
    319 	error = kpause("nanoslp", true, timo, NULL);
    320 	if (error == ERESTART)
    321 		error = EINTR;
    322 	if (error == EWOULDBLOCK)
    323 		error = 0;
    324 
    325 	if (rmt != NULL) {
    326 		struct timespec rmtend;
    327 
    328 		getnanouptime(&rmtend);
    329 
    330 		timespecsub(&rmtend, rmt, rmt);
    331 		timespecsub(rqt, rmt, rmt);
    332 		if (rmt->tv_sec < 0)
    333 			timespecclear(rmt);
    334 	}
    335 
    336 	return error;
    337 }
    338 
    339 /* ARGSUSED */
    340 int
    341 sys_gettimeofday(struct lwp *l, const struct sys_gettimeofday_args *uap,
    342     register_t *retval)
    343 {
    344 	/* {
    345 		syscallarg(struct timeval *) tp;
    346 		syscallarg(void *) tzp;		really "struct timezone *";
    347 	} */
    348 	struct timeval atv;
    349 	int error = 0;
    350 	struct timezone tzfake;
    351 
    352 	if (SCARG(uap, tp)) {
    353 		microtime(&atv);
    354 		error = copyout(&atv, SCARG(uap, tp), sizeof(atv));
    355 		if (error)
    356 			return (error);
    357 	}
    358 	if (SCARG(uap, tzp)) {
    359 		/*
    360 		 * NetBSD has no kernel notion of time zone, so we just
    361 		 * fake up a timezone struct and return it if demanded.
    362 		 */
    363 		tzfake.tz_minuteswest = 0;
    364 		tzfake.tz_dsttime = 0;
    365 		error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
    366 	}
    367 	return (error);
    368 }
    369 
    370 /* ARGSUSED */
    371 int
    372 sys_settimeofday(struct lwp *l, const struct sys_settimeofday_args *uap,
    373     register_t *retval)
    374 {
    375 	/* {
    376 		syscallarg(const struct timeval *) tv;
    377 		syscallarg(const void *) tzp; really "const struct timezone *";
    378 	} */
    379 
    380 	return settimeofday1(SCARG(uap, tv), true, SCARG(uap, tzp), l, true);
    381 }
    382 
    383 int
    384 settimeofday1(const struct timeval *utv, bool userspace,
    385     const void *utzp, struct lwp *l, bool check_kauth)
    386 {
    387 	struct timeval atv;
    388 	struct timespec ts;
    389 	int error;
    390 
    391 	/* Verify all parameters before changing time. */
    392 
    393 	/*
    394 	 * NetBSD has no kernel notion of time zone, and only an
    395 	 * obsolete program would try to set it, so we log a warning.
    396 	 */
    397 	if (utzp)
    398 		log(LOG_WARNING, "pid %d attempted to set the "
    399 		    "(obsolete) kernel time zone\n", l->l_proc->p_pid);
    400 
    401 	if (utv == NULL)
    402 		return 0;
    403 
    404 	if (userspace) {
    405 		if ((error = copyin(utv, &atv, sizeof(atv))) != 0)
    406 			return error;
    407 		utv = &atv;
    408 	}
    409 
    410 	TIMEVAL_TO_TIMESPEC(utv, &ts);
    411 	return settime1(l->l_proc, &ts, check_kauth);
    412 }
    413 
    414 int	time_adjusted;			/* set if an adjustment is made */
    415 
    416 /* ARGSUSED */
    417 int
    418 sys_adjtime(struct lwp *l, const struct sys_adjtime_args *uap,
    419     register_t *retval)
    420 {
    421 	/* {
    422 		syscallarg(const struct timeval *) delta;
    423 		syscallarg(struct timeval *) olddelta;
    424 	} */
    425 	int error;
    426 
    427 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
    428 	    KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL, NULL)) != 0)
    429 		return (error);
    430 
    431 	return adjtime1(SCARG(uap, delta), SCARG(uap, olddelta), l->l_proc);
    432 }
    433 
    434 int
    435 adjtime1(const struct timeval *delta, struct timeval *olddelta, struct proc *p)
    436 {
    437 	struct timeval atv;
    438 	int error = 0;
    439 
    440 	extern int64_t time_adjtime;  /* in kern_ntptime.c */
    441 
    442 	if (olddelta) {
    443 		atv.tv_sec = time_adjtime / 1000000;
    444 		atv.tv_usec = time_adjtime % 1000000;
    445 		if (atv.tv_usec < 0) {
    446 			atv.tv_usec += 1000000;
    447 			atv.tv_sec--;
    448 		}
    449 		error = copyout(&atv, olddelta, sizeof(struct timeval));
    450 		if (error)
    451 			return (error);
    452 	}
    453 
    454 	if (delta) {
    455 		error = copyin(delta, &atv, sizeof(struct timeval));
    456 		if (error)
    457 			return (error);
    458 
    459 		time_adjtime = (int64_t)atv.tv_sec * 1000000 +
    460 			atv.tv_usec;
    461 
    462 		if (time_adjtime)
    463 			/* We need to save the system time during shutdown */
    464 			time_adjusted |= 1;
    465 	}
    466 
    467 	return error;
    468 }
    469 
    470 /*
    471  * Interval timer support. Both the BSD getitimer() family and the POSIX
    472  * timer_*() family of routines are supported.
    473  *
    474  * All timers are kept in an array pointed to by p_timers, which is
    475  * allocated on demand - many processes don't use timers at all. The
    476  * first three elements in this array are reserved for the BSD timers:
    477  * element 0 is ITIMER_REAL, element 1 is ITIMER_VIRTUAL, and element
    478  * 2 is ITIMER_PROF. The rest may be allocated by the timer_create()
    479  * syscall.
    480  *
    481  * Realtime timers are kept in the ptimer structure as an absolute
    482  * time; virtual time timers are kept as a linked list of deltas.
    483  * Virtual time timers are processed in the hardclock() routine of
    484  * kern_clock.c.  The real time timer is processed by a callout
    485  * routine, called from the softclock() routine.  Since a callout may
    486  * be delayed in real time due to interrupt processing in the system,
    487  * it is possible for the real time timeout routine (realtimeexpire,
    488  * given below), to be delayed in real time past when it is supposed
    489  * to occur.  It does not suffice, therefore, to reload the real timer
    490  * .it_value from the real time timers .it_interval.  Rather, we
    491  * compute the next time in absolute time the timer should go off.  */
    492 
    493 /* Allocate a POSIX realtime timer. */
    494 int
    495 sys_timer_create(struct lwp *l, const struct sys_timer_create_args *uap,
    496     register_t *retval)
    497 {
    498 	/* {
    499 		syscallarg(clockid_t) clock_id;
    500 		syscallarg(struct sigevent *) evp;
    501 		syscallarg(timer_t *) timerid;
    502 	} */
    503 
    504 	return timer_create1(SCARG(uap, timerid), SCARG(uap, clock_id),
    505 	    SCARG(uap, evp), copyin, l);
    506 }
    507 
    508 int
    509 timer_create1(timer_t *tid, clockid_t id, struct sigevent *evp,
    510     copyin_t fetch_event, struct lwp *l)
    511 {
    512 	int error;
    513 	timer_t timerid;
    514 	struct ptimer *pt;
    515 	struct proc *p;
    516 
    517 	p = l->l_proc;
    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 = kauth_cred_getuid(l->l_cred);
    563 	pt->pt_info.ksi_value = 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, 0);
    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 */
    582 int
    583 sys_timer_delete(struct lwp *l, const struct sys_timer_delete_args *uap,
    584     register_t *retval)
    585 {
    586 	/* {
    587 		syscallarg(timer_t) timerid;
    588 	} */
    589 	struct proc *p = l->l_proc;
    590 	timer_t timerid;
    591 	struct ptimer *pt, *ptn;
    592 	int s;
    593 
    594 	timerid = SCARG(uap, timerid);
    595 
    596 	if ((p->p_timers == NULL) ||
    597 	    (timerid < 2) || (timerid >= TIMER_MAX) ||
    598 	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
    599 		return (EINVAL);
    600 
    601 	if (pt->pt_type == CLOCK_REALTIME) {
    602 		callout_stop(&pt->pt_ch);
    603 		callout_destroy(&pt->pt_ch);
    604 	} else if (pt->pt_active) {
    605 		s = splclock();
    606 		ptn = LIST_NEXT(pt, pt_list);
    607 		LIST_REMOVE(pt, pt_list);
    608 		for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
    609 			timeradd(&pt->pt_time.it_value, &ptn->pt_time.it_value,
    610 			    &ptn->pt_time.it_value);
    611 		splx(s);
    612 	}
    613 
    614 	p->p_timers->pts_timers[timerid] = NULL;
    615 	pool_put(&ptimer_pool, pt);
    616 
    617 	return (0);
    618 }
    619 
    620 /*
    621  * Set up the given timer. The value in pt->pt_time.it_value is taken
    622  * to be an absolute time for CLOCK_REALTIME timers and a relative
    623  * time for virtual timers.
    624  * Must be called at splclock().
    625  */
    626 void
    627 timer_settime(struct ptimer *pt)
    628 {
    629 	struct ptimer *ptn, *pptn;
    630 	struct ptlist *ptl;
    631 
    632 	if (pt->pt_type == CLOCK_REALTIME) {
    633 		callout_stop(&pt->pt_ch);
    634 		if (timerisset(&pt->pt_time.it_value)) {
    635 			/*
    636 			 * Don't need to check hzto() return value, here.
    637 			 * callout_reset() does it for us.
    638 			 */
    639 			callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
    640 			    realtimerexpire, pt);
    641 		}
    642 	} else {
    643 		if (pt->pt_active) {
    644 			ptn = LIST_NEXT(pt, pt_list);
    645 			LIST_REMOVE(pt, pt_list);
    646 			for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
    647 				timeradd(&pt->pt_time.it_value,
    648 				    &ptn->pt_time.it_value,
    649 				    &ptn->pt_time.it_value);
    650 		}
    651 		if (timerisset(&pt->pt_time.it_value)) {
    652 			if (pt->pt_type == CLOCK_VIRTUAL)
    653 				ptl = &pt->pt_proc->p_timers->pts_virtual;
    654 			else
    655 				ptl = &pt->pt_proc->p_timers->pts_prof;
    656 
    657 			for (ptn = LIST_FIRST(ptl), pptn = NULL;
    658 			     ptn && timercmp(&pt->pt_time.it_value,
    659 				 &ptn->pt_time.it_value, >);
    660 			     pptn = ptn, ptn = LIST_NEXT(ptn, pt_list))
    661 				timersub(&pt->pt_time.it_value,
    662 				    &ptn->pt_time.it_value,
    663 				    &pt->pt_time.it_value);
    664 
    665 			if (pptn)
    666 				LIST_INSERT_AFTER(pptn, pt, pt_list);
    667 			else
    668 				LIST_INSERT_HEAD(ptl, pt, pt_list);
    669 
    670 			for ( ; ptn ; ptn = LIST_NEXT(ptn, pt_list))
    671 				timersub(&ptn->pt_time.it_value,
    672 				    &pt->pt_time.it_value,
    673 				    &ptn->pt_time.it_value);
    674 
    675 			pt->pt_active = 1;
    676 		} else
    677 			pt->pt_active = 0;
    678 	}
    679 }
    680 
    681 void
    682 timer_gettime(struct ptimer *pt, struct itimerval *aitv)
    683 {
    684 	struct timeval now;
    685 	struct ptimer *ptn;
    686 
    687 	*aitv = pt->pt_time;
    688 	if (pt->pt_type == CLOCK_REALTIME) {
    689 		/*
    690 		 * Convert from absolute to relative time in .it_value
    691 		 * part of real time timer.  If time for real time
    692 		 * timer has passed return 0, else return difference
    693 		 * between current time and time for the timer to go
    694 		 * off.
    695 		 */
    696 		if (timerisset(&aitv->it_value)) {
    697 			getmicrotime(&now);
    698 			if (timercmp(&aitv->it_value, &now, <))
    699 				timerclear(&aitv->it_value);
    700 			else
    701 				timersub(&aitv->it_value, &now,
    702 				    &aitv->it_value);
    703 		}
    704 	} else if (pt->pt_active) {
    705 		if (pt->pt_type == CLOCK_VIRTUAL)
    706 			ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_virtual);
    707 		else
    708 			ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_prof);
    709 		for ( ; ptn && ptn != pt; ptn = LIST_NEXT(ptn, pt_list))
    710 			timeradd(&aitv->it_value,
    711 			    &ptn->pt_time.it_value, &aitv->it_value);
    712 		KASSERT(ptn != NULL); /* pt should be findable on the list */
    713 	} else
    714 		timerclear(&aitv->it_value);
    715 }
    716 
    717 
    718 
    719 /* Set and arm a POSIX realtime timer */
    720 int
    721 sys_timer_settime(struct lwp *l, const struct sys_timer_settime_args *uap,
    722     register_t *retval)
    723 {
    724 	/* {
    725 		syscallarg(timer_t) timerid;
    726 		syscallarg(int) flags;
    727 		syscallarg(const struct itimerspec *) value;
    728 		syscallarg(struct itimerspec *) ovalue;
    729 	} */
    730 	int error;
    731 	struct itimerspec value, ovalue, *ovp = NULL;
    732 
    733 	if ((error = copyin(SCARG(uap, value), &value,
    734 	    sizeof(struct itimerspec))) != 0)
    735 		return (error);
    736 
    737 	if (SCARG(uap, ovalue))
    738 		ovp = &ovalue;
    739 
    740 	if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
    741 	    SCARG(uap, flags), l->l_proc)) != 0)
    742 		return error;
    743 
    744 	if (ovp)
    745 		return copyout(&ovalue, SCARG(uap, ovalue),
    746 		    sizeof(struct itimerspec));
    747 	return 0;
    748 }
    749 
    750 int
    751 dotimer_settime(int timerid, struct itimerspec *value,
    752     struct itimerspec *ovalue, int flags, struct proc *p)
    753 {
    754 	struct timeval now;
    755 	struct itimerval val, oval;
    756 	struct ptimer *pt;
    757 	int s;
    758 
    759 	if ((p->p_timers == NULL) ||
    760 	    (timerid < 2) || (timerid >= TIMER_MAX) ||
    761 	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
    762 		return (EINVAL);
    763 
    764 	TIMESPEC_TO_TIMEVAL(&val.it_value, &value->it_value);
    765 	TIMESPEC_TO_TIMEVAL(&val.it_interval, &value->it_interval);
    766 	if (itimerfix(&val.it_value) || itimerfix(&val.it_interval))
    767 		return (EINVAL);
    768 
    769 	oval = pt->pt_time;
    770 	pt->pt_time = val;
    771 
    772 	s = splclock();
    773 	/*
    774 	 * If we've been passed a relative time for a realtime timer,
    775 	 * convert it to absolute; if an absolute time for a virtual
    776 	 * timer, convert it to relative and make sure we don't set it
    777 	 * to zero, which would cancel the timer, or let it go
    778 	 * negative, which would confuse the comparison tests.
    779 	 */
    780 	if (timerisset(&pt->pt_time.it_value)) {
    781 		if (pt->pt_type == CLOCK_REALTIME) {
    782 			if ((flags & TIMER_ABSTIME) == 0) {
    783 				getmicrotime(&now);
    784 				timeradd(&pt->pt_time.it_value, &now,
    785 				    &pt->pt_time.it_value);
    786 			}
    787 		} else {
    788 			if ((flags & TIMER_ABSTIME) != 0) {
    789 				getmicrotime(&now);
    790 				timersub(&pt->pt_time.it_value, &now,
    791 				    &pt->pt_time.it_value);
    792 				if (!timerisset(&pt->pt_time.it_value) ||
    793 				    pt->pt_time.it_value.tv_sec < 0) {
    794 					pt->pt_time.it_value.tv_sec = 0;
    795 					pt->pt_time.it_value.tv_usec = 1;
    796 				}
    797 			}
    798 		}
    799 	}
    800 
    801 	timer_settime(pt);
    802 	splx(s);
    803 
    804 	if (ovalue) {
    805 		TIMEVAL_TO_TIMESPEC(&oval.it_value, &ovalue->it_value);
    806 		TIMEVAL_TO_TIMESPEC(&oval.it_interval, &ovalue->it_interval);
    807 	}
    808 
    809 	return (0);
    810 }
    811 
    812 /* Return the time remaining until a POSIX timer fires. */
    813 int
    814 sys_timer_gettime(struct lwp *l, const struct sys_timer_gettime_args *uap,
    815     register_t *retval)
    816 {
    817 	/* {
    818 		syscallarg(timer_t) timerid;
    819 		syscallarg(struct itimerspec *) value;
    820 	} */
    821 	struct itimerspec its;
    822 	int error;
    823 
    824 	if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
    825 	    &its)) != 0)
    826 		return error;
    827 
    828 	return copyout(&its, SCARG(uap, value), sizeof(its));
    829 }
    830 
    831 int
    832 dotimer_gettime(int timerid, struct proc *p, struct itimerspec *its)
    833 {
    834 	int s;
    835 	struct ptimer *pt;
    836 	struct itimerval aitv;
    837 
    838 	if ((p->p_timers == NULL) ||
    839 	    (timerid < 2) || (timerid >= TIMER_MAX) ||
    840 	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
    841 		return (EINVAL);
    842 
    843 	s = splclock();
    844 	timer_gettime(pt, &aitv);
    845 	splx(s);
    846 
    847 	TIMEVAL_TO_TIMESPEC(&aitv.it_interval, &its->it_interval);
    848 	TIMEVAL_TO_TIMESPEC(&aitv.it_value, &its->it_value);
    849 
    850 	return 0;
    851 }
    852 
    853 /*
    854  * Return the count of the number of times a periodic timer expired
    855  * while a notification was already pending. The counter is reset when
    856  * a timer expires and a notification can be posted.
    857  */
    858 int
    859 sys_timer_getoverrun(struct lwp *l, const struct sys_timer_getoverrun_args *uap,
    860     register_t *retval)
    861 {
    862 	/* {
    863 		syscallarg(timer_t) timerid;
    864 	} */
    865 	struct proc *p = l->l_proc;
    866 	int timerid;
    867 	struct ptimer *pt;
    868 
    869 	timerid = SCARG(uap, timerid);
    870 
    871 	if ((p->p_timers == NULL) ||
    872 	    (timerid < 2) || (timerid >= TIMER_MAX) ||
    873 	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
    874 		return (EINVAL);
    875 
    876 	*retval = pt->pt_poverruns;
    877 
    878 	return (0);
    879 }
    880 
    881 /*
    882  * Real interval timer expired:
    883  * send process whose timer expired an alarm signal.
    884  * If time is not set up to reload, then just return.
    885  * Else compute next time timer should go off which is > current time.
    886  * This is where delay in processing this timeout causes multiple
    887  * SIGALRM calls to be compressed into one.
    888  */
    889 void
    890 realtimerexpire(void *arg)
    891 {
    892 	struct timeval now;
    893 	struct ptimer *pt;
    894 	int s;
    895 
    896 	pt = (struct ptimer *)arg;
    897 
    898 	itimerfire(pt);
    899 
    900 	if (!timerisset(&pt->pt_time.it_interval)) {
    901 		timerclear(&pt->pt_time.it_value);
    902 		return;
    903 	}
    904 	for (;;) {
    905 		s = splclock();	/* XXX need spl now? */
    906 		timeradd(&pt->pt_time.it_value,
    907 		    &pt->pt_time.it_interval, &pt->pt_time.it_value);
    908 		getmicrotime(&now);
    909 		if (timercmp(&pt->pt_time.it_value, &now, >)) {
    910 			/*
    911 			 * Don't need to check hzto() return value, here.
    912 			 * callout_reset() does it for us.
    913 			 */
    914 			callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
    915 			    realtimerexpire, pt);
    916 			splx(s);
    917 			return;
    918 		}
    919 		splx(s);
    920 		pt->pt_overruns++;
    921 	}
    922 }
    923 
    924 /* BSD routine to get the value of an interval timer. */
    925 /* ARGSUSED */
    926 int
    927 sys_getitimer(struct lwp *l, const struct sys_getitimer_args *uap,
    928     register_t *retval)
    929 {
    930 	/* {
    931 		syscallarg(int) which;
    932 		syscallarg(struct itimerval *) itv;
    933 	} */
    934 	struct proc *p = l->l_proc;
    935 	struct itimerval aitv;
    936 	int error;
    937 
    938 	error = dogetitimer(p, SCARG(uap, which), &aitv);
    939 	if (error)
    940 		return error;
    941 	return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
    942 }
    943 
    944 int
    945 dogetitimer(struct proc *p, int which, struct itimerval *itvp)
    946 {
    947 	int s;
    948 
    949 	if ((u_int)which > ITIMER_PROF)
    950 		return (EINVAL);
    951 
    952 	if ((p->p_timers == NULL) || (p->p_timers->pts_timers[which] == NULL)){
    953 		timerclear(&itvp->it_value);
    954 		timerclear(&itvp->it_interval);
    955 	} else {
    956 		s = splclock();
    957 		timer_gettime(p->p_timers->pts_timers[which], itvp);
    958 		splx(s);
    959 	}
    960 
    961 	return 0;
    962 }
    963 
    964 /* BSD routine to set/arm an interval timer. */
    965 /* ARGSUSED */
    966 int
    967 sys_setitimer(struct lwp *l, const struct sys_setitimer_args *uap,
    968     register_t *retval)
    969 {
    970 	/* {
    971 		syscallarg(int) which;
    972 		syscallarg(const struct itimerval *) itv;
    973 		syscallarg(struct itimerval *) oitv;
    974 	} */
    975 	struct proc *p = l->l_proc;
    976 	int which = SCARG(uap, which);
    977 	struct sys_getitimer_args getargs;
    978 	const struct itimerval *itvp;
    979 	struct itimerval aitv;
    980 	int error;
    981 
    982 	if ((u_int)which > ITIMER_PROF)
    983 		return (EINVAL);
    984 	itvp = SCARG(uap, itv);
    985 	if (itvp &&
    986 	    (error = copyin(itvp, &aitv, sizeof(struct itimerval)) != 0))
    987 		return (error);
    988 	if (SCARG(uap, oitv) != NULL) {
    989 		SCARG(&getargs, which) = which;
    990 		SCARG(&getargs, itv) = SCARG(uap, oitv);
    991 		if ((error = sys_getitimer(l, &getargs, retval)) != 0)
    992 			return (error);
    993 	}
    994 	if (itvp == 0)
    995 		return (0);
    996 
    997 	return dosetitimer(p, which, &aitv);
    998 }
    999 
   1000 int
   1001 dosetitimer(struct proc *p, int which, struct itimerval *itvp)
   1002 {
   1003 	struct timeval now;
   1004 	struct ptimer *pt;
   1005 	int s;
   1006 
   1007 	if (itimerfix(&itvp->it_value) || itimerfix(&itvp->it_interval))
   1008 		return (EINVAL);
   1009 
   1010 	/*
   1011 	 * Don't bother allocating data structures if the process just
   1012 	 * wants to clear the timer.
   1013 	 */
   1014 	if (!timerisset(&itvp->it_value) &&
   1015 	    ((p->p_timers == NULL) ||(p->p_timers->pts_timers[which] == NULL)))
   1016 		return (0);
   1017 
   1018 	if (p->p_timers == NULL)
   1019 		timers_alloc(p);
   1020 	if (p->p_timers->pts_timers[which] == NULL) {
   1021 		pt = pool_get(&ptimer_pool, PR_WAITOK);
   1022 		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
   1023 		pt->pt_ev.sigev_value.sival_int = which;
   1024 		pt->pt_overruns = 0;
   1025 		pt->pt_proc = p;
   1026 		pt->pt_type = which;
   1027 		pt->pt_entry = which;
   1028 		switch (which) {
   1029 		case ITIMER_REAL:
   1030 			callout_init(&pt->pt_ch, 0);
   1031 			pt->pt_ev.sigev_signo = SIGALRM;
   1032 			break;
   1033 		case ITIMER_VIRTUAL:
   1034 			pt->pt_active = 0;
   1035 			pt->pt_ev.sigev_signo = SIGVTALRM;
   1036 			break;
   1037 		case ITIMER_PROF:
   1038 			pt->pt_active = 0;
   1039 			pt->pt_ev.sigev_signo = SIGPROF;
   1040 			break;
   1041 		}
   1042 	} else
   1043 		pt = p->p_timers->pts_timers[which];
   1044 
   1045 	pt->pt_time = *itvp;
   1046 	p->p_timers->pts_timers[which] = pt;
   1047 
   1048 	s = splclock();
   1049 	if ((which == ITIMER_REAL) && timerisset(&pt->pt_time.it_value)) {
   1050 		/* Convert to absolute time */
   1051 		/* XXX need to wrap in splclock for timecounters case? */
   1052 		getmicrotime(&now);
   1053 		timeradd(&pt->pt_time.it_value, &now, &pt->pt_time.it_value);
   1054 	}
   1055 	timer_settime(pt);
   1056 	splx(s);
   1057 
   1058 	return (0);
   1059 }
   1060 
   1061 /* Utility routines to manage the array of pointers to timers. */
   1062 void
   1063 timers_alloc(struct proc *p)
   1064 {
   1065 	int i;
   1066 	struct ptimers *pts;
   1067 
   1068 	pts = pool_get(&ptimers_pool, PR_WAITOK);
   1069 	LIST_INIT(&pts->pts_virtual);
   1070 	LIST_INIT(&pts->pts_prof);
   1071 	for (i = 0; i < TIMER_MAX; i++)
   1072 		pts->pts_timers[i] = NULL;
   1073 	pts->pts_fired = 0;
   1074 	p->p_timers = pts;
   1075 }
   1076 
   1077 /*
   1078  * Clean up the per-process timers. If "which" is set to TIMERS_ALL,
   1079  * then clean up all timers and free all the data structures. If
   1080  * "which" is set to TIMERS_POSIX, only clean up the timers allocated
   1081  * by timer_create(), not the BSD setitimer() timers, and only free the
   1082  * structure if none of those remain.
   1083  */
   1084 void
   1085 timers_free(struct proc *p, int which)
   1086 {
   1087 	int i, s;
   1088 	struct ptimers *pts;
   1089 	struct ptimer *pt, *ptn;
   1090 	struct timeval tv;
   1091 
   1092 	if (p->p_timers) {
   1093 		pts = p->p_timers;
   1094 		if (which == TIMERS_ALL)
   1095 			i = 0;
   1096 		else {
   1097 			s = splclock();
   1098 			timerclear(&tv);
   1099 			for (ptn = LIST_FIRST(&p->p_timers->pts_virtual);
   1100 			     ptn && ptn != pts->pts_timers[ITIMER_VIRTUAL];
   1101 			     ptn = LIST_NEXT(ptn, pt_list))
   1102 				timeradd(&tv, &ptn->pt_time.it_value, &tv);
   1103 			LIST_FIRST(&p->p_timers->pts_virtual) = NULL;
   1104 			if (ptn) {
   1105 				timeradd(&tv, &ptn->pt_time.it_value,
   1106 				    &ptn->pt_time.it_value);
   1107 				LIST_INSERT_HEAD(&p->p_timers->pts_virtual,
   1108 				    ptn, pt_list);
   1109 			}
   1110 
   1111 			timerclear(&tv);
   1112 			for (ptn = LIST_FIRST(&p->p_timers->pts_prof);
   1113 			     ptn && ptn != pts->pts_timers[ITIMER_PROF];
   1114 			     ptn = LIST_NEXT(ptn, pt_list))
   1115 				timeradd(&tv, &ptn->pt_time.it_value, &tv);
   1116 			LIST_FIRST(&p->p_timers->pts_prof) = NULL;
   1117 			if (ptn) {
   1118 				timeradd(&tv, &ptn->pt_time.it_value,
   1119 				    &ptn->pt_time.it_value);
   1120 				LIST_INSERT_HEAD(&p->p_timers->pts_prof, ptn,
   1121 				    pt_list);
   1122 			}
   1123 			splx(s);
   1124 			i = 3;
   1125 		}
   1126 		for ( ; i < TIMER_MAX; i++)
   1127 			if ((pt = pts->pts_timers[i]) != NULL) {
   1128 				if (pt->pt_type == CLOCK_REALTIME) {
   1129 					callout_stop(&pt->pt_ch);
   1130 					callout_destroy(&pt->pt_ch);
   1131 				}
   1132 				pts->pts_timers[i] = NULL;
   1133 				pool_put(&ptimer_pool, pt);
   1134 			}
   1135 		if ((pts->pts_timers[0] == NULL) &&
   1136 		    (pts->pts_timers[1] == NULL) &&
   1137 		    (pts->pts_timers[2] == NULL)) {
   1138 			p->p_timers = NULL;
   1139 			pool_put(&ptimers_pool, pts);
   1140 		}
   1141 	}
   1142 }
   1143 
   1144 /*
   1145  * Decrement an interval timer by a specified number
   1146  * of microseconds, which must be less than a second,
   1147  * i.e. < 1000000.  If the timer expires, then reload
   1148  * it.  In this case, carry over (usec - old value) to
   1149  * reduce the value reloaded into the timer so that
   1150  * the timer does not drift.  This routine assumes
   1151  * that it is called in a context where the timers
   1152  * on which it is operating cannot change in value.
   1153  */
   1154 int
   1155 itimerdecr(struct ptimer *pt, int usec)
   1156 {
   1157 	struct itimerval *itp;
   1158 
   1159 	itp = &pt->pt_time;
   1160 	if (itp->it_value.tv_usec < usec) {
   1161 		if (itp->it_value.tv_sec == 0) {
   1162 			/* expired, and already in next interval */
   1163 			usec -= itp->it_value.tv_usec;
   1164 			goto expire;
   1165 		}
   1166 		itp->it_value.tv_usec += 1000000;
   1167 		itp->it_value.tv_sec--;
   1168 	}
   1169 	itp->it_value.tv_usec -= usec;
   1170 	usec = 0;
   1171 	if (timerisset(&itp->it_value))
   1172 		return (1);
   1173 	/* expired, exactly at end of interval */
   1174 expire:
   1175 	if (timerisset(&itp->it_interval)) {
   1176 		itp->it_value = itp->it_interval;
   1177 		itp->it_value.tv_usec -= usec;
   1178 		if (itp->it_value.tv_usec < 0) {
   1179 			itp->it_value.tv_usec += 1000000;
   1180 			itp->it_value.tv_sec--;
   1181 		}
   1182 		timer_settime(pt);
   1183 	} else
   1184 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
   1185 	return (0);
   1186 }
   1187 
   1188 void
   1189 itimerfire(struct ptimer *pt)
   1190 {
   1191 	struct proc *p = pt->pt_proc;
   1192 
   1193 	if (pt->pt_ev.sigev_notify == SIGEV_SIGNAL) {
   1194 		/*
   1195 		 * No RT signal infrastructure exists at this time;
   1196 		 * just post the signal number and throw away the
   1197 		 * value.
   1198 		 */
   1199 		if (sigismember(&p->p_sigpend.sp_set, pt->pt_ev.sigev_signo))
   1200 			pt->pt_overruns++;
   1201 		else {
   1202 			ksiginfo_t ksi;
   1203 			KSI_INIT(&ksi);
   1204 			ksi.ksi_signo = pt->pt_ev.sigev_signo;
   1205 			ksi.ksi_code = SI_TIMER;
   1206 			ksi.ksi_value = pt->pt_ev.sigev_value;
   1207 			pt->pt_poverruns = pt->pt_overruns;
   1208 			pt->pt_overruns = 0;
   1209 			mutex_enter(&proclist_mutex);
   1210 			kpsignal(p, &ksi, NULL);
   1211 			mutex_exit(&proclist_mutex);
   1212 		}
   1213 	}
   1214 }
   1215 
   1216 /*
   1217  * ratecheck(): simple time-based rate-limit checking.  see ratecheck(9)
   1218  * for usage and rationale.
   1219  */
   1220 int
   1221 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
   1222 {
   1223 	struct timeval tv, delta;
   1224 	int rv = 0;
   1225 
   1226 	getmicrouptime(&tv);
   1227 	timersub(&tv, lasttime, &delta);
   1228 
   1229 	/*
   1230 	 * check for 0,0 is so that the message will be seen at least once,
   1231 	 * even if interval is huge.
   1232 	 */
   1233 	if (timercmp(&delta, mininterval, >=) ||
   1234 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
   1235 		*lasttime = tv;
   1236 		rv = 1;
   1237 	}
   1238 
   1239 	return (rv);
   1240 }
   1241 
   1242 /*
   1243  * ppsratecheck(): packets (or events) per second limitation.
   1244  */
   1245 int
   1246 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
   1247 {
   1248 	struct timeval tv, delta;
   1249 	int rv;
   1250 
   1251 	getmicrouptime(&tv);
   1252 	timersub(&tv, lasttime, &delta);
   1253 
   1254 	/*
   1255 	 * check for 0,0 is so that the message will be seen at least once.
   1256 	 * if more than one second have passed since the last update of
   1257 	 * lasttime, reset the counter.
   1258 	 *
   1259 	 * we do increment *curpps even in *curpps < maxpps case, as some may
   1260 	 * try to use *curpps for stat purposes as well.
   1261 	 */
   1262 	if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
   1263 	    delta.tv_sec >= 1) {
   1264 		*lasttime = tv;
   1265 		*curpps = 0;
   1266 	}
   1267 	if (maxpps < 0)
   1268 		rv = 1;
   1269 	else if (*curpps < maxpps)
   1270 		rv = 1;
   1271 	else
   1272 		rv = 0;
   1273 
   1274 #if 1 /*DIAGNOSTIC?*/
   1275 	/* be careful about wrap-around */
   1276 	if (*curpps + 1 > *curpps)
   1277 		*curpps = *curpps + 1;
   1278 #else
   1279 	/*
   1280 	 * assume that there's not too many calls to this function.
   1281 	 * not sure if the assumption holds, as it depends on *caller's*
   1282 	 * behavior, not the behavior of this function.
   1283 	 * IMHO it is wrong to make assumption on the caller's behavior,
   1284 	 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
   1285 	 */
   1286 	*curpps = *curpps + 1;
   1287 #endif
   1288 
   1289 	return (rv);
   1290 }
   1291