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