xref: /freebsd/sys/kern/kern_time.c (revision acd3428b7d3e94cef0e1881c868cb4b131d4ff41)
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>
48efa42cbcSPaul Saab #include <sys/syscallsubr.h>
4977e718f7SDavid Xu #include <sys/sysctl.h>
5094c8fcd8SPeter Wemm #include <sys/sysent.h>
51acd3428bSRobert Watson #include <sys/priv.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 
60aed55708SRobert Watson #include <security/mac/mac_framework.h>
61aed55708SRobert Watson 
625b870b7bSPeter Wemm #include <vm/vm.h>
635b870b7bSPeter Wemm #include <vm/vm_extern.h>
64df8bae1dSRodney W. Grimes 
6586857b36SDavid Xu #define MAX_CLOCKS 	(CLOCK_MONOTONIC+1)
6686857b36SDavid Xu 
6786857b36SDavid Xu static struct kclock	posix_clocks[MAX_CLOCKS];
6886857b36SDavid Xu static uma_zone_t	itimer_zone = NULL;
6986857b36SDavid Xu 
70df8bae1dSRodney W. Grimes /*
71df8bae1dSRodney W. Grimes  * Time of day and interval timer support.
72df8bae1dSRodney W. Grimes  *
73df8bae1dSRodney W. Grimes  * These routines provide the kernel entry points to get and set
74df8bae1dSRodney W. Grimes  * the time-of-day and per-process interval timers.  Subroutines
75df8bae1dSRodney W. Grimes  * here provide support for adding and subtracting timeval structures
76df8bae1dSRodney W. Grimes  * and decrementing interval timers, optionally reloading the interval
77df8bae1dSRodney W. Grimes  * timers when they expire.
78df8bae1dSRodney W. Grimes  */
79df8bae1dSRodney W. Grimes 
807edfb592SJohn Baldwin static int	settime(struct thread *, struct timeval *);
814d77a549SAlfred Perlstein static void	timevalfix(struct timeval *);
824d77a549SAlfred Perlstein static void	no_lease_updatetime(int);
8394c8fcd8SPeter Wemm 
8486857b36SDavid Xu static void	itimer_start(void);
8586857b36SDavid Xu static int	itimer_init(void *, int, int);
8686857b36SDavid Xu static void	itimer_fini(void *, int);
8786857b36SDavid Xu static void	itimer_enter(struct itimer *);
8886857b36SDavid Xu static void	itimer_leave(struct itimer *);
8961d3a4efSDavid Xu static struct itimer *itimer_find(struct proc *, int, int);
9086857b36SDavid Xu static void	itimers_alloc(struct proc *);
91993182e5SAlexander Leidinger static void	itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp);
92993182e5SAlexander Leidinger static void	itimers_event_hook_exit(void *arg, struct proc *p);
9386857b36SDavid Xu static int	realtimer_create(struct itimer *);
9486857b36SDavid Xu static int	realtimer_gettime(struct itimer *, struct itimerspec *);
9586857b36SDavid Xu static int	realtimer_settime(struct itimer *, int,
9686857b36SDavid Xu 			struct itimerspec *, struct itimerspec *);
9786857b36SDavid Xu static int	realtimer_delete(struct itimer *);
9856c06c4bSDavid Xu static void	realtimer_clocktime(clockid_t, struct timespec *);
9986857b36SDavid Xu static void	realtimer_expire(void *);
10086857b36SDavid Xu static void	realtimer_event_hook(struct proc *, clockid_t, int event);
10186857b36SDavid Xu static int	kern_timer_create(struct thread *, clockid_t,
10261d3a4efSDavid Xu 			struct sigevent *, int *, int);
10361d3a4efSDavid Xu static int	kern_timer_delete(struct thread *, int);
10486857b36SDavid Xu 
10586857b36SDavid Xu int		register_posix_clock(int, struct kclock *);
10686857b36SDavid Xu void		itimer_fire(struct itimer *it);
10756c06c4bSDavid Xu int		itimespecfix(struct timespec *ts);
10886857b36SDavid Xu 
10986857b36SDavid Xu #define CLOCK_CALL(clock, call, arglist)		\
11086857b36SDavid Xu 	((*posix_clocks[clock].call) arglist)
11186857b36SDavid Xu 
11286857b36SDavid Xu SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL);
11386857b36SDavid Xu 
11486857b36SDavid Xu 
1151b09ae77SPoul-Henning Kamp static void
1161b09ae77SPoul-Henning Kamp no_lease_updatetime(deltat)
1171b09ae77SPoul-Henning Kamp 	int deltat;
1181b09ae77SPoul-Henning Kamp {
1191b09ae77SPoul-Henning Kamp }
1201b09ae77SPoul-Henning Kamp 
1214d77a549SAlfred Perlstein void (*lease_updatetime)(int)  = no_lease_updatetime;
1221b09ae77SPoul-Henning Kamp 
12394c8fcd8SPeter Wemm static int
12491afe087SPoul-Henning Kamp settime(struct thread *td, struct timeval *tv)
12594c8fcd8SPeter Wemm {
126fcae3aa6SNick Sayer 	struct timeval delta, tv1, tv2;
127c0bd94a7SNick Sayer 	static struct timeval maxtime, laststep;
1287ec73f64SPoul-Henning Kamp 	struct timespec ts;
12994c8fcd8SPeter Wemm 	int s;
13094c8fcd8SPeter Wemm 
131708e7684SPeter Wemm 	s = splclock();
1329c8fff87SBruce Evans 	microtime(&tv1);
13300af9731SPoul-Henning Kamp 	delta = *tv;
13400af9731SPoul-Henning Kamp 	timevalsub(&delta, &tv1);
13594c8fcd8SPeter Wemm 
13694c8fcd8SPeter Wemm 	/*
1379c8fff87SBruce Evans 	 * If the system is secure, we do not allow the time to be
138fcae3aa6SNick Sayer 	 * set to a value earlier than 1 second less than the highest
139fcae3aa6SNick Sayer 	 * time we have yet seen. The worst a miscreant can do in
140fcae3aa6SNick Sayer 	 * this circumstance is "freeze" time. He couldn't go
141fcae3aa6SNick Sayer 	 * back to the past.
142c0bd94a7SNick Sayer 	 *
143c0bd94a7SNick Sayer 	 * We similarly do not allow the clock to be stepped more
144c0bd94a7SNick Sayer 	 * than one second, nor more than once per second. This allows
145c0bd94a7SNick Sayer 	 * a miscreant to make the clock march double-time, but no worse.
14694c8fcd8SPeter Wemm 	 */
1477edfb592SJohn Baldwin 	if (securelevel_gt(td->td_ucred, 1) != 0) {
148fcae3aa6SNick Sayer 		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
1493f92429aSMatt Jacob 			/*
150c0bd94a7SNick Sayer 			 * Update maxtime to latest time we've seen.
1513f92429aSMatt Jacob 			 */
152fcae3aa6SNick Sayer 			if (tv1.tv_sec > maxtime.tv_sec)
153fcae3aa6SNick Sayer 				maxtime = tv1;
154fcae3aa6SNick Sayer 			tv2 = *tv;
155fcae3aa6SNick Sayer 			timevalsub(&tv2, &maxtime);
156fcae3aa6SNick Sayer 			if (tv2.tv_sec < -1) {
1573f92429aSMatt Jacob 				tv->tv_sec = maxtime.tv_sec - 1;
158fcae3aa6SNick Sayer 				printf("Time adjustment clamped to -1 second\n");
159fcae3aa6SNick Sayer 			}
1603f92429aSMatt Jacob 		} else {
161c0bd94a7SNick Sayer 			if (tv1.tv_sec == laststep.tv_sec) {
162c0bd94a7SNick Sayer 				splx(s);
163c0bd94a7SNick Sayer 				return (EPERM);
164c0bd94a7SNick Sayer 			}
165c0bd94a7SNick Sayer 			if (delta.tv_sec > 1) {
166c0bd94a7SNick Sayer 				tv->tv_sec = tv1.tv_sec + 1;
167c0bd94a7SNick Sayer 				printf("Time adjustment clamped to +1 second\n");
168c0bd94a7SNick Sayer 			}
169c0bd94a7SNick Sayer 			laststep = *tv;
170fcae3aa6SNick Sayer 		}
1719c8fff87SBruce Evans 	}
1729c8fff87SBruce Evans 
1737ec73f64SPoul-Henning Kamp 	ts.tv_sec = tv->tv_sec;
1747ec73f64SPoul-Henning Kamp 	ts.tv_nsec = tv->tv_usec * 1000;
1757edfb592SJohn Baldwin 	mtx_lock(&Giant);
17691266b96SPoul-Henning Kamp 	tc_setclock(&ts);
17794c8fcd8SPeter Wemm 	(void) splsoftclock();
17894c8fcd8SPeter Wemm 	lease_updatetime(delta.tv_sec);
17994c8fcd8SPeter Wemm 	splx(s);
18094c8fcd8SPeter Wemm 	resettodr();
1817edfb592SJohn Baldwin 	mtx_unlock(&Giant);
18294c8fcd8SPeter Wemm 	return (0);
18394c8fcd8SPeter Wemm }
18494c8fcd8SPeter Wemm 
18594c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
18694c8fcd8SPeter Wemm struct clock_gettime_args {
18794c8fcd8SPeter Wemm 	clockid_t clock_id;
18894c8fcd8SPeter Wemm 	struct	timespec *tp;
18994c8fcd8SPeter Wemm };
19094c8fcd8SPeter Wemm #endif
191708e7684SPeter Wemm 
192fb99ab88SMatthew Dillon /*
193fb99ab88SMatthew Dillon  * MPSAFE
194fb99ab88SMatthew Dillon  */
19594c8fcd8SPeter Wemm /* ARGSUSED */
19694c8fcd8SPeter Wemm int
19791afe087SPoul-Henning Kamp clock_gettime(struct thread *td, struct clock_gettime_args *uap)
19894c8fcd8SPeter Wemm {
19994c8fcd8SPeter Wemm 	struct timespec ats;
200f0b479cdSPaul Saab 	int error;
201f0b479cdSPaul Saab 
202f0b479cdSPaul Saab 	error = kern_clock_gettime(td, uap->clock_id, &ats);
203f0b479cdSPaul Saab 	if (error == 0)
204f0b479cdSPaul Saab 		error = copyout(&ats, uap->tp, sizeof(ats));
205f0b479cdSPaul Saab 
206f0b479cdSPaul Saab 	return (error);
207f0b479cdSPaul Saab }
208f0b479cdSPaul Saab 
209f0b479cdSPaul Saab int
210f0b479cdSPaul Saab kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
211f0b479cdSPaul Saab {
212de0a9241SKelly Yancey 	struct timeval sys, user;
21378c85e8dSJohn Baldwin 	struct proc *p;
21494c8fcd8SPeter Wemm 
21578c85e8dSJohn Baldwin 	p = td->td_proc;
216f0b479cdSPaul Saab 	switch (clock_id) {
2175e758b95SRobert Watson 	case CLOCK_REALTIME:		/* Default to precise. */
2185e758b95SRobert Watson 	case CLOCK_REALTIME_PRECISE:
219f0b479cdSPaul Saab 		nanotime(ats);
220b8817154SKelly Yancey 		break;
2215e758b95SRobert Watson 	case CLOCK_REALTIME_FAST:
2225e758b95SRobert Watson 		getnanotime(ats);
2235e758b95SRobert Watson 		break;
224b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
22578c85e8dSJohn Baldwin 		PROC_LOCK(p);
22678c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
22778c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
228f0b479cdSPaul Saab 		TIMEVAL_TO_TIMESPEC(&user, ats);
229b8817154SKelly Yancey 		break;
230b8817154SKelly Yancey 	case CLOCK_PROF:
23178c85e8dSJohn Baldwin 		PROC_LOCK(p);
23278c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
23378c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
234de0a9241SKelly Yancey 		timevaladd(&user, &sys);
235f0b479cdSPaul Saab 		TIMEVAL_TO_TIMESPEC(&user, ats);
236de0a9241SKelly Yancey 		break;
2375e758b95SRobert Watson 	case CLOCK_MONOTONIC:		/* Default to precise. */
2385e758b95SRobert Watson 	case CLOCK_MONOTONIC_PRECISE:
2395eefd889SAndre Oppermann 	case CLOCK_UPTIME:
2405e758b95SRobert Watson 	case CLOCK_UPTIME_PRECISE:
241f0b479cdSPaul Saab 		nanouptime(ats);
242b8817154SKelly Yancey 		break;
2435e758b95SRobert Watson 	case CLOCK_UPTIME_FAST:
2445e758b95SRobert Watson 	case CLOCK_MONOTONIC_FAST:
2455e758b95SRobert Watson 		getnanouptime(ats);
2465e758b95SRobert Watson 		break;
2475e758b95SRobert Watson 	case CLOCK_SECOND:
2485e758b95SRobert Watson 		ats->tv_sec = time_second;
2495e758b95SRobert Watson 		ats->tv_nsec = 0;
2505e758b95SRobert Watson 		break;
251b8817154SKelly Yancey 	default:
2525cb3dc8fSPoul-Henning Kamp 		return (EINVAL);
253b8817154SKelly Yancey 	}
254f0b479cdSPaul Saab 	return (0);
25594c8fcd8SPeter Wemm }
25694c8fcd8SPeter Wemm 
25794c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
25894c8fcd8SPeter Wemm struct clock_settime_args {
25994c8fcd8SPeter Wemm 	clockid_t clock_id;
26094c8fcd8SPeter Wemm 	const struct	timespec *tp;
26194c8fcd8SPeter Wemm };
26294c8fcd8SPeter Wemm #endif
263708e7684SPeter Wemm 
264fb99ab88SMatthew Dillon /*
265fb99ab88SMatthew Dillon  * MPSAFE
266fb99ab88SMatthew Dillon  */
26794c8fcd8SPeter Wemm /* ARGSUSED */
26894c8fcd8SPeter Wemm int
26991afe087SPoul-Henning Kamp clock_settime(struct thread *td, struct clock_settime_args *uap)
27094c8fcd8SPeter Wemm {
27194c8fcd8SPeter Wemm 	struct timespec ats;
27294c8fcd8SPeter Wemm 	int error;
27394c8fcd8SPeter Wemm 
274f0b479cdSPaul Saab 	if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
275f0b479cdSPaul Saab 		return (error);
276f0b479cdSPaul Saab 	return (kern_clock_settime(td, uap->clock_id, &ats));
277f0b479cdSPaul Saab }
278f0b479cdSPaul Saab 
279f0b479cdSPaul Saab int
280f0b479cdSPaul Saab kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
281f0b479cdSPaul Saab {
282f0b479cdSPaul Saab 	struct timeval atv;
283f0b479cdSPaul Saab 	int error;
284f0b479cdSPaul Saab 
2854b8d5f2dSRobert Watson #ifdef MAC
2864b8d5f2dSRobert Watson 	error = mac_check_system_settime(td->td_ucred);
2874b8d5f2dSRobert Watson 	if (error)
2884b8d5f2dSRobert Watson 		return (error);
2894b8d5f2dSRobert Watson #endif
290acd3428bSRobert Watson 	if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
2917edfb592SJohn Baldwin 		return (error);
292f0b479cdSPaul Saab 	if (clock_id != CLOCK_REALTIME)
2937edfb592SJohn Baldwin 		return (EINVAL);
294f0b479cdSPaul Saab 	if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000)
2957edfb592SJohn Baldwin 		return (EINVAL);
296a0502b19SPoul-Henning Kamp 	/* XXX Don't convert nsec->usec and back */
297f0b479cdSPaul Saab 	TIMESPEC_TO_TIMEVAL(&atv, ats);
2987edfb592SJohn Baldwin 	error = settime(td, &atv);
29994c8fcd8SPeter Wemm 	return (error);
30094c8fcd8SPeter Wemm }
30194c8fcd8SPeter Wemm 
30294c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
30394c8fcd8SPeter Wemm struct clock_getres_args {
30494c8fcd8SPeter Wemm 	clockid_t clock_id;
30594c8fcd8SPeter Wemm 	struct	timespec *tp;
30694c8fcd8SPeter Wemm };
30794c8fcd8SPeter Wemm #endif
308708e7684SPeter Wemm 
30994c8fcd8SPeter Wemm int
31091afe087SPoul-Henning Kamp clock_getres(struct thread *td, struct clock_getres_args *uap)
31194c8fcd8SPeter Wemm {
31294c8fcd8SPeter Wemm 	struct timespec ts;
313f0b479cdSPaul Saab 	int error;
31494c8fcd8SPeter Wemm 
315f0b479cdSPaul Saab 	if (uap->tp == NULL)
316f0b479cdSPaul Saab 		return (0);
317f0b479cdSPaul Saab 
318f0b479cdSPaul Saab 	error = kern_clock_getres(td, uap->clock_id, &ts);
319f0b479cdSPaul Saab 	if (error == 0)
320f0b479cdSPaul Saab 		error = copyout(&ts, uap->tp, sizeof(ts));
321f0b479cdSPaul Saab 	return (error);
322f0b479cdSPaul Saab }
323f0b479cdSPaul Saab 
324f0b479cdSPaul Saab int
325f0b479cdSPaul Saab kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
326f0b479cdSPaul Saab {
327f0b479cdSPaul Saab 
328f0b479cdSPaul Saab 	ts->tv_sec = 0;
329f0b479cdSPaul Saab 	switch (clock_id) {
330b8817154SKelly Yancey 	case CLOCK_REALTIME:
3315e758b95SRobert Watson 	case CLOCK_REALTIME_FAST:
3325e758b95SRobert Watson 	case CLOCK_REALTIME_PRECISE:
333b8817154SKelly Yancey 	case CLOCK_MONOTONIC:
3345e758b95SRobert Watson 	case CLOCK_MONOTONIC_FAST:
3355e758b95SRobert Watson 	case CLOCK_MONOTONIC_PRECISE:
3365eefd889SAndre Oppermann 	case CLOCK_UPTIME:
3375e758b95SRobert Watson 	case CLOCK_UPTIME_FAST:
3385e758b95SRobert Watson 	case CLOCK_UPTIME_PRECISE:
339ac0653dcSBruce Evans 		/*
340ac0653dcSBruce Evans 		 * Round up the result of the division cheaply by adding 1.
341ac0653dcSBruce Evans 		 * Rounding up is especially important if rounding down
342ac0653dcSBruce Evans 		 * would give 0.  Perfect rounding is unimportant.
343ac0653dcSBruce Evans 		 */
344f0b479cdSPaul Saab 		ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
345b8817154SKelly Yancey 		break;
346b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
347b8817154SKelly Yancey 	case CLOCK_PROF:
348b8817154SKelly Yancey 		/* Accurately round up here because we can do so cheaply. */
349f0b479cdSPaul Saab 		ts->tv_nsec = (1000000000 + hz - 1) / hz;
350b8817154SKelly Yancey 		break;
3515e758b95SRobert Watson 	case CLOCK_SECOND:
3525e758b95SRobert Watson 		ts->tv_sec = 1;
3535e758b95SRobert Watson 		ts->tv_nsec = 0;
3545e758b95SRobert Watson 		break;
355b8817154SKelly Yancey 	default:
356b8817154SKelly Yancey 		return (EINVAL);
35794c8fcd8SPeter Wemm 	}
358de0a9241SKelly Yancey 	return (0);
35994c8fcd8SPeter Wemm }
36094c8fcd8SPeter Wemm 
36194c8fcd8SPeter Wemm static int nanowait;
36294c8fcd8SPeter Wemm 
3637fdf2c85SPaul Saab int
3647fdf2c85SPaul Saab kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
3655b870b7bSPeter Wemm {
3665704ba6aSPoul-Henning Kamp 	struct timespec ts, ts2, ts3;
36733841826SPoul-Henning Kamp 	struct timeval tv;
36833841826SPoul-Henning Kamp 	int error;
3695b870b7bSPeter Wemm 
3707d7fb492SBruce Evans 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
371708e7684SPeter Wemm 		return (EINVAL);
372d254af07SMatthew Dillon 	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
3737d7fb492SBruce Evans 		return (0);
374c21410e1SPoul-Henning Kamp 	getnanouptime(&ts);
37500af9731SPoul-Henning Kamp 	timespecadd(&ts, rqt);
37633841826SPoul-Henning Kamp 	TIMESPEC_TO_TIMEVAL(&tv, rqt);
37733841826SPoul-Henning Kamp 	for (;;) {
37833841826SPoul-Henning Kamp 		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
37933841826SPoul-Henning Kamp 		    tvtohz(&tv));
380c21410e1SPoul-Henning Kamp 		getnanouptime(&ts2);
38133841826SPoul-Henning Kamp 		if (error != EWOULDBLOCK) {
38233841826SPoul-Henning Kamp 			if (error == ERESTART)
38394c8fcd8SPeter Wemm 				error = EINTR;
38433841826SPoul-Henning Kamp 			if (rmt != NULL) {
38533841826SPoul-Henning Kamp 				timespecsub(&ts, &ts2);
38633841826SPoul-Henning Kamp 				if (ts.tv_sec < 0)
38733841826SPoul-Henning Kamp 					timespecclear(&ts);
38800af9731SPoul-Henning Kamp 				*rmt = ts;
38900af9731SPoul-Henning Kamp 			}
39033841826SPoul-Henning Kamp 			return (error);
39133841826SPoul-Henning Kamp 		}
39233841826SPoul-Henning Kamp 		if (timespeccmp(&ts2, &ts, >=))
39333841826SPoul-Henning Kamp 			return (0);
3945704ba6aSPoul-Henning Kamp 		ts3 = ts;
3955704ba6aSPoul-Henning Kamp 		timespecsub(&ts3, &ts2);
3965704ba6aSPoul-Henning Kamp 		TIMESPEC_TO_TIMEVAL(&tv, &ts3);
39733841826SPoul-Henning Kamp 	}
3985b870b7bSPeter Wemm }
39994c8fcd8SPeter Wemm 
4005b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
4015b870b7bSPeter Wemm struct nanosleep_args {
4025b870b7bSPeter Wemm 	struct	timespec *rqtp;
4035b870b7bSPeter Wemm 	struct	timespec *rmtp;
4045b870b7bSPeter Wemm };
4055b870b7bSPeter Wemm #endif
4065b870b7bSPeter Wemm 
407fb99ab88SMatthew Dillon /*
408fb99ab88SMatthew Dillon  * MPSAFE
409fb99ab88SMatthew Dillon  */
4105b870b7bSPeter Wemm /* ARGSUSED */
4115b870b7bSPeter Wemm int
41291afe087SPoul-Henning Kamp nanosleep(struct thread *td, struct nanosleep_args *uap)
4135b870b7bSPeter Wemm {
4145b870b7bSPeter Wemm 	struct timespec rmt, rqt;
415fb99ab88SMatthew Dillon 	int error;
4165b870b7bSPeter Wemm 
417d1e405c5SAlfred Perlstein 	error = copyin(uap->rqtp, &rqt, sizeof(rqt));
4185b870b7bSPeter Wemm 	if (error)
4195b870b7bSPeter Wemm 		return (error);
420fb99ab88SMatthew Dillon 
42131f3e2adSAlfred Perlstein 	if (uap->rmtp &&
42231f3e2adSAlfred Perlstein 	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
42331f3e2adSAlfred Perlstein 			return (EFAULT);
4247fdf2c85SPaul Saab 	error = kern_nanosleep(td, &rqt, &rmt);
425d1e405c5SAlfred Perlstein 	if (error && uap->rmtp) {
426fb99ab88SMatthew Dillon 		int error2;
427fb99ab88SMatthew Dillon 
428d1e405c5SAlfred Perlstein 		error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
42931f3e2adSAlfred Perlstein 		if (error2)
430fb99ab88SMatthew Dillon 			error = error2;
431708e7684SPeter Wemm 	}
432708e7684SPeter Wemm 	return (error);
43394c8fcd8SPeter Wemm }
43494c8fcd8SPeter Wemm 
4355b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
436df8bae1dSRodney W. Grimes struct gettimeofday_args {
437df8bae1dSRodney W. Grimes 	struct	timeval *tp;
438df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
439df8bae1dSRodney W. Grimes };
440d2d3e875SBruce Evans #endif
441fb99ab88SMatthew Dillon /*
442fb99ab88SMatthew Dillon  * MPSAFE
443fb99ab88SMatthew Dillon  */
444df8bae1dSRodney W. Grimes /* ARGSUSED */
44526f9a767SRodney W. Grimes int
44691afe087SPoul-Henning Kamp gettimeofday(struct thread *td, struct gettimeofday_args *uap)
447df8bae1dSRodney W. Grimes {
448df8bae1dSRodney W. Grimes 	struct timeval atv;
449411c25edSTim J. Robbins 	struct timezone rtz;
450df8bae1dSRodney W. Grimes 	int error = 0;
451df8bae1dSRodney W. Grimes 
452df8bae1dSRodney W. Grimes 	if (uap->tp) {
453df8bae1dSRodney W. Grimes 		microtime(&atv);
45401609114SAlfred Perlstein 		error = copyout(&atv, uap->tp, sizeof (atv));
455df8bae1dSRodney W. Grimes 	}
45621dcdb38SPoul-Henning Kamp 	if (error == 0 && uap->tzp != NULL) {
45791f1c2b3SPoul-Henning Kamp 		rtz.tz_minuteswest = tz_minuteswest;
45891f1c2b3SPoul-Henning Kamp 		rtz.tz_dsttime = tz_dsttime;
459411c25edSTim J. Robbins 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
46021dcdb38SPoul-Henning Kamp 	}
461df8bae1dSRodney W. Grimes 	return (error);
462df8bae1dSRodney W. Grimes }
463df8bae1dSRodney W. Grimes 
464d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
465df8bae1dSRodney W. Grimes struct settimeofday_args {
466df8bae1dSRodney W. Grimes 	struct	timeval *tv;
467df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
468df8bae1dSRodney W. Grimes };
469d2d3e875SBruce Evans #endif
470fb99ab88SMatthew Dillon /*
471fb99ab88SMatthew Dillon  * MPSAFE
472fb99ab88SMatthew Dillon  */
473df8bae1dSRodney W. Grimes /* ARGSUSED */
47426f9a767SRodney W. Grimes int
47591afe087SPoul-Henning Kamp settimeofday(struct thread *td, struct settimeofday_args *uap)
476df8bae1dSRodney W. Grimes {
477b88ec951SJohn Baldwin 	struct timeval atv, *tvp;
478b88ec951SJohn Baldwin 	struct timezone atz, *tzp;
479b88ec951SJohn Baldwin 	int error;
480b88ec951SJohn Baldwin 
481b88ec951SJohn Baldwin 	if (uap->tv) {
482b88ec951SJohn Baldwin 		error = copyin(uap->tv, &atv, sizeof(atv));
483b88ec951SJohn Baldwin 		if (error)
484b88ec951SJohn Baldwin 			return (error);
485b88ec951SJohn Baldwin 		tvp = &atv;
486b88ec951SJohn Baldwin 	} else
487b88ec951SJohn Baldwin 		tvp = NULL;
488b88ec951SJohn Baldwin 	if (uap->tzp) {
489b88ec951SJohn Baldwin 		error = copyin(uap->tzp, &atz, sizeof(atz));
490b88ec951SJohn Baldwin 		if (error)
491b88ec951SJohn Baldwin 			return (error);
492b88ec951SJohn Baldwin 		tzp = &atz;
493b88ec951SJohn Baldwin 	} else
494b88ec951SJohn Baldwin 		tzp = NULL;
495b88ec951SJohn Baldwin 	return (kern_settimeofday(td, tvp, tzp));
496b88ec951SJohn Baldwin }
497b88ec951SJohn Baldwin 
498b88ec951SJohn Baldwin int
499b88ec951SJohn Baldwin kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
500b88ec951SJohn Baldwin {
501b88ec951SJohn Baldwin 	int error;
502fb99ab88SMatthew Dillon 
5034b8d5f2dSRobert Watson #ifdef MAC
5044b8d5f2dSRobert Watson 	error = mac_check_system_settime(td->td_ucred);
5054b8d5f2dSRobert Watson 	if (error)
5064b8d5f2dSRobert Watson 		return (error);
5074b8d5f2dSRobert Watson #endif
508acd3428bSRobert Watson 	error = priv_check(td, PRIV_SETTIMEOFDAY);
509b88ec951SJohn Baldwin 	if (error)
5107edfb592SJohn Baldwin 		return (error);
511df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
512b88ec951SJohn Baldwin 	if (tv) {
513b88ec951SJohn Baldwin 		if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
5147edfb592SJohn Baldwin 			return (EINVAL);
515b88ec951SJohn Baldwin 		error = settime(td, tv);
516708e7684SPeter Wemm 	}
517b88ec951SJohn Baldwin 	if (tzp && error == 0) {
518b88ec951SJohn Baldwin 		tz_minuteswest = tzp->tz_minuteswest;
519b88ec951SJohn Baldwin 		tz_dsttime = tzp->tz_dsttime;
520b88ec951SJohn Baldwin 	}
5217edfb592SJohn Baldwin 	return (error);
522b88ec951SJohn Baldwin }
5237edfb592SJohn Baldwin 
524df8bae1dSRodney W. Grimes /*
525df8bae1dSRodney W. Grimes  * Get value of an interval timer.  The process virtual and
526df8bae1dSRodney W. Grimes  * profiling virtual time timers are kept in the p_stats area, since
527df8bae1dSRodney W. Grimes  * they can be swapped out.  These are kept internally in the
528df8bae1dSRodney W. Grimes  * way they are specified externally: in time until they expire.
529df8bae1dSRodney W. Grimes  *
530df8bae1dSRodney W. Grimes  * The real time interval timer is kept in the process table slot
531df8bae1dSRodney W. Grimes  * for the process, and its value (it_value) is kept as an
532df8bae1dSRodney W. Grimes  * absolute time rather than as a delta, so that it is easy to keep
533df8bae1dSRodney W. Grimes  * periodic real-time signals from drifting.
534df8bae1dSRodney W. Grimes  *
535df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
536df8bae1dSRodney W. Grimes  * kern_clock.c.  The real time timer is processed by a timeout
537df8bae1dSRodney W. Grimes  * routine, called from the softclock() routine.  Since a callout
538df8bae1dSRodney W. Grimes  * may be delayed in real time due to interrupt processing in the system,
539df8bae1dSRodney W. Grimes  * it is possible for the real time timeout routine (realitexpire, given below),
540df8bae1dSRodney W. Grimes  * to be delayed in real time past when it is supposed to occur.  It
541df8bae1dSRodney W. Grimes  * does not suffice, therefore, to reload the real timer .it_value from the
542df8bae1dSRodney W. Grimes  * real time timers .it_interval.  Rather, we compute the next time in
543df8bae1dSRodney W. Grimes  * absolute time the timer should go off.
544df8bae1dSRodney W. Grimes  */
545d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
546df8bae1dSRodney W. Grimes struct getitimer_args {
547df8bae1dSRodney W. Grimes 	u_int	which;
548df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
549df8bae1dSRodney W. Grimes };
550d2d3e875SBruce Evans #endif
551fb99ab88SMatthew Dillon /*
552fb99ab88SMatthew Dillon  * MPSAFE
553fb99ab88SMatthew Dillon  */
55426f9a767SRodney W. Grimes int
55591afe087SPoul-Henning Kamp getitimer(struct thread *td, struct getitimer_args *uap)
556df8bae1dSRodney W. Grimes {
557df8bae1dSRodney W. Grimes 	struct itimerval aitv;
558c90110d6SJohn Baldwin 	int error;
559df8bae1dSRodney W. Grimes 
560cfa0efe7SMaxim Sobolev 	error = kern_getitimer(td, uap->which, &aitv);
561cfa0efe7SMaxim Sobolev 	if (error != 0)
562cfa0efe7SMaxim Sobolev 		return (error);
563cfa0efe7SMaxim Sobolev 	return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
564cfa0efe7SMaxim Sobolev }
565cfa0efe7SMaxim Sobolev 
566cfa0efe7SMaxim Sobolev int
567cfa0efe7SMaxim Sobolev kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
568cfa0efe7SMaxim Sobolev {
569cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
570cfa0efe7SMaxim Sobolev 	struct timeval ctv;
571cfa0efe7SMaxim Sobolev 
572cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
573df8bae1dSRodney W. Grimes 		return (EINVAL);
574fb99ab88SMatthew Dillon 
575cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
576df8bae1dSRodney W. Grimes 		/*
577ee002b68SBruce Evans 		 * Convert from absolute to relative time in .it_value
578df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
579df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
580df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
581df8bae1dSRodney W. Grimes 		 */
58296d7f8efSTim J. Robbins 		PROC_LOCK(p);
583cfa0efe7SMaxim Sobolev 		*aitv = p->p_realtimer;
58496d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
585cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
586c21410e1SPoul-Henning Kamp 			getmicrouptime(&ctv);
587cfa0efe7SMaxim Sobolev 			if (timevalcmp(&aitv->it_value, &ctv, <))
588cfa0efe7SMaxim Sobolev 				timevalclear(&aitv->it_value);
589df8bae1dSRodney W. Grimes 			else
590cfa0efe7SMaxim Sobolev 				timevalsub(&aitv->it_value, &ctv);
591227ee8a1SPoul-Henning Kamp 		}
592fb99ab88SMatthew Dillon 	} else {
59396d7f8efSTim J. Robbins 		mtx_lock_spin(&sched_lock);
594cfa0efe7SMaxim Sobolev 		*aitv = p->p_stats->p_timer[which];
59596d7f8efSTim J. Robbins 		mtx_unlock_spin(&sched_lock);
596fb99ab88SMatthew Dillon 	}
597cfa0efe7SMaxim Sobolev 	return (0);
598df8bae1dSRodney W. Grimes }
599df8bae1dSRodney W. Grimes 
600d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
601df8bae1dSRodney W. Grimes struct setitimer_args {
602df8bae1dSRodney W. Grimes 	u_int	which;
603df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
604df8bae1dSRodney W. Grimes };
605d2d3e875SBruce Evans #endif
606cfa0efe7SMaxim Sobolev 
607fb99ab88SMatthew Dillon /*
608fb99ab88SMatthew Dillon  * MPSAFE
609fb99ab88SMatthew Dillon  */
61026f9a767SRodney W. Grimes int
61191afe087SPoul-Henning Kamp setitimer(struct thread *td, struct setitimer_args *uap)
612df8bae1dSRodney W. Grimes {
613cfa0efe7SMaxim Sobolev 	struct itimerval aitv, oitv;
614c90110d6SJohn Baldwin 	int error;
61596d7f8efSTim J. Robbins 
61696d7f8efSTim J. Robbins 	if (uap->itv == NULL) {
61796d7f8efSTim J. Robbins 		uap->itv = uap->oitv;
61896d7f8efSTim J. Robbins 		return (getitimer(td, (struct getitimer_args *)uap));
61996d7f8efSTim J. Robbins 	}
620df8bae1dSRodney W. Grimes 
62196d7f8efSTim J. Robbins 	if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
622df8bae1dSRodney W. Grimes 		return (error);
623cfa0efe7SMaxim Sobolev 	error = kern_setitimer(td, uap->which, &aitv, &oitv);
624cfa0efe7SMaxim Sobolev 	if (error != 0 || uap->oitv == NULL)
625cfa0efe7SMaxim Sobolev 		return (error);
626cfa0efe7SMaxim Sobolev 	return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
627cfa0efe7SMaxim Sobolev }
628cfa0efe7SMaxim Sobolev 
629cfa0efe7SMaxim Sobolev int
630c90110d6SJohn Baldwin kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
631c90110d6SJohn Baldwin     struct itimerval *oitv)
632cfa0efe7SMaxim Sobolev {
633cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
634cfa0efe7SMaxim Sobolev 	struct timeval ctv;
635cfa0efe7SMaxim Sobolev 
6365e85ac17SJohn Baldwin 	if (aitv == NULL)
6375e85ac17SJohn Baldwin 		return (kern_getitimer(td, which, oitv));
6385e85ac17SJohn Baldwin 
639cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
64096d7f8efSTim J. Robbins 		return (EINVAL);
641cfa0efe7SMaxim Sobolev 	if (itimerfix(&aitv->it_value))
642cfa0efe7SMaxim Sobolev 		return (EINVAL);
643cfa0efe7SMaxim Sobolev 	if (!timevalisset(&aitv->it_value))
644cfa0efe7SMaxim Sobolev 		timevalclear(&aitv->it_interval);
645cfa0efe7SMaxim Sobolev 	else if (itimerfix(&aitv->it_interval))
64696d7f8efSTim J. Robbins 		return (EINVAL);
64796d7f8efSTim J. Robbins 
648cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
64996d7f8efSTim J. Robbins 		PROC_LOCK(p);
6504cf41af3SPoul-Henning Kamp 		if (timevalisset(&p->p_realtimer.it_value))
6514f559836SJake Burkholder 			callout_stop(&p->p_itcallout);
65225b4d3a8SJohn Baldwin 		getmicrouptime(&ctv);
653cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
654cfa0efe7SMaxim Sobolev 			callout_reset(&p->p_itcallout, tvtohz(&aitv->it_value),
6554f559836SJake Burkholder 			    realitexpire, p);
656cfa0efe7SMaxim Sobolev 			timevaladd(&aitv->it_value, &ctv);
65725b4d3a8SJohn Baldwin 		}
658cfa0efe7SMaxim Sobolev 		*oitv = p->p_realtimer;
659cfa0efe7SMaxim Sobolev 		p->p_realtimer = *aitv;
66096d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
661cfa0efe7SMaxim Sobolev 		if (timevalisset(&oitv->it_value)) {
662cfa0efe7SMaxim Sobolev 			if (timevalcmp(&oitv->it_value, &ctv, <))
663cfa0efe7SMaxim Sobolev 				timevalclear(&oitv->it_value);
66496d7f8efSTim J. Robbins 			else
665cfa0efe7SMaxim Sobolev 				timevalsub(&oitv->it_value, &ctv);
666fb99ab88SMatthew Dillon 		}
66796d7f8efSTim J. Robbins 	} else {
66896d7f8efSTim J. Robbins 		mtx_lock_spin(&sched_lock);
669cfa0efe7SMaxim Sobolev 		*oitv = p->p_stats->p_timer[which];
670cfa0efe7SMaxim Sobolev 		p->p_stats->p_timer[which] = *aitv;
67196d7f8efSTim J. Robbins 		mtx_unlock_spin(&sched_lock);
67296d7f8efSTim J. Robbins 	}
67396d7f8efSTim J. Robbins 	return (0);
674df8bae1dSRodney W. Grimes }
675df8bae1dSRodney W. Grimes 
676df8bae1dSRodney W. Grimes /*
677df8bae1dSRodney W. Grimes  * Real interval timer expired:
678df8bae1dSRodney W. Grimes  * send process whose timer expired an alarm signal.
679df8bae1dSRodney W. Grimes  * If time is not set up to reload, then just return.
680df8bae1dSRodney W. Grimes  * Else compute next time timer should go off which is > current time.
681df8bae1dSRodney W. Grimes  * This is where delay in processing this timeout causes multiple
682df8bae1dSRodney W. Grimes  * SIGALRM calls to be compressed into one.
683c8b47828SBruce Evans  * tvtohz() always adds 1 to allow for the time until the next clock
6849207f00aSBruce Evans  * interrupt being strictly less than 1 clock tick, but we don't want
6859207f00aSBruce Evans  * that here since we want to appear to be in sync with the clock
6869207f00aSBruce Evans  * interrupt even when we're delayed.
687df8bae1dSRodney W. Grimes  */
688df8bae1dSRodney W. Grimes void
68991afe087SPoul-Henning Kamp realitexpire(void *arg)
690df8bae1dSRodney W. Grimes {
69191afe087SPoul-Henning Kamp 	struct proc *p;
692bfe6c9faSPoul-Henning Kamp 	struct timeval ctv, ntv;
693df8bae1dSRodney W. Grimes 
694df8bae1dSRodney W. Grimes 	p = (struct proc *)arg;
69537824023SJohn Baldwin 	PROC_LOCK(p);
696df8bae1dSRodney W. Grimes 	psignal(p, SIGALRM);
6974cf41af3SPoul-Henning Kamp 	if (!timevalisset(&p->p_realtimer.it_interval)) {
6984cf41af3SPoul-Henning Kamp 		timevalclear(&p->p_realtimer.it_value);
6995499ea01SJohn Baldwin 		if (p->p_flag & P_WEXIT)
7005499ea01SJohn Baldwin 			wakeup(&p->p_itcallout);
70137824023SJohn Baldwin 		PROC_UNLOCK(p);
702df8bae1dSRodney W. Grimes 		return;
703df8bae1dSRodney W. Grimes 	}
704df8bae1dSRodney W. Grimes 	for (;;) {
705df8bae1dSRodney W. Grimes 		timevaladd(&p->p_realtimer.it_value,
706df8bae1dSRodney W. Grimes 		    &p->p_realtimer.it_interval);
707c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
7084cf41af3SPoul-Henning Kamp 		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
709bfe6c9faSPoul-Henning Kamp 			ntv = p->p_realtimer.it_value;
710bfe6c9faSPoul-Henning Kamp 			timevalsub(&ntv, &ctv);
7114f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
7124f559836SJake Burkholder 			    realitexpire, p);
71337824023SJohn Baldwin 			PROC_UNLOCK(p);
714df8bae1dSRodney W. Grimes 			return;
715df8bae1dSRodney W. Grimes 		}
716df8bae1dSRodney W. Grimes 	}
71737824023SJohn Baldwin 	/*NOTREACHED*/
718df8bae1dSRodney W. Grimes }
719df8bae1dSRodney W. Grimes 
720df8bae1dSRodney W. Grimes /*
721df8bae1dSRodney W. Grimes  * Check that a proposed value to load into the .it_value or
722df8bae1dSRodney W. Grimes  * .it_interval part of an interval timer is acceptable, and
723df8bae1dSRodney W. Grimes  * fix it to have at least minimal value (i.e. if it is less
724df8bae1dSRodney W. Grimes  * than the resolution of the clock, round it up.)
725df8bae1dSRodney W. Grimes  */
72626f9a767SRodney W. Grimes int
72791afe087SPoul-Henning Kamp itimerfix(struct timeval *tv)
728df8bae1dSRodney W. Grimes {
729df8bae1dSRodney W. Grimes 
73086857b36SDavid Xu 	if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
731df8bae1dSRodney W. Grimes 		return (EINVAL);
732df8bae1dSRodney W. Grimes 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
733df8bae1dSRodney W. Grimes 		tv->tv_usec = tick;
734df8bae1dSRodney W. Grimes 	return (0);
735df8bae1dSRodney W. Grimes }
736df8bae1dSRodney W. Grimes 
737df8bae1dSRodney W. Grimes /*
738df8bae1dSRodney W. Grimes  * Decrement an interval timer by a specified number
739df8bae1dSRodney W. Grimes  * of microseconds, which must be less than a second,
740df8bae1dSRodney W. Grimes  * i.e. < 1000000.  If the timer expires, then reload
741df8bae1dSRodney W. Grimes  * it.  In this case, carry over (usec - old value) to
742df8bae1dSRodney W. Grimes  * reduce the value reloaded into the timer so that
743df8bae1dSRodney W. Grimes  * the timer does not drift.  This routine assumes
744df8bae1dSRodney W. Grimes  * that it is called in a context where the timers
745df8bae1dSRodney W. Grimes  * on which it is operating cannot change in value.
746df8bae1dSRodney W. Grimes  */
74726f9a767SRodney W. Grimes int
74891afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec)
749df8bae1dSRodney W. Grimes {
750df8bae1dSRodney W. Grimes 
751df8bae1dSRodney W. Grimes 	if (itp->it_value.tv_usec < usec) {
752df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_sec == 0) {
753df8bae1dSRodney W. Grimes 			/* expired, and already in next interval */
754df8bae1dSRodney W. Grimes 			usec -= itp->it_value.tv_usec;
755df8bae1dSRodney W. Grimes 			goto expire;
756df8bae1dSRodney W. Grimes 		}
757df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec += 1000000;
758df8bae1dSRodney W. Grimes 		itp->it_value.tv_sec--;
759df8bae1dSRodney W. Grimes 	}
760df8bae1dSRodney W. Grimes 	itp->it_value.tv_usec -= usec;
761df8bae1dSRodney W. Grimes 	usec = 0;
7624cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_value))
763df8bae1dSRodney W. Grimes 		return (1);
764df8bae1dSRodney W. Grimes 	/* expired, exactly at end of interval */
765df8bae1dSRodney W. Grimes expire:
7664cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_interval)) {
767df8bae1dSRodney W. Grimes 		itp->it_value = itp->it_interval;
768df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec -= usec;
769df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_usec < 0) {
770df8bae1dSRodney W. Grimes 			itp->it_value.tv_usec += 1000000;
771df8bae1dSRodney W. Grimes 			itp->it_value.tv_sec--;
772df8bae1dSRodney W. Grimes 		}
773df8bae1dSRodney W. Grimes 	} else
774df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
775df8bae1dSRodney W. Grimes 	return (0);
776df8bae1dSRodney W. Grimes }
777df8bae1dSRodney W. Grimes 
778df8bae1dSRodney W. Grimes /*
779df8bae1dSRodney W. Grimes  * Add and subtract routines for timevals.
780df8bae1dSRodney W. Grimes  * N.B.: subtract routine doesn't deal with
781df8bae1dSRodney W. Grimes  * results which are before the beginning,
782df8bae1dSRodney W. Grimes  * it just gets very confused in this case.
783df8bae1dSRodney W. Grimes  * Caveat emptor.
784df8bae1dSRodney W. Grimes  */
78526f9a767SRodney W. Grimes void
7866ff7636eSAlfred Perlstein timevaladd(struct timeval *t1, const struct timeval *t2)
787df8bae1dSRodney W. Grimes {
788df8bae1dSRodney W. Grimes 
789df8bae1dSRodney W. Grimes 	t1->tv_sec += t2->tv_sec;
790df8bae1dSRodney W. Grimes 	t1->tv_usec += t2->tv_usec;
791df8bae1dSRodney W. Grimes 	timevalfix(t1);
792df8bae1dSRodney W. Grimes }
793df8bae1dSRodney W. Grimes 
79426f9a767SRodney W. Grimes void
7956ff7636eSAlfred Perlstein timevalsub(struct timeval *t1, const struct timeval *t2)
796df8bae1dSRodney W. Grimes {
797df8bae1dSRodney W. Grimes 
798df8bae1dSRodney W. Grimes 	t1->tv_sec -= t2->tv_sec;
799df8bae1dSRodney W. Grimes 	t1->tv_usec -= t2->tv_usec;
800df8bae1dSRodney W. Grimes 	timevalfix(t1);
801df8bae1dSRodney W. Grimes }
802df8bae1dSRodney W. Grimes 
80387b6de2bSPoul-Henning Kamp static void
80491afe087SPoul-Henning Kamp timevalfix(struct timeval *t1)
805df8bae1dSRodney W. Grimes {
806df8bae1dSRodney W. Grimes 
807df8bae1dSRodney W. Grimes 	if (t1->tv_usec < 0) {
808df8bae1dSRodney W. Grimes 		t1->tv_sec--;
809df8bae1dSRodney W. Grimes 		t1->tv_usec += 1000000;
810df8bae1dSRodney W. Grimes 	}
811df8bae1dSRodney W. Grimes 	if (t1->tv_usec >= 1000000) {
812df8bae1dSRodney W. Grimes 		t1->tv_sec++;
813df8bae1dSRodney W. Grimes 		t1->tv_usec -= 1000000;
814df8bae1dSRodney W. Grimes 	}
815df8bae1dSRodney W. Grimes }
81691974ce1SSam Leffler 
81791974ce1SSam Leffler /*
818addea9d4SSam Leffler  * ratecheck(): simple time-based rate-limit checking.
81991974ce1SSam Leffler  */
82091974ce1SSam Leffler int
82191974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
82291974ce1SSam Leffler {
82391974ce1SSam Leffler 	struct timeval tv, delta;
82491974ce1SSam Leffler 	int rv = 0;
82591974ce1SSam Leffler 
826addea9d4SSam Leffler 	getmicrouptime(&tv);		/* NB: 10ms precision */
827addea9d4SSam Leffler 	delta = tv;
828addea9d4SSam Leffler 	timevalsub(&delta, lasttime);
82991974ce1SSam Leffler 
83091974ce1SSam Leffler 	/*
83191974ce1SSam Leffler 	 * check for 0,0 is so that the message will be seen at least once,
83291974ce1SSam Leffler 	 * even if interval is huge.
83391974ce1SSam Leffler 	 */
83491974ce1SSam Leffler 	if (timevalcmp(&delta, mininterval, >=) ||
83591974ce1SSam Leffler 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
83691974ce1SSam Leffler 		*lasttime = tv;
83791974ce1SSam Leffler 		rv = 1;
83891974ce1SSam Leffler 	}
83991974ce1SSam Leffler 
84091974ce1SSam Leffler 	return (rv);
84191974ce1SSam Leffler }
84291974ce1SSam Leffler 
84391974ce1SSam Leffler /*
84491974ce1SSam Leffler  * ppsratecheck(): packets (or events) per second limitation.
845addea9d4SSam Leffler  *
846addea9d4SSam Leffler  * Return 0 if the limit is to be enforced (e.g. the caller
847addea9d4SSam Leffler  * should drop a packet because of the rate limitation).
848addea9d4SSam Leffler  *
849893bec80SSam Leffler  * maxpps of 0 always causes zero to be returned.  maxpps of -1
850893bec80SSam Leffler  * always causes 1 to be returned; this effectively defeats rate
851893bec80SSam Leffler  * limiting.
852893bec80SSam Leffler  *
853addea9d4SSam Leffler  * Note that we maintain the struct timeval for compatibility
854addea9d4SSam Leffler  * with other bsd systems.  We reuse the storage and just monitor
855addea9d4SSam Leffler  * clock ticks for minimal overhead.
85691974ce1SSam Leffler  */
85791974ce1SSam Leffler int
85891974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
85991974ce1SSam Leffler {
860addea9d4SSam Leffler 	int now;
86191974ce1SSam Leffler 
86291974ce1SSam Leffler 	/*
863addea9d4SSam Leffler 	 * Reset the last time and counter if this is the first call
864addea9d4SSam Leffler 	 * or more than a second has passed since the last update of
865addea9d4SSam Leffler 	 * lasttime.
86691974ce1SSam Leffler 	 */
867addea9d4SSam Leffler 	now = ticks;
868addea9d4SSam Leffler 	if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
869addea9d4SSam Leffler 		lasttime->tv_sec = now;
870addea9d4SSam Leffler 		*curpps = 1;
871893bec80SSam Leffler 		return (maxpps != 0);
872addea9d4SSam Leffler 	} else {
873addea9d4SSam Leffler 		(*curpps)++;		/* NB: ignore potential overflow */
874addea9d4SSam Leffler 		return (maxpps < 0 || *curpps < maxpps);
875addea9d4SSam Leffler 	}
87691974ce1SSam Leffler }
87786857b36SDavid Xu 
87886857b36SDavid Xu static void
87986857b36SDavid Xu itimer_start(void)
88086857b36SDavid Xu {
88186857b36SDavid Xu 	struct kclock rt_clock = {
88286857b36SDavid Xu 		.timer_create  = realtimer_create,
88386857b36SDavid Xu 		.timer_delete  = realtimer_delete,
88486857b36SDavid Xu 		.timer_settime = realtimer_settime,
88586857b36SDavid Xu 		.timer_gettime = realtimer_gettime,
88686857b36SDavid Xu 		.event_hook     = realtimer_event_hook
88786857b36SDavid Xu 	};
88886857b36SDavid Xu 
88986857b36SDavid Xu 	itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
89086857b36SDavid Xu 		NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
89186857b36SDavid Xu 	register_posix_clock(CLOCK_REALTIME,  &rt_clock);
89286857b36SDavid Xu 	register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
89377e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
89477e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
89577e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
896993182e5SAlexander Leidinger 	EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit,
897d26b1a1fSDavid Xu 		(void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY);
898993182e5SAlexander Leidinger 	EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec,
899d26b1a1fSDavid Xu 		(void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY);
90086857b36SDavid Xu }
90186857b36SDavid Xu 
90286857b36SDavid Xu int
90386857b36SDavid Xu register_posix_clock(int clockid, struct kclock *clk)
90486857b36SDavid Xu {
90586857b36SDavid Xu 	if ((unsigned)clockid >= MAX_CLOCKS) {
90686857b36SDavid Xu 		printf("%s: invalid clockid\n", __func__);
90786857b36SDavid Xu 		return (0);
90886857b36SDavid Xu 	}
90986857b36SDavid Xu 	posix_clocks[clockid] = *clk;
91086857b36SDavid Xu 	return (1);
91186857b36SDavid Xu }
91286857b36SDavid Xu 
91386857b36SDavid Xu static int
91486857b36SDavid Xu itimer_init(void *mem, int size, int flags)
91586857b36SDavid Xu {
91686857b36SDavid Xu 	struct itimer *it;
91786857b36SDavid Xu 
91886857b36SDavid Xu 	it = (struct itimer *)mem;
91986857b36SDavid Xu 	mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
92086857b36SDavid Xu 	return (0);
92186857b36SDavid Xu }
92286857b36SDavid Xu 
92386857b36SDavid Xu static void
92486857b36SDavid Xu itimer_fini(void *mem, int size)
92586857b36SDavid Xu {
92686857b36SDavid Xu 	struct itimer *it;
92786857b36SDavid Xu 
92886857b36SDavid Xu 	it = (struct itimer *)mem;
92986857b36SDavid Xu 	mtx_destroy(&it->it_mtx);
93086857b36SDavid Xu }
93186857b36SDavid Xu 
93286857b36SDavid Xu static void
93386857b36SDavid Xu itimer_enter(struct itimer *it)
93486857b36SDavid Xu {
93586857b36SDavid Xu 
93686857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
93786857b36SDavid Xu 	it->it_usecount++;
93886857b36SDavid Xu }
93986857b36SDavid Xu 
94086857b36SDavid Xu static void
94186857b36SDavid Xu itimer_leave(struct itimer *it)
94286857b36SDavid Xu {
94386857b36SDavid Xu 
94486857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
94586857b36SDavid Xu 	KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
94686857b36SDavid Xu 
94786857b36SDavid Xu 	if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
94886857b36SDavid Xu 		wakeup(it);
94986857b36SDavid Xu }
95086857b36SDavid Xu 
95186857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
95261d3a4efSDavid Xu struct ktimer_create_args {
95386857b36SDavid Xu 	clockid_t clock_id;
95486857b36SDavid Xu 	struct sigevent * evp;
95561d3a4efSDavid Xu 	int * timerid;
95686857b36SDavid Xu };
95786857b36SDavid Xu #endif
95886857b36SDavid Xu 
95986857b36SDavid Xu int
96061d3a4efSDavid Xu ktimer_create(struct thread *td, struct ktimer_create_args *uap)
96186857b36SDavid Xu {
96286857b36SDavid Xu 	struct sigevent *evp1, ev;
96361d3a4efSDavid Xu 	int id;
96486857b36SDavid Xu 	int error;
96586857b36SDavid Xu 
96686857b36SDavid Xu 	if (uap->evp != NULL) {
96786857b36SDavid Xu 		error = copyin(uap->evp, &ev, sizeof(ev));
96886857b36SDavid Xu 		if (error != 0)
96986857b36SDavid Xu 			return (error);
97086857b36SDavid Xu 		evp1 = &ev;
97186857b36SDavid Xu 	} else
97286857b36SDavid Xu 		evp1 = NULL;
97386857b36SDavid Xu 
97486857b36SDavid Xu 	error = kern_timer_create(td, uap->clock_id, evp1, &id, -1);
97586857b36SDavid Xu 
97686857b36SDavid Xu 	if (error == 0) {
97761d3a4efSDavid Xu 		error = copyout(&id, uap->timerid, sizeof(int));
97886857b36SDavid Xu 		if (error != 0)
97986857b36SDavid Xu 			kern_timer_delete(td, id);
98086857b36SDavid Xu 	}
98186857b36SDavid Xu 	return (error);
98286857b36SDavid Xu }
98386857b36SDavid Xu 
98486857b36SDavid Xu static int
98586857b36SDavid Xu kern_timer_create(struct thread *td, clockid_t clock_id,
98661d3a4efSDavid Xu 	struct sigevent *evp, int *timerid, int preset_id)
98786857b36SDavid Xu {
98886857b36SDavid Xu 	struct proc *p = td->td_proc;
98986857b36SDavid Xu 	struct itimer *it;
99086857b36SDavid Xu 	int id;
99186857b36SDavid Xu 	int error;
99286857b36SDavid Xu 
99386857b36SDavid Xu 	if (clock_id < 0 || clock_id >= MAX_CLOCKS)
99486857b36SDavid Xu 		return (EINVAL);
99586857b36SDavid Xu 
99686857b36SDavid Xu 	if (posix_clocks[clock_id].timer_create == NULL)
99786857b36SDavid Xu 		return (EINVAL);
99886857b36SDavid Xu 
99986857b36SDavid Xu 	if (evp != NULL) {
100086857b36SDavid Xu 		if (evp->sigev_notify != SIGEV_NONE &&
100156c06c4bSDavid Xu 		    evp->sigev_notify != SIGEV_SIGNAL &&
100256c06c4bSDavid Xu 		    evp->sigev_notify != SIGEV_THREAD_ID)
100386857b36SDavid Xu 			return (EINVAL);
100456c06c4bSDavid Xu 		if ((evp->sigev_notify == SIGEV_SIGNAL ||
100556c06c4bSDavid Xu 		     evp->sigev_notify == SIGEV_THREAD_ID) &&
100686857b36SDavid Xu 			!_SIG_VALID(evp->sigev_signo))
100786857b36SDavid Xu 			return (EINVAL);
100886857b36SDavid Xu 	}
100986857b36SDavid Xu 
101060354683SDavid Xu 	if (p->p_itimers == NULL)
101186857b36SDavid Xu 		itimers_alloc(p);
101286857b36SDavid Xu 
101386857b36SDavid Xu 	it = uma_zalloc(itimer_zone, M_WAITOK);
101486857b36SDavid Xu 	it->it_flags = 0;
101586857b36SDavid Xu 	it->it_usecount = 0;
101686857b36SDavid Xu 	it->it_active = 0;
101756c06c4bSDavid Xu 	timespecclear(&it->it_time.it_value);
101856c06c4bSDavid Xu 	timespecclear(&it->it_time.it_interval);
101986857b36SDavid Xu 	it->it_overrun = 0;
102086857b36SDavid Xu 	it->it_overrun_last = 0;
102186857b36SDavid Xu 	it->it_clockid = clock_id;
102286857b36SDavid Xu 	it->it_timerid = -1;
102386857b36SDavid Xu 	it->it_proc = p;
102486857b36SDavid Xu 	ksiginfo_init(&it->it_ksi);
102586857b36SDavid Xu 	it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
102686857b36SDavid Xu 	error = CLOCK_CALL(clock_id, timer_create, (it));
102786857b36SDavid Xu 	if (error != 0)
102886857b36SDavid Xu 		goto out;
102986857b36SDavid Xu 
103086857b36SDavid Xu 	PROC_LOCK(p);
103186857b36SDavid Xu 	if (preset_id != -1) {
103286857b36SDavid Xu 		KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
103386857b36SDavid Xu 		id = preset_id;
103460354683SDavid Xu 		if (p->p_itimers->its_timers[id] != NULL) {
103586857b36SDavid Xu 			PROC_UNLOCK(p);
103686857b36SDavid Xu 			error = 0;
103786857b36SDavid Xu 			goto out;
103886857b36SDavid Xu 		}
103986857b36SDavid Xu 	} else {
104086857b36SDavid Xu 		/*
104186857b36SDavid Xu 		 * Find a free timer slot, skipping those reserved
104286857b36SDavid Xu 		 * for setitimer().
104386857b36SDavid Xu 		 */
104486857b36SDavid Xu 		for (id = 3; id < TIMER_MAX; id++)
104560354683SDavid Xu 			if (p->p_itimers->its_timers[id] == NULL)
104686857b36SDavid Xu 				break;
104786857b36SDavid Xu 		if (id == TIMER_MAX) {
104886857b36SDavid Xu 			PROC_UNLOCK(p);
104986857b36SDavid Xu 			error = EAGAIN;
105086857b36SDavid Xu 			goto out;
105186857b36SDavid Xu 		}
105286857b36SDavid Xu 	}
105386857b36SDavid Xu 	it->it_timerid = id;
105460354683SDavid Xu 	p->p_itimers->its_timers[id] = it;
105586857b36SDavid Xu 	if (evp != NULL)
105686857b36SDavid Xu 		it->it_sigev = *evp;
105786857b36SDavid Xu 	else {
105886857b36SDavid Xu 		it->it_sigev.sigev_notify = SIGEV_SIGNAL;
105986857b36SDavid Xu 		switch (clock_id) {
106086857b36SDavid Xu 		default:
106186857b36SDavid Xu 		case CLOCK_REALTIME:
106286857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGALRM;
106386857b36SDavid Xu 			break;
106486857b36SDavid Xu 		case CLOCK_VIRTUAL:
106586857b36SDavid Xu  			it->it_sigev.sigev_signo = SIGVTALRM;
106686857b36SDavid Xu 			break;
106786857b36SDavid Xu 		case CLOCK_PROF:
106886857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGPROF;
106986857b36SDavid Xu 			break;
107086857b36SDavid Xu 		}
10718f0371f1SDavid Xu 		it->it_sigev.sigev_value.sival_int = id;
107286857b36SDavid Xu 	}
107386857b36SDavid Xu 
107456c06c4bSDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
107556c06c4bSDavid Xu 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
107686857b36SDavid Xu 		it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
107786857b36SDavid Xu 		it->it_ksi.ksi_code = SI_TIMER;
107886857b36SDavid Xu 		it->it_ksi.ksi_value = it->it_sigev.sigev_value;
107986857b36SDavid Xu 		it->it_ksi.ksi_timerid = id;
108086857b36SDavid Xu 	}
108186857b36SDavid Xu 	PROC_UNLOCK(p);
108286857b36SDavid Xu 	*timerid = id;
108386857b36SDavid Xu 	return (0);
108486857b36SDavid Xu 
108586857b36SDavid Xu out:
108686857b36SDavid Xu 	ITIMER_LOCK(it);
108786857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
108886857b36SDavid Xu 	ITIMER_UNLOCK(it);
108986857b36SDavid Xu 	uma_zfree(itimer_zone, it);
109086857b36SDavid Xu 	return (error);
109186857b36SDavid Xu }
109286857b36SDavid Xu 
109386857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
109461d3a4efSDavid Xu struct ktimer_delete_args {
109561d3a4efSDavid Xu 	int timerid;
109686857b36SDavid Xu };
109786857b36SDavid Xu #endif
109886857b36SDavid Xu 
109986857b36SDavid Xu int
110061d3a4efSDavid Xu ktimer_delete(struct thread *td, struct ktimer_delete_args *uap)
110186857b36SDavid Xu {
110286857b36SDavid Xu 	return (kern_timer_delete(td, uap->timerid));
110386857b36SDavid Xu }
110486857b36SDavid Xu 
110586857b36SDavid Xu static struct itimer *
110661d3a4efSDavid Xu itimer_find(struct proc *p, int timerid, int include_deleting)
110786857b36SDavid Xu {
110886857b36SDavid Xu 	struct itimer *it;
110986857b36SDavid Xu 
111086857b36SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
111160354683SDavid Xu 	if ((p->p_itimers == NULL) || (timerid >= TIMER_MAX) ||
111260354683SDavid Xu 	    (it = p->p_itimers->its_timers[timerid]) == NULL) {
111386857b36SDavid Xu 		return (NULL);
111486857b36SDavid Xu 	}
111586857b36SDavid Xu 	ITIMER_LOCK(it);
111686857b36SDavid Xu 	if (!include_deleting && (it->it_flags & ITF_DELETING) != 0) {
111786857b36SDavid Xu 		ITIMER_UNLOCK(it);
111886857b36SDavid Xu 		it = NULL;
111986857b36SDavid Xu 	}
112086857b36SDavid Xu 	return (it);
112186857b36SDavid Xu }
112286857b36SDavid Xu 
112386857b36SDavid Xu static int
112461d3a4efSDavid Xu kern_timer_delete(struct thread *td, int timerid)
112586857b36SDavid Xu {
112686857b36SDavid Xu 	struct proc *p = td->td_proc;
112786857b36SDavid Xu 	struct itimer *it;
112886857b36SDavid Xu 
112986857b36SDavid Xu 	PROC_LOCK(p);
113086857b36SDavid Xu 	it = itimer_find(p, timerid, 0);
113186857b36SDavid Xu 	if (it == NULL) {
113286857b36SDavid Xu 		PROC_UNLOCK(p);
113386857b36SDavid Xu 		return (EINVAL);
113486857b36SDavid Xu 	}
113586857b36SDavid Xu 	PROC_UNLOCK(p);
113686857b36SDavid Xu 
113786857b36SDavid Xu 	it->it_flags |= ITF_DELETING;
113886857b36SDavid Xu 	while (it->it_usecount > 0) {
113986857b36SDavid Xu 		it->it_flags |= ITF_WANTED;
114086857b36SDavid Xu 		msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
114186857b36SDavid Xu 	}
114286857b36SDavid Xu 	it->it_flags &= ~ITF_WANTED;
114386857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
114486857b36SDavid Xu 	ITIMER_UNLOCK(it);
114586857b36SDavid Xu 
114686857b36SDavid Xu 	PROC_LOCK(p);
114786857b36SDavid Xu 	if (KSI_ONQ(&it->it_ksi))
114886857b36SDavid Xu 		sigqueue_take(&it->it_ksi);
114960354683SDavid Xu 	p->p_itimers->its_timers[timerid] = NULL;
115086857b36SDavid Xu 	PROC_UNLOCK(p);
115186857b36SDavid Xu 	uma_zfree(itimer_zone, it);
115286857b36SDavid Xu 	return (0);
115386857b36SDavid Xu }
115486857b36SDavid Xu 
115586857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
115661d3a4efSDavid Xu struct ktimer_settime_args {
115761d3a4efSDavid Xu 	int timerid;
115886857b36SDavid Xu 	int flags;
115986857b36SDavid Xu 	const struct itimerspec * value;
116086857b36SDavid Xu 	struct itimerspec * ovalue;
116186857b36SDavid Xu };
116286857b36SDavid Xu #endif
116386857b36SDavid Xu 
116486857b36SDavid Xu int
116561d3a4efSDavid Xu ktimer_settime(struct thread *td, struct ktimer_settime_args *uap)
116686857b36SDavid Xu {
116786857b36SDavid Xu 	struct proc *p = td->td_proc;
116886857b36SDavid Xu 	struct itimer *it;
116986857b36SDavid Xu 	struct itimerspec val, oval, *ovalp;
117086857b36SDavid Xu 	int error;
117186857b36SDavid Xu 
117286857b36SDavid Xu 	error = copyin(uap->value, &val, sizeof(val));
117386857b36SDavid Xu 	if (error != 0)
117486857b36SDavid Xu 		return (error);
117586857b36SDavid Xu 
117686857b36SDavid Xu 	if (uap->ovalue != NULL)
117786857b36SDavid Xu 		ovalp = &oval;
117886857b36SDavid Xu 	else
117986857b36SDavid Xu 		ovalp = NULL;
118086857b36SDavid Xu 
118186857b36SDavid Xu 	PROC_LOCK(p);
118286857b36SDavid Xu 	if (uap->timerid < 3 ||
118386857b36SDavid Xu 	    (it = itimer_find(p, uap->timerid, 0)) == NULL) {
118486857b36SDavid Xu 		PROC_UNLOCK(p);
118586857b36SDavid Xu 		error = EINVAL;
118686857b36SDavid Xu 	} else {
118786857b36SDavid Xu 		PROC_UNLOCK(p);
118886857b36SDavid Xu 		itimer_enter(it);
118986857b36SDavid Xu 		error = CLOCK_CALL(it->it_clockid, timer_settime,
119086857b36SDavid Xu 				(it, uap->flags, &val, ovalp));
119186857b36SDavid Xu 		itimer_leave(it);
119286857b36SDavid Xu 		ITIMER_UNLOCK(it);
119386857b36SDavid Xu 	}
119486857b36SDavid Xu 	if (error == 0 && uap->ovalue != NULL)
119586857b36SDavid Xu 		error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
119686857b36SDavid Xu 	return (error);
119786857b36SDavid Xu }
119886857b36SDavid Xu 
119986857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
120061d3a4efSDavid Xu struct ktimer_gettime_args {
120161d3a4efSDavid Xu 	int timerid;
120286857b36SDavid Xu 	struct itimerspec * value;
120386857b36SDavid Xu };
120486857b36SDavid Xu #endif
120586857b36SDavid Xu 
120686857b36SDavid Xu int
120761d3a4efSDavid Xu ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap)
120886857b36SDavid Xu {
120986857b36SDavid Xu 	struct proc *p = td->td_proc;
121086857b36SDavid Xu 	struct itimer *it;
121186857b36SDavid Xu 	struct itimerspec val;
121286857b36SDavid Xu 	int error;
121386857b36SDavid Xu 
121486857b36SDavid Xu 	PROC_LOCK(p);
121586857b36SDavid Xu 	if (uap->timerid < 3 ||
121686857b36SDavid Xu 	   (it = itimer_find(p, uap->timerid, 0)) == NULL) {
121786857b36SDavid Xu 		PROC_UNLOCK(p);
121886857b36SDavid Xu 		error = EINVAL;
121986857b36SDavid Xu 	} else {
122086857b36SDavid Xu 		PROC_UNLOCK(p);
122186857b36SDavid Xu 		itimer_enter(it);
122286857b36SDavid Xu 		error = CLOCK_CALL(it->it_clockid, timer_gettime,
122386857b36SDavid Xu 				(it, &val));
122486857b36SDavid Xu 		itimer_leave(it);
122586857b36SDavid Xu 		ITIMER_UNLOCK(it);
122686857b36SDavid Xu 	}
122786857b36SDavid Xu 	if (error == 0)
122886857b36SDavid Xu 		error = copyout(&val, uap->value, sizeof(val));
122986857b36SDavid Xu 	return (error);
123086857b36SDavid Xu }
123186857b36SDavid Xu 
123286857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
123386857b36SDavid Xu struct timer_getoverrun_args {
123461d3a4efSDavid Xu 	int timerid;
123586857b36SDavid Xu };
123686857b36SDavid Xu #endif
123786857b36SDavid Xu 
123886857b36SDavid Xu int
123961d3a4efSDavid Xu ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap)
124086857b36SDavid Xu {
124186857b36SDavid Xu 	struct proc *p = td->td_proc;
124286857b36SDavid Xu 	struct itimer *it;
124386857b36SDavid Xu 	int error ;
124486857b36SDavid Xu 
124586857b36SDavid Xu 	PROC_LOCK(p);
124686857b36SDavid Xu 	if (uap->timerid < 3 ||
124786857b36SDavid Xu 	    (it = itimer_find(p, uap->timerid, 0)) == NULL) {
124886857b36SDavid Xu 		PROC_UNLOCK(p);
124986857b36SDavid Xu 		error = EINVAL;
125086857b36SDavid Xu 	} else {
125186857b36SDavid Xu 		td->td_retval[0] = it->it_overrun_last;
125286857b36SDavid Xu 		ITIMER_UNLOCK(it);
125356c06c4bSDavid Xu 		PROC_UNLOCK(p);
125486857b36SDavid Xu 		error = 0;
125586857b36SDavid Xu 	}
125686857b36SDavid Xu 	return (error);
125786857b36SDavid Xu }
125886857b36SDavid Xu 
125986857b36SDavid Xu static int
126086857b36SDavid Xu realtimer_create(struct itimer *it)
126186857b36SDavid Xu {
126286857b36SDavid Xu 	callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
126386857b36SDavid Xu 	return (0);
126486857b36SDavid Xu }
126586857b36SDavid Xu 
126686857b36SDavid Xu static int
126786857b36SDavid Xu realtimer_delete(struct itimer *it)
126886857b36SDavid Xu {
126986857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
127086857b36SDavid Xu 	callout_stop(&it->it_callout);
127186857b36SDavid Xu 	return (0);
127286857b36SDavid Xu }
127386857b36SDavid Xu 
127486857b36SDavid Xu static int
127586857b36SDavid Xu realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
127686857b36SDavid Xu {
127756c06c4bSDavid Xu 	struct timespec cts;
127886857b36SDavid Xu 
127986857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
128086857b36SDavid Xu 
128156c06c4bSDavid Xu 	realtimer_clocktime(it->it_clockid, &cts);
128256c06c4bSDavid Xu 	*ovalue = it->it_time;
128386857b36SDavid Xu 	if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
128456c06c4bSDavid Xu 		timespecsub(&ovalue->it_value, &cts);
128586857b36SDavid Xu 		if (ovalue->it_value.tv_sec < 0 ||
128686857b36SDavid Xu 		    (ovalue->it_value.tv_sec == 0 &&
128786857b36SDavid Xu 		     ovalue->it_value.tv_nsec == 0)) {
128886857b36SDavid Xu 			ovalue->it_value.tv_sec  = 0;
128986857b36SDavid Xu 			ovalue->it_value.tv_nsec = 1;
129086857b36SDavid Xu 		}
129186857b36SDavid Xu 	}
129286857b36SDavid Xu 	return (0);
129386857b36SDavid Xu }
129486857b36SDavid Xu 
129586857b36SDavid Xu static int
129686857b36SDavid Xu realtimer_settime(struct itimer *it, int flags,
129786857b36SDavid Xu 	struct itimerspec *value, struct itimerspec *ovalue)
129886857b36SDavid Xu {
129956c06c4bSDavid Xu 	struct timespec cts, ts;
130056c06c4bSDavid Xu 	struct timeval tv;
130156c06c4bSDavid Xu 	struct itimerspec val;
130286857b36SDavid Xu 
130386857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
130486857b36SDavid Xu 
130556c06c4bSDavid Xu 	val = *value;
130656c06c4bSDavid Xu 	if (itimespecfix(&val.it_value))
130786857b36SDavid Xu 		return (EINVAL);
130886857b36SDavid Xu 
130956c06c4bSDavid Xu 	if (timespecisset(&val.it_value)) {
131056c06c4bSDavid Xu 		if (itimespecfix(&val.it_interval))
131186857b36SDavid Xu 			return (EINVAL);
131286857b36SDavid Xu 	} else {
131356c06c4bSDavid Xu 		timespecclear(&val.it_interval);
131486857b36SDavid Xu 	}
131586857b36SDavid Xu 
131686857b36SDavid Xu 	if (ovalue != NULL)
131786857b36SDavid Xu 		realtimer_gettime(it, ovalue);
131886857b36SDavid Xu 
131986857b36SDavid Xu 	it->it_time = val;
132056c06c4bSDavid Xu 	if (timespecisset(&val.it_value)) {
132156c06c4bSDavid Xu 		realtimer_clocktime(it->it_clockid, &cts);
132256c06c4bSDavid Xu 		ts = val.it_value;
132386857b36SDavid Xu 		if ((flags & TIMER_ABSTIME) == 0) {
132486857b36SDavid Xu 			/* Convert to absolute time. */
132556c06c4bSDavid Xu 			timespecadd(&it->it_time.it_value, &cts);
132686857b36SDavid Xu 		} else {
132756c06c4bSDavid Xu 			timespecsub(&ts, &cts);
132886857b36SDavid Xu 			/*
132956c06c4bSDavid Xu 			 * We don't care if ts is negative, tztohz will
133086857b36SDavid Xu 			 * fix it.
133186857b36SDavid Xu 			 */
133286857b36SDavid Xu 		}
133356c06c4bSDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
133456c06c4bSDavid Xu 		callout_reset(&it->it_callout, tvtohz(&tv),
133586857b36SDavid Xu 			realtimer_expire, it);
133686857b36SDavid Xu 	} else {
133786857b36SDavid Xu 		callout_stop(&it->it_callout);
133886857b36SDavid Xu 	}
133986857b36SDavid Xu 
134086857b36SDavid Xu 	return (0);
134186857b36SDavid Xu }
134286857b36SDavid Xu 
134386857b36SDavid Xu static void
134456c06c4bSDavid Xu realtimer_clocktime(clockid_t id, struct timespec *ts)
134586857b36SDavid Xu {
134686857b36SDavid Xu 	if (id == CLOCK_REALTIME)
134756c06c4bSDavid Xu 		getnanotime(ts);
134886857b36SDavid Xu 	else	/* CLOCK_MONOTONIC */
134956c06c4bSDavid Xu 		getnanouptime(ts);
135056c06c4bSDavid Xu }
135156c06c4bSDavid Xu 
135256c06c4bSDavid Xu int
135361d3a4efSDavid Xu itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi)
135456c06c4bSDavid Xu {
135556c06c4bSDavid Xu 	struct itimer *it;
135656c06c4bSDavid Xu 
135756c06c4bSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
135856c06c4bSDavid Xu 	it = itimer_find(p, timerid, 0);
135956c06c4bSDavid Xu 	if (it != NULL) {
136056c06c4bSDavid Xu 		ksi->ksi_overrun = it->it_overrun;
136156c06c4bSDavid Xu 		it->it_overrun_last = it->it_overrun;
136256c06c4bSDavid Xu 		it->it_overrun = 0;
136356c06c4bSDavid Xu 		ITIMER_UNLOCK(it);
136456c06c4bSDavid Xu 		return (0);
136556c06c4bSDavid Xu 	}
136656c06c4bSDavid Xu 	return (EINVAL);
136756c06c4bSDavid Xu }
136856c06c4bSDavid Xu 
136956c06c4bSDavid Xu int
137056c06c4bSDavid Xu itimespecfix(struct timespec *ts)
137156c06c4bSDavid Xu {
137256c06c4bSDavid Xu 
137356c06c4bSDavid Xu 	if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
137456c06c4bSDavid Xu 		return (EINVAL);
137556c06c4bSDavid Xu 	if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
137656c06c4bSDavid Xu 		ts->tv_nsec = tick * 1000;
137756c06c4bSDavid Xu 	return (0);
137886857b36SDavid Xu }
137986857b36SDavid Xu 
138086857b36SDavid Xu static void
138186857b36SDavid Xu realtimer_event_hook(struct proc *p, clockid_t clock_id, int event)
138286857b36SDavid Xu {
138386857b36SDavid Xu 	struct itimers *its;
138486857b36SDavid Xu 	struct itimer  *it;
138586857b36SDavid Xu 	int i;
138686857b36SDavid Xu 
138786857b36SDavid Xu 	/*
138886857b36SDavid Xu 	 * Timer 0 (ITIMER_REAL) is XSI interval timer, according to POSIX
138986857b36SDavid Xu 	 * specification, it should be inherited by new process image.
139086857b36SDavid Xu 	 */
139186857b36SDavid Xu 	if (event == ITIMER_EV_EXEC)
139286857b36SDavid Xu 		i = 1;
139386857b36SDavid Xu 	else
139486857b36SDavid Xu 		i = 0;
139560354683SDavid Xu 	its = p->p_itimers;
139686857b36SDavid Xu 	for (; i < TIMER_MAX; i++) {
139786857b36SDavid Xu 		if ((it = its->its_timers[i]) != NULL &&
139886857b36SDavid Xu 		     it->it_clockid == clock_id) {
139986857b36SDavid Xu 			ITIMER_LOCK(it);
140086857b36SDavid Xu 			callout_stop(&it->it_callout);
140186857b36SDavid Xu 			ITIMER_UNLOCK(it);
140286857b36SDavid Xu 		}
140386857b36SDavid Xu 	}
140486857b36SDavid Xu }
140586857b36SDavid Xu 
140686857b36SDavid Xu /* Timeout callback for realtime timer */
140786857b36SDavid Xu static void
140886857b36SDavid Xu realtimer_expire(void *arg)
140986857b36SDavid Xu {
141056c06c4bSDavid Xu 	struct timespec cts, ts;
141156c06c4bSDavid Xu 	struct timeval tv;
141286857b36SDavid Xu 	struct itimer *it;
141386857b36SDavid Xu 	struct proc *p;
141486857b36SDavid Xu 
141586857b36SDavid Xu 	it = (struct itimer *)arg;
141686857b36SDavid Xu 	p = it->it_proc;
141786857b36SDavid Xu 
141856c06c4bSDavid Xu 	realtimer_clocktime(it->it_clockid, &cts);
141986857b36SDavid Xu 	/* Only fire if time is reached. */
142056c06c4bSDavid Xu 	if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
142156c06c4bSDavid Xu 		if (timespecisset(&it->it_time.it_interval)) {
142256c06c4bSDavid Xu 			timespecadd(&it->it_time.it_value,
142386857b36SDavid Xu 				    &it->it_time.it_interval);
142456c06c4bSDavid Xu 			while (timespeccmp(&cts, &it->it_time.it_value, >=)) {
142577e718f7SDavid Xu 				if (it->it_overrun < INT_MAX)
142686857b36SDavid Xu 					it->it_overrun++;
142777e718f7SDavid Xu 				else
142877e718f7SDavid Xu 					it->it_ksi.ksi_errno = ERANGE;
142956c06c4bSDavid Xu 				timespecadd(&it->it_time.it_value,
143086857b36SDavid Xu 					    &it->it_time.it_interval);
143186857b36SDavid Xu 			}
143286857b36SDavid Xu 		} else {
143386857b36SDavid Xu 			/* single shot timer ? */
143456c06c4bSDavid Xu 			timespecclear(&it->it_time.it_value);
143586857b36SDavid Xu 		}
143656c06c4bSDavid Xu 		if (timespecisset(&it->it_time.it_value)) {
143756c06c4bSDavid Xu 			ts = it->it_time.it_value;
143856c06c4bSDavid Xu 			timespecsub(&ts, &cts);
143956c06c4bSDavid Xu 			TIMESPEC_TO_TIMEVAL(&tv, &ts);
144056c06c4bSDavid Xu 			callout_reset(&it->it_callout, tvtohz(&tv),
144186857b36SDavid Xu 				 realtimer_expire, it);
144286857b36SDavid Xu 		}
144386857b36SDavid Xu 		ITIMER_UNLOCK(it);
144486857b36SDavid Xu 		itimer_fire(it);
144586857b36SDavid Xu 		ITIMER_LOCK(it);
144656c06c4bSDavid Xu 	} else if (timespecisset(&it->it_time.it_value)) {
144756c06c4bSDavid Xu 		ts = it->it_time.it_value;
144856c06c4bSDavid Xu 		timespecsub(&ts, &cts);
144956c06c4bSDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
145056c06c4bSDavid Xu 		callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
145186857b36SDavid Xu  			it);
145286857b36SDavid Xu 	}
145386857b36SDavid Xu }
145486857b36SDavid Xu 
145586857b36SDavid Xu void
145686857b36SDavid Xu itimer_fire(struct itimer *it)
145786857b36SDavid Xu {
145886857b36SDavid Xu 	struct proc *p = it->it_proc;
14596d7b314bSDavid Xu 	int ret;
146086857b36SDavid Xu 
146156c06c4bSDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
146256c06c4bSDavid Xu 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
146386857b36SDavid Xu 		PROC_LOCK(p);
14646d7b314bSDavid Xu 		if (!KSI_ONQ(&it->it_ksi)) {
146577e718f7SDavid Xu 			it->it_ksi.ksi_errno = 0;
14666d7b314bSDavid Xu 			ret = psignal_event(p, &it->it_sigev, &it->it_ksi);
14676d7b314bSDavid Xu 			if (__predict_false(ret != 0)) {
146886857b36SDavid Xu 				it->it_overrun++;
146956c06c4bSDavid Xu 				/*
147056c06c4bSDavid Xu 				 * Broken userland code, thread went
147156c06c4bSDavid Xu 				 * away, disarm the timer.
147256c06c4bSDavid Xu 				 */
14736d7b314bSDavid Xu 				if (ret == ESRCH) {
147456c06c4bSDavid Xu 					ITIMER_LOCK(it);
147556c06c4bSDavid Xu 					timespecclear(&it->it_time.it_value);
147656c06c4bSDavid Xu 					timespecclear(&it->it_time.it_interval);
147756c06c4bSDavid Xu 					callout_stop(&it->it_callout);
147856c06c4bSDavid Xu 					ITIMER_UNLOCK(it);
14796d7b314bSDavid Xu 				}
148056c06c4bSDavid Xu 			}
148156c06c4bSDavid Xu 		} else {
148277e718f7SDavid Xu 			if (it->it_overrun < INT_MAX)
14836d7b314bSDavid Xu 				it->it_overrun++;
148477e718f7SDavid Xu 			else
148577e718f7SDavid Xu 				it->it_ksi.ksi_errno = ERANGE;
148656c06c4bSDavid Xu 		}
148786857b36SDavid Xu 		PROC_UNLOCK(p);
148886857b36SDavid Xu 	}
148986857b36SDavid Xu }
149086857b36SDavid Xu 
149186857b36SDavid Xu static void
149286857b36SDavid Xu itimers_alloc(struct proc *p)
149386857b36SDavid Xu {
149460354683SDavid Xu 	struct itimers *its;
149560354683SDavid Xu 	int i;
149686857b36SDavid Xu 
149760354683SDavid Xu 	its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
149886857b36SDavid Xu 	LIST_INIT(&its->its_virtual);
149986857b36SDavid Xu 	LIST_INIT(&its->its_prof);
150086857b36SDavid Xu 	TAILQ_INIT(&its->its_worklist);
150160354683SDavid Xu 	for (i = 0; i < TIMER_MAX; i++)
150260354683SDavid Xu 		its->its_timers[i] = NULL;
150360354683SDavid Xu 	PROC_LOCK(p);
150460354683SDavid Xu 	if (p->p_itimers == NULL) {
150560354683SDavid Xu 		p->p_itimers = its;
150660354683SDavid Xu 		PROC_UNLOCK(p);
150760354683SDavid Xu 	}
150860354683SDavid Xu 	else {
150960354683SDavid Xu 		PROC_UNLOCK(p);
151060354683SDavid Xu 		free(its, M_SUBPROC);
151160354683SDavid Xu 	}
151286857b36SDavid Xu }
151386857b36SDavid Xu 
1514993182e5SAlexander Leidinger static void
1515993182e5SAlexander Leidinger itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused)
1516993182e5SAlexander Leidinger {
1517993182e5SAlexander Leidinger    	itimers_event_hook_exit(arg, p);
1518993182e5SAlexander Leidinger }
1519993182e5SAlexander Leidinger 
152086857b36SDavid Xu /* Clean up timers when some process events are being triggered. */
1521d26b1a1fSDavid Xu static void
1522993182e5SAlexander Leidinger itimers_event_hook_exit(void *arg, struct proc *p)
152386857b36SDavid Xu {
152486857b36SDavid Xu 	struct itimers *its;
152586857b36SDavid Xu 	struct itimer *it;
15263e70c6f0SDavid Xu 	int event = (int)(intptr_t)arg;
152786857b36SDavid Xu 	int i;
152886857b36SDavid Xu 
152960354683SDavid Xu 	if (p->p_itimers != NULL) {
153060354683SDavid Xu 		its = p->p_itimers;
153186857b36SDavid Xu 		for (i = 0; i < MAX_CLOCKS; ++i) {
153286857b36SDavid Xu 			if (posix_clocks[i].event_hook != NULL)
153386857b36SDavid Xu 				CLOCK_CALL(i, event_hook, (p, i, event));
153486857b36SDavid Xu 		}
153586857b36SDavid Xu 		/*
153686857b36SDavid Xu 		 * According to susv3, XSI interval timers should be inherited
153786857b36SDavid Xu 		 * by new image.
153886857b36SDavid Xu 		 */
153986857b36SDavid Xu 		if (event == ITIMER_EV_EXEC)
154086857b36SDavid Xu 			i = 3;
154186857b36SDavid Xu 		else if (event == ITIMER_EV_EXIT)
154286857b36SDavid Xu 			i = 0;
154386857b36SDavid Xu 		else
154486857b36SDavid Xu 			panic("unhandled event");
154586857b36SDavid Xu 		for (; i < TIMER_MAX; ++i) {
154686857b36SDavid Xu 			if ((it = its->its_timers[i]) != NULL) {
154786857b36SDavid Xu 				PROC_LOCK(p);
154886857b36SDavid Xu 				if (KSI_ONQ(&it->it_ksi))
154986857b36SDavid Xu 					sigqueue_take(&it->it_ksi);
155086857b36SDavid Xu 				PROC_UNLOCK(p);
155186857b36SDavid Xu 				uma_zfree(itimer_zone, its->its_timers[i]);
155286857b36SDavid Xu 				its->its_timers[i] = NULL;
155386857b36SDavid Xu 			}
155486857b36SDavid Xu 		}
155586857b36SDavid Xu 		if (its->its_timers[0] == NULL &&
155686857b36SDavid Xu 		    its->its_timers[1] == NULL &&
155786857b36SDavid Xu 		    its->its_timers[2] == NULL) {
155860354683SDavid Xu 			free(its, M_SUBPROC);
155960354683SDavid Xu 			p->p_itimers = NULL;
156086857b36SDavid Xu 		}
156186857b36SDavid Xu 	}
156286857b36SDavid Xu }
1563