Home | History | Annotate | Line # | Download | only in kern
kern_time.c revision 1.157
      1 /*	$NetBSD: kern_time.c,v 1.157 2009/01/11 15:57:29 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000, 2004, 2005, 2007, 2008 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1982, 1986, 1989, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the University nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  *
     60  *	@(#)kern_time.c	8.4 (Berkeley) 5/26/95
     61  */
     62 
     63 #include <sys/cdefs.h>
     64 __KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.157 2009/01/11 15:57:29 christos Exp $");
     65 
     66 #include <sys/param.h>
     67 #include <sys/resourcevar.h>
     68 #include <sys/kernel.h>
     69 #include <sys/systm.h>
     70 #include <sys/proc.h>
     71 #include <sys/vnode.h>
     72 #include <sys/signalvar.h>
     73 #include <sys/syslog.h>
     74 #include <sys/timetc.h>
     75 #include <sys/timex.h>
     76 #include <sys/kauth.h>
     77 #include <sys/mount.h>
     78 #include <sys/sa.h>
     79 #include <sys/savar.h>
     80 #include <sys/syscallargs.h>
     81 #include <sys/cpu.h>
     82 
     83 #include <uvm/uvm_extern.h>
     84 
     85 #include "opt_sa.h"
     86 
     87 static void	timer_intr(void *);
     88 static void	itimerfire(struct ptimer *);
     89 static void	itimerfree(struct ptimers *, int);
     90 
     91 kmutex_t	timer_lock;
     92 
     93 static void	*timer_sih;
     94 static TAILQ_HEAD(, ptimer) timer_queue;
     95 
     96 POOL_INIT(ptimer_pool, sizeof(struct ptimer), 0, 0, 0, "ptimerpl",
     97     &pool_allocator_nointr, IPL_NONE);
     98 POOL_INIT(ptimers_pool, sizeof(struct ptimers), 0, 0, 0, "ptimerspl",
     99     &pool_allocator_nointr, IPL_NONE);
    100 
    101 /*
    102  * Initialize timekeeping.
    103  */
    104 void
    105 time_init(void)
    106 {
    107 
    108 	/* nothing yet */
    109 }
    110 
    111 void
    112 time_init2(void)
    113 {
    114 
    115 	TAILQ_INIT(&timer_queue);
    116 	mutex_init(&timer_lock, MUTEX_DEFAULT, IPL_SCHED);
    117 	timer_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
    118 	    timer_intr, NULL);
    119 }
    120 
    121 /* Time of day and interval timer support.
    122  *
    123  * These routines provide the kernel entry points to get and set
    124  * the time-of-day and per-process interval timers.  Subroutines
    125  * here provide support for adding and subtracting timeval structures
    126  * and decrementing interval timers, optionally reloading the interval
    127  * timers when they expire.
    128  */
    129 
    130 /* This function is used by clock_settime and settimeofday */
    131 static int
    132 settime1(struct proc *p, const struct timespec *ts, bool check_kauth)
    133 {
    134 	struct timespec delta, now;
    135 	struct bintime btdelta;
    136 	lwp_t *l;
    137 	int s;
    138 
    139 	/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
    140 	s = splclock();
    141 	nanotime(&now);
    142 	timespecsub(ts, &now, &delta);
    143 
    144 	if (check_kauth && kauth_authorize_system(kauth_cred_get(),
    145 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_SYSTEM, __UNCONST(ts),
    146 	    &delta, KAUTH_ARG(check_kauth ? false : true)) != 0) {
    147 		splx(s);
    148 		return (EPERM);
    149 	}
    150 
    151 #ifdef notyet
    152 	if ((delta.tv_sec < 86400) && securelevel > 0) { /* XXX elad - notyet */
    153 		splx(s);
    154 		return (EPERM);
    155 	}
    156 #endif
    157 
    158 	tc_setclock(ts);
    159 
    160 	timespecadd(&boottime, &delta, &boottime);
    161 
    162 	/*
    163 	 * XXXSMP: There is a short race between setting the time above
    164 	 * and adjusting LWP's run times.  Fixing this properly means
    165 	 * pausing all CPUs while we adjust the clock.
    166 	 */
    167 	timespec2bintime(&delta, &btdelta);
    168 	mutex_enter(proc_lock);
    169 	LIST_FOREACH(l, &alllwp, l_list) {
    170 		lwp_lock(l);
    171 		bintime_add(&l->l_stime, &btdelta);
    172 		lwp_unlock(l);
    173 	}
    174 	mutex_exit(proc_lock);
    175 	resettodr();
    176 	splx(s);
    177 
    178 	return (0);
    179 }
    180 
    181 int
    182 settime(struct proc *p, struct timespec *ts)
    183 {
    184 	return (settime1(p, ts, true));
    185 }
    186 
    187 /* ARGSUSED */
    188 int
    189 sys___clock_gettime50(struct lwp *l,
    190     const struct sys___clock_gettime50_args *uap, register_t *retval)
    191 {
    192 	/* {
    193 		syscallarg(clockid_t) clock_id;
    194 		syscallarg(struct timespec *) tp;
    195 	} */
    196 	clockid_t clock_id;
    197 	struct timespec ats;
    198 
    199 	clock_id = SCARG(uap, clock_id);
    200 	switch (clock_id) {
    201 	case CLOCK_REALTIME:
    202 		nanotime(&ats);
    203 		break;
    204 	case CLOCK_MONOTONIC:
    205 		nanouptime(&ats);
    206 		break;
    207 	default:
    208 		return (EINVAL);
    209 	}
    210 
    211 	return copyout(&ats, SCARG(uap, tp), sizeof(ats));
    212 }
    213 
    214 /* ARGSUSED */
    215 int
    216 sys___clock_settime50(struct lwp *l,
    217     const struct sys___clock_settime50_args *uap, register_t *retval)
    218 {
    219 	/* {
    220 		syscallarg(clockid_t) clock_id;
    221 		syscallarg(const struct timespec *) tp;
    222 	} */
    223 	int error;
    224 	struct timespec ats;
    225 
    226 	if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
    227 		return error;
    228 
    229 	return clock_settime1(l->l_proc, SCARG(uap, clock_id), &ats, true);
    230 }
    231 
    232 
    233 int
    234 clock_settime1(struct proc *p, clockid_t clock_id, const struct timespec *tp,
    235     bool check_kauth)
    236 {
    237 	int error;
    238 
    239 	switch (clock_id) {
    240 	case CLOCK_REALTIME:
    241 		if ((error = settime1(p, tp, check_kauth)) != 0)
    242 			return (error);
    243 		break;
    244 	case CLOCK_MONOTONIC:
    245 		return (EINVAL);	/* read-only clock */
    246 	default:
    247 		return (EINVAL);
    248 	}
    249 
    250 	return 0;
    251 }
    252 
    253 int
    254 sys___clock_getres50(struct lwp *l, const struct sys___clock_getres50_args *uap,
    255     register_t *retval)
    256 {
    257 	/* {
    258 		syscallarg(clockid_t) clock_id;
    259 		syscallarg(struct timespec *) tp;
    260 	} */
    261 	clockid_t clock_id;
    262 	struct timespec ts;
    263 	int error = 0;
    264 
    265 	clock_id = SCARG(uap, clock_id);
    266 	switch (clock_id) {
    267 	case CLOCK_REALTIME:
    268 	case CLOCK_MONOTONIC:
    269 		ts.tv_sec = 0;
    270 		if (tc_getfrequency() > 1000000000)
    271 			ts.tv_nsec = 1;
    272 		else
    273 			ts.tv_nsec = 1000000000 / tc_getfrequency();
    274 		break;
    275 	default:
    276 		return (EINVAL);
    277 	}
    278 
    279 	if (SCARG(uap, tp))
    280 		error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
    281 
    282 	return error;
    283 }
    284 
    285 /* ARGSUSED */
    286 int
    287 sys___nanosleep50(struct lwp *l, const struct sys___nanosleep50_args *uap,
    288     register_t *retval)
    289 {
    290 	/* {
    291 		syscallarg(struct timespec *) rqtp;
    292 		syscallarg(struct timespec *) rmtp;
    293 	} */
    294 	struct timespec rmt, rqt;
    295 	int error, error1;
    296 
    297 	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(struct timespec));
    298 	if (error)
    299 		return (error);
    300 
    301 	error = nanosleep1(l, &rqt, SCARG(uap, rmtp) ? &rmt : NULL);
    302 	if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
    303 		return error;
    304 
    305 	error1 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
    306 	return error1 ? error1 : error;
    307 }
    308 
    309 int
    310 nanosleep1(struct lwp *l, struct timespec *rqt, struct timespec *rmt)
    311 {
    312 	struct timespec rmtstart;
    313 	int error, timo;
    314 
    315 	if (itimespecfix(rqt))
    316 		return (EINVAL);
    317 
    318 	timo = tstohz(rqt);
    319 	/*
    320 	 * Avoid inadvertantly sleeping forever
    321 	 */
    322 	if (timo == 0)
    323 		timo = 1;
    324 	getnanouptime(&rmtstart);
    325 again:
    326 	error = kpause("nanoslp", true, timo, NULL);
    327 	if (rmt != NULL || error == 0) {
    328 		struct timespec rmtend;
    329 		struct timespec t0;
    330 		struct timespec *t;
    331 
    332 		getnanouptime(&rmtend);
    333 		t = (rmt != NULL) ? rmt : &t0;
    334 		timespecsub(&rmtend, &rmtstart, t);
    335 		timespecsub(rqt, t, t);
    336 		if (t->tv_sec < 0)
    337 			timespecclear(t);
    338 		if (error == 0) {
    339 			timo = tstohz(t);
    340 			if (timo > 0)
    341 				goto again;
    342 		}
    343 	}
    344 
    345 	if (error == ERESTART)
    346 		error = EINTR;
    347 	if (error == EWOULDBLOCK)
    348 		error = 0;
    349 
    350 	return error;
    351 }
    352 
    353 /* ARGSUSED */
    354 int
    355 sys___gettimeofday50(struct lwp *l, const struct sys___gettimeofday50_args *uap,
    356     register_t *retval)
    357 {
    358 	/* {
    359 		syscallarg(struct timeval *) tp;
    360 		syscallarg(void *) tzp;		really "struct timezone *";
    361 	} */
    362 	struct timeval atv;
    363 	int error = 0;
    364 	struct timezone tzfake;
    365 
    366 	if (SCARG(uap, tp)) {
    367 		microtime(&atv);
    368 		error = copyout(&atv, SCARG(uap, tp), sizeof(atv));
    369 		if (error)
    370 			return (error);
    371 	}
    372 	if (SCARG(uap, tzp)) {
    373 		/*
    374 		 * NetBSD has no kernel notion of time zone, so we just
    375 		 * fake up a timezone struct and return it if demanded.
    376 		 */
    377 		tzfake.tz_minuteswest = 0;
    378 		tzfake.tz_dsttime = 0;
    379 		error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
    380 	}
    381 	return (error);
    382 }
    383 
    384 /* ARGSUSED */
    385 int
    386 sys___settimeofday50(struct lwp *l, const struct sys___settimeofday50_args *uap,
    387     register_t *retval)
    388 {
    389 	/* {
    390 		syscallarg(const struct timeval *) tv;
    391 		syscallarg(const void *) tzp; really "const struct timezone *";
    392 	} */
    393 
    394 	return settimeofday1(SCARG(uap, tv), true, SCARG(uap, tzp), l, true);
    395 }
    396 
    397 int
    398 settimeofday1(const struct timeval *utv, bool userspace,
    399     const void *utzp, struct lwp *l, bool check_kauth)
    400 {
    401 	struct timeval atv;
    402 	struct timespec ts;
    403 	int error;
    404 
    405 	/* Verify all parameters before changing time. */
    406 
    407 	/*
    408 	 * NetBSD has no kernel notion of time zone, and only an
    409 	 * obsolete program would try to set it, so we log a warning.
    410 	 */
    411 	if (utzp)
    412 		log(LOG_WARNING, "pid %d attempted to set the "
    413 		    "(obsolete) kernel time zone\n", l->l_proc->p_pid);
    414 
    415 	if (utv == NULL)
    416 		return 0;
    417 
    418 	if (userspace) {
    419 		if ((error = copyin(utv, &atv, sizeof(atv))) != 0)
    420 			return error;
    421 		utv = &atv;
    422 	}
    423 
    424 	TIMEVAL_TO_TIMESPEC(utv, &ts);
    425 	return settime1(l->l_proc, &ts, check_kauth);
    426 }
    427 
    428 int	time_adjusted;			/* set if an adjustment is made */
    429 
    430 /* ARGSUSED */
    431 int
    432 sys___adjtime50(struct lwp *l, const struct sys___adjtime50_args *uap,
    433     register_t *retval)
    434 {
    435 	/* {
    436 		syscallarg(const struct timeval *) delta;
    437 		syscallarg(struct timeval *) olddelta;
    438 	} */
    439 	int error = 0;
    440 	struct timeval atv, oldatv;
    441 
    442 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
    443 	    KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL, NULL)) != 0)
    444 		return error;
    445 
    446 	if (SCARG(uap, delta)) {
    447 		error = copyin(SCARG(uap, delta), &atv,
    448 		    sizeof(*SCARG(uap, delta)));
    449 		if (error)
    450 			return (error);
    451 	}
    452 	adjtime1(SCARG(uap, delta) ? &atv : NULL,
    453 	    SCARG(uap, olddelta) ? &oldatv : NULL, l->l_proc);
    454 	if (SCARG(uap, olddelta))
    455 		error = copyout(&oldatv, SCARG(uap, olddelta),
    456 		    sizeof(*SCARG(uap, olddelta)));
    457 	return error;
    458 }
    459 
    460 void
    461 adjtime1(const struct timeval *delta, struct timeval *olddelta, struct proc *p)
    462 {
    463 	extern int64_t time_adjtime;  /* in kern_ntptime.c */
    464 
    465 	if (olddelta) {
    466 		mutex_spin_enter(&timecounter_lock);
    467 		olddelta->tv_sec = time_adjtime / 1000000;
    468 		olddelta->tv_usec = time_adjtime % 1000000;
    469 		if (olddelta->tv_usec < 0) {
    470 			olddelta->tv_usec += 1000000;
    471 			olddelta->tv_sec--;
    472 		}
    473 		mutex_spin_exit(&timecounter_lock);
    474 	}
    475 
    476 	if (delta) {
    477 		mutex_spin_enter(&timecounter_lock);
    478 		time_adjtime = delta->tv_sec * 1000000 + delta->tv_usec;
    479 
    480 		if (time_adjtime) {
    481 			/* We need to save the system time during shutdown */
    482 			time_adjusted |= 1;
    483 		}
    484 		mutex_spin_exit(&timecounter_lock);
    485 	}
    486 }
    487 
    488 /*
    489  * Interval timer support. Both the BSD getitimer() family and the POSIX
    490  * timer_*() family of routines are supported.
    491  *
    492  * All timers are kept in an array pointed to by p_timers, which is
    493  * allocated on demand - many processes don't use timers at all. The
    494  * first three elements in this array are reserved for the BSD timers:
    495  * element 0 is ITIMER_REAL, element 1 is ITIMER_VIRTUAL, and element
    496  * 2 is ITIMER_PROF. The rest may be allocated by the timer_create()
    497  * syscall.
    498  *
    499  * Realtime timers are kept in the ptimer structure as an absolute
    500  * time; virtual time timers are kept as a linked list of deltas.
    501  * Virtual time timers are processed in the hardclock() routine of
    502  * kern_clock.c.  The real time timer is processed by a callout
    503  * routine, called from the softclock() routine.  Since a callout may
    504  * be delayed in real time due to interrupt processing in the system,
    505  * it is possible for the real time timeout routine (realtimeexpire,
    506  * given below), to be delayed in real time past when it is supposed
    507  * to occur.  It does not suffice, therefore, to reload the real timer
    508  * .it_value from the real time timers .it_interval.  Rather, we
    509  * compute the next time in absolute time the timer should go off.  */
    510 
    511 /* Allocate a POSIX realtime timer. */
    512 int
    513 sys_timer_create(struct lwp *l, const struct sys_timer_create_args *uap,
    514     register_t *retval)
    515 {
    516 	/* {
    517 		syscallarg(clockid_t) clock_id;
    518 		syscallarg(struct sigevent *) evp;
    519 		syscallarg(timer_t *) timerid;
    520 	} */
    521 
    522 	return timer_create1(SCARG(uap, timerid), SCARG(uap, clock_id),
    523 	    SCARG(uap, evp), copyin, l);
    524 }
    525 
    526 int
    527 timer_create1(timer_t *tid, clockid_t id, struct sigevent *evp,
    528     copyin_t fetch_event, struct lwp *l)
    529 {
    530 	int error;
    531 	timer_t timerid;
    532 	struct ptimers *pts;
    533 	struct ptimer *pt;
    534 	struct proc *p;
    535 
    536 	p = l->l_proc;
    537 
    538 	if (id < CLOCK_REALTIME || id > CLOCK_PROF)
    539 		return (EINVAL);
    540 
    541 	if ((pts = p->p_timers) == NULL)
    542 		pts = timers_alloc(p);
    543 
    544 	pt = pool_get(&ptimer_pool, PR_WAITOK);
    545 	if (evp != NULL) {
    546 		if (((error =
    547 		    (*fetch_event)(evp, &pt->pt_ev, sizeof(pt->pt_ev))) != 0) ||
    548 		    ((pt->pt_ev.sigev_notify < SIGEV_NONE) ||
    549 			(pt->pt_ev.sigev_notify > SIGEV_SA))) {
    550 			pool_put(&ptimer_pool, pt);
    551 			return (error ? error : EINVAL);
    552 		}
    553 	}
    554 
    555 	/* Find a free timer slot, skipping those reserved for setitimer(). */
    556 	mutex_spin_enter(&timer_lock);
    557 	for (timerid = 3; timerid < TIMER_MAX; timerid++)
    558 		if (pts->pts_timers[timerid] == NULL)
    559 			break;
    560 	if (timerid == TIMER_MAX) {
    561 		mutex_spin_exit(&timer_lock);
    562 		pool_put(&ptimer_pool, pt);
    563 		return EAGAIN;
    564 	}
    565 	if (evp == NULL) {
    566 		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
    567 		switch (id) {
    568 		case CLOCK_REALTIME:
    569 			pt->pt_ev.sigev_signo = SIGALRM;
    570 			break;
    571 		case CLOCK_VIRTUAL:
    572 			pt->pt_ev.sigev_signo = SIGVTALRM;
    573 			break;
    574 		case CLOCK_PROF:
    575 			pt->pt_ev.sigev_signo = SIGPROF;
    576 			break;
    577 		}
    578 		pt->pt_ev.sigev_value.sival_int = timerid;
    579 	}
    580 	pt->pt_info.ksi_signo = pt->pt_ev.sigev_signo;
    581 	pt->pt_info.ksi_errno = 0;
    582 	pt->pt_info.ksi_code = 0;
    583 	pt->pt_info.ksi_pid = p->p_pid;
    584 	pt->pt_info.ksi_uid = kauth_cred_getuid(l->l_cred);
    585 	pt->pt_info.ksi_value = pt->pt_ev.sigev_value;
    586 	pt->pt_type = id;
    587 	pt->pt_proc = p;
    588 	pt->pt_overruns = 0;
    589 	pt->pt_poverruns = 0;
    590 	pt->pt_entry = timerid;
    591 	pt->pt_queued = false;
    592 	timespecclear(&pt->pt_time.it_value);
    593 	if (id == CLOCK_REALTIME)
    594 		callout_init(&pt->pt_ch, 0);
    595 	else
    596 		pt->pt_active = 0;
    597 
    598 	pts->pts_timers[timerid] = pt;
    599 	mutex_spin_exit(&timer_lock);
    600 
    601 	return copyout(&timerid, tid, sizeof(timerid));
    602 }
    603 
    604 /* Delete a POSIX realtime timer */
    605 int
    606 sys_timer_delete(struct lwp *l, const struct sys_timer_delete_args *uap,
    607     register_t *retval)
    608 {
    609 	/* {
    610 		syscallarg(timer_t) timerid;
    611 	} */
    612 	struct proc *p = l->l_proc;
    613 	timer_t timerid;
    614 	struct ptimers *pts;
    615 	struct ptimer *pt, *ptn;
    616 
    617 	timerid = SCARG(uap, timerid);
    618 	pts = p->p_timers;
    619 
    620 	if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
    621 		return (EINVAL);
    622 
    623 	mutex_spin_enter(&timer_lock);
    624 	if ((pt = pts->pts_timers[timerid]) == NULL) {
    625 		mutex_spin_exit(&timer_lock);
    626 		return (EINVAL);
    627 	}
    628 	if (pt->pt_type != CLOCK_REALTIME) {
    629 		if (pt->pt_active) {
    630 			ptn = LIST_NEXT(pt, pt_list);
    631 			LIST_REMOVE(pt, pt_list);
    632 			for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
    633 				timespecadd(&pt->pt_time.it_value,
    634 				    &ptn->pt_time.it_value,
    635 				    &ptn->pt_time.it_value);
    636 			pt->pt_active = 0;
    637 		}
    638 	}
    639 	itimerfree(pts, timerid);
    640 
    641 	return (0);
    642 }
    643 
    644 /*
    645  * Set up the given timer. The value in pt->pt_time.it_value is taken
    646  * to be an absolute time for CLOCK_REALTIME timers and a relative
    647  * time for virtual timers.
    648  * Must be called at splclock().
    649  */
    650 void
    651 timer_settime(struct ptimer *pt)
    652 {
    653 	struct ptimer *ptn, *pptn;
    654 	struct ptlist *ptl;
    655 
    656 	KASSERT(mutex_owned(&timer_lock));
    657 
    658 	if (pt->pt_type == CLOCK_REALTIME) {
    659 		callout_stop(&pt->pt_ch);
    660 		if (timespecisset(&pt->pt_time.it_value)) {
    661 			/*
    662 			 * Don't need to check tshzto() return value, here.
    663 			 * callout_reset() does it for us.
    664 			 */
    665 			callout_reset(&pt->pt_ch, tshzto(&pt->pt_time.it_value),
    666 			    realtimerexpire, pt);
    667 		}
    668 	} else {
    669 		if (pt->pt_active) {
    670 			ptn = LIST_NEXT(pt, pt_list);
    671 			LIST_REMOVE(pt, pt_list);
    672 			for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
    673 				timespecadd(&pt->pt_time.it_value,
    674 				    &ptn->pt_time.it_value,
    675 				    &ptn->pt_time.it_value);
    676 		}
    677 		if (timespecisset(&pt->pt_time.it_value)) {
    678 			if (pt->pt_type == CLOCK_VIRTUAL)
    679 				ptl = &pt->pt_proc->p_timers->pts_virtual;
    680 			else
    681 				ptl = &pt->pt_proc->p_timers->pts_prof;
    682 
    683 			for (ptn = LIST_FIRST(ptl), pptn = NULL;
    684 			     ptn && timespeccmp(&pt->pt_time.it_value,
    685 				 &ptn->pt_time.it_value, >);
    686 			     pptn = ptn, ptn = LIST_NEXT(ptn, pt_list))
    687 				timespecsub(&pt->pt_time.it_value,
    688 				    &ptn->pt_time.it_value,
    689 				    &pt->pt_time.it_value);
    690 
    691 			if (pptn)
    692 				LIST_INSERT_AFTER(pptn, pt, pt_list);
    693 			else
    694 				LIST_INSERT_HEAD(ptl, pt, pt_list);
    695 
    696 			for ( ; ptn ; ptn = LIST_NEXT(ptn, pt_list))
    697 				timespecsub(&ptn->pt_time.it_value,
    698 				    &pt->pt_time.it_value,
    699 				    &ptn->pt_time.it_value);
    700 
    701 			pt->pt_active = 1;
    702 		} else
    703 			pt->pt_active = 0;
    704 	}
    705 }
    706 
    707 void
    708 timer_gettime(struct ptimer *pt, struct itimerspec *aits)
    709 {
    710 	struct timespec now;
    711 	struct ptimer *ptn;
    712 
    713 	KASSERT(mutex_owned(&timer_lock));
    714 
    715 	*aits = pt->pt_time;
    716 	if (pt->pt_type == CLOCK_REALTIME) {
    717 		/*
    718 		 * Convert from absolute to relative time in .it_value
    719 		 * part of real time timer.  If time for real time
    720 		 * timer has passed return 0, else return difference
    721 		 * between current time and time for the timer to go
    722 		 * off.
    723 		 */
    724 		if (timespecisset(&aits->it_value)) {
    725 			getnanotime(&now);
    726 			if (timespeccmp(&aits->it_value, &now, <))
    727 				timespecclear(&aits->it_value);
    728 			else
    729 				timespecsub(&aits->it_value, &now,
    730 				    &aits->it_value);
    731 		}
    732 	} else if (pt->pt_active) {
    733 		if (pt->pt_type == CLOCK_VIRTUAL)
    734 			ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_virtual);
    735 		else
    736 			ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_prof);
    737 		for ( ; ptn && ptn != pt; ptn = LIST_NEXT(ptn, pt_list))
    738 			timespecadd(&aits->it_value,
    739 			    &ptn->pt_time.it_value, &aits->it_value);
    740 		KASSERT(ptn != NULL); /* pt should be findable on the list */
    741 	} else
    742 		timespecclear(&aits->it_value);
    743 }
    744 
    745 
    746 
    747 /* Set and arm a POSIX realtime timer */
    748 int
    749 sys___timer_settime50(struct lwp *l,
    750     const struct sys___timer_settime50_args *uap,
    751     register_t *retval)
    752 {
    753 	/* {
    754 		syscallarg(timer_t) timerid;
    755 		syscallarg(int) flags;
    756 		syscallarg(const struct itimerspec *) value;
    757 		syscallarg(struct itimerspec *) ovalue;
    758 	} */
    759 	int error;
    760 	struct itimerspec value, ovalue, *ovp = NULL;
    761 
    762 	if ((error = copyin(SCARG(uap, value), &value,
    763 	    sizeof(struct itimerspec))) != 0)
    764 		return (error);
    765 
    766 	if (SCARG(uap, ovalue))
    767 		ovp = &ovalue;
    768 
    769 	if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
    770 	    SCARG(uap, flags), l->l_proc)) != 0)
    771 		return error;
    772 
    773 	if (ovp)
    774 		return copyout(&ovalue, SCARG(uap, ovalue),
    775 		    sizeof(struct itimerspec));
    776 	return 0;
    777 }
    778 
    779 int
    780 dotimer_settime(int timerid, struct itimerspec *value,
    781     struct itimerspec *ovalue, int flags, struct proc *p)
    782 {
    783 	struct timespec now;
    784 	struct itimerspec val, oval;
    785 	struct ptimers *pts;
    786 	struct ptimer *pt;
    787 
    788 	pts = p->p_timers;
    789 
    790 	if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
    791 		return EINVAL;
    792 	val = *value;
    793 	if (itimespecfix(&val.it_value) || itimespecfix(&val.it_interval))
    794 		return EINVAL;
    795 
    796 	mutex_spin_enter(&timer_lock);
    797 	if ((pt = pts->pts_timers[timerid]) == NULL) {
    798 		mutex_spin_exit(&timer_lock);
    799 		return EINVAL;
    800 	}
    801 
    802 	oval = pt->pt_time;
    803 	pt->pt_time = val;
    804 
    805 	/*
    806 	 * If we've been passed a relative time for a realtime timer,
    807 	 * convert it to absolute; if an absolute time for a virtual
    808 	 * timer, convert it to relative and make sure we don't set it
    809 	 * to zero, which would cancel the timer, or let it go
    810 	 * negative, which would confuse the comparison tests.
    811 	 */
    812 	if (timespecisset(&pt->pt_time.it_value)) {
    813 		if (pt->pt_type == CLOCK_REALTIME) {
    814 			if ((flags & TIMER_ABSTIME) == 0) {
    815 				getnanotime(&now);
    816 				timespecadd(&pt->pt_time.it_value, &now,
    817 				    &pt->pt_time.it_value);
    818 			}
    819 		} else {
    820 			if ((flags & TIMER_ABSTIME) != 0) {
    821 				getnanotime(&now);
    822 				timespecsub(&pt->pt_time.it_value, &now,
    823 				    &pt->pt_time.it_value);
    824 				if (!timespecisset(&pt->pt_time.it_value) ||
    825 				    pt->pt_time.it_value.tv_sec < 0) {
    826 					pt->pt_time.it_value.tv_sec = 0;
    827 					pt->pt_time.it_value.tv_nsec = 1;
    828 				}
    829 			}
    830 		}
    831 	}
    832 
    833 	timer_settime(pt);
    834 	mutex_spin_exit(&timer_lock);
    835 
    836 	if (ovalue)
    837 		*ovalue = oval;
    838 
    839 	return (0);
    840 }
    841 
    842 /* Return the time remaining until a POSIX timer fires. */
    843 int
    844 sys___timer_gettime50(struct lwp *l,
    845     const struct sys___timer_gettime50_args *uap, register_t *retval)
    846 {
    847 	/* {
    848 		syscallarg(timer_t) timerid;
    849 		syscallarg(struct itimerspec *) value;
    850 	} */
    851 	struct itimerspec its;
    852 	int error;
    853 
    854 	if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
    855 	    &its)) != 0)
    856 		return error;
    857 
    858 	return copyout(&its, SCARG(uap, value), sizeof(its));
    859 }
    860 
    861 int
    862 dotimer_gettime(int timerid, struct proc *p, struct itimerspec *its)
    863 {
    864 	struct ptimer *pt;
    865 	struct ptimers *pts;
    866 
    867 	pts = p->p_timers;
    868 	if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
    869 		return (EINVAL);
    870 	mutex_spin_enter(&timer_lock);
    871 	if ((pt = pts->pts_timers[timerid]) == NULL) {
    872 		mutex_spin_exit(&timer_lock);
    873 		return (EINVAL);
    874 	}
    875 	timer_gettime(pt, its);
    876 	mutex_spin_exit(&timer_lock);
    877 
    878 	return 0;
    879 }
    880 
    881 /*
    882  * Return the count of the number of times a periodic timer expired
    883  * while a notification was already pending. The counter is reset when
    884  * a timer expires and a notification can be posted.
    885  */
    886 int
    887 sys_timer_getoverrun(struct lwp *l, const struct sys_timer_getoverrun_args *uap,
    888     register_t *retval)
    889 {
    890 	/* {
    891 		syscallarg(timer_t) timerid;
    892 	} */
    893 	struct proc *p = l->l_proc;
    894 	struct ptimers *pts;
    895 	int timerid;
    896 	struct ptimer *pt;
    897 
    898 	timerid = SCARG(uap, timerid);
    899 
    900 	pts = p->p_timers;
    901 	if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
    902 		return (EINVAL);
    903 	mutex_spin_enter(&timer_lock);
    904 	if ((pt = pts->pts_timers[timerid]) == NULL) {
    905 		mutex_spin_exit(&timer_lock);
    906 		return (EINVAL);
    907 	}
    908 	*retval = pt->pt_poverruns;
    909 	mutex_spin_exit(&timer_lock);
    910 
    911 	return (0);
    912 }
    913 
    914 #ifdef KERN_SA
    915 /* Glue function that triggers an upcall; called from userret(). */
    916 void
    917 timerupcall(struct lwp *l)
    918 {
    919 	struct ptimers *pt = l->l_proc->p_timers;
    920 	struct proc *p = l->l_proc;
    921 	unsigned int i, fired, done;
    922 
    923 	KDASSERT(l->l_proc->p_sa);
    924 	/* Bail out if we do not own the virtual processor */
    925 	if (l->l_savp->savp_lwp != l)
    926 		return ;
    927 
    928 	mutex_enter(p->p_lock);
    929 
    930 	fired = pt->pts_fired;
    931 	done = 0;
    932 	while ((i = ffs(fired)) != 0) {
    933 		siginfo_t *si;
    934 		int mask = 1 << --i;
    935 		int f;
    936 
    937 		f = ~l->l_pflag & LP_SA_NOBLOCK;
    938 		l->l_pflag |= LP_SA_NOBLOCK;
    939 		si = siginfo_alloc(PR_WAITOK);
    940 		si->_info = pt->pts_timers[i]->pt_info.ksi_info;
    941 		if (sa_upcall(l, SA_UPCALL_SIGEV | SA_UPCALL_DEFER, NULL, l,
    942 		    sizeof(*si), si, siginfo_free) != 0) {
    943 			siginfo_free(si);
    944 			/* XXX What do we do here?? */
    945 		} else
    946 			done |= mask;
    947 		fired &= ~mask;
    948 		l->l_pflag ^= f;
    949 	}
    950 	pt->pts_fired &= ~done;
    951 	if (pt->pts_fired == 0)
    952 		l->l_proc->p_timerpend = 0;
    953 
    954 	mutex_exit(p->p_lock);
    955 }
    956 #endif /* KERN_SA */
    957 
    958 /*
    959  * Real interval timer expired:
    960  * send process whose timer expired an alarm signal.
    961  * If time is not set up to reload, then just return.
    962  * Else compute next time timer should go off which is > current time.
    963  * This is where delay in processing this timeout causes multiple
    964  * SIGALRM calls to be compressed into one.
    965  */
    966 void
    967 realtimerexpire(void *arg)
    968 {
    969 	uint64_t last_val, next_val, interval, now_ms;
    970 	struct timespec now, next;
    971 	struct ptimer *pt;
    972 	int backwards;
    973 
    974 	pt = arg;
    975 
    976 	mutex_spin_enter(&timer_lock);
    977 	itimerfire(pt);
    978 
    979 	if (!timespecisset(&pt->pt_time.it_interval)) {
    980 		timespecclear(&pt->pt_time.it_value);
    981 		mutex_spin_exit(&timer_lock);
    982 		return;
    983 	}
    984 
    985 	getnanotime(&now);
    986 	backwards = (timespeccmp(&pt->pt_time.it_value, &now, >));
    987 	timespecadd(&pt->pt_time.it_value, &pt->pt_time.it_interval, &next);
    988 	/* Handle the easy case of non-overflown timers first. */
    989 	if (!backwards && timespeccmp(&next, &now, >)) {
    990 		pt->pt_time.it_value = next;
    991 	} else {
    992 		now_ms = timespec2ns(&now);
    993 		last_val = timespec2ns(&pt->pt_time.it_value);
    994 		interval = timespec2ns(&pt->pt_time.it_interval);
    995 
    996 		next_val = now_ms +
    997 		    (now_ms - last_val + interval - 1) % interval;
    998 
    999 		if (backwards)
   1000 			next_val += interval;
   1001 		else
   1002 			pt->pt_overruns += (now_ms - last_val) / interval;
   1003 
   1004 		pt->pt_time.it_value.tv_sec = next_val / 1000000000;
   1005 		pt->pt_time.it_value.tv_nsec = next_val % 1000000000;
   1006 	}
   1007 
   1008 	/*
   1009 	 * Don't need to check tshzto() return value, here.
   1010 	 * callout_reset() does it for us.
   1011 	 */
   1012 	callout_reset(&pt->pt_ch, tshzto(&pt->pt_time.it_value),
   1013 	    realtimerexpire, pt);
   1014 	mutex_spin_exit(&timer_lock);
   1015 }
   1016 
   1017 /* BSD routine to get the value of an interval timer. */
   1018 /* ARGSUSED */
   1019 int
   1020 sys___getitimer50(struct lwp *l, const struct sys___getitimer50_args *uap,
   1021     register_t *retval)
   1022 {
   1023 	/* {
   1024 		syscallarg(int) which;
   1025 		syscallarg(struct itimerval *) itv;
   1026 	} */
   1027 	struct proc *p = l->l_proc;
   1028 	struct itimerval aitv;
   1029 	int error;
   1030 
   1031 	error = dogetitimer(p, SCARG(uap, which), &aitv);
   1032 	if (error)
   1033 		return error;
   1034 	return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
   1035 }
   1036 
   1037 int
   1038 dogetitimer(struct proc *p, int which, struct itimerval *itvp)
   1039 {
   1040 	struct ptimers *pts;
   1041 	struct ptimer *pt;
   1042 	struct itimerspec its;
   1043 
   1044 	if ((u_int)which > ITIMER_PROF)
   1045 		return (EINVAL);
   1046 
   1047 	mutex_spin_enter(&timer_lock);
   1048 	pts = p->p_timers;
   1049 	if (pts == NULL || (pt = pts->pts_timers[which]) == NULL) {
   1050 		timerclear(&itvp->it_value);
   1051 		timerclear(&itvp->it_interval);
   1052 	} else {
   1053 		timer_gettime(pt, &its);
   1054 		TIMESPEC_TO_TIMEVAL(&itvp->it_value, &its.it_value);
   1055 		TIMESPEC_TO_TIMEVAL(&itvp->it_interval, &its.it_interval);
   1056 	}
   1057 	mutex_spin_exit(&timer_lock);
   1058 
   1059 	return 0;
   1060 }
   1061 
   1062 /* BSD routine to set/arm an interval timer. */
   1063 /* ARGSUSED */
   1064 int
   1065 sys___setitimer50(struct lwp *l, const struct sys___setitimer50_args *uap,
   1066     register_t *retval)
   1067 {
   1068 	/* {
   1069 		syscallarg(int) which;
   1070 		syscallarg(const struct itimerval *) itv;
   1071 		syscallarg(struct itimerval *) oitv;
   1072 	} */
   1073 	struct proc *p = l->l_proc;
   1074 	int which = SCARG(uap, which);
   1075 	struct sys___getitimer50_args getargs;
   1076 	const struct itimerval *itvp;
   1077 	struct itimerval aitv;
   1078 	int error;
   1079 
   1080 	if ((u_int)which > ITIMER_PROF)
   1081 		return (EINVAL);
   1082 	itvp = SCARG(uap, itv);
   1083 	if (itvp &&
   1084 	    (error = copyin(itvp, &aitv, sizeof(struct itimerval)) != 0))
   1085 		return (error);
   1086 	if (SCARG(uap, oitv) != NULL) {
   1087 		SCARG(&getargs, which) = which;
   1088 		SCARG(&getargs, itv) = SCARG(uap, oitv);
   1089 		if ((error = sys___getitimer50(l, &getargs, retval)) != 0)
   1090 			return (error);
   1091 	}
   1092 	if (itvp == 0)
   1093 		return (0);
   1094 
   1095 	return dosetitimer(p, which, &aitv);
   1096 }
   1097 
   1098 int
   1099 dosetitimer(struct proc *p, int which, struct itimerval *itvp)
   1100 {
   1101 	struct timespec now;
   1102 	struct ptimers *pts;
   1103 	struct ptimer *pt, *spare;
   1104 
   1105 	if (itimerfix(&itvp->it_value) || itimerfix(&itvp->it_interval))
   1106 		return (EINVAL);
   1107 
   1108 	/*
   1109 	 * Don't bother allocating data structures if the process just
   1110 	 * wants to clear the timer.
   1111 	 */
   1112 	spare = NULL;
   1113 	pts = p->p_timers;
   1114  retry:
   1115 	if (!timerisset(&itvp->it_value) && (pts == NULL ||
   1116 	    pts->pts_timers[which] == NULL))
   1117 		return (0);
   1118 	if (pts == NULL)
   1119 		pts = timers_alloc(p);
   1120 	mutex_spin_enter(&timer_lock);
   1121 	pt = pts->pts_timers[which];
   1122 	if (pt == NULL) {
   1123 		if (spare == NULL) {
   1124 			mutex_spin_exit(&timer_lock);
   1125 			spare = pool_get(&ptimer_pool, PR_WAITOK);
   1126 			goto retry;
   1127 		}
   1128 		pt = spare;
   1129 		spare = NULL;
   1130 		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
   1131 		pt->pt_ev.sigev_value.sival_int = which;
   1132 		pt->pt_overruns = 0;
   1133 		pt->pt_proc = p;
   1134 		pt->pt_type = which;
   1135 		pt->pt_entry = which;
   1136 		pt->pt_queued = false;
   1137 		if (pt->pt_type == CLOCK_REALTIME)
   1138 			callout_init(&pt->pt_ch, CALLOUT_MPSAFE);
   1139 		else
   1140 			pt->pt_active = 0;
   1141 
   1142 		switch (which) {
   1143 		case ITIMER_REAL:
   1144 			pt->pt_ev.sigev_signo = SIGALRM;
   1145 			break;
   1146 		case ITIMER_VIRTUAL:
   1147 			pt->pt_ev.sigev_signo = SIGVTALRM;
   1148 			break;
   1149 		case ITIMER_PROF:
   1150 			pt->pt_ev.sigev_signo = SIGPROF;
   1151 			break;
   1152 		}
   1153 		pts->pts_timers[which] = pt;
   1154 	}
   1155 
   1156 	TIMEVAL_TO_TIMESPEC(&itvp->it_value, &pt->pt_time.it_value);
   1157 	TIMEVAL_TO_TIMESPEC(&itvp->it_interval, &pt->pt_time.it_interval);
   1158 
   1159 	if ((which == ITIMER_REAL) && timespecisset(&pt->pt_time.it_value)) {
   1160 		/* Convert to absolute time */
   1161 		/* XXX need to wrap in splclock for timecounters case? */
   1162 		getnanotime(&now);
   1163 		timespecadd(&pt->pt_time.it_value, &now, &pt->pt_time.it_value);
   1164 	}
   1165 	timer_settime(pt);
   1166 	mutex_spin_exit(&timer_lock);
   1167 	if (spare != NULL)
   1168 		pool_put(&ptimer_pool, spare);
   1169 
   1170 	return (0);
   1171 }
   1172 
   1173 /* Utility routines to manage the array of pointers to timers. */
   1174 struct ptimers *
   1175 timers_alloc(struct proc *p)
   1176 {
   1177 	struct ptimers *pts;
   1178 	int i;
   1179 
   1180 	pts = pool_get(&ptimers_pool, PR_WAITOK);
   1181 	LIST_INIT(&pts->pts_virtual);
   1182 	LIST_INIT(&pts->pts_prof);
   1183 	for (i = 0; i < TIMER_MAX; i++)
   1184 		pts->pts_timers[i] = NULL;
   1185 	pts->pts_fired = 0;
   1186 	mutex_spin_enter(&timer_lock);
   1187 	if (p->p_timers == NULL) {
   1188 		p->p_timers = pts;
   1189 		mutex_spin_exit(&timer_lock);
   1190 		return pts;
   1191 	}
   1192 	mutex_spin_exit(&timer_lock);
   1193 	pool_put(&ptimers_pool, pts);
   1194 	return p->p_timers;
   1195 }
   1196 
   1197 /*
   1198  * Clean up the per-process timers. If "which" is set to TIMERS_ALL,
   1199  * then clean up all timers and free all the data structures. If
   1200  * "which" is set to TIMERS_POSIX, only clean up the timers allocated
   1201  * by timer_create(), not the BSD setitimer() timers, and only free the
   1202  * structure if none of those remain.
   1203  */
   1204 void
   1205 timers_free(struct proc *p, int which)
   1206 {
   1207 	struct ptimers *pts;
   1208 	struct ptimer *ptn;
   1209 	struct timespec ts;
   1210 	int i;
   1211 
   1212 	if (p->p_timers == NULL)
   1213 		return;
   1214 
   1215 	pts = p->p_timers;
   1216 	mutex_spin_enter(&timer_lock);
   1217 	if (which == TIMERS_ALL) {
   1218 		p->p_timers = NULL;
   1219 		i = 0;
   1220 	} else {
   1221 		timespecclear(&ts);
   1222 		for (ptn = LIST_FIRST(&pts->pts_virtual);
   1223 		     ptn && ptn != pts->pts_timers[ITIMER_VIRTUAL];
   1224 		     ptn = LIST_NEXT(ptn, pt_list)) {
   1225 			KASSERT(ptn->pt_type != CLOCK_REALTIME);
   1226 			timespecadd(&ts, &ptn->pt_time.it_value, &ts);
   1227 		}
   1228 		LIST_FIRST(&pts->pts_virtual) = NULL;
   1229 		if (ptn) {
   1230 			KASSERT(ptn->pt_type != CLOCK_REALTIME);
   1231 			timespecadd(&ts, &ptn->pt_time.it_value,
   1232 			    &ptn->pt_time.it_value);
   1233 			LIST_INSERT_HEAD(&pts->pts_virtual, ptn, pt_list);
   1234 		}
   1235 		timespecclear(&ts);
   1236 		for (ptn = LIST_FIRST(&pts->pts_prof);
   1237 		     ptn && ptn != pts->pts_timers[ITIMER_PROF];
   1238 		     ptn = LIST_NEXT(ptn, pt_list)) {
   1239 			KASSERT(ptn->pt_type != CLOCK_REALTIME);
   1240 			timespecadd(&ts, &ptn->pt_time.it_value, &ts);
   1241 		}
   1242 		LIST_FIRST(&pts->pts_prof) = NULL;
   1243 		if (ptn) {
   1244 			KASSERT(ptn->pt_type != CLOCK_REALTIME);
   1245 			timespecadd(&ts, &ptn->pt_time.it_value,
   1246 			    &ptn->pt_time.it_value);
   1247 			LIST_INSERT_HEAD(&pts->pts_prof, ptn, pt_list);
   1248 		}
   1249 		i = 3;
   1250 	}
   1251 	for ( ; i < TIMER_MAX; i++) {
   1252 		if (pts->pts_timers[i] != NULL) {
   1253 			itimerfree(pts, i);
   1254 			mutex_spin_enter(&timer_lock);
   1255 		}
   1256 	}
   1257 	if (pts->pts_timers[0] == NULL && pts->pts_timers[1] == NULL &&
   1258 	    pts->pts_timers[2] == NULL) {
   1259 		p->p_timers = NULL;
   1260 		mutex_spin_exit(&timer_lock);
   1261 		pool_put(&ptimers_pool, pts);
   1262 	} else
   1263 		mutex_spin_exit(&timer_lock);
   1264 }
   1265 
   1266 static void
   1267 itimerfree(struct ptimers *pts, int index)
   1268 {
   1269 	struct ptimer *pt;
   1270 
   1271 	KASSERT(mutex_owned(&timer_lock));
   1272 
   1273 	pt = pts->pts_timers[index];
   1274 	pts->pts_timers[index] = NULL;
   1275 	if (pt->pt_type == CLOCK_REALTIME)
   1276 		callout_halt(&pt->pt_ch, &timer_lock);
   1277 	else if (pt->pt_queued)
   1278 		TAILQ_REMOVE(&timer_queue, pt, pt_chain);
   1279 	mutex_spin_exit(&timer_lock);
   1280 	if (pt->pt_type == CLOCK_REALTIME)
   1281 		callout_destroy(&pt->pt_ch);
   1282 	pool_put(&ptimer_pool, pt);
   1283 }
   1284 
   1285 /*
   1286  * Decrement an interval timer by a specified number
   1287  * of nanoseconds, which must be less than a second,
   1288  * i.e. < 1000000000.  If the timer expires, then reload
   1289  * it.  In this case, carry over (nsec - old value) to
   1290  * reduce the value reloaded into the timer so that
   1291  * the timer does not drift.  This routine assumes
   1292  * that it is called in a context where the timers
   1293  * on which it is operating cannot change in value.
   1294  */
   1295 static int
   1296 itimerdecr(struct ptimer *pt, int nsec)
   1297 {
   1298 	struct itimerspec *itp;
   1299 
   1300 	KASSERT(mutex_owned(&timer_lock));
   1301 
   1302 	itp = &pt->pt_time;
   1303 	if (itp->it_value.tv_nsec < nsec) {
   1304 		if (itp->it_value.tv_sec == 0) {
   1305 			/* expired, and already in next interval */
   1306 			nsec -= itp->it_value.tv_nsec;
   1307 			goto expire;
   1308 		}
   1309 		itp->it_value.tv_nsec += 1000000000;
   1310 		itp->it_value.tv_sec--;
   1311 	}
   1312 	itp->it_value.tv_nsec -= nsec;
   1313 	nsec = 0;
   1314 	if (timespecisset(&itp->it_value))
   1315 		return (1);
   1316 	/* expired, exactly at end of interval */
   1317 expire:
   1318 	if (timespecisset(&itp->it_interval)) {
   1319 		itp->it_value = itp->it_interval;
   1320 		itp->it_value.tv_nsec -= nsec;
   1321 		if (itp->it_value.tv_nsec < 0) {
   1322 			itp->it_value.tv_nsec += 1000000000;
   1323 			itp->it_value.tv_sec--;
   1324 		}
   1325 		timer_settime(pt);
   1326 	} else
   1327 		itp->it_value.tv_nsec = 0;		/* sec is already 0 */
   1328 	return (0);
   1329 }
   1330 
   1331 static void
   1332 itimerfire(struct ptimer *pt)
   1333 {
   1334 
   1335 	KASSERT(mutex_owned(&timer_lock));
   1336 
   1337 	/*
   1338 	 * XXX Can overrun, but we don't do signal queueing yet, anyway.
   1339 	 * XXX Relying on the clock interrupt is stupid.
   1340 	 */
   1341 	if ((pt->pt_ev.sigev_notify == SIGEV_SA && pt->pt_proc->p_sa == NULL) ||
   1342 	    (pt->pt_ev.sigev_notify != SIGEV_SIGNAL &&
   1343 	    pt->pt_ev.sigev_notify != SIGEV_SA) || pt->pt_queued)
   1344 		return;
   1345 	TAILQ_INSERT_TAIL(&timer_queue, pt, pt_chain);
   1346 	pt->pt_queued = true;
   1347 	softint_schedule(timer_sih);
   1348 }
   1349 
   1350 void
   1351 timer_tick(lwp_t *l, bool user)
   1352 {
   1353 	struct ptimers *pts;
   1354 	struct ptimer *pt;
   1355 	proc_t *p;
   1356 
   1357 	p = l->l_proc;
   1358 	if (p->p_timers == NULL)
   1359 		return;
   1360 
   1361 	mutex_spin_enter(&timer_lock);
   1362 	if ((pts = l->l_proc->p_timers) != NULL) {
   1363 		/*
   1364 		 * Run current process's virtual and profile time, as needed.
   1365 		 */
   1366 		if (user && (pt = LIST_FIRST(&pts->pts_virtual)) != NULL)
   1367 			if (itimerdecr(pt, tick * 1000) == 0)
   1368 				itimerfire(pt);
   1369 		if ((pt = LIST_FIRST(&pts->pts_prof)) != NULL)
   1370 			if (itimerdecr(pt, tick * 1000) == 0)
   1371 				itimerfire(pt);
   1372 	}
   1373 	mutex_spin_exit(&timer_lock);
   1374 }
   1375 
   1376 #ifdef KERN_SA
   1377 /*
   1378  * timer_sa_intr:
   1379  *
   1380  *	SIGEV_SA handling for timer_intr(). We are called (and return)
   1381  * with the timer lock held. We know that the process had SA enabled
   1382  * when this timer was enqueued. As timer_intr() is a soft interrupt
   1383  * handler, SA should still be enabled by the time we get here.
   1384  */
   1385 static void
   1386 timer_sa_intr(struct ptimer *pt, proc_t *p)
   1387 {
   1388 	unsigned int		i;
   1389 	struct sadata		*sa;
   1390 	struct sadata_vp	*vp;
   1391 
   1392 	/* Cause the process to generate an upcall when it returns. */
   1393 	if (!p->p_timerpend) {
   1394 		/*
   1395 		 * XXX stop signals can be processed inside tsleep,
   1396 		 * which can be inside sa_yield's inner loop, which
   1397 		 * makes testing for sa_idle alone insuffucent to
   1398 		 * determine if we really should call setrunnable.
   1399 		 */
   1400 		pt->pt_poverruns = pt->pt_overruns;
   1401 		pt->pt_overruns = 0;
   1402 		i = 1 << pt->pt_entry;
   1403 		p->p_timers->pts_fired = i;
   1404 		p->p_timerpend = 1;
   1405 
   1406 		sa = p->p_sa;
   1407 		mutex_enter(&sa->sa_mutex);
   1408 		SLIST_FOREACH(vp, &sa->sa_vps, savp_next) {
   1409 			struct lwp *vp_lwp = vp->savp_lwp;
   1410 			lwp_lock(vp_lwp);
   1411 			lwp_need_userret(vp_lwp);
   1412 			if (vp_lwp->l_flag & LW_SA_IDLE) {
   1413 				vp_lwp->l_flag &= ~LW_SA_IDLE;
   1414 				lwp_unsleep(vp_lwp, true);
   1415 				break;
   1416 			}
   1417 			lwp_unlock(vp_lwp);
   1418 		}
   1419 		mutex_exit(&sa->sa_mutex);
   1420 	} else {
   1421 		i = 1 << pt->pt_entry;
   1422 		if ((p->p_timers->pts_fired & i) == 0) {
   1423 			pt->pt_poverruns = pt->pt_overruns;
   1424 			pt->pt_overruns = 0;
   1425 			p->p_timers->pts_fired |= i;
   1426 		} else
   1427 			pt->pt_overruns++;
   1428 	}
   1429 }
   1430 #endif /* KERN_SA */
   1431 
   1432 static void
   1433 timer_intr(void *cookie)
   1434 {
   1435 	ksiginfo_t ksi;
   1436 	struct ptimer *pt;
   1437 	proc_t *p;
   1438 
   1439 	mutex_spin_enter(&timer_lock);
   1440 	while ((pt = TAILQ_FIRST(&timer_queue)) != NULL) {
   1441 		TAILQ_REMOVE(&timer_queue, pt, pt_chain);
   1442 		KASSERT(pt->pt_queued);
   1443 		pt->pt_queued = false;
   1444 
   1445 		if (pt->pt_proc->p_timers == NULL) {
   1446 			/* Process is dying. */
   1447 			continue;
   1448 		}
   1449 		p = pt->pt_proc;
   1450 #ifdef KERN_SA
   1451 		if (pt->pt_ev.sigev_notify == SIGEV_SA) {
   1452 			timer_sa_intr(pt, p);
   1453 			continue;
   1454 		}
   1455 #endif /* KERN_SA */
   1456 		if (pt->pt_ev.sigev_notify != SIGEV_SIGNAL)
   1457 			continue;
   1458 		if (sigismember(&p->p_sigpend.sp_set, pt->pt_ev.sigev_signo)) {
   1459 			pt->pt_overruns++;
   1460 			continue;
   1461 		}
   1462 
   1463 		KSI_INIT(&ksi);
   1464 		ksi.ksi_signo = pt->pt_ev.sigev_signo;
   1465 		ksi.ksi_code = SI_TIMER;
   1466 		ksi.ksi_value = pt->pt_ev.sigev_value;
   1467 		pt->pt_poverruns = pt->pt_overruns;
   1468 		pt->pt_overruns = 0;
   1469 		mutex_spin_exit(&timer_lock);
   1470 
   1471 		mutex_enter(proc_lock);
   1472 		kpsignal(p, &ksi, NULL);
   1473 		mutex_exit(proc_lock);
   1474 
   1475 		mutex_spin_enter(&timer_lock);
   1476 	}
   1477 	mutex_spin_exit(&timer_lock);
   1478 }
   1479