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