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