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