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