xref: /freebsd/sys/kern/kern_time.c (revision 77e718f7737732665c489a6a4b2dbaf6acc51b0c)
19454b2d8SWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)kern_time.c	8.1 (Berkeley) 6/10/93
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
32677b542eSDavid E. O'Brien #include <sys/cdefs.h>
33677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
34677b542eSDavid E. O'Brien 
354b8d5f2dSRobert Watson #include "opt_mac.h"
364b8d5f2dSRobert Watson 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38e96c1fdcSPeter Wemm #include <sys/systm.h>
39fb919e4dSMark Murray #include <sys/lock.h>
40fb919e4dSMark Murray #include <sys/mutex.h>
41d2d3e875SBruce Evans #include <sys/sysproto.h>
42df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
43797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
44df8bae1dSRodney W. Grimes #include <sys/kernel.h>
454b8d5f2dSRobert Watson #include <sys/mac.h>
46efa42cbcSPaul Saab #include <sys/syscallsubr.h>
4777e718f7SDavid Xu #include <sys/sysctl.h>
4894c8fcd8SPeter Wemm #include <sys/sysent.h>
49df8bae1dSRodney W. Grimes #include <sys/proc.h>
50708e7684SPeter Wemm #include <sys/time.h>
5186857b36SDavid Xu #include <sys/timers.h>
5291266b96SPoul-Henning Kamp #include <sys/timetc.h>
53df8bae1dSRodney W. Grimes #include <sys/vnode.h>
54fb919e4dSMark Murray 
5577e718f7SDavid Xu #include <posix4/posix4.h>
5677e718f7SDavid Xu 
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 
6291f1c2b3SPoul-Henning Kamp int tz_minuteswest;
6391f1c2b3SPoul-Henning Kamp int tz_dsttime;
64ac7e6123SDavid Greenman 
6586857b36SDavid Xu static struct kclock	posix_clocks[MAX_CLOCKS];
6686857b36SDavid Xu static uma_zone_t	itimer_zone = NULL;
6786857b36SDavid Xu 
68df8bae1dSRodney W. Grimes /*
69df8bae1dSRodney W. Grimes  * Time of day and interval timer support.
70df8bae1dSRodney W. Grimes  *
71df8bae1dSRodney W. Grimes  * These routines provide the kernel entry points to get and set
72df8bae1dSRodney W. Grimes  * the time-of-day and per-process interval timers.  Subroutines
73df8bae1dSRodney W. Grimes  * here provide support for adding and subtracting timeval structures
74df8bae1dSRodney W. Grimes  * and decrementing interval timers, optionally reloading the interval
75df8bae1dSRodney W. Grimes  * timers when they expire.
76df8bae1dSRodney W. Grimes  */
77df8bae1dSRodney W. Grimes 
787edfb592SJohn Baldwin static int	settime(struct thread *, struct timeval *);
794d77a549SAlfred Perlstein static void	timevalfix(struct timeval *);
804d77a549SAlfred Perlstein static void	no_lease_updatetime(int);
8194c8fcd8SPeter Wemm 
8286857b36SDavid Xu static void	itimer_start(void);
8386857b36SDavid Xu static int	itimer_init(void *, int, int);
8486857b36SDavid Xu static void	itimer_fini(void *, int);
8586857b36SDavid Xu static void	itimer_enter(struct itimer *);
8686857b36SDavid Xu static void	itimer_leave(struct itimer *);
8786857b36SDavid Xu static struct itimer *itimer_find(struct proc *, timer_t, int);
8886857b36SDavid Xu static void	itimers_alloc(struct proc *);
8986857b36SDavid Xu static int	realtimer_create(struct itimer *);
9086857b36SDavid Xu static int	realtimer_gettime(struct itimer *, struct itimerspec *);
9186857b36SDavid Xu static int	realtimer_settime(struct itimer *, int,
9286857b36SDavid Xu 			struct itimerspec *, struct itimerspec *);
9386857b36SDavid Xu static int	realtimer_delete(struct itimer *);
9456c06c4bSDavid Xu static void	realtimer_clocktime(clockid_t, struct timespec *);
9586857b36SDavid Xu static void	realtimer_expire(void *);
9686857b36SDavid Xu static void	realtimer_event_hook(struct proc *, clockid_t, int event);
9786857b36SDavid Xu static int	kern_timer_create(struct thread *, clockid_t,
9886857b36SDavid Xu 			struct sigevent *, timer_t *, timer_t);
9986857b36SDavid Xu static int	kern_timer_delete(struct thread *, timer_t);
10086857b36SDavid Xu 
10186857b36SDavid Xu int		register_posix_clock(int, struct kclock *);
10286857b36SDavid Xu void		itimer_fire(struct itimer *it);
10356c06c4bSDavid Xu int		itimespecfix(struct timespec *ts);
10486857b36SDavid Xu 
10586857b36SDavid Xu #define CLOCK_CALL(clock, call, arglist)		\
10686857b36SDavid Xu 	((*posix_clocks[clock].call) arglist)
10786857b36SDavid Xu 
10886857b36SDavid Xu SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL);
10986857b36SDavid Xu 
11086857b36SDavid Xu 
1111b09ae77SPoul-Henning Kamp static void
1121b09ae77SPoul-Henning Kamp no_lease_updatetime(deltat)
1131b09ae77SPoul-Henning Kamp 	int deltat;
1141b09ae77SPoul-Henning Kamp {
1151b09ae77SPoul-Henning Kamp }
1161b09ae77SPoul-Henning Kamp 
1174d77a549SAlfred Perlstein void (*lease_updatetime)(int)  = no_lease_updatetime;
1181b09ae77SPoul-Henning Kamp 
11994c8fcd8SPeter Wemm static int
12091afe087SPoul-Henning Kamp settime(struct thread *td, struct timeval *tv)
12194c8fcd8SPeter Wemm {
122fcae3aa6SNick Sayer 	struct timeval delta, tv1, tv2;
123c0bd94a7SNick Sayer 	static struct timeval maxtime, laststep;
1247ec73f64SPoul-Henning Kamp 	struct timespec ts;
12594c8fcd8SPeter Wemm 	int s;
12694c8fcd8SPeter Wemm 
127708e7684SPeter Wemm 	s = splclock();
1289c8fff87SBruce Evans 	microtime(&tv1);
12900af9731SPoul-Henning Kamp 	delta = *tv;
13000af9731SPoul-Henning Kamp 	timevalsub(&delta, &tv1);
13194c8fcd8SPeter Wemm 
13294c8fcd8SPeter Wemm 	/*
1339c8fff87SBruce Evans 	 * If the system is secure, we do not allow the time to be
134fcae3aa6SNick Sayer 	 * set to a value earlier than 1 second less than the highest
135fcae3aa6SNick Sayer 	 * time we have yet seen. The worst a miscreant can do in
136fcae3aa6SNick Sayer 	 * this circumstance is "freeze" time. He couldn't go
137fcae3aa6SNick Sayer 	 * back to the past.
138c0bd94a7SNick Sayer 	 *
139c0bd94a7SNick Sayer 	 * We similarly do not allow the clock to be stepped more
140c0bd94a7SNick Sayer 	 * than one second, nor more than once per second. This allows
141c0bd94a7SNick Sayer 	 * a miscreant to make the clock march double-time, but no worse.
14294c8fcd8SPeter Wemm 	 */
1437edfb592SJohn Baldwin 	if (securelevel_gt(td->td_ucred, 1) != 0) {
144fcae3aa6SNick Sayer 		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
1453f92429aSMatt Jacob 			/*
146c0bd94a7SNick Sayer 			 * Update maxtime to latest time we've seen.
1473f92429aSMatt Jacob 			 */
148fcae3aa6SNick Sayer 			if (tv1.tv_sec > maxtime.tv_sec)
149fcae3aa6SNick Sayer 				maxtime = tv1;
150fcae3aa6SNick Sayer 			tv2 = *tv;
151fcae3aa6SNick Sayer 			timevalsub(&tv2, &maxtime);
152fcae3aa6SNick Sayer 			if (tv2.tv_sec < -1) {
1533f92429aSMatt Jacob 				tv->tv_sec = maxtime.tv_sec - 1;
154fcae3aa6SNick Sayer 				printf("Time adjustment clamped to -1 second\n");
155fcae3aa6SNick Sayer 			}
1563f92429aSMatt Jacob 		} else {
157c0bd94a7SNick Sayer 			if (tv1.tv_sec == laststep.tv_sec) {
158c0bd94a7SNick Sayer 				splx(s);
159c0bd94a7SNick Sayer 				return (EPERM);
160c0bd94a7SNick Sayer 			}
161c0bd94a7SNick Sayer 			if (delta.tv_sec > 1) {
162c0bd94a7SNick Sayer 				tv->tv_sec = tv1.tv_sec + 1;
163c0bd94a7SNick Sayer 				printf("Time adjustment clamped to +1 second\n");
164c0bd94a7SNick Sayer 			}
165c0bd94a7SNick Sayer 			laststep = *tv;
166fcae3aa6SNick Sayer 		}
1679c8fff87SBruce Evans 	}
1689c8fff87SBruce Evans 
1697ec73f64SPoul-Henning Kamp 	ts.tv_sec = tv->tv_sec;
1707ec73f64SPoul-Henning Kamp 	ts.tv_nsec = tv->tv_usec * 1000;
1717edfb592SJohn Baldwin 	mtx_lock(&Giant);
17291266b96SPoul-Henning Kamp 	tc_setclock(&ts);
17394c8fcd8SPeter Wemm 	(void) splsoftclock();
17494c8fcd8SPeter Wemm 	lease_updatetime(delta.tv_sec);
17594c8fcd8SPeter Wemm 	splx(s);
17694c8fcd8SPeter Wemm 	resettodr();
1777edfb592SJohn Baldwin 	mtx_unlock(&Giant);
17894c8fcd8SPeter Wemm 	return (0);
17994c8fcd8SPeter Wemm }
18094c8fcd8SPeter Wemm 
18194c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
18294c8fcd8SPeter Wemm struct clock_gettime_args {
18394c8fcd8SPeter Wemm 	clockid_t clock_id;
18494c8fcd8SPeter Wemm 	struct	timespec *tp;
18594c8fcd8SPeter Wemm };
18694c8fcd8SPeter Wemm #endif
187708e7684SPeter Wemm 
188fb99ab88SMatthew Dillon /*
189fb99ab88SMatthew Dillon  * MPSAFE
190fb99ab88SMatthew Dillon  */
19194c8fcd8SPeter Wemm /* ARGSUSED */
19294c8fcd8SPeter Wemm int
19391afe087SPoul-Henning Kamp clock_gettime(struct thread *td, struct clock_gettime_args *uap)
19494c8fcd8SPeter Wemm {
19594c8fcd8SPeter Wemm 	struct timespec ats;
196f0b479cdSPaul Saab 	int error;
197f0b479cdSPaul Saab 
198f0b479cdSPaul Saab 	error = kern_clock_gettime(td, uap->clock_id, &ats);
199f0b479cdSPaul Saab 	if (error == 0)
200f0b479cdSPaul Saab 		error = copyout(&ats, uap->tp, sizeof(ats));
201f0b479cdSPaul Saab 
202f0b479cdSPaul Saab 	return (error);
203f0b479cdSPaul Saab }
204f0b479cdSPaul Saab 
205f0b479cdSPaul Saab int
206f0b479cdSPaul Saab kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
207f0b479cdSPaul Saab {
208de0a9241SKelly Yancey 	struct timeval sys, user;
20978c85e8dSJohn Baldwin 	struct proc *p;
21094c8fcd8SPeter Wemm 
21178c85e8dSJohn Baldwin 	p = td->td_proc;
212f0b479cdSPaul Saab 	switch (clock_id) {
2135e758b95SRobert Watson 	case CLOCK_REALTIME:		/* Default to precise. */
2145e758b95SRobert Watson 	case CLOCK_REALTIME_PRECISE:
215f0b479cdSPaul Saab 		nanotime(ats);
216b8817154SKelly Yancey 		break;
2175e758b95SRobert Watson 	case CLOCK_REALTIME_FAST:
2185e758b95SRobert Watson 		getnanotime(ats);
2195e758b95SRobert Watson 		break;
220b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
22178c85e8dSJohn Baldwin 		PROC_LOCK(p);
22278c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
22378c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
224f0b479cdSPaul Saab 		TIMEVAL_TO_TIMESPEC(&user, ats);
225b8817154SKelly Yancey 		break;
226b8817154SKelly Yancey 	case CLOCK_PROF:
22778c85e8dSJohn Baldwin 		PROC_LOCK(p);
22878c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
22978c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
230de0a9241SKelly Yancey 		timevaladd(&user, &sys);
231f0b479cdSPaul Saab 		TIMEVAL_TO_TIMESPEC(&user, ats);
232de0a9241SKelly Yancey 		break;
2335e758b95SRobert Watson 	case CLOCK_MONOTONIC:		/* Default to precise. */
2345e758b95SRobert Watson 	case CLOCK_MONOTONIC_PRECISE:
2355eefd889SAndre Oppermann 	case CLOCK_UPTIME:
2365e758b95SRobert Watson 	case CLOCK_UPTIME_PRECISE:
237f0b479cdSPaul Saab 		nanouptime(ats);
238b8817154SKelly Yancey 		break;
2395e758b95SRobert Watson 	case CLOCK_UPTIME_FAST:
2405e758b95SRobert Watson 	case CLOCK_MONOTONIC_FAST:
2415e758b95SRobert Watson 		getnanouptime(ats);
2425e758b95SRobert Watson 		break;
2435e758b95SRobert Watson 	case CLOCK_SECOND:
2445e758b95SRobert Watson 		ats->tv_sec = time_second;
2455e758b95SRobert Watson 		ats->tv_nsec = 0;
2465e758b95SRobert Watson 		break;
247b8817154SKelly Yancey 	default:
2485cb3dc8fSPoul-Henning Kamp 		return (EINVAL);
249b8817154SKelly Yancey 	}
250f0b479cdSPaul Saab 	return (0);
25194c8fcd8SPeter Wemm }
25294c8fcd8SPeter Wemm 
25394c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
25494c8fcd8SPeter Wemm struct clock_settime_args {
25594c8fcd8SPeter Wemm 	clockid_t clock_id;
25694c8fcd8SPeter Wemm 	const struct	timespec *tp;
25794c8fcd8SPeter Wemm };
25894c8fcd8SPeter Wemm #endif
259708e7684SPeter Wemm 
260fb99ab88SMatthew Dillon /*
261fb99ab88SMatthew Dillon  * MPSAFE
262fb99ab88SMatthew Dillon  */
26394c8fcd8SPeter Wemm /* ARGSUSED */
26494c8fcd8SPeter Wemm int
26591afe087SPoul-Henning Kamp clock_settime(struct thread *td, struct clock_settime_args *uap)
26694c8fcd8SPeter Wemm {
26794c8fcd8SPeter Wemm 	struct timespec ats;
26894c8fcd8SPeter Wemm 	int error;
26994c8fcd8SPeter Wemm 
270f0b479cdSPaul Saab 	if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
271f0b479cdSPaul Saab 		return (error);
272f0b479cdSPaul Saab 	return (kern_clock_settime(td, uap->clock_id, &ats));
273f0b479cdSPaul Saab }
274f0b479cdSPaul Saab 
275f0b479cdSPaul Saab int
276f0b479cdSPaul Saab kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
277f0b479cdSPaul Saab {
278f0b479cdSPaul Saab 	struct timeval atv;
279f0b479cdSPaul Saab 	int error;
280f0b479cdSPaul Saab 
2814b8d5f2dSRobert Watson #ifdef MAC
2824b8d5f2dSRobert Watson 	error = mac_check_system_settime(td->td_ucred);
2834b8d5f2dSRobert Watson 	if (error)
2844b8d5f2dSRobert Watson 		return (error);
2854b8d5f2dSRobert Watson #endif
28644731cabSJohn Baldwin 	if ((error = suser(td)) != 0)
2877edfb592SJohn Baldwin 		return (error);
288f0b479cdSPaul Saab 	if (clock_id != CLOCK_REALTIME)
2897edfb592SJohn Baldwin 		return (EINVAL);
290f0b479cdSPaul Saab 	if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000)
2917edfb592SJohn Baldwin 		return (EINVAL);
292a0502b19SPoul-Henning Kamp 	/* XXX Don't convert nsec->usec and back */
293f0b479cdSPaul Saab 	TIMESPEC_TO_TIMEVAL(&atv, ats);
2947edfb592SJohn Baldwin 	error = settime(td, &atv);
29594c8fcd8SPeter Wemm 	return (error);
29694c8fcd8SPeter Wemm }
29794c8fcd8SPeter Wemm 
29894c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
29994c8fcd8SPeter Wemm struct clock_getres_args {
30094c8fcd8SPeter Wemm 	clockid_t clock_id;
30194c8fcd8SPeter Wemm 	struct	timespec *tp;
30294c8fcd8SPeter Wemm };
30394c8fcd8SPeter Wemm #endif
304708e7684SPeter Wemm 
30594c8fcd8SPeter Wemm int
30691afe087SPoul-Henning Kamp clock_getres(struct thread *td, struct clock_getres_args *uap)
30794c8fcd8SPeter Wemm {
30894c8fcd8SPeter Wemm 	struct timespec ts;
309f0b479cdSPaul Saab 	int error;
31094c8fcd8SPeter Wemm 
311f0b479cdSPaul Saab 	if (uap->tp == NULL)
312f0b479cdSPaul Saab 		return (0);
313f0b479cdSPaul Saab 
314f0b479cdSPaul Saab 	error = kern_clock_getres(td, uap->clock_id, &ts);
315f0b479cdSPaul Saab 	if (error == 0)
316f0b479cdSPaul Saab 		error = copyout(&ts, uap->tp, sizeof(ts));
317f0b479cdSPaul Saab 	return (error);
318f0b479cdSPaul Saab }
319f0b479cdSPaul Saab 
320f0b479cdSPaul Saab int
321f0b479cdSPaul Saab kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
322f0b479cdSPaul Saab {
323f0b479cdSPaul Saab 
324f0b479cdSPaul Saab 	ts->tv_sec = 0;
325f0b479cdSPaul Saab 	switch (clock_id) {
326b8817154SKelly Yancey 	case CLOCK_REALTIME:
3275e758b95SRobert Watson 	case CLOCK_REALTIME_FAST:
3285e758b95SRobert Watson 	case CLOCK_REALTIME_PRECISE:
329b8817154SKelly Yancey 	case CLOCK_MONOTONIC:
3305e758b95SRobert Watson 	case CLOCK_MONOTONIC_FAST:
3315e758b95SRobert Watson 	case CLOCK_MONOTONIC_PRECISE:
3325eefd889SAndre Oppermann 	case CLOCK_UPTIME:
3335e758b95SRobert Watson 	case CLOCK_UPTIME_FAST:
3345e758b95SRobert Watson 	case CLOCK_UPTIME_PRECISE:
335ac0653dcSBruce Evans 		/*
336ac0653dcSBruce Evans 		 * Round up the result of the division cheaply by adding 1.
337ac0653dcSBruce Evans 		 * Rounding up is especially important if rounding down
338ac0653dcSBruce Evans 		 * would give 0.  Perfect rounding is unimportant.
339ac0653dcSBruce Evans 		 */
340f0b479cdSPaul Saab 		ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
341b8817154SKelly Yancey 		break;
342b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
343b8817154SKelly Yancey 	case CLOCK_PROF:
344b8817154SKelly Yancey 		/* Accurately round up here because we can do so cheaply. */
345f0b479cdSPaul Saab 		ts->tv_nsec = (1000000000 + hz - 1) / hz;
346b8817154SKelly Yancey 		break;
3475e758b95SRobert Watson 	case CLOCK_SECOND:
3485e758b95SRobert Watson 		ts->tv_sec = 1;
3495e758b95SRobert Watson 		ts->tv_nsec = 0;
3505e758b95SRobert Watson 		break;
351b8817154SKelly Yancey 	default:
352b8817154SKelly Yancey 		return (EINVAL);
35394c8fcd8SPeter Wemm 	}
354de0a9241SKelly Yancey 	return (0);
35594c8fcd8SPeter Wemm }
35694c8fcd8SPeter Wemm 
35794c8fcd8SPeter Wemm static int nanowait;
35894c8fcd8SPeter Wemm 
3597fdf2c85SPaul Saab int
3607fdf2c85SPaul Saab kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
3615b870b7bSPeter Wemm {
3625704ba6aSPoul-Henning Kamp 	struct timespec ts, ts2, ts3;
36333841826SPoul-Henning Kamp 	struct timeval tv;
36433841826SPoul-Henning Kamp 	int error;
3655b870b7bSPeter Wemm 
3667d7fb492SBruce Evans 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
367708e7684SPeter Wemm 		return (EINVAL);
368d254af07SMatthew Dillon 	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
3697d7fb492SBruce Evans 		return (0);
370c21410e1SPoul-Henning Kamp 	getnanouptime(&ts);
37100af9731SPoul-Henning Kamp 	timespecadd(&ts, rqt);
37233841826SPoul-Henning Kamp 	TIMESPEC_TO_TIMEVAL(&tv, rqt);
37333841826SPoul-Henning Kamp 	for (;;) {
37433841826SPoul-Henning Kamp 		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
37533841826SPoul-Henning Kamp 		    tvtohz(&tv));
376c21410e1SPoul-Henning Kamp 		getnanouptime(&ts2);
37733841826SPoul-Henning Kamp 		if (error != EWOULDBLOCK) {
37833841826SPoul-Henning Kamp 			if (error == ERESTART)
37994c8fcd8SPeter Wemm 				error = EINTR;
38033841826SPoul-Henning Kamp 			if (rmt != NULL) {
38133841826SPoul-Henning Kamp 				timespecsub(&ts, &ts2);
38233841826SPoul-Henning Kamp 				if (ts.tv_sec < 0)
38333841826SPoul-Henning Kamp 					timespecclear(&ts);
38400af9731SPoul-Henning Kamp 				*rmt = ts;
38500af9731SPoul-Henning Kamp 			}
38633841826SPoul-Henning Kamp 			return (error);
38733841826SPoul-Henning Kamp 		}
38833841826SPoul-Henning Kamp 		if (timespeccmp(&ts2, &ts, >=))
38933841826SPoul-Henning Kamp 			return (0);
3905704ba6aSPoul-Henning Kamp 		ts3 = ts;
3915704ba6aSPoul-Henning Kamp 		timespecsub(&ts3, &ts2);
3925704ba6aSPoul-Henning Kamp 		TIMESPEC_TO_TIMEVAL(&tv, &ts3);
39333841826SPoul-Henning Kamp 	}
3945b870b7bSPeter Wemm }
39594c8fcd8SPeter Wemm 
3965b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
3975b870b7bSPeter Wemm struct nanosleep_args {
3985b870b7bSPeter Wemm 	struct	timespec *rqtp;
3995b870b7bSPeter Wemm 	struct	timespec *rmtp;
4005b870b7bSPeter Wemm };
4015b870b7bSPeter Wemm #endif
4025b870b7bSPeter Wemm 
403fb99ab88SMatthew Dillon /*
404fb99ab88SMatthew Dillon  * MPSAFE
405fb99ab88SMatthew Dillon  */
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
437fb99ab88SMatthew Dillon /*
438fb99ab88SMatthew Dillon  * MPSAFE
439fb99ab88SMatthew Dillon  */
440df8bae1dSRodney W. Grimes /* ARGSUSED */
44126f9a767SRodney W. Grimes int
44291afe087SPoul-Henning Kamp gettimeofday(struct thread *td, struct gettimeofday_args *uap)
443df8bae1dSRodney W. Grimes {
444df8bae1dSRodney W. Grimes 	struct timeval atv;
445411c25edSTim J. Robbins 	struct timezone rtz;
446df8bae1dSRodney W. Grimes 	int error = 0;
447df8bae1dSRodney W. Grimes 
448df8bae1dSRodney W. Grimes 	if (uap->tp) {
449df8bae1dSRodney W. Grimes 		microtime(&atv);
45001609114SAlfred Perlstein 		error = copyout(&atv, uap->tp, sizeof (atv));
451df8bae1dSRodney W. Grimes 	}
45221dcdb38SPoul-Henning Kamp 	if (error == 0 && uap->tzp != NULL) {
45391f1c2b3SPoul-Henning Kamp 		rtz.tz_minuteswest = tz_minuteswest;
45491f1c2b3SPoul-Henning Kamp 		rtz.tz_dsttime = tz_dsttime;
455411c25edSTim J. Robbins 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
45621dcdb38SPoul-Henning Kamp 	}
457df8bae1dSRodney W. Grimes 	return (error);
458df8bae1dSRodney W. Grimes }
459df8bae1dSRodney W. Grimes 
460d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
461df8bae1dSRodney W. Grimes struct settimeofday_args {
462df8bae1dSRodney W. Grimes 	struct	timeval *tv;
463df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
464df8bae1dSRodney W. Grimes };
465d2d3e875SBruce Evans #endif
466fb99ab88SMatthew Dillon /*
467fb99ab88SMatthew Dillon  * MPSAFE
468fb99ab88SMatthew Dillon  */
469df8bae1dSRodney W. Grimes /* ARGSUSED */
47026f9a767SRodney W. Grimes int
47191afe087SPoul-Henning Kamp settimeofday(struct thread *td, struct settimeofday_args *uap)
472df8bae1dSRodney W. Grimes {
473b88ec951SJohn Baldwin 	struct timeval atv, *tvp;
474b88ec951SJohn Baldwin 	struct timezone atz, *tzp;
475b88ec951SJohn Baldwin 	int error;
476b88ec951SJohn Baldwin 
477b88ec951SJohn Baldwin 	if (uap->tv) {
478b88ec951SJohn Baldwin 		error = copyin(uap->tv, &atv, sizeof(atv));
479b88ec951SJohn Baldwin 		if (error)
480b88ec951SJohn Baldwin 			return (error);
481b88ec951SJohn Baldwin 		tvp = &atv;
482b88ec951SJohn Baldwin 	} else
483b88ec951SJohn Baldwin 		tvp = NULL;
484b88ec951SJohn Baldwin 	if (uap->tzp) {
485b88ec951SJohn Baldwin 		error = copyin(uap->tzp, &atz, sizeof(atz));
486b88ec951SJohn Baldwin 		if (error)
487b88ec951SJohn Baldwin 			return (error);
488b88ec951SJohn Baldwin 		tzp = &atz;
489b88ec951SJohn Baldwin 	} else
490b88ec951SJohn Baldwin 		tzp = NULL;
491b88ec951SJohn Baldwin 	return (kern_settimeofday(td, tvp, tzp));
492b88ec951SJohn Baldwin }
493b88ec951SJohn Baldwin 
494b88ec951SJohn Baldwin int
495b88ec951SJohn Baldwin kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
496b88ec951SJohn Baldwin {
497b88ec951SJohn Baldwin 	int error;
498fb99ab88SMatthew Dillon 
4994b8d5f2dSRobert Watson #ifdef MAC
5004b8d5f2dSRobert Watson 	error = mac_check_system_settime(td->td_ucred);
5014b8d5f2dSRobert Watson 	if (error)
5024b8d5f2dSRobert Watson 		return (error);
5034b8d5f2dSRobert Watson #endif
504b88ec951SJohn Baldwin 	error = suser(td);
505b88ec951SJohn Baldwin 	if (error)
5067edfb592SJohn Baldwin 		return (error);
507df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
508b88ec951SJohn Baldwin 	if (tv) {
509b88ec951SJohn Baldwin 		if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
5107edfb592SJohn Baldwin 			return (EINVAL);
511b88ec951SJohn Baldwin 		error = settime(td, tv);
512708e7684SPeter Wemm 	}
513b88ec951SJohn Baldwin 	if (tzp && error == 0) {
514b88ec951SJohn Baldwin 		tz_minuteswest = tzp->tz_minuteswest;
515b88ec951SJohn Baldwin 		tz_dsttime = tzp->tz_dsttime;
516b88ec951SJohn Baldwin 	}
5177edfb592SJohn Baldwin 	return (error);
518b88ec951SJohn Baldwin }
5197edfb592SJohn Baldwin 
520df8bae1dSRodney W. Grimes /*
521df8bae1dSRodney W. Grimes  * Get value of an interval timer.  The process virtual and
522df8bae1dSRodney W. Grimes  * profiling virtual time timers are kept in the p_stats area, since
523df8bae1dSRodney W. Grimes  * they can be swapped out.  These are kept internally in the
524df8bae1dSRodney W. Grimes  * way they are specified externally: in time until they expire.
525df8bae1dSRodney W. Grimes  *
526df8bae1dSRodney W. Grimes  * The real time interval timer is kept in the process table slot
527df8bae1dSRodney W. Grimes  * for the process, and its value (it_value) is kept as an
528df8bae1dSRodney W. Grimes  * absolute time rather than as a delta, so that it is easy to keep
529df8bae1dSRodney W. Grimes  * periodic real-time signals from drifting.
530df8bae1dSRodney W. Grimes  *
531df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
532df8bae1dSRodney W. Grimes  * kern_clock.c.  The real time timer is processed by a timeout
533df8bae1dSRodney W. Grimes  * routine, called from the softclock() routine.  Since a callout
534df8bae1dSRodney W. Grimes  * may be delayed in real time due to interrupt processing in the system,
535df8bae1dSRodney W. Grimes  * it is possible for the real time timeout routine (realitexpire, given below),
536df8bae1dSRodney W. Grimes  * to be delayed in real time past when it is supposed to occur.  It
537df8bae1dSRodney W. Grimes  * does not suffice, therefore, to reload the real timer .it_value from the
538df8bae1dSRodney W. Grimes  * real time timers .it_interval.  Rather, we compute the next time in
539df8bae1dSRodney W. Grimes  * absolute time the timer should go off.
540df8bae1dSRodney W. Grimes  */
541d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
542df8bae1dSRodney W. Grimes struct getitimer_args {
543df8bae1dSRodney W. Grimes 	u_int	which;
544df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
545df8bae1dSRodney W. Grimes };
546d2d3e875SBruce Evans #endif
547fb99ab88SMatthew Dillon /*
548fb99ab88SMatthew Dillon  * MPSAFE
549fb99ab88SMatthew Dillon  */
55026f9a767SRodney W. Grimes int
55191afe087SPoul-Henning Kamp getitimer(struct thread *td, struct getitimer_args *uap)
552df8bae1dSRodney W. Grimes {
553df8bae1dSRodney W. Grimes 	struct itimerval aitv;
554c90110d6SJohn Baldwin 	int error;
555df8bae1dSRodney W. Grimes 
556cfa0efe7SMaxim Sobolev 	error = kern_getitimer(td, uap->which, &aitv);
557cfa0efe7SMaxim Sobolev 	if (error != 0)
558cfa0efe7SMaxim Sobolev 		return (error);
559cfa0efe7SMaxim Sobolev 	return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
560cfa0efe7SMaxim Sobolev }
561cfa0efe7SMaxim Sobolev 
562cfa0efe7SMaxim Sobolev int
563cfa0efe7SMaxim Sobolev kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
564cfa0efe7SMaxim Sobolev {
565cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
566cfa0efe7SMaxim Sobolev 	struct timeval ctv;
567cfa0efe7SMaxim Sobolev 
568cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
569df8bae1dSRodney W. Grimes 		return (EINVAL);
570fb99ab88SMatthew Dillon 
571cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
572df8bae1dSRodney W. Grimes 		/*
573ee002b68SBruce Evans 		 * Convert from absolute to relative time in .it_value
574df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
575df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
576df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
577df8bae1dSRodney W. Grimes 		 */
57896d7f8efSTim J. Robbins 		PROC_LOCK(p);
579cfa0efe7SMaxim Sobolev 		*aitv = p->p_realtimer;
58096d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
581cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
582c21410e1SPoul-Henning Kamp 			getmicrouptime(&ctv);
583cfa0efe7SMaxim Sobolev 			if (timevalcmp(&aitv->it_value, &ctv, <))
584cfa0efe7SMaxim Sobolev 				timevalclear(&aitv->it_value);
585df8bae1dSRodney W. Grimes 			else
586cfa0efe7SMaxim Sobolev 				timevalsub(&aitv->it_value, &ctv);
587227ee8a1SPoul-Henning Kamp 		}
588fb99ab88SMatthew Dillon 	} else {
58996d7f8efSTim J. Robbins 		mtx_lock_spin(&sched_lock);
590cfa0efe7SMaxim Sobolev 		*aitv = p->p_stats->p_timer[which];
59196d7f8efSTim J. Robbins 		mtx_unlock_spin(&sched_lock);
592fb99ab88SMatthew Dillon 	}
593cfa0efe7SMaxim Sobolev 	return (0);
594df8bae1dSRodney W. Grimes }
595df8bae1dSRodney W. Grimes 
596d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
597df8bae1dSRodney W. Grimes struct setitimer_args {
598df8bae1dSRodney W. Grimes 	u_int	which;
599df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
600df8bae1dSRodney W. Grimes };
601d2d3e875SBruce Evans #endif
602cfa0efe7SMaxim Sobolev 
603fb99ab88SMatthew Dillon /*
604fb99ab88SMatthew Dillon  * MPSAFE
605fb99ab88SMatthew Dillon  */
60626f9a767SRodney W. Grimes int
60791afe087SPoul-Henning Kamp setitimer(struct thread *td, struct setitimer_args *uap)
608df8bae1dSRodney W. Grimes {
609cfa0efe7SMaxim Sobolev 	struct itimerval aitv, oitv;
610c90110d6SJohn Baldwin 	int error;
61196d7f8efSTim J. Robbins 
61296d7f8efSTim J. Robbins 	if (uap->itv == NULL) {
61396d7f8efSTim J. Robbins 		uap->itv = uap->oitv;
61496d7f8efSTim J. Robbins 		return (getitimer(td, (struct getitimer_args *)uap));
61596d7f8efSTim J. Robbins 	}
616df8bae1dSRodney W. Grimes 
61796d7f8efSTim J. Robbins 	if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
618df8bae1dSRodney W. Grimes 		return (error);
619cfa0efe7SMaxim Sobolev 	error = kern_setitimer(td, uap->which, &aitv, &oitv);
620cfa0efe7SMaxim Sobolev 	if (error != 0 || uap->oitv == NULL)
621cfa0efe7SMaxim Sobolev 		return (error);
622cfa0efe7SMaxim Sobolev 	return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
623cfa0efe7SMaxim Sobolev }
624cfa0efe7SMaxim Sobolev 
625cfa0efe7SMaxim Sobolev int
626c90110d6SJohn Baldwin kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
627c90110d6SJohn Baldwin     struct itimerval *oitv)
628cfa0efe7SMaxim Sobolev {
629cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
630cfa0efe7SMaxim Sobolev 	struct timeval ctv;
631cfa0efe7SMaxim Sobolev 
6325e85ac17SJohn Baldwin 	if (aitv == NULL)
6335e85ac17SJohn Baldwin 		return (kern_getitimer(td, which, oitv));
6345e85ac17SJohn Baldwin 
635cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
63696d7f8efSTim J. Robbins 		return (EINVAL);
637cfa0efe7SMaxim Sobolev 	if (itimerfix(&aitv->it_value))
638cfa0efe7SMaxim Sobolev 		return (EINVAL);
639cfa0efe7SMaxim Sobolev 	if (!timevalisset(&aitv->it_value))
640cfa0efe7SMaxim Sobolev 		timevalclear(&aitv->it_interval);
641cfa0efe7SMaxim Sobolev 	else if (itimerfix(&aitv->it_interval))
64296d7f8efSTim J. Robbins 		return (EINVAL);
64396d7f8efSTim J. Robbins 
644cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
64596d7f8efSTim J. Robbins 		PROC_LOCK(p);
6464cf41af3SPoul-Henning Kamp 		if (timevalisset(&p->p_realtimer.it_value))
6474f559836SJake Burkholder 			callout_stop(&p->p_itcallout);
64825b4d3a8SJohn Baldwin 		getmicrouptime(&ctv);
649cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
650cfa0efe7SMaxim Sobolev 			callout_reset(&p->p_itcallout, tvtohz(&aitv->it_value),
6514f559836SJake Burkholder 			    realitexpire, p);
652cfa0efe7SMaxim Sobolev 			timevaladd(&aitv->it_value, &ctv);
65325b4d3a8SJohn Baldwin 		}
654cfa0efe7SMaxim Sobolev 		*oitv = p->p_realtimer;
655cfa0efe7SMaxim Sobolev 		p->p_realtimer = *aitv;
65696d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
657cfa0efe7SMaxim Sobolev 		if (timevalisset(&oitv->it_value)) {
658cfa0efe7SMaxim Sobolev 			if (timevalcmp(&oitv->it_value, &ctv, <))
659cfa0efe7SMaxim Sobolev 				timevalclear(&oitv->it_value);
66096d7f8efSTim J. Robbins 			else
661cfa0efe7SMaxim Sobolev 				timevalsub(&oitv->it_value, &ctv);
662fb99ab88SMatthew Dillon 		}
66396d7f8efSTim J. Robbins 	} else {
66496d7f8efSTim J. Robbins 		mtx_lock_spin(&sched_lock);
665cfa0efe7SMaxim Sobolev 		*oitv = p->p_stats->p_timer[which];
666cfa0efe7SMaxim Sobolev 		p->p_stats->p_timer[which] = *aitv;
66796d7f8efSTim J. Robbins 		mtx_unlock_spin(&sched_lock);
66896d7f8efSTim J. Robbins 	}
66996d7f8efSTim J. Robbins 	return (0);
670df8bae1dSRodney W. Grimes }
671df8bae1dSRodney W. Grimes 
672df8bae1dSRodney W. Grimes /*
673df8bae1dSRodney W. Grimes  * Real interval timer expired:
674df8bae1dSRodney W. Grimes  * send process whose timer expired an alarm signal.
675df8bae1dSRodney W. Grimes  * If time is not set up to reload, then just return.
676df8bae1dSRodney W. Grimes  * Else compute next time timer should go off which is > current time.
677df8bae1dSRodney W. Grimes  * This is where delay in processing this timeout causes multiple
678df8bae1dSRodney W. Grimes  * SIGALRM calls to be compressed into one.
679c8b47828SBruce Evans  * tvtohz() always adds 1 to allow for the time until the next clock
6809207f00aSBruce Evans  * interrupt being strictly less than 1 clock tick, but we don't want
6819207f00aSBruce Evans  * that here since we want to appear to be in sync with the clock
6829207f00aSBruce Evans  * interrupt even when we're delayed.
683df8bae1dSRodney W. Grimes  */
684df8bae1dSRodney W. Grimes void
68591afe087SPoul-Henning Kamp realitexpire(void *arg)
686df8bae1dSRodney W. Grimes {
68791afe087SPoul-Henning Kamp 	struct proc *p;
688bfe6c9faSPoul-Henning Kamp 	struct timeval ctv, ntv;
689df8bae1dSRodney W. Grimes 
690df8bae1dSRodney W. Grimes 	p = (struct proc *)arg;
69137824023SJohn Baldwin 	PROC_LOCK(p);
692df8bae1dSRodney W. Grimes 	psignal(p, SIGALRM);
6934cf41af3SPoul-Henning Kamp 	if (!timevalisset(&p->p_realtimer.it_interval)) {
6944cf41af3SPoul-Henning Kamp 		timevalclear(&p->p_realtimer.it_value);
6955499ea01SJohn Baldwin 		if (p->p_flag & P_WEXIT)
6965499ea01SJohn Baldwin 			wakeup(&p->p_itcallout);
69737824023SJohn Baldwin 		PROC_UNLOCK(p);
698df8bae1dSRodney W. Grimes 		return;
699df8bae1dSRodney W. Grimes 	}
700df8bae1dSRodney W. Grimes 	for (;;) {
701df8bae1dSRodney W. Grimes 		timevaladd(&p->p_realtimer.it_value,
702df8bae1dSRodney W. Grimes 		    &p->p_realtimer.it_interval);
703c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
7044cf41af3SPoul-Henning Kamp 		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
705bfe6c9faSPoul-Henning Kamp 			ntv = p->p_realtimer.it_value;
706bfe6c9faSPoul-Henning Kamp 			timevalsub(&ntv, &ctv);
7074f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
7084f559836SJake Burkholder 			    realitexpire, p);
70937824023SJohn Baldwin 			PROC_UNLOCK(p);
710df8bae1dSRodney W. Grimes 			return;
711df8bae1dSRodney W. Grimes 		}
712df8bae1dSRodney W. Grimes 	}
71337824023SJohn Baldwin 	/*NOTREACHED*/
714df8bae1dSRodney W. Grimes }
715df8bae1dSRodney W. Grimes 
716df8bae1dSRodney W. Grimes /*
717df8bae1dSRodney W. Grimes  * Check that a proposed value to load into the .it_value or
718df8bae1dSRodney W. Grimes  * .it_interval part of an interval timer is acceptable, and
719df8bae1dSRodney W. Grimes  * fix it to have at least minimal value (i.e. if it is less
720df8bae1dSRodney W. Grimes  * than the resolution of the clock, round it up.)
721df8bae1dSRodney W. Grimes  */
72226f9a767SRodney W. Grimes int
72391afe087SPoul-Henning Kamp itimerfix(struct timeval *tv)
724df8bae1dSRodney W. Grimes {
725df8bae1dSRodney W. Grimes 
72686857b36SDavid Xu 	if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
727df8bae1dSRodney W. Grimes 		return (EINVAL);
728df8bae1dSRodney W. Grimes 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
729df8bae1dSRodney W. Grimes 		tv->tv_usec = tick;
730df8bae1dSRodney W. Grimes 	return (0);
731df8bae1dSRodney W. Grimes }
732df8bae1dSRodney W. Grimes 
733df8bae1dSRodney W. Grimes /*
734df8bae1dSRodney W. Grimes  * Decrement an interval timer by a specified number
735df8bae1dSRodney W. Grimes  * of microseconds, which must be less than a second,
736df8bae1dSRodney W. Grimes  * i.e. < 1000000.  If the timer expires, then reload
737df8bae1dSRodney W. Grimes  * it.  In this case, carry over (usec - old value) to
738df8bae1dSRodney W. Grimes  * reduce the value reloaded into the timer so that
739df8bae1dSRodney W. Grimes  * the timer does not drift.  This routine assumes
740df8bae1dSRodney W. Grimes  * that it is called in a context where the timers
741df8bae1dSRodney W. Grimes  * on which it is operating cannot change in value.
742df8bae1dSRodney W. Grimes  */
74326f9a767SRodney W. Grimes int
74491afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec)
745df8bae1dSRodney W. Grimes {
746df8bae1dSRodney W. Grimes 
747df8bae1dSRodney W. Grimes 	if (itp->it_value.tv_usec < usec) {
748df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_sec == 0) {
749df8bae1dSRodney W. Grimes 			/* expired, and already in next interval */
750df8bae1dSRodney W. Grimes 			usec -= itp->it_value.tv_usec;
751df8bae1dSRodney W. Grimes 			goto expire;
752df8bae1dSRodney W. Grimes 		}
753df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec += 1000000;
754df8bae1dSRodney W. Grimes 		itp->it_value.tv_sec--;
755df8bae1dSRodney W. Grimes 	}
756df8bae1dSRodney W. Grimes 	itp->it_value.tv_usec -= usec;
757df8bae1dSRodney W. Grimes 	usec = 0;
7584cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_value))
759df8bae1dSRodney W. Grimes 		return (1);
760df8bae1dSRodney W. Grimes 	/* expired, exactly at end of interval */
761df8bae1dSRodney W. Grimes expire:
7624cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_interval)) {
763df8bae1dSRodney W. Grimes 		itp->it_value = itp->it_interval;
764df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec -= usec;
765df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_usec < 0) {
766df8bae1dSRodney W. Grimes 			itp->it_value.tv_usec += 1000000;
767df8bae1dSRodney W. Grimes 			itp->it_value.tv_sec--;
768df8bae1dSRodney W. Grimes 		}
769df8bae1dSRodney W. Grimes 	} else
770df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
771df8bae1dSRodney W. Grimes 	return (0);
772df8bae1dSRodney W. Grimes }
773df8bae1dSRodney W. Grimes 
774df8bae1dSRodney W. Grimes /*
775df8bae1dSRodney W. Grimes  * Add and subtract routines for timevals.
776df8bae1dSRodney W. Grimes  * N.B.: subtract routine doesn't deal with
777df8bae1dSRodney W. Grimes  * results which are before the beginning,
778df8bae1dSRodney W. Grimes  * it just gets very confused in this case.
779df8bae1dSRodney W. Grimes  * Caveat emptor.
780df8bae1dSRodney W. Grimes  */
78126f9a767SRodney W. Grimes void
7826ff7636eSAlfred Perlstein timevaladd(struct timeval *t1, const struct timeval *t2)
783df8bae1dSRodney W. Grimes {
784df8bae1dSRodney W. Grimes 
785df8bae1dSRodney W. Grimes 	t1->tv_sec += t2->tv_sec;
786df8bae1dSRodney W. Grimes 	t1->tv_usec += t2->tv_usec;
787df8bae1dSRodney W. Grimes 	timevalfix(t1);
788df8bae1dSRodney W. Grimes }
789df8bae1dSRodney W. Grimes 
79026f9a767SRodney W. Grimes void
7916ff7636eSAlfred Perlstein timevalsub(struct timeval *t1, const struct timeval *t2)
792df8bae1dSRodney W. Grimes {
793df8bae1dSRodney W. Grimes 
794df8bae1dSRodney W. Grimes 	t1->tv_sec -= t2->tv_sec;
795df8bae1dSRodney W. Grimes 	t1->tv_usec -= t2->tv_usec;
796df8bae1dSRodney W. Grimes 	timevalfix(t1);
797df8bae1dSRodney W. Grimes }
798df8bae1dSRodney W. Grimes 
79987b6de2bSPoul-Henning Kamp static void
80091afe087SPoul-Henning Kamp timevalfix(struct timeval *t1)
801df8bae1dSRodney W. Grimes {
802df8bae1dSRodney W. Grimes 
803df8bae1dSRodney W. Grimes 	if (t1->tv_usec < 0) {
804df8bae1dSRodney W. Grimes 		t1->tv_sec--;
805df8bae1dSRodney W. Grimes 		t1->tv_usec += 1000000;
806df8bae1dSRodney W. Grimes 	}
807df8bae1dSRodney W. Grimes 	if (t1->tv_usec >= 1000000) {
808df8bae1dSRodney W. Grimes 		t1->tv_sec++;
809df8bae1dSRodney W. Grimes 		t1->tv_usec -= 1000000;
810df8bae1dSRodney W. Grimes 	}
811df8bae1dSRodney W. Grimes }
81291974ce1SSam Leffler 
81391974ce1SSam Leffler /*
814addea9d4SSam Leffler  * ratecheck(): simple time-based rate-limit checking.
81591974ce1SSam Leffler  */
81691974ce1SSam Leffler int
81791974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
81891974ce1SSam Leffler {
81991974ce1SSam Leffler 	struct timeval tv, delta;
82091974ce1SSam Leffler 	int rv = 0;
82191974ce1SSam Leffler 
822addea9d4SSam Leffler 	getmicrouptime(&tv);		/* NB: 10ms precision */
823addea9d4SSam Leffler 	delta = tv;
824addea9d4SSam Leffler 	timevalsub(&delta, lasttime);
82591974ce1SSam Leffler 
82691974ce1SSam Leffler 	/*
82791974ce1SSam Leffler 	 * check for 0,0 is so that the message will be seen at least once,
82891974ce1SSam Leffler 	 * even if interval is huge.
82991974ce1SSam Leffler 	 */
83091974ce1SSam Leffler 	if (timevalcmp(&delta, mininterval, >=) ||
83191974ce1SSam Leffler 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
83291974ce1SSam Leffler 		*lasttime = tv;
83391974ce1SSam Leffler 		rv = 1;
83491974ce1SSam Leffler 	}
83591974ce1SSam Leffler 
83691974ce1SSam Leffler 	return (rv);
83791974ce1SSam Leffler }
83891974ce1SSam Leffler 
83991974ce1SSam Leffler /*
84091974ce1SSam Leffler  * ppsratecheck(): packets (or events) per second limitation.
841addea9d4SSam Leffler  *
842addea9d4SSam Leffler  * Return 0 if the limit is to be enforced (e.g. the caller
843addea9d4SSam Leffler  * should drop a packet because of the rate limitation).
844addea9d4SSam Leffler  *
845893bec80SSam Leffler  * maxpps of 0 always causes zero to be returned.  maxpps of -1
846893bec80SSam Leffler  * always causes 1 to be returned; this effectively defeats rate
847893bec80SSam Leffler  * limiting.
848893bec80SSam Leffler  *
849addea9d4SSam Leffler  * Note that we maintain the struct timeval for compatibility
850addea9d4SSam Leffler  * with other bsd systems.  We reuse the storage and just monitor
851addea9d4SSam Leffler  * clock ticks for minimal overhead.
85291974ce1SSam Leffler  */
85391974ce1SSam Leffler int
85491974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
85591974ce1SSam Leffler {
856addea9d4SSam Leffler 	int now;
85791974ce1SSam Leffler 
85891974ce1SSam Leffler 	/*
859addea9d4SSam Leffler 	 * Reset the last time and counter if this is the first call
860addea9d4SSam Leffler 	 * or more than a second has passed since the last update of
861addea9d4SSam Leffler 	 * lasttime.
86291974ce1SSam Leffler 	 */
863addea9d4SSam Leffler 	now = ticks;
864addea9d4SSam Leffler 	if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
865addea9d4SSam Leffler 		lasttime->tv_sec = now;
866addea9d4SSam Leffler 		*curpps = 1;
867893bec80SSam Leffler 		return (maxpps != 0);
868addea9d4SSam Leffler 	} else {
869addea9d4SSam Leffler 		(*curpps)++;		/* NB: ignore potential overflow */
870addea9d4SSam Leffler 		return (maxpps < 0 || *curpps < maxpps);
871addea9d4SSam Leffler 	}
87291974ce1SSam Leffler }
87386857b36SDavid Xu 
87486857b36SDavid Xu static void
87586857b36SDavid Xu itimer_start(void)
87686857b36SDavid Xu {
87786857b36SDavid Xu 	struct kclock rt_clock = {
87886857b36SDavid Xu 		.timer_create  = realtimer_create,
87986857b36SDavid Xu 		.timer_delete  = realtimer_delete,
88086857b36SDavid Xu 		.timer_settime = realtimer_settime,
88186857b36SDavid Xu 		.timer_gettime = realtimer_gettime,
88286857b36SDavid Xu 		.event_hook     = realtimer_event_hook
88386857b36SDavid Xu 	};
88486857b36SDavid Xu 
88586857b36SDavid Xu 	itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
88686857b36SDavid Xu 		NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
88786857b36SDavid Xu 	register_posix_clock(CLOCK_REALTIME,  &rt_clock);
88886857b36SDavid Xu 	register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
88977e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
89077e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
89177e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
89286857b36SDavid Xu }
89386857b36SDavid Xu 
89486857b36SDavid Xu int
89586857b36SDavid Xu register_posix_clock(int clockid, struct kclock *clk)
89686857b36SDavid Xu {
89786857b36SDavid Xu 	if ((unsigned)clockid >= MAX_CLOCKS) {
89886857b36SDavid Xu 		printf("%s: invalid clockid\n", __func__);
89986857b36SDavid Xu 		return (0);
90086857b36SDavid Xu 	}
90186857b36SDavid Xu 	posix_clocks[clockid] = *clk;
90286857b36SDavid Xu 	return (1);
90386857b36SDavid Xu }
90486857b36SDavid Xu 
90586857b36SDavid Xu static int
90686857b36SDavid Xu itimer_init(void *mem, int size, int flags)
90786857b36SDavid Xu {
90886857b36SDavid Xu 	struct itimer *it;
90986857b36SDavid Xu 
91086857b36SDavid Xu 	it = (struct itimer *)mem;
91186857b36SDavid Xu 	mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
91286857b36SDavid Xu 	return (0);
91386857b36SDavid Xu }
91486857b36SDavid Xu 
91586857b36SDavid Xu static void
91686857b36SDavid Xu itimer_fini(void *mem, int size)
91786857b36SDavid Xu {
91886857b36SDavid Xu 	struct itimer *it;
91986857b36SDavid Xu 
92086857b36SDavid Xu 	it = (struct itimer *)mem;
92186857b36SDavid Xu 	mtx_destroy(&it->it_mtx);
92286857b36SDavid Xu }
92386857b36SDavid Xu 
92486857b36SDavid Xu static void
92586857b36SDavid Xu itimer_enter(struct itimer *it)
92686857b36SDavid Xu {
92786857b36SDavid Xu 
92886857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
92986857b36SDavid Xu 	it->it_usecount++;
93086857b36SDavid Xu }
93186857b36SDavid Xu 
93286857b36SDavid Xu static void
93386857b36SDavid Xu itimer_leave(struct itimer *it)
93486857b36SDavid Xu {
93586857b36SDavid Xu 
93686857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
93786857b36SDavid Xu 	KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
93886857b36SDavid Xu 
93986857b36SDavid Xu 	if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
94086857b36SDavid Xu 		wakeup(it);
94186857b36SDavid Xu }
94286857b36SDavid Xu 
94386857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
94486857b36SDavid Xu struct timer_create_args {
94586857b36SDavid Xu 	clockid_t clock_id;
94686857b36SDavid Xu 	struct sigevent * evp;
94786857b36SDavid Xu 	timer_t * timerid;
94886857b36SDavid Xu };
94986857b36SDavid Xu #endif
95086857b36SDavid Xu 
95186857b36SDavid Xu int
95286857b36SDavid Xu timer_create(struct thread *td, struct timer_create_args *uap)
95386857b36SDavid Xu {
95486857b36SDavid Xu 	struct sigevent *evp1, ev;
95586857b36SDavid Xu 	timer_t id;
95686857b36SDavid Xu 	int error;
95786857b36SDavid Xu 
95886857b36SDavid Xu 	if (uap->evp != NULL) {
95986857b36SDavid Xu 		error = copyin(uap->evp, &ev, sizeof(ev));
96086857b36SDavid Xu 		if (error != 0)
96186857b36SDavid Xu 			return (error);
96286857b36SDavid Xu 		evp1 = &ev;
96386857b36SDavid Xu 	} else
96486857b36SDavid Xu 		evp1 = NULL;
96586857b36SDavid Xu 
96686857b36SDavid Xu 	error = kern_timer_create(td, uap->clock_id, evp1, &id, -1);
96786857b36SDavid Xu 
96886857b36SDavid Xu 	if (error == 0) {
96986857b36SDavid Xu 		error = copyout(&id, uap->timerid, sizeof(timer_t));
97086857b36SDavid Xu 		if (error != 0)
97186857b36SDavid Xu 			kern_timer_delete(td, id);
97286857b36SDavid Xu 	}
97386857b36SDavid Xu 	return (error);
97486857b36SDavid Xu }
97586857b36SDavid Xu 
97686857b36SDavid Xu static int
97786857b36SDavid Xu kern_timer_create(struct thread *td, clockid_t clock_id,
97886857b36SDavid Xu 	struct sigevent *evp, timer_t *timerid, timer_t preset_id)
97986857b36SDavid Xu {
98086857b36SDavid Xu 	struct proc *p = td->td_proc;
98186857b36SDavid Xu 	struct itimer *it;
98286857b36SDavid Xu 	int id;
98386857b36SDavid Xu 	int error;
98486857b36SDavid Xu 
98586857b36SDavid Xu 	if (clock_id < 0 || clock_id >= MAX_CLOCKS)
98686857b36SDavid Xu 		return (EINVAL);
98786857b36SDavid Xu 
98886857b36SDavid Xu 	if (posix_clocks[clock_id].timer_create == NULL)
98986857b36SDavid Xu 		return (EINVAL);
99086857b36SDavid Xu 
99186857b36SDavid Xu 	if (evp != NULL) {
99286857b36SDavid Xu 		if (evp->sigev_notify != SIGEV_NONE &&
99356c06c4bSDavid Xu 		    evp->sigev_notify != SIGEV_SIGNAL &&
99456c06c4bSDavid Xu 		    evp->sigev_notify != SIGEV_THREAD_ID)
99586857b36SDavid Xu 			return (EINVAL);
99656c06c4bSDavid Xu 		if ((evp->sigev_notify == SIGEV_SIGNAL ||
99756c06c4bSDavid Xu 		     evp->sigev_notify == SIGEV_THREAD_ID) &&
99886857b36SDavid Xu 			!_SIG_VALID(evp->sigev_signo))
99986857b36SDavid Xu 			return (EINVAL);
100086857b36SDavid Xu 	}
100186857b36SDavid Xu 
100260354683SDavid Xu 	if (p->p_itimers == NULL)
100386857b36SDavid Xu 		itimers_alloc(p);
100486857b36SDavid Xu 
100586857b36SDavid Xu 	it = uma_zalloc(itimer_zone, M_WAITOK);
100686857b36SDavid Xu 	it->it_flags = 0;
100786857b36SDavid Xu 	it->it_usecount = 0;
100886857b36SDavid Xu 	it->it_active = 0;
100956c06c4bSDavid Xu 	timespecclear(&it->it_time.it_value);
101056c06c4bSDavid Xu 	timespecclear(&it->it_time.it_interval);
101186857b36SDavid Xu 	it->it_overrun = 0;
101286857b36SDavid Xu 	it->it_overrun_last = 0;
101386857b36SDavid Xu 	it->it_clockid = clock_id;
101486857b36SDavid Xu 	it->it_timerid = -1;
101586857b36SDavid Xu 	it->it_proc = p;
101686857b36SDavid Xu 	ksiginfo_init(&it->it_ksi);
101786857b36SDavid Xu 	it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
101886857b36SDavid Xu 	error = CLOCK_CALL(clock_id, timer_create, (it));
101986857b36SDavid Xu 	if (error != 0)
102086857b36SDavid Xu 		goto out;
102186857b36SDavid Xu 
102286857b36SDavid Xu 	PROC_LOCK(p);
102386857b36SDavid Xu 	if (preset_id != -1) {
102486857b36SDavid Xu 		KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
102586857b36SDavid Xu 		id = preset_id;
102660354683SDavid Xu 		if (p->p_itimers->its_timers[id] != NULL) {
102786857b36SDavid Xu 			PROC_UNLOCK(p);
102886857b36SDavid Xu 			error = 0;
102986857b36SDavid Xu 			goto out;
103086857b36SDavid Xu 		}
103186857b36SDavid Xu 	} else {
103286857b36SDavid Xu 		/*
103386857b36SDavid Xu 		 * Find a free timer slot, skipping those reserved
103486857b36SDavid Xu 		 * for setitimer().
103586857b36SDavid Xu 		 */
103686857b36SDavid Xu 		for (id = 3; id < TIMER_MAX; id++)
103760354683SDavid Xu 			if (p->p_itimers->its_timers[id] == NULL)
103886857b36SDavid Xu 				break;
103986857b36SDavid Xu 		if (id == TIMER_MAX) {
104086857b36SDavid Xu 			PROC_UNLOCK(p);
104186857b36SDavid Xu 			error = EAGAIN;
104286857b36SDavid Xu 			goto out;
104386857b36SDavid Xu 		}
104486857b36SDavid Xu 	}
104586857b36SDavid Xu 	it->it_timerid = id;
104660354683SDavid Xu 	p->p_itimers->its_timers[id] = it;
104786857b36SDavid Xu 	if (evp != NULL)
104886857b36SDavid Xu 		it->it_sigev = *evp;
104986857b36SDavid Xu 	else {
105086857b36SDavid Xu 		it->it_sigev.sigev_notify = SIGEV_SIGNAL;
105186857b36SDavid Xu 		switch (clock_id) {
105286857b36SDavid Xu 		default:
105386857b36SDavid Xu 		case CLOCK_REALTIME:
105486857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGALRM;
105586857b36SDavid Xu 			break;
105686857b36SDavid Xu 		case CLOCK_VIRTUAL:
105786857b36SDavid Xu  			it->it_sigev.sigev_signo = SIGVTALRM;
105886857b36SDavid Xu 			break;
105986857b36SDavid Xu 		case CLOCK_PROF:
106086857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGPROF;
106186857b36SDavid Xu 			break;
106286857b36SDavid Xu 		}
10638f0371f1SDavid Xu 		it->it_sigev.sigev_value.sival_int = id;
106486857b36SDavid Xu 	}
106586857b36SDavid Xu 
106656c06c4bSDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
106756c06c4bSDavid Xu 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
106886857b36SDavid Xu 		it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
106986857b36SDavid Xu 		it->it_ksi.ksi_code = SI_TIMER;
107086857b36SDavid Xu 		it->it_ksi.ksi_value = it->it_sigev.sigev_value;
107186857b36SDavid Xu 		it->it_ksi.ksi_timerid = id;
107286857b36SDavid Xu 	}
107386857b36SDavid Xu 	PROC_UNLOCK(p);
107486857b36SDavid Xu 	*timerid = id;
107586857b36SDavid Xu 	return (0);
107686857b36SDavid Xu 
107786857b36SDavid Xu out:
107886857b36SDavid Xu 	ITIMER_LOCK(it);
107986857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
108086857b36SDavid Xu 	ITIMER_UNLOCK(it);
108186857b36SDavid Xu 	uma_zfree(itimer_zone, it);
108286857b36SDavid Xu 	return (error);
108386857b36SDavid Xu }
108486857b36SDavid Xu 
108586857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
108686857b36SDavid Xu struct timer_delete_args {
108786857b36SDavid Xu 	timer_t timerid;
108886857b36SDavid Xu };
108986857b36SDavid Xu #endif
109086857b36SDavid Xu 
109186857b36SDavid Xu int
109286857b36SDavid Xu timer_delete(struct thread *td, struct timer_delete_args *uap)
109386857b36SDavid Xu {
109486857b36SDavid Xu 	return (kern_timer_delete(td, uap->timerid));
109586857b36SDavid Xu }
109686857b36SDavid Xu 
109786857b36SDavid Xu static struct itimer *
109886857b36SDavid Xu itimer_find(struct proc *p, timer_t timerid, int include_deleting)
109986857b36SDavid Xu {
110086857b36SDavid Xu 	struct itimer *it;
110186857b36SDavid Xu 
110286857b36SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
110360354683SDavid Xu 	if ((p->p_itimers == NULL) || (timerid >= TIMER_MAX) ||
110460354683SDavid Xu 	    (it = p->p_itimers->its_timers[timerid]) == NULL) {
110586857b36SDavid Xu 		return (NULL);
110686857b36SDavid Xu 	}
110786857b36SDavid Xu 	ITIMER_LOCK(it);
110886857b36SDavid Xu 	if (!include_deleting && (it->it_flags & ITF_DELETING) != 0) {
110986857b36SDavid Xu 		ITIMER_UNLOCK(it);
111086857b36SDavid Xu 		it = NULL;
111186857b36SDavid Xu 	}
111286857b36SDavid Xu 	return (it);
111386857b36SDavid Xu }
111486857b36SDavid Xu 
111586857b36SDavid Xu static int
111686857b36SDavid Xu kern_timer_delete(struct thread *td, timer_t timerid)
111786857b36SDavid Xu {
111886857b36SDavid Xu 	struct proc *p = td->td_proc;
111986857b36SDavid Xu 	struct itimer *it;
112086857b36SDavid Xu 
112186857b36SDavid Xu 	PROC_LOCK(p);
112286857b36SDavid Xu 	it = itimer_find(p, timerid, 0);
112386857b36SDavid Xu 	if (it == NULL) {
112486857b36SDavid Xu 		PROC_UNLOCK(p);
112586857b36SDavid Xu 		return (EINVAL);
112686857b36SDavid Xu 	}
112786857b36SDavid Xu 	PROC_UNLOCK(p);
112886857b36SDavid Xu 
112986857b36SDavid Xu 	it->it_flags |= ITF_DELETING;
113086857b36SDavid Xu 	while (it->it_usecount > 0) {
113186857b36SDavid Xu 		it->it_flags |= ITF_WANTED;
113286857b36SDavid Xu 		msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
113386857b36SDavid Xu 	}
113486857b36SDavid Xu 	it->it_flags &= ~ITF_WANTED;
113586857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
113686857b36SDavid Xu 	ITIMER_UNLOCK(it);
113786857b36SDavid Xu 
113886857b36SDavid Xu 	PROC_LOCK(p);
113986857b36SDavid Xu 	if (KSI_ONQ(&it->it_ksi))
114086857b36SDavid Xu 		sigqueue_take(&it->it_ksi);
114160354683SDavid Xu 	p->p_itimers->its_timers[timerid] = NULL;
114286857b36SDavid Xu 	PROC_UNLOCK(p);
114386857b36SDavid Xu 	uma_zfree(itimer_zone, it);
114486857b36SDavid Xu 	return (0);
114586857b36SDavid Xu }
114686857b36SDavid Xu 
114786857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
114886857b36SDavid Xu struct timer_settime_args {
114986857b36SDavid Xu 	timer_t timerid;
115086857b36SDavid Xu 	int flags;
115186857b36SDavid Xu 	const struct itimerspec * value;
115286857b36SDavid Xu 	struct itimerspec * ovalue;
115386857b36SDavid Xu };
115486857b36SDavid Xu #endif
115586857b36SDavid Xu 
115686857b36SDavid Xu int
115786857b36SDavid Xu timer_settime(struct thread *td, struct timer_settime_args *uap)
115886857b36SDavid Xu {
115986857b36SDavid Xu 	struct proc *p = td->td_proc;
116086857b36SDavid Xu 	struct itimer *it;
116186857b36SDavid Xu 	struct itimerspec val, oval, *ovalp;
116286857b36SDavid Xu 	int error;
116386857b36SDavid Xu 
116486857b36SDavid Xu 	error = copyin(uap->value, &val, sizeof(val));
116586857b36SDavid Xu 	if (error != 0)
116686857b36SDavid Xu 		return (error);
116786857b36SDavid Xu 
116886857b36SDavid Xu 	if (uap->ovalue != NULL)
116986857b36SDavid Xu 		ovalp = &oval;
117086857b36SDavid Xu 	else
117186857b36SDavid Xu 		ovalp = NULL;
117286857b36SDavid Xu 
117386857b36SDavid Xu 	PROC_LOCK(p);
117486857b36SDavid Xu 	if (uap->timerid < 3 ||
117586857b36SDavid Xu 	    (it = itimer_find(p, uap->timerid, 0)) == NULL) {
117686857b36SDavid Xu 		PROC_UNLOCK(p);
117786857b36SDavid Xu 		error = EINVAL;
117886857b36SDavid Xu 	} else {
117986857b36SDavid Xu 		PROC_UNLOCK(p);
118086857b36SDavid Xu 		itimer_enter(it);
118186857b36SDavid Xu 		error = CLOCK_CALL(it->it_clockid, timer_settime,
118286857b36SDavid Xu 				(it, uap->flags, &val, ovalp));
118386857b36SDavid Xu 		itimer_leave(it);
118486857b36SDavid Xu 		ITIMER_UNLOCK(it);
118586857b36SDavid Xu 	}
118686857b36SDavid Xu 	if (error == 0 && uap->ovalue != NULL)
118786857b36SDavid Xu 		error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
118886857b36SDavid Xu 	return (error);
118986857b36SDavid Xu }
119086857b36SDavid Xu 
119186857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
119286857b36SDavid Xu struct timer_gettime_args {
119386857b36SDavid Xu 	timer_t timerid;
119486857b36SDavid Xu 	struct itimerspec * value;
119586857b36SDavid Xu };
119686857b36SDavid Xu #endif
119786857b36SDavid Xu 
119886857b36SDavid Xu int
119986857b36SDavid Xu timer_gettime(struct thread *td, struct timer_gettime_args *uap)
120086857b36SDavid Xu {
120186857b36SDavid Xu 	struct proc *p = td->td_proc;
120286857b36SDavid Xu 	struct itimer *it;
120386857b36SDavid Xu 	struct itimerspec val;
120486857b36SDavid Xu 	int error;
120586857b36SDavid Xu 
120686857b36SDavid Xu 	PROC_LOCK(p);
120786857b36SDavid Xu 	if (uap->timerid < 3 ||
120886857b36SDavid Xu 	   (it = itimer_find(p, uap->timerid, 0)) == NULL) {
120986857b36SDavid Xu 		PROC_UNLOCK(p);
121086857b36SDavid Xu 		error = EINVAL;
121186857b36SDavid Xu 	} else {
121286857b36SDavid Xu 		PROC_UNLOCK(p);
121386857b36SDavid Xu 		itimer_enter(it);
121486857b36SDavid Xu 		error = CLOCK_CALL(it->it_clockid, timer_gettime,
121586857b36SDavid Xu 				(it, &val));
121686857b36SDavid Xu 		itimer_leave(it);
121786857b36SDavid Xu 		ITIMER_UNLOCK(it);
121886857b36SDavid Xu 	}
121986857b36SDavid Xu 	if (error == 0)
122086857b36SDavid Xu 		error = copyout(&val, uap->value, sizeof(val));
122186857b36SDavid Xu 	return (error);
122286857b36SDavid Xu }
122386857b36SDavid Xu 
122486857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
122586857b36SDavid Xu struct timer_getoverrun_args {
122686857b36SDavid Xu 	timer_t timerid;
122786857b36SDavid Xu };
122886857b36SDavid Xu #endif
122986857b36SDavid Xu 
123086857b36SDavid Xu int
123186857b36SDavid Xu timer_getoverrun(struct thread *td, struct timer_getoverrun_args *uap)
123286857b36SDavid Xu {
123386857b36SDavid Xu 	struct proc *p = td->td_proc;
123486857b36SDavid Xu 	struct itimer *it;
123586857b36SDavid Xu 	int error ;
123686857b36SDavid Xu 
123786857b36SDavid Xu 	PROC_LOCK(p);
123886857b36SDavid Xu 	if (uap->timerid < 3 ||
123986857b36SDavid Xu 	    (it = itimer_find(p, uap->timerid, 0)) == NULL) {
124086857b36SDavid Xu 		PROC_UNLOCK(p);
124186857b36SDavid Xu 		error = EINVAL;
124286857b36SDavid Xu 	} else {
124386857b36SDavid Xu 		td->td_retval[0] = it->it_overrun_last;
124486857b36SDavid Xu 		ITIMER_UNLOCK(it);
124556c06c4bSDavid Xu 		PROC_UNLOCK(p);
124686857b36SDavid Xu 		error = 0;
124786857b36SDavid Xu 	}
124886857b36SDavid Xu 	return (error);
124986857b36SDavid Xu }
125086857b36SDavid Xu 
125186857b36SDavid Xu static int
125286857b36SDavid Xu realtimer_create(struct itimer *it)
125386857b36SDavid Xu {
125486857b36SDavid Xu 	callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
125586857b36SDavid Xu 	return (0);
125686857b36SDavid Xu }
125786857b36SDavid Xu 
125886857b36SDavid Xu static int
125986857b36SDavid Xu realtimer_delete(struct itimer *it)
126086857b36SDavid Xu {
126186857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
126286857b36SDavid Xu 	callout_stop(&it->it_callout);
126386857b36SDavid Xu 	return (0);
126486857b36SDavid Xu }
126586857b36SDavid Xu 
126686857b36SDavid Xu static int
126786857b36SDavid Xu realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
126886857b36SDavid Xu {
126956c06c4bSDavid Xu 	struct timespec cts;
127086857b36SDavid Xu 
127186857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
127286857b36SDavid Xu 
127356c06c4bSDavid Xu 	realtimer_clocktime(it->it_clockid, &cts);
127456c06c4bSDavid Xu 	*ovalue = it->it_time;
127586857b36SDavid Xu 	if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
127656c06c4bSDavid Xu 		timespecsub(&ovalue->it_value, &cts);
127786857b36SDavid Xu 		if (ovalue->it_value.tv_sec < 0 ||
127886857b36SDavid Xu 		    (ovalue->it_value.tv_sec == 0 &&
127986857b36SDavid Xu 		     ovalue->it_value.tv_nsec == 0)) {
128086857b36SDavid Xu 			ovalue->it_value.tv_sec  = 0;
128186857b36SDavid Xu 			ovalue->it_value.tv_nsec = 1;
128286857b36SDavid Xu 		}
128386857b36SDavid Xu 	}
128486857b36SDavid Xu 	return (0);
128586857b36SDavid Xu }
128686857b36SDavid Xu 
128786857b36SDavid Xu static int
128886857b36SDavid Xu realtimer_settime(struct itimer *it, int flags,
128986857b36SDavid Xu 	struct itimerspec *value, struct itimerspec *ovalue)
129086857b36SDavid Xu {
129156c06c4bSDavid Xu 	struct timespec cts, ts;
129256c06c4bSDavid Xu 	struct timeval tv;
129356c06c4bSDavid Xu 	struct itimerspec val;
129486857b36SDavid Xu 
129586857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
129686857b36SDavid Xu 
129756c06c4bSDavid Xu 	val = *value;
129856c06c4bSDavid Xu 	if (itimespecfix(&val.it_value))
129986857b36SDavid Xu 		return (EINVAL);
130086857b36SDavid Xu 
130156c06c4bSDavid Xu 	if (timespecisset(&val.it_value)) {
130256c06c4bSDavid Xu 		if (itimespecfix(&val.it_interval))
130386857b36SDavid Xu 			return (EINVAL);
130486857b36SDavid Xu 	} else {
130556c06c4bSDavid Xu 		timespecclear(&val.it_interval);
130686857b36SDavid Xu 	}
130786857b36SDavid Xu 
130886857b36SDavid Xu 	if (ovalue != NULL)
130986857b36SDavid Xu 		realtimer_gettime(it, ovalue);
131086857b36SDavid Xu 
131186857b36SDavid Xu 	it->it_time = val;
131256c06c4bSDavid Xu 	if (timespecisset(&val.it_value)) {
131356c06c4bSDavid Xu 		realtimer_clocktime(it->it_clockid, &cts);
131456c06c4bSDavid Xu 		ts = val.it_value;
131586857b36SDavid Xu 		if ((flags & TIMER_ABSTIME) == 0) {
131686857b36SDavid Xu 			/* Convert to absolute time. */
131756c06c4bSDavid Xu 			timespecadd(&it->it_time.it_value, &cts);
131886857b36SDavid Xu 		} else {
131956c06c4bSDavid Xu 			timespecsub(&ts, &cts);
132086857b36SDavid Xu 			/*
132156c06c4bSDavid Xu 			 * We don't care if ts is negative, tztohz will
132286857b36SDavid Xu 			 * fix it.
132386857b36SDavid Xu 			 */
132486857b36SDavid Xu 		}
132556c06c4bSDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
132656c06c4bSDavid Xu 		callout_reset(&it->it_callout, tvtohz(&tv),
132786857b36SDavid Xu 			realtimer_expire, it);
132886857b36SDavid Xu 	} else {
132986857b36SDavid Xu 		callout_stop(&it->it_callout);
133086857b36SDavid Xu 	}
133186857b36SDavid Xu 
133286857b36SDavid Xu 	return (0);
133386857b36SDavid Xu }
133486857b36SDavid Xu 
133586857b36SDavid Xu static void
133656c06c4bSDavid Xu realtimer_clocktime(clockid_t id, struct timespec *ts)
133786857b36SDavid Xu {
133886857b36SDavid Xu 	if (id == CLOCK_REALTIME)
133956c06c4bSDavid Xu 		getnanotime(ts);
134086857b36SDavid Xu 	else	/* CLOCK_MONOTONIC */
134156c06c4bSDavid Xu 		getnanouptime(ts);
134256c06c4bSDavid Xu }
134356c06c4bSDavid Xu 
134456c06c4bSDavid Xu int
134556c06c4bSDavid Xu itimer_accept(struct proc *p, timer_t timerid, ksiginfo_t *ksi)
134656c06c4bSDavid Xu {
134756c06c4bSDavid Xu 	struct itimer *it;
134856c06c4bSDavid Xu 
134956c06c4bSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
135056c06c4bSDavid Xu 	it = itimer_find(p, timerid, 0);
135156c06c4bSDavid Xu 	if (it != NULL) {
135256c06c4bSDavid Xu 		ksi->ksi_overrun = it->it_overrun;
135356c06c4bSDavid Xu 		it->it_overrun_last = it->it_overrun;
135456c06c4bSDavid Xu 		it->it_overrun = 0;
135556c06c4bSDavid Xu 		ITIMER_UNLOCK(it);
135656c06c4bSDavid Xu 		return (0);
135756c06c4bSDavid Xu 	}
135856c06c4bSDavid Xu 	return (EINVAL);
135956c06c4bSDavid Xu }
136056c06c4bSDavid Xu 
136156c06c4bSDavid Xu int
136256c06c4bSDavid Xu itimespecfix(struct timespec *ts)
136356c06c4bSDavid Xu {
136456c06c4bSDavid Xu 
136556c06c4bSDavid Xu 	if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
136656c06c4bSDavid Xu 		return (EINVAL);
136756c06c4bSDavid Xu 	if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
136856c06c4bSDavid Xu 		ts->tv_nsec = tick * 1000;
136956c06c4bSDavid Xu 	return (0);
137086857b36SDavid Xu }
137186857b36SDavid Xu 
137286857b36SDavid Xu static void
137386857b36SDavid Xu realtimer_event_hook(struct proc *p, clockid_t clock_id, int event)
137486857b36SDavid Xu {
137586857b36SDavid Xu 	struct itimers *its;
137686857b36SDavid Xu 	struct itimer  *it;
137786857b36SDavid Xu 	int i;
137886857b36SDavid Xu 
137986857b36SDavid Xu 	/*
138086857b36SDavid Xu 	 * Timer 0 (ITIMER_REAL) is XSI interval timer, according to POSIX
138186857b36SDavid Xu 	 * specification, it should be inherited by new process image.
138286857b36SDavid Xu 	 */
138386857b36SDavid Xu 	if (event == ITIMER_EV_EXEC)
138486857b36SDavid Xu 		i = 1;
138586857b36SDavid Xu 	else
138686857b36SDavid Xu 		i = 0;
138760354683SDavid Xu 	its = p->p_itimers;
138886857b36SDavid Xu 	for (; i < TIMER_MAX; i++) {
138986857b36SDavid Xu 		if ((it = its->its_timers[i]) != NULL &&
139086857b36SDavid Xu 		     it->it_clockid == clock_id) {
139186857b36SDavid Xu 			ITIMER_LOCK(it);
139286857b36SDavid Xu 			callout_stop(&it->it_callout);
139386857b36SDavid Xu 			ITIMER_UNLOCK(it);
139486857b36SDavid Xu 		}
139586857b36SDavid Xu 	}
139686857b36SDavid Xu }
139786857b36SDavid Xu 
139886857b36SDavid Xu /* Timeout callback for realtime timer */
139986857b36SDavid Xu static void
140086857b36SDavid Xu realtimer_expire(void *arg)
140186857b36SDavid Xu {
140256c06c4bSDavid Xu 	struct timespec cts, ts;
140356c06c4bSDavid Xu 	struct timeval tv;
140486857b36SDavid Xu 	struct itimer *it;
140586857b36SDavid Xu 	struct proc *p;
140686857b36SDavid Xu 
140786857b36SDavid Xu 	it = (struct itimer *)arg;
140886857b36SDavid Xu 	p = it->it_proc;
140986857b36SDavid Xu 
141056c06c4bSDavid Xu 	realtimer_clocktime(it->it_clockid, &cts);
141186857b36SDavid Xu 	/* Only fire if time is reached. */
141256c06c4bSDavid Xu 	if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
141356c06c4bSDavid Xu 		if (timespecisset(&it->it_time.it_interval)) {
141456c06c4bSDavid Xu 			timespecadd(&it->it_time.it_value,
141586857b36SDavid Xu 				    &it->it_time.it_interval);
141656c06c4bSDavid Xu 			while (timespeccmp(&cts, &it->it_time.it_value, >=)) {
141777e718f7SDavid Xu 				if (it->it_overrun < INT_MAX)
141886857b36SDavid Xu 					it->it_overrun++;
141977e718f7SDavid Xu 				else
142077e718f7SDavid Xu 					it->it_ksi.ksi_errno = ERANGE;
142156c06c4bSDavid Xu 				timespecadd(&it->it_time.it_value,
142286857b36SDavid Xu 					    &it->it_time.it_interval);
142386857b36SDavid Xu 			}
142486857b36SDavid Xu 		} else {
142586857b36SDavid Xu 			/* single shot timer ? */
142656c06c4bSDavid Xu 			timespecclear(&it->it_time.it_value);
142786857b36SDavid Xu 		}
142856c06c4bSDavid Xu 		if (timespecisset(&it->it_time.it_value)) {
142956c06c4bSDavid Xu 			ts = it->it_time.it_value;
143056c06c4bSDavid Xu 			timespecsub(&ts, &cts);
143156c06c4bSDavid Xu 			TIMESPEC_TO_TIMEVAL(&tv, &ts);
143256c06c4bSDavid Xu 			callout_reset(&it->it_callout, tvtohz(&tv),
143386857b36SDavid Xu 				 realtimer_expire, it);
143486857b36SDavid Xu 		}
143586857b36SDavid Xu 		ITIMER_UNLOCK(it);
143686857b36SDavid Xu 		itimer_fire(it);
143786857b36SDavid Xu 		ITIMER_LOCK(it);
143856c06c4bSDavid Xu 	} else if (timespecisset(&it->it_time.it_value)) {
143956c06c4bSDavid Xu 		ts = it->it_time.it_value;
144056c06c4bSDavid Xu 		timespecsub(&ts, &cts);
144156c06c4bSDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
144256c06c4bSDavid Xu 		callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
144386857b36SDavid Xu  			it);
144486857b36SDavid Xu 	}
144586857b36SDavid Xu }
144686857b36SDavid Xu 
144786857b36SDavid Xu void
144886857b36SDavid Xu itimer_fire(struct itimer *it)
144986857b36SDavid Xu {
145086857b36SDavid Xu 	struct proc *p = it->it_proc;
14516d7b314bSDavid Xu 	int ret;
145286857b36SDavid Xu 
145356c06c4bSDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
145456c06c4bSDavid Xu 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
145586857b36SDavid Xu 		PROC_LOCK(p);
14566d7b314bSDavid Xu 		if (!KSI_ONQ(&it->it_ksi)) {
145777e718f7SDavid Xu 			it->it_ksi.ksi_errno = 0;
14586d7b314bSDavid Xu 			ret = psignal_event(p, &it->it_sigev, &it->it_ksi);
14596d7b314bSDavid Xu 			if (__predict_false(ret != 0)) {
146086857b36SDavid Xu 				it->it_overrun++;
146156c06c4bSDavid Xu 				/*
146256c06c4bSDavid Xu 				 * Broken userland code, thread went
146356c06c4bSDavid Xu 				 * away, disarm the timer.
146456c06c4bSDavid Xu 				 */
14656d7b314bSDavid Xu 				if (ret == ESRCH) {
146656c06c4bSDavid Xu 					ITIMER_LOCK(it);
146756c06c4bSDavid Xu 					timespecclear(&it->it_time.it_value);
146856c06c4bSDavid Xu 					timespecclear(&it->it_time.it_interval);
146956c06c4bSDavid Xu 					callout_stop(&it->it_callout);
147056c06c4bSDavid Xu 					ITIMER_UNLOCK(it);
14716d7b314bSDavid Xu 				}
147256c06c4bSDavid Xu 			}
147356c06c4bSDavid Xu 		} else {
147477e718f7SDavid Xu 			if (it->it_overrun < INT_MAX)
14756d7b314bSDavid Xu 				it->it_overrun++;
147677e718f7SDavid Xu 			else
147777e718f7SDavid Xu 				it->it_ksi.ksi_errno = ERANGE;
147856c06c4bSDavid Xu 		}
147986857b36SDavid Xu 		PROC_UNLOCK(p);
148086857b36SDavid Xu 	}
148186857b36SDavid Xu }
148286857b36SDavid Xu 
148386857b36SDavid Xu static void
148486857b36SDavid Xu itimers_alloc(struct proc *p)
148586857b36SDavid Xu {
148660354683SDavid Xu 	struct itimers *its;
148760354683SDavid Xu 	int i;
148886857b36SDavid Xu 
148960354683SDavid Xu 	its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
149086857b36SDavid Xu 	LIST_INIT(&its->its_virtual);
149186857b36SDavid Xu 	LIST_INIT(&its->its_prof);
149286857b36SDavid Xu 	TAILQ_INIT(&its->its_worklist);
149360354683SDavid Xu 	for (i = 0; i < TIMER_MAX; i++)
149460354683SDavid Xu 		its->its_timers[i] = NULL;
149560354683SDavid Xu 	PROC_LOCK(p);
149660354683SDavid Xu 	if (p->p_itimers == NULL) {
149760354683SDavid Xu 		p->p_itimers = its;
149860354683SDavid Xu 		PROC_UNLOCK(p);
149960354683SDavid Xu 	}
150060354683SDavid Xu 	else {
150160354683SDavid Xu 		PROC_UNLOCK(p);
150260354683SDavid Xu 		free(its, M_SUBPROC);
150360354683SDavid Xu 	}
150486857b36SDavid Xu }
150586857b36SDavid Xu 
150686857b36SDavid Xu /* Clean up timers when some process events are being triggered. */
150786857b36SDavid Xu void
150886857b36SDavid Xu itimers_event_hook(struct proc *p, int event)
150986857b36SDavid Xu {
151086857b36SDavid Xu 	struct itimers *its;
151186857b36SDavid Xu 	struct itimer *it;
151286857b36SDavid Xu 	int i;
151386857b36SDavid Xu 
151460354683SDavid Xu 	if (p->p_itimers != NULL) {
151560354683SDavid Xu 		its = p->p_itimers;
151686857b36SDavid Xu 		for (i = 0; i < MAX_CLOCKS; ++i) {
151786857b36SDavid Xu 			if (posix_clocks[i].event_hook != NULL)
151886857b36SDavid Xu 				CLOCK_CALL(i, event_hook, (p, i, event));
151986857b36SDavid Xu 		}
152086857b36SDavid Xu 		/*
152186857b36SDavid Xu 		 * According to susv3, XSI interval timers should be inherited
152286857b36SDavid Xu 		 * by new image.
152386857b36SDavid Xu 		 */
152486857b36SDavid Xu 		if (event == ITIMER_EV_EXEC)
152586857b36SDavid Xu 			i = 3;
152686857b36SDavid Xu 		else if (event == ITIMER_EV_EXIT)
152786857b36SDavid Xu 			i = 0;
152886857b36SDavid Xu 		else
152986857b36SDavid Xu 			panic("unhandled event");
153086857b36SDavid Xu 		for (; i < TIMER_MAX; ++i) {
153186857b36SDavid Xu 			if ((it = its->its_timers[i]) != NULL) {
153286857b36SDavid Xu 				PROC_LOCK(p);
153386857b36SDavid Xu 				if (KSI_ONQ(&it->it_ksi))
153486857b36SDavid Xu 					sigqueue_take(&it->it_ksi);
153586857b36SDavid Xu 				PROC_UNLOCK(p);
153686857b36SDavid Xu 				uma_zfree(itimer_zone, its->its_timers[i]);
153786857b36SDavid Xu 				its->its_timers[i] = NULL;
153886857b36SDavid Xu 			}
153986857b36SDavid Xu 		}
154086857b36SDavid Xu 		if (its->its_timers[0] == NULL &&
154186857b36SDavid Xu 		    its->its_timers[1] == NULL &&
154286857b36SDavid Xu 		    its->its_timers[2] == NULL) {
154360354683SDavid Xu 			free(its, M_SUBPROC);
154460354683SDavid Xu 			p->p_itimers = NULL;
154586857b36SDavid Xu 		}
154686857b36SDavid Xu 	}
154786857b36SDavid Xu }
1548