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