xref: /freebsd/sys/kern/kern_time.c (revision 86857b368d4186c9c57f3472f55f5c3346d8a0c1)
19454b2d8SWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)kern_time.c	8.1 (Berkeley) 6/10/93
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
32677b542eSDavid E. O'Brien #include <sys/cdefs.h>
33677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
34677b542eSDavid E. O'Brien 
354b8d5f2dSRobert Watson #include "opt_mac.h"
364b8d5f2dSRobert Watson 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38e96c1fdcSPeter Wemm #include <sys/systm.h>
39fb919e4dSMark Murray #include <sys/lock.h>
40fb919e4dSMark Murray #include <sys/mutex.h>
41d2d3e875SBruce Evans #include <sys/sysproto.h>
42df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
43797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
44df8bae1dSRodney W. Grimes #include <sys/kernel.h>
454b8d5f2dSRobert Watson #include <sys/mac.h>
46efa42cbcSPaul Saab #include <sys/syscallsubr.h>
4794c8fcd8SPeter Wemm #include <sys/sysent.h>
48df8bae1dSRodney W. Grimes #include <sys/proc.h>
49708e7684SPeter Wemm #include <sys/time.h>
5086857b36SDavid Xu #include <sys/timers.h>
5191266b96SPoul-Henning Kamp #include <sys/timetc.h>
52df8bae1dSRodney W. Grimes #include <sys/vnode.h>
53fb919e4dSMark Murray 
545b870b7bSPeter Wemm #include <vm/vm.h>
555b870b7bSPeter Wemm #include <vm/vm_extern.h>
56df8bae1dSRodney W. Grimes 
5786857b36SDavid Xu #define MAX_CLOCKS 	(CLOCK_MONOTONIC+1)
5886857b36SDavid Xu 
5991f1c2b3SPoul-Henning Kamp int tz_minuteswest;
6091f1c2b3SPoul-Henning Kamp int tz_dsttime;
61ac7e6123SDavid Greenman 
6286857b36SDavid Xu static struct kclock	posix_clocks[MAX_CLOCKS];
6386857b36SDavid Xu static uma_zone_t	itimer_zone = NULL;
6486857b36SDavid Xu 
65df8bae1dSRodney W. Grimes /*
66df8bae1dSRodney W. Grimes  * Time of day and interval timer support.
67df8bae1dSRodney W. Grimes  *
68df8bae1dSRodney W. Grimes  * These routines provide the kernel entry points to get and set
69df8bae1dSRodney W. Grimes  * the time-of-day and per-process interval timers.  Subroutines
70df8bae1dSRodney W. Grimes  * here provide support for adding and subtracting timeval structures
71df8bae1dSRodney W. Grimes  * and decrementing interval timers, optionally reloading the interval
72df8bae1dSRodney W. Grimes  * timers when they expire.
73df8bae1dSRodney W. Grimes  */
74df8bae1dSRodney W. Grimes 
757edfb592SJohn Baldwin static int	settime(struct thread *, struct timeval *);
764d77a549SAlfred Perlstein static void	timevalfix(struct timeval *);
774d77a549SAlfred Perlstein static void	no_lease_updatetime(int);
7894c8fcd8SPeter Wemm 
7986857b36SDavid Xu static void	itimer_start(void);
8086857b36SDavid Xu static int	itimer_init(void *, int, int);
8186857b36SDavid Xu static void	itimer_fini(void *, int);
8286857b36SDavid Xu static void	itimer_enter(struct itimer *);
8386857b36SDavid Xu static void	itimer_leave(struct itimer *);
8486857b36SDavid Xu static struct itimer *itimer_find(struct proc *, timer_t, int);
8586857b36SDavid Xu static void	itimers_alloc(struct proc *);
8686857b36SDavid Xu static int	realtimer_create(struct itimer *);
8786857b36SDavid Xu static int	realtimer_gettime(struct itimer *, struct itimerspec *);
8886857b36SDavid Xu static int	realtimer_settime(struct itimer *, int,
8986857b36SDavid Xu 			struct itimerspec *, struct itimerspec *);
9086857b36SDavid Xu static int	realtimer_delete(struct itimer *);
9186857b36SDavid Xu static void	realtimer_clocktime(clockid_t, struct timeval *);
9286857b36SDavid Xu static void	realtimer_expire(void *);
9386857b36SDavid Xu static void	realtimer_event_hook(struct proc *, clockid_t, int event);
9486857b36SDavid Xu static int	kern_timer_create(struct thread *, clockid_t,
9586857b36SDavid Xu 			struct sigevent *, timer_t *, timer_t);
9686857b36SDavid Xu static int	kern_timer_delete(struct thread *, timer_t);
9786857b36SDavid Xu 
9886857b36SDavid Xu int		register_posix_clock(int, struct kclock *);
9986857b36SDavid Xu void		itimer_fire(struct itimer *it);
10086857b36SDavid Xu 
10186857b36SDavid Xu #define CLOCK_CALL(clock, call, arglist)		\
10286857b36SDavid Xu 	((*posix_clocks[clock].call) arglist)
10386857b36SDavid Xu 
10486857b36SDavid Xu SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL);
10586857b36SDavid Xu 
10686857b36SDavid Xu 
1071b09ae77SPoul-Henning Kamp static void
1081b09ae77SPoul-Henning Kamp no_lease_updatetime(deltat)
1091b09ae77SPoul-Henning Kamp 	int deltat;
1101b09ae77SPoul-Henning Kamp {
1111b09ae77SPoul-Henning Kamp }
1121b09ae77SPoul-Henning Kamp 
1134d77a549SAlfred Perlstein void (*lease_updatetime)(int)  = no_lease_updatetime;
1141b09ae77SPoul-Henning Kamp 
11594c8fcd8SPeter Wemm static int
11691afe087SPoul-Henning Kamp settime(struct thread *td, struct timeval *tv)
11794c8fcd8SPeter Wemm {
118fcae3aa6SNick Sayer 	struct timeval delta, tv1, tv2;
119c0bd94a7SNick Sayer 	static struct timeval maxtime, laststep;
1207ec73f64SPoul-Henning Kamp 	struct timespec ts;
12194c8fcd8SPeter Wemm 	int s;
12294c8fcd8SPeter Wemm 
123708e7684SPeter Wemm 	s = splclock();
1249c8fff87SBruce Evans 	microtime(&tv1);
12500af9731SPoul-Henning Kamp 	delta = *tv;
12600af9731SPoul-Henning Kamp 	timevalsub(&delta, &tv1);
12794c8fcd8SPeter Wemm 
12894c8fcd8SPeter Wemm 	/*
1299c8fff87SBruce Evans 	 * If the system is secure, we do not allow the time to be
130fcae3aa6SNick Sayer 	 * set to a value earlier than 1 second less than the highest
131fcae3aa6SNick Sayer 	 * time we have yet seen. The worst a miscreant can do in
132fcae3aa6SNick Sayer 	 * this circumstance is "freeze" time. He couldn't go
133fcae3aa6SNick Sayer 	 * back to the past.
134c0bd94a7SNick Sayer 	 *
135c0bd94a7SNick Sayer 	 * We similarly do not allow the clock to be stepped more
136c0bd94a7SNick Sayer 	 * than one second, nor more than once per second. This allows
137c0bd94a7SNick Sayer 	 * a miscreant to make the clock march double-time, but no worse.
13894c8fcd8SPeter Wemm 	 */
1397edfb592SJohn Baldwin 	if (securelevel_gt(td->td_ucred, 1) != 0) {
140fcae3aa6SNick Sayer 		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
1413f92429aSMatt Jacob 			/*
142c0bd94a7SNick Sayer 			 * Update maxtime to latest time we've seen.
1433f92429aSMatt Jacob 			 */
144fcae3aa6SNick Sayer 			if (tv1.tv_sec > maxtime.tv_sec)
145fcae3aa6SNick Sayer 				maxtime = tv1;
146fcae3aa6SNick Sayer 			tv2 = *tv;
147fcae3aa6SNick Sayer 			timevalsub(&tv2, &maxtime);
148fcae3aa6SNick Sayer 			if (tv2.tv_sec < -1) {
1493f92429aSMatt Jacob 				tv->tv_sec = maxtime.tv_sec - 1;
150fcae3aa6SNick Sayer 				printf("Time adjustment clamped to -1 second\n");
151fcae3aa6SNick Sayer 			}
1523f92429aSMatt Jacob 		} else {
153c0bd94a7SNick Sayer 			if (tv1.tv_sec == laststep.tv_sec) {
154c0bd94a7SNick Sayer 				splx(s);
155c0bd94a7SNick Sayer 				return (EPERM);
156c0bd94a7SNick Sayer 			}
157c0bd94a7SNick Sayer 			if (delta.tv_sec > 1) {
158c0bd94a7SNick Sayer 				tv->tv_sec = tv1.tv_sec + 1;
159c0bd94a7SNick Sayer 				printf("Time adjustment clamped to +1 second\n");
160c0bd94a7SNick Sayer 			}
161c0bd94a7SNick Sayer 			laststep = *tv;
162fcae3aa6SNick Sayer 		}
1639c8fff87SBruce Evans 	}
1649c8fff87SBruce Evans 
1657ec73f64SPoul-Henning Kamp 	ts.tv_sec = tv->tv_sec;
1667ec73f64SPoul-Henning Kamp 	ts.tv_nsec = tv->tv_usec * 1000;
1677edfb592SJohn Baldwin 	mtx_lock(&Giant);
16891266b96SPoul-Henning Kamp 	tc_setclock(&ts);
16994c8fcd8SPeter Wemm 	(void) splsoftclock();
17094c8fcd8SPeter Wemm 	lease_updatetime(delta.tv_sec);
17194c8fcd8SPeter Wemm 	splx(s);
17294c8fcd8SPeter Wemm 	resettodr();
1737edfb592SJohn Baldwin 	mtx_unlock(&Giant);
17494c8fcd8SPeter Wemm 	return (0);
17594c8fcd8SPeter Wemm }
17694c8fcd8SPeter Wemm 
17794c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
17894c8fcd8SPeter Wemm struct clock_gettime_args {
17994c8fcd8SPeter Wemm 	clockid_t clock_id;
18094c8fcd8SPeter Wemm 	struct	timespec *tp;
18194c8fcd8SPeter Wemm };
18294c8fcd8SPeter Wemm #endif
183708e7684SPeter Wemm 
184fb99ab88SMatthew Dillon /*
185fb99ab88SMatthew Dillon  * MPSAFE
186fb99ab88SMatthew Dillon  */
18794c8fcd8SPeter Wemm /* ARGSUSED */
18894c8fcd8SPeter Wemm int
18991afe087SPoul-Henning Kamp clock_gettime(struct thread *td, struct clock_gettime_args *uap)
19094c8fcd8SPeter Wemm {
19194c8fcd8SPeter Wemm 	struct timespec ats;
192f0b479cdSPaul Saab 	int error;
193f0b479cdSPaul Saab 
194f0b479cdSPaul Saab 	error = kern_clock_gettime(td, uap->clock_id, &ats);
195f0b479cdSPaul Saab 	if (error == 0)
196f0b479cdSPaul Saab 		error = copyout(&ats, uap->tp, sizeof(ats));
197f0b479cdSPaul Saab 
198f0b479cdSPaul Saab 	return (error);
199f0b479cdSPaul Saab }
200f0b479cdSPaul Saab 
201f0b479cdSPaul Saab int
202f0b479cdSPaul Saab kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
203f0b479cdSPaul Saab {
204de0a9241SKelly Yancey 	struct timeval sys, user;
20578c85e8dSJohn Baldwin 	struct proc *p;
20694c8fcd8SPeter Wemm 
20778c85e8dSJohn Baldwin 	p = td->td_proc;
208f0b479cdSPaul Saab 	switch (clock_id) {
209b8817154SKelly Yancey 	case CLOCK_REALTIME:
210f0b479cdSPaul Saab 		nanotime(ats);
211b8817154SKelly Yancey 		break;
212b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
21378c85e8dSJohn Baldwin 		PROC_LOCK(p);
21478c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
21578c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
216f0b479cdSPaul Saab 		TIMEVAL_TO_TIMESPEC(&user, ats);
217b8817154SKelly Yancey 		break;
218b8817154SKelly Yancey 	case CLOCK_PROF:
21978c85e8dSJohn Baldwin 		PROC_LOCK(p);
22078c85e8dSJohn Baldwin 		calcru(p, &user, &sys);
22178c85e8dSJohn Baldwin 		PROC_UNLOCK(p);
222de0a9241SKelly Yancey 		timevaladd(&user, &sys);
223f0b479cdSPaul Saab 		TIMEVAL_TO_TIMESPEC(&user, ats);
224de0a9241SKelly Yancey 		break;
225de0a9241SKelly Yancey 	case CLOCK_MONOTONIC:
226f0b479cdSPaul Saab 		nanouptime(ats);
227b8817154SKelly Yancey 		break;
228b8817154SKelly Yancey 	default:
2295cb3dc8fSPoul-Henning Kamp 		return (EINVAL);
230b8817154SKelly Yancey 	}
231f0b479cdSPaul Saab 	return (0);
23294c8fcd8SPeter Wemm }
23394c8fcd8SPeter Wemm 
23494c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
23594c8fcd8SPeter Wemm struct clock_settime_args {
23694c8fcd8SPeter Wemm 	clockid_t clock_id;
23794c8fcd8SPeter Wemm 	const struct	timespec *tp;
23894c8fcd8SPeter Wemm };
23994c8fcd8SPeter Wemm #endif
240708e7684SPeter Wemm 
241fb99ab88SMatthew Dillon /*
242fb99ab88SMatthew Dillon  * MPSAFE
243fb99ab88SMatthew Dillon  */
24494c8fcd8SPeter Wemm /* ARGSUSED */
24594c8fcd8SPeter Wemm int
24691afe087SPoul-Henning Kamp clock_settime(struct thread *td, struct clock_settime_args *uap)
24794c8fcd8SPeter Wemm {
24894c8fcd8SPeter Wemm 	struct timespec ats;
24994c8fcd8SPeter Wemm 	int error;
25094c8fcd8SPeter Wemm 
251f0b479cdSPaul Saab 	if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
252f0b479cdSPaul Saab 		return (error);
253f0b479cdSPaul Saab 	return (kern_clock_settime(td, uap->clock_id, &ats));
254f0b479cdSPaul Saab }
255f0b479cdSPaul Saab 
256f0b479cdSPaul Saab int
257f0b479cdSPaul Saab kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
258f0b479cdSPaul Saab {
259f0b479cdSPaul Saab 	struct timeval atv;
260f0b479cdSPaul Saab 	int error;
261f0b479cdSPaul Saab 
2624b8d5f2dSRobert Watson #ifdef MAC
2634b8d5f2dSRobert Watson 	error = mac_check_system_settime(td->td_ucred);
2644b8d5f2dSRobert Watson 	if (error)
2654b8d5f2dSRobert Watson 		return (error);
2664b8d5f2dSRobert Watson #endif
26744731cabSJohn Baldwin 	if ((error = suser(td)) != 0)
2687edfb592SJohn Baldwin 		return (error);
269f0b479cdSPaul Saab 	if (clock_id != CLOCK_REALTIME)
2707edfb592SJohn Baldwin 		return (EINVAL);
271f0b479cdSPaul Saab 	if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000)
2727edfb592SJohn Baldwin 		return (EINVAL);
273a0502b19SPoul-Henning Kamp 	/* XXX Don't convert nsec->usec and back */
274f0b479cdSPaul Saab 	TIMESPEC_TO_TIMEVAL(&atv, ats);
2757edfb592SJohn Baldwin 	error = settime(td, &atv);
27694c8fcd8SPeter Wemm 	return (error);
27794c8fcd8SPeter Wemm }
27894c8fcd8SPeter Wemm 
27994c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
28094c8fcd8SPeter Wemm struct clock_getres_args {
28194c8fcd8SPeter Wemm 	clockid_t clock_id;
28294c8fcd8SPeter Wemm 	struct	timespec *tp;
28394c8fcd8SPeter Wemm };
28494c8fcd8SPeter Wemm #endif
285708e7684SPeter Wemm 
28694c8fcd8SPeter Wemm int
28791afe087SPoul-Henning Kamp clock_getres(struct thread *td, struct clock_getres_args *uap)
28894c8fcd8SPeter Wemm {
28994c8fcd8SPeter Wemm 	struct timespec ts;
290f0b479cdSPaul Saab 	int error;
29194c8fcd8SPeter Wemm 
292f0b479cdSPaul Saab 	if (uap->tp == NULL)
293f0b479cdSPaul Saab 		return (0);
294f0b479cdSPaul Saab 
295f0b479cdSPaul Saab 	error = kern_clock_getres(td, uap->clock_id, &ts);
296f0b479cdSPaul Saab 	if (error == 0)
297f0b479cdSPaul Saab 		error = copyout(&ts, uap->tp, sizeof(ts));
298f0b479cdSPaul Saab 	return (error);
299f0b479cdSPaul Saab }
300f0b479cdSPaul Saab 
301f0b479cdSPaul Saab int
302f0b479cdSPaul Saab kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
303f0b479cdSPaul Saab {
304f0b479cdSPaul Saab 
305f0b479cdSPaul Saab 	ts->tv_sec = 0;
306f0b479cdSPaul Saab 	switch (clock_id) {
307b8817154SKelly Yancey 	case CLOCK_REALTIME:
308b8817154SKelly Yancey 	case CLOCK_MONOTONIC:
309ac0653dcSBruce Evans 		/*
310ac0653dcSBruce Evans 		 * Round up the result of the division cheaply by adding 1.
311ac0653dcSBruce Evans 		 * Rounding up is especially important if rounding down
312ac0653dcSBruce Evans 		 * would give 0.  Perfect rounding is unimportant.
313ac0653dcSBruce Evans 		 */
314f0b479cdSPaul Saab 		ts->tv_nsec = 1000000000 / tc_getfrequency() + 1;
315b8817154SKelly Yancey 		break;
316b8817154SKelly Yancey 	case CLOCK_VIRTUAL:
317b8817154SKelly Yancey 	case CLOCK_PROF:
318b8817154SKelly Yancey 		/* Accurately round up here because we can do so cheaply. */
319f0b479cdSPaul Saab 		ts->tv_nsec = (1000000000 + hz - 1) / hz;
320b8817154SKelly Yancey 		break;
321b8817154SKelly Yancey 	default:
322b8817154SKelly Yancey 		return (EINVAL);
32394c8fcd8SPeter Wemm 	}
324de0a9241SKelly Yancey 	return (0);
32594c8fcd8SPeter Wemm }
32694c8fcd8SPeter Wemm 
32794c8fcd8SPeter Wemm static int nanowait;
32894c8fcd8SPeter Wemm 
3297fdf2c85SPaul Saab int
3307fdf2c85SPaul Saab kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
3315b870b7bSPeter Wemm {
3325704ba6aSPoul-Henning Kamp 	struct timespec ts, ts2, ts3;
33333841826SPoul-Henning Kamp 	struct timeval tv;
33433841826SPoul-Henning Kamp 	int error;
3355b870b7bSPeter Wemm 
3367d7fb492SBruce Evans 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
337708e7684SPeter Wemm 		return (EINVAL);
338d254af07SMatthew Dillon 	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
3397d7fb492SBruce Evans 		return (0);
340c21410e1SPoul-Henning Kamp 	getnanouptime(&ts);
34100af9731SPoul-Henning Kamp 	timespecadd(&ts, rqt);
34233841826SPoul-Henning Kamp 	TIMESPEC_TO_TIMEVAL(&tv, rqt);
34333841826SPoul-Henning Kamp 	for (;;) {
34433841826SPoul-Henning Kamp 		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
34533841826SPoul-Henning Kamp 		    tvtohz(&tv));
346c21410e1SPoul-Henning Kamp 		getnanouptime(&ts2);
34733841826SPoul-Henning Kamp 		if (error != EWOULDBLOCK) {
34833841826SPoul-Henning Kamp 			if (error == ERESTART)
34994c8fcd8SPeter Wemm 				error = EINTR;
35033841826SPoul-Henning Kamp 			if (rmt != NULL) {
35133841826SPoul-Henning Kamp 				timespecsub(&ts, &ts2);
35233841826SPoul-Henning Kamp 				if (ts.tv_sec < 0)
35333841826SPoul-Henning Kamp 					timespecclear(&ts);
35400af9731SPoul-Henning Kamp 				*rmt = ts;
35500af9731SPoul-Henning Kamp 			}
35633841826SPoul-Henning Kamp 			return (error);
35733841826SPoul-Henning Kamp 		}
35833841826SPoul-Henning Kamp 		if (timespeccmp(&ts2, &ts, >=))
35933841826SPoul-Henning Kamp 			return (0);
3605704ba6aSPoul-Henning Kamp 		ts3 = ts;
3615704ba6aSPoul-Henning Kamp 		timespecsub(&ts3, &ts2);
3625704ba6aSPoul-Henning Kamp 		TIMESPEC_TO_TIMEVAL(&tv, &ts3);
36333841826SPoul-Henning Kamp 	}
3645b870b7bSPeter Wemm }
36594c8fcd8SPeter Wemm 
3665b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
3675b870b7bSPeter Wemm struct nanosleep_args {
3685b870b7bSPeter Wemm 	struct	timespec *rqtp;
3695b870b7bSPeter Wemm 	struct	timespec *rmtp;
3705b870b7bSPeter Wemm };
3715b870b7bSPeter Wemm #endif
3725b870b7bSPeter Wemm 
373fb99ab88SMatthew Dillon /*
374fb99ab88SMatthew Dillon  * MPSAFE
375fb99ab88SMatthew Dillon  */
3765b870b7bSPeter Wemm /* ARGSUSED */
3775b870b7bSPeter Wemm int
37891afe087SPoul-Henning Kamp nanosleep(struct thread *td, struct nanosleep_args *uap)
3795b870b7bSPeter Wemm {
3805b870b7bSPeter Wemm 	struct timespec rmt, rqt;
381fb99ab88SMatthew Dillon 	int error;
3825b870b7bSPeter Wemm 
383d1e405c5SAlfred Perlstein 	error = copyin(uap->rqtp, &rqt, sizeof(rqt));
3845b870b7bSPeter Wemm 	if (error)
3855b870b7bSPeter Wemm 		return (error);
386fb99ab88SMatthew Dillon 
38731f3e2adSAlfred Perlstein 	if (uap->rmtp &&
38831f3e2adSAlfred Perlstein 	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
38931f3e2adSAlfred Perlstein 			return (EFAULT);
3907fdf2c85SPaul Saab 	error = kern_nanosleep(td, &rqt, &rmt);
391d1e405c5SAlfred Perlstein 	if (error && uap->rmtp) {
392fb99ab88SMatthew Dillon 		int error2;
393fb99ab88SMatthew Dillon 
394d1e405c5SAlfred Perlstein 		error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
39531f3e2adSAlfred Perlstein 		if (error2)
396fb99ab88SMatthew Dillon 			error = error2;
397708e7684SPeter Wemm 	}
398708e7684SPeter Wemm 	return (error);
39994c8fcd8SPeter Wemm }
40094c8fcd8SPeter Wemm 
4015b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
402df8bae1dSRodney W. Grimes struct gettimeofday_args {
403df8bae1dSRodney W. Grimes 	struct	timeval *tp;
404df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
405df8bae1dSRodney W. Grimes };
406d2d3e875SBruce Evans #endif
407fb99ab88SMatthew Dillon /*
408fb99ab88SMatthew Dillon  * MPSAFE
409fb99ab88SMatthew Dillon  */
410df8bae1dSRodney W. Grimes /* ARGSUSED */
41126f9a767SRodney W. Grimes int
41291afe087SPoul-Henning Kamp gettimeofday(struct thread *td, struct gettimeofday_args *uap)
413df8bae1dSRodney W. Grimes {
414df8bae1dSRodney W. Grimes 	struct timeval atv;
415411c25edSTim J. Robbins 	struct timezone rtz;
416df8bae1dSRodney W. Grimes 	int error = 0;
417df8bae1dSRodney W. Grimes 
418df8bae1dSRodney W. Grimes 	if (uap->tp) {
419df8bae1dSRodney W. Grimes 		microtime(&atv);
42001609114SAlfred Perlstein 		error = copyout(&atv, uap->tp, sizeof (atv));
421df8bae1dSRodney W. Grimes 	}
42221dcdb38SPoul-Henning Kamp 	if (error == 0 && uap->tzp != NULL) {
42391f1c2b3SPoul-Henning Kamp 		rtz.tz_minuteswest = tz_minuteswest;
42491f1c2b3SPoul-Henning Kamp 		rtz.tz_dsttime = tz_dsttime;
425411c25edSTim J. Robbins 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
42621dcdb38SPoul-Henning Kamp 	}
427df8bae1dSRodney W. Grimes 	return (error);
428df8bae1dSRodney W. Grimes }
429df8bae1dSRodney W. Grimes 
430d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
431df8bae1dSRodney W. Grimes struct settimeofday_args {
432df8bae1dSRodney W. Grimes 	struct	timeval *tv;
433df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
434df8bae1dSRodney W. Grimes };
435d2d3e875SBruce Evans #endif
436fb99ab88SMatthew Dillon /*
437fb99ab88SMatthew Dillon  * MPSAFE
438fb99ab88SMatthew Dillon  */
439df8bae1dSRodney W. Grimes /* ARGSUSED */
44026f9a767SRodney W. Grimes int
44191afe087SPoul-Henning Kamp settimeofday(struct thread *td, struct settimeofday_args *uap)
442df8bae1dSRodney W. Grimes {
443b88ec951SJohn Baldwin 	struct timeval atv, *tvp;
444b88ec951SJohn Baldwin 	struct timezone atz, *tzp;
445b88ec951SJohn Baldwin 	int error;
446b88ec951SJohn Baldwin 
447b88ec951SJohn Baldwin 	if (uap->tv) {
448b88ec951SJohn Baldwin 		error = copyin(uap->tv, &atv, sizeof(atv));
449b88ec951SJohn Baldwin 		if (error)
450b88ec951SJohn Baldwin 			return (error);
451b88ec951SJohn Baldwin 		tvp = &atv;
452b88ec951SJohn Baldwin 	} else
453b88ec951SJohn Baldwin 		tvp = NULL;
454b88ec951SJohn Baldwin 	if (uap->tzp) {
455b88ec951SJohn Baldwin 		error = copyin(uap->tzp, &atz, sizeof(atz));
456b88ec951SJohn Baldwin 		if (error)
457b88ec951SJohn Baldwin 			return (error);
458b88ec951SJohn Baldwin 		tzp = &atz;
459b88ec951SJohn Baldwin 	} else
460b88ec951SJohn Baldwin 		tzp = NULL;
461b88ec951SJohn Baldwin 	return (kern_settimeofday(td, tvp, tzp));
462b88ec951SJohn Baldwin }
463b88ec951SJohn Baldwin 
464b88ec951SJohn Baldwin int
465b88ec951SJohn Baldwin kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
466b88ec951SJohn Baldwin {
467b88ec951SJohn Baldwin 	int error;
468fb99ab88SMatthew Dillon 
4694b8d5f2dSRobert Watson #ifdef MAC
4704b8d5f2dSRobert Watson 	error = mac_check_system_settime(td->td_ucred);
4714b8d5f2dSRobert Watson 	if (error)
4724b8d5f2dSRobert Watson 		return (error);
4734b8d5f2dSRobert Watson #endif
474b88ec951SJohn Baldwin 	error = suser(td);
475b88ec951SJohn Baldwin 	if (error)
4767edfb592SJohn Baldwin 		return (error);
477df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
478b88ec951SJohn Baldwin 	if (tv) {
479b88ec951SJohn Baldwin 		if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
4807edfb592SJohn Baldwin 			return (EINVAL);
481b88ec951SJohn Baldwin 		error = settime(td, tv);
482708e7684SPeter Wemm 	}
483b88ec951SJohn Baldwin 	if (tzp && error == 0) {
484b88ec951SJohn Baldwin 		tz_minuteswest = tzp->tz_minuteswest;
485b88ec951SJohn Baldwin 		tz_dsttime = tzp->tz_dsttime;
486b88ec951SJohn Baldwin 	}
4877edfb592SJohn Baldwin 	return (error);
488b88ec951SJohn Baldwin }
4897edfb592SJohn Baldwin 
490df8bae1dSRodney W. Grimes /*
491df8bae1dSRodney W. Grimes  * Get value of an interval timer.  The process virtual and
492df8bae1dSRodney W. Grimes  * profiling virtual time timers are kept in the p_stats area, since
493df8bae1dSRodney W. Grimes  * they can be swapped out.  These are kept internally in the
494df8bae1dSRodney W. Grimes  * way they are specified externally: in time until they expire.
495df8bae1dSRodney W. Grimes  *
496df8bae1dSRodney W. Grimes  * The real time interval timer is kept in the process table slot
497df8bae1dSRodney W. Grimes  * for the process, and its value (it_value) is kept as an
498df8bae1dSRodney W. Grimes  * absolute time rather than as a delta, so that it is easy to keep
499df8bae1dSRodney W. Grimes  * periodic real-time signals from drifting.
500df8bae1dSRodney W. Grimes  *
501df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
502df8bae1dSRodney W. Grimes  * kern_clock.c.  The real time timer is processed by a timeout
503df8bae1dSRodney W. Grimes  * routine, called from the softclock() routine.  Since a callout
504df8bae1dSRodney W. Grimes  * may be delayed in real time due to interrupt processing in the system,
505df8bae1dSRodney W. Grimes  * it is possible for the real time timeout routine (realitexpire, given below),
506df8bae1dSRodney W. Grimes  * to be delayed in real time past when it is supposed to occur.  It
507df8bae1dSRodney W. Grimes  * does not suffice, therefore, to reload the real timer .it_value from the
508df8bae1dSRodney W. Grimes  * real time timers .it_interval.  Rather, we compute the next time in
509df8bae1dSRodney W. Grimes  * absolute time the timer should go off.
510df8bae1dSRodney W. Grimes  */
511d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
512df8bae1dSRodney W. Grimes struct getitimer_args {
513df8bae1dSRodney W. Grimes 	u_int	which;
514df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
515df8bae1dSRodney W. Grimes };
516d2d3e875SBruce Evans #endif
517fb99ab88SMatthew Dillon /*
518fb99ab88SMatthew Dillon  * MPSAFE
519fb99ab88SMatthew Dillon  */
52026f9a767SRodney W. Grimes int
52191afe087SPoul-Henning Kamp getitimer(struct thread *td, struct getitimer_args *uap)
522df8bae1dSRodney W. Grimes {
523df8bae1dSRodney W. Grimes 	struct itimerval aitv;
524c90110d6SJohn Baldwin 	int error;
525df8bae1dSRodney W. Grimes 
526cfa0efe7SMaxim Sobolev 	error = kern_getitimer(td, uap->which, &aitv);
527cfa0efe7SMaxim Sobolev 	if (error != 0)
528cfa0efe7SMaxim Sobolev 		return (error);
529cfa0efe7SMaxim Sobolev 	return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
530cfa0efe7SMaxim Sobolev }
531cfa0efe7SMaxim Sobolev 
532cfa0efe7SMaxim Sobolev int
533cfa0efe7SMaxim Sobolev kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
534cfa0efe7SMaxim Sobolev {
535cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
536cfa0efe7SMaxim Sobolev 	struct timeval ctv;
537cfa0efe7SMaxim Sobolev 
538cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
539df8bae1dSRodney W. Grimes 		return (EINVAL);
540fb99ab88SMatthew Dillon 
541cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
542df8bae1dSRodney W. Grimes 		/*
543ee002b68SBruce Evans 		 * Convert from absolute to relative time in .it_value
544df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
545df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
546df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
547df8bae1dSRodney W. Grimes 		 */
54896d7f8efSTim J. Robbins 		PROC_LOCK(p);
549cfa0efe7SMaxim Sobolev 		*aitv = p->p_realtimer;
55096d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
551cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
552c21410e1SPoul-Henning Kamp 			getmicrouptime(&ctv);
553cfa0efe7SMaxim Sobolev 			if (timevalcmp(&aitv->it_value, &ctv, <))
554cfa0efe7SMaxim Sobolev 				timevalclear(&aitv->it_value);
555df8bae1dSRodney W. Grimes 			else
556cfa0efe7SMaxim Sobolev 				timevalsub(&aitv->it_value, &ctv);
557227ee8a1SPoul-Henning Kamp 		}
558fb99ab88SMatthew Dillon 	} else {
55996d7f8efSTim J. Robbins 		mtx_lock_spin(&sched_lock);
560cfa0efe7SMaxim Sobolev 		*aitv = p->p_stats->p_timer[which];
56196d7f8efSTim J. Robbins 		mtx_unlock_spin(&sched_lock);
562fb99ab88SMatthew Dillon 	}
563cfa0efe7SMaxim Sobolev 	return (0);
564df8bae1dSRodney W. Grimes }
565df8bae1dSRodney W. Grimes 
566d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
567df8bae1dSRodney W. Grimes struct setitimer_args {
568df8bae1dSRodney W. Grimes 	u_int	which;
569df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
570df8bae1dSRodney W. Grimes };
571d2d3e875SBruce Evans #endif
572cfa0efe7SMaxim Sobolev 
573fb99ab88SMatthew Dillon /*
574fb99ab88SMatthew Dillon  * MPSAFE
575fb99ab88SMatthew Dillon  */
57626f9a767SRodney W. Grimes int
57791afe087SPoul-Henning Kamp setitimer(struct thread *td, struct setitimer_args *uap)
578df8bae1dSRodney W. Grimes {
579cfa0efe7SMaxim Sobolev 	struct itimerval aitv, oitv;
580c90110d6SJohn Baldwin 	int error;
58196d7f8efSTim J. Robbins 
58296d7f8efSTim J. Robbins 	if (uap->itv == NULL) {
58396d7f8efSTim J. Robbins 		uap->itv = uap->oitv;
58496d7f8efSTim J. Robbins 		return (getitimer(td, (struct getitimer_args *)uap));
58596d7f8efSTim J. Robbins 	}
586df8bae1dSRodney W. Grimes 
58796d7f8efSTim J. Robbins 	if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
588df8bae1dSRodney W. Grimes 		return (error);
589cfa0efe7SMaxim Sobolev 	error = kern_setitimer(td, uap->which, &aitv, &oitv);
590cfa0efe7SMaxim Sobolev 	if (error != 0 || uap->oitv == NULL)
591cfa0efe7SMaxim Sobolev 		return (error);
592cfa0efe7SMaxim Sobolev 	return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
593cfa0efe7SMaxim Sobolev }
594cfa0efe7SMaxim Sobolev 
595cfa0efe7SMaxim Sobolev int
596c90110d6SJohn Baldwin kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
597c90110d6SJohn Baldwin     struct itimerval *oitv)
598cfa0efe7SMaxim Sobolev {
599cfa0efe7SMaxim Sobolev 	struct proc *p = td->td_proc;
600cfa0efe7SMaxim Sobolev 	struct timeval ctv;
601cfa0efe7SMaxim Sobolev 
6025e85ac17SJohn Baldwin 	if (aitv == NULL)
6035e85ac17SJohn Baldwin 		return (kern_getitimer(td, which, oitv));
6045e85ac17SJohn Baldwin 
605cfa0efe7SMaxim Sobolev 	if (which > ITIMER_PROF)
60696d7f8efSTim J. Robbins 		return (EINVAL);
607cfa0efe7SMaxim Sobolev 	if (itimerfix(&aitv->it_value))
608cfa0efe7SMaxim Sobolev 		return (EINVAL);
609cfa0efe7SMaxim Sobolev 	if (!timevalisset(&aitv->it_value))
610cfa0efe7SMaxim Sobolev 		timevalclear(&aitv->it_interval);
611cfa0efe7SMaxim Sobolev 	else if (itimerfix(&aitv->it_interval))
61296d7f8efSTim J. Robbins 		return (EINVAL);
61396d7f8efSTim J. Robbins 
614cfa0efe7SMaxim Sobolev 	if (which == ITIMER_REAL) {
61596d7f8efSTim J. Robbins 		PROC_LOCK(p);
6164cf41af3SPoul-Henning Kamp 		if (timevalisset(&p->p_realtimer.it_value))
6174f559836SJake Burkholder 			callout_stop(&p->p_itcallout);
61825b4d3a8SJohn Baldwin 		getmicrouptime(&ctv);
619cfa0efe7SMaxim Sobolev 		if (timevalisset(&aitv->it_value)) {
620cfa0efe7SMaxim Sobolev 			callout_reset(&p->p_itcallout, tvtohz(&aitv->it_value),
6214f559836SJake Burkholder 			    realitexpire, p);
622cfa0efe7SMaxim Sobolev 			timevaladd(&aitv->it_value, &ctv);
62325b4d3a8SJohn Baldwin 		}
624cfa0efe7SMaxim Sobolev 		*oitv = p->p_realtimer;
625cfa0efe7SMaxim Sobolev 		p->p_realtimer = *aitv;
62696d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
627cfa0efe7SMaxim Sobolev 		if (timevalisset(&oitv->it_value)) {
628cfa0efe7SMaxim Sobolev 			if (timevalcmp(&oitv->it_value, &ctv, <))
629cfa0efe7SMaxim Sobolev 				timevalclear(&oitv->it_value);
63096d7f8efSTim J. Robbins 			else
631cfa0efe7SMaxim Sobolev 				timevalsub(&oitv->it_value, &ctv);
632fb99ab88SMatthew Dillon 		}
63396d7f8efSTim J. Robbins 	} else {
63496d7f8efSTim J. Robbins 		mtx_lock_spin(&sched_lock);
635cfa0efe7SMaxim Sobolev 		*oitv = p->p_stats->p_timer[which];
636cfa0efe7SMaxim Sobolev 		p->p_stats->p_timer[which] = *aitv;
63796d7f8efSTim J. Robbins 		mtx_unlock_spin(&sched_lock);
63896d7f8efSTim J. Robbins 	}
63996d7f8efSTim J. Robbins 	return (0);
640df8bae1dSRodney W. Grimes }
641df8bae1dSRodney W. Grimes 
642df8bae1dSRodney W. Grimes /*
643df8bae1dSRodney W. Grimes  * Real interval timer expired:
644df8bae1dSRodney W. Grimes  * send process whose timer expired an alarm signal.
645df8bae1dSRodney W. Grimes  * If time is not set up to reload, then just return.
646df8bae1dSRodney W. Grimes  * Else compute next time timer should go off which is > current time.
647df8bae1dSRodney W. Grimes  * This is where delay in processing this timeout causes multiple
648df8bae1dSRodney W. Grimes  * SIGALRM calls to be compressed into one.
649c8b47828SBruce Evans  * tvtohz() always adds 1 to allow for the time until the next clock
6509207f00aSBruce Evans  * interrupt being strictly less than 1 clock tick, but we don't want
6519207f00aSBruce Evans  * that here since we want to appear to be in sync with the clock
6529207f00aSBruce Evans  * interrupt even when we're delayed.
653df8bae1dSRodney W. Grimes  */
654df8bae1dSRodney W. Grimes void
65591afe087SPoul-Henning Kamp realitexpire(void *arg)
656df8bae1dSRodney W. Grimes {
65791afe087SPoul-Henning Kamp 	struct proc *p;
658bfe6c9faSPoul-Henning Kamp 	struct timeval ctv, ntv;
659df8bae1dSRodney W. Grimes 
660df8bae1dSRodney W. Grimes 	p = (struct proc *)arg;
66137824023SJohn Baldwin 	PROC_LOCK(p);
662df8bae1dSRodney W. Grimes 	psignal(p, SIGALRM);
6634cf41af3SPoul-Henning Kamp 	if (!timevalisset(&p->p_realtimer.it_interval)) {
6644cf41af3SPoul-Henning Kamp 		timevalclear(&p->p_realtimer.it_value);
6655499ea01SJohn Baldwin 		if (p->p_flag & P_WEXIT)
6665499ea01SJohn Baldwin 			wakeup(&p->p_itcallout);
66737824023SJohn Baldwin 		PROC_UNLOCK(p);
668df8bae1dSRodney W. Grimes 		return;
669df8bae1dSRodney W. Grimes 	}
670df8bae1dSRodney W. Grimes 	for (;;) {
671df8bae1dSRodney W. Grimes 		timevaladd(&p->p_realtimer.it_value,
672df8bae1dSRodney W. Grimes 		    &p->p_realtimer.it_interval);
673c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
6744cf41af3SPoul-Henning Kamp 		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
675bfe6c9faSPoul-Henning Kamp 			ntv = p->p_realtimer.it_value;
676bfe6c9faSPoul-Henning Kamp 			timevalsub(&ntv, &ctv);
6774f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
6784f559836SJake Burkholder 			    realitexpire, p);
67937824023SJohn Baldwin 			PROC_UNLOCK(p);
680df8bae1dSRodney W. Grimes 			return;
681df8bae1dSRodney W. Grimes 		}
682df8bae1dSRodney W. Grimes 	}
68337824023SJohn Baldwin 	/*NOTREACHED*/
684df8bae1dSRodney W. Grimes }
685df8bae1dSRodney W. Grimes 
686df8bae1dSRodney W. Grimes /*
687df8bae1dSRodney W. Grimes  * Check that a proposed value to load into the .it_value or
688df8bae1dSRodney W. Grimes  * .it_interval part of an interval timer is acceptable, and
689df8bae1dSRodney W. Grimes  * fix it to have at least minimal value (i.e. if it is less
690df8bae1dSRodney W. Grimes  * than the resolution of the clock, round it up.)
691df8bae1dSRodney W. Grimes  */
69226f9a767SRodney W. Grimes int
69391afe087SPoul-Henning Kamp itimerfix(struct timeval *tv)
694df8bae1dSRodney W. Grimes {
695df8bae1dSRodney W. Grimes 
69686857b36SDavid Xu 	if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
697df8bae1dSRodney W. Grimes 		return (EINVAL);
698df8bae1dSRodney W. Grimes 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
699df8bae1dSRodney W. Grimes 		tv->tv_usec = tick;
700df8bae1dSRodney W. Grimes 	return (0);
701df8bae1dSRodney W. Grimes }
702df8bae1dSRodney W. Grimes 
703df8bae1dSRodney W. Grimes /*
704df8bae1dSRodney W. Grimes  * Decrement an interval timer by a specified number
705df8bae1dSRodney W. Grimes  * of microseconds, which must be less than a second,
706df8bae1dSRodney W. Grimes  * i.e. < 1000000.  If the timer expires, then reload
707df8bae1dSRodney W. Grimes  * it.  In this case, carry over (usec - old value) to
708df8bae1dSRodney W. Grimes  * reduce the value reloaded into the timer so that
709df8bae1dSRodney W. Grimes  * the timer does not drift.  This routine assumes
710df8bae1dSRodney W. Grimes  * that it is called in a context where the timers
711df8bae1dSRodney W. Grimes  * on which it is operating cannot change in value.
712df8bae1dSRodney W. Grimes  */
71326f9a767SRodney W. Grimes int
71491afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec)
715df8bae1dSRodney W. Grimes {
716df8bae1dSRodney W. Grimes 
717df8bae1dSRodney W. Grimes 	if (itp->it_value.tv_usec < usec) {
718df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_sec == 0) {
719df8bae1dSRodney W. Grimes 			/* expired, and already in next interval */
720df8bae1dSRodney W. Grimes 			usec -= itp->it_value.tv_usec;
721df8bae1dSRodney W. Grimes 			goto expire;
722df8bae1dSRodney W. Grimes 		}
723df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec += 1000000;
724df8bae1dSRodney W. Grimes 		itp->it_value.tv_sec--;
725df8bae1dSRodney W. Grimes 	}
726df8bae1dSRodney W. Grimes 	itp->it_value.tv_usec -= usec;
727df8bae1dSRodney W. Grimes 	usec = 0;
7284cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_value))
729df8bae1dSRodney W. Grimes 		return (1);
730df8bae1dSRodney W. Grimes 	/* expired, exactly at end of interval */
731df8bae1dSRodney W. Grimes expire:
7324cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_interval)) {
733df8bae1dSRodney W. Grimes 		itp->it_value = itp->it_interval;
734df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec -= usec;
735df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_usec < 0) {
736df8bae1dSRodney W. Grimes 			itp->it_value.tv_usec += 1000000;
737df8bae1dSRodney W. Grimes 			itp->it_value.tv_sec--;
738df8bae1dSRodney W. Grimes 		}
739df8bae1dSRodney W. Grimes 	} else
740df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
741df8bae1dSRodney W. Grimes 	return (0);
742df8bae1dSRodney W. Grimes }
743df8bae1dSRodney W. Grimes 
744df8bae1dSRodney W. Grimes /*
745df8bae1dSRodney W. Grimes  * Add and subtract routines for timevals.
746df8bae1dSRodney W. Grimes  * N.B.: subtract routine doesn't deal with
747df8bae1dSRodney W. Grimes  * results which are before the beginning,
748df8bae1dSRodney W. Grimes  * it just gets very confused in this case.
749df8bae1dSRodney W. Grimes  * Caveat emptor.
750df8bae1dSRodney W. Grimes  */
75126f9a767SRodney W. Grimes void
7526ff7636eSAlfred Perlstein timevaladd(struct timeval *t1, const struct timeval *t2)
753df8bae1dSRodney W. Grimes {
754df8bae1dSRodney W. Grimes 
755df8bae1dSRodney W. Grimes 	t1->tv_sec += t2->tv_sec;
756df8bae1dSRodney W. Grimes 	t1->tv_usec += t2->tv_usec;
757df8bae1dSRodney W. Grimes 	timevalfix(t1);
758df8bae1dSRodney W. Grimes }
759df8bae1dSRodney W. Grimes 
76026f9a767SRodney W. Grimes void
7616ff7636eSAlfred Perlstein timevalsub(struct timeval *t1, const struct timeval *t2)
762df8bae1dSRodney W. Grimes {
763df8bae1dSRodney W. Grimes 
764df8bae1dSRodney W. Grimes 	t1->tv_sec -= t2->tv_sec;
765df8bae1dSRodney W. Grimes 	t1->tv_usec -= t2->tv_usec;
766df8bae1dSRodney W. Grimes 	timevalfix(t1);
767df8bae1dSRodney W. Grimes }
768df8bae1dSRodney W. Grimes 
76987b6de2bSPoul-Henning Kamp static void
77091afe087SPoul-Henning Kamp timevalfix(struct timeval *t1)
771df8bae1dSRodney W. Grimes {
772df8bae1dSRodney W. Grimes 
773df8bae1dSRodney W. Grimes 	if (t1->tv_usec < 0) {
774df8bae1dSRodney W. Grimes 		t1->tv_sec--;
775df8bae1dSRodney W. Grimes 		t1->tv_usec += 1000000;
776df8bae1dSRodney W. Grimes 	}
777df8bae1dSRodney W. Grimes 	if (t1->tv_usec >= 1000000) {
778df8bae1dSRodney W. Grimes 		t1->tv_sec++;
779df8bae1dSRodney W. Grimes 		t1->tv_usec -= 1000000;
780df8bae1dSRodney W. Grimes 	}
781df8bae1dSRodney W. Grimes }
78291974ce1SSam Leffler 
78391974ce1SSam Leffler /*
784addea9d4SSam Leffler  * ratecheck(): simple time-based rate-limit checking.
78591974ce1SSam Leffler  */
78691974ce1SSam Leffler int
78791974ce1SSam Leffler ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
78891974ce1SSam Leffler {
78991974ce1SSam Leffler 	struct timeval tv, delta;
79091974ce1SSam Leffler 	int rv = 0;
79191974ce1SSam Leffler 
792addea9d4SSam Leffler 	getmicrouptime(&tv);		/* NB: 10ms precision */
793addea9d4SSam Leffler 	delta = tv;
794addea9d4SSam Leffler 	timevalsub(&delta, lasttime);
79591974ce1SSam Leffler 
79691974ce1SSam Leffler 	/*
79791974ce1SSam Leffler 	 * check for 0,0 is so that the message will be seen at least once,
79891974ce1SSam Leffler 	 * even if interval is huge.
79991974ce1SSam Leffler 	 */
80091974ce1SSam Leffler 	if (timevalcmp(&delta, mininterval, >=) ||
80191974ce1SSam Leffler 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
80291974ce1SSam Leffler 		*lasttime = tv;
80391974ce1SSam Leffler 		rv = 1;
80491974ce1SSam Leffler 	}
80591974ce1SSam Leffler 
80691974ce1SSam Leffler 	return (rv);
80791974ce1SSam Leffler }
80891974ce1SSam Leffler 
80991974ce1SSam Leffler /*
81091974ce1SSam Leffler  * ppsratecheck(): packets (or events) per second limitation.
811addea9d4SSam Leffler  *
812addea9d4SSam Leffler  * Return 0 if the limit is to be enforced (e.g. the caller
813addea9d4SSam Leffler  * should drop a packet because of the rate limitation).
814addea9d4SSam Leffler  *
815893bec80SSam Leffler  * maxpps of 0 always causes zero to be returned.  maxpps of -1
816893bec80SSam Leffler  * always causes 1 to be returned; this effectively defeats rate
817893bec80SSam Leffler  * limiting.
818893bec80SSam Leffler  *
819addea9d4SSam Leffler  * Note that we maintain the struct timeval for compatibility
820addea9d4SSam Leffler  * with other bsd systems.  We reuse the storage and just monitor
821addea9d4SSam Leffler  * clock ticks for minimal overhead.
82291974ce1SSam Leffler  */
82391974ce1SSam Leffler int
82491974ce1SSam Leffler ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
82591974ce1SSam Leffler {
826addea9d4SSam Leffler 	int now;
82791974ce1SSam Leffler 
82891974ce1SSam Leffler 	/*
829addea9d4SSam Leffler 	 * Reset the last time and counter if this is the first call
830addea9d4SSam Leffler 	 * or more than a second has passed since the last update of
831addea9d4SSam Leffler 	 * lasttime.
83291974ce1SSam Leffler 	 */
833addea9d4SSam Leffler 	now = ticks;
834addea9d4SSam Leffler 	if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
835addea9d4SSam Leffler 		lasttime->tv_sec = now;
836addea9d4SSam Leffler 		*curpps = 1;
837893bec80SSam Leffler 		return (maxpps != 0);
838addea9d4SSam Leffler 	} else {
839addea9d4SSam Leffler 		(*curpps)++;		/* NB: ignore potential overflow */
840addea9d4SSam Leffler 		return (maxpps < 0 || *curpps < maxpps);
841addea9d4SSam Leffler 	}
84291974ce1SSam Leffler }
84386857b36SDavid Xu 
84486857b36SDavid Xu static void
84586857b36SDavid Xu itimer_start(void)
84686857b36SDavid Xu {
84786857b36SDavid Xu 	struct kclock rt_clock = {
84886857b36SDavid Xu 		.timer_create  = realtimer_create,
84986857b36SDavid Xu 		.timer_delete  = realtimer_delete,
85086857b36SDavid Xu 		.timer_settime = realtimer_settime,
85186857b36SDavid Xu 		.timer_gettime = realtimer_gettime,
85286857b36SDavid Xu 		.event_hook     = realtimer_event_hook
85386857b36SDavid Xu 	};
85486857b36SDavid Xu 
85586857b36SDavid Xu 	itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
85686857b36SDavid Xu 		NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
85786857b36SDavid Xu 	register_posix_clock(CLOCK_REALTIME,  &rt_clock);
85886857b36SDavid Xu 	register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
85986857b36SDavid Xu }
86086857b36SDavid Xu 
86186857b36SDavid Xu int
86286857b36SDavid Xu register_posix_clock(int clockid, struct kclock *clk)
86386857b36SDavid Xu {
86486857b36SDavid Xu 	if ((unsigned)clockid >= MAX_CLOCKS) {
86586857b36SDavid Xu 		printf("%s: invalid clockid\n", __func__);
86686857b36SDavid Xu 		return (0);
86786857b36SDavid Xu 	}
86886857b36SDavid Xu 	posix_clocks[clockid] = *clk;
86986857b36SDavid Xu 	return (1);
87086857b36SDavid Xu }
87186857b36SDavid Xu 
87286857b36SDavid Xu static int
87386857b36SDavid Xu itimer_init(void *mem, int size, int flags)
87486857b36SDavid Xu {
87586857b36SDavid Xu 	struct itimer *it;
87686857b36SDavid Xu 
87786857b36SDavid Xu 	it = (struct itimer *)mem;
87886857b36SDavid Xu 	mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
87986857b36SDavid Xu 	return (0);
88086857b36SDavid Xu }
88186857b36SDavid Xu 
88286857b36SDavid Xu static void
88386857b36SDavid Xu itimer_fini(void *mem, int size)
88486857b36SDavid Xu {
88586857b36SDavid Xu 	struct itimer *it;
88686857b36SDavid Xu 
88786857b36SDavid Xu 	it = (struct itimer *)mem;
88886857b36SDavid Xu 	mtx_destroy(&it->it_mtx);
88986857b36SDavid Xu }
89086857b36SDavid Xu 
89186857b36SDavid Xu static void
89286857b36SDavid Xu itimer_enter(struct itimer *it)
89386857b36SDavid Xu {
89486857b36SDavid Xu 
89586857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
89686857b36SDavid Xu 	it->it_usecount++;
89786857b36SDavid Xu }
89886857b36SDavid Xu 
89986857b36SDavid Xu static void
90086857b36SDavid Xu itimer_leave(struct itimer *it)
90186857b36SDavid Xu {
90286857b36SDavid Xu 
90386857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
90486857b36SDavid Xu 	KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
90586857b36SDavid Xu 
90686857b36SDavid Xu 	if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
90786857b36SDavid Xu 		wakeup(it);
90886857b36SDavid Xu }
90986857b36SDavid Xu 
91086857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
91186857b36SDavid Xu struct timer_create_args {
91286857b36SDavid Xu 	clockid_t clock_id;
91386857b36SDavid Xu 	struct sigevent * evp;
91486857b36SDavid Xu 	timer_t * timerid;
91586857b36SDavid Xu };
91686857b36SDavid Xu #endif
91786857b36SDavid Xu 
91886857b36SDavid Xu int
91986857b36SDavid Xu timer_create(struct thread *td, struct timer_create_args *uap)
92086857b36SDavid Xu {
92186857b36SDavid Xu 	struct sigevent *evp1, ev;
92286857b36SDavid Xu 	timer_t id;
92386857b36SDavid Xu 	int error;
92486857b36SDavid Xu 
92586857b36SDavid Xu 	if (uap->evp != NULL) {
92686857b36SDavid Xu 		error = copyin(uap->evp, &ev, sizeof(ev));
92786857b36SDavid Xu 		if (error != 0)
92886857b36SDavid Xu 			return (error);
92986857b36SDavid Xu 		evp1 = &ev;
93086857b36SDavid Xu 	} else
93186857b36SDavid Xu 		evp1 = NULL;
93286857b36SDavid Xu 
93386857b36SDavid Xu 	error = kern_timer_create(td, uap->clock_id, evp1, &id, -1);
93486857b36SDavid Xu 
93586857b36SDavid Xu 	if (error == 0) {
93686857b36SDavid Xu 		error = copyout(&id, uap->timerid, sizeof(timer_t));
93786857b36SDavid Xu 		if (error != 0)
93886857b36SDavid Xu 			kern_timer_delete(td, id);
93986857b36SDavid Xu 	}
94086857b36SDavid Xu 	return (error);
94186857b36SDavid Xu }
94286857b36SDavid Xu 
94386857b36SDavid Xu static int
94486857b36SDavid Xu kern_timer_create(struct thread *td, clockid_t clock_id,
94586857b36SDavid Xu 	struct sigevent *evp, timer_t *timerid, timer_t preset_id)
94686857b36SDavid Xu {
94786857b36SDavid Xu 	struct proc *p = td->td_proc;
94886857b36SDavid Xu 	struct itimer *it;
94986857b36SDavid Xu 	int id;
95086857b36SDavid Xu 	int error;
95186857b36SDavid Xu 
95286857b36SDavid Xu 	if (clock_id < 0 || clock_id >= MAX_CLOCKS)
95386857b36SDavid Xu 		return (EINVAL);
95486857b36SDavid Xu 
95586857b36SDavid Xu 	if (posix_clocks[clock_id].timer_create == NULL)
95686857b36SDavid Xu 		return (EINVAL);
95786857b36SDavid Xu 
95886857b36SDavid Xu 	if (evp != NULL) {
95986857b36SDavid Xu 		if (evp->sigev_notify != SIGEV_NONE &&
96086857b36SDavid Xu 		    evp->sigev_notify != SIGEV_SIGNAL)
96186857b36SDavid Xu 			return (EINVAL);
96286857b36SDavid Xu 		if (evp->sigev_notify == SIGEV_SIGNAL &&
96386857b36SDavid Xu 			!_SIG_VALID(evp->sigev_signo))
96486857b36SDavid Xu 			return (EINVAL);
96586857b36SDavid Xu 	}
96686857b36SDavid Xu 
96786857b36SDavid Xu 	if (p->p_itimers.its_timers == NULL)
96886857b36SDavid Xu 		itimers_alloc(p);
96986857b36SDavid Xu 
97086857b36SDavid Xu 	it = uma_zalloc(itimer_zone, M_WAITOK);
97186857b36SDavid Xu 	it->it_flags = 0;
97286857b36SDavid Xu 	it->it_usecount = 0;
97386857b36SDavid Xu 	it->it_active = 0;
97486857b36SDavid Xu 	timevalclear(&it->it_time.it_value);
97586857b36SDavid Xu 	timevalclear(&it->it_time.it_interval);
97686857b36SDavid Xu 	it->it_overrun = 0;
97786857b36SDavid Xu 	it->it_overrun_last = 0;
97886857b36SDavid Xu 	it->it_clockid = clock_id;
97986857b36SDavid Xu 	it->it_timerid = -1;
98086857b36SDavid Xu 	it->it_proc = p;
98186857b36SDavid Xu 	ksiginfo_init(&it->it_ksi);
98286857b36SDavid Xu 	it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
98386857b36SDavid Xu 	error = CLOCK_CALL(clock_id, timer_create, (it));
98486857b36SDavid Xu 	if (error != 0)
98586857b36SDavid Xu 		goto out;
98686857b36SDavid Xu 
98786857b36SDavid Xu 	PROC_LOCK(p);
98886857b36SDavid Xu 	if (preset_id != -1) {
98986857b36SDavid Xu 		KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
99086857b36SDavid Xu 		id = preset_id;
99186857b36SDavid Xu 		if (p->p_itimers.its_timers[id] != NULL) {
99286857b36SDavid Xu 			PROC_UNLOCK(p);
99386857b36SDavid Xu 			error = 0;
99486857b36SDavid Xu 			goto out;
99586857b36SDavid Xu 		}
99686857b36SDavid Xu 	} else {
99786857b36SDavid Xu 		/*
99886857b36SDavid Xu 		 * Find a free timer slot, skipping those reserved
99986857b36SDavid Xu 		 * for setitimer().
100086857b36SDavid Xu 		 */
100186857b36SDavid Xu 		for (id = 3; id < TIMER_MAX; id++)
100286857b36SDavid Xu 			if (p->p_itimers.its_timers[id] == NULL)
100386857b36SDavid Xu 				break;
100486857b36SDavid Xu 		if (id == TIMER_MAX) {
100586857b36SDavid Xu 			PROC_UNLOCK(p);
100686857b36SDavid Xu 			error = EAGAIN;
100786857b36SDavid Xu 			goto out;
100886857b36SDavid Xu 		}
100986857b36SDavid Xu 	}
101086857b36SDavid Xu 	it->it_timerid = id;
101186857b36SDavid Xu 	p->p_itimers.its_timers[id] = it;
101286857b36SDavid Xu 	if (evp != NULL)
101386857b36SDavid Xu 		it->it_sigev = *evp;
101486857b36SDavid Xu 	else {
101586857b36SDavid Xu 		it->it_sigev.sigev_notify = SIGEV_SIGNAL;
101686857b36SDavid Xu 		switch (clock_id) {
101786857b36SDavid Xu 		default:
101886857b36SDavid Xu 		case CLOCK_REALTIME:
101986857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGALRM;
102086857b36SDavid Xu 			break;
102186857b36SDavid Xu 		case CLOCK_VIRTUAL:
102286857b36SDavid Xu  			it->it_sigev.sigev_signo = SIGVTALRM;
102386857b36SDavid Xu 			break;
102486857b36SDavid Xu 		case CLOCK_PROF:
102586857b36SDavid Xu 			it->it_sigev.sigev_signo = SIGPROF;
102686857b36SDavid Xu 			break;
102786857b36SDavid Xu 		}
102886857b36SDavid Xu 		it->it_sigev.sigev_value.sigval_int = id;
102986857b36SDavid Xu 	}
103086857b36SDavid Xu 
103186857b36SDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL) {
103286857b36SDavid Xu 		it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
103386857b36SDavid Xu 		it->it_ksi.ksi_code = SI_TIMER;
103486857b36SDavid Xu 		it->it_ksi.ksi_value = it->it_sigev.sigev_value;
103586857b36SDavid Xu 		it->it_ksi.ksi_timerid = id;
103686857b36SDavid Xu 	}
103786857b36SDavid Xu 	PROC_UNLOCK(p);
103886857b36SDavid Xu 	*timerid = id;
103986857b36SDavid Xu 	return (0);
104086857b36SDavid Xu 
104186857b36SDavid Xu out:
104286857b36SDavid Xu 	ITIMER_LOCK(it);
104386857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
104486857b36SDavid Xu 	ITIMER_UNLOCK(it);
104586857b36SDavid Xu 	uma_zfree(itimer_zone, it);
104686857b36SDavid Xu 	return (error);
104786857b36SDavid Xu }
104886857b36SDavid Xu 
104986857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
105086857b36SDavid Xu struct timer_delete_args {
105186857b36SDavid Xu 	timer_t timerid;
105286857b36SDavid Xu };
105386857b36SDavid Xu #endif
105486857b36SDavid Xu 
105586857b36SDavid Xu int
105686857b36SDavid Xu timer_delete(struct thread *td, struct timer_delete_args *uap)
105786857b36SDavid Xu {
105886857b36SDavid Xu 	return (kern_timer_delete(td, uap->timerid));
105986857b36SDavid Xu }
106086857b36SDavid Xu 
106186857b36SDavid Xu static struct itimer *
106286857b36SDavid Xu itimer_find(struct proc *p, timer_t timerid, int include_deleting)
106386857b36SDavid Xu {
106486857b36SDavid Xu 	struct itimer *it;
106586857b36SDavid Xu 
106686857b36SDavid Xu 	PROC_LOCK_ASSERT(p, MA_OWNED);
106786857b36SDavid Xu 	if ((p->p_itimers.its_timers == NULL) || (timerid >= TIMER_MAX) ||
106886857b36SDavid Xu 	    (it = p->p_itimers.its_timers[timerid]) == NULL) {
106986857b36SDavid Xu 		return (NULL);
107086857b36SDavid Xu 	}
107186857b36SDavid Xu 	ITIMER_LOCK(it);
107286857b36SDavid Xu 	if (!include_deleting && (it->it_flags & ITF_DELETING) != 0) {
107386857b36SDavid Xu 		ITIMER_UNLOCK(it);
107486857b36SDavid Xu 		it = NULL;
107586857b36SDavid Xu 	}
107686857b36SDavid Xu 	return (it);
107786857b36SDavid Xu }
107886857b36SDavid Xu 
107986857b36SDavid Xu static int
108086857b36SDavid Xu kern_timer_delete(struct thread *td, timer_t timerid)
108186857b36SDavid Xu {
108286857b36SDavid Xu 	struct proc *p = td->td_proc;
108386857b36SDavid Xu 	struct itimer *it;
108486857b36SDavid Xu 
108586857b36SDavid Xu 	PROC_LOCK(p);
108686857b36SDavid Xu 	it = itimer_find(p, timerid, 0);
108786857b36SDavid Xu 	if (it == NULL) {
108886857b36SDavid Xu 		PROC_UNLOCK(p);
108986857b36SDavid Xu 		return (EINVAL);
109086857b36SDavid Xu 	}
109186857b36SDavid Xu 	PROC_UNLOCK(p);
109286857b36SDavid Xu 
109386857b36SDavid Xu 	it->it_flags |= ITF_DELETING;
109486857b36SDavid Xu 	while (it->it_usecount > 0) {
109586857b36SDavid Xu 		it->it_flags |= ITF_WANTED;
109686857b36SDavid Xu 		msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
109786857b36SDavid Xu 	}
109886857b36SDavid Xu 	it->it_flags &= ~ITF_WANTED;
109986857b36SDavid Xu 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
110086857b36SDavid Xu 	ITIMER_UNLOCK(it);
110186857b36SDavid Xu 
110286857b36SDavid Xu 	PROC_LOCK(p);
110386857b36SDavid Xu 	if (KSI_ONQ(&it->it_ksi))
110486857b36SDavid Xu 		sigqueue_take(&it->it_ksi);
110586857b36SDavid Xu 	p->p_itimers.its_timers[timerid] = NULL;
110686857b36SDavid Xu 	PROC_UNLOCK(p);
110786857b36SDavid Xu 	uma_zfree(itimer_zone, it);
110886857b36SDavid Xu 	return (0);
110986857b36SDavid Xu }
111086857b36SDavid Xu 
111186857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
111286857b36SDavid Xu struct timer_settime_args {
111386857b36SDavid Xu 	timer_t timerid;
111486857b36SDavid Xu 	int flags;
111586857b36SDavid Xu 	const struct itimerspec * value;
111686857b36SDavid Xu 	struct itimerspec * ovalue;
111786857b36SDavid Xu };
111886857b36SDavid Xu #endif
111986857b36SDavid Xu 
112086857b36SDavid Xu int
112186857b36SDavid Xu timer_settime(struct thread *td, struct timer_settime_args *uap)
112286857b36SDavid Xu {
112386857b36SDavid Xu 	struct proc *p = td->td_proc;
112486857b36SDavid Xu 	struct itimer *it;
112586857b36SDavid Xu 	struct itimerspec val, oval, *ovalp;
112686857b36SDavid Xu 	int error;
112786857b36SDavid Xu 
112886857b36SDavid Xu 	error = copyin(uap->value, &val, sizeof(val));
112986857b36SDavid Xu 	if (error != 0)
113086857b36SDavid Xu 		return (error);
113186857b36SDavid Xu 
113286857b36SDavid Xu 	if (uap->ovalue != NULL)
113386857b36SDavid Xu 		ovalp = &oval;
113486857b36SDavid Xu 	else
113586857b36SDavid Xu 		ovalp = NULL;
113686857b36SDavid Xu 
113786857b36SDavid Xu 	PROC_LOCK(p);
113886857b36SDavid Xu 	if (uap->timerid < 3 ||
113986857b36SDavid Xu 	    (it = itimer_find(p, uap->timerid, 0)) == NULL) {
114086857b36SDavid Xu 		PROC_UNLOCK(p);
114186857b36SDavid Xu 		error = EINVAL;
114286857b36SDavid Xu 	} else {
114386857b36SDavid Xu 		PROC_UNLOCK(p);
114486857b36SDavid Xu 		itimer_enter(it);
114586857b36SDavid Xu 		error = CLOCK_CALL(it->it_clockid, timer_settime,
114686857b36SDavid Xu 				(it, uap->flags, &val, ovalp));
114786857b36SDavid Xu 		itimer_leave(it);
114886857b36SDavid Xu 		ITIMER_UNLOCK(it);
114986857b36SDavid Xu 	}
115086857b36SDavid Xu 	if (error == 0 && uap->ovalue != NULL)
115186857b36SDavid Xu 		error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
115286857b36SDavid Xu 	return (error);
115386857b36SDavid Xu }
115486857b36SDavid Xu 
115586857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
115686857b36SDavid Xu struct timer_gettime_args {
115786857b36SDavid Xu 	timer_t timerid;
115886857b36SDavid Xu 	struct itimerspec * value;
115986857b36SDavid Xu };
116086857b36SDavid Xu #endif
116186857b36SDavid Xu 
116286857b36SDavid Xu int
116386857b36SDavid Xu timer_gettime(struct thread *td, struct timer_gettime_args *uap)
116486857b36SDavid Xu {
116586857b36SDavid Xu 	struct proc *p = td->td_proc;
116686857b36SDavid Xu 	struct itimer *it;
116786857b36SDavid Xu 	struct itimerspec val;
116886857b36SDavid Xu 	int error;
116986857b36SDavid Xu 
117086857b36SDavid Xu 	PROC_LOCK(p);
117186857b36SDavid Xu 	if (uap->timerid < 3 ||
117286857b36SDavid Xu 	   (it = itimer_find(p, uap->timerid, 0)) == NULL) {
117386857b36SDavid Xu 		PROC_UNLOCK(p);
117486857b36SDavid Xu 		error = EINVAL;
117586857b36SDavid Xu 	} else {
117686857b36SDavid Xu 		PROC_UNLOCK(p);
117786857b36SDavid Xu 		itimer_enter(it);
117886857b36SDavid Xu 		error = CLOCK_CALL(it->it_clockid, timer_gettime,
117986857b36SDavid Xu 				(it, &val));
118086857b36SDavid Xu 		itimer_leave(it);
118186857b36SDavid Xu 		ITIMER_UNLOCK(it);
118286857b36SDavid Xu 	}
118386857b36SDavid Xu 	if (error == 0)
118486857b36SDavid Xu 		error = copyout(&val, uap->value, sizeof(val));
118586857b36SDavid Xu 	return (error);
118686857b36SDavid Xu }
118786857b36SDavid Xu 
118886857b36SDavid Xu #ifndef _SYS_SYSPROTO_H_
118986857b36SDavid Xu struct timer_getoverrun_args {
119086857b36SDavid Xu 	timer_t timerid;
119186857b36SDavid Xu };
119286857b36SDavid Xu #endif
119386857b36SDavid Xu 
119486857b36SDavid Xu int
119586857b36SDavid Xu timer_getoverrun(struct thread *td, struct timer_getoverrun_args *uap)
119686857b36SDavid Xu {
119786857b36SDavid Xu 	struct proc *p = td->td_proc;
119886857b36SDavid Xu 	struct itimer *it;
119986857b36SDavid Xu 	int error ;
120086857b36SDavid Xu 
120186857b36SDavid Xu 	PROC_LOCK(p);
120286857b36SDavid Xu 	if (uap->timerid < 3 ||
120386857b36SDavid Xu 	    (it = itimer_find(p, uap->timerid, 0)) == NULL) {
120486857b36SDavid Xu 		PROC_UNLOCK(p);
120586857b36SDavid Xu 		error = EINVAL;
120686857b36SDavid Xu 	} else {
120786857b36SDavid Xu 		PROC_UNLOCK(p);
120886857b36SDavid Xu 		td->td_retval[0] = it->it_overrun_last;
120986857b36SDavid Xu 		ITIMER_UNLOCK(it);
121086857b36SDavid Xu 		error = 0;
121186857b36SDavid Xu 	}
121286857b36SDavid Xu 	return (error);
121386857b36SDavid Xu }
121486857b36SDavid Xu 
121586857b36SDavid Xu static int
121686857b36SDavid Xu realtimer_create(struct itimer *it)
121786857b36SDavid Xu {
121886857b36SDavid Xu 	callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
121986857b36SDavid Xu 	return (0);
122086857b36SDavid Xu }
122186857b36SDavid Xu 
122286857b36SDavid Xu static int
122386857b36SDavid Xu realtimer_delete(struct itimer *it)
122486857b36SDavid Xu {
122586857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
122686857b36SDavid Xu 	callout_stop(&it->it_callout);
122786857b36SDavid Xu 	return (0);
122886857b36SDavid Xu }
122986857b36SDavid Xu 
123086857b36SDavid Xu static int
123186857b36SDavid Xu realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
123286857b36SDavid Xu {
123386857b36SDavid Xu 	struct timespec ts;
123486857b36SDavid Xu 
123586857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
123686857b36SDavid Xu 
123786857b36SDavid Xu 	TIMEVAL_TO_TIMESPEC(&it->it_time.it_value, &ovalue->it_value);
123886857b36SDavid Xu 	TIMEVAL_TO_TIMESPEC(&it->it_time.it_interval, &ovalue->it_interval);
123986857b36SDavid Xu 	if (it->it_clockid == CLOCK_REALTIME)
124086857b36SDavid Xu 		getnanotime(&ts);
124186857b36SDavid Xu 	else /* CLOCK_MONOTONIC */
124286857b36SDavid Xu 		getnanouptime(&ts);
124386857b36SDavid Xu 	if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
124486857b36SDavid Xu 		timespecsub(&ovalue->it_value, &ts);
124586857b36SDavid Xu 		if (ovalue->it_value.tv_sec < 0 ||
124686857b36SDavid Xu 		    (ovalue->it_value.tv_sec == 0 &&
124786857b36SDavid Xu 		     ovalue->it_value.tv_nsec == 0)) {
124886857b36SDavid Xu 			ovalue->it_value.tv_sec  = 0;
124986857b36SDavid Xu 			ovalue->it_value.tv_nsec = 1;
125086857b36SDavid Xu 		}
125186857b36SDavid Xu 	}
125286857b36SDavid Xu 	return (0);
125386857b36SDavid Xu }
125486857b36SDavid Xu 
125586857b36SDavid Xu static int
125686857b36SDavid Xu realtimer_settime(struct itimer *it, int flags,
125786857b36SDavid Xu 	struct itimerspec *value, struct itimerspec *ovalue)
125886857b36SDavid Xu {
125986857b36SDavid Xu 	struct timeval tv, tv2;
126086857b36SDavid Xu 	struct itimerval val;
126186857b36SDavid Xu 
126286857b36SDavid Xu 	mtx_assert(&it->it_mtx, MA_OWNED);
126386857b36SDavid Xu 
126486857b36SDavid Xu 	TIMESPEC_TO_TIMEVAL(&val.it_value, &value->it_value);
126586857b36SDavid Xu 	TIMESPEC_TO_TIMEVAL(&val.it_interval, &value->it_interval);
126686857b36SDavid Xu 
126786857b36SDavid Xu 	if (itimerfix(&val.it_value))
126886857b36SDavid Xu 		return (EINVAL);
126986857b36SDavid Xu 
127086857b36SDavid Xu 	if (timevalisset(&val.it_value)) {
127186857b36SDavid Xu 		if (itimerfix(&val.it_interval))
127286857b36SDavid Xu 			return (EINVAL);
127386857b36SDavid Xu 	} else {
127486857b36SDavid Xu 		timevalclear(&val.it_interval);
127586857b36SDavid Xu 	}
127686857b36SDavid Xu 
127786857b36SDavid Xu 	if (ovalue != NULL)
127886857b36SDavid Xu 		realtimer_gettime(it, ovalue);
127986857b36SDavid Xu 
128086857b36SDavid Xu 	it->it_time = val;
128186857b36SDavid Xu 	if (timevalisset(&val.it_value)) {
128286857b36SDavid Xu 		realtimer_clocktime(it->it_clockid, &tv);
128386857b36SDavid Xu 		tv2 = val.it_value;
128486857b36SDavid Xu 		if ((flags & TIMER_ABSTIME) == 0) {
128586857b36SDavid Xu 			/* Convert to absolute time. */
128686857b36SDavid Xu 			timevaladd(&it->it_time.it_value, &tv);
128786857b36SDavid Xu 		} else {
128886857b36SDavid Xu 			timevalsub(&tv2, &tv);
128986857b36SDavid Xu 			/*
129086857b36SDavid Xu 			 * We don't care if tv2 is negative, tztohz will
129186857b36SDavid Xu 			 * fix it.
129286857b36SDavid Xu 			 */
129386857b36SDavid Xu 		}
129486857b36SDavid Xu 		callout_reset(&it->it_callout, tvtohz(&tv2),
129586857b36SDavid Xu 			realtimer_expire, it);
129686857b36SDavid Xu 	} else {
129786857b36SDavid Xu 		callout_stop(&it->it_callout);
129886857b36SDavid Xu 	}
129986857b36SDavid Xu 
130086857b36SDavid Xu 	return (0);
130186857b36SDavid Xu }
130286857b36SDavid Xu 
130386857b36SDavid Xu static void
130486857b36SDavid Xu realtimer_clocktime(clockid_t id, struct timeval *tv)
130586857b36SDavid Xu {
130686857b36SDavid Xu 	if (id == CLOCK_REALTIME)
130786857b36SDavid Xu 		getmicrotime(tv);
130886857b36SDavid Xu 	else	/* CLOCK_MONOTONIC */
130986857b36SDavid Xu 		getmicrouptime(tv);
131086857b36SDavid Xu }
131186857b36SDavid Xu 
131286857b36SDavid Xu static void
131386857b36SDavid Xu realtimer_event_hook(struct proc *p, clockid_t clock_id, int event)
131486857b36SDavid Xu {
131586857b36SDavid Xu 	struct itimers *its;
131686857b36SDavid Xu 	struct itimer  *it;
131786857b36SDavid Xu 	int i;
131886857b36SDavid Xu 
131986857b36SDavid Xu 	/*
132086857b36SDavid Xu 	 * Timer 0 (ITIMER_REAL) is XSI interval timer, according to POSIX
132186857b36SDavid Xu 	 * specification, it should be inherited by new process image.
132286857b36SDavid Xu 	 */
132386857b36SDavid Xu 	if (event == ITIMER_EV_EXEC)
132486857b36SDavid Xu 		i = 1;
132586857b36SDavid Xu 	else
132686857b36SDavid Xu 		i = 0;
132786857b36SDavid Xu 	its = &p->p_itimers;
132886857b36SDavid Xu 	for (; i < TIMER_MAX; i++) {
132986857b36SDavid Xu 		if ((it = its->its_timers[i]) != NULL &&
133086857b36SDavid Xu 		     it->it_clockid == clock_id) {
133186857b36SDavid Xu 			ITIMER_LOCK(it);
133286857b36SDavid Xu 			callout_stop(&it->it_callout);
133386857b36SDavid Xu 			ITIMER_UNLOCK(it);
133486857b36SDavid Xu 		}
133586857b36SDavid Xu 	}
133686857b36SDavid Xu }
133786857b36SDavid Xu 
133886857b36SDavid Xu /* Timeout callback for realtime timer */
133986857b36SDavid Xu static void
134086857b36SDavid Xu realtimer_expire(void *arg)
134186857b36SDavid Xu {
134286857b36SDavid Xu 	struct timeval tv, tv2;
134386857b36SDavid Xu 	struct itimer *it;
134486857b36SDavid Xu 	struct proc *p;
134586857b36SDavid Xu 
134686857b36SDavid Xu 	it = (struct itimer *)arg;
134786857b36SDavid Xu 	p = it->it_proc;
134886857b36SDavid Xu 
134986857b36SDavid Xu 	realtimer_clocktime(it->it_clockid, &tv);
135086857b36SDavid Xu 	/* Only fire if time is reached. */
135186857b36SDavid Xu 	if (timevalcmp(&it->it_time.it_value, &tv, <=)) {
135286857b36SDavid Xu 		if (timevalisset(&it->it_time.it_interval)) {
135386857b36SDavid Xu 			timevaladd(&it->it_time.it_value,
135486857b36SDavid Xu 				   &it->it_time.it_interval);
135586857b36SDavid Xu 			while (timevalcmp(&it->it_time.it_value, &tv, <=)) {
135686857b36SDavid Xu 				it->it_overrun++;
135786857b36SDavid Xu 				timevaladd(&it->it_time.it_value,
135886857b36SDavid Xu 					&it->it_time.it_interval);
135986857b36SDavid Xu 			}
136086857b36SDavid Xu 		} else {
136186857b36SDavid Xu 			/* single shot timer ? */
136286857b36SDavid Xu 			timevalclear(&it->it_time.it_value);
136386857b36SDavid Xu 		}
136486857b36SDavid Xu 		if (timevalisset(&it->it_time.it_value)) {
136586857b36SDavid Xu 			tv2 = it->it_time.it_value;
136686857b36SDavid Xu 			timevalsub(&tv2, &tv);
136786857b36SDavid Xu 			callout_reset(&it->it_callout, tvtohz(&tv2),
136886857b36SDavid Xu 				 realtimer_expire, it);
136986857b36SDavid Xu 		}
137086857b36SDavid Xu 		ITIMER_UNLOCK(it);
137186857b36SDavid Xu 		itimer_fire(it);
137286857b36SDavid Xu 		ITIMER_LOCK(it);
137386857b36SDavid Xu 	} else if (timevalisset(&it->it_time.it_value)) {
137486857b36SDavid Xu 		tv2 = it->it_time.it_value;
137586857b36SDavid Xu 		timevalsub(&tv2, &tv);
137686857b36SDavid Xu 		callout_reset(&it->it_callout, tvtohz(&tv2), realtimer_expire,
137786857b36SDavid Xu  			it);
137886857b36SDavid Xu 	}
137986857b36SDavid Xu }
138086857b36SDavid Xu 
138186857b36SDavid Xu void
138286857b36SDavid Xu itimer_fire(struct itimer *it)
138386857b36SDavid Xu {
138486857b36SDavid Xu 	struct proc *p = it->it_proc;
138586857b36SDavid Xu 
138686857b36SDavid Xu 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL) {
138786857b36SDavid Xu 		PROC_LOCK(p);
138886857b36SDavid Xu 		ITIMER_LOCK(it);
138986857b36SDavid Xu 		if (KSI_ONQ(&it->it_ksi)) {
139086857b36SDavid Xu 			it->it_overrun++;
139186857b36SDavid Xu 			it->it_ksi.ksi_overrun = it->it_overrun;
139286857b36SDavid Xu 			it->it_overrun_last = it->it_overrun;
139386857b36SDavid Xu 		} else {
139486857b36SDavid Xu 			it->it_ksi.ksi_overrun = it->it_overrun;
139586857b36SDavid Xu 			it->it_overrun_last = it->it_overrun;
139686857b36SDavid Xu 			it->it_overrun = 0;
139786857b36SDavid Xu 			psignal_info(p, &it->it_ksi);
139886857b36SDavid Xu 		}
139986857b36SDavid Xu 		PROC_UNLOCK(p);
140086857b36SDavid Xu 		ITIMER_UNLOCK(it);
140186857b36SDavid Xu 	}
140286857b36SDavid Xu }
140386857b36SDavid Xu 
140486857b36SDavid Xu static void
140586857b36SDavid Xu itimers_alloc(struct proc *p)
140686857b36SDavid Xu {
140786857b36SDavid Xu 	struct itimer **itp;
140886857b36SDavid Xu 
140986857b36SDavid Xu 	itp = malloc(sizeof(struct itimer *) * TIMER_MAX, M_SUBPROC,
141086857b36SDavid Xu 		M_WAITOK | M_ZERO);
141186857b36SDavid Xu 	PROC_LOCK(p);
141286857b36SDavid Xu 	if (p->p_itimers.its_timers == NULL) {
141386857b36SDavid Xu 		p->p_itimers.its_timers = itp;
141486857b36SDavid Xu 		PROC_UNLOCK(p);
141586857b36SDavid Xu 	} else {
141686857b36SDavid Xu 		PROC_UNLOCK(p);
141786857b36SDavid Xu 		free(itp, M_SUBPROC);
141886857b36SDavid Xu 	}
141986857b36SDavid Xu }
142086857b36SDavid Xu 
142186857b36SDavid Xu void
142286857b36SDavid Xu itimers_init(struct itimers *its)
142386857b36SDavid Xu {
142486857b36SDavid Xu 	LIST_INIT(&its->its_virtual);
142586857b36SDavid Xu 	LIST_INIT(&its->its_prof);
142686857b36SDavid Xu 	TAILQ_INIT(&its->its_worklist);
142786857b36SDavid Xu 	its->its_timers = NULL;
142886857b36SDavid Xu }
142986857b36SDavid Xu 
143086857b36SDavid Xu /* Clean up timers when some process events are being triggered. */
143186857b36SDavid Xu void
143286857b36SDavid Xu itimers_event_hook(struct proc *p, int event)
143386857b36SDavid Xu {
143486857b36SDavid Xu 	struct itimers *its;
143586857b36SDavid Xu 	struct itimer *it;
143686857b36SDavid Xu 	int i;
143786857b36SDavid Xu 
143886857b36SDavid Xu 	if (p->p_itimers.its_timers != NULL) {
143986857b36SDavid Xu 		its = &p->p_itimers;
144086857b36SDavid Xu 		for (i = 0; i < MAX_CLOCKS; ++i) {
144186857b36SDavid Xu 			if (posix_clocks[i].event_hook != NULL)
144286857b36SDavid Xu 				CLOCK_CALL(i, event_hook, (p, i, event));
144386857b36SDavid Xu 		}
144486857b36SDavid Xu 		/*
144586857b36SDavid Xu 		 * According to susv3, XSI interval timers should be inherited
144686857b36SDavid Xu 		 * by new image.
144786857b36SDavid Xu 		 */
144886857b36SDavid Xu 		if (event == ITIMER_EV_EXEC)
144986857b36SDavid Xu 			i = 3;
145086857b36SDavid Xu 		else if (event == ITIMER_EV_EXIT)
145186857b36SDavid Xu 			i = 0;
145286857b36SDavid Xu 		else
145386857b36SDavid Xu 			panic("unhandled event");
145486857b36SDavid Xu 		for (; i < TIMER_MAX; ++i) {
145586857b36SDavid Xu 			if ((it = its->its_timers[i]) != NULL) {
145686857b36SDavid Xu 				PROC_LOCK(p);
145786857b36SDavid Xu 				if (KSI_ONQ(&it->it_ksi))
145886857b36SDavid Xu 					sigqueue_take(&it->it_ksi);
145986857b36SDavid Xu 				PROC_UNLOCK(p);
146086857b36SDavid Xu 				uma_zfree(itimer_zone, its->its_timers[i]);
146186857b36SDavid Xu 				its->its_timers[i] = NULL;
146286857b36SDavid Xu 			}
146386857b36SDavid Xu 		}
146486857b36SDavid Xu 		if (its->its_timers[0] == NULL &&
146586857b36SDavid Xu 		    its->its_timers[1] == NULL &&
146686857b36SDavid Xu 		    its->its_timers[2] == NULL) {
146786857b36SDavid Xu 			free(its->its_timers, M_SUBPROC);
146886857b36SDavid Xu 			p->p_itimers.its_timers = NULL;
146986857b36SDavid Xu 		}
147086857b36SDavid Xu 	}
147186857b36SDavid Xu }
1472