xref: /freebsd/sys/kern/kern_time.c (revision cf7d9a8ca86f4febb0a4d0d029474b4ec6b6706b)
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 
35df8bae1dSRodney W. Grimes #include <sys/param.h>
36e96c1fdcSPeter Wemm #include <sys/systm.h>
37aff5bcb1SDavid Xu #include <sys/limits.h>
38f645b0b5SPoul-Henning Kamp #include <sys/clock.h>
39fb919e4dSMark Murray #include <sys/lock.h>
40fb919e4dSMark Murray #include <sys/mutex.h>
41d2d3e875SBruce Evans #include <sys/sysproto.h>
42d26b1a1fSDavid Xu #include <sys/eventhandler.h>
43df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
44797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
45df8bae1dSRodney W. Grimes #include <sys/kernel.h>
46efa42cbcSPaul Saab #include <sys/syscallsubr.h>
4777e718f7SDavid Xu #include <sys/sysctl.h>
4894c8fcd8SPeter Wemm #include <sys/sysent.h>
49acd3428bSRobert Watson #include <sys/priv.h>
50df8bae1dSRodney W. Grimes #include <sys/proc.h>
516aeb05d7STom Rhodes #include <sys/posix4.h>
52708e7684SPeter Wemm #include <sys/time.h>
5386857b36SDavid Xu #include <sys/timers.h>
5491266b96SPoul-Henning Kamp #include <sys/timetc.h>
55df8bae1dSRodney W. Grimes #include <sys/vnode.h>
56fb919e4dSMark Murray 
575b870b7bSPeter Wemm #include <vm/vm.h>
585b870b7bSPeter Wemm #include <vm/vm_extern.h>
59df8bae1dSRodney W. Grimes 
6086857b36SDavid Xu #define MAX_CLOCKS 	(CLOCK_MONOTONIC+1)
6186857b36SDavid Xu 
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 *);
7794c8fcd8SPeter Wemm 
7886857b36SDavid Xu static void	itimer_start(void);
7986857b36SDavid Xu static int	itimer_init(void *, int, int);
8086857b36SDavid Xu static void	itimer_fini(void *, int);
8186857b36SDavid Xu static void	itimer_enter(struct itimer *);
8286857b36SDavid Xu static void	itimer_leave(struct itimer *);
83843b99c6SDavid Xu static struct itimer *itimer_find(struct proc *, int);
8486857b36SDavid Xu static void	itimers_alloc(struct proc *);
85993182e5SAlexander Leidinger static void	itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp);
86993182e5SAlexander Leidinger static void	itimers_event_hook_exit(void *arg, struct proc *p);
8786857b36SDavid Xu static int	realtimer_create(struct itimer *);
8886857b36SDavid Xu static int	realtimer_gettime(struct itimer *, struct itimerspec *);
8986857b36SDavid Xu static int	realtimer_settime(struct itimer *, int,
9086857b36SDavid Xu 			struct itimerspec *, struct itimerspec *);
9186857b36SDavid Xu static int	realtimer_delete(struct itimer *);
9256c06c4bSDavid Xu static void	realtimer_clocktime(clockid_t, struct timespec *);
9386857b36SDavid Xu static void	realtimer_expire(void *);
9486857b36SDavid Xu static int	kern_timer_create(struct thread *, clockid_t,
9561d3a4efSDavid Xu 			struct sigevent *, int *, int);
9661d3a4efSDavid Xu static int	kern_timer_delete(struct thread *, int);
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 
10894c8fcd8SPeter Wemm static int
10991afe087SPoul-Henning Kamp settime(struct thread *td, struct timeval *tv)
11094c8fcd8SPeter Wemm {
111fcae3aa6SNick Sayer 	struct timeval delta, tv1, tv2;
112c0bd94a7SNick Sayer 	static struct timeval maxtime, laststep;
1137ec73f64SPoul-Henning Kamp 	struct timespec ts;
11494c8fcd8SPeter Wemm 	int s;
11594c8fcd8SPeter Wemm 
116708e7684SPeter Wemm 	s = splclock();
1179c8fff87SBruce Evans 	microtime(&tv1);
11800af9731SPoul-Henning Kamp 	delta = *tv;
11900af9731SPoul-Henning Kamp 	timevalsub(&delta, &tv1);
12094c8fcd8SPeter Wemm 
12194c8fcd8SPeter Wemm 	/*
1229c8fff87SBruce Evans 	 * If the system is secure, we do not allow the time to be
123fcae3aa6SNick Sayer 	 * set to a value earlier than 1 second less than the highest
124fcae3aa6SNick Sayer 	 * time we have yet seen. The worst a miscreant can do in
125fcae3aa6SNick Sayer 	 * this circumstance is "freeze" time. He couldn't go
126fcae3aa6SNick Sayer 	 * back to the past.
127c0bd94a7SNick Sayer 	 *
128c0bd94a7SNick Sayer 	 * We similarly do not allow the clock to be stepped more
129c0bd94a7SNick Sayer 	 * than one second, nor more than once per second. This allows
130c0bd94a7SNick Sayer 	 * a miscreant to make the clock march double-time, but no worse.
13194c8fcd8SPeter Wemm 	 */
1327edfb592SJohn Baldwin 	if (securelevel_gt(td->td_ucred, 1) != 0) {
133fcae3aa6SNick Sayer 		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
1343f92429aSMatt Jacob 			/*
135c0bd94a7SNick Sayer 			 * Update maxtime to latest time we've seen.
1363f92429aSMatt Jacob 			 */
137fcae3aa6SNick Sayer 			if (tv1.tv_sec > maxtime.tv_sec)
138fcae3aa6SNick Sayer 				maxtime = tv1;
139fcae3aa6SNick Sayer 			tv2 = *tv;
140fcae3aa6SNick Sayer 			timevalsub(&tv2, &maxtime);
141fcae3aa6SNick Sayer 			if (tv2.tv_sec < -1) {
1423f92429aSMatt Jacob 				tv->tv_sec = maxtime.tv_sec - 1;
143fcae3aa6SNick Sayer 				printf("Time adjustment clamped to -1 second\n");
144fcae3aa6SNick Sayer 			}
1453f92429aSMatt Jacob 		} else {
146c0bd94a7SNick Sayer 			if (tv1.tv_sec == laststep.tv_sec) {
147c0bd94a7SNick Sayer 				splx(s);
148c0bd94a7SNick Sayer 				return (EPERM);
149c0bd94a7SNick Sayer 			}
150c0bd94a7SNick Sayer 			if (delta.tv_sec > 1) {
151c0bd94a7SNick Sayer 				tv->tv_sec = tv1.tv_sec + 1;
152c0bd94a7SNick Sayer 				printf("Time adjustment clamped to +1 second\n");
153c0bd94a7SNick Sayer 			}
154c0bd94a7SNick Sayer 			laststep = *tv;
155fcae3aa6SNick Sayer 		}
1569c8fff87SBruce Evans 	}
1579c8fff87SBruce Evans 
1587ec73f64SPoul-Henning Kamp 	ts.tv_sec = tv->tv_sec;
1597ec73f64SPoul-Henning Kamp 	ts.tv_nsec = tv->tv_usec * 1000;
1607edfb592SJohn Baldwin 	mtx_lock(&Giant);
16191266b96SPoul-Henning Kamp 	tc_setclock(&ts);
16294c8fcd8SPeter Wemm 	resettodr();
1637edfb592SJohn Baldwin 	mtx_unlock(&Giant);
16494c8fcd8SPeter Wemm 	return (0);
16594c8fcd8SPeter Wemm }
16694c8fcd8SPeter Wemm 
16794c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
16894c8fcd8SPeter Wemm struct clock_gettime_args {
16994c8fcd8SPeter Wemm 	clockid_t clock_id;
17094c8fcd8SPeter Wemm 	struct	timespec *tp;
17194c8fcd8SPeter Wemm };
17294c8fcd8SPeter Wemm #endif
17394c8fcd8SPeter Wemm /* ARGSUSED */
17494c8fcd8SPeter Wemm int
17591afe087SPoul-Henning Kamp clock_gettime(struct thread *td, struct clock_gettime_args *uap)
17694c8fcd8SPeter Wemm {
17794c8fcd8SPeter Wemm 	struct timespec ats;
178f0b479cdSPaul Saab 	int error;
179f0b479cdSPaul Saab 
180f0b479cdSPaul Saab 	error = kern_clock_gettime(td, uap->clock_id, &ats);
181f0b479cdSPaul Saab 	if (error == 0)
182f0b479cdSPaul Saab 		error = copyout(&ats, uap->tp, sizeof(ats));
183f0b479cdSPaul Saab 
184f0b479cdSPaul Saab 	return (error);
185f0b479cdSPaul Saab }
186f0b479cdSPaul Saab 
187f0b479cdSPaul Saab int
188f0b479cdSPaul Saab kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
189f0b479cdSPaul Saab {
190de0a9241SKelly Yancey 	struct timeval sys, user;
19178c85e8dSJohn Baldwin 	struct proc *p;
19200d6ac63SDavid Xu 	uint64_t runtime, curtime, switchtime;
19394c8fcd8SPeter Wemm 
19478c85e8dSJohn Baldwin 	p = td->td_proc;
195f0b479cdSPaul Saab 	switch (clock_id) {
1965e758b95SRobert Watson 	case CLOCK_REALTIME:		/* Default to precise. */
1975e758b95SRobert Watson 	case CLOCK_REALTIME_PRECISE:
198f0b479cdSPaul Saab 		nanotime(ats);
199b8817154SKelly Yancey 		break;
2005e758b95SRobert Watson 	case CLOCK_REALTIME_FAST:
2015e758b95SRobert Watson 		getnanotime(ats);
2025e758b95SRobert Watson 		break;
203b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
20478c85e8dSJohn Baldwin 		PROC_LOCK(p);
205a1fe14bcSAttilio Rao 		PROC_SLOCK(p);
20678c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
207a1fe14bcSAttilio Rao 		PROC_SUNLOCK(p);
20878c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
209f0b479cdSPaul Saab 		TIMEVAL_TO_TIMESPEC(&user, ats);
210b8817154SKelly Yancey 		break;
211b8817154SKelly Yancey 	case CLOCK_PROF:
21278c85e8dSJohn Baldwin 		PROC_LOCK(p);
213a1fe14bcSAttilio Rao 		PROC_SLOCK(p);
21478c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
215a1fe14bcSAttilio Rao 		PROC_SUNLOCK(p);
21678c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
217de0a9241SKelly Yancey 		timevaladd(&user, &sys);
218f0b479cdSPaul Saab 		TIMEVAL_TO_TIMESPEC(&user, ats);
219de0a9241SKelly Yancey 		break;
2205e758b95SRobert Watson 	case CLOCK_MONOTONIC:		/* Default to precise. */
2215e758b95SRobert Watson 	case CLOCK_MONOTONIC_PRECISE:
2225eefd889SAndre Oppermann 	case CLOCK_UPTIME:
2235e758b95SRobert Watson 	case CLOCK_UPTIME_PRECISE:
224f0b479cdSPaul Saab 		nanouptime(ats);
225b8817154SKelly Yancey 		break;
2265e758b95SRobert Watson 	case CLOCK_UPTIME_FAST:
2275e758b95SRobert Watson 	case CLOCK_MONOTONIC_FAST:
2285e758b95SRobert Watson 		getnanouptime(ats);
2295e758b95SRobert Watson 		break;
2305e758b95SRobert Watson 	case CLOCK_SECOND:
2315e758b95SRobert Watson 		ats->tv_sec = time_second;
2325e758b95SRobert Watson 		ats->tv_nsec = 0;
2335e758b95SRobert Watson 		break;
23400d6ac63SDavid Xu 	case CLOCK_THREAD_CPUTIME_ID:
23500d6ac63SDavid Xu 		critical_enter();
23600d6ac63SDavid Xu 		switchtime = PCPU_GET(switchtime);
23700d6ac63SDavid Xu 		curtime = cpu_ticks();
2380e17ccbeSDavid Xu 		runtime = td->td_runtime;
23900d6ac63SDavid Xu 		critical_exit();
2400e17ccbeSDavid Xu 		runtime = cputick2usec(runtime + curtime - switchtime);
24100d6ac63SDavid Xu 		ats->tv_sec = runtime / 1000000;
24200d6ac63SDavid Xu 		ats->tv_nsec = runtime % 1000000 * 1000;
24300d6ac63SDavid Xu 		break;
244b8817154SKelly Yancey 	default:
2455cb3dc8fSPoul-Henning Kamp 		return (EINVAL);
246b8817154SKelly Yancey 	}
247f0b479cdSPaul Saab 	return (0);
24894c8fcd8SPeter Wemm }
24994c8fcd8SPeter Wemm 
25094c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
25194c8fcd8SPeter Wemm struct clock_settime_args {
25294c8fcd8SPeter Wemm 	clockid_t clock_id;
25394c8fcd8SPeter Wemm 	const struct	timespec *tp;
25494c8fcd8SPeter Wemm };
25594c8fcd8SPeter Wemm #endif
25694c8fcd8SPeter Wemm /* ARGSUSED */
25794c8fcd8SPeter Wemm int
25891afe087SPoul-Henning Kamp clock_settime(struct thread *td, struct clock_settime_args *uap)
25994c8fcd8SPeter Wemm {
26094c8fcd8SPeter Wemm 	struct timespec ats;
26194c8fcd8SPeter Wemm 	int error;
26294c8fcd8SPeter Wemm 
263f0b479cdSPaul Saab 	if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
264f0b479cdSPaul Saab 		return (error);
265f0b479cdSPaul Saab 	return (kern_clock_settime(td, uap->clock_id, &ats));
266f0b479cdSPaul Saab }
267f0b479cdSPaul Saab 
268f0b479cdSPaul Saab int
269f0b479cdSPaul Saab kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
270f0b479cdSPaul Saab {
271f0b479cdSPaul Saab 	struct timeval atv;
272f0b479cdSPaul Saab 	int error;
273f0b479cdSPaul Saab 
274acd3428bSRobert Watson 	if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
2757edfb592SJohn Baldwin 		return (error);
276f0b479cdSPaul Saab 	if (clock_id != CLOCK_REALTIME)
2777edfb592SJohn Baldwin 		return (EINVAL);
278f0b479cdSPaul Saab 	if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000)
2797edfb592SJohn Baldwin 		return (EINVAL);
280a0502b19SPoul-Henning Kamp 	/* XXX Don't convert nsec->usec and back */
281f0b479cdSPaul Saab 	TIMESPEC_TO_TIMEVAL(&atv, ats);
2827edfb592SJohn Baldwin 	error = settime(td, &atv);
28394c8fcd8SPeter Wemm 	return (error);
28494c8fcd8SPeter Wemm }
28594c8fcd8SPeter Wemm 
28694c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
28794c8fcd8SPeter Wemm struct clock_getres_args {
28894c8fcd8SPeter Wemm 	clockid_t clock_id;
28994c8fcd8SPeter Wemm 	struct	timespec *tp;
29094c8fcd8SPeter Wemm };
29194c8fcd8SPeter Wemm #endif
29294c8fcd8SPeter Wemm int
29391afe087SPoul-Henning Kamp clock_getres(struct thread *td, struct clock_getres_args *uap)
29494c8fcd8SPeter Wemm {
29594c8fcd8SPeter Wemm 	struct timespec ts;
296f0b479cdSPaul Saab 	int error;
29794c8fcd8SPeter Wemm 
298f0b479cdSPaul Saab 	if (uap->tp == NULL)
299f0b479cdSPaul Saab 		return (0);
300f0b479cdSPaul Saab 
301f0b479cdSPaul Saab 	error = kern_clock_getres(td, uap->clock_id, &ts);
302f0b479cdSPaul Saab 	if (error == 0)
303f0b479cdSPaul Saab 		error = copyout(&ts, uap->tp, sizeof(ts));
304f0b479cdSPaul Saab 	return (error);
305f0b479cdSPaul Saab }
306f0b479cdSPaul Saab 
307f0b479cdSPaul Saab int
308f0b479cdSPaul Saab kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
309f0b479cdSPaul Saab {
310f0b479cdSPaul Saab 
311f0b479cdSPaul Saab 	ts->tv_sec = 0;
312f0b479cdSPaul Saab 	switch (clock_id) {
313b8817154SKelly Yancey 	case CLOCK_REALTIME:
3145e758b95SRobert Watson 	case CLOCK_REALTIME_FAST:
3155e758b95SRobert Watson 	case CLOCK_REALTIME_PRECISE:
316b8817154SKelly Yancey 	case CLOCK_MONOTONIC:
3175e758b95SRobert Watson 	case CLOCK_MONOTONIC_FAST:
3185e758b95SRobert Watson 	case CLOCK_MONOTONIC_PRECISE:
3195eefd889SAndre Oppermann 	case CLOCK_UPTIME:
3205e758b95SRobert Watson 	case CLOCK_UPTIME_FAST:
3215e758b95SRobert Watson 	case CLOCK_UPTIME_PRECISE:
322ac0653dcSBruce Evans 		/*
323ac0653dcSBruce Evans 		 * Round up the result of the division cheaply by adding 1.
324ac0653dcSBruce Evans 		 * Rounding up is especially important if rounding down
325ac0653dcSBruce Evans 		 * would give 0.  Perfect rounding is unimportant.
326ac0653dcSBruce Evans 		 */
327f0b479cdSPaul Saab 		ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
328b8817154SKelly Yancey 		break;
329b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
330b8817154SKelly Yancey 	case CLOCK_PROF:
331b8817154SKelly Yancey 		/* Accurately round up here because we can do so cheaply. */
332f0b479cdSPaul Saab 		ts->tv_nsec = (1000000000 + hz - 1) / hz;
333b8817154SKelly Yancey 		break;
3345e758b95SRobert Watson 	case CLOCK_SECOND:
3355e758b95SRobert Watson 		ts->tv_sec = 1;
3365e758b95SRobert Watson 		ts->tv_nsec = 0;
3375e758b95SRobert Watson 		break;
33800d6ac63SDavid Xu 	case CLOCK_THREAD_CPUTIME_ID:
33900d6ac63SDavid Xu 		/* sync with cputick2usec */
34000d6ac63SDavid Xu 		ts->tv_nsec = 1000000 / cpu_tickrate();
34100d6ac63SDavid Xu 		if (ts->tv_nsec == 0)
34200d6ac63SDavid Xu 			ts->tv_nsec = 1000;
34300d6ac63SDavid Xu 		break;
344b8817154SKelly Yancey 	default:
345b8817154SKelly Yancey 		return (EINVAL);
34694c8fcd8SPeter Wemm 	}
347de0a9241SKelly Yancey 	return (0);
34894c8fcd8SPeter Wemm }
34994c8fcd8SPeter Wemm 
35094c8fcd8SPeter Wemm static int nanowait;
35194c8fcd8SPeter Wemm 
3527fdf2c85SPaul Saab int
3537fdf2c85SPaul Saab kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
3545b870b7bSPeter Wemm {
3555704ba6aSPoul-Henning Kamp 	struct timespec ts, ts2, ts3;
35633841826SPoul-Henning Kamp 	struct timeval tv;
35733841826SPoul-Henning Kamp 	int error;
3585b870b7bSPeter Wemm 
3597d7fb492SBruce Evans 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
360708e7684SPeter Wemm 		return (EINVAL);
361d254af07SMatthew Dillon 	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
3627d7fb492SBruce Evans 		return (0);
363c21410e1SPoul-Henning Kamp 	getnanouptime(&ts);
36400af9731SPoul-Henning Kamp 	timespecadd(&ts, rqt);
36533841826SPoul-Henning Kamp 	TIMESPEC_TO_TIMEVAL(&tv, rqt);
36633841826SPoul-Henning Kamp 	for (;;) {
36733841826SPoul-Henning Kamp 		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
36833841826SPoul-Henning Kamp 		    tvtohz(&tv));
369c21410e1SPoul-Henning Kamp 		getnanouptime(&ts2);
37033841826SPoul-Henning Kamp 		if (error != EWOULDBLOCK) {
37133841826SPoul-Henning Kamp 			if (error == ERESTART)
37294c8fcd8SPeter Wemm 				error = EINTR;
37333841826SPoul-Henning Kamp 			if (rmt != NULL) {
37433841826SPoul-Henning Kamp 				timespecsub(&ts, &ts2);
37533841826SPoul-Henning Kamp 				if (ts.tv_sec < 0)
37633841826SPoul-Henning Kamp 					timespecclear(&ts);
37700af9731SPoul-Henning Kamp 				*rmt = ts;
37800af9731SPoul-Henning Kamp 			}
37933841826SPoul-Henning Kamp 			return (error);
38033841826SPoul-Henning Kamp 		}
38133841826SPoul-Henning Kamp 		if (timespeccmp(&ts2, &ts, >=))
38233841826SPoul-Henning Kamp 			return (0);
3835704ba6aSPoul-Henning Kamp 		ts3 = ts;
3845704ba6aSPoul-Henning Kamp 		timespecsub(&ts3, &ts2);
3855704ba6aSPoul-Henning Kamp 		TIMESPEC_TO_TIMEVAL(&tv, &ts3);
38633841826SPoul-Henning Kamp 	}
3875b870b7bSPeter Wemm }
38894c8fcd8SPeter Wemm 
3895b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
3905b870b7bSPeter Wemm struct nanosleep_args {
3915b870b7bSPeter Wemm 	struct	timespec *rqtp;
3925b870b7bSPeter Wemm 	struct	timespec *rmtp;
3935b870b7bSPeter Wemm };
3945b870b7bSPeter Wemm #endif
3955b870b7bSPeter Wemm /* ARGSUSED */
3965b870b7bSPeter Wemm int
39791afe087SPoul-Henning Kamp nanosleep(struct thread *td, struct nanosleep_args *uap)
3985b870b7bSPeter Wemm {
3995b870b7bSPeter Wemm 	struct timespec rmt, rqt;
400fb99ab88SMatthew Dillon 	int error;
4015b870b7bSPeter Wemm 
402d1e405c5SAlfred Perlstein 	error = copyin(uap->rqtp, &rqt, sizeof(rqt));
4035b870b7bSPeter Wemm 	if (error)
4045b870b7bSPeter Wemm 		return (error);
405fb99ab88SMatthew Dillon 
40631f3e2adSAlfred Perlstein 	if (uap->rmtp &&
40731f3e2adSAlfred Perlstein 	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
40831f3e2adSAlfred Perlstein 			return (EFAULT);
4097fdf2c85SPaul Saab 	error = kern_nanosleep(td, &rqt, &rmt);
410d1e405c5SAlfred Perlstein 	if (error && uap->rmtp) {
411fb99ab88SMatthew Dillon 		int error2;
412fb99ab88SMatthew Dillon 
413d1e405c5SAlfred Perlstein 		error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
41431f3e2adSAlfred Perlstein 		if (error2)
415fb99ab88SMatthew Dillon 			error = error2;
416708e7684SPeter Wemm 	}
417708e7684SPeter Wemm 	return (error);
41894c8fcd8SPeter Wemm }
41994c8fcd8SPeter Wemm 
4205b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
421df8bae1dSRodney W. Grimes struct gettimeofday_args {
422df8bae1dSRodney W. Grimes 	struct	timeval *tp;
423df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
424df8bae1dSRodney W. Grimes };
425d2d3e875SBruce Evans #endif
426df8bae1dSRodney W. Grimes /* ARGSUSED */
42726f9a767SRodney W. Grimes int
42891afe087SPoul-Henning Kamp gettimeofday(struct thread *td, struct gettimeofday_args *uap)
429df8bae1dSRodney W. Grimes {
430df8bae1dSRodney W. Grimes 	struct timeval atv;
431411c25edSTim J. Robbins 	struct timezone rtz;
432df8bae1dSRodney W. Grimes 	int error = 0;
433df8bae1dSRodney W. Grimes 
434df8bae1dSRodney W. Grimes 	if (uap->tp) {
435df8bae1dSRodney W. Grimes 		microtime(&atv);
43601609114SAlfred Perlstein 		error = copyout(&atv, uap->tp, sizeof (atv));
437df8bae1dSRodney W. Grimes 	}
43821dcdb38SPoul-Henning Kamp 	if (error == 0 && uap->tzp != NULL) {
43991f1c2b3SPoul-Henning Kamp 		rtz.tz_minuteswest = tz_minuteswest;
44091f1c2b3SPoul-Henning Kamp 		rtz.tz_dsttime = tz_dsttime;
441411c25edSTim J. Robbins 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
44221dcdb38SPoul-Henning Kamp 	}
443df8bae1dSRodney W. Grimes 	return (error);
444df8bae1dSRodney W. Grimes }
445df8bae1dSRodney W. Grimes 
446d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
447df8bae1dSRodney W. Grimes struct settimeofday_args {
448df8bae1dSRodney W. Grimes 	struct	timeval *tv;
449df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
450df8bae1dSRodney W. Grimes };
451d2d3e875SBruce Evans #endif
452df8bae1dSRodney W. Grimes /* ARGSUSED */
45326f9a767SRodney W. Grimes int
45491afe087SPoul-Henning Kamp settimeofday(struct thread *td, struct settimeofday_args *uap)
455df8bae1dSRodney W. Grimes {
456b88ec951SJohn Baldwin 	struct timeval atv, *tvp;
457b88ec951SJohn Baldwin 	struct timezone atz, *tzp;
458b88ec951SJohn Baldwin 	int error;
459b88ec951SJohn Baldwin 
460b88ec951SJohn Baldwin 	if (uap->tv) {
461b88ec951SJohn Baldwin 		error = copyin(uap->tv, &atv, sizeof(atv));
462b88ec951SJohn Baldwin 		if (error)
463b88ec951SJohn Baldwin 			return (error);
464b88ec951SJohn Baldwin 		tvp = &atv;
465b88ec951SJohn Baldwin 	} else
466b88ec951SJohn Baldwin 		tvp = NULL;
467b88ec951SJohn Baldwin 	if (uap->tzp) {
468b88ec951SJohn Baldwin 		error = copyin(uap->tzp, &atz, sizeof(atz));
469b88ec951SJohn Baldwin 		if (error)
470b88ec951SJohn Baldwin 			return (error);
471b88ec951SJohn Baldwin 		tzp = &atz;
472b88ec951SJohn Baldwin 	} else
473b88ec951SJohn Baldwin 		tzp = NULL;
474b88ec951SJohn Baldwin 	return (kern_settimeofday(td, tvp, tzp));
475b88ec951SJohn Baldwin }
476b88ec951SJohn Baldwin 
477b88ec951SJohn Baldwin int
478b88ec951SJohn Baldwin kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
479b88ec951SJohn Baldwin {
480b88ec951SJohn Baldwin 	int error;
481fb99ab88SMatthew Dillon 
482acd3428bSRobert Watson 	error = priv_check(td, PRIV_SETTIMEOFDAY);
483b88ec951SJohn Baldwin 	if (error)
4847edfb592SJohn Baldwin 		return (error);
485df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
486b88ec951SJohn Baldwin 	if (tv) {
487b88ec951SJohn Baldwin 		if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
4887edfb592SJohn Baldwin 			return (EINVAL);
489b88ec951SJohn Baldwin 		error = settime(td, tv);
490708e7684SPeter Wemm 	}
491b88ec951SJohn Baldwin 	if (tzp && error == 0) {
492b88ec951SJohn Baldwin 		tz_minuteswest = tzp->tz_minuteswest;
493b88ec951SJohn Baldwin 		tz_dsttime = tzp->tz_dsttime;
494b88ec951SJohn Baldwin 	}
4957edfb592SJohn Baldwin 	return (error);
496b88ec951SJohn Baldwin }
4977edfb592SJohn Baldwin 
498df8bae1dSRodney W. Grimes /*
499873fbcd7SRobert Watson  * Get value of an interval timer.  The process virtual and profiling virtual
500873fbcd7SRobert Watson  * time timers are kept in the p_stats area, since they can be swapped out.
501873fbcd7SRobert Watson  * These are kept internally in the way they are specified externally: in
502873fbcd7SRobert Watson  * time until they expire.
503df8bae1dSRodney W. Grimes  *
504873fbcd7SRobert Watson  * The real time interval timer is kept in the process table slot for the
505873fbcd7SRobert Watson  * process, and its value (it_value) is kept as an absolute time rather than
506873fbcd7SRobert Watson  * as a delta, so that it is easy to keep periodic real-time signals from
507873fbcd7SRobert Watson  * drifting.
508df8bae1dSRodney W. Grimes  *
509df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
510873fbcd7SRobert Watson  * kern_clock.c.  The real time timer is processed by a timeout routine,
511873fbcd7SRobert Watson  * called from the softclock() routine.  Since a callout may be delayed in
512873fbcd7SRobert Watson  * real time due to interrupt processing in the system, it is possible for
513873fbcd7SRobert Watson  * the real time timeout routine (realitexpire, given below), to be delayed
514873fbcd7SRobert Watson  * in real time past when it is supposed to occur.  It does not suffice,
515873fbcd7SRobert Watson  * therefore, to reload the real timer .it_value from the real time timers
516873fbcd7SRobert Watson  * .it_interval.  Rather, we compute the next time in absolute time the timer
517873fbcd7SRobert Watson  * should go off.
518df8bae1dSRodney W. Grimes  */
519d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
520df8bae1dSRodney W. Grimes struct getitimer_args {
521df8bae1dSRodney W. Grimes 	u_int	which;
522df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
523df8bae1dSRodney W. Grimes };
524d2d3e875SBruce Evans #endif
52526f9a767SRodney W. Grimes int
52691afe087SPoul-Henning Kamp getitimer(struct thread *td, struct getitimer_args *uap)
527df8bae1dSRodney W. Grimes {
528df8bae1dSRodney W. Grimes 	struct itimerval aitv;
529c90110d6SJohn Baldwin 	int error;
530df8bae1dSRodney W. Grimes 
531cfa0efe7SMaxim Sobolev 	error = kern_getitimer(td, uap->which, &aitv);
532cfa0efe7SMaxim Sobolev 	if (error != 0)
533cfa0efe7SMaxim Sobolev 		return (error);
534cfa0efe7SMaxim Sobolev 	return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
535cfa0efe7SMaxim Sobolev }
536cfa0efe7SMaxim Sobolev 
537cfa0efe7SMaxim Sobolev int
538cfa0efe7SMaxim Sobolev kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
539cfa0efe7SMaxim Sobolev {
540cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
541cfa0efe7SMaxim Sobolev 	struct timeval ctv;
542cfa0efe7SMaxim Sobolev 
543cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
544df8bae1dSRodney W. Grimes 		return (EINVAL);
545fb99ab88SMatthew Dillon 
546cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
547df8bae1dSRodney W. Grimes 		/*
548ee002b68SBruce Evans 		 * Convert from absolute to relative time in .it_value
549df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
550df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
551df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
552df8bae1dSRodney W. Grimes 		 */
55396d7f8efSTim J. Robbins 		PROC_LOCK(p);
554cfa0efe7SMaxim Sobolev 		*aitv = p->p_realtimer;
55596d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
556cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
557c21410e1SPoul-Henning Kamp 			getmicrouptime(&ctv);
558cfa0efe7SMaxim Sobolev 			if (timevalcmp(&aitv->it_value, &ctv, <))
559cfa0efe7SMaxim Sobolev 				timevalclear(&aitv->it_value);
560df8bae1dSRodney W. Grimes 			else
561cfa0efe7SMaxim Sobolev 				timevalsub(&aitv->it_value, &ctv);
562227ee8a1SPoul-Henning Kamp 		}
563fb99ab88SMatthew Dillon 	} else {
564982d11f8SJeff Roberson 		PROC_SLOCK(p);
565cfa0efe7SMaxim Sobolev 		*aitv = p->p_stats->p_timer[which];
566982d11f8SJeff Roberson 		PROC_SUNLOCK(p);
567fb99ab88SMatthew Dillon 	}
568cfa0efe7SMaxim Sobolev 	return (0);
569df8bae1dSRodney W. Grimes }
570df8bae1dSRodney W. Grimes 
571d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
572df8bae1dSRodney W. Grimes struct setitimer_args {
573df8bae1dSRodney W. Grimes 	u_int	which;
574df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
575df8bae1dSRodney W. Grimes };
576d2d3e875SBruce Evans #endif
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 {
635982d11f8SJeff Roberson 		PROC_SLOCK(p);
636cfa0efe7SMaxim Sobolev 		*oitv = p->p_stats->p_timer[which];
637cfa0efe7SMaxim Sobolev 		p->p_stats->p_timer[which] = *aitv;
638982d11f8SJeff Roberson 		PROC_SUNLOCK(p);
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,
853843b99c6SDavid Xu 		.event_hook    = NULL
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);
86077e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
86177e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
86277e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
863993182e5SAlexander Leidinger 	EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit,
864d26b1a1fSDavid Xu 		(void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY);
865993182e5SAlexander Leidinger 	EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec,
866d26b1a1fSDavid Xu 		(void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY);
86786857b36SDavid Xu }
86886857b36SDavid Xu 
86986857b36SDavid Xu int
87086857b36SDavid Xu register_posix_clock(int clockid, struct kclock *clk)
87186857b36SDavid Xu {
87286857b36SDavid Xu 	if ((unsigned)clockid >= MAX_CLOCKS) {
87386857b36SDavid Xu 		printf("%s: invalid clockid\n", __func__);
87486857b36SDavid Xu 		return (0);
87586857b36SDavid Xu 	}
87686857b36SDavid Xu 	posix_clocks[clockid] = *clk;
87786857b36SDavid Xu 	return (1);
87886857b36SDavid Xu }
87986857b36SDavid Xu 
88086857b36SDavid Xu static int
88186857b36SDavid Xu itimer_init(void *mem, int size, int flags)
88286857b36SDavid Xu {
88386857b36SDavid Xu 	struct itimer *it;
88486857b36SDavid Xu 
88586857b36SDavid Xu 	it = (struct itimer *)mem;
88686857b36SDavid Xu 	mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
88786857b36SDavid Xu 	return (0);
88886857b36SDavid Xu }
88986857b36SDavid Xu 
89086857b36SDavid Xu static void
89186857b36SDavid Xu itimer_fini(void *mem, int size)
89286857b36SDavid Xu {
89386857b36SDavid Xu 	struct itimer *it;
89486857b36SDavid Xu 
89586857b36SDavid Xu 	it = (struct itimer *)mem;
89686857b36SDavid Xu 	mtx_destroy(&it->it_mtx);
89786857b36SDavid Xu }
89886857b36SDavid Xu 
89986857b36SDavid Xu static void
90086857b36SDavid Xu itimer_enter(struct itimer *it)
90186857b36SDavid Xu {
90286857b36SDavid Xu 
90386857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
90486857b36SDavid Xu 	it->it_usecount++;
90586857b36SDavid Xu }
90686857b36SDavid Xu 
90786857b36SDavid Xu static void
90886857b36SDavid Xu itimer_leave(struct itimer *it)
90986857b36SDavid Xu {
91086857b36SDavid Xu 
91186857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
91286857b36SDavid Xu 	KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
91386857b36SDavid Xu 
91486857b36SDavid Xu 	if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
91586857b36SDavid Xu 		wakeup(it);
91686857b36SDavid Xu }
91786857b36SDavid Xu 
91886857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
91961d3a4efSDavid Xu struct ktimer_create_args {
92086857b36SDavid Xu 	clockid_t clock_id;
92186857b36SDavid Xu 	struct sigevent * evp;
92261d3a4efSDavid Xu 	int * timerid;
92386857b36SDavid Xu };
92486857b36SDavid Xu #endif
92586857b36SDavid Xu int
92661d3a4efSDavid Xu ktimer_create(struct thread *td, struct ktimer_create_args *uap)
92786857b36SDavid Xu {
92886857b36SDavid Xu 	struct sigevent *evp1, ev;
92961d3a4efSDavid Xu 	int id;
93086857b36SDavid Xu 	int error;
93186857b36SDavid Xu 
93286857b36SDavid Xu 	if (uap->evp != NULL) {
93386857b36SDavid Xu 		error = copyin(uap->evp, &ev, sizeof(ev));
93486857b36SDavid Xu 		if (error != 0)
93586857b36SDavid Xu 			return (error);
93686857b36SDavid Xu 		evp1 = &ev;
93786857b36SDavid Xu 	} else
93886857b36SDavid Xu 		evp1 = NULL;
93986857b36SDavid Xu 
94086857b36SDavid Xu 	error = kern_timer_create(td, uap->clock_id, evp1, &id, -1);
94186857b36SDavid Xu 
94286857b36SDavid Xu 	if (error == 0) {
94361d3a4efSDavid Xu 		error = copyout(&id, uap->timerid, sizeof(int));
94486857b36SDavid Xu 		if (error != 0)
94586857b36SDavid Xu 			kern_timer_delete(td, id);
94686857b36SDavid Xu 	}
94786857b36SDavid Xu 	return (error);
94886857b36SDavid Xu }
94986857b36SDavid Xu 
95086857b36SDavid Xu static int
95186857b36SDavid Xu kern_timer_create(struct thread *td, clockid_t clock_id,
95261d3a4efSDavid Xu 	struct sigevent *evp, int *timerid, int preset_id)
95386857b36SDavid Xu {
95486857b36SDavid Xu 	struct proc *p = td->td_proc;
95586857b36SDavid Xu 	struct itimer *it;
95686857b36SDavid Xu 	int id;
95786857b36SDavid Xu 	int error;
95886857b36SDavid Xu 
95986857b36SDavid Xu 	if (clock_id < 0 || clock_id >= MAX_CLOCKS)
96086857b36SDavid Xu 		return (EINVAL);
96186857b36SDavid Xu 
96286857b36SDavid Xu 	if (posix_clocks[clock_id].timer_create == NULL)
96386857b36SDavid Xu 		return (EINVAL);
96486857b36SDavid Xu 
96586857b36SDavid Xu 	if (evp != NULL) {
96686857b36SDavid Xu 		if (evp->sigev_notify != SIGEV_NONE &&
96756c06c4bSDavid Xu 		    evp->sigev_notify != SIGEV_SIGNAL &&
96856c06c4bSDavid Xu 		    evp->sigev_notify != SIGEV_THREAD_ID)
96986857b36SDavid Xu 			return (EINVAL);
97056c06c4bSDavid Xu 		if ((evp->sigev_notify == SIGEV_SIGNAL ||
97156c06c4bSDavid Xu 		     evp->sigev_notify == SIGEV_THREAD_ID) &&
97286857b36SDavid Xu 			!_SIG_VALID(evp->sigev_signo))
97386857b36SDavid Xu 			return (EINVAL);
97486857b36SDavid Xu 	}
97586857b36SDavid Xu 
97660354683SDavid Xu 	if (p->p_itimers == NULL)
97786857b36SDavid Xu 		itimers_alloc(p);
97886857b36SDavid Xu 
97986857b36SDavid Xu 	it = uma_zalloc(itimer_zone, M_WAITOK);
98086857b36SDavid Xu 	it->it_flags = 0;
98186857b36SDavid Xu 	it->it_usecount = 0;
98286857b36SDavid Xu 	it->it_active = 0;
98356c06c4bSDavid Xu 	timespecclear(&it->it_time.it_value);
98456c06c4bSDavid Xu 	timespecclear(&it->it_time.it_interval);
98586857b36SDavid Xu 	it->it_overrun = 0;
98686857b36SDavid Xu 	it->it_overrun_last = 0;
98786857b36SDavid Xu 	it->it_clockid = clock_id;
98886857b36SDavid Xu 	it->it_timerid = -1;
98986857b36SDavid Xu 	it->it_proc = p;
99086857b36SDavid Xu 	ksiginfo_init(&it->it_ksi);
99186857b36SDavid Xu 	it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
99286857b36SDavid Xu 	error = CLOCK_CALL(clock_id, timer_create, (it));
99386857b36SDavid Xu 	if (error != 0)
99486857b36SDavid Xu 		goto out;
99586857b36SDavid Xu 
99686857b36SDavid Xu 	PROC_LOCK(p);
99786857b36SDavid Xu 	if (preset_id != -1) {
99886857b36SDavid Xu 		KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
99986857b36SDavid Xu 		id = preset_id;
100060354683SDavid Xu 		if (p->p_itimers->its_timers[id] != NULL) {
100186857b36SDavid Xu 			PROC_UNLOCK(p);
100286857b36SDavid Xu 			error = 0;
100386857b36SDavid Xu 			goto out;
100486857b36SDavid Xu 		}
100586857b36SDavid Xu 	} else {
100686857b36SDavid Xu 		/*
100786857b36SDavid Xu 		 * Find a free timer slot, skipping those reserved
100886857b36SDavid Xu 		 * for setitimer().
100986857b36SDavid Xu 		 */
101086857b36SDavid Xu 		for (id = 3; id < TIMER_MAX; id++)
101160354683SDavid Xu 			if (p->p_itimers->its_timers[id] == NULL)
101286857b36SDavid Xu 				break;
101386857b36SDavid Xu 		if (id == TIMER_MAX) {
101486857b36SDavid Xu 			PROC_UNLOCK(p);
101586857b36SDavid Xu 			error = EAGAIN;
101686857b36SDavid Xu 			goto out;
101786857b36SDavid Xu 		}
101886857b36SDavid Xu 	}
101986857b36SDavid Xu 	it->it_timerid = id;
102060354683SDavid Xu 	p->p_itimers->its_timers[id] = it;
102186857b36SDavid Xu 	if (evp != NULL)
102286857b36SDavid Xu 		it->it_sigev = *evp;
102386857b36SDavid Xu 	else {
102486857b36SDavid Xu 		it->it_sigev.sigev_notify = SIGEV_SIGNAL;
102586857b36SDavid Xu 		switch (clock_id) {
102686857b36SDavid Xu 		default:
102786857b36SDavid Xu 		case CLOCK_REALTIME:
102886857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGALRM;
102986857b36SDavid Xu 			break;
103086857b36SDavid Xu 		case CLOCK_VIRTUAL:
103186857b36SDavid Xu  			it->it_sigev.sigev_signo = SIGVTALRM;
103286857b36SDavid Xu 			break;
103386857b36SDavid Xu 		case CLOCK_PROF:
103486857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGPROF;
103586857b36SDavid Xu 			break;
103686857b36SDavid Xu 		}
10378f0371f1SDavid Xu 		it->it_sigev.sigev_value.sival_int = id;
103886857b36SDavid Xu 	}
103986857b36SDavid Xu 
104056c06c4bSDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
104156c06c4bSDavid Xu 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
104286857b36SDavid Xu 		it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
104386857b36SDavid Xu 		it->it_ksi.ksi_code = SI_TIMER;
104486857b36SDavid Xu 		it->it_ksi.ksi_value = it->it_sigev.sigev_value;
104586857b36SDavid Xu 		it->it_ksi.ksi_timerid = id;
104686857b36SDavid Xu 	}
104786857b36SDavid Xu 	PROC_UNLOCK(p);
104886857b36SDavid Xu 	*timerid = id;
104986857b36SDavid Xu 	return (0);
105086857b36SDavid Xu 
105186857b36SDavid Xu out:
105286857b36SDavid Xu 	ITIMER_LOCK(it);
105386857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
105486857b36SDavid Xu 	ITIMER_UNLOCK(it);
105586857b36SDavid Xu 	uma_zfree(itimer_zone, it);
105686857b36SDavid Xu 	return (error);
105786857b36SDavid Xu }
105886857b36SDavid Xu 
105986857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
106061d3a4efSDavid Xu struct ktimer_delete_args {
106161d3a4efSDavid Xu 	int timerid;
106286857b36SDavid Xu };
106386857b36SDavid Xu #endif
106486857b36SDavid Xu int
106561d3a4efSDavid Xu ktimer_delete(struct thread *td, struct ktimer_delete_args *uap)
106686857b36SDavid Xu {
106786857b36SDavid Xu 	return (kern_timer_delete(td, uap->timerid));
106886857b36SDavid Xu }
106986857b36SDavid Xu 
107086857b36SDavid Xu static struct itimer *
1071843b99c6SDavid Xu itimer_find(struct proc *p, int timerid)
107286857b36SDavid Xu {
107386857b36SDavid Xu 	struct itimer *it;
107486857b36SDavid Xu 
107586857b36SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
10763f935cf3SColin Percival 	if ((p->p_itimers == NULL) ||
10773f935cf3SColin Percival 	    (timerid < 0) || (timerid >= TIMER_MAX) ||
107860354683SDavid Xu 	    (it = p->p_itimers->its_timers[timerid]) == NULL) {
107986857b36SDavid Xu 		return (NULL);
108086857b36SDavid Xu 	}
108186857b36SDavid Xu 	ITIMER_LOCK(it);
1082843b99c6SDavid Xu 	if ((it->it_flags & ITF_DELETING) != 0) {
108386857b36SDavid Xu 		ITIMER_UNLOCK(it);
108486857b36SDavid Xu 		it = NULL;
108586857b36SDavid Xu 	}
108686857b36SDavid Xu 	return (it);
108786857b36SDavid Xu }
108886857b36SDavid Xu 
108986857b36SDavid Xu static int
109061d3a4efSDavid Xu kern_timer_delete(struct thread *td, int timerid)
109186857b36SDavid Xu {
109286857b36SDavid Xu 	struct proc *p = td->td_proc;
109386857b36SDavid Xu 	struct itimer *it;
109486857b36SDavid Xu 
109586857b36SDavid Xu 	PROC_LOCK(p);
1096843b99c6SDavid Xu 	it = itimer_find(p, timerid);
109786857b36SDavid Xu 	if (it == NULL) {
109886857b36SDavid Xu 		PROC_UNLOCK(p);
109986857b36SDavid Xu 		return (EINVAL);
110086857b36SDavid Xu 	}
110186857b36SDavid Xu 	PROC_UNLOCK(p);
110286857b36SDavid Xu 
110386857b36SDavid Xu 	it->it_flags |= ITF_DELETING;
110486857b36SDavid Xu 	while (it->it_usecount > 0) {
110586857b36SDavid Xu 		it->it_flags |= ITF_WANTED;
110686857b36SDavid Xu 		msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
110786857b36SDavid Xu 	}
110886857b36SDavid Xu 	it->it_flags &= ~ITF_WANTED;
110986857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
111086857b36SDavid Xu 	ITIMER_UNLOCK(it);
111186857b36SDavid Xu 
111286857b36SDavid Xu 	PROC_LOCK(p);
111386857b36SDavid Xu 	if (KSI_ONQ(&it->it_ksi))
111486857b36SDavid Xu 		sigqueue_take(&it->it_ksi);
111560354683SDavid Xu 	p->p_itimers->its_timers[timerid] = NULL;
111686857b36SDavid Xu 	PROC_UNLOCK(p);
111786857b36SDavid Xu 	uma_zfree(itimer_zone, it);
111886857b36SDavid Xu 	return (0);
111986857b36SDavid Xu }
112086857b36SDavid Xu 
112186857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
112261d3a4efSDavid Xu struct ktimer_settime_args {
112361d3a4efSDavid Xu 	int timerid;
112486857b36SDavid Xu 	int flags;
112586857b36SDavid Xu 	const struct itimerspec * value;
112686857b36SDavid Xu 	struct itimerspec * ovalue;
112786857b36SDavid Xu };
112886857b36SDavid Xu #endif
112986857b36SDavid Xu int
113061d3a4efSDavid Xu ktimer_settime(struct thread *td, struct ktimer_settime_args *uap)
113186857b36SDavid Xu {
113286857b36SDavid Xu 	struct proc *p = td->td_proc;
113386857b36SDavid Xu 	struct itimer *it;
113486857b36SDavid Xu 	struct itimerspec val, oval, *ovalp;
113586857b36SDavid Xu 	int error;
113686857b36SDavid Xu 
113786857b36SDavid Xu 	error = copyin(uap->value, &val, sizeof(val));
113886857b36SDavid Xu 	if (error != 0)
113986857b36SDavid Xu 		return (error);
114086857b36SDavid Xu 
114186857b36SDavid Xu 	if (uap->ovalue != NULL)
114286857b36SDavid Xu 		ovalp = &oval;
114386857b36SDavid Xu 	else
114486857b36SDavid Xu 		ovalp = NULL;
114586857b36SDavid Xu 
114686857b36SDavid Xu 	PROC_LOCK(p);
114786857b36SDavid Xu 	if (uap->timerid < 3 ||
1148843b99c6SDavid Xu 	    (it = itimer_find(p, uap->timerid)) == NULL) {
114986857b36SDavid Xu 		PROC_UNLOCK(p);
115086857b36SDavid Xu 		error = EINVAL;
115186857b36SDavid Xu 	} else {
115286857b36SDavid Xu 		PROC_UNLOCK(p);
115386857b36SDavid Xu 		itimer_enter(it);
115486857b36SDavid Xu 		error = CLOCK_CALL(it->it_clockid, timer_settime,
115586857b36SDavid Xu 				(it, uap->flags, &val, ovalp));
115686857b36SDavid Xu 		itimer_leave(it);
115786857b36SDavid Xu 		ITIMER_UNLOCK(it);
115886857b36SDavid Xu 	}
115986857b36SDavid Xu 	if (error == 0 && uap->ovalue != NULL)
116086857b36SDavid Xu 		error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
116186857b36SDavid Xu 	return (error);
116286857b36SDavid Xu }
116386857b36SDavid Xu 
116486857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
116561d3a4efSDavid Xu struct ktimer_gettime_args {
116661d3a4efSDavid Xu 	int timerid;
116786857b36SDavid Xu 	struct itimerspec * value;
116886857b36SDavid Xu };
116986857b36SDavid Xu #endif
117086857b36SDavid Xu int
117161d3a4efSDavid Xu ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap)
117286857b36SDavid Xu {
117386857b36SDavid Xu 	struct proc *p = td->td_proc;
117486857b36SDavid Xu 	struct itimer *it;
117586857b36SDavid Xu 	struct itimerspec val;
117686857b36SDavid Xu 	int error;
117786857b36SDavid Xu 
117886857b36SDavid Xu 	PROC_LOCK(p);
117986857b36SDavid Xu 	if (uap->timerid < 3 ||
1180843b99c6SDavid Xu 	   (it = itimer_find(p, uap->timerid)) == NULL) {
118186857b36SDavid Xu 		PROC_UNLOCK(p);
118286857b36SDavid Xu 		error = EINVAL;
118386857b36SDavid Xu 	} else {
118486857b36SDavid Xu 		PROC_UNLOCK(p);
118586857b36SDavid Xu 		itimer_enter(it);
118686857b36SDavid Xu 		error = CLOCK_CALL(it->it_clockid, timer_gettime,
118786857b36SDavid Xu 				(it, &val));
118886857b36SDavid Xu 		itimer_leave(it);
118986857b36SDavid Xu 		ITIMER_UNLOCK(it);
119086857b36SDavid Xu 	}
119186857b36SDavid Xu 	if (error == 0)
119286857b36SDavid Xu 		error = copyout(&val, uap->value, sizeof(val));
119386857b36SDavid Xu 	return (error);
119486857b36SDavid Xu }
119586857b36SDavid Xu 
119686857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
119786857b36SDavid Xu struct timer_getoverrun_args {
119861d3a4efSDavid Xu 	int timerid;
119986857b36SDavid Xu };
120086857b36SDavid Xu #endif
120186857b36SDavid Xu int
120261d3a4efSDavid Xu ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap)
120386857b36SDavid Xu {
120486857b36SDavid Xu 	struct proc *p = td->td_proc;
120586857b36SDavid Xu 	struct itimer *it;
120686857b36SDavid Xu 	int error ;
120786857b36SDavid Xu 
120886857b36SDavid Xu 	PROC_LOCK(p);
120986857b36SDavid Xu 	if (uap->timerid < 3 ||
1210843b99c6SDavid Xu 	    (it = itimer_find(p, uap->timerid)) == NULL) {
121186857b36SDavid Xu 		PROC_UNLOCK(p);
121286857b36SDavid Xu 		error = EINVAL;
121386857b36SDavid Xu 	} else {
121486857b36SDavid Xu 		td->td_retval[0] = it->it_overrun_last;
121586857b36SDavid Xu 		ITIMER_UNLOCK(it);
121656c06c4bSDavid Xu 		PROC_UNLOCK(p);
121786857b36SDavid Xu 		error = 0;
121886857b36SDavid Xu 	}
121986857b36SDavid Xu 	return (error);
122086857b36SDavid Xu }
122186857b36SDavid Xu 
122286857b36SDavid Xu static int
122386857b36SDavid Xu realtimer_create(struct itimer *it)
122486857b36SDavid Xu {
122586857b36SDavid Xu 	callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
122686857b36SDavid Xu 	return (0);
122786857b36SDavid Xu }
122886857b36SDavid Xu 
122986857b36SDavid Xu static int
123086857b36SDavid Xu realtimer_delete(struct itimer *it)
123186857b36SDavid Xu {
123286857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
1233843b99c6SDavid Xu 
1234d6b6592eSDavid Xu 	/*
1235d6b6592eSDavid Xu 	 * clear timer's value and interval to tell realtimer_expire
1236d6b6592eSDavid Xu 	 * to not rearm the timer.
1237d6b6592eSDavid Xu 	 */
1238d6b6592eSDavid Xu 	timespecclear(&it->it_time.it_value);
1239d6b6592eSDavid Xu 	timespecclear(&it->it_time.it_interval);
1240843b99c6SDavid Xu 	ITIMER_UNLOCK(it);
1241843b99c6SDavid Xu 	callout_drain(&it->it_callout);
1242843b99c6SDavid Xu 	ITIMER_LOCK(it);
124386857b36SDavid Xu 	return (0);
124486857b36SDavid Xu }
124586857b36SDavid Xu 
124686857b36SDavid Xu static int
124786857b36SDavid Xu realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
124886857b36SDavid Xu {
124956c06c4bSDavid Xu 	struct timespec cts;
125086857b36SDavid Xu 
125186857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
125286857b36SDavid Xu 
125356c06c4bSDavid Xu 	realtimer_clocktime(it->it_clockid, &cts);
125456c06c4bSDavid Xu 	*ovalue = it->it_time;
125586857b36SDavid Xu 	if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
125656c06c4bSDavid Xu 		timespecsub(&ovalue->it_value, &cts);
125786857b36SDavid Xu 		if (ovalue->it_value.tv_sec < 0 ||
125886857b36SDavid Xu 		    (ovalue->it_value.tv_sec == 0 &&
125986857b36SDavid Xu 		     ovalue->it_value.tv_nsec == 0)) {
126086857b36SDavid Xu 			ovalue->it_value.tv_sec  = 0;
126186857b36SDavid Xu 			ovalue->it_value.tv_nsec = 1;
126286857b36SDavid Xu 		}
126386857b36SDavid Xu 	}
126486857b36SDavid Xu 	return (0);
126586857b36SDavid Xu }
126686857b36SDavid Xu 
126786857b36SDavid Xu static int
126886857b36SDavid Xu realtimer_settime(struct itimer *it, int flags,
126986857b36SDavid Xu 	struct itimerspec *value, struct itimerspec *ovalue)
127086857b36SDavid Xu {
127156c06c4bSDavid Xu 	struct timespec cts, ts;
127256c06c4bSDavid Xu 	struct timeval tv;
127356c06c4bSDavid Xu 	struct itimerspec val;
127486857b36SDavid Xu 
127586857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
127686857b36SDavid Xu 
127756c06c4bSDavid Xu 	val = *value;
127856c06c4bSDavid Xu 	if (itimespecfix(&val.it_value))
127986857b36SDavid Xu 		return (EINVAL);
128086857b36SDavid Xu 
128156c06c4bSDavid Xu 	if (timespecisset(&val.it_value)) {
128256c06c4bSDavid Xu 		if (itimespecfix(&val.it_interval))
128386857b36SDavid Xu 			return (EINVAL);
128486857b36SDavid Xu 	} else {
128556c06c4bSDavid Xu 		timespecclear(&val.it_interval);
128686857b36SDavid Xu 	}
128786857b36SDavid Xu 
128886857b36SDavid Xu 	if (ovalue != NULL)
128986857b36SDavid Xu 		realtimer_gettime(it, ovalue);
129086857b36SDavid Xu 
129186857b36SDavid Xu 	it->it_time = val;
129256c06c4bSDavid Xu 	if (timespecisset(&val.it_value)) {
129356c06c4bSDavid Xu 		realtimer_clocktime(it->it_clockid, &cts);
129456c06c4bSDavid Xu 		ts = val.it_value;
129586857b36SDavid Xu 		if ((flags & TIMER_ABSTIME) == 0) {
129686857b36SDavid Xu 			/* Convert to absolute time. */
129756c06c4bSDavid Xu 			timespecadd(&it->it_time.it_value, &cts);
129886857b36SDavid Xu 		} else {
129956c06c4bSDavid Xu 			timespecsub(&ts, &cts);
130086857b36SDavid Xu 			/*
130156c06c4bSDavid Xu 			 * We don't care if ts is negative, tztohz will
130286857b36SDavid Xu 			 * fix it.
130386857b36SDavid Xu 			 */
130486857b36SDavid Xu 		}
130556c06c4bSDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
130656c06c4bSDavid Xu 		callout_reset(&it->it_callout, tvtohz(&tv),
130786857b36SDavid Xu 			realtimer_expire, it);
130886857b36SDavid Xu 	} else {
130986857b36SDavid Xu 		callout_stop(&it->it_callout);
131086857b36SDavid Xu 	}
131186857b36SDavid Xu 
131286857b36SDavid Xu 	return (0);
131386857b36SDavid Xu }
131486857b36SDavid Xu 
131586857b36SDavid Xu static void
131656c06c4bSDavid Xu realtimer_clocktime(clockid_t id, struct timespec *ts)
131786857b36SDavid Xu {
131886857b36SDavid Xu 	if (id == CLOCK_REALTIME)
131956c06c4bSDavid Xu 		getnanotime(ts);
132086857b36SDavid Xu 	else	/* CLOCK_MONOTONIC */
132156c06c4bSDavid Xu 		getnanouptime(ts);
132256c06c4bSDavid Xu }
132356c06c4bSDavid Xu 
132456c06c4bSDavid Xu int
132561d3a4efSDavid Xu itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi)
132656c06c4bSDavid Xu {
132756c06c4bSDavid Xu 	struct itimer *it;
132856c06c4bSDavid Xu 
132956c06c4bSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
1330843b99c6SDavid Xu 	it = itimer_find(p, timerid);
133156c06c4bSDavid Xu 	if (it != NULL) {
133256c06c4bSDavid Xu 		ksi->ksi_overrun = it->it_overrun;
133356c06c4bSDavid Xu 		it->it_overrun_last = it->it_overrun;
133456c06c4bSDavid Xu 		it->it_overrun = 0;
133556c06c4bSDavid Xu 		ITIMER_UNLOCK(it);
133656c06c4bSDavid Xu 		return (0);
133756c06c4bSDavid Xu 	}
133856c06c4bSDavid Xu 	return (EINVAL);
133956c06c4bSDavid Xu }
134056c06c4bSDavid Xu 
134156c06c4bSDavid Xu int
134256c06c4bSDavid Xu itimespecfix(struct timespec *ts)
134356c06c4bSDavid Xu {
134456c06c4bSDavid Xu 
134556c06c4bSDavid Xu 	if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
134656c06c4bSDavid Xu 		return (EINVAL);
134756c06c4bSDavid Xu 	if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
134856c06c4bSDavid Xu 		ts->tv_nsec = tick * 1000;
134956c06c4bSDavid Xu 	return (0);
135086857b36SDavid Xu }
135186857b36SDavid Xu 
135286857b36SDavid Xu /* Timeout callback for realtime timer */
135386857b36SDavid Xu static void
135486857b36SDavid Xu realtimer_expire(void *arg)
135586857b36SDavid Xu {
135656c06c4bSDavid Xu 	struct timespec cts, ts;
135756c06c4bSDavid Xu 	struct timeval tv;
135886857b36SDavid Xu 	struct itimer *it;
135986857b36SDavid Xu 
136086857b36SDavid Xu 	it = (struct itimer *)arg;
136186857b36SDavid Xu 
136256c06c4bSDavid Xu 	realtimer_clocktime(it->it_clockid, &cts);
136386857b36SDavid Xu 	/* Only fire if time is reached. */
136456c06c4bSDavid Xu 	if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
136556c06c4bSDavid Xu 		if (timespecisset(&it->it_time.it_interval)) {
136656c06c4bSDavid Xu 			timespecadd(&it->it_time.it_value,
136786857b36SDavid Xu 				    &it->it_time.it_interval);
136856c06c4bSDavid Xu 			while (timespeccmp(&cts, &it->it_time.it_value, >=)) {
136977e718f7SDavid Xu 				if (it->it_overrun < INT_MAX)
137086857b36SDavid Xu 					it->it_overrun++;
137177e718f7SDavid Xu 				else
137277e718f7SDavid Xu 					it->it_ksi.ksi_errno = ERANGE;
137356c06c4bSDavid Xu 				timespecadd(&it->it_time.it_value,
137486857b36SDavid Xu 					    &it->it_time.it_interval);
137586857b36SDavid Xu 			}
137686857b36SDavid Xu 		} else {
137786857b36SDavid Xu 			/* single shot timer ? */
137856c06c4bSDavid Xu 			timespecclear(&it->it_time.it_value);
137986857b36SDavid Xu 		}
138056c06c4bSDavid Xu 		if (timespecisset(&it->it_time.it_value)) {
138156c06c4bSDavid Xu 			ts = it->it_time.it_value;
138256c06c4bSDavid Xu 			timespecsub(&ts, &cts);
138356c06c4bSDavid Xu 			TIMESPEC_TO_TIMEVAL(&tv, &ts);
138456c06c4bSDavid Xu 			callout_reset(&it->it_callout, tvtohz(&tv),
138586857b36SDavid Xu 				 realtimer_expire, it);
138686857b36SDavid Xu 		}
1387d6b6592eSDavid Xu 		itimer_enter(it);
138886857b36SDavid Xu 		ITIMER_UNLOCK(it);
138986857b36SDavid Xu 		itimer_fire(it);
139086857b36SDavid Xu 		ITIMER_LOCK(it);
1391d6b6592eSDavid Xu 		itimer_leave(it);
139256c06c4bSDavid Xu 	} else if (timespecisset(&it->it_time.it_value)) {
139356c06c4bSDavid Xu 		ts = it->it_time.it_value;
139456c06c4bSDavid Xu 		timespecsub(&ts, &cts);
139556c06c4bSDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
139656c06c4bSDavid Xu 		callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
139786857b36SDavid Xu  			it);
139886857b36SDavid Xu 	}
139986857b36SDavid Xu }
140086857b36SDavid Xu 
140186857b36SDavid Xu void
140286857b36SDavid Xu itimer_fire(struct itimer *it)
140386857b36SDavid Xu {
140486857b36SDavid Xu 	struct proc *p = it->it_proc;
1405*cf7d9a8cSDavid Xu 	struct thread *td;
140686857b36SDavid Xu 
140756c06c4bSDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
140856c06c4bSDavid Xu 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1409*cf7d9a8cSDavid Xu 		if (sigev_findtd(p, &it->it_sigev, &td) != 0) {
141056c06c4bSDavid Xu 			ITIMER_LOCK(it);
141156c06c4bSDavid Xu 			timespecclear(&it->it_time.it_value);
141256c06c4bSDavid Xu 			timespecclear(&it->it_time.it_interval);
141356c06c4bSDavid Xu 			callout_stop(&it->it_callout);
141456c06c4bSDavid Xu 			ITIMER_UNLOCK(it);
1415*cf7d9a8cSDavid Xu 			return;
14166d7b314bSDavid Xu 		}
1417*cf7d9a8cSDavid Xu 		if (!KSI_ONQ(&it->it_ksi)) {
1418*cf7d9a8cSDavid Xu 			it->it_ksi.ksi_errno = 0;
1419*cf7d9a8cSDavid Xu 			ksiginfo_set_sigev(&it->it_ksi, &it->it_sigev);
1420*cf7d9a8cSDavid Xu 			tdsendsignal(p, td, it->it_ksi.ksi_signo, &it->it_ksi);
142156c06c4bSDavid Xu 		} else {
142277e718f7SDavid Xu 			if (it->it_overrun < INT_MAX)
14236d7b314bSDavid Xu 				it->it_overrun++;
142477e718f7SDavid Xu 			else
142577e718f7SDavid Xu 				it->it_ksi.ksi_errno = ERANGE;
142656c06c4bSDavid Xu 		}
142786857b36SDavid Xu 		PROC_UNLOCK(p);
142886857b36SDavid Xu 	}
142986857b36SDavid Xu }
143086857b36SDavid Xu 
143186857b36SDavid Xu static void
143286857b36SDavid Xu itimers_alloc(struct proc *p)
143386857b36SDavid Xu {
143460354683SDavid Xu 	struct itimers *its;
143560354683SDavid Xu 	int i;
143686857b36SDavid Xu 
143760354683SDavid Xu 	its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
143886857b36SDavid Xu 	LIST_INIT(&its->its_virtual);
143986857b36SDavid Xu 	LIST_INIT(&its->its_prof);
144086857b36SDavid Xu 	TAILQ_INIT(&its->its_worklist);
144160354683SDavid Xu 	for (i = 0; i < TIMER_MAX; i++)
144260354683SDavid Xu 		its->its_timers[i] = NULL;
144360354683SDavid Xu 	PROC_LOCK(p);
144460354683SDavid Xu 	if (p->p_itimers == NULL) {
144560354683SDavid Xu 		p->p_itimers = its;
144660354683SDavid Xu 		PROC_UNLOCK(p);
144760354683SDavid Xu 	}
144860354683SDavid Xu 	else {
144960354683SDavid Xu 		PROC_UNLOCK(p);
145060354683SDavid Xu 		free(its, M_SUBPROC);
145160354683SDavid Xu 	}
145286857b36SDavid Xu }
145386857b36SDavid Xu 
1454993182e5SAlexander Leidinger static void
1455993182e5SAlexander Leidinger itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
1456993182e5SAlexander Leidinger {
1457993182e5SAlexander Leidinger 	itimers_event_hook_exit(arg, p);
1458993182e5SAlexander Leidinger }
1459993182e5SAlexander Leidinger 
146086857b36SDavid Xu /* Clean up timers when some process events are being triggered. */
1461d26b1a1fSDavid Xu static void
1462993182e5SAlexander Leidinger itimers_event_hook_exit(void *arg, struct proc *p)
146386857b36SDavid Xu {
146486857b36SDavid Xu 	struct itimers *its;
146586857b36SDavid Xu 	struct itimer *it;
14663e70c6f0SDavid Xu 	int event = (int)(intptr_t)arg;
146786857b36SDavid Xu 	int i;
146886857b36SDavid Xu 
146960354683SDavid Xu 	if (p->p_itimers != NULL) {
147060354683SDavid Xu 		its = p->p_itimers;
147186857b36SDavid Xu 		for (i = 0; i < MAX_CLOCKS; ++i) {
147286857b36SDavid Xu 			if (posix_clocks[i].event_hook != NULL)
147386857b36SDavid Xu 				CLOCK_CALL(i, event_hook, (p, i, event));
147486857b36SDavid Xu 		}
147586857b36SDavid Xu 		/*
147686857b36SDavid Xu 		 * According to susv3, XSI interval timers should be inherited
147786857b36SDavid Xu 		 * by new image.
147886857b36SDavid Xu 		 */
147986857b36SDavid Xu 		if (event == ITIMER_EV_EXEC)
148086857b36SDavid Xu 			i = 3;
148186857b36SDavid Xu 		else if (event == ITIMER_EV_EXIT)
148286857b36SDavid Xu 			i = 0;
148386857b36SDavid Xu 		else
148486857b36SDavid Xu 			panic("unhandled event");
148586857b36SDavid Xu 		for (; i < TIMER_MAX; ++i) {
1486843b99c6SDavid Xu 			if ((it = its->its_timers[i]) != NULL)
1487843b99c6SDavid Xu 				kern_timer_delete(curthread, i);
148886857b36SDavid Xu 		}
148986857b36SDavid Xu 		if (its->its_timers[0] == NULL &&
149086857b36SDavid Xu 		    its->its_timers[1] == NULL &&
149186857b36SDavid Xu 		    its->its_timers[2] == NULL) {
149260354683SDavid Xu 			free(its, M_SUBPROC);
149360354683SDavid Xu 			p->p_itimers = NULL;
149486857b36SDavid Xu 		}
149586857b36SDavid Xu 	}
149686857b36SDavid Xu }
1497