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