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