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