Home | History | Annotate | Line # | Download | only in kern
kern_time.c revision 1.91
      1 /*	$NetBSD: kern_time.c,v 1.91 2005/07/11 19:50:42 cube 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.91 2005/07/11 19:50:42 cube 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_var.h>
     98 #endif
     99 
    100 #include <machine/cpu.h>
    101 
    102 static void timerupcall(struct lwp *, void *);
    103 
    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 */
    115 int
    116 settime(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 */
    156 int
    157 sys_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 */
    189 int
    190 sys_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 
    206 int
    207 clock_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 
    231 int
    232 sys_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 */
    260 int
    261 sys_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 */
    319 int
    320 sys_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 */
    349 int
    350 sys_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 
    365 int
    366 settimeofday1(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 
    401 int	tickdelta;			/* current clock skew, us. per tick */
    402 long	timedelta;			/* unapplied time correction, us. */
    403 long	bigadj = 1000000;		/* use 10x skew above bigadj us. */
    404 int	time_adjusted;			/* set if an adjustment is made */
    405 
    406 /* ARGSUSED */
    407 int
    408 sys_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 
    423 int
    424 adjtime1(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. */
    498 int
    499 sys_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 	struct proc *p = l->l_proc;
    507 	clockid_t id;
    508 	struct sigevent *evp;
    509 	struct ptimer *pt;
    510 	timer_t timerid;
    511 	int error;
    512 
    513 	id = SCARG(uap, clock_id);
    514 	if (id < CLOCK_REALTIME ||
    515 	    id > CLOCK_PROF)
    516 		return (EINVAL);
    517 
    518 	if (p->p_timers == NULL)
    519 		timers_alloc(p);
    520 
    521 	/* Find a free timer slot, skipping those reserved for setitimer(). */
    522 	for (timerid = 3; timerid < TIMER_MAX; timerid++)
    523 		if (p->p_timers->pts_timers[timerid] == NULL)
    524 			break;
    525 
    526 	if (timerid == TIMER_MAX)
    527 		return EAGAIN;
    528 
    529 	pt = pool_get(&ptimer_pool, PR_WAITOK);
    530 	evp = SCARG(uap, evp);
    531 	if (evp) {
    532 		if (((error =
    533 		    copyin(evp, &pt->pt_ev, sizeof (pt->pt_ev))) != 0) ||
    534 		    ((pt->pt_ev.sigev_notify < SIGEV_NONE) ||
    535 			(pt->pt_ev.sigev_notify > SIGEV_SA))) {
    536 			pool_put(&ptimer_pool, pt);
    537 			return (error ? error : EINVAL);
    538 		}
    539 	} else {
    540 		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
    541 		switch (id) {
    542 		case CLOCK_REALTIME:
    543 			pt->pt_ev.sigev_signo = SIGALRM;
    544 			break;
    545 		case CLOCK_VIRTUAL:
    546 			pt->pt_ev.sigev_signo = SIGVTALRM;
    547 			break;
    548 		case CLOCK_PROF:
    549 			pt->pt_ev.sigev_signo = SIGPROF;
    550 			break;
    551 		}
    552 		pt->pt_ev.sigev_value.sival_int = timerid;
    553 	}
    554 	pt->pt_info.ksi_signo = pt->pt_ev.sigev_signo;
    555 	pt->pt_info.ksi_errno = 0;
    556 	pt->pt_info.ksi_code = 0;
    557 	pt->pt_info.ksi_pid = p->p_pid;
    558 	pt->pt_info.ksi_uid = p->p_cred->p_ruid;
    559 	pt->pt_info.ksi_sigval = pt->pt_ev.sigev_value;
    560 
    561 	pt->pt_type = id;
    562 	pt->pt_proc = p;
    563 	pt->pt_overruns = 0;
    564 	pt->pt_poverruns = 0;
    565 	pt->pt_entry = timerid;
    566 	timerclear(&pt->pt_time.it_value);
    567 	if (id == CLOCK_REALTIME)
    568 		callout_init(&pt->pt_ch);
    569 	else
    570 		pt->pt_active = 0;
    571 
    572 	p->p_timers->pts_timers[timerid] = pt;
    573 
    574 	return copyout(&timerid, SCARG(uap, timerid), sizeof(timerid));
    575 }
    576 
    577 
    578 /* Delete a POSIX realtime timer */
    579 int
    580 sys_timer_delete(struct lwp *l, void *v, register_t *retval)
    581 {
    582 	struct sys_timer_delete_args /*  {
    583 		syscallarg(timer_t) timerid;
    584 	} */ *uap = v;
    585 	struct proc *p = l->l_proc;
    586 	timer_t timerid;
    587 	struct ptimer *pt, *ptn;
    588 	int s;
    589 
    590 	timerid = SCARG(uap, timerid);
    591 
    592 	if ((p->p_timers == NULL) ||
    593 	    (timerid < 2) || (timerid >= TIMER_MAX) ||
    594 	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
    595 		return (EINVAL);
    596 
    597 	if (pt->pt_type == CLOCK_REALTIME)
    598 		callout_stop(&pt->pt_ch);
    599 	else if (pt->pt_active) {
    600 		s = splclock();
    601 		ptn = LIST_NEXT(pt, pt_list);
    602 		LIST_REMOVE(pt, pt_list);
    603 		for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
    604 			timeradd(&pt->pt_time.it_value, &ptn->pt_time.it_value,
    605 			    &ptn->pt_time.it_value);
    606 		splx(s);
    607 	}
    608 
    609 	p->p_timers->pts_timers[timerid] = NULL;
    610 	pool_put(&ptimer_pool, pt);
    611 
    612 	return (0);
    613 }
    614 
    615 /*
    616  * Set up the given timer. The value in pt->pt_time.it_value is taken
    617  * to be an absolute time for CLOCK_REALTIME timers and a relative
    618  * time for virtual timers.
    619  * Must be called at splclock().
    620  */
    621 void
    622 timer_settime(struct ptimer *pt)
    623 {
    624 	struct ptimer *ptn, *pptn;
    625 	struct ptlist *ptl;
    626 
    627 	if (pt->pt_type == CLOCK_REALTIME) {
    628 		callout_stop(&pt->pt_ch);
    629 		if (timerisset(&pt->pt_time.it_value)) {
    630 			/*
    631 			 * Don't need to check hzto() return value, here.
    632 			 * callout_reset() does it for us.
    633 			 */
    634 			callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
    635 			    realtimerexpire, pt);
    636 		}
    637 	} else {
    638 		if (pt->pt_active) {
    639 			ptn = LIST_NEXT(pt, pt_list);
    640 			LIST_REMOVE(pt, pt_list);
    641 			for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
    642 				timeradd(&pt->pt_time.it_value,
    643 				    &ptn->pt_time.it_value,
    644 				    &ptn->pt_time.it_value);
    645 		}
    646 		if (timerisset(&pt->pt_time.it_value)) {
    647 			if (pt->pt_type == CLOCK_VIRTUAL)
    648 				ptl = &pt->pt_proc->p_timers->pts_virtual;
    649 			else
    650 				ptl = &pt->pt_proc->p_timers->pts_prof;
    651 
    652 			for (ptn = LIST_FIRST(ptl), pptn = NULL;
    653 			     ptn && timercmp(&pt->pt_time.it_value,
    654 				 &ptn->pt_time.it_value, >);
    655 			     pptn = ptn, ptn = LIST_NEXT(ptn, pt_list))
    656 				timersub(&pt->pt_time.it_value,
    657 				    &ptn->pt_time.it_value,
    658 				    &pt->pt_time.it_value);
    659 
    660 			if (pptn)
    661 				LIST_INSERT_AFTER(pptn, pt, pt_list);
    662 			else
    663 				LIST_INSERT_HEAD(ptl, pt, pt_list);
    664 
    665 			for ( ; ptn ; ptn = LIST_NEXT(ptn, pt_list))
    666 				timersub(&ptn->pt_time.it_value,
    667 				    &pt->pt_time.it_value,
    668 				    &ptn->pt_time.it_value);
    669 
    670 			pt->pt_active = 1;
    671 		} else
    672 			pt->pt_active = 0;
    673 	}
    674 }
    675 
    676 void
    677 timer_gettime(struct ptimer *pt, struct itimerval *aitv)
    678 {
    679 	struct ptimer *ptn;
    680 
    681 	*aitv = pt->pt_time;
    682 	if (pt->pt_type == CLOCK_REALTIME) {
    683 		/*
    684 		 * Convert from absolute to relative time in .it_value
    685 		 * part of real time timer.  If time for real time
    686 		 * timer has passed return 0, else return difference
    687 		 * between current time and time for the timer to go
    688 		 * off.
    689 		 */
    690 		if (timerisset(&aitv->it_value)) {
    691 			if (timercmp(&aitv->it_value, &time, <))
    692 				timerclear(&aitv->it_value);
    693 			else
    694 				timersub(&aitv->it_value, &time,
    695 				    &aitv->it_value);
    696 		}
    697 	} else if (pt->pt_active) {
    698 		if (pt->pt_type == CLOCK_VIRTUAL)
    699 			ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_virtual);
    700 		else
    701 			ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_prof);
    702 		for ( ; ptn && ptn != pt; ptn = LIST_NEXT(ptn, pt_list))
    703 			timeradd(&aitv->it_value,
    704 			    &ptn->pt_time.it_value, &aitv->it_value);
    705 		KASSERT(ptn != NULL); /* pt should be findable on the list */
    706 	} else
    707 		timerclear(&aitv->it_value);
    708 }
    709 
    710 
    711 
    712 /* Set and arm a POSIX realtime timer */
    713 int
    714 sys_timer_settime(struct lwp *l, void *v, register_t *retval)
    715 {
    716 	struct sys_timer_settime_args /* {
    717 		syscallarg(timer_t) timerid;
    718 		syscallarg(int) flags;
    719 		syscallarg(const struct itimerspec *) value;
    720 		syscallarg(struct itimerspec *) ovalue;
    721 	} */ *uap = v;
    722 	struct proc *p = l->l_proc;
    723 	int error, s, timerid;
    724 	struct itimerval val, oval;
    725 	struct itimerspec value, ovalue;
    726 	struct ptimer *pt;
    727 
    728 	timerid = SCARG(uap, timerid);
    729 
    730 	if ((p->p_timers == NULL) ||
    731 	    (timerid < 2) || (timerid >= TIMER_MAX) ||
    732 	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
    733 		return (EINVAL);
    734 
    735 	if ((error = copyin(SCARG(uap, value), &value,
    736 	    sizeof(struct itimerspec))) != 0)
    737 		return (error);
    738 
    739 	TIMESPEC_TO_TIMEVAL(&val.it_value, &value.it_value);
    740 	TIMESPEC_TO_TIMEVAL(&val.it_interval, &value.it_interval);
    741 	if (itimerfix(&val.it_value) || itimerfix(&val.it_interval))
    742 		return (EINVAL);
    743 
    744 	oval = pt->pt_time;
    745 	pt->pt_time = val;
    746 
    747 	s = splclock();
    748 	/*
    749 	 * If we've been passed a relative time for a realtime timer,
    750 	 * convert it to absolute; if an absolute time for a virtual
    751 	 * timer, convert it to relative and make sure we don't set it
    752 	 * to zero, which would cancel the timer, or let it go
    753 	 * negative, which would confuse the comparison tests.
    754 	 */
    755 	if (timerisset(&pt->pt_time.it_value)) {
    756 		if (pt->pt_type == CLOCK_REALTIME) {
    757 			if ((SCARG(uap, flags) & TIMER_ABSTIME) == 0)
    758 				timeradd(&pt->pt_time.it_value, &time,
    759 				    &pt->pt_time.it_value);
    760 		} else {
    761 			if ((SCARG(uap, flags) & TIMER_ABSTIME) != 0) {
    762 				timersub(&pt->pt_time.it_value, &time,
    763 				    &pt->pt_time.it_value);
    764 				if (!timerisset(&pt->pt_time.it_value) ||
    765 				    pt->pt_time.it_value.tv_sec < 0) {
    766 					pt->pt_time.it_value.tv_sec = 0;
    767 					pt->pt_time.it_value.tv_usec = 1;
    768 				}
    769 			}
    770 		}
    771 	}
    772 
    773 	timer_settime(pt);
    774 	splx(s);
    775 
    776 	if (SCARG(uap, ovalue)) {
    777 		TIMEVAL_TO_TIMESPEC(&oval.it_value, &ovalue.it_value);
    778 		TIMEVAL_TO_TIMESPEC(&oval.it_interval, &ovalue.it_interval);
    779 		return copyout(&ovalue, SCARG(uap, ovalue),
    780 		    sizeof(struct itimerspec));
    781 	}
    782 
    783 	return (0);
    784 }
    785 
    786 /* Return the time remaining until a POSIX timer fires. */
    787 int
    788 sys_timer_gettime(struct lwp *l, void *v, register_t *retval)
    789 {
    790 	struct sys_timer_gettime_args /* {
    791 		syscallarg(timer_t) timerid;
    792 		syscallarg(struct itimerspec *) value;
    793 	} */ *uap = v;
    794 	struct itimerval aitv;
    795 	struct itimerspec its;
    796 	struct proc *p = l->l_proc;
    797 	int s, timerid;
    798 	struct ptimer *pt;
    799 
    800 	timerid = SCARG(uap, timerid);
    801 
    802 	if ((p->p_timers == NULL) ||
    803 	    (timerid < 2) || (timerid >= TIMER_MAX) ||
    804 	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
    805 		return (EINVAL);
    806 
    807 	s = splclock();
    808 	timer_gettime(pt, &aitv);
    809 	splx(s);
    810 
    811 	TIMEVAL_TO_TIMESPEC(&aitv.it_interval, &its.it_interval);
    812 	TIMEVAL_TO_TIMESPEC(&aitv.it_value, &its.it_value);
    813 
    814 	return copyout(&its, SCARG(uap, value), sizeof(its));
    815 }
    816 
    817 /*
    818  * Return the count of the number of times a periodic timer expired
    819  * while a notification was already pending. The counter is reset when
    820  * a timer expires and a notification can be posted.
    821  */
    822 int
    823 sys_timer_getoverrun(struct lwp *l, void *v, register_t *retval)
    824 {
    825 	struct sys_timer_getoverrun_args /* {
    826 		syscallarg(timer_t) timerid;
    827 	} */ *uap = v;
    828 	struct proc *p = l->l_proc;
    829 	int timerid;
    830 	struct ptimer *pt;
    831 
    832 	timerid = SCARG(uap, timerid);
    833 
    834 	if ((p->p_timers == NULL) ||
    835 	    (timerid < 2) || (timerid >= TIMER_MAX) ||
    836 	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
    837 		return (EINVAL);
    838 
    839 	*retval = pt->pt_poverruns;
    840 
    841 	return (0);
    842 }
    843 
    844 /* Glue function that triggers an upcall; called from userret(). */
    845 static void
    846 timerupcall(struct lwp *l, void *arg)
    847 {
    848 	struct ptimers *pt = (struct ptimers *)arg;
    849 	unsigned int i, fired, done;
    850 	extern struct pool siginfo_pool;	/* XXX Ew. */
    851 
    852 	KDASSERT(l->l_proc->p_sa);
    853 	/* Bail out if we do not own the virtual processor */
    854 	if (l->l_savp->savp_lwp != l)
    855 		return ;
    856 
    857 	KERNEL_PROC_LOCK(l);
    858 
    859 	fired = pt->pts_fired;
    860 	done = 0;
    861 	while ((i = ffs(fired)) != 0) {
    862 		siginfo_t *si;
    863 		int mask = 1 << --i;
    864 		int f;
    865 
    866 		f = l->l_flag & L_SA;
    867 		l->l_flag &= ~L_SA;
    868 		si = pool_get(&siginfo_pool, PR_WAITOK);
    869 		si->_info = pt->pts_timers[i]->pt_info.ksi_info;
    870 		if (sa_upcall(l, SA_UPCALL_SIGEV | SA_UPCALL_DEFER, NULL, l,
    871 		    sizeof(*si), si) != 0) {
    872 			pool_put(&siginfo_pool, si);
    873 			/* XXX What do we do here?? */
    874 		} else
    875 			done |= mask;
    876 		fired &= ~mask;
    877 		l->l_flag |= f;
    878 	}
    879 	pt->pts_fired &= ~done;
    880 	if (pt->pts_fired == 0)
    881 		l->l_proc->p_userret = NULL;
    882 
    883 	KERNEL_PROC_UNLOCK(l);
    884 }
    885 
    886 
    887 /*
    888  * Real interval timer expired:
    889  * send process whose timer expired an alarm signal.
    890  * If time is not set up to reload, then just return.
    891  * Else compute next time timer should go off which is > current time.
    892  * This is where delay in processing this timeout causes multiple
    893  * SIGALRM calls to be compressed into one.
    894  */
    895 void
    896 realtimerexpire(void *arg)
    897 {
    898 	struct ptimer *pt;
    899 	int s;
    900 
    901 	pt = (struct ptimer *)arg;
    902 
    903 	itimerfire(pt);
    904 
    905 	if (!timerisset(&pt->pt_time.it_interval)) {
    906 		timerclear(&pt->pt_time.it_value);
    907 		return;
    908 	}
    909 	for (;;) {
    910 		s = splclock();
    911 		timeradd(&pt->pt_time.it_value,
    912 		    &pt->pt_time.it_interval, &pt->pt_time.it_value);
    913 		if (timercmp(&pt->pt_time.it_value, &time, >)) {
    914 			/*
    915 			 * Don't need to check hzto() return value, here.
    916 			 * callout_reset() does it for us.
    917 			 */
    918 			callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
    919 			    realtimerexpire, pt);
    920 			splx(s);
    921 			return;
    922 		}
    923 		splx(s);
    924 		pt->pt_overruns++;
    925 	}
    926 }
    927 
    928 /* BSD routine to get the value of an interval timer. */
    929 /* ARGSUSED */
    930 int
    931 sys_getitimer(struct lwp *l, void *v, register_t *retval)
    932 {
    933 	struct sys_getitimer_args /* {
    934 		syscallarg(int) which;
    935 		syscallarg(struct itimerval *) itv;
    936 	} */ *uap = v;
    937 	struct proc *p = l->l_proc;
    938 	struct itimerval aitv;
    939 	int error;
    940 
    941 	error = dogetitimer(p, SCARG(uap, which), &aitv);
    942 	if (error)
    943 		return error;
    944 	return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
    945 }
    946 
    947 int
    948 dogetitimer(struct proc *p, int which, struct itimerval *itvp)
    949 {
    950 	int s;
    951 
    952 	if ((u_int)which > ITIMER_PROF)
    953 		return (EINVAL);
    954 
    955 	if ((p->p_timers == NULL) || (p->p_timers->pts_timers[which] == NULL)){
    956 		timerclear(&itvp->it_value);
    957 		timerclear(&itvp->it_interval);
    958 	} else {
    959 		s = splclock();
    960 		timer_gettime(p->p_timers->pts_timers[which], itvp);
    961 		splx(s);
    962 	}
    963 
    964 	return 0;
    965 }
    966 
    967 /* BSD routine to set/arm an interval timer. */
    968 /* ARGSUSED */
    969 int
    970 sys_setitimer(struct lwp *l, void *v, register_t *retval)
    971 {
    972 	struct sys_setitimer_args /* {
    973 		syscallarg(int) which;
    974 		syscallarg(const struct itimerval *) itv;
    975 		syscallarg(struct itimerval *) oitv;
    976 	} */ *uap = v;
    977 	struct proc *p = l->l_proc;
    978 	int which = SCARG(uap, which);
    979 	struct sys_getitimer_args getargs;
    980 	const struct itimerval *itvp;
    981 	struct itimerval aitv;
    982 	int error;
    983 
    984 	if ((u_int)which > ITIMER_PROF)
    985 		return (EINVAL);
    986 	itvp = SCARG(uap, itv);
    987 	if (itvp &&
    988 	    (error = copyin(itvp, &aitv, sizeof(struct itimerval)) != 0))
    989 		return (error);
    990 	if (SCARG(uap, oitv) != NULL) {
    991 		SCARG(&getargs, which) = which;
    992 		SCARG(&getargs, itv) = SCARG(uap, oitv);
    993 		if ((error = sys_getitimer(l, &getargs, retval)) != 0)
    994 			return (error);
    995 	}
    996 	if (itvp == 0)
    997 		return (0);
    998 
    999 	return dosetitimer(p, which, &aitv);
   1000 }
   1001 
   1002 int
   1003 dosetitimer(struct proc *p, int which, struct itimerval *itvp)
   1004 {
   1005 	struct ptimer *pt;
   1006 	int s;
   1007 
   1008 	if (itimerfix(&itvp->it_value) || itimerfix(&itvp->it_interval))
   1009 		return (EINVAL);
   1010 
   1011 	/*
   1012 	 * Don't bother allocating data structures if the process just
   1013 	 * wants to clear the timer.
   1014 	 */
   1015 	if (!timerisset(&itvp->it_value) &&
   1016 	    ((p->p_timers == NULL) ||(p->p_timers->pts_timers[which] == NULL)))
   1017 		return (0);
   1018 
   1019 	if (p->p_timers == NULL)
   1020 		timers_alloc(p);
   1021 	if (p->p_timers->pts_timers[which] == NULL) {
   1022 		pt = pool_get(&ptimer_pool, PR_WAITOK);
   1023 		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
   1024 		pt->pt_ev.sigev_value.sival_int = which;
   1025 		pt->pt_overruns = 0;
   1026 		pt->pt_proc = p;
   1027 		pt->pt_type = which;
   1028 		pt->pt_entry = which;
   1029 		switch (which) {
   1030 		case ITIMER_REAL:
   1031 			callout_init(&pt->pt_ch);
   1032 			pt->pt_ev.sigev_signo = SIGALRM;
   1033 			break;
   1034 		case ITIMER_VIRTUAL:
   1035 			pt->pt_active = 0;
   1036 			pt->pt_ev.sigev_signo = SIGVTALRM;
   1037 			break;
   1038 		case ITIMER_PROF:
   1039 			pt->pt_active = 0;
   1040 			pt->pt_ev.sigev_signo = SIGPROF;
   1041 			break;
   1042 		}
   1043 	} else
   1044 		pt = p->p_timers->pts_timers[which];
   1045 
   1046 	pt->pt_time = *itvp;
   1047 	p->p_timers->pts_timers[which] = pt;
   1048 
   1049 	s = splclock();
   1050 	if ((which == ITIMER_REAL) && timerisset(&pt->pt_time.it_value)) {
   1051 		/* Convert to absolute time */
   1052 		timeradd(&pt->pt_time.it_value, &time, &pt->pt_time.it_value);
   1053 	}
   1054 	timer_settime(pt);
   1055 	splx(s);
   1056 
   1057 	return (0);
   1058 }
   1059 
   1060 /* Utility routines to manage the array of pointers to timers. */
   1061 void
   1062 timers_alloc(struct proc *p)
   1063 {
   1064 	int i;
   1065 	struct ptimers *pts;
   1066 
   1067 	pts = malloc(sizeof (struct ptimers), M_SUBPROC, 0);
   1068 	LIST_INIT(&pts->pts_virtual);
   1069 	LIST_INIT(&pts->pts_prof);
   1070 	for (i = 0; i < TIMER_MAX; i++)
   1071 		pts->pts_timers[i] = NULL;
   1072 	pts->pts_fired = 0;
   1073 	p->p_timers = pts;
   1074 }
   1075 
   1076 /*
   1077  * Clean up the per-process timers. If "which" is set to TIMERS_ALL,
   1078  * then clean up all timers and free all the data structures. If
   1079  * "which" is set to TIMERS_POSIX, only clean up the timers allocated
   1080  * by timer_create(), not the BSD setitimer() timers, and only free the
   1081  * structure if none of those remain.
   1082  */
   1083 void
   1084 timers_free(struct proc *p, int which)
   1085 {
   1086 	int i, s;
   1087 	struct ptimers *pts;
   1088 	struct ptimer *pt, *ptn;
   1089 	struct timeval tv;
   1090 
   1091 	if (p->p_timers) {
   1092 		pts = p->p_timers;
   1093 		if (which == TIMERS_ALL)
   1094 			i = 0;
   1095 		else {
   1096 			s = splclock();
   1097 			timerclear(&tv);
   1098 			for (ptn = LIST_FIRST(&p->p_timers->pts_virtual);
   1099 			     ptn && ptn != pts->pts_timers[ITIMER_VIRTUAL];
   1100 			     ptn = LIST_NEXT(ptn, pt_list))
   1101 				timeradd(&tv, &ptn->pt_time.it_value, &tv);
   1102 			LIST_FIRST(&p->p_timers->pts_virtual) = NULL;
   1103 			if (ptn) {
   1104 				timeradd(&tv, &ptn->pt_time.it_value,
   1105 				    &ptn->pt_time.it_value);
   1106 				LIST_INSERT_HEAD(&p->p_timers->pts_virtual,
   1107 				    ptn, pt_list);
   1108 			}
   1109 
   1110 			timerclear(&tv);
   1111 			for (ptn = LIST_FIRST(&p->p_timers->pts_prof);
   1112 			     ptn && ptn != pts->pts_timers[ITIMER_PROF];
   1113 			     ptn = LIST_NEXT(ptn, pt_list))
   1114 				timeradd(&tv, &ptn->pt_time.it_value, &tv);
   1115 			LIST_FIRST(&p->p_timers->pts_prof) = NULL;
   1116 			if (ptn) {
   1117 				timeradd(&tv, &ptn->pt_time.it_value,
   1118 				    &ptn->pt_time.it_value);
   1119 				LIST_INSERT_HEAD(&p->p_timers->pts_prof, ptn,
   1120 				    pt_list);
   1121 			}
   1122 			splx(s);
   1123 			i = 3;
   1124 		}
   1125 		for ( ; i < TIMER_MAX; i++)
   1126 			if ((pt = pts->pts_timers[i]) != NULL) {
   1127 				if (pt->pt_type == CLOCK_REALTIME)
   1128 					callout_stop(&pt->pt_ch);
   1129 				pts->pts_timers[i] = NULL;
   1130 				pool_put(&ptimer_pool, pt);
   1131 			}
   1132 		if ((pts->pts_timers[0] == NULL) &&
   1133 		    (pts->pts_timers[1] == NULL) &&
   1134 		    (pts->pts_timers[2] == NULL)) {
   1135 			p->p_timers = NULL;
   1136 			free(pts, M_SUBPROC);
   1137 		}
   1138 	}
   1139 }
   1140 
   1141 /*
   1142  * Check that a proposed value to load into the .it_value or
   1143  * .it_interval part of an interval timer is acceptable, and
   1144  * fix it to have at least minimal value (i.e. if it is less
   1145  * than the resolution of the clock, round it up.)
   1146  */
   1147 int
   1148 itimerfix(struct timeval *tv)
   1149 {
   1150 
   1151 	if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
   1152 		return (EINVAL);
   1153 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
   1154 		tv->tv_usec = tick;
   1155 	return (0);
   1156 }
   1157 
   1158 /*
   1159  * Decrement an interval timer by a specified number
   1160  * of microseconds, which must be less than a second,
   1161  * i.e. < 1000000.  If the timer expires, then reload
   1162  * it.  In this case, carry over (usec - old value) to
   1163  * reduce the value reloaded into the timer so that
   1164  * the timer does not drift.  This routine assumes
   1165  * that it is called in a context where the timers
   1166  * on which it is operating cannot change in value.
   1167  */
   1168 int
   1169 itimerdecr(struct ptimer *pt, int usec)
   1170 {
   1171 	struct itimerval *itp;
   1172 
   1173 	itp = &pt->pt_time;
   1174 	if (itp->it_value.tv_usec < usec) {
   1175 		if (itp->it_value.tv_sec == 0) {
   1176 			/* expired, and already in next interval */
   1177 			usec -= itp->it_value.tv_usec;
   1178 			goto expire;
   1179 		}
   1180 		itp->it_value.tv_usec += 1000000;
   1181 		itp->it_value.tv_sec--;
   1182 	}
   1183 	itp->it_value.tv_usec -= usec;
   1184 	usec = 0;
   1185 	if (timerisset(&itp->it_value))
   1186 		return (1);
   1187 	/* expired, exactly at end of interval */
   1188 expire:
   1189 	if (timerisset(&itp->it_interval)) {
   1190 		itp->it_value = itp->it_interval;
   1191 		itp->it_value.tv_usec -= usec;
   1192 		if (itp->it_value.tv_usec < 0) {
   1193 			itp->it_value.tv_usec += 1000000;
   1194 			itp->it_value.tv_sec--;
   1195 		}
   1196 		timer_settime(pt);
   1197 	} else
   1198 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
   1199 	return (0);
   1200 }
   1201 
   1202 void
   1203 itimerfire(struct ptimer *pt)
   1204 {
   1205 	struct proc *p = pt->pt_proc;
   1206 	struct sadata_vp *vp;
   1207 	int s;
   1208 	unsigned int i;
   1209 
   1210 	if (pt->pt_ev.sigev_notify == SIGEV_SIGNAL) {
   1211 		/*
   1212 		 * No RT signal infrastructure exists at this time;
   1213 		 * just post the signal number and throw away the
   1214 		 * value.
   1215 		 */
   1216 		if (sigismember(&p->p_sigctx.ps_siglist, pt->pt_ev.sigev_signo))
   1217 			pt->pt_overruns++;
   1218 		else {
   1219 			ksiginfo_t ksi;
   1220 			(void)memset(&ksi, 0, sizeof(ksi));
   1221 			ksi.ksi_signo = pt->pt_ev.sigev_signo;
   1222 			ksi.ksi_code = SI_TIMER;
   1223 			ksi.ksi_sigval = pt->pt_ev.sigev_value;
   1224 			pt->pt_poverruns = pt->pt_overruns;
   1225 			pt->pt_overruns = 0;
   1226 			kpsignal(p, &ksi, NULL);
   1227 		}
   1228 	} else if (pt->pt_ev.sigev_notify == SIGEV_SA && (p->p_flag & P_SA)) {
   1229 		/* Cause the process to generate an upcall when it returns. */
   1230 
   1231 		if (p->p_userret == NULL) {
   1232 			/*
   1233 			 * XXX stop signals can be processed inside tsleep,
   1234 			 * which can be inside sa_yield's inner loop, which
   1235 			 * makes testing for sa_idle alone insuffucent to
   1236 			 * determine if we really should call setrunnable.
   1237 			 */
   1238 			pt->pt_poverruns = pt->pt_overruns;
   1239 			pt->pt_overruns = 0;
   1240 			i = 1 << pt->pt_entry;
   1241 			p->p_timers->pts_fired = i;
   1242 			p->p_userret = timerupcall;
   1243 			p->p_userret_arg = p->p_timers;
   1244 
   1245 			SCHED_LOCK(s);
   1246 			SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
   1247 				if (vp->savp_lwp->l_flag & L_SA_IDLE) {
   1248 					vp->savp_lwp->l_flag &= ~L_SA_IDLE;
   1249 					sched_wakeup(vp->savp_lwp);
   1250 					break;
   1251 				}
   1252 			}
   1253 			SCHED_UNLOCK(s);
   1254 		} else if (p->p_userret == timerupcall) {
   1255 			i = 1 << pt->pt_entry;
   1256 			if ((p->p_timers->pts_fired & i) == 0) {
   1257 				pt->pt_poverruns = pt->pt_overruns;
   1258 				pt->pt_overruns = 0;
   1259 				p->p_timers->pts_fired |= i;
   1260 			} else
   1261 				pt->pt_overruns++;
   1262 		} else {
   1263 			pt->pt_overruns++;
   1264 			if ((p->p_flag & P_WEXIT) == 0)
   1265 				printf("itimerfire(%d): overrun %d on timer %x (userret is %p)\n",
   1266 				    p->p_pid, pt->pt_overruns,
   1267 				    pt->pt_ev.sigev_value.sival_int,
   1268 				    p->p_userret);
   1269 		}
   1270 	}
   1271 
   1272 }
   1273 
   1274 /*
   1275  * ratecheck(): simple time-based rate-limit checking.  see ratecheck(9)
   1276  * for usage and rationale.
   1277  */
   1278 int
   1279 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
   1280 {
   1281 	struct timeval tv, delta;
   1282 	int s, rv = 0;
   1283 
   1284 	s = splclock();
   1285 	tv = mono_time;
   1286 	splx(s);
   1287 
   1288 	timersub(&tv, lasttime, &delta);
   1289 
   1290 	/*
   1291 	 * check for 0,0 is so that the message will be seen at least once,
   1292 	 * even if interval is huge.
   1293 	 */
   1294 	if (timercmp(&delta, mininterval, >=) ||
   1295 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
   1296 		*lasttime = tv;
   1297 		rv = 1;
   1298 	}
   1299 
   1300 	return (rv);
   1301 }
   1302 
   1303 /*
   1304  * ppsratecheck(): packets (or events) per second limitation.
   1305  */
   1306 int
   1307 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
   1308 {
   1309 	struct timeval tv, delta;
   1310 	int s, rv;
   1311 
   1312 	s = splclock();
   1313 	tv = mono_time;
   1314 	splx(s);
   1315 
   1316 	timersub(&tv, lasttime, &delta);
   1317 
   1318 	/*
   1319 	 * check for 0,0 is so that the message will be seen at least once.
   1320 	 * if more than one second have passed since the last update of
   1321 	 * lasttime, reset the counter.
   1322 	 *
   1323 	 * we do increment *curpps even in *curpps < maxpps case, as some may
   1324 	 * try to use *curpps for stat purposes as well.
   1325 	 */
   1326 	if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
   1327 	    delta.tv_sec >= 1) {
   1328 		*lasttime = tv;
   1329 		*curpps = 0;
   1330 	}
   1331 	if (maxpps < 0)
   1332 		rv = 1;
   1333 	else if (*curpps < maxpps)
   1334 		rv = 1;
   1335 	else
   1336 		rv = 0;
   1337 
   1338 #if 1 /*DIAGNOSTIC?*/
   1339 	/* be careful about wrap-around */
   1340 	if (*curpps + 1 > *curpps)
   1341 		*curpps = *curpps + 1;
   1342 #else
   1343 	/*
   1344 	 * assume that there's not too many calls to this function.
   1345 	 * not sure if the assumption holds, as it depends on *caller's*
   1346 	 * behavior, not the behavior of this function.
   1347 	 * IMHO it is wrong to make assumption on the caller's behavior,
   1348 	 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
   1349 	 */
   1350 	*curpps = *curpps + 1;
   1351 #endif
   1352 
   1353 	return (rv);
   1354 }
   1355