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