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