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