xref: /freebsd/sys/kern/kern_time.c (revision f645b0b51cd22b129405c2c32dba1d051ae3d99c)
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>
39aff5bcb1SDavid Xu #include <sys/limits.h>
40f645b0b5SPoul-Henning Kamp #include <sys/clock.h>
41fb919e4dSMark Murray #include <sys/lock.h>
42fb919e4dSMark Murray #include <sys/mutex.h>
43d2d3e875SBruce Evans #include <sys/sysproto.h>
44d26b1a1fSDavid Xu #include <sys/eventhandler.h>
45df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
46797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
47df8bae1dSRodney W. Grimes #include <sys/kernel.h>
484b8d5f2dSRobert Watson #include <sys/mac.h>
49efa42cbcSPaul Saab #include <sys/syscallsubr.h>
5077e718f7SDavid Xu #include <sys/sysctl.h>
5194c8fcd8SPeter Wemm #include <sys/sysent.h>
52df8bae1dSRodney W. Grimes #include <sys/proc.h>
53708e7684SPeter Wemm #include <sys/time.h>
5486857b36SDavid Xu #include <sys/timers.h>
5591266b96SPoul-Henning Kamp #include <sys/timetc.h>
56df8bae1dSRodney W. Grimes #include <sys/vnode.h>
57fb919e4dSMark Murray 
5877e718f7SDavid Xu #include <posix4/posix4.h>
5977e718f7SDavid Xu 
605b870b7bSPeter Wemm #include <vm/vm.h>
615b870b7bSPeter Wemm #include <vm/vm_extern.h>
62df8bae1dSRodney W. Grimes 
6386857b36SDavid Xu #define MAX_CLOCKS 	(CLOCK_MONOTONIC+1)
6486857b36SDavid Xu 
6591f1c2b3SPoul-Henning Kamp int tz_minuteswest;
6691f1c2b3SPoul-Henning Kamp int tz_dsttime;
67ac7e6123SDavid Greenman 
6886857b36SDavid Xu static struct kclock	posix_clocks[MAX_CLOCKS];
6986857b36SDavid Xu static uma_zone_t	itimer_zone = NULL;
7086857b36SDavid Xu 
71df8bae1dSRodney W. Grimes /*
72df8bae1dSRodney W. Grimes  * Time of day and interval timer support.
73df8bae1dSRodney W. Grimes  *
74df8bae1dSRodney W. Grimes  * These routines provide the kernel entry points to get and set
75df8bae1dSRodney W. Grimes  * the time-of-day and per-process interval timers.  Subroutines
76df8bae1dSRodney W. Grimes  * here provide support for adding and subtracting timeval structures
77df8bae1dSRodney W. Grimes  * and decrementing interval timers, optionally reloading the interval
78df8bae1dSRodney W. Grimes  * timers when they expire.
79df8bae1dSRodney W. Grimes  */
80df8bae1dSRodney W. Grimes 
817edfb592SJohn Baldwin static int	settime(struct thread *, struct timeval *);
824d77a549SAlfred Perlstein static void	timevalfix(struct timeval *);
834d77a549SAlfred Perlstein static void	no_lease_updatetime(int);
8494c8fcd8SPeter Wemm 
8586857b36SDavid Xu static void	itimer_start(void);
8686857b36SDavid Xu static int	itimer_init(void *, int, int);
8786857b36SDavid Xu static void	itimer_fini(void *, int);
8886857b36SDavid Xu static void	itimer_enter(struct itimer *);
8986857b36SDavid Xu static void	itimer_leave(struct itimer *);
9061d3a4efSDavid Xu static struct itimer *itimer_find(struct proc *, int, int);
9186857b36SDavid Xu static void	itimers_alloc(struct proc *);
92993182e5SAlexander Leidinger static void	itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp);
93993182e5SAlexander Leidinger static void	itimers_event_hook_exit(void *arg, struct proc *p);
9486857b36SDavid Xu static int	realtimer_create(struct itimer *);
9586857b36SDavid Xu static int	realtimer_gettime(struct itimer *, struct itimerspec *);
9686857b36SDavid Xu static int	realtimer_settime(struct itimer *, int,
9786857b36SDavid Xu 			struct itimerspec *, struct itimerspec *);
9886857b36SDavid Xu static int	realtimer_delete(struct itimer *);
9956c06c4bSDavid Xu static void	realtimer_clocktime(clockid_t, struct timespec *);
10086857b36SDavid Xu static void	realtimer_expire(void *);
10186857b36SDavid Xu static void	realtimer_event_hook(struct proc *, clockid_t, int event);
10286857b36SDavid Xu static int	kern_timer_create(struct thread *, clockid_t,
10361d3a4efSDavid Xu 			struct sigevent *, int *, int);
10461d3a4efSDavid Xu static int	kern_timer_delete(struct thread *, int);
10586857b36SDavid Xu 
10686857b36SDavid Xu int		register_posix_clock(int, struct kclock *);
10786857b36SDavid Xu void		itimer_fire(struct itimer *it);
10856c06c4bSDavid Xu int		itimespecfix(struct timespec *ts);
10986857b36SDavid Xu 
11086857b36SDavid Xu #define CLOCK_CALL(clock, call, arglist)		\
11186857b36SDavid Xu 	((*posix_clocks[clock].call) arglist)
11286857b36SDavid Xu 
11386857b36SDavid Xu SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL);
11486857b36SDavid Xu 
11586857b36SDavid Xu 
1161b09ae77SPoul-Henning Kamp static void
1171b09ae77SPoul-Henning Kamp no_lease_updatetime(deltat)
1181b09ae77SPoul-Henning Kamp 	int deltat;
1191b09ae77SPoul-Henning Kamp {
1201b09ae77SPoul-Henning Kamp }
1211b09ae77SPoul-Henning Kamp 
1224d77a549SAlfred Perlstein void (*lease_updatetime)(int)  = no_lease_updatetime;
1231b09ae77SPoul-Henning Kamp 
12494c8fcd8SPeter Wemm static int
12591afe087SPoul-Henning Kamp settime(struct thread *td, struct timeval *tv)
12694c8fcd8SPeter Wemm {
127fcae3aa6SNick Sayer 	struct timeval delta, tv1, tv2;
128c0bd94a7SNick Sayer 	static struct timeval maxtime, laststep;
1297ec73f64SPoul-Henning Kamp 	struct timespec ts;
13094c8fcd8SPeter Wemm 	int s;
13194c8fcd8SPeter Wemm 
132708e7684SPeter Wemm 	s = splclock();
1339c8fff87SBruce Evans 	microtime(&tv1);
13400af9731SPoul-Henning Kamp 	delta = *tv;
13500af9731SPoul-Henning Kamp 	timevalsub(&delta, &tv1);
13694c8fcd8SPeter Wemm 
13794c8fcd8SPeter Wemm 	/*
1389c8fff87SBruce Evans 	 * If the system is secure, we do not allow the time to be
139fcae3aa6SNick Sayer 	 * set to a value earlier than 1 second less than the highest
140fcae3aa6SNick Sayer 	 * time we have yet seen. The worst a miscreant can do in
141fcae3aa6SNick Sayer 	 * this circumstance is "freeze" time. He couldn't go
142fcae3aa6SNick Sayer 	 * back to the past.
143c0bd94a7SNick Sayer 	 *
144c0bd94a7SNick Sayer 	 * We similarly do not allow the clock to be stepped more
145c0bd94a7SNick Sayer 	 * than one second, nor more than once per second. This allows
146c0bd94a7SNick Sayer 	 * a miscreant to make the clock march double-time, but no worse.
14794c8fcd8SPeter Wemm 	 */
1487edfb592SJohn Baldwin 	if (securelevel_gt(td->td_ucred, 1) != 0) {
149fcae3aa6SNick Sayer 		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
1503f92429aSMatt Jacob 			/*
151c0bd94a7SNick Sayer 			 * Update maxtime to latest time we've seen.
1523f92429aSMatt Jacob 			 */
153fcae3aa6SNick Sayer 			if (tv1.tv_sec > maxtime.tv_sec)
154fcae3aa6SNick Sayer 				maxtime = tv1;
155fcae3aa6SNick Sayer 			tv2 = *tv;
156fcae3aa6SNick Sayer 			timevalsub(&tv2, &maxtime);
157fcae3aa6SNick Sayer 			if (tv2.tv_sec < -1) {
1583f92429aSMatt Jacob 				tv->tv_sec = maxtime.tv_sec - 1;
159fcae3aa6SNick Sayer 				printf("Time adjustment clamped to -1 second\n");
160fcae3aa6SNick Sayer 			}
1613f92429aSMatt Jacob 		} else {
162c0bd94a7SNick Sayer 			if (tv1.tv_sec == laststep.tv_sec) {
163c0bd94a7SNick Sayer 				splx(s);
164c0bd94a7SNick Sayer 				return (EPERM);
165c0bd94a7SNick Sayer 			}
166c0bd94a7SNick Sayer 			if (delta.tv_sec > 1) {
167c0bd94a7SNick Sayer 				tv->tv_sec = tv1.tv_sec + 1;
168c0bd94a7SNick Sayer 				printf("Time adjustment clamped to +1 second\n");
169c0bd94a7SNick Sayer 			}
170c0bd94a7SNick Sayer 			laststep = *tv;
171fcae3aa6SNick Sayer 		}
1729c8fff87SBruce Evans 	}
1739c8fff87SBruce Evans 
1747ec73f64SPoul-Henning Kamp 	ts.tv_sec = tv->tv_sec;
1757ec73f64SPoul-Henning Kamp 	ts.tv_nsec = tv->tv_usec * 1000;
1767edfb592SJohn Baldwin 	mtx_lock(&Giant);
17791266b96SPoul-Henning Kamp 	tc_setclock(&ts);
17894c8fcd8SPeter Wemm 	(void) splsoftclock();
17994c8fcd8SPeter Wemm 	lease_updatetime(delta.tv_sec);
18094c8fcd8SPeter Wemm 	splx(s);
18194c8fcd8SPeter Wemm 	resettodr();
1827edfb592SJohn Baldwin 	mtx_unlock(&Giant);
18394c8fcd8SPeter Wemm 	return (0);
18494c8fcd8SPeter Wemm }
18594c8fcd8SPeter Wemm 
18694c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
18794c8fcd8SPeter Wemm struct clock_gettime_args {
18894c8fcd8SPeter Wemm 	clockid_t clock_id;
18994c8fcd8SPeter Wemm 	struct	timespec *tp;
19094c8fcd8SPeter Wemm };
19194c8fcd8SPeter Wemm #endif
192708e7684SPeter Wemm 
193fb99ab88SMatthew Dillon /*
194fb99ab88SMatthew Dillon  * MPSAFE
195fb99ab88SMatthew Dillon  */
19694c8fcd8SPeter Wemm /* ARGSUSED */
19794c8fcd8SPeter Wemm int
19891afe087SPoul-Henning Kamp clock_gettime(struct thread *td, struct clock_gettime_args *uap)
19994c8fcd8SPeter Wemm {
20094c8fcd8SPeter Wemm 	struct timespec ats;
201f0b479cdSPaul Saab 	int error;
202f0b479cdSPaul Saab 
203f0b479cdSPaul Saab 	error = kern_clock_gettime(td, uap->clock_id, &ats);
204f0b479cdSPaul Saab 	if (error == 0)
205f0b479cdSPaul Saab 		error = copyout(&ats, uap->tp, sizeof(ats));
206f0b479cdSPaul Saab 
207f0b479cdSPaul Saab 	return (error);
208f0b479cdSPaul Saab }
209f0b479cdSPaul Saab 
210f0b479cdSPaul Saab int
211f0b479cdSPaul Saab kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
212f0b479cdSPaul Saab {
213de0a9241SKelly Yancey 	struct timeval sys, user;
21478c85e8dSJohn Baldwin 	struct proc *p;
21594c8fcd8SPeter Wemm 
21678c85e8dSJohn Baldwin 	p = td->td_proc;
217f0b479cdSPaul Saab 	switch (clock_id) {
2185e758b95SRobert Watson 	case CLOCK_REALTIME:		/* Default to precise. */
2195e758b95SRobert Watson 	case CLOCK_REALTIME_PRECISE:
220f0b479cdSPaul Saab 		nanotime(ats);
221b8817154SKelly Yancey 		break;
2225e758b95SRobert Watson 	case CLOCK_REALTIME_FAST:
2235e758b95SRobert Watson 		getnanotime(ats);
2245e758b95SRobert Watson 		break;
225b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
22678c85e8dSJohn Baldwin 		PROC_LOCK(p);
22778c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
22878c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
229f0b479cdSPaul Saab 		TIMEVAL_TO_TIMESPEC(&user, ats);
230b8817154SKelly Yancey 		break;
231b8817154SKelly Yancey 	case CLOCK_PROF:
23278c85e8dSJohn Baldwin 		PROC_LOCK(p);
23378c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
23478c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
235de0a9241SKelly Yancey 		timevaladd(&user, &sys);
236f0b479cdSPaul Saab 		TIMEVAL_TO_TIMESPEC(&user, ats);
237de0a9241SKelly Yancey 		break;
2385e758b95SRobert Watson 	case CLOCK_MONOTONIC:		/* Default to precise. */
2395e758b95SRobert Watson 	case CLOCK_MONOTONIC_PRECISE:
2405eefd889SAndre Oppermann 	case CLOCK_UPTIME:
2415e758b95SRobert Watson 	case CLOCK_UPTIME_PRECISE:
242f0b479cdSPaul Saab 		nanouptime(ats);
243b8817154SKelly Yancey 		break;
2445e758b95SRobert Watson 	case CLOCK_UPTIME_FAST:
2455e758b95SRobert Watson 	case CLOCK_MONOTONIC_FAST:
2465e758b95SRobert Watson 		getnanouptime(ats);
2475e758b95SRobert Watson 		break;
2485e758b95SRobert Watson 	case CLOCK_SECOND:
2495e758b95SRobert Watson 		ats->tv_sec = time_second;
2505e758b95SRobert Watson 		ats->tv_nsec = 0;
2515e758b95SRobert Watson 		break;
252b8817154SKelly Yancey 	default:
2535cb3dc8fSPoul-Henning Kamp 		return (EINVAL);
254b8817154SKelly Yancey 	}
255f0b479cdSPaul Saab 	return (0);
25694c8fcd8SPeter Wemm }
25794c8fcd8SPeter Wemm 
25894c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
25994c8fcd8SPeter Wemm struct clock_settime_args {
26094c8fcd8SPeter Wemm 	clockid_t clock_id;
26194c8fcd8SPeter Wemm 	const struct	timespec *tp;
26294c8fcd8SPeter Wemm };
26394c8fcd8SPeter Wemm #endif
264708e7684SPeter Wemm 
265fb99ab88SMatthew Dillon /*
266fb99ab88SMatthew Dillon  * MPSAFE
267fb99ab88SMatthew Dillon  */
26894c8fcd8SPeter Wemm /* ARGSUSED */
26994c8fcd8SPeter Wemm int
27091afe087SPoul-Henning Kamp clock_settime(struct thread *td, struct clock_settime_args *uap)
27194c8fcd8SPeter Wemm {
27294c8fcd8SPeter Wemm 	struct timespec ats;
27394c8fcd8SPeter Wemm 	int error;
27494c8fcd8SPeter Wemm 
275f0b479cdSPaul Saab 	if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
276f0b479cdSPaul Saab 		return (error);
277f0b479cdSPaul Saab 	return (kern_clock_settime(td, uap->clock_id, &ats));
278f0b479cdSPaul Saab }
279f0b479cdSPaul Saab 
280f0b479cdSPaul Saab int
281f0b479cdSPaul Saab kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
282f0b479cdSPaul Saab {
283f0b479cdSPaul Saab 	struct timeval atv;
284f0b479cdSPaul Saab 	int error;
285f0b479cdSPaul Saab 
2864b8d5f2dSRobert Watson #ifdef MAC
2874b8d5f2dSRobert Watson 	error = mac_check_system_settime(td->td_ucred);
2884b8d5f2dSRobert Watson 	if (error)
2894b8d5f2dSRobert Watson 		return (error);
2904b8d5f2dSRobert Watson #endif
29144731cabSJohn Baldwin 	if ((error = suser(td)) != 0)
2927edfb592SJohn Baldwin 		return (error);
293f0b479cdSPaul Saab 	if (clock_id != CLOCK_REALTIME)
2947edfb592SJohn Baldwin 		return (EINVAL);
295f0b479cdSPaul Saab 	if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000)
2967edfb592SJohn Baldwin 		return (EINVAL);
297a0502b19SPoul-Henning Kamp 	/* XXX Don't convert nsec->usec and back */
298f0b479cdSPaul Saab 	TIMESPEC_TO_TIMEVAL(&atv, ats);
2997edfb592SJohn Baldwin 	error = settime(td, &atv);
30094c8fcd8SPeter Wemm 	return (error);
30194c8fcd8SPeter Wemm }
30294c8fcd8SPeter Wemm 
30394c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
30494c8fcd8SPeter Wemm struct clock_getres_args {
30594c8fcd8SPeter Wemm 	clockid_t clock_id;
30694c8fcd8SPeter Wemm 	struct	timespec *tp;
30794c8fcd8SPeter Wemm };
30894c8fcd8SPeter Wemm #endif
309708e7684SPeter Wemm 
31094c8fcd8SPeter Wemm int
31191afe087SPoul-Henning Kamp clock_getres(struct thread *td, struct clock_getres_args *uap)
31294c8fcd8SPeter Wemm {
31394c8fcd8SPeter Wemm 	struct timespec ts;
314f0b479cdSPaul Saab 	int error;
31594c8fcd8SPeter Wemm 
316f0b479cdSPaul Saab 	if (uap->tp == NULL)
317f0b479cdSPaul Saab 		return (0);
318f0b479cdSPaul Saab 
319f0b479cdSPaul Saab 	error = kern_clock_getres(td, uap->clock_id, &ts);
320f0b479cdSPaul Saab 	if (error == 0)
321f0b479cdSPaul Saab 		error = copyout(&ts, uap->tp, sizeof(ts));
322f0b479cdSPaul Saab 	return (error);
323f0b479cdSPaul Saab }
324f0b479cdSPaul Saab 
325f0b479cdSPaul Saab int
326f0b479cdSPaul Saab kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
327f0b479cdSPaul Saab {
328f0b479cdSPaul Saab 
329f0b479cdSPaul Saab 	ts->tv_sec = 0;
330f0b479cdSPaul Saab 	switch (clock_id) {
331b8817154SKelly Yancey 	case CLOCK_REALTIME:
3325e758b95SRobert Watson 	case CLOCK_REALTIME_FAST:
3335e758b95SRobert Watson 	case CLOCK_REALTIME_PRECISE:
334b8817154SKelly Yancey 	case CLOCK_MONOTONIC:
3355e758b95SRobert Watson 	case CLOCK_MONOTONIC_FAST:
3365e758b95SRobert Watson 	case CLOCK_MONOTONIC_PRECISE:
3375eefd889SAndre Oppermann 	case CLOCK_UPTIME:
3385e758b95SRobert Watson 	case CLOCK_UPTIME_FAST:
3395e758b95SRobert Watson 	case CLOCK_UPTIME_PRECISE:
340ac0653dcSBruce Evans 		/*
341ac0653dcSBruce Evans 		 * Round up the result of the division cheaply by adding 1.
342ac0653dcSBruce Evans 		 * Rounding up is especially important if rounding down
343ac0653dcSBruce Evans 		 * would give 0.  Perfect rounding is unimportant.
344ac0653dcSBruce Evans 		 */
345f0b479cdSPaul Saab 		ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
346b8817154SKelly Yancey 		break;
347b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
348b8817154SKelly Yancey 	case CLOCK_PROF:
349b8817154SKelly Yancey 		/* Accurately round up here because we can do so cheaply. */
350f0b479cdSPaul Saab 		ts->tv_nsec = (1000000000 + hz - 1) / hz;
351b8817154SKelly Yancey 		break;
3525e758b95SRobert Watson 	case CLOCK_SECOND:
3535e758b95SRobert Watson 		ts->tv_sec = 1;
3545e758b95SRobert Watson 		ts->tv_nsec = 0;
3555e758b95SRobert Watson 		break;
356b8817154SKelly Yancey 	default:
357b8817154SKelly Yancey 		return (EINVAL);
35894c8fcd8SPeter Wemm 	}
359de0a9241SKelly Yancey 	return (0);
36094c8fcd8SPeter Wemm }
36194c8fcd8SPeter Wemm 
36294c8fcd8SPeter Wemm static int nanowait;
36394c8fcd8SPeter Wemm 
3647fdf2c85SPaul Saab int
3657fdf2c85SPaul Saab kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
3665b870b7bSPeter Wemm {
3675704ba6aSPoul-Henning Kamp 	struct timespec ts, ts2, ts3;
36833841826SPoul-Henning Kamp 	struct timeval tv;
36933841826SPoul-Henning Kamp 	int error;
3705b870b7bSPeter Wemm 
3717d7fb492SBruce Evans 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
372708e7684SPeter Wemm 		return (EINVAL);
373d254af07SMatthew Dillon 	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
3747d7fb492SBruce Evans 		return (0);
375c21410e1SPoul-Henning Kamp 	getnanouptime(&ts);
37600af9731SPoul-Henning Kamp 	timespecadd(&ts, rqt);
37733841826SPoul-Henning Kamp 	TIMESPEC_TO_TIMEVAL(&tv, rqt);
37833841826SPoul-Henning Kamp 	for (;;) {
37933841826SPoul-Henning Kamp 		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
38033841826SPoul-Henning Kamp 		    tvtohz(&tv));
381c21410e1SPoul-Henning Kamp 		getnanouptime(&ts2);
38233841826SPoul-Henning Kamp 		if (error != EWOULDBLOCK) {
38333841826SPoul-Henning Kamp 			if (error == ERESTART)
38494c8fcd8SPeter Wemm 				error = EINTR;
38533841826SPoul-Henning Kamp 			if (rmt != NULL) {
38633841826SPoul-Henning Kamp 				timespecsub(&ts, &ts2);
38733841826SPoul-Henning Kamp 				if (ts.tv_sec < 0)
38833841826SPoul-Henning Kamp 					timespecclear(&ts);
38900af9731SPoul-Henning Kamp 				*rmt = ts;
39000af9731SPoul-Henning Kamp 			}
39133841826SPoul-Henning Kamp 			return (error);
39233841826SPoul-Henning Kamp 		}
39333841826SPoul-Henning Kamp 		if (timespeccmp(&ts2, &ts, >=))
39433841826SPoul-Henning Kamp 			return (0);
3955704ba6aSPoul-Henning Kamp 		ts3 = ts;
3965704ba6aSPoul-Henning Kamp 		timespecsub(&ts3, &ts2);
3975704ba6aSPoul-Henning Kamp 		TIMESPEC_TO_TIMEVAL(&tv, &ts3);
39833841826SPoul-Henning Kamp 	}
3995b870b7bSPeter Wemm }
40094c8fcd8SPeter Wemm 
4015b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
4025b870b7bSPeter Wemm struct nanosleep_args {
4035b870b7bSPeter Wemm 	struct	timespec *rqtp;
4045b870b7bSPeter Wemm 	struct	timespec *rmtp;
4055b870b7bSPeter Wemm };
4065b870b7bSPeter Wemm #endif
4075b870b7bSPeter Wemm 
408fb99ab88SMatthew Dillon /*
409fb99ab88SMatthew Dillon  * MPSAFE
410fb99ab88SMatthew Dillon  */
4115b870b7bSPeter Wemm /* ARGSUSED */
4125b870b7bSPeter Wemm int
41391afe087SPoul-Henning Kamp nanosleep(struct thread *td, struct nanosleep_args *uap)
4145b870b7bSPeter Wemm {
4155b870b7bSPeter Wemm 	struct timespec rmt, rqt;
416fb99ab88SMatthew Dillon 	int error;
4175b870b7bSPeter Wemm 
418d1e405c5SAlfred Perlstein 	error = copyin(uap->rqtp, &rqt, sizeof(rqt));
4195b870b7bSPeter Wemm 	if (error)
4205b870b7bSPeter Wemm 		return (error);
421fb99ab88SMatthew Dillon 
42231f3e2adSAlfred Perlstein 	if (uap->rmtp &&
42331f3e2adSAlfred Perlstein 	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
42431f3e2adSAlfred Perlstein 			return (EFAULT);
4257fdf2c85SPaul Saab 	error = kern_nanosleep(td, &rqt, &rmt);
426d1e405c5SAlfred Perlstein 	if (error && uap->rmtp) {
427fb99ab88SMatthew Dillon 		int error2;
428fb99ab88SMatthew Dillon 
429d1e405c5SAlfred Perlstein 		error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
43031f3e2adSAlfred Perlstein 		if (error2)
431fb99ab88SMatthew Dillon 			error = error2;
432708e7684SPeter Wemm 	}
433708e7684SPeter Wemm 	return (error);
43494c8fcd8SPeter Wemm }
43594c8fcd8SPeter Wemm 
4365b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
437df8bae1dSRodney W. Grimes struct gettimeofday_args {
438df8bae1dSRodney W. Grimes 	struct	timeval *tp;
439df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
440df8bae1dSRodney W. Grimes };
441d2d3e875SBruce Evans #endif
442fb99ab88SMatthew Dillon /*
443fb99ab88SMatthew Dillon  * MPSAFE
444fb99ab88SMatthew Dillon  */
445df8bae1dSRodney W. Grimes /* ARGSUSED */
44626f9a767SRodney W. Grimes int
44791afe087SPoul-Henning Kamp gettimeofday(struct thread *td, struct gettimeofday_args *uap)
448df8bae1dSRodney W. Grimes {
449df8bae1dSRodney W. Grimes 	struct timeval atv;
450411c25edSTim J. Robbins 	struct timezone rtz;
451df8bae1dSRodney W. Grimes 	int error = 0;
452df8bae1dSRodney W. Grimes 
453df8bae1dSRodney W. Grimes 	if (uap->tp) {
454df8bae1dSRodney W. Grimes 		microtime(&atv);
45501609114SAlfred Perlstein 		error = copyout(&atv, uap->tp, sizeof (atv));
456df8bae1dSRodney W. Grimes 	}
45721dcdb38SPoul-Henning Kamp 	if (error == 0 && uap->tzp != NULL) {
45891f1c2b3SPoul-Henning Kamp 		rtz.tz_minuteswest = tz_minuteswest;
45991f1c2b3SPoul-Henning Kamp 		rtz.tz_dsttime = tz_dsttime;
460411c25edSTim J. Robbins 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
46121dcdb38SPoul-Henning Kamp 	}
462df8bae1dSRodney W. Grimes 	return (error);
463df8bae1dSRodney W. Grimes }
464df8bae1dSRodney W. Grimes 
465d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
466df8bae1dSRodney W. Grimes struct settimeofday_args {
467df8bae1dSRodney W. Grimes 	struct	timeval *tv;
468df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
469df8bae1dSRodney W. Grimes };
470d2d3e875SBruce Evans #endif
471fb99ab88SMatthew Dillon /*
472fb99ab88SMatthew Dillon  * MPSAFE
473fb99ab88SMatthew Dillon  */
474df8bae1dSRodney W. Grimes /* ARGSUSED */
47526f9a767SRodney W. Grimes int
47691afe087SPoul-Henning Kamp settimeofday(struct thread *td, struct settimeofday_args *uap)
477df8bae1dSRodney W. Grimes {
478b88ec951SJohn Baldwin 	struct timeval atv, *tvp;
479b88ec951SJohn Baldwin 	struct timezone atz, *tzp;
480b88ec951SJohn Baldwin 	int error;
481b88ec951SJohn Baldwin 
482b88ec951SJohn Baldwin 	if (uap->tv) {
483b88ec951SJohn Baldwin 		error = copyin(uap->tv, &atv, sizeof(atv));
484b88ec951SJohn Baldwin 		if (error)
485b88ec951SJohn Baldwin 			return (error);
486b88ec951SJohn Baldwin 		tvp = &atv;
487b88ec951SJohn Baldwin 	} else
488b88ec951SJohn Baldwin 		tvp = NULL;
489b88ec951SJohn Baldwin 	if (uap->tzp) {
490b88ec951SJohn Baldwin 		error = copyin(uap->tzp, &atz, sizeof(atz));
491b88ec951SJohn Baldwin 		if (error)
492b88ec951SJohn Baldwin 			return (error);
493b88ec951SJohn Baldwin 		tzp = &atz;
494b88ec951SJohn Baldwin 	} else
495b88ec951SJohn Baldwin 		tzp = NULL;
496b88ec951SJohn Baldwin 	return (kern_settimeofday(td, tvp, tzp));
497b88ec951SJohn Baldwin }
498b88ec951SJohn Baldwin 
499b88ec951SJohn Baldwin int
500b88ec951SJohn Baldwin kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
501b88ec951SJohn Baldwin {
502b88ec951SJohn Baldwin 	int error;
503fb99ab88SMatthew Dillon 
5044b8d5f2dSRobert Watson #ifdef MAC
5054b8d5f2dSRobert Watson 	error = mac_check_system_settime(td->td_ucred);
5064b8d5f2dSRobert Watson 	if (error)
5074b8d5f2dSRobert Watson 		return (error);
5084b8d5f2dSRobert Watson #endif
509b88ec951SJohn Baldwin 	error = suser(td);
510b88ec951SJohn Baldwin 	if (error)
5117edfb592SJohn Baldwin 		return (error);
512df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
513b88ec951SJohn Baldwin 	if (tv) {
514b88ec951SJohn Baldwin 		if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
5157edfb592SJohn Baldwin 			return (EINVAL);
516b88ec951SJohn Baldwin 		error = settime(td, tv);
517708e7684SPeter Wemm 	}
518b88ec951SJohn Baldwin 	if (tzp && error == 0) {
519b88ec951SJohn Baldwin 		tz_minuteswest = tzp->tz_minuteswest;
520b88ec951SJohn Baldwin 		tz_dsttime = tzp->tz_dsttime;
521b88ec951SJohn Baldwin 	}
5227edfb592SJohn Baldwin 	return (error);
523b88ec951SJohn Baldwin }
5247edfb592SJohn Baldwin 
525df8bae1dSRodney W. Grimes /*
526df8bae1dSRodney W. Grimes  * Get value of an interval timer.  The process virtual and
527df8bae1dSRodney W. Grimes  * profiling virtual time timers are kept in the p_stats area, since
528df8bae1dSRodney W. Grimes  * they can be swapped out.  These are kept internally in the
529df8bae1dSRodney W. Grimes  * way they are specified externally: in time until they expire.
530df8bae1dSRodney W. Grimes  *
531df8bae1dSRodney W. Grimes  * The real time interval timer is kept in the process table slot
532df8bae1dSRodney W. Grimes  * for the process, and its value (it_value) is kept as an
533df8bae1dSRodney W. Grimes  * absolute time rather than as a delta, so that it is easy to keep
534df8bae1dSRodney W. Grimes  * periodic real-time signals from drifting.
535df8bae1dSRodney W. Grimes  *
536df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
537df8bae1dSRodney W. Grimes  * kern_clock.c.  The real time timer is processed by a timeout
538df8bae1dSRodney W. Grimes  * routine, called from the softclock() routine.  Since a callout
539df8bae1dSRodney W. Grimes  * may be delayed in real time due to interrupt processing in the system,
540df8bae1dSRodney W. Grimes  * it is possible for the real time timeout routine (realitexpire, given below),
541df8bae1dSRodney W. Grimes  * to be delayed in real time past when it is supposed to occur.  It
542df8bae1dSRodney W. Grimes  * does not suffice, therefore, to reload the real timer .it_value from the
543df8bae1dSRodney W. Grimes  * real time timers .it_interval.  Rather, we compute the next time in
544df8bae1dSRodney W. Grimes  * absolute time the timer should go off.
545df8bae1dSRodney W. Grimes  */
546d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
547df8bae1dSRodney W. Grimes struct getitimer_args {
548df8bae1dSRodney W. Grimes 	u_int	which;
549df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
550df8bae1dSRodney W. Grimes };
551d2d3e875SBruce Evans #endif
552fb99ab88SMatthew Dillon /*
553fb99ab88SMatthew Dillon  * MPSAFE
554fb99ab88SMatthew Dillon  */
55526f9a767SRodney W. Grimes int
55691afe087SPoul-Henning Kamp getitimer(struct thread *td, struct getitimer_args *uap)
557df8bae1dSRodney W. Grimes {
558df8bae1dSRodney W. Grimes 	struct itimerval aitv;
559c90110d6SJohn Baldwin 	int error;
560df8bae1dSRodney W. Grimes 
561cfa0efe7SMaxim Sobolev 	error = kern_getitimer(td, uap->which, &aitv);
562cfa0efe7SMaxim Sobolev 	if (error != 0)
563cfa0efe7SMaxim Sobolev 		return (error);
564cfa0efe7SMaxim Sobolev 	return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
565cfa0efe7SMaxim Sobolev }
566cfa0efe7SMaxim Sobolev 
567cfa0efe7SMaxim Sobolev int
568cfa0efe7SMaxim Sobolev kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
569cfa0efe7SMaxim Sobolev {
570cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
571cfa0efe7SMaxim Sobolev 	struct timeval ctv;
572cfa0efe7SMaxim Sobolev 
573cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
574df8bae1dSRodney W. Grimes 		return (EINVAL);
575fb99ab88SMatthew Dillon 
576cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
577df8bae1dSRodney W. Grimes 		/*
578ee002b68SBruce Evans 		 * Convert from absolute to relative time in .it_value
579df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
580df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
581df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
582df8bae1dSRodney W. Grimes 		 */
58396d7f8efSTim J. Robbins 		PROC_LOCK(p);
584cfa0efe7SMaxim Sobolev 		*aitv = p->p_realtimer;
58596d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
586cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
587c21410e1SPoul-Henning Kamp 			getmicrouptime(&ctv);
588cfa0efe7SMaxim Sobolev 			if (timevalcmp(&aitv->it_value, &ctv, <))
589cfa0efe7SMaxim Sobolev 				timevalclear(&aitv->it_value);
590df8bae1dSRodney W. Grimes 			else
591cfa0efe7SMaxim Sobolev 				timevalsub(&aitv->it_value, &ctv);
592227ee8a1SPoul-Henning Kamp 		}
593fb99ab88SMatthew Dillon 	} else {
59496d7f8efSTim J. Robbins 		mtx_lock_spin(&sched_lock);
595cfa0efe7SMaxim Sobolev 		*aitv = p->p_stats->p_timer[which];
59696d7f8efSTim J. Robbins 		mtx_unlock_spin(&sched_lock);
597fb99ab88SMatthew Dillon 	}
598cfa0efe7SMaxim Sobolev 	return (0);
599df8bae1dSRodney W. Grimes }
600df8bae1dSRodney W. Grimes 
601d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
602df8bae1dSRodney W. Grimes struct setitimer_args {
603df8bae1dSRodney W. Grimes 	u_int	which;
604df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
605df8bae1dSRodney W. Grimes };
606d2d3e875SBruce Evans #endif
607cfa0efe7SMaxim Sobolev 
608fb99ab88SMatthew Dillon /*
609fb99ab88SMatthew Dillon  * MPSAFE
610fb99ab88SMatthew Dillon  */
61126f9a767SRodney W. Grimes int
61291afe087SPoul-Henning Kamp setitimer(struct thread *td, struct setitimer_args *uap)
613df8bae1dSRodney W. Grimes {
614cfa0efe7SMaxim Sobolev 	struct itimerval aitv, oitv;
615c90110d6SJohn Baldwin 	int error;
61696d7f8efSTim J. Robbins 
61796d7f8efSTim J. Robbins 	if (uap->itv == NULL) {
61896d7f8efSTim J. Robbins 		uap->itv = uap->oitv;
61996d7f8efSTim J. Robbins 		return (getitimer(td, (struct getitimer_args *)uap));
62096d7f8efSTim J. Robbins 	}
621df8bae1dSRodney W. Grimes 
62296d7f8efSTim J. Robbins 	if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
623df8bae1dSRodney W. Grimes 		return (error);
624cfa0efe7SMaxim Sobolev 	error = kern_setitimer(td, uap->which, &aitv, &oitv);
625cfa0efe7SMaxim Sobolev 	if (error != 0 || uap->oitv == NULL)
626cfa0efe7SMaxim Sobolev 		return (error);
627cfa0efe7SMaxim Sobolev 	return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
628cfa0efe7SMaxim Sobolev }
629cfa0efe7SMaxim Sobolev 
630cfa0efe7SMaxim Sobolev int
631c90110d6SJohn Baldwin kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
632c90110d6SJohn Baldwin     struct itimerval *oitv)
633cfa0efe7SMaxim Sobolev {
634cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
635cfa0efe7SMaxim Sobolev 	struct timeval ctv;
636cfa0efe7SMaxim Sobolev 
6375e85ac17SJohn Baldwin 	if (aitv == NULL)
6385e85ac17SJohn Baldwin 		return (kern_getitimer(td, which, oitv));
6395e85ac17SJohn Baldwin 
640cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
64196d7f8efSTim J. Robbins 		return (EINVAL);
642cfa0efe7SMaxim Sobolev 	if (itimerfix(&aitv->it_value))
643cfa0efe7SMaxim Sobolev 		return (EINVAL);
644cfa0efe7SMaxim Sobolev 	if (!timevalisset(&aitv->it_value))
645cfa0efe7SMaxim Sobolev 		timevalclear(&aitv->it_interval);
646cfa0efe7SMaxim Sobolev 	else if (itimerfix(&aitv->it_interval))
64796d7f8efSTim J. Robbins 		return (EINVAL);
64896d7f8efSTim J. Robbins 
649cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
65096d7f8efSTim J. Robbins 		PROC_LOCK(p);
6514cf41af3SPoul-Henning Kamp 		if (timevalisset(&p->p_realtimer.it_value))
6524f559836SJake Burkholder 			callout_stop(&p->p_itcallout);
65325b4d3a8SJohn Baldwin 		getmicrouptime(&ctv);
654cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
655cfa0efe7SMaxim Sobolev 			callout_reset(&p->p_itcallout, tvtohz(&aitv->it_value),
6564f559836SJake Burkholder 			    realitexpire, p);
657cfa0efe7SMaxim Sobolev 			timevaladd(&aitv->it_value, &ctv);
65825b4d3a8SJohn Baldwin 		}
659cfa0efe7SMaxim Sobolev 		*oitv = p->p_realtimer;
660cfa0efe7SMaxim Sobolev 		p->p_realtimer = *aitv;
66196d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
662cfa0efe7SMaxim Sobolev 		if (timevalisset(&oitv->it_value)) {
663cfa0efe7SMaxim Sobolev 			if (timevalcmp(&oitv->it_value, &ctv, <))
664cfa0efe7SMaxim Sobolev 				timevalclear(&oitv->it_value);
66596d7f8efSTim J. Robbins 			else
666cfa0efe7SMaxim Sobolev 				timevalsub(&oitv->it_value, &ctv);
667fb99ab88SMatthew Dillon 		}
66896d7f8efSTim J. Robbins 	} else {
66996d7f8efSTim J. Robbins 		mtx_lock_spin(&sched_lock);
670cfa0efe7SMaxim Sobolev 		*oitv = p->p_stats->p_timer[which];
671cfa0efe7SMaxim Sobolev 		p->p_stats->p_timer[which] = *aitv;
67296d7f8efSTim J. Robbins 		mtx_unlock_spin(&sched_lock);
67396d7f8efSTim J. Robbins 	}
67496d7f8efSTim J. Robbins 	return (0);
675df8bae1dSRodney W. Grimes }
676df8bae1dSRodney W. Grimes 
677df8bae1dSRodney W. Grimes /*
678df8bae1dSRodney W. Grimes  * Real interval timer expired:
679df8bae1dSRodney W. Grimes  * send process whose timer expired an alarm signal.
680df8bae1dSRodney W. Grimes  * If time is not set up to reload, then just return.
681df8bae1dSRodney W. Grimes  * Else compute next time timer should go off which is > current time.
682df8bae1dSRodney W. Grimes  * This is where delay in processing this timeout causes multiple
683df8bae1dSRodney W. Grimes  * SIGALRM calls to be compressed into one.
684c8b47828SBruce Evans  * tvtohz() always adds 1 to allow for the time until the next clock
6859207f00aSBruce Evans  * interrupt being strictly less than 1 clock tick, but we don't want
6869207f00aSBruce Evans  * that here since we want to appear to be in sync with the clock
6879207f00aSBruce Evans  * interrupt even when we're delayed.
688df8bae1dSRodney W. Grimes  */
689df8bae1dSRodney W. Grimes void
69091afe087SPoul-Henning Kamp realitexpire(void *arg)
691df8bae1dSRodney W. Grimes {
69291afe087SPoul-Henning Kamp 	struct proc *p;
693bfe6c9faSPoul-Henning Kamp 	struct timeval ctv, ntv;
694df8bae1dSRodney W. Grimes 
695df8bae1dSRodney W. Grimes 	p = (struct proc *)arg;
69637824023SJohn Baldwin 	PROC_LOCK(p);
697df8bae1dSRodney W. Grimes 	psignal(p, SIGALRM);
6984cf41af3SPoul-Henning Kamp 	if (!timevalisset(&p->p_realtimer.it_interval)) {
6994cf41af3SPoul-Henning Kamp 		timevalclear(&p->p_realtimer.it_value);
7005499ea01SJohn Baldwin 		if (p->p_flag & P_WEXIT)
7015499ea01SJohn Baldwin 			wakeup(&p->p_itcallout);
70237824023SJohn Baldwin 		PROC_UNLOCK(p);
703df8bae1dSRodney W. Grimes 		return;
704df8bae1dSRodney W. Grimes 	}
705df8bae1dSRodney W. Grimes 	for (;;) {
706df8bae1dSRodney W. Grimes 		timevaladd(&p->p_realtimer.it_value,
707df8bae1dSRodney W. Grimes 		    &p->p_realtimer.it_interval);
708c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
7094cf41af3SPoul-Henning Kamp 		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
710bfe6c9faSPoul-Henning Kamp 			ntv = p->p_realtimer.it_value;
711bfe6c9faSPoul-Henning Kamp 			timevalsub(&ntv, &ctv);
7124f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
7134f559836SJake Burkholder 			    realitexpire, p);
71437824023SJohn Baldwin 			PROC_UNLOCK(p);
715df8bae1dSRodney W. Grimes 			return;
716df8bae1dSRodney W. Grimes 		}
717df8bae1dSRodney W. Grimes 	}
71837824023SJohn Baldwin 	/*NOTREACHED*/
719df8bae1dSRodney W. Grimes }
720df8bae1dSRodney W. Grimes 
721df8bae1dSRodney W. Grimes /*
722df8bae1dSRodney W. Grimes  * Check that a proposed value to load into the .it_value or
723df8bae1dSRodney W. Grimes  * .it_interval part of an interval timer is acceptable, and
724df8bae1dSRodney W. Grimes  * fix it to have at least minimal value (i.e. if it is less
725df8bae1dSRodney W. Grimes  * than the resolution of the clock, round it up.)
726df8bae1dSRodney W. Grimes  */
72726f9a767SRodney W. Grimes int
72891afe087SPoul-Henning Kamp itimerfix(struct timeval *tv)
729df8bae1dSRodney W. Grimes {
730df8bae1dSRodney W. Grimes 
73186857b36SDavid Xu 	if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
732df8bae1dSRodney W. Grimes 		return (EINVAL);
733df8bae1dSRodney W. Grimes 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
734df8bae1dSRodney W. Grimes 		tv->tv_usec = tick;
735df8bae1dSRodney W. Grimes 	return (0);
736df8bae1dSRodney W. Grimes }
737df8bae1dSRodney W. Grimes 
738df8bae1dSRodney W. Grimes /*
739df8bae1dSRodney W. Grimes  * Decrement an interval timer by a specified number
740df8bae1dSRodney W. Grimes  * of microseconds, which must be less than a second,
741df8bae1dSRodney W. Grimes  * i.e. < 1000000.  If the timer expires, then reload
742df8bae1dSRodney W. Grimes  * it.  In this case, carry over (usec - old value) to
743df8bae1dSRodney W. Grimes  * reduce the value reloaded into the timer so that
744df8bae1dSRodney W. Grimes  * the timer does not drift.  This routine assumes
745df8bae1dSRodney W. Grimes  * that it is called in a context where the timers
746df8bae1dSRodney W. Grimes  * on which it is operating cannot change in value.
747df8bae1dSRodney W. Grimes  */
74826f9a767SRodney W. Grimes int
74991afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec)
750df8bae1dSRodney W. Grimes {
751df8bae1dSRodney W. Grimes 
752df8bae1dSRodney W. Grimes 	if (itp->it_value.tv_usec < usec) {
753df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_sec == 0) {
754df8bae1dSRodney W. Grimes 			/* expired, and already in next interval */
755df8bae1dSRodney W. Grimes 			usec -= itp->it_value.tv_usec;
756df8bae1dSRodney W. Grimes 			goto expire;
757df8bae1dSRodney W. Grimes 		}
758df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec += 1000000;
759df8bae1dSRodney W. Grimes 		itp->it_value.tv_sec--;
760df8bae1dSRodney W. Grimes 	}
761df8bae1dSRodney W. Grimes 	itp->it_value.tv_usec -= usec;
762df8bae1dSRodney W. Grimes 	usec = 0;
7634cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_value))
764df8bae1dSRodney W. Grimes 		return (1);
765df8bae1dSRodney W. Grimes 	/* expired, exactly at end of interval */
766df8bae1dSRodney W. Grimes expire:
7674cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_interval)) {
768df8bae1dSRodney W. Grimes 		itp->it_value = itp->it_interval;
769df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec -= usec;
770df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_usec < 0) {
771df8bae1dSRodney W. Grimes 			itp->it_value.tv_usec += 1000000;
772df8bae1dSRodney W. Grimes 			itp->it_value.tv_sec--;
773df8bae1dSRodney W. Grimes 		}
774df8bae1dSRodney W. Grimes 	} else
775df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
776df8bae1dSRodney W. Grimes 	return (0);
777df8bae1dSRodney W. Grimes }
778df8bae1dSRodney W. Grimes 
779df8bae1dSRodney W. Grimes /*
780df8bae1dSRodney W. Grimes  * Add and subtract routines for timevals.
781df8bae1dSRodney W. Grimes  * N.B.: subtract routine doesn't deal with
782df8bae1dSRodney W. Grimes  * results which are before the beginning,
783df8bae1dSRodney W. Grimes  * it just gets very confused in this case.
784df8bae1dSRodney W. Grimes  * Caveat emptor.
785df8bae1dSRodney W. Grimes  */
78626f9a767SRodney W. Grimes void
7876ff7636eSAlfred Perlstein timevaladd(struct timeval *t1, const struct timeval *t2)
788df8bae1dSRodney W. Grimes {
789df8bae1dSRodney W. Grimes 
790df8bae1dSRodney W. Grimes 	t1->tv_sec += t2->tv_sec;
791df8bae1dSRodney W. Grimes 	t1->tv_usec += t2->tv_usec;
792df8bae1dSRodney W. Grimes 	timevalfix(t1);
793df8bae1dSRodney W. Grimes }
794df8bae1dSRodney W. Grimes 
79526f9a767SRodney W. Grimes void
7966ff7636eSAlfred Perlstein timevalsub(struct timeval *t1, const struct timeval *t2)
797df8bae1dSRodney W. Grimes {
798df8bae1dSRodney W. Grimes 
799df8bae1dSRodney W. Grimes 	t1->tv_sec -= t2->tv_sec;
800df8bae1dSRodney W. Grimes 	t1->tv_usec -= t2->tv_usec;
801df8bae1dSRodney W. Grimes 	timevalfix(t1);
802df8bae1dSRodney W. Grimes }
803df8bae1dSRodney W. Grimes 
80487b6de2bSPoul-Henning Kamp static void
80591afe087SPoul-Henning Kamp timevalfix(struct timeval *t1)
806df8bae1dSRodney W. Grimes {
807df8bae1dSRodney W. Grimes 
808df8bae1dSRodney W. Grimes 	if (t1->tv_usec < 0) {
809df8bae1dSRodney W. Grimes 		t1->tv_sec--;
810df8bae1dSRodney W. Grimes 		t1->tv_usec += 1000000;
811df8bae1dSRodney W. Grimes 	}
812df8bae1dSRodney W. Grimes 	if (t1->tv_usec >= 1000000) {
813df8bae1dSRodney W. Grimes 		t1->tv_sec++;
814df8bae1dSRodney W. Grimes 		t1->tv_usec -= 1000000;
815df8bae1dSRodney W. Grimes 	}
816df8bae1dSRodney W. Grimes }
81791974ce1SSam Leffler 
81891974ce1SSam Leffler /*
819addea9d4SSam Leffler  * ratecheck(): simple time-based rate-limit checking.
82091974ce1SSam Leffler  */
82191974ce1SSam Leffler int
82291974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
82391974ce1SSam Leffler {
82491974ce1SSam Leffler 	struct timeval tv, delta;
82591974ce1SSam Leffler 	int rv = 0;
82691974ce1SSam Leffler 
827addea9d4SSam Leffler 	getmicrouptime(&tv);		/* NB: 10ms precision */
828addea9d4SSam Leffler 	delta = tv;
829addea9d4SSam Leffler 	timevalsub(&delta, lasttime);
83091974ce1SSam Leffler 
83191974ce1SSam Leffler 	/*
83291974ce1SSam Leffler 	 * check for 0,0 is so that the message will be seen at least once,
83391974ce1SSam Leffler 	 * even if interval is huge.
83491974ce1SSam Leffler 	 */
83591974ce1SSam Leffler 	if (timevalcmp(&delta, mininterval, >=) ||
83691974ce1SSam Leffler 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
83791974ce1SSam Leffler 		*lasttime = tv;
83891974ce1SSam Leffler 		rv = 1;
83991974ce1SSam Leffler 	}
84091974ce1SSam Leffler 
84191974ce1SSam Leffler 	return (rv);
84291974ce1SSam Leffler }
84391974ce1SSam Leffler 
84491974ce1SSam Leffler /*
84591974ce1SSam Leffler  * ppsratecheck(): packets (or events) per second limitation.
846addea9d4SSam Leffler  *
847addea9d4SSam Leffler  * Return 0 if the limit is to be enforced (e.g. the caller
848addea9d4SSam Leffler  * should drop a packet because of the rate limitation).
849addea9d4SSam Leffler  *
850893bec80SSam Leffler  * maxpps of 0 always causes zero to be returned.  maxpps of -1
851893bec80SSam Leffler  * always causes 1 to be returned; this effectively defeats rate
852893bec80SSam Leffler  * limiting.
853893bec80SSam Leffler  *
854addea9d4SSam Leffler  * Note that we maintain the struct timeval for compatibility
855addea9d4SSam Leffler  * with other bsd systems.  We reuse the storage and just monitor
856addea9d4SSam Leffler  * clock ticks for minimal overhead.
85791974ce1SSam Leffler  */
85891974ce1SSam Leffler int
85991974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
86091974ce1SSam Leffler {
861addea9d4SSam Leffler 	int now;
86291974ce1SSam Leffler 
86391974ce1SSam Leffler 	/*
864addea9d4SSam Leffler 	 * Reset the last time and counter if this is the first call
865addea9d4SSam Leffler 	 * or more than a second has passed since the last update of
866addea9d4SSam Leffler 	 * lasttime.
86791974ce1SSam Leffler 	 */
868addea9d4SSam Leffler 	now = ticks;
869addea9d4SSam Leffler 	if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
870addea9d4SSam Leffler 		lasttime->tv_sec = now;
871addea9d4SSam Leffler 		*curpps = 1;
872893bec80SSam Leffler 		return (maxpps != 0);
873addea9d4SSam Leffler 	} else {
874addea9d4SSam Leffler 		(*curpps)++;		/* NB: ignore potential overflow */
875addea9d4SSam Leffler 		return (maxpps < 0 || *curpps < maxpps);
876addea9d4SSam Leffler 	}
87791974ce1SSam Leffler }
87886857b36SDavid Xu 
87986857b36SDavid Xu static void
88086857b36SDavid Xu itimer_start(void)
88186857b36SDavid Xu {
88286857b36SDavid Xu 	struct kclock rt_clock = {
88386857b36SDavid Xu 		.timer_create  = realtimer_create,
88486857b36SDavid Xu 		.timer_delete  = realtimer_delete,
88586857b36SDavid Xu 		.timer_settime = realtimer_settime,
88686857b36SDavid Xu 		.timer_gettime = realtimer_gettime,
88786857b36SDavid Xu 		.event_hook     = realtimer_event_hook
88886857b36SDavid Xu 	};
88986857b36SDavid Xu 
89086857b36SDavid Xu 	itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
89186857b36SDavid Xu 		NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
89286857b36SDavid Xu 	register_posix_clock(CLOCK_REALTIME,  &rt_clock);
89386857b36SDavid Xu 	register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
89477e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
89577e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
89677e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
897993182e5SAlexander Leidinger 	EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit,
898d26b1a1fSDavid Xu 		(void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY);
899993182e5SAlexander Leidinger 	EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec,
900d26b1a1fSDavid Xu 		(void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY);
90186857b36SDavid Xu }
90286857b36SDavid Xu 
90386857b36SDavid Xu int
90486857b36SDavid Xu register_posix_clock(int clockid, struct kclock *clk)
90586857b36SDavid Xu {
90686857b36SDavid Xu 	if ((unsigned)clockid >= MAX_CLOCKS) {
90786857b36SDavid Xu 		printf("%s: invalid clockid\n", __func__);
90886857b36SDavid Xu 		return (0);
90986857b36SDavid Xu 	}
91086857b36SDavid Xu 	posix_clocks[clockid] = *clk;
91186857b36SDavid Xu 	return (1);
91286857b36SDavid Xu }
91386857b36SDavid Xu 
91486857b36SDavid Xu static int
91586857b36SDavid Xu itimer_init(void *mem, int size, int flags)
91686857b36SDavid Xu {
91786857b36SDavid Xu 	struct itimer *it;
91886857b36SDavid Xu 
91986857b36SDavid Xu 	it = (struct itimer *)mem;
92086857b36SDavid Xu 	mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
92186857b36SDavid Xu 	return (0);
92286857b36SDavid Xu }
92386857b36SDavid Xu 
92486857b36SDavid Xu static void
92586857b36SDavid Xu itimer_fini(void *mem, int size)
92686857b36SDavid Xu {
92786857b36SDavid Xu 	struct itimer *it;
92886857b36SDavid Xu 
92986857b36SDavid Xu 	it = (struct itimer *)mem;
93086857b36SDavid Xu 	mtx_destroy(&it->it_mtx);
93186857b36SDavid Xu }
93286857b36SDavid Xu 
93386857b36SDavid Xu static void
93486857b36SDavid Xu itimer_enter(struct itimer *it)
93586857b36SDavid Xu {
93686857b36SDavid Xu 
93786857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
93886857b36SDavid Xu 	it->it_usecount++;
93986857b36SDavid Xu }
94086857b36SDavid Xu 
94186857b36SDavid Xu static void
94286857b36SDavid Xu itimer_leave(struct itimer *it)
94386857b36SDavid Xu {
94486857b36SDavid Xu 
94586857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
94686857b36SDavid Xu 	KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
94786857b36SDavid Xu 
94886857b36SDavid Xu 	if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
94986857b36SDavid Xu 		wakeup(it);
95086857b36SDavid Xu }
95186857b36SDavid Xu 
95286857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
95361d3a4efSDavid Xu struct ktimer_create_args {
95486857b36SDavid Xu 	clockid_t clock_id;
95586857b36SDavid Xu 	struct sigevent * evp;
95661d3a4efSDavid Xu 	int * timerid;
95786857b36SDavid Xu };
95886857b36SDavid Xu #endif
95986857b36SDavid Xu 
96086857b36SDavid Xu int
96161d3a4efSDavid Xu ktimer_create(struct thread *td, struct ktimer_create_args *uap)
96286857b36SDavid Xu {
96386857b36SDavid Xu 	struct sigevent *evp1, ev;
96461d3a4efSDavid Xu 	int id;
96586857b36SDavid Xu 	int error;
96686857b36SDavid Xu 
96786857b36SDavid Xu 	if (uap->evp != NULL) {
96886857b36SDavid Xu 		error = copyin(uap->evp, &ev, sizeof(ev));
96986857b36SDavid Xu 		if (error != 0)
97086857b36SDavid Xu 			return (error);
97186857b36SDavid Xu 		evp1 = &ev;
97286857b36SDavid Xu 	} else
97386857b36SDavid Xu 		evp1 = NULL;
97486857b36SDavid Xu 
97586857b36SDavid Xu 	error = kern_timer_create(td, uap->clock_id, evp1, &id, -1);
97686857b36SDavid Xu 
97786857b36SDavid Xu 	if (error == 0) {
97861d3a4efSDavid Xu 		error = copyout(&id, uap->timerid, sizeof(int));
97986857b36SDavid Xu 		if (error != 0)
98086857b36SDavid Xu 			kern_timer_delete(td, id);
98186857b36SDavid Xu 	}
98286857b36SDavid Xu 	return (error);
98386857b36SDavid Xu }
98486857b36SDavid Xu 
98586857b36SDavid Xu static int
98686857b36SDavid Xu kern_timer_create(struct thread *td, clockid_t clock_id,
98761d3a4efSDavid Xu 	struct sigevent *evp, int *timerid, int preset_id)
98886857b36SDavid Xu {
98986857b36SDavid Xu 	struct proc *p = td->td_proc;
99086857b36SDavid Xu 	struct itimer *it;
99186857b36SDavid Xu 	int id;
99286857b36SDavid Xu 	int error;
99386857b36SDavid Xu 
99486857b36SDavid Xu 	if (clock_id < 0 || clock_id >= MAX_CLOCKS)
99586857b36SDavid Xu 		return (EINVAL);
99686857b36SDavid Xu 
99786857b36SDavid Xu 	if (posix_clocks[clock_id].timer_create == NULL)
99886857b36SDavid Xu 		return (EINVAL);
99986857b36SDavid Xu 
100086857b36SDavid Xu 	if (evp != NULL) {
100186857b36SDavid Xu 		if (evp->sigev_notify != SIGEV_NONE &&
100256c06c4bSDavid Xu 		    evp->sigev_notify != SIGEV_SIGNAL &&
100356c06c4bSDavid Xu 		    evp->sigev_notify != SIGEV_THREAD_ID)
100486857b36SDavid Xu 			return (EINVAL);
100556c06c4bSDavid Xu 		if ((evp->sigev_notify == SIGEV_SIGNAL ||
100656c06c4bSDavid Xu 		     evp->sigev_notify == SIGEV_THREAD_ID) &&
100786857b36SDavid Xu 			!_SIG_VALID(evp->sigev_signo))
100886857b36SDavid Xu 			return (EINVAL);
100986857b36SDavid Xu 	}
101086857b36SDavid Xu 
101160354683SDavid Xu 	if (p->p_itimers == NULL)
101286857b36SDavid Xu 		itimers_alloc(p);
101386857b36SDavid Xu 
101486857b36SDavid Xu 	it = uma_zalloc(itimer_zone, M_WAITOK);
101586857b36SDavid Xu 	it->it_flags = 0;
101686857b36SDavid Xu 	it->it_usecount = 0;
101786857b36SDavid Xu 	it->it_active = 0;
101856c06c4bSDavid Xu 	timespecclear(&it->it_time.it_value);
101956c06c4bSDavid Xu 	timespecclear(&it->it_time.it_interval);
102086857b36SDavid Xu 	it->it_overrun = 0;
102186857b36SDavid Xu 	it->it_overrun_last = 0;
102286857b36SDavid Xu 	it->it_clockid = clock_id;
102386857b36SDavid Xu 	it->it_timerid = -1;
102486857b36SDavid Xu 	it->it_proc = p;
102586857b36SDavid Xu 	ksiginfo_init(&it->it_ksi);
102686857b36SDavid Xu 	it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
102786857b36SDavid Xu 	error = CLOCK_CALL(clock_id, timer_create, (it));
102886857b36SDavid Xu 	if (error != 0)
102986857b36SDavid Xu 		goto out;
103086857b36SDavid Xu 
103186857b36SDavid Xu 	PROC_LOCK(p);
103286857b36SDavid Xu 	if (preset_id != -1) {
103386857b36SDavid Xu 		KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
103486857b36SDavid Xu 		id = preset_id;
103560354683SDavid Xu 		if (p->p_itimers->its_timers[id] != NULL) {
103686857b36SDavid Xu 			PROC_UNLOCK(p);
103786857b36SDavid Xu 			error = 0;
103886857b36SDavid Xu 			goto out;
103986857b36SDavid Xu 		}
104086857b36SDavid Xu 	} else {
104186857b36SDavid Xu 		/*
104286857b36SDavid Xu 		 * Find a free timer slot, skipping those reserved
104386857b36SDavid Xu 		 * for setitimer().
104486857b36SDavid Xu 		 */
104586857b36SDavid Xu 		for (id = 3; id < TIMER_MAX; id++)
104660354683SDavid Xu 			if (p->p_itimers->its_timers[id] == NULL)
104786857b36SDavid Xu 				break;
104886857b36SDavid Xu 		if (id == TIMER_MAX) {
104986857b36SDavid Xu 			PROC_UNLOCK(p);
105086857b36SDavid Xu 			error = EAGAIN;
105186857b36SDavid Xu 			goto out;
105286857b36SDavid Xu 		}
105386857b36SDavid Xu 	}
105486857b36SDavid Xu 	it->it_timerid = id;
105560354683SDavid Xu 	p->p_itimers->its_timers[id] = it;
105686857b36SDavid Xu 	if (evp != NULL)
105786857b36SDavid Xu 		it->it_sigev = *evp;
105886857b36SDavid Xu 	else {
105986857b36SDavid Xu 		it->it_sigev.sigev_notify = SIGEV_SIGNAL;
106086857b36SDavid Xu 		switch (clock_id) {
106186857b36SDavid Xu 		default:
106286857b36SDavid Xu 		case CLOCK_REALTIME:
106386857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGALRM;
106486857b36SDavid Xu 			break;
106586857b36SDavid Xu 		case CLOCK_VIRTUAL:
106686857b36SDavid Xu  			it->it_sigev.sigev_signo = SIGVTALRM;
106786857b36SDavid Xu 			break;
106886857b36SDavid Xu 		case CLOCK_PROF:
106986857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGPROF;
107086857b36SDavid Xu 			break;
107186857b36SDavid Xu 		}
10728f0371f1SDavid Xu 		it->it_sigev.sigev_value.sival_int = id;
107386857b36SDavid Xu 	}
107486857b36SDavid Xu 
107556c06c4bSDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
107656c06c4bSDavid Xu 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
107786857b36SDavid Xu 		it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
107886857b36SDavid Xu 		it->it_ksi.ksi_code = SI_TIMER;
107986857b36SDavid Xu 		it->it_ksi.ksi_value = it->it_sigev.sigev_value;
108086857b36SDavid Xu 		it->it_ksi.ksi_timerid = id;
108186857b36SDavid Xu 	}
108286857b36SDavid Xu 	PROC_UNLOCK(p);
108386857b36SDavid Xu 	*timerid = id;
108486857b36SDavid Xu 	return (0);
108586857b36SDavid Xu 
108686857b36SDavid Xu out:
108786857b36SDavid Xu 	ITIMER_LOCK(it);
108886857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
108986857b36SDavid Xu 	ITIMER_UNLOCK(it);
109086857b36SDavid Xu 	uma_zfree(itimer_zone, it);
109186857b36SDavid Xu 	return (error);
109286857b36SDavid Xu }
109386857b36SDavid Xu 
109486857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
109561d3a4efSDavid Xu struct ktimer_delete_args {
109661d3a4efSDavid Xu 	int timerid;
109786857b36SDavid Xu };
109886857b36SDavid Xu #endif
109986857b36SDavid Xu 
110086857b36SDavid Xu int
110161d3a4efSDavid Xu ktimer_delete(struct thread *td, struct ktimer_delete_args *uap)
110286857b36SDavid Xu {
110386857b36SDavid Xu 	return (kern_timer_delete(td, uap->timerid));
110486857b36SDavid Xu }
110586857b36SDavid Xu 
110686857b36SDavid Xu static struct itimer *
110761d3a4efSDavid Xu itimer_find(struct proc *p, int timerid, int include_deleting)
110886857b36SDavid Xu {
110986857b36SDavid Xu 	struct itimer *it;
111086857b36SDavid Xu 
111186857b36SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
111260354683SDavid Xu 	if ((p->p_itimers == NULL) || (timerid >= TIMER_MAX) ||
111360354683SDavid Xu 	    (it = p->p_itimers->its_timers[timerid]) == NULL) {
111486857b36SDavid Xu 		return (NULL);
111586857b36SDavid Xu 	}
111686857b36SDavid Xu 	ITIMER_LOCK(it);
111786857b36SDavid Xu 	if (!include_deleting && (it->it_flags & ITF_DELETING) != 0) {
111886857b36SDavid Xu 		ITIMER_UNLOCK(it);
111986857b36SDavid Xu 		it = NULL;
112086857b36SDavid Xu 	}
112186857b36SDavid Xu 	return (it);
112286857b36SDavid Xu }
112386857b36SDavid Xu 
112486857b36SDavid Xu static int
112561d3a4efSDavid Xu kern_timer_delete(struct thread *td, int timerid)
112686857b36SDavid Xu {
112786857b36SDavid Xu 	struct proc *p = td->td_proc;
112886857b36SDavid Xu 	struct itimer *it;
112986857b36SDavid Xu 
113086857b36SDavid Xu 	PROC_LOCK(p);
113186857b36SDavid Xu 	it = itimer_find(p, timerid, 0);
113286857b36SDavid Xu 	if (it == NULL) {
113386857b36SDavid Xu 		PROC_UNLOCK(p);
113486857b36SDavid Xu 		return (EINVAL);
113586857b36SDavid Xu 	}
113686857b36SDavid Xu 	PROC_UNLOCK(p);
113786857b36SDavid Xu 
113886857b36SDavid Xu 	it->it_flags |= ITF_DELETING;
113986857b36SDavid Xu 	while (it->it_usecount > 0) {
114086857b36SDavid Xu 		it->it_flags |= ITF_WANTED;
114186857b36SDavid Xu 		msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
114286857b36SDavid Xu 	}
114386857b36SDavid Xu 	it->it_flags &= ~ITF_WANTED;
114486857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
114586857b36SDavid Xu 	ITIMER_UNLOCK(it);
114686857b36SDavid Xu 
114786857b36SDavid Xu 	PROC_LOCK(p);
114886857b36SDavid Xu 	if (KSI_ONQ(&it->it_ksi))
114986857b36SDavid Xu 		sigqueue_take(&it->it_ksi);
115060354683SDavid Xu 	p->p_itimers->its_timers[timerid] = NULL;
115186857b36SDavid Xu 	PROC_UNLOCK(p);
115286857b36SDavid Xu 	uma_zfree(itimer_zone, it);
115386857b36SDavid Xu 	return (0);
115486857b36SDavid Xu }
115586857b36SDavid Xu 
115686857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
115761d3a4efSDavid Xu struct ktimer_settime_args {
115861d3a4efSDavid Xu 	int timerid;
115986857b36SDavid Xu 	int flags;
116086857b36SDavid Xu 	const struct itimerspec * value;
116186857b36SDavid Xu 	struct itimerspec * ovalue;
116286857b36SDavid Xu };
116386857b36SDavid Xu #endif
116486857b36SDavid Xu 
116586857b36SDavid Xu int
116661d3a4efSDavid Xu ktimer_settime(struct thread *td, struct ktimer_settime_args *uap)
116786857b36SDavid Xu {
116886857b36SDavid Xu 	struct proc *p = td->td_proc;
116986857b36SDavid Xu 	struct itimer *it;
117086857b36SDavid Xu 	struct itimerspec val, oval, *ovalp;
117186857b36SDavid Xu 	int error;
117286857b36SDavid Xu 
117386857b36SDavid Xu 	error = copyin(uap->value, &val, sizeof(val));
117486857b36SDavid Xu 	if (error != 0)
117586857b36SDavid Xu 		return (error);
117686857b36SDavid Xu 
117786857b36SDavid Xu 	if (uap->ovalue != NULL)
117886857b36SDavid Xu 		ovalp = &oval;
117986857b36SDavid Xu 	else
118086857b36SDavid Xu 		ovalp = NULL;
118186857b36SDavid Xu 
118286857b36SDavid Xu 	PROC_LOCK(p);
118386857b36SDavid Xu 	if (uap->timerid < 3 ||
118486857b36SDavid Xu 	    (it = itimer_find(p, uap->timerid, 0)) == NULL) {
118586857b36SDavid Xu 		PROC_UNLOCK(p);
118686857b36SDavid Xu 		error = EINVAL;
118786857b36SDavid Xu 	} else {
118886857b36SDavid Xu 		PROC_UNLOCK(p);
118986857b36SDavid Xu 		itimer_enter(it);
119086857b36SDavid Xu 		error = CLOCK_CALL(it->it_clockid, timer_settime,
119186857b36SDavid Xu 				(it, uap->flags, &val, ovalp));
119286857b36SDavid Xu 		itimer_leave(it);
119386857b36SDavid Xu 		ITIMER_UNLOCK(it);
119486857b36SDavid Xu 	}
119586857b36SDavid Xu 	if (error == 0 && uap->ovalue != NULL)
119686857b36SDavid Xu 		error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
119786857b36SDavid Xu 	return (error);
119886857b36SDavid Xu }
119986857b36SDavid Xu 
120086857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
120161d3a4efSDavid Xu struct ktimer_gettime_args {
120261d3a4efSDavid Xu 	int timerid;
120386857b36SDavid Xu 	struct itimerspec * value;
120486857b36SDavid Xu };
120586857b36SDavid Xu #endif
120686857b36SDavid Xu 
120786857b36SDavid Xu int
120861d3a4efSDavid Xu ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap)
120986857b36SDavid Xu {
121086857b36SDavid Xu 	struct proc *p = td->td_proc;
121186857b36SDavid Xu 	struct itimer *it;
121286857b36SDavid Xu 	struct itimerspec val;
121386857b36SDavid Xu 	int error;
121486857b36SDavid Xu 
121586857b36SDavid Xu 	PROC_LOCK(p);
121686857b36SDavid Xu 	if (uap->timerid < 3 ||
121786857b36SDavid Xu 	   (it = itimer_find(p, uap->timerid, 0)) == NULL) {
121886857b36SDavid Xu 		PROC_UNLOCK(p);
121986857b36SDavid Xu 		error = EINVAL;
122086857b36SDavid Xu 	} else {
122186857b36SDavid Xu 		PROC_UNLOCK(p);
122286857b36SDavid Xu 		itimer_enter(it);
122386857b36SDavid Xu 		error = CLOCK_CALL(it->it_clockid, timer_gettime,
122486857b36SDavid Xu 				(it, &val));
122586857b36SDavid Xu 		itimer_leave(it);
122686857b36SDavid Xu 		ITIMER_UNLOCK(it);
122786857b36SDavid Xu 	}
122886857b36SDavid Xu 	if (error == 0)
122986857b36SDavid Xu 		error = copyout(&val, uap->value, sizeof(val));
123086857b36SDavid Xu 	return (error);
123186857b36SDavid Xu }
123286857b36SDavid Xu 
123386857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
123486857b36SDavid Xu struct timer_getoverrun_args {
123561d3a4efSDavid Xu 	int timerid;
123686857b36SDavid Xu };
123786857b36SDavid Xu #endif
123886857b36SDavid Xu 
123986857b36SDavid Xu int
124061d3a4efSDavid Xu ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap)
124186857b36SDavid Xu {
124286857b36SDavid Xu 	struct proc *p = td->td_proc;
124386857b36SDavid Xu 	struct itimer *it;
124486857b36SDavid Xu 	int error ;
124586857b36SDavid Xu 
124686857b36SDavid Xu 	PROC_LOCK(p);
124786857b36SDavid Xu 	if (uap->timerid < 3 ||
124886857b36SDavid Xu 	    (it = itimer_find(p, uap->timerid, 0)) == NULL) {
124986857b36SDavid Xu 		PROC_UNLOCK(p);
125086857b36SDavid Xu 		error = EINVAL;
125186857b36SDavid Xu 	} else {
125286857b36SDavid Xu 		td->td_retval[0] = it->it_overrun_last;
125386857b36SDavid Xu 		ITIMER_UNLOCK(it);
125456c06c4bSDavid Xu 		PROC_UNLOCK(p);
125586857b36SDavid Xu 		error = 0;
125686857b36SDavid Xu 	}
125786857b36SDavid Xu 	return (error);
125886857b36SDavid Xu }
125986857b36SDavid Xu 
126086857b36SDavid Xu static int
126186857b36SDavid Xu realtimer_create(struct itimer *it)
126286857b36SDavid Xu {
126386857b36SDavid Xu 	callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
126486857b36SDavid Xu 	return (0);
126586857b36SDavid Xu }
126686857b36SDavid Xu 
126786857b36SDavid Xu static int
126886857b36SDavid Xu realtimer_delete(struct itimer *it)
126986857b36SDavid Xu {
127086857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
127186857b36SDavid Xu 	callout_stop(&it->it_callout);
127286857b36SDavid Xu 	return (0);
127386857b36SDavid Xu }
127486857b36SDavid Xu 
127586857b36SDavid Xu static int
127686857b36SDavid Xu realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
127786857b36SDavid Xu {
127856c06c4bSDavid Xu 	struct timespec cts;
127986857b36SDavid Xu 
128086857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
128186857b36SDavid Xu 
128256c06c4bSDavid Xu 	realtimer_clocktime(it->it_clockid, &cts);
128356c06c4bSDavid Xu 	*ovalue = it->it_time;
128486857b36SDavid Xu 	if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
128556c06c4bSDavid Xu 		timespecsub(&ovalue->it_value, &cts);
128686857b36SDavid Xu 		if (ovalue->it_value.tv_sec < 0 ||
128786857b36SDavid Xu 		    (ovalue->it_value.tv_sec == 0 &&
128886857b36SDavid Xu 		     ovalue->it_value.tv_nsec == 0)) {
128986857b36SDavid Xu 			ovalue->it_value.tv_sec  = 0;
129086857b36SDavid Xu 			ovalue->it_value.tv_nsec = 1;
129186857b36SDavid Xu 		}
129286857b36SDavid Xu 	}
129386857b36SDavid Xu 	return (0);
129486857b36SDavid Xu }
129586857b36SDavid Xu 
129686857b36SDavid Xu static int
129786857b36SDavid Xu realtimer_settime(struct itimer *it, int flags,
129886857b36SDavid Xu 	struct itimerspec *value, struct itimerspec *ovalue)
129986857b36SDavid Xu {
130056c06c4bSDavid Xu 	struct timespec cts, ts;
130156c06c4bSDavid Xu 	struct timeval tv;
130256c06c4bSDavid Xu 	struct itimerspec val;
130386857b36SDavid Xu 
130486857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
130586857b36SDavid Xu 
130656c06c4bSDavid Xu 	val = *value;
130756c06c4bSDavid Xu 	if (itimespecfix(&val.it_value))
130886857b36SDavid Xu 		return (EINVAL);
130986857b36SDavid Xu 
131056c06c4bSDavid Xu 	if (timespecisset(&val.it_value)) {
131156c06c4bSDavid Xu 		if (itimespecfix(&val.it_interval))
131286857b36SDavid Xu 			return (EINVAL);
131386857b36SDavid Xu 	} else {
131456c06c4bSDavid Xu 		timespecclear(&val.it_interval);
131586857b36SDavid Xu 	}
131686857b36SDavid Xu 
131786857b36SDavid Xu 	if (ovalue != NULL)
131886857b36SDavid Xu 		realtimer_gettime(it, ovalue);
131986857b36SDavid Xu 
132086857b36SDavid Xu 	it->it_time = val;
132156c06c4bSDavid Xu 	if (timespecisset(&val.it_value)) {
132256c06c4bSDavid Xu 		realtimer_clocktime(it->it_clockid, &cts);
132356c06c4bSDavid Xu 		ts = val.it_value;
132486857b36SDavid Xu 		if ((flags & TIMER_ABSTIME) == 0) {
132586857b36SDavid Xu 			/* Convert to absolute time. */
132656c06c4bSDavid Xu 			timespecadd(&it->it_time.it_value, &cts);
132786857b36SDavid Xu 		} else {
132856c06c4bSDavid Xu 			timespecsub(&ts, &cts);
132986857b36SDavid Xu 			/*
133056c06c4bSDavid Xu 			 * We don't care if ts is negative, tztohz will
133186857b36SDavid Xu 			 * fix it.
133286857b36SDavid Xu 			 */
133386857b36SDavid Xu 		}
133456c06c4bSDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
133556c06c4bSDavid Xu 		callout_reset(&it->it_callout, tvtohz(&tv),
133686857b36SDavid Xu 			realtimer_expire, it);
133786857b36SDavid Xu 	} else {
133886857b36SDavid Xu 		callout_stop(&it->it_callout);
133986857b36SDavid Xu 	}
134086857b36SDavid Xu 
134186857b36SDavid Xu 	return (0);
134286857b36SDavid Xu }
134386857b36SDavid Xu 
134486857b36SDavid Xu static void
134556c06c4bSDavid Xu realtimer_clocktime(clockid_t id, struct timespec *ts)
134686857b36SDavid Xu {
134786857b36SDavid Xu 	if (id == CLOCK_REALTIME)
134856c06c4bSDavid Xu 		getnanotime(ts);
134986857b36SDavid Xu 	else	/* CLOCK_MONOTONIC */
135056c06c4bSDavid Xu 		getnanouptime(ts);
135156c06c4bSDavid Xu }
135256c06c4bSDavid Xu 
135356c06c4bSDavid Xu int
135461d3a4efSDavid Xu itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi)
135556c06c4bSDavid Xu {
135656c06c4bSDavid Xu 	struct itimer *it;
135756c06c4bSDavid Xu 
135856c06c4bSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
135956c06c4bSDavid Xu 	it = itimer_find(p, timerid, 0);
136056c06c4bSDavid Xu 	if (it != NULL) {
136156c06c4bSDavid Xu 		ksi->ksi_overrun = it->it_overrun;
136256c06c4bSDavid Xu 		it->it_overrun_last = it->it_overrun;
136356c06c4bSDavid Xu 		it->it_overrun = 0;
136456c06c4bSDavid Xu 		ITIMER_UNLOCK(it);
136556c06c4bSDavid Xu 		return (0);
136656c06c4bSDavid Xu 	}
136756c06c4bSDavid Xu 	return (EINVAL);
136856c06c4bSDavid Xu }
136956c06c4bSDavid Xu 
137056c06c4bSDavid Xu int
137156c06c4bSDavid Xu itimespecfix(struct timespec *ts)
137256c06c4bSDavid Xu {
137356c06c4bSDavid Xu 
137456c06c4bSDavid Xu 	if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
137556c06c4bSDavid Xu 		return (EINVAL);
137656c06c4bSDavid Xu 	if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
137756c06c4bSDavid Xu 		ts->tv_nsec = tick * 1000;
137856c06c4bSDavid Xu 	return (0);
137986857b36SDavid Xu }
138086857b36SDavid Xu 
138186857b36SDavid Xu static void
138286857b36SDavid Xu realtimer_event_hook(struct proc *p, clockid_t clock_id, int event)
138386857b36SDavid Xu {
138486857b36SDavid Xu 	struct itimers *its;
138586857b36SDavid Xu 	struct itimer  *it;
138686857b36SDavid Xu 	int i;
138786857b36SDavid Xu 
138886857b36SDavid Xu 	/*
138986857b36SDavid Xu 	 * Timer 0 (ITIMER_REAL) is XSI interval timer, according to POSIX
139086857b36SDavid Xu 	 * specification, it should be inherited by new process image.
139186857b36SDavid Xu 	 */
139286857b36SDavid Xu 	if (event == ITIMER_EV_EXEC)
139386857b36SDavid Xu 		i = 1;
139486857b36SDavid Xu 	else
139586857b36SDavid Xu 		i = 0;
139660354683SDavid Xu 	its = p->p_itimers;
139786857b36SDavid Xu 	for (; i < TIMER_MAX; i++) {
139886857b36SDavid Xu 		if ((it = its->its_timers[i]) != NULL &&
139986857b36SDavid Xu 		     it->it_clockid == clock_id) {
140086857b36SDavid Xu 			ITIMER_LOCK(it);
140186857b36SDavid Xu 			callout_stop(&it->it_callout);
140286857b36SDavid Xu 			ITIMER_UNLOCK(it);
140386857b36SDavid Xu 		}
140486857b36SDavid Xu 	}
140586857b36SDavid Xu }
140686857b36SDavid Xu 
140786857b36SDavid Xu /* Timeout callback for realtime timer */
140886857b36SDavid Xu static void
140986857b36SDavid Xu realtimer_expire(void *arg)
141086857b36SDavid Xu {
141156c06c4bSDavid Xu 	struct timespec cts, ts;
141256c06c4bSDavid Xu 	struct timeval tv;
141386857b36SDavid Xu 	struct itimer *it;
141486857b36SDavid Xu 	struct proc *p;
141586857b36SDavid Xu 
141686857b36SDavid Xu 	it = (struct itimer *)arg;
141786857b36SDavid Xu 	p = it->it_proc;
141886857b36SDavid Xu 
141956c06c4bSDavid Xu 	realtimer_clocktime(it->it_clockid, &cts);
142086857b36SDavid Xu 	/* Only fire if time is reached. */
142156c06c4bSDavid Xu 	if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
142256c06c4bSDavid Xu 		if (timespecisset(&it->it_time.it_interval)) {
142356c06c4bSDavid Xu 			timespecadd(&it->it_time.it_value,
142486857b36SDavid Xu 				    &it->it_time.it_interval);
142556c06c4bSDavid Xu 			while (timespeccmp(&cts, &it->it_time.it_value, >=)) {
142677e718f7SDavid Xu 				if (it->it_overrun < INT_MAX)
142786857b36SDavid Xu 					it->it_overrun++;
142877e718f7SDavid Xu 				else
142977e718f7SDavid Xu 					it->it_ksi.ksi_errno = ERANGE;
143056c06c4bSDavid Xu 				timespecadd(&it->it_time.it_value,
143186857b36SDavid Xu 					    &it->it_time.it_interval);
143286857b36SDavid Xu 			}
143386857b36SDavid Xu 		} else {
143486857b36SDavid Xu 			/* single shot timer ? */
143556c06c4bSDavid Xu 			timespecclear(&it->it_time.it_value);
143686857b36SDavid Xu 		}
143756c06c4bSDavid Xu 		if (timespecisset(&it->it_time.it_value)) {
143856c06c4bSDavid Xu 			ts = it->it_time.it_value;
143956c06c4bSDavid Xu 			timespecsub(&ts, &cts);
144056c06c4bSDavid Xu 			TIMESPEC_TO_TIMEVAL(&tv, &ts);
144156c06c4bSDavid Xu 			callout_reset(&it->it_callout, tvtohz(&tv),
144286857b36SDavid Xu 				 realtimer_expire, it);
144386857b36SDavid Xu 		}
144486857b36SDavid Xu 		ITIMER_UNLOCK(it);
144586857b36SDavid Xu 		itimer_fire(it);
144686857b36SDavid Xu 		ITIMER_LOCK(it);
144756c06c4bSDavid Xu 	} else if (timespecisset(&it->it_time.it_value)) {
144856c06c4bSDavid Xu 		ts = it->it_time.it_value;
144956c06c4bSDavid Xu 		timespecsub(&ts, &cts);
145056c06c4bSDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
145156c06c4bSDavid Xu 		callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
145286857b36SDavid Xu  			it);
145386857b36SDavid Xu 	}
145486857b36SDavid Xu }
145586857b36SDavid Xu 
145686857b36SDavid Xu void
145786857b36SDavid Xu itimer_fire(struct itimer *it)
145886857b36SDavid Xu {
145986857b36SDavid Xu 	struct proc *p = it->it_proc;
14606d7b314bSDavid Xu 	int ret;
146186857b36SDavid Xu 
146256c06c4bSDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
146356c06c4bSDavid Xu 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
146486857b36SDavid Xu 		PROC_LOCK(p);
14656d7b314bSDavid Xu 		if (!KSI_ONQ(&it->it_ksi)) {
146677e718f7SDavid Xu 			it->it_ksi.ksi_errno = 0;
14676d7b314bSDavid Xu 			ret = psignal_event(p, &it->it_sigev, &it->it_ksi);
14686d7b314bSDavid Xu 			if (__predict_false(ret != 0)) {
146986857b36SDavid Xu 				it->it_overrun++;
147056c06c4bSDavid Xu 				/*
147156c06c4bSDavid Xu 				 * Broken userland code, thread went
147256c06c4bSDavid Xu 				 * away, disarm the timer.
147356c06c4bSDavid Xu 				 */
14746d7b314bSDavid Xu 				if (ret == ESRCH) {
147556c06c4bSDavid Xu 					ITIMER_LOCK(it);
147656c06c4bSDavid Xu 					timespecclear(&it->it_time.it_value);
147756c06c4bSDavid Xu 					timespecclear(&it->it_time.it_interval);
147856c06c4bSDavid Xu 					callout_stop(&it->it_callout);
147956c06c4bSDavid Xu 					ITIMER_UNLOCK(it);
14806d7b314bSDavid Xu 				}
148156c06c4bSDavid Xu 			}
148256c06c4bSDavid Xu 		} else {
148377e718f7SDavid Xu 			if (it->it_overrun < INT_MAX)
14846d7b314bSDavid Xu 				it->it_overrun++;
148577e718f7SDavid Xu 			else
148677e718f7SDavid Xu 				it->it_ksi.ksi_errno = ERANGE;
148756c06c4bSDavid Xu 		}
148886857b36SDavid Xu 		PROC_UNLOCK(p);
148986857b36SDavid Xu 	}
149086857b36SDavid Xu }
149186857b36SDavid Xu 
149286857b36SDavid Xu static void
149386857b36SDavid Xu itimers_alloc(struct proc *p)
149486857b36SDavid Xu {
149560354683SDavid Xu 	struct itimers *its;
149660354683SDavid Xu 	int i;
149786857b36SDavid Xu 
149860354683SDavid Xu 	its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
149986857b36SDavid Xu 	LIST_INIT(&its->its_virtual);
150086857b36SDavid Xu 	LIST_INIT(&its->its_prof);
150186857b36SDavid Xu 	TAILQ_INIT(&its->its_worklist);
150260354683SDavid Xu 	for (i = 0; i < TIMER_MAX; i++)
150360354683SDavid Xu 		its->its_timers[i] = NULL;
150460354683SDavid Xu 	PROC_LOCK(p);
150560354683SDavid Xu 	if (p->p_itimers == NULL) {
150660354683SDavid Xu 		p->p_itimers = its;
150760354683SDavid Xu 		PROC_UNLOCK(p);
150860354683SDavid Xu 	}
150960354683SDavid Xu 	else {
151060354683SDavid Xu 		PROC_UNLOCK(p);
151160354683SDavid Xu 		free(its, M_SUBPROC);
151260354683SDavid Xu 	}
151386857b36SDavid Xu }
151486857b36SDavid Xu 
1515993182e5SAlexander Leidinger static void
1516993182e5SAlexander Leidinger itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
1517993182e5SAlexander Leidinger {
1518993182e5SAlexander Leidinger    	itimers_event_hook_exit(arg, p);
1519993182e5SAlexander Leidinger }
1520993182e5SAlexander Leidinger 
152186857b36SDavid Xu /* Clean up timers when some process events are being triggered. */
1522d26b1a1fSDavid Xu static void
1523993182e5SAlexander Leidinger itimers_event_hook_exit(void *arg, struct proc *p)
152486857b36SDavid Xu {
152586857b36SDavid Xu 	struct itimers *its;
152686857b36SDavid Xu 	struct itimer *it;
15273e70c6f0SDavid Xu 	int event = (int)(intptr_t)arg;
152886857b36SDavid Xu 	int i;
152986857b36SDavid Xu 
153060354683SDavid Xu 	if (p->p_itimers != NULL) {
153160354683SDavid Xu 		its = p->p_itimers;
153286857b36SDavid Xu 		for (i = 0; i < MAX_CLOCKS; ++i) {
153386857b36SDavid Xu 			if (posix_clocks[i].event_hook != NULL)
153486857b36SDavid Xu 				CLOCK_CALL(i, event_hook, (p, i, event));
153586857b36SDavid Xu 		}
153686857b36SDavid Xu 		/*
153786857b36SDavid Xu 		 * According to susv3, XSI interval timers should be inherited
153886857b36SDavid Xu 		 * by new image.
153986857b36SDavid Xu 		 */
154086857b36SDavid Xu 		if (event == ITIMER_EV_EXEC)
154186857b36SDavid Xu 			i = 3;
154286857b36SDavid Xu 		else if (event == ITIMER_EV_EXIT)
154386857b36SDavid Xu 			i = 0;
154486857b36SDavid Xu 		else
154586857b36SDavid Xu 			panic("unhandled event");
154686857b36SDavid Xu 		for (; i < TIMER_MAX; ++i) {
154786857b36SDavid Xu 			if ((it = its->its_timers[i]) != NULL) {
154886857b36SDavid Xu 				PROC_LOCK(p);
154986857b36SDavid Xu 				if (KSI_ONQ(&it->it_ksi))
155086857b36SDavid Xu 					sigqueue_take(&it->it_ksi);
155186857b36SDavid Xu 				PROC_UNLOCK(p);
155286857b36SDavid Xu 				uma_zfree(itimer_zone, its->its_timers[i]);
155386857b36SDavid Xu 				its->its_timers[i] = NULL;
155486857b36SDavid Xu 			}
155586857b36SDavid Xu 		}
155686857b36SDavid Xu 		if (its->its_timers[0] == NULL &&
155786857b36SDavid Xu 		    its->its_timers[1] == NULL &&
155886857b36SDavid Xu 		    its->its_timers[2] == NULL) {
155960354683SDavid Xu 			free(its, M_SUBPROC);
156060354683SDavid Xu 			p->p_itimers = NULL;
156186857b36SDavid Xu 		}
156286857b36SDavid Xu 	}
156386857b36SDavid Xu }
1564