Home | History | Annotate | Line # | Download | only in kern
      1 /*	$NetBSD: kern_time.c,v 1.231 2026/02/07 01:47:23 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000, 2004, 2005, 2007, 2008, 2009, 2020
      5  *     The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Christopher G. Demetriou, by Andrew Doran, and by Jason R. Thorpe.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1982, 1986, 1989, 1993
     35  *	The Regents of the University of California.  All rights reserved.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  * 3. Neither the name of the University nor the names of its contributors
     46  *    may be used to endorse or promote products derived from this software
     47  *    without specific prior written permission.
     48  *
     49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59  * SUCH DAMAGE.
     60  *
     61  *	@(#)kern_time.c	8.4 (Berkeley) 5/26/95
     62  */
     63 
     64 #include <sys/cdefs.h>
     65 __KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.231 2026/02/07 01:47:23 riastradh Exp $");
     66 
     67 #include <sys/param.h>
     68 #include <sys/types.h>
     69 
     70 #include <sys/callout.h>
     71 #include <sys/cpu.h>
     72 #include <sys/errno.h>
     73 #include <sys/intr.h>
     74 #include <sys/kauth.h>
     75 #include <sys/kernel.h>
     76 #include <sys/kmem.h>
     77 #include <sys/lwp.h>
     78 #include <sys/mount.h>
     79 #include <sys/mutex.h>
     80 #include <sys/proc.h>
     81 #include <sys/queue.h>
     82 #include <sys/resourcevar.h>
     83 #include <sys/sdt.h>
     84 #include <sys/signal.h>
     85 #include <sys/signalvar.h>
     86 #include <sys/syscallargs.h>
     87 #include <sys/syslog.h>
     88 #include <sys/systm.h>
     89 #include <sys/timetc.h>
     90 #include <sys/timevar.h>
     91 #include <sys/timex.h>
     92 #include <sys/vnode.h>
     93 
     94 #include <machine/limits.h>
     95 
     96 kmutex_t	itimer_mutex __cacheline_aligned;	/* XXX static */
     97 static struct itlist itimer_realtime_changed_notify;
     98 
     99 static void	itimer_callout(void *);
    100 static void	ptimer_intr(void *);
    101 static void	*ptimer_sih __read_mostly;
    102 static TAILQ_HEAD(, ptimer) ptimer_queue;
    103 
    104 #define	CLOCK_VIRTUAL_P(clockid)	\
    105 	((clockid) == CLOCK_VIRTUAL || (clockid) == CLOCK_PROF)
    106 
    107 CTASSERT(ITIMER_REAL == CLOCK_REALTIME);
    108 CTASSERT(ITIMER_VIRTUAL == CLOCK_VIRTUAL);
    109 CTASSERT(ITIMER_PROF == CLOCK_PROF);
    110 CTASSERT(ITIMER_MONOTONIC == CLOCK_MONOTONIC);
    111 
    112 /*
    113  * Initialize timekeeping.
    114  */
    115 void
    116 time_init(void)
    117 {
    118 
    119 	mutex_init(&itimer_mutex, MUTEX_DEFAULT, IPL_SCHED);
    120 	LIST_INIT(&itimer_realtime_changed_notify);
    121 
    122 	TAILQ_INIT(&ptimer_queue);
    123 	ptimer_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
    124 	    ptimer_intr, NULL);
    125 }
    126 
    127 /*
    128  * Check if the time will wrap if set to ts.
    129  *
    130  * ts - timespec describing the new time
    131  * delta - the delta between the current time and ts
    132  */
    133 bool
    134 time_wraps(struct timespec *ts, struct timespec *delta)
    135 {
    136 
    137 	/*
    138 	 * Don't allow the time to be set forward so far it
    139 	 * will wrap and become negative, thus allowing an
    140 	 * attacker to bypass the next check below.  The
    141 	 * cutoff is 1 year before rollover occurs, so even
    142 	 * if the attacker uses adjtime(2) to move the time
    143 	 * past the cutoff, it will take a very long time
    144 	 * to get to the wrap point.
    145 	 */
    146 	if ((ts->tv_sec > LLONG_MAX - 365*24*60*60) ||
    147 	    (delta->tv_sec < 0 || delta->tv_nsec < 0))
    148 		return true;
    149 
    150 	return false;
    151 }
    152 
    153 /*
    154  * itimer_lock:
    155  *
    156  *	Acquire the interval timer data lock.
    157  */
    158 void
    159 itimer_lock(void)
    160 {
    161 	mutex_spin_enter(&itimer_mutex);
    162 }
    163 
    164 /*
    165  * itimer_unlock:
    166  *
    167  *	Release the interval timer data lock.
    168  */
    169 void
    170 itimer_unlock(void)
    171 {
    172 	mutex_spin_exit(&itimer_mutex);
    173 }
    174 
    175 /*
    176  * itimer_lock_held:
    177  *
    178  *	Check that the interval timer lock is held for diagnostic
    179  *	assertions.
    180  */
    181 inline bool __diagused
    182 itimer_lock_held(void)
    183 {
    184 	return mutex_owned(&itimer_mutex);
    185 }
    186 
    187 /*
    188  * Time of day and interval timer support.
    189  *
    190  * These routines provide the kernel entry points to get and set
    191  * the time-of-day and per-process interval timers.  Subroutines
    192  * here provide support for adding and subtracting timeval structures
    193  * and decrementing interval timers, optionally reloading the interval
    194  * timers when they expire.
    195  */
    196 
    197 /* This function is used by clock_settime and settimeofday */
    198 static int
    199 settime1(struct proc *p, const struct timespec *ts, bool check_kauth)
    200 {
    201 	struct timespec delta, now;
    202 
    203 	/*
    204 	 * The time being set to an unreasonable value will cause
    205 	 * unreasonable system behaviour.
    206 	 */
    207 	if (ts->tv_sec < 0 || ts->tv_sec > (1LL << 36))
    208 		return SET_ERROR(EINVAL);
    209 
    210 	nanotime(&now);
    211 	timespecsub(ts, &now, &delta);
    212 
    213 	if (check_kauth && kauth_authorize_system(kauth_cred_get(),
    214 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_SYSTEM, __UNCONST(ts),
    215 	    &delta, KAUTH_ARG(check_kauth ? false : true)) != 0) {
    216 		return SET_ERROR(EPERM);
    217 	}
    218 
    219 #ifdef notyet
    220 	if ((delta.tv_sec < 86400) && securelevel > 0) { /* XXX elad - notyet */
    221 		return SET_ERROR(EPERM);
    222 	}
    223 #endif
    224 
    225 	tc_setclock(ts);
    226 
    227 	resettodr();
    228 
    229 	/*
    230 	 * Notify pending CLOCK_REALTIME timers about the real time change.
    231 	 * There may be inactive timers on this list, but this happens
    232 	 * comparatively less often than timers firing, and so it's better
    233 	 * to put the extra checks here than to complicate the other code
    234 	 * path.
    235 	 */
    236 	struct itimer *it;
    237 	itimer_lock();
    238 	LIST_FOREACH(it, &itimer_realtime_changed_notify, it_rtchgq) {
    239 		KASSERT(it->it_ops->ito_realtime_changed != NULL);
    240 		if (timespecisset(&it->it_time.it_value)) {
    241 			(*it->it_ops->ito_realtime_changed)(it);
    242 		}
    243 	}
    244 	itimer_unlock();
    245 
    246 	return 0;
    247 }
    248 
    249 int
    250 settime(struct proc *p, struct timespec *ts)
    251 {
    252 	return settime1(p, ts, true);
    253 }
    254 
    255 /* ARGSUSED */
    256 int
    257 sys___clock_gettime50(struct lwp *l,
    258     const struct sys___clock_gettime50_args *uap, register_t *retval)
    259 {
    260 	/* {
    261 		syscallarg(clockid_t) clock_id;
    262 		syscallarg(struct timespec *) tp;
    263 	} */
    264 	int error;
    265 	struct timespec ats;
    266 
    267 	error = clock_gettime1(SCARG(uap, clock_id), &ats);
    268 	if (error != 0)
    269 		return error;
    270 
    271 	return copyout(&ats, SCARG(uap, tp), sizeof(ats));
    272 }
    273 
    274 /* ARGSUSED */
    275 int
    276 sys___clock_settime50(struct lwp *l,
    277     const struct sys___clock_settime50_args *uap, register_t *retval)
    278 {
    279 	/* {
    280 		syscallarg(clockid_t) clock_id;
    281 		syscallarg(const struct timespec *) tp;
    282 	} */
    283 	int error;
    284 	struct timespec ats;
    285 
    286 	if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
    287 		return error;
    288 
    289 	return clock_settime1(l->l_proc, SCARG(uap, clock_id), &ats, true);
    290 }
    291 
    292 
    293 int
    294 clock_settime1(struct proc *p, clockid_t clock_id, const struct timespec *tp,
    295     bool check_kauth)
    296 {
    297 	int error;
    298 
    299 	if (tp->tv_nsec < 0 || tp->tv_nsec >= 1000000000L)
    300 		return SET_ERROR(EINVAL);
    301 
    302 	switch (clock_id) {
    303 	case CLOCK_REALTIME:
    304 		if ((error = settime1(p, tp, check_kauth)) != 0)
    305 			return error;
    306 		break;
    307 	case CLOCK_MONOTONIC:
    308 		return SET_ERROR(EINVAL);	/* read-only clock */
    309 	default:
    310 		return SET_ERROR(EINVAL);
    311 	}
    312 
    313 	return 0;
    314 }
    315 
    316 int
    317 sys___clock_getres50(struct lwp *l, const struct sys___clock_getres50_args *uap,
    318     register_t *retval)
    319 {
    320 	/* {
    321 		syscallarg(clockid_t) clock_id;
    322 		syscallarg(struct timespec *) tp;
    323 	} */
    324 	struct timespec ts;
    325 	int error;
    326 
    327 	if ((error = clock_getres1(SCARG(uap, clock_id), &ts)) != 0)
    328 		return error;
    329 
    330 	if (SCARG(uap, tp))
    331 		error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
    332 
    333 	return error;
    334 }
    335 
    336 int
    337 clock_getres1(clockid_t clock_id, struct timespec *ts)
    338 {
    339 
    340 	switch (clock_id) {
    341 	case CLOCK_REALTIME:
    342 	case CLOCK_MONOTONIC:
    343 	case CLOCK_PROCESS_CPUTIME_ID:
    344 	case CLOCK_THREAD_CPUTIME_ID:
    345 		ts->tv_sec = 0;
    346 		if (tc_getfrequency() > 1000000000)
    347 			ts->tv_nsec = 1;
    348 		else
    349 			ts->tv_nsec = 1000000000 / tc_getfrequency();
    350 		break;
    351 	default:
    352 		return SET_ERROR(EINVAL);
    353 	}
    354 
    355 	return 0;
    356 }
    357 
    358 /* ARGSUSED */
    359 int
    360 sys___nanosleep50(struct lwp *l, const struct sys___nanosleep50_args *uap,
    361     register_t *retval)
    362 {
    363 	/* {
    364 		syscallarg(struct timespec *) rqtp;
    365 		syscallarg(struct timespec *) rmtp;
    366 	} */
    367 	struct timespec rmt, rqt;
    368 	int error, error1;
    369 
    370 	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(struct timespec));
    371 	if (error)
    372 		return error;
    373 
    374 	error = nanosleep1(l, CLOCK_MONOTONIC, 0, &rqt,
    375 	    SCARG(uap, rmtp) ? &rmt : NULL);
    376 	if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
    377 		return error;
    378 
    379 	error1 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
    380 	return error1 ? error1 : error;
    381 }
    382 
    383 /* ARGSUSED */
    384 int
    385 sys_clock_nanosleep(struct lwp *l, const struct sys_clock_nanosleep_args *uap,
    386     register_t *retval)
    387 {
    388 	/* {
    389 		syscallarg(clockid_t) clock_id;
    390 		syscallarg(int) flags;
    391 		syscallarg(struct timespec *) rqtp;
    392 		syscallarg(struct timespec *) rmtp;
    393 	} */
    394 	struct timespec rmt, rqt;
    395 	int error, error1;
    396 
    397 	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(struct timespec));
    398 	if (error)
    399 		goto out;
    400 
    401 	error = nanosleep1(l, SCARG(uap, clock_id), SCARG(uap, flags), &rqt,
    402 	    SCARG(uap, rmtp) ? &rmt : NULL);
    403 	if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
    404 		goto out;
    405 
    406 	if ((SCARG(uap, flags) & TIMER_ABSTIME) == 0 &&
    407 	    (error1 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt))) != 0)
    408 		error = error1;
    409 out:
    410 	*retval = error;
    411 	return 0;
    412 }
    413 
    414 int
    415 nanosleep1(struct lwp *l, clockid_t clock_id, int flags, struct timespec *rqt,
    416     struct timespec *rmt)
    417 {
    418 	struct timespec rmtstart;
    419 	int error, timo;
    420 
    421 	if ((error = ts2timo(clock_id, flags, rqt, &timo, &rmtstart)) != 0) {
    422 		if (error == ETIMEDOUT) {
    423 			error = 0;
    424 			if (rmt != NULL)
    425 				rmt->tv_sec = rmt->tv_nsec = 0;
    426 		}
    427 		return error;
    428 	}
    429 
    430 	/*
    431 	 * Avoid inadvertently sleeping forever
    432 	 */
    433 	if (timo == 0)
    434 		timo = 1;
    435 again:
    436 	error = kpause("nanoslp", true, timo, NULL);
    437 	if (error == EWOULDBLOCK)
    438 		error = 0;
    439 	if (rmt != NULL || error == 0) {
    440 		struct timespec rmtend;
    441 		struct timespec t0;
    442 		struct timespec *t;
    443 		int err;
    444 
    445 		err = clock_gettime1(clock_id, &rmtend);
    446 		if (err != 0)
    447 			return err;
    448 
    449 		t = (rmt != NULL) ? rmt : &t0;
    450 		if (flags & TIMER_ABSTIME) {
    451 			timespecsub(rqt, &rmtend, t);
    452 		} else {
    453 			if (timespeccmp(&rmtend, &rmtstart, <))
    454 				timespecclear(t); /* clock wound back */
    455 			else
    456 				timespecsub(&rmtend, &rmtstart, t);
    457 			if (timespeccmp(rqt, t, <))
    458 				timespecclear(t);
    459 			else
    460 				timespecsub(rqt, t, t);
    461 		}
    462 		if (t->tv_sec < 0)
    463 			timespecclear(t);
    464 		if (error == 0) {
    465 			timo = tstohz(t);
    466 			if (timo > 0)
    467 				goto again;
    468 		}
    469 	}
    470 
    471 	if (error == ERESTART)
    472 		error = SET_ERROR(EINTR);
    473 
    474 	return error;
    475 }
    476 
    477 int
    478 sys_clock_getcpuclockid2(struct lwp *l,
    479     const struct sys_clock_getcpuclockid2_args *uap,
    480     register_t *retval)
    481 {
    482 	/* {
    483 		syscallarg(idtype_t idtype;
    484 		syscallarg(id_t id);
    485 		syscallarg(clockid_t *)clock_id;
    486 	} */
    487 	pid_t pid;
    488 	lwpid_t lid;
    489 	clockid_t clock_id;
    490 	id_t id = SCARG(uap, id);
    491 
    492 	switch (SCARG(uap, idtype)) {
    493 	case P_PID:
    494 		pid = id == 0 ? l->l_proc->p_pid : id;
    495 		clock_id = CLOCK_PROCESS_CPUTIME_ID | pid;
    496 		break;
    497 	case P_LWPID:
    498 		lid = id == 0 ? l->l_lid : id;
    499 		clock_id = CLOCK_THREAD_CPUTIME_ID | lid;
    500 		break;
    501 	default:
    502 		return SET_ERROR(EINVAL);
    503 	}
    504 	return copyout(&clock_id, SCARG(uap, clock_id), sizeof(clock_id));
    505 }
    506 
    507 /* ARGSUSED */
    508 int
    509 sys___gettimeofday50(struct lwp *l, const struct sys___gettimeofday50_args *uap,
    510     register_t *retval)
    511 {
    512 	/* {
    513 		syscallarg(struct timeval *) tp;
    514 		syscallarg(void *) tzp;		really "struct timezone *";
    515 	} */
    516 	struct timeval atv;
    517 	int error = 0;
    518 	struct timezone tzfake;
    519 
    520 	if (SCARG(uap, tp)) {
    521 		memset(&atv, 0, sizeof(atv));
    522 		microtime(&atv);
    523 		error = copyout(&atv, SCARG(uap, tp), sizeof(atv));
    524 		if (error)
    525 			return error;
    526 	}
    527 	if (SCARG(uap, tzp)) {
    528 		/*
    529 		 * NetBSD has no kernel notion of time zone, so we just
    530 		 * fake up a timezone struct and return it if demanded.
    531 		 */
    532 		tzfake.tz_minuteswest = 0;
    533 		tzfake.tz_dsttime = 0;
    534 		error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
    535 	}
    536 	return error;
    537 }
    538 
    539 /* ARGSUSED */
    540 int
    541 sys___settimeofday50(struct lwp *l, const struct sys___settimeofday50_args *uap,
    542     register_t *retval)
    543 {
    544 	/* {
    545 		syscallarg(const struct timeval *) tv;
    546 		syscallarg(const void *) tzp; really "const struct timezone *";
    547 	} */
    548 
    549 	return settimeofday1(SCARG(uap, tv), true, SCARG(uap, tzp), l, true);
    550 }
    551 
    552 int
    553 settimeofday1(const struct timeval *utv, bool userspace,
    554     const void *utzp, struct lwp *l, bool check_kauth)
    555 {
    556 	struct timeval atv;
    557 	struct timespec ts;
    558 	int error;
    559 
    560 	/* Verify all parameters before changing time. */
    561 
    562 	/*
    563 	 * NetBSD has no kernel notion of time zone, and only an
    564 	 * obsolete program would try to set it, so we log a warning.
    565 	 */
    566 	if (utzp)
    567 		log(LOG_WARNING, "pid %d attempted to set the "
    568 		    "(obsolete) kernel time zone\n", l->l_proc->p_pid);
    569 
    570 	if (utv == NULL)
    571 		return 0;
    572 
    573 	if (userspace) {
    574 		if ((error = copyin(utv, &atv, sizeof(atv))) != 0)
    575 			return error;
    576 		utv = &atv;
    577 	}
    578 
    579 	if (utv->tv_usec < 0 || utv->tv_usec >= 1000000)
    580 		return SET_ERROR(EINVAL);
    581 
    582 	TIMEVAL_TO_TIMESPEC(utv, &ts);
    583 	return settime1(l->l_proc, &ts, check_kauth);
    584 }
    585 
    586 int	time_adjusted;			/* set if an adjustment is made */
    587 
    588 /* ARGSUSED */
    589 int
    590 sys___adjtime50(struct lwp *l, const struct sys___adjtime50_args *uap,
    591     register_t *retval)
    592 {
    593 	/* {
    594 		syscallarg(const struct timeval *) delta;
    595 		syscallarg(struct timeval *) olddelta;
    596 	} */
    597 	int error;
    598 	struct timeval atv, oldatv;
    599 
    600 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
    601 	    KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL, NULL)) != 0)
    602 		return error;
    603 
    604 	if (SCARG(uap, delta)) {
    605 		error = copyin(SCARG(uap, delta), &atv,
    606 		    sizeof(*SCARG(uap, delta)));
    607 		if (error)
    608 			return error;
    609 	}
    610 	adjtime1(SCARG(uap, delta) ? &atv : NULL,
    611 	    SCARG(uap, olddelta) ? &oldatv : NULL, l->l_proc);
    612 	if (SCARG(uap, olddelta))
    613 		error = copyout(&oldatv, SCARG(uap, olddelta),
    614 		    sizeof(*SCARG(uap, olddelta)));
    615 	return error;
    616 }
    617 
    618 void
    619 adjtime1(const struct timeval *delta, struct timeval *olddelta, struct proc *p)
    620 {
    621 
    622 	if (olddelta) {
    623 		memset(olddelta, 0, sizeof(*olddelta));
    624 		mutex_spin_enter(&timecounter_lock);
    625 		olddelta->tv_sec = time_adjtime / 1000000;
    626 		olddelta->tv_usec = time_adjtime % 1000000;
    627 		if (olddelta->tv_usec < 0) {
    628 			olddelta->tv_usec += 1000000;
    629 			olddelta->tv_sec--;
    630 		}
    631 		mutex_spin_exit(&timecounter_lock);
    632 	}
    633 
    634 	if (delta) {
    635 		mutex_spin_enter(&timecounter_lock);
    636 		/*
    637 		 * XXX This should maybe just report failure to
    638 		 * userland for nonsense deltas.
    639 		 */
    640 		if (delta->tv_sec > INT64_MAX/1000000 - 1) {
    641 			time_adjtime = INT64_MAX;
    642 		} else if (delta->tv_sec < INT64_MIN/1000000 + 1) {
    643 			time_adjtime = INT64_MIN;
    644 		} else {
    645 			time_adjtime = delta->tv_sec * 1000000
    646 			    + MAX(-999999, MIN(999999, delta->tv_usec));
    647 		}
    648 
    649 		if (time_adjtime) {
    650 			/* We need to save the system time during shutdown */
    651 			time_adjusted |= 1;
    652 		}
    653 		mutex_spin_exit(&timecounter_lock);
    654 	}
    655 }
    656 
    657 /*
    658  * Interval timer support.
    659  *
    660  * The itimer_*() routines provide generic support for interval timers,
    661  * both real (CLOCK_REALTIME, CLOCK_MONOTIME), and virtual (CLOCK_VIRTUAL,
    662  * CLOCK_PROF).
    663  *
    664  * Real timers keep their deadline as an absolute time, and are fired
    665  * by a callout.  Virtual timers are kept as a linked-list of deltas,
    666  * and are processed by hardclock().
    667  *
    668  * Because the real time timer callout may be delayed in real time due
    669  * to interrupt processing on the system, it is possible for the real
    670  * time timeout routine (itimer_callout()) run past after its deadline.
    671  * It does not suffice, therefore, to reload the real timer .it_value
    672  * from the timer's .it_interval.  Rather, we compute the next deadline
    673  * in absolute time based on the current time and the .it_interval value,
    674  * and report any overruns.
    675  *
    676  * Note that while the virtual timers are supported in a generic fashion
    677  * here, they only (currently) make sense as per-process timers, and thus
    678  * only really work for that case.
    679  */
    680 
    681 /*
    682  * itimer_init:
    683  *
    684  *	Initialize the common data for an interval timer.
    685  */
    686 void
    687 itimer_init(struct itimer * const it, const struct itimer_ops * const ops,
    688     clockid_t const id, struct itlist * const itl)
    689 {
    690 
    691 	KASSERT(itimer_lock_held());
    692 	KASSERT(ops != NULL);
    693 
    694 	timespecclear(&it->it_time.it_value);
    695 	it->it_ops = ops;
    696 	it->it_clockid = id;
    697 	it->it_overruns = 0;
    698 	it->it_dying = false;
    699 	if (!CLOCK_VIRTUAL_P(id)) {
    700 		KASSERT(itl == NULL);
    701 		callout_init(&it->it_ch, CALLOUT_MPSAFE);
    702 		callout_setfunc(&it->it_ch, itimer_callout, it);
    703 		if (id == CLOCK_REALTIME && ops->ito_realtime_changed != NULL) {
    704 			LIST_INSERT_HEAD(&itimer_realtime_changed_notify,
    705 			    it, it_rtchgq);
    706 		}
    707 	} else {
    708 		KASSERT(itl != NULL);
    709 		it->it_vlist = itl;
    710 		it->it_active = false;
    711 	}
    712 }
    713 
    714 /*
    715  * itimer_poison:
    716  *
    717  *	Poison an interval timer, preventing it from being scheduled
    718  *	or processed, in preparation for freeing the timer.
    719  */
    720 void
    721 itimer_poison(struct itimer * const it)
    722 {
    723 
    724 	KASSERT(itimer_lock_held());
    725 
    726 	it->it_dying = true;
    727 
    728 	/*
    729 	 * For non-virtual timers, stop the callout, or wait for it to
    730 	 * run if it has already fired.  It cannot restart again after
    731 	 * this point: the callout won't restart itself when dying, no
    732 	 * other users holding the lock can restart it, and any other
    733 	 * users waiting for callout_halt concurrently (itimer_settime)
    734 	 * will restart from the top.
    735 	 */
    736 	if (!CLOCK_VIRTUAL_P(it->it_clockid)) {
    737 		callout_halt(&it->it_ch, &itimer_mutex);
    738 		if (it->it_clockid == CLOCK_REALTIME &&
    739 		    it->it_ops->ito_realtime_changed != NULL) {
    740 			LIST_REMOVE(it, it_rtchgq);
    741 		}
    742 	}
    743 }
    744 
    745 /*
    746  * itimer_fini:
    747  *
    748  *	Release resources used by an interval timer.
    749  *
    750  *	N.B. itimer_lock must be held on entry, and is released on exit.
    751  */
    752 void
    753 itimer_fini(struct itimer * const it)
    754 {
    755 
    756 	KASSERT(itimer_lock_held());
    757 
    758 	/* All done with the global state. */
    759 	itimer_unlock();
    760 
    761 	/* Destroy the callout, if needed. */
    762 	if (!CLOCK_VIRTUAL_P(it->it_clockid))
    763 		callout_destroy(&it->it_ch);
    764 }
    765 
    766 /*
    767  * itimer_decr:
    768  *
    769  *	Decrement an interval timer by a specified number of nanoseconds,
    770  *	which must be less than a second, i.e. < 1000000000.  If the timer
    771  *	expires, then reload it.  In this case, carry over (nsec - old value)
    772  *	to reduce the value reloaded into the timer so that the timer does
    773  *	not drift.  This routine assumes that it is called in a context where
    774  *	the timers on which it is operating cannot change in value.
    775  *
    776  *	Returns true if the timer has expired.
    777  */
    778 static bool
    779 itimer_decr(struct itimer *it, int nsec)
    780 {
    781 	struct itimerspec *itp;
    782 	int error __diagused;
    783 
    784 	KASSERT(itimer_lock_held());
    785 	KASSERT(CLOCK_VIRTUAL_P(it->it_clockid));
    786 
    787 	itp = &it->it_time;
    788 	if (itp->it_value.tv_nsec < nsec) {
    789 		if (itp->it_value.tv_sec == 0) {
    790 			/* expired, and already in next interval */
    791 			nsec -= itp->it_value.tv_nsec;
    792 			goto expire;
    793 		}
    794 		itp->it_value.tv_nsec += 1000000000;
    795 		itp->it_value.tv_sec--;
    796 	}
    797 	itp->it_value.tv_nsec -= nsec;
    798 	nsec = 0;
    799 	if (timespecisset(&itp->it_value))
    800 		return false;
    801 	/* expired, exactly at end of interval */
    802  expire:
    803 	if (timespecisset(&itp->it_interval)) {
    804 		itp->it_value = itp->it_interval;
    805 		itp->it_value.tv_nsec -= nsec;
    806 		if (itp->it_value.tv_nsec < 0) {
    807 			itp->it_value.tv_nsec += 1000000000;
    808 			itp->it_value.tv_sec--;
    809 		}
    810 		error = itimer_settime(it);
    811 		KASSERT(error == 0); /* virtual, never fails */
    812 	} else
    813 		itp->it_value.tv_nsec = 0;		/* sec is already 0 */
    814 	return true;
    815 }
    816 
    817 /*
    818  * itimer_arm_real:
    819  *
    820  *	Arm a non-virtual timer.
    821  */
    822 static void
    823 itimer_arm_real(struct itimer * const it)
    824 {
    825 
    826 	KASSERT(!it->it_dying);
    827 	KASSERT(!CLOCK_VIRTUAL_P(it->it_clockid));
    828 	KASSERT(!callout_pending(&it->it_ch));
    829 
    830 	/*
    831 	 * Don't need to check tshzto() return value, here.
    832 	 * callout_schedule() does it for us.
    833 	 */
    834 	callout_schedule(&it->it_ch,
    835 	    (it->it_clockid == CLOCK_MONOTONIC
    836 		? tshztoup(&it->it_time.it_value)
    837 		: tshzto(&it->it_time.it_value)));
    838 }
    839 
    840 /*
    841  * itimer_callout:
    842  *
    843  *	Callout to expire a non-virtual timer.  Queue it up for processing,
    844  *	and then reload, if it is configured to do so.
    845  *
    846  *	N.B. A delay in processing this callout causes multiple
    847  *	SIGALRM calls to be compressed into one.
    848  */
    849 static void
    850 itimer_callout(void *arg)
    851 {
    852 	struct timespec now, next;
    853 	struct itimer * const it = arg;
    854 	int overruns;
    855 
    856 	itimer_lock();
    857 	(*it->it_ops->ito_fire)(it);
    858 
    859 	if (!timespecisset(&it->it_time.it_interval)) {
    860 		timespecclear(&it->it_time.it_value);
    861 		itimer_unlock();
    862 		return;
    863 	}
    864 
    865 	if (it->it_clockid == CLOCK_MONOTONIC) {
    866 		getnanouptime(&now);
    867 	} else {
    868 		getnanotime(&now);
    869 	}
    870 
    871 	/*
    872 	 * Given the current itimer value and interval and the time
    873 	 * now, compute the next itimer value and count overruns.
    874 	 */
    875 	itimer_transition(&it->it_time, &now, &next, &overruns);
    876 	it->it_time.it_value = next;
    877 	it->it_overruns += MIN(INT_MAX - overruns, overruns);
    878 
    879 	/*
    880 	 * Reset the callout, if it's not going away.
    881 	 */
    882 	if (!it->it_dying)
    883 		itimer_arm_real(it);
    884 	itimer_unlock();
    885 }
    886 
    887 /*
    888  * itimer_settime:
    889  *
    890  *	Set up the given interval timer. The value in it->it_time.it_value
    891  *	is taken to be an absolute time for CLOCK_REALTIME/CLOCK_MONOTONIC
    892  *	timers and a relative time for CLOCK_VIRTUAL/CLOCK_PROF timers.
    893  *
    894  *	If the callout had already fired but not yet run, fails with
    895  *	ERESTART -- caller must restart from the top to look up a timer.
    896  *
    897  *	Caller is responsible for validating it->it_value and
    898  *	it->it_interval, e.g. with itimerfix or itimespecfix.
    899  */
    900 int
    901 itimer_settime(struct itimer *it)
    902 {
    903 	struct itimer *itn, *pitn;
    904 	struct itlist *itl;
    905 
    906 	KASSERT(itimer_lock_held());
    907 	KASSERT(!it->it_dying);
    908 	KASSERT(it->it_time.it_value.tv_sec >= 0);
    909 	KASSERT(it->it_time.it_value.tv_nsec >= 0);
    910 	KASSERT(it->it_time.it_value.tv_nsec < 1000000000);
    911 	KASSERT(it->it_time.it_interval.tv_sec >= 0);
    912 	KASSERT(it->it_time.it_interval.tv_nsec >= 0);
    913 	KASSERT(it->it_time.it_interval.tv_nsec < 1000000000);
    914 
    915 	if (!CLOCK_VIRTUAL_P(it->it_clockid)) {
    916 		/*
    917 		 * Try to stop the callout.  However, if it had already
    918 		 * fired, we have to drop the lock to wait for it, so
    919 		 * the world may have changed and pt may not be there
    920 		 * any more.  In that case, tell the caller to start
    921 		 * over from the top.
    922 		 */
    923 		if (callout_halt(&it->it_ch, &itimer_mutex))
    924 			return SET_ERROR(ERESTART);
    925 		KASSERT(!it->it_dying);
    926 
    927 		/* Now we can touch it and start it up again. */
    928 		if (timespecisset(&it->it_time.it_value))
    929 			itimer_arm_real(it);
    930 	} else {
    931 		if (it->it_active) {
    932 			itn = LIST_NEXT(it, it_list);
    933 			LIST_REMOVE(it, it_list);
    934 			for ( ; itn; itn = LIST_NEXT(itn, it_list))
    935 				timespecadd(&it->it_time.it_value,
    936 				    &itn->it_time.it_value,
    937 				    &itn->it_time.it_value);
    938 		}
    939 		if (timespecisset(&it->it_time.it_value)) {
    940 			itl = it->it_vlist;
    941 			for (itn = LIST_FIRST(itl), pitn = NULL;
    942 			     itn && timespeccmp(&it->it_time.it_value,
    943 				 &itn->it_time.it_value, >);
    944 			     pitn = itn, itn = LIST_NEXT(itn, it_list))
    945 				timespecsub(&it->it_time.it_value,
    946 				    &itn->it_time.it_value,
    947 				    &it->it_time.it_value);
    948 
    949 			if (pitn)
    950 				LIST_INSERT_AFTER(pitn, it, it_list);
    951 			else
    952 				LIST_INSERT_HEAD(itl, it, it_list);
    953 
    954 			for ( ; itn ; itn = LIST_NEXT(itn, it_list))
    955 				timespecsub(&itn->it_time.it_value,
    956 				    &it->it_time.it_value,
    957 				    &itn->it_time.it_value);
    958 
    959 			it->it_active = true;
    960 		} else {
    961 			it->it_active = false;
    962 		}
    963 	}
    964 
    965 	/* Success!  */
    966 	return 0;
    967 }
    968 
    969 /*
    970  * itimer_gettime:
    971  *
    972  *	Return the remaining time of an interval timer.
    973  */
    974 void
    975 itimer_gettime(const struct itimer *it, struct itimerspec *aits)
    976 {
    977 	struct timespec now;
    978 	struct itimer *itn;
    979 
    980 	KASSERT(itimer_lock_held());
    981 	KASSERT(!it->it_dying);
    982 
    983 	*aits = it->it_time;
    984 	if (!CLOCK_VIRTUAL_P(it->it_clockid)) {
    985 		/*
    986 		 * Convert from absolute to relative time in .it_value
    987 		 * part of real time timer.  If time for real time
    988 		 * timer has passed return 0, else return difference
    989 		 * between current time and time for the timer to go
    990 		 * off.
    991 		 */
    992 		if (timespecisset(&aits->it_value)) {
    993 			if (it->it_clockid == CLOCK_REALTIME) {
    994 				getnanotime(&now);
    995 			} else { /* CLOCK_MONOTONIC */
    996 				getnanouptime(&now);
    997 			}
    998 			if (timespeccmp(&aits->it_value, &now, <))
    999 				timespecclear(&aits->it_value);
   1000 			else
   1001 				timespecsub(&aits->it_value, &now,
   1002 				    &aits->it_value);
   1003 		}
   1004 	} else if (it->it_active) {
   1005 		for (itn = LIST_FIRST(it->it_vlist); itn && itn != it;
   1006 		     itn = LIST_NEXT(itn, it_list))
   1007 			timespecadd(&aits->it_value,
   1008 			    &itn->it_time.it_value, &aits->it_value);
   1009 		KASSERT(itn != NULL); /* it should be findable on the list */
   1010 	} else
   1011 		timespecclear(&aits->it_value);
   1012 }
   1013 
   1014 /*
   1015  * Per-process timer support.
   1016  *
   1017  * Both the BSD getitimer() family and the POSIX timer_*() family of
   1018  * routines are supported.
   1019  *
   1020  * All timers are kept in an array pointed to by p_timers, which is
   1021  * allocated on demand - many processes don't use timers at all. The
   1022  * first four elements in this array are reserved for the BSD timers:
   1023  * element 0 is ITIMER_REAL, element 1 is ITIMER_VIRTUAL, element
   1024  * 2 is ITIMER_PROF, and element 3 is ITIMER_MONOTONIC. The rest may be
   1025  * allocated by the timer_create() syscall.
   1026  *
   1027  * These timers are a "sub-class" of interval timer.
   1028  */
   1029 
   1030 /*
   1031  * ptimer_free:
   1032  *
   1033  *	Free the per-process timer at the specified index.
   1034  */
   1035 static void
   1036 ptimer_free(struct ptimers *pts, int index)
   1037 {
   1038 	struct itimer *it;
   1039 	struct ptimer *pt;
   1040 
   1041 	KASSERT(itimer_lock_held());
   1042 
   1043 	it = pts->pts_timers[index];
   1044 	pt = container_of(it, struct ptimer, pt_itimer);
   1045 	pts->pts_timers[index] = NULL;
   1046 	itimer_poison(it);
   1047 
   1048 	/*
   1049 	 * Remove it from the queue to be signalled.  Must be done
   1050 	 * after itimer is poisoned, because we may have had to wait
   1051 	 * for the callout to complete.
   1052 	 */
   1053 	if (pt->pt_queued) {
   1054 		TAILQ_REMOVE(&ptimer_queue, pt, pt_chain);
   1055 		pt->pt_queued = false;
   1056 	}
   1057 
   1058 	itimer_fini(it);	/* releases itimer_lock */
   1059 	kmem_free(pt, sizeof(*pt));
   1060 }
   1061 
   1062 /*
   1063  * ptimers_alloc:
   1064  *
   1065  *	Allocate a ptimers for the specified process.
   1066  */
   1067 static struct ptimers *
   1068 ptimers_alloc(struct proc *p)
   1069 {
   1070 	struct ptimers *pts;
   1071 	int i;
   1072 
   1073 	pts = kmem_alloc(sizeof(*pts), KM_SLEEP);
   1074 	LIST_INIT(&pts->pts_virtual);
   1075 	LIST_INIT(&pts->pts_prof);
   1076 	for (i = 0; i < TIMER_MAX; i++)
   1077 		pts->pts_timers[i] = NULL;
   1078 	itimer_lock();
   1079 	if (p->p_timers == NULL) {
   1080 		p->p_timers = pts;
   1081 		itimer_unlock();
   1082 		return pts;
   1083 	}
   1084 	itimer_unlock();
   1085 	kmem_free(pts, sizeof(*pts));
   1086 	return p->p_timers;
   1087 }
   1088 
   1089 /*
   1090  * ptimers_free:
   1091  *
   1092  *	Clean up the per-process timers. If "which" is set to TIMERS_ALL,
   1093  *	then clean up all timers and free all the data structures. If
   1094  *	"which" is set to TIMERS_POSIX, only clean up the timers allocated
   1095  *	by timer_create(), not the BSD setitimer() timers, and only free the
   1096  *	structure if none of those remain.
   1097  *
   1098  *	This function is exported because it is needed in the exec and
   1099  *	exit code paths.
   1100  */
   1101 void
   1102 ptimers_free(struct proc *p, int which)
   1103 {
   1104 	struct ptimers *pts;
   1105 	struct itimer *itn;
   1106 	struct timespec ts;
   1107 	int i;
   1108 
   1109 	if (p->p_timers == NULL)
   1110 		return;
   1111 
   1112 	pts = p->p_timers;
   1113 	itimer_lock();
   1114 	if (which == TIMERS_ALL) {
   1115 		p->p_timers = NULL;
   1116 		i = 0;
   1117 	} else {
   1118 		timespecclear(&ts);
   1119 		for (itn = LIST_FIRST(&pts->pts_virtual);
   1120 		     itn && itn != pts->pts_timers[ITIMER_VIRTUAL];
   1121 		     itn = LIST_NEXT(itn, it_list)) {
   1122 			KASSERT(itn->it_clockid == CLOCK_VIRTUAL);
   1123 			timespecadd(&ts, &itn->it_time.it_value, &ts);
   1124 		}
   1125 		LIST_FIRST(&pts->pts_virtual) = NULL;
   1126 		if (itn) {
   1127 			KASSERT(itn->it_clockid == CLOCK_VIRTUAL);
   1128 			timespecadd(&ts, &itn->it_time.it_value,
   1129 			    &itn->it_time.it_value);
   1130 			LIST_INSERT_HEAD(&pts->pts_virtual, itn, it_list);
   1131 		}
   1132 		timespecclear(&ts);
   1133 		for (itn = LIST_FIRST(&pts->pts_prof);
   1134 		     itn && itn != pts->pts_timers[ITIMER_PROF];
   1135 		     itn = LIST_NEXT(itn, it_list)) {
   1136 			KASSERT(itn->it_clockid == CLOCK_PROF);
   1137 			timespecadd(&ts, &itn->it_time.it_value, &ts);
   1138 		}
   1139 		LIST_FIRST(&pts->pts_prof) = NULL;
   1140 		if (itn) {
   1141 			KASSERT(itn->it_clockid == CLOCK_PROF);
   1142 			timespecadd(&ts, &itn->it_time.it_value,
   1143 			    &itn->it_time.it_value);
   1144 			LIST_INSERT_HEAD(&pts->pts_prof, itn, it_list);
   1145 		}
   1146 		i = TIMER_MIN;
   1147 	}
   1148 	for ( ; i < TIMER_MAX; i++) {
   1149 		if (pts->pts_timers[i] != NULL) {
   1150 			/* Free the timer and release the lock.  */
   1151 			ptimer_free(pts, i);
   1152 			/* Reacquire the lock for the next one.  */
   1153 			itimer_lock();
   1154 		}
   1155 	}
   1156 	if (pts->pts_timers[0] == NULL && pts->pts_timers[1] == NULL &&
   1157 	    pts->pts_timers[2] == NULL && pts->pts_timers[3] == NULL) {
   1158 		p->p_timers = NULL;
   1159 		itimer_unlock();
   1160 		kmem_free(pts, sizeof(*pts));
   1161 	} else
   1162 		itimer_unlock();
   1163 }
   1164 
   1165 /*
   1166  * ptimer_fire:
   1167  *
   1168  *	Fire a per-process timer.
   1169  */
   1170 static void
   1171 ptimer_fire(struct itimer *it)
   1172 {
   1173 	struct ptimer *pt = container_of(it, struct ptimer, pt_itimer);
   1174 
   1175 	KASSERT(itimer_lock_held());
   1176 
   1177 	/*
   1178 	 * XXX Can overrun, but we don't do signal queueing yet, anyway.
   1179 	 * XXX Relying on the clock interrupt is stupid.
   1180 	 */
   1181 	if (pt->pt_ev.sigev_notify != SIGEV_SIGNAL) {
   1182 		return;
   1183 	}
   1184 
   1185 	if (!pt->pt_queued) {
   1186 		TAILQ_INSERT_TAIL(&ptimer_queue, pt, pt_chain);
   1187 		pt->pt_queued = true;
   1188 		softint_schedule(ptimer_sih);
   1189 	}
   1190 }
   1191 
   1192 /*
   1193  * Operations vector for per-process timers (BSD and POSIX).
   1194  */
   1195 static const struct itimer_ops ptimer_itimer_ops = {
   1196 	.ito_fire = ptimer_fire,
   1197 };
   1198 
   1199 /*
   1200  * sys_timer_create:
   1201  *
   1202  *	System call to create a POSIX timer.
   1203  */
   1204 int
   1205 sys_timer_create(struct lwp *l, const struct sys_timer_create_args *uap,
   1206     register_t *retval)
   1207 {
   1208 	/* {
   1209 		syscallarg(clockid_t) clock_id;
   1210 		syscallarg(struct sigevent *) evp;
   1211 		syscallarg(timer_t *) timerid;
   1212 	} */
   1213 
   1214 	return timer_create1(SCARG(uap, timerid), SCARG(uap, clock_id),
   1215 	    SCARG(uap, evp), copyin, l);
   1216 }
   1217 
   1218 int
   1219 timer_create1(timer_t *tid, clockid_t id, struct sigevent *evp,
   1220     copyin_t fetch_event, struct lwp *l)
   1221 {
   1222 	int error;
   1223 	timer_t timerid;
   1224 	struct itlist *itl;
   1225 	struct ptimers *pts;
   1226 	struct ptimer *pt;
   1227 	struct proc *p;
   1228 
   1229 	p = l->l_proc;
   1230 
   1231 	if ((u_int)id > CLOCK_MONOTONIC)
   1232 		return SET_ERROR(EINVAL);
   1233 
   1234 	if ((pts = p->p_timers) == NULL)
   1235 		pts = ptimers_alloc(p);
   1236 
   1237 	pt = kmem_zalloc(sizeof(*pt), KM_SLEEP);
   1238 	if (evp != NULL) {
   1239 		if (((error =
   1240 		    (*fetch_event)(evp, &pt->pt_ev, sizeof(pt->pt_ev))) != 0) ||
   1241 		    ((pt->pt_ev.sigev_notify < SIGEV_NONE) ||
   1242 			(pt->pt_ev.sigev_notify > SIGEV_SA)) ||
   1243 			(pt->pt_ev.sigev_notify == SIGEV_SIGNAL &&
   1244 			 (pt->pt_ev.sigev_signo <= 0 ||
   1245 			  pt->pt_ev.sigev_signo >= NSIG))) {
   1246 			kmem_free(pt, sizeof(*pt));
   1247 			return (error ? error : SET_ERROR(EINVAL));
   1248 		}
   1249 	}
   1250 
   1251 	/* Find a free timer slot, skipping those reserved for setitimer(). */
   1252 	itimer_lock();
   1253 	for (timerid = TIMER_MIN; timerid < TIMER_MAX; timerid++)
   1254 		if (pts->pts_timers[timerid] == NULL)
   1255 			break;
   1256 	if (timerid == TIMER_MAX) {
   1257 		itimer_unlock();
   1258 		kmem_free(pt, sizeof(*pt));
   1259 		return SET_ERROR(EAGAIN);
   1260 	}
   1261 	if (evp == NULL) {
   1262 		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
   1263 		switch (id) {
   1264 		case CLOCK_REALTIME:
   1265 		case CLOCK_MONOTONIC:
   1266 			pt->pt_ev.sigev_signo = SIGALRM;
   1267 			break;
   1268 		case CLOCK_VIRTUAL:
   1269 			pt->pt_ev.sigev_signo = SIGVTALRM;
   1270 			break;
   1271 		case CLOCK_PROF:
   1272 			pt->pt_ev.sigev_signo = SIGPROF;
   1273 			break;
   1274 		}
   1275 		pt->pt_ev.sigev_value.sival_int = timerid;
   1276 	}
   1277 
   1278 	switch (id) {
   1279 	case CLOCK_VIRTUAL:
   1280 		itl = &pts->pts_virtual;
   1281 		break;
   1282 	case CLOCK_PROF:
   1283 		itl = &pts->pts_prof;
   1284 		break;
   1285 	default:
   1286 		itl = NULL;
   1287 	}
   1288 
   1289 	itimer_init(&pt->pt_itimer, &ptimer_itimer_ops, id, itl);
   1290 	pt->pt_proc = p;
   1291 	pt->pt_poverruns = 0;
   1292 	pt->pt_entry = timerid;
   1293 	pt->pt_queued = false;
   1294 
   1295 	pts->pts_timers[timerid] = &pt->pt_itimer;
   1296 	itimer_unlock();
   1297 
   1298 	return copyout(&timerid, tid, sizeof(timerid));
   1299 }
   1300 
   1301 /*
   1302  * sys_timer_delete:
   1303  *
   1304  *	System call to delete a POSIX timer.
   1305  */
   1306 int
   1307 sys_timer_delete(struct lwp *l, const struct sys_timer_delete_args *uap,
   1308     register_t *retval)
   1309 {
   1310 	/* {
   1311 		syscallarg(timer_t) timerid;
   1312 	} */
   1313 	struct proc *p = l->l_proc;
   1314 	timer_t timerid;
   1315 	struct ptimers *pts;
   1316 	struct itimer *it, *itn;
   1317 
   1318 	timerid = SCARG(uap, timerid);
   1319 	pts = p->p_timers;
   1320 
   1321 	if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
   1322 		return SET_ERROR(EINVAL);
   1323 
   1324 	itimer_lock();
   1325 	if ((it = pts->pts_timers[timerid]) == NULL) {
   1326 		itimer_unlock();
   1327 		return SET_ERROR(EINVAL);
   1328 	}
   1329 
   1330 	if (CLOCK_VIRTUAL_P(it->it_clockid)) {
   1331 		if (it->it_active) {
   1332 			itn = LIST_NEXT(it, it_list);
   1333 			LIST_REMOVE(it, it_list);
   1334 			for ( ; itn; itn = LIST_NEXT(itn, it_list))
   1335 				timespecadd(&it->it_time.it_value,
   1336 				    &itn->it_time.it_value,
   1337 				    &itn->it_time.it_value);
   1338 			it->it_active = false;
   1339 		}
   1340 	}
   1341 
   1342 	/* Free the timer and release the lock.  */
   1343 	ptimer_free(pts, timerid);
   1344 
   1345 	return 0;
   1346 }
   1347 
   1348 /*
   1349  * sys___timer_settime50:
   1350  *
   1351  *	System call to set/arm a POSIX timer.
   1352  */
   1353 int
   1354 sys___timer_settime50(struct lwp *l,
   1355     const struct sys___timer_settime50_args *uap,
   1356     register_t *retval)
   1357 {
   1358 	/* {
   1359 		syscallarg(timer_t) timerid;
   1360 		syscallarg(int) flags;
   1361 		syscallarg(const struct itimerspec *) value;
   1362 		syscallarg(struct itimerspec *) ovalue;
   1363 	} */
   1364 	int error;
   1365 	struct itimerspec value, ovalue, *ovp = NULL;
   1366 
   1367 	if ((error = copyin(SCARG(uap, value), &value,
   1368 	    sizeof(struct itimerspec))) != 0)
   1369 		return error;
   1370 
   1371 	if (SCARG(uap, ovalue))
   1372 		ovp = &ovalue;
   1373 
   1374 	if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
   1375 	    SCARG(uap, flags), l->l_proc)) != 0)
   1376 		return error;
   1377 
   1378 	if (ovp)
   1379 		return copyout(&ovalue, SCARG(uap, ovalue),
   1380 		    sizeof(struct itimerspec));
   1381 	return 0;
   1382 }
   1383 
   1384 int
   1385 dotimer_settime(int timerid, struct itimerspec *value,
   1386     struct itimerspec *ovalue, int flags, struct proc *p)
   1387 {
   1388 	struct timespec now;
   1389 	struct itimerspec val;
   1390 	struct ptimers *pts;
   1391 	struct itimer *it;
   1392 	int error;
   1393 
   1394 	pts = p->p_timers;
   1395 
   1396 	if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
   1397 		return SET_ERROR(EINVAL);
   1398 	val = *value;
   1399 	if (itimespecfix(&val.it_value) != 0 ||
   1400 	    itimespecfix(&val.it_interval) != 0)
   1401 		return SET_ERROR(EINVAL);
   1402 
   1403 	itimer_lock();
   1404  restart:
   1405 	if ((it = pts->pts_timers[timerid]) == NULL) {
   1406 		error = SET_ERROR(EINVAL);
   1407 		goto out;
   1408 	}
   1409 
   1410 	if (ovalue)
   1411 		itimer_gettime(it, ovalue);
   1412 	it->it_time = val;
   1413 
   1414 	/*
   1415 	 * If we've been passed a relative time for a realtime timer,
   1416 	 * convert it to absolute; if an absolute time for a virtual
   1417 	 * timer, convert it to relative and make sure we don't set it
   1418 	 * to zero, which would cancel the timer, or let it go
   1419 	 * negative, which would confuse the comparison tests.
   1420 	 */
   1421 	if (timespecisset(&it->it_time.it_value)) {
   1422 		if (!CLOCK_VIRTUAL_P(it->it_clockid)) {
   1423 			if ((flags & TIMER_ABSTIME) == 0) {
   1424 				if (it->it_clockid == CLOCK_REALTIME) {
   1425 					getnanotime(&now);
   1426 				} else { /* CLOCK_MONOTONIC */
   1427 					getnanouptime(&now);
   1428 				}
   1429 				if (!timespecaddok(&it->it_time.it_value,
   1430 					&now)) {
   1431 					error = SET_ERROR(EINVAL);
   1432 					goto out;
   1433 				}
   1434 				timespecadd(&it->it_time.it_value, &now,
   1435 				    &it->it_time.it_value);
   1436 			}
   1437 		} else {
   1438 			if ((flags & TIMER_ABSTIME) != 0) {
   1439 				getnanotime(&now);
   1440 				if (!timespecsubok(&it->it_time.it_value,
   1441 					&now)) {
   1442 					error = SET_ERROR(EINVAL);
   1443 					goto out;
   1444 				}
   1445 				timespecsub(&it->it_time.it_value, &now,
   1446 				    &it->it_time.it_value);
   1447 				if (!timespecisset(&it->it_time.it_value) ||
   1448 				    it->it_time.it_value.tv_sec < 0) {
   1449 					it->it_time.it_value.tv_sec = 0;
   1450 					it->it_time.it_value.tv_nsec = 1;
   1451 				}
   1452 			}
   1453 		}
   1454 	}
   1455 
   1456 	error = itimer_settime(it);
   1457 	if (error == ERESTART) {
   1458 		KASSERT(!CLOCK_VIRTUAL_P(it->it_clockid));
   1459 		goto restart;
   1460 	}
   1461 	KASSERT(error == 0);
   1462  out:
   1463 	itimer_unlock();
   1464 
   1465 	return error;
   1466 }
   1467 
   1468 /*
   1469  * sys___timer_gettime50:
   1470  *
   1471  *	System call to return the time remaining until a POSIX timer fires.
   1472  */
   1473 int
   1474 sys___timer_gettime50(struct lwp *l,
   1475     const struct sys___timer_gettime50_args *uap, register_t *retval)
   1476 {
   1477 	/* {
   1478 		syscallarg(timer_t) timerid;
   1479 		syscallarg(struct itimerspec *) value;
   1480 	} */
   1481 	struct itimerspec its;
   1482 	int error;
   1483 
   1484 	if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
   1485 	    &its)) != 0)
   1486 		return error;
   1487 
   1488 	return copyout(&its, SCARG(uap, value), sizeof(its));
   1489 }
   1490 
   1491 int
   1492 dotimer_gettime(int timerid, struct proc *p, struct itimerspec *its)
   1493 {
   1494 	struct itimer *it;
   1495 	struct ptimers *pts;
   1496 
   1497 	pts = p->p_timers;
   1498 	if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
   1499 		return SET_ERROR(EINVAL);
   1500 	itimer_lock();
   1501 	if ((it = pts->pts_timers[timerid]) == NULL) {
   1502 		itimer_unlock();
   1503 		return SET_ERROR(EINVAL);
   1504 	}
   1505 	itimer_gettime(it, its);
   1506 	itimer_unlock();
   1507 
   1508 	return 0;
   1509 }
   1510 
   1511 /*
   1512  * sys_timer_getoverrun:
   1513  *
   1514  *	System call to return the number of times a POSIX timer has
   1515  *	expired while a notification was already pending.  The counter
   1516  *	is reset when a timer expires and a notification can be posted.
   1517  */
   1518 int
   1519 sys_timer_getoverrun(struct lwp *l, const struct sys_timer_getoverrun_args *uap,
   1520     register_t *retval)
   1521 {
   1522 	/* {
   1523 		syscallarg(timer_t) timerid;
   1524 	} */
   1525 	struct proc *p = l->l_proc;
   1526 	struct ptimers *pts;
   1527 	int timerid;
   1528 	struct itimer *it;
   1529 	struct ptimer *pt;
   1530 
   1531 	timerid = SCARG(uap, timerid);
   1532 
   1533 	pts = p->p_timers;
   1534 	if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
   1535 		return SET_ERROR(EINVAL);
   1536 	itimer_lock();
   1537 	if ((it = pts->pts_timers[timerid]) == NULL) {
   1538 		itimer_unlock();
   1539 		return SET_ERROR(EINVAL);
   1540 	}
   1541 	pt = container_of(it, struct ptimer, pt_itimer);
   1542 	*retval = pt->pt_poverruns;
   1543 	if (*retval >= DELAYTIMER_MAX)
   1544 		*retval = DELAYTIMER_MAX;
   1545 	itimer_unlock();
   1546 
   1547 	return 0;
   1548 }
   1549 
   1550 /*
   1551  * sys___getitimer50:
   1552  *
   1553  *	System call to get the time remaining before a BSD timer fires.
   1554  */
   1555 int
   1556 sys___getitimer50(struct lwp *l, const struct sys___getitimer50_args *uap,
   1557     register_t *retval)
   1558 {
   1559 	/* {
   1560 		syscallarg(int) which;
   1561 		syscallarg(struct itimerval *) itv;
   1562 	} */
   1563 	struct proc *p = l->l_proc;
   1564 	struct itimerval aitv;
   1565 	int error;
   1566 
   1567 	memset(&aitv, 0, sizeof(aitv));
   1568 	error = dogetitimer(p, SCARG(uap, which), &aitv);
   1569 	if (error)
   1570 		return error;
   1571 	return copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval));
   1572 }
   1573 
   1574 int
   1575 dogetitimer(struct proc *p, int which, struct itimerval *itvp)
   1576 {
   1577 	struct ptimers *pts;
   1578 	struct itimer *it;
   1579 	struct itimerspec its;
   1580 
   1581 	if ((u_int)which > ITIMER_MONOTONIC)
   1582 		return SET_ERROR(EINVAL);
   1583 
   1584 	itimer_lock();
   1585 	pts = p->p_timers;
   1586 	if (pts == NULL || (it = pts->pts_timers[which]) == NULL) {
   1587 		timerclear(&itvp->it_value);
   1588 		timerclear(&itvp->it_interval);
   1589 	} else {
   1590 		itimer_gettime(it, &its);
   1591 		TIMESPEC_TO_TIMEVAL(&itvp->it_value, &its.it_value);
   1592 		TIMESPEC_TO_TIMEVAL(&itvp->it_interval, &its.it_interval);
   1593 	}
   1594 	itimer_unlock();
   1595 
   1596 	return 0;
   1597 }
   1598 
   1599 /*
   1600  * sys___setitimer50:
   1601  *
   1602  *	System call to set/arm a BSD timer.
   1603  */
   1604 int
   1605 sys___setitimer50(struct lwp *l, const struct sys___setitimer50_args *uap,
   1606     register_t *retval)
   1607 {
   1608 	/* {
   1609 		syscallarg(int) which;
   1610 		syscallarg(const struct itimerval *) itv;
   1611 		syscallarg(struct itimerval *) oitv;
   1612 	} */
   1613 	struct proc *p = l->l_proc;
   1614 	int which = SCARG(uap, which);
   1615 	struct sys___getitimer50_args getargs;
   1616 	const struct itimerval *itvp;
   1617 	struct itimerval aitv;
   1618 	int error;
   1619 
   1620 	itvp = SCARG(uap, itv);
   1621 	if (itvp &&
   1622 	    (error = copyin(itvp, &aitv, sizeof(struct itimerval))) != 0)
   1623 		return error;
   1624 	if (SCARG(uap, oitv) != NULL) {
   1625 		SCARG(&getargs, which) = which;
   1626 		SCARG(&getargs, itv) = SCARG(uap, oitv);
   1627 		if ((error = sys___getitimer50(l, &getargs, retval)) != 0)
   1628 			return error;
   1629 	}
   1630 	if (itvp == 0)
   1631 		return 0;
   1632 
   1633 	return dosetitimer(p, which, &aitv);
   1634 }
   1635 
   1636 int
   1637 dosetitimer(struct proc *p, int which, struct itimerval *itvp)
   1638 {
   1639 	struct timespec now;
   1640 	struct ptimers *pts;
   1641 	struct ptimer *spare;
   1642 	struct itimer *it;
   1643 	struct itlist *itl;
   1644 	int error;
   1645 
   1646 	if ((u_int)which > ITIMER_MONOTONIC)
   1647 		return SET_ERROR(EINVAL);
   1648 	if (itimerfix(&itvp->it_value) || itimerfix(&itvp->it_interval))
   1649 		return SET_ERROR(EINVAL);
   1650 
   1651 	/*
   1652 	 * Don't bother allocating data structures if the process just
   1653 	 * wants to clear the timer.
   1654 	 */
   1655 	spare = NULL;
   1656 	pts = p->p_timers;
   1657  retry:
   1658 	if (!timerisset(&itvp->it_value) && (pts == NULL ||
   1659 	    pts->pts_timers[which] == NULL))
   1660 		return 0;
   1661 	if (pts == NULL)
   1662 		pts = ptimers_alloc(p);
   1663 	itimer_lock();
   1664  restart:
   1665 	it = pts->pts_timers[which];
   1666 	if (it == NULL) {
   1667 		struct ptimer *pt;
   1668 
   1669 		if (spare == NULL) {
   1670 			itimer_unlock();
   1671 			spare = kmem_zalloc(sizeof(*spare), KM_SLEEP);
   1672 			goto retry;
   1673 		}
   1674 		pt = spare;
   1675 		spare = NULL;
   1676 
   1677 		it = &pt->pt_itimer;
   1678 		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
   1679 		pt->pt_ev.sigev_value.sival_int = which;
   1680 
   1681 		switch (which) {
   1682 		case ITIMER_REAL:
   1683 		case ITIMER_MONOTONIC:
   1684 			itl = NULL;
   1685 			pt->pt_ev.sigev_signo = SIGALRM;
   1686 			break;
   1687 		case ITIMER_VIRTUAL:
   1688 			itl = &pts->pts_virtual;
   1689 			pt->pt_ev.sigev_signo = SIGVTALRM;
   1690 			break;
   1691 		case ITIMER_PROF:
   1692 			itl = &pts->pts_prof;
   1693 			pt->pt_ev.sigev_signo = SIGPROF;
   1694 			break;
   1695 		default:
   1696 			panic("%s: can't happen %d", __func__, which);
   1697 		}
   1698 		itimer_init(it, &ptimer_itimer_ops, which, itl);
   1699 		pt->pt_proc = p;
   1700 		pt->pt_entry = which;
   1701 
   1702 		pts->pts_timers[which] = it;
   1703 	}
   1704 
   1705 	TIMEVAL_TO_TIMESPEC(&itvp->it_value, &it->it_time.it_value);
   1706 	TIMEVAL_TO_TIMESPEC(&itvp->it_interval, &it->it_time.it_interval);
   1707 
   1708 	error = 0;
   1709 	if (timespecisset(&it->it_time.it_value)) {
   1710 		/* Convert to absolute time */
   1711 		/* XXX need to wrap in splclock for timecounters case? */
   1712 		switch (which) {
   1713 		case ITIMER_REAL:
   1714 			getnanotime(&now);
   1715 			if (!timespecaddok(&it->it_time.it_value, &now)) {
   1716 				error = SET_ERROR(EINVAL);
   1717 				goto out;
   1718 			}
   1719 			timespecadd(&it->it_time.it_value, &now,
   1720 			    &it->it_time.it_value);
   1721 			break;
   1722 		case ITIMER_MONOTONIC:
   1723 			getnanouptime(&now);
   1724 			if (!timespecaddok(&it->it_time.it_value, &now)) {
   1725 				error = SET_ERROR(EINVAL);
   1726 				goto out;
   1727 			}
   1728 			timespecadd(&it->it_time.it_value, &now,
   1729 			    &it->it_time.it_value);
   1730 			break;
   1731 		default:
   1732 			break;
   1733 		}
   1734 	}
   1735 
   1736 	error = itimer_settime(it);
   1737 	if (error == ERESTART) {
   1738 		KASSERT(!CLOCK_VIRTUAL_P(it->it_clockid));
   1739 		goto restart;
   1740 	}
   1741 	KASSERT(error == 0);
   1742 out:
   1743 	itimer_unlock();
   1744 	if (spare != NULL)
   1745 		kmem_free(spare, sizeof(*spare));
   1746 
   1747 	return error;
   1748 }
   1749 
   1750 /*
   1751  * ptimer_tick:
   1752  *
   1753  *	Called from hardclock() to decrement per-process virtual timers.
   1754  */
   1755 void
   1756 ptimer_tick(lwp_t *l, bool user)
   1757 {
   1758 	struct ptimers *pts;
   1759 	struct itimer *it;
   1760 	proc_t *p;
   1761 
   1762 	p = l->l_proc;
   1763 	if (p->p_timers == NULL)
   1764 		return;
   1765 
   1766 	itimer_lock();
   1767 	if ((pts = l->l_proc->p_timers) != NULL) {
   1768 		/*
   1769 		 * Run current process's virtual and profile time, as needed.
   1770 		 */
   1771 		if (user && (it = LIST_FIRST(&pts->pts_virtual)) != NULL)
   1772 			if (itimer_decr(it, tick * 1000))
   1773 				(*it->it_ops->ito_fire)(it);
   1774 		if ((it = LIST_FIRST(&pts->pts_prof)) != NULL)
   1775 			if (itimer_decr(it, tick * 1000))
   1776 				(*it->it_ops->ito_fire)(it);
   1777 	}
   1778 	itimer_unlock();
   1779 }
   1780 
   1781 /*
   1782  * ptimer_intr:
   1783  *
   1784  *	Software interrupt handler for processing per-process
   1785  *	timer expiration.
   1786  */
   1787 static void
   1788 ptimer_intr(void *cookie)
   1789 {
   1790 	ksiginfo_t ksi;
   1791 	struct itimer *it;
   1792 	struct ptimer *pt;
   1793 	proc_t *p;
   1794 
   1795 	mutex_enter(&proc_lock);
   1796 	itimer_lock();
   1797 	while ((pt = TAILQ_FIRST(&ptimer_queue)) != NULL) {
   1798 		it = &pt->pt_itimer;
   1799 
   1800 		TAILQ_REMOVE(&ptimer_queue, pt, pt_chain);
   1801 		KASSERT(pt->pt_queued);
   1802 		pt->pt_queued = false;
   1803 
   1804 		p = pt->pt_proc;
   1805 		if (p->p_timers == NULL) {
   1806 			/* Process is dying. */
   1807 			continue;
   1808 		}
   1809 		if (pt->pt_ev.sigev_notify != SIGEV_SIGNAL) {
   1810 			continue;
   1811 		}
   1812 		if (sigismember(&p->p_sigpend.sp_set, pt->pt_ev.sigev_signo)) {
   1813 			if (it->it_overruns < INT_MAX)
   1814 				it->it_overruns++;
   1815 			continue;
   1816 		}
   1817 
   1818 		KSI_INIT(&ksi);
   1819 		ksi.ksi_signo = pt->pt_ev.sigev_signo;
   1820 		ksi.ksi_code = SI_TIMER;
   1821 		ksi.ksi_value = pt->pt_ev.sigev_value;
   1822 		pt->pt_poverruns = it->it_overruns;
   1823 		it->it_overruns = 0;
   1824 		itimer_unlock();
   1825 		kpsignal(p, &ksi, NULL);
   1826 		itimer_lock();
   1827 	}
   1828 	itimer_unlock();
   1829 	mutex_exit(&proc_lock);
   1830 }
   1831