xref: /freebsd/sys/kern/kern_time.c (revision fcae3aa61f4ad12abec2048d769d6211e3fa91c2)
1df8bae1dSRodney W. Grimes /*
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  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33df8bae1dSRodney W. Grimes  *	@(#)kern_time.c	8.1 (Berkeley) 6/10/93
34fcae3aa6SNick Sayer  * $Id: kern_time.c,v 1.61 1999/02/25 15:54:05 bde Exp $
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38a5c9bce7SBruce Evans #include <sys/buf.h>
39d2d3e875SBruce Evans #include <sys/sysproto.h>
40df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
41797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
42df8bae1dSRodney W. Grimes #include <sys/kernel.h>
43df8bae1dSRodney W. Grimes #include <sys/systm.h>
4494c8fcd8SPeter Wemm #include <sys/sysent.h>
45df8bae1dSRodney W. Grimes #include <sys/proc.h>
46708e7684SPeter Wemm #include <sys/time.h>
47df8bae1dSRodney W. Grimes #include <sys/vnode.h>
485b870b7bSPeter Wemm #include <vm/vm.h>
495b870b7bSPeter Wemm #include <vm/vm_extern.h>
50df8bae1dSRodney W. Grimes 
51ac7e6123SDavid Greenman struct timezone tz;
52ac7e6123SDavid Greenman 
53df8bae1dSRodney W. Grimes /*
54df8bae1dSRodney W. Grimes  * Time of day and interval timer support.
55df8bae1dSRodney W. Grimes  *
56df8bae1dSRodney W. Grimes  * These routines provide the kernel entry points to get and set
57df8bae1dSRodney W. Grimes  * the time-of-day and per-process interval timers.  Subroutines
58df8bae1dSRodney W. Grimes  * here provide support for adding and subtracting timeval structures
59df8bae1dSRodney W. Grimes  * and decrementing interval timers, optionally reloading the interval
60df8bae1dSRodney W. Grimes  * timers when they expire.
61df8bae1dSRodney W. Grimes  */
62df8bae1dSRodney W. Grimes 
635b870b7bSPeter Wemm static int	nanosleep1 __P((struct proc *p, struct timespec *rqt,
645b870b7bSPeter Wemm 		    struct timespec *rmt));
657d7fb492SBruce Evans static int	settime __P((struct timeval *));
667d7fb492SBruce Evans static void	timevalfix __P((struct timeval *));
67cb226aaaSPoul-Henning Kamp static void	no_lease_updatetime __P((int));
6894c8fcd8SPeter Wemm 
691b09ae77SPoul-Henning Kamp static void
701b09ae77SPoul-Henning Kamp no_lease_updatetime(deltat)
711b09ae77SPoul-Henning Kamp 	int deltat;
721b09ae77SPoul-Henning Kamp {
731b09ae77SPoul-Henning Kamp }
741b09ae77SPoul-Henning Kamp 
751b09ae77SPoul-Henning Kamp void (*lease_updatetime) __P((int))  = no_lease_updatetime;
761b09ae77SPoul-Henning Kamp 
7794c8fcd8SPeter Wemm static int
7894c8fcd8SPeter Wemm settime(tv)
7994c8fcd8SPeter Wemm 	struct timeval *tv;
8094c8fcd8SPeter Wemm {
81fcae3aa6SNick Sayer 	struct timeval delta, tv1, tv2;
82fcae3aa6SNick Sayer 	static struct timeval maxtime;
837ec73f64SPoul-Henning Kamp 	struct timespec ts;
8494c8fcd8SPeter Wemm 	int s;
8594c8fcd8SPeter Wemm 
86708e7684SPeter Wemm 	s = splclock();
879c8fff87SBruce Evans 	microtime(&tv1);
8800af9731SPoul-Henning Kamp 	delta = *tv;
8900af9731SPoul-Henning Kamp 	timevalsub(&delta, &tv1);
9094c8fcd8SPeter Wemm 
9194c8fcd8SPeter Wemm 	/*
929c8fff87SBruce Evans 	 * If the system is secure, we do not allow the time to be
93fcae3aa6SNick Sayer 	 * set to a value earlier than 1 second less than the highest
94fcae3aa6SNick Sayer 	 * time we have yet seen. The worst a miscreant can do in
95fcae3aa6SNick Sayer 	 * this circumstance is "freeze" time. He couldn't go
96fcae3aa6SNick Sayer 	 * back to the past.
9794c8fcd8SPeter Wemm 	 */
98fcae3aa6SNick Sayer 	if (securelevel > 1) {
99fcae3aa6SNick Sayer 		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
100fcae3aa6SNick Sayer 			if ( tv1.tv_sec > maxtime.tv_sec )
101fcae3aa6SNick Sayer 				maxtime=tv1;
102fcae3aa6SNick Sayer 			tv2=*tv;
103fcae3aa6SNick Sayer 			timevalsub( &tv2, &maxtime );
104fcae3aa6SNick Sayer 			if ( tv2.tv_sec < -1 ) {
105fcae3aa6SNick Sayer 				tv.tv_sec=maxtime.tv_sec-1;
106fcae3aa6SNick Sayer 				printf("Time adjustment clamped to -1 second\n");
107fcae3aa6SNick Sayer 			}
108fcae3aa6SNick Sayer 		}
109fcae3aa6SNick Sayer 		else {
110fcae3aa6SNick Sayer 			/* XXX
111fcae3aa6SNick Sayer 			 * We have to figure out how to be secure
112fcae3aa6SNick Sayer 			 * in this case. Allowing arbitrary
113fcae3aa6SNick Sayer 			 * positive increases allows a miscreant
114fcae3aa6SNick Sayer 			 * to simply wrap time around the end
115fcae3aa6SNick Sayer 			 * of time.
116fcae3aa6SNick Sayer 			 */
117fcae3aa6SNick Sayer 		}
1189c8fff87SBruce Evans 	}
1199c8fff87SBruce Evans 
1207ec73f64SPoul-Henning Kamp 	ts.tv_sec = tv->tv_sec;
1217ec73f64SPoul-Henning Kamp 	ts.tv_nsec = tv->tv_usec * 1000;
1227ec73f64SPoul-Henning Kamp 	set_timecounter(&ts);
12394c8fcd8SPeter Wemm 	(void) splsoftclock();
12494c8fcd8SPeter Wemm 	lease_updatetime(delta.tv_sec);
12594c8fcd8SPeter Wemm 	splx(s);
12694c8fcd8SPeter Wemm 	resettodr();
12794c8fcd8SPeter Wemm 	return (0);
12894c8fcd8SPeter Wemm }
12994c8fcd8SPeter Wemm 
13094c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
13194c8fcd8SPeter Wemm struct clock_gettime_args {
13294c8fcd8SPeter Wemm 	clockid_t clock_id;
13394c8fcd8SPeter Wemm 	struct	timespec *tp;
13494c8fcd8SPeter Wemm };
13594c8fcd8SPeter Wemm #endif
136708e7684SPeter Wemm 
13794c8fcd8SPeter Wemm /* ARGSUSED */
13894c8fcd8SPeter Wemm int
139cb226aaaSPoul-Henning Kamp clock_gettime(p, uap)
14094c8fcd8SPeter Wemm 	struct proc *p;
14194c8fcd8SPeter Wemm 	struct clock_gettime_args *uap;
14294c8fcd8SPeter Wemm {
14394c8fcd8SPeter Wemm 	struct timespec ats;
14494c8fcd8SPeter Wemm 
145708e7684SPeter Wemm 	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
14694c8fcd8SPeter Wemm 		return (EINVAL);
1477ec73f64SPoul-Henning Kamp 	nanotime(&ats);
148708e7684SPeter Wemm 	return (copyout(&ats, SCARG(uap, tp), sizeof(ats)));
14994c8fcd8SPeter Wemm }
15094c8fcd8SPeter Wemm 
15194c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
15294c8fcd8SPeter Wemm struct clock_settime_args {
15394c8fcd8SPeter Wemm 	clockid_t clock_id;
15494c8fcd8SPeter Wemm 	const struct	timespec *tp;
15594c8fcd8SPeter Wemm };
15694c8fcd8SPeter Wemm #endif
157708e7684SPeter Wemm 
15894c8fcd8SPeter Wemm /* ARGSUSED */
15994c8fcd8SPeter Wemm int
160cb226aaaSPoul-Henning Kamp clock_settime(p, uap)
16194c8fcd8SPeter Wemm 	struct proc *p;
16294c8fcd8SPeter Wemm 	struct clock_settime_args *uap;
16394c8fcd8SPeter Wemm {
16494c8fcd8SPeter Wemm 	struct timeval atv;
16594c8fcd8SPeter Wemm 	struct timespec ats;
16694c8fcd8SPeter Wemm 	int error;
16794c8fcd8SPeter Wemm 
16894c8fcd8SPeter Wemm 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
16994c8fcd8SPeter Wemm 		return (error);
170708e7684SPeter Wemm 	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
17194c8fcd8SPeter Wemm 		return (EINVAL);
17294c8fcd8SPeter Wemm 	if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
17394c8fcd8SPeter Wemm 		return (error);
1743fc9295dSBruce Evans 	if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
175708e7684SPeter Wemm 		return (EINVAL);
176a0502b19SPoul-Henning Kamp 	/* XXX Don't convert nsec->usec and back */
17794c8fcd8SPeter Wemm 	TIMESPEC_TO_TIMEVAL(&atv, &ats);
17894c8fcd8SPeter Wemm 	if ((error = settime(&atv)))
17994c8fcd8SPeter Wemm 		return (error);
180708e7684SPeter Wemm 	return (0);
18194c8fcd8SPeter Wemm }
18294c8fcd8SPeter Wemm 
18394c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
18494c8fcd8SPeter Wemm struct clock_getres_args {
18594c8fcd8SPeter Wemm 	clockid_t clock_id;
18694c8fcd8SPeter Wemm 	struct	timespec *tp;
18794c8fcd8SPeter Wemm };
18894c8fcd8SPeter Wemm #endif
189708e7684SPeter Wemm 
19094c8fcd8SPeter Wemm int
191cb226aaaSPoul-Henning Kamp clock_getres(p, uap)
19294c8fcd8SPeter Wemm 	struct proc *p;
19394c8fcd8SPeter Wemm 	struct clock_getres_args *uap;
19494c8fcd8SPeter Wemm {
19594c8fcd8SPeter Wemm 	struct timespec ts;
196708e7684SPeter Wemm 	int error;
19794c8fcd8SPeter Wemm 
198708e7684SPeter Wemm 	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
19994c8fcd8SPeter Wemm 		return (EINVAL);
200708e7684SPeter Wemm 	error = 0;
20194c8fcd8SPeter Wemm 	if (SCARG(uap, tp)) {
20294c8fcd8SPeter Wemm 		ts.tv_sec = 0;
203a58f0f8eSPoul-Henning Kamp 		ts.tv_nsec = 1000000000 / timecounter->tc_frequency;
20494c8fcd8SPeter Wemm 		error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
20594c8fcd8SPeter Wemm 	}
206708e7684SPeter Wemm 	return (error);
20794c8fcd8SPeter Wemm }
20894c8fcd8SPeter Wemm 
20994c8fcd8SPeter Wemm static int nanowait;
21094c8fcd8SPeter Wemm 
2115b870b7bSPeter Wemm static int
2125b870b7bSPeter Wemm nanosleep1(p, rqt, rmt)
2135b870b7bSPeter Wemm 	struct proc *p;
2145b870b7bSPeter Wemm 	struct timespec *rqt, *rmt;
2155b870b7bSPeter Wemm {
2165704ba6aSPoul-Henning Kamp 	struct timespec ts, ts2, ts3;
21733841826SPoul-Henning Kamp 	struct timeval tv;
21833841826SPoul-Henning Kamp 	int error;
2195b870b7bSPeter Wemm 
2207d7fb492SBruce Evans 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
221708e7684SPeter Wemm 		return (EINVAL);
222d254af07SMatthew Dillon 	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
2237d7fb492SBruce Evans 		return (0);
224c21410e1SPoul-Henning Kamp 	getnanouptime(&ts);
22500af9731SPoul-Henning Kamp 	timespecadd(&ts, rqt);
22633841826SPoul-Henning Kamp 	TIMESPEC_TO_TIMEVAL(&tv, rqt);
22733841826SPoul-Henning Kamp 	for (;;) {
22833841826SPoul-Henning Kamp 		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
22933841826SPoul-Henning Kamp 		    tvtohz(&tv));
230c21410e1SPoul-Henning Kamp 		getnanouptime(&ts2);
23133841826SPoul-Henning Kamp 		if (error != EWOULDBLOCK) {
23233841826SPoul-Henning Kamp 			if (error == ERESTART)
23394c8fcd8SPeter Wemm 				error = EINTR;
23433841826SPoul-Henning Kamp 			if (rmt != NULL) {
23533841826SPoul-Henning Kamp 				timespecsub(&ts, &ts2);
23633841826SPoul-Henning Kamp 				if (ts.tv_sec < 0)
23733841826SPoul-Henning Kamp 					timespecclear(&ts);
23800af9731SPoul-Henning Kamp 				*rmt = ts;
23900af9731SPoul-Henning Kamp 			}
24033841826SPoul-Henning Kamp 			return (error);
24133841826SPoul-Henning Kamp 		}
24233841826SPoul-Henning Kamp 		if (timespeccmp(&ts2, &ts, >=))
24333841826SPoul-Henning Kamp 			return (0);
2445704ba6aSPoul-Henning Kamp 		ts3 = ts;
2455704ba6aSPoul-Henning Kamp 		timespecsub(&ts3, &ts2);
2465704ba6aSPoul-Henning Kamp 		TIMESPEC_TO_TIMEVAL(&tv, &ts3);
24733841826SPoul-Henning Kamp 	}
2485b870b7bSPeter Wemm }
24994c8fcd8SPeter Wemm 
2505b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
2515b870b7bSPeter Wemm struct nanosleep_args {
2525b870b7bSPeter Wemm 	struct	timespec *rqtp;
2535b870b7bSPeter Wemm 	struct	timespec *rmtp;
2545b870b7bSPeter Wemm };
2555b870b7bSPeter Wemm #endif
2565b870b7bSPeter Wemm 
2575b870b7bSPeter Wemm /* ARGSUSED */
2585b870b7bSPeter Wemm int
259cb226aaaSPoul-Henning Kamp nanosleep(p, uap)
2605b870b7bSPeter Wemm 	struct proc *p;
2615b870b7bSPeter Wemm 	struct nanosleep_args *uap;
2625b870b7bSPeter Wemm {
2635b870b7bSPeter Wemm 	struct timespec rmt, rqt;
2645b870b7bSPeter Wemm 	int error, error2;
2655b870b7bSPeter Wemm 
2665b870b7bSPeter Wemm 	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(rqt));
2675b870b7bSPeter Wemm 	if (error)
2685b870b7bSPeter Wemm 		return (error);
269bf5acbf5SPeter Wemm 	if (SCARG(uap, rmtp))
2705b870b7bSPeter Wemm 		if (!useracc((caddr_t)SCARG(uap, rmtp), sizeof(rmt), B_WRITE))
2715b870b7bSPeter Wemm 			return (EFAULT);
2725b870b7bSPeter Wemm 	error = nanosleep1(p, &rqt, &rmt);
273d59fbbf6SPeter Wemm 	if (error && SCARG(uap, rmtp)) {
274708e7684SPeter Wemm 		error2 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
2755b870b7bSPeter Wemm 		if (error2)	/* XXX shouldn't happen, did useracc() above */
276708e7684SPeter Wemm 			return (error2);
277708e7684SPeter Wemm 	}
278708e7684SPeter Wemm 	return (error);
27994c8fcd8SPeter Wemm }
28094c8fcd8SPeter Wemm 
2815b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
282df8bae1dSRodney W. Grimes struct gettimeofday_args {
283df8bae1dSRodney W. Grimes 	struct	timeval *tp;
284df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
285df8bae1dSRodney W. Grimes };
286d2d3e875SBruce Evans #endif
287df8bae1dSRodney W. Grimes /* ARGSUSED */
28826f9a767SRodney W. Grimes int
289cb226aaaSPoul-Henning Kamp gettimeofday(p, uap)
290df8bae1dSRodney W. Grimes 	struct proc *p;
291df8bae1dSRodney W. Grimes 	register struct gettimeofday_args *uap;
292df8bae1dSRodney W. Grimes {
293df8bae1dSRodney W. Grimes 	struct timeval atv;
294df8bae1dSRodney W. Grimes 	int error = 0;
295df8bae1dSRodney W. Grimes 
296df8bae1dSRodney W. Grimes 	if (uap->tp) {
297df8bae1dSRodney W. Grimes 		microtime(&atv);
298bb56ec4aSPoul-Henning Kamp 		if ((error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
299bb56ec4aSPoul-Henning Kamp 		    sizeof (atv))))
300df8bae1dSRodney W. Grimes 			return (error);
301df8bae1dSRodney W. Grimes 	}
302df8bae1dSRodney W. Grimes 	if (uap->tzp)
303df8bae1dSRodney W. Grimes 		error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
304df8bae1dSRodney W. Grimes 		    sizeof (tz));
305df8bae1dSRodney W. Grimes 	return (error);
306df8bae1dSRodney W. Grimes }
307df8bae1dSRodney W. Grimes 
308d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
309df8bae1dSRodney W. Grimes struct settimeofday_args {
310df8bae1dSRodney W. Grimes 	struct	timeval *tv;
311df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
312df8bae1dSRodney W. Grimes };
313d2d3e875SBruce Evans #endif
314df8bae1dSRodney W. Grimes /* ARGSUSED */
31526f9a767SRodney W. Grimes int
316cb226aaaSPoul-Henning Kamp settimeofday(p, uap)
317df8bae1dSRodney W. Grimes 	struct proc *p;
318df8bae1dSRodney W. Grimes 	struct settimeofday_args *uap;
319df8bae1dSRodney W. Grimes {
320708e7684SPeter Wemm 	struct timeval atv;
321df8bae1dSRodney W. Grimes 	struct timezone atz;
322708e7684SPeter Wemm 	int error;
323df8bae1dSRodney W. Grimes 
324bb56ec4aSPoul-Henning Kamp 	if ((error = suser(p->p_ucred, &p->p_acflag)))
325df8bae1dSRodney W. Grimes 		return (error);
326df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
327708e7684SPeter Wemm 	if (uap->tv) {
328708e7684SPeter Wemm 		if ((error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
329708e7684SPeter Wemm 		    sizeof(atv))))
330df8bae1dSRodney W. Grimes 			return (error);
3310808c591SBruce Evans 		if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
3320808c591SBruce Evans 			return (EINVAL);
333708e7684SPeter Wemm 	}
334df8bae1dSRodney W. Grimes 	if (uap->tzp &&
335df8bae1dSRodney W. Grimes 	    (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz))))
336df8bae1dSRodney W. Grimes 		return (error);
33794c8fcd8SPeter Wemm 	if (uap->tv && (error = settime(&atv)))
33894c8fcd8SPeter Wemm 		return (error);
339df8bae1dSRodney W. Grimes 	if (uap->tzp)
340df8bae1dSRodney W. Grimes 		tz = atz;
341df8bae1dSRodney W. Grimes 	return (0);
342df8bae1dSRodney W. Grimes }
343df8bae1dSRodney W. Grimes 
344df8bae1dSRodney W. Grimes int	tickdelta;			/* current clock skew, us. per tick */
345df8bae1dSRodney W. Grimes long	timedelta;			/* unapplied time correction, us. */
34687b6de2bSPoul-Henning Kamp static long	bigadj = 1000000;	/* use 10x skew above bigadj us. */
347df8bae1dSRodney W. Grimes 
348d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
349df8bae1dSRodney W. Grimes struct adjtime_args {
350df8bae1dSRodney W. Grimes 	struct timeval *delta;
351df8bae1dSRodney W. Grimes 	struct timeval *olddelta;
352df8bae1dSRodney W. Grimes };
353d2d3e875SBruce Evans #endif
354df8bae1dSRodney W. Grimes /* ARGSUSED */
35526f9a767SRodney W. Grimes int
356cb226aaaSPoul-Henning Kamp adjtime(p, uap)
357df8bae1dSRodney W. Grimes 	struct proc *p;
358df8bae1dSRodney W. Grimes 	register struct adjtime_args *uap;
359df8bae1dSRodney W. Grimes {
360df8bae1dSRodney W. Grimes 	struct timeval atv;
361df8bae1dSRodney W. Grimes 	register long ndelta, ntickdelta, odelta;
362df8bae1dSRodney W. Grimes 	int s, error;
363df8bae1dSRodney W. Grimes 
364bb56ec4aSPoul-Henning Kamp 	if ((error = suser(p->p_ucred, &p->p_acflag)))
365df8bae1dSRodney W. Grimes 		return (error);
366bb56ec4aSPoul-Henning Kamp 	if ((error =
367bb56ec4aSPoul-Henning Kamp 	    copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof(struct timeval))))
368df8bae1dSRodney W. Grimes 		return (error);
369df8bae1dSRodney W. Grimes 
370df8bae1dSRodney W. Grimes 	/*
371df8bae1dSRodney W. Grimes 	 * Compute the total correction and the rate at which to apply it.
372df8bae1dSRodney W. Grimes 	 * Round the adjustment down to a whole multiple of the per-tick
373df8bae1dSRodney W. Grimes 	 * delta, so that after some number of incremental changes in
374df8bae1dSRodney W. Grimes 	 * hardclock(), tickdelta will become zero, lest the correction
375df8bae1dSRodney W. Grimes 	 * overshoot and start taking us away from the desired final time.
376df8bae1dSRodney W. Grimes 	 */
377df8bae1dSRodney W. Grimes 	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
3780cd97b20SBruce Evans 	if (ndelta > bigadj || ndelta < -bigadj)
379df8bae1dSRodney W. Grimes 		ntickdelta = 10 * tickadj;
380df8bae1dSRodney W. Grimes 	else
381df8bae1dSRodney W. Grimes 		ntickdelta = tickadj;
382df8bae1dSRodney W. Grimes 	if (ndelta % ntickdelta)
383df8bae1dSRodney W. Grimes 		ndelta = ndelta / ntickdelta * ntickdelta;
384df8bae1dSRodney W. Grimes 
385df8bae1dSRodney W. Grimes 	/*
386df8bae1dSRodney W. Grimes 	 * To make hardclock()'s job easier, make the per-tick delta negative
387df8bae1dSRodney W. Grimes 	 * if we want time to run slower; then hardclock can simply compute
388df8bae1dSRodney W. Grimes 	 * tick + tickdelta, and subtract tickdelta from timedelta.
389df8bae1dSRodney W. Grimes 	 */
390df8bae1dSRodney W. Grimes 	if (ndelta < 0)
391df8bae1dSRodney W. Grimes 		ntickdelta = -ntickdelta;
392df8bae1dSRodney W. Grimes 	s = splclock();
393df8bae1dSRodney W. Grimes 	odelta = timedelta;
394df8bae1dSRodney W. Grimes 	timedelta = ndelta;
395df8bae1dSRodney W. Grimes 	tickdelta = ntickdelta;
396df8bae1dSRodney W. Grimes 	splx(s);
397df8bae1dSRodney W. Grimes 
398df8bae1dSRodney W. Grimes 	if (uap->olddelta) {
399df8bae1dSRodney W. Grimes 		atv.tv_sec = odelta / 1000000;
400df8bae1dSRodney W. Grimes 		atv.tv_usec = odelta % 1000000;
401df8bae1dSRodney W. Grimes 		(void) copyout((caddr_t)&atv, (caddr_t)uap->olddelta,
402df8bae1dSRodney W. Grimes 		    sizeof(struct timeval));
403df8bae1dSRodney W. Grimes 	}
404df8bae1dSRodney W. Grimes 	return (0);
405df8bae1dSRodney W. Grimes }
406df8bae1dSRodney W. Grimes 
407df8bae1dSRodney W. Grimes /*
408df8bae1dSRodney W. Grimes  * Get value of an interval timer.  The process virtual and
409df8bae1dSRodney W. Grimes  * profiling virtual time timers are kept in the p_stats area, since
410df8bae1dSRodney W. Grimes  * they can be swapped out.  These are kept internally in the
411df8bae1dSRodney W. Grimes  * way they are specified externally: in time until they expire.
412df8bae1dSRodney W. Grimes  *
413df8bae1dSRodney W. Grimes  * The real time interval timer is kept in the process table slot
414df8bae1dSRodney W. Grimes  * for the process, and its value (it_value) is kept as an
415df8bae1dSRodney W. Grimes  * absolute time rather than as a delta, so that it is easy to keep
416df8bae1dSRodney W. Grimes  * periodic real-time signals from drifting.
417df8bae1dSRodney W. Grimes  *
418df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
419df8bae1dSRodney W. Grimes  * kern_clock.c.  The real time timer is processed by a timeout
420df8bae1dSRodney W. Grimes  * routine, called from the softclock() routine.  Since a callout
421df8bae1dSRodney W. Grimes  * may be delayed in real time due to interrupt processing in the system,
422df8bae1dSRodney W. Grimes  * it is possible for the real time timeout routine (realitexpire, given below),
423df8bae1dSRodney W. Grimes  * to be delayed in real time past when it is supposed to occur.  It
424df8bae1dSRodney W. Grimes  * does not suffice, therefore, to reload the real timer .it_value from the
425df8bae1dSRodney W. Grimes  * real time timers .it_interval.  Rather, we compute the next time in
426df8bae1dSRodney W. Grimes  * absolute time the timer should go off.
427df8bae1dSRodney W. Grimes  */
428d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
429df8bae1dSRodney W. Grimes struct getitimer_args {
430df8bae1dSRodney W. Grimes 	u_int	which;
431df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
432df8bae1dSRodney W. Grimes };
433d2d3e875SBruce Evans #endif
434df8bae1dSRodney W. Grimes /* ARGSUSED */
43526f9a767SRodney W. Grimes int
436cb226aaaSPoul-Henning Kamp getitimer(p, uap)
437df8bae1dSRodney W. Grimes 	struct proc *p;
438df8bae1dSRodney W. Grimes 	register struct getitimer_args *uap;
439df8bae1dSRodney W. Grimes {
440227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
441df8bae1dSRodney W. Grimes 	struct itimerval aitv;
442df8bae1dSRodney W. Grimes 	int s;
443df8bae1dSRodney W. Grimes 
444df8bae1dSRodney W. Grimes 	if (uap->which > ITIMER_PROF)
445df8bae1dSRodney W. Grimes 		return (EINVAL);
446227ee8a1SPoul-Henning Kamp 	s = splclock(); /* XXX still needed ? */
447df8bae1dSRodney W. Grimes 	if (uap->which == ITIMER_REAL) {
448df8bae1dSRodney W. Grimes 		/*
449ee002b68SBruce Evans 		 * Convert from absolute to relative time in .it_value
450df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
451df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
452df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
453df8bae1dSRodney W. Grimes 		 */
454df8bae1dSRodney W. Grimes 		aitv = p->p_realtimer;
4554cf41af3SPoul-Henning Kamp 		if (timevalisset(&aitv.it_value)) {
456c21410e1SPoul-Henning Kamp 			getmicrouptime(&ctv);
4574cf41af3SPoul-Henning Kamp 			if (timevalcmp(&aitv.it_value, &ctv, <))
4584cf41af3SPoul-Henning Kamp 				timevalclear(&aitv.it_value);
459df8bae1dSRodney W. Grimes 			else
460227ee8a1SPoul-Henning Kamp 				timevalsub(&aitv.it_value, &ctv);
461227ee8a1SPoul-Henning Kamp 		}
462df8bae1dSRodney W. Grimes 	} else
463df8bae1dSRodney W. Grimes 		aitv = p->p_stats->p_timer[uap->which];
464df8bae1dSRodney W. Grimes 	splx(s);
465df8bae1dSRodney W. Grimes 	return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
466df8bae1dSRodney W. Grimes 	    sizeof (struct itimerval)));
467df8bae1dSRodney W. Grimes }
468df8bae1dSRodney W. Grimes 
469d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
470df8bae1dSRodney W. Grimes struct setitimer_args {
471df8bae1dSRodney W. Grimes 	u_int	which;
472df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
473df8bae1dSRodney W. Grimes };
474d2d3e875SBruce Evans #endif
475df8bae1dSRodney W. Grimes /* ARGSUSED */
47626f9a767SRodney W. Grimes int
477cb226aaaSPoul-Henning Kamp setitimer(p, uap)
478df8bae1dSRodney W. Grimes 	struct proc *p;
479df8bae1dSRodney W. Grimes 	register struct setitimer_args *uap;
480df8bae1dSRodney W. Grimes {
481df8bae1dSRodney W. Grimes 	struct itimerval aitv;
482227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
483df8bae1dSRodney W. Grimes 	register struct itimerval *itvp;
484df8bae1dSRodney W. Grimes 	int s, error;
485df8bae1dSRodney W. Grimes 
486df8bae1dSRodney W. Grimes 	if (uap->which > ITIMER_PROF)
487df8bae1dSRodney W. Grimes 		return (EINVAL);
488df8bae1dSRodney W. Grimes 	itvp = uap->itv;
489df8bae1dSRodney W. Grimes 	if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
490df8bae1dSRodney W. Grimes 	    sizeof(struct itimerval))))
491df8bae1dSRodney W. Grimes 		return (error);
4920808c591SBruce Evans 	if ((uap->itv = uap->oitv) &&
493cb226aaaSPoul-Henning Kamp 	    (error = getitimer(p, (struct getitimer_args *)uap)))
494df8bae1dSRodney W. Grimes 		return (error);
495df8bae1dSRodney W. Grimes 	if (itvp == 0)
496df8bae1dSRodney W. Grimes 		return (0);
497cd9f713dSAndrey A. Chernov 	if (itimerfix(&aitv.it_value))
498cd9f713dSAndrey A. Chernov 		return (EINVAL);
4994cf41af3SPoul-Henning Kamp 	if (!timevalisset(&aitv.it_value))
5004cf41af3SPoul-Henning Kamp 		timevalclear(&aitv.it_interval);
501cd9f713dSAndrey A. Chernov 	else if (itimerfix(&aitv.it_interval))
502df8bae1dSRodney W. Grimes 		return (EINVAL);
503227ee8a1SPoul-Henning Kamp 	s = splclock(); /* XXX: still needed ? */
504df8bae1dSRodney W. Grimes 	if (uap->which == ITIMER_REAL) {
5054cf41af3SPoul-Henning Kamp 		if (timevalisset(&p->p_realtimer.it_value))
506ab36c067SJustin T. Gibbs 			untimeout(realitexpire, (caddr_t)p, p->p_ithandle);
5074cf41af3SPoul-Henning Kamp 		if (timevalisset(&aitv.it_value))
508ab36c067SJustin T. Gibbs 			p->p_ithandle = timeout(realitexpire, (caddr_t)p,
50991ad39c6SPoul-Henning Kamp 						tvtohz(&aitv.it_value));
510c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
511bfe6c9faSPoul-Henning Kamp 		timevaladd(&aitv.it_value, &ctv);
512df8bae1dSRodney W. Grimes 		p->p_realtimer = aitv;
513df8bae1dSRodney W. Grimes 	} else
514df8bae1dSRodney W. Grimes 		p->p_stats->p_timer[uap->which] = aitv;
515df8bae1dSRodney W. Grimes 	splx(s);
516df8bae1dSRodney W. Grimes 	return (0);
517df8bae1dSRodney W. Grimes }
518df8bae1dSRodney W. Grimes 
519df8bae1dSRodney W. Grimes /*
520df8bae1dSRodney W. Grimes  * Real interval timer expired:
521df8bae1dSRodney W. Grimes  * send process whose timer expired an alarm signal.
522df8bae1dSRodney W. Grimes  * If time is not set up to reload, then just return.
523df8bae1dSRodney W. Grimes  * Else compute next time timer should go off which is > current time.
524df8bae1dSRodney W. Grimes  * This is where delay in processing this timeout causes multiple
525df8bae1dSRodney W. Grimes  * SIGALRM calls to be compressed into one.
526c8b47828SBruce Evans  * tvtohz() always adds 1 to allow for the time until the next clock
5279207f00aSBruce Evans  * interrupt being strictly less than 1 clock tick, but we don't want
5289207f00aSBruce Evans  * that here since we want to appear to be in sync with the clock
5299207f00aSBruce Evans  * interrupt even when we're delayed.
530df8bae1dSRodney W. Grimes  */
531df8bae1dSRodney W. Grimes void
532df8bae1dSRodney W. Grimes realitexpire(arg)
533df8bae1dSRodney W. Grimes 	void *arg;
534df8bae1dSRodney W. Grimes {
535df8bae1dSRodney W. Grimes 	register struct proc *p;
536bfe6c9faSPoul-Henning Kamp 	struct timeval ctv, ntv;
537df8bae1dSRodney W. Grimes 	int s;
538df8bae1dSRodney W. Grimes 
539df8bae1dSRodney W. Grimes 	p = (struct proc *)arg;
540df8bae1dSRodney W. Grimes 	psignal(p, SIGALRM);
5414cf41af3SPoul-Henning Kamp 	if (!timevalisset(&p->p_realtimer.it_interval)) {
5424cf41af3SPoul-Henning Kamp 		timevalclear(&p->p_realtimer.it_value);
543df8bae1dSRodney W. Grimes 		return;
544df8bae1dSRodney W. Grimes 	}
545df8bae1dSRodney W. Grimes 	for (;;) {
546227ee8a1SPoul-Henning Kamp 		s = splclock(); /* XXX: still neeeded ? */
547df8bae1dSRodney W. Grimes 		timevaladd(&p->p_realtimer.it_value,
548df8bae1dSRodney W. Grimes 		    &p->p_realtimer.it_interval);
549c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
5504cf41af3SPoul-Henning Kamp 		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
551bfe6c9faSPoul-Henning Kamp 			ntv = p->p_realtimer.it_value;
552bfe6c9faSPoul-Henning Kamp 			timevalsub(&ntv, &ctv);
553ee002b68SBruce Evans 			p->p_ithandle = timeout(realitexpire, (caddr_t)p,
554ee002b68SBruce Evans 			    tvtohz(&ntv) - 1);
555df8bae1dSRodney W. Grimes 			splx(s);
556df8bae1dSRodney W. Grimes 			return;
557df8bae1dSRodney W. Grimes 		}
558df8bae1dSRodney W. Grimes 		splx(s);
559df8bae1dSRodney W. Grimes 	}
560df8bae1dSRodney W. Grimes }
561df8bae1dSRodney W. Grimes 
562df8bae1dSRodney W. Grimes /*
563df8bae1dSRodney W. Grimes  * Check that a proposed value to load into the .it_value or
564df8bae1dSRodney W. Grimes  * .it_interval part of an interval timer is acceptable, and
565df8bae1dSRodney W. Grimes  * fix it to have at least minimal value (i.e. if it is less
566df8bae1dSRodney W. Grimes  * than the resolution of the clock, round it up.)
567df8bae1dSRodney W. Grimes  */
56826f9a767SRodney W. Grimes int
569df8bae1dSRodney W. Grimes itimerfix(tv)
570df8bae1dSRodney W. Grimes 	struct timeval *tv;
571df8bae1dSRodney W. Grimes {
572df8bae1dSRodney W. Grimes 
573df8bae1dSRodney W. Grimes 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
574df8bae1dSRodney W. Grimes 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
575df8bae1dSRodney W. Grimes 		return (EINVAL);
576df8bae1dSRodney W. Grimes 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
577df8bae1dSRodney W. Grimes 		tv->tv_usec = tick;
578df8bae1dSRodney W. Grimes 	return (0);
579df8bae1dSRodney W. Grimes }
580df8bae1dSRodney W. Grimes 
581df8bae1dSRodney W. Grimes /*
582df8bae1dSRodney W. Grimes  * Decrement an interval timer by a specified number
583df8bae1dSRodney W. Grimes  * of microseconds, which must be less than a second,
584df8bae1dSRodney W. Grimes  * i.e. < 1000000.  If the timer expires, then reload
585df8bae1dSRodney W. Grimes  * it.  In this case, carry over (usec - old value) to
586df8bae1dSRodney W. Grimes  * reduce the value reloaded into the timer so that
587df8bae1dSRodney W. Grimes  * the timer does not drift.  This routine assumes
588df8bae1dSRodney W. Grimes  * that it is called in a context where the timers
589df8bae1dSRodney W. Grimes  * on which it is operating cannot change in value.
590df8bae1dSRodney W. Grimes  */
59126f9a767SRodney W. Grimes int
592df8bae1dSRodney W. Grimes itimerdecr(itp, usec)
593df8bae1dSRodney W. Grimes 	register struct itimerval *itp;
594df8bae1dSRodney W. Grimes 	int usec;
595df8bae1dSRodney W. Grimes {
596df8bae1dSRodney W. Grimes 
597df8bae1dSRodney W. Grimes 	if (itp->it_value.tv_usec < usec) {
598df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_sec == 0) {
599df8bae1dSRodney W. Grimes 			/* expired, and already in next interval */
600df8bae1dSRodney W. Grimes 			usec -= itp->it_value.tv_usec;
601df8bae1dSRodney W. Grimes 			goto expire;
602df8bae1dSRodney W. Grimes 		}
603df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec += 1000000;
604df8bae1dSRodney W. Grimes 		itp->it_value.tv_sec--;
605df8bae1dSRodney W. Grimes 	}
606df8bae1dSRodney W. Grimes 	itp->it_value.tv_usec -= usec;
607df8bae1dSRodney W. Grimes 	usec = 0;
6084cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_value))
609df8bae1dSRodney W. Grimes 		return (1);
610df8bae1dSRodney W. Grimes 	/* expired, exactly at end of interval */
611df8bae1dSRodney W. Grimes expire:
6124cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_interval)) {
613df8bae1dSRodney W. Grimes 		itp->it_value = itp->it_interval;
614df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec -= usec;
615df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_usec < 0) {
616df8bae1dSRodney W. Grimes 			itp->it_value.tv_usec += 1000000;
617df8bae1dSRodney W. Grimes 			itp->it_value.tv_sec--;
618df8bae1dSRodney W. Grimes 		}
619df8bae1dSRodney W. Grimes 	} else
620df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
621df8bae1dSRodney W. Grimes 	return (0);
622df8bae1dSRodney W. Grimes }
623df8bae1dSRodney W. Grimes 
624df8bae1dSRodney W. Grimes /*
625df8bae1dSRodney W. Grimes  * Add and subtract routines for timevals.
626df8bae1dSRodney W. Grimes  * N.B.: subtract routine doesn't deal with
627df8bae1dSRodney W. Grimes  * results which are before the beginning,
628df8bae1dSRodney W. Grimes  * it just gets very confused in this case.
629df8bae1dSRodney W. Grimes  * Caveat emptor.
630df8bae1dSRodney W. Grimes  */
63126f9a767SRodney W. Grimes void
632df8bae1dSRodney W. Grimes timevaladd(t1, t2)
633df8bae1dSRodney W. Grimes 	struct timeval *t1, *t2;
634df8bae1dSRodney W. Grimes {
635df8bae1dSRodney W. Grimes 
636df8bae1dSRodney W. Grimes 	t1->tv_sec += t2->tv_sec;
637df8bae1dSRodney W. Grimes 	t1->tv_usec += t2->tv_usec;
638df8bae1dSRodney W. Grimes 	timevalfix(t1);
639df8bae1dSRodney W. Grimes }
640df8bae1dSRodney W. Grimes 
64126f9a767SRodney W. Grimes void
642df8bae1dSRodney W. Grimes timevalsub(t1, t2)
643df8bae1dSRodney W. Grimes 	struct timeval *t1, *t2;
644df8bae1dSRodney W. Grimes {
645df8bae1dSRodney W. Grimes 
646df8bae1dSRodney W. Grimes 	t1->tv_sec -= t2->tv_sec;
647df8bae1dSRodney W. Grimes 	t1->tv_usec -= t2->tv_usec;
648df8bae1dSRodney W. Grimes 	timevalfix(t1);
649df8bae1dSRodney W. Grimes }
650df8bae1dSRodney W. Grimes 
65187b6de2bSPoul-Henning Kamp static void
652df8bae1dSRodney W. Grimes timevalfix(t1)
653df8bae1dSRodney W. Grimes 	struct timeval *t1;
654df8bae1dSRodney W. Grimes {
655df8bae1dSRodney W. Grimes 
656df8bae1dSRodney W. Grimes 	if (t1->tv_usec < 0) {
657df8bae1dSRodney W. Grimes 		t1->tv_sec--;
658df8bae1dSRodney W. Grimes 		t1->tv_usec += 1000000;
659df8bae1dSRodney W. Grimes 	}
660df8bae1dSRodney W. Grimes 	if (t1->tv_usec >= 1000000) {
661df8bae1dSRodney W. Grimes 		t1->tv_sec++;
662df8bae1dSRodney W. Grimes 		t1->tv_usec -= 1000000;
663df8bae1dSRodney W. Grimes 	}
664df8bae1dSRodney W. Grimes }
665