xref: /freebsd/sys/kern/kern_time.c (revision 8f0371f19d85ab269c94258d30b87aef416d529d)
19454b2d8SWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)kern_time.c	8.1 (Berkeley) 6/10/93
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
32677b542eSDavid E. O'Brien #include <sys/cdefs.h>
33677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
34677b542eSDavid E. O'Brien 
354b8d5f2dSRobert Watson #include "opt_mac.h"
364b8d5f2dSRobert Watson 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38e96c1fdcSPeter Wemm #include <sys/systm.h>
39fb919e4dSMark Murray #include <sys/lock.h>
40fb919e4dSMark Murray #include <sys/mutex.h>
41d2d3e875SBruce Evans #include <sys/sysproto.h>
42df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
43797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
44df8bae1dSRodney W. Grimes #include <sys/kernel.h>
454b8d5f2dSRobert Watson #include <sys/mac.h>
46efa42cbcSPaul Saab #include <sys/syscallsubr.h>
4794c8fcd8SPeter Wemm #include <sys/sysent.h>
48df8bae1dSRodney W. Grimes #include <sys/proc.h>
49708e7684SPeter Wemm #include <sys/time.h>
5086857b36SDavid Xu #include <sys/timers.h>
5191266b96SPoul-Henning Kamp #include <sys/timetc.h>
52df8bae1dSRodney W. Grimes #include <sys/vnode.h>
53fb919e4dSMark Murray 
545b870b7bSPeter Wemm #include <vm/vm.h>
555b870b7bSPeter Wemm #include <vm/vm_extern.h>
56df8bae1dSRodney W. Grimes 
5786857b36SDavid Xu #define MAX_CLOCKS 	(CLOCK_MONOTONIC+1)
5886857b36SDavid Xu 
5991f1c2b3SPoul-Henning Kamp int tz_minuteswest;
6091f1c2b3SPoul-Henning Kamp int tz_dsttime;
61ac7e6123SDavid Greenman 
6286857b36SDavid Xu static struct kclock	posix_clocks[MAX_CLOCKS];
6386857b36SDavid Xu static uma_zone_t	itimer_zone = NULL;
6486857b36SDavid Xu 
65df8bae1dSRodney W. Grimes /*
66df8bae1dSRodney W. Grimes  * Time of day and interval timer support.
67df8bae1dSRodney W. Grimes  *
68df8bae1dSRodney W. Grimes  * These routines provide the kernel entry points to get and set
69df8bae1dSRodney W. Grimes  * the time-of-day and per-process interval timers.  Subroutines
70df8bae1dSRodney W. Grimes  * here provide support for adding and subtracting timeval structures
71df8bae1dSRodney W. Grimes  * and decrementing interval timers, optionally reloading the interval
72df8bae1dSRodney W. Grimes  * timers when they expire.
73df8bae1dSRodney W. Grimes  */
74df8bae1dSRodney W. Grimes 
757edfb592SJohn Baldwin static int	settime(struct thread *, struct timeval *);
764d77a549SAlfred Perlstein static void	timevalfix(struct timeval *);
774d77a549SAlfred Perlstein static void	no_lease_updatetime(int);
7894c8fcd8SPeter Wemm 
7986857b36SDavid Xu static void	itimer_start(void);
8086857b36SDavid Xu static int	itimer_init(void *, int, int);
8186857b36SDavid Xu static void	itimer_fini(void *, int);
8286857b36SDavid Xu static void	itimer_enter(struct itimer *);
8386857b36SDavid Xu static void	itimer_leave(struct itimer *);
8486857b36SDavid Xu static struct itimer *itimer_find(struct proc *, timer_t, int);
8586857b36SDavid Xu static void	itimers_alloc(struct proc *);
8686857b36SDavid Xu static int	realtimer_create(struct itimer *);
8786857b36SDavid Xu static int	realtimer_gettime(struct itimer *, struct itimerspec *);
8886857b36SDavid Xu static int	realtimer_settime(struct itimer *, int,
8986857b36SDavid Xu 			struct itimerspec *, struct itimerspec *);
9086857b36SDavid Xu static int	realtimer_delete(struct itimer *);
9156c06c4bSDavid Xu static void	realtimer_clocktime(clockid_t, struct timespec *);
9286857b36SDavid Xu static void	realtimer_expire(void *);
9386857b36SDavid Xu static void	realtimer_event_hook(struct proc *, clockid_t, int event);
9486857b36SDavid Xu static int	kern_timer_create(struct thread *, clockid_t,
9586857b36SDavid Xu 			struct sigevent *, timer_t *, timer_t);
9686857b36SDavid Xu static int	kern_timer_delete(struct thread *, timer_t);
9786857b36SDavid Xu 
9886857b36SDavid Xu int		register_posix_clock(int, struct kclock *);
9986857b36SDavid Xu void		itimer_fire(struct itimer *it);
10056c06c4bSDavid Xu int		itimespecfix(struct timespec *ts);
10186857b36SDavid Xu 
10286857b36SDavid Xu #define CLOCK_CALL(clock, call, arglist)		\
10386857b36SDavid Xu 	((*posix_clocks[clock].call) arglist)
10486857b36SDavid Xu 
10586857b36SDavid Xu SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL);
10686857b36SDavid Xu 
10786857b36SDavid Xu 
1081b09ae77SPoul-Henning Kamp static void
1091b09ae77SPoul-Henning Kamp no_lease_updatetime(deltat)
1101b09ae77SPoul-Henning Kamp 	int deltat;
1111b09ae77SPoul-Henning Kamp {
1121b09ae77SPoul-Henning Kamp }
1131b09ae77SPoul-Henning Kamp 
1144d77a549SAlfred Perlstein void (*lease_updatetime)(int)  = no_lease_updatetime;
1151b09ae77SPoul-Henning Kamp 
11694c8fcd8SPeter Wemm static int
11791afe087SPoul-Henning Kamp settime(struct thread *td, struct timeval *tv)
11894c8fcd8SPeter Wemm {
119fcae3aa6SNick Sayer 	struct timeval delta, tv1, tv2;
120c0bd94a7SNick Sayer 	static struct timeval maxtime, laststep;
1217ec73f64SPoul-Henning Kamp 	struct timespec ts;
12294c8fcd8SPeter Wemm 	int s;
12394c8fcd8SPeter Wemm 
124708e7684SPeter Wemm 	s = splclock();
1259c8fff87SBruce Evans 	microtime(&tv1);
12600af9731SPoul-Henning Kamp 	delta = *tv;
12700af9731SPoul-Henning Kamp 	timevalsub(&delta, &tv1);
12894c8fcd8SPeter Wemm 
12994c8fcd8SPeter Wemm 	/*
1309c8fff87SBruce Evans 	 * If the system is secure, we do not allow the time to be
131fcae3aa6SNick Sayer 	 * set to a value earlier than 1 second less than the highest
132fcae3aa6SNick Sayer 	 * time we have yet seen. The worst a miscreant can do in
133fcae3aa6SNick Sayer 	 * this circumstance is "freeze" time. He couldn't go
134fcae3aa6SNick Sayer 	 * back to the past.
135c0bd94a7SNick Sayer 	 *
136c0bd94a7SNick Sayer 	 * We similarly do not allow the clock to be stepped more
137c0bd94a7SNick Sayer 	 * than one second, nor more than once per second. This allows
138c0bd94a7SNick Sayer 	 * a miscreant to make the clock march double-time, but no worse.
13994c8fcd8SPeter Wemm 	 */
1407edfb592SJohn Baldwin 	if (securelevel_gt(td->td_ucred, 1) != 0) {
141fcae3aa6SNick Sayer 		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
1423f92429aSMatt Jacob 			/*
143c0bd94a7SNick Sayer 			 * Update maxtime to latest time we've seen.
1443f92429aSMatt Jacob 			 */
145fcae3aa6SNick Sayer 			if (tv1.tv_sec > maxtime.tv_sec)
146fcae3aa6SNick Sayer 				maxtime = tv1;
147fcae3aa6SNick Sayer 			tv2 = *tv;
148fcae3aa6SNick Sayer 			timevalsub(&tv2, &maxtime);
149fcae3aa6SNick Sayer 			if (tv2.tv_sec < -1) {
1503f92429aSMatt Jacob 				tv->tv_sec = maxtime.tv_sec - 1;
151fcae3aa6SNick Sayer 				printf("Time adjustment clamped to -1 second\n");
152fcae3aa6SNick Sayer 			}
1533f92429aSMatt Jacob 		} else {
154c0bd94a7SNick Sayer 			if (tv1.tv_sec == laststep.tv_sec) {
155c0bd94a7SNick Sayer 				splx(s);
156c0bd94a7SNick Sayer 				return (EPERM);
157c0bd94a7SNick Sayer 			}
158c0bd94a7SNick Sayer 			if (delta.tv_sec > 1) {
159c0bd94a7SNick Sayer 				tv->tv_sec = tv1.tv_sec + 1;
160c0bd94a7SNick Sayer 				printf("Time adjustment clamped to +1 second\n");
161c0bd94a7SNick Sayer 			}
162c0bd94a7SNick Sayer 			laststep = *tv;
163fcae3aa6SNick Sayer 		}
1649c8fff87SBruce Evans 	}
1659c8fff87SBruce Evans 
1667ec73f64SPoul-Henning Kamp 	ts.tv_sec = tv->tv_sec;
1677ec73f64SPoul-Henning Kamp 	ts.tv_nsec = tv->tv_usec * 1000;
1687edfb592SJohn Baldwin 	mtx_lock(&Giant);
16991266b96SPoul-Henning Kamp 	tc_setclock(&ts);
17094c8fcd8SPeter Wemm 	(void) splsoftclock();
17194c8fcd8SPeter Wemm 	lease_updatetime(delta.tv_sec);
17294c8fcd8SPeter Wemm 	splx(s);
17394c8fcd8SPeter Wemm 	resettodr();
1747edfb592SJohn Baldwin 	mtx_unlock(&Giant);
17594c8fcd8SPeter Wemm 	return (0);
17694c8fcd8SPeter Wemm }
17794c8fcd8SPeter Wemm 
17894c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
17994c8fcd8SPeter Wemm struct clock_gettime_args {
18094c8fcd8SPeter Wemm 	clockid_t clock_id;
18194c8fcd8SPeter Wemm 	struct	timespec *tp;
18294c8fcd8SPeter Wemm };
18394c8fcd8SPeter Wemm #endif
184708e7684SPeter Wemm 
185fb99ab88SMatthew Dillon /*
186fb99ab88SMatthew Dillon  * MPSAFE
187fb99ab88SMatthew Dillon  */
18894c8fcd8SPeter Wemm /* ARGSUSED */
18994c8fcd8SPeter Wemm int
19091afe087SPoul-Henning Kamp clock_gettime(struct thread *td, struct clock_gettime_args *uap)
19194c8fcd8SPeter Wemm {
19294c8fcd8SPeter Wemm 	struct timespec ats;
193f0b479cdSPaul Saab 	int error;
194f0b479cdSPaul Saab 
195f0b479cdSPaul Saab 	error = kern_clock_gettime(td, uap->clock_id, &ats);
196f0b479cdSPaul Saab 	if (error == 0)
197f0b479cdSPaul Saab 		error = copyout(&ats, uap->tp, sizeof(ats));
198f0b479cdSPaul Saab 
199f0b479cdSPaul Saab 	return (error);
200f0b479cdSPaul Saab }
201f0b479cdSPaul Saab 
202f0b479cdSPaul Saab int
203f0b479cdSPaul Saab kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
204f0b479cdSPaul Saab {
205de0a9241SKelly Yancey 	struct timeval sys, user;
20678c85e8dSJohn Baldwin 	struct proc *p;
20794c8fcd8SPeter Wemm 
20878c85e8dSJohn Baldwin 	p = td->td_proc;
209f0b479cdSPaul Saab 	switch (clock_id) {
210b8817154SKelly Yancey 	case CLOCK_REALTIME:
211f0b479cdSPaul Saab 		nanotime(ats);
212b8817154SKelly Yancey 		break;
213b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
21478c85e8dSJohn Baldwin 		PROC_LOCK(p);
21578c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
21678c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
217f0b479cdSPaul Saab 		TIMEVAL_TO_TIMESPEC(&user, ats);
218b8817154SKelly Yancey 		break;
219b8817154SKelly Yancey 	case CLOCK_PROF:
22078c85e8dSJohn Baldwin 		PROC_LOCK(p);
22178c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
22278c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
223de0a9241SKelly Yancey 		timevaladd(&user, &sys);
224f0b479cdSPaul Saab 		TIMEVAL_TO_TIMESPEC(&user, ats);
225de0a9241SKelly Yancey 		break;
226de0a9241SKelly Yancey 	case CLOCK_MONOTONIC:
227f0b479cdSPaul Saab 		nanouptime(ats);
228b8817154SKelly Yancey 		break;
229b8817154SKelly Yancey 	default:
2305cb3dc8fSPoul-Henning Kamp 		return (EINVAL);
231b8817154SKelly Yancey 	}
232f0b479cdSPaul Saab 	return (0);
23394c8fcd8SPeter Wemm }
23494c8fcd8SPeter Wemm 
23594c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
23694c8fcd8SPeter Wemm struct clock_settime_args {
23794c8fcd8SPeter Wemm 	clockid_t clock_id;
23894c8fcd8SPeter Wemm 	const struct	timespec *tp;
23994c8fcd8SPeter Wemm };
24094c8fcd8SPeter Wemm #endif
241708e7684SPeter Wemm 
242fb99ab88SMatthew Dillon /*
243fb99ab88SMatthew Dillon  * MPSAFE
244fb99ab88SMatthew Dillon  */
24594c8fcd8SPeter Wemm /* ARGSUSED */
24694c8fcd8SPeter Wemm int
24791afe087SPoul-Henning Kamp clock_settime(struct thread *td, struct clock_settime_args *uap)
24894c8fcd8SPeter Wemm {
24994c8fcd8SPeter Wemm 	struct timespec ats;
25094c8fcd8SPeter Wemm 	int error;
25194c8fcd8SPeter Wemm 
252f0b479cdSPaul Saab 	if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
253f0b479cdSPaul Saab 		return (error);
254f0b479cdSPaul Saab 	return (kern_clock_settime(td, uap->clock_id, &ats));
255f0b479cdSPaul Saab }
256f0b479cdSPaul Saab 
257f0b479cdSPaul Saab int
258f0b479cdSPaul Saab kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
259f0b479cdSPaul Saab {
260f0b479cdSPaul Saab 	struct timeval atv;
261f0b479cdSPaul Saab 	int error;
262f0b479cdSPaul Saab 
2634b8d5f2dSRobert Watson #ifdef MAC
2644b8d5f2dSRobert Watson 	error = mac_check_system_settime(td->td_ucred);
2654b8d5f2dSRobert Watson 	if (error)
2664b8d5f2dSRobert Watson 		return (error);
2674b8d5f2dSRobert Watson #endif
26844731cabSJohn Baldwin 	if ((error = suser(td)) != 0)
2697edfb592SJohn Baldwin 		return (error);
270f0b479cdSPaul Saab 	if (clock_id != CLOCK_REALTIME)
2717edfb592SJohn Baldwin 		return (EINVAL);
272f0b479cdSPaul Saab 	if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000)
2737edfb592SJohn Baldwin 		return (EINVAL);
274a0502b19SPoul-Henning Kamp 	/* XXX Don't convert nsec->usec and back */
275f0b479cdSPaul Saab 	TIMESPEC_TO_TIMEVAL(&atv, ats);
2767edfb592SJohn Baldwin 	error = settime(td, &atv);
27794c8fcd8SPeter Wemm 	return (error);
27894c8fcd8SPeter Wemm }
27994c8fcd8SPeter Wemm 
28094c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
28194c8fcd8SPeter Wemm struct clock_getres_args {
28294c8fcd8SPeter Wemm 	clockid_t clock_id;
28394c8fcd8SPeter Wemm 	struct	timespec *tp;
28494c8fcd8SPeter Wemm };
28594c8fcd8SPeter Wemm #endif
286708e7684SPeter Wemm 
28794c8fcd8SPeter Wemm int
28891afe087SPoul-Henning Kamp clock_getres(struct thread *td, struct clock_getres_args *uap)
28994c8fcd8SPeter Wemm {
29094c8fcd8SPeter Wemm 	struct timespec ts;
291f0b479cdSPaul Saab 	int error;
29294c8fcd8SPeter Wemm 
293f0b479cdSPaul Saab 	if (uap->tp == NULL)
294f0b479cdSPaul Saab 		return (0);
295f0b479cdSPaul Saab 
296f0b479cdSPaul Saab 	error = kern_clock_getres(td, uap->clock_id, &ts);
297f0b479cdSPaul Saab 	if (error == 0)
298f0b479cdSPaul Saab 		error = copyout(&ts, uap->tp, sizeof(ts));
299f0b479cdSPaul Saab 	return (error);
300f0b479cdSPaul Saab }
301f0b479cdSPaul Saab 
302f0b479cdSPaul Saab int
303f0b479cdSPaul Saab kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
304f0b479cdSPaul Saab {
305f0b479cdSPaul Saab 
306f0b479cdSPaul Saab 	ts->tv_sec = 0;
307f0b479cdSPaul Saab 	switch (clock_id) {
308b8817154SKelly Yancey 	case CLOCK_REALTIME:
309b8817154SKelly Yancey 	case CLOCK_MONOTONIC:
310ac0653dcSBruce Evans 		/*
311ac0653dcSBruce Evans 		 * Round up the result of the division cheaply by adding 1.
312ac0653dcSBruce Evans 		 * Rounding up is especially important if rounding down
313ac0653dcSBruce Evans 		 * would give 0.  Perfect rounding is unimportant.
314ac0653dcSBruce Evans 		 */
315f0b479cdSPaul Saab 		ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
316b8817154SKelly Yancey 		break;
317b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
318b8817154SKelly Yancey 	case CLOCK_PROF:
319b8817154SKelly Yancey 		/* Accurately round up here because we can do so cheaply. */
320f0b479cdSPaul Saab 		ts->tv_nsec = (1000000000 + hz - 1) / hz;
321b8817154SKelly Yancey 		break;
322b8817154SKelly Yancey 	default:
323b8817154SKelly Yancey 		return (EINVAL);
32494c8fcd8SPeter Wemm 	}
325de0a9241SKelly Yancey 	return (0);
32694c8fcd8SPeter Wemm }
32794c8fcd8SPeter Wemm 
32894c8fcd8SPeter Wemm static int nanowait;
32994c8fcd8SPeter Wemm 
3307fdf2c85SPaul Saab int
3317fdf2c85SPaul Saab kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
3325b870b7bSPeter Wemm {
3335704ba6aSPoul-Henning Kamp 	struct timespec ts, ts2, ts3;
33433841826SPoul-Henning Kamp 	struct timeval tv;
33533841826SPoul-Henning Kamp 	int error;
3365b870b7bSPeter Wemm 
3377d7fb492SBruce Evans 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
338708e7684SPeter Wemm 		return (EINVAL);
339d254af07SMatthew Dillon 	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
3407d7fb492SBruce Evans 		return (0);
341c21410e1SPoul-Henning Kamp 	getnanouptime(&ts);
34200af9731SPoul-Henning Kamp 	timespecadd(&ts, rqt);
34333841826SPoul-Henning Kamp 	TIMESPEC_TO_TIMEVAL(&tv, rqt);
34433841826SPoul-Henning Kamp 	for (;;) {
34533841826SPoul-Henning Kamp 		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
34633841826SPoul-Henning Kamp 		    tvtohz(&tv));
347c21410e1SPoul-Henning Kamp 		getnanouptime(&ts2);
34833841826SPoul-Henning Kamp 		if (error != EWOULDBLOCK) {
34933841826SPoul-Henning Kamp 			if (error == ERESTART)
35094c8fcd8SPeter Wemm 				error = EINTR;
35133841826SPoul-Henning Kamp 			if (rmt != NULL) {
35233841826SPoul-Henning Kamp 				timespecsub(&ts, &ts2);
35333841826SPoul-Henning Kamp 				if (ts.tv_sec < 0)
35433841826SPoul-Henning Kamp 					timespecclear(&ts);
35500af9731SPoul-Henning Kamp 				*rmt = ts;
35600af9731SPoul-Henning Kamp 			}
35733841826SPoul-Henning Kamp 			return (error);
35833841826SPoul-Henning Kamp 		}
35933841826SPoul-Henning Kamp 		if (timespeccmp(&ts2, &ts, >=))
36033841826SPoul-Henning Kamp 			return (0);
3615704ba6aSPoul-Henning Kamp 		ts3 = ts;
3625704ba6aSPoul-Henning Kamp 		timespecsub(&ts3, &ts2);
3635704ba6aSPoul-Henning Kamp 		TIMESPEC_TO_TIMEVAL(&tv, &ts3);
36433841826SPoul-Henning Kamp 	}
3655b870b7bSPeter Wemm }
36694c8fcd8SPeter Wemm 
3675b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
3685b870b7bSPeter Wemm struct nanosleep_args {
3695b870b7bSPeter Wemm 	struct	timespec *rqtp;
3705b870b7bSPeter Wemm 	struct	timespec *rmtp;
3715b870b7bSPeter Wemm };
3725b870b7bSPeter Wemm #endif
3735b870b7bSPeter Wemm 
374fb99ab88SMatthew Dillon /*
375fb99ab88SMatthew Dillon  * MPSAFE
376fb99ab88SMatthew Dillon  */
3775b870b7bSPeter Wemm /* ARGSUSED */
3785b870b7bSPeter Wemm int
37991afe087SPoul-Henning Kamp nanosleep(struct thread *td, struct nanosleep_args *uap)
3805b870b7bSPeter Wemm {
3815b870b7bSPeter Wemm 	struct timespec rmt, rqt;
382fb99ab88SMatthew Dillon 	int error;
3835b870b7bSPeter Wemm 
384d1e405c5SAlfred Perlstein 	error = copyin(uap->rqtp, &rqt, sizeof(rqt));
3855b870b7bSPeter Wemm 	if (error)
3865b870b7bSPeter Wemm 		return (error);
387fb99ab88SMatthew Dillon 
38831f3e2adSAlfred Perlstein 	if (uap->rmtp &&
38931f3e2adSAlfred Perlstein 	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
39031f3e2adSAlfred Perlstein 			return (EFAULT);
3917fdf2c85SPaul Saab 	error = kern_nanosleep(td, &rqt, &rmt);
392d1e405c5SAlfred Perlstein 	if (error && uap->rmtp) {
393fb99ab88SMatthew Dillon 		int error2;
394fb99ab88SMatthew Dillon 
395d1e405c5SAlfred Perlstein 		error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
39631f3e2adSAlfred Perlstein 		if (error2)
397fb99ab88SMatthew Dillon 			error = error2;
398708e7684SPeter Wemm 	}
399708e7684SPeter Wemm 	return (error);
40094c8fcd8SPeter Wemm }
40194c8fcd8SPeter Wemm 
4025b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
403df8bae1dSRodney W. Grimes struct gettimeofday_args {
404df8bae1dSRodney W. Grimes 	struct	timeval *tp;
405df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
406df8bae1dSRodney W. Grimes };
407d2d3e875SBruce Evans #endif
408fb99ab88SMatthew Dillon /*
409fb99ab88SMatthew Dillon  * MPSAFE
410fb99ab88SMatthew Dillon  */
411df8bae1dSRodney W. Grimes /* ARGSUSED */
41226f9a767SRodney W. Grimes int
41391afe087SPoul-Henning Kamp gettimeofday(struct thread *td, struct gettimeofday_args *uap)
414df8bae1dSRodney W. Grimes {
415df8bae1dSRodney W. Grimes 	struct timeval atv;
416411c25edSTim J. Robbins 	struct timezone rtz;
417df8bae1dSRodney W. Grimes 	int error = 0;
418df8bae1dSRodney W. Grimes 
419df8bae1dSRodney W. Grimes 	if (uap->tp) {
420df8bae1dSRodney W. Grimes 		microtime(&atv);
42101609114SAlfred Perlstein 		error = copyout(&atv, uap->tp, sizeof (atv));
422df8bae1dSRodney W. Grimes 	}
42321dcdb38SPoul-Henning Kamp 	if (error == 0 && uap->tzp != NULL) {
42491f1c2b3SPoul-Henning Kamp 		rtz.tz_minuteswest = tz_minuteswest;
42591f1c2b3SPoul-Henning Kamp 		rtz.tz_dsttime = tz_dsttime;
426411c25edSTim J. Robbins 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
42721dcdb38SPoul-Henning Kamp 	}
428df8bae1dSRodney W. Grimes 	return (error);
429df8bae1dSRodney W. Grimes }
430df8bae1dSRodney W. Grimes 
431d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
432df8bae1dSRodney W. Grimes struct settimeofday_args {
433df8bae1dSRodney W. Grimes 	struct	timeval *tv;
434df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
435df8bae1dSRodney W. Grimes };
436d2d3e875SBruce Evans #endif
437fb99ab88SMatthew Dillon /*
438fb99ab88SMatthew Dillon  * MPSAFE
439fb99ab88SMatthew Dillon  */
440df8bae1dSRodney W. Grimes /* ARGSUSED */
44126f9a767SRodney W. Grimes int
44291afe087SPoul-Henning Kamp settimeofday(struct thread *td, struct settimeofday_args *uap)
443df8bae1dSRodney W. Grimes {
444b88ec951SJohn Baldwin 	struct timeval atv, *tvp;
445b88ec951SJohn Baldwin 	struct timezone atz, *tzp;
446b88ec951SJohn Baldwin 	int error;
447b88ec951SJohn Baldwin 
448b88ec951SJohn Baldwin 	if (uap->tv) {
449b88ec951SJohn Baldwin 		error = copyin(uap->tv, &atv, sizeof(atv));
450b88ec951SJohn Baldwin 		if (error)
451b88ec951SJohn Baldwin 			return (error);
452b88ec951SJohn Baldwin 		tvp = &atv;
453b88ec951SJohn Baldwin 	} else
454b88ec951SJohn Baldwin 		tvp = NULL;
455b88ec951SJohn Baldwin 	if (uap->tzp) {
456b88ec951SJohn Baldwin 		error = copyin(uap->tzp, &atz, sizeof(atz));
457b88ec951SJohn Baldwin 		if (error)
458b88ec951SJohn Baldwin 			return (error);
459b88ec951SJohn Baldwin 		tzp = &atz;
460b88ec951SJohn Baldwin 	} else
461b88ec951SJohn Baldwin 		tzp = NULL;
462b88ec951SJohn Baldwin 	return (kern_settimeofday(td, tvp, tzp));
463b88ec951SJohn Baldwin }
464b88ec951SJohn Baldwin 
465b88ec951SJohn Baldwin int
466b88ec951SJohn Baldwin kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
467b88ec951SJohn Baldwin {
468b88ec951SJohn Baldwin 	int error;
469fb99ab88SMatthew Dillon 
4704b8d5f2dSRobert Watson #ifdef MAC
4714b8d5f2dSRobert Watson 	error = mac_check_system_settime(td->td_ucred);
4724b8d5f2dSRobert Watson 	if (error)
4734b8d5f2dSRobert Watson 		return (error);
4744b8d5f2dSRobert Watson #endif
475b88ec951SJohn Baldwin 	error = suser(td);
476b88ec951SJohn Baldwin 	if (error)
4777edfb592SJohn Baldwin 		return (error);
478df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
479b88ec951SJohn Baldwin 	if (tv) {
480b88ec951SJohn Baldwin 		if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
4817edfb592SJohn Baldwin 			return (EINVAL);
482b88ec951SJohn Baldwin 		error = settime(td, tv);
483708e7684SPeter Wemm 	}
484b88ec951SJohn Baldwin 	if (tzp && error == 0) {
485b88ec951SJohn Baldwin 		tz_minuteswest = tzp->tz_minuteswest;
486b88ec951SJohn Baldwin 		tz_dsttime = tzp->tz_dsttime;
487b88ec951SJohn Baldwin 	}
4887edfb592SJohn Baldwin 	return (error);
489b88ec951SJohn Baldwin }
4907edfb592SJohn Baldwin 
491df8bae1dSRodney W. Grimes /*
492df8bae1dSRodney W. Grimes  * Get value of an interval timer.  The process virtual and
493df8bae1dSRodney W. Grimes  * profiling virtual time timers are kept in the p_stats area, since
494df8bae1dSRodney W. Grimes  * they can be swapped out.  These are kept internally in the
495df8bae1dSRodney W. Grimes  * way they are specified externally: in time until they expire.
496df8bae1dSRodney W. Grimes  *
497df8bae1dSRodney W. Grimes  * The real time interval timer is kept in the process table slot
498df8bae1dSRodney W. Grimes  * for the process, and its value (it_value) is kept as an
499df8bae1dSRodney W. Grimes  * absolute time rather than as a delta, so that it is easy to keep
500df8bae1dSRodney W. Grimes  * periodic real-time signals from drifting.
501df8bae1dSRodney W. Grimes  *
502df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
503df8bae1dSRodney W. Grimes  * kern_clock.c.  The real time timer is processed by a timeout
504df8bae1dSRodney W. Grimes  * routine, called from the softclock() routine.  Since a callout
505df8bae1dSRodney W. Grimes  * may be delayed in real time due to interrupt processing in the system,
506df8bae1dSRodney W. Grimes  * it is possible for the real time timeout routine (realitexpire, given below),
507df8bae1dSRodney W. Grimes  * to be delayed in real time past when it is supposed to occur.  It
508df8bae1dSRodney W. Grimes  * does not suffice, therefore, to reload the real timer .it_value from the
509df8bae1dSRodney W. Grimes  * real time timers .it_interval.  Rather, we compute the next time in
510df8bae1dSRodney W. Grimes  * absolute time the timer should go off.
511df8bae1dSRodney W. Grimes  */
512d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
513df8bae1dSRodney W. Grimes struct getitimer_args {
514df8bae1dSRodney W. Grimes 	u_int	which;
515df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
516df8bae1dSRodney W. Grimes };
517d2d3e875SBruce Evans #endif
518fb99ab88SMatthew Dillon /*
519fb99ab88SMatthew Dillon  * MPSAFE
520fb99ab88SMatthew Dillon  */
52126f9a767SRodney W. Grimes int
52291afe087SPoul-Henning Kamp getitimer(struct thread *td, struct getitimer_args *uap)
523df8bae1dSRodney W. Grimes {
524df8bae1dSRodney W. Grimes 	struct itimerval aitv;
525c90110d6SJohn Baldwin 	int error;
526df8bae1dSRodney W. Grimes 
527cfa0efe7SMaxim Sobolev 	error = kern_getitimer(td, uap->which, &aitv);
528cfa0efe7SMaxim Sobolev 	if (error != 0)
529cfa0efe7SMaxim Sobolev 		return (error);
530cfa0efe7SMaxim Sobolev 	return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
531cfa0efe7SMaxim Sobolev }
532cfa0efe7SMaxim Sobolev 
533cfa0efe7SMaxim Sobolev int
534cfa0efe7SMaxim Sobolev kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
535cfa0efe7SMaxim Sobolev {
536cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
537cfa0efe7SMaxim Sobolev 	struct timeval ctv;
538cfa0efe7SMaxim Sobolev 
539cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
540df8bae1dSRodney W. Grimes 		return (EINVAL);
541fb99ab88SMatthew Dillon 
542cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
543df8bae1dSRodney W. Grimes 		/*
544ee002b68SBruce Evans 		 * Convert from absolute to relative time in .it_value
545df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
546df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
547df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
548df8bae1dSRodney W. Grimes 		 */
54996d7f8efSTim J. Robbins 		PROC_LOCK(p);
550cfa0efe7SMaxim Sobolev 		*aitv = p->p_realtimer;
55196d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
552cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
553c21410e1SPoul-Henning Kamp 			getmicrouptime(&ctv);
554cfa0efe7SMaxim Sobolev 			if (timevalcmp(&aitv->it_value, &ctv, <))
555cfa0efe7SMaxim Sobolev 				timevalclear(&aitv->it_value);
556df8bae1dSRodney W. Grimes 			else
557cfa0efe7SMaxim Sobolev 				timevalsub(&aitv->it_value, &ctv);
558227ee8a1SPoul-Henning Kamp 		}
559fb99ab88SMatthew Dillon 	} else {
56096d7f8efSTim J. Robbins 		mtx_lock_spin(&sched_lock);
561cfa0efe7SMaxim Sobolev 		*aitv = p->p_stats->p_timer[which];
56296d7f8efSTim J. Robbins 		mtx_unlock_spin(&sched_lock);
563fb99ab88SMatthew Dillon 	}
564cfa0efe7SMaxim Sobolev 	return (0);
565df8bae1dSRodney W. Grimes }
566df8bae1dSRodney W. Grimes 
567d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
568df8bae1dSRodney W. Grimes struct setitimer_args {
569df8bae1dSRodney W. Grimes 	u_int	which;
570df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
571df8bae1dSRodney W. Grimes };
572d2d3e875SBruce Evans #endif
573cfa0efe7SMaxim Sobolev 
574fb99ab88SMatthew Dillon /*
575fb99ab88SMatthew Dillon  * MPSAFE
576fb99ab88SMatthew Dillon  */
57726f9a767SRodney W. Grimes int
57891afe087SPoul-Henning Kamp setitimer(struct thread *td, struct setitimer_args *uap)
579df8bae1dSRodney W. Grimes {
580cfa0efe7SMaxim Sobolev 	struct itimerval aitv, oitv;
581c90110d6SJohn Baldwin 	int error;
58296d7f8efSTim J. Robbins 
58396d7f8efSTim J. Robbins 	if (uap->itv == NULL) {
58496d7f8efSTim J. Robbins 		uap->itv = uap->oitv;
58596d7f8efSTim J. Robbins 		return (getitimer(td, (struct getitimer_args *)uap));
58696d7f8efSTim J. Robbins 	}
587df8bae1dSRodney W. Grimes 
58896d7f8efSTim J. Robbins 	if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
589df8bae1dSRodney W. Grimes 		return (error);
590cfa0efe7SMaxim Sobolev 	error = kern_setitimer(td, uap->which, &aitv, &oitv);
591cfa0efe7SMaxim Sobolev 	if (error != 0 || uap->oitv == NULL)
592cfa0efe7SMaxim Sobolev 		return (error);
593cfa0efe7SMaxim Sobolev 	return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
594cfa0efe7SMaxim Sobolev }
595cfa0efe7SMaxim Sobolev 
596cfa0efe7SMaxim Sobolev int
597c90110d6SJohn Baldwin kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
598c90110d6SJohn Baldwin     struct itimerval *oitv)
599cfa0efe7SMaxim Sobolev {
600cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
601cfa0efe7SMaxim Sobolev 	struct timeval ctv;
602cfa0efe7SMaxim Sobolev 
6035e85ac17SJohn Baldwin 	if (aitv == NULL)
6045e85ac17SJohn Baldwin 		return (kern_getitimer(td, which, oitv));
6055e85ac17SJohn Baldwin 
606cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
60796d7f8efSTim J. Robbins 		return (EINVAL);
608cfa0efe7SMaxim Sobolev 	if (itimerfix(&aitv->it_value))
609cfa0efe7SMaxim Sobolev 		return (EINVAL);
610cfa0efe7SMaxim Sobolev 	if (!timevalisset(&aitv->it_value))
611cfa0efe7SMaxim Sobolev 		timevalclear(&aitv->it_interval);
612cfa0efe7SMaxim Sobolev 	else if (itimerfix(&aitv->it_interval))
61396d7f8efSTim J. Robbins 		return (EINVAL);
61496d7f8efSTim J. Robbins 
615cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
61696d7f8efSTim J. Robbins 		PROC_LOCK(p);
6174cf41af3SPoul-Henning Kamp 		if (timevalisset(&p->p_realtimer.it_value))
6184f559836SJake Burkholder 			callout_stop(&p->p_itcallout);
61925b4d3a8SJohn Baldwin 		getmicrouptime(&ctv);
620cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
621cfa0efe7SMaxim Sobolev 			callout_reset(&p->p_itcallout, tvtohz(&aitv->it_value),
6224f559836SJake Burkholder 			    realitexpire, p);
623cfa0efe7SMaxim Sobolev 			timevaladd(&aitv->it_value, &ctv);
62425b4d3a8SJohn Baldwin 		}
625cfa0efe7SMaxim Sobolev 		*oitv = p->p_realtimer;
626cfa0efe7SMaxim Sobolev 		p->p_realtimer = *aitv;
62796d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
628cfa0efe7SMaxim Sobolev 		if (timevalisset(&oitv->it_value)) {
629cfa0efe7SMaxim Sobolev 			if (timevalcmp(&oitv->it_value, &ctv, <))
630cfa0efe7SMaxim Sobolev 				timevalclear(&oitv->it_value);
63196d7f8efSTim J. Robbins 			else
632cfa0efe7SMaxim Sobolev 				timevalsub(&oitv->it_value, &ctv);
633fb99ab88SMatthew Dillon 		}
63496d7f8efSTim J. Robbins 	} else {
63596d7f8efSTim J. Robbins 		mtx_lock_spin(&sched_lock);
636cfa0efe7SMaxim Sobolev 		*oitv = p->p_stats->p_timer[which];
637cfa0efe7SMaxim Sobolev 		p->p_stats->p_timer[which] = *aitv;
63896d7f8efSTim J. Robbins 		mtx_unlock_spin(&sched_lock);
63996d7f8efSTim J. Robbins 	}
64096d7f8efSTim J. Robbins 	return (0);
641df8bae1dSRodney W. Grimes }
642df8bae1dSRodney W. Grimes 
643df8bae1dSRodney W. Grimes /*
644df8bae1dSRodney W. Grimes  * Real interval timer expired:
645df8bae1dSRodney W. Grimes  * send process whose timer expired an alarm signal.
646df8bae1dSRodney W. Grimes  * If time is not set up to reload, then just return.
647df8bae1dSRodney W. Grimes  * Else compute next time timer should go off which is > current time.
648df8bae1dSRodney W. Grimes  * This is where delay in processing this timeout causes multiple
649df8bae1dSRodney W. Grimes  * SIGALRM calls to be compressed into one.
650c8b47828SBruce Evans  * tvtohz() always adds 1 to allow for the time until the next clock
6519207f00aSBruce Evans  * interrupt being strictly less than 1 clock tick, but we don't want
6529207f00aSBruce Evans  * that here since we want to appear to be in sync with the clock
6539207f00aSBruce Evans  * interrupt even when we're delayed.
654df8bae1dSRodney W. Grimes  */
655df8bae1dSRodney W. Grimes void
65691afe087SPoul-Henning Kamp realitexpire(void *arg)
657df8bae1dSRodney W. Grimes {
65891afe087SPoul-Henning Kamp 	struct proc *p;
659bfe6c9faSPoul-Henning Kamp 	struct timeval ctv, ntv;
660df8bae1dSRodney W. Grimes 
661df8bae1dSRodney W. Grimes 	p = (struct proc *)arg;
66237824023SJohn Baldwin 	PROC_LOCK(p);
663df8bae1dSRodney W. Grimes 	psignal(p, SIGALRM);
6644cf41af3SPoul-Henning Kamp 	if (!timevalisset(&p->p_realtimer.it_interval)) {
6654cf41af3SPoul-Henning Kamp 		timevalclear(&p->p_realtimer.it_value);
6665499ea01SJohn Baldwin 		if (p->p_flag & P_WEXIT)
6675499ea01SJohn Baldwin 			wakeup(&p->p_itcallout);
66837824023SJohn Baldwin 		PROC_UNLOCK(p);
669df8bae1dSRodney W. Grimes 		return;
670df8bae1dSRodney W. Grimes 	}
671df8bae1dSRodney W. Grimes 	for (;;) {
672df8bae1dSRodney W. Grimes 		timevaladd(&p->p_realtimer.it_value,
673df8bae1dSRodney W. Grimes 		    &p->p_realtimer.it_interval);
674c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
6754cf41af3SPoul-Henning Kamp 		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
676bfe6c9faSPoul-Henning Kamp 			ntv = p->p_realtimer.it_value;
677bfe6c9faSPoul-Henning Kamp 			timevalsub(&ntv, &ctv);
6784f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
6794f559836SJake Burkholder 			    realitexpire, p);
68037824023SJohn Baldwin 			PROC_UNLOCK(p);
681df8bae1dSRodney W. Grimes 			return;
682df8bae1dSRodney W. Grimes 		}
683df8bae1dSRodney W. Grimes 	}
68437824023SJohn Baldwin 	/*NOTREACHED*/
685df8bae1dSRodney W. Grimes }
686df8bae1dSRodney W. Grimes 
687df8bae1dSRodney W. Grimes /*
688df8bae1dSRodney W. Grimes  * Check that a proposed value to load into the .it_value or
689df8bae1dSRodney W. Grimes  * .it_interval part of an interval timer is acceptable, and
690df8bae1dSRodney W. Grimes  * fix it to have at least minimal value (i.e. if it is less
691df8bae1dSRodney W. Grimes  * than the resolution of the clock, round it up.)
692df8bae1dSRodney W. Grimes  */
69326f9a767SRodney W. Grimes int
69491afe087SPoul-Henning Kamp itimerfix(struct timeval *tv)
695df8bae1dSRodney W. Grimes {
696df8bae1dSRodney W. Grimes 
69786857b36SDavid Xu 	if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
698df8bae1dSRodney W. Grimes 		return (EINVAL);
699df8bae1dSRodney W. Grimes 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
700df8bae1dSRodney W. Grimes 		tv->tv_usec = tick;
701df8bae1dSRodney W. Grimes 	return (0);
702df8bae1dSRodney W. Grimes }
703df8bae1dSRodney W. Grimes 
704df8bae1dSRodney W. Grimes /*
705df8bae1dSRodney W. Grimes  * Decrement an interval timer by a specified number
706df8bae1dSRodney W. Grimes  * of microseconds, which must be less than a second,
707df8bae1dSRodney W. Grimes  * i.e. < 1000000.  If the timer expires, then reload
708df8bae1dSRodney W. Grimes  * it.  In this case, carry over (usec - old value) to
709df8bae1dSRodney W. Grimes  * reduce the value reloaded into the timer so that
710df8bae1dSRodney W. Grimes  * the timer does not drift.  This routine assumes
711df8bae1dSRodney W. Grimes  * that it is called in a context where the timers
712df8bae1dSRodney W. Grimes  * on which it is operating cannot change in value.
713df8bae1dSRodney W. Grimes  */
71426f9a767SRodney W. Grimes int
71591afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec)
716df8bae1dSRodney W. Grimes {
717df8bae1dSRodney W. Grimes 
718df8bae1dSRodney W. Grimes 	if (itp->it_value.tv_usec < usec) {
719df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_sec == 0) {
720df8bae1dSRodney W. Grimes 			/* expired, and already in next interval */
721df8bae1dSRodney W. Grimes 			usec -= itp->it_value.tv_usec;
722df8bae1dSRodney W. Grimes 			goto expire;
723df8bae1dSRodney W. Grimes 		}
724df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec += 1000000;
725df8bae1dSRodney W. Grimes 		itp->it_value.tv_sec--;
726df8bae1dSRodney W. Grimes 	}
727df8bae1dSRodney W. Grimes 	itp->it_value.tv_usec -= usec;
728df8bae1dSRodney W. Grimes 	usec = 0;
7294cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_value))
730df8bae1dSRodney W. Grimes 		return (1);
731df8bae1dSRodney W. Grimes 	/* expired, exactly at end of interval */
732df8bae1dSRodney W. Grimes expire:
7334cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_interval)) {
734df8bae1dSRodney W. Grimes 		itp->it_value = itp->it_interval;
735df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec -= usec;
736df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_usec < 0) {
737df8bae1dSRodney W. Grimes 			itp->it_value.tv_usec += 1000000;
738df8bae1dSRodney W. Grimes 			itp->it_value.tv_sec--;
739df8bae1dSRodney W. Grimes 		}
740df8bae1dSRodney W. Grimes 	} else
741df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
742df8bae1dSRodney W. Grimes 	return (0);
743df8bae1dSRodney W. Grimes }
744df8bae1dSRodney W. Grimes 
745df8bae1dSRodney W. Grimes /*
746df8bae1dSRodney W. Grimes  * Add and subtract routines for timevals.
747df8bae1dSRodney W. Grimes  * N.B.: subtract routine doesn't deal with
748df8bae1dSRodney W. Grimes  * results which are before the beginning,
749df8bae1dSRodney W. Grimes  * it just gets very confused in this case.
750df8bae1dSRodney W. Grimes  * Caveat emptor.
751df8bae1dSRodney W. Grimes  */
75226f9a767SRodney W. Grimes void
7536ff7636eSAlfred Perlstein timevaladd(struct timeval *t1, const struct timeval *t2)
754df8bae1dSRodney W. Grimes {
755df8bae1dSRodney W. Grimes 
756df8bae1dSRodney W. Grimes 	t1->tv_sec += t2->tv_sec;
757df8bae1dSRodney W. Grimes 	t1->tv_usec += t2->tv_usec;
758df8bae1dSRodney W. Grimes 	timevalfix(t1);
759df8bae1dSRodney W. Grimes }
760df8bae1dSRodney W. Grimes 
76126f9a767SRodney W. Grimes void
7626ff7636eSAlfred Perlstein timevalsub(struct timeval *t1, const struct timeval *t2)
763df8bae1dSRodney W. Grimes {
764df8bae1dSRodney W. Grimes 
765df8bae1dSRodney W. Grimes 	t1->tv_sec -= t2->tv_sec;
766df8bae1dSRodney W. Grimes 	t1->tv_usec -= t2->tv_usec;
767df8bae1dSRodney W. Grimes 	timevalfix(t1);
768df8bae1dSRodney W. Grimes }
769df8bae1dSRodney W. Grimes 
77087b6de2bSPoul-Henning Kamp static void
77191afe087SPoul-Henning Kamp timevalfix(struct timeval *t1)
772df8bae1dSRodney W. Grimes {
773df8bae1dSRodney W. Grimes 
774df8bae1dSRodney W. Grimes 	if (t1->tv_usec < 0) {
775df8bae1dSRodney W. Grimes 		t1->tv_sec--;
776df8bae1dSRodney W. Grimes 		t1->tv_usec += 1000000;
777df8bae1dSRodney W. Grimes 	}
778df8bae1dSRodney W. Grimes 	if (t1->tv_usec >= 1000000) {
779df8bae1dSRodney W. Grimes 		t1->tv_sec++;
780df8bae1dSRodney W. Grimes 		t1->tv_usec -= 1000000;
781df8bae1dSRodney W. Grimes 	}
782df8bae1dSRodney W. Grimes }
78391974ce1SSam Leffler 
78491974ce1SSam Leffler /*
785addea9d4SSam Leffler  * ratecheck(): simple time-based rate-limit checking.
78691974ce1SSam Leffler  */
78791974ce1SSam Leffler int
78891974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
78991974ce1SSam Leffler {
79091974ce1SSam Leffler 	struct timeval tv, delta;
79191974ce1SSam Leffler 	int rv = 0;
79291974ce1SSam Leffler 
793addea9d4SSam Leffler 	getmicrouptime(&tv);		/* NB: 10ms precision */
794addea9d4SSam Leffler 	delta = tv;
795addea9d4SSam Leffler 	timevalsub(&delta, lasttime);
79691974ce1SSam Leffler 
79791974ce1SSam Leffler 	/*
79891974ce1SSam Leffler 	 * check for 0,0 is so that the message will be seen at least once,
79991974ce1SSam Leffler 	 * even if interval is huge.
80091974ce1SSam Leffler 	 */
80191974ce1SSam Leffler 	if (timevalcmp(&delta, mininterval, >=) ||
80291974ce1SSam Leffler 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
80391974ce1SSam Leffler 		*lasttime = tv;
80491974ce1SSam Leffler 		rv = 1;
80591974ce1SSam Leffler 	}
80691974ce1SSam Leffler 
80791974ce1SSam Leffler 	return (rv);
80891974ce1SSam Leffler }
80991974ce1SSam Leffler 
81091974ce1SSam Leffler /*
81191974ce1SSam Leffler  * ppsratecheck(): packets (or events) per second limitation.
812addea9d4SSam Leffler  *
813addea9d4SSam Leffler  * Return 0 if the limit is to be enforced (e.g. the caller
814addea9d4SSam Leffler  * should drop a packet because of the rate limitation).
815addea9d4SSam Leffler  *
816893bec80SSam Leffler  * maxpps of 0 always causes zero to be returned.  maxpps of -1
817893bec80SSam Leffler  * always causes 1 to be returned; this effectively defeats rate
818893bec80SSam Leffler  * limiting.
819893bec80SSam Leffler  *
820addea9d4SSam Leffler  * Note that we maintain the struct timeval for compatibility
821addea9d4SSam Leffler  * with other bsd systems.  We reuse the storage and just monitor
822addea9d4SSam Leffler  * clock ticks for minimal overhead.
82391974ce1SSam Leffler  */
82491974ce1SSam Leffler int
82591974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
82691974ce1SSam Leffler {
827addea9d4SSam Leffler 	int now;
82891974ce1SSam Leffler 
82991974ce1SSam Leffler 	/*
830addea9d4SSam Leffler 	 * Reset the last time and counter if this is the first call
831addea9d4SSam Leffler 	 * or more than a second has passed since the last update of
832addea9d4SSam Leffler 	 * lasttime.
83391974ce1SSam Leffler 	 */
834addea9d4SSam Leffler 	now = ticks;
835addea9d4SSam Leffler 	if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
836addea9d4SSam Leffler 		lasttime->tv_sec = now;
837addea9d4SSam Leffler 		*curpps = 1;
838893bec80SSam Leffler 		return (maxpps != 0);
839addea9d4SSam Leffler 	} else {
840addea9d4SSam Leffler 		(*curpps)++;		/* NB: ignore potential overflow */
841addea9d4SSam Leffler 		return (maxpps < 0 || *curpps < maxpps);
842addea9d4SSam Leffler 	}
84391974ce1SSam Leffler }
84486857b36SDavid Xu 
84586857b36SDavid Xu static void
84686857b36SDavid Xu itimer_start(void)
84786857b36SDavid Xu {
84886857b36SDavid Xu 	struct kclock rt_clock = {
84986857b36SDavid Xu 		.timer_create  = realtimer_create,
85086857b36SDavid Xu 		.timer_delete  = realtimer_delete,
85186857b36SDavid Xu 		.timer_settime = realtimer_settime,
85286857b36SDavid Xu 		.timer_gettime = realtimer_gettime,
85386857b36SDavid Xu 		.event_hook     = realtimer_event_hook
85486857b36SDavid Xu 	};
85586857b36SDavid Xu 
85686857b36SDavid Xu 	itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
85786857b36SDavid Xu 		NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
85886857b36SDavid Xu 	register_posix_clock(CLOCK_REALTIME,  &rt_clock);
85986857b36SDavid Xu 	register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
86086857b36SDavid Xu }
86186857b36SDavid Xu 
86286857b36SDavid Xu int
86386857b36SDavid Xu register_posix_clock(int clockid, struct kclock *clk)
86486857b36SDavid Xu {
86586857b36SDavid Xu 	if ((unsigned)clockid >= MAX_CLOCKS) {
86686857b36SDavid Xu 		printf("%s: invalid clockid\n", __func__);
86786857b36SDavid Xu 		return (0);
86886857b36SDavid Xu 	}
86986857b36SDavid Xu 	posix_clocks[clockid] = *clk;
87086857b36SDavid Xu 	return (1);
87186857b36SDavid Xu }
87286857b36SDavid Xu 
87386857b36SDavid Xu static int
87486857b36SDavid Xu itimer_init(void *mem, int size, int flags)
87586857b36SDavid Xu {
87686857b36SDavid Xu 	struct itimer *it;
87786857b36SDavid Xu 
87886857b36SDavid Xu 	it = (struct itimer *)mem;
87986857b36SDavid Xu 	mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
88086857b36SDavid Xu 	return (0);
88186857b36SDavid Xu }
88286857b36SDavid Xu 
88386857b36SDavid Xu static void
88486857b36SDavid Xu itimer_fini(void *mem, int size)
88586857b36SDavid Xu {
88686857b36SDavid Xu 	struct itimer *it;
88786857b36SDavid Xu 
88886857b36SDavid Xu 	it = (struct itimer *)mem;
88986857b36SDavid Xu 	mtx_destroy(&it->it_mtx);
89086857b36SDavid Xu }
89186857b36SDavid Xu 
89286857b36SDavid Xu static void
89386857b36SDavid Xu itimer_enter(struct itimer *it)
89486857b36SDavid Xu {
89586857b36SDavid Xu 
89686857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
89786857b36SDavid Xu 	it->it_usecount++;
89886857b36SDavid Xu }
89986857b36SDavid Xu 
90086857b36SDavid Xu static void
90186857b36SDavid Xu itimer_leave(struct itimer *it)
90286857b36SDavid Xu {
90386857b36SDavid Xu 
90486857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
90586857b36SDavid Xu 	KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
90686857b36SDavid Xu 
90786857b36SDavid Xu 	if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
90886857b36SDavid Xu 		wakeup(it);
90986857b36SDavid Xu }
91086857b36SDavid Xu 
91186857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
91286857b36SDavid Xu struct timer_create_args {
91386857b36SDavid Xu 	clockid_t clock_id;
91486857b36SDavid Xu 	struct sigevent * evp;
91586857b36SDavid Xu 	timer_t * timerid;
91686857b36SDavid Xu };
91786857b36SDavid Xu #endif
91886857b36SDavid Xu 
91986857b36SDavid Xu int
92086857b36SDavid Xu timer_create(struct thread *td, struct timer_create_args *uap)
92186857b36SDavid Xu {
92286857b36SDavid Xu 	struct sigevent *evp1, ev;
92386857b36SDavid Xu 	timer_t id;
92486857b36SDavid Xu 	int error;
92586857b36SDavid Xu 
92686857b36SDavid Xu 	if (uap->evp != NULL) {
92786857b36SDavid Xu 		error = copyin(uap->evp, &ev, sizeof(ev));
92886857b36SDavid Xu 		if (error != 0)
92986857b36SDavid Xu 			return (error);
93086857b36SDavid Xu 		evp1 = &ev;
93186857b36SDavid Xu 	} else
93286857b36SDavid Xu 		evp1 = NULL;
93386857b36SDavid Xu 
93486857b36SDavid Xu 	error = kern_timer_create(td, uap->clock_id, evp1, &id, -1);
93586857b36SDavid Xu 
93686857b36SDavid Xu 	if (error == 0) {
93786857b36SDavid Xu 		error = copyout(&id, uap->timerid, sizeof(timer_t));
93886857b36SDavid Xu 		if (error != 0)
93986857b36SDavid Xu 			kern_timer_delete(td, id);
94086857b36SDavid Xu 	}
94186857b36SDavid Xu 	return (error);
94286857b36SDavid Xu }
94386857b36SDavid Xu 
94486857b36SDavid Xu static int
94586857b36SDavid Xu kern_timer_create(struct thread *td, clockid_t clock_id,
94686857b36SDavid Xu 	struct sigevent *evp, timer_t *timerid, timer_t preset_id)
94786857b36SDavid Xu {
94886857b36SDavid Xu 	struct proc *p = td->td_proc;
94986857b36SDavid Xu 	struct itimer *it;
95086857b36SDavid Xu 	int id;
95186857b36SDavid Xu 	int error;
95286857b36SDavid Xu 
95386857b36SDavid Xu 	if (clock_id < 0 || clock_id >= MAX_CLOCKS)
95486857b36SDavid Xu 		return (EINVAL);
95586857b36SDavid Xu 
95686857b36SDavid Xu 	if (posix_clocks[clock_id].timer_create == NULL)
95786857b36SDavid Xu 		return (EINVAL);
95886857b36SDavid Xu 
95986857b36SDavid Xu 	if (evp != NULL) {
96086857b36SDavid Xu 		if (evp->sigev_notify != SIGEV_NONE &&
96156c06c4bSDavid Xu 		    evp->sigev_notify != SIGEV_SIGNAL &&
96256c06c4bSDavid Xu 		    evp->sigev_notify != SIGEV_THREAD_ID)
96386857b36SDavid Xu 			return (EINVAL);
96456c06c4bSDavid Xu 		if ((evp->sigev_notify == SIGEV_SIGNAL ||
96556c06c4bSDavid Xu 		     evp->sigev_notify == SIGEV_THREAD_ID) &&
96686857b36SDavid Xu 			!_SIG_VALID(evp->sigev_signo))
96786857b36SDavid Xu 			return (EINVAL);
96886857b36SDavid Xu 	}
96986857b36SDavid Xu 
97060354683SDavid Xu 	if (p->p_itimers == NULL)
97186857b36SDavid Xu 		itimers_alloc(p);
97286857b36SDavid Xu 
97386857b36SDavid Xu 	it = uma_zalloc(itimer_zone, M_WAITOK);
97486857b36SDavid Xu 	it->it_flags = 0;
97586857b36SDavid Xu 	it->it_usecount = 0;
97686857b36SDavid Xu 	it->it_active = 0;
97756c06c4bSDavid Xu 	timespecclear(&it->it_time.it_value);
97856c06c4bSDavid Xu 	timespecclear(&it->it_time.it_interval);
97986857b36SDavid Xu 	it->it_overrun = 0;
98086857b36SDavid Xu 	it->it_overrun_last = 0;
98186857b36SDavid Xu 	it->it_clockid = clock_id;
98286857b36SDavid Xu 	it->it_timerid = -1;
98386857b36SDavid Xu 	it->it_proc = p;
98486857b36SDavid Xu 	ksiginfo_init(&it->it_ksi);
98586857b36SDavid Xu 	it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
98686857b36SDavid Xu 	error = CLOCK_CALL(clock_id, timer_create, (it));
98786857b36SDavid Xu 	if (error != 0)
98886857b36SDavid Xu 		goto out;
98986857b36SDavid Xu 
99086857b36SDavid Xu 	PROC_LOCK(p);
99186857b36SDavid Xu 	if (preset_id != -1) {
99286857b36SDavid Xu 		KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
99386857b36SDavid Xu 		id = preset_id;
99460354683SDavid Xu 		if (p->p_itimers->its_timers[id] != NULL) {
99586857b36SDavid Xu 			PROC_UNLOCK(p);
99686857b36SDavid Xu 			error = 0;
99786857b36SDavid Xu 			goto out;
99886857b36SDavid Xu 		}
99986857b36SDavid Xu 	} else {
100086857b36SDavid Xu 		/*
100186857b36SDavid Xu 		 * Find a free timer slot, skipping those reserved
100286857b36SDavid Xu 		 * for setitimer().
100386857b36SDavid Xu 		 */
100486857b36SDavid Xu 		for (id = 3; id < TIMER_MAX; id++)
100560354683SDavid Xu 			if (p->p_itimers->its_timers[id] == NULL)
100686857b36SDavid Xu 				break;
100786857b36SDavid Xu 		if (id == TIMER_MAX) {
100886857b36SDavid Xu 			PROC_UNLOCK(p);
100986857b36SDavid Xu 			error = EAGAIN;
101086857b36SDavid Xu 			goto out;
101186857b36SDavid Xu 		}
101286857b36SDavid Xu 	}
101386857b36SDavid Xu 	it->it_timerid = id;
101460354683SDavid Xu 	p->p_itimers->its_timers[id] = it;
101586857b36SDavid Xu 	if (evp != NULL)
101686857b36SDavid Xu 		it->it_sigev = *evp;
101786857b36SDavid Xu 	else {
101886857b36SDavid Xu 		it->it_sigev.sigev_notify = SIGEV_SIGNAL;
101986857b36SDavid Xu 		switch (clock_id) {
102086857b36SDavid Xu 		default:
102186857b36SDavid Xu 		case CLOCK_REALTIME:
102286857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGALRM;
102386857b36SDavid Xu 			break;
102486857b36SDavid Xu 		case CLOCK_VIRTUAL:
102586857b36SDavid Xu  			it->it_sigev.sigev_signo = SIGVTALRM;
102686857b36SDavid Xu 			break;
102786857b36SDavid Xu 		case CLOCK_PROF:
102886857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGPROF;
102986857b36SDavid Xu 			break;
103086857b36SDavid Xu 		}
10318f0371f1SDavid Xu 		it->it_sigev.sigev_value.sival_int = id;
103286857b36SDavid Xu 	}
103386857b36SDavid Xu 
103456c06c4bSDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
103556c06c4bSDavid Xu 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
103686857b36SDavid Xu 		it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
103786857b36SDavid Xu 		it->it_ksi.ksi_code = SI_TIMER;
103886857b36SDavid Xu 		it->it_ksi.ksi_value = it->it_sigev.sigev_value;
103986857b36SDavid Xu 		it->it_ksi.ksi_timerid = id;
104086857b36SDavid Xu 	}
104186857b36SDavid Xu 	PROC_UNLOCK(p);
104286857b36SDavid Xu 	*timerid = id;
104386857b36SDavid Xu 	return (0);
104486857b36SDavid Xu 
104586857b36SDavid Xu out:
104686857b36SDavid Xu 	ITIMER_LOCK(it);
104786857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
104886857b36SDavid Xu 	ITIMER_UNLOCK(it);
104986857b36SDavid Xu 	uma_zfree(itimer_zone, it);
105086857b36SDavid Xu 	return (error);
105186857b36SDavid Xu }
105286857b36SDavid Xu 
105386857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
105486857b36SDavid Xu struct timer_delete_args {
105586857b36SDavid Xu 	timer_t timerid;
105686857b36SDavid Xu };
105786857b36SDavid Xu #endif
105886857b36SDavid Xu 
105986857b36SDavid Xu int
106086857b36SDavid Xu timer_delete(struct thread *td, struct timer_delete_args *uap)
106186857b36SDavid Xu {
106286857b36SDavid Xu 	return (kern_timer_delete(td, uap->timerid));
106386857b36SDavid Xu }
106486857b36SDavid Xu 
106586857b36SDavid Xu static struct itimer *
106686857b36SDavid Xu itimer_find(struct proc *p, timer_t timerid, int include_deleting)
106786857b36SDavid Xu {
106886857b36SDavid Xu 	struct itimer *it;
106986857b36SDavid Xu 
107086857b36SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
107160354683SDavid Xu 	if ((p->p_itimers == NULL) || (timerid >= TIMER_MAX) ||
107260354683SDavid Xu 	    (it = p->p_itimers->its_timers[timerid]) == NULL) {
107386857b36SDavid Xu 		return (NULL);
107486857b36SDavid Xu 	}
107586857b36SDavid Xu 	ITIMER_LOCK(it);
107686857b36SDavid Xu 	if (!include_deleting && (it->it_flags & ITF_DELETING) != 0) {
107786857b36SDavid Xu 		ITIMER_UNLOCK(it);
107886857b36SDavid Xu 		it = NULL;
107986857b36SDavid Xu 	}
108086857b36SDavid Xu 	return (it);
108186857b36SDavid Xu }
108286857b36SDavid Xu 
108386857b36SDavid Xu static int
108486857b36SDavid Xu kern_timer_delete(struct thread *td, timer_t timerid)
108586857b36SDavid Xu {
108686857b36SDavid Xu 	struct proc *p = td->td_proc;
108786857b36SDavid Xu 	struct itimer *it;
108886857b36SDavid Xu 
108986857b36SDavid Xu 	PROC_LOCK(p);
109086857b36SDavid Xu 	it = itimer_find(p, timerid, 0);
109186857b36SDavid Xu 	if (it == NULL) {
109286857b36SDavid Xu 		PROC_UNLOCK(p);
109386857b36SDavid Xu 		return (EINVAL);
109486857b36SDavid Xu 	}
109586857b36SDavid Xu 	PROC_UNLOCK(p);
109686857b36SDavid Xu 
109786857b36SDavid Xu 	it->it_flags |= ITF_DELETING;
109886857b36SDavid Xu 	while (it->it_usecount > 0) {
109986857b36SDavid Xu 		it->it_flags |= ITF_WANTED;
110086857b36SDavid Xu 		msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
110186857b36SDavid Xu 	}
110286857b36SDavid Xu 	it->it_flags &= ~ITF_WANTED;
110386857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
110486857b36SDavid Xu 	ITIMER_UNLOCK(it);
110586857b36SDavid Xu 
110686857b36SDavid Xu 	PROC_LOCK(p);
110786857b36SDavid Xu 	if (KSI_ONQ(&it->it_ksi))
110886857b36SDavid Xu 		sigqueue_take(&it->it_ksi);
110960354683SDavid Xu 	p->p_itimers->its_timers[timerid] = NULL;
111086857b36SDavid Xu 	PROC_UNLOCK(p);
111186857b36SDavid Xu 	uma_zfree(itimer_zone, it);
111286857b36SDavid Xu 	return (0);
111386857b36SDavid Xu }
111486857b36SDavid Xu 
111586857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
111686857b36SDavid Xu struct timer_settime_args {
111786857b36SDavid Xu 	timer_t timerid;
111886857b36SDavid Xu 	int flags;
111986857b36SDavid Xu 	const struct itimerspec * value;
112086857b36SDavid Xu 	struct itimerspec * ovalue;
112186857b36SDavid Xu };
112286857b36SDavid Xu #endif
112386857b36SDavid Xu 
112486857b36SDavid Xu int
112586857b36SDavid Xu timer_settime(struct thread *td, struct timer_settime_args *uap)
112686857b36SDavid Xu {
112786857b36SDavid Xu 	struct proc *p = td->td_proc;
112886857b36SDavid Xu 	struct itimer *it;
112986857b36SDavid Xu 	struct itimerspec val, oval, *ovalp;
113086857b36SDavid Xu 	int error;
113186857b36SDavid Xu 
113286857b36SDavid Xu 	error = copyin(uap->value, &val, sizeof(val));
113386857b36SDavid Xu 	if (error != 0)
113486857b36SDavid Xu 		return (error);
113586857b36SDavid Xu 
113686857b36SDavid Xu 	if (uap->ovalue != NULL)
113786857b36SDavid Xu 		ovalp = &oval;
113886857b36SDavid Xu 	else
113986857b36SDavid Xu 		ovalp = NULL;
114086857b36SDavid Xu 
114186857b36SDavid Xu 	PROC_LOCK(p);
114286857b36SDavid Xu 	if (uap->timerid < 3 ||
114386857b36SDavid Xu 	    (it = itimer_find(p, uap->timerid, 0)) == NULL) {
114486857b36SDavid Xu 		PROC_UNLOCK(p);
114586857b36SDavid Xu 		error = EINVAL;
114686857b36SDavid Xu 	} else {
114786857b36SDavid Xu 		PROC_UNLOCK(p);
114886857b36SDavid Xu 		itimer_enter(it);
114986857b36SDavid Xu 		error = CLOCK_CALL(it->it_clockid, timer_settime,
115086857b36SDavid Xu 				(it, uap->flags, &val, ovalp));
115186857b36SDavid Xu 		itimer_leave(it);
115286857b36SDavid Xu 		ITIMER_UNLOCK(it);
115386857b36SDavid Xu 	}
115486857b36SDavid Xu 	if (error == 0 && uap->ovalue != NULL)
115586857b36SDavid Xu 		error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
115686857b36SDavid Xu 	return (error);
115786857b36SDavid Xu }
115886857b36SDavid Xu 
115986857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
116086857b36SDavid Xu struct timer_gettime_args {
116186857b36SDavid Xu 	timer_t timerid;
116286857b36SDavid Xu 	struct itimerspec * value;
116386857b36SDavid Xu };
116486857b36SDavid Xu #endif
116586857b36SDavid Xu 
116686857b36SDavid Xu int
116786857b36SDavid Xu timer_gettime(struct thread *td, struct timer_gettime_args *uap)
116886857b36SDavid Xu {
116986857b36SDavid Xu 	struct proc *p = td->td_proc;
117086857b36SDavid Xu 	struct itimer *it;
117186857b36SDavid Xu 	struct itimerspec val;
117286857b36SDavid Xu 	int error;
117386857b36SDavid Xu 
117486857b36SDavid Xu 	PROC_LOCK(p);
117586857b36SDavid Xu 	if (uap->timerid < 3 ||
117686857b36SDavid Xu 	   (it = itimer_find(p, uap->timerid, 0)) == NULL) {
117786857b36SDavid Xu 		PROC_UNLOCK(p);
117886857b36SDavid Xu 		error = EINVAL;
117986857b36SDavid Xu 	} else {
118086857b36SDavid Xu 		PROC_UNLOCK(p);
118186857b36SDavid Xu 		itimer_enter(it);
118286857b36SDavid Xu 		error = CLOCK_CALL(it->it_clockid, timer_gettime,
118386857b36SDavid Xu 				(it, &val));
118486857b36SDavid Xu 		itimer_leave(it);
118586857b36SDavid Xu 		ITIMER_UNLOCK(it);
118686857b36SDavid Xu 	}
118786857b36SDavid Xu 	if (error == 0)
118886857b36SDavid Xu 		error = copyout(&val, uap->value, sizeof(val));
118986857b36SDavid Xu 	return (error);
119086857b36SDavid Xu }
119186857b36SDavid Xu 
119286857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
119386857b36SDavid Xu struct timer_getoverrun_args {
119486857b36SDavid Xu 	timer_t timerid;
119586857b36SDavid Xu };
119686857b36SDavid Xu #endif
119786857b36SDavid Xu 
119886857b36SDavid Xu int
119986857b36SDavid Xu timer_getoverrun(struct thread *td, struct timer_getoverrun_args *uap)
120086857b36SDavid Xu {
120186857b36SDavid Xu 	struct proc *p = td->td_proc;
120286857b36SDavid Xu 	struct itimer *it;
120386857b36SDavid Xu 	int error ;
120486857b36SDavid Xu 
120586857b36SDavid Xu 	PROC_LOCK(p);
120686857b36SDavid Xu 	if (uap->timerid < 3 ||
120786857b36SDavid Xu 	    (it = itimer_find(p, uap->timerid, 0)) == NULL) {
120886857b36SDavid Xu 		PROC_UNLOCK(p);
120986857b36SDavid Xu 		error = EINVAL;
121086857b36SDavid Xu 	} else {
121186857b36SDavid Xu 		td->td_retval[0] = it->it_overrun_last;
121286857b36SDavid Xu 		ITIMER_UNLOCK(it);
121356c06c4bSDavid Xu 		PROC_UNLOCK(p);
121486857b36SDavid Xu 		error = 0;
121586857b36SDavid Xu 	}
121686857b36SDavid Xu 	return (error);
121786857b36SDavid Xu }
121886857b36SDavid Xu 
121986857b36SDavid Xu static int
122086857b36SDavid Xu realtimer_create(struct itimer *it)
122186857b36SDavid Xu {
122286857b36SDavid Xu 	callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
122386857b36SDavid Xu 	return (0);
122486857b36SDavid Xu }
122586857b36SDavid Xu 
122686857b36SDavid Xu static int
122786857b36SDavid Xu realtimer_delete(struct itimer *it)
122886857b36SDavid Xu {
122986857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
123086857b36SDavid Xu 	callout_stop(&it->it_callout);
123186857b36SDavid Xu 	return (0);
123286857b36SDavid Xu }
123386857b36SDavid Xu 
123486857b36SDavid Xu static int
123586857b36SDavid Xu realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
123686857b36SDavid Xu {
123756c06c4bSDavid Xu 	struct timespec cts;
123886857b36SDavid Xu 
123986857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
124086857b36SDavid Xu 
124156c06c4bSDavid Xu 	realtimer_clocktime(it->it_clockid, &cts);
124256c06c4bSDavid Xu 	*ovalue = it->it_time;
124386857b36SDavid Xu 	if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
124456c06c4bSDavid Xu 		timespecsub(&ovalue->it_value, &cts);
124586857b36SDavid Xu 		if (ovalue->it_value.tv_sec < 0 ||
124686857b36SDavid Xu 		    (ovalue->it_value.tv_sec == 0 &&
124786857b36SDavid Xu 		     ovalue->it_value.tv_nsec == 0)) {
124886857b36SDavid Xu 			ovalue->it_value.tv_sec  = 0;
124986857b36SDavid Xu 			ovalue->it_value.tv_nsec = 1;
125086857b36SDavid Xu 		}
125186857b36SDavid Xu 	}
125286857b36SDavid Xu 	return (0);
125386857b36SDavid Xu }
125486857b36SDavid Xu 
125586857b36SDavid Xu static int
125686857b36SDavid Xu realtimer_settime(struct itimer *it, int flags,
125786857b36SDavid Xu 	struct itimerspec *value, struct itimerspec *ovalue)
125886857b36SDavid Xu {
125956c06c4bSDavid Xu 	struct timespec cts, ts;
126056c06c4bSDavid Xu 	struct timeval tv;
126156c06c4bSDavid Xu 	struct itimerspec val;
126286857b36SDavid Xu 
126386857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
126486857b36SDavid Xu 
126556c06c4bSDavid Xu 	val = *value;
126656c06c4bSDavid Xu 	if (itimespecfix(&val.it_value))
126786857b36SDavid Xu 		return (EINVAL);
126886857b36SDavid Xu 
126956c06c4bSDavid Xu 	if (timespecisset(&val.it_value)) {
127056c06c4bSDavid Xu 		if (itimespecfix(&val.it_interval))
127186857b36SDavid Xu 			return (EINVAL);
127286857b36SDavid Xu 	} else {
127356c06c4bSDavid Xu 		timespecclear(&val.it_interval);
127486857b36SDavid Xu 	}
127586857b36SDavid Xu 
127686857b36SDavid Xu 	if (ovalue != NULL)
127786857b36SDavid Xu 		realtimer_gettime(it, ovalue);
127886857b36SDavid Xu 
127986857b36SDavid Xu 	it->it_time = val;
128056c06c4bSDavid Xu 	if (timespecisset(&val.it_value)) {
128156c06c4bSDavid Xu 		realtimer_clocktime(it->it_clockid, &cts);
128256c06c4bSDavid Xu 		ts = val.it_value;
128386857b36SDavid Xu 		if ((flags & TIMER_ABSTIME) == 0) {
128486857b36SDavid Xu 			/* Convert to absolute time. */
128556c06c4bSDavid Xu 			timespecadd(&it->it_time.it_value, &cts);
128686857b36SDavid Xu 		} else {
128756c06c4bSDavid Xu 			timespecsub(&ts, &cts);
128886857b36SDavid Xu 			/*
128956c06c4bSDavid Xu 			 * We don't care if ts is negative, tztohz will
129086857b36SDavid Xu 			 * fix it.
129186857b36SDavid Xu 			 */
129286857b36SDavid Xu 		}
129356c06c4bSDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
129456c06c4bSDavid Xu 		callout_reset(&it->it_callout, tvtohz(&tv),
129586857b36SDavid Xu 			realtimer_expire, it);
129686857b36SDavid Xu 	} else {
129786857b36SDavid Xu 		callout_stop(&it->it_callout);
129886857b36SDavid Xu 	}
129986857b36SDavid Xu 
130086857b36SDavid Xu 	return (0);
130186857b36SDavid Xu }
130286857b36SDavid Xu 
130386857b36SDavid Xu static void
130456c06c4bSDavid Xu realtimer_clocktime(clockid_t id, struct timespec *ts)
130586857b36SDavid Xu {
130686857b36SDavid Xu 	if (id == CLOCK_REALTIME)
130756c06c4bSDavid Xu 		getnanotime(ts);
130886857b36SDavid Xu 	else	/* CLOCK_MONOTONIC */
130956c06c4bSDavid Xu 		getnanouptime(ts);
131056c06c4bSDavid Xu }
131156c06c4bSDavid Xu 
131256c06c4bSDavid Xu int
131356c06c4bSDavid Xu itimer_accept(struct proc *p, timer_t timerid, ksiginfo_t *ksi)
131456c06c4bSDavid Xu {
131556c06c4bSDavid Xu 	struct itimer *it;
131656c06c4bSDavid Xu 
131756c06c4bSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
131856c06c4bSDavid Xu 	it = itimer_find(p, timerid, 0);
131956c06c4bSDavid Xu 	if (it != NULL) {
132056c06c4bSDavid Xu 		ksi->ksi_overrun = it->it_overrun;
132156c06c4bSDavid Xu 		it->it_overrun_last = it->it_overrun;
132256c06c4bSDavid Xu 		it->it_overrun = 0;
132356c06c4bSDavid Xu 		ITIMER_UNLOCK(it);
132456c06c4bSDavid Xu 		return (0);
132556c06c4bSDavid Xu 	}
132656c06c4bSDavid Xu 	return (EINVAL);
132756c06c4bSDavid Xu }
132856c06c4bSDavid Xu 
132956c06c4bSDavid Xu int
133056c06c4bSDavid Xu itimespecfix(struct timespec *ts)
133156c06c4bSDavid Xu {
133256c06c4bSDavid Xu 
133356c06c4bSDavid Xu 	if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
133456c06c4bSDavid Xu 		return (EINVAL);
133556c06c4bSDavid Xu 	if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
133656c06c4bSDavid Xu 		ts->tv_nsec = tick * 1000;
133756c06c4bSDavid Xu 	return (0);
133886857b36SDavid Xu }
133986857b36SDavid Xu 
134086857b36SDavid Xu static void
134186857b36SDavid Xu realtimer_event_hook(struct proc *p, clockid_t clock_id, int event)
134286857b36SDavid Xu {
134386857b36SDavid Xu 	struct itimers *its;
134486857b36SDavid Xu 	struct itimer  *it;
134586857b36SDavid Xu 	int i;
134686857b36SDavid Xu 
134786857b36SDavid Xu 	/*
134886857b36SDavid Xu 	 * Timer 0 (ITIMER_REAL) is XSI interval timer, according to POSIX
134986857b36SDavid Xu 	 * specification, it should be inherited by new process image.
135086857b36SDavid Xu 	 */
135186857b36SDavid Xu 	if (event == ITIMER_EV_EXEC)
135286857b36SDavid Xu 		i = 1;
135386857b36SDavid Xu 	else
135486857b36SDavid Xu 		i = 0;
135560354683SDavid Xu 	its = p->p_itimers;
135686857b36SDavid Xu 	for (; i < TIMER_MAX; i++) {
135786857b36SDavid Xu 		if ((it = its->its_timers[i]) != NULL &&
135886857b36SDavid Xu 		     it->it_clockid == clock_id) {
135986857b36SDavid Xu 			ITIMER_LOCK(it);
136086857b36SDavid Xu 			callout_stop(&it->it_callout);
136186857b36SDavid Xu 			ITIMER_UNLOCK(it);
136286857b36SDavid Xu 		}
136386857b36SDavid Xu 	}
136486857b36SDavid Xu }
136586857b36SDavid Xu 
136686857b36SDavid Xu /* Timeout callback for realtime timer */
136786857b36SDavid Xu static void
136886857b36SDavid Xu realtimer_expire(void *arg)
136986857b36SDavid Xu {
137056c06c4bSDavid Xu 	struct timespec cts, ts;
137156c06c4bSDavid Xu 	struct timeval tv;
137286857b36SDavid Xu 	struct itimer *it;
137386857b36SDavid Xu 	struct proc *p;
137486857b36SDavid Xu 
137586857b36SDavid Xu 	it = (struct itimer *)arg;
137686857b36SDavid Xu 	p = it->it_proc;
137786857b36SDavid Xu 
137856c06c4bSDavid Xu 	realtimer_clocktime(it->it_clockid, &cts);
137986857b36SDavid Xu 	/* Only fire if time is reached. */
138056c06c4bSDavid Xu 	if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
138156c06c4bSDavid Xu 		if (timespecisset(&it->it_time.it_interval)) {
138256c06c4bSDavid Xu 			timespecadd(&it->it_time.it_value,
138386857b36SDavid Xu 				    &it->it_time.it_interval);
138456c06c4bSDavid Xu 			while (timespeccmp(&cts, &it->it_time.it_value, >=)) {
138586857b36SDavid Xu 				it->it_overrun++;
138656c06c4bSDavid Xu 				timespecadd(&it->it_time.it_value,
138786857b36SDavid Xu 					    &it->it_time.it_interval);
138886857b36SDavid Xu 			}
138986857b36SDavid Xu 		} else {
139086857b36SDavid Xu 			/* single shot timer ? */
139156c06c4bSDavid Xu 			timespecclear(&it->it_time.it_value);
139286857b36SDavid Xu 		}
139356c06c4bSDavid Xu 		if (timespecisset(&it->it_time.it_value)) {
139456c06c4bSDavid Xu 			ts = it->it_time.it_value;
139556c06c4bSDavid Xu 			timespecsub(&ts, &cts);
139656c06c4bSDavid Xu 			TIMESPEC_TO_TIMEVAL(&tv, &ts);
139756c06c4bSDavid Xu 			callout_reset(&it->it_callout, tvtohz(&tv),
139886857b36SDavid Xu 				 realtimer_expire, it);
139986857b36SDavid Xu 		}
140086857b36SDavid Xu 		ITIMER_UNLOCK(it);
140186857b36SDavid Xu 		itimer_fire(it);
140286857b36SDavid Xu 		ITIMER_LOCK(it);
140356c06c4bSDavid Xu 	} else if (timespecisset(&it->it_time.it_value)) {
140456c06c4bSDavid Xu 		ts = it->it_time.it_value;
140556c06c4bSDavid Xu 		timespecsub(&ts, &cts);
140656c06c4bSDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
140756c06c4bSDavid Xu 		callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
140886857b36SDavid Xu  			it);
140986857b36SDavid Xu 	}
141086857b36SDavid Xu }
141186857b36SDavid Xu 
141286857b36SDavid Xu void
141386857b36SDavid Xu itimer_fire(struct itimer *it)
141486857b36SDavid Xu {
141586857b36SDavid Xu 	struct proc *p = it->it_proc;
14166d7b314bSDavid Xu 	int ret;
141786857b36SDavid Xu 
141856c06c4bSDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
141956c06c4bSDavid Xu 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
142086857b36SDavid Xu 		PROC_LOCK(p);
14216d7b314bSDavid Xu 		if (!KSI_ONQ(&it->it_ksi)) {
14226d7b314bSDavid Xu 			ret = psignal_event(p, &it->it_sigev, &it->it_ksi);
14236d7b314bSDavid Xu 			if (__predict_false(ret != 0)) {
142486857b36SDavid Xu 				it->it_overrun++;
142556c06c4bSDavid Xu 				/*
142656c06c4bSDavid Xu 				 * Broken userland code, thread went
142756c06c4bSDavid Xu 				 * away, disarm the timer.
142856c06c4bSDavid Xu 				 */
14296d7b314bSDavid Xu 				if (ret == ESRCH) {
143056c06c4bSDavid Xu 					ITIMER_LOCK(it);
143156c06c4bSDavid Xu 					timespecclear(&it->it_time.it_value);
143256c06c4bSDavid Xu 					timespecclear(&it->it_time.it_interval);
143356c06c4bSDavid Xu 					callout_stop(&it->it_callout);
143456c06c4bSDavid Xu 					ITIMER_UNLOCK(it);
14356d7b314bSDavid Xu 				}
143656c06c4bSDavid Xu 			}
143756c06c4bSDavid Xu 		} else {
14386d7b314bSDavid Xu 			it->it_overrun++;
143956c06c4bSDavid Xu 		}
144086857b36SDavid Xu 		PROC_UNLOCK(p);
144186857b36SDavid Xu 	}
144286857b36SDavid Xu }
144386857b36SDavid Xu 
144486857b36SDavid Xu static void
144586857b36SDavid Xu itimers_alloc(struct proc *p)
144686857b36SDavid Xu {
144760354683SDavid Xu 	struct itimers *its;
144860354683SDavid Xu 	int i;
144986857b36SDavid Xu 
145060354683SDavid Xu 	its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
145186857b36SDavid Xu 	LIST_INIT(&its->its_virtual);
145286857b36SDavid Xu 	LIST_INIT(&its->its_prof);
145386857b36SDavid Xu 	TAILQ_INIT(&its->its_worklist);
145460354683SDavid Xu 	for (i = 0; i < TIMER_MAX; i++)
145560354683SDavid Xu 		its->its_timers[i] = NULL;
145660354683SDavid Xu 	PROC_LOCK(p);
145760354683SDavid Xu 	if (p->p_itimers == NULL) {
145860354683SDavid Xu 		p->p_itimers = its;
145960354683SDavid Xu 		PROC_UNLOCK(p);
146060354683SDavid Xu 	}
146160354683SDavid Xu 	else {
146260354683SDavid Xu 		PROC_UNLOCK(p);
146360354683SDavid Xu 		free(its, M_SUBPROC);
146460354683SDavid Xu 	}
146586857b36SDavid Xu }
146686857b36SDavid Xu 
146786857b36SDavid Xu /* Clean up timers when some process events are being triggered. */
146886857b36SDavid Xu void
146986857b36SDavid Xu itimers_event_hook(struct proc *p, int event)
147086857b36SDavid Xu {
147186857b36SDavid Xu 	struct itimers *its;
147286857b36SDavid Xu 	struct itimer *it;
147386857b36SDavid Xu 	int i;
147486857b36SDavid Xu 
147560354683SDavid Xu 	if (p->p_itimers != NULL) {
147660354683SDavid Xu 		its = p->p_itimers;
147786857b36SDavid Xu 		for (i = 0; i < MAX_CLOCKS; ++i) {
147886857b36SDavid Xu 			if (posix_clocks[i].event_hook != NULL)
147986857b36SDavid Xu 				CLOCK_CALL(i, event_hook, (p, i, event));
148086857b36SDavid Xu 		}
148186857b36SDavid Xu 		/*
148286857b36SDavid Xu 		 * According to susv3, XSI interval timers should be inherited
148386857b36SDavid Xu 		 * by new image.
148486857b36SDavid Xu 		 */
148586857b36SDavid Xu 		if (event == ITIMER_EV_EXEC)
148686857b36SDavid Xu 			i = 3;
148786857b36SDavid Xu 		else if (event == ITIMER_EV_EXIT)
148886857b36SDavid Xu 			i = 0;
148986857b36SDavid Xu 		else
149086857b36SDavid Xu 			panic("unhandled event");
149186857b36SDavid Xu 		for (; i < TIMER_MAX; ++i) {
149286857b36SDavid Xu 			if ((it = its->its_timers[i]) != NULL) {
149386857b36SDavid Xu 				PROC_LOCK(p);
149486857b36SDavid Xu 				if (KSI_ONQ(&it->it_ksi))
149586857b36SDavid Xu 					sigqueue_take(&it->it_ksi);
149686857b36SDavid Xu 				PROC_UNLOCK(p);
149786857b36SDavid Xu 				uma_zfree(itimer_zone, its->its_timers[i]);
149886857b36SDavid Xu 				its->its_timers[i] = NULL;
149986857b36SDavid Xu 			}
150086857b36SDavid Xu 		}
150186857b36SDavid Xu 		if (its->its_timers[0] == NULL &&
150286857b36SDavid Xu 		    its->its_timers[1] == NULL &&
150386857b36SDavid Xu 		    its->its_timers[2] == NULL) {
150460354683SDavid Xu 			free(its, M_SUBPROC);
150560354683SDavid Xu 			p->p_itimers = NULL;
150686857b36SDavid Xu 		}
150786857b36SDavid Xu 	}
150886857b36SDavid Xu }
1509