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