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