xref: /freebsd/sys/kern/kern_time.c (revision aff5bcb1b257bcb7ee8fdd8683215fe79bec3f7e)
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>
40fb919e4dSMark Murray #include <sys/lock.h>
41fb919e4dSMark Murray #include <sys/mutex.h>
42d2d3e875SBruce Evans #include <sys/sysproto.h>
43d26b1a1fSDavid Xu #include <sys/eventhandler.h>
44df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
45797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
46df8bae1dSRodney W. Grimes #include <sys/kernel.h>
474b8d5f2dSRobert Watson #include <sys/mac.h>
48efa42cbcSPaul Saab #include <sys/syscallsubr.h>
4977e718f7SDavid Xu #include <sys/sysctl.h>
5094c8fcd8SPeter Wemm #include <sys/sysent.h>
51df8bae1dSRodney W. Grimes #include <sys/proc.h>
52708e7684SPeter Wemm #include <sys/time.h>
5386857b36SDavid Xu #include <sys/timers.h>
5491266b96SPoul-Henning Kamp #include <sys/timetc.h>
55df8bae1dSRodney W. Grimes #include <sys/vnode.h>
56fb919e4dSMark Murray 
5777e718f7SDavid Xu #include <posix4/posix4.h>
5877e718f7SDavid Xu 
595b870b7bSPeter Wemm #include <vm/vm.h>
605b870b7bSPeter Wemm #include <vm/vm_extern.h>
61df8bae1dSRodney W. Grimes 
6286857b36SDavid Xu #define MAX_CLOCKS 	(CLOCK_MONOTONIC+1)
6386857b36SDavid Xu 
6491f1c2b3SPoul-Henning Kamp int tz_minuteswest;
6591f1c2b3SPoul-Henning Kamp int tz_dsttime;
66ac7e6123SDavid Greenman 
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 *);
91d26b1a1fSDavid Xu static void	itimers_event_hook(void *arg, struct proc *p);
9286857b36SDavid Xu static int	realtimer_create(struct itimer *);
9386857b36SDavid Xu static int	realtimer_gettime(struct itimer *, struct itimerspec *);
9486857b36SDavid Xu static int	realtimer_settime(struct itimer *, int,
9586857b36SDavid Xu 			struct itimerspec *, struct itimerspec *);
9686857b36SDavid Xu static int	realtimer_delete(struct itimer *);
9756c06c4bSDavid Xu static void	realtimer_clocktime(clockid_t, struct timespec *);
9886857b36SDavid Xu static void	realtimer_expire(void *);
9986857b36SDavid Xu static void	realtimer_event_hook(struct proc *, clockid_t, int event);
10086857b36SDavid Xu static int	kern_timer_create(struct thread *, clockid_t,
10161d3a4efSDavid Xu 			struct sigevent *, int *, int);
10261d3a4efSDavid Xu static int	kern_timer_delete(struct thread *, int);
10386857b36SDavid Xu 
10486857b36SDavid Xu int		register_posix_clock(int, struct kclock *);
10586857b36SDavid Xu void		itimer_fire(struct itimer *it);
10656c06c4bSDavid Xu int		itimespecfix(struct timespec *ts);
10786857b36SDavid Xu 
10886857b36SDavid Xu #define CLOCK_CALL(clock, call, arglist)		\
10986857b36SDavid Xu 	((*posix_clocks[clock].call) arglist)
11086857b36SDavid Xu 
11186857b36SDavid Xu SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL);
11286857b36SDavid Xu 
11386857b36SDavid Xu 
1141b09ae77SPoul-Henning Kamp static void
1151b09ae77SPoul-Henning Kamp no_lease_updatetime(deltat)
1161b09ae77SPoul-Henning Kamp 	int deltat;
1171b09ae77SPoul-Henning Kamp {
1181b09ae77SPoul-Henning Kamp }
1191b09ae77SPoul-Henning Kamp 
1204d77a549SAlfred Perlstein void (*lease_updatetime)(int)  = no_lease_updatetime;
1211b09ae77SPoul-Henning Kamp 
12294c8fcd8SPeter Wemm static int
12391afe087SPoul-Henning Kamp settime(struct thread *td, struct timeval *tv)
12494c8fcd8SPeter Wemm {
125fcae3aa6SNick Sayer 	struct timeval delta, tv1, tv2;
126c0bd94a7SNick Sayer 	static struct timeval maxtime, laststep;
1277ec73f64SPoul-Henning Kamp 	struct timespec ts;
12894c8fcd8SPeter Wemm 	int s;
12994c8fcd8SPeter Wemm 
130708e7684SPeter Wemm 	s = splclock();
1319c8fff87SBruce Evans 	microtime(&tv1);
13200af9731SPoul-Henning Kamp 	delta = *tv;
13300af9731SPoul-Henning Kamp 	timevalsub(&delta, &tv1);
13494c8fcd8SPeter Wemm 
13594c8fcd8SPeter Wemm 	/*
1369c8fff87SBruce Evans 	 * If the system is secure, we do not allow the time to be
137fcae3aa6SNick Sayer 	 * set to a value earlier than 1 second less than the highest
138fcae3aa6SNick Sayer 	 * time we have yet seen. The worst a miscreant can do in
139fcae3aa6SNick Sayer 	 * this circumstance is "freeze" time. He couldn't go
140fcae3aa6SNick Sayer 	 * back to the past.
141c0bd94a7SNick Sayer 	 *
142c0bd94a7SNick Sayer 	 * We similarly do not allow the clock to be stepped more
143c0bd94a7SNick Sayer 	 * than one second, nor more than once per second. This allows
144c0bd94a7SNick Sayer 	 * a miscreant to make the clock march double-time, but no worse.
14594c8fcd8SPeter Wemm 	 */
1467edfb592SJohn Baldwin 	if (securelevel_gt(td->td_ucred, 1) != 0) {
147fcae3aa6SNick Sayer 		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
1483f92429aSMatt Jacob 			/*
149c0bd94a7SNick Sayer 			 * Update maxtime to latest time we've seen.
1503f92429aSMatt Jacob 			 */
151fcae3aa6SNick Sayer 			if (tv1.tv_sec > maxtime.tv_sec)
152fcae3aa6SNick Sayer 				maxtime = tv1;
153fcae3aa6SNick Sayer 			tv2 = *tv;
154fcae3aa6SNick Sayer 			timevalsub(&tv2, &maxtime);
155fcae3aa6SNick Sayer 			if (tv2.tv_sec < -1) {
1563f92429aSMatt Jacob 				tv->tv_sec = maxtime.tv_sec - 1;
157fcae3aa6SNick Sayer 				printf("Time adjustment clamped to -1 second\n");
158fcae3aa6SNick Sayer 			}
1593f92429aSMatt Jacob 		} else {
160c0bd94a7SNick Sayer 			if (tv1.tv_sec == laststep.tv_sec) {
161c0bd94a7SNick Sayer 				splx(s);
162c0bd94a7SNick Sayer 				return (EPERM);
163c0bd94a7SNick Sayer 			}
164c0bd94a7SNick Sayer 			if (delta.tv_sec > 1) {
165c0bd94a7SNick Sayer 				tv->tv_sec = tv1.tv_sec + 1;
166c0bd94a7SNick Sayer 				printf("Time adjustment clamped to +1 second\n");
167c0bd94a7SNick Sayer 			}
168c0bd94a7SNick Sayer 			laststep = *tv;
169fcae3aa6SNick Sayer 		}
1709c8fff87SBruce Evans 	}
1719c8fff87SBruce Evans 
1727ec73f64SPoul-Henning Kamp 	ts.tv_sec = tv->tv_sec;
1737ec73f64SPoul-Henning Kamp 	ts.tv_nsec = tv->tv_usec * 1000;
1747edfb592SJohn Baldwin 	mtx_lock(&Giant);
17591266b96SPoul-Henning Kamp 	tc_setclock(&ts);
17694c8fcd8SPeter Wemm 	(void) splsoftclock();
17794c8fcd8SPeter Wemm 	lease_updatetime(delta.tv_sec);
17894c8fcd8SPeter Wemm 	splx(s);
17994c8fcd8SPeter Wemm 	resettodr();
1807edfb592SJohn Baldwin 	mtx_unlock(&Giant);
18194c8fcd8SPeter Wemm 	return (0);
18294c8fcd8SPeter Wemm }
18394c8fcd8SPeter Wemm 
18494c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
18594c8fcd8SPeter Wemm struct clock_gettime_args {
18694c8fcd8SPeter Wemm 	clockid_t clock_id;
18794c8fcd8SPeter Wemm 	struct	timespec *tp;
18894c8fcd8SPeter Wemm };
18994c8fcd8SPeter Wemm #endif
190708e7684SPeter Wemm 
191fb99ab88SMatthew Dillon /*
192fb99ab88SMatthew Dillon  * MPSAFE
193fb99ab88SMatthew Dillon  */
19494c8fcd8SPeter Wemm /* ARGSUSED */
19594c8fcd8SPeter Wemm int
19691afe087SPoul-Henning Kamp clock_gettime(struct thread *td, struct clock_gettime_args *uap)
19794c8fcd8SPeter Wemm {
19894c8fcd8SPeter Wemm 	struct timespec ats;
199f0b479cdSPaul Saab 	int error;
200f0b479cdSPaul Saab 
201f0b479cdSPaul Saab 	error = kern_clock_gettime(td, uap->clock_id, &ats);
202f0b479cdSPaul Saab 	if (error == 0)
203f0b479cdSPaul Saab 		error = copyout(&ats, uap->tp, sizeof(ats));
204f0b479cdSPaul Saab 
205f0b479cdSPaul Saab 	return (error);
206f0b479cdSPaul Saab }
207f0b479cdSPaul Saab 
208f0b479cdSPaul Saab int
209f0b479cdSPaul Saab kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
210f0b479cdSPaul Saab {
211de0a9241SKelly Yancey 	struct timeval sys, user;
21278c85e8dSJohn Baldwin 	struct proc *p;
21394c8fcd8SPeter Wemm 
21478c85e8dSJohn Baldwin 	p = td->td_proc;
215f0b479cdSPaul Saab 	switch (clock_id) {
2165e758b95SRobert Watson 	case CLOCK_REALTIME:		/* Default to precise. */
2175e758b95SRobert Watson 	case CLOCK_REALTIME_PRECISE:
218f0b479cdSPaul Saab 		nanotime(ats);
219b8817154SKelly Yancey 		break;
2205e758b95SRobert Watson 	case CLOCK_REALTIME_FAST:
2215e758b95SRobert Watson 		getnanotime(ats);
2225e758b95SRobert Watson 		break;
223b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
22478c85e8dSJohn Baldwin 		PROC_LOCK(p);
22578c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
22678c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
227f0b479cdSPaul Saab 		TIMEVAL_TO_TIMESPEC(&user, ats);
228b8817154SKelly Yancey 		break;
229b8817154SKelly Yancey 	case CLOCK_PROF:
23078c85e8dSJohn Baldwin 		PROC_LOCK(p);
23178c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
23278c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
233de0a9241SKelly Yancey 		timevaladd(&user, &sys);
234f0b479cdSPaul Saab 		TIMEVAL_TO_TIMESPEC(&user, ats);
235de0a9241SKelly Yancey 		break;
2365e758b95SRobert Watson 	case CLOCK_MONOTONIC:		/* Default to precise. */
2375e758b95SRobert Watson 	case CLOCK_MONOTONIC_PRECISE:
2385eefd889SAndre Oppermann 	case CLOCK_UPTIME:
2395e758b95SRobert Watson 	case CLOCK_UPTIME_PRECISE:
240f0b479cdSPaul Saab 		nanouptime(ats);
241b8817154SKelly Yancey 		break;
2425e758b95SRobert Watson 	case CLOCK_UPTIME_FAST:
2435e758b95SRobert Watson 	case CLOCK_MONOTONIC_FAST:
2445e758b95SRobert Watson 		getnanouptime(ats);
2455e758b95SRobert Watson 		break;
2465e758b95SRobert Watson 	case CLOCK_SECOND:
2475e758b95SRobert Watson 		ats->tv_sec = time_second;
2485e758b95SRobert Watson 		ats->tv_nsec = 0;
2495e758b95SRobert Watson 		break;
250b8817154SKelly Yancey 	default:
2515cb3dc8fSPoul-Henning Kamp 		return (EINVAL);
252b8817154SKelly Yancey 	}
253f0b479cdSPaul Saab 	return (0);
25494c8fcd8SPeter Wemm }
25594c8fcd8SPeter Wemm 
25694c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
25794c8fcd8SPeter Wemm struct clock_settime_args {
25894c8fcd8SPeter Wemm 	clockid_t clock_id;
25994c8fcd8SPeter Wemm 	const struct	timespec *tp;
26094c8fcd8SPeter Wemm };
26194c8fcd8SPeter Wemm #endif
262708e7684SPeter Wemm 
263fb99ab88SMatthew Dillon /*
264fb99ab88SMatthew Dillon  * MPSAFE
265fb99ab88SMatthew Dillon  */
26694c8fcd8SPeter Wemm /* ARGSUSED */
26794c8fcd8SPeter Wemm int
26891afe087SPoul-Henning Kamp clock_settime(struct thread *td, struct clock_settime_args *uap)
26994c8fcd8SPeter Wemm {
27094c8fcd8SPeter Wemm 	struct timespec ats;
27194c8fcd8SPeter Wemm 	int error;
27294c8fcd8SPeter Wemm 
273f0b479cdSPaul Saab 	if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
274f0b479cdSPaul Saab 		return (error);
275f0b479cdSPaul Saab 	return (kern_clock_settime(td, uap->clock_id, &ats));
276f0b479cdSPaul Saab }
277f0b479cdSPaul Saab 
278f0b479cdSPaul Saab int
279f0b479cdSPaul Saab kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
280f0b479cdSPaul Saab {
281f0b479cdSPaul Saab 	struct timeval atv;
282f0b479cdSPaul Saab 	int error;
283f0b479cdSPaul Saab 
2844b8d5f2dSRobert Watson #ifdef MAC
2854b8d5f2dSRobert Watson 	error = mac_check_system_settime(td->td_ucred);
2864b8d5f2dSRobert Watson 	if (error)
2874b8d5f2dSRobert Watson 		return (error);
2884b8d5f2dSRobert Watson #endif
28944731cabSJohn Baldwin 	if ((error = suser(td)) != 0)
2907edfb592SJohn Baldwin 		return (error);
291f0b479cdSPaul Saab 	if (clock_id != CLOCK_REALTIME)
2927edfb592SJohn Baldwin 		return (EINVAL);
293f0b479cdSPaul Saab 	if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000)
2947edfb592SJohn Baldwin 		return (EINVAL);
295a0502b19SPoul-Henning Kamp 	/* XXX Don't convert nsec->usec and back */
296f0b479cdSPaul Saab 	TIMESPEC_TO_TIMEVAL(&atv, ats);
2977edfb592SJohn Baldwin 	error = settime(td, &atv);
29894c8fcd8SPeter Wemm 	return (error);
29994c8fcd8SPeter Wemm }
30094c8fcd8SPeter Wemm 
30194c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
30294c8fcd8SPeter Wemm struct clock_getres_args {
30394c8fcd8SPeter Wemm 	clockid_t clock_id;
30494c8fcd8SPeter Wemm 	struct	timespec *tp;
30594c8fcd8SPeter Wemm };
30694c8fcd8SPeter Wemm #endif
307708e7684SPeter Wemm 
30894c8fcd8SPeter Wemm int
30991afe087SPoul-Henning Kamp clock_getres(struct thread *td, struct clock_getres_args *uap)
31094c8fcd8SPeter Wemm {
31194c8fcd8SPeter Wemm 	struct timespec ts;
312f0b479cdSPaul Saab 	int error;
31394c8fcd8SPeter Wemm 
314f0b479cdSPaul Saab 	if (uap->tp == NULL)
315f0b479cdSPaul Saab 		return (0);
316f0b479cdSPaul Saab 
317f0b479cdSPaul Saab 	error = kern_clock_getres(td, uap->clock_id, &ts);
318f0b479cdSPaul Saab 	if (error == 0)
319f0b479cdSPaul Saab 		error = copyout(&ts, uap->tp, sizeof(ts));
320f0b479cdSPaul Saab 	return (error);
321f0b479cdSPaul Saab }
322f0b479cdSPaul Saab 
323f0b479cdSPaul Saab int
324f0b479cdSPaul Saab kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
325f0b479cdSPaul Saab {
326f0b479cdSPaul Saab 
327f0b479cdSPaul Saab 	ts->tv_sec = 0;
328f0b479cdSPaul Saab 	switch (clock_id) {
329b8817154SKelly Yancey 	case CLOCK_REALTIME:
3305e758b95SRobert Watson 	case CLOCK_REALTIME_FAST:
3315e758b95SRobert Watson 	case CLOCK_REALTIME_PRECISE:
332b8817154SKelly Yancey 	case CLOCK_MONOTONIC:
3335e758b95SRobert Watson 	case CLOCK_MONOTONIC_FAST:
3345e758b95SRobert Watson 	case CLOCK_MONOTONIC_PRECISE:
3355eefd889SAndre Oppermann 	case CLOCK_UPTIME:
3365e758b95SRobert Watson 	case CLOCK_UPTIME_FAST:
3375e758b95SRobert Watson 	case CLOCK_UPTIME_PRECISE:
338ac0653dcSBruce Evans 		/*
339ac0653dcSBruce Evans 		 * Round up the result of the division cheaply by adding 1.
340ac0653dcSBruce Evans 		 * Rounding up is especially important if rounding down
341ac0653dcSBruce Evans 		 * would give 0.  Perfect rounding is unimportant.
342ac0653dcSBruce Evans 		 */
343f0b479cdSPaul Saab 		ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
344b8817154SKelly Yancey 		break;
345b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
346b8817154SKelly Yancey 	case CLOCK_PROF:
347b8817154SKelly Yancey 		/* Accurately round up here because we can do so cheaply. */
348f0b479cdSPaul Saab 		ts->tv_nsec = (1000000000 + hz - 1) / hz;
349b8817154SKelly Yancey 		break;
3505e758b95SRobert Watson 	case CLOCK_SECOND:
3515e758b95SRobert Watson 		ts->tv_sec = 1;
3525e758b95SRobert Watson 		ts->tv_nsec = 0;
3535e758b95SRobert Watson 		break;
354b8817154SKelly Yancey 	default:
355b8817154SKelly Yancey 		return (EINVAL);
35694c8fcd8SPeter Wemm 	}
357de0a9241SKelly Yancey 	return (0);
35894c8fcd8SPeter Wemm }
35994c8fcd8SPeter Wemm 
36094c8fcd8SPeter Wemm static int nanowait;
36194c8fcd8SPeter Wemm 
3627fdf2c85SPaul Saab int
3637fdf2c85SPaul Saab kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
3645b870b7bSPeter Wemm {
3655704ba6aSPoul-Henning Kamp 	struct timespec ts, ts2, ts3;
36633841826SPoul-Henning Kamp 	struct timeval tv;
36733841826SPoul-Henning Kamp 	int error;
3685b870b7bSPeter Wemm 
3697d7fb492SBruce Evans 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
370708e7684SPeter Wemm 		return (EINVAL);
371d254af07SMatthew Dillon 	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
3727d7fb492SBruce Evans 		return (0);
373c21410e1SPoul-Henning Kamp 	getnanouptime(&ts);
37400af9731SPoul-Henning Kamp 	timespecadd(&ts, rqt);
37533841826SPoul-Henning Kamp 	TIMESPEC_TO_TIMEVAL(&tv, rqt);
37633841826SPoul-Henning Kamp 	for (;;) {
37733841826SPoul-Henning Kamp 		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
37833841826SPoul-Henning Kamp 		    tvtohz(&tv));
379c21410e1SPoul-Henning Kamp 		getnanouptime(&ts2);
38033841826SPoul-Henning Kamp 		if (error != EWOULDBLOCK) {
38133841826SPoul-Henning Kamp 			if (error == ERESTART)
38294c8fcd8SPeter Wemm 				error = EINTR;
38333841826SPoul-Henning Kamp 			if (rmt != NULL) {
38433841826SPoul-Henning Kamp 				timespecsub(&ts, &ts2);
38533841826SPoul-Henning Kamp 				if (ts.tv_sec < 0)
38633841826SPoul-Henning Kamp 					timespecclear(&ts);
38700af9731SPoul-Henning Kamp 				*rmt = ts;
38800af9731SPoul-Henning Kamp 			}
38933841826SPoul-Henning Kamp 			return (error);
39033841826SPoul-Henning Kamp 		}
39133841826SPoul-Henning Kamp 		if (timespeccmp(&ts2, &ts, >=))
39233841826SPoul-Henning Kamp 			return (0);
3935704ba6aSPoul-Henning Kamp 		ts3 = ts;
3945704ba6aSPoul-Henning Kamp 		timespecsub(&ts3, &ts2);
3955704ba6aSPoul-Henning Kamp 		TIMESPEC_TO_TIMEVAL(&tv, &ts3);
39633841826SPoul-Henning Kamp 	}
3975b870b7bSPeter Wemm }
39894c8fcd8SPeter Wemm 
3995b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
4005b870b7bSPeter Wemm struct nanosleep_args {
4015b870b7bSPeter Wemm 	struct	timespec *rqtp;
4025b870b7bSPeter Wemm 	struct	timespec *rmtp;
4035b870b7bSPeter Wemm };
4045b870b7bSPeter Wemm #endif
4055b870b7bSPeter Wemm 
406fb99ab88SMatthew Dillon /*
407fb99ab88SMatthew Dillon  * MPSAFE
408fb99ab88SMatthew Dillon  */
4095b870b7bSPeter Wemm /* ARGSUSED */
4105b870b7bSPeter Wemm int
41191afe087SPoul-Henning Kamp nanosleep(struct thread *td, struct nanosleep_args *uap)
4125b870b7bSPeter Wemm {
4135b870b7bSPeter Wemm 	struct timespec rmt, rqt;
414fb99ab88SMatthew Dillon 	int error;
4155b870b7bSPeter Wemm 
416d1e405c5SAlfred Perlstein 	error = copyin(uap->rqtp, &rqt, sizeof(rqt));
4175b870b7bSPeter Wemm 	if (error)
4185b870b7bSPeter Wemm 		return (error);
419fb99ab88SMatthew Dillon 
42031f3e2adSAlfred Perlstein 	if (uap->rmtp &&
42131f3e2adSAlfred Perlstein 	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
42231f3e2adSAlfred Perlstein 			return (EFAULT);
4237fdf2c85SPaul Saab 	error = kern_nanosleep(td, &rqt, &rmt);
424d1e405c5SAlfred Perlstein 	if (error && uap->rmtp) {
425fb99ab88SMatthew Dillon 		int error2;
426fb99ab88SMatthew Dillon 
427d1e405c5SAlfred Perlstein 		error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
42831f3e2adSAlfred Perlstein 		if (error2)
429fb99ab88SMatthew Dillon 			error = error2;
430708e7684SPeter Wemm 	}
431708e7684SPeter Wemm 	return (error);
43294c8fcd8SPeter Wemm }
43394c8fcd8SPeter Wemm 
4345b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
435df8bae1dSRodney W. Grimes struct gettimeofday_args {
436df8bae1dSRodney W. Grimes 	struct	timeval *tp;
437df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
438df8bae1dSRodney W. Grimes };
439d2d3e875SBruce Evans #endif
440fb99ab88SMatthew Dillon /*
441fb99ab88SMatthew Dillon  * MPSAFE
442fb99ab88SMatthew Dillon  */
443df8bae1dSRodney W. Grimes /* ARGSUSED */
44426f9a767SRodney W. Grimes int
44591afe087SPoul-Henning Kamp gettimeofday(struct thread *td, struct gettimeofday_args *uap)
446df8bae1dSRodney W. Grimes {
447df8bae1dSRodney W. Grimes 	struct timeval atv;
448411c25edSTim J. Robbins 	struct timezone rtz;
449df8bae1dSRodney W. Grimes 	int error = 0;
450df8bae1dSRodney W. Grimes 
451df8bae1dSRodney W. Grimes 	if (uap->tp) {
452df8bae1dSRodney W. Grimes 		microtime(&atv);
45301609114SAlfred Perlstein 		error = copyout(&atv, uap->tp, sizeof (atv));
454df8bae1dSRodney W. Grimes 	}
45521dcdb38SPoul-Henning Kamp 	if (error == 0 && uap->tzp != NULL) {
45691f1c2b3SPoul-Henning Kamp 		rtz.tz_minuteswest = tz_minuteswest;
45791f1c2b3SPoul-Henning Kamp 		rtz.tz_dsttime = tz_dsttime;
458411c25edSTim J. Robbins 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
45921dcdb38SPoul-Henning Kamp 	}
460df8bae1dSRodney W. Grimes 	return (error);
461df8bae1dSRodney W. Grimes }
462df8bae1dSRodney W. Grimes 
463d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
464df8bae1dSRodney W. Grimes struct settimeofday_args {
465df8bae1dSRodney W. Grimes 	struct	timeval *tv;
466df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
467df8bae1dSRodney W. Grimes };
468d2d3e875SBruce Evans #endif
469fb99ab88SMatthew Dillon /*
470fb99ab88SMatthew Dillon  * MPSAFE
471fb99ab88SMatthew Dillon  */
472df8bae1dSRodney W. Grimes /* ARGSUSED */
47326f9a767SRodney W. Grimes int
47491afe087SPoul-Henning Kamp settimeofday(struct thread *td, struct settimeofday_args *uap)
475df8bae1dSRodney W. Grimes {
476b88ec951SJohn Baldwin 	struct timeval atv, *tvp;
477b88ec951SJohn Baldwin 	struct timezone atz, *tzp;
478b88ec951SJohn Baldwin 	int error;
479b88ec951SJohn Baldwin 
480b88ec951SJohn Baldwin 	if (uap->tv) {
481b88ec951SJohn Baldwin 		error = copyin(uap->tv, &atv, sizeof(atv));
482b88ec951SJohn Baldwin 		if (error)
483b88ec951SJohn Baldwin 			return (error);
484b88ec951SJohn Baldwin 		tvp = &atv;
485b88ec951SJohn Baldwin 	} else
486b88ec951SJohn Baldwin 		tvp = NULL;
487b88ec951SJohn Baldwin 	if (uap->tzp) {
488b88ec951SJohn Baldwin 		error = copyin(uap->tzp, &atz, sizeof(atz));
489b88ec951SJohn Baldwin 		if (error)
490b88ec951SJohn Baldwin 			return (error);
491b88ec951SJohn Baldwin 		tzp = &atz;
492b88ec951SJohn Baldwin 	} else
493b88ec951SJohn Baldwin 		tzp = NULL;
494b88ec951SJohn Baldwin 	return (kern_settimeofday(td, tvp, tzp));
495b88ec951SJohn Baldwin }
496b88ec951SJohn Baldwin 
497b88ec951SJohn Baldwin int
498b88ec951SJohn Baldwin kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
499b88ec951SJohn Baldwin {
500b88ec951SJohn Baldwin 	int error;
501fb99ab88SMatthew Dillon 
5024b8d5f2dSRobert Watson #ifdef MAC
5034b8d5f2dSRobert Watson 	error = mac_check_system_settime(td->td_ucred);
5044b8d5f2dSRobert Watson 	if (error)
5054b8d5f2dSRobert Watson 		return (error);
5064b8d5f2dSRobert Watson #endif
507b88ec951SJohn Baldwin 	error = suser(td);
508b88ec951SJohn Baldwin 	if (error)
5097edfb592SJohn Baldwin 		return (error);
510df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
511b88ec951SJohn Baldwin 	if (tv) {
512b88ec951SJohn Baldwin 		if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
5137edfb592SJohn Baldwin 			return (EINVAL);
514b88ec951SJohn Baldwin 		error = settime(td, tv);
515708e7684SPeter Wemm 	}
516b88ec951SJohn Baldwin 	if (tzp && error == 0) {
517b88ec951SJohn Baldwin 		tz_minuteswest = tzp->tz_minuteswest;
518b88ec951SJohn Baldwin 		tz_dsttime = tzp->tz_dsttime;
519b88ec951SJohn Baldwin 	}
5207edfb592SJohn Baldwin 	return (error);
521b88ec951SJohn Baldwin }
5227edfb592SJohn Baldwin 
523df8bae1dSRodney W. Grimes /*
524df8bae1dSRodney W. Grimes  * Get value of an interval timer.  The process virtual and
525df8bae1dSRodney W. Grimes  * profiling virtual time timers are kept in the p_stats area, since
526df8bae1dSRodney W. Grimes  * they can be swapped out.  These are kept internally in the
527df8bae1dSRodney W. Grimes  * way they are specified externally: in time until they expire.
528df8bae1dSRodney W. Grimes  *
529df8bae1dSRodney W. Grimes  * The real time interval timer is kept in the process table slot
530df8bae1dSRodney W. Grimes  * for the process, and its value (it_value) is kept as an
531df8bae1dSRodney W. Grimes  * absolute time rather than as a delta, so that it is easy to keep
532df8bae1dSRodney W. Grimes  * periodic real-time signals from drifting.
533df8bae1dSRodney W. Grimes  *
534df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
535df8bae1dSRodney W. Grimes  * kern_clock.c.  The real time timer is processed by a timeout
536df8bae1dSRodney W. Grimes  * routine, called from the softclock() routine.  Since a callout
537df8bae1dSRodney W. Grimes  * may be delayed in real time due to interrupt processing in the system,
538df8bae1dSRodney W. Grimes  * it is possible for the real time timeout routine (realitexpire, given below),
539df8bae1dSRodney W. Grimes  * to be delayed in real time past when it is supposed to occur.  It
540df8bae1dSRodney W. Grimes  * does not suffice, therefore, to reload the real timer .it_value from the
541df8bae1dSRodney W. Grimes  * real time timers .it_interval.  Rather, we compute the next time in
542df8bae1dSRodney W. Grimes  * absolute time the timer should go off.
543df8bae1dSRodney W. Grimes  */
544d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
545df8bae1dSRodney W. Grimes struct getitimer_args {
546df8bae1dSRodney W. Grimes 	u_int	which;
547df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
548df8bae1dSRodney W. Grimes };
549d2d3e875SBruce Evans #endif
550fb99ab88SMatthew Dillon /*
551fb99ab88SMatthew Dillon  * MPSAFE
552fb99ab88SMatthew Dillon  */
55326f9a767SRodney W. Grimes int
55491afe087SPoul-Henning Kamp getitimer(struct thread *td, struct getitimer_args *uap)
555df8bae1dSRodney W. Grimes {
556df8bae1dSRodney W. Grimes 	struct itimerval aitv;
557c90110d6SJohn Baldwin 	int error;
558df8bae1dSRodney W. Grimes 
559cfa0efe7SMaxim Sobolev 	error = kern_getitimer(td, uap->which, &aitv);
560cfa0efe7SMaxim Sobolev 	if (error != 0)
561cfa0efe7SMaxim Sobolev 		return (error);
562cfa0efe7SMaxim Sobolev 	return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
563cfa0efe7SMaxim Sobolev }
564cfa0efe7SMaxim Sobolev 
565cfa0efe7SMaxim Sobolev int
566cfa0efe7SMaxim Sobolev kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
567cfa0efe7SMaxim Sobolev {
568cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
569cfa0efe7SMaxim Sobolev 	struct timeval ctv;
570cfa0efe7SMaxim Sobolev 
571cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
572df8bae1dSRodney W. Grimes 		return (EINVAL);
573fb99ab88SMatthew Dillon 
574cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
575df8bae1dSRodney W. Grimes 		/*
576ee002b68SBruce Evans 		 * Convert from absolute to relative time in .it_value
577df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
578df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
579df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
580df8bae1dSRodney W. Grimes 		 */
58196d7f8efSTim J. Robbins 		PROC_LOCK(p);
582cfa0efe7SMaxim Sobolev 		*aitv = p->p_realtimer;
58396d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
584cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
585c21410e1SPoul-Henning Kamp 			getmicrouptime(&ctv);
586cfa0efe7SMaxim Sobolev 			if (timevalcmp(&aitv->it_value, &ctv, <))
587cfa0efe7SMaxim Sobolev 				timevalclear(&aitv->it_value);
588df8bae1dSRodney W. Grimes 			else
589cfa0efe7SMaxim Sobolev 				timevalsub(&aitv->it_value, &ctv);
590227ee8a1SPoul-Henning Kamp 		}
591fb99ab88SMatthew Dillon 	} else {
59296d7f8efSTim J. Robbins 		mtx_lock_spin(&sched_lock);
593cfa0efe7SMaxim Sobolev 		*aitv = p->p_stats->p_timer[which];
59496d7f8efSTim J. Robbins 		mtx_unlock_spin(&sched_lock);
595fb99ab88SMatthew Dillon 	}
596cfa0efe7SMaxim Sobolev 	return (0);
597df8bae1dSRodney W. Grimes }
598df8bae1dSRodney W. Grimes 
599d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
600df8bae1dSRodney W. Grimes struct setitimer_args {
601df8bae1dSRodney W. Grimes 	u_int	which;
602df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
603df8bae1dSRodney W. Grimes };
604d2d3e875SBruce Evans #endif
605cfa0efe7SMaxim Sobolev 
606fb99ab88SMatthew Dillon /*
607fb99ab88SMatthew Dillon  * MPSAFE
608fb99ab88SMatthew Dillon  */
60926f9a767SRodney W. Grimes int
61091afe087SPoul-Henning Kamp setitimer(struct thread *td, struct setitimer_args *uap)
611df8bae1dSRodney W. Grimes {
612cfa0efe7SMaxim Sobolev 	struct itimerval aitv, oitv;
613c90110d6SJohn Baldwin 	int error;
61496d7f8efSTim J. Robbins 
61596d7f8efSTim J. Robbins 	if (uap->itv == NULL) {
61696d7f8efSTim J. Robbins 		uap->itv = uap->oitv;
61796d7f8efSTim J. Robbins 		return (getitimer(td, (struct getitimer_args *)uap));
61896d7f8efSTim J. Robbins 	}
619df8bae1dSRodney W. Grimes 
62096d7f8efSTim J. Robbins 	if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
621df8bae1dSRodney W. Grimes 		return (error);
622cfa0efe7SMaxim Sobolev 	error = kern_setitimer(td, uap->which, &aitv, &oitv);
623cfa0efe7SMaxim Sobolev 	if (error != 0 || uap->oitv == NULL)
624cfa0efe7SMaxim Sobolev 		return (error);
625cfa0efe7SMaxim Sobolev 	return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
626cfa0efe7SMaxim Sobolev }
627cfa0efe7SMaxim Sobolev 
628cfa0efe7SMaxim Sobolev int
629c90110d6SJohn Baldwin kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
630c90110d6SJohn Baldwin     struct itimerval *oitv)
631cfa0efe7SMaxim Sobolev {
632cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
633cfa0efe7SMaxim Sobolev 	struct timeval ctv;
634cfa0efe7SMaxim Sobolev 
6355e85ac17SJohn Baldwin 	if (aitv == NULL)
6365e85ac17SJohn Baldwin 		return (kern_getitimer(td, which, oitv));
6375e85ac17SJohn Baldwin 
638cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
63996d7f8efSTim J. Robbins 		return (EINVAL);
640cfa0efe7SMaxim Sobolev 	if (itimerfix(&aitv->it_value))
641cfa0efe7SMaxim Sobolev 		return (EINVAL);
642cfa0efe7SMaxim Sobolev 	if (!timevalisset(&aitv->it_value))
643cfa0efe7SMaxim Sobolev 		timevalclear(&aitv->it_interval);
644cfa0efe7SMaxim Sobolev 	else if (itimerfix(&aitv->it_interval))
64596d7f8efSTim J. Robbins 		return (EINVAL);
64696d7f8efSTim J. Robbins 
647cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
64896d7f8efSTim J. Robbins 		PROC_LOCK(p);
6494cf41af3SPoul-Henning Kamp 		if (timevalisset(&p->p_realtimer.it_value))
6504f559836SJake Burkholder 			callout_stop(&p->p_itcallout);
65125b4d3a8SJohn Baldwin 		getmicrouptime(&ctv);
652cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
653cfa0efe7SMaxim Sobolev 			callout_reset(&p->p_itcallout, tvtohz(&aitv->it_value),
6544f559836SJake Burkholder 			    realitexpire, p);
655cfa0efe7SMaxim Sobolev 			timevaladd(&aitv->it_value, &ctv);
65625b4d3a8SJohn Baldwin 		}
657cfa0efe7SMaxim Sobolev 		*oitv = p->p_realtimer;
658cfa0efe7SMaxim Sobolev 		p->p_realtimer = *aitv;
65996d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
660cfa0efe7SMaxim Sobolev 		if (timevalisset(&oitv->it_value)) {
661cfa0efe7SMaxim Sobolev 			if (timevalcmp(&oitv->it_value, &ctv, <))
662cfa0efe7SMaxim Sobolev 				timevalclear(&oitv->it_value);
66396d7f8efSTim J. Robbins 			else
664cfa0efe7SMaxim Sobolev 				timevalsub(&oitv->it_value, &ctv);
665fb99ab88SMatthew Dillon 		}
66696d7f8efSTim J. Robbins 	} else {
66796d7f8efSTim J. Robbins 		mtx_lock_spin(&sched_lock);
668cfa0efe7SMaxim Sobolev 		*oitv = p->p_stats->p_timer[which];
669cfa0efe7SMaxim Sobolev 		p->p_stats->p_timer[which] = *aitv;
67096d7f8efSTim J. Robbins 		mtx_unlock_spin(&sched_lock);
67196d7f8efSTim J. Robbins 	}
67296d7f8efSTim J. Robbins 	return (0);
673df8bae1dSRodney W. Grimes }
674df8bae1dSRodney W. Grimes 
675df8bae1dSRodney W. Grimes /*
676df8bae1dSRodney W. Grimes  * Real interval timer expired:
677df8bae1dSRodney W. Grimes  * send process whose timer expired an alarm signal.
678df8bae1dSRodney W. Grimes  * If time is not set up to reload, then just return.
679df8bae1dSRodney W. Grimes  * Else compute next time timer should go off which is > current time.
680df8bae1dSRodney W. Grimes  * This is where delay in processing this timeout causes multiple
681df8bae1dSRodney W. Grimes  * SIGALRM calls to be compressed into one.
682c8b47828SBruce Evans  * tvtohz() always adds 1 to allow for the time until the next clock
6839207f00aSBruce Evans  * interrupt being strictly less than 1 clock tick, but we don't want
6849207f00aSBruce Evans  * that here since we want to appear to be in sync with the clock
6859207f00aSBruce Evans  * interrupt even when we're delayed.
686df8bae1dSRodney W. Grimes  */
687df8bae1dSRodney W. Grimes void
68891afe087SPoul-Henning Kamp realitexpire(void *arg)
689df8bae1dSRodney W. Grimes {
69091afe087SPoul-Henning Kamp 	struct proc *p;
691bfe6c9faSPoul-Henning Kamp 	struct timeval ctv, ntv;
692df8bae1dSRodney W. Grimes 
693df8bae1dSRodney W. Grimes 	p = (struct proc *)arg;
69437824023SJohn Baldwin 	PROC_LOCK(p);
695df8bae1dSRodney W. Grimes 	psignal(p, SIGALRM);
6964cf41af3SPoul-Henning Kamp 	if (!timevalisset(&p->p_realtimer.it_interval)) {
6974cf41af3SPoul-Henning Kamp 		timevalclear(&p->p_realtimer.it_value);
6985499ea01SJohn Baldwin 		if (p->p_flag & P_WEXIT)
6995499ea01SJohn Baldwin 			wakeup(&p->p_itcallout);
70037824023SJohn Baldwin 		PROC_UNLOCK(p);
701df8bae1dSRodney W. Grimes 		return;
702df8bae1dSRodney W. Grimes 	}
703df8bae1dSRodney W. Grimes 	for (;;) {
704df8bae1dSRodney W. Grimes 		timevaladd(&p->p_realtimer.it_value,
705df8bae1dSRodney W. Grimes 		    &p->p_realtimer.it_interval);
706c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
7074cf41af3SPoul-Henning Kamp 		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
708bfe6c9faSPoul-Henning Kamp 			ntv = p->p_realtimer.it_value;
709bfe6c9faSPoul-Henning Kamp 			timevalsub(&ntv, &ctv);
7104f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
7114f559836SJake Burkholder 			    realitexpire, p);
71237824023SJohn Baldwin 			PROC_UNLOCK(p);
713df8bae1dSRodney W. Grimes 			return;
714df8bae1dSRodney W. Grimes 		}
715df8bae1dSRodney W. Grimes 	}
71637824023SJohn Baldwin 	/*NOTREACHED*/
717df8bae1dSRodney W. Grimes }
718df8bae1dSRodney W. Grimes 
719df8bae1dSRodney W. Grimes /*
720df8bae1dSRodney W. Grimes  * Check that a proposed value to load into the .it_value or
721df8bae1dSRodney W. Grimes  * .it_interval part of an interval timer is acceptable, and
722df8bae1dSRodney W. Grimes  * fix it to have at least minimal value (i.e. if it is less
723df8bae1dSRodney W. Grimes  * than the resolution of the clock, round it up.)
724df8bae1dSRodney W. Grimes  */
72526f9a767SRodney W. Grimes int
72691afe087SPoul-Henning Kamp itimerfix(struct timeval *tv)
727df8bae1dSRodney W. Grimes {
728df8bae1dSRodney W. Grimes 
72986857b36SDavid Xu 	if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
730df8bae1dSRodney W. Grimes 		return (EINVAL);
731df8bae1dSRodney W. Grimes 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
732df8bae1dSRodney W. Grimes 		tv->tv_usec = tick;
733df8bae1dSRodney W. Grimes 	return (0);
734df8bae1dSRodney W. Grimes }
735df8bae1dSRodney W. Grimes 
736df8bae1dSRodney W. Grimes /*
737df8bae1dSRodney W. Grimes  * Decrement an interval timer by a specified number
738df8bae1dSRodney W. Grimes  * of microseconds, which must be less than a second,
739df8bae1dSRodney W. Grimes  * i.e. < 1000000.  If the timer expires, then reload
740df8bae1dSRodney W. Grimes  * it.  In this case, carry over (usec - old value) to
741df8bae1dSRodney W. Grimes  * reduce the value reloaded into the timer so that
742df8bae1dSRodney W. Grimes  * the timer does not drift.  This routine assumes
743df8bae1dSRodney W. Grimes  * that it is called in a context where the timers
744df8bae1dSRodney W. Grimes  * on which it is operating cannot change in value.
745df8bae1dSRodney W. Grimes  */
74626f9a767SRodney W. Grimes int
74791afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec)
748df8bae1dSRodney W. Grimes {
749df8bae1dSRodney W. Grimes 
750df8bae1dSRodney W. Grimes 	if (itp->it_value.tv_usec < usec) {
751df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_sec == 0) {
752df8bae1dSRodney W. Grimes 			/* expired, and already in next interval */
753df8bae1dSRodney W. Grimes 			usec -= itp->it_value.tv_usec;
754df8bae1dSRodney W. Grimes 			goto expire;
755df8bae1dSRodney W. Grimes 		}
756df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec += 1000000;
757df8bae1dSRodney W. Grimes 		itp->it_value.tv_sec--;
758df8bae1dSRodney W. Grimes 	}
759df8bae1dSRodney W. Grimes 	itp->it_value.tv_usec -= usec;
760df8bae1dSRodney W. Grimes 	usec = 0;
7614cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_value))
762df8bae1dSRodney W. Grimes 		return (1);
763df8bae1dSRodney W. Grimes 	/* expired, exactly at end of interval */
764df8bae1dSRodney W. Grimes expire:
7654cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_interval)) {
766df8bae1dSRodney W. Grimes 		itp->it_value = itp->it_interval;
767df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec -= usec;
768df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_usec < 0) {
769df8bae1dSRodney W. Grimes 			itp->it_value.tv_usec += 1000000;
770df8bae1dSRodney W. Grimes 			itp->it_value.tv_sec--;
771df8bae1dSRodney W. Grimes 		}
772df8bae1dSRodney W. Grimes 	} else
773df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
774df8bae1dSRodney W. Grimes 	return (0);
775df8bae1dSRodney W. Grimes }
776df8bae1dSRodney W. Grimes 
777df8bae1dSRodney W. Grimes /*
778df8bae1dSRodney W. Grimes  * Add and subtract routines for timevals.
779df8bae1dSRodney W. Grimes  * N.B.: subtract routine doesn't deal with
780df8bae1dSRodney W. Grimes  * results which are before the beginning,
781df8bae1dSRodney W. Grimes  * it just gets very confused in this case.
782df8bae1dSRodney W. Grimes  * Caveat emptor.
783df8bae1dSRodney W. Grimes  */
78426f9a767SRodney W. Grimes void
7856ff7636eSAlfred Perlstein timevaladd(struct timeval *t1, const struct timeval *t2)
786df8bae1dSRodney W. Grimes {
787df8bae1dSRodney W. Grimes 
788df8bae1dSRodney W. Grimes 	t1->tv_sec += t2->tv_sec;
789df8bae1dSRodney W. Grimes 	t1->tv_usec += t2->tv_usec;
790df8bae1dSRodney W. Grimes 	timevalfix(t1);
791df8bae1dSRodney W. Grimes }
792df8bae1dSRodney W. Grimes 
79326f9a767SRodney W. Grimes void
7946ff7636eSAlfred Perlstein timevalsub(struct timeval *t1, const struct timeval *t2)
795df8bae1dSRodney W. Grimes {
796df8bae1dSRodney W. Grimes 
797df8bae1dSRodney W. Grimes 	t1->tv_sec -= t2->tv_sec;
798df8bae1dSRodney W. Grimes 	t1->tv_usec -= t2->tv_usec;
799df8bae1dSRodney W. Grimes 	timevalfix(t1);
800df8bae1dSRodney W. Grimes }
801df8bae1dSRodney W. Grimes 
80287b6de2bSPoul-Henning Kamp static void
80391afe087SPoul-Henning Kamp timevalfix(struct timeval *t1)
804df8bae1dSRodney W. Grimes {
805df8bae1dSRodney W. Grimes 
806df8bae1dSRodney W. Grimes 	if (t1->tv_usec < 0) {
807df8bae1dSRodney W. Grimes 		t1->tv_sec--;
808df8bae1dSRodney W. Grimes 		t1->tv_usec += 1000000;
809df8bae1dSRodney W. Grimes 	}
810df8bae1dSRodney W. Grimes 	if (t1->tv_usec >= 1000000) {
811df8bae1dSRodney W. Grimes 		t1->tv_sec++;
812df8bae1dSRodney W. Grimes 		t1->tv_usec -= 1000000;
813df8bae1dSRodney W. Grimes 	}
814df8bae1dSRodney W. Grimes }
81591974ce1SSam Leffler 
81691974ce1SSam Leffler /*
817addea9d4SSam Leffler  * ratecheck(): simple time-based rate-limit checking.
81891974ce1SSam Leffler  */
81991974ce1SSam Leffler int
82091974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
82191974ce1SSam Leffler {
82291974ce1SSam Leffler 	struct timeval tv, delta;
82391974ce1SSam Leffler 	int rv = 0;
82491974ce1SSam Leffler 
825addea9d4SSam Leffler 	getmicrouptime(&tv);		/* NB: 10ms precision */
826addea9d4SSam Leffler 	delta = tv;
827addea9d4SSam Leffler 	timevalsub(&delta, lasttime);
82891974ce1SSam Leffler 
82991974ce1SSam Leffler 	/*
83091974ce1SSam Leffler 	 * check for 0,0 is so that the message will be seen at least once,
83191974ce1SSam Leffler 	 * even if interval is huge.
83291974ce1SSam Leffler 	 */
83391974ce1SSam Leffler 	if (timevalcmp(&delta, mininterval, >=) ||
83491974ce1SSam Leffler 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
83591974ce1SSam Leffler 		*lasttime = tv;
83691974ce1SSam Leffler 		rv = 1;
83791974ce1SSam Leffler 	}
83891974ce1SSam Leffler 
83991974ce1SSam Leffler 	return (rv);
84091974ce1SSam Leffler }
84191974ce1SSam Leffler 
84291974ce1SSam Leffler /*
84391974ce1SSam Leffler  * ppsratecheck(): packets (or events) per second limitation.
844addea9d4SSam Leffler  *
845addea9d4SSam Leffler  * Return 0 if the limit is to be enforced (e.g. the caller
846addea9d4SSam Leffler  * should drop a packet because of the rate limitation).
847addea9d4SSam Leffler  *
848893bec80SSam Leffler  * maxpps of 0 always causes zero to be returned.  maxpps of -1
849893bec80SSam Leffler  * always causes 1 to be returned; this effectively defeats rate
850893bec80SSam Leffler  * limiting.
851893bec80SSam Leffler  *
852addea9d4SSam Leffler  * Note that we maintain the struct timeval for compatibility
853addea9d4SSam Leffler  * with other bsd systems.  We reuse the storage and just monitor
854addea9d4SSam Leffler  * clock ticks for minimal overhead.
85591974ce1SSam Leffler  */
85691974ce1SSam Leffler int
85791974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
85891974ce1SSam Leffler {
859addea9d4SSam Leffler 	int now;
86091974ce1SSam Leffler 
86191974ce1SSam Leffler 	/*
862addea9d4SSam Leffler 	 * Reset the last time and counter if this is the first call
863addea9d4SSam Leffler 	 * or more than a second has passed since the last update of
864addea9d4SSam Leffler 	 * lasttime.
86591974ce1SSam Leffler 	 */
866addea9d4SSam Leffler 	now = ticks;
867addea9d4SSam Leffler 	if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
868addea9d4SSam Leffler 		lasttime->tv_sec = now;
869addea9d4SSam Leffler 		*curpps = 1;
870893bec80SSam Leffler 		return (maxpps != 0);
871addea9d4SSam Leffler 	} else {
872addea9d4SSam Leffler 		(*curpps)++;		/* NB: ignore potential overflow */
873addea9d4SSam Leffler 		return (maxpps < 0 || *curpps < maxpps);
874addea9d4SSam Leffler 	}
87591974ce1SSam Leffler }
87686857b36SDavid Xu 
87786857b36SDavid Xu static void
87886857b36SDavid Xu itimer_start(void)
87986857b36SDavid Xu {
88086857b36SDavid Xu 	struct kclock rt_clock = {
88186857b36SDavid Xu 		.timer_create  = realtimer_create,
88286857b36SDavid Xu 		.timer_delete  = realtimer_delete,
88386857b36SDavid Xu 		.timer_settime = realtimer_settime,
88486857b36SDavid Xu 		.timer_gettime = realtimer_gettime,
88586857b36SDavid Xu 		.event_hook     = realtimer_event_hook
88686857b36SDavid Xu 	};
88786857b36SDavid Xu 
88886857b36SDavid Xu 	itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
88986857b36SDavid Xu 		NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
89086857b36SDavid Xu 	register_posix_clock(CLOCK_REALTIME,  &rt_clock);
89186857b36SDavid Xu 	register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
89277e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
89377e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
89477e718f7SDavid Xu 	p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
895d26b1a1fSDavid Xu 	EVENTHANDLER_REGISTER(process_exit, itimers_event_hook,
896d26b1a1fSDavid Xu 		(void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY);
897d26b1a1fSDavid Xu 	EVENTHANDLER_REGISTER(process_exec, itimers_event_hook,
898d26b1a1fSDavid Xu 		(void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY);
89986857b36SDavid Xu }
90086857b36SDavid Xu 
90186857b36SDavid Xu int
90286857b36SDavid Xu register_posix_clock(int clockid, struct kclock *clk)
90386857b36SDavid Xu {
90486857b36SDavid Xu 	if ((unsigned)clockid >= MAX_CLOCKS) {
90586857b36SDavid Xu 		printf("%s: invalid clockid\n", __func__);
90686857b36SDavid Xu 		return (0);
90786857b36SDavid Xu 	}
90886857b36SDavid Xu 	posix_clocks[clockid] = *clk;
90986857b36SDavid Xu 	return (1);
91086857b36SDavid Xu }
91186857b36SDavid Xu 
91286857b36SDavid Xu static int
91386857b36SDavid Xu itimer_init(void *mem, int size, int flags)
91486857b36SDavid Xu {
91586857b36SDavid Xu 	struct itimer *it;
91686857b36SDavid Xu 
91786857b36SDavid Xu 	it = (struct itimer *)mem;
91886857b36SDavid Xu 	mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
91986857b36SDavid Xu 	return (0);
92086857b36SDavid Xu }
92186857b36SDavid Xu 
92286857b36SDavid Xu static void
92386857b36SDavid Xu itimer_fini(void *mem, int size)
92486857b36SDavid Xu {
92586857b36SDavid Xu 	struct itimer *it;
92686857b36SDavid Xu 
92786857b36SDavid Xu 	it = (struct itimer *)mem;
92886857b36SDavid Xu 	mtx_destroy(&it->it_mtx);
92986857b36SDavid Xu }
93086857b36SDavid Xu 
93186857b36SDavid Xu static void
93286857b36SDavid Xu itimer_enter(struct itimer *it)
93386857b36SDavid Xu {
93486857b36SDavid Xu 
93586857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
93686857b36SDavid Xu 	it->it_usecount++;
93786857b36SDavid Xu }
93886857b36SDavid Xu 
93986857b36SDavid Xu static void
94086857b36SDavid Xu itimer_leave(struct itimer *it)
94186857b36SDavid Xu {
94286857b36SDavid Xu 
94386857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
94486857b36SDavid Xu 	KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
94586857b36SDavid Xu 
94686857b36SDavid Xu 	if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
94786857b36SDavid Xu 		wakeup(it);
94886857b36SDavid Xu }
94986857b36SDavid Xu 
95086857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
95161d3a4efSDavid Xu struct ktimer_create_args {
95286857b36SDavid Xu 	clockid_t clock_id;
95386857b36SDavid Xu 	struct sigevent * evp;
95461d3a4efSDavid Xu 	int * timerid;
95586857b36SDavid Xu };
95686857b36SDavid Xu #endif
95786857b36SDavid Xu 
95886857b36SDavid Xu int
95961d3a4efSDavid Xu ktimer_create(struct thread *td, struct ktimer_create_args *uap)
96086857b36SDavid Xu {
96186857b36SDavid Xu 	struct sigevent *evp1, ev;
96261d3a4efSDavid Xu 	int id;
96386857b36SDavid Xu 	int error;
96486857b36SDavid Xu 
96586857b36SDavid Xu 	if (uap->evp != NULL) {
96686857b36SDavid Xu 		error = copyin(uap->evp, &ev, sizeof(ev));
96786857b36SDavid Xu 		if (error != 0)
96886857b36SDavid Xu 			return (error);
96986857b36SDavid Xu 		evp1 = &ev;
97086857b36SDavid Xu 	} else
97186857b36SDavid Xu 		evp1 = NULL;
97286857b36SDavid Xu 
97386857b36SDavid Xu 	error = kern_timer_create(td, uap->clock_id, evp1, &id, -1);
97486857b36SDavid Xu 
97586857b36SDavid Xu 	if (error == 0) {
97661d3a4efSDavid Xu 		error = copyout(&id, uap->timerid, sizeof(int));
97786857b36SDavid Xu 		if (error != 0)
97886857b36SDavid Xu 			kern_timer_delete(td, id);
97986857b36SDavid Xu 	}
98086857b36SDavid Xu 	return (error);
98186857b36SDavid Xu }
98286857b36SDavid Xu 
98386857b36SDavid Xu static int
98486857b36SDavid Xu kern_timer_create(struct thread *td, clockid_t clock_id,
98561d3a4efSDavid Xu 	struct sigevent *evp, int *timerid, int preset_id)
98686857b36SDavid Xu {
98786857b36SDavid Xu 	struct proc *p = td->td_proc;
98886857b36SDavid Xu 	struct itimer *it;
98986857b36SDavid Xu 	int id;
99086857b36SDavid Xu 	int error;
99186857b36SDavid Xu 
99286857b36SDavid Xu 	if (clock_id < 0 || clock_id >= MAX_CLOCKS)
99386857b36SDavid Xu 		return (EINVAL);
99486857b36SDavid Xu 
99586857b36SDavid Xu 	if (posix_clocks[clock_id].timer_create == NULL)
99686857b36SDavid Xu 		return (EINVAL);
99786857b36SDavid Xu 
99886857b36SDavid Xu 	if (evp != NULL) {
99986857b36SDavid Xu 		if (evp->sigev_notify != SIGEV_NONE &&
100056c06c4bSDavid Xu 		    evp->sigev_notify != SIGEV_SIGNAL &&
100156c06c4bSDavid Xu 		    evp->sigev_notify != SIGEV_THREAD_ID)
100286857b36SDavid Xu 			return (EINVAL);
100356c06c4bSDavid Xu 		if ((evp->sigev_notify == SIGEV_SIGNAL ||
100456c06c4bSDavid Xu 		     evp->sigev_notify == SIGEV_THREAD_ID) &&
100586857b36SDavid Xu 			!_SIG_VALID(evp->sigev_signo))
100686857b36SDavid Xu 			return (EINVAL);
100786857b36SDavid Xu 	}
100886857b36SDavid Xu 
100960354683SDavid Xu 	if (p->p_itimers == NULL)
101086857b36SDavid Xu 		itimers_alloc(p);
101186857b36SDavid Xu 
101286857b36SDavid Xu 	it = uma_zalloc(itimer_zone, M_WAITOK);
101386857b36SDavid Xu 	it->it_flags = 0;
101486857b36SDavid Xu 	it->it_usecount = 0;
101586857b36SDavid Xu 	it->it_active = 0;
101656c06c4bSDavid Xu 	timespecclear(&it->it_time.it_value);
101756c06c4bSDavid Xu 	timespecclear(&it->it_time.it_interval);
101886857b36SDavid Xu 	it->it_overrun = 0;
101986857b36SDavid Xu 	it->it_overrun_last = 0;
102086857b36SDavid Xu 	it->it_clockid = clock_id;
102186857b36SDavid Xu 	it->it_timerid = -1;
102286857b36SDavid Xu 	it->it_proc = p;
102386857b36SDavid Xu 	ksiginfo_init(&it->it_ksi);
102486857b36SDavid Xu 	it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
102586857b36SDavid Xu 	error = CLOCK_CALL(clock_id, timer_create, (it));
102686857b36SDavid Xu 	if (error != 0)
102786857b36SDavid Xu 		goto out;
102886857b36SDavid Xu 
102986857b36SDavid Xu 	PROC_LOCK(p);
103086857b36SDavid Xu 	if (preset_id != -1) {
103186857b36SDavid Xu 		KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
103286857b36SDavid Xu 		id = preset_id;
103360354683SDavid Xu 		if (p->p_itimers->its_timers[id] != NULL) {
103486857b36SDavid Xu 			PROC_UNLOCK(p);
103586857b36SDavid Xu 			error = 0;
103686857b36SDavid Xu 			goto out;
103786857b36SDavid Xu 		}
103886857b36SDavid Xu 	} else {
103986857b36SDavid Xu 		/*
104086857b36SDavid Xu 		 * Find a free timer slot, skipping those reserved
104186857b36SDavid Xu 		 * for setitimer().
104286857b36SDavid Xu 		 */
104386857b36SDavid Xu 		for (id = 3; id < TIMER_MAX; id++)
104460354683SDavid Xu 			if (p->p_itimers->its_timers[id] == NULL)
104586857b36SDavid Xu 				break;
104686857b36SDavid Xu 		if (id == TIMER_MAX) {
104786857b36SDavid Xu 			PROC_UNLOCK(p);
104886857b36SDavid Xu 			error = EAGAIN;
104986857b36SDavid Xu 			goto out;
105086857b36SDavid Xu 		}
105186857b36SDavid Xu 	}
105286857b36SDavid Xu 	it->it_timerid = id;
105360354683SDavid Xu 	p->p_itimers->its_timers[id] = it;
105486857b36SDavid Xu 	if (evp != NULL)
105586857b36SDavid Xu 		it->it_sigev = *evp;
105686857b36SDavid Xu 	else {
105786857b36SDavid Xu 		it->it_sigev.sigev_notify = SIGEV_SIGNAL;
105886857b36SDavid Xu 		switch (clock_id) {
105986857b36SDavid Xu 		default:
106086857b36SDavid Xu 		case CLOCK_REALTIME:
106186857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGALRM;
106286857b36SDavid Xu 			break;
106386857b36SDavid Xu 		case CLOCK_VIRTUAL:
106486857b36SDavid Xu  			it->it_sigev.sigev_signo = SIGVTALRM;
106586857b36SDavid Xu 			break;
106686857b36SDavid Xu 		case CLOCK_PROF:
106786857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGPROF;
106886857b36SDavid Xu 			break;
106986857b36SDavid Xu 		}
10708f0371f1SDavid Xu 		it->it_sigev.sigev_value.sival_int = id;
107186857b36SDavid Xu 	}
107286857b36SDavid Xu 
107356c06c4bSDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
107456c06c4bSDavid Xu 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
107586857b36SDavid Xu 		it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
107686857b36SDavid Xu 		it->it_ksi.ksi_code = SI_TIMER;
107786857b36SDavid Xu 		it->it_ksi.ksi_value = it->it_sigev.sigev_value;
107886857b36SDavid Xu 		it->it_ksi.ksi_timerid = id;
107986857b36SDavid Xu 	}
108086857b36SDavid Xu 	PROC_UNLOCK(p);
108186857b36SDavid Xu 	*timerid = id;
108286857b36SDavid Xu 	return (0);
108386857b36SDavid Xu 
108486857b36SDavid Xu out:
108586857b36SDavid Xu 	ITIMER_LOCK(it);
108686857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
108786857b36SDavid Xu 	ITIMER_UNLOCK(it);
108886857b36SDavid Xu 	uma_zfree(itimer_zone, it);
108986857b36SDavid Xu 	return (error);
109086857b36SDavid Xu }
109186857b36SDavid Xu 
109286857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
109361d3a4efSDavid Xu struct ktimer_delete_args {
109461d3a4efSDavid Xu 	int timerid;
109586857b36SDavid Xu };
109686857b36SDavid Xu #endif
109786857b36SDavid Xu 
109886857b36SDavid Xu int
109961d3a4efSDavid Xu ktimer_delete(struct thread *td, struct ktimer_delete_args *uap)
110086857b36SDavid Xu {
110186857b36SDavid Xu 	return (kern_timer_delete(td, uap->timerid));
110286857b36SDavid Xu }
110386857b36SDavid Xu 
110486857b36SDavid Xu static struct itimer *
110561d3a4efSDavid Xu itimer_find(struct proc *p, int timerid, int include_deleting)
110686857b36SDavid Xu {
110786857b36SDavid Xu 	struct itimer *it;
110886857b36SDavid Xu 
110986857b36SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
111060354683SDavid Xu 	if ((p->p_itimers == NULL) || (timerid >= TIMER_MAX) ||
111160354683SDavid Xu 	    (it = p->p_itimers->its_timers[timerid]) == NULL) {
111286857b36SDavid Xu 		return (NULL);
111386857b36SDavid Xu 	}
111486857b36SDavid Xu 	ITIMER_LOCK(it);
111586857b36SDavid Xu 	if (!include_deleting && (it->it_flags & ITF_DELETING) != 0) {
111686857b36SDavid Xu 		ITIMER_UNLOCK(it);
111786857b36SDavid Xu 		it = NULL;
111886857b36SDavid Xu 	}
111986857b36SDavid Xu 	return (it);
112086857b36SDavid Xu }
112186857b36SDavid Xu 
112286857b36SDavid Xu static int
112361d3a4efSDavid Xu kern_timer_delete(struct thread *td, int timerid)
112486857b36SDavid Xu {
112586857b36SDavid Xu 	struct proc *p = td->td_proc;
112686857b36SDavid Xu 	struct itimer *it;
112786857b36SDavid Xu 
112886857b36SDavid Xu 	PROC_LOCK(p);
112986857b36SDavid Xu 	it = itimer_find(p, timerid, 0);
113086857b36SDavid Xu 	if (it == NULL) {
113186857b36SDavid Xu 		PROC_UNLOCK(p);
113286857b36SDavid Xu 		return (EINVAL);
113386857b36SDavid Xu 	}
113486857b36SDavid Xu 	PROC_UNLOCK(p);
113586857b36SDavid Xu 
113686857b36SDavid Xu 	it->it_flags |= ITF_DELETING;
113786857b36SDavid Xu 	while (it->it_usecount > 0) {
113886857b36SDavid Xu 		it->it_flags |= ITF_WANTED;
113986857b36SDavid Xu 		msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
114086857b36SDavid Xu 	}
114186857b36SDavid Xu 	it->it_flags &= ~ITF_WANTED;
114286857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
114386857b36SDavid Xu 	ITIMER_UNLOCK(it);
114486857b36SDavid Xu 
114586857b36SDavid Xu 	PROC_LOCK(p);
114686857b36SDavid Xu 	if (KSI_ONQ(&it->it_ksi))
114786857b36SDavid Xu 		sigqueue_take(&it->it_ksi);
114860354683SDavid Xu 	p->p_itimers->its_timers[timerid] = NULL;
114986857b36SDavid Xu 	PROC_UNLOCK(p);
115086857b36SDavid Xu 	uma_zfree(itimer_zone, it);
115186857b36SDavid Xu 	return (0);
115286857b36SDavid Xu }
115386857b36SDavid Xu 
115486857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
115561d3a4efSDavid Xu struct ktimer_settime_args {
115661d3a4efSDavid Xu 	int timerid;
115786857b36SDavid Xu 	int flags;
115886857b36SDavid Xu 	const struct itimerspec * value;
115986857b36SDavid Xu 	struct itimerspec * ovalue;
116086857b36SDavid Xu };
116186857b36SDavid Xu #endif
116286857b36SDavid Xu 
116386857b36SDavid Xu int
116461d3a4efSDavid Xu ktimer_settime(struct thread *td, struct ktimer_settime_args *uap)
116586857b36SDavid Xu {
116686857b36SDavid Xu 	struct proc *p = td->td_proc;
116786857b36SDavid Xu 	struct itimer *it;
116886857b36SDavid Xu 	struct itimerspec val, oval, *ovalp;
116986857b36SDavid Xu 	int error;
117086857b36SDavid Xu 
117186857b36SDavid Xu 	error = copyin(uap->value, &val, sizeof(val));
117286857b36SDavid Xu 	if (error != 0)
117386857b36SDavid Xu 		return (error);
117486857b36SDavid Xu 
117586857b36SDavid Xu 	if (uap->ovalue != NULL)
117686857b36SDavid Xu 		ovalp = &oval;
117786857b36SDavid Xu 	else
117886857b36SDavid Xu 		ovalp = NULL;
117986857b36SDavid Xu 
118086857b36SDavid Xu 	PROC_LOCK(p);
118186857b36SDavid Xu 	if (uap->timerid < 3 ||
118286857b36SDavid Xu 	    (it = itimer_find(p, uap->timerid, 0)) == NULL) {
118386857b36SDavid Xu 		PROC_UNLOCK(p);
118486857b36SDavid Xu 		error = EINVAL;
118586857b36SDavid Xu 	} else {
118686857b36SDavid Xu 		PROC_UNLOCK(p);
118786857b36SDavid Xu 		itimer_enter(it);
118886857b36SDavid Xu 		error = CLOCK_CALL(it->it_clockid, timer_settime,
118986857b36SDavid Xu 				(it, uap->flags, &val, ovalp));
119086857b36SDavid Xu 		itimer_leave(it);
119186857b36SDavid Xu 		ITIMER_UNLOCK(it);
119286857b36SDavid Xu 	}
119386857b36SDavid Xu 	if (error == 0 && uap->ovalue != NULL)
119486857b36SDavid Xu 		error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
119586857b36SDavid Xu 	return (error);
119686857b36SDavid Xu }
119786857b36SDavid Xu 
119886857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
119961d3a4efSDavid Xu struct ktimer_gettime_args {
120061d3a4efSDavid Xu 	int timerid;
120186857b36SDavid Xu 	struct itimerspec * value;
120286857b36SDavid Xu };
120386857b36SDavid Xu #endif
120486857b36SDavid Xu 
120586857b36SDavid Xu int
120661d3a4efSDavid Xu ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap)
120786857b36SDavid Xu {
120886857b36SDavid Xu 	struct proc *p = td->td_proc;
120986857b36SDavid Xu 	struct itimer *it;
121086857b36SDavid Xu 	struct itimerspec val;
121186857b36SDavid Xu 	int error;
121286857b36SDavid Xu 
121386857b36SDavid Xu 	PROC_LOCK(p);
121486857b36SDavid Xu 	if (uap->timerid < 3 ||
121586857b36SDavid Xu 	   (it = itimer_find(p, uap->timerid, 0)) == NULL) {
121686857b36SDavid Xu 		PROC_UNLOCK(p);
121786857b36SDavid Xu 		error = EINVAL;
121886857b36SDavid Xu 	} else {
121986857b36SDavid Xu 		PROC_UNLOCK(p);
122086857b36SDavid Xu 		itimer_enter(it);
122186857b36SDavid Xu 		error = CLOCK_CALL(it->it_clockid, timer_gettime,
122286857b36SDavid Xu 				(it, &val));
122386857b36SDavid Xu 		itimer_leave(it);
122486857b36SDavid Xu 		ITIMER_UNLOCK(it);
122586857b36SDavid Xu 	}
122686857b36SDavid Xu 	if (error == 0)
122786857b36SDavid Xu 		error = copyout(&val, uap->value, sizeof(val));
122886857b36SDavid Xu 	return (error);
122986857b36SDavid Xu }
123086857b36SDavid Xu 
123186857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
123286857b36SDavid Xu struct timer_getoverrun_args {
123361d3a4efSDavid Xu 	int timerid;
123486857b36SDavid Xu };
123586857b36SDavid Xu #endif
123686857b36SDavid Xu 
123786857b36SDavid Xu int
123861d3a4efSDavid Xu ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap)
123986857b36SDavid Xu {
124086857b36SDavid Xu 	struct proc *p = td->td_proc;
124186857b36SDavid Xu 	struct itimer *it;
124286857b36SDavid Xu 	int error ;
124386857b36SDavid Xu 
124486857b36SDavid Xu 	PROC_LOCK(p);
124586857b36SDavid Xu 	if (uap->timerid < 3 ||
124686857b36SDavid Xu 	    (it = itimer_find(p, uap->timerid, 0)) == NULL) {
124786857b36SDavid Xu 		PROC_UNLOCK(p);
124886857b36SDavid Xu 		error = EINVAL;
124986857b36SDavid Xu 	} else {
125086857b36SDavid Xu 		td->td_retval[0] = it->it_overrun_last;
125186857b36SDavid Xu 		ITIMER_UNLOCK(it);
125256c06c4bSDavid Xu 		PROC_UNLOCK(p);
125386857b36SDavid Xu 		error = 0;
125486857b36SDavid Xu 	}
125586857b36SDavid Xu 	return (error);
125686857b36SDavid Xu }
125786857b36SDavid Xu 
125886857b36SDavid Xu static int
125986857b36SDavid Xu realtimer_create(struct itimer *it)
126086857b36SDavid Xu {
126186857b36SDavid Xu 	callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
126286857b36SDavid Xu 	return (0);
126386857b36SDavid Xu }
126486857b36SDavid Xu 
126586857b36SDavid Xu static int
126686857b36SDavid Xu realtimer_delete(struct itimer *it)
126786857b36SDavid Xu {
126886857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
126986857b36SDavid Xu 	callout_stop(&it->it_callout);
127086857b36SDavid Xu 	return (0);
127186857b36SDavid Xu }
127286857b36SDavid Xu 
127386857b36SDavid Xu static int
127486857b36SDavid Xu realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
127586857b36SDavid Xu {
127656c06c4bSDavid Xu 	struct timespec cts;
127786857b36SDavid Xu 
127886857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
127986857b36SDavid Xu 
128056c06c4bSDavid Xu 	realtimer_clocktime(it->it_clockid, &cts);
128156c06c4bSDavid Xu 	*ovalue = it->it_time;
128286857b36SDavid Xu 	if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
128356c06c4bSDavid Xu 		timespecsub(&ovalue->it_value, &cts);
128486857b36SDavid Xu 		if (ovalue->it_value.tv_sec < 0 ||
128586857b36SDavid Xu 		    (ovalue->it_value.tv_sec == 0 &&
128686857b36SDavid Xu 		     ovalue->it_value.tv_nsec == 0)) {
128786857b36SDavid Xu 			ovalue->it_value.tv_sec  = 0;
128886857b36SDavid Xu 			ovalue->it_value.tv_nsec = 1;
128986857b36SDavid Xu 		}
129086857b36SDavid Xu 	}
129186857b36SDavid Xu 	return (0);
129286857b36SDavid Xu }
129386857b36SDavid Xu 
129486857b36SDavid Xu static int
129586857b36SDavid Xu realtimer_settime(struct itimer *it, int flags,
129686857b36SDavid Xu 	struct itimerspec *value, struct itimerspec *ovalue)
129786857b36SDavid Xu {
129856c06c4bSDavid Xu 	struct timespec cts, ts;
129956c06c4bSDavid Xu 	struct timeval tv;
130056c06c4bSDavid Xu 	struct itimerspec val;
130186857b36SDavid Xu 
130286857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
130386857b36SDavid Xu 
130456c06c4bSDavid Xu 	val = *value;
130556c06c4bSDavid Xu 	if (itimespecfix(&val.it_value))
130686857b36SDavid Xu 		return (EINVAL);
130786857b36SDavid Xu 
130856c06c4bSDavid Xu 	if (timespecisset(&val.it_value)) {
130956c06c4bSDavid Xu 		if (itimespecfix(&val.it_interval))
131086857b36SDavid Xu 			return (EINVAL);
131186857b36SDavid Xu 	} else {
131256c06c4bSDavid Xu 		timespecclear(&val.it_interval);
131386857b36SDavid Xu 	}
131486857b36SDavid Xu 
131586857b36SDavid Xu 	if (ovalue != NULL)
131686857b36SDavid Xu 		realtimer_gettime(it, ovalue);
131786857b36SDavid Xu 
131886857b36SDavid Xu 	it->it_time = val;
131956c06c4bSDavid Xu 	if (timespecisset(&val.it_value)) {
132056c06c4bSDavid Xu 		realtimer_clocktime(it->it_clockid, &cts);
132156c06c4bSDavid Xu 		ts = val.it_value;
132286857b36SDavid Xu 		if ((flags & TIMER_ABSTIME) == 0) {
132386857b36SDavid Xu 			/* Convert to absolute time. */
132456c06c4bSDavid Xu 			timespecadd(&it->it_time.it_value, &cts);
132586857b36SDavid Xu 		} else {
132656c06c4bSDavid Xu 			timespecsub(&ts, &cts);
132786857b36SDavid Xu 			/*
132856c06c4bSDavid Xu 			 * We don't care if ts is negative, tztohz will
132986857b36SDavid Xu 			 * fix it.
133086857b36SDavid Xu 			 */
133186857b36SDavid Xu 		}
133256c06c4bSDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
133356c06c4bSDavid Xu 		callout_reset(&it->it_callout, tvtohz(&tv),
133486857b36SDavid Xu 			realtimer_expire, it);
133586857b36SDavid Xu 	} else {
133686857b36SDavid Xu 		callout_stop(&it->it_callout);
133786857b36SDavid Xu 	}
133886857b36SDavid Xu 
133986857b36SDavid Xu 	return (0);
134086857b36SDavid Xu }
134186857b36SDavid Xu 
134286857b36SDavid Xu static void
134356c06c4bSDavid Xu realtimer_clocktime(clockid_t id, struct timespec *ts)
134486857b36SDavid Xu {
134586857b36SDavid Xu 	if (id == CLOCK_REALTIME)
134656c06c4bSDavid Xu 		getnanotime(ts);
134786857b36SDavid Xu 	else	/* CLOCK_MONOTONIC */
134856c06c4bSDavid Xu 		getnanouptime(ts);
134956c06c4bSDavid Xu }
135056c06c4bSDavid Xu 
135156c06c4bSDavid Xu int
135261d3a4efSDavid Xu itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi)
135356c06c4bSDavid Xu {
135456c06c4bSDavid Xu 	struct itimer *it;
135556c06c4bSDavid Xu 
135656c06c4bSDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
135756c06c4bSDavid Xu 	it = itimer_find(p, timerid, 0);
135856c06c4bSDavid Xu 	if (it != NULL) {
135956c06c4bSDavid Xu 		ksi->ksi_overrun = it->it_overrun;
136056c06c4bSDavid Xu 		it->it_overrun_last = it->it_overrun;
136156c06c4bSDavid Xu 		it->it_overrun = 0;
136256c06c4bSDavid Xu 		ITIMER_UNLOCK(it);
136356c06c4bSDavid Xu 		return (0);
136456c06c4bSDavid Xu 	}
136556c06c4bSDavid Xu 	return (EINVAL);
136656c06c4bSDavid Xu }
136756c06c4bSDavid Xu 
136856c06c4bSDavid Xu int
136956c06c4bSDavid Xu itimespecfix(struct timespec *ts)
137056c06c4bSDavid Xu {
137156c06c4bSDavid Xu 
137256c06c4bSDavid Xu 	if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
137356c06c4bSDavid Xu 		return (EINVAL);
137456c06c4bSDavid Xu 	if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
137556c06c4bSDavid Xu 		ts->tv_nsec = tick * 1000;
137656c06c4bSDavid Xu 	return (0);
137786857b36SDavid Xu }
137886857b36SDavid Xu 
137986857b36SDavid Xu static void
138086857b36SDavid Xu realtimer_event_hook(struct proc *p, clockid_t clock_id, int event)
138186857b36SDavid Xu {
138286857b36SDavid Xu 	struct itimers *its;
138386857b36SDavid Xu 	struct itimer  *it;
138486857b36SDavid Xu 	int i;
138586857b36SDavid Xu 
138686857b36SDavid Xu 	/*
138786857b36SDavid Xu 	 * Timer 0 (ITIMER_REAL) is XSI interval timer, according to POSIX
138886857b36SDavid Xu 	 * specification, it should be inherited by new process image.
138986857b36SDavid Xu 	 */
139086857b36SDavid Xu 	if (event == ITIMER_EV_EXEC)
139186857b36SDavid Xu 		i = 1;
139286857b36SDavid Xu 	else
139386857b36SDavid Xu 		i = 0;
139460354683SDavid Xu 	its = p->p_itimers;
139586857b36SDavid Xu 	for (; i < TIMER_MAX; i++) {
139686857b36SDavid Xu 		if ((it = its->its_timers[i]) != NULL &&
139786857b36SDavid Xu 		     it->it_clockid == clock_id) {
139886857b36SDavid Xu 			ITIMER_LOCK(it);
139986857b36SDavid Xu 			callout_stop(&it->it_callout);
140086857b36SDavid Xu 			ITIMER_UNLOCK(it);
140186857b36SDavid Xu 		}
140286857b36SDavid Xu 	}
140386857b36SDavid Xu }
140486857b36SDavid Xu 
140586857b36SDavid Xu /* Timeout callback for realtime timer */
140686857b36SDavid Xu static void
140786857b36SDavid Xu realtimer_expire(void *arg)
140886857b36SDavid Xu {
140956c06c4bSDavid Xu 	struct timespec cts, ts;
141056c06c4bSDavid Xu 	struct timeval tv;
141186857b36SDavid Xu 	struct itimer *it;
141286857b36SDavid Xu 	struct proc *p;
141386857b36SDavid Xu 
141486857b36SDavid Xu 	it = (struct itimer *)arg;
141586857b36SDavid Xu 	p = it->it_proc;
141686857b36SDavid Xu 
141756c06c4bSDavid Xu 	realtimer_clocktime(it->it_clockid, &cts);
141886857b36SDavid Xu 	/* Only fire if time is reached. */
141956c06c4bSDavid Xu 	if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
142056c06c4bSDavid Xu 		if (timespecisset(&it->it_time.it_interval)) {
142156c06c4bSDavid Xu 			timespecadd(&it->it_time.it_value,
142286857b36SDavid Xu 				    &it->it_time.it_interval);
142356c06c4bSDavid Xu 			while (timespeccmp(&cts, &it->it_time.it_value, >=)) {
142477e718f7SDavid Xu 				if (it->it_overrun < INT_MAX)
142586857b36SDavid Xu 					it->it_overrun++;
142677e718f7SDavid Xu 				else
142777e718f7SDavid Xu 					it->it_ksi.ksi_errno = ERANGE;
142856c06c4bSDavid Xu 				timespecadd(&it->it_time.it_value,
142986857b36SDavid Xu 					    &it->it_time.it_interval);
143086857b36SDavid Xu 			}
143186857b36SDavid Xu 		} else {
143286857b36SDavid Xu 			/* single shot timer ? */
143356c06c4bSDavid Xu 			timespecclear(&it->it_time.it_value);
143486857b36SDavid Xu 		}
143556c06c4bSDavid Xu 		if (timespecisset(&it->it_time.it_value)) {
143656c06c4bSDavid Xu 			ts = it->it_time.it_value;
143756c06c4bSDavid Xu 			timespecsub(&ts, &cts);
143856c06c4bSDavid Xu 			TIMESPEC_TO_TIMEVAL(&tv, &ts);
143956c06c4bSDavid Xu 			callout_reset(&it->it_callout, tvtohz(&tv),
144086857b36SDavid Xu 				 realtimer_expire, it);
144186857b36SDavid Xu 		}
144286857b36SDavid Xu 		ITIMER_UNLOCK(it);
144386857b36SDavid Xu 		itimer_fire(it);
144486857b36SDavid Xu 		ITIMER_LOCK(it);
144556c06c4bSDavid Xu 	} else if (timespecisset(&it->it_time.it_value)) {
144656c06c4bSDavid Xu 		ts = it->it_time.it_value;
144756c06c4bSDavid Xu 		timespecsub(&ts, &cts);
144856c06c4bSDavid Xu 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
144956c06c4bSDavid Xu 		callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
145086857b36SDavid Xu  			it);
145186857b36SDavid Xu 	}
145286857b36SDavid Xu }
145386857b36SDavid Xu 
145486857b36SDavid Xu void
145586857b36SDavid Xu itimer_fire(struct itimer *it)
145686857b36SDavid Xu {
145786857b36SDavid Xu 	struct proc *p = it->it_proc;
14586d7b314bSDavid Xu 	int ret;
145986857b36SDavid Xu 
146056c06c4bSDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
146156c06c4bSDavid Xu 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
146286857b36SDavid Xu 		PROC_LOCK(p);
14636d7b314bSDavid Xu 		if (!KSI_ONQ(&it->it_ksi)) {
146477e718f7SDavid Xu 			it->it_ksi.ksi_errno = 0;
14656d7b314bSDavid Xu 			ret = psignal_event(p, &it->it_sigev, &it->it_ksi);
14666d7b314bSDavid Xu 			if (__predict_false(ret != 0)) {
146786857b36SDavid Xu 				it->it_overrun++;
146856c06c4bSDavid Xu 				/*
146956c06c4bSDavid Xu 				 * Broken userland code, thread went
147056c06c4bSDavid Xu 				 * away, disarm the timer.
147156c06c4bSDavid Xu 				 */
14726d7b314bSDavid Xu 				if (ret == ESRCH) {
147356c06c4bSDavid Xu 					ITIMER_LOCK(it);
147456c06c4bSDavid Xu 					timespecclear(&it->it_time.it_value);
147556c06c4bSDavid Xu 					timespecclear(&it->it_time.it_interval);
147656c06c4bSDavid Xu 					callout_stop(&it->it_callout);
147756c06c4bSDavid Xu 					ITIMER_UNLOCK(it);
14786d7b314bSDavid Xu 				}
147956c06c4bSDavid Xu 			}
148056c06c4bSDavid Xu 		} else {
148177e718f7SDavid Xu 			if (it->it_overrun < INT_MAX)
14826d7b314bSDavid Xu 				it->it_overrun++;
148377e718f7SDavid Xu 			else
148477e718f7SDavid Xu 				it->it_ksi.ksi_errno = ERANGE;
148556c06c4bSDavid Xu 		}
148686857b36SDavid Xu 		PROC_UNLOCK(p);
148786857b36SDavid Xu 	}
148886857b36SDavid Xu }
148986857b36SDavid Xu 
149086857b36SDavid Xu static void
149186857b36SDavid Xu itimers_alloc(struct proc *p)
149286857b36SDavid Xu {
149360354683SDavid Xu 	struct itimers *its;
149460354683SDavid Xu 	int i;
149586857b36SDavid Xu 
149660354683SDavid Xu 	its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
149786857b36SDavid Xu 	LIST_INIT(&its->its_virtual);
149886857b36SDavid Xu 	LIST_INIT(&its->its_prof);
149986857b36SDavid Xu 	TAILQ_INIT(&its->its_worklist);
150060354683SDavid Xu 	for (i = 0; i < TIMER_MAX; i++)
150160354683SDavid Xu 		its->its_timers[i] = NULL;
150260354683SDavid Xu 	PROC_LOCK(p);
150360354683SDavid Xu 	if (p->p_itimers == NULL) {
150460354683SDavid Xu 		p->p_itimers = its;
150560354683SDavid Xu 		PROC_UNLOCK(p);
150660354683SDavid Xu 	}
150760354683SDavid Xu 	else {
150860354683SDavid Xu 		PROC_UNLOCK(p);
150960354683SDavid Xu 		free(its, M_SUBPROC);
151060354683SDavid Xu 	}
151186857b36SDavid Xu }
151286857b36SDavid Xu 
151386857b36SDavid Xu /* Clean up timers when some process events are being triggered. */
1514d26b1a1fSDavid Xu static void
1515d26b1a1fSDavid Xu itimers_event_hook(void *arg, struct proc *p)
151686857b36SDavid Xu {
151786857b36SDavid Xu 	struct itimers *its;
151886857b36SDavid Xu 	struct itimer *it;
15193e70c6f0SDavid Xu 	int event = (int)(intptr_t)arg;
152086857b36SDavid Xu 	int i;
152186857b36SDavid Xu 
152260354683SDavid Xu 	if (p->p_itimers != NULL) {
152360354683SDavid Xu 		its = p->p_itimers;
152486857b36SDavid Xu 		for (i = 0; i < MAX_CLOCKS; ++i) {
152586857b36SDavid Xu 			if (posix_clocks[i].event_hook != NULL)
152686857b36SDavid Xu 				CLOCK_CALL(i, event_hook, (p, i, event));
152786857b36SDavid Xu 		}
152886857b36SDavid Xu 		/*
152986857b36SDavid Xu 		 * According to susv3, XSI interval timers should be inherited
153086857b36SDavid Xu 		 * by new image.
153186857b36SDavid Xu 		 */
153286857b36SDavid Xu 		if (event == ITIMER_EV_EXEC)
153386857b36SDavid Xu 			i = 3;
153486857b36SDavid Xu 		else if (event == ITIMER_EV_EXIT)
153586857b36SDavid Xu 			i = 0;
153686857b36SDavid Xu 		else
153786857b36SDavid Xu 			panic("unhandled event");
153886857b36SDavid Xu 		for (; i < TIMER_MAX; ++i) {
153986857b36SDavid Xu 			if ((it = its->its_timers[i]) != NULL) {
154086857b36SDavid Xu 				PROC_LOCK(p);
154186857b36SDavid Xu 				if (KSI_ONQ(&it->it_ksi))
154286857b36SDavid Xu 					sigqueue_take(&it->it_ksi);
154386857b36SDavid Xu 				PROC_UNLOCK(p);
154486857b36SDavid Xu 				uma_zfree(itimer_zone, its->its_timers[i]);
154586857b36SDavid Xu 				its->its_timers[i] = NULL;
154686857b36SDavid Xu 			}
154786857b36SDavid Xu 		}
154886857b36SDavid Xu 		if (its->its_timers[0] == NULL &&
154986857b36SDavid Xu 		    its->its_timers[1] == NULL &&
155086857b36SDavid Xu 		    its->its_timers[2] == NULL) {
155160354683SDavid Xu 			free(its, M_SUBPROC);
155260354683SDavid Xu 			p->p_itimers = NULL;
155386857b36SDavid Xu 		}
155486857b36SDavid Xu 	}
155586857b36SDavid Xu }
1556