xref: /freebsd/sys/kern/kern_time.c (revision 00d6ac63cdfa1ca702907490d9d4753651b7c3db)
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 *);
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 *);
84843b99c6SDavid Xu static struct itimer *itimer_find(struct proc *, int);
8586857b36SDavid Xu static void	itimers_alloc(struct proc *);
86993182e5SAlexander Leidinger static void	itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp);
87993182e5SAlexander Leidinger static void	itimers_event_hook_exit(void *arg, struct proc *p);
8886857b36SDavid Xu static int	realtimer_create(struct itimer *);
8986857b36SDavid Xu static int	realtimer_gettime(struct itimer *, struct itimerspec *);
9086857b36SDavid Xu static int	realtimer_settime(struct itimer *, int,
9186857b36SDavid Xu 			struct itimerspec *, struct itimerspec *);
9286857b36SDavid Xu static int	realtimer_delete(struct itimer *);
9356c06c4bSDavid Xu static void	realtimer_clocktime(clockid_t, struct timespec *);
9486857b36SDavid Xu static void	realtimer_expire(void *);
9586857b36SDavid Xu static int	kern_timer_create(struct thread *, clockid_t,
9661d3a4efSDavid Xu 			struct sigevent *, int *, int);
9761d3a4efSDavid Xu static int	kern_timer_delete(struct thread *, int);
9886857b36SDavid Xu 
9986857b36SDavid Xu int		register_posix_clock(int, struct kclock *);
10086857b36SDavid Xu void		itimer_fire(struct itimer *it);
10156c06c4bSDavid Xu int		itimespecfix(struct timespec *ts);
10286857b36SDavid Xu 
10386857b36SDavid Xu #define CLOCK_CALL(clock, call, arglist)		\
10486857b36SDavid Xu 	((*posix_clocks[clock].call) arglist)
10586857b36SDavid Xu 
10686857b36SDavid Xu SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL);
10786857b36SDavid Xu 
10886857b36SDavid Xu 
1091b09ae77SPoul-Henning Kamp static void
1101b09ae77SPoul-Henning Kamp no_lease_updatetime(deltat)
1111b09ae77SPoul-Henning Kamp 	int deltat;
1121b09ae77SPoul-Henning Kamp {
1131b09ae77SPoul-Henning Kamp }
1141b09ae77SPoul-Henning Kamp 
1154d77a549SAlfred Perlstein void (*lease_updatetime)(int)  = no_lease_updatetime;
1161b09ae77SPoul-Henning Kamp 
11794c8fcd8SPeter Wemm static int
11891afe087SPoul-Henning Kamp settime(struct thread *td, struct timeval *tv)
11994c8fcd8SPeter Wemm {
120fcae3aa6SNick Sayer 	struct timeval delta, tv1, tv2;
121c0bd94a7SNick Sayer 	static struct timeval maxtime, laststep;
1227ec73f64SPoul-Henning Kamp 	struct timespec ts;
12394c8fcd8SPeter Wemm 	int s;
12494c8fcd8SPeter Wemm 
125708e7684SPeter Wemm 	s = splclock();
1269c8fff87SBruce Evans 	microtime(&tv1);
12700af9731SPoul-Henning Kamp 	delta = *tv;
12800af9731SPoul-Henning Kamp 	timevalsub(&delta, &tv1);
12994c8fcd8SPeter Wemm 
13094c8fcd8SPeter Wemm 	/*
1319c8fff87SBruce Evans 	 * If the system is secure, we do not allow the time to be
132fcae3aa6SNick Sayer 	 * set to a value earlier than 1 second less than the highest
133fcae3aa6SNick Sayer 	 * time we have yet seen. The worst a miscreant can do in
134fcae3aa6SNick Sayer 	 * this circumstance is "freeze" time. He couldn't go
135fcae3aa6SNick Sayer 	 * back to the past.
136c0bd94a7SNick Sayer 	 *
137c0bd94a7SNick Sayer 	 * We similarly do not allow the clock to be stepped more
138c0bd94a7SNick Sayer 	 * than one second, nor more than once per second. This allows
139c0bd94a7SNick Sayer 	 * a miscreant to make the clock march double-time, but no worse.
14094c8fcd8SPeter Wemm 	 */
1417edfb592SJohn Baldwin 	if (securelevel_gt(td->td_ucred, 1) != 0) {
142fcae3aa6SNick Sayer 		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
1433f92429aSMatt Jacob 			/*
144c0bd94a7SNick Sayer 			 * Update maxtime to latest time we've seen.
1453f92429aSMatt Jacob 			 */
146fcae3aa6SNick Sayer 			if (tv1.tv_sec > maxtime.tv_sec)
147fcae3aa6SNick Sayer 				maxtime = tv1;
148fcae3aa6SNick Sayer 			tv2 = *tv;
149fcae3aa6SNick Sayer 			timevalsub(&tv2, &maxtime);
150fcae3aa6SNick Sayer 			if (tv2.tv_sec < -1) {
1513f92429aSMatt Jacob 				tv->tv_sec = maxtime.tv_sec - 1;
152fcae3aa6SNick Sayer 				printf("Time adjustment clamped to -1 second\n");
153fcae3aa6SNick Sayer 			}
1543f92429aSMatt Jacob 		} else {
155c0bd94a7SNick Sayer 			if (tv1.tv_sec == laststep.tv_sec) {
156c0bd94a7SNick Sayer 				splx(s);
157c0bd94a7SNick Sayer 				return (EPERM);
158c0bd94a7SNick Sayer 			}
159c0bd94a7SNick Sayer 			if (delta.tv_sec > 1) {
160c0bd94a7SNick Sayer 				tv->tv_sec = tv1.tv_sec + 1;
161c0bd94a7SNick Sayer 				printf("Time adjustment clamped to +1 second\n");
162c0bd94a7SNick Sayer 			}
163c0bd94a7SNick Sayer 			laststep = *tv;
164fcae3aa6SNick Sayer 		}
1659c8fff87SBruce Evans 	}
1669c8fff87SBruce Evans 
1677ec73f64SPoul-Henning Kamp 	ts.tv_sec = tv->tv_sec;
1687ec73f64SPoul-Henning Kamp 	ts.tv_nsec = tv->tv_usec * 1000;
1697edfb592SJohn Baldwin 	mtx_lock(&Giant);
17091266b96SPoul-Henning Kamp 	tc_setclock(&ts);
17194c8fcd8SPeter Wemm 	(void) splsoftclock();
17294c8fcd8SPeter Wemm 	lease_updatetime(delta.tv_sec);
17394c8fcd8SPeter Wemm 	splx(s);
17494c8fcd8SPeter Wemm 	resettodr();
1757edfb592SJohn Baldwin 	mtx_unlock(&Giant);
17694c8fcd8SPeter Wemm 	return (0);
17794c8fcd8SPeter Wemm }
17894c8fcd8SPeter Wemm 
17994c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
18094c8fcd8SPeter Wemm struct clock_gettime_args {
18194c8fcd8SPeter Wemm 	clockid_t clock_id;
18294c8fcd8SPeter Wemm 	struct	timespec *tp;
18394c8fcd8SPeter Wemm };
18494c8fcd8SPeter Wemm #endif
18594c8fcd8SPeter Wemm /* ARGSUSED */
18694c8fcd8SPeter Wemm int
18791afe087SPoul-Henning Kamp clock_gettime(struct thread *td, struct clock_gettime_args *uap)
18894c8fcd8SPeter Wemm {
18994c8fcd8SPeter Wemm 	struct timespec ats;
190f0b479cdSPaul Saab 	int error;
191f0b479cdSPaul Saab 
192f0b479cdSPaul Saab 	error = kern_clock_gettime(td, uap->clock_id, &ats);
193f0b479cdSPaul Saab 	if (error == 0)
194f0b479cdSPaul Saab 		error = copyout(&ats, uap->tp, sizeof(ats));
195f0b479cdSPaul Saab 
196f0b479cdSPaul Saab 	return (error);
197f0b479cdSPaul Saab }
198f0b479cdSPaul Saab 
199f0b479cdSPaul Saab int
200f0b479cdSPaul Saab kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
201f0b479cdSPaul Saab {
202de0a9241SKelly Yancey 	struct timeval sys, user;
20378c85e8dSJohn Baldwin 	struct proc *p;
20400d6ac63SDavid Xu 	uint64_t runtime, curtime, switchtime;
20594c8fcd8SPeter Wemm 
20678c85e8dSJohn Baldwin 	p = td->td_proc;
207f0b479cdSPaul Saab 	switch (clock_id) {
2085e758b95SRobert Watson 	case CLOCK_REALTIME:		/* Default to precise. */
2095e758b95SRobert Watson 	case CLOCK_REALTIME_PRECISE:
210f0b479cdSPaul Saab 		nanotime(ats);
211b8817154SKelly Yancey 		break;
2125e758b95SRobert Watson 	case CLOCK_REALTIME_FAST:
2135e758b95SRobert Watson 		getnanotime(ats);
2145e758b95SRobert Watson 		break;
215b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
21678c85e8dSJohn Baldwin 		PROC_LOCK(p);
217a1fe14bcSAttilio Rao 		PROC_SLOCK(p);
21878c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
219a1fe14bcSAttilio Rao 		PROC_SUNLOCK(p);
22078c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
221f0b479cdSPaul Saab 		TIMEVAL_TO_TIMESPEC(&user, ats);
222b8817154SKelly Yancey 		break;
223b8817154SKelly Yancey 	case CLOCK_PROF:
22478c85e8dSJohn Baldwin 		PROC_LOCK(p);
225a1fe14bcSAttilio Rao 		PROC_SLOCK(p);
22678c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
227a1fe14bcSAttilio Rao 		PROC_SUNLOCK(p);
22878c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
229de0a9241SKelly Yancey 		timevaladd(&user, &sys);
230f0b479cdSPaul Saab 		TIMEVAL_TO_TIMESPEC(&user, ats);
231de0a9241SKelly Yancey 		break;
2325e758b95SRobert Watson 	case CLOCK_MONOTONIC:		/* Default to precise. */
2335e758b95SRobert Watson 	case CLOCK_MONOTONIC_PRECISE:
2345eefd889SAndre Oppermann 	case CLOCK_UPTIME:
2355e758b95SRobert Watson 	case CLOCK_UPTIME_PRECISE:
236f0b479cdSPaul Saab 		nanouptime(ats);
237b8817154SKelly Yancey 		break;
2385e758b95SRobert Watson 	case CLOCK_UPTIME_FAST:
2395e758b95SRobert Watson 	case CLOCK_MONOTONIC_FAST:
2405e758b95SRobert Watson 		getnanouptime(ats);
2415e758b95SRobert Watson 		break;
2425e758b95SRobert Watson 	case CLOCK_SECOND:
2435e758b95SRobert Watson 		ats->tv_sec = time_second;
2445e758b95SRobert Watson 		ats->tv_nsec = 0;
2455e758b95SRobert Watson 		break;
24600d6ac63SDavid Xu 	case CLOCK_THREAD_CPUTIME_ID:
24700d6ac63SDavid Xu 		critical_enter();
24800d6ac63SDavid Xu 		switchtime = PCPU_GET(switchtime);
24900d6ac63SDavid Xu 		curtime = cpu_ticks();
25000d6ac63SDavid Xu 		critical_exit();
25100d6ac63SDavid Xu 		runtime = cputick2usec(td->td_runtime + curtime - switchtime);
25200d6ac63SDavid Xu 		ats->tv_sec = runtime / 1000000;
25300d6ac63SDavid Xu 		ats->tv_nsec = runtime % 1000000 * 1000;
25400d6ac63SDavid Xu 		break;
255b8817154SKelly Yancey 	default:
2565cb3dc8fSPoul-Henning Kamp 		return (EINVAL);
257b8817154SKelly Yancey 	}
258f0b479cdSPaul Saab 	return (0);
25994c8fcd8SPeter Wemm }
26094c8fcd8SPeter Wemm 
26194c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
26294c8fcd8SPeter Wemm struct clock_settime_args {
26394c8fcd8SPeter Wemm 	clockid_t clock_id;
26494c8fcd8SPeter Wemm 	const struct	timespec *tp;
26594c8fcd8SPeter Wemm };
26694c8fcd8SPeter Wemm #endif
26794c8fcd8SPeter Wemm /* ARGSUSED */
26894c8fcd8SPeter Wemm int
26991afe087SPoul-Henning Kamp clock_settime(struct thread *td, struct clock_settime_args *uap)
27094c8fcd8SPeter Wemm {
27194c8fcd8SPeter Wemm 	struct timespec ats;
27294c8fcd8SPeter Wemm 	int error;
27394c8fcd8SPeter Wemm 
274f0b479cdSPaul Saab 	if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
275f0b479cdSPaul Saab 		return (error);
276f0b479cdSPaul Saab 	return (kern_clock_settime(td, uap->clock_id, &ats));
277f0b479cdSPaul Saab }
278f0b479cdSPaul Saab 
279f0b479cdSPaul Saab int
280f0b479cdSPaul Saab kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
281f0b479cdSPaul Saab {
282f0b479cdSPaul Saab 	struct timeval atv;
283f0b479cdSPaul Saab 	int error;
284f0b479cdSPaul Saab 
285acd3428bSRobert Watson 	if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
2867edfb592SJohn Baldwin 		return (error);
287f0b479cdSPaul Saab 	if (clock_id != CLOCK_REALTIME)
2887edfb592SJohn Baldwin 		return (EINVAL);
289f0b479cdSPaul Saab 	if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000)
2907edfb592SJohn Baldwin 		return (EINVAL);
291a0502b19SPoul-Henning Kamp 	/* XXX Don't convert nsec->usec and back */
292f0b479cdSPaul Saab 	TIMESPEC_TO_TIMEVAL(&atv, ats);
2937edfb592SJohn Baldwin 	error = settime(td, &atv);
29494c8fcd8SPeter Wemm 	return (error);
29594c8fcd8SPeter Wemm }
29694c8fcd8SPeter Wemm 
29794c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
29894c8fcd8SPeter Wemm struct clock_getres_args {
29994c8fcd8SPeter Wemm 	clockid_t clock_id;
30094c8fcd8SPeter Wemm 	struct	timespec *tp;
30194c8fcd8SPeter Wemm };
30294c8fcd8SPeter Wemm #endif
30394c8fcd8SPeter Wemm int
30491afe087SPoul-Henning Kamp clock_getres(struct thread *td, struct clock_getres_args *uap)
30594c8fcd8SPeter Wemm {
30694c8fcd8SPeter Wemm 	struct timespec ts;
307f0b479cdSPaul Saab 	int error;
30894c8fcd8SPeter Wemm 
309f0b479cdSPaul Saab 	if (uap->tp == NULL)
310f0b479cdSPaul Saab 		return (0);
311f0b479cdSPaul Saab 
312f0b479cdSPaul Saab 	error = kern_clock_getres(td, uap->clock_id, &ts);
313f0b479cdSPaul Saab 	if (error == 0)
314f0b479cdSPaul Saab 		error = copyout(&ts, uap->tp, sizeof(ts));
315f0b479cdSPaul Saab 	return (error);
316f0b479cdSPaul Saab }
317f0b479cdSPaul Saab 
318f0b479cdSPaul Saab int
319f0b479cdSPaul Saab kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
320f0b479cdSPaul Saab {
321f0b479cdSPaul Saab 
322f0b479cdSPaul Saab 	ts->tv_sec = 0;
323f0b479cdSPaul Saab 	switch (clock_id) {
324b8817154SKelly Yancey 	case CLOCK_REALTIME:
3255e758b95SRobert Watson 	case CLOCK_REALTIME_FAST:
3265e758b95SRobert Watson 	case CLOCK_REALTIME_PRECISE:
327b8817154SKelly Yancey 	case CLOCK_MONOTONIC:
3285e758b95SRobert Watson 	case CLOCK_MONOTONIC_FAST:
3295e758b95SRobert Watson 	case CLOCK_MONOTONIC_PRECISE:
3305eefd889SAndre Oppermann 	case CLOCK_UPTIME:
3315e758b95SRobert Watson 	case CLOCK_UPTIME_FAST:
3325e758b95SRobert Watson 	case CLOCK_UPTIME_PRECISE:
333ac0653dcSBruce Evans 		/*
334ac0653dcSBruce Evans 		 * Round up the result of the division cheaply by adding 1.
335ac0653dcSBruce Evans 		 * Rounding up is especially important if rounding down
336ac0653dcSBruce Evans 		 * would give 0.  Perfect rounding is unimportant.
337ac0653dcSBruce Evans 		 */
338f0b479cdSPaul Saab 		ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
339b8817154SKelly Yancey 		break;
340b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
341b8817154SKelly Yancey 	case CLOCK_PROF:
342b8817154SKelly Yancey 		/* Accurately round up here because we can do so cheaply. */
343f0b479cdSPaul Saab 		ts->tv_nsec = (1000000000 + hz - 1) / hz;
344b8817154SKelly Yancey 		break;
3455e758b95SRobert Watson 	case CLOCK_SECOND:
3465e758b95SRobert Watson 		ts->tv_sec = 1;
3475e758b95SRobert Watson 		ts->tv_nsec = 0;
3485e758b95SRobert Watson 		break;
34900d6ac63SDavid Xu 	case CLOCK_THREAD_CPUTIME_ID:
35000d6ac63SDavid Xu 		/* sync with cputick2usec */
35100d6ac63SDavid Xu 		ts->tv_nsec = 1000000 / cpu_tickrate();
35200d6ac63SDavid Xu 		if (ts->tv_nsec == 0)
35300d6ac63SDavid Xu 			ts->tv_nsec = 1000;
35400d6ac63SDavid Xu 		break;
355b8817154SKelly Yancey 	default:
356b8817154SKelly Yancey 		return (EINVAL);
35794c8fcd8SPeter Wemm 	}
358de0a9241SKelly Yancey 	return (0);
35994c8fcd8SPeter Wemm }
36094c8fcd8SPeter Wemm 
36194c8fcd8SPeter Wemm static int nanowait;
36294c8fcd8SPeter Wemm 
3637fdf2c85SPaul Saab int
3647fdf2c85SPaul Saab kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
3655b870b7bSPeter Wemm {
3665704ba6aSPoul-Henning Kamp 	struct timespec ts, ts2, ts3;
36733841826SPoul-Henning Kamp 	struct timeval tv;
36833841826SPoul-Henning Kamp 	int error;
3695b870b7bSPeter Wemm 
3707d7fb492SBruce Evans 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
371708e7684SPeter Wemm 		return (EINVAL);
372d254af07SMatthew Dillon 	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
3737d7fb492SBruce Evans 		return (0);
374c21410e1SPoul-Henning Kamp 	getnanouptime(&ts);
37500af9731SPoul-Henning Kamp 	timespecadd(&ts, rqt);
37633841826SPoul-Henning Kamp 	TIMESPEC_TO_TIMEVAL(&tv, rqt);
37733841826SPoul-Henning Kamp 	for (;;) {
37833841826SPoul-Henning Kamp 		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
37933841826SPoul-Henning Kamp 		    tvtohz(&tv));
380c21410e1SPoul-Henning Kamp 		getnanouptime(&ts2);
38133841826SPoul-Henning Kamp 		if (error != EWOULDBLOCK) {
38233841826SPoul-Henning Kamp 			if (error == ERESTART)
38394c8fcd8SPeter Wemm 				error = EINTR;
38433841826SPoul-Henning Kamp 			if (rmt != NULL) {
38533841826SPoul-Henning Kamp 				timespecsub(&ts, &ts2);
38633841826SPoul-Henning Kamp 				if (ts.tv_sec < 0)
38733841826SPoul-Henning Kamp 					timespecclear(&ts);
38800af9731SPoul-Henning Kamp 				*rmt = ts;
38900af9731SPoul-Henning Kamp 			}
39033841826SPoul-Henning Kamp 			return (error);
39133841826SPoul-Henning Kamp 		}
39233841826SPoul-Henning Kamp 		if (timespeccmp(&ts2, &ts, >=))
39333841826SPoul-Henning Kamp 			return (0);
3945704ba6aSPoul-Henning Kamp 		ts3 = ts;
3955704ba6aSPoul-Henning Kamp 		timespecsub(&ts3, &ts2);
3965704ba6aSPoul-Henning Kamp 		TIMESPEC_TO_TIMEVAL(&tv, &ts3);
39733841826SPoul-Henning Kamp 	}
3985b870b7bSPeter Wemm }
39994c8fcd8SPeter Wemm 
4005b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
4015b870b7bSPeter Wemm struct nanosleep_args {
4025b870b7bSPeter Wemm 	struct	timespec *rqtp;
4035b870b7bSPeter Wemm 	struct	timespec *rmtp;
4045b870b7bSPeter Wemm };
4055b870b7bSPeter Wemm #endif
4065b870b7bSPeter Wemm /* ARGSUSED */
4075b870b7bSPeter Wemm int
40891afe087SPoul-Henning Kamp nanosleep(struct thread *td, struct nanosleep_args *uap)
4095b870b7bSPeter Wemm {
4105b870b7bSPeter Wemm 	struct timespec rmt, rqt;
411fb99ab88SMatthew Dillon 	int error;
4125b870b7bSPeter Wemm 
413d1e405c5SAlfred Perlstein 	error = copyin(uap->rqtp, &rqt, sizeof(rqt));
4145b870b7bSPeter Wemm 	if (error)
4155b870b7bSPeter Wemm 		return (error);
416fb99ab88SMatthew Dillon 
41731f3e2adSAlfred Perlstein 	if (uap->rmtp &&
41831f3e2adSAlfred Perlstein 	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
41931f3e2adSAlfred Perlstein 			return (EFAULT);
4207fdf2c85SPaul Saab 	error = kern_nanosleep(td, &rqt, &rmt);
421d1e405c5SAlfred Perlstein 	if (error && uap->rmtp) {
422fb99ab88SMatthew Dillon 		int error2;
423fb99ab88SMatthew Dillon 
424d1e405c5SAlfred Perlstein 		error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
42531f3e2adSAlfred Perlstein 		if (error2)
426fb99ab88SMatthew Dillon 			error = error2;
427708e7684SPeter Wemm 	}
428708e7684SPeter Wemm 	return (error);
42994c8fcd8SPeter Wemm }
43094c8fcd8SPeter Wemm 
4315b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
432df8bae1dSRodney W. Grimes struct gettimeofday_args {
433df8bae1dSRodney W. Grimes 	struct	timeval *tp;
434df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
435df8bae1dSRodney W. Grimes };
436d2d3e875SBruce Evans #endif
437df8bae1dSRodney W. Grimes /* ARGSUSED */
43826f9a767SRodney W. Grimes int
43991afe087SPoul-Henning Kamp gettimeofday(struct thread *td, struct gettimeofday_args *uap)
440df8bae1dSRodney W. Grimes {
441df8bae1dSRodney W. Grimes 	struct timeval atv;
442411c25edSTim J. Robbins 	struct timezone rtz;
443df8bae1dSRodney W. Grimes 	int error = 0;
444df8bae1dSRodney W. Grimes 
445df8bae1dSRodney W. Grimes 	if (uap->tp) {
446df8bae1dSRodney W. Grimes 		microtime(&atv);
44701609114SAlfred Perlstein 		error = copyout(&atv, uap->tp, sizeof (atv));
448df8bae1dSRodney W. Grimes 	}
44921dcdb38SPoul-Henning Kamp 	if (error == 0 && uap->tzp != NULL) {
45091f1c2b3SPoul-Henning Kamp 		rtz.tz_minuteswest = tz_minuteswest;
45191f1c2b3SPoul-Henning Kamp 		rtz.tz_dsttime = tz_dsttime;
452411c25edSTim J. Robbins 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
45321dcdb38SPoul-Henning Kamp 	}
454df8bae1dSRodney W. Grimes 	return (error);
455df8bae1dSRodney W. Grimes }
456df8bae1dSRodney W. Grimes 
457d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
458df8bae1dSRodney W. Grimes struct settimeofday_args {
459df8bae1dSRodney W. Grimes 	struct	timeval *tv;
460df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
461df8bae1dSRodney W. Grimes };
462d2d3e875SBruce Evans #endif
463df8bae1dSRodney W. Grimes /* ARGSUSED */
46426f9a767SRodney W. Grimes int
46591afe087SPoul-Henning Kamp settimeofday(struct thread *td, struct settimeofday_args *uap)
466df8bae1dSRodney W. Grimes {
467b88ec951SJohn Baldwin 	struct timeval atv, *tvp;
468b88ec951SJohn Baldwin 	struct timezone atz, *tzp;
469b88ec951SJohn Baldwin 	int error;
470b88ec951SJohn Baldwin 
471b88ec951SJohn Baldwin 	if (uap->tv) {
472b88ec951SJohn Baldwin 		error = copyin(uap->tv, &atv, sizeof(atv));
473b88ec951SJohn Baldwin 		if (error)
474b88ec951SJohn Baldwin 			return (error);
475b88ec951SJohn Baldwin 		tvp = &atv;
476b88ec951SJohn Baldwin 	} else
477b88ec951SJohn Baldwin 		tvp = NULL;
478b88ec951SJohn Baldwin 	if (uap->tzp) {
479b88ec951SJohn Baldwin 		error = copyin(uap->tzp, &atz, sizeof(atz));
480b88ec951SJohn Baldwin 		if (error)
481b88ec951SJohn Baldwin 			return (error);
482b88ec951SJohn Baldwin 		tzp = &atz;
483b88ec951SJohn Baldwin 	} else
484b88ec951SJohn Baldwin 		tzp = NULL;
485b88ec951SJohn Baldwin 	return (kern_settimeofday(td, tvp, tzp));
486b88ec951SJohn Baldwin }
487b88ec951SJohn Baldwin 
488b88ec951SJohn Baldwin int
489b88ec951SJohn Baldwin kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
490b88ec951SJohn Baldwin {
491b88ec951SJohn Baldwin 	int error;
492fb99ab88SMatthew Dillon 
493acd3428bSRobert Watson 	error = priv_check(td, PRIV_SETTIMEOFDAY);
494b88ec951SJohn Baldwin 	if (error)
4957edfb592SJohn Baldwin 		return (error);
496df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
497b88ec951SJohn Baldwin 	if (tv) {
498b88ec951SJohn Baldwin 		if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
4997edfb592SJohn Baldwin 			return (EINVAL);
500b88ec951SJohn Baldwin 		error = settime(td, tv);
501708e7684SPeter Wemm 	}
502b88ec951SJohn Baldwin 	if (tzp && error == 0) {
503b88ec951SJohn Baldwin 		tz_minuteswest = tzp->tz_minuteswest;
504b88ec951SJohn Baldwin 		tz_dsttime = tzp->tz_dsttime;
505b88ec951SJohn Baldwin 	}
5067edfb592SJohn Baldwin 	return (error);
507b88ec951SJohn Baldwin }
5087edfb592SJohn Baldwin 
509df8bae1dSRodney W. Grimes /*
510873fbcd7SRobert Watson  * Get value of an interval timer.  The process virtual and profiling virtual
511873fbcd7SRobert Watson  * time timers are kept in the p_stats area, since they can be swapped out.
512873fbcd7SRobert Watson  * These are kept internally in the way they are specified externally: in
513873fbcd7SRobert Watson  * time until they expire.
514df8bae1dSRodney W. Grimes  *
515873fbcd7SRobert Watson  * The real time interval timer is kept in the process table slot for the
516873fbcd7SRobert Watson  * process, and its value (it_value) is kept as an absolute time rather than
517873fbcd7SRobert Watson  * as a delta, so that it is easy to keep periodic real-time signals from
518873fbcd7SRobert Watson  * drifting.
519df8bae1dSRodney W. Grimes  *
520df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
521873fbcd7SRobert Watson  * kern_clock.c.  The real time timer is processed by a timeout routine,
522873fbcd7SRobert Watson  * called from the softclock() routine.  Since a callout may be delayed in
523873fbcd7SRobert Watson  * real time due to interrupt processing in the system, it is possible for
524873fbcd7SRobert Watson  * the real time timeout routine (realitexpire, given below), to be delayed
525873fbcd7SRobert Watson  * in real time past when it is supposed to occur.  It does not suffice,
526873fbcd7SRobert Watson  * therefore, to reload the real timer .it_value from the real time timers
527873fbcd7SRobert Watson  * .it_interval.  Rather, we compute the next time in absolute time the timer
528873fbcd7SRobert Watson  * should go off.
529df8bae1dSRodney W. Grimes  */
530d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
531df8bae1dSRodney W. Grimes struct getitimer_args {
532df8bae1dSRodney W. Grimes 	u_int	which;
533df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
534df8bae1dSRodney W. Grimes };
535d2d3e875SBruce Evans #endif
53626f9a767SRodney W. Grimes int
53791afe087SPoul-Henning Kamp getitimer(struct thread *td, struct getitimer_args *uap)
538df8bae1dSRodney W. Grimes {
539df8bae1dSRodney W. Grimes 	struct itimerval aitv;
540c90110d6SJohn Baldwin 	int error;
541df8bae1dSRodney W. Grimes 
542cfa0efe7SMaxim Sobolev 	error = kern_getitimer(td, uap->which, &aitv);
543cfa0efe7SMaxim Sobolev 	if (error != 0)
544cfa0efe7SMaxim Sobolev 		return (error);
545cfa0efe7SMaxim Sobolev 	return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
546cfa0efe7SMaxim Sobolev }
547cfa0efe7SMaxim Sobolev 
548cfa0efe7SMaxim Sobolev int
549cfa0efe7SMaxim Sobolev kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
550cfa0efe7SMaxim Sobolev {
551cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
552cfa0efe7SMaxim Sobolev 	struct timeval ctv;
553cfa0efe7SMaxim Sobolev 
554cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
555df8bae1dSRodney W. Grimes 		return (EINVAL);
556fb99ab88SMatthew Dillon 
557cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
558df8bae1dSRodney W. Grimes 		/*
559ee002b68SBruce Evans 		 * Convert from absolute to relative time in .it_value
560df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
561df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
562df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
563df8bae1dSRodney W. Grimes 		 */
56496d7f8efSTim J. Robbins 		PROC_LOCK(p);
565cfa0efe7SMaxim Sobolev 		*aitv = p->p_realtimer;
56696d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
567cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
568c21410e1SPoul-Henning Kamp 			getmicrouptime(&ctv);
569cfa0efe7SMaxim Sobolev 			if (timevalcmp(&aitv->it_value, &ctv, <))
570cfa0efe7SMaxim Sobolev 				timevalclear(&aitv->it_value);
571df8bae1dSRodney W. Grimes 			else
572cfa0efe7SMaxim Sobolev 				timevalsub(&aitv->it_value, &ctv);
573227ee8a1SPoul-Henning Kamp 		}
574fb99ab88SMatthew Dillon 	} else {
575982d11f8SJeff Roberson 		PROC_SLOCK(p);
576cfa0efe7SMaxim Sobolev 		*aitv = p->p_stats->p_timer[which];
577982d11f8SJeff Roberson 		PROC_SUNLOCK(p);
578fb99ab88SMatthew Dillon 	}
579cfa0efe7SMaxim Sobolev 	return (0);
580df8bae1dSRodney W. Grimes }
581df8bae1dSRodney W. Grimes 
582d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
583df8bae1dSRodney W. Grimes struct setitimer_args {
584df8bae1dSRodney W. Grimes 	u_int	which;
585df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
586df8bae1dSRodney W. Grimes };
587d2d3e875SBruce Evans #endif
58826f9a767SRodney W. Grimes int
58991afe087SPoul-Henning Kamp setitimer(struct thread *td, struct setitimer_args *uap)
590df8bae1dSRodney W. Grimes {
591cfa0efe7SMaxim Sobolev 	struct itimerval aitv, oitv;
592c90110d6SJohn Baldwin 	int error;
59396d7f8efSTim J. Robbins 
59496d7f8efSTim J. Robbins 	if (uap->itv == NULL) {
59596d7f8efSTim J. Robbins 		uap->itv = uap->oitv;
59696d7f8efSTim J. Robbins 		return (getitimer(td, (struct getitimer_args *)uap));
59796d7f8efSTim J. Robbins 	}
598df8bae1dSRodney W. Grimes 
59996d7f8efSTim J. Robbins 	if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
600df8bae1dSRodney W. Grimes 		return (error);
601cfa0efe7SMaxim Sobolev 	error = kern_setitimer(td, uap->which, &aitv, &oitv);
602cfa0efe7SMaxim Sobolev 	if (error != 0 || uap->oitv == NULL)
603cfa0efe7SMaxim Sobolev 		return (error);
604cfa0efe7SMaxim Sobolev 	return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
605cfa0efe7SMaxim Sobolev }
606cfa0efe7SMaxim Sobolev 
607cfa0efe7SMaxim Sobolev int
608c90110d6SJohn Baldwin kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
609c90110d6SJohn Baldwin     struct itimerval *oitv)
610cfa0efe7SMaxim Sobolev {
611cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
612cfa0efe7SMaxim Sobolev 	struct timeval ctv;
613cfa0efe7SMaxim Sobolev 
6145e85ac17SJohn Baldwin 	if (aitv == NULL)
6155e85ac17SJohn Baldwin 		return (kern_getitimer(td, which, oitv));
6165e85ac17SJohn Baldwin 
617cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
61896d7f8efSTim J. Robbins 		return (EINVAL);
619cfa0efe7SMaxim Sobolev 	if (itimerfix(&aitv->it_value))
620cfa0efe7SMaxim Sobolev 		return (EINVAL);
621cfa0efe7SMaxim Sobolev 	if (!timevalisset(&aitv->it_value))
622cfa0efe7SMaxim Sobolev 		timevalclear(&aitv->it_interval);
623cfa0efe7SMaxim Sobolev 	else if (itimerfix(&aitv->it_interval))
62496d7f8efSTim J. Robbins 		return (EINVAL);
62596d7f8efSTim J. Robbins 
626cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
62796d7f8efSTim J. Robbins 		PROC_LOCK(p);
6284cf41af3SPoul-Henning Kamp 		if (timevalisset(&p->p_realtimer.it_value))
6294f559836SJake Burkholder 			callout_stop(&p->p_itcallout);
63025b4d3a8SJohn Baldwin 		getmicrouptime(&ctv);
631cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
632cfa0efe7SMaxim Sobolev 			callout_reset(&p->p_itcallout, tvtohz(&aitv->it_value),
6334f559836SJake Burkholder 			    realitexpire, p);
634cfa0efe7SMaxim Sobolev 			timevaladd(&aitv->it_value, &ctv);
63525b4d3a8SJohn Baldwin 		}
636cfa0efe7SMaxim Sobolev 		*oitv = p->p_realtimer;
637cfa0efe7SMaxim Sobolev 		p->p_realtimer = *aitv;
63896d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
639cfa0efe7SMaxim Sobolev 		if (timevalisset(&oitv->it_value)) {
640cfa0efe7SMaxim Sobolev 			if (timevalcmp(&oitv->it_value, &ctv, <))
641cfa0efe7SMaxim Sobolev 				timevalclear(&oitv->it_value);
64296d7f8efSTim J. Robbins 			else
643cfa0efe7SMaxim Sobolev 				timevalsub(&oitv->it_value, &ctv);
644fb99ab88SMatthew Dillon 		}
64596d7f8efSTim J. Robbins 	} else {
646982d11f8SJeff Roberson 		PROC_SLOCK(p);
647cfa0efe7SMaxim Sobolev 		*oitv = p->p_stats->p_timer[which];
648cfa0efe7SMaxim Sobolev 		p->p_stats->p_timer[which] = *aitv;
649982d11f8SJeff Roberson 		PROC_SUNLOCK(p);
65096d7f8efSTim J. Robbins 	}
65196d7f8efSTim J. Robbins 	return (0);
652df8bae1dSRodney W. Grimes }
653df8bae1dSRodney W. Grimes 
654df8bae1dSRodney W. Grimes /*
655df8bae1dSRodney W. Grimes  * Real interval timer expired:
656df8bae1dSRodney W. Grimes  * send process whose timer expired an alarm signal.
657df8bae1dSRodney W. Grimes  * If time is not set up to reload, then just return.
658df8bae1dSRodney W. Grimes  * Else compute next time timer should go off which is > current time.
659df8bae1dSRodney W. Grimes  * This is where delay in processing this timeout causes multiple
660df8bae1dSRodney W. Grimes  * SIGALRM calls to be compressed into one.
661c8b47828SBruce Evans  * tvtohz() always adds 1 to allow for the time until the next clock
6629207f00aSBruce Evans  * interrupt being strictly less than 1 clock tick, but we don't want
6639207f00aSBruce Evans  * that here since we want to appear to be in sync with the clock
6649207f00aSBruce Evans  * interrupt even when we're delayed.
665df8bae1dSRodney W. Grimes  */
666df8bae1dSRodney W. Grimes void
66791afe087SPoul-Henning Kamp realitexpire(void *arg)
668df8bae1dSRodney W. Grimes {
66991afe087SPoul-Henning Kamp 	struct proc *p;
670bfe6c9faSPoul-Henning Kamp 	struct timeval ctv, ntv;
671df8bae1dSRodney W. Grimes 
672df8bae1dSRodney W. Grimes 	p = (struct proc *)arg;
67337824023SJohn Baldwin 	PROC_LOCK(p);
674df8bae1dSRodney W. Grimes 	psignal(p, SIGALRM);
6754cf41af3SPoul-Henning Kamp 	if (!timevalisset(&p->p_realtimer.it_interval)) {
6764cf41af3SPoul-Henning Kamp 		timevalclear(&p->p_realtimer.it_value);
6775499ea01SJohn Baldwin 		if (p->p_flag & P_WEXIT)
6785499ea01SJohn Baldwin 			wakeup(&p->p_itcallout);
67937824023SJohn Baldwin 		PROC_UNLOCK(p);
680df8bae1dSRodney W. Grimes 		return;
681df8bae1dSRodney W. Grimes 	}
682df8bae1dSRodney W. Grimes 	for (;;) {
683df8bae1dSRodney W. Grimes 		timevaladd(&p->p_realtimer.it_value,
684df8bae1dSRodney W. Grimes 		    &p->p_realtimer.it_interval);
685c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
6864cf41af3SPoul-Henning Kamp 		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
687bfe6c9faSPoul-Henning Kamp 			ntv = p->p_realtimer.it_value;
688bfe6c9faSPoul-Henning Kamp 			timevalsub(&ntv, &ctv);
6894f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
6904f559836SJake Burkholder 			    realitexpire, p);
69137824023SJohn Baldwin 			PROC_UNLOCK(p);
692df8bae1dSRodney W. Grimes 			return;
693df8bae1dSRodney W. Grimes 		}
694df8bae1dSRodney W. Grimes 	}
69537824023SJohn Baldwin 	/*NOTREACHED*/
696df8bae1dSRodney W. Grimes }
697df8bae1dSRodney W. Grimes 
698df8bae1dSRodney W. Grimes /*
699df8bae1dSRodney W. Grimes  * Check that a proposed value to load into the .it_value or
700df8bae1dSRodney W. Grimes  * .it_interval part of an interval timer is acceptable, and
701df8bae1dSRodney W. Grimes  * fix it to have at least minimal value (i.e. if it is less
702df8bae1dSRodney W. Grimes  * than the resolution of the clock, round it up.)
703df8bae1dSRodney W. Grimes  */
70426f9a767SRodney W. Grimes int
70591afe087SPoul-Henning Kamp itimerfix(struct timeval *tv)
706df8bae1dSRodney W. Grimes {
707df8bae1dSRodney W. Grimes 
70886857b36SDavid Xu 	if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
709df8bae1dSRodney W. Grimes 		return (EINVAL);
710df8bae1dSRodney W. Grimes 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
711df8bae1dSRodney W. Grimes 		tv->tv_usec = tick;
712df8bae1dSRodney W. Grimes 	return (0);
713df8bae1dSRodney W. Grimes }
714df8bae1dSRodney W. Grimes 
715df8bae1dSRodney W. Grimes /*
716df8bae1dSRodney W. Grimes  * Decrement an interval timer by a specified number
717df8bae1dSRodney W. Grimes  * of microseconds, which must be less than a second,
718df8bae1dSRodney W. Grimes  * i.e. < 1000000.  If the timer expires, then reload
719df8bae1dSRodney W. Grimes  * it.  In this case, carry over (usec - old value) to
720df8bae1dSRodney W. Grimes  * reduce the value reloaded into the timer so that
721df8bae1dSRodney W. Grimes  * the timer does not drift.  This routine assumes
722df8bae1dSRodney W. Grimes  * that it is called in a context where the timers
723df8bae1dSRodney W. Grimes  * on which it is operating cannot change in value.
724df8bae1dSRodney W. Grimes  */
72526f9a767SRodney W. Grimes int
72691afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec)
727df8bae1dSRodney W. Grimes {
728df8bae1dSRodney W. Grimes 
729df8bae1dSRodney W. Grimes 	if (itp->it_value.tv_usec < usec) {
730df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_sec == 0) {
731df8bae1dSRodney W. Grimes 			/* expired, and already in next interval */
732df8bae1dSRodney W. Grimes 			usec -= itp->it_value.tv_usec;
733df8bae1dSRodney W. Grimes 			goto expire;
734df8bae1dSRodney W. Grimes 		}
735df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec += 1000000;
736df8bae1dSRodney W. Grimes 		itp->it_value.tv_sec--;
737df8bae1dSRodney W. Grimes 	}
738df8bae1dSRodney W. Grimes 	itp->it_value.tv_usec -= usec;
739df8bae1dSRodney W. Grimes 	usec = 0;
7404cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_value))
741df8bae1dSRodney W. Grimes 		return (1);
742df8bae1dSRodney W. Grimes 	/* expired, exactly at end of interval */
743df8bae1dSRodney W. Grimes expire:
7444cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_interval)) {
745df8bae1dSRodney W. Grimes 		itp->it_value = itp->it_interval;
746df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec -= usec;
747df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_usec < 0) {
748df8bae1dSRodney W. Grimes 			itp->it_value.tv_usec += 1000000;
749df8bae1dSRodney W. Grimes 			itp->it_value.tv_sec--;
750df8bae1dSRodney W. Grimes 		}
751df8bae1dSRodney W. Grimes 	} else
752df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
753df8bae1dSRodney W. Grimes 	return (0);
754df8bae1dSRodney W. Grimes }
755df8bae1dSRodney W. Grimes 
756df8bae1dSRodney W. Grimes /*
757df8bae1dSRodney W. Grimes  * Add and subtract routines for timevals.
758df8bae1dSRodney W. Grimes  * N.B.: subtract routine doesn't deal with
759df8bae1dSRodney W. Grimes  * results which are before the beginning,
760df8bae1dSRodney W. Grimes  * it just gets very confused in this case.
761df8bae1dSRodney W. Grimes  * Caveat emptor.
762df8bae1dSRodney W. Grimes  */
76326f9a767SRodney W. Grimes void
7646ff7636eSAlfred Perlstein timevaladd(struct timeval *t1, const struct timeval *t2)
765df8bae1dSRodney W. Grimes {
766df8bae1dSRodney W. Grimes 
767df8bae1dSRodney W. Grimes 	t1->tv_sec += t2->tv_sec;
768df8bae1dSRodney W. Grimes 	t1->tv_usec += t2->tv_usec;
769df8bae1dSRodney W. Grimes 	timevalfix(t1);
770df8bae1dSRodney W. Grimes }
771df8bae1dSRodney W. Grimes 
77226f9a767SRodney W. Grimes void
7736ff7636eSAlfred Perlstein timevalsub(struct timeval *t1, const struct timeval *t2)
774df8bae1dSRodney W. Grimes {
775df8bae1dSRodney W. Grimes 
776df8bae1dSRodney W. Grimes 	t1->tv_sec -= t2->tv_sec;
777df8bae1dSRodney W. Grimes 	t1->tv_usec -= t2->tv_usec;
778df8bae1dSRodney W. Grimes 	timevalfix(t1);
779df8bae1dSRodney W. Grimes }
780df8bae1dSRodney W. Grimes 
78187b6de2bSPoul-Henning Kamp static void
78291afe087SPoul-Henning Kamp timevalfix(struct timeval *t1)
783df8bae1dSRodney W. Grimes {
784df8bae1dSRodney W. Grimes 
785df8bae1dSRodney W. Grimes 	if (t1->tv_usec < 0) {
786df8bae1dSRodney W. Grimes 		t1->tv_sec--;
787df8bae1dSRodney W. Grimes 		t1->tv_usec += 1000000;
788df8bae1dSRodney W. Grimes 	}
789df8bae1dSRodney W. Grimes 	if (t1->tv_usec >= 1000000) {
790df8bae1dSRodney W. Grimes 		t1->tv_sec++;
791df8bae1dSRodney W. Grimes 		t1->tv_usec -= 1000000;
792df8bae1dSRodney W. Grimes 	}
793df8bae1dSRodney W. Grimes }
79491974ce1SSam Leffler 
79591974ce1SSam Leffler /*
796addea9d4SSam Leffler  * ratecheck(): simple time-based rate-limit checking.
79791974ce1SSam Leffler  */
79891974ce1SSam Leffler int
79991974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
80091974ce1SSam Leffler {
80191974ce1SSam Leffler 	struct timeval tv, delta;
80291974ce1SSam Leffler 	int rv = 0;
80391974ce1SSam Leffler 
804addea9d4SSam Leffler 	getmicrouptime(&tv);		/* NB: 10ms precision */
805addea9d4SSam Leffler 	delta = tv;
806addea9d4SSam Leffler 	timevalsub(&delta, lasttime);
80791974ce1SSam Leffler 
80891974ce1SSam Leffler 	/*
80991974ce1SSam Leffler 	 * check for 0,0 is so that the message will be seen at least once,
81091974ce1SSam Leffler 	 * even if interval is huge.
81191974ce1SSam Leffler 	 */
81291974ce1SSam Leffler 	if (timevalcmp(&delta, mininterval, >=) ||
81391974ce1SSam Leffler 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
81491974ce1SSam Leffler 		*lasttime = tv;
81591974ce1SSam Leffler 		rv = 1;
81691974ce1SSam Leffler 	}
81791974ce1SSam Leffler 
81891974ce1SSam Leffler 	return (rv);
81991974ce1SSam Leffler }
82091974ce1SSam Leffler 
82191974ce1SSam Leffler /*
82291974ce1SSam Leffler  * ppsratecheck(): packets (or events) per second limitation.
823addea9d4SSam Leffler  *
824addea9d4SSam Leffler  * Return 0 if the limit is to be enforced (e.g. the caller
825addea9d4SSam Leffler  * should drop a packet because of the rate limitation).
826addea9d4SSam Leffler  *
827893bec80SSam Leffler  * maxpps of 0 always causes zero to be returned.  maxpps of -1
828893bec80SSam Leffler  * always causes 1 to be returned; this effectively defeats rate
829893bec80SSam Leffler  * limiting.
830893bec80SSam Leffler  *
831addea9d4SSam Leffler  * Note that we maintain the struct timeval for compatibility
832addea9d4SSam Leffler  * with other bsd systems.  We reuse the storage and just monitor
833addea9d4SSam Leffler  * clock ticks for minimal overhead.
83491974ce1SSam Leffler  */
83591974ce1SSam Leffler int
83691974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
83791974ce1SSam Leffler {
838addea9d4SSam Leffler 	int now;
83991974ce1SSam Leffler 
84091974ce1SSam Leffler 	/*
841addea9d4SSam Leffler 	 * Reset the last time and counter if this is the first call
842addea9d4SSam Leffler 	 * or more than a second has passed since the last update of
843addea9d4SSam Leffler 	 * lasttime.
84491974ce1SSam Leffler 	 */
845addea9d4SSam Leffler 	now = ticks;
846addea9d4SSam Leffler 	if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
847addea9d4SSam Leffler 		lasttime->tv_sec = now;
848addea9d4SSam Leffler 		*curpps = 1;
849893bec80SSam Leffler 		return (maxpps != 0);
850addea9d4SSam Leffler 	} else {
851addea9d4SSam Leffler 		(*curpps)++;		/* NB: ignore potential overflow */
852addea9d4SSam Leffler 		return (maxpps < 0 || *curpps < maxpps);
853addea9d4SSam Leffler 	}
85491974ce1SSam Leffler }
85586857b36SDavid Xu 
85686857b36SDavid Xu static void
85786857b36SDavid Xu itimer_start(void)
85886857b36SDavid Xu {
85986857b36SDavid Xu 	struct kclock rt_clock = {
86086857b36SDavid Xu 		.timer_create  = realtimer_create,
86186857b36SDavid Xu 		.timer_delete  = realtimer_delete,
86286857b36SDavid Xu 		.timer_settime = realtimer_settime,
86386857b36SDavid Xu 		.timer_gettime = realtimer_gettime,
864843b99c6SDavid Xu 		.event_hook    = NULL
86586857b36SDavid Xu 	};
86686857b36SDavid Xu 
86786857b36SDavid Xu 	itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
86886857b36SDavid Xu 		NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
86986857b36SDavid Xu 	register_posix_clock(CLOCK_REALTIME,  &rt_clock);
87086857b36SDavid Xu 	register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
87177e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
87277e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
87377e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
874993182e5SAlexander Leidinger 	EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit,
875d26b1a1fSDavid Xu 		(void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY);
876993182e5SAlexander Leidinger 	EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec,
877d26b1a1fSDavid Xu 		(void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY);
87886857b36SDavid Xu }
87986857b36SDavid Xu 
88086857b36SDavid Xu int
88186857b36SDavid Xu register_posix_clock(int clockid, struct kclock *clk)
88286857b36SDavid Xu {
88386857b36SDavid Xu 	if ((unsigned)clockid >= MAX_CLOCKS) {
88486857b36SDavid Xu 		printf("%s: invalid clockid\n", __func__);
88586857b36SDavid Xu 		return (0);
88686857b36SDavid Xu 	}
88786857b36SDavid Xu 	posix_clocks[clockid] = *clk;
88886857b36SDavid Xu 	return (1);
88986857b36SDavid Xu }
89086857b36SDavid Xu 
89186857b36SDavid Xu static int
89286857b36SDavid Xu itimer_init(void *mem, int size, int flags)
89386857b36SDavid Xu {
89486857b36SDavid Xu 	struct itimer *it;
89586857b36SDavid Xu 
89686857b36SDavid Xu 	it = (struct itimer *)mem;
89786857b36SDavid Xu 	mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
89886857b36SDavid Xu 	return (0);
89986857b36SDavid Xu }
90086857b36SDavid Xu 
90186857b36SDavid Xu static void
90286857b36SDavid Xu itimer_fini(void *mem, int size)
90386857b36SDavid Xu {
90486857b36SDavid Xu 	struct itimer *it;
90586857b36SDavid Xu 
90686857b36SDavid Xu 	it = (struct itimer *)mem;
90786857b36SDavid Xu 	mtx_destroy(&it->it_mtx);
90886857b36SDavid Xu }
90986857b36SDavid Xu 
91086857b36SDavid Xu static void
91186857b36SDavid Xu itimer_enter(struct itimer *it)
91286857b36SDavid Xu {
91386857b36SDavid Xu 
91486857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
91586857b36SDavid Xu 	it->it_usecount++;
91686857b36SDavid Xu }
91786857b36SDavid Xu 
91886857b36SDavid Xu static void
91986857b36SDavid Xu itimer_leave(struct itimer *it)
92086857b36SDavid Xu {
92186857b36SDavid Xu 
92286857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
92386857b36SDavid Xu 	KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
92486857b36SDavid Xu 
92586857b36SDavid Xu 	if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
92686857b36SDavid Xu 		wakeup(it);
92786857b36SDavid Xu }
92886857b36SDavid Xu 
92986857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
93061d3a4efSDavid Xu struct ktimer_create_args {
93186857b36SDavid Xu 	clockid_t clock_id;
93286857b36SDavid Xu 	struct sigevent * evp;
93361d3a4efSDavid Xu 	int * timerid;
93486857b36SDavid Xu };
93586857b36SDavid Xu #endif
93686857b36SDavid Xu int
93761d3a4efSDavid Xu ktimer_create(struct thread *td, struct ktimer_create_args *uap)
93886857b36SDavid Xu {
93986857b36SDavid Xu 	struct sigevent *evp1, ev;
94061d3a4efSDavid Xu 	int id;
94186857b36SDavid Xu 	int error;
94286857b36SDavid Xu 
94386857b36SDavid Xu 	if (uap->evp != NULL) {
94486857b36SDavid Xu 		error = copyin(uap->evp, &ev, sizeof(ev));
94586857b36SDavid Xu 		if (error != 0)
94686857b36SDavid Xu 			return (error);
94786857b36SDavid Xu 		evp1 = &ev;
94886857b36SDavid Xu 	} else
94986857b36SDavid Xu 		evp1 = NULL;
95086857b36SDavid Xu 
95186857b36SDavid Xu 	error = kern_timer_create(td, uap->clock_id, evp1, &id, -1);
95286857b36SDavid Xu 
95386857b36SDavid Xu 	if (error == 0) {
95461d3a4efSDavid Xu 		error = copyout(&id, uap->timerid, sizeof(int));
95586857b36SDavid Xu 		if (error != 0)
95686857b36SDavid Xu 			kern_timer_delete(td, id);
95786857b36SDavid Xu 	}
95886857b36SDavid Xu 	return (error);
95986857b36SDavid Xu }
96086857b36SDavid Xu 
96186857b36SDavid Xu static int
96286857b36SDavid Xu kern_timer_create(struct thread *td, clockid_t clock_id,
96361d3a4efSDavid Xu 	struct sigevent *evp, int *timerid, int preset_id)
96486857b36SDavid Xu {
96586857b36SDavid Xu 	struct proc *p = td->td_proc;
96686857b36SDavid Xu 	struct itimer *it;
96786857b36SDavid Xu 	int id;
96886857b36SDavid Xu 	int error;
96986857b36SDavid Xu 
97086857b36SDavid Xu 	if (clock_id < 0 || clock_id >= MAX_CLOCKS)
97186857b36SDavid Xu 		return (EINVAL);
97286857b36SDavid Xu 
97386857b36SDavid Xu 	if (posix_clocks[clock_id].timer_create == NULL)
97486857b36SDavid Xu 		return (EINVAL);
97586857b36SDavid Xu 
97686857b36SDavid Xu 	if (evp != NULL) {
97786857b36SDavid Xu 		if (evp->sigev_notify != SIGEV_NONE &&
97856c06c4bSDavid Xu 		    evp->sigev_notify != SIGEV_SIGNAL &&
97956c06c4bSDavid Xu 		    evp->sigev_notify != SIGEV_THREAD_ID)
98086857b36SDavid Xu 			return (EINVAL);
98156c06c4bSDavid Xu 		if ((evp->sigev_notify == SIGEV_SIGNAL ||
98256c06c4bSDavid Xu 		     evp->sigev_notify == SIGEV_THREAD_ID) &&
98386857b36SDavid Xu 			!_SIG_VALID(evp->sigev_signo))
98486857b36SDavid Xu 			return (EINVAL);
98586857b36SDavid Xu 	}
98686857b36SDavid Xu 
98760354683SDavid Xu 	if (p->p_itimers == NULL)
98886857b36SDavid Xu 		itimers_alloc(p);
98986857b36SDavid Xu 
99086857b36SDavid Xu 	it = uma_zalloc(itimer_zone, M_WAITOK);
99186857b36SDavid Xu 	it->it_flags = 0;
99286857b36SDavid Xu 	it->it_usecount = 0;
99386857b36SDavid Xu 	it->it_active = 0;
99456c06c4bSDavid Xu 	timespecclear(&it->it_time.it_value);
99556c06c4bSDavid Xu 	timespecclear(&it->it_time.it_interval);
99686857b36SDavid Xu 	it->it_overrun = 0;
99786857b36SDavid Xu 	it->it_overrun_last = 0;
99886857b36SDavid Xu 	it->it_clockid = clock_id;
99986857b36SDavid Xu 	it->it_timerid = -1;
100086857b36SDavid Xu 	it->it_proc = p;
100186857b36SDavid Xu 	ksiginfo_init(&it->it_ksi);
100286857b36SDavid Xu 	it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
100386857b36SDavid Xu 	error = CLOCK_CALL(clock_id, timer_create, (it));
100486857b36SDavid Xu 	if (error != 0)
100586857b36SDavid Xu 		goto out;
100686857b36SDavid Xu 
100786857b36SDavid Xu 	PROC_LOCK(p);
100886857b36SDavid Xu 	if (preset_id != -1) {
100986857b36SDavid Xu 		KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
101086857b36SDavid Xu 		id = preset_id;
101160354683SDavid Xu 		if (p->p_itimers->its_timers[id] != NULL) {
101286857b36SDavid Xu 			PROC_UNLOCK(p);
101386857b36SDavid Xu 			error = 0;
101486857b36SDavid Xu 			goto out;
101586857b36SDavid Xu 		}
101686857b36SDavid Xu 	} else {
101786857b36SDavid Xu 		/*
101886857b36SDavid Xu 		 * Find a free timer slot, skipping those reserved
101986857b36SDavid Xu 		 * for setitimer().
102086857b36SDavid Xu 		 */
102186857b36SDavid Xu 		for (id = 3; id < TIMER_MAX; id++)
102260354683SDavid Xu 			if (p->p_itimers->its_timers[id] == NULL)
102386857b36SDavid Xu 				break;
102486857b36SDavid Xu 		if (id == TIMER_MAX) {
102586857b36SDavid Xu 			PROC_UNLOCK(p);
102686857b36SDavid Xu 			error = EAGAIN;
102786857b36SDavid Xu 			goto out;
102886857b36SDavid Xu 		}
102986857b36SDavid Xu 	}
103086857b36SDavid Xu 	it->it_timerid = id;
103160354683SDavid Xu 	p->p_itimers->its_timers[id] = it;
103286857b36SDavid Xu 	if (evp != NULL)
103386857b36SDavid Xu 		it->it_sigev = *evp;
103486857b36SDavid Xu 	else {
103586857b36SDavid Xu 		it->it_sigev.sigev_notify = SIGEV_SIGNAL;
103686857b36SDavid Xu 		switch (clock_id) {
103786857b36SDavid Xu 		default:
103886857b36SDavid Xu 		case CLOCK_REALTIME:
103986857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGALRM;
104086857b36SDavid Xu 			break;
104186857b36SDavid Xu 		case CLOCK_VIRTUAL:
104286857b36SDavid Xu  			it->it_sigev.sigev_signo = SIGVTALRM;
104386857b36SDavid Xu 			break;
104486857b36SDavid Xu 		case CLOCK_PROF:
104586857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGPROF;
104686857b36SDavid Xu 			break;
104786857b36SDavid Xu 		}
10488f0371f1SDavid Xu 		it->it_sigev.sigev_value.sival_int = id;
104986857b36SDavid Xu 	}
105086857b36SDavid Xu 
105156c06c4bSDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
105256c06c4bSDavid Xu 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
105386857b36SDavid Xu 		it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
105486857b36SDavid Xu 		it->it_ksi.ksi_code = SI_TIMER;
105586857b36SDavid Xu 		it->it_ksi.ksi_value = it->it_sigev.sigev_value;
105686857b36SDavid Xu 		it->it_ksi.ksi_timerid = id;
105786857b36SDavid Xu 	}
105886857b36SDavid Xu 	PROC_UNLOCK(p);
105986857b36SDavid Xu 	*timerid = id;
106086857b36SDavid Xu 	return (0);
106186857b36SDavid Xu 
106286857b36SDavid Xu out:
106386857b36SDavid Xu 	ITIMER_LOCK(it);
106486857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
106586857b36SDavid Xu 	ITIMER_UNLOCK(it);
106686857b36SDavid Xu 	uma_zfree(itimer_zone, it);
106786857b36SDavid Xu 	return (error);
106886857b36SDavid Xu }
106986857b36SDavid Xu 
107086857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
107161d3a4efSDavid Xu struct ktimer_delete_args {
107261d3a4efSDavid Xu 	int timerid;
107386857b36SDavid Xu };
107486857b36SDavid Xu #endif
107586857b36SDavid Xu int
107661d3a4efSDavid Xu ktimer_delete(struct thread *td, struct ktimer_delete_args *uap)
107786857b36SDavid Xu {
107886857b36SDavid Xu 	return (kern_timer_delete(td, uap->timerid));
107986857b36SDavid Xu }
108086857b36SDavid Xu 
108186857b36SDavid Xu static struct itimer *
1082843b99c6SDavid Xu itimer_find(struct proc *p, int timerid)
108386857b36SDavid Xu {
108486857b36SDavid Xu 	struct itimer *it;
108586857b36SDavid Xu 
108686857b36SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
108760354683SDavid Xu 	if ((p->p_itimers == NULL) || (timerid >= TIMER_MAX) ||
108860354683SDavid Xu 	    (it = p->p_itimers->its_timers[timerid]) == NULL) {
108986857b36SDavid Xu 		return (NULL);
109086857b36SDavid Xu 	}
109186857b36SDavid Xu 	ITIMER_LOCK(it);
1092843b99c6SDavid Xu 	if ((it->it_flags & ITF_DELETING) != 0) {
109386857b36SDavid Xu 		ITIMER_UNLOCK(it);
109486857b36SDavid Xu 		it = NULL;
109586857b36SDavid Xu 	}
109686857b36SDavid Xu 	return (it);
109786857b36SDavid Xu }
109886857b36SDavid Xu 
109986857b36SDavid Xu static int
110061d3a4efSDavid Xu kern_timer_delete(struct thread *td, int timerid)
110186857b36SDavid Xu {
110286857b36SDavid Xu 	struct proc *p = td->td_proc;
110386857b36SDavid Xu 	struct itimer *it;
110486857b36SDavid Xu 
110586857b36SDavid Xu 	PROC_LOCK(p);
1106843b99c6SDavid Xu 	it = itimer_find(p, timerid);
110786857b36SDavid Xu 	if (it == NULL) {
110886857b36SDavid Xu 		PROC_UNLOCK(p);
110986857b36SDavid Xu 		return (EINVAL);
111086857b36SDavid Xu 	}
111186857b36SDavid Xu 	PROC_UNLOCK(p);
111286857b36SDavid Xu 
111386857b36SDavid Xu 	it->it_flags |= ITF_DELETING;
111486857b36SDavid Xu 	while (it->it_usecount > 0) {
111586857b36SDavid Xu 		it->it_flags |= ITF_WANTED;
111686857b36SDavid Xu 		msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
111786857b36SDavid Xu 	}
111886857b36SDavid Xu 	it->it_flags &= ~ITF_WANTED;
111986857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
112086857b36SDavid Xu 	ITIMER_UNLOCK(it);
112186857b36SDavid Xu 
112286857b36SDavid Xu 	PROC_LOCK(p);
112386857b36SDavid Xu 	if (KSI_ONQ(&it->it_ksi))
112486857b36SDavid Xu 		sigqueue_take(&it->it_ksi);
112560354683SDavid Xu 	p->p_itimers->its_timers[timerid] = NULL;
112686857b36SDavid Xu 	PROC_UNLOCK(p);
112786857b36SDavid Xu 	uma_zfree(itimer_zone, it);
112886857b36SDavid Xu 	return (0);
112986857b36SDavid Xu }
113086857b36SDavid Xu 
113186857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
113261d3a4efSDavid Xu struct ktimer_settime_args {
113361d3a4efSDavid Xu 	int timerid;
113486857b36SDavid Xu 	int flags;
113586857b36SDavid Xu 	const struct itimerspec * value;
113686857b36SDavid Xu 	struct itimerspec * ovalue;
113786857b36SDavid Xu };
113886857b36SDavid Xu #endif
113986857b36SDavid Xu int
114061d3a4efSDavid Xu ktimer_settime(struct thread *td, struct ktimer_settime_args *uap)
114186857b36SDavid Xu {
114286857b36SDavid Xu 	struct proc *p = td->td_proc;
114386857b36SDavid Xu 	struct itimer *it;
114486857b36SDavid Xu 	struct itimerspec val, oval, *ovalp;
114586857b36SDavid Xu 	int error;
114686857b36SDavid Xu 
114786857b36SDavid Xu 	error = copyin(uap->value, &val, sizeof(val));
114886857b36SDavid Xu 	if (error != 0)
114986857b36SDavid Xu 		return (error);
115086857b36SDavid Xu 
115186857b36SDavid Xu 	if (uap->ovalue != NULL)
115286857b36SDavid Xu 		ovalp = &oval;
115386857b36SDavid Xu 	else
115486857b36SDavid Xu 		ovalp = NULL;
115586857b36SDavid Xu 
115686857b36SDavid Xu 	PROC_LOCK(p);
115786857b36SDavid Xu 	if (uap->timerid < 3 ||
1158843b99c6SDavid Xu 	    (it = itimer_find(p, uap->timerid)) == NULL) {
115986857b36SDavid Xu 		PROC_UNLOCK(p);
116086857b36SDavid Xu 		error = EINVAL;
116186857b36SDavid Xu 	} else {
116286857b36SDavid Xu 		PROC_UNLOCK(p);
116386857b36SDavid Xu 		itimer_enter(it);
116486857b36SDavid Xu 		error = CLOCK_CALL(it->it_clockid, timer_settime,
116586857b36SDavid Xu 				(it, uap->flags, &val, ovalp));
116686857b36SDavid Xu 		itimer_leave(it);
116786857b36SDavid Xu 		ITIMER_UNLOCK(it);
116886857b36SDavid Xu 	}
116986857b36SDavid Xu 	if (error == 0 && uap->ovalue != NULL)
117086857b36SDavid Xu 		error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
117186857b36SDavid Xu 	return (error);
117286857b36SDavid Xu }
117386857b36SDavid Xu 
117486857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
117561d3a4efSDavid Xu struct ktimer_gettime_args {
117661d3a4efSDavid Xu 	int timerid;
117786857b36SDavid Xu 	struct itimerspec * value;
117886857b36SDavid Xu };
117986857b36SDavid Xu #endif
118086857b36SDavid Xu int
118161d3a4efSDavid Xu ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap)
118286857b36SDavid Xu {
118386857b36SDavid Xu 	struct proc *p = td->td_proc;
118486857b36SDavid Xu 	struct itimer *it;
118586857b36SDavid Xu 	struct itimerspec val;
118686857b36SDavid Xu 	int error;
118786857b36SDavid Xu 
118886857b36SDavid Xu 	PROC_LOCK(p);
118986857b36SDavid Xu 	if (uap->timerid < 3 ||
1190843b99c6SDavid Xu 	   (it = itimer_find(p, uap->timerid)) == NULL) {
119186857b36SDavid Xu 		PROC_UNLOCK(p);
119286857b36SDavid Xu 		error = EINVAL;
119386857b36SDavid Xu 	} else {
119486857b36SDavid Xu 		PROC_UNLOCK(p);
119586857b36SDavid Xu 		itimer_enter(it);
119686857b36SDavid Xu 		error = CLOCK_CALL(it->it_clockid, timer_gettime,
119786857b36SDavid Xu 				(it, &val));
119886857b36SDavid Xu 		itimer_leave(it);
119986857b36SDavid Xu 		ITIMER_UNLOCK(it);
120086857b36SDavid Xu 	}
120186857b36SDavid Xu 	if (error == 0)
120286857b36SDavid Xu 		error = copyout(&val, uap->value, sizeof(val));
120386857b36SDavid Xu 	return (error);
120486857b36SDavid Xu }
120586857b36SDavid Xu 
120686857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
120786857b36SDavid Xu struct timer_getoverrun_args {
120861d3a4efSDavid Xu 	int timerid;
120986857b36SDavid Xu };
121086857b36SDavid Xu #endif
121186857b36SDavid Xu int
121261d3a4efSDavid Xu ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap)
121386857b36SDavid Xu {
121486857b36SDavid Xu 	struct proc *p = td->td_proc;
121586857b36SDavid Xu 	struct itimer *it;
121686857b36SDavid Xu 	int error ;
121786857b36SDavid Xu 
121886857b36SDavid Xu 	PROC_LOCK(p);
121986857b36SDavid Xu 	if (uap->timerid < 3 ||
1220843b99c6SDavid Xu 	    (it = itimer_find(p, uap->timerid)) == NULL) {
122186857b36SDavid Xu 		PROC_UNLOCK(p);
122286857b36SDavid Xu 		error = EINVAL;
122386857b36SDavid Xu 	} else {
122486857b36SDavid Xu 		td->td_retval[0] = it->it_overrun_last;
122586857b36SDavid Xu 		ITIMER_UNLOCK(it);
122656c06c4bSDavid Xu 		PROC_UNLOCK(p);
122786857b36SDavid Xu 		error = 0;
122886857b36SDavid Xu 	}
122986857b36SDavid Xu 	return (error);
123086857b36SDavid Xu }
123186857b36SDavid Xu 
123286857b36SDavid Xu static int
123386857b36SDavid Xu realtimer_create(struct itimer *it)
123486857b36SDavid Xu {
123586857b36SDavid Xu 	callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
123686857b36SDavid Xu 	return (0);
123786857b36SDavid Xu }
123886857b36SDavid Xu 
123986857b36SDavid Xu static int
124086857b36SDavid Xu realtimer_delete(struct itimer *it)
124186857b36SDavid Xu {
124286857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
1243843b99c6SDavid Xu 
1244843b99c6SDavid Xu 	ITIMER_UNLOCK(it);
1245843b99c6SDavid Xu 	callout_drain(&it->it_callout);
1246843b99c6SDavid Xu 	ITIMER_LOCK(it);
124786857b36SDavid Xu 	return (0);
124886857b36SDavid Xu }
124986857b36SDavid Xu 
125086857b36SDavid Xu static int
125186857b36SDavid Xu realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
125286857b36SDavid Xu {
125356c06c4bSDavid Xu 	struct timespec cts;
125486857b36SDavid Xu 
125586857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
125686857b36SDavid Xu 
125756c06c4bSDavid Xu 	realtimer_clocktime(it->it_clockid, &cts);
125856c06c4bSDavid Xu 	*ovalue = it->it_time;
125986857b36SDavid Xu 	if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
126056c06c4bSDavid Xu 		timespecsub(&ovalue->it_value, &cts);
126186857b36SDavid Xu 		if (ovalue->it_value.tv_sec < 0 ||
126286857b36SDavid Xu 		    (ovalue->it_value.tv_sec == 0 &&
126386857b36SDavid Xu 		     ovalue->it_value.tv_nsec == 0)) {
126486857b36SDavid Xu 			ovalue->it_value.tv_sec  = 0;
126586857b36SDavid Xu 			ovalue->it_value.tv_nsec = 1;
126686857b36SDavid Xu 		}
126786857b36SDavid Xu 	}
126886857b36SDavid Xu 	return (0);
126986857b36SDavid Xu }
127086857b36SDavid Xu 
127186857b36SDavid Xu static int
127286857b36SDavid Xu realtimer_settime(struct itimer *it, int flags,
127386857b36SDavid Xu 	struct itimerspec *value, struct itimerspec *ovalue)
127486857b36SDavid Xu {
127556c06c4bSDavid Xu 	struct timespec cts, ts;
127656c06c4bSDavid Xu 	struct timeval tv;
127756c06c4bSDavid Xu 	struct itimerspec val;
127886857b36SDavid Xu 
127986857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
128086857b36SDavid Xu 
128156c06c4bSDavid Xu 	val = *value;
128256c06c4bSDavid Xu 	if (itimespecfix(&val.it_value))
128386857b36SDavid Xu 		return (EINVAL);
128486857b36SDavid Xu 
128556c06c4bSDavid Xu 	if (timespecisset(&val.it_value)) {
128656c06c4bSDavid Xu 		if (itimespecfix(&val.it_interval))
128786857b36SDavid Xu 			return (EINVAL);
128886857b36SDavid Xu 	} else {
128956c06c4bSDavid Xu 		timespecclear(&val.it_interval);
129086857b36SDavid Xu 	}
129186857b36SDavid Xu 
129286857b36SDavid Xu 	if (ovalue != NULL)
129386857b36SDavid Xu 		realtimer_gettime(it, ovalue);
129486857b36SDavid Xu 
129586857b36SDavid Xu 	it->it_time = val;
129656c06c4bSDavid Xu 	if (timespecisset(&val.it_value)) {
129756c06c4bSDavid Xu 		realtimer_clocktime(it->it_clockid, &cts);
129856c06c4bSDavid Xu 		ts = val.it_value;
129986857b36SDavid Xu 		if ((flags & TIMER_ABSTIME) == 0) {
130086857b36SDavid Xu 			/* Convert to absolute time. */
130156c06c4bSDavid Xu 			timespecadd(&it->it_time.it_value, &cts);
130286857b36SDavid Xu 		} else {
130356c06c4bSDavid Xu 			timespecsub(&ts, &cts);
130486857b36SDavid Xu 			/*
130556c06c4bSDavid Xu 			 * We don't care if ts is negative, tztohz will
130686857b36SDavid Xu 			 * fix it.
130786857b36SDavid Xu 			 */
130886857b36SDavid Xu 		}
130956c06c4bSDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
131056c06c4bSDavid Xu 		callout_reset(&it->it_callout, tvtohz(&tv),
131186857b36SDavid Xu 			realtimer_expire, it);
131286857b36SDavid Xu 	} else {
131386857b36SDavid Xu 		callout_stop(&it->it_callout);
131486857b36SDavid Xu 	}
131586857b36SDavid Xu 
131686857b36SDavid Xu 	return (0);
131786857b36SDavid Xu }
131886857b36SDavid Xu 
131986857b36SDavid Xu static void
132056c06c4bSDavid Xu realtimer_clocktime(clockid_t id, struct timespec *ts)
132186857b36SDavid Xu {
132286857b36SDavid Xu 	if (id == CLOCK_REALTIME)
132356c06c4bSDavid Xu 		getnanotime(ts);
132486857b36SDavid Xu 	else	/* CLOCK_MONOTONIC */
132556c06c4bSDavid Xu 		getnanouptime(ts);
132656c06c4bSDavid Xu }
132756c06c4bSDavid Xu 
132856c06c4bSDavid Xu int
132961d3a4efSDavid Xu itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi)
133056c06c4bSDavid Xu {
133156c06c4bSDavid Xu 	struct itimer *it;
133256c06c4bSDavid Xu 
133356c06c4bSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
1334843b99c6SDavid Xu 	it = itimer_find(p, timerid);
133556c06c4bSDavid Xu 	if (it != NULL) {
133656c06c4bSDavid Xu 		ksi->ksi_overrun = it->it_overrun;
133756c06c4bSDavid Xu 		it->it_overrun_last = it->it_overrun;
133856c06c4bSDavid Xu 		it->it_overrun = 0;
133956c06c4bSDavid Xu 		ITIMER_UNLOCK(it);
134056c06c4bSDavid Xu 		return (0);
134156c06c4bSDavid Xu 	}
134256c06c4bSDavid Xu 	return (EINVAL);
134356c06c4bSDavid Xu }
134456c06c4bSDavid Xu 
134556c06c4bSDavid Xu int
134656c06c4bSDavid Xu itimespecfix(struct timespec *ts)
134756c06c4bSDavid Xu {
134856c06c4bSDavid Xu 
134956c06c4bSDavid Xu 	if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
135056c06c4bSDavid Xu 		return (EINVAL);
135156c06c4bSDavid Xu 	if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
135256c06c4bSDavid Xu 		ts->tv_nsec = tick * 1000;
135356c06c4bSDavid Xu 	return (0);
135486857b36SDavid Xu }
135586857b36SDavid Xu 
135686857b36SDavid Xu /* Timeout callback for realtime timer */
135786857b36SDavid Xu static void
135886857b36SDavid Xu realtimer_expire(void *arg)
135986857b36SDavid Xu {
136056c06c4bSDavid Xu 	struct timespec cts, ts;
136156c06c4bSDavid Xu 	struct timeval tv;
136286857b36SDavid Xu 	struct itimer *it;
136386857b36SDavid Xu 	struct proc *p;
136486857b36SDavid Xu 
136586857b36SDavid Xu 	it = (struct itimer *)arg;
136686857b36SDavid Xu 	p = it->it_proc;
136786857b36SDavid Xu 
136856c06c4bSDavid Xu 	realtimer_clocktime(it->it_clockid, &cts);
136986857b36SDavid Xu 	/* Only fire if time is reached. */
137056c06c4bSDavid Xu 	if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
137156c06c4bSDavid Xu 		if (timespecisset(&it->it_time.it_interval)) {
137256c06c4bSDavid Xu 			timespecadd(&it->it_time.it_value,
137386857b36SDavid Xu 				    &it->it_time.it_interval);
137456c06c4bSDavid Xu 			while (timespeccmp(&cts, &it->it_time.it_value, >=)) {
137577e718f7SDavid Xu 				if (it->it_overrun < INT_MAX)
137686857b36SDavid Xu 					it->it_overrun++;
137777e718f7SDavid Xu 				else
137877e718f7SDavid Xu 					it->it_ksi.ksi_errno = ERANGE;
137956c06c4bSDavid Xu 				timespecadd(&it->it_time.it_value,
138086857b36SDavid Xu 					    &it->it_time.it_interval);
138186857b36SDavid Xu 			}
138286857b36SDavid Xu 		} else {
138386857b36SDavid Xu 			/* single shot timer ? */
138456c06c4bSDavid Xu 			timespecclear(&it->it_time.it_value);
138586857b36SDavid Xu 		}
138656c06c4bSDavid Xu 		if (timespecisset(&it->it_time.it_value)) {
138756c06c4bSDavid Xu 			ts = it->it_time.it_value;
138856c06c4bSDavid Xu 			timespecsub(&ts, &cts);
138956c06c4bSDavid Xu 			TIMESPEC_TO_TIMEVAL(&tv, &ts);
139056c06c4bSDavid Xu 			callout_reset(&it->it_callout, tvtohz(&tv),
139186857b36SDavid Xu 				 realtimer_expire, it);
139286857b36SDavid Xu 		}
139386857b36SDavid Xu 		ITIMER_UNLOCK(it);
139486857b36SDavid Xu 		itimer_fire(it);
139586857b36SDavid Xu 		ITIMER_LOCK(it);
139656c06c4bSDavid Xu 	} else if (timespecisset(&it->it_time.it_value)) {
139756c06c4bSDavid Xu 		ts = it->it_time.it_value;
139856c06c4bSDavid Xu 		timespecsub(&ts, &cts);
139956c06c4bSDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
140056c06c4bSDavid Xu 		callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
140186857b36SDavid Xu  			it);
140286857b36SDavid Xu 	}
140386857b36SDavid Xu }
140486857b36SDavid Xu 
140586857b36SDavid Xu void
140686857b36SDavid Xu itimer_fire(struct itimer *it)
140786857b36SDavid Xu {
140886857b36SDavid Xu 	struct proc *p = it->it_proc;
14096d7b314bSDavid Xu 	int ret;
141086857b36SDavid Xu 
141156c06c4bSDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
141256c06c4bSDavid Xu 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
141386857b36SDavid Xu 		PROC_LOCK(p);
14146d7b314bSDavid Xu 		if (!KSI_ONQ(&it->it_ksi)) {
141577e718f7SDavid Xu 			it->it_ksi.ksi_errno = 0;
14166d7b314bSDavid Xu 			ret = psignal_event(p, &it->it_sigev, &it->it_ksi);
14176d7b314bSDavid Xu 			if (__predict_false(ret != 0)) {
141886857b36SDavid Xu 				it->it_overrun++;
141956c06c4bSDavid Xu 				/*
142056c06c4bSDavid Xu 				 * Broken userland code, thread went
142156c06c4bSDavid Xu 				 * away, disarm the timer.
142256c06c4bSDavid Xu 				 */
14236d7b314bSDavid Xu 				if (ret == ESRCH) {
142456c06c4bSDavid Xu 					ITIMER_LOCK(it);
142556c06c4bSDavid Xu 					timespecclear(&it->it_time.it_value);
142656c06c4bSDavid Xu 					timespecclear(&it->it_time.it_interval);
142756c06c4bSDavid Xu 					callout_stop(&it->it_callout);
142856c06c4bSDavid Xu 					ITIMER_UNLOCK(it);
14296d7b314bSDavid Xu 				}
143056c06c4bSDavid Xu 			}
143156c06c4bSDavid Xu 		} else {
143277e718f7SDavid Xu 			if (it->it_overrun < INT_MAX)
14336d7b314bSDavid Xu 				it->it_overrun++;
143477e718f7SDavid Xu 			else
143577e718f7SDavid Xu 				it->it_ksi.ksi_errno = ERANGE;
143656c06c4bSDavid Xu 		}
143786857b36SDavid Xu 		PROC_UNLOCK(p);
143886857b36SDavid Xu 	}
143986857b36SDavid Xu }
144086857b36SDavid Xu 
144186857b36SDavid Xu static void
144286857b36SDavid Xu itimers_alloc(struct proc *p)
144386857b36SDavid Xu {
144460354683SDavid Xu 	struct itimers *its;
144560354683SDavid Xu 	int i;
144686857b36SDavid Xu 
144760354683SDavid Xu 	its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
144886857b36SDavid Xu 	LIST_INIT(&its->its_virtual);
144986857b36SDavid Xu 	LIST_INIT(&its->its_prof);
145086857b36SDavid Xu 	TAILQ_INIT(&its->its_worklist);
145160354683SDavid Xu 	for (i = 0; i < TIMER_MAX; i++)
145260354683SDavid Xu 		its->its_timers[i] = NULL;
145360354683SDavid Xu 	PROC_LOCK(p);
145460354683SDavid Xu 	if (p->p_itimers == NULL) {
145560354683SDavid Xu 		p->p_itimers = its;
145660354683SDavid Xu 		PROC_UNLOCK(p);
145760354683SDavid Xu 	}
145860354683SDavid Xu 	else {
145960354683SDavid Xu 		PROC_UNLOCK(p);
146060354683SDavid Xu 		free(its, M_SUBPROC);
146160354683SDavid Xu 	}
146286857b36SDavid Xu }
146386857b36SDavid Xu 
1464993182e5SAlexander Leidinger static void
1465993182e5SAlexander Leidinger itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
1466993182e5SAlexander Leidinger {
1467993182e5SAlexander Leidinger 	itimers_event_hook_exit(arg, p);
1468993182e5SAlexander Leidinger }
1469993182e5SAlexander Leidinger 
147086857b36SDavid Xu /* Clean up timers when some process events are being triggered. */
1471d26b1a1fSDavid Xu static void
1472993182e5SAlexander Leidinger itimers_event_hook_exit(void *arg, struct proc *p)
147386857b36SDavid Xu {
147486857b36SDavid Xu 	struct itimers *its;
147586857b36SDavid Xu 	struct itimer *it;
14763e70c6f0SDavid Xu 	int event = (int)(intptr_t)arg;
147786857b36SDavid Xu 	int i;
147886857b36SDavid Xu 
147960354683SDavid Xu 	if (p->p_itimers != NULL) {
148060354683SDavid Xu 		its = p->p_itimers;
148186857b36SDavid Xu 		for (i = 0; i < MAX_CLOCKS; ++i) {
148286857b36SDavid Xu 			if (posix_clocks[i].event_hook != NULL)
148386857b36SDavid Xu 				CLOCK_CALL(i, event_hook, (p, i, event));
148486857b36SDavid Xu 		}
148586857b36SDavid Xu 		/*
148686857b36SDavid Xu 		 * According to susv3, XSI interval timers should be inherited
148786857b36SDavid Xu 		 * by new image.
148886857b36SDavid Xu 		 */
148986857b36SDavid Xu 		if (event == ITIMER_EV_EXEC)
149086857b36SDavid Xu 			i = 3;
149186857b36SDavid Xu 		else if (event == ITIMER_EV_EXIT)
149286857b36SDavid Xu 			i = 0;
149386857b36SDavid Xu 		else
149486857b36SDavid Xu 			panic("unhandled event");
149586857b36SDavid Xu 		for (; i < TIMER_MAX; ++i) {
1496843b99c6SDavid Xu 			if ((it = its->its_timers[i]) != NULL)
1497843b99c6SDavid Xu 				kern_timer_delete(curthread, i);
149886857b36SDavid Xu 		}
149986857b36SDavid Xu 		if (its->its_timers[0] == NULL &&
150086857b36SDavid Xu 		    its->its_timers[1] == NULL &&
150186857b36SDavid Xu 		    its->its_timers[2] == NULL) {
150260354683SDavid Xu 			free(its, M_SUBPROC);
150360354683SDavid Xu 			p->p_itimers = NULL;
150486857b36SDavid Xu 		}
150586857b36SDavid Xu 	}
150686857b36SDavid Xu }
1507