xref: /netbsd-src/sys/kern/kern_time.c (revision dd3ee07da436799d8de85f3055253118b76bf345)
1 /*	$NetBSD: kern_time.c,v 1.214 2022/05/15 16:20:10 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.214 2022/05/15 16:20:10 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 if (delta->tv_sec < INT64_MIN/1000000 + 1) {
629 			time_adjtime = INT64_MIN;
630 		} else {
631 			time_adjtime = delta->tv_sec * 1000000
632 			    + MAX(-999999, MIN(999999, delta->tv_usec));
633 		}
634 
635 		if (time_adjtime) {
636 			/* We need to save the system time during shutdown */
637 			time_adjusted |= 1;
638 		}
639 		mutex_spin_exit(&timecounter_lock);
640 	}
641 }
642 
643 /*
644  * Interval timer support.
645  *
646  * The itimer_*() routines provide generic support for interval timers,
647  * both real (CLOCK_REALTIME, CLOCK_MONOTIME), and virtual (CLOCK_VIRTUAL,
648  * CLOCK_PROF).
649  *
650  * Real timers keep their deadline as an absolute time, and are fired
651  * by a callout.  Virtual timers are kept as a linked-list of deltas,
652  * and are processed by hardclock().
653  *
654  * Because the real time timer callout may be delayed in real time due
655  * to interrupt processing on the system, it is possible for the real
656  * time timeout routine (itimer_callout()) run past after its deadline.
657  * It does not suffice, therefore, to reload the real timer .it_value
658  * from the timer's .it_interval.  Rather, we compute the next deadline
659  * in absolute time based on the current time and the .it_interval value,
660  * and report any overruns.
661  *
662  * Note that while the virtual timers are supported in a generic fashion
663  * here, they only (currently) make sense as per-process timers, and thus
664  * only really work for that case.
665  */
666 
667 /*
668  * itimer_init:
669  *
670  *	Initialize the common data for an interval timer.
671  */
672 void
673 itimer_init(struct itimer * const it, const struct itimer_ops * const ops,
674     clockid_t const id, struct itlist * const itl)
675 {
676 
677 	KASSERT(itimer_lock_held());
678 	KASSERT(ops != NULL);
679 
680 	timespecclear(&it->it_time.it_value);
681 	it->it_ops = ops;
682 	it->it_clockid = id;
683 	it->it_overruns = 0;
684 	it->it_dying = false;
685 	if (!CLOCK_VIRTUAL_P(id)) {
686 		KASSERT(itl == NULL);
687 		callout_init(&it->it_ch, CALLOUT_MPSAFE);
688 		if (id == CLOCK_REALTIME && ops->ito_realtime_changed != NULL) {
689 			LIST_INSERT_HEAD(&itimer_realtime_changed_notify,
690 			    it, it_rtchgq);
691 		}
692 	} else {
693 		KASSERT(itl != NULL);
694 		it->it_vlist = itl;
695 		it->it_active = false;
696 	}
697 }
698 
699 /*
700  * itimer_poison:
701  *
702  *	Poison an interval timer, preventing it from being scheduled
703  *	or processed, in preparation for freeing the timer.
704  */
705 void
706 itimer_poison(struct itimer * const it)
707 {
708 
709 	KASSERT(itimer_lock_held());
710 
711 	it->it_dying = true;
712 
713 	/*
714 	 * For non-virtual timers, stop the callout, or wait for it to
715 	 * run if it has already fired.  It cannot restart again after
716 	 * this point: the callout won't restart itself when dying, no
717 	 * other users holding the lock can restart it, and any other
718 	 * users waiting for callout_halt concurrently (itimer_settime)
719 	 * will restart from the top.
720 	 */
721 	if (!CLOCK_VIRTUAL_P(it->it_clockid)) {
722 		callout_halt(&it->it_ch, &itimer_mutex);
723 		if (it->it_clockid == CLOCK_REALTIME &&
724 		    it->it_ops->ito_realtime_changed != NULL) {
725 			LIST_REMOVE(it, it_rtchgq);
726 		}
727 	}
728 }
729 
730 /*
731  * itimer_fini:
732  *
733  *	Release resources used by an interval timer.
734  *
735  *	N.B. itimer_lock must be held on entry, and is released on exit.
736  */
737 void
738 itimer_fini(struct itimer * const it)
739 {
740 
741 	KASSERT(itimer_lock_held());
742 
743 	/* All done with the global state. */
744 	itimer_unlock();
745 
746 	/* Destroy the callout, if needed. */
747 	if (!CLOCK_VIRTUAL_P(it->it_clockid))
748 		callout_destroy(&it->it_ch);
749 }
750 
751 /*
752  * itimer_decr:
753  *
754  *	Decrement an interval timer by a specified number of nanoseconds,
755  *	which must be less than a second, i.e. < 1000000000.  If the timer
756  *	expires, then reload it.  In this case, carry over (nsec - old value)
757  *	to reduce the value reloaded into the timer so that the timer does
758  *	not drift.  This routine assumes that it is called in a context where
759  *	the timers on which it is operating cannot change in value.
760  *
761  *	Returns true if the timer has expired.
762  */
763 static bool
764 itimer_decr(struct itimer *it, int nsec)
765 {
766 	struct itimerspec *itp;
767 	int error __diagused;
768 
769 	KASSERT(itimer_lock_held());
770 	KASSERT(CLOCK_VIRTUAL_P(it->it_clockid));
771 
772 	itp = &it->it_time;
773 	if (itp->it_value.tv_nsec < nsec) {
774 		if (itp->it_value.tv_sec == 0) {
775 			/* expired, and already in next interval */
776 			nsec -= itp->it_value.tv_nsec;
777 			goto expire;
778 		}
779 		itp->it_value.tv_nsec += 1000000000;
780 		itp->it_value.tv_sec--;
781 	}
782 	itp->it_value.tv_nsec -= nsec;
783 	nsec = 0;
784 	if (timespecisset(&itp->it_value))
785 		return false;
786 	/* expired, exactly at end of interval */
787  expire:
788 	if (timespecisset(&itp->it_interval)) {
789 		itp->it_value = itp->it_interval;
790 		itp->it_value.tv_nsec -= nsec;
791 		if (itp->it_value.tv_nsec < 0) {
792 			itp->it_value.tv_nsec += 1000000000;
793 			itp->it_value.tv_sec--;
794 		}
795 		error = itimer_settime(it);
796 		KASSERT(error == 0); /* virtual, never fails */
797 	} else
798 		itp->it_value.tv_nsec = 0;		/* sec is already 0 */
799 	return true;
800 }
801 
802 static void itimer_callout(void *);
803 
804 /*
805  * itimer_arm_real:
806  *
807  *	Arm a non-virtual timer.
808  */
809 static void
810 itimer_arm_real(struct itimer * const it)
811 {
812 	/*
813 	 * Don't need to check tshzto() return value, here.
814 	 * callout_reset() does it for us.
815 	 */
816 	callout_reset(&it->it_ch,
817 	    (it->it_clockid == CLOCK_MONOTONIC
818 		? tshztoup(&it->it_time.it_value)
819 		: tshzto(&it->it_time.it_value)),
820 	    itimer_callout, it);
821 }
822 
823 /*
824  * itimer_callout:
825  *
826  *	Callout to expire a non-virtual timer.  Queue it up for processing,
827  *	and then reload, if it is configured to do so.
828  *
829  *	N.B. A delay in processing this callout causes multiple
830  *	SIGALRM calls to be compressed into one.
831  */
832 static void
833 itimer_callout(void *arg)
834 {
835 	uint64_t last_val, next_val, interval, now_ns;
836 	struct timespec now, next;
837 	struct itimer * const it = arg;
838 	int backwards;
839 
840 	itimer_lock();
841 	(*it->it_ops->ito_fire)(it);
842 
843 	if (!timespecisset(&it->it_time.it_interval)) {
844 		timespecclear(&it->it_time.it_value);
845 		itimer_unlock();
846 		return;
847 	}
848 
849 	if (it->it_clockid == CLOCK_MONOTONIC) {
850 		getnanouptime(&now);
851 	} else {
852 		getnanotime(&now);
853 	}
854 	backwards = (timespeccmp(&it->it_time.it_value, &now, >));
855 	timespecadd(&it->it_time.it_value, &it->it_time.it_interval, &next);
856 	/* Handle the easy case of non-overflown timers first. */
857 	if (!backwards && timespeccmp(&next, &now, >)) {
858 		it->it_time.it_value = next;
859 	} else {
860 		now_ns = timespec2ns(&now);
861 		last_val = timespec2ns(&it->it_time.it_value);
862 		interval = timespec2ns(&it->it_time.it_interval);
863 
864 		next_val = now_ns +
865 		    (now_ns - last_val + interval - 1) % interval;
866 
867 		if (backwards)
868 			next_val += interval;
869 		else
870 			it->it_overruns += (now_ns - last_val) / interval;
871 
872 		it->it_time.it_value.tv_sec = next_val / 1000000000;
873 		it->it_time.it_value.tv_nsec = next_val % 1000000000;
874 	}
875 
876 	/*
877 	 * Reset the callout, if it's not going away.
878 	 */
879 	if (!it->it_dying)
880 		itimer_arm_real(it);
881 	itimer_unlock();
882 }
883 
884 /*
885  * itimer_settime:
886  *
887  *	Set up the given interval timer. The value in it->it_time.it_value
888  *	is taken to be an absolute time for CLOCK_REALTIME/CLOCK_MONOTONIC
889  *	timers and a relative time for CLOCK_VIRTUAL/CLOCK_PROF timers.
890  *
891  *	If the callout had already fired but not yet run, fails with
892  *	ERESTART -- caller must restart from the top to look up a timer.
893  */
894 int
895 itimer_settime(struct itimer *it)
896 {
897 	struct itimer *itn, *pitn;
898 	struct itlist *itl;
899 
900 	KASSERT(itimer_lock_held());
901 
902 	if (!CLOCK_VIRTUAL_P(it->it_clockid)) {
903 		/*
904 		 * Try to stop the callout.  However, if it had already
905 		 * fired, we have to drop the lock to wait for it, so
906 		 * the world may have changed and pt may not be there
907 		 * any more.  In that case, tell the caller to start
908 		 * over from the top.
909 		 */
910 		if (callout_halt(&it->it_ch, &itimer_mutex))
911 			return ERESTART;
912 
913 		/* Now we can touch it and start it up again. */
914 		if (timespecisset(&it->it_time.it_value))
915 			itimer_arm_real(it);
916 	} else {
917 		if (it->it_active) {
918 			itn = LIST_NEXT(it, it_list);
919 			LIST_REMOVE(it, it_list);
920 			for ( ; itn; itn = LIST_NEXT(itn, it_list))
921 				timespecadd(&it->it_time.it_value,
922 				    &itn->it_time.it_value,
923 				    &itn->it_time.it_value);
924 		}
925 		if (timespecisset(&it->it_time.it_value)) {
926 			itl = it->it_vlist;
927 			for (itn = LIST_FIRST(itl), pitn = NULL;
928 			     itn && timespeccmp(&it->it_time.it_value,
929 				 &itn->it_time.it_value, >);
930 			     pitn = itn, itn = LIST_NEXT(itn, it_list))
931 				timespecsub(&it->it_time.it_value,
932 				    &itn->it_time.it_value,
933 				    &it->it_time.it_value);
934 
935 			if (pitn)
936 				LIST_INSERT_AFTER(pitn, it, it_list);
937 			else
938 				LIST_INSERT_HEAD(itl, it, it_list);
939 
940 			for ( ; itn ; itn = LIST_NEXT(itn, it_list))
941 				timespecsub(&itn->it_time.it_value,
942 				    &it->it_time.it_value,
943 				    &itn->it_time.it_value);
944 
945 			it->it_active = true;
946 		} else {
947 			it->it_active = false;
948 		}
949 	}
950 
951 	/* Success!  */
952 	return 0;
953 }
954 
955 /*
956  * itimer_gettime:
957  *
958  *	Return the remaining time of an interval timer.
959  */
960 void
961 itimer_gettime(const struct itimer *it, struct itimerspec *aits)
962 {
963 	struct timespec now;
964 	struct itimer *itn;
965 
966 	KASSERT(itimer_lock_held());
967 
968 	*aits = it->it_time;
969 	if (!CLOCK_VIRTUAL_P(it->it_clockid)) {
970 		/*
971 		 * Convert from absolute to relative time in .it_value
972 		 * part of real time timer.  If time for real time
973 		 * timer has passed return 0, else return difference
974 		 * between current time and time for the timer to go
975 		 * off.
976 		 */
977 		if (timespecisset(&aits->it_value)) {
978 			if (it->it_clockid == CLOCK_REALTIME) {
979 				getnanotime(&now);
980 			} else { /* CLOCK_MONOTONIC */
981 				getnanouptime(&now);
982 			}
983 			if (timespeccmp(&aits->it_value, &now, <))
984 				timespecclear(&aits->it_value);
985 			else
986 				timespecsub(&aits->it_value, &now,
987 				    &aits->it_value);
988 		}
989 	} else if (it->it_active) {
990 		for (itn = LIST_FIRST(it->it_vlist); itn && itn != it;
991 		     itn = LIST_NEXT(itn, it_list))
992 			timespecadd(&aits->it_value,
993 			    &itn->it_time.it_value, &aits->it_value);
994 		KASSERT(itn != NULL); /* it should be findable on the list */
995 	} else
996 		timespecclear(&aits->it_value);
997 }
998 
999 /*
1000  * Per-process timer support.
1001  *
1002  * Both the BSD getitimer() family and the POSIX timer_*() family of
1003  * routines are supported.
1004  *
1005  * All timers are kept in an array pointed to by p_timers, which is
1006  * allocated on demand - many processes don't use timers at all. The
1007  * first four elements in this array are reserved for the BSD timers:
1008  * element 0 is ITIMER_REAL, element 1 is ITIMER_VIRTUAL, element
1009  * 2 is ITIMER_PROF, and element 3 is ITIMER_MONOTONIC. The rest may be
1010  * allocated by the timer_create() syscall.
1011  *
1012  * These timers are a "sub-class" of interval timer.
1013  */
1014 
1015 /*
1016  * ptimer_free:
1017  *
1018  *	Free the per-process timer at the specified index.
1019  */
1020 static void
1021 ptimer_free(struct ptimers *pts, int index)
1022 {
1023 	struct itimer *it;
1024 	struct ptimer *pt;
1025 
1026 	KASSERT(itimer_lock_held());
1027 
1028 	it = pts->pts_timers[index];
1029 	pt = container_of(it, struct ptimer, pt_itimer);
1030 	pts->pts_timers[index] = NULL;
1031 	itimer_poison(it);
1032 
1033 	/*
1034 	 * Remove it from the queue to be signalled.  Must be done
1035 	 * after itimer is poisoned, because we may have had to wait
1036 	 * for the callout to complete.
1037 	 */
1038 	if (pt->pt_queued) {
1039 		TAILQ_REMOVE(&ptimer_queue, pt, pt_chain);
1040 		pt->pt_queued = false;
1041 	}
1042 
1043 	itimer_fini(it);	/* releases itimer_lock */
1044 	kmem_free(pt, sizeof(*pt));
1045 }
1046 
1047 /*
1048  * ptimers_alloc:
1049  *
1050  *	Allocate a ptimers for the specified process.
1051  */
1052 static struct ptimers *
1053 ptimers_alloc(struct proc *p)
1054 {
1055 	struct ptimers *pts;
1056 	int i;
1057 
1058 	pts = kmem_alloc(sizeof(*pts), KM_SLEEP);
1059 	LIST_INIT(&pts->pts_virtual);
1060 	LIST_INIT(&pts->pts_prof);
1061 	for (i = 0; i < TIMER_MAX; i++)
1062 		pts->pts_timers[i] = NULL;
1063 	itimer_lock();
1064 	if (p->p_timers == NULL) {
1065 		p->p_timers = pts;
1066 		itimer_unlock();
1067 		return pts;
1068 	}
1069 	itimer_unlock();
1070 	kmem_free(pts, sizeof(*pts));
1071 	return p->p_timers;
1072 }
1073 
1074 /*
1075  * ptimers_free:
1076  *
1077  *	Clean up the per-process timers. If "which" is set to TIMERS_ALL,
1078  *	then clean up all timers and free all the data structures. If
1079  *	"which" is set to TIMERS_POSIX, only clean up the timers allocated
1080  *	by timer_create(), not the BSD setitimer() timers, and only free the
1081  *	structure if none of those remain.
1082  *
1083  *	This function is exported because it is needed in the exec and
1084  *	exit code paths.
1085  */
1086 void
1087 ptimers_free(struct proc *p, int which)
1088 {
1089 	struct ptimers *pts;
1090 	struct itimer *itn;
1091 	struct timespec ts;
1092 	int i;
1093 
1094 	if (p->p_timers == NULL)
1095 		return;
1096 
1097 	pts = p->p_timers;
1098 	itimer_lock();
1099 	if (which == TIMERS_ALL) {
1100 		p->p_timers = NULL;
1101 		i = 0;
1102 	} else {
1103 		timespecclear(&ts);
1104 		for (itn = LIST_FIRST(&pts->pts_virtual);
1105 		     itn && itn != pts->pts_timers[ITIMER_VIRTUAL];
1106 		     itn = LIST_NEXT(itn, it_list)) {
1107 			KASSERT(itn->it_clockid == CLOCK_VIRTUAL);
1108 			timespecadd(&ts, &itn->it_time.it_value, &ts);
1109 		}
1110 		LIST_FIRST(&pts->pts_virtual) = NULL;
1111 		if (itn) {
1112 			KASSERT(itn->it_clockid == CLOCK_VIRTUAL);
1113 			timespecadd(&ts, &itn->it_time.it_value,
1114 			    &itn->it_time.it_value);
1115 			LIST_INSERT_HEAD(&pts->pts_virtual, itn, it_list);
1116 		}
1117 		timespecclear(&ts);
1118 		for (itn = LIST_FIRST(&pts->pts_prof);
1119 		     itn && itn != pts->pts_timers[ITIMER_PROF];
1120 		     itn = LIST_NEXT(itn, it_list)) {
1121 			KASSERT(itn->it_clockid == CLOCK_PROF);
1122 			timespecadd(&ts, &itn->it_time.it_value, &ts);
1123 		}
1124 		LIST_FIRST(&pts->pts_prof) = NULL;
1125 		if (itn) {
1126 			KASSERT(itn->it_clockid == CLOCK_PROF);
1127 			timespecadd(&ts, &itn->it_time.it_value,
1128 			    &itn->it_time.it_value);
1129 			LIST_INSERT_HEAD(&pts->pts_prof, itn, it_list);
1130 		}
1131 		i = TIMER_MIN;
1132 	}
1133 	for ( ; i < TIMER_MAX; i++) {
1134 		if (pts->pts_timers[i] != NULL) {
1135 			/* Free the timer and release the lock.  */
1136 			ptimer_free(pts, i);
1137 			/* Reacquire the lock for the next one.  */
1138 			itimer_lock();
1139 		}
1140 	}
1141 	if (pts->pts_timers[0] == NULL && pts->pts_timers[1] == NULL &&
1142 	    pts->pts_timers[2] == NULL && pts->pts_timers[3] == NULL) {
1143 		p->p_timers = NULL;
1144 		itimer_unlock();
1145 		kmem_free(pts, sizeof(*pts));
1146 	} else
1147 		itimer_unlock();
1148 }
1149 
1150 /*
1151  * ptimer_fire:
1152  *
1153  *	Fire a per-process timer.
1154  */
1155 static void
1156 ptimer_fire(struct itimer *it)
1157 {
1158 	struct ptimer *pt = container_of(it, struct ptimer, pt_itimer);
1159 
1160 	KASSERT(itimer_lock_held());
1161 
1162 	/*
1163 	 * XXX Can overrun, but we don't do signal queueing yet, anyway.
1164 	 * XXX Relying on the clock interrupt is stupid.
1165 	 */
1166 	if (pt->pt_ev.sigev_notify != SIGEV_SIGNAL) {
1167 		return;
1168 	}
1169 
1170 	if (!pt->pt_queued) {
1171 		TAILQ_INSERT_TAIL(&ptimer_queue, pt, pt_chain);
1172 		pt->pt_queued = true;
1173 		softint_schedule(ptimer_sih);
1174 	}
1175 }
1176 
1177 /*
1178  * Operations vector for per-process timers (BSD and POSIX).
1179  */
1180 static const struct itimer_ops ptimer_itimer_ops = {
1181 	.ito_fire = ptimer_fire,
1182 };
1183 
1184 /*
1185  * sys_timer_create:
1186  *
1187  *	System call to create a POSIX timer.
1188  */
1189 int
1190 sys_timer_create(struct lwp *l, const struct sys_timer_create_args *uap,
1191     register_t *retval)
1192 {
1193 	/* {
1194 		syscallarg(clockid_t) clock_id;
1195 		syscallarg(struct sigevent *) evp;
1196 		syscallarg(timer_t *) timerid;
1197 	} */
1198 
1199 	return timer_create1(SCARG(uap, timerid), SCARG(uap, clock_id),
1200 	    SCARG(uap, evp), copyin, l);
1201 }
1202 
1203 int
1204 timer_create1(timer_t *tid, clockid_t id, struct sigevent *evp,
1205     copyin_t fetch_event, struct lwp *l)
1206 {
1207 	int error;
1208 	timer_t timerid;
1209 	struct itlist *itl;
1210 	struct ptimers *pts;
1211 	struct ptimer *pt;
1212 	struct proc *p;
1213 
1214 	p = l->l_proc;
1215 
1216 	if ((u_int)id > CLOCK_MONOTONIC)
1217 		return (EINVAL);
1218 
1219 	if ((pts = p->p_timers) == NULL)
1220 		pts = ptimers_alloc(p);
1221 
1222 	pt = kmem_zalloc(sizeof(*pt), KM_SLEEP);
1223 	if (evp != NULL) {
1224 		if (((error =
1225 		    (*fetch_event)(evp, &pt->pt_ev, sizeof(pt->pt_ev))) != 0) ||
1226 		    ((pt->pt_ev.sigev_notify < SIGEV_NONE) ||
1227 			(pt->pt_ev.sigev_notify > SIGEV_SA)) ||
1228 			(pt->pt_ev.sigev_notify == SIGEV_SIGNAL &&
1229 			 (pt->pt_ev.sigev_signo <= 0 ||
1230 			  pt->pt_ev.sigev_signo >= NSIG))) {
1231 			kmem_free(pt, sizeof(*pt));
1232 			return (error ? error : EINVAL);
1233 		}
1234 	}
1235 
1236 	/* Find a free timer slot, skipping those reserved for setitimer(). */
1237 	itimer_lock();
1238 	for (timerid = TIMER_MIN; timerid < TIMER_MAX; timerid++)
1239 		if (pts->pts_timers[timerid] == NULL)
1240 			break;
1241 	if (timerid == TIMER_MAX) {
1242 		itimer_unlock();
1243 		kmem_free(pt, sizeof(*pt));
1244 		return EAGAIN;
1245 	}
1246 	if (evp == NULL) {
1247 		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
1248 		switch (id) {
1249 		case CLOCK_REALTIME:
1250 		case CLOCK_MONOTONIC:
1251 			pt->pt_ev.sigev_signo = SIGALRM;
1252 			break;
1253 		case CLOCK_VIRTUAL:
1254 			pt->pt_ev.sigev_signo = SIGVTALRM;
1255 			break;
1256 		case CLOCK_PROF:
1257 			pt->pt_ev.sigev_signo = SIGPROF;
1258 			break;
1259 		}
1260 		pt->pt_ev.sigev_value.sival_int = timerid;
1261 	}
1262 
1263 	switch (id) {
1264 	case CLOCK_VIRTUAL:
1265 		itl = &pts->pts_virtual;
1266 		break;
1267 	case CLOCK_PROF:
1268 		itl = &pts->pts_prof;
1269 		break;
1270 	default:
1271 		itl = NULL;
1272 	}
1273 
1274 	itimer_init(&pt->pt_itimer, &ptimer_itimer_ops, id, itl);
1275 	pt->pt_proc = p;
1276 	pt->pt_poverruns = 0;
1277 	pt->pt_entry = timerid;
1278 	pt->pt_queued = false;
1279 
1280 	pts->pts_timers[timerid] = &pt->pt_itimer;
1281 	itimer_unlock();
1282 
1283 	return copyout(&timerid, tid, sizeof(timerid));
1284 }
1285 
1286 /*
1287  * sys_timer_delete:
1288  *
1289  *	System call to delete a POSIX timer.
1290  */
1291 int
1292 sys_timer_delete(struct lwp *l, const struct sys_timer_delete_args *uap,
1293     register_t *retval)
1294 {
1295 	/* {
1296 		syscallarg(timer_t) timerid;
1297 	} */
1298 	struct proc *p = l->l_proc;
1299 	timer_t timerid;
1300 	struct ptimers *pts;
1301 	struct itimer *it, *itn;
1302 
1303 	timerid = SCARG(uap, timerid);
1304 	pts = p->p_timers;
1305 
1306 	if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
1307 		return (EINVAL);
1308 
1309 	itimer_lock();
1310 	if ((it = pts->pts_timers[timerid]) == NULL) {
1311 		itimer_unlock();
1312 		return (EINVAL);
1313 	}
1314 
1315 	if (CLOCK_VIRTUAL_P(it->it_clockid)) {
1316 		if (it->it_active) {
1317 			itn = LIST_NEXT(it, it_list);
1318 			LIST_REMOVE(it, it_list);
1319 			for ( ; itn; itn = LIST_NEXT(itn, it_list))
1320 				timespecadd(&it->it_time.it_value,
1321 				    &itn->it_time.it_value,
1322 				    &itn->it_time.it_value);
1323 			it->it_active = false;
1324 		}
1325 	}
1326 
1327 	/* Free the timer and release the lock.  */
1328 	ptimer_free(pts, timerid);
1329 
1330 	return (0);
1331 }
1332 
1333 /*
1334  * sys___timer_settime50:
1335  *
1336  *	System call to set/arm a POSIX timer.
1337  */
1338 int
1339 sys___timer_settime50(struct lwp *l,
1340     const struct sys___timer_settime50_args *uap,
1341     register_t *retval)
1342 {
1343 	/* {
1344 		syscallarg(timer_t) timerid;
1345 		syscallarg(int) flags;
1346 		syscallarg(const struct itimerspec *) value;
1347 		syscallarg(struct itimerspec *) ovalue;
1348 	} */
1349 	int error;
1350 	struct itimerspec value, ovalue, *ovp = NULL;
1351 
1352 	if ((error = copyin(SCARG(uap, value), &value,
1353 	    sizeof(struct itimerspec))) != 0)
1354 		return (error);
1355 
1356 	if (SCARG(uap, ovalue))
1357 		ovp = &ovalue;
1358 
1359 	if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
1360 	    SCARG(uap, flags), l->l_proc)) != 0)
1361 		return error;
1362 
1363 	if (ovp)
1364 		return copyout(&ovalue, SCARG(uap, ovalue),
1365 		    sizeof(struct itimerspec));
1366 	return 0;
1367 }
1368 
1369 int
1370 dotimer_settime(int timerid, struct itimerspec *value,
1371     struct itimerspec *ovalue, int flags, struct proc *p)
1372 {
1373 	struct timespec now;
1374 	struct itimerspec val, oval;
1375 	struct ptimers *pts;
1376 	struct itimer *it;
1377 	int error;
1378 
1379 	pts = p->p_timers;
1380 
1381 	if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
1382 		return EINVAL;
1383 	val = *value;
1384 	if ((error = itimespecfix(&val.it_value)) != 0 ||
1385 	    (error = itimespecfix(&val.it_interval)) != 0)
1386 		return error;
1387 
1388 	itimer_lock();
1389  restart:
1390 	if ((it = pts->pts_timers[timerid]) == NULL) {
1391 		itimer_unlock();
1392 		return EINVAL;
1393 	}
1394 
1395 	oval = it->it_time;
1396 	it->it_time = val;
1397 
1398 	/*
1399 	 * If we've been passed a relative time for a realtime timer,
1400 	 * convert it to absolute; if an absolute time for a virtual
1401 	 * timer, convert it to relative and make sure we don't set it
1402 	 * to zero, which would cancel the timer, or let it go
1403 	 * negative, which would confuse the comparison tests.
1404 	 */
1405 	if (timespecisset(&it->it_time.it_value)) {
1406 		if (!CLOCK_VIRTUAL_P(it->it_clockid)) {
1407 			if ((flags & TIMER_ABSTIME) == 0) {
1408 				if (it->it_clockid == CLOCK_REALTIME) {
1409 					getnanotime(&now);
1410 				} else { /* CLOCK_MONOTONIC */
1411 					getnanouptime(&now);
1412 				}
1413 				timespecadd(&it->it_time.it_value, &now,
1414 				    &it->it_time.it_value);
1415 			}
1416 		} else {
1417 			if ((flags & TIMER_ABSTIME) != 0) {
1418 				getnanotime(&now);
1419 				timespecsub(&it->it_time.it_value, &now,
1420 				    &it->it_time.it_value);
1421 				if (!timespecisset(&it->it_time.it_value) ||
1422 				    it->it_time.it_value.tv_sec < 0) {
1423 					it->it_time.it_value.tv_sec = 0;
1424 					it->it_time.it_value.tv_nsec = 1;
1425 				}
1426 			}
1427 		}
1428 	}
1429 
1430 	error = itimer_settime(it);
1431 	if (error == ERESTART) {
1432 		KASSERT(!CLOCK_VIRTUAL_P(it->it_clockid));
1433 		goto restart;
1434 	}
1435 	KASSERT(error == 0);
1436 	itimer_unlock();
1437 
1438 	if (ovalue)
1439 		*ovalue = oval;
1440 
1441 	return (0);
1442 }
1443 
1444 /*
1445  * sys___timer_gettime50:
1446  *
1447  *	System call to return the time remaining until a POSIX timer fires.
1448  */
1449 int
1450 sys___timer_gettime50(struct lwp *l,
1451     const struct sys___timer_gettime50_args *uap, register_t *retval)
1452 {
1453 	/* {
1454 		syscallarg(timer_t) timerid;
1455 		syscallarg(struct itimerspec *) value;
1456 	} */
1457 	struct itimerspec its;
1458 	int error;
1459 
1460 	if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
1461 	    &its)) != 0)
1462 		return error;
1463 
1464 	return copyout(&its, SCARG(uap, value), sizeof(its));
1465 }
1466 
1467 int
1468 dotimer_gettime(int timerid, struct proc *p, struct itimerspec *its)
1469 {
1470 	struct itimer *it;
1471 	struct ptimers *pts;
1472 
1473 	pts = p->p_timers;
1474 	if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
1475 		return (EINVAL);
1476 	itimer_lock();
1477 	if ((it = pts->pts_timers[timerid]) == NULL) {
1478 		itimer_unlock();
1479 		return (EINVAL);
1480 	}
1481 	itimer_gettime(it, its);
1482 	itimer_unlock();
1483 
1484 	return 0;
1485 }
1486 
1487 /*
1488  * sys_timer_getoverrun:
1489  *
1490  *	System call to return the number of times a POSIX timer has
1491  *	expired while a notification was already pending.  The counter
1492  *	is reset when a timer expires and a notification can be posted.
1493  */
1494 int
1495 sys_timer_getoverrun(struct lwp *l, const struct sys_timer_getoverrun_args *uap,
1496     register_t *retval)
1497 {
1498 	/* {
1499 		syscallarg(timer_t) timerid;
1500 	} */
1501 	struct proc *p = l->l_proc;
1502 	struct ptimers *pts;
1503 	int timerid;
1504 	struct itimer *it;
1505 	struct ptimer *pt;
1506 
1507 	timerid = SCARG(uap, timerid);
1508 
1509 	pts = p->p_timers;
1510 	if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
1511 		return (EINVAL);
1512 	itimer_lock();
1513 	if ((it = pts->pts_timers[timerid]) == NULL) {
1514 		itimer_unlock();
1515 		return (EINVAL);
1516 	}
1517 	pt = container_of(it, struct ptimer, pt_itimer);
1518 	*retval = pt->pt_poverruns;
1519 	if (*retval >= DELAYTIMER_MAX)
1520 		*retval = DELAYTIMER_MAX;
1521 	itimer_unlock();
1522 
1523 	return (0);
1524 }
1525 
1526 /*
1527  * sys___getitimer50:
1528  *
1529  *	System call to get the time remaining before a BSD timer fires.
1530  */
1531 int
1532 sys___getitimer50(struct lwp *l, const struct sys___getitimer50_args *uap,
1533     register_t *retval)
1534 {
1535 	/* {
1536 		syscallarg(int) which;
1537 		syscallarg(struct itimerval *) itv;
1538 	} */
1539 	struct proc *p = l->l_proc;
1540 	struct itimerval aitv;
1541 	int error;
1542 
1543 	memset(&aitv, 0, sizeof(aitv));
1544 	error = dogetitimer(p, SCARG(uap, which), &aitv);
1545 	if (error)
1546 		return error;
1547 	return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
1548 }
1549 
1550 int
1551 dogetitimer(struct proc *p, int which, struct itimerval *itvp)
1552 {
1553 	struct ptimers *pts;
1554 	struct itimer *it;
1555 	struct itimerspec its;
1556 
1557 	if ((u_int)which > ITIMER_MONOTONIC)
1558 		return (EINVAL);
1559 
1560 	itimer_lock();
1561 	pts = p->p_timers;
1562 	if (pts == NULL || (it = pts->pts_timers[which]) == NULL) {
1563 		timerclear(&itvp->it_value);
1564 		timerclear(&itvp->it_interval);
1565 	} else {
1566 		itimer_gettime(it, &its);
1567 		TIMESPEC_TO_TIMEVAL(&itvp->it_value, &its.it_value);
1568 		TIMESPEC_TO_TIMEVAL(&itvp->it_interval, &its.it_interval);
1569 	}
1570 	itimer_unlock();
1571 
1572 	return 0;
1573 }
1574 
1575 /*
1576  * sys___setitimer50:
1577  *
1578  *	System call to set/arm a BSD timer.
1579  */
1580 int
1581 sys___setitimer50(struct lwp *l, const struct sys___setitimer50_args *uap,
1582     register_t *retval)
1583 {
1584 	/* {
1585 		syscallarg(int) which;
1586 		syscallarg(const struct itimerval *) itv;
1587 		syscallarg(struct itimerval *) oitv;
1588 	} */
1589 	struct proc *p = l->l_proc;
1590 	int which = SCARG(uap, which);
1591 	struct sys___getitimer50_args getargs;
1592 	const struct itimerval *itvp;
1593 	struct itimerval aitv;
1594 	int error;
1595 
1596 	itvp = SCARG(uap, itv);
1597 	if (itvp &&
1598 	    (error = copyin(itvp, &aitv, sizeof(struct itimerval))) != 0)
1599 		return (error);
1600 	if (SCARG(uap, oitv) != NULL) {
1601 		SCARG(&getargs, which) = which;
1602 		SCARG(&getargs, itv) = SCARG(uap, oitv);
1603 		if ((error = sys___getitimer50(l, &getargs, retval)) != 0)
1604 			return (error);
1605 	}
1606 	if (itvp == 0)
1607 		return (0);
1608 
1609 	return dosetitimer(p, which, &aitv);
1610 }
1611 
1612 int
1613 dosetitimer(struct proc *p, int which, struct itimerval *itvp)
1614 {
1615 	struct timespec now;
1616 	struct ptimers *pts;
1617 	struct ptimer *spare;
1618 	struct itimer *it;
1619 	struct itlist *itl;
1620 	int error;
1621 
1622 	if ((u_int)which > ITIMER_MONOTONIC)
1623 		return (EINVAL);
1624 	if (itimerfix(&itvp->it_value) || itimerfix(&itvp->it_interval))
1625 		return (EINVAL);
1626 
1627 	/*
1628 	 * Don't bother allocating data structures if the process just
1629 	 * wants to clear the timer.
1630 	 */
1631 	spare = NULL;
1632 	pts = p->p_timers;
1633  retry:
1634 	if (!timerisset(&itvp->it_value) && (pts == NULL ||
1635 	    pts->pts_timers[which] == NULL))
1636 		return (0);
1637 	if (pts == NULL)
1638 		pts = ptimers_alloc(p);
1639 	itimer_lock();
1640  restart:
1641 	it = pts->pts_timers[which];
1642 	if (it == NULL) {
1643 		struct ptimer *pt;
1644 
1645 		if (spare == NULL) {
1646 			itimer_unlock();
1647 			spare = kmem_zalloc(sizeof(*spare), KM_SLEEP);
1648 			goto retry;
1649 		}
1650 		pt = spare;
1651 		spare = NULL;
1652 
1653 		it = &pt->pt_itimer;
1654 		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
1655 		pt->pt_ev.sigev_value.sival_int = which;
1656 
1657 		switch (which) {
1658 		case ITIMER_REAL:
1659 		case ITIMER_MONOTONIC:
1660 			itl = NULL;
1661 			pt->pt_ev.sigev_signo = SIGALRM;
1662 			break;
1663 		case ITIMER_VIRTUAL:
1664 			itl = &pts->pts_virtual;
1665 			pt->pt_ev.sigev_signo = SIGVTALRM;
1666 			break;
1667 		case ITIMER_PROF:
1668 			itl = &pts->pts_prof;
1669 			pt->pt_ev.sigev_signo = SIGPROF;
1670 			break;
1671 		default:
1672 			panic("%s: can't happen %d", __func__, which);
1673 		}
1674 		itimer_init(it, &ptimer_itimer_ops, which, itl);
1675 		pt->pt_proc = p;
1676 		pt->pt_entry = which;
1677 
1678 		pts->pts_timers[which] = it;
1679 	}
1680 
1681 	TIMEVAL_TO_TIMESPEC(&itvp->it_value, &it->it_time.it_value);
1682 	TIMEVAL_TO_TIMESPEC(&itvp->it_interval, &it->it_time.it_interval);
1683 
1684 	if (timespecisset(&it->it_time.it_value)) {
1685 		/* Convert to absolute time */
1686 		/* XXX need to wrap in splclock for timecounters case? */
1687 		switch (which) {
1688 		case ITIMER_REAL:
1689 			getnanotime(&now);
1690 			timespecadd(&it->it_time.it_value, &now,
1691 			    &it->it_time.it_value);
1692 			break;
1693 		case ITIMER_MONOTONIC:
1694 			getnanouptime(&now);
1695 			timespecadd(&it->it_time.it_value, &now,
1696 			    &it->it_time.it_value);
1697 			break;
1698 		default:
1699 			break;
1700 		}
1701 	}
1702 	error = itimer_settime(it);
1703 	if (error == ERESTART) {
1704 		KASSERT(!CLOCK_VIRTUAL_P(it->it_clockid));
1705 		goto restart;
1706 	}
1707 	KASSERT(error == 0);
1708 	itimer_unlock();
1709 	if (spare != NULL)
1710 		kmem_free(spare, sizeof(*spare));
1711 
1712 	return (0);
1713 }
1714 
1715 /*
1716  * ptimer_tick:
1717  *
1718  *	Called from hardclock() to decrement per-process virtual timers.
1719  */
1720 void
1721 ptimer_tick(lwp_t *l, bool user)
1722 {
1723 	struct ptimers *pts;
1724 	struct itimer *it;
1725 	proc_t *p;
1726 
1727 	p = l->l_proc;
1728 	if (p->p_timers == NULL)
1729 		return;
1730 
1731 	itimer_lock();
1732 	if ((pts = l->l_proc->p_timers) != NULL) {
1733 		/*
1734 		 * Run current process's virtual and profile time, as needed.
1735 		 */
1736 		if (user && (it = LIST_FIRST(&pts->pts_virtual)) != NULL)
1737 			if (itimer_decr(it, tick * 1000))
1738 				(*it->it_ops->ito_fire)(it);
1739 		if ((it = LIST_FIRST(&pts->pts_prof)) != NULL)
1740 			if (itimer_decr(it, tick * 1000))
1741 				(*it->it_ops->ito_fire)(it);
1742 	}
1743 	itimer_unlock();
1744 }
1745 
1746 /*
1747  * ptimer_intr:
1748  *
1749  *	Software interrupt handler for processing per-process
1750  *	timer expiration.
1751  */
1752 static void
1753 ptimer_intr(void *cookie)
1754 {
1755 	ksiginfo_t ksi;
1756 	struct itimer *it;
1757 	struct ptimer *pt;
1758 	proc_t *p;
1759 
1760 	mutex_enter(&proc_lock);
1761 	itimer_lock();
1762 	while ((pt = TAILQ_FIRST(&ptimer_queue)) != NULL) {
1763 		it = &pt->pt_itimer;
1764 
1765 		TAILQ_REMOVE(&ptimer_queue, pt, pt_chain);
1766 		KASSERT(pt->pt_queued);
1767 		pt->pt_queued = false;
1768 
1769 		p = pt->pt_proc;
1770 		if (p->p_timers == NULL) {
1771 			/* Process is dying. */
1772 			continue;
1773 		}
1774 		if (pt->pt_ev.sigev_notify != SIGEV_SIGNAL) {
1775 			continue;
1776 		}
1777 		if (sigismember(&p->p_sigpend.sp_set, pt->pt_ev.sigev_signo)) {
1778 			it->it_overruns++;
1779 			continue;
1780 		}
1781 
1782 		KSI_INIT(&ksi);
1783 		ksi.ksi_signo = pt->pt_ev.sigev_signo;
1784 		ksi.ksi_code = SI_TIMER;
1785 		ksi.ksi_value = pt->pt_ev.sigev_value;
1786 		pt->pt_poverruns = it->it_overruns;
1787 		it->it_overruns = 0;
1788 		itimer_unlock();
1789 		kpsignal(p, &ksi, NULL);
1790 		itimer_lock();
1791 	}
1792 	itimer_unlock();
1793 	mutex_exit(&proc_lock);
1794 }
1795